Skip to content

Commit

Permalink
Merge pull request #4 from wenyongh/test/riscv_aot
Browse files Browse the repository at this point in the history
Enable os_mmap with MAP_32BIT flag for riscv64
  • Loading branch information
no1wudi committed Jul 22, 2021
2 parents 421598c + f3b7bee commit 0a6ba90
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
3 changes: 2 additions & 1 deletion core/app-mgr/app-manager/module_wasm_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,8 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
if (section->section_type == AOT_SECTION_TYPE_TEXT) {
int map_prot =
MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64)
/* aot code and data in x86_64 must be in range 0 to 2G due to
relocation for R_X86_64_32/32S/PC32 */
int map_flags = MMAP_MAP_32BIT;
Expand Down
6 changes: 4 additions & 2 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,8 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
/* Create each data section */
for (i = 0; i < module->data_section_count; i++) {
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE;
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64)
/* aot code and data in x86_64 must be in range 0 to 2G due to
relocation for R_X86_64_32/32S/PC32 */
int map_flags = MMAP_MAP_32BIT;
Expand Down Expand Up @@ -2240,7 +2241,8 @@ create_sections(const uint8 *buf, uint32 size,
if (section_size > 0) {
int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE
| MMAP_PROT_EXEC;
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64)
/* aot code and data in x86_64 must be in range 0 to 2G due to
relocation for R_X86_64_32/32S/PC32 */
int map_flags = MMAP_MAP_32BIT;
Expand Down
4 changes: 3 additions & 1 deletion core/iwasm/aot/arch/aot_reloc_riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
#define RV_OPCODE_SW 0x23

void __divdi3();
void __moddi3();
void __muldi3();
void __udivdi3();
void __umoddi3();

static SymbolMap target_sym_map[] = {
REG_COMMON_SYMBOLS
REG_SYM(__divdi3),
REG_SYM(__moddi3),
REG_SYM(__muldi3),
REG_SYM(__udivdi3),
REG_SYM(__umoddi3),
Expand Down Expand Up @@ -233,7 +235,7 @@ apply_relocation(AOTModule *module,

CHECK_RELOC_OFFSET(sizeof(uint32));
if (val != (intptr_t)((uint8 *)symbol_addr - addr)) {
if (reloc_type == R_RISCV_CALL_PLT && symbol_index >= 0) {
if (symbol_index >= 0) {
/* Call runtime function by plt code */
symbol_addr = (uint8*)module->code + module->code_size
- get_plt_table_size()
Expand Down
51 changes: 50 additions & 1 deletion core/shared/platform/common/posix/posix_memmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,57 @@ os_mmap(void *hint, size_t size, int prot, int flags)
if (flags & MMAP_MAP_FIXED)
map_flags |= MAP_FIXED;

#if defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64)
/* As AOT relocation in RISCV64 may require that the code/data mapped
* is in range 0 to 2GB, we try to map the memory with hint address
* (mmap's first argument) to meet the requirement.
*/
if (!hint && !(flags & MMAP_MAP_FIXED) && (flags & MMAP_MAP_32BIT)) {
uint8 *stack_addr = (uint8*)&map_prot;
uint8 *text_addr = (uint8*)os_mmap;
/* hint address begins with 1MB */
static uint8 *hint_addr = (uint8 *)(uintptr_t)BH_MB;

if ((hint_addr - text_addr >= 0
&& hint_addr - text_addr < 100 * BH_MB)
|| (text_addr - hint_addr >= 0
&& text_addr - hint_addr < 100 * BH_MB)) {
/* hint address is possibly in text section, skip it */
hint_addr += 100 * BH_MB;
}

if ((hint_addr - stack_addr >= 0
&& hint_addr - stack_addr < 8 * BH_MB)
|| (stack_addr - hint_addr >= 0
&& stack_addr - hint_addr < 8 * BH_MB)) {
/* hint address is possibly in native stack area, skip it */
hint_addr += 8 * BH_MB;
}

/* try 10 times, step with 1MB each time */
for (i = 0;
i < 10 && hint_addr < (uint8 *)(uintptr_t)(2ULL * BH_GB);
i++) {
addr = mmap(hint_addr, request_size, map_prot, map_flags, -1, 0);
if (addr != MAP_FAILED) {
if (addr > (uint8 *)(uintptr_t)(2ULL * BH_GB)) {
/* unmap and try again if the mapped address doesn't
* meet the requirement */
os_munmap(addr, request_size);
}
else {
/* reset next hint address */
hint_addr += request_size;
return addr;
}
}
hint_addr += BH_MB;
}
}
#endif

/* try 5 times */
for (i = 0; i < 5; i ++) {
for (i = 0; i < 5; i++) {
addr = mmap(hint, request_size, map_prot, map_flags, -1, 0);
if (addr != MAP_FAILED)
break;
Expand Down

0 comments on commit 0a6ba90

Please sign in to comment.