#if defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__) # include # include # include #endif #if defined WIN32 || defined _WIN32 || defined _WIN32 # include #endif #include #include #include #include #include <86box/86box.h> #include "cpu.h" #include <86box/mem.h> #include <86box/plat.h> #include <86box/plat_unused.h> #include "codegen.h" #include "codegen_allocator.h" #include "codegen_backend.h" struct mem_code_block_t; typedef struct mem_code_block_t { struct mem_code_block_t* prev; struct mem_code_block_t* next; int number; } mem_code_block_t; static bool valid_code_blocks[BLOCK_SIZE]; static mem_code_block_t mem_code_blocks[BLOCK_SIZE]; static mem_code_block_t* mem_code_block_head = NULL; static mem_code_block_t* mem_code_block_tail = NULL; static void remove_from_block_list(mem_code_block_t* block) { valid_code_blocks[block->number] = 0; if (block->prev) { block->prev->next = block->next; if (block->next) { block->next->prev = block->prev; } else { mem_code_block_tail = block->prev; } } else if (block->next) { /* This node was head; its successor becomes head and must lose its now-stale prev (it pointed at this node), or a later removal of the new head takes the wrong branch below. */ mem_code_block_head = block->next; mem_code_block_head->prev = NULL; } else if (block == mem_code_block_head) { mem_code_block_head = mem_code_block_tail = NULL; } block->next = block->prev = NULL; } static void add_to_block_list(int code_block) { if (!mem_code_block_head) { mem_code_block_head = &mem_code_blocks[code_block]; mem_code_block_head->number = code_block; mem_code_block_tail = mem_code_block_head; } else { mem_code_block_tail->next = &mem_code_blocks[code_block]; mem_code_blocks[code_block].prev = mem_code_block_tail; mem_code_block_tail = &mem_code_blocks[code_block]; mem_code_blocks[code_block].number = code_block; } } typedef struct mem_block_t { uint32_t offset; /*Offset into mem_block_alloc*/ uint32_t next; uint32_t tail; uint32_t code_block; } mem_block_t; static mem_block_t mem_blocks[MEM_BLOCK_NR]; static uint32_t mem_block_free_list; static uint8_t *mem_block_alloc = NULL; int codegen_allocator_usage = 0; /* Matches the per-arch direct-branch ranges the header comment above documents (128MB on ARMv8 and LoongArch64 (`b`/`bl` +/-128MiB), 2GB on x86) - same arch check already used elsewhere in this codebase (cpu.h, 386_common.h). */ #if defined(__aarch64__) || defined(_M_ARM64) || defined(__loongarch_lp64) # define CODEGEN_ALLOCATOR_MAX_POOL_BYTES (128ull * 1024 * 1024) #else # define CODEGEN_ALLOCATOR_MAX_POOL_BYTES (2ull * 1024 * 1024 * 1024) #endif void codegen_allocator_init(void) { _Static_assert((uint64_t) MEM_BLOCK_NR * MEM_BLOCK_SIZE <= CODEGEN_ALLOCATOR_MAX_POOL_BYTES, "MEM_BLOCK_NR * MEM_BLOCK_SIZE exceeds this architecture's direct-branch range"); uint8_t large = 0; mem_block_alloc = (uint8_t *) plat_mmap(MEM_BLOCK_NR * MEM_BLOCK_SIZE, 1, &large); if (large) pclog("Allocated %u bytes of large pages of recompiled code memory\n", MEM_BLOCK_NR * MEM_BLOCK_SIZE); #ifdef MEM_BLOCK_VENEER_STRIDE /*The first block of every stride after the first holds the veneers, and is never handed out*/ uint32_t last_free = 0; for (uint32_t c = 0; c < MEM_BLOCK_NR; c++) { mem_blocks[c].offset = c * MEM_BLOCK_SIZE; mem_blocks[c].code_block = BLOCK_INVALID; mem_blocks[c].tail = 0; mem_blocks[c].next = 0; if (c && !(c % MEM_BLOCK_VENEER_STRIDE)) continue; if (last_free) mem_blocks[last_free - 1].next = c + 1; last_free = c + 1; } #else for (uint32_t c = 0; c < MEM_BLOCK_NR; c++) { mem_blocks[c].offset = c * MEM_BLOCK_SIZE; mem_blocks[c].code_block = BLOCK_INVALID; mem_blocks[c].tail = 0; if (c < MEM_BLOCK_NR - 1) mem_blocks[c].next = c + 2; else mem_blocks[c].next = 0; } #endif mem_block_free_list = 1; } mem_block_t * codegen_allocator_allocate(mem_block_t *parent, int code_block) { mem_block_t *block; uint32_t block_nr; if (!mem_block_free_list) { if (mem_code_block_head == mem_code_block_tail) { fatal("Out of memory blocks!\n"); } else { mem_code_block_t* mem_code_block = mem_code_block_head; while (mem_code_block) { /* Capture next before deleting: codegen_delete_block() frees this node via remove_from_block_list(), which nulls its ->next, so reading it after would end the walk early. */ mem_code_block_t *next = mem_code_block->next; if (code_block != mem_code_block->number) { codegen_delete_block(&codeblock[mem_code_block->number]); } mem_code_block = next; } if (mem_block_free_list) goto block_allocate; fatal("Out of memory blocks!\n"); } } block_allocate: /*Remove from free list*/ block_nr = mem_block_free_list; block = &mem_blocks[block_nr - 1]; mem_block_free_list = block->next; block->code_block = code_block; if (parent) { /*Add to parent list*/ if (parent->tail) { mem_blocks[parent->tail - 1].next = block_nr; parent->tail = block_nr; } else parent->next = parent->tail = block_nr; block->next = block->tail = 0; } else { block->next = block->tail = 0; if (!valid_code_blocks[code_block]) { valid_code_blocks[code_block] = 1; add_to_block_list(code_block); } } codegen_allocator_usage++; return block; } void codegen_allocator_free(mem_block_t *block) { int block_nr = (((uintptr_t) block - (uintptr_t) mem_blocks) / sizeof(mem_block_t)) + 1; block->tail = 0; if (valid_code_blocks[block->code_block]) remove_from_block_list(&mem_code_blocks[block->code_block]); while (1) { int next_block_nr = block->next; codegen_allocator_usage--; block->next = mem_block_free_list; block->code_block = BLOCK_INVALID; mem_block_free_list = block_nr; block_nr = next_block_nr; if (block_nr) block = &mem_blocks[block_nr - 1]; else break; } } /*Free the memory of a code block's kept earlier version. The code block keeps its place in the list freed from when memory runs out: it may still own its current code.*/ void codegen_allocator_free_version(mem_block_t *block) { int block_nr = (((uintptr_t) block - (uintptr_t) mem_blocks) / sizeof(mem_block_t)) + 1; block->tail = 0; while (1) { int next_block_nr = block->next; codegen_allocator_usage--; block->next = mem_block_free_list; block->code_block = BLOCK_INVALID; mem_block_free_list = block_nr; block_nr = next_block_nr; if (block_nr) block = &mem_blocks[block_nr - 1]; else break; } } /*Keep a code block in the list freed from when memory runs out exactly while it owns memory, its kept versions' included*/ void codegen_allocator_owner(int code_block, int owns) { if (owns && !valid_code_blocks[code_block]) { valid_code_blocks[code_block] = 1; add_to_block_list(code_block); } else if (!owns && valid_code_blocks[code_block]) remove_from_block_list(&mem_code_blocks[code_block]); } uint8_t * codeblock_allocator_get_ptr(mem_block_t *block) { return &mem_block_alloc[block->offset]; } /* On LoongArch64 glibc does not provide the legacy __clear_cache() helper; use GCC's __builtin___clear_cache, which emits `ibar 0`. */ #if defined(__loongarch_lp64) # define CODEGEN_HOST_CLEAR_CACHE __builtin___clear_cache #else # define CODEGEN_HOST_CLEAR_CACHE __clear_cache #endif void codegen_allocator_clean_blocks(UNUSED(struct mem_block_t *block)) { #if defined __ARM_EABI__ || defined __aarch64__ || defined _M_ARM64 || defined(__loongarch_lp64) while (1) { CODEGEN_HOST_CLEAR_CACHE(&mem_block_alloc[block->offset], &mem_block_alloc[block->offset + MEM_BLOCK_SIZE]); if (block->next) block = &mem_blocks[block->next - 1]; else break; } #endif } #ifdef MEM_BLOCK_VENEER_STRIDE # define MEM_BLOCK_VENEER_REGION (MEM_BLOCK_VENEER_STRIDE * MEM_BLOCK_SIZE) static const void *veneer_targets[MEM_BLOCK_SIZE / 4]; static int nr_veneer_targets; /*Region r's veneer block holds, at i * 4, a B to targets[i]*/ void codegen_allocator_set_veneers(const void **targets, int nr) { if (nr > (int) (sizeof(veneer_targets) / sizeof(veneer_targets[0]))) fatal("codegen_allocator_set_veneers - too many targets\n"); /*Every reserved block, a last partial stride's included*/ for (uint32_t c = MEM_BLOCK_VENEER_STRIDE; c < MEM_BLOCK_NR; c += MEM_BLOCK_VENEER_STRIDE) { uint32_t *veneer = (uint32_t *) &mem_block_alloc[c * MEM_BLOCK_SIZE]; for (int i = 0; i < nr; i++) { intptr_t offset = (const uint8_t *) targets[i] - (uint8_t *) &veneer[i]; veneer[i] = 0x14000000 | ((uint32_t) (offset >> 2) & 0x3ffffff); /*B*/ } CODEGEN_HOST_CLEAR_CACHE(veneer, &veneer[nr]); } for (int i = 0; i < nr; i++) veneer_targets[i] = targets[i]; nr_veneer_targets = nr; } /*A B to dst within 1MB of src (in the first region the routines themselves are), or NULL*/ void * codegen_allocator_veneer(const void *src, const void *dst) { const uint8_t *ptr = (const uint8_t *) src; uint32_t region; if (!mem_block_alloc || (ptr < mem_block_alloc) || (ptr >= (mem_block_alloc + MEM_BLOCK_NR * MEM_BLOCK_SIZE))) return NULL; region = (ptr - mem_block_alloc) / MEM_BLOCK_VENEER_REGION; if (!region) return NULL; for (int i = 0; i < nr_veneer_targets; i++) { if (veneer_targets[i] == dst) return &mem_block_alloc[(region * MEM_BLOCK_VENEER_REGION) + (i * 4)]; } return NULL; } #endif bool codegen_allocator_contains_host_ptr(const void *p) { const uint8_t *ptr = (const uint8_t *) p; uint8_t *base; size_t size; if (!mem_block_alloc || !ptr) return false; base = mem_block_alloc; size = MEM_BLOCK_NR * MEM_BLOCK_SIZE; return (ptr >= base) && (ptr < (base + size)); } bool codegen_allocator_can_branch_imm14(const uint8_t *src_insn_addr, const void *dst) { intptr_t offset; if (!src_insn_addr || !dst) return false; /* AArch64 TBZ/TBNZ uses signed imm14 scaled by 4 bytes, so legal offsets are [-2^15, 2^15) and must be 4-byte aligned. */ offset = (intptr_t) ((const uint8_t *) dst - src_insn_addr); if (offset & 3) return false; return (offset >= -(1 << 15)) && (offset < (1 << 15)); } bool codegen_allocator_can_branch_imm26(const uint8_t *src_insn_addr, const void *dst) { intptr_t offset; if (!src_insn_addr || !dst) return false; /* AArch64 B/BL uses signed imm26 scaled by 4 bytes, so legal offsets are [-2^27, 2^27) and must be 4-byte aligned. */ offset = (intptr_t) ((const uint8_t *) dst - src_insn_addr); if (offset & 3) return false; return (offset >= -(1 << 27)) && (offset < (1 << 27)); } bool codegen_allocator_can_branch_imm19(const uint8_t *src_insn_addr, const void *dst) { intptr_t offset; if (!src_insn_addr || !dst) return false; /* AArch64 CBZ/CBNZ uses signed imm19 scaled by 4 bytes, so legal offsets are [-2^20, 2^20) and must be 4-byte aligned. */ offset = (intptr_t) ((const uint8_t *) dst - src_insn_addr); if (offset & 3) return false; return (offset >= -(1 << 20)) && (offset < (1 << 20)); }