12 #define USE_DL_PREFIX 1
14 #include "mono-codeman.h"
15 #include "mono-mmap.h"
16 #include "mono-counters.h"
18 #include <mono/io-layer/io-layer.h>
19 #include <mono/metadata/profiler-private.h>
20 #ifdef HAVE_VALGRIND_MEMCHECK_H
21 #include <valgrind/memcheck.h>
24 #if defined(__native_client_codegen__) && defined(__native_client__)
26 #include <nacl/nacl_dyncode.h>
27 #include <mono/mini/mini.h>
29 #include <mono/utils/mono-os-mutex.h>
32 static uintptr_t code_memory_used = 0;
33 static size_t dynamic_code_alloc_count;
34 static size_t dynamic_code_bytes_count;
35 static size_t dynamic_code_frees_count;
38 * AMD64 processors maintain icache coherency only for pages which are
39 * marked executable. Also, windows DEP requires us to obtain executable memory from
40 * malloc when using dynamic code managers. The system malloc can't do this so we use a
41 * slighly modified version of Doug Lea's Malloc package for this purpose:
42 * http://g.oswego.edu/dl/html/malloc.html
47 #if defined(__ia64__) || defined(__x86_64__) || defined (_WIN64)
49 * We require 16 byte alignment on amd64 so the fp literals embedded in the code are
50 * properly aligned for SSE2.
56 #ifdef __native_client_codegen__
57 /* For Google Native Client, all targets of indirect control flow need to */
58 /* be aligned to bundle boundary. 16 bytes on ARM, 32 bytes on x86.
59 * MIN_ALIGN was updated to force alignment for calls from
60 * tramp-<arch>.c to mono_global_codeman_reserve() */
61 /* and mono_domain_code_reserve(). */
63 #define MIN_ALIGN kNaClBundleSize
67 /* if a chunk has less than this amount of free space it's considered full */
68 #define MAX_WASTAGE 32
72 #define ARCH_MAP_FLAGS MONO_MMAP_32BIT
74 #define ARCH_MAP_FLAGS 0
77 #define MONO_PROT_RWX (MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC)
79 typedef struct _CodeChunck CodeChunk;
91 unsigned int flags: 8;
92 /* this number of bytes is available to resolve addresses far in memory */
93 unsigned int bsize: 24;
96 struct _MonoCodeManager {
102 #if defined(__native_client_codegen__) && defined(__native_client__)
107 #define ALIGN_INT(val,alignment) (((val) + (alignment - 1)) & ~(alignment - 1))
109 #if defined(__native_client_codegen__) && defined(__native_client__)
110 /* End of text segment, set by linker.
111 * Dynamic text starts on the next allocated page.
114 char *next_dynamic_code_addr = NULL;
117 * This routine gets the next available bundle aligned
118 * pointer in the dynamic code section. It does not check
119 * for the section end, this error will be caught in the
123 allocate_code(intptr_t increment)
126 if (increment < 0) return NULL;
127 increment = increment & kNaClBundleMask ? (increment & ~kNaClBundleMask) + kNaClBundleSize : increment;
128 addr = next_dynamic_code_addr;
129 next_dynamic_code_addr += increment;
134 nacl_is_code_address (void *target)
136 return (char *)target < next_dynamic_code_addr;
139 /* Fill code buffer with arch-specific NOPs. */
141 mono_nacl_fill_code_buffer (guint8 *data, int size);
143 #ifndef USE_JUMP_TABLES
144 const int kMaxPatchDepth = 32;
145 __thread unsigned char **patch_source_base = NULL;
146 __thread unsigned char **patch_dest_base = NULL;
147 __thread int *patch_alloc_size = NULL;
148 __thread int patch_current_depth = -1;
149 __thread int allow_target_modification = 1;
152 nacl_jit_check_init ()
154 if (patch_source_base == NULL) {
155 patch_source_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
156 patch_dest_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
157 patch_alloc_size = g_malloc (kMaxPatchDepth * sizeof(int));
163 nacl_allow_target_modification (int val)
165 #ifndef USE_JUMP_TABLES
166 allow_target_modification = val;
167 #endif /* USE_JUMP_TABLES */
170 /* Given a patch target, modify the target such that patching will work when
171 * the code is copied to the data section.
174 nacl_modify_patch_target (unsigned char *target)
177 * There's no need in patch tricks for jumptables,
178 * as we always patch same jumptable.
180 #ifndef USE_JUMP_TABLES
181 /* This seems like a bit of an ugly way to do this but the advantage
182 * is we don't have to worry about all the conditions in
183 * mono_resolve_patch_target, and it can be used by all the bare uses
189 if (!allow_target_modification) return target;
191 nacl_jit_check_init ();
192 sb = patch_source_base[patch_current_depth];
193 db = patch_dest_base[patch_current_depth];
195 if (target >= sb && (target < sb + patch_alloc_size[patch_current_depth])) {
196 /* Do nothing. target is in the section being generated.
197 * no need to modify, the disp will be the same either way.
200 int target_offset = target - db;
201 target = sb + target_offset;
208 nacl_inverse_modify_patch_target (unsigned char *target)
211 * There's no need in patch tricks for jumptables,
212 * as we always patch same jumptable.
214 #ifndef USE_JUMP_TABLES
219 if (!allow_target_modification) return target;
221 nacl_jit_check_init ();
222 sb = patch_source_base[patch_current_depth];
223 db = patch_dest_base[patch_current_depth];
225 target_offset = target - sb;
226 target = db + target_offset;
232 #endif /* __native_client_codegen && __native_client__ */
234 #define VALLOC_FREELIST_SIZE 16
236 static mono_mutex_t valloc_mutex;
237 static GHashTable *valloc_freelists;
240 codechunk_valloc (void *preferred, guint32 size)
245 if (!valloc_freelists) {
246 mono_os_mutex_init_recursive (&valloc_mutex);
247 valloc_freelists = g_hash_table_new (NULL, NULL);
251 * Keep a small freelist of memory blocks to decrease pressure on the kernel memory subsystem to avoid #3321.
253 mono_os_mutex_lock (&valloc_mutex);
254 freelist = (GSList *) g_hash_table_lookup (valloc_freelists, GUINT_TO_POINTER (size));
256 ptr = freelist->data;
257 memset (ptr, 0, size);
258 freelist = g_slist_delete_link (freelist, freelist);
259 g_hash_table_insert (valloc_freelists, GUINT_TO_POINTER (size), freelist);
261 ptr = mono_valloc (preferred, size, MONO_PROT_RWX | ARCH_MAP_FLAGS);
262 if (!ptr && preferred)
263 ptr = mono_valloc (NULL, size, MONO_PROT_RWX | ARCH_MAP_FLAGS);
265 mono_os_mutex_unlock (&valloc_mutex);
270 codechunk_vfree (void *ptr, guint32 size)
274 mono_os_mutex_lock (&valloc_mutex);
275 freelist = (GSList *) g_hash_table_lookup (valloc_freelists, GUINT_TO_POINTER (size));
276 if (!freelist || g_slist_length (freelist) < VALLOC_FREELIST_SIZE) {
277 freelist = g_slist_prepend (freelist, ptr);
278 g_hash_table_insert (valloc_freelists, GUINT_TO_POINTER (size), freelist);
280 mono_vfree (ptr, size);
282 mono_os_mutex_unlock (&valloc_mutex);
286 codechunk_cleanup (void)
291 if (!valloc_freelists)
293 g_hash_table_iter_init (&iter, valloc_freelists);
294 while (g_hash_table_iter_next (&iter, &key, &value)) {
295 GSList *freelist = (GSList *) value;
298 for (l = freelist; l; l = l->next) {
299 mono_vfree (l->data, GPOINTER_TO_UINT (key));
301 g_slist_free (freelist);
303 g_hash_table_destroy (valloc_freelists);
307 mono_code_manager_init (void)
309 mono_counters_register ("Dynamic code allocs", MONO_COUNTER_JIT | MONO_COUNTER_ULONG, &dynamic_code_alloc_count);
310 mono_counters_register ("Dynamic code bytes", MONO_COUNTER_JIT | MONO_COUNTER_ULONG, &dynamic_code_bytes_count);
311 mono_counters_register ("Dynamic code frees", MONO_COUNTER_JIT | MONO_COUNTER_ULONG, &dynamic_code_frees_count);
315 mono_code_manager_cleanup (void)
317 codechunk_cleanup ();
321 * mono_code_manager_new:
323 * Creates a new code manager. A code manager can be used to allocate memory
324 * suitable for storing native code that can be later executed.
325 * A code manager allocates memory from the operating system in large chunks
326 * (typically 64KB in size) so that many methods can be allocated inside them
327 * close together, improving cache locality.
329 * Returns: the new code manager
332 mono_code_manager_new (void)
334 MonoCodeManager *cman = (MonoCodeManager *) g_malloc0 (sizeof (MonoCodeManager));
337 #if defined(__native_client_codegen__) && defined(__native_client__)
338 if (next_dynamic_code_addr == NULL) {
339 const guint kPageMask = 0xFFFF; /* 64K pages */
340 next_dynamic_code_addr = (uintptr_t)(etext + kPageMask) & ~kPageMask;
341 #if defined (__GLIBC__)
342 /* TODO: For now, just jump 64MB ahead to avoid dynamic libraries. */
343 next_dynamic_code_addr += (uintptr_t)0x4000000;
345 /* Workaround bug in service runtime, unable to allocate */
346 /* from the first page in the dynamic code section. */
347 next_dynamic_code_addr += (uintptr_t)0x10000;
350 cman->hash = g_hash_table_new (NULL, NULL);
351 # ifndef USE_JUMP_TABLES
352 if (patch_source_base == NULL) {
353 patch_source_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
354 patch_dest_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
355 patch_alloc_size = g_malloc (kMaxPatchDepth * sizeof(int));
363 * mono_code_manager_new_dynamic:
365 * Creates a new code manager suitable for holding native code that can be
366 * used for single or small methods that need to be deallocated independently
367 * of other native code.
369 * Returns: the new code manager
372 mono_code_manager_new_dynamic (void)
374 MonoCodeManager *cman = mono_code_manager_new ();
381 free_chunklist (CodeChunk *chunk)
385 #if defined(HAVE_VALGRIND_MEMCHECK_H) && defined (VALGRIND_JIT_UNREGISTER_MAP)
386 int valgrind_unregister = 0;
387 if (RUNNING_ON_VALGRIND)
388 valgrind_unregister = 1;
389 #define valgrind_unregister(x) do { if (valgrind_unregister) { VALGRIND_JIT_UNREGISTER_MAP(NULL,x); } } while (0)
391 #define valgrind_unregister(x)
396 mono_profiler_code_chunk_destroy ((gpointer) dead->data);
398 if (dead->flags == CODE_FLAG_MMAP) {
399 codechunk_vfree (dead->data, dead->size);
400 /* valgrind_unregister(dead->data); */
401 } else if (dead->flags == CODE_FLAG_MALLOC) {
404 code_memory_used -= dead->size;
410 * mono_code_manager_destroy:
411 * @cman: a code manager
413 * Free all the memory associated with the code manager @cman.
416 mono_code_manager_destroy (MonoCodeManager *cman)
418 free_chunklist (cman->full);
419 free_chunklist (cman->current);
424 * mono_code_manager_invalidate:
425 * @cman: a code manager
427 * Fill all the memory with an invalid native code value
428 * so that any attempt to execute code allocated in the code
429 * manager @cman will fail. This is used for debugging purposes.
432 mono_code_manager_invalidate (MonoCodeManager *cman)
436 #if defined(__i386__) || defined(__x86_64__)
437 int fill_value = 0xcc; /* x86 break */
439 int fill_value = 0x2a;
442 for (chunk = cman->current; chunk; chunk = chunk->next)
443 memset (chunk->data, fill_value, chunk->size);
444 for (chunk = cman->full; chunk; chunk = chunk->next)
445 memset (chunk->data, fill_value, chunk->size);
449 * mono_code_manager_set_read_only:
450 * @cman: a code manager
452 * Make the code manager read only, so further allocation requests cause an assert.
455 mono_code_manager_set_read_only (MonoCodeManager *cman)
457 cman->read_only = TRUE;
461 * mono_code_manager_foreach:
462 * @cman: a code manager
463 * @func: a callback function pointer
464 * @user_data: additional data to pass to @func
466 * Invokes the callback @func for each different chunk of memory allocated
467 * in the code manager @cman.
470 mono_code_manager_foreach (MonoCodeManager *cman, MonoCodeManagerFunc func, void *user_data)
473 for (chunk = cman->current; chunk; chunk = chunk->next) {
474 if (func (chunk->data, chunk->size, chunk->bsize, user_data))
477 for (chunk = cman->full; chunk; chunk = chunk->next) {
478 if (func (chunk->data, chunk->size, chunk->bsize, user_data))
483 /* BIND_ROOM is the divisor for the chunck of code size dedicated
484 * to binding branches (branches not reachable with the immediate displacement)
485 * bind_size = size/BIND_ROOM;
486 * we should reduce it and make MIN_PAGES bigger for such systems
488 #if defined(__ppc__) || defined(__powerpc__)
491 #if defined(TARGET_ARM64)
496 new_codechunk (CodeChunk *last, int dynamic, int size)
498 int minsize, flags = CODE_FLAG_MMAP;
499 int chunk_size, bsize = 0;
505 flags = CODE_FLAG_MALLOC;
508 pagesize = mono_pagesize ();
512 flags = CODE_FLAG_MALLOC;
514 minsize = pagesize * MIN_PAGES;
516 chunk_size = minsize;
518 /* Allocate MIN_ALIGN-1 more than we need so we can still */
519 /* guarantee MIN_ALIGN alignment for individual allocs */
520 /* from mono_code_manager_reserve_align. */
521 size += MIN_ALIGN - 1;
522 size &= ~(MIN_ALIGN - 1);
524 chunk_size += pagesize - 1;
525 chunk_size &= ~ (pagesize - 1);
530 /* Reserve more space since there are no other chunks we might use if this one gets full */
531 bsize = (chunk_size * 2) / BIND_ROOM;
533 bsize = chunk_size / BIND_ROOM;
534 if (bsize < MIN_BSIZE)
536 bsize += MIN_ALIGN -1;
537 bsize &= ~ (MIN_ALIGN - 1);
538 if (chunk_size - size < bsize) {
539 chunk_size = size + bsize;
541 chunk_size += pagesize - 1;
542 chunk_size &= ~ (pagesize - 1);
547 if (flags == CODE_FLAG_MALLOC) {
548 ptr = dlmemalign (MIN_ALIGN, chunk_size + MIN_ALIGN - 1);
552 /* Try to allocate code chunks next to each other to help the VM */
555 ptr = codechunk_valloc ((guint8*)last->data + last->size, chunk_size);
557 ptr = codechunk_valloc (NULL, chunk_size);
562 if (flags == CODE_FLAG_MALLOC) {
564 /* Make sure the thunks area is zeroed */
565 memset (ptr, 0, bsize);
569 chunk = (CodeChunk *) malloc (sizeof (CodeChunk));
571 if (flags == CODE_FLAG_MALLOC)
574 mono_vfree (ptr, chunk_size);
578 chunk->size = chunk_size;
579 chunk->data = (char *) ptr;
580 chunk->flags = flags;
582 chunk->bsize = bsize;
583 mono_profiler_code_chunk_new((gpointer) chunk->data, chunk->size);
585 code_memory_used += chunk_size;
586 mono_runtime_resource_check_limit (MONO_RESOURCE_JIT_CODE, code_memory_used);
587 /*printf ("code chunk at: %p\n", ptr);*/
592 * mono_code_manager_reserve:
593 * @cman: a code manager
594 * @size: size of memory to allocate
595 * @alignment: power of two alignment value
597 * Allocates at least @size bytes of memory inside the code manager @cman.
599 * Returns: the pointer to the allocated memory or #NULL on failure
602 mono_code_manager_reserve_align (MonoCodeManager *cman, int size, int alignment)
604 #if !defined(__native_client__) || !defined(__native_client_codegen__)
605 CodeChunk *chunk, *prev;
607 guint32 align_mask = alignment - 1;
609 g_assert (!cman->read_only);
611 /* eventually allow bigger alignments, but we need to fix the dynamic alloc code to
614 g_assert (alignment <= MIN_ALIGN);
617 ++dynamic_code_alloc_count;
618 dynamic_code_bytes_count += size;
621 if (!cman->current) {
622 cman->current = new_codechunk (cman->last, cman->dynamic, size);
625 cman->last = cman->current;
628 for (chunk = cman->current; chunk; chunk = chunk->next) {
629 if (ALIGN_INT (chunk->pos, alignment) + size <= chunk->size) {
630 chunk->pos = ALIGN_INT (chunk->pos, alignment);
631 /* Align the chunk->data we add to chunk->pos */
632 /* or we can't guarantee proper alignment */
633 ptr = (void*)((((uintptr_t)chunk->data + align_mask) & ~(uintptr_t)align_mask) + chunk->pos);
634 chunk->pos = ((char*)ptr - chunk->data) + size;
639 * no room found, move one filled chunk to cman->full
640 * to keep cman->current from growing too much
643 for (chunk = cman->current; chunk; prev = chunk, chunk = chunk->next) {
644 if (chunk->pos + MIN_ALIGN * 4 <= chunk->size)
647 prev->next = chunk->next;
649 cman->current = chunk->next;
651 chunk->next = cman->full;
655 chunk = new_codechunk (cman->last, cman->dynamic, size);
658 chunk->next = cman->current;
659 cman->current = chunk;
660 cman->last = cman->current;
661 chunk->pos = ALIGN_INT (chunk->pos, alignment);
662 /* Align the chunk->data we add to chunk->pos */
663 /* or we can't guarantee proper alignment */
664 ptr = (void*)((((uintptr_t)chunk->data + align_mask) & ~(uintptr_t)align_mask) + chunk->pos);
665 chunk->pos = ((char*)ptr - chunk->data) + size;
668 unsigned char *temp_ptr, *code_ptr;
669 /* Round up size to next bundle */
670 alignment = kNaClBundleSize;
671 size = (size + kNaClBundleSize) & (~kNaClBundleMask);
672 /* Allocate a temp buffer */
673 temp_ptr = memalign (alignment, size);
674 g_assert (((uintptr_t)temp_ptr & kNaClBundleMask) == 0);
675 /* Allocate code space from the service runtime */
676 code_ptr = allocate_code (size);
677 /* Insert pointer to code space in hash, keyed by buffer ptr */
678 g_hash_table_insert (cman->hash, temp_ptr, code_ptr);
680 #ifndef USE_JUMP_TABLES
681 nacl_jit_check_init ();
683 patch_current_depth++;
684 patch_source_base[patch_current_depth] = temp_ptr;
685 patch_dest_base[patch_current_depth] = code_ptr;
686 patch_alloc_size[patch_current_depth] = size;
687 g_assert (patch_current_depth < kMaxPatchDepth);
695 * mono_code_manager_reserve:
696 * @cman: a code manager
697 * @size: size of memory to allocate
699 * Allocates at least @size bytes of memory inside the code manager @cman.
701 * Returns: the pointer to the allocated memory or #NULL on failure
704 mono_code_manager_reserve (MonoCodeManager *cman, int size)
706 return mono_code_manager_reserve_align (cman, size, MIN_ALIGN);
710 * mono_code_manager_commit:
711 * @cman: a code manager
712 * @data: the pointer returned by mono_code_manager_reserve ()
713 * @size: the size requested in the call to mono_code_manager_reserve ()
714 * @newsize: the new size to reserve
716 * If we reserved too much room for a method and we didn't allocate
717 * already from the code manager, we can get back the excess allocation
718 * for later use in the code manager.
721 mono_code_manager_commit (MonoCodeManager *cman, void *data, int size, int newsize)
723 #if !defined(__native_client__) || !defined(__native_client_codegen__)
724 g_assert (newsize <= size);
726 if (cman->current && (size != newsize) && (data == cman->current->data + cman->current->pos - size)) {
727 cman->current->pos -= size - newsize;
732 g_assert (NACL_BUNDLE_ALIGN_UP(newsize) <= size);
733 code = g_hash_table_lookup (cman->hash, data);
734 g_assert (code != NULL);
735 mono_nacl_fill_code_buffer ((uint8_t*)data + newsize, size - newsize);
736 newsize = NACL_BUNDLE_ALIGN_UP(newsize);
737 g_assert ((GPOINTER_TO_UINT (data) & kNaClBundleMask) == 0);
738 g_assert ((newsize & kNaClBundleMask) == 0);
739 status = nacl_dyncode_create (code, data, newsize);
741 unsigned char *codep;
742 fprintf(stderr, "Error creating Native Client dynamic code section attempted to be\n"
743 "emitted at %p (hex dissasembly of code follows):\n", code);
744 for (codep = data; codep < data + newsize; codep++)
745 fprintf(stderr, "%02x ", *codep);
746 fprintf(stderr, "\n");
747 g_assert_not_reached ();
749 g_hash_table_remove (cman->hash, data);
750 # ifndef USE_JUMP_TABLES
751 g_assert (data == patch_source_base[patch_current_depth]);
752 g_assert (code == patch_dest_base[patch_current_depth]);
753 patch_current_depth--;
754 g_assert (patch_current_depth >= -1);
760 #if defined(__native_client_codegen__) && defined(__native_client__)
762 nacl_code_manager_get_code_dest (MonoCodeManager *cman, void *data)
764 return g_hash_table_lookup (cman->hash, data);
769 * mono_code_manager_size:
770 * @cman: a code manager
771 * @used_size: pointer to an integer for the result
773 * This function can be used to get statistics about a code manager:
774 * the integer pointed to by @used_size will contain how much
775 * memory is actually used inside the code managed @cman.
777 * Returns: the amount of memory allocated in @cman
780 mono_code_manager_size (MonoCodeManager *cman, int *used_size)
785 for (chunk = cman->current; chunk; chunk = chunk->next) {
789 for (chunk = cman->full; chunk; chunk = chunk->next) {
798 #ifdef __native_client_codegen__
799 # if defined(TARGET_ARM)
800 /* Fill empty space with UDF instruction used as halt on ARM. */
802 mono_nacl_fill_code_buffer (guint8 *data, int size)
804 guint32* data32 = (guint32*)data;
806 g_assert(size % 4 == 0);
807 for (i = 0; i < size / 4; i++)
808 data32[i] = 0xE7FEDEFF;
810 # elif (defined(TARGET_X86) || defined(TARGET_AMD64))
811 /* Fill empty space with HLT instruction */
813 mono_nacl_fill_code_buffer(guint8 *data, int size)
815 memset (data, 0xf4, size);