When including config.h, include it first.
[wine] / server / mapping.c
1 /*
2  * Server-side file mapping management
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13
14 #include "winnt.h"
15 #include "winbase.h"
16
17 #include "handle.h"
18 #include "thread.h"
19 #include "request.h"
20
21 struct mapping
22 {
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 */
34 };
35
36 static int mapping_get_fd( struct object *obj );
37 static int mapping_get_info( struct object *obj, struct get_file_info_request *req );
38 static void mapping_dump( struct object *obj, int verbose );
39 static void mapping_destroy( struct object *obj );
40
41 static const struct object_ops mapping_ops =
42 {
43     sizeof(struct mapping),      /* size */
44     mapping_dump,                /* dump */
45     no_add_queue,                /* add_queue */
46     NULL,                        /* remove_queue */
47     NULL,                        /* signaled */
48     NULL,                        /* satisfied */
49     NULL,                        /* get_poll_events */
50     NULL,                        /* poll_event */
51     mapping_get_fd,              /* get_fd */
52     no_flush,                    /* flush */
53     mapping_get_info,            /* get_file_info */
54     mapping_destroy              /* destroy */
55 };
56
57 static struct mapping *shared_first;
58
59 #ifdef __i386__
60
61 /* These are always the same on an i386, and it will be faster this way */
62 # define page_mask  0xfff
63 # define page_shift 12
64 # define init_page_size() do { /* nothing */ } while(0)
65
66 #else  /* __i386__ */
67
68 static int page_shift, page_mask;
69
70 static void init_page_size(void)
71 {
72     int page_size;
73 # ifdef HAVE_GETPAGESIZE
74     page_size = getpagesize();
75 # else
76 #  ifdef __svr4__
77     page_size = sysconf(_SC_PAGESIZE);
78 #  else
79 #   error Cannot get the page size on this platform
80 #  endif
81 # endif
82     page_mask = page_size - 1;
83     /* Make sure we have a power of 2 */
84     assert( !(page_size & page_mask) );
85     page_shift = 0;
86     while ((1 << page_shift) != page_size) page_shift++;
87 }
88 #endif  /* __i386__ */
89
90 #define ROUND_ADDR(addr) \
91    ((int)(addr) & ~page_mask)
92
93 #define ROUND_SIZE(addr,size) \
94    (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
95
96 /* get the fd to use for mmaping a file */
97 inline static int get_mmap_fd( struct file *file )
98 {
99     struct object *obj;
100     if (!(obj = (struct object *)file))
101     {
102         set_error( STATUS_INVALID_HANDLE );
103         return -1;
104     }
105     return obj->ops->get_fd( obj );
106 }
107
108 /* find the shared PE mapping for a given mapping */
109 static struct file *get_shared_file( struct mapping *mapping )
110 {
111     struct mapping *ptr;
112
113     for (ptr = shared_first; ptr; ptr = ptr->shared_next)
114         if (is_same_file( ptr->file, mapping->file ))
115             return (struct file *)grab_object( ptr->shared_file );
116     return NULL;
117 }
118
119 /* allocate and fill the temp file for a shared PE image mapping */
120 static int build_shared_mapping( struct mapping *mapping, int fd,
121                                  IMAGE_SECTION_HEADER *sec, int nb_sec )
122 {
123     int i, max_size, total_size, pos;
124     char *buffer = NULL;
125     int shared_fd;
126     long toread;
127
128     /* compute the total size of the shared mapping */
129
130     total_size = max_size = 0;
131     for (i = 0; i < nb_sec; i++)
132     {
133         if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
134             (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
135         {
136             int size = ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
137             if (size > max_size) max_size = size;
138             total_size += size;
139         }
140     }
141     if (!(mapping->shared_size = total_size)) return 1;  /* nothing to do */
142
143     if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
144
145     /* create a temp file for the mapping */
146
147     if (!(mapping->shared_file = create_temp_file( GENERIC_READ|GENERIC_WRITE ))) goto error;
148     if (!grow_file( mapping->shared_file, 0, total_size )) goto error;
149     if ((shared_fd = get_mmap_fd( mapping->shared_file )) == -1) goto error;
150
151     if (!(buffer = malloc( max_size ))) goto error;
152
153     /* copy the shared sections data into the temp file */
154
155     for (i = pos = 0; i < nb_sec; i++)
156     {
157         if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
158         if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
159         if (lseek( shared_fd, pos, SEEK_SET ) != pos) goto error;
160         pos += ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
161         if (sec[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) continue;
162         if (!sec[i].PointerToRawData || !sec[i].SizeOfRawData) continue;
163         if (lseek( fd, sec[i].PointerToRawData, SEEK_SET ) != sec[i].PointerToRawData) goto error;
164         toread = sec[i].SizeOfRawData;
165         while (toread)
166         {
167             long res = read( fd, buffer + sec[i].SizeOfRawData - toread, toread );
168             if (res <= 0) goto error;
169             toread -= res;
170         }
171         if (write( shared_fd, buffer, sec[i].SizeOfRawData ) != sec[i].SizeOfRawData) goto error;
172     }
173     free( buffer );
174     return 1;
175
176  error:
177     if (buffer) free( buffer );
178     return 0;
179 }
180
181 /* retrieve the mapping parameters for an executable (PE) image */
182 static int get_image_params( struct mapping *mapping )
183 {
184     IMAGE_DOS_HEADER dos;
185     IMAGE_NT_HEADERS nt;
186     IMAGE_SECTION_HEADER *sec = NULL;
187     int fd, filepos, size;
188
189     /* load the headers */
190
191     if ((fd = get_mmap_fd( mapping->file )) == -1) return 0;
192     filepos = lseek( fd, 0, SEEK_SET );
193     if (read( fd, &dos, sizeof(dos) ) != sizeof(dos)) goto error;
194     if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
195     if (lseek( fd, dos.e_lfanew, SEEK_SET ) == -1) goto error;
196     if (read( fd, &nt, sizeof(nt) ) != sizeof(nt)) goto error;
197     if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
198
199     /* load the section headers */
200
201     size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
202     if (!(sec = malloc( size ))) goto error;
203     if (read( fd, sec, size ) != size) goto error;
204
205     if (!build_shared_mapping( mapping, fd, sec, nt.FileHeader.NumberOfSections )) goto error;
206
207     if (mapping->shared_file)  /* link it in the list */
208     {
209         if ((mapping->shared_next = shared_first)) shared_first->shared_prev = mapping;
210         mapping->shared_prev = NULL;
211         shared_first = mapping;
212     }
213
214     mapping->size_low    = ROUND_SIZE( 0, nt.OptionalHeader.SizeOfImage );
215     mapping->size_high   = 0;
216     mapping->base        = (void *)nt.OptionalHeader.ImageBase;
217     mapping->header_size = ROUND_SIZE( mapping->base, nt.OptionalHeader.SizeOfHeaders );
218     mapping->protect     = VPROT_IMAGE;
219
220     /* sanity check */
221     if (mapping->header_size > mapping->size_low) goto error;
222
223     lseek( fd, filepos, SEEK_SET );
224     free( sec );
225     return 1;
226
227  error:
228     lseek( fd, filepos, SEEK_SET );
229     if (sec) free( sec );
230     set_error( STATUS_INVALID_FILE_FOR_SECTION );
231     return 0;
232 }
233
234
235 static struct object *create_mapping( int size_high, int size_low, int protect,
236                                       handle_t handle, const WCHAR *name, size_t len )
237 {
238     struct mapping *mapping;
239     int access = 0;
240
241     if (!page_mask) init_page_size();
242
243     if (!(mapping = create_named_object( &mapping_ops, name, len )))
244         return NULL;
245     if (get_error() == STATUS_OBJECT_NAME_COLLISION)
246         return &mapping->obj;  /* Nothing else to do */
247
248     mapping->header_size = 0;
249     mapping->base        = NULL;
250     mapping->shared_file = NULL;
251     mapping->shared_size = 0;
252
253     if (protect & VPROT_READ) access |= GENERIC_READ;
254     if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
255
256     if (handle)
257     {
258         if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
259         if (protect & VPROT_IMAGE)
260         {
261             if (!get_image_params( mapping )) goto error;
262             return &mapping->obj;
263         }
264         if (!size_high && !size_low)
265         {
266             struct get_file_info_request req;
267             struct object *obj = (struct object *)mapping->file;
268             obj->ops->get_file_info( obj, &req );
269             size_high = req.size_high;
270             size_low  = ROUND_SIZE( 0, req.size_low );
271         }
272         else if (!grow_file( mapping->file, size_high, size_low )) goto error;
273     }
274     else  /* Anonymous mapping (no associated file) */
275     {
276         if ((!size_high && !size_low) || (protect & VPROT_IMAGE))
277         {
278             set_error( STATUS_INVALID_PARAMETER );
279             mapping->file = NULL;
280             goto error;
281         }
282         if (!(mapping->file = create_temp_file( access ))) goto error;
283         if (!grow_file( mapping->file, size_high, size_low )) goto error;
284     }
285     mapping->size_high = size_high;
286     mapping->size_low  = ROUND_SIZE( 0, size_low );
287     mapping->protect   = protect;
288     return &mapping->obj;
289
290  error:
291     release_object( mapping );
292     return NULL;
293 }
294
295 static void mapping_dump( struct object *obj, int verbose )
296 {
297     struct mapping *mapping = (struct mapping *)obj;
298     assert( obj->ops == &mapping_ops );
299     fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
300              "shared_file=%p shared_size=%08x ",
301              mapping->size_high, mapping->size_low, mapping->protect, mapping->file,
302              mapping->header_size, mapping->base, mapping->shared_file, mapping->shared_size );
303     dump_object_name( &mapping->obj );
304     fputc( '\n', stderr );
305 }
306
307 static int mapping_get_fd( struct object *obj )
308 {
309     struct mapping *mapping = (struct mapping *)obj;
310     assert( obj->ops == &mapping_ops );
311     return get_mmap_fd( mapping->file );
312 }
313
314 static int mapping_get_info( struct object *obj, struct get_file_info_request *req )
315 {
316     struct mapping *mapping = (struct mapping *)obj;
317     struct object *file = (struct object *)mapping->file;
318
319     assert( obj->ops == &mapping_ops );
320     assert( file );
321     return file->ops->get_file_info( file, req );
322 }
323
324 static void mapping_destroy( struct object *obj )
325 {
326     struct mapping *mapping = (struct mapping *)obj;
327     assert( obj->ops == &mapping_ops );
328     if (mapping->file) release_object( mapping->file );
329     if (mapping->shared_file)
330     {
331         release_object( mapping->shared_file );
332         if (mapping->shared_next) mapping->shared_next->shared_prev = mapping->shared_prev;
333         if (mapping->shared_prev) mapping->shared_prev->shared_next = mapping->shared_next;
334         else shared_first = mapping->shared_next;
335     }
336 }
337
338 int get_page_size(void)
339 {
340     if (!page_mask) init_page_size();
341     return page_mask + 1;
342 }
343
344 /* create a file mapping */
345 DECL_HANDLER(create_mapping)
346 {
347     struct object *obj;
348
349     req->handle = 0;
350     if ((obj = create_mapping( req->size_high, req->size_low,
351                                req->protect, req->file_handle,
352                                get_req_data(req), get_req_data_size(req) )))
353     {
354         int access = FILE_MAP_ALL_ACCESS;
355         if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
356         req->handle = alloc_handle( current->process, obj, access, req->inherit );
357         release_object( obj );
358     }
359 }
360
361 /* open a handle to a mapping */
362 DECL_HANDLER(open_mapping)
363 {
364     req->handle = open_object( get_req_data(req), get_req_data_size(req),
365                                &mapping_ops, req->access, req->inherit );
366 }
367
368 /* get a mapping information */
369 DECL_HANDLER(get_mapping_info)
370 {
371     struct mapping *mapping;
372
373     if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
374                                                      0, &mapping_ops )))
375     {
376         req->size_high   = mapping->size_high;
377         req->size_low    = mapping->size_low;
378         req->protect     = mapping->protect;
379         req->header_size = mapping->header_size;
380         req->base        = mapping->base;
381         req->shared_file = 0;
382         req->shared_size = mapping->shared_size;
383         if (mapping->shared_file)
384             req->shared_file = alloc_handle( current->process, mapping->shared_file,
385                                              GENERIC_READ|GENERIC_WRITE, 0 );
386         release_object( mapping );
387     }
388 }