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