Skip to content

Commit

Permalink
Merge pull request #686 from folkertdev/use-naked-asm
Browse files Browse the repository at this point in the history
use `naked_asm!` in `#[naked]` functions
  • Loading branch information
tgross35 authored Sep 11, 2024
2 parents b411563 + a863636 commit cced7fb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ intrinsics! {
#[naked]
#[cfg(all(target_os = "uefi", not(feature = "no-asm")))]
pub unsafe extern "C" fn __chkstk() {
core::arch::asm!(
core::arch::naked_asm!(
".p2align 2",
"lsl x16, x15, #4",
"mov x17, sp",
Expand All @@ -16,7 +16,6 @@ intrinsics! {
"ldr xzr, [x17]",
"b.gt 1b",
"ret",
options(noreturn)
);
}
}
12 changes: 4 additions & 8 deletions src/aarch64_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ macro_rules! compare_and_swap {
expected: int_ty!($bytes), desired: int_ty!($bytes), ptr: *mut int_ty!($bytes)
) -> int_ty!($bytes) {
// We can't use `AtomicI8::compare_and_swap`; we *are* compare_and_swap.
unsafe { core::arch::asm! {
unsafe { core::arch::naked_asm! {
// UXT s(tmp0), s(0)
concat!(uxt!($bytes), " ", reg!($bytes, 16), ", ", reg!($bytes, 0)),
"0:",
Expand All @@ -150,7 +150,6 @@ macro_rules! compare_and_swap {
"cbnz w17, 0b",
"1:",
"ret",
options(noreturn)
} }
}
}
Expand All @@ -166,7 +165,7 @@ macro_rules! compare_and_swap_i128 {
pub unsafe extern "C" fn $name (
expected: i128, desired: i128, ptr: *mut i128
) -> i128 {
unsafe { core::arch::asm! {
unsafe { core::arch::naked_asm! {
"mov x16, x0",
"mov x17, x1",
"0:",
Expand All @@ -180,7 +179,6 @@ macro_rules! compare_and_swap_i128 {
"cbnz w15, 0b",
"1:",
"ret",
options(noreturn)
} }
}
}
Expand All @@ -196,7 +194,7 @@ macro_rules! swap {
pub unsafe extern "C" fn $name (
left: int_ty!($bytes), right_ptr: *mut int_ty!($bytes)
) -> int_ty!($bytes) {
unsafe { core::arch::asm! {
unsafe { core::arch::naked_asm! {
// mov s(tmp0), s(0)
concat!("mov ", reg!($bytes, 16), ", ", reg!($bytes, 0)),
"0:",
Expand All @@ -206,7 +204,6 @@ macro_rules! swap {
concat!(stxr!($ordering, $bytes), " w17, ", reg!($bytes, 16), ", [x1]"),
"cbnz w17, 0b",
"ret",
options(noreturn)
} }
}
}
Expand All @@ -222,7 +219,7 @@ macro_rules! fetch_op {
pub unsafe extern "C" fn $name (
val: int_ty!($bytes), ptr: *mut int_ty!($bytes)
) -> int_ty!($bytes) {
unsafe { core::arch::asm! {
unsafe { core::arch::naked_asm! {
// mov s(tmp0), s(0)
concat!("mov ", reg!($bytes, 16), ", ", reg!($bytes, 0)),
"0:",
Expand All @@ -234,7 +231,6 @@ macro_rules! fetch_op {
concat!(stxr!($ordering, $bytes), " w15, ", reg!($bytes, 17), ", [x1]"),
"cbnz w15, 0b",
"ret",
options(noreturn)
} }
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ intrinsics! {
#[naked]
#[cfg(not(target_env = "msvc"))]
pub unsafe extern "C" fn __aeabi_uidivmod() {
core::arch::asm!(
core::arch::naked_asm!(
"push {{lr}}",
"sub sp, sp, #4",
"mov r2, sp",
bl!("__udivmodsi4"),
"ldr r1, [sp]",
"add sp, sp, #4",
"pop {{pc}}",
options(noreturn)
);
}

#[naked]
pub unsafe extern "C" fn __aeabi_uldivmod() {
core::arch::asm!(
core::arch::naked_asm!(
"push {{r4, lr}}",
"sub sp, sp, #16",
"add r4, sp, #8",
Expand All @@ -47,26 +46,24 @@ intrinsics! {
"ldr r3, [sp, #12]",
"add sp, sp, #16",
"pop {{r4, pc}}",
options(noreturn)
);
}

#[naked]
pub unsafe extern "C" fn __aeabi_idivmod() {
core::arch::asm!(
core::arch::naked_asm!(
"push {{r0, r1, r4, lr}}",
bl!("__aeabi_idiv"),
"pop {{r1, r2}}",
"muls r2, r2, r0",
"subs r1, r1, r2",
"pop {{r4, pc}}",
options(noreturn)
);
}

#[naked]
pub unsafe extern "C" fn __aeabi_ldivmod() {
core::arch::asm!(
core::arch::naked_asm!(
"push {{r4, lr}}",
"sub sp, sp, #16",
"add r4, sp, #8",
Expand All @@ -76,7 +73,6 @@ intrinsics! {
"ldr r3, [sp, #12]",
"add sp, sp, #16",
"pop {{r4, pc}}",
options(noreturn)
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ intrinsics! {
not(feature = "no-asm")
))]
pub unsafe extern "C" fn __chkstk() {
core::arch::asm!(
core::arch::naked_asm!(
"jmp __alloca", // Jump to __alloca since fallthrough may be unreliable"
options(noreturn, att_syntax)
options(att_syntax)
);
}

Expand All @@ -27,7 +27,7 @@ intrinsics! {
))]
pub unsafe extern "C" fn _alloca() {
// __chkstk and _alloca are the same function
core::arch::asm!(
core::arch::naked_asm!(
"push %ecx",
"cmp $0x1000,%eax",
"lea 8(%esp),%ecx", // esp before calling this routine -> ecx
Expand All @@ -47,7 +47,7 @@ intrinsics! {
"push (%eax)", // push return address onto the stack
"sub %esp,%eax", // restore the original value in eax
"ret",
options(noreturn, att_syntax)
options(att_syntax)
);
}
}
4 changes: 2 additions & 2 deletions src/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ intrinsics! {
not(feature = "no-asm")
))]
pub unsafe extern "C" fn ___chkstk_ms() {
core::arch::asm!(
core::arch::naked_asm!(
"push %rcx",
"push %rax",
"cmp $0x1000,%rax",
Expand All @@ -32,7 +32,7 @@ intrinsics! {
"pop %rax",
"pop %rcx",
"ret",
options(noreturn, att_syntax)
options(att_syntax)
);
}
}
Expand Down

0 comments on commit cced7fb

Please sign in to comment.