Skip to content

Commit

Permalink
Refactor arch-specific instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Evian-Zhang committed Jul 25, 2024
1 parent 1c39270 commit 9de0815
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 80 deletions.
38 changes: 0 additions & 38 deletions src/arch/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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;
}};
}
38 changes: 0 additions & 38 deletions src/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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;
}};
}
46 changes: 42 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,54 @@ unsafe fn jump_entry_update<M: CodeManipulator>(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]
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 }
}
}
}};
Expand All @@ -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 }
}
}
}};
Expand Down

0 comments on commit 9de0815

Please sign in to comment.