Skip to content

Commit 773a2bf

Browse files
committed
Use naked_asm!, delete asm_func!
This deletes our home-grown `asm_func!` macro in favor of using `#[unsafe(naked)]` functions within Wasmtime. This is needed for fiber-related bits right now where we need tight control over the exact assembly of some functions. This additionally migrates s390x fiber bits to Rust as inline assembly is now stable for s390x. prtest:full
1 parent 370042d commit 773a2bf

File tree

21 files changed

+262
-400
lines changed

21 files changed

+262
-400
lines changed

Cargo.lock

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ wasmtime-fiber = { path = "crates/fiber", version = "=38.0.0", package = 'wasmti
259259
wasmtime-jit-debug = { path = "crates/jit-debug", version = "=38.0.0", package = 'wasmtime-internal-jit-debug' }
260260
wasmtime-component-util = { path = "crates/component-util", version = "=38.0.0", package = 'wasmtime-internal-component-util' }
261261
wasmtime-component-macro = { path = "crates/component-macro", version = "=38.0.0", package = 'wasmtime-internal-component-macro' }
262-
wasmtime-asm-macros = { path = "crates/asm-macros", version = "=38.0.0", package = 'wasmtime-internal-asm-macros' }
263262
wasmtime-versioned-export-macros = { path = "crates/versioned-export-macros", version = "=38.0.0", package = 'wasmtime-internal-versioned-export-macros' }
264263
wasmtime-slab = { path = "crates/slab", version = "=38.0.0", package = 'wasmtime-internal-slab' }
265264
wasmtime-jit-icache-coherence = { path = "crates/jit-icache-coherence", version = "=38.0.0", package = 'wasmtime-internal-jit-icache-coherence' }

crates/asm-macros/Cargo.toml

Lines changed: 0 additions & 14 deletions
This file was deleted.

crates/asm-macros/src/lib.rs

Lines changed: 0 additions & 90 deletions
This file was deleted.

crates/fiber/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ workspace = true
1515
anyhow = { workspace = true }
1616
cfg-if = { workspace = true }
1717
wasmtime-versioned-export-macros = { workspace = true }
18-
wasmtime-asm-macros = { workspace = true }
1918

2019
[target.'cfg(unix)'.dependencies]
2120
rustix = { workspace = true, features = ["mm"] }

crates/fiber/build.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ fn main() {
2222
println!("cargo:rerun-if-changed=src/windows.c");
2323
build.file("src/windows.c");
2424
build.define("VERSIONED_SUFFIX", Some(versioned_suffix!()));
25-
} else if arch == "s390x" {
26-
println!("cargo:rerun-if-changed=src/stackswitch/s390x.S");
27-
build.file("src/stackswitch/s390x.S");
28-
build.define("VERSIONED_SUFFIX", Some(versioned_suffix!()));
2925
} else {
3026
// assume that this is included via inline assembly in the crate itself,
3127
// and the crate will otherwise have a `compile_error!` for unsupported

crates/fiber/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ cfg_if::cfg_if! {
2222
if #[cfg(not(feature = "std"))] {
2323
mod nostd;
2424
use nostd as imp;
25+
mod stackswitch;
2526
} else if #[cfg(miri)] {
2627
mod miri;
2728
use miri as imp;
@@ -31,16 +32,12 @@ cfg_if::cfg_if! {
3132
} else if #[cfg(unix)] {
3233
mod unix;
3334
use unix as imp;
35+
mod stackswitch;
3436
} else {
3537
compile_error!("fibers are not supported on this platform");
3638
}
3739
}
3840

39-
// Our own stack switcher routines are used on Unix and no_std
40-
// platforms, but not on Windows (it has its own fiber API).
41-
#[cfg(any(unix, not(feature = "std")))]
42-
pub(crate) mod stackswitch;
43-
4441
/// Represents an execution stack to use for a fiber.
4542
pub struct FiberStack(imp::FiberStack);
4643

