3 * Support for mapping code into the process address space
6 * Mono Team (mono-list@lists.ximian.com)
8 * Copyright 2001-2008 Novell, Inc.
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include <sys/types.h>
30 #endif /* !HOST_WIN32 */
32 #include "mono-mmap.h"
33 #include "mono-mmap-internals.h"
34 #include "mono-proclib.h"
35 #include <mono/utils/mono-threads.h>
36 #include <mono/utils/atomic.h>
37 #include <mono/utils/mono-counters.h>
39 #define BEGIN_CRITICAL_SECTION do { \
40 MonoThreadInfo *__info = mono_thread_info_current_unchecked (); \
41 if (__info) __info->inside_critical_region = TRUE; \
43 #define END_CRITICAL_SECTION \
44 if (__info) __info->inside_critical_region = FALSE; \
48 #define MAP_ANONYMOUS MAP_ANON
64 malloc_shared_area (int pid)
66 int size = mono_pagesize ();
67 SAreaHeader *sarea = (SAreaHeader *) g_malloc0 (size);
70 sarea->stats_start = sizeof (SAreaHeader);
71 sarea->stats_end = sizeof (SAreaHeader);
77 aligned_address (char *mem, size_t size, size_t alignment)
79 char *aligned = (char*)((size_t)(mem + (alignment - 1)) & ~(alignment - 1));
80 g_assert (aligned >= mem && aligned + size <= mem + size + alignment && !((size_t)aligned & (alignment - 1)));
84 static size_t allocation_count [MONO_MEM_ACCOUNT_MAX];
85 static size_t total_allocation_count;
86 static size_t alloc_limit;
89 account_mem (MonoMemAccountType type, ssize_t size)
91 InterlockedAddP (&allocation_count [type], size);
92 InterlockedAddP (&total_allocation_count, size);
96 mono_valloc_set_limit (size_t size)
102 mono_valloc_can_alloc (size_t size)
105 return (total_allocation_count + size) < alloc_limit;
110 mono_mem_account_type_name (MonoMemAccountType type)
112 static const char *names[] = {
121 "SGen shadow card table",
123 "SGen binary protocol",
133 mono_mem_account_register_counters (void)
135 for (int i = 0; i < MONO_MEM_ACCOUNT_MAX; ++i) {
136 const char *prefix = "Valloc ";
137 const char *name = mono_mem_account_type_name (i);
139 g_assert (strlen (prefix) + strlen (name) < sizeof (descr));
140 sprintf (descr, "%s%s", prefix, name);
141 mono_counters_register (descr, MONO_COUNTER_WORD | MONO_COUNTER_RUNTIME | MONO_COUNTER_BYTES | MONO_COUNTER_VARIABLE, (void*)&allocation_count [i]);
146 // Windows specific implementation in mono-mmap-windows.c
147 #define HAVE_VALLOC_ALIGNED
151 static void* malloced_shared_area = NULL;
152 #if defined(HAVE_MMAP)
156 * Get the page size in use on the system. Addresses and sizes in the
157 * mono_mmap(), mono_munmap() and mono_mprotect() calls must be pagesize
160 * Returns: the page size in bytes.
165 static int saved_pagesize = 0;
167 return saved_pagesize;
168 saved_pagesize = getpagesize ();
169 return saved_pagesize;
173 mono_valloc_granule (void)
175 return mono_pagesize ();
179 prot_from_flags (int flags)
181 int prot = PROT_NONE;
182 /* translate the protection bits */
183 if (flags & MONO_MMAP_READ)
185 if (flags & MONO_MMAP_WRITE)
187 if (flags & MONO_MMAP_EXEC)
194 * \param addr memory address
195 * \param length memory area size
196 * \param flags protection flags
197 * Allocates \p length bytes of virtual memory with the \p flags
198 * protection. \p addr can be a preferred memory address or a
199 * mandatory one if MONO_MMAP_FIXED is set in \p flags.
200 * \p addr must be pagesize aligned and can be NULL.
201 * \p length must be a multiple of pagesize.
202 * \returns NULL on failure, the address of the memory area otherwise
205 mono_valloc (void *addr, size_t length, int flags, MonoMemAccountType type)
209 int prot = prot_from_flags (flags);
211 if (!mono_valloc_can_alloc (length))
214 /* translate the flags */
215 if (flags & MONO_MMAP_FIXED)
217 if (flags & MONO_MMAP_32BIT)
220 mflags |= MAP_ANONYMOUS;
221 mflags |= MAP_PRIVATE;
223 BEGIN_CRITICAL_SECTION;
224 ptr = mmap (addr, length, prot, mflags, -1, 0);
225 if (ptr == MAP_FAILED) {
226 int fd = open ("/dev/zero", O_RDONLY);
228 ptr = mmap (addr, length, prot, mflags, fd, 0);
232 END_CRITICAL_SECTION;
234 if (ptr == MAP_FAILED)
237 account_mem (type, (ssize_t)length);
244 * \param addr memory address returned by mono_valloc ()
245 * \param length size of memory area
246 * Remove the memory mapping at the address \p addr.
247 * \returns \c 0 on success.
250 mono_vfree (void *addr, size_t length, MonoMemAccountType type)
253 BEGIN_CRITICAL_SECTION;
254 res = munmap (addr, length);
255 END_CRITICAL_SECTION;
257 account_mem (type, -(ssize_t)length);
264 * \param length size of data to map
265 * \param flags protection flags
266 * \param fd file descriptor
267 * \param offset offset in the file
268 * \param ret_handle pointer to storage for returning a handle for the map
269 * Map the area of the file pointed to by the file descriptor \p fd, at offset
270 * \p offset and of size \p length in memory according to the protection flags
272 * \p offset and \p length must be multiples of the page size.
273 * \p ret_handle must point to a void*: this value must be used when unmapping
274 * the memory area using \c mono_file_unmap().
277 mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
281 int prot = prot_from_flags (flags);
282 /* translate the flags */
283 if (flags & MONO_MMAP_PRIVATE)
284 mflags |= MAP_PRIVATE;
285 if (flags & MONO_MMAP_SHARED)
286 mflags |= MAP_SHARED;
287 if (flags & MONO_MMAP_FIXED)
289 if (flags & MONO_MMAP_32BIT)
292 BEGIN_CRITICAL_SECTION;
293 ptr = mmap (0, length, prot, mflags, fd, offset);
294 END_CRITICAL_SECTION;
295 if (ptr == MAP_FAILED)
297 *ret_handle = (void*)length;
303 * \param addr memory address returned by mono_file_map ()
304 * \param handle handle of memory map
305 * Remove the memory mapping at the address \p addr.
306 * \p handle must be the value returned in ret_handle by \c mono_file_map().
307 * \returns \c 0 on success.
310 mono_file_unmap (void *addr, void *handle)
314 BEGIN_CRITICAL_SECTION;
315 res = munmap (addr, (size_t)handle);
316 END_CRITICAL_SECTION;
323 * \param addr memory address
324 * \param length size of memory area
325 * \param flags new protection flags
326 * Change the protection for the memory area at \p addr for \p length bytes
327 * to matche the supplied \p flags.
328 * If \p flags includes MON_MMAP_DISCARD the pages are discarded from memory
329 * and the area is cleared to zero.
330 * \p addr must be aligned to the page size.
331 * \p length must be a multiple of the page size.
332 * \returns \c 0 on success.
335 mono_mprotect (void *addr, size_t length, int flags)
337 int prot = prot_from_flags (flags);
339 if (flags & MONO_MMAP_DISCARD) {
340 /* on non-linux the pages are not guaranteed to be zeroed (*bsd, osx at least) */
342 if (madvise (addr, length, MADV_DONTNEED))
343 memset (addr, 0, length);
345 memset (addr, 0, length);
347 madvise (addr, length, MADV_DONTNEED);
348 madvise (addr, length, MADV_FREE);
350 posix_madvise (addr, length, POSIX_MADV_DONTNEED);
354 return mprotect (addr, length, prot);
359 /* dummy malloc-based implementation */
367 mono_valloc_granule (void)
369 return mono_pagesize ();
373 mono_valloc (void *addr, size_t length, int flags, MonoMemAccountType type)
375 return g_malloc (length);
379 mono_valloc_aligned (size_t size, size_t alignment, int flags, MonoMemAccountType type)
381 g_assert_not_reached ();
384 #define HAVE_VALLOC_ALIGNED
387 mono_vfree (void *addr, size_t length, MonoMemAccountType type)
394 mono_mprotect (void *addr, size_t length, int flags)
396 if (flags & MONO_MMAP_DISCARD) {
397 memset (addr, 0, length);
404 #if defined(HAVE_SHM_OPEN) && !defined (DISABLE_SHARED_PERFCOUNTERS)
406 static int use_shared_area;
409 shared_area_disabled (void)
411 if (!use_shared_area) {
412 if (g_hasenv ("MONO_DISABLE_SHARED_AREA"))
413 use_shared_area = -1;
417 return use_shared_area == -1;
421 mono_shared_area_instances_slow (void **array, int count, gboolean cleanup)
426 gpointer *processes = mono_process_list (&num);
427 for (i = 0; i < num; ++i) {
428 data = mono_shared_area_for_pid (processes [i]);
431 mono_shared_area_unload (data);
434 array [j++] = processes [i];
444 mono_shared_area_instances_helper (void **array, int count, gboolean cleanup)
448 int curpid = getpid ();
449 GDir *dir = g_dir_open ("/dev/shm/", 0, NULL);
451 return mono_shared_area_instances_slow (array, count, cleanup);
452 while ((name = g_dir_read_name (dir))) {
455 if (strncmp (name, "mono.", 5))
457 pid = strtol (name + 5, &nend, 10);
458 if (pid <= 0 || nend == name + 5 || *nend)
462 array [i++] = GINT_TO_POINTER (pid);
466 if (curpid != pid && kill (pid, 0) == -1 && (errno == ESRCH || errno == ENOMEM)) {
468 g_snprintf (buf, sizeof (buf), "/mono.%d", pid);
477 mono_shared_area (void)
481 /* we should allow the user to configure the size */
482 int size = mono_pagesize ();
487 if (shared_area_disabled ()) {
488 if (!malloced_shared_area)
489 malloced_shared_area = malloc_shared_area (0);
490 /* get the pid here */
491 return malloced_shared_area;
494 /* perform cleanup of segments left over from dead processes */
495 mono_shared_area_instances_helper (NULL, 0, TRUE);
497 g_snprintf (buf, sizeof (buf), "/mono.%d", pid);
499 fd = shm_open (buf, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
500 if (fd == -1 && errno == EEXIST) {
503 fd = shm_open (buf, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
505 /* in case of failure we try to return a memory area anyway,
506 * even if it means the data can't be read by other processes
509 return malloc_shared_area (pid);
510 if (ftruncate (fd, size) != 0) {
514 BEGIN_CRITICAL_SECTION;
515 res = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
516 END_CRITICAL_SECTION;
518 if (res == MAP_FAILED) {
521 return malloc_shared_area (pid);
523 /* we don't need the file descriptor anymore */
525 header = (SAreaHeader *) res;
528 header->stats_start = sizeof (SAreaHeader);
529 header->stats_end = sizeof (SAreaHeader);
531 mono_atexit (mono_shared_area_remove);
536 mono_shared_area_remove (void)
540 if (shared_area_disabled ()) {
541 if (malloced_shared_area)
542 g_free (malloced_shared_area);
546 g_snprintf (buf, sizeof (buf), "/mono.%d", getpid ());
548 if (malloced_shared_area)
549 g_free (malloced_shared_area);
553 mono_shared_area_for_pid (void *pid)
556 /* we should allow the user to configure the size */
557 int size = mono_pagesize ();
561 if (shared_area_disabled ())
564 g_snprintf (buf, sizeof (buf), "/mono.%d", GPOINTER_TO_INT (pid));
566 fd = shm_open (buf, O_RDONLY, S_IRUSR|S_IRGRP);
569 BEGIN_CRITICAL_SECTION;
570 res = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
571 END_CRITICAL_SECTION;
573 if (res == MAP_FAILED) {
577 /* FIXME: validate the area */
578 /* we don't need the file descriptor anymore */
584 mono_shared_area_unload (void *area)
586 /* FIXME: currently we load only a page */
587 BEGIN_CRITICAL_SECTION;
588 munmap (area, mono_pagesize ());
589 END_CRITICAL_SECTION;
593 mono_shared_area_instances (void **array, int count)
595 return mono_shared_area_instances_helper (array, count, FALSE);
599 mono_shared_area (void)
601 if (!malloced_shared_area)
602 malloced_shared_area = malloc_shared_area (getpid ());
603 /* get the pid here */
604 return malloced_shared_area;
608 mono_shared_area_remove (void)
610 if (malloced_shared_area)
611 g_free (malloced_shared_area);
612 malloced_shared_area = NULL;
616 mono_shared_area_for_pid (void *pid)
622 mono_shared_area_unload (void *area)
627 mono_shared_area_instances (void **array, int count)
632 #endif // HAVE_SHM_OPEN
636 #ifndef HAVE_VALLOC_ALIGNED
638 mono_valloc_aligned (size_t size, size_t alignment, int flags, MonoMemAccountType type)
640 /* Allocate twice the memory to be able to put the block on an aligned address */
641 char *mem = (char *) mono_valloc (NULL, size + alignment, flags, type);
647 aligned = aligned_address (mem, size, alignment);
650 mono_vfree (mem, aligned - mem, type);
651 if (aligned + size < mem + size + alignment)
652 mono_vfree (aligned + size, (mem + size + alignment) - (aligned + size), type);
659 mono_pages_not_faulted (void *addr, size_t size)
664 int pagesize = mono_pagesize ();
665 int npages = (size + pagesize - 1) / pagesize;
666 char *faulted = (char *) g_malloc0 (sizeof (char*) * npages);
669 * We cast `faulted` to void* because Linux wants an unsigned
670 * char* while BSD wants a char*.
673 if (mincore (addr, size, (unsigned char *)faulted) != 0) {
675 if (mincore (addr, size, (char *)faulted) != 0) {
680 for (i = 0; i < npages; ++i) {
681 if (faulted [i] != 0)