More general approach to file descriptor types and flags.
[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_reply *reply, int *flags );
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     NULL,                        /* queue_async */
55     mapping_destroy              /* destroy */
56 };
57
58 static struct mapping *shared_first;
59
60 #ifdef __i386__
61
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)
66
67 #else  /* __i386__ */
68
69 static int page_shift, page_mask;
70
71 static void init_page_size(void)
72 {
73     int page_size;
74 # ifdef HAVE_GETPAGESIZE
75     page_size = getpagesize();
76 # else
77 #  ifdef __svr4__
78     page_size = sysconf(_SC_PAGESIZE);
79 #  else
80 #   error Cannot get the page size on this platform
81 #  endif
82 # endif
83     page_mask = page_size - 1;
84     /* Make sure we have a power of 2 */
85     assert( !(page_size & page_mask) );
86     page_shift = 0;
87     while ((1 << page_shift) != page_size) page_shift++;
88 }
89 #endif  /* __i386__ */
90
91 #define ROUND_ADDR(addr) \
92    ((int)(addr) & ~page_mask)
93
94 #define ROUND_SIZE(addr,size) \
95    (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
96
97 /* get the fd to use for mmaping a file */
98 inline static int get_mmap_fd( struct file *file )
99 {
100     struct object *obj;
101     if (!(obj = (struct object *)file))
102     {
103         set_error( STATUS_INVALID_HANDLE );
104         return -1;
105     }
106     return obj->ops->get_fd( obj );
107 }
108
109 /* find the shared PE mapping for a given mapping */
110 static struct file *get_shared_file( struct mapping *mapping )
111 {
112     struct mapping *ptr;
113
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 );
117     return NULL;
118 }
119
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 )
123 {
124     int i, max_size, total_size, pos;
125     char *buffer = NULL;
126     int shared_fd;
127     long toread;
128
129     /* compute the total size of the shared mapping */
130
131     total_size = max_size = 0;
132     for (i = 0; i < nb_sec; i++)
133     {
134         if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
135             (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
136         {
137             int size = ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
138             if (size > max_size) max_size = size;
139             total_size += size;
140         }
141     }
142     if (!(mapping->shared_size = total_size)) return 1;  /* nothing to do */
143
144     if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
145
146     /* create a temp file for the mapping */
147
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;
151
152     if (!(buffer = malloc( max_size ))) goto error;
153
154     /* copy the shared sections data into the temp file */
155
156     for (i = pos = 0; i < nb_sec; i++)
157     {
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;
166         while (toread)
167         {
168             long res = read( fd, buffer + sec[i].SizeOfRawData - toread, toread );
169             if (res <= 0) goto error;
170             toread -= res;
171         }
172         if (write( shared_fd, buffer, sec[i].SizeOfRawData ) != sec[i].SizeOfRawData) goto error;
173     }
174     free( buffer );
175     return 1;
176
177  error:
178     if (buffer) free( buffer );
179     return 0;
180 }
181
182 /* retrieve the mapping parameters for an executable (PE) image */
183 static int get_image_params( struct mapping *mapping )
184 {
185     IMAGE_DOS_HEADER dos;
186     IMAGE_NT_HEADERS nt;
187     IMAGE_SECTION_HEADER *sec = NULL;
188     int fd, filepos, size;
189
190     /* load the headers */
191
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;
199
200     /* load the section headers */
201
202     size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
203     if (!(sec = malloc( size ))) goto error;
204     if (read( fd, sec, size ) != size) goto error;
205
206     if (!build_shared_mapping( mapping, fd, sec, nt.FileHeader.NumberOfSections )) goto error;
207
208     if (mapping->shared_file)  /* link it in the list */
209     {
210         if ((mapping->shared_next = shared_first)) shared_first->shared_prev = mapping;
211         mapping->shared_prev = NULL;
212         shared_first = mapping;
213     }
214
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;
220
221     /* sanity check */
222     if (mapping->header_size > mapping->size_low) goto error;
223
224     lseek( fd, filepos, SEEK_SET );
225     free( sec );
226     return 1;
227
228  error:
229     lseek( fd, filepos, SEEK_SET );
230     if (sec) free( sec );
231     set_error( STATUS_INVALID_FILE_FOR_SECTION );
232     return 0;
233 }
234
235
236 static struct object *create_mapping( int size_high, int size_low, int protect,
237                                       handle_t handle, const WCHAR *name, size_t len )
238 {
239     struct mapping *mapping;
240     int access = 0;
241
242     if (!page_mask) init_page_size();
243
244     if (!(mapping = create_named_object( &mapping_ops, name, len )))
245         return NULL;
246     if (get_error() == STATUS_OBJECT_NAME_COLLISION)
247         return &mapping->obj;  /* Nothing else to do */
248
249     mapping->header_size = 0;
250     mapping->base        = NULL;
251     mapping->shared_file = NULL;
252     mapping->shared_size = 0;
253
254     if (protect & VPROT_READ) access |= GENERIC_READ;
255     if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
256
257     if (handle)
258     {
259         if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
260         if (protect & VPROT_IMAGE)
261         {
262             if (!get_image_params( mapping )) goto error;
263             return &mapping->obj;
264         }
265         if (!size_high && !size_low)
266         {
267             int flags;
268             struct get_file_info_reply reply;
269             struct object *obj = (struct object *)mapping->file;
270             obj->ops->get_file_info( obj, &reply, &flags );
271             size_high = reply.size_high;
272             size_low  = ROUND_SIZE( 0, reply.size_low );
273         }
274         else if (!grow_file( mapping->file, size_high, size_low )) goto error;
275     }
276     else  /* Anonymous mapping (no associated file) */
277     {
278         if ((!size_high && !size_low) || (protect & VPROT_IMAGE))
279         {
280             set_error( STATUS_INVALID_PARAMETER );
281             mapping->file = NULL;
282             goto error;
283         }
284         if (!(mapping->file = create_temp_file( access ))) goto error;
285         if (!grow_file( mapping->file, size_high, size_low )) goto error;
286     }
287     mapping->size_high = size_high;
288     mapping->size_low  = ROUND_SIZE( 0, size_low );
289     mapping->protect   = protect;
290     return &mapping->obj;
291
292  error:
293     release_object( mapping );
294     return NULL;
295 }
296
297 static void mapping_dump( struct object *obj, int verbose )
298 {
299     struct mapping *mapping = (struct mapping *)obj;
300     assert( obj->ops == &mapping_ops );
301     fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
302              "shared_file=%p shared_size=%08x ",
303              mapping->size_high, mapping->size_low, mapping->protect, mapping->file,
304              mapping->header_size, mapping->base, mapping->shared_file, mapping->shared_size );
305     dump_object_name( &mapping->obj );
306     fputc( '\n', stderr );
307 }
308
309 static int mapping_get_fd( struct object *obj )
310 {
311     struct mapping *mapping = (struct mapping *)obj;
312     assert( obj->ops == &mapping_ops );
313     return get_mmap_fd( mapping->file );
314 }
315
316 static int mapping_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
317 {
318     struct mapping *mapping = (struct mapping *)obj;
319     struct object *file = (struct object *)mapping->file;
320
321     assert( obj->ops == &mapping_ops );
322     assert( file );
323     return file->ops->get_file_info( file, reply, flags );
324 }
325
326 static void mapping_destroy( struct object *obj )
327 {
328     struct mapping *mapping = (struct mapping *)obj;
329     assert( obj->ops == &mapping_ops );
330     if (mapping->file) release_object( mapping->file );
331     if (mapping->shared_file)
332     {
333         release_object( mapping->shared_file );
334         if (mapping->shared_next) mapping->shared_next->shared_prev = mapping->shared_prev;
335         if (mapping->shared_prev) mapping->shared_prev->shared_next = mapping->shared_next;
336         else shared_first = mapping->shared_next;
337     }
338 }
339
340 int get_page_size(void)
341 {
342     if (!page_mask) init_page_size();
343     return page_mask + 1;
344 }
345
346 /* create a file mapping */
347 DECL_HANDLER(create_mapping)
348 {
349     struct object *obj;
350
351     reply->handle = 0;
352     if ((obj = create_mapping( req->size_high, req->size_low,
353                                req->protect, req->file_handle,
354                                get_req_data(), get_req_data_size() )))
355     {
356         int access = FILE_MAP_ALL_ACCESS;
357         if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
358         reply->handle = alloc_handle( current->process, obj, access, req->inherit );
359         release_object( obj );
360     }
361 }
362
363 /* open a handle to a mapping */
364 DECL_HANDLER(open_mapping)
365 {
366     reply->handle = open_object( get_req_data(), get_req_data_size(),
367                                  &mapping_ops, req->access, req->inherit );
368 }
369
370 /* get a mapping information */
371 DECL_HANDLER(get_mapping_info)
372 {
373     struct mapping *mapping;
374
375     if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
376                                                      0, &mapping_ops )))
377     {
378         reply->size_high   = mapping->size_high;
379         reply->size_low    = mapping->size_low;
380         reply->protect     = mapping->protect;
381         reply->header_size = mapping->header_size;
382         reply->base        = mapping->base;
383         reply->shared_file = 0;
384         reply->shared_size = mapping->shared_size;
385         reply->drive_type  = get_file_drive_type( mapping->file );
386         if (mapping->shared_file)
387             reply->shared_file = alloc_handle( current->process, mapping->shared_file,
388                                                GENERIC_READ|GENERIC_WRITE, 0 );
389         release_object( mapping );
390     }
391 }