From 9de081509618d67c9ae3f223d129c0e8de0fde31 Mon Sep 17 00:00:00 2001 From: Evian-Zhang Date: Thu, 25 Jul 2024 09:32:18 +0800 Subject: [PATCH] Refactor arch-specific instructions --- src/arch/x86.rs | 38 -------------------------------------- src/arch/x86_64.rs | 38 -------------------------------------- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 42 insertions(+), 80 deletions(-) diff --git a/src/arch/x86.rs b/src/arch/x86.rs index ed0aa5b..f50b06c 100644 --- a/src/arch/x86.rs +++ b/src/arch/x86.rs @@ -43,25 +43,6 @@ macro_rules! arch_static_key_init_nop_asm_template { }; } -/// With given branch as likely branch, initialize the instruction here as a 5-byte NOP instruction -#[doc(hidden)] -#[macro_export] -macro_rules! arch_static_key_init_nop_with_given_branch_likely { - ($key:path, $branch:expr) => {'my_label: { - ::core::arch::asm!( - $crate::arch_static_key_init_nop_asm_template!(), - label { - break 'my_label !$branch; - }, - sym $key, - const $branch as usize, - ); - - // This branch will be adjcent to the NOP/JMP instruction - break 'my_label $branch; - }}; -} - // The `0x90,0x90,0x90` are three NOPs, which is to make sure the `jmp {0}` is at least 5 bytes long. #[doc(hidden)] #[macro_export] @@ -84,22 +65,3 @@ macro_rules! arch_static_key_init_jmp_asm_template { ) }; } - -/// With given branch as likely branch, initialize the instruction here as JMP instruction -#[doc(hidden)] -#[macro_export] -macro_rules! arch_static_key_init_jmp_with_given_branch_likely { - ($key:path, $branch:expr) => {'my_label: { - ::core::arch::asm!( - $crate::arch_static_key_init_jmp_asm_template!(), - label { - break 'my_label !$branch; - }, - sym $key, - const $branch as usize, - ); - - // This branch will be adjcent to the NOP/JMP instruction - break 'my_label $branch; - }}; -} diff --git a/src/arch/x86_64.rs b/src/arch/x86_64.rs index 77c99f4..2adcadd 100644 --- a/src/arch/x86_64.rs +++ b/src/arch/x86_64.rs @@ -43,25 +43,6 @@ macro_rules! arch_static_key_init_nop_asm_template { }; } -/// With given branch as likely branch, initialize the instruction here as a 5-byte NOP instruction -#[doc(hidden)] -#[macro_export] -macro_rules! arch_static_key_init_nop_with_given_branch_likely { - ($key:path, $branch:expr) => {'my_label: { - ::core::arch::asm!( - $crate::arch_static_key_init_nop_asm_template!(), - label { - break 'my_label !$branch; - }, - sym $key, - const $branch as usize, - ); - - // This branch will be adjcent to the NOP/JMP instruction - break 'my_label $branch; - }}; -} - // The `0x90,0x90,0x90` are three NOPs, which is to make sure the `jmp {0}` is at least 5 bytes long. #[doc(hidden)] #[macro_export] @@ -84,22 +65,3 @@ macro_rules! arch_static_key_init_jmp_asm_template { ) }; } - -/// With given branch as likely branch, initialize the instruction here as JMP instruction -#[doc(hidden)] -#[macro_export] -macro_rules! arch_static_key_init_jmp_with_given_branch_likely { - ($key:path, $branch:expr) => {'my_label: { - ::core::arch::asm!( - $crate::arch_static_key_init_jmp_asm_template!(), - label { - break 'my_label !$branch; - }, - sym $key, - const $branch as usize, - ); - - // This branch will be adjcent to the NOP/JMP instruction - break 'my_label $branch; - }}; -} diff --git a/src/lib.rs b/src/lib.rs index 1ad3c94..d55b306 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -342,6 +342,44 @@ unsafe fn jump_entry_update(jump_entry: &JumpEntry, enabled: } // ---------------------------- Use ---------------------------- +/// With given branch as likely branch, initialize the instruction here as JMP instruction +#[doc(hidden)] +#[macro_export] +macro_rules! static_key_init_jmp_with_given_branch_likely { + ($key:path, $branch:expr) => {'my_label: { + ::core::arch::asm!( + $crate::arch_static_key_init_jmp_asm_template!(), + label { + break 'my_label !$branch; + }, + sym $key, + const $branch as usize, + ); + + // This branch will be adjcent to the NOP/JMP instruction + break 'my_label $branch; + }}; +} + +/// With given branch as likely branch, initialize the instruction here as NOP instruction +#[doc(hidden)] +#[macro_export] +macro_rules! static_key_init_nop_with_given_branch_likely { + ($key:path, $branch:expr) => {'my_label: { + ::core::arch::asm!( + $crate::arch_static_key_init_nop_asm_template!(), + label { + break 'my_label !$branch; + }, + sym $key, + const $branch as usize, + ); + + // This branch will be adjcent to the NOP/JMP instruction + break 'my_label $branch; + }}; +} + /// Use this in a `if` condition, just like the common [`likely`][core::intrinsics::likely] /// and [`unlikely`][core::intrinsics::unlikely] intrinsics #[macro_export] @@ -349,9 +387,9 @@ macro_rules! static_branch_unlikely { ($key:path) => {{ unsafe { if $key.initial_enabled() { - $crate::arch_static_key_init_jmp_with_given_branch_likely! { $key, false } + $crate::static_key_init_jmp_with_given_branch_likely! { $key, false } } else { - $crate::arch_static_key_init_nop_with_given_branch_likely! { $key, false } + $crate::static_key_init_nop_with_given_branch_likely! { $key, false } } } }}; @@ -364,9 +402,9 @@ macro_rules! static_branch_likely { ($key:path) => {{ unsafe { if $key.initial_enabled() { - $crate::arch_static_key_init_nop_with_given_branch_likely! { $key, true } + $crate::static_key_init_nop_with_given_branch_likely! { $key, true } } else { - $crate::arch_static_key_init_jmp_with_given_branch_likely! { $key, true } + $crate::static_key_init_jmp_with_given_branch_likely! { $key, true } } } }};