Skip to content

Commit

Permalink
Migrate to naked_asm
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Oct 8, 2024
1 parent 76a9f9d commit e651ead
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 27 deletions.
6 changes: 2 additions & 4 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 @@ -61,7 +60,7 @@ impl ops::IndexMut<gimli::Register> for Context {
macro_rules! save {
(gp$(, $fp:ident)?) => {
// No need to save caller-saved registers here.
asm!(
core::arch::naked_asm!(
"
stp x29, x30, [sp, -16]!
.cfi_def_cfa_offset 16
Expand Down Expand Up @@ -93,7 +92,6 @@ macro_rules! save {
.cfi_restore x30
ret
",
options(noreturn)
);
};
(maybesavefp(fp)) => {
Expand All @@ -119,7 +117,7 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p

macro_rules! restore {
($ctx:expr, gp$(, $fp:ident)?) => {
asm!(
core::arch::asm!(
restore!(mayberestore($($fp)?)),
"
ldp x2, x3, [x0, 0x10]
Expand Down
11 changes: 4 additions & 7 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 @@ -173,7 +172,7 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
// No need to save caller-saved registers here.
#[cfg(target_feature = "d")]
unsafe {
asm!(
core::arch::naked_asm!(
"
mv t0, sp
add sp, sp, -0x190
Expand All @@ -193,12 +192,11 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
.cfi_restore ra
ret
",
options(noreturn)
);
}
#[cfg(not(target_feature = "d"))]
unsafe {
asm!(
core::arch::naked_asm!(
"
mv t0, sp
add sp, sp, -0x90
Expand All @@ -217,15 +215,14 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
.cfi_restore ra
ret
",
options(noreturn)
);
}
}

pub unsafe fn restore_context(ctx: &Context) -> ! {
#[cfg(target_feature = "d")]
unsafe {
asm!(
core::arch::asm!(
code!(restore_fp),
code!(restore_gp),
"
Expand All @@ -238,7 +235,7 @@ pub unsafe fn restore_context(ctx: &Context) -> ! {
}
#[cfg(not(target_feature = "d"))]
unsafe {
asm!(
core::arch::asm!(
code!(restore_gp),
"
lw a0, 0x28(a0)
Expand Down
11 changes: 4 additions & 7 deletions src/unwinder/arch/riscv64.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 @@ -173,7 +172,7 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
// No need to save caller-saved registers here.
#[cfg(target_feature = "d")]
unsafe {
asm!(
core::arch::naked_asm!(
"
mv t0, sp
add sp, sp, -0x210
Expand All @@ -193,12 +192,11 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
.cfi_restore ra
ret
",
options(noreturn)
);
}
#[cfg(not(target_feature = "d"))]
unsafe {
asm!(
core::arch::naked_asm!(
"
mv t0, sp
add sp, sp, -0x110
Expand All @@ -217,15 +215,14 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
.cfi_restore ra
ret
",
options(noreturn)
);
}
}

pub unsafe fn restore_context(ctx: &Context) -> ! {
#[cfg(target_feature = "d")]
unsafe {
asm!(
core::arch::asm!(
code!(restore_fp),
code!(restore_gp),
"
Expand All @@ -238,7 +235,7 @@ pub unsafe fn restore_context(ctx: &Context) -> ! {
}
#[cfg(not(target_feature = "d"))]
unsafe {
asm!(
core::arch::asm!(
code!(restore_gp),
"
ld a0, 0x50(a0)
Expand Down
6 changes: 2 additions & 4 deletions src/unwinder/arch/x86.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, X86};
Expand Down Expand Up @@ -59,7 +58,7 @@ impl ops::IndexMut<gimli::Register> for Context {
pub 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!(
core::arch::naked_asm!(
"
sub esp, 52
.cfi_def_cfa_offset 56
Expand Down Expand Up @@ -95,14 +94,13 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
.cfi_def_cfa_offset 4
ret
",
options(noreturn)
);
}
}

pub unsafe fn restore_context(ctx: &Context) -> ! {
unsafe {
asm!(
core::arch::asm!(
"
/* Restore stack */
mov esp, [edx + 16]
Expand Down
8 changes: 3 additions & 5 deletions src/unwinder/arch/x86_64.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, X86_64};
Expand Down Expand Up @@ -61,7 +60,7 @@ impl ops::IndexMut<gimli::Register> for Context {
pub 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!(
core::arch::naked_asm!(
"
sub rsp, 0x98
.cfi_def_cfa_offset 0xA0
Expand Down Expand Up @@ -90,15 +89,14 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
add rsp, 0x98
.cfi_def_cfa_offset 8
ret
",
options(noreturn)
"
);
}
}

pub unsafe fn restore_context(ctx: &Context) -> ! {
unsafe {
asm!(
core::arch::asm!(
"
/* Restore stack */
mov rsp, [rdi + 0x38]
Expand Down

0 comments on commit e651ead

Please sign in to comment.