2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
23 struct object obj
; /* object header */
24 int size_high
; /* mapping size */
25 int size_low
; /* mapping size */
26 int protect
; /* protection flags */
27 struct file
*file
; /* file mapped */
28 int header_size
; /* size of headers (for PE image mapping) */
29 void *base
; /* default base addr (for PE image mapping) */
30 struct file
*shared_file
; /* temp file for shared PE mapping */
31 int shared_size
; /* shared mapping total size */
32 struct mapping
*shared_next
; /* next in shared PE mapping list */
33 struct mapping
*shared_prev
; /* prev in shared PE mapping list */
36 static int mapping_get_fd( struct object
*obj
);
37 static int mapping_get_info( struct object
*obj
, struct get_file_info_reply
*reply
);
38 static void mapping_dump( struct object
*obj
, int verbose
);
39 static void mapping_destroy( struct object
*obj
);
41 static const struct object_ops mapping_ops
=
43 sizeof(struct mapping
), /* size */
44 mapping_dump
, /* dump */
45 no_add_queue
, /* add_queue */
46 NULL
, /* remove_queue */
49 NULL
, /* get_poll_events */
50 NULL
, /* poll_event */
51 mapping_get_fd
, /* get_fd */
53 mapping_get_info
, /* get_file_info */
54 NULL
, /* queue_async */
55 mapping_destroy
/* destroy */
58 static struct mapping
*shared_first
;
62 /* These are always the same on an i386, and it will be faster this way */
63 # define page_mask 0xfff
64 # define page_shift 12
65 # define init_page_size() do { /* nothing */ } while(0)
69 static int page_shift
, page_mask
;
71 static void init_page_size(void)
74 # ifdef HAVE_GETPAGESIZE
75 page_size
= getpagesize();
78 page_size
= sysconf(_SC_PAGESIZE
);
80 # error Cannot get the page size on this platform
83 page_mask
= page_size
- 1;
84 /* Make sure we have a power of 2 */
85 assert( !(page_size
& page_mask
) );
87 while ((1 << page_shift
) != page_size
) page_shift
++;
91 #define ROUND_ADDR(addr) \
92 ((int)(addr) & ~page_mask)
94 #define ROUND_SIZE(addr,size) \
95 (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
97 /* get the fd to use for mmaping a file */
98 inline static int get_mmap_fd( struct file
*file
)
101 if (!(obj
= (struct object
*)file
))
103 set_error( STATUS_INVALID_HANDLE
);
106 return obj
->ops
->get_fd( obj
);
109 /* find the shared PE mapping for a given mapping */
110 static struct file
*get_shared_file( struct mapping
*mapping
)
114 for (ptr
= shared_first
; ptr
; ptr
= ptr
->shared_next
)
115 if (is_same_file( ptr
->file
, mapping
->file
))
116 return (struct file
*)grab_object( ptr
->shared_file
);
120 /* allocate and fill the temp file for a shared PE image mapping */
121 static int build_shared_mapping( struct mapping
*mapping
, int fd
,
122 IMAGE_SECTION_HEADER
*sec
, int nb_sec
)
124 int i
, max_size
, total_size
, pos
;
129 /* compute the total size of the shared mapping */
131 total_size
= max_size
= 0;
132 for (i
= 0; i
< nb_sec
; i
++)
134 if ((sec
[i
].Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
135 (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
))
137 int size
= ROUND_SIZE( 0, sec
[i
].Misc
.VirtualSize
);
138 if (size
> max_size
) max_size
= size
;
142 if (!(mapping
->shared_size
= total_size
)) return 1; /* nothing to do */
144 if ((mapping
->shared_file
= get_shared_file( mapping
))) return 1;
146 /* create a temp file for the mapping */
148 if (!(mapping
->shared_file
= create_temp_file( GENERIC_READ
|GENERIC_WRITE
))) goto error
;
149 if (!grow_file( mapping
->shared_file
, 0, total_size
)) goto error
;
150 if ((shared_fd
= get_mmap_fd( mapping
->shared_file
)) == -1) goto error
;
152 if (!(buffer
= malloc( max_size
))) goto error
;
154 /* copy the shared sections data into the temp file */
156 for (i
= pos
= 0; i
< nb_sec
; i
++)
158 if (!(sec
[i
].Characteristics
& IMAGE_SCN_MEM_SHARED
)) continue;
159 if (!(sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
)) continue;
160 if (lseek( shared_fd
, pos
, SEEK_SET
) != pos
) goto error
;
161 pos
+= ROUND_SIZE( 0, sec
[i
].Misc
.VirtualSize
);
162 if (sec
[i
].Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
) continue;
163 if (!sec
[i
].PointerToRawData
|| !sec
[i
].SizeOfRawData
) continue;
164 if (lseek( fd
, sec
[i
].PointerToRawData
, SEEK_SET
) != sec
[i
].PointerToRawData
) goto error
;
165 toread
= sec
[i
].SizeOfRawData
;
168 long res
= read( fd
, buffer
+ sec
[i
].SizeOfRawData
- toread
, toread
);
169 if (res
<= 0) goto error
;
172 if (write( shared_fd
, buffer
, sec
[i
].SizeOfRawData
) != sec
[i
].SizeOfRawData
) goto error
;
178 if (buffer
) free( buffer
);
182 /* retrieve the mapping parameters for an executable (PE) image */
183 static int get_image_params( struct mapping
*mapping
)
185 IMAGE_DOS_HEADER dos
;
187 IMAGE_SECTION_HEADER
*sec
= NULL
;
188 int fd
, filepos
, size
;
190 /* load the headers */
192 if ((fd
= get_mmap_fd( mapping
->file
)) == -1) return 0;
193 filepos
= lseek( fd
, 0, SEEK_SET
);
194 if (read( fd
, &dos
, sizeof(dos
) ) != sizeof(dos
)) goto error
;
195 if (dos
.e_magic
!= IMAGE_DOS_SIGNATURE
) goto error
;
196 if (lseek( fd
, dos
.e_lfanew
, SEEK_SET
) == -1) goto error
;
197 if (read( fd
, &nt
, sizeof(nt
) ) != sizeof(nt
)) goto error
;
198 if (nt
.Signature
!= IMAGE_NT_SIGNATURE
) goto error
;
200 /* load the section headers */
202 size
= sizeof(*sec
) * nt
.FileHeader
.NumberOfSections
;
203 if (!(sec
= malloc( size
))) goto error
;
204 if (read( fd
, sec
, size
) != size
) goto error
;
206 if (!build_shared_mapping( mapping
, fd
, sec
, nt
.FileHeader
.NumberOfSections
)) goto error
;
208 if (mapping
->shared_file
) /* link it in the list */
210 if ((mapping
->shared_next
= shared_first
)) shared_first
->shared_prev
= mapping
;
211 mapping
->shared_prev
= NULL
;
212 shared_first
= mapping
;
215 mapping
->size_low
= ROUND_SIZE( 0, nt
.OptionalHeader
.SizeOfImage
);
216 mapping
->size_high
= 0;
217 mapping
->base
= (void *)nt
.OptionalHeader
.ImageBase
;
218 mapping
->header_size
= ROUND_SIZE( mapping
->base
, nt
.OptionalHeader
.SizeOfHeaders
);
219 mapping
->protect
= VPROT_IMAGE
;
222 if (mapping
->header_size
> mapping
->size_low
) goto error
;
224 lseek( fd
, filepos
, SEEK_SET
);
229 lseek( fd
, filepos
, SEEK_SET
);
230 if (sec
) free( sec
);
231 set_error( STATUS_INVALID_FILE_FOR_SECTION
);
236 static struct object
*create_mapping( int size_high
, int size_low
, int protect
,
237 handle_t handle
, const WCHAR
*name
, size_t len
)
239 struct mapping
*mapping
;
242 if (!page_mask
) init_page_size();
244 if (!(mapping
= create_named_object( &mapping_ops
, name
, len
)))
246 if (get_error() == STATUS_OBJECT_NAME_COLLISION
)
247 return &mapping
->obj
; /* Nothing else to do */
249 mapping
->header_size
= 0;
250 mapping
->base
= NULL
;
251 mapping
->shared_file
= NULL
;
252 mapping
->shared_size
= 0;
254 if (protect
& VPROT_READ
) access
|= GENERIC_READ
;
255 if (protect
& VPROT_WRITE
) access
|= GENERIC_WRITE
;
259 if (!(mapping
->file
= get_file_obj( current
->process
, handle
, access
))) goto error
;
260 if (protect
& VPROT_IMAGE
)
262 if (!get_image_params( mapping
)) goto error
;
263 return &mapping
->obj
;
265 if (!size_high
&& !size_low
)
267 struct get_file_info_reply reply
;
268 struct object
*obj
= (struct object
*)mapping
->file
;
269 obj
->ops
->get_file_info( obj
, &reply
);
270 size_high
= reply
.size_high
;
271 size_low
= ROUND_SIZE( 0, reply
.size_low
);
273 else if (!grow_file( mapping
->file
, size_high
, size_low
)) goto error
;
275 else /* Anonymous mapping (no associated file) */
277 if ((!size_high
&& !size_low
) || (protect
& VPROT_IMAGE
))
279 set_error( STATUS_INVALID_PARAMETER
);
280 mapping
->file
= NULL
;
283 if (!(mapping
->file
= create_temp_file( access
))) goto error
;
284 if (!grow_file( mapping
->file
, size_high
, size_low
)) goto error
;
286 mapping
->size_high
= size_high
;
287 mapping
->size_low
= ROUND_SIZE( 0, size_low
);
288 mapping
->protect
= protect
;
289 return &mapping
->obj
;
292 release_object( mapping
);
296 static void mapping_dump( struct object
*obj
, int verbose
)
298 struct mapping
*mapping
= (struct mapping
*)obj
;
299 assert( obj
->ops
== &mapping_ops
);
300 fprintf( stderr
, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
301 "shared_file=%p shared_size=%08x ",
302 mapping
->size_high
, mapping
->size_low
, mapping
->protect
, mapping
->file
,
303 mapping
->header_size
, mapping
->base
, mapping
->shared_file
, mapping
->shared_size
);
304 dump_object_name( &mapping
->obj
);
305 fputc( '\n', stderr
);
308 static int mapping_get_fd( struct object
*obj
)
310 struct mapping
*mapping
= (struct mapping
*)obj
;
311 assert( obj
->ops
== &mapping_ops
);
312 return get_mmap_fd( mapping
->file
);
315 static int mapping_get_info( struct object
*obj
, struct get_file_info_reply
*reply
)
317 struct mapping
*mapping
= (struct mapping
*)obj
;
318 struct object
*file
= (struct object
*)mapping
->file
;
320 assert( obj
->ops
== &mapping_ops
);
322 return file
->ops
->get_file_info( file
, reply
);
325 static void mapping_destroy( struct object
*obj
)
327 struct mapping
*mapping
= (struct mapping
*)obj
;
328 assert( obj
->ops
== &mapping_ops
);
329 if (mapping
->file
) release_object( mapping
->file
);
330 if (mapping
->shared_file
)
332 release_object( mapping
->shared_file
);
333 if (mapping
->shared_next
) mapping
->shared_next
->shared_prev
= mapping
->shared_prev
;
334 if (mapping
->shared_prev
) mapping
->shared_prev
->shared_next
= mapping
->shared_next
;
335 else shared_first
= mapping
->shared_next
;
339 int get_page_size(void)
341 if (!page_mask
) init_page_size();
342 return page_mask
+ 1;
345 /* create a file mapping */
346 DECL_HANDLER(create_mapping
)
351 if ((obj
= create_mapping( req
->size_high
, req
->size_low
,
352 req
->protect
, req
->file_handle
,
353 get_req_data(), get_req_data_size() )))
355 int access
= FILE_MAP_ALL_ACCESS
;
356 if (!(req
->protect
& VPROT_WRITE
)) access
&= ~FILE_MAP_WRITE
;
357 reply
->handle
= alloc_handle( current
->process
, obj
, access
, req
->inherit
);
358 release_object( obj
);
362 /* open a handle to a mapping */
363 DECL_HANDLER(open_mapping
)
365 reply
->handle
= open_object( get_req_data(), get_req_data_size(),
366 &mapping_ops
, req
->access
, req
->inherit
);
369 /* get a mapping information */
370 DECL_HANDLER(get_mapping_info
)
372 struct mapping
*mapping
;
374 if ((mapping
= (struct mapping
*)get_handle_obj( current
->process
, req
->handle
,
377 reply
->size_high
= mapping
->size_high
;
378 reply
->size_low
= mapping
->size_low
;
379 reply
->protect
= mapping
->protect
;
380 reply
->header_size
= mapping
->header_size
;
381 reply
->base
= mapping
->base
;
382 reply
->shared_file
= 0;
383 reply
->shared_size
= mapping
->shared_size
;
384 reply
->drive_type
= get_file_drive_type( mapping
->file
);
385 if (mapping
->shared_file
)
386 reply
->shared_file
= alloc_handle( current
->process
, mapping
->shared_file
,
387 GENERIC_READ
|GENERIC_WRITE
, 0 );
388 release_object( mapping
);