2009-04-03 Miguel de Icaza <miguel@novell.com>
[mono.git] / mono / utils / mono-mmap.c
1 /*
2  * mono-mmap.c: Support for mapping code into the process address space
3  *
4  * Author:
5  *   Mono Team (mono-list@lists.ximian.com)
6  *
7  * Copyright 2001-2008 Novell, Inc.
8  */
9
10 #include "config.h"
11
12 #ifdef PLATFORM_WIN32
13 #include <windows.h>
14 #include <io.h>
15 #else
16 #include <sys/types.h>
17 #if HAVE_SYS_STAT_H
18 #include <sys/stat.h>
19 #endif
20 #if HAVE_SYS_MMAN_H
21 #include <sys/mman.h>
22 #endif
23 #include <fcntl.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <errno.h>
29 #endif
30
31 #include "mono-mmap.h"
32 #include "mono-proclib.h"
33
34 #ifndef MAP_ANONYMOUS
35 #define MAP_ANONYMOUS MAP_ANON
36 #endif
37
38 #ifndef MAP_32BIT
39 #define MAP_32BIT 0
40 #endif
41
42 typedef struct {
43         int size;
44         int pid;
45         int reserved;
46         short stats_start;
47         short stats_end;
48 } SAreaHeader;
49
50 static void* malloced_shared_area = NULL;
51
52 static void*
53 malloc_shared_area (int pid)
54 {
55         int size = mono_pagesize ();
56         SAreaHeader *sarea = g_malloc0 (size);
57         sarea->size = size;
58         sarea->pid = pid;
59         sarea->stats_start = sizeof (SAreaHeader);
60         sarea->stats_end = sizeof (SAreaHeader);
61
62         return sarea;
63 }
64
65 #ifdef PLATFORM_WIN32
66
67 int
68 mono_pagesize (void)
69 {
70         SYSTEM_INFO info;
71         static int saved_pagesize = 0;
72         if (saved_pagesize)
73                 return saved_pagesize;
74         GetSystemInfo (&info);
75         saved_pagesize = info.dwAllocationGranularity;
76         return saved_pagesize;
77 }
78
79 static int
80 prot_from_flags (int flags)
81 {
82         int prot = flags & (MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
83         switch (prot) {
84         case 0: prot = PAGE_NOACCESS; break;
85         case MONO_MMAP_READ: prot = PAGE_READONLY; break;
86         case MONO_MMAP_READ|MONO_MMAP_EXEC: prot = PAGE_EXECUTE_READ; break;
87         case MONO_MMAP_READ|MONO_MMAP_WRITE: prot = PAGE_READWRITE; break;
88         case MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC: prot = PAGE_EXECUTE_READWRITE; break;
89         case MONO_MMAP_WRITE: prot = PAGE_READWRITE; break;
90         case MONO_MMAP_WRITE|MONO_MMAP_EXEC: prot = PAGE_EXECUTE_READWRITE; break;
91         case MONO_MMAP_EXEC: prot = PAGE_EXECUTE; break;
92         default:
93                 g_assert_not_reached ();
94         }
95         return prot;
96 }
97
98 void*
99 mono_valloc (void *addr, size_t length, int flags)
100 {
101         void *ptr;
102         int mflags = MEM_COMMIT;
103         int prot = prot_from_flags (flags);
104         /* translate the flags */
105
106         ptr = VirtualAlloc (addr, length, mflags, prot);
107         return ptr;
108 }
109
110 int
111 mono_vfree (void *addr, size_t length)
112 {
113         int res = VirtualFree (addr, 0, MEM_RELEASE);
114
115         g_assert (res);
116
117         return 0;
118 }
119
120 void*
121 mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
122 {
123         void *ptr;
124         int mflags = 0;
125         HANDLE file, mapping;
126         int prot = prot_from_flags (flags);
127         /* translate the flags */
128         /*if (flags & MONO_MMAP_PRIVATE)
129                 mflags |= MAP_PRIVATE;
130         if (flags & MONO_MMAP_SHARED)
131                 mflags |= MAP_SHARED;
132         if (flags & MONO_MMAP_ANON)
133                 mflags |= MAP_ANONYMOUS;
134         if (flags & MONO_MMAP_FIXED)
135                 mflags |= MAP_FIXED;
136         if (flags & MONO_MMAP_32BIT)
137                 mflags |= MAP_32BIT;*/
138
139         mflags = FILE_MAP_READ;
140         if (flags & MONO_MMAP_WRITE)
141                 mflags = FILE_MAP_COPY;
142
143         file = (HANDLE) _get_osfhandle (fd);
144         mapping = CreateFileMapping (file, NULL, prot, 0, 0, NULL);
145         if (mapping == NULL)
146                 return NULL;
147         ptr = MapViewOfFile (mapping, mflags, 0, offset, length);
148         if (ptr == NULL) {
149                 CloseHandle (mapping);
150                 return NULL;
151         }
152         *ret_handle = (void*)mapping;
153         return ptr;
154 }
155
156 int
157 mono_file_unmap (void *addr, void *handle)
158 {
159         UnmapViewOfFile (addr);
160         CloseHandle ((HANDLE)handle);
161         return 0;
162 }
163
164 int
165 mono_mprotect (void *addr, size_t length, int flags)
166 {
167         DWORD oldprot;
168         int prot = prot_from_flags (flags);
169
170         if (flags & MONO_MMAP_DISCARD) {
171                 VirtualFree (addr, length, MEM_DECOMMIT);
172                 VirtualAlloc (addr, length, MEM_COMMIT, prot);
173                 return 0;
174         }
175         return VirtualProtect (addr, length, prot, &oldprot) == 0;
176 }
177
178 void*
179 mono_shared_area (void)
180 {
181         /* get the pid here */
182         return malloc_shared_area (0);
183 }
184
185 void
186 mono_shared_area_remove (void)
187 {
188         if (malloced_shared_area)
189                 g_free (malloced_shared_area);
190 }
191
192 void*
193 mono_shared_area_for_pid (void *pid)
194 {
195         return NULL;
196 }
197
198 void
199 mono_shared_area_unload (void *area)
200 {
201 }
202
203 int
204 mono_shared_area_instances (void **array, int count)
205 {
206         return 0;
207 }
208
209 #else
210 #if defined(HAVE_MMAP)
211
212 /**
213  * mono_pagesize:
214  * Get the page size in use on the system. Addresses and sizes in the
215  * mono_mmap(), mono_munmap() and mono_mprotect() calls must be pagesize
216  * aligned.
217  *
218  * Returns: the page size in bytes.
219  */
220 int
221 mono_pagesize (void)
222 {
223         static int saved_pagesize = 0;
224         if (saved_pagesize)
225                 return saved_pagesize;
226         saved_pagesize = getpagesize ();
227         return saved_pagesize;
228 }
229
230 static int
231 prot_from_flags (int flags)
232 {
233         int prot = PROT_NONE;
234         /* translate the protection bits */
235         if (flags & MONO_MMAP_READ)
236                 prot |= PROT_READ;
237         if (flags & MONO_MMAP_WRITE)
238                 prot |= PROT_WRITE;
239         if (flags & MONO_MMAP_EXEC)
240                 prot |= PROT_EXEC;
241         return prot;
242 }
243
244 /**
245  * mono_valloc:
246  * @addr: memory address
247  * @length: memory area size
248  * @flags: protection flags
249  *
250  * Allocates @length bytes of virtual memory with the @flags
251  * protection. @addr can be a preferred memory address or a
252  * mandatory one if MONO_MMAP_FIXED is set in @flags.
253  * @addr must be pagesize aligned and can be NULL.
254  * @length must be a multiple of pagesize.
255  *
256  * Returns: NULL on failure, the address of the memory area otherwise
257  */
258 void*
259 mono_valloc (void *addr, size_t length, int flags)
260 {
261         void *ptr;
262         int mflags = 0;
263         int prot = prot_from_flags (flags);
264         /* translate the flags */
265         if (flags & MONO_MMAP_FIXED)
266                 mflags |= MAP_FIXED;
267         if (flags & MONO_MMAP_32BIT)
268                 mflags |= MAP_32BIT;
269
270         mflags |= MAP_ANONYMOUS;
271         mflags |= MAP_PRIVATE;
272
273         ptr = mmap (addr, length, prot, mflags, -1, 0);
274         if (ptr == (void*)-1) {
275                 int fd = open ("/dev/zero", O_RDONLY);
276                 if (fd != -1) {
277                         ptr = mmap (addr, length, prot, mflags, fd, 0);
278                         close (fd);
279                 }
280                 if (ptr == (void*)-1)
281                         return NULL;
282         }
283         return ptr;
284 }
285
286 /**
287  * mono_vfree:
288  * @addr: memory address returned by mono_valloc ()
289  * @length: size of memory area
290  *
291  * Remove the memory mapping at the address @addr.
292  *
293  * Returns: 0 on success.
294  */
295 int
296 mono_vfree (void *addr, size_t length)
297 {
298         return munmap (addr, length);
299 }
300
301 /**
302  * mono_file_map:
303  * @length: size of data to map
304  * @flags: protection flags
305  * @fd: file descriptor
306  * @offset: offset in the file
307  * @ret_handle: pointer to storage for returning a handle for the map
308  *
309  * Map the area of the file pointed to by the file descriptor @fd, at offset
310  * @offset and of size @length in memory according to the protection flags
311  * @flags.
312  * @offset and @length must be multiples of the page size.
313  * @ret_handle must point to a void*: this value must be used when unmapping
314  * the memory area using mono_file_unmap ().
315  *
316  */
317 void*
318 mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
319 {
320         void *ptr;
321         int mflags = 0;
322         int prot = prot_from_flags (flags);
323         /* translate the flags */
324         if (flags & MONO_MMAP_PRIVATE)
325                 mflags |= MAP_PRIVATE;
326         if (flags & MONO_MMAP_SHARED)
327                 mflags |= MAP_SHARED;
328         if (flags & MONO_MMAP_FIXED)
329                 mflags |= MAP_FIXED;
330         if (flags & MONO_MMAP_32BIT)
331                 mflags |= MAP_32BIT;
332
333         ptr = mmap (0, length, prot, mflags, fd, offset);
334         if (ptr == (void*)-1)
335                 return NULL;
336         *ret_handle = (void*)length;
337         return ptr;
338 }
339
340 /**
341  * mono_file_unmap:
342  * @addr: memory address returned by mono_file_map ()
343  * @handle: handle of memory map
344  *
345  * Remove the memory mapping at the address @addr.
346  * @handle must be the value returned in ret_handle by mono_file_map ().
347  *
348  * Returns: 0 on success.
349  */
350 int
351 mono_file_unmap (void *addr, void *handle)
352 {
353         return munmap (addr, (size_t)handle);
354 }
355
356 /**
357  * mono_mprotect:
358  * @addr: memory address
359  * @length: size of memory area
360  * @flags: new protection flags
361  *
362  * Change the protection for the memory area at @addr for @length bytes
363  * to matche the supplied @flags.
364  * If @flags includes MON_MMAP_DISCARD the pages are discarded from memory
365  * and the area is cleared to zero.
366  * @addr must be aligned to the page size.
367  * @length must be a multiple of the page size.
368  *
369  * Returns: 0 on success.
370  */
371 int
372 mono_mprotect (void *addr, size_t length, int flags)
373 {
374         int prot = prot_from_flags (flags);
375
376         if (flags & MONO_MMAP_DISCARD) {
377                 /* on non-linux the pages are not guaranteed to be zeroed (*bsd, osx at least) */
378 #ifdef __linux__
379                 if (madvise (addr, length, MADV_DONTNEED))
380                         memset (addr, 0, length);
381 #else
382                 memset (addr, 0, length);
383                 madvise (addr, length, MADV_DONTNEED);
384                 madvise (addr, length, MADV_FREE);
385 #endif
386         }
387         return mprotect (addr, length, prot);
388 }
389
390 #else
391
392 /* dummy malloc-based implementation */
393 int
394 mono_pagesize (void)
395 {
396         return 4096;
397 }
398
399 void*
400 mono_valloc (void *addr, size_t length, int flags)
401 {
402         return malloc (length);
403 }
404
405 int
406 mono_vfree (void *addr, size_t length)
407 {
408         free (addr);
409         return 0;
410 }
411
412 int
413 mono_mprotect (void *addr, size_t length, int flags)
414 {
415         if (flags & MONO_MMAP_DISCARD) {
416                 memset (addr, 0, length);
417         }
418         return 0;
419 }
420 #endif // HAVE_MMAP
421
422 #if defined(HAVE_SHM_OPEN)
423
424 static int
425 mono_shared_area_instances_slow (void **array, int count, gboolean cleanup)
426 {
427         int i, j = 0;
428         int num;
429         void *data;
430         gpointer *processes = mono_process_list (&num);
431         for (i = 0; i < num; ++i) {
432                 data = mono_shared_area_for_pid (processes [i]);
433                 if (!data)
434                         continue;
435                 mono_shared_area_unload (data);
436                 if (!cleanup) {
437                         if (j < count)
438                                 array [j++] = processes [i];
439                         else
440                                 break;
441                 }
442         }
443         g_free (processes);
444         return j;
445 }
446
447 static int
448 mono_shared_area_instances_helper (void **array, int count, gboolean cleanup)
449 {
450         const char *name;
451         int i = 0;
452         int curpid = getpid ();
453         GDir *dir = g_dir_open ("/dev/shm/", 0, NULL);
454         if (!dir)
455                 return mono_shared_area_instances_slow (array, count, cleanup);
456         while ((name = g_dir_read_name (dir))) {
457                 int pid;
458                 char *nend;
459                 if (strncmp (name, "mono.", 5))
460                         continue;
461                 pid = strtol (name + 5, &nend, 10);
462                 if (pid <= 0 || nend == name + 5 || *nend)
463                         continue;
464                 if (!cleanup) {
465                         if (i < count)
466                                 array [i++] = GINT_TO_POINTER (pid);
467                         else
468                                 break;
469                 }
470                 if (curpid != pid && kill (pid, SIGCONT) == -1 && errno == ESRCH) {
471                         char buf [128];
472                         g_snprintf (buf, sizeof (buf), "/mono.%d", pid);
473                         shm_unlink (buf);
474                 }
475         }
476         g_dir_close (dir);
477         return i;
478 }
479
480 void*
481 mono_shared_area (void)
482 {
483         int fd;
484         int pid = getpid ();
485         /* we should allow the user to configure the size */
486         int size = mono_pagesize ();
487         char buf [128];
488         void *res;
489         SAreaHeader *header;
490
491         /* perform cleanup of segments left over from dead processes */
492         mono_shared_area_instances_helper (NULL, 0, TRUE);
493
494         g_snprintf (buf, sizeof (buf), "/mono.%d", pid);
495
496         fd = shm_open (buf, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
497         if (fd == -1 && errno == EEXIST) {
498                 /* leftover */
499                 shm_unlink (buf);
500                 fd = shm_open (buf, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
501         }
502         /* in case of failure we try to return a memory area anyway,
503          * even if it means the data can't be read by other processes
504          */
505         if (fd == -1)
506                 return malloc_shared_area (pid);
507         if (ftruncate (fd, size) != 0) {
508                 shm_unlink (buf);
509                 close (fd);
510         }
511         res = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
512         if (res == MAP_FAILED) {
513                 shm_unlink (buf);
514                 close (fd);
515                 return malloc_shared_area (pid);
516         }
517         /* we don't need the file descriptor anymore */
518         close (fd);
519         header = res;
520         header->size = size;
521         header->pid = pid;
522         header->stats_start = sizeof (SAreaHeader);
523         header->stats_end = sizeof (SAreaHeader);
524
525         atexit (mono_shared_area_remove);
526         return res;
527 }
528
529 void
530 mono_shared_area_remove (void)
531 {
532         char buf [128];
533         g_snprintf (buf, sizeof (buf), "/mono.%d", getpid ());
534         shm_unlink (buf);
535         if (malloced_shared_area)
536                 g_free (malloced_shared_area);
537 }
538
539 void*
540 mono_shared_area_for_pid (void *pid)
541 {
542         int fd;
543         /* we should allow the user to configure the size */
544         int size = mono_pagesize ();
545         char buf [128];
546         void *res;
547
548         g_snprintf (buf, sizeof (buf), "/mono.%d", GPOINTER_TO_INT (pid));
549
550         fd = shm_open (buf, O_RDONLY, S_IRUSR|S_IRGRP);
551         if (fd == -1)
552                 return NULL;
553         res = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
554         if (res == MAP_FAILED) {
555                 close (fd);
556                 return NULL;
557         }
558         /* FIXME: validate the area */
559         /* we don't need the file descriptor anymore */
560         close (fd);
561         return res;
562 }
563
564 void
565 mono_shared_area_unload  (void *area)
566 {
567         /* FIXME: currently we load only a page */
568         munmap (area, mono_pagesize ());
569 }
570
571 int
572 mono_shared_area_instances (void **array, int count)
573 {
574         return mono_shared_area_instances_helper (array, count, FALSE);
575 }
576 #else
577 void*
578 mono_shared_area (void)
579 {
580         return malloc_shared_area (getpid ());
581 }
582
583 void
584 mono_shared_area_remove (void)
585 {
586         if (malloced_shared_area)
587                 g_free (malloced_shared_area);
588         malloced_shared_area = NULL;
589 }
590
591 void*
592 mono_shared_area_for_pid (void *pid)
593 {
594         return NULL;
595 }
596
597 void
598 mono_shared_area_unload (void *area)
599 {
600 }
601
602 int
603 mono_shared_area_instances (void **array, int count)
604 {
605         return 0;
606 }
607
608 #endif // HAVE_SHM_OPEN
609
610 #endif // PLATFORM_WIN32