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

Add the iretq function to the InterruptStackFrameValue struct. #431

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/structures/idt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,40 @@ pub struct InterruptStackFrameValue {
pub stack_segment: u64,
}

impl InterruptStackFrameValue {
/// Call the `iretq` (interrupt return) instruction.
///
/// This function doesn't have to be called in an interrupt handler.
/// By manually construction a new [`InterruptStackFrameValue`] it's possible to transition
/// from a higher privilege level to a lower one.
///
/// ## Safety
///
/// Calling `iretq` is unsafe because setting the instruction pointer, stack pointer, RFlags,
/// CS and SS register can all cause undefined behaviour when done incorrectly.
///
#[inline(always)]
#[cfg(feature = "instructions")]
pub unsafe fn iretq(&self) -> ! {
unsafe {
core::arch::asm!(
"push {stack_segment}",
"push {new_stack_pointer}",
"push {rflags}",
"push {code_segment}",
"push {new_instruction_pointer}",
"iretq",
rflags = in(reg) self.cpu_flags,
new_instruction_pointer = in(reg) self.instruction_pointer.as_u64(),
new_stack_pointer = in(reg) self.stack_pointer.as_u64(),
code_segment = in(reg) self.code_segment,
stack_segment = in(reg) self.stack_segment,
options(noreturn)
)
}
}
}

impl fmt::Debug for InterruptStackFrameValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
struct Hex(u64);
Expand Down