include: Fix definition of DRIVER_EXTENSION.
[wine.git] / server / mapping.c
blobf82907ba9830645dbd1c74159fcd16920f1bd417
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
31 #endif
32 #include <unistd.h>
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "windef.h"
37 #include "winternl.h"
39 #include "file.h"
40 #include "handle.h"
41 #include "thread.h"
42 #include "process.h"
43 #include "request.h"
44 #include "security.h"
46 /* list of memory ranges, used to store committed info */
47 struct ranges
49 unsigned int count;
50 unsigned int max;
51 struct range
53 file_pos_t start;
54 file_pos_t end;
55 } ranges[1];
58 struct mapping
60 struct object obj; /* object header */
61 mem_size_t size; /* mapping size */
62 unsigned int flags; /* SEC_* flags */
63 int protect; /* protection flags */
64 struct fd *fd; /* fd for mapped file */
65 enum cpu_type cpu; /* client CPU (for PE image mapping) */
66 pe_image_info_t image; /* image info (for PE image mapping) */
67 struct ranges *committed; /* list of committed ranges in this mapping */
68 struct file *shared_file; /* temp file for shared PE mapping */
69 struct list shared_entry; /* entry in global shared PE mappings list */
72 static void mapping_dump( struct object *obj, int verbose );
73 static struct object_type *mapping_get_type( struct object *obj );
74 static struct fd *mapping_get_fd( struct object *obj );
75 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
76 static void mapping_destroy( struct object *obj );
77 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
79 static const struct object_ops mapping_ops =
81 sizeof(struct mapping), /* size */
82 mapping_dump, /* dump */
83 mapping_get_type, /* get_type */
84 no_add_queue, /* add_queue */
85 NULL, /* remove_queue */
86 NULL, /* signaled */
87 NULL, /* satisfied */
88 no_signal, /* signal */
89 mapping_get_fd, /* get_fd */
90 mapping_map_access, /* map_access */
91 default_get_sd, /* get_sd */
92 default_set_sd, /* set_sd */
93 no_lookup_name, /* lookup_name */
94 directory_link_name, /* link_name */
95 default_unlink_name, /* unlink_name */
96 no_open_file, /* open_file */
97 fd_close_handle, /* close_handle */
98 mapping_destroy /* destroy */
101 static const struct fd_ops mapping_fd_ops =
103 default_fd_get_poll_events, /* get_poll_events */
104 default_poll_event, /* poll_event */
105 mapping_get_fd_type, /* get_fd_type */
106 no_fd_read, /* read */
107 no_fd_write, /* write */
108 no_fd_flush, /* flush */
109 no_fd_ioctl, /* ioctl */
110 no_fd_queue_async, /* queue_async */
111 default_fd_reselect_async, /* reselect_async */
112 default_fd_cancel_async /* cancel_async */
115 static struct list shared_list = LIST_INIT(shared_list);
117 static size_t page_mask;
119 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
122 /* extend a file beyond the current end of file */
123 static int grow_file( int unix_fd, file_pos_t new_size )
125 static const char zero;
126 off_t size = new_size;
128 if (sizeof(new_size) > sizeof(size) && size != new_size)
130 set_error( STATUS_INVALID_PARAMETER );
131 return 0;
133 /* extend the file one byte beyond the requested size and then truncate it */
134 /* this should work around ftruncate implementations that can't extend files */
135 if (pwrite( unix_fd, &zero, 1, size ) != -1)
137 ftruncate( unix_fd, size );
138 return 1;
140 file_set_error();
141 return 0;
144 /* check if the current directory allows exec mappings */
145 static int check_current_dir_for_exec(void)
147 int fd;
148 char tmpfn[] = "anonmap.XXXXXX";
149 void *ret = MAP_FAILED;
151 fd = mkstemps( tmpfn, 0 );
152 if (fd == -1) return 0;
153 if (grow_file( fd, 1 ))
155 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
156 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
158 close( fd );
159 unlink( tmpfn );
160 return (ret != MAP_FAILED);
163 /* create a temp file for anonymous mappings */
164 static int create_temp_file( file_pos_t size )
166 static int temp_dir_fd = -1;
167 char tmpfn[] = "anonmap.XXXXXX";
168 int fd;
170 if (temp_dir_fd == -1)
172 temp_dir_fd = server_dir_fd;
173 if (!check_current_dir_for_exec())
175 /* the server dir is noexec, try the config dir instead */
176 fchdir( config_dir_fd );
177 if (check_current_dir_for_exec())
178 temp_dir_fd = config_dir_fd;
179 else /* neither works, fall back to server dir */
180 fchdir( server_dir_fd );
183 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
185 fd = mkstemps( tmpfn, 0 );
186 if (fd != -1)
188 if (!grow_file( fd, size ))
190 close( fd );
191 fd = -1;
193 unlink( tmpfn );
195 else file_set_error();
197 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
198 return fd;
201 /* find the shared PE mapping for a given mapping */
202 static struct file *get_shared_file( struct mapping *mapping )
204 struct mapping *ptr;
206 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
207 if (is_same_file_fd( ptr->fd, mapping->fd ))
208 return (struct file *)grab_object( ptr->shared_file );
209 return NULL;
212 /* return the size of the memory mapping and file range of a given section */
213 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
214 off_t *file_start, size_t *file_size )
216 static const unsigned int sector_align = 0x1ff;
218 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
219 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
221 *file_start = sec->PointerToRawData & ~sector_align;
222 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
223 if (*file_size > *map_size) *file_size = *map_size;
226 /* add a range to the committed list */
227 static void add_committed_range( struct mapping *mapping, file_pos_t start, file_pos_t end )
229 unsigned int i, j;
230 struct range *ranges;
232 if (!mapping->committed) return; /* everything committed already */
234 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
236 if (ranges[i].start > end) break;
237 if (ranges[i].end < start) continue;
238 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
239 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
241 for (j = i + 1; j < mapping->committed->count; j++)
243 if (ranges[j].start > end) break;
244 if (ranges[j].end > end) end = ranges[j].end;
246 if (j > i + 1)
248 memmove( &ranges[i + 1], &ranges[j], (mapping->committed->count - j) * sizeof(*ranges) );
249 mapping->committed->count -= j - (i + 1);
251 ranges[i].end = end;
253 return;
256 /* now add a new range */
258 if (mapping->committed->count == mapping->committed->max)
260 unsigned int new_size = mapping->committed->max * 2;
261 struct ranges *new_ptr = realloc( mapping->committed, offsetof( struct ranges, ranges[new_size] ));
262 if (!new_ptr) return;
263 new_ptr->max = new_size;
264 ranges = new_ptr->ranges;
265 mapping->committed = new_ptr;
267 memmove( &ranges[i + 1], &ranges[i], (mapping->committed->count - i) * sizeof(*ranges) );
268 ranges[i].start = start;
269 ranges[i].end = end;
270 mapping->committed->count++;
273 /* find the range containing start and return whether it's committed */
274 static int find_committed_range( struct mapping *mapping, file_pos_t start, mem_size_t *size )
276 unsigned int i;
277 struct range *ranges;
279 if (!mapping->committed) /* everything is committed */
281 *size = mapping->size - start;
282 return 1;
284 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
286 if (ranges[i].start > start)
288 *size = ranges[i].start - start;
289 return 0;
291 if (ranges[i].end > start)
293 *size = ranges[i].end - start;
294 return 1;
297 *size = mapping->size - start;
298 return 0;
301 /* allocate and fill the temp file for a shared PE image mapping */
302 static int build_shared_mapping( struct mapping *mapping, int fd,
303 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
305 unsigned int i;
306 mem_size_t total_size;
307 size_t file_size, map_size, max_size;
308 off_t shared_pos, read_pos, write_pos;
309 char *buffer = NULL;
310 int shared_fd;
311 long toread;
313 /* compute the total size of the shared mapping */
315 total_size = max_size = 0;
316 for (i = 0; i < nb_sec; i++)
318 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
319 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
321 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
322 if (file_size > max_size) max_size = file_size;
323 total_size += map_size;
326 if (!total_size) return 1; /* nothing to do */
328 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
330 /* create a temp file for the mapping */
332 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
333 if (!(mapping->shared_file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 )))
334 return 0;
336 if (!(buffer = malloc( max_size ))) goto error;
338 /* copy the shared sections data into the temp file */
340 shared_pos = 0;
341 for (i = 0; i < nb_sec; i++)
343 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
344 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
345 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
346 write_pos = shared_pos;
347 shared_pos += map_size;
348 if (!sec[i].PointerToRawData || !file_size) continue;
349 toread = file_size;
350 while (toread)
352 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
353 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
355 file_size -= toread;
356 break;
358 if (res <= 0) goto error;
359 toread -= res;
360 read_pos += res;
362 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
364 free( buffer );
365 return 1;
367 error:
368 release_object( mapping->shared_file );
369 mapping->shared_file = NULL;
370 free( buffer );
371 return 0;
374 /* retrieve the mapping parameters for an executable (PE) image */
375 static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
377 IMAGE_DOS_HEADER dos;
378 IMAGE_SECTION_HEADER *sec = NULL;
379 struct
381 DWORD Signature;
382 IMAGE_FILE_HEADER FileHeader;
383 union
385 IMAGE_OPTIONAL_HEADER32 hdr32;
386 IMAGE_OPTIONAL_HEADER64 hdr64;
387 } opt;
388 } nt;
389 off_t pos;
390 int size;
392 /* load the headers */
394 if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
395 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
396 if (dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
397 pos = dos.e_lfanew;
399 size = pread( unix_fd, &nt, sizeof(nt), pos );
400 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_FORMAT;
401 /* zero out Optional header in the case it's not present or partial */
402 size = min( size, sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader );
403 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
404 if (nt.Signature != IMAGE_NT_SIGNATURE)
406 if (*(WORD *)&nt.Signature == IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_NE_FORMAT;
407 return STATUS_INVALID_IMAGE_PROTECT;
410 mapping->cpu = current->process->cpu;
411 switch (mapping->cpu)
413 case CPU_x86:
414 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_I386) return STATUS_INVALID_IMAGE_FORMAT;
415 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
416 break;
417 case CPU_x86_64:
418 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) return STATUS_INVALID_IMAGE_FORMAT;
419 if (nt.opt.hdr64.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
420 break;
421 case CPU_POWERPC:
422 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_POWERPC) return STATUS_INVALID_IMAGE_FORMAT;
423 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
424 break;
425 case CPU_ARM:
426 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARM &&
427 nt.FileHeader.Machine != IMAGE_FILE_MACHINE_THUMB &&
428 nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARMNT) return STATUS_INVALID_IMAGE_FORMAT;
429 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
430 break;
431 case CPU_ARM64:
432 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARM64) return STATUS_INVALID_IMAGE_FORMAT;
433 if (nt.opt.hdr64.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
434 break;
435 default:
436 return STATUS_INVALID_IMAGE_FORMAT;
439 switch (nt.opt.hdr32.Magic)
441 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
442 mapping->image.base = nt.opt.hdr32.ImageBase;
443 mapping->image.entry_point = nt.opt.hdr32.ImageBase + nt.opt.hdr32.AddressOfEntryPoint;
444 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
445 mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
446 mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
447 mapping->image.subsystem = nt.opt.hdr32.Subsystem;
448 mapping->image.subsystem_low = nt.opt.hdr32.MinorSubsystemVersion;
449 mapping->image.subsystem_high = nt.opt.hdr32.MajorSubsystemVersion;
450 mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
451 mapping->image.loader_flags = nt.opt.hdr32.LoaderFlags;
452 mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
453 mapping->image.checksum = nt.opt.hdr32.CheckSum;
454 break;
455 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
456 mapping->image.base = nt.opt.hdr64.ImageBase;
457 mapping->image.entry_point = nt.opt.hdr64.ImageBase + nt.opt.hdr64.AddressOfEntryPoint;
458 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
459 mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
460 mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
461 mapping->image.subsystem = nt.opt.hdr64.Subsystem;
462 mapping->image.subsystem_low = nt.opt.hdr64.MinorSubsystemVersion;
463 mapping->image.subsystem_high = nt.opt.hdr64.MajorSubsystemVersion;
464 mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
465 mapping->image.loader_flags = nt.opt.hdr64.LoaderFlags;
466 mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
467 mapping->image.checksum = nt.opt.hdr64.CheckSum;
468 break;
470 mapping->image.image_charact = nt.FileHeader.Characteristics;
471 mapping->image.machine = nt.FileHeader.Machine;
472 mapping->image.zerobits = 0; /* FIXME */
473 mapping->image.gp = 0; /* FIXME */
474 mapping->image.contains_code = 0; /* FIXME */
475 mapping->image.image_flags = 0; /* FIXME */
476 mapping->image.file_size = file_size;
478 /* load the section headers */
480 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
481 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
482 if (!mapping->size) mapping->size = mapping->image.map_size;
483 else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
484 if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
485 if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
486 if (!(sec = malloc( size ))) goto error;
487 if (pread( unix_fd, sec, size, pos ) != size) goto error;
489 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
491 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
493 free( sec );
494 return 0;
496 error:
497 free( sec );
498 return STATUS_INVALID_FILE_FOR_SECTION;
501 static struct object *create_mapping( struct object *root, const struct unicode_str *name,
502 unsigned int attr, mem_size_t size, unsigned int flags, int protect,
503 obj_handle_t handle, const struct security_descriptor *sd )
505 struct mapping *mapping;
506 struct file *file;
507 struct fd *fd;
508 int access = 0;
509 int unix_fd;
510 struct stat st;
512 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
514 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
515 return NULL;
516 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
517 return &mapping->obj; /* Nothing else to do */
519 mapping->size = size;
520 mapping->flags = flags & (SEC_IMAGE | SEC_NOCACHE | SEC_WRITECOMBINE | SEC_LARGE_PAGES);
521 mapping->protect = protect;
522 mapping->fd = NULL;
523 mapping->shared_file = NULL;
524 mapping->committed = NULL;
526 if (protect & VPROT_READ) access |= FILE_READ_DATA;
527 if (protect & VPROT_WRITE) access |= FILE_WRITE_DATA;
529 if (handle)
531 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
532 unsigned int mapping_access = FILE_MAPPING_ACCESS;
534 if (flags & SEC_RESERVE)
536 set_error( STATUS_INVALID_PARAMETER );
537 goto error;
539 if (!(file = get_file_obj( current->process, handle, access ))) goto error;
540 fd = get_obj_fd( (struct object *)file );
542 /* file sharing rules for mappings are different so we use magic the access rights */
543 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
544 else if (protect & VPROT_WRITE) mapping_access |= FILE_MAPPING_WRITE;
546 mapping->flags |= SEC_FILE;
547 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
549 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
550 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
552 release_object( file );
553 release_object( fd );
554 if (!mapping->fd) goto error;
556 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
557 if (fstat( unix_fd, &st ) == -1)
559 file_set_error();
560 goto error;
562 if (flags & SEC_IMAGE)
564 unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
565 if (!err) return &mapping->obj;
566 set_error( err );
567 goto error;
569 if (!mapping->size)
571 if (!(mapping->size = st.st_size))
573 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
574 goto error;
577 else if (st.st_size < mapping->size)
579 if (!(access & FILE_WRITE_DATA))
581 set_error( STATUS_SECTION_TOO_BIG );
582 goto error;
584 if (!grow_file( unix_fd, mapping->size )) goto error;
587 else /* Anonymous mapping (no associated file) */
589 if (!mapping->size || (flags & SEC_IMAGE))
591 set_error( STATUS_INVALID_PARAMETER );
592 goto error;
594 mapping->flags |= flags & (SEC_COMMIT | SEC_RESERVE);
595 if (flags & SEC_RESERVE)
597 if (!(mapping->committed = mem_alloc( offsetof(struct ranges, ranges[8]) ))) goto error;
598 mapping->committed->count = 0;
599 mapping->committed->max = 8;
601 mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
602 if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
603 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
604 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
605 allow_fd_caching( mapping->fd );
607 return &mapping->obj;
609 error:
610 release_object( mapping );
611 return NULL;
614 struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
616 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
619 /* open a new file handle to the file backing the mapping */
620 obj_handle_t open_mapping_file( struct process *process, struct mapping *mapping,
621 unsigned int access, unsigned int sharing )
623 obj_handle_t handle;
624 struct file *file = create_file_for_fd_obj( mapping->fd, access, sharing );
626 if (!file) return 0;
627 handle = alloc_handle( process, file, access, 0 );
628 release_object( file );
629 return handle;
632 struct mapping *grab_mapping_unless_removable( struct mapping *mapping )
634 if (is_fd_removable( mapping->fd )) return NULL;
635 return (struct mapping *)grab_object( mapping );
638 static void mapping_dump( struct object *obj, int verbose )
640 struct mapping *mapping = (struct mapping *)obj;
641 assert( obj->ops == &mapping_ops );
642 fprintf( stderr, "Mapping size=%08x%08x flags=%08x prot=%08x fd=%p shared_file=%p\n",
643 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
644 mapping->flags, mapping->protect, mapping->fd, mapping->shared_file );
647 static struct object_type *mapping_get_type( struct object *obj )
649 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
650 static const struct unicode_str str = { name, sizeof(name) };
651 return get_object_type( &str );
654 static struct fd *mapping_get_fd( struct object *obj )
656 struct mapping *mapping = (struct mapping *)obj;
657 return (struct fd *)grab_object( mapping->fd );
660 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
662 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
663 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
664 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
665 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
666 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
669 static void mapping_destroy( struct object *obj )
671 struct mapping *mapping = (struct mapping *)obj;
672 assert( obj->ops == &mapping_ops );
673 if (mapping->fd) release_object( mapping->fd );
674 if (mapping->shared_file)
676 release_object( mapping->shared_file );
677 list_remove( &mapping->shared_entry );
679 free( mapping->committed );
682 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
684 return FD_TYPE_FILE;
687 int get_page_size(void)
689 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
690 return page_mask + 1;
693 /* create a file mapping */
694 DECL_HANDLER(create_mapping)
696 struct object *root, *obj;
697 struct unicode_str name;
698 const struct security_descriptor *sd;
699 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
701 if (!objattr) return;
703 if ((obj = create_mapping( root, &name, objattr->attributes,
704 req->size, req->flags, req->protect, req->file_handle, sd )))
706 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
707 reply->handle = alloc_handle( current->process, obj, req->access, objattr->attributes );
708 else
709 reply->handle = alloc_handle_no_access_check( current->process, obj,
710 req->access, objattr->attributes );
711 release_object( obj );
714 if (root) release_object( root );
717 /* open a handle to a mapping */
718 DECL_HANDLER(open_mapping)
720 struct unicode_str name = get_req_unicode_str();
722 reply->handle = open_object( current->process, req->rootdir, req->access,
723 &mapping_ops, &name, req->attributes );
726 /* get a mapping information */
727 DECL_HANDLER(get_mapping_info)
729 struct mapping *mapping;
730 struct fd *fd;
732 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
734 reply->size = mapping->size;
735 reply->flags = mapping->flags;
736 reply->protect = mapping->protect;
738 if (mapping->flags & SEC_IMAGE)
739 set_reply_data( &mapping->image, min( sizeof(mapping->image), get_reply_max_size() ));
741 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
743 release_object( mapping );
744 return;
747 if ((mapping->flags & SEC_IMAGE) && mapping->cpu != current->process->cpu)
749 set_error( STATUS_INVALID_IMAGE_FORMAT );
750 release_object( mapping );
751 return;
754 if ((fd = get_obj_fd( &mapping->obj )))
756 if (!is_fd_removable(fd)) reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
757 release_object( fd );
759 if (mapping->shared_file)
761 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
762 GENERIC_READ|GENERIC_WRITE, 0 )))
764 if (reply->mapping) close_handle( current->process, reply->mapping );
767 release_object( mapping );
770 /* get a range of committed pages in a file mapping */
771 DECL_HANDLER(get_mapping_committed_range)
773 struct mapping *mapping;
775 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
777 if (!(req->offset & page_mask) && req->offset < mapping->size)
778 reply->committed = find_committed_range( mapping, req->offset, &reply->size );
779 else
780 set_error( STATUS_INVALID_PARAMETER );
782 release_object( mapping );
786 /* add a range to the committed pages in a file mapping */
787 DECL_HANDLER(add_mapping_committed_range)
789 struct mapping *mapping;
791 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
793 if (!(req->size & page_mask) &&
794 !(req->offset & page_mask) &&
795 req->offset < mapping->size &&
796 req->size > 0 &&
797 req->size <= mapping->size - req->offset)
798 add_committed_range( mapping, req->offset, req->offset + req->size );
799 else
800 set_error( STATUS_INVALID_PARAMETER );
802 release_object( mapping );