I made some progress with running riscv64 binaries on x86_64. I used RVVM code for RISC-V CPU emulator because it was easy to separate CPU emulator from the rest of code, run emulator without memory virtualization and handle traps by callback function.
Userland virtual machine setup initial state (stack, TLS block etc) loads riscv64 runtime_loader
into virtual machine, setup arguments structue and run runtime_loader
entry point. rumtime_loader
is a program that loads/unloads executables and libraries into process address space. Kernel do not load launched user executable directly, instead it load runtime_loader
, pass startup arguments (argv, environ) to it and then runtime_loader
do process initialization and executable loading. In Linux world such thing is called “interpreter”.
Guest runtime_loader works fine and it load target application. Currently crash in <libroot.so>__init_time
because of improperly implemented TLS block and commpage.
> UserlandVM ../env
delta: 0x3d5d5fd000
INFO: Hart 0x7fba50309070 started
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fb64, cause b, tval 00000000
syscall 193(_kern_create_area)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fb64, cause b, tval 00000000
syscall 193(_kern_create_area)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f604, cause b, tval 00000000
syscall 107(_kern_open)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f5e4, cause b, tval 00000000
syscall 105(_kern_normalize_path)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f834, cause b, tval 00000000
syscall 142(_kern_read)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f834, cause b, tval 00000000
syscall 142(_kern_read)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fc04, cause b, tval 00000000
syscall 203(_kern_reserve_address_range)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fc24, cause b, tval 00000000
syscall 205(_kern_map_file)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fc24, cause b, tval 00000000
syscall 205(_kern_map_file)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f8a4, cause b, tval 00000000
syscall 149(_kern_read_stat)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f504, cause b, tval 00000000
syscall 91(_kern_register_image)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f8c4, cause b, tval 00000000
syscall 151(_kern_close)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f8a4, cause b, tval 00000000
syscall 149(_kern_read_stat)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f604, cause b, tval 00000000
syscall 107(_kern_open)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f8a4, cause b, tval 00000000
syscall 149(_kern_read_stat)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f5e4, cause b, tval 00000000
syscall 105(_kern_normalize_path)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f834, cause b, tval 00000000
syscall 142(_kern_read)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f834, cause b, tval 00000000
syscall 142(_kern_read)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fc04, cause b, tval 00000000
syscall 203(_kern_reserve_address_range)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fc24, cause b, tval 00000000
syscall 205(_kern_map_file)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fc24, cause b, tval 00000000
syscall 205(_kern_map_file)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fb64, cause b, tval 00000000
syscall 193(_kern_create_area)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f8a4, cause b, tval 00000000
syscall 149(_kern_read_stat)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f504, cause b, tval 00000000
syscall 91(_kern_register_image)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f8c4, cause b, tval 00000000
syscall 151(_kern_close)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f8a4, cause b, tval 00000000
syscall 149(_kern_read_stat)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f604, cause b, tval 00000000
syscall 107(_kern_open)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f8a4, cause b, tval 00000000
syscall 149(_kern_read_stat)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f5e4, cause b, tval 00000000
syscall 105(_kern_normalize_path)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f834, cause b, tval 00000000
syscall 142(_kern_read)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f834, cause b, tval 00000000
syscall 142(_kern_read)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fc04, cause b, tval 00000000
syscall 203(_kern_reserve_address_range)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fc24, cause b, tval 00000000
syscall 205(_kern_map_file)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fc24, cause b, tval 00000000
syscall 205(_kern_map_file)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f834, cause b, tval 00000000
syscall 142(_kern_read)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f834, cause b, tval 00000000
syscall 142(_kern_read)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f834, cause b, tval 00000000
syscall 142(_kern_read)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f8a4, cause b, tval 00000000
syscall 149(_kern_read_stat)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f504, cause b, tval 00000000
syscall 91(_kern_register_image)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f8c4, cause b, tval 00000000
syscall 151(_kern_close)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f524, cause b, tval 00000000
syscall 93(_kern_image_relocated)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f524, cause b, tval 00000000
syscall 93(_kern_image_relocated)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70f524, cause b, tval 00000000
syscall 93(_kern_image_relocated)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fbe4, cause b, tval 00000000
syscall 201(_kern_set_area_protection)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fbe4, cause b, tval 00000000
syscall 201(_kern_set_area_protection)
INFO: Hart 0x7fba50309070 trap at 0x3d5d70fbe4, cause b, tval 00000000
syscall 201(_kern_set_area_protection)
INFO: Hart 0x7fba50309070 trap at 0x10293d308b4, cause b, tval 00000000
syscall 241(_kern_get_system_info)
INFO: Hart 0x7fba50309070 trap at 0x10293d2f848, cause 5, tval 0x000010
[!] unhandled trap
Crash here (guest libroot.so
):
000000000003c842 <__init_time>:
3c842: 41 11 addi sp, sp, -16
3c844: 22 e4 sd s0, 8(sp)
3c846: 00 08 addi s0, sp, 16
3c848: 1c 69 ld a5, 16(a0) // <--