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 size_t dynamic_code_alloc_count;
32 static size_t dynamic_code_bytes_count;
33 static size_t 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__) || defined (_WIN64)
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_WORD, &dynamic_code_alloc_count);
308 mono_counters_register ("Dynamic code bytes", MONO_COUNTER_JIT | MONO_COUNTER_WORD, &dynamic_code_bytes_count);
309 mono_counters_register ("Dynamic code frees", MONO_COUNTER_JIT | MONO_COUNTER_WORD, &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__)
492 #if defined(TARGET_ARM64)
497 new_codechunk (CodeChunk *last, int dynamic, int size)
499 int minsize, flags = CODE_FLAG_MMAP;
500 int chunk_size, bsize = 0;
506 flags = CODE_FLAG_MALLOC;
509 pagesize = mono_pagesize ();
513 flags = CODE_FLAG_MALLOC;
515 minsize = pagesize * MIN_PAGES;
517 chunk_size = minsize;
519 /* Allocate MIN_ALIGN-1 more than we need so we can still */
520 /* guarantee MIN_ALIGN alignment for individual allocs */
521 /* from mono_code_manager_reserve_align. */
522 size += MIN_ALIGN - 1;
523 size &= ~(MIN_ALIGN - 1);
525 chunk_size += pagesize - 1;
526 chunk_size &= ~ (pagesize - 1);
530 bsize = chunk_size / BIND_ROOM;
531 if (bsize < MIN_BSIZE)
533 bsize += MIN_ALIGN -1;
534 bsize &= ~ (MIN_ALIGN - 1);
535 if (chunk_size - size < bsize) {
536 chunk_size = size + bsize;
537 chunk_size += pagesize - 1;
538 chunk_size &= ~ (pagesize - 1);
542 if (flags == CODE_FLAG_MALLOC) {
543 ptr = dlmemalign (MIN_ALIGN, chunk_size + MIN_ALIGN - 1);
547 /* Try to allocate code chunks next to each other to help the VM */
549 ptr = codechunk_valloc ((guint8*)last->data + last->size, chunk_size);
551 ptr = codechunk_valloc (NULL, chunk_size);
556 if (flags == CODE_FLAG_MALLOC) {
558 /* Make sure the thunks area is zeroed */
559 memset (ptr, 0, bsize);
563 chunk = malloc (sizeof (CodeChunk));
565 if (flags == CODE_FLAG_MALLOC)
568 mono_vfree (ptr, chunk_size);
572 chunk->size = chunk_size;
574 chunk->flags = flags;
576 chunk->bsize = bsize;
577 mono_profiler_code_chunk_new((gpointer) chunk->data, chunk->size);
579 code_memory_used += chunk_size;
580 mono_runtime_resource_check_limit (MONO_RESOURCE_JIT_CODE, code_memory_used);
581 /*printf ("code chunk at: %p\n", ptr);*/
586 * mono_code_manager_reserve:
587 * @cman: a code manager
588 * @size: size of memory to allocate
589 * @alignment: power of two alignment value
591 * Allocates at least @size bytes of memory inside the code manager @cman.
593 * Returns: the pointer to the allocated memory or #NULL on failure
596 mono_code_manager_reserve_align (MonoCodeManager *cman, int size, int alignment)
598 #if !defined(__native_client__) || !defined(__native_client_codegen__)
599 CodeChunk *chunk, *prev;
601 guint32 align_mask = alignment - 1;
603 g_assert (!cman->read_only);
605 /* eventually allow bigger alignments, but we need to fix the dynamic alloc code to
608 g_assert (alignment <= MIN_ALIGN);
611 ++dynamic_code_alloc_count;
612 dynamic_code_bytes_count += size;
615 if (!cman->current) {
616 cman->current = new_codechunk (cman->last, cman->dynamic, size);
619 cman->last = cman->current;
622 for (chunk = cman->current; chunk; chunk = chunk->next) {
623 if (ALIGN_INT (chunk->pos, alignment) + size <= chunk->size) {
624 chunk->pos = ALIGN_INT (chunk->pos, alignment);
625 /* Align the chunk->data we add to chunk->pos */
626 /* or we can't guarantee proper alignment */
627 ptr = (void*)((((uintptr_t)chunk->data + align_mask) & ~(uintptr_t)align_mask) + chunk->pos);
628 chunk->pos = ((char*)ptr - chunk->data) + size;
633 * no room found, move one filled chunk to cman->full
634 * to keep cman->current from growing too much
637 for (chunk = cman->current; chunk; prev = chunk, chunk = chunk->next) {
638 if (chunk->pos + MIN_ALIGN * 4 <= chunk->size)
641 prev->next = chunk->next;
643 cman->current = chunk->next;
645 chunk->next = cman->full;
649 chunk = new_codechunk (cman->last, cman->dynamic, size);
652 chunk->next = cman->current;
653 cman->current = chunk;
654 cman->last = cman->current;
655 chunk->pos = ALIGN_INT (chunk->pos, alignment);
656 /* Align the chunk->data we add to chunk->pos */
657 /* or we can't guarantee proper alignment */
658 ptr = (void*)((((uintptr_t)chunk->data + align_mask) & ~(uintptr_t)align_mask) + chunk->pos);
659 chunk->pos = ((char*)ptr - chunk->data) + size;
662 unsigned char *temp_ptr, *code_ptr;
663 /* Round up size to next bundle */
664 alignment = kNaClBundleSize;
665 size = (size + kNaClBundleSize) & (~kNaClBundleMask);
666 /* Allocate a temp buffer */
667 temp_ptr = memalign (alignment, size);
668 g_assert (((uintptr_t)temp_ptr & kNaClBundleMask) == 0);
669 /* Allocate code space from the service runtime */
670 code_ptr = allocate_code (size);
671 /* Insert pointer to code space in hash, keyed by buffer ptr */
672 g_hash_table_insert (cman->hash, temp_ptr, code_ptr);
674 #ifndef USE_JUMP_TABLES
675 nacl_jit_check_init ();
677 patch_current_depth++;
678 patch_source_base[patch_current_depth] = temp_ptr;
679 patch_dest_base[patch_current_depth] = code_ptr;
680 patch_alloc_size[patch_current_depth] = size;
681 g_assert (patch_current_depth < kMaxPatchDepth);
689 * mono_code_manager_reserve:
690 * @cman: a code manager
691 * @size: size of memory to allocate
693 * Allocates at least @size bytes of memory inside the code manager @cman.
695 * Returns: the pointer to the allocated memory or #NULL on failure
698 mono_code_manager_reserve (MonoCodeManager *cman, int size)
700 return mono_code_manager_reserve_align (cman, size, MIN_ALIGN);
704 * mono_code_manager_commit:
705 * @cman: a code manager
706 * @data: the pointer returned by mono_code_manager_reserve ()
707 * @size: the size requested in the call to mono_code_manager_reserve ()
708 * @newsize: the new size to reserve
710 * If we reserved too much room for a method and we didn't allocate
711 * already from the code manager, we can get back the excess allocation
712 * for later use in the code manager.
715 mono_code_manager_commit (MonoCodeManager *cman, void *data, int size, int newsize)
717 #if !defined(__native_client__) || !defined(__native_client_codegen__)
718 g_assert (newsize <= size);
720 if (cman->current && (size != newsize) && (data == cman->current->data + cman->current->pos - size)) {
721 cman->current->pos -= size - newsize;
726 g_assert (NACL_BUNDLE_ALIGN_UP(newsize) <= size);
727 code = g_hash_table_lookup (cman->hash, data);
728 g_assert (code != NULL);
729 mono_nacl_fill_code_buffer ((uint8_t*)data + newsize, size - newsize);
730 newsize = NACL_BUNDLE_ALIGN_UP(newsize);
731 g_assert ((GPOINTER_TO_UINT (data) & kNaClBundleMask) == 0);
732 g_assert ((newsize & kNaClBundleMask) == 0);
733 status = nacl_dyncode_create (code, data, newsize);
735 unsigned char *codep;
736 fprintf(stderr, "Error creating Native Client dynamic code section attempted to be\n"
737 "emitted at %p (hex dissasembly of code follows):\n", code);
738 for (codep = data; codep < data + newsize; codep++)
739 fprintf(stderr, "%02x ", *codep);
740 fprintf(stderr, "\n");
741 g_assert_not_reached ();
743 g_hash_table_remove (cman->hash, data);
744 # ifndef USE_JUMP_TABLES
745 g_assert (data == patch_source_base[patch_current_depth]);
746 g_assert (code == patch_dest_base[patch_current_depth]);
747 patch_current_depth--;
748 g_assert (patch_current_depth >= -1);
754 #if defined(__native_client_codegen__) && defined(__native_client__)
756 nacl_code_manager_get_code_dest (MonoCodeManager *cman, void *data)
758 return g_hash_table_lookup (cman->hash, data);
763 * mono_code_manager_size:
764 * @cman: a code manager
765 * @used_size: pointer to an integer for the result
767 * This function can be used to get statistics about a code manager:
768 * the integer pointed to by @used_size will contain how much
769 * memory is actually used inside the code managed @cman.
771 * Returns: the amount of memory allocated in @cman
774 mono_code_manager_size (MonoCodeManager *cman, int *used_size)
779 for (chunk = cman->current; chunk; chunk = chunk->next) {
783 for (chunk = cman->full; chunk; chunk = chunk->next) {
792 #ifdef __native_client_codegen__
793 # if defined(TARGET_ARM)
794 /* Fill empty space with UDF instruction used as halt on ARM. */
796 mono_nacl_fill_code_buffer (guint8 *data, int size)
798 guint32* data32 = (guint32*)data;
800 g_assert(size % 4 == 0);
801 for (i = 0; i < size / 4; i++)
802 data32[i] = 0xE7FEDEFF;
804 # elif (defined(TARGET_X86) || defined(TARGET_AMD64))
805 /* Fill empty space with HLT instruction */
807 mono_nacl_fill_code_buffer(guint8 *data, int size)
809 memset (data, 0xf4, size);