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>
30 static uintptr_t code_memory_used = 0;
31 static gulong dynamic_code_alloc_count;
32 static gulong dynamic_code_bytes_count;
33 static gulong dynamic_code_frees_count;
36 * AMD64 processors maintain icache coherency only for pages which are
37 * marked executable. Also, windows DEP requires us to obtain executable memory from
38 * malloc when using dynamic code managers. The system malloc can't do this so we use a
39 * slighly modified version of Doug Lea's Malloc package for this purpose:
40 * http://g.oswego.edu/dl/html/malloc.html
45 #if defined(__ia64__) || defined(__x86_64__)
47 * We require 16 byte alignment on amd64 so the fp literals embedded in the code are
48 * properly aligned for SSE2.
54 #ifdef __native_client_codegen__
55 /* For Google Native Client, all targets of indirect control flow need to */
56 /* be aligned to bundle boundary. 16 bytes on ARM, 32 bytes on x86.
57 * MIN_ALIGN was updated to force alignment for calls from
58 * tramp-<arch>.c to mono_global_codeman_reserve() */
59 /* and mono_domain_code_reserve(). */
61 #define MIN_ALIGN kNaClBundleSize
65 /* if a chunk has less than this amount of free space it's considered full */
66 #define MAX_WASTAGE 32
70 #define ARCH_MAP_FLAGS MONO_MMAP_32BIT
72 #define ARCH_MAP_FLAGS 0
75 #define MONO_PROT_RWX (MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC)
77 typedef struct _CodeChunck CodeChunk;
89 unsigned int flags: 8;
90 /* this number of bytes is available to resolve addresses far in memory */
91 unsigned int bsize: 24;
94 struct _MonoCodeManager {
100 #if defined(__native_client_codegen__) && defined(__native_client__)
105 #define ALIGN_INT(val,alignment) (((val) + (alignment - 1)) & ~(alignment - 1))
107 #if defined(__native_client_codegen__) && defined(__native_client__)
108 /* End of text segment, set by linker.
109 * Dynamic text starts on the next allocated page.
112 char *next_dynamic_code_addr = NULL;
115 * This routine gets the next available bundle aligned
116 * pointer in the dynamic code section. It does not check
117 * for the section end, this error will be caught in the
121 allocate_code(intptr_t increment)
124 if (increment < 0) return NULL;
125 increment = increment & kNaClBundleMask ? (increment & ~kNaClBundleMask) + kNaClBundleSize : increment;
126 addr = next_dynamic_code_addr;
127 next_dynamic_code_addr += increment;
132 nacl_is_code_address (void *target)
134 return (char *)target < next_dynamic_code_addr;
137 /* Fill code buffer with arch-specific NOPs. */
139 mono_nacl_fill_code_buffer (guint8 *data, int size);
141 #ifndef USE_JUMP_TABLES
142 const int kMaxPatchDepth = 32;
143 __thread unsigned char **patch_source_base = NULL;
144 __thread unsigned char **patch_dest_base = NULL;
145 __thread int *patch_alloc_size = NULL;
146 __thread int patch_current_depth = -1;
147 __thread int allow_target_modification = 1;
150 nacl_jit_check_init ()
152 if (patch_source_base == NULL) {
153 patch_source_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
154 patch_dest_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
155 patch_alloc_size = g_malloc (kMaxPatchDepth * sizeof(int));
161 nacl_allow_target_modification (int val)
163 #ifndef USE_JUMP_TABLES
164 allow_target_modification = val;
165 #endif /* USE_JUMP_TABLES */
168 /* Given a patch target, modify the target such that patching will work when
169 * the code is copied to the data section.
172 nacl_modify_patch_target (unsigned char *target)
175 * There's no need in patch tricks for jumptables,
176 * as we always patch same jumptable.
178 #ifndef USE_JUMP_TABLES
179 /* This seems like a bit of an ugly way to do this but the advantage
180 * is we don't have to worry about all the conditions in
181 * mono_resolve_patch_target, and it can be used by all the bare uses
187 if (!allow_target_modification) return target;
189 nacl_jit_check_init ();
190 sb = patch_source_base[patch_current_depth];
191 db = patch_dest_base[patch_current_depth];
193 if (target >= sb && (target < sb + patch_alloc_size[patch_current_depth])) {
194 /* Do nothing. target is in the section being generated.
195 * no need to modify, the disp will be the same either way.
198 int target_offset = target - db;
199 target = sb + target_offset;
206 nacl_inverse_modify_patch_target (unsigned char *target)
209 * There's no need in patch tricks for jumptables,
210 * as we always patch same jumptable.
212 #ifndef USE_JUMP_TABLES
217 if (!allow_target_modification) return target;
219 nacl_jit_check_init ();
220 sb = patch_source_base[patch_current_depth];
221 db = patch_dest_base[patch_current_depth];
223 target_offset = target - sb;
224 target = db + target_offset;
230 #endif /* __native_client_codegen && __native_client__ */
232 #define VALLOC_FREELIST_SIZE 16
234 static CRITICAL_SECTION valloc_mutex;
235 static GHashTable *valloc_freelists;
238 codechunk_valloc (void *preferred, guint32 size)
243 if (!valloc_freelists) {
244 InitializeCriticalSection (&valloc_mutex);
245 valloc_freelists = g_hash_table_new (NULL, NULL);
249 * Keep a small freelist of memory blocks to decrease pressure on the kernel memory subsystem to avoid #3321.
251 EnterCriticalSection (&valloc_mutex);
252 freelist = g_hash_table_lookup (valloc_freelists, GUINT_TO_POINTER (size));
254 ptr = freelist->data;
255 memset (ptr, 0, size);
256 freelist = g_slist_delete_link (freelist, freelist);
257 g_hash_table_insert (valloc_freelists, GUINT_TO_POINTER (size), freelist);
259 ptr = mono_valloc (preferred, size, MONO_PROT_RWX | ARCH_MAP_FLAGS);
260 if (!ptr && preferred)
261 ptr = mono_valloc (NULL, size, MONO_PROT_RWX | ARCH_MAP_FLAGS);
263 LeaveCriticalSection (&valloc_mutex);
268 codechunk_vfree (void *ptr, guint32 size)
272 EnterCriticalSection (&valloc_mutex);
273 freelist = g_hash_table_lookup (valloc_freelists, GUINT_TO_POINTER (size));
274 if (!freelist || g_slist_length (freelist) < VALLOC_FREELIST_SIZE) {
275 freelist = g_slist_prepend (freelist, ptr);
276 g_hash_table_insert (valloc_freelists, GUINT_TO_POINTER (size), freelist);
278 mono_vfree (ptr, size);
280 LeaveCriticalSection (&valloc_mutex);
284 codechunk_cleanup (void)
289 if (!valloc_freelists)
291 g_hash_table_iter_init (&iter, valloc_freelists);
292 while (g_hash_table_iter_next (&iter, &key, &value)) {
293 GSList *freelist = value;
296 for (l = freelist; l; l = l->next) {
297 mono_vfree (l->data, GPOINTER_TO_UINT (key));
299 g_slist_free (freelist);
301 g_hash_table_destroy (valloc_freelists);
305 mono_code_manager_init (void)
307 mono_counters_register ("Dynamic code allocs", MONO_COUNTER_JIT | MONO_COUNTER_ULONG, &dynamic_code_alloc_count);
308 mono_counters_register ("Dynamic code bytes", MONO_COUNTER_JIT | MONO_COUNTER_ULONG, &dynamic_code_bytes_count);
309 mono_counters_register ("Dynamic code frees", MONO_COUNTER_JIT | MONO_COUNTER_ULONG, &dynamic_code_frees_count);
313 mono_code_manager_cleanup (void)
315 codechunk_cleanup ();
319 * mono_code_manager_new:
321 * Creates a new code manager. A code manager can be used to allocate memory
322 * suitable for storing native code that can be later executed.
323 * A code manager allocates memory from the operating system in large chunks
324 * (typically 64KB in size) so that many methods can be allocated inside them
325 * close together, improving cache locality.
327 * Returns: the new code manager
330 mono_code_manager_new (void)
332 MonoCodeManager *cman = g_malloc0 (sizeof (MonoCodeManager));
335 #if defined(__native_client_codegen__) && defined(__native_client__)
336 if (next_dynamic_code_addr == NULL) {
337 const guint kPageMask = 0xFFFF; /* 64K pages */
338 next_dynamic_code_addr = (uintptr_t)(etext + kPageMask) & ~kPageMask;
339 #if defined (__GLIBC__)
340 /* TODO: For now, just jump 64MB ahead to avoid dynamic libraries. */
341 next_dynamic_code_addr += (uintptr_t)0x4000000;
343 /* Workaround bug in service runtime, unable to allocate */
344 /* from the first page in the dynamic code section. */
345 next_dynamic_code_addr += (uintptr_t)0x10000;
348 cman->hash = g_hash_table_new (NULL, NULL);
349 # ifndef USE_JUMP_TABLES
350 if (patch_source_base == NULL) {
351 patch_source_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
352 patch_dest_base = g_malloc (kMaxPatchDepth * sizeof(unsigned char *));
353 patch_alloc_size = g_malloc (kMaxPatchDepth * sizeof(int));
361 * mono_code_manager_new_dynamic:
363 * Creates a new code manager suitable for holding native code that can be
364 * used for single or small methods that need to be deallocated independently
365 * of other native code.
367 * Returns: the new code manager
370 mono_code_manager_new_dynamic (void)
372 MonoCodeManager *cman = mono_code_manager_new ();
379 free_chunklist (CodeChunk *chunk)
383 #if defined(HAVE_VALGRIND_MEMCHECK_H) && defined (VALGRIND_JIT_UNREGISTER_MAP)
384 int valgrind_unregister = 0;
385 if (RUNNING_ON_VALGRIND)
386 valgrind_unregister = 1;
387 #define valgrind_unregister(x) do { if (valgrind_unregister) { VALGRIND_JIT_UNREGISTER_MAP(NULL,x); } } while (0)
389 #define valgrind_unregister(x)
394 mono_profiler_code_chunk_destroy ((gpointer) dead->data);
396 if (dead->flags == CODE_FLAG_MMAP) {
397 codechunk_vfree (dead->data, dead->size);
398 /* valgrind_unregister(dead->data); */
399 } else if (dead->flags == CODE_FLAG_MALLOC) {
402 code_memory_used -= dead->size;
408 * mono_code_manager_destroy:
409 * @cman: a code manager
411 * Free all the memory associated with the code manager @cman.
414 mono_code_manager_destroy (MonoCodeManager *cman)
416 free_chunklist (cman->full);
417 free_chunklist (cman->current);
422 * mono_code_manager_invalidate:
423 * @cman: a code manager
425 * Fill all the memory with an invalid native code value
426 * so that any attempt to execute code allocated in the code
427 * manager @cman will fail. This is used for debugging purposes.
430 mono_code_manager_invalidate (MonoCodeManager *cman)
434 #if defined(__i386__) || defined(__x86_64__)
435 int fill_value = 0xcc; /* x86 break */
437 int fill_value = 0x2a;
440 for (chunk = cman->current; chunk; chunk = chunk->next)
441 memset (chunk->data, fill_value, chunk->size);
442 for (chunk = cman->full; chunk; chunk = chunk->next)
443 memset (chunk->data, fill_value, chunk->size);
447 * mono_code_manager_set_read_only:
448 * @cman: a code manager
450 * Make the code manager read only, so further allocation requests cause an assert.
453 mono_code_manager_set_read_only (MonoCodeManager *cman)
455 cman->read_only = TRUE;
459 * mono_code_manager_foreach:
460 * @cman: a code manager
461 * @func: a callback function pointer
462 * @user_data: additional data to pass to @func
464 * Invokes the callback @func for each different chunk of memory allocated
465 * in the code manager @cman.
468 mono_code_manager_foreach (MonoCodeManager *cman, MonoCodeManagerFunc func, void *user_data)
471 for (chunk = cman->current; chunk; chunk = chunk->next) {
472 if (func (chunk->data, chunk->size, chunk->bsize, user_data))
475 for (chunk = cman->full; chunk; chunk = chunk->next) {
476 if (func (chunk->data, chunk->size, chunk->bsize, user_data))
481 /* BIND_ROOM is the divisor for the chunck of code size dedicated
482 * to binding branches (branches not reachable with the immediate displacement)
483 * bind_size = size/BIND_ROOM;
484 * we should reduce it and make MIN_PAGES bigger for such systems
486 #if defined(__ppc__) || defined(__powerpc__)
494 new_codechunk (CodeChunk *last, int dynamic, int size)
496 int minsize, flags = CODE_FLAG_MMAP;
497 int chunk_size, bsize = 0;
503 flags = CODE_FLAG_MALLOC;
506 pagesize = mono_pagesize ();
510 flags = CODE_FLAG_MALLOC;
512 minsize = pagesize * MIN_PAGES;
514 chunk_size = minsize;
516 /* Allocate MIN_ALIGN-1 more than we need so we can still */
517 /* guarantee MIN_ALIGN alignment for individual allocs */
518 /* from mono_code_manager_reserve_align. */
519 size += MIN_ALIGN - 1;
520 size &= ~(MIN_ALIGN - 1);
522 chunk_size += pagesize - 1;
523 chunk_size &= ~ (pagesize - 1);
527 bsize = chunk_size / BIND_ROOM;
528 if (bsize < MIN_BSIZE)
530 bsize += MIN_ALIGN -1;
531 bsize &= ~ (MIN_ALIGN - 1);
532 if (chunk_size - size < bsize) {
533 chunk_size = size + bsize;
534 chunk_size += pagesize - 1;
535 chunk_size &= ~ (pagesize - 1);
539 if (flags == CODE_FLAG_MALLOC) {
540 ptr = dlmemalign (MIN_ALIGN, chunk_size + MIN_ALIGN - 1);
544 /* Try to allocate code chunks next to each other to help the VM */
546 ptr = codechunk_valloc ((guint8*)last->data + last->size, chunk_size);
548 ptr = codechunk_valloc (NULL, chunk_size);
553 if (flags == CODE_FLAG_MALLOC) {
555 /* Make sure the thunks area is zeroed */
556 memset (ptr, 0, bsize);
560 chunk = malloc (sizeof (CodeChunk));
562 if (flags == CODE_FLAG_MALLOC)
565 mono_vfree (ptr, chunk_size);
569 chunk->size = chunk_size;
571 chunk->flags = flags;
573 chunk->bsize = bsize;
574 mono_profiler_code_chunk_new((gpointer) chunk->data, chunk->size);
576 code_memory_used += chunk_size;
577 mono_runtime_resource_check_limit (MONO_RESOURCE_JIT_CODE, code_memory_used);
578 /*printf ("code chunk at: %p\n", ptr);*/
583 * mono_code_manager_reserve:
584 * @cman: a code manager
585 * @size: size of memory to allocate
586 * @alignment: power of two alignment value
588 * Allocates at least @size bytes of memory inside the code manager @cman.
590 * Returns: the pointer to the allocated memory or #NULL on failure
593 mono_code_manager_reserve_align (MonoCodeManager *cman, int size, int alignment)
595 #if !defined(__native_client__) || !defined(__native_client_codegen__)
596 CodeChunk *chunk, *prev;
598 guint32 align_mask = alignment - 1;
600 g_assert (!cman->read_only);
602 /* eventually allow bigger alignments, but we need to fix the dynamic alloc code to
605 g_assert (alignment <= MIN_ALIGN);
608 ++dynamic_code_alloc_count;
609 dynamic_code_bytes_count += size;
612 if (!cman->current) {
613 cman->current = new_codechunk (cman->last, cman->dynamic, size);
616 cman->last = cman->current;
619 for (chunk = cman->current; chunk; chunk = chunk->next) {
620 if (ALIGN_INT (chunk->pos, alignment) + size <= chunk->size) {
621 chunk->pos = ALIGN_INT (chunk->pos, alignment);
622 /* Align the chunk->data we add to chunk->pos */
623 /* or we can't guarantee proper alignment */
624 ptr = (void*)((((uintptr_t)chunk->data + align_mask) & ~(uintptr_t)align_mask) + chunk->pos);
625 chunk->pos = ((char*)ptr - chunk->data) + size;
630 * no room found, move one filled chunk to cman->full
631 * to keep cman->current from growing too much
634 for (chunk = cman->current; chunk; prev = chunk, chunk = chunk->next) {
635 if (chunk->pos + MIN_ALIGN * 4 <= chunk->size)
638 prev->next = chunk->next;
640 cman->current = chunk->next;
642 chunk->next = cman->full;
646 chunk = new_codechunk (cman->last, cman->dynamic, size);
649 chunk->next = cman->current;
650 cman->current = chunk;
651 cman->last = cman->current;
652 chunk->pos = ALIGN_INT (chunk->pos, alignment);
653 /* Align the chunk->data we add to chunk->pos */
654 /* or we can't guarantee proper alignment */
655 ptr = (void*)((((uintptr_t)chunk->data + align_mask) & ~(uintptr_t)align_mask) + chunk->pos);
656 chunk->pos = ((char*)ptr - chunk->data) + size;
659 unsigned char *temp_ptr, *code_ptr;
660 /* Round up size to next bundle */
661 alignment = kNaClBundleSize;
662 size = (size + kNaClBundleSize) & (~kNaClBundleMask);
663 /* Allocate a temp buffer */
664 temp_ptr = memalign (alignment, size);
665 g_assert (((uintptr_t)temp_ptr & kNaClBundleMask) == 0);
666 /* Allocate code space from the service runtime */
667 code_ptr = allocate_code (size);
668 /* Insert pointer to code space in hash, keyed by buffer ptr */
669 g_hash_table_insert (cman->hash, temp_ptr, code_ptr);
671 #ifndef USE_JUMP_TABLES
672 nacl_jit_check_init ();
674 patch_current_depth++;
675 patch_source_base[patch_current_depth] = temp_ptr;
676 patch_dest_base[patch_current_depth] = code_ptr;
677 patch_alloc_size[patch_current_depth] = size;
678 g_assert (patch_current_depth < kMaxPatchDepth);
686 * mono_code_manager_reserve:
687 * @cman: a code manager
688 * @size: size of memory to allocate
690 * Allocates at least @size bytes of memory inside the code manager @cman.
692 * Returns: the pointer to the allocated memory or #NULL on failure
695 mono_code_manager_reserve (MonoCodeManager *cman, int size)
697 return mono_code_manager_reserve_align (cman, size, MIN_ALIGN);
701 * mono_code_manager_commit:
702 * @cman: a code manager
703 * @data: the pointer returned by mono_code_manager_reserve ()
704 * @size: the size requested in the call to mono_code_manager_reserve ()
705 * @newsize: the new size to reserve
707 * If we reserved too much room for a method and we didn't allocate
708 * already from the code manager, we can get back the excess allocation
709 * for later use in the code manager.
712 mono_code_manager_commit (MonoCodeManager *cman, void *data, int size, int newsize)
714 #if !defined(__native_client__) || !defined(__native_client_codegen__)
715 g_assert (newsize <= size);
717 if (cman->current && (size != newsize) && (data == cman->current->data + cman->current->pos - size)) {
718 cman->current->pos -= size - newsize;
723 g_assert (NACL_BUNDLE_ALIGN_UP(newsize) <= size);
724 code = g_hash_table_lookup (cman->hash, data);
725 g_assert (code != NULL);
726 mono_nacl_fill_code_buffer ((uint8_t*)data + newsize, size - newsize);
727 newsize = NACL_BUNDLE_ALIGN_UP(newsize);
728 g_assert ((GPOINTER_TO_UINT (data) & kNaClBundleMask) == 0);
729 g_assert ((newsize & kNaClBundleMask) == 0);
730 status = nacl_dyncode_create (code, data, newsize);
732 unsigned char *codep;
733 fprintf(stderr, "Error creating Native Client dynamic code section attempted to be\n"
734 "emitted at %p (hex dissasembly of code follows):\n", code);
735 for (codep = data; codep < data + newsize; codep++)
736 fprintf(stderr, "%02x ", *codep);
737 fprintf(stderr, "\n");
738 g_assert_not_reached ();
740 g_hash_table_remove (cman->hash, data);
741 # ifndef USE_JUMP_TABLES
742 g_assert (data == patch_source_base[patch_current_depth]);
743 g_assert (code == patch_dest_base[patch_current_depth]);
744 patch_current_depth--;
745 g_assert (patch_current_depth >= -1);
751 #if defined(__native_client_codegen__) && defined(__native_client__)
753 nacl_code_manager_get_code_dest (MonoCodeManager *cman, void *data)
755 return g_hash_table_lookup (cman->hash, data);
760 * mono_code_manager_size:
761 * @cman: a code manager
762 * @used_size: pointer to an integer for the result
764 * This function can be used to get statistics about a code manager:
765 * the integer pointed to by @used_size will contain how much
766 * memory is actually used inside the code managed @cman.
768 * Returns: the amount of memory allocated in @cman
771 mono_code_manager_size (MonoCodeManager *cman, int *used_size)
776 for (chunk = cman->current; chunk; chunk = chunk->next) {
780 for (chunk = cman->full; chunk; chunk = chunk->next) {
789 #ifdef __native_client_codegen__
790 # if defined(TARGET_ARM)
791 /* Fill empty space with UDF instruction used as halt on ARM. */
793 mono_nacl_fill_code_buffer (guint8 *data, int size)
795 guint32* data32 = (guint32*)data;
797 g_assert(size % 4 == 0);
798 for (i = 0; i < size / 4; i++)
799 data32[i] = 0xE7FEDEFF;
801 # elif (defined(TARGET_X86) || defined(TARGET_AMD64))
802 /* Fill empty space with HLT instruction */
804 mono_nacl_fill_code_buffer(guint8 *data, int size)
806 memset (data, 0xf4, size);