add riscv64 backend for cranelift. (#4271)
Add a RISC-V 64 (`riscv64`, RV64GC) backend. Co-authored-by: yuyang <756445638@qq.com> Co-authored-by: Chris Fallin <chris@cfallin.org> Co-authored-by: Afonso Bordado <afonsobordado@az8.co>
This commit is contained in:
@@ -51,6 +51,9 @@ cfg_if::cfg_if! {
|
||||
} else if #[cfg(target_arch = "s390x")] {
|
||||
#[macro_use]
|
||||
mod s390x;
|
||||
}else if #[cfg(target_arch = "riscv64")] {
|
||||
#[macro_use]
|
||||
mod riscv64;
|
||||
} else {
|
||||
compile_error!("unsupported architecture");
|
||||
}
|
||||
|
||||
117
crates/runtime/src/trampolines/riscv64.rs
Normal file
117
crates/runtime/src/trampolines/riscv64.rs
Normal file
@@ -0,0 +1,117 @@
|
||||
use wasmtime_asm_macros::asm_func;
|
||||
|
||||
#[rustfmt::skip]
|
||||
asm_func!(
|
||||
"host_to_wasm_trampoline",
|
||||
r#"
|
||||
.cfi_startproc
|
||||
|
||||
// Load the pointer to `VMRuntimeLimits` in `t0`.
|
||||
ld t0, 8(a1)
|
||||
|
||||
// Check to see if callee is a core `VMContext` (MAGIC == "core"). NB:
|
||||
// we do not support big-endian riscv64 so the magic value is always
|
||||
// little-endian encoded.
|
||||
li t1,0x65726f63
|
||||
lwu t3,0(a0)
|
||||
bne t3,t1,ne
|
||||
mv t1,sp
|
||||
j over
|
||||
ne:
|
||||
li t1,-1
|
||||
over:
|
||||
// Store the last Wasm SP into the `last_wasm_entry_sp` in the limits, if this
|
||||
// was core Wasm, otherwise store an invalid sentinal value.
|
||||
sd t1,40(t0)
|
||||
|
||||
ld t0,16(a1)
|
||||
jr t0
|
||||
|
||||
.cfi_endproc
|
||||
"#
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
mod host_to_wasm_trampoline_offsets_tests {
|
||||
use wasmtime_environ::{Module, PtrSize, VMOffsets};
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
let module = Module::new();
|
||||
let offsets = VMOffsets::new(std::mem::size_of::<*mut u8>() as u8, &module);
|
||||
|
||||
assert_eq!(8, offsets.vmctx_runtime_limits());
|
||||
assert_eq!(40, offsets.ptr.vmruntime_limits_last_wasm_entry_sp());
|
||||
assert_eq!(16, offsets.vmctx_callee());
|
||||
assert_eq!(0x65726f63, u32::from_le_bytes(*b"core"));
|
||||
}
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
asm_func!(
|
||||
"wasm_to_host_trampoline",
|
||||
"
|
||||
.cfi_startproc simple
|
||||
|
||||
// Load the pointer to `VMRuntimeLimits` in `t0`.
|
||||
ld t0,8(a1)
|
||||
|
||||
// Store the last Wasm FP into the `last_wasm_exit_fp` in the limits.
|
||||
sd fp,24(t0)
|
||||
|
||||
// Store the last Wasm PC into the `last_wasm_exit_pc` in the limits.
|
||||
sd ra,32(t0)
|
||||
|
||||
// Tail call to the actual host function.
|
||||
//
|
||||
// This *must* be a tail call so that we do not push to the stack and mess
|
||||
// up the offsets of stack arguments (if any).
|
||||
ld t0, 8(a0)
|
||||
jr t0
|
||||
.cfi_endproc
|
||||
",
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
mod wasm_to_host_trampoline_offsets_tests {
|
||||
use crate::VMHostFuncContext;
|
||||
use memoffset::offset_of;
|
||||
use wasmtime_environ::{Module, PtrSize, VMOffsets};
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
let module = Module::new();
|
||||
let offsets = VMOffsets::new(std::mem::size_of::<*mut u8>() as u8, &module);
|
||||
|
||||
assert_eq!(8, offsets.vmctx_runtime_limits());
|
||||
assert_eq!(24, offsets.ptr.vmruntime_limits_last_wasm_exit_fp());
|
||||
assert_eq!(32, offsets.ptr.vmruntime_limits_last_wasm_exit_pc());
|
||||
assert_eq!(8, offset_of!(VMHostFuncContext, host_func));
|
||||
}
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
macro_rules! wasm_to_libcall_trampoline {
|
||||
($libcall:ident ; $libcall_impl:ident) => {
|
||||
wasmtime_asm_macros::asm_func!(
|
||||
stringify!($libcall),
|
||||
"
|
||||
.cfi_startproc
|
||||
|
||||
// Load the pointer to `VMRuntimeLimits` in `t0`.
|
||||
ld t0, 8(a0)
|
||||
|
||||
// Store the last Wasm FP into the `last_wasm_exit_fp` in the limits.
|
||||
sd fp, 24(t0)
|
||||
|
||||
// Store the last Wasm PC into the `last_wasm_exit_pc` in the limits.
|
||||
sd ra, 32(t0)
|
||||
|
||||
// Tail call to the actual implementation of this libcall.
|
||||
j ", wasmtime_asm_macros::asm_sym!(stringify!($libcall_impl)), "
|
||||
|
||||
.cfi_endproc
|
||||
"
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -42,6 +42,9 @@ cfg_if! {
|
||||
} else if #[cfg(target_arch = "s390x")] {
|
||||
mod s390x;
|
||||
use s390x as arch;
|
||||
} else if #[cfg(target_arch = "riscv64")] {
|
||||
mod riscv64;
|
||||
use riscv64 as arch;
|
||||
} else {
|
||||
compile_error!("unsupported architecture");
|
||||
}
|
||||
|
||||
21
crates/runtime/src/traphandlers/backtrace/riscv64.rs
Normal file
21
crates/runtime/src/traphandlers/backtrace/riscv64.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
pub unsafe fn get_next_older_pc_from_fp(fp: usize) -> usize {
|
||||
*(fp as *mut usize).offset(1)
|
||||
}
|
||||
|
||||
// And the current frame pointer points to the next older frame pointer.
|
||||
pub const NEXT_OLDER_FP_FROM_FP_OFFSET: usize = 0;
|
||||
|
||||
pub fn reached_entry_sp(fp: usize, first_wasm_sp: usize) -> bool {
|
||||
// Calls in riscv64 push two i64s (old FP and return PC) so our entry SP is
|
||||
// two i64s above the first Wasm FP.
|
||||
fp == first_wasm_sp - 16
|
||||
}
|
||||
|
||||
pub fn assert_entry_sp_is_aligned(sp: usize) {
|
||||
assert_eq!(sp % 16, 0, "stack should always be aligned to 16");
|
||||
}
|
||||
|
||||
pub fn assert_fp_is_aligned(fp: usize) {
|
||||
assert_eq!(fp % 16, 0, "stack should always be aligned to 16");
|
||||
}
|
||||
@@ -228,7 +228,14 @@ unsafe fn get_pc_and_fp(cx: *mut libc::c_void, _signum: libc::c_int) -> (*const
|
||||
cx.uc_mcontext.mc_rip as *const u8,
|
||||
cx.uc_mcontext.mc_rbp as usize,
|
||||
)
|
||||
} else {
|
||||
} else if #[cfg(all(target_os = "linux", target_arch = "riscv64"))] {
|
||||
let cx = &*(cx as *const libc::ucontext_t);
|
||||
(
|
||||
cx.uc_mcontext.__gregs[libc::REG_PC] as *const u8,
|
||||
cx.uc_mcontext.__gregs[libc::REG_S0] as usize,
|
||||
)
|
||||
}
|
||||
else {
|
||||
compile_error!("unsupported platform");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user