rustubs/arch/x86_64/
arch_regs.rs

1//! both [Context64] and [TrapFrame] define architecture specific registers and
2//! combine into the full execution context of a thread.
3//! [Context64] includes the callee saved registers plus FP state, and
4//! [TrapFrame] includes caller saved registers.
5
6use core::arch::asm;
7#[repr(C)]
8#[repr(packed)]
9#[derive(Debug)]
10/// the Context64 is part of the task struct; it's saved and restored explicitly
11/// on context swap.
12pub struct Context64 {
13	pub rbx: u64,
14	pub r12: u64,
15	pub r13: u64,
16	pub r14: u64,
17	pub r15: u64,
18	pub rbp: u64,
19	pub rsp: u64,
20	pub fpu: [u8; 108],
21}
22
23impl Default for Context64 {
24	fn default() -> Context64 {
25		Context64 {
26			rbx: 0,
27			r12: 0,
28			r13: 0,
29			r14: 0,
30			r15: 0,
31			rbp: 0,
32			rsp: 0,
33			fpu: [0; 108],
34		}
35	}
36}
37
38/// `TrapFrame` is saved and restored by the interrupt handler assembly code
39/// upon interrupt entry and exit.
40#[repr(C)]
41#[repr(packed)]
42#[derive(Debug)]
43pub struct TrapFrame {
44	pub r11: u64,
45	pub r10: u64,
46	pub r9: u64,
47	pub r8: u64,
48	pub rsi: u64,
49	pub rdi: u64,
50	pub rdx: u64,
51	pub rcx: u64,
52	pub rax: u64,
53	/// for some exceptions, the CPU automatically pushes an error code (see
54	/// `docs/interrupt.txt`) to the stack. For those who don't have error code,
55	/// we manually push a dummy value (0)
56	pub err_code: u64,
57}
58
59/// get the current stack pointer
60#[inline]
61pub fn get_sp() -> u64 {
62	let sp: u64;
63	unsafe {
64		asm!("mov {}, rsp", out(reg) sp);
65	}
66	sp
67}