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"
29 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
35 #define WIN32_NO_STATUS
46 /* list of memory ranges, used to store committed info */
49 struct object obj
; /* object header */
50 unsigned int count
; /* number of used ranges */
51 unsigned int max
; /* number of allocated ranges */
59 static void ranges_dump( struct object
*obj
, int verbose
);
60 static void ranges_destroy( struct object
*obj
);
62 static const struct object_ops ranges_ops
=
64 sizeof(struct ranges
), /* size */
65 ranges_dump
, /* dump */
66 no_get_type
, /* get_type */
67 no_add_queue
, /* add_queue */
68 NULL
, /* remove_queue */
71 no_signal
, /* signal */
72 no_get_fd
, /* get_fd */
73 no_map_access
, /* map_access */
74 default_get_sd
, /* get_sd */
75 default_set_sd
, /* set_sd */
76 no_lookup_name
, /* lookup_name */
77 no_link_name
, /* link_name */
78 NULL
, /* unlink_name */
79 no_open_file
, /* open_file */
80 no_close_handle
, /* close_handle */
81 ranges_destroy
/* destroy */
84 /* memory view mapped in client address space */
87 struct list entry
; /* entry in per-process view list */
88 struct fd
*fd
; /* fd for mapped file */
89 struct ranges
*committed
; /* list of committed ranges in this mapping */
90 unsigned int flags
; /* SEC_* flags */
91 client_ptr_t base
; /* view base address (in process addr space) */
92 mem_size_t size
; /* view size */
93 file_pos_t start
; /* start offset in mapping */
98 struct object obj
; /* object header */
99 mem_size_t size
; /* mapping size */
100 unsigned int flags
; /* SEC_* flags */
101 struct fd
*fd
; /* fd for mapped file */
102 enum cpu_type cpu
; /* client CPU (for PE image mapping) */
103 pe_image_info_t image
; /* image info (for PE image mapping) */
104 struct ranges
*committed
; /* list of committed ranges in this mapping */
105 struct file
*shared_file
; /* temp file for shared PE mapping */
106 struct list shared_entry
; /* entry in global shared PE mappings list */
109 static void mapping_dump( struct object
*obj
, int verbose
);
110 static struct object_type
*mapping_get_type( struct object
*obj
);
111 static struct fd
*mapping_get_fd( struct object
*obj
);
112 static unsigned int mapping_map_access( struct object
*obj
, unsigned int access
);
113 static void mapping_destroy( struct object
*obj
);
114 static enum server_fd_type
mapping_get_fd_type( struct fd
*fd
);
116 static const struct object_ops mapping_ops
=
118 sizeof(struct mapping
), /* size */
119 mapping_dump
, /* dump */
120 mapping_get_type
, /* get_type */
121 no_add_queue
, /* add_queue */
122 NULL
, /* remove_queue */
124 NULL
, /* satisfied */
125 no_signal
, /* signal */
126 mapping_get_fd
, /* get_fd */
127 mapping_map_access
, /* map_access */
128 default_get_sd
, /* get_sd */
129 default_set_sd
, /* set_sd */
130 no_lookup_name
, /* lookup_name */
131 directory_link_name
, /* link_name */
132 default_unlink_name
, /* unlink_name */
133 no_open_file
, /* open_file */
134 fd_close_handle
, /* close_handle */
135 mapping_destroy
/* destroy */
138 static const struct fd_ops mapping_fd_ops
=
140 default_fd_get_poll_events
, /* get_poll_events */
141 default_poll_event
, /* poll_event */
142 mapping_get_fd_type
, /* get_fd_type */
143 no_fd_read
, /* read */
144 no_fd_write
, /* write */
145 no_fd_flush
, /* flush */
146 no_fd_ioctl
, /* ioctl */
147 no_fd_queue_async
, /* queue_async */
148 default_fd_reselect_async
/* reselect_async */
151 static struct list shared_list
= LIST_INIT(shared_list
);
153 static size_t page_mask
;
155 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
158 static void ranges_dump( struct object
*obj
, int verbose
)
160 struct ranges
*ranges
= (struct ranges
*)obj
;
161 fprintf( stderr
, "Memory ranges count=%u\n", ranges
->count
);
164 static void ranges_destroy( struct object
*obj
)
166 struct ranges
*ranges
= (struct ranges
*)obj
;
167 free( ranges
->ranges
);
170 /* extend a file beyond the current end of file */
171 static int grow_file( int unix_fd
, file_pos_t new_size
)
173 static const char zero
;
174 off_t size
= new_size
;
176 if (sizeof(new_size
) > sizeof(size
) && size
!= new_size
)
178 set_error( STATUS_INVALID_PARAMETER
);
181 /* extend the file one byte beyond the requested size and then truncate it */
182 /* this should work around ftruncate implementations that can't extend files */
183 if (pwrite( unix_fd
, &zero
, 1, size
) != -1)
185 ftruncate( unix_fd
, size
);
192 /* check if the current directory allows exec mappings */
193 static int check_current_dir_for_exec(void)
196 char tmpfn
[] = "anonmap.XXXXXX";
197 void *ret
= MAP_FAILED
;
199 fd
= mkstemps( tmpfn
, 0 );
200 if (fd
== -1) return 0;
201 if (grow_file( fd
, 1 ))
203 ret
= mmap( NULL
, get_page_size(), PROT_READ
| PROT_EXEC
, MAP_PRIVATE
, fd
, 0 );
204 if (ret
!= MAP_FAILED
) munmap( ret
, get_page_size() );
208 return (ret
!= MAP_FAILED
);
211 /* create a temp file for anonymous mappings */
212 static int create_temp_file( file_pos_t size
)
214 static int temp_dir_fd
= -1;
215 char tmpfn
[] = "anonmap.XXXXXX";
218 if (temp_dir_fd
== -1)
220 temp_dir_fd
= server_dir_fd
;
221 if (!check_current_dir_for_exec())
223 /* the server dir is noexec, try the config dir instead */
224 fchdir( config_dir_fd
);
225 if (check_current_dir_for_exec())
226 temp_dir_fd
= config_dir_fd
;
227 else /* neither works, fall back to server dir */
228 fchdir( server_dir_fd
);
231 else if (temp_dir_fd
!= server_dir_fd
) fchdir( temp_dir_fd
);
233 fd
= mkstemps( tmpfn
, 0 );
236 if (!grow_file( fd
, size
))
243 else file_set_error();
245 if (temp_dir_fd
!= server_dir_fd
) fchdir( server_dir_fd
);
249 /* find a memory view from its base address */
250 static struct memory_view
*find_mapped_view( struct process
*process
, client_ptr_t base
)
252 struct memory_view
*view
;
254 LIST_FOR_EACH_ENTRY( view
, &process
->views
, struct memory_view
, entry
)
255 if (view
->base
== base
) return view
;
257 set_error( STATUS_NOT_MAPPED_VIEW
);
261 static void free_memory_view( struct memory_view
*view
)
263 if (view
->fd
) release_object( view
->fd
);
264 if (view
->committed
) release_object( view
->committed
);
265 list_remove( &view
->entry
);
269 /* free all mapped views at process exit */
270 void free_mapped_views( struct process
*process
)
274 while ((ptr
= list_head( &process
->views
)))
275 free_memory_view( LIST_ENTRY( ptr
, struct memory_view
, entry
));
278 /* find the shared PE mapping for a given mapping */
279 static struct file
*get_shared_file( struct mapping
*mapping
)
283 LIST_FOR_EACH_ENTRY( ptr
, &shared_list
, struct mapping
, shared_entry
)
284 if (is_same_file_fd( ptr
->fd
, mapping
->fd
))
285 return (struct file
*)grab_object( ptr
->shared_file
);
289 /* return the size of the memory mapping and file range of a given section */
290 static inline void get_section_sizes( const IMAGE_SECTION_HEADER
*sec
, size_t *map_size
,
291 off_t
*file_start
, size_t *file_size
)
293 static const unsigned int sector_align
= 0x1ff;
295 if (!sec
->Misc
.VirtualSize
) *map_size
= ROUND_SIZE( sec
->SizeOfRawData
);
296 else *map_size
= ROUND_SIZE( sec
->Misc
.VirtualSize
);
298 *file_start
= sec
->PointerToRawData
& ~sector_align
;
299 *file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
300 if (*file_size
> *map_size
) *file_size
= *map_size
;
303 /* add a range to the committed list */
304 static void add_committed_range( struct memory_view
*view
, file_pos_t start
, file_pos_t end
)
307 struct ranges
*committed
= view
->committed
;
308 struct range
*ranges
;
310 if ((start
& page_mask
) || (end
& page_mask
) ||
311 start
>= view
->size
|| end
>= view
->size
||
314 set_error( STATUS_INVALID_PARAMETER
);
318 if (!committed
) return; /* everything committed already */
320 start
+= view
->start
;
323 for (i
= 0, ranges
= committed
->ranges
; i
< committed
->count
; i
++)
325 if (ranges
[i
].start
> end
) break;
326 if (ranges
[i
].end
< start
) continue;
327 if (ranges
[i
].start
> start
) ranges
[i
].start
= start
; /* extend downwards */
328 if (ranges
[i
].end
< end
) /* extend upwards and maybe merge with next */
330 for (j
= i
+ 1; j
< committed
->count
; j
++)
332 if (ranges
[j
].start
> end
) break;
333 if (ranges
[j
].end
> end
) end
= ranges
[j
].end
;
337 memmove( &ranges
[i
+ 1], &ranges
[j
], (committed
->count
- j
) * sizeof(*ranges
) );
338 committed
->count
-= j
- (i
+ 1);
345 /* now add a new range */
347 if (committed
->count
== committed
->max
)
349 unsigned int new_size
= committed
->max
* 2;
350 struct range
*new_ptr
= realloc( committed
->ranges
, new_size
* sizeof(*new_ptr
) );
351 if (!new_ptr
) return;
352 committed
->max
= new_size
;
353 committed
->ranges
= new_ptr
;
355 memmove( &ranges
[i
+ 1], &ranges
[i
], (committed
->count
- i
) * sizeof(*ranges
) );
356 ranges
[i
].start
= start
;
361 /* find the range containing start and return whether it's committed */
362 static int find_committed_range( struct memory_view
*view
, file_pos_t start
, mem_size_t
*size
)
365 struct ranges
*committed
= view
->committed
;
366 struct range
*ranges
;
368 if ((start
& page_mask
) || start
>= view
->size
)
370 set_error( STATUS_INVALID_PARAMETER
);
373 if (!committed
) /* everything is committed */
375 *size
= view
->size
- start
;
378 for (i
= 0, ranges
= committed
->ranges
; i
< committed
->count
; i
++)
380 if (ranges
[i
].start
> view
->start
+ start
)
382 *size
= min( ranges
[i
].start
, view
->start
+ view
->size
) - (view
->start
+ start
);
385 if (ranges
[i
].end
> view
->start
+ start
)
387 *size
= min( ranges
[i
].end
, view
->start
+ view
->size
) - (view
->start
+ start
);
391 *size
= view
->size
- start
;
395 /* allocate and fill the temp file for a shared PE image mapping */
396 static int build_shared_mapping( struct mapping
*mapping
, int fd
,
397 IMAGE_SECTION_HEADER
*sec
, unsigned int nb_sec
)
400 mem_size_t total_size
;
401 size_t file_size
, map_size
, max_size
;
402 off_t shared_pos
, read_pos
, write_pos
;
407 /* compute the total size of the shared mapping */
409 total_size
= max_size
= 0;
410 for (i
= 0; i
< nb_sec
; i
++)
412 if ((sec
[i
].Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
413 (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
))
415 get_section_sizes( &sec
[i
], &map_size
, &read_pos
, &file_size
);
416 if (file_size
> max_size
) max_size
= file_size
;
417 total_size
+= map_size
;
420 if (!total_size
) return 1; /* nothing to do */
422 if ((mapping
->shared_file
= get_shared_file( mapping
))) return 1;
424 /* create a temp file for the mapping */
426 if ((shared_fd
= create_temp_file( total_size
)) == -1) return 0;
427 if (!(mapping
->shared_file
= create_file_for_fd( shared_fd
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, 0 )))
430 if (!(buffer
= malloc( max_size
))) goto error
;
432 /* copy the shared sections data into the temp file */
435 for (i
= 0; i
< nb_sec
; i
++)
437 if (!(sec
[i
].Characteristics
& IMAGE_SCN_MEM_SHARED
)) continue;
438 if (!(sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
)) continue;
439 get_section_sizes( &sec
[i
], &map_size
, &read_pos
, &file_size
);
440 write_pos
= shared_pos
;
441 shared_pos
+= map_size
;
442 if (!sec
[i
].PointerToRawData
|| !file_size
) continue;
446 long res
= pread( fd
, buffer
+ file_size
- toread
, toread
, read_pos
);
447 if (!res
&& toread
< 0x200) /* partial sector at EOF is not an error */
452 if (res
<= 0) goto error
;
456 if (pwrite( shared_fd
, buffer
, file_size
, write_pos
) != file_size
) goto error
;
462 release_object( mapping
->shared_file
);
463 mapping
->shared_file
= NULL
;
468 /* retrieve the mapping parameters for an executable (PE) image */
469 static unsigned int get_image_params( struct mapping
*mapping
, file_pos_t file_size
, int unix_fd
)
471 IMAGE_DOS_HEADER dos
;
472 IMAGE_SECTION_HEADER
*sec
= NULL
;
476 IMAGE_FILE_HEADER FileHeader
;
479 IMAGE_OPTIONAL_HEADER32 hdr32
;
480 IMAGE_OPTIONAL_HEADER64 hdr64
;
486 /* load the headers */
488 if (!file_size
) return STATUS_INVALID_FILE_FOR_SECTION
;
489 if (pread( unix_fd
, &dos
, sizeof(dos
), 0 ) != sizeof(dos
)) return STATUS_INVALID_IMAGE_NOT_MZ
;
490 if (dos
.e_magic
!= IMAGE_DOS_SIGNATURE
) return STATUS_INVALID_IMAGE_NOT_MZ
;
493 size
= pread( unix_fd
, &nt
, sizeof(nt
), pos
);
494 if (size
< sizeof(nt
.Signature
) + sizeof(nt
.FileHeader
)) return STATUS_INVALID_IMAGE_FORMAT
;
495 /* zero out Optional header in the case it's not present or partial */
496 size
= min( size
, sizeof(nt
.Signature
) + sizeof(nt
.FileHeader
) + nt
.FileHeader
.SizeOfOptionalHeader
);
497 if (size
< sizeof(nt
)) memset( (char *)&nt
+ size
, 0, sizeof(nt
) - size
);
498 if (nt
.Signature
!= IMAGE_NT_SIGNATURE
)
500 if (*(WORD
*)&nt
.Signature
== IMAGE_OS2_SIGNATURE
) return STATUS_INVALID_IMAGE_NE_FORMAT
;
501 return STATUS_INVALID_IMAGE_PROTECT
;
504 mapping
->cpu
= current
->process
->cpu
;
505 switch (mapping
->cpu
)
508 if (nt
.FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
) return STATUS_INVALID_IMAGE_FORMAT
;
509 if (nt
.opt
.hdr32
.Magic
!= IMAGE_NT_OPTIONAL_HDR32_MAGIC
) return STATUS_INVALID_IMAGE_FORMAT
;
512 if (nt
.FileHeader
.Machine
!= IMAGE_FILE_MACHINE_AMD64
) return STATUS_INVALID_IMAGE_FORMAT
;
513 if (nt
.opt
.hdr64
.Magic
!= IMAGE_NT_OPTIONAL_HDR64_MAGIC
) return STATUS_INVALID_IMAGE_FORMAT
;
516 if (nt
.FileHeader
.Machine
!= IMAGE_FILE_MACHINE_POWERPC
) return STATUS_INVALID_IMAGE_FORMAT
;
517 if (nt
.opt
.hdr32
.Magic
!= IMAGE_NT_OPTIONAL_HDR32_MAGIC
) return STATUS_INVALID_IMAGE_FORMAT
;
520 if (nt
.FileHeader
.Machine
!= IMAGE_FILE_MACHINE_ARM
&&
521 nt
.FileHeader
.Machine
!= IMAGE_FILE_MACHINE_THUMB
&&
522 nt
.FileHeader
.Machine
!= IMAGE_FILE_MACHINE_ARMNT
) return STATUS_INVALID_IMAGE_FORMAT
;
523 if (nt
.opt
.hdr32
.Magic
!= IMAGE_NT_OPTIONAL_HDR32_MAGIC
) return STATUS_INVALID_IMAGE_FORMAT
;
526 if (nt
.FileHeader
.Machine
!= IMAGE_FILE_MACHINE_ARM64
) return STATUS_INVALID_IMAGE_FORMAT
;
527 if (nt
.opt
.hdr64
.Magic
!= IMAGE_NT_OPTIONAL_HDR64_MAGIC
) return STATUS_INVALID_IMAGE_FORMAT
;
530 return STATUS_INVALID_IMAGE_FORMAT
;
533 switch (nt
.opt
.hdr32
.Magic
)
535 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
536 mapping
->image
.base
= nt
.opt
.hdr32
.ImageBase
;
537 mapping
->image
.entry_point
= nt
.opt
.hdr32
.ImageBase
+ nt
.opt
.hdr32
.AddressOfEntryPoint
;
538 mapping
->image
.map_size
= ROUND_SIZE( nt
.opt
.hdr32
.SizeOfImage
);
539 mapping
->image
.stack_size
= nt
.opt
.hdr32
.SizeOfStackReserve
;
540 mapping
->image
.stack_commit
= nt
.opt
.hdr32
.SizeOfStackCommit
;
541 mapping
->image
.subsystem
= nt
.opt
.hdr32
.Subsystem
;
542 mapping
->image
.subsystem_low
= nt
.opt
.hdr32
.MinorSubsystemVersion
;
543 mapping
->image
.subsystem_high
= nt
.opt
.hdr32
.MajorSubsystemVersion
;
544 mapping
->image
.dll_charact
= nt
.opt
.hdr32
.DllCharacteristics
;
545 mapping
->image
.loader_flags
= nt
.opt
.hdr32
.LoaderFlags
;
546 mapping
->image
.header_size
= nt
.opt
.hdr32
.SizeOfHeaders
;
547 mapping
->image
.checksum
= nt
.opt
.hdr32
.CheckSum
;
549 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
550 mapping
->image
.base
= nt
.opt
.hdr64
.ImageBase
;
551 mapping
->image
.entry_point
= nt
.opt
.hdr64
.ImageBase
+ nt
.opt
.hdr64
.AddressOfEntryPoint
;
552 mapping
->image
.map_size
= ROUND_SIZE( nt
.opt
.hdr64
.SizeOfImage
);
553 mapping
->image
.stack_size
= nt
.opt
.hdr64
.SizeOfStackReserve
;
554 mapping
->image
.stack_commit
= nt
.opt
.hdr64
.SizeOfStackCommit
;
555 mapping
->image
.subsystem
= nt
.opt
.hdr64
.Subsystem
;
556 mapping
->image
.subsystem_low
= nt
.opt
.hdr64
.MinorSubsystemVersion
;
557 mapping
->image
.subsystem_high
= nt
.opt
.hdr64
.MajorSubsystemVersion
;
558 mapping
->image
.dll_charact
= nt
.opt
.hdr64
.DllCharacteristics
;
559 mapping
->image
.loader_flags
= nt
.opt
.hdr64
.LoaderFlags
;
560 mapping
->image
.header_size
= nt
.opt
.hdr64
.SizeOfHeaders
;
561 mapping
->image
.checksum
= nt
.opt
.hdr64
.CheckSum
;
564 mapping
->image
.image_charact
= nt
.FileHeader
.Characteristics
;
565 mapping
->image
.machine
= nt
.FileHeader
.Machine
;
566 mapping
->image
.zerobits
= 0; /* FIXME */
567 mapping
->image
.gp
= 0; /* FIXME */
568 mapping
->image
.contains_code
= 0; /* FIXME */
569 mapping
->image
.image_flags
= 0; /* FIXME */
570 mapping
->image
.file_size
= file_size
;
572 /* load the section headers */
574 pos
+= sizeof(nt
.Signature
) + sizeof(nt
.FileHeader
) + nt
.FileHeader
.SizeOfOptionalHeader
;
575 size
= sizeof(*sec
) * nt
.FileHeader
.NumberOfSections
;
576 if (!mapping
->size
) mapping
->size
= mapping
->image
.map_size
;
577 else if (mapping
->size
> mapping
->image
.map_size
) return STATUS_SECTION_TOO_BIG
;
578 if (pos
+ size
> mapping
->image
.map_size
) return STATUS_INVALID_FILE_FOR_SECTION
;
579 if (pos
+ size
> mapping
->image
.header_size
) mapping
->image
.header_size
= pos
+ size
;
580 if (!(sec
= malloc( size
))) goto error
;
581 if (pread( unix_fd
, sec
, size
, pos
) != size
) goto error
;
583 if (!build_shared_mapping( mapping
, unix_fd
, sec
, nt
.FileHeader
.NumberOfSections
)) goto error
;
585 if (mapping
->shared_file
) list_add_head( &shared_list
, &mapping
->shared_entry
);
592 return STATUS_INVALID_FILE_FOR_SECTION
;
595 static struct ranges
*create_ranges(void)
597 struct ranges
*ranges
= alloc_object( &ranges_ops
);
599 if (!ranges
) return NULL
;
602 if (!(ranges
->ranges
= mem_alloc( ranges
->max
* sizeof(ranges
->ranges
) )))
604 release_object( ranges
);
610 static unsigned int get_mapping_flags( obj_handle_t handle
, unsigned int flags
)
612 switch (flags
& (SEC_IMAGE
| SEC_RESERVE
| SEC_COMMIT
| SEC_FILE
))
615 if (flags
& (SEC_WRITECOMBINE
| SEC_LARGE_PAGES
)) break;
616 if (handle
) return SEC_FILE
| SEC_IMAGE
;
617 set_error( STATUS_INVALID_FILE_FOR_SECTION
);
620 if (!handle
) return flags
;
623 if (flags
& SEC_LARGE_PAGES
) break;
624 if (handle
) return SEC_FILE
| (flags
& (SEC_NOCACHE
| SEC_WRITECOMBINE
));
627 set_error( STATUS_INVALID_PARAMETER
);
632 static struct object
*create_mapping( struct object
*root
, const struct unicode_str
*name
,
633 unsigned int attr
, mem_size_t size
, unsigned int flags
,
634 obj_handle_t handle
, unsigned int file_access
,
635 const struct security_descriptor
*sd
)
637 struct mapping
*mapping
;
643 if (!page_mask
) page_mask
= sysconf( _SC_PAGESIZE
) - 1;
645 if (!(mapping
= create_named_object( root
, &mapping_ops
, name
, attr
, sd
)))
647 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
648 return &mapping
->obj
; /* Nothing else to do */
650 mapping
->size
= size
;
652 mapping
->shared_file
= NULL
;
653 mapping
->committed
= NULL
;
655 if (!(mapping
->flags
= get_mapping_flags( handle
, flags
))) goto error
;
659 const unsigned int sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
660 unsigned int mapping_access
= FILE_MAPPING_ACCESS
;
662 if (!(file
= get_file_obj( current
->process
, handle
, file_access
))) goto error
;
663 fd
= get_obj_fd( (struct object
*)file
);
665 /* file sharing rules for mappings are different so we use magic the access rights */
666 if (flags
& SEC_IMAGE
) mapping_access
|= FILE_MAPPING_IMAGE
;
667 else if (file_access
& FILE_WRITE_DATA
) mapping_access
|= FILE_MAPPING_WRITE
;
669 if (!(mapping
->fd
= get_fd_object_for_mapping( fd
, mapping_access
, sharing
)))
671 mapping
->fd
= dup_fd_object( fd
, mapping_access
, sharing
, FILE_SYNCHRONOUS_IO_NONALERT
);
672 if (mapping
->fd
) set_fd_user( mapping
->fd
, &mapping_fd_ops
, NULL
);
674 release_object( file
);
675 release_object( fd
);
676 if (!mapping
->fd
) goto error
;
678 if ((unix_fd
= get_unix_fd( mapping
->fd
)) == -1) goto error
;
679 if (fstat( unix_fd
, &st
) == -1)
684 if (flags
& SEC_IMAGE
)
686 unsigned int err
= get_image_params( mapping
, st
.st_size
, unix_fd
);
687 if (!err
) return &mapping
->obj
;
693 if (!(mapping
->size
= st
.st_size
))
695 set_error( STATUS_MAPPED_FILE_SIZE_ZERO
);
699 else if (st
.st_size
< mapping
->size
)
701 if (!(file_access
& FILE_WRITE_DATA
))
703 set_error( STATUS_SECTION_TOO_BIG
);
706 if (!grow_file( unix_fd
, mapping
->size
)) goto error
;
709 else /* Anonymous mapping (no associated file) */
713 set_error( STATUS_INVALID_PARAMETER
);
716 if ((flags
& SEC_RESERVE
) && !(mapping
->committed
= create_ranges())) goto error
;
717 mapping
->size
= (mapping
->size
+ page_mask
) & ~((mem_size_t
)page_mask
);
718 if ((unix_fd
= create_temp_file( mapping
->size
)) == -1) goto error
;
719 if (!(mapping
->fd
= create_anonymous_fd( &mapping_fd_ops
, unix_fd
, &mapping
->obj
,
720 FILE_SYNCHRONOUS_IO_NONALERT
))) goto error
;
721 allow_fd_caching( mapping
->fd
);
723 return &mapping
->obj
;
726 release_object( mapping
);
730 struct mapping
*get_mapping_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
732 return (struct mapping
*)get_handle_obj( process
, handle
, access
, &mapping_ops
);
735 /* open a new file handle to the file backing the mapping */
736 obj_handle_t
open_mapping_file( struct process
*process
, client_ptr_t base
,
737 unsigned int access
, unsigned int sharing
)
740 struct memory_view
*view
= find_mapped_view( process
, base
);
743 if (!view
|| !view
->fd
) return 0;
744 if (!(file
= create_file_for_fd_obj( view
->fd
, access
, sharing
))) return 0;
745 handle
= alloc_handle( process
, file
, access
, 0 );
746 release_object( file
);
750 static void mapping_dump( struct object
*obj
, int verbose
)
752 struct mapping
*mapping
= (struct mapping
*)obj
;
753 assert( obj
->ops
== &mapping_ops
);
754 fprintf( stderr
, "Mapping size=%08x%08x flags=%08x fd=%p shared_file=%p\n",
755 (unsigned int)(mapping
->size
>> 32), (unsigned int)mapping
->size
,
756 mapping
->flags
, mapping
->fd
, mapping
->shared_file
);
759 static struct object_type
*mapping_get_type( struct object
*obj
)
761 static const WCHAR name
[] = {'S','e','c','t','i','o','n'};
762 static const struct unicode_str str
= { name
, sizeof(name
) };
763 return get_object_type( &str
);
766 static struct fd
*mapping_get_fd( struct object
*obj
)
768 struct mapping
*mapping
= (struct mapping
*)obj
;
769 return (struct fd
*)grab_object( mapping
->fd
);
772 static unsigned int mapping_map_access( struct object
*obj
, unsigned int access
)
774 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SECTION_QUERY
| SECTION_MAP_READ
;
775 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SECTION_MAP_WRITE
;
776 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SECTION_MAP_EXECUTE
;
777 if (access
& GENERIC_ALL
) access
|= SECTION_ALL_ACCESS
;
778 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
781 static void mapping_destroy( struct object
*obj
)
783 struct mapping
*mapping
= (struct mapping
*)obj
;
784 assert( obj
->ops
== &mapping_ops
);
785 if (mapping
->fd
) release_object( mapping
->fd
);
786 if (mapping
->committed
) release_object( mapping
->committed
);
787 if (mapping
->shared_file
)
789 release_object( mapping
->shared_file
);
790 list_remove( &mapping
->shared_entry
);
794 static enum server_fd_type
mapping_get_fd_type( struct fd
*fd
)
799 int get_page_size(void)
801 if (!page_mask
) page_mask
= sysconf( _SC_PAGESIZE
) - 1;
802 return page_mask
+ 1;
805 /* create a file mapping */
806 DECL_HANDLER(create_mapping
)
808 struct object
*root
, *obj
;
809 struct unicode_str name
;
810 const struct security_descriptor
*sd
;
811 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
813 if (!objattr
) return;
815 if ((obj
= create_mapping( root
, &name
, objattr
->attributes
, req
->size
, req
->flags
,
816 req
->file_handle
, req
->file_access
, sd
)))
818 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
819 reply
->handle
= alloc_handle( current
->process
, obj
, req
->access
, objattr
->attributes
);
821 reply
->handle
= alloc_handle_no_access_check( current
->process
, obj
,
822 req
->access
, objattr
->attributes
);
823 release_object( obj
);
826 if (root
) release_object( root
);
829 /* open a handle to a mapping */
830 DECL_HANDLER(open_mapping
)
832 struct unicode_str name
= get_req_unicode_str();
834 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
835 &mapping_ops
, &name
, req
->attributes
);
838 /* get a mapping information */
839 DECL_HANDLER(get_mapping_info
)
841 struct mapping
*mapping
;
843 if (!(mapping
= get_mapping_obj( current
->process
, req
->handle
, req
->access
))) return;
845 reply
->size
= mapping
->size
;
846 reply
->flags
= mapping
->flags
;
848 if (mapping
->flags
& SEC_IMAGE
)
849 set_reply_data( &mapping
->image
, min( sizeof(mapping
->image
), get_reply_max_size() ));
851 if (!(req
->access
& (SECTION_MAP_READ
| SECTION_MAP_WRITE
))) /* query only */
853 release_object( mapping
);
857 if ((mapping
->flags
& SEC_IMAGE
) && mapping
->cpu
!= current
->process
->cpu
)
859 set_error( STATUS_INVALID_IMAGE_FORMAT
);
860 release_object( mapping
);
864 if (mapping
->shared_file
)
865 reply
->shared_file
= alloc_handle( current
->process
, mapping
->shared_file
,
866 GENERIC_READ
|GENERIC_WRITE
, 0 );
867 release_object( mapping
);
870 /* add a memory view in the current process */
871 DECL_HANDLER(map_view
)
873 struct mapping
*mapping
= NULL
;
874 struct memory_view
*view
;
876 if (!req
->size
|| (req
->base
& page_mask
) || req
->base
+ req
->size
< req
->base
) /* overflow */
878 set_error( STATUS_INVALID_PARAMETER
);
882 /* make sure we don't already have an overlapping view */
883 LIST_FOR_EACH_ENTRY( view
, ¤t
->process
->views
, struct memory_view
, entry
)
885 if (view
->base
+ view
->size
<= req
->base
) continue;
886 if (view
->base
>= req
->base
+ req
->size
) continue;
887 set_error( STATUS_INVALID_PARAMETER
);
891 if (!(mapping
= get_mapping_obj( current
->process
, req
->mapping
, req
->access
))) return;
893 if (mapping
->flags
& SEC_IMAGE
)
895 if (req
->start
|| req
->size
> mapping
->image
.map_size
)
897 set_error( STATUS_INVALID_PARAMETER
);
901 else if (req
->start
>= mapping
->size
||
902 req
->start
+ req
->size
< req
->start
||
903 req
->start
+ req
->size
> ((mapping
->size
+ page_mask
) & ~(mem_size_t
)page_mask
))
905 set_error( STATUS_INVALID_PARAMETER
);
909 if ((view
= mem_alloc( sizeof(*view
) )))
911 view
->base
= req
->base
;
912 view
->size
= req
->size
;
913 view
->start
= req
->start
;
914 view
->flags
= mapping
->flags
;
915 view
->fd
= !is_fd_removable( mapping
->fd
) ? (struct fd
*)grab_object( mapping
->fd
) : NULL
;
916 view
->committed
= mapping
->committed
? (struct ranges
*)grab_object( mapping
->committed
) : NULL
;
917 list_add_tail( ¤t
->process
->views
, &view
->entry
);
921 release_object( mapping
);
924 /* unmap a memory view from the current process */
925 DECL_HANDLER(unmap_view
)
927 struct memory_view
*view
= find_mapped_view( current
->process
, req
->base
);
929 if (view
) free_memory_view( view
);
932 /* get a range of committed pages in a file mapping */
933 DECL_HANDLER(get_mapping_committed_range
)
935 struct memory_view
*view
= find_mapped_view( current
->process
, req
->base
);
937 if (view
) reply
->committed
= find_committed_range( view
, req
->offset
, &reply
->size
);
940 /* add a range to the committed pages in a file mapping */
941 DECL_HANDLER(add_mapping_committed_range
)
943 struct memory_view
*view
= find_mapped_view( current
->process
, req
->base
);
945 if (view
) add_committed_range( view
, req
->offset
, req
->offset
+ req
->size
);
948 /* check if two memory maps are for the same file */
949 DECL_HANDLER(is_same_mapping
)
951 struct memory_view
*view1
= find_mapped_view( current
->process
, req
->base1
);
952 struct memory_view
*view2
= find_mapped_view( current
->process
, req
->base2
);
954 if (!view1
|| !view2
) return;
955 if (!view1
->fd
|| !view2
->fd
||
956 !(view1
->flags
& SEC_IMAGE
) || !(view2
->flags
& SEC_IMAGE
) ||
957 !is_same_file_fd( view1
->fd
, view2
->fd
))
958 set_error( STATUS_NOT_SAME_DEVICE
);