Use the sym operator for inline assembly (#5459)

* Use the `sym` operator for inline assembly

Avoids extra `#[no_mangle]` functions and undue symbols being exposed
from Wasmtime. This is a newly stabilized feature in Rust 1.66.0. I've
also added a `rust-version` entry to the `wasmtime` crate to try to head
off possible reports in the future about odd error messages or usage of
unstable features if the rustc version is too old.

* Fix a s390x warning

* Add `rust-version` annotation to Wasmtime crate

As the other main entrypoint for embedders.
This commit is contained in:
Alex Crichton
2022-12-16 14:12:24 -06:00
committed by GitHub
parent 25bf8e0e67
commit d9fdbfd50e
12 changed files with 204 additions and 185 deletions

View File

@@ -18,6 +18,7 @@
// `DW_CFA_AARCH64_negate_ra_state` DWARF operation (aliased with the
// `.cfi_window_save` assembler directive) informs an unwinder about this
use super::wasmtime_fiber_start;
use wasmtime_asm_macros::asm_func;
cfg_if::cfg_if! {
@@ -25,8 +26,8 @@ cfg_if::cfg_if! {
macro_rules! paci1716 { () => ("pacib1716\n"); }
macro_rules! pacisp { () => ("pacibsp\n"); }
macro_rules! autisp { () => ("autibsp\n"); }
macro_rules! sym_adrp { ($s:tt) => (concat!("_", $s, "@PAGE")); }
macro_rules! sym_add { ($s:tt) => (concat!("_", $s, "@PAGEOFF")); }
macro_rules! sym_adrp { ($s:tt) => (concat!($s, "@PAGE")); }
macro_rules! sym_add { ($s:tt) => (concat!($s, "@PAGEOFF")); }
} else {
macro_rules! paci1716 { () => ("pacia1716\n"); }
macro_rules! pacisp { () => ("paciasp\n"); }
@@ -39,52 +40,54 @@ cfg_if::cfg_if! {
// fn(top_of_stack(%x0): *mut u8)
asm_func!(
"wasmtime_fiber_switch",
"
.cfi_startproc
",
pacisp!(),
"
.cfi_window_save
// Save all callee-saved registers on the stack since we're
// assuming they're clobbered as a result of the stack switch.
stp x29, x30, [sp, -16]!
stp x20, x19, [sp, -16]!
stp x22, x21, [sp, -16]!
stp x24, x23, [sp, -16]!
stp x26, x25, [sp, -16]!
stp x28, x27, [sp, -16]!
stp d9, d8, [sp, -16]!
stp d11, d10, [sp, -16]!
stp d13, d12, [sp, -16]!
stp d15, d14, [sp, -16]!
concat!(
"
.cfi_startproc
",
pacisp!(),
"
.cfi_window_save
// Save all callee-saved registers on the stack since we're
// assuming they're clobbered as a result of the stack switch.
stp x29, x30, [sp, -16]!
stp x20, x19, [sp, -16]!
stp x22, x21, [sp, -16]!
stp x24, x23, [sp, -16]!
stp x26, x25, [sp, -16]!
stp x28, x27, [sp, -16]!
stp d9, d8, [sp, -16]!
stp d11, d10, [sp, -16]!
stp d13, d12, [sp, -16]!
stp d15, d14, [sp, -16]!
// Load our previously saved stack pointer to resume to, and save
// off our current stack pointer on where to come back to
// eventually.
ldr x8, [x0, -0x10]
mov x9, sp
str x9, [x0, -0x10]
// Load our previously saved stack pointer to resume to, and save
// off our current stack pointer on where to come back to
// eventually.
ldr x8, [x0, -0x10]
mov x9, sp
str x9, [x0, -0x10]
// Switch to the new stack and restore all our callee-saved
// registers after the switch and return to our new stack.
mov sp, x8
ldp d15, d14, [sp], 16
ldp d13, d12, [sp], 16
ldp d11, d10, [sp], 16
ldp d9, d8, [sp], 16
ldp x28, x27, [sp], 16
ldp x26, x25, [sp], 16
ldp x24, x23, [sp], 16
ldp x22, x21, [sp], 16
ldp x20, x19, [sp], 16
ldp x29, x30, [sp], 16
",
autisp!(),
"
.cfi_window_save
ret
.cfi_endproc
",
// Switch to the new stack and restore all our callee-saved
// registers after the switch and return to our new stack.
mov sp, x8
ldp d15, d14, [sp], 16
ldp d13, d12, [sp], 16
ldp d11, d10, [sp], 16
ldp d9, d8, [sp], 16
ldp x28, x27, [sp], 16
ldp x26, x25, [sp], 16
ldp x24, x23, [sp], 16
ldp x22, x21, [sp], 16
ldp x20, x19, [sp], 16
ldp x29, x30, [sp], 16
",
autisp!(),
"
.cfi_window_save
ret
.cfi_endproc
",
),
);
// fn(
@@ -112,26 +115,29 @@ asm_func!(
#[rustfmt::skip]
asm_func!(
"wasmtime_fiber_init",
"
.cfi_startproc
hint #34 // bti c
sub x16, x0, #16
adrp x17, ", sym_adrp!("wasmtime_fiber_start"), "
add x17, x17, ", sym_add!("wasmtime_fiber_start"), "
",
paci1716!(),
"
str x17, [x16, -0x8] // x17 => lr
str x0, [x16, -0x18] // x0 => x19
stp x2, x1, [x0, -0x38] // x1 => x20, x2 => x21
concat!(
"
.cfi_startproc
hint #34 // bti c
sub x16, x0, #16
adrp x17, ", sym_adrp!("{fiber}"), "
add x17, x17, ", sym_add!("{fiber}"), "
",
paci1716!(),
"
str x17, [x16, -0x8] // x17 => lr
str x0, [x16, -0x18] // x0 => x19
stp x2, x1, [x0, -0x38] // x1 => x20, x2 => x21
// `wasmtime_fiber_switch` has an 0xa0 byte stack, and we add 0x10 more for
// the original reserved 16 bytes.
add x8, x0, -0xb0
str x8, [x0, -0x10]
ret
.cfi_endproc
",
// `wasmtime_fiber_switch` has an 0xa0 byte stack, and we add 0x10 more for
// the original reserved 16 bytes.
add x8, x0, -0xb0
str x8, [x0, -0x10]
ret
.cfi_endproc
",
),
fiber = sym wasmtime_fiber_start,
);
// See the x86_64 file for more commentary on what these CFI directives are

View File

@@ -92,17 +92,18 @@ asm_func!(
asm_func!(
"wasmtime_fiber_init",
"
lla t0,wasmtime_fiber_start
lla t0,{}
sd t0,-0x18(a0) // ra,first should be wasmtime_fiber_start.
sd a0,-0x20(a0) // fp pointer.
sd a1,-0x28(a0) // entry_point will load to s1.
sd a1,-0x28(a0) // entry_point will load to s1.
sd a2,-0x30(a0) // entry_arg0 will load to s2.
//
//
addi t0,a0,-0xe0
sd t0,-0x10(a0)
ret
",
sym super::wasmtime_fiber_start,
);
asm_func!(
@@ -118,8 +119,8 @@ asm_func!(
0x06, /* DW_OP_deref */ \
0x08, 0xd0 , /* DW_OP_const1u 0xc8 */ \
0x22 /* DW_OP_plus */
.cfi_rel_offset ra,-0x8
.cfi_rel_offset fp,-0x10
.cfi_rel_offset s1,-0x18

View File

@@ -5,7 +5,7 @@
// all the other bits. Documentation tries to reference various bits here and
// there but try to make sure to read over everything before tweaking things!
use wasmtime_asm_macros::{asm_func, asm_sym};
use wasmtime_asm_macros::asm_func;
// fn(top_of_stack(rdi): *mut u8)
asm_func!(
@@ -58,7 +58,7 @@ asm_func!(
//
// The first 16 bytes of stack are reserved for metadata, so we start
// storing values beneath that.
lea rax, ", asm_sym!("wasmtime_fiber_start"), "[rip]
lea rax, {start}[rip]
mov -0x18[rdi], rax
mov -0x20[rdi], rdi // loaded into rbp during switch
mov -0x28[rdi], rsi // loaded into rbx during switch
@@ -73,6 +73,7 @@ asm_func!(
mov -0x10[rdi], rax
ret
",
start = sym super::wasmtime_fiber_start,
);
// This is a pretty special function that has no real signature. Its use is to