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 */
33 static void mapping_dump( struct object
*obj
, int verbose
);
34 static void mapping_destroy( struct object
*obj
);
36 static const struct object_ops mapping_ops
=
38 sizeof(struct mapping
), /* size */
39 mapping_dump
, /* dump */
40 no_add_queue
, /* add_queue */
41 NULL
, /* remove_queue */
44 NULL
, /* get_poll_events */
45 NULL
, /* poll_event */
46 no_read_fd
, /* get_read_fd */
47 no_write_fd
, /* get_write_fd */
49 no_get_file_info
, /* get_file_info */
50 mapping_destroy
/* destroy */
55 /* These are always the same on an i386, and it will be faster this way */
56 # define page_mask 0xfff
57 # define page_shift 12
58 # define init_page_size() /* nothing */
62 static int page_shift
, page_mask
;
64 static void init_page_size(void)
67 # ifdef HAVE_GETPAGESIZE
68 page_size
= getpagesize();
71 page_size
= sysconf(_SC_PAGESIZE
);
73 # error Cannot get the page size on this platform
76 page_mask
= page_size
- 1;
77 /* Make sure we have a power of 2 */
78 assert( !(page_size
& page_mask
) );
80 while ((1 << page_shift
) != page_size
) page_shift
++;
84 #define ROUND_ADDR(addr) \
85 ((int)(addr) & ~page_mask)
87 #define ROUND_SIZE(addr,size) \
88 (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
91 /* allocate and fill the temp file for a shared PE image mapping */
92 static int build_shared_mapping( struct mapping
*mapping
, int fd
,
93 IMAGE_SECTION_HEADER
*sec
, int nb_sec
)
95 int i
, max_size
, total_size
, pos
;
99 /* compute the total size of the shared mapping */
101 total_size
= max_size
= 0;
102 for (i
= 0; i
< nb_sec
; i
++)
104 if ((sec
[i
].Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
105 (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
))
107 int size
= ROUND_SIZE( 0, sec
[i
].Misc
.VirtualSize
);
108 if (size
> max_size
) max_size
= size
;
112 if (!(mapping
->shared_size
= total_size
)) return 1; /* nothing to do */
114 /* create a temp file for the mapping */
116 if (!(mapping
->shared_file
= create_temp_file( GENERIC_READ
|GENERIC_WRITE
))) goto error
;
117 if (!grow_file( mapping
->shared_file
, 0, total_size
)) goto error
;
118 if ((shared_fd
= file_get_mmap_fd( mapping
->shared_file
)) == -1) goto error
;
120 if (!(buffer
= malloc( max_size
))) goto error
;
122 /* copy the shared sections data into the temp file */
124 for (i
= pos
= 0; i
< nb_sec
; i
++)
126 if (!(sec
[i
].Characteristics
& IMAGE_SCN_MEM_SHARED
)) continue;
127 if (!(sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
)) continue;
128 if (lseek( shared_fd
, pos
, SEEK_SET
) != pos
) goto error
;
129 pos
+= ROUND_SIZE( 0, sec
[i
].Misc
.VirtualSize
);
130 if (sec
->Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
) continue;
131 if (!sec
->PointerToRawData
|| !sec
->SizeOfRawData
) continue;
132 if (lseek( fd
, sec
[i
].PointerToRawData
, SEEK_SET
) != sec
[i
].PointerToRawData
) goto error
;
133 if (read( fd
, buffer
, sec
[i
].SizeOfRawData
) != sec
[i
].SizeOfRawData
) goto error
;
134 if (write( shared_fd
, buffer
, sec
[i
].SizeOfRawData
) != sec
[i
].SizeOfRawData
) goto error
;
141 if (shared_fd
!= -1) close( shared_fd
);
142 if (buffer
) free( buffer
);
146 /* retrieve the mapping parameters for an executable (PE) image */
147 static int get_image_params( struct mapping
*mapping
)
149 IMAGE_DOS_HEADER dos
;
151 IMAGE_SECTION_HEADER
*sec
= NULL
;
152 int fd
, filepos
, size
;
154 /* load the headers */
156 if ((fd
= file_get_mmap_fd( mapping
->file
)) == -1) return 0;
157 filepos
= lseek( fd
, 0, SEEK_SET
);
158 if (read( fd
, &dos
, sizeof(dos
) ) != sizeof(dos
)) goto error
;
159 if (dos
.e_magic
!= IMAGE_DOS_SIGNATURE
) goto error
;
160 if (lseek( fd
, dos
.e_lfanew
, SEEK_SET
) == -1) goto error
;
161 if (read( fd
, &nt
, sizeof(nt
) ) != sizeof(nt
)) goto error
;
162 if (nt
.Signature
!= IMAGE_NT_SIGNATURE
) goto error
;
164 /* load the section headers */
166 size
= sizeof(*sec
) * nt
.FileHeader
.NumberOfSections
;
167 if (!(sec
= malloc( size
))) goto error
;
168 if (read( fd
, sec
, size
) != size
) goto error
;
170 if (!build_shared_mapping( mapping
, fd
, sec
, nt
.FileHeader
.NumberOfSections
)) goto error
;
172 mapping
->size_low
= ROUND_SIZE( 0, nt
.OptionalHeader
.SizeOfImage
);
173 mapping
->size_high
= 0;
174 mapping
->base
= (void *)nt
.OptionalHeader
.ImageBase
;
175 mapping
->header_size
= ROUND_SIZE( mapping
->base
, nt
.OptionalHeader
.SizeOfHeaders
);
176 mapping
->protect
= VPROT_IMAGE
;
179 if (mapping
->header_size
> mapping
->size_low
) goto error
;
181 lseek( fd
, filepos
, SEEK_SET
);
187 lseek( fd
, filepos
, SEEK_SET
);
189 if (sec
) free( sec
);
190 set_error( STATUS_INVALID_FILE_FOR_SECTION
);
195 static struct object
*create_mapping( int size_high
, int size_low
, int protect
,
196 int handle
, const WCHAR
*name
, size_t len
)
198 struct mapping
*mapping
;
201 if (!page_mask
) init_page_size();
203 if (!(mapping
= create_named_object( &mapping_ops
, name
, len
)))
205 if (get_error() == STATUS_OBJECT_NAME_COLLISION
)
206 return &mapping
->obj
; /* Nothing else to do */
208 mapping
->header_size
= 0;
209 mapping
->base
= NULL
;
210 mapping
->shared_file
= NULL
;
211 mapping
->shared_size
= 0;
213 if (protect
& VPROT_READ
) access
|= GENERIC_READ
;
214 if (protect
& VPROT_WRITE
) access
|= GENERIC_WRITE
;
218 if (!(mapping
->file
= get_file_obj( current
->process
, handle
, access
))) goto error
;
219 if (protect
& VPROT_IMAGE
)
221 if (!get_image_params( mapping
)) goto error
;
222 return &mapping
->obj
;
224 if (!size_high
&& !size_low
)
226 struct get_file_info_request req
;
227 struct object
*obj
= (struct object
*)mapping
->file
;
228 obj
->ops
->get_file_info( obj
, &req
);
229 size_high
= req
.size_high
;
230 size_low
= ROUND_SIZE( 0, req
.size_low
);
232 else if (!grow_file( mapping
->file
, size_high
, size_low
)) goto error
;
234 else /* Anonymous mapping (no associated file) */
236 if ((!size_high
&& !size_low
) || (protect
& VPROT_IMAGE
))
238 set_error( STATUS_INVALID_PARAMETER
);
239 mapping
->file
= NULL
;
242 if (!(mapping
->file
= create_temp_file( access
))) goto error
;
243 if (!grow_file( mapping
->file
, size_high
, size_low
)) goto error
;
245 mapping
->size_high
= size_high
;
246 mapping
->size_low
= ROUND_SIZE( 0, size_low
);
247 mapping
->protect
= protect
;
248 return &mapping
->obj
;
251 release_object( mapping
);
255 static void mapping_dump( struct object
*obj
, int verbose
)
257 struct mapping
*mapping
= (struct mapping
*)obj
;
258 assert( obj
->ops
== &mapping_ops
);
259 fprintf( stderr
, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
260 "shared_file=%p shared_size=%08x ",
261 mapping
->size_high
, mapping
->size_low
, mapping
->protect
, mapping
->file
,
262 mapping
->header_size
, mapping
->base
, mapping
->shared_file
, mapping
->shared_size
);
263 dump_object_name( &mapping
->obj
);
264 fputc( '\n', stderr
);
267 static void mapping_destroy( struct object
*obj
)
269 struct mapping
*mapping
= (struct mapping
*)obj
;
270 assert( obj
->ops
== &mapping_ops
);
271 if (mapping
->file
) release_object( mapping
->file
);
272 if (mapping
->shared_file
) release_object( mapping
->shared_file
);
275 int get_page_size(void)
277 if (!page_mask
) init_page_size();
278 return page_mask
+ 1;
281 /* create a file mapping */
282 DECL_HANDLER(create_mapping
)
284 size_t len
= get_req_strlenW( req
, req
->name
);
288 if ((obj
= create_mapping( req
->size_high
, req
->size_low
,
289 req
->protect
, req
->file_handle
, req
->name
, len
)))
291 int access
= FILE_MAP_ALL_ACCESS
;
292 if (!(req
->protect
& VPROT_WRITE
)) access
&= ~FILE_MAP_WRITE
;
293 req
->handle
= alloc_handle( current
->process
, obj
, access
, req
->inherit
);
294 release_object( obj
);
298 /* open a handle to a mapping */
299 DECL_HANDLER(open_mapping
)
301 size_t len
= get_req_strlenW( req
, req
->name
);
302 req
->handle
= open_object( req
->name
, len
, &mapping_ops
, req
->access
, req
->inherit
);
305 /* get a mapping information */
306 DECL_HANDLER(get_mapping_info
)
308 struct mapping
*mapping
;
310 if ((mapping
= (struct mapping
*)get_handle_obj( current
->process
, req
->handle
,
313 req
->size_high
= mapping
->size_high
;
314 req
->size_low
= mapping
->size_low
;
315 req
->protect
= mapping
->protect
;
316 req
->header_size
= mapping
->header_size
;
317 req
->base
= mapping
->base
;
318 req
->shared_file
= -1;
319 req
->shared_size
= mapping
->shared_size
;
320 if (mapping
->shared_file
)
321 req
->shared_file
= alloc_handle( current
->process
, mapping
->shared_file
,
322 GENERIC_READ
|GENERIC_WRITE
, 0 );
323 if (mapping
->file
) set_reply_fd( current
, file_get_mmap_fd( mapping
->file
) );
324 release_object( mapping
);