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 */
31 struct mapping
*shared_next
; /* next in shared PE mapping list */
32 struct mapping
*shared_prev
; /* prev in shared PE mapping list */
35 static int mapping_get_fd( struct object
*obj
);
36 static void mapping_dump( struct object
*obj
, int verbose
);
37 static void mapping_destroy( struct object
*obj
);
39 static const struct object_ops mapping_ops
=
41 sizeof(struct mapping
), /* size */
42 mapping_dump
, /* dump */
43 no_add_queue
, /* add_queue */
44 NULL
, /* remove_queue */
47 NULL
, /* get_poll_events */
48 NULL
, /* poll_event */
49 mapping_get_fd
, /* get_fd */
51 no_get_file_info
, /* get_file_info */
52 mapping_destroy
/* destroy */
55 static struct mapping
*shared_first
;
59 /* These are always the same on an i386, and it will be faster this way */
60 # define page_mask 0xfff
61 # define page_shift 12
62 # define init_page_size() do { /* nothing */ } while(0)
66 static int page_shift
, page_mask
;
68 static void init_page_size(void)
71 # ifdef HAVE_GETPAGESIZE
72 page_size
= getpagesize();
75 page_size
= sysconf(_SC_PAGESIZE
);
77 # error Cannot get the page size on this platform
80 page_mask
= page_size
- 1;
81 /* Make sure we have a power of 2 */
82 assert( !(page_size
& page_mask
) );
84 while ((1 << page_shift
) != page_size
) page_shift
++;
88 #define ROUND_ADDR(addr) \
89 ((int)(addr) & ~page_mask)
91 #define ROUND_SIZE(addr,size) \
92 (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
94 /* get the fd to use for mmaping a file */
95 inline static int get_mmap_fd( struct file
*file
)
98 if (!(obj
= (struct object
*)file
))
100 set_error( STATUS_INVALID_HANDLE
);
103 return obj
->ops
->get_fd( obj
);
106 /* find the shared PE mapping for a given mapping */
107 static struct file
*get_shared_file( struct mapping
*mapping
)
111 for (ptr
= shared_first
; ptr
; ptr
= ptr
->shared_next
)
112 if (is_same_file( ptr
->file
, mapping
->file
))
113 return (struct file
*)grab_object( ptr
->shared_file
);
117 /* allocate and fill the temp file for a shared PE image mapping */
118 static int build_shared_mapping( struct mapping
*mapping
, int fd
,
119 IMAGE_SECTION_HEADER
*sec
, int nb_sec
)
121 int i
, max_size
, total_size
, pos
;
126 /* compute the total size of the shared mapping */
128 total_size
= max_size
= 0;
129 for (i
= 0; i
< nb_sec
; i
++)
131 if ((sec
[i
].Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
132 (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
))
134 int size
= ROUND_SIZE( 0, sec
[i
].Misc
.VirtualSize
);
135 if (size
> max_size
) max_size
= size
;
139 if (!(mapping
->shared_size
= total_size
)) return 1; /* nothing to do */
141 if ((mapping
->shared_file
= get_shared_file( mapping
))) return 1;
143 /* create a temp file for the mapping */
145 if (!(mapping
->shared_file
= create_temp_file( GENERIC_READ
|GENERIC_WRITE
))) goto error
;
146 if (!grow_file( mapping
->shared_file
, 0, total_size
)) goto error
;
147 if ((shared_fd
= get_mmap_fd( mapping
->shared_file
)) == -1) goto error
;
149 if (!(buffer
= malloc( max_size
))) goto error
;
151 /* copy the shared sections data into the temp file */
153 for (i
= pos
= 0; i
< nb_sec
; i
++)
155 if (!(sec
[i
].Characteristics
& IMAGE_SCN_MEM_SHARED
)) continue;
156 if (!(sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
)) continue;
157 if (lseek( shared_fd
, pos
, SEEK_SET
) != pos
) goto error
;
158 pos
+= ROUND_SIZE( 0, sec
[i
].Misc
.VirtualSize
);
159 if (sec
[i
].Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
) continue;
160 if (!sec
[i
].PointerToRawData
|| !sec
[i
].SizeOfRawData
) continue;
161 if (lseek( fd
, sec
[i
].PointerToRawData
, SEEK_SET
) != sec
[i
].PointerToRawData
) goto error
;
162 toread
= sec
[i
].SizeOfRawData
;
165 long res
= read( fd
, buffer
+ sec
[i
].SizeOfRawData
- toread
, toread
);
166 if (res
<= 0) goto error
;
169 if (write( shared_fd
, buffer
, sec
[i
].SizeOfRawData
) != sec
[i
].SizeOfRawData
) goto error
;
175 if (buffer
) free( buffer
);
179 /* retrieve the mapping parameters for an executable (PE) image */
180 static int get_image_params( struct mapping
*mapping
)
182 IMAGE_DOS_HEADER dos
;
184 IMAGE_SECTION_HEADER
*sec
= NULL
;
185 int fd
, filepos
, size
;
187 /* load the headers */
189 if ((fd
= get_mmap_fd( mapping
->file
)) == -1) return 0;
190 filepos
= lseek( fd
, 0, SEEK_SET
);
191 if (read( fd
, &dos
, sizeof(dos
) ) != sizeof(dos
)) goto error
;
192 if (dos
.e_magic
!= IMAGE_DOS_SIGNATURE
) goto error
;
193 if (lseek( fd
, dos
.e_lfanew
, SEEK_SET
) == -1) goto error
;
194 if (read( fd
, &nt
, sizeof(nt
) ) != sizeof(nt
)) goto error
;
195 if (nt
.Signature
!= IMAGE_NT_SIGNATURE
) goto error
;
197 /* load the section headers */
199 size
= sizeof(*sec
) * nt
.FileHeader
.NumberOfSections
;
200 if (!(sec
= malloc( size
))) goto error
;
201 if (read( fd
, sec
, size
) != size
) goto error
;
203 if (!build_shared_mapping( mapping
, fd
, sec
, nt
.FileHeader
.NumberOfSections
)) goto error
;
205 if (mapping
->shared_file
) /* link it in the list */
207 if ((mapping
->shared_next
= shared_first
)) shared_first
->shared_prev
= mapping
;
208 mapping
->shared_prev
= NULL
;
209 shared_first
= mapping
;
212 mapping
->size_low
= ROUND_SIZE( 0, nt
.OptionalHeader
.SizeOfImage
);
213 mapping
->size_high
= 0;
214 mapping
->base
= (void *)nt
.OptionalHeader
.ImageBase
;
215 mapping
->header_size
= ROUND_SIZE( mapping
->base
, nt
.OptionalHeader
.SizeOfHeaders
);
216 mapping
->protect
= VPROT_IMAGE
;
219 if (mapping
->header_size
> mapping
->size_low
) goto error
;
221 lseek( fd
, filepos
, SEEK_SET
);
226 lseek( fd
, filepos
, SEEK_SET
);
227 if (sec
) free( sec
);
228 set_error( STATUS_INVALID_FILE_FOR_SECTION
);
233 static struct object
*create_mapping( int size_high
, int size_low
, int protect
,
234 handle_t handle
, const WCHAR
*name
, size_t len
)
236 struct mapping
*mapping
;
239 if (!page_mask
) init_page_size();
241 if (!(mapping
= create_named_object( &mapping_ops
, name
, len
)))
243 if (get_error() == STATUS_OBJECT_NAME_COLLISION
)
244 return &mapping
->obj
; /* Nothing else to do */
246 mapping
->header_size
= 0;
247 mapping
->base
= NULL
;
248 mapping
->shared_file
= NULL
;
249 mapping
->shared_size
= 0;
251 if (protect
& VPROT_READ
) access
|= GENERIC_READ
;
252 if (protect
& VPROT_WRITE
) access
|= GENERIC_WRITE
;
256 if (!(mapping
->file
= get_file_obj( current
->process
, handle
, access
))) goto error
;
257 if (protect
& VPROT_IMAGE
)
259 if (!get_image_params( mapping
)) goto error
;
260 return &mapping
->obj
;
262 if (!size_high
&& !size_low
)
264 struct get_file_info_request req
;
265 struct object
*obj
= (struct object
*)mapping
->file
;
266 obj
->ops
->get_file_info( obj
, &req
);
267 size_high
= req
.size_high
;
268 size_low
= ROUND_SIZE( 0, req
.size_low
);
270 else if (!grow_file( mapping
->file
, size_high
, size_low
)) goto error
;
272 else /* Anonymous mapping (no associated file) */
274 if ((!size_high
&& !size_low
) || (protect
& VPROT_IMAGE
))
276 set_error( STATUS_INVALID_PARAMETER
);
277 mapping
->file
= NULL
;
280 if (!(mapping
->file
= create_temp_file( access
))) goto error
;
281 if (!grow_file( mapping
->file
, size_high
, size_low
)) goto error
;
283 mapping
->size_high
= size_high
;
284 mapping
->size_low
= ROUND_SIZE( 0, size_low
);
285 mapping
->protect
= protect
;
286 return &mapping
->obj
;
289 release_object( mapping
);
293 static void mapping_dump( struct object
*obj
, int verbose
)
295 struct mapping
*mapping
= (struct mapping
*)obj
;
296 assert( obj
->ops
== &mapping_ops
);
297 fprintf( stderr
, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
298 "shared_file=%p shared_size=%08x ",
299 mapping
->size_high
, mapping
->size_low
, mapping
->protect
, mapping
->file
,
300 mapping
->header_size
, mapping
->base
, mapping
->shared_file
, mapping
->shared_size
);
301 dump_object_name( &mapping
->obj
);
302 fputc( '\n', stderr
);
305 static int mapping_get_fd( struct object
*obj
)
307 struct mapping
*mapping
= (struct mapping
*)obj
;
308 assert( obj
->ops
== &mapping_ops
);
309 return get_mmap_fd( mapping
->file
);
312 static void mapping_destroy( struct object
*obj
)
314 struct mapping
*mapping
= (struct mapping
*)obj
;
315 assert( obj
->ops
== &mapping_ops
);
316 if (mapping
->file
) release_object( mapping
->file
);
317 if (mapping
->shared_file
)
319 release_object( mapping
->shared_file
);
320 if (mapping
->shared_next
) mapping
->shared_next
->shared_prev
= mapping
->shared_prev
;
321 if (mapping
->shared_prev
) mapping
->shared_prev
->shared_next
= mapping
->shared_next
;
322 else shared_first
= mapping
->shared_next
;
326 int get_page_size(void)
328 if (!page_mask
) init_page_size();
329 return page_mask
+ 1;
332 /* create a file mapping */
333 DECL_HANDLER(create_mapping
)
338 if ((obj
= create_mapping( req
->size_high
, req
->size_low
,
339 req
->protect
, req
->file_handle
,
340 get_req_data(req
), get_req_data_size(req
) )))
342 int access
= FILE_MAP_ALL_ACCESS
;
343 if (!(req
->protect
& VPROT_WRITE
)) access
&= ~FILE_MAP_WRITE
;
344 req
->handle
= alloc_handle( current
->process
, obj
, access
, req
->inherit
);
345 release_object( obj
);
349 /* open a handle to a mapping */
350 DECL_HANDLER(open_mapping
)
352 req
->handle
= open_object( get_req_data(req
), get_req_data_size(req
),
353 &mapping_ops
, req
->access
, req
->inherit
);
356 /* get a mapping information */
357 DECL_HANDLER(get_mapping_info
)
359 struct mapping
*mapping
;
361 if ((mapping
= (struct mapping
*)get_handle_obj( current
->process
, req
->handle
,
364 req
->size_high
= mapping
->size_high
;
365 req
->size_low
= mapping
->size_low
;
366 req
->protect
= mapping
->protect
;
367 req
->header_size
= mapping
->header_size
;
368 req
->base
= mapping
->base
;
369 req
->shared_file
= 0;
370 req
->shared_size
= mapping
->shared_size
;
371 if (mapping
->shared_file
)
372 req
->shared_file
= alloc_handle( current
->process
, mapping
->shared_file
,
373 GENERIC_READ
|GENERIC_WRITE
, 0 );
374 release_object( mapping
);