2 * mono-mmap.c: Support for mapping code into the process address space
5 * Mono Team (mono-list@lists.ximian.com)
7 * Copyright 2001-2008 Novell, Inc.
16 #include <sys/types.h>
31 #include "mono-mmap.h"
32 #include "mono-proclib.h"
35 #define MAP_ANONYMOUS MAP_ANON
50 static void* malloced_shared_area = NULL;
53 malloc_shared_area (int pid)
55 int size = mono_pagesize ();
56 SAreaHeader *sarea = g_malloc0 (size);
59 sarea->stats_start = sizeof (SAreaHeader);
60 sarea->stats_end = sizeof (SAreaHeader);
66 aligned_address (char *mem, size_t size, size_t alignment)
68 char *aligned = (char*)((gulong)(mem + (alignment - 1)) & ~(alignment - 1));
69 g_assert (aligned >= mem && aligned + size <= mem + size + alignment && !((gulong)aligned & (alignment - 1)));
79 static int saved_pagesize = 0;
81 return saved_pagesize;
82 GetSystemInfo (&info);
83 saved_pagesize = info.dwAllocationGranularity;
84 return saved_pagesize;
88 prot_from_flags (int flags)
90 int prot = flags & (MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
92 case 0: prot = PAGE_NOACCESS; break;
93 case MONO_MMAP_READ: prot = PAGE_READONLY; break;
94 case MONO_MMAP_READ|MONO_MMAP_EXEC: prot = PAGE_EXECUTE_READ; break;
95 case MONO_MMAP_READ|MONO_MMAP_WRITE: prot = PAGE_READWRITE; break;
96 case MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC: prot = PAGE_EXECUTE_READWRITE; break;
97 case MONO_MMAP_WRITE: prot = PAGE_READWRITE; break;
98 case MONO_MMAP_WRITE|MONO_MMAP_EXEC: prot = PAGE_EXECUTE_READWRITE; break;
99 case MONO_MMAP_EXEC: prot = PAGE_EXECUTE; break;
101 g_assert_not_reached ();
107 mono_valloc (void *addr, size_t length, int flags)
110 int mflags = MEM_COMMIT;
111 int prot = prot_from_flags (flags);
112 /* translate the flags */
114 ptr = VirtualAlloc (addr, length, mflags, prot);
119 mono_valloc_aligned (size_t length, size_t alignment, int flags)
121 int prot = prot_from_flags (flags);
122 char *mem = VirtualAlloc (NULL, length + alignment, MEM_RESERVE, prot);
128 aligned = aligned_address (mem, length, alignment);
130 aligned = VirtualAlloc (aligned, length, MEM_COMMIT, prot);
136 #define HAVE_VALLOC_ALIGNED
139 mono_vfree (void *addr, size_t length)
141 MEMORY_BASIC_INFORMATION mbi;
142 SIZE_T query_result = VirtualQuery (addr, &mbi, sizeof (mbi));
145 g_assert (query_result);
147 res = VirtualFree (mbi.AllocationBase, 0, MEM_RELEASE);
155 mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
159 HANDLE file, mapping;
160 int prot = prot_from_flags (flags);
161 /* translate the flags */
162 /*if (flags & MONO_MMAP_PRIVATE)
163 mflags |= MAP_PRIVATE;
164 if (flags & MONO_MMAP_SHARED)
165 mflags |= MAP_SHARED;
166 if (flags & MONO_MMAP_ANON)
167 mflags |= MAP_ANONYMOUS;
168 if (flags & MONO_MMAP_FIXED)
170 if (flags & MONO_MMAP_32BIT)
171 mflags |= MAP_32BIT;*/
173 mflags = FILE_MAP_READ;
174 if (flags & MONO_MMAP_WRITE)
175 mflags = FILE_MAP_COPY;
177 file = (HANDLE) _get_osfhandle (fd);
178 mapping = CreateFileMapping (file, NULL, prot, 0, 0, NULL);
181 ptr = MapViewOfFile (mapping, mflags, 0, offset, length);
183 CloseHandle (mapping);
186 *ret_handle = (void*)mapping;
191 mono_file_unmap (void *addr, void *handle)
193 UnmapViewOfFile (addr);
194 CloseHandle ((HANDLE)handle);
199 mono_mprotect (void *addr, size_t length, int flags)
202 int prot = prot_from_flags (flags);
204 if (flags & MONO_MMAP_DISCARD) {
205 VirtualFree (addr, length, MEM_DECOMMIT);
206 VirtualAlloc (addr, length, MEM_COMMIT, prot);
209 return VirtualProtect (addr, length, prot, &oldprot) == 0;
213 mono_shared_area (void)
215 /* get the pid here */
216 return malloc_shared_area (0);
220 mono_shared_area_remove (void)
222 if (malloced_shared_area)
223 g_free (malloced_shared_area);
227 mono_shared_area_for_pid (void *pid)
233 mono_shared_area_unload (void *area)
238 mono_shared_area_instances (void **array, int count)
244 #if defined(HAVE_MMAP)
248 * Get the page size in use on the system. Addresses and sizes in the
249 * mono_mmap(), mono_munmap() and mono_mprotect() calls must be pagesize
252 * Returns: the page size in bytes.
257 static int saved_pagesize = 0;
259 return saved_pagesize;
260 saved_pagesize = getpagesize ();
261 return saved_pagesize;
265 prot_from_flags (int flags)
267 int prot = PROT_NONE;
268 /* translate the protection bits */
269 if (flags & MONO_MMAP_READ)
271 if (flags & MONO_MMAP_WRITE)
273 if (flags & MONO_MMAP_EXEC)
280 * @addr: memory address
281 * @length: memory area size
282 * @flags: protection flags
284 * Allocates @length bytes of virtual memory with the @flags
285 * protection. @addr can be a preferred memory address or a
286 * mandatory one if MONO_MMAP_FIXED is set in @flags.
287 * @addr must be pagesize aligned and can be NULL.
288 * @length must be a multiple of pagesize.
290 * Returns: NULL on failure, the address of the memory area otherwise
293 mono_valloc (void *addr, size_t length, int flags)
297 int prot = prot_from_flags (flags);
298 /* translate the flags */
299 if (flags & MONO_MMAP_FIXED)
301 if (flags & MONO_MMAP_32BIT)
304 mflags |= MAP_ANONYMOUS;
305 mflags |= MAP_PRIVATE;
307 ptr = mmap (addr, length, prot, mflags, -1, 0);
308 if (ptr == (void*)-1) {
309 int fd = open ("/dev/zero", O_RDONLY);
311 ptr = mmap (addr, length, prot, mflags, fd, 0);
314 if (ptr == (void*)-1)
322 * @addr: memory address returned by mono_valloc ()
323 * @length: size of memory area
325 * Remove the memory mapping at the address @addr.
327 * Returns: 0 on success.
330 mono_vfree (void *addr, size_t length)
332 return munmap (addr, length);
337 * @length: size of data to map
338 * @flags: protection flags
339 * @fd: file descriptor
340 * @offset: offset in the file
341 * @ret_handle: pointer to storage for returning a handle for the map
343 * Map the area of the file pointed to by the file descriptor @fd, at offset
344 * @offset and of size @length in memory according to the protection flags
346 * @offset and @length must be multiples of the page size.
347 * @ret_handle must point to a void*: this value must be used when unmapping
348 * the memory area using mono_file_unmap ().
352 mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
356 int prot = prot_from_flags (flags);
357 /* translate the flags */
358 if (flags & MONO_MMAP_PRIVATE)
359 mflags |= MAP_PRIVATE;
360 if (flags & MONO_MMAP_SHARED)
361 mflags |= MAP_SHARED;
362 if (flags & MONO_MMAP_FIXED)
364 if (flags & MONO_MMAP_32BIT)
367 ptr = mmap (0, length, prot, mflags, fd, offset);
368 if (ptr == (void*)-1)
370 *ret_handle = (void*)length;
376 * @addr: memory address returned by mono_file_map ()
377 * @handle: handle of memory map
379 * Remove the memory mapping at the address @addr.
380 * @handle must be the value returned in ret_handle by mono_file_map ().
382 * Returns: 0 on success.
385 mono_file_unmap (void *addr, void *handle)
387 return munmap (addr, (size_t)handle);
392 * @addr: memory address
393 * @length: size of memory area
394 * @flags: new protection flags
396 * Change the protection for the memory area at @addr for @length bytes
397 * to matche the supplied @flags.
398 * If @flags includes MON_MMAP_DISCARD the pages are discarded from memory
399 * and the area is cleared to zero.
400 * @addr must be aligned to the page size.
401 * @length must be a multiple of the page size.
403 * Returns: 0 on success.
406 mono_mprotect (void *addr, size_t length, int flags)
408 int prot = prot_from_flags (flags);
410 if (flags & MONO_MMAP_DISCARD) {
411 /* on non-linux the pages are not guaranteed to be zeroed (*bsd, osx at least) */
413 if (madvise (addr, length, MADV_DONTNEED))
414 memset (addr, 0, length);
416 memset (addr, 0, length);
418 madvise (addr, length, MADV_DONTNEED);
419 madvise (addr, length, MADV_FREE);
421 posix_madvise (addr, length, POSIX_MADV_DONTNEED);
425 return mprotect (addr, length, prot);
430 /* dummy malloc-based implementation */
438 mono_valloc (void *addr, size_t length, int flags)
440 return malloc (length);
444 mono_valloc_aligned (size_t length, size_t alignment, int flags)
446 g_assert_not_reached ();
449 #define HAVE_VALLOC_ALIGNED
452 mono_vfree (void *addr, size_t length)
459 mono_mprotect (void *addr, size_t length, int flags)
461 if (flags & MONO_MMAP_DISCARD) {
462 memset (addr, 0, length);
468 #if defined(HAVE_SHM_OPEN) && !defined (DISABLE_SHARED_PERFCOUNTERS)
471 mono_shared_area_instances_slow (void **array, int count, gboolean cleanup)
476 gpointer *processes = mono_process_list (&num);
477 for (i = 0; i < num; ++i) {
478 data = mono_shared_area_for_pid (processes [i]);
481 mono_shared_area_unload (data);
484 array [j++] = processes [i];
494 mono_shared_area_instances_helper (void **array, int count, gboolean cleanup)
498 int curpid = getpid ();
499 GDir *dir = g_dir_open ("/dev/shm/", 0, NULL);
501 return mono_shared_area_instances_slow (array, count, cleanup);
502 while ((name = g_dir_read_name (dir))) {
505 if (strncmp (name, "mono.", 5))
507 pid = strtol (name + 5, &nend, 10);
508 if (pid <= 0 || nend == name + 5 || *nend)
512 array [i++] = GINT_TO_POINTER (pid);
516 if (curpid != pid && kill (pid, 0) == -1 && (errno == ESRCH || errno == ENOMEM)) {
518 g_snprintf (buf, sizeof (buf), "/mono.%d", pid);
527 mono_shared_area (void)
531 /* we should allow the user to configure the size */
532 int size = mono_pagesize ();
537 /* perform cleanup of segments left over from dead processes */
538 mono_shared_area_instances_helper (NULL, 0, TRUE);
540 g_snprintf (buf, sizeof (buf), "/mono.%d", pid);
542 fd = shm_open (buf, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
543 if (fd == -1 && errno == EEXIST) {
546 fd = shm_open (buf, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
548 /* in case of failure we try to return a memory area anyway,
549 * even if it means the data can't be read by other processes
552 return malloc_shared_area (pid);
553 if (ftruncate (fd, size) != 0) {
557 res = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
558 if (res == MAP_FAILED) {
561 return malloc_shared_area (pid);
563 /* we don't need the file descriptor anymore */
568 header->stats_start = sizeof (SAreaHeader);
569 header->stats_end = sizeof (SAreaHeader);
571 atexit (mono_shared_area_remove);
576 mono_shared_area_remove (void)
579 g_snprintf (buf, sizeof (buf), "/mono.%d", getpid ());
581 if (malloced_shared_area)
582 g_free (malloced_shared_area);
586 mono_shared_area_for_pid (void *pid)
589 /* we should allow the user to configure the size */
590 int size = mono_pagesize ();
594 g_snprintf (buf, sizeof (buf), "/mono.%d", GPOINTER_TO_INT (pid));
596 fd = shm_open (buf, O_RDONLY, S_IRUSR|S_IRGRP);
599 res = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
600 if (res == MAP_FAILED) {
604 /* FIXME: validate the area */
605 /* we don't need the file descriptor anymore */
611 mono_shared_area_unload (void *area)
613 /* FIXME: currently we load only a page */
614 munmap (area, mono_pagesize ());
618 mono_shared_area_instances (void **array, int count)
620 return mono_shared_area_instances_helper (array, count, FALSE);
624 mono_shared_area (void)
626 return malloc_shared_area (getpid ());
630 mono_shared_area_remove (void)
632 if (malloced_shared_area)
633 g_free (malloced_shared_area);
634 malloced_shared_area = NULL;
638 mono_shared_area_for_pid (void *pid)
644 mono_shared_area_unload (void *area)
649 mono_shared_area_instances (void **array, int count)
654 #endif // HAVE_SHM_OPEN
658 #ifndef HAVE_VALLOC_ALIGNED
660 mono_valloc_aligned (size_t size, size_t alignment, int flags)
662 /* Allocate twice the memory to be able to put the block on an aligned address */
663 char *mem = mono_valloc (NULL, size + alignment, flags);
669 aligned = aligned_address (mem, size, alignment);
672 mono_vfree (mem, aligned - mem);
673 if (aligned + size < mem + size + alignment)
674 mono_vfree (aligned + size, (mem + size + alignment) - (aligned + size));