crates/fiber/src/stackswitch.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,27 @@ cfg_if::cfg_if! {
88
if #[cfg(target_arch = "aarch64")] {
99
mod aarch64;
1010
pub(crate) use supported::*;
11+
pub(crate) use aarch64::*;
1112
} else if #[cfg(target_arch = "x86_64")] {
1213
mod x86_64;
1314
pub(crate) use supported::*;
15+
pub(crate) use x86_64::*;
1416
} else if #[cfg(target_arch = "x86")] {
1517
mod x86;
1618
pub(crate) use supported::*;
19+
pub(crate) use x86::*;
1720
} else if #[cfg(target_arch = "arm")] {
1821
mod arm;
1922
pub(crate) use supported::*;
23+
pub(crate) use arm::*;
2024
} else if #[cfg(target_arch = "s390x")] {
21-
// currently `global_asm!` isn't stable on s390x so this is an external
22-
// assembler file built with the `build.rs`.
25+
mod s390x;
2326
pub(crate) use supported::*;
27+
pub(crate) use s390x::*;
2428
} else if #[cfg(target_arch = "riscv64")] {
2529
mod riscv64;
2630
pub(crate) use supported::*;
31+
pub(crate) use riscv64::*;
2732
} else {
2833
// No support for this platform. Don't fail compilation though and
2934
// instead defer the error to happen at runtime when a fiber is created.
@@ -42,18 +47,6 @@ cfg_if::cfg_if! {
4247
)]
4348
mod supported {
4449
pub const SUPPORTED_ARCH: bool = true;
45-
unsafe extern "C" {
46-
#[wasmtime_versioned_export_macros::versioned_link]
47-
pub(crate) fn wasmtime_fiber_init(
48-
top_of_stack: *mut u8,
49-
entry: extern "C" fn(*mut u8, *mut u8),
50-
entry_arg0: *mut u8,
51-
);
52-
#[wasmtime_versioned_export_macros::versioned_link]
53-
pub(crate) fn wasmtime_fiber_switch(top_of_stack: *mut u8);
54-
#[wasmtime_versioned_export_macros::versioned_link]
55-
pub(crate) fn wasmtime_fiber_start();
56-
}
5750
}
5851

5952
/// Helper module reexported in the fallback case above when the current host

crates/fiber/src/stackswitch/aarch64.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
// `DW_CFA_AARCH64_negate_ra_state` DWARF operation (aliased with the
1919
// `.cfi_window_save` assembler directive) informs an unwinder about this
2020

21-
use super::wasmtime_fiber_start;
22-
use wasmtime_asm_macros::asm_func;
21+
use core::arch::naked_asm;
2322

2423
cfg_if::cfg_if! {
2524
if #[cfg(target_vendor = "apple")] {
@@ -37,10 +36,9 @@ cfg_if::cfg_if! {
3736
}
3837
}
3938

40-
// fn(top_of_stack(%x0): *mut u8)
41-
asm_func!(
42-
wasmtime_versioned_export_macros::versioned_stringify_ident!(wasmtime_fiber_switch),
43-
concat!(
39+
#[unsafe(naked)]
40+
pub(crate) unsafe extern "C" fn wasmtime_fiber_switch(top_of_stack: *mut u8 /* x0 */) {
41+
naked_asm!(concat!(
4442
"
4543
.cfi_startproc
4644
",
@@ -87,14 +85,9 @@ asm_func!(
8785
ret
8886
.cfi_endproc
8987
",
90-
),
91-
);
88+
));
89+
}
9290

93-
// fn(
94-
// top_of_stack(%x0): *mut u8,
95-
// entry_point(%x1): extern fn(*mut u8, *mut u8),
96-
// entry_arg0(%x2): *mut u8,
97-
// )
9891
// We set up the newly initialized fiber, so that it resumes execution
9992
// from wasmtime_fiber_start(). As a result, we need a signed address
10093
// of this function, so there are 2 requirements:
@@ -112,10 +105,14 @@ asm_func!(
112105
// TODO: Use the PACGA instruction to authenticate the saved register
113106
// state, which avoids creating signed pointers to
114107
// wasmtime_fiber_start(), and provides wider coverage.
115-
#[rustfmt::skip]
116-
asm_func!(
117-
wasmtime_versioned_export_macros::versioned_stringify_ident!(wasmtime_fiber_init),
118-
concat!(
108+
#[unsafe(naked)]
109+
pub(crate) unsafe extern "C" fn wasmtime_fiber_init(
110+
top_of_stack: *mut u8, // x0
111+
entry_point: extern "C" fn(*mut u8, *mut u8), // x1
112+
entry_arg0: *mut u8, // x2
113+
) {
114+
naked_asm!(
115+
concat!(
119116
"
120117
.cfi_startproc
121118
hint #34 // bti c
@@ -136,16 +133,18 @@ asm_func!(
136133
ret
137134
.cfi_endproc
138135
",
139-
),
140-
fiber = sym wasmtime_fiber_start,
141-
);
136+
),
137+
fiber = sym wasmtime_fiber_start,
138+
);
139+
}
142140

143141
// See the x86_64 file for more commentary on what these CFI directives are
144142
// doing. Like over there note that the relative offsets to registers here
145143
// match the frame layout in `wasmtime_fiber_switch`.
146-
asm_func!(
147-
wasmtime_versioned_export_macros::versioned_stringify_ident!(wasmtime_fiber_start),
148-
"
144+
#[unsafe(naked)]
145+
unsafe extern "C" fn wasmtime_fiber_start() -> ! {
146+
naked_asm!(
147+
"
149148
.cfi_startproc simple
150149
.cfi_def_cfa_offset 0
151150
.cfi_escape 0x0f, /* DW_CFA_def_cfa_expression */ \
@@ -180,5 +179,6 @@ asm_func!(
180179
// codebase.
181180
brk 0xf1b3
182181
.cfi_endproc
183-
",
184-
);
182+
",
183+
);
184+
}

0 commit comments

Comments
 (0)