2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
22 struct object obj; /* object header */
23 int size_high; /* mapping size */
24 int size_low; /* mapping size */
25 int protect; /* protection flags */
26 struct file *file; /* file mapped */
27 int header_size; /* size of headers (for PE image mapping) */
28 void *base; /* default base addr (for PE image mapping) */
29 struct file *shared_file; /* temp file for shared PE mapping */
30 int shared_size; /* shared mapping total size */
33 static void mapping_dump( struct object *obj, int verbose );
34 static void mapping_destroy( struct object *obj );
36 static const struct object_ops mapping_ops =
38 sizeof(struct mapping), /* size */
39 mapping_dump, /* dump */
40 no_add_queue, /* add_queue */
41 NULL, /* remove_queue */
44 NULL, /* get_poll_events */
45 NULL, /* poll_event */
46 no_get_fd, /* get_fd */
48 no_get_file_info, /* get_file_info */
49 mapping_destroy /* destroy */
54 /* These are always the same on an i386, and it will be faster this way */
55 # define page_mask 0xfff
56 # define page_shift 12
57 # define init_page_size() do { /* nothing */ } while(0)
61 static int page_shift, page_mask;
63 static void init_page_size(void)
66 # ifdef HAVE_GETPAGESIZE
67 page_size = getpagesize();
70 page_size = sysconf(_SC_PAGESIZE);
72 # error Cannot get the page size on this platform
75 page_mask = page_size - 1;
76 /* Make sure we have a power of 2 */
77 assert( !(page_size & page_mask) );
79 while ((1 << page_shift) != page_size) page_shift++;
83 #define ROUND_ADDR(addr) \
84 ((int)(addr) & ~page_mask)
86 #define ROUND_SIZE(addr,size) \
87 (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
90 /* allocate and fill the temp file for a shared PE image mapping */
91 static int build_shared_mapping( struct mapping *mapping, int fd,
92 IMAGE_SECTION_HEADER *sec, int nb_sec )
94 int i, max_size, total_size, pos;
99 /* compute the total size of the shared mapping */
101 total_size = max_size = 0;
102 for (i = 0; i < nb_sec; i++)
104 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
105 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
107 int size = ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
108 if (size > max_size) max_size = size;
112 if (!(mapping->shared_size = total_size)) return 1; /* nothing to do */
114 /* create a temp file for the mapping */
116 if (!(mapping->shared_file = create_temp_file( GENERIC_READ|GENERIC_WRITE ))) goto error;
117 if (!grow_file( mapping->shared_file, 0, total_size )) goto error;
118 if ((shared_fd = file_get_mmap_fd( mapping->shared_file )) == -1) goto error;
120 if (!(buffer = malloc( max_size ))) goto error;
122 /* copy the shared sections data into the temp file */
124 for (i = pos = 0; i < nb_sec; i++)
126 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
127 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
128 if (lseek( shared_fd, pos, SEEK_SET ) != pos) goto error;
129 pos += ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
130 if (sec[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) continue;
131 if (!sec[i].PointerToRawData || !sec[i].SizeOfRawData) continue;
132 if (lseek( fd, sec[i].PointerToRawData, SEEK_SET ) != sec[i].PointerToRawData) goto error;
133 toread = sec[i].SizeOfRawData;
136 long res = read( fd, buffer + sec[i].SizeOfRawData - toread, toread );
137 if (res <= 0) goto error;
140 if (write( shared_fd, buffer, sec[i].SizeOfRawData ) != sec[i].SizeOfRawData) goto error;
147 if (shared_fd != -1) close( shared_fd );
148 if (buffer) free( buffer );
152 /* retrieve the mapping parameters for an executable (PE) image */
153 static int get_image_params( struct mapping *mapping )
155 IMAGE_DOS_HEADER dos;
157 IMAGE_SECTION_HEADER *sec = NULL;
158 int fd, filepos, size;
160 /* load the headers */
162 if ((fd = file_get_mmap_fd( mapping->file )) == -1) return 0;
163 filepos = lseek( fd, 0, SEEK_SET );
164 if (read( fd, &dos, sizeof(dos) ) != sizeof(dos)) goto error;
165 if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
166 if (lseek( fd, dos.e_lfanew, SEEK_SET ) == -1) goto error;
167 if (read( fd, &nt, sizeof(nt) ) != sizeof(nt)) goto error;
168 if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
170 /* load the section headers */
172 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
173 if (!(sec = malloc( size ))) goto error;
174 if (read( fd, sec, size ) != size) goto error;
176 if (!build_shared_mapping( mapping, fd, sec, nt.FileHeader.NumberOfSections )) goto error;
178 mapping->size_low = ROUND_SIZE( 0, nt.OptionalHeader.SizeOfImage );
179 mapping->size_high = 0;
180 mapping->base = (void *)nt.OptionalHeader.ImageBase;
181 mapping->header_size = ROUND_SIZE( mapping->base, nt.OptionalHeader.SizeOfHeaders );
182 mapping->protect = VPROT_IMAGE;
185 if (mapping->header_size > mapping->size_low) goto error;
187 lseek( fd, filepos, SEEK_SET );
193 lseek( fd, filepos, SEEK_SET );
195 if (sec) free( sec );
196 set_error( STATUS_INVALID_FILE_FOR_SECTION );
201 static struct object *create_mapping( int size_high, int size_low, int protect,
202 int handle, const WCHAR *name, size_t len )
204 struct mapping *mapping;
207 if (!page_mask) init_page_size();
209 if (!(mapping = create_named_object( &mapping_ops, name, len )))
211 if (get_error() == STATUS_OBJECT_NAME_COLLISION)
212 return &mapping->obj; /* Nothing else to do */
214 mapping->header_size = 0;
215 mapping->base = NULL;
216 mapping->shared_file = NULL;
217 mapping->shared_size = 0;
219 if (protect & VPROT_READ) access |= GENERIC_READ;
220 if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
224 if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
225 if (protect & VPROT_IMAGE)
227 if (!get_image_params( mapping )) goto error;
228 return &mapping->obj;
230 if (!size_high && !size_low)
232 struct get_file_info_request req;
233 struct object *obj = (struct object *)mapping->file;
234 obj->ops->get_file_info( obj, &req );
235 size_high = req.size_high;
236 size_low = ROUND_SIZE( 0, req.size_low );
238 else if (!grow_file( mapping->file, size_high, size_low )) goto error;
240 else /* Anonymous mapping (no associated file) */
242 if ((!size_high && !size_low) || (protect & VPROT_IMAGE))
244 set_error( STATUS_INVALID_PARAMETER );
245 mapping->file = NULL;
248 if (!(mapping->file = create_temp_file( access ))) goto error;
249 if (!grow_file( mapping->file, size_high, size_low )) goto error;
251 mapping->size_high = size_high;
252 mapping->size_low = ROUND_SIZE( 0, size_low );
253 mapping->protect = protect;
254 return &mapping->obj;
257 release_object( mapping );
261 static void mapping_dump( struct object *obj, int verbose )
263 struct mapping *mapping = (struct mapping *)obj;
264 assert( obj->ops == &mapping_ops );
265 fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
266 "shared_file=%p shared_size=%08x ",
267 mapping->size_high, mapping->size_low, mapping->protect, mapping->file,
268 mapping->header_size, mapping->base, mapping->shared_file, mapping->shared_size );
269 dump_object_name( &mapping->obj );
270 fputc( '\n', stderr );
273 static void mapping_destroy( struct object *obj )
275 struct mapping *mapping = (struct mapping *)obj;
276 assert( obj->ops == &mapping_ops );
277 if (mapping->file) release_object( mapping->file );
278 if (mapping->shared_file) release_object( mapping->shared_file );
281 int get_page_size(void)
283 if (!page_mask) init_page_size();
284 return page_mask + 1;
287 /* create a file mapping */
288 DECL_HANDLER(create_mapping)
293 if ((obj = create_mapping( req->size_high, req->size_low,
294 req->protect, req->file_handle,
295 get_req_data(req), get_req_data_size(req) )))
297 int access = FILE_MAP_ALL_ACCESS;
298 if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
299 req->handle = alloc_handle( current->process, obj, access, req->inherit );
300 release_object( obj );
304 /* open a handle to a mapping */
305 DECL_HANDLER(open_mapping)
307 req->handle = open_object( get_req_data(req), get_req_data_size(req),
308 &mapping_ops, req->access, req->inherit );
311 /* get a mapping information */
312 DECL_HANDLER(get_mapping_info)
314 struct mapping *mapping;
316 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
319 req->size_high = mapping->size_high;
320 req->size_low = mapping->size_low;
321 req->protect = mapping->protect;
322 req->header_size = mapping->header_size;
323 req->base = mapping->base;
324 req->shared_file = -1;
325 req->shared_size = mapping->shared_size;
326 if (mapping->shared_file)
327 req->shared_file = alloc_handle( current->process, mapping->shared_file,
328 GENERIC_READ|GENERIC_WRITE, 0 );
329 if (mapping->file) set_reply_fd( current, file_get_mmap_fd( mapping->file ) );
330 release_object( mapping );