Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Stable rust support #19

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ libc = { version = "0.2", optional = true }
spin = { version = "0.9.8", optional = true, default-features = false, features = ["mutex", "spin_mutex"] }
core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' }
compiler_builtins = { version = "0.1.2", optional = true }
naked-function = "0.1.5"

[features]
alloc = []
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![doc = include_str!("../README.md")]
#![feature(c_unwind)]
#![feature(naked_functions)]
// lang_items is an internal feature. `internal_features` lint is added recently
// so also allow unknown lints to prevent warning in older nightly versions.
#![allow(unknown_lints)]
Expand Down
137 changes: 65 additions & 72 deletions src/unwinder/arch/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::arch::asm;
use core::fmt;
use core::ops;
use gimli::{AArch64, Register};
Expand Down Expand Up @@ -58,85 +57,79 @@ impl ops::IndexMut<gimli::Register> for Context {
}
}

#[naked]
pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), ptr: *mut ()) {
#[naked_function::naked]
pub unsafe extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), ptr: *mut ()) {
// No need to save caller-saved registers here.
unsafe {
asm!(
"
stp x29, x30, [sp, -16]!
sub sp, sp, 512
mov x8, x0
mov x0, sp
asm!(
"
stp x29, x30, [sp, -16]!
sub sp, sp, 512
mov x8, x0
mov x0, sp

stp d8, d9, [sp, 0x140]
stp d10, d11, [sp, 0x150]
stp d12, d13, [sp, 0x160]
stp d14, d15, [sp, 0x170]
stp d8, d9, [sp, 0x140]
stp d10, d11, [sp, 0x150]
stp d12, d13, [sp, 0x160]
stp d14, d15, [sp, 0x170]

str x19, [sp, 0x98]
stp x20, x21, [sp, 0xA0]
stp x22, x23, [sp, 0xB0]
stp x24, x25, [sp, 0xC0]
stp x26, x27, [sp, 0xD0]
stp x28, x29, [sp, 0xE0]
add x2, sp, 528
stp x30, x2, [sp, 0xF0]
str x19, [sp, 0x98]
stp x20, x21, [sp, 0xA0]
stp x22, x23, [sp, 0xB0]
stp x24, x25, [sp, 0xC0]
stp x26, x27, [sp, 0xD0]
stp x28, x29, [sp, 0xE0]
add x2, sp, 528
stp x30, x2, [sp, 0xF0]

blr x8
blr x8

add sp, sp, 512
ldp x29, x30, [sp], 16
ret
",
options(noreturn)
);
}
add sp, sp, 512
ldp x29, x30, [sp], 16
ret
"
);
}

