2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
32 #define WIN32_NO_STATUS
42 /* list of memory ranges, used to store committed info */
56 struct object obj; /* object header */
57 mem_size_t size; /* mapping size */
58 int protect; /* protection flags */
59 struct fd *fd; /* fd for mapped file */
60 struct file *file; /* file mapped */
61 int header_size; /* size of headers (for PE image mapping) */
62 client_ptr_t base; /* default base addr (for PE image mapping) */
63 struct ranges *committed; /* list of committed ranges in this mapping */
64 struct file *shared_file; /* temp file for shared PE mapping */
65 struct list shared_entry; /* entry in global shared PE mappings list */
68 static void mapping_dump( struct object *obj, int verbose );
69 static struct object_type *mapping_get_type( struct object *obj );
70 static struct fd *mapping_get_fd( struct object *obj );
71 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
72 static void mapping_destroy( struct object *obj );
73 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
75 static const struct object_ops mapping_ops =
77 sizeof(struct mapping), /* size */
78 mapping_dump, /* dump */
79 mapping_get_type, /* get_type */
80 no_add_queue, /* add_queue */
81 NULL, /* remove_queue */
84 no_signal, /* signal */
85 mapping_get_fd, /* get_fd */
86 mapping_map_access, /* map_access */
87 default_get_sd, /* get_sd */
88 default_set_sd, /* set_sd */
89 no_lookup_name, /* lookup_name */
90 no_open_file, /* open_file */
91 fd_close_handle, /* close_handle */
92 mapping_destroy /* destroy */
95 static const struct fd_ops mapping_fd_ops =
97 default_fd_get_poll_events, /* get_poll_events */
98 default_poll_event, /* poll_event */
100 mapping_get_fd_type, /* get_fd_type */
101 no_fd_ioctl, /* ioctl */
102 no_fd_queue_async, /* queue_async */
103 default_fd_reselect_async, /* reselect_async */
104 default_fd_cancel_async /* cancel_async */
107 static struct list shared_list = LIST_INIT(shared_list);
111 /* These are always the same on an i386, and it will be faster this way */
112 # define page_mask 0xfff
113 # define page_shift 12
114 # define init_page_size() do { /* nothing */ } while(0)
118 static int page_shift, page_mask;
120 static void init_page_size(void)
123 # ifdef HAVE_GETPAGESIZE
124 page_size = getpagesize();
127 page_size = sysconf(_SC_PAGESIZE);
129 # error Cannot get the page size on this platform
132 page_mask = page_size - 1;
133 /* Make sure we have a power of 2 */
134 assert( !(page_size & page_mask) );
136 while ((1 << page_shift) != page_size) page_shift++;
138 #endif /* __i386__ */
140 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
143 /* extend a file beyond the current end of file */
144 static int grow_file( int unix_fd, file_pos_t new_size )
146 static const char zero;
147 off_t size = new_size;
149 if (sizeof(new_size) > sizeof(size) && size != new_size)
151 set_error( STATUS_INVALID_PARAMETER );
154 /* extend the file one byte beyond the requested size and then truncate it */
155 /* this should work around ftruncate implementations that can't extend files */
156 if (pwrite( unix_fd, &zero, 1, size ) != -1)
158 ftruncate( unix_fd, size );
165 /* find the shared PE mapping for a given mapping */
166 static struct file *get_shared_file( struct mapping *mapping )
170 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
171 if (is_same_file_fd( ptr->fd, mapping->fd ))
172 return (struct file *)grab_object( ptr->shared_file );
176 /* return the size of the memory mapping and file range of a given section */
177 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
178 off_t *file_start, size_t *file_size )
180 static const unsigned int sector_align = 0x1ff;
182 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
183 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
185 *file_start = sec->PointerToRawData & ~sector_align;
186 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
187 if (*file_size > *map_size) *file_size = *map_size;
190 /* add a range to the committed list */
191 static void add_committed_range( struct mapping *mapping, file_pos_t start, file_pos_t end )
194 struct range *ranges;
196 if (!mapping->committed) return; /* everything committed already */
198 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
200 if (ranges[i].start > end) break;
201 if (ranges[i].end < start) continue;
202 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
203 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
205 for (j = i + 1; j < mapping->committed->count; j++)
207 if (ranges[j].start > end) break;
208 if (ranges[j].end > end) end = ranges[j].end;
212 memmove( &ranges[i + 1], &ranges[j], (mapping->committed->count - j) * sizeof(*ranges) );
213 mapping->committed->count -= j - (i + 1);
220 /* now add a new range */
222 if (mapping->committed->count == mapping->committed->max)
224 unsigned int new_size = mapping->committed->max * 2;
225 struct ranges *new_ptr = realloc( mapping->committed, offsetof( struct ranges, ranges[new_size] ));
226 if (!new_ptr) return;
227 new_ptr->max = new_size;
228 ranges = new_ptr->ranges;
229 mapping->committed = new_ptr;
231 memmove( &ranges[i + 1], &ranges[i], (mapping->committed->count - i) * sizeof(*ranges) );
232 ranges[i].start = start;
234 mapping->committed->count++;
237 /* find the range containing start and return whether it's committed */
238 static int find_committed_range( struct mapping *mapping, file_pos_t start, mem_size_t *size )
241 struct range *ranges;
243 if (!mapping->committed) /* everything is committed */
245 *size = mapping->size - start;
248 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
250 if (ranges[i].start > start)
252 *size = ranges[i].start - start;
255 if (ranges[i].end > start)
257 *size = ranges[i].end - start;
261 *size = mapping->size - start;
265 /* allocate and fill the temp file for a shared PE image mapping */
266 static int build_shared_mapping( struct mapping *mapping, int fd,
267 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
270 mem_size_t total_size;
271 size_t file_size, map_size, max_size;
272 off_t shared_pos, read_pos, write_pos;
277 /* compute the total size of the shared mapping */
279 total_size = max_size = 0;
280 for (i = 0; i < nb_sec; i++)
282 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
283 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
285 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
286 if (file_size > max_size) max_size = file_size;
287 total_size += map_size;
290 if (!total_size) return 1; /* nothing to do */
292 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
294 /* create a temp file for the mapping */
296 if (!(mapping->shared_file = create_temp_file( FILE_GENERIC_READ|FILE_GENERIC_WRITE ))) return 0;
297 if ((shared_fd = get_file_unix_fd( mapping->shared_file )) == -1) goto error;
298 if (!grow_file( shared_fd, total_size )) goto error;
300 if (!(buffer = malloc( max_size ))) goto error;
302 /* copy the shared sections data into the temp file */
305 for (i = 0; i < nb_sec; i++)
307 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
308 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
309 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
310 write_pos = shared_pos;
311 shared_pos += map_size;
312 if (!sec[i].PointerToRawData || !file_size) continue;
316 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
317 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
322 if (res <= 0) goto error;
326 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
332 release_object( mapping->shared_file );
333 mapping->shared_file = NULL;
338 /* retrieve the mapping parameters for an executable (PE) image */
339 static int get_image_params( struct mapping *mapping, int unix_fd )
341 IMAGE_DOS_HEADER dos;
342 IMAGE_SECTION_HEADER *sec = NULL;
346 IMAGE_FILE_HEADER FileHeader;
349 IMAGE_OPTIONAL_HEADER32 hdr32;
350 IMAGE_OPTIONAL_HEADER64 hdr64;
356 /* load the headers */
358 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) goto error;
359 if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
362 size = pread( unix_fd, &nt, sizeof(nt), pos );
363 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) goto error;
364 /* zero out Optional header in the case it's not present or partial */
365 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
366 if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
368 switch (nt.opt.hdr32.Magic)
370 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
371 mapping->size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
372 mapping->base = nt.opt.hdr32.ImageBase;
373 mapping->header_size = nt.opt.hdr32.SizeOfHeaders;
375 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
376 mapping->size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
377 mapping->base = nt.opt.hdr64.ImageBase;
378 mapping->header_size = nt.opt.hdr64.SizeOfHeaders;
384 /* load the section headers */
386 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
387 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
388 if (pos + size > mapping->size) goto error;
389 if (pos + size > mapping->header_size) mapping->header_size = pos + size;
390 if (!(sec = malloc( size ))) goto error;
391 if (pread( unix_fd, sec, size, pos ) != size) goto error;
393 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
395 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
397 mapping->protect = VPROT_IMAGE;
403 set_error( STATUS_INVALID_FILE_FOR_SECTION );
407 static struct object *create_mapping( struct directory *root, const struct unicode_str *name,
408 unsigned int attr, mem_size_t size, int protect,
409 obj_handle_t handle, const struct security_descriptor *sd )
411 struct mapping *mapping;
418 if (!page_mask) init_page_size();
420 if (!(mapping = create_named_object_dir( root, name, attr, &mapping_ops )))
422 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
423 return &mapping->obj; /* Nothing else to do */
425 if (sd) default_set_sd( &mapping->obj, sd, OWNER_SECURITY_INFORMATION|
426 GROUP_SECURITY_INFORMATION|
427 DACL_SECURITY_INFORMATION|
428 SACL_SECURITY_INFORMATION );
429 mapping->header_size = 0;
432 mapping->file = NULL;
433 mapping->shared_file = NULL;
434 mapping->committed = NULL;
436 if (protect & VPROT_READ) access |= FILE_READ_DATA;
437 if (protect & VPROT_WRITE) access |= FILE_WRITE_DATA;
441 if (!(protect & VPROT_COMMITTED))
443 set_error( STATUS_INVALID_PARAMETER );
446 if (!(file = get_file_obj( current->process, handle, access ))) goto error;
447 fd = get_obj_fd( (struct object *)file );
448 mapping->fd = dup_fd_object( fd );
449 release_object( file );
450 release_object( fd );
451 if (!mapping->fd) goto error;
453 set_fd_user( mapping->fd, &mapping_fd_ops, &mapping->obj );
454 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
455 if (protect & VPROT_IMAGE)
457 if (!get_image_params( mapping, unix_fd )) goto error;
458 return &mapping->obj;
460 if (fstat( unix_fd, &st ) == -1)
467 if (!(size = st.st_size))
469 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
473 else if (st.st_size < size && !grow_file( unix_fd, size )) goto error;
475 else /* Anonymous mapping (no associated file) */
477 if (!size || (protect & VPROT_IMAGE))
479 set_error( STATUS_INVALID_PARAMETER );
482 if (!(protect & VPROT_COMMITTED))
484 if (!(mapping->committed = mem_alloc( offsetof(struct ranges, ranges[8]) ))) goto error;
485 mapping->committed->count = 0;
486 mapping->committed->max = 8;
488 if (!(mapping->file = create_temp_file( access ))) goto error;
489 mapping->fd = get_obj_fd( (struct object *)mapping->file );
490 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
491 if (!grow_file( unix_fd, size )) goto error;
493 mapping->size = (size + page_mask) & ~((mem_size_t)page_mask);
494 mapping->protect = protect;
495 return &mapping->obj;
498 release_object( mapping );
502 static void mapping_dump( struct object *obj, int verbose )
504 struct mapping *mapping = (struct mapping *)obj;
505 assert( obj->ops == &mapping_ops );
506 fprintf( stderr, "Mapping size=%08x%08x prot=%08x fd=%p header_size=%08x base=%08lx "
508 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
509 mapping->protect, mapping->fd, mapping->header_size,
510 (unsigned long)mapping->base, mapping->shared_file );
511 dump_object_name( &mapping->obj );
512 fputc( '\n', stderr );
515 static struct object_type *mapping_get_type( struct object *obj )
517 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
518 static const struct unicode_str str = { name, sizeof(name) };
519 return get_object_type( &str );
522 static struct fd *mapping_get_fd( struct object *obj )
524 struct mapping *mapping = (struct mapping *)obj;
525 return (struct fd *)grab_object( mapping->fd );
528 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
530 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
531 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
532 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
533 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
534 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
537 static void mapping_destroy( struct object *obj )
539 struct mapping *mapping = (struct mapping *)obj;
540 assert( obj->ops == &mapping_ops );
541 if (mapping->file) release_object( mapping->file );
542 if (mapping->fd) release_object( mapping->fd );
543 if (mapping->shared_file)
545 release_object( mapping->shared_file );
546 list_remove( &mapping->shared_entry );
548 free( mapping->committed );
551 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
556 int get_page_size(void)
558 if (!page_mask) init_page_size();
559 return page_mask + 1;
562 /* create a file mapping */
563 DECL_HANDLER(create_mapping)
566 struct unicode_str name;
567 struct directory *root = NULL;
568 const struct object_attributes *objattr = get_req_data();
569 const struct security_descriptor *sd;
573 if (!objattr_is_valid( objattr, get_req_data_size() ))
576 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
577 objattr_get_name( objattr, &name );
579 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
582 if ((obj = create_mapping( root, &name, req->attributes, req->size, req->protect, req->file_handle, sd )))
584 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
585 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
587 reply->handle = alloc_handle_no_access_check( current->process, obj, req->access, req->attributes );
588 release_object( obj );
591 if (root) release_object( root );
594 /* open a handle to a mapping */
595 DECL_HANDLER(open_mapping)
597 struct unicode_str name;
598 struct directory *root = NULL;
599 struct mapping *mapping;
601 get_req_unicode_str( &name );
602 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
605 if ((mapping = open_object_dir( root, &name, req->attributes, &mapping_ops )))
607 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, req->attributes );
608 release_object( mapping );
611 if (root) release_object( root );
614 /* get a mapping information */
615 DECL_HANDLER(get_mapping_info)
617 struct mapping *mapping;
620 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
621 req->access, &mapping_ops )))
623 reply->size = mapping->size;
624 reply->protect = mapping->protect;
625 reply->header_size = mapping->header_size;
626 reply->base = mapping->base;
627 reply->shared_file = 0;
628 if ((fd = get_obj_fd( &mapping->obj )))
630 if (!is_fd_removable(fd))
631 reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
632 release_object( fd );
634 if (mapping->shared_file)
636 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
637 GENERIC_READ|GENERIC_WRITE, 0 )))
639 if (reply->mapping) close_handle( current->process, reply->mapping );
642 release_object( mapping );
646 /* get a range of committed pages in a file mapping */
647 DECL_HANDLER(get_mapping_committed_range)
649 struct mapping *mapping;
651 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle, 0, &mapping_ops )))
653 if (!(req->offset & page_mask) && req->offset < mapping->size)
654 reply->committed = find_committed_range( mapping, req->offset, &reply->size );
656 set_error( STATUS_INVALID_PARAMETER );
658 release_object( mapping );
662 /* add a range to the committed pages in a file mapping */
663 DECL_HANDLER(add_mapping_committed_range)
665 struct mapping *mapping;
667 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle, 0, &mapping_ops )))
669 if (!(req->size & page_mask) &&
670 !(req->offset & page_mask) &&
671 req->offset < mapping->size &&
673 req->size <= mapping->size - req->offset)
674 add_committed_range( mapping, req->offset, req->offset + req->size );
676 set_error( STATUS_INVALID_PARAMETER );
678 release_object( mapping );