#[naked]
#[naked_function::naked]
pub unsafe extern "C" fn restore_context(ctx: &Context) -> ! {
unsafe {
asm!(
"
ldp d0, d1, [x0, 0x100]
ldp d2, d3, [x0, 0x110]
ldp d4, d5, [x0, 0x120]
ldp d6, d7, [x0, 0x130]
ldp d8, d9, [x0, 0x140]
ldp d10, d11, [x0, 0x150]
ldp d12, d13, [x0, 0x160]
ldp d14, d15, [x0, 0x170]
ldp d16, d17, [x0, 0x180]
ldp d18, d19, [x0, 0x190]
ldp d20, d21, [x0, 0x1A0]
ldp d22, d23, [x0, 0x1B0]
ldp d24, d25, [x0, 0x1C0]
ldp d26, d27, [x0, 0x1D0]
ldp d28, d29, [x0, 0x1E0]
ldp d30, d31, [x0, 0x1F0]
asm!(
"
ldp d0, d1, [x0, 0x100]
ldp d2, d3, [x0, 0x110]
ldp d4, d5, [x0, 0x120]
ldp d6, d7, [x0, 0x130]
ldp d8, d9, [x0, 0x140]
ldp d10, d11, [x0, 0x150]
ldp d12, d13, [x0, 0x160]
ldp d14, d15, [x0, 0x170]
ldp d16, d17, [x0, 0x180]
ldp d18, d19, [x0, 0x190]
ldp d20, d21, [x0, 0x1A0]
ldp d22, d23, [x0, 0x1B0]
ldp d24, d25, [x0, 0x1C0]
ldp d26, d27, [x0, 0x1D0]
ldp d28, d29, [x0, 0x1E0]
ldp d30, d31, [x0, 0x1F0]

ldp x2, x3, [x0, 0x10]
ldp x4, x5, [x0, 0x20]
ldp x6, x7, [x0, 0x30]
ldp x8, x9, [x0, 0x40]
ldp x10, x11, [x0, 0x50]
ldp x12, x13, [x0, 0x60]
ldp x14, x15, [x0, 0x70]
ldp x16, x17, [x0, 0x80]
ldp x18, x19, [x0, 0x90]
ldp x20, x21, [x0, 0xA0]
ldp x22, x23, [x0, 0xB0]
ldp x24, x25, [x0, 0xC0]
ldp x26, x27, [x0, 0xD0]
ldp x28, x29, [x0, 0xE0]
ldp x30, x1, [x0, 0xF0]
mov sp, x1
ldp x2, x3, [x0, 0x10]
ldp x4, x5, [x0, 0x20]
ldp x6, x7, [x0, 0x30]
ldp x8, x9, [x0, 0x40]
ldp x10, x11, [x0, 0x50]
ldp x12, x13, [x0, 0x60]
ldp x14, x15, [x0, 0x70]
ldp x16, x17, [x0, 0x80]
ldp x18, x19, [x0, 0x90]
ldp x20, x21, [x0, 0xA0]
ldp x22, x23, [x0, 0xB0]
ldp x24, x25, [x0, 0xC0]
ldp x26, x27, [x0, 0xD0]
ldp x28, x29, [x0, 0xE0]
ldp x30, x1, [x0, 0xF0]
mov sp, x1

ldp x0, x1, [x0, 0x00]
ret
",
options(noreturn)
);
}
ldp x0, x1, [x0, 0x00]
ret
"
);
}
130 changes: 62 additions & 68 deletions src/unwinder/arch/riscv32.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::arch::asm;
use core::fmt;
use core::ops;
use gimli::{Register, RiscV};
Expand Down Expand Up @@ -168,75 +167,70 @@ macro_rules! code {
};
}

#[naked]
pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), ptr: *mut ()) {
#[cfg(target_feature = "d")]
#[naked_function::naked]
pub unsafe extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), ptr: *mut ()) {
// No need to save caller-saved registers here.
#[cfg(target_feature = "d")]
unsafe {
asm!(
"
mv t0, sp
add sp, sp, -0x188
sw ra, 0x180(sp)
",
code!(save_gp),
code!(save_fp),
"
mv t0, a0
mv a0, sp
jalr t0
lw ra, 0x180(sp)
add sp, sp, 0x188
ret
",
options(noreturn)
);
}
#[cfg(not(target_feature = "d"))]
unsafe {
asm!(
"
mv t0, sp
add sp, sp, -0x88
sw ra, 0x80(sp)
",
code!(save_gp),
"
mv t0, a0
mv a0, sp
jalr t0
lw ra, 0x80(sp)
add sp, sp, 0x88
ret
",
options(noreturn)
);
}
asm!(
"
mv t0, sp
add sp, sp, -0x188
sw ra, 0x180(sp)
",
code!(save_gp),
code!(save_fp),
"
mv t0, a0
mv a0, sp
jalr t0
lw ra, 0x180(sp)
add sp, sp, 0x188
ret
"
);
}

#[cfg(not(target_feature = "d"))]
#[naked_function::naked]
pub unsafe extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), ptr: *mut ()) {
asm!(
"
mv t0, sp
add sp, sp, -0x88
sw ra, 0x80(sp)
",
code!(save_gp),
"
mv t0, a0
mv a0, sp
jalr t0
lw ra, 0x80(sp)
add sp, sp, 0x88
ret
"
);
}

#[naked]
#[cfg(target_feature = "d")]
#[naked_function::naked]
pub unsafe extern "C" fn restore_context(ctx: &Context) -> ! {
#[cfg(target_feature = "d")]
unsafe {
asm!(
code!(restore_fp),
code!(restore_gp),
"
lw a0, 0x28(a0)
ret
",
options(noreturn)
);
}
#[cfg(not(target_feature = "d"))]
unsafe {
asm!(
code!(restore_gp),
"
lw a0, 0x28(a0)
ret
",
options(noreturn)
);
}
asm!(
code!(restore_fp),
code!(restore_gp),
"
lw a0, 0x28(a0)
ret
"
);
}
#[cfg(not(target_feature = "d"))]
#[naked_function::naked]
pub unsafe extern "C" fn restore_context(ctx: &Context) -> ! {
asm!(
code!(restore_gp),
"
lw a0, 0x28(a0)
ret
"
);
}
Loading
Loading