Skip to main content

strat9_syscall/arch/
x86_64.rs

1use core::{
2    mem,
3    ops::{Deref, DerefMut},
4    slice,
5};
6
7pub const PAGE_SIZE: usize = 4096;
8/// Size of the metadata region used to transfer information from the kernel to the bootstrapper.
9pub const KERNEL_METADATA_SIZE: usize = 4 * PAGE_SIZE;
10
11// Note: syscall0..syscall6 raw invocation functions are defined in lib.rs
12// (explicit functions instead of macro for compatibility with recent Rust nightlies).
13
14#[derive(Copy, Clone, Debug, Default)]
15#[repr(C)]
16pub struct IntRegisters {
17    pub r15: usize,
18    pub r14: usize,
19    pub r13: usize,
20    pub r12: usize,
21    pub rbp: usize,
22    pub rbx: usize,
23    pub r11: usize,
24    pub r10: usize,
25    pub r9: usize,
26    pub r8: usize,
27    pub rax: usize,
28    pub rcx: usize,
29    pub rdx: usize,
30    pub rsi: usize,
31    pub rdi: usize,
32    pub rip: usize,
33    pub cs: usize,
34    pub rflags: usize,
35    pub rsp: usize,
36    pub ss: usize,
37}
38
39impl Deref for IntRegisters {
40    type Target = [u8];
41    /// Implements deref.
42    fn deref(&self) -> &[u8] {
43        unsafe { slice::from_raw_parts(self as *const Self as *const u8, mem::size_of::<Self>()) }
44    }
45}
46
47impl DerefMut for IntRegisters {
48    /// Implements deref mut.
49    fn deref_mut(&mut self) -> &mut [u8] {
50        unsafe { slice::from_raw_parts_mut(self as *mut Self as *mut u8, mem::size_of::<Self>()) }
51    }
52}
53
54#[derive(Clone, Copy, Debug, Default)]
55#[repr(C, packed)]
56pub struct FloatRegisters {
57    pub fcw: u16,
58    pub fsw: u16,
59    pub ftw: u8,
60    pub _reserved: u8,
61    pub fop: u16,
62    pub fip: u64,
63    pub fdp: u64,
64    pub mxcsr: u32,
65    pub mxcsr_mask: u32,
66    pub st_space: [u128; 8],
67    pub xmm_space: [u128; 16],
68    // TODO: YMM/ZMM
69}
70
71impl Deref for FloatRegisters {
72    type Target = [u8];
73    /// Implements deref.
74    fn deref(&self) -> &[u8] {
75        unsafe {
76            slice::from_raw_parts(
77                self as *const FloatRegisters as *const u8,
78                mem::size_of::<FloatRegisters>(),
79            )
80        }
81    }
82}
83
84impl DerefMut for FloatRegisters {
85    /// Implements deref mut.
86    fn deref_mut(&mut self) -> &mut [u8] {
87        unsafe {
88            slice::from_raw_parts_mut(
89                self as *mut FloatRegisters as *mut u8,
90                mem::size_of::<FloatRegisters>(),
91            )
92        }
93    }
94}
95#[derive(Clone, Copy, Debug, Default)]
96#[repr(C, packed)]
97pub struct EnvRegisters {
98    pub fsbase: u64,
99    pub gsbase: u64,
100    // TODO: PKRU?
101}
102impl Deref for EnvRegisters {
103    type Target = [u8];
104    /// Implements deref.
105    fn deref(&self) -> &[u8] {
106        unsafe {
107            slice::from_raw_parts(
108                self as *const EnvRegisters as *const u8,
109                mem::size_of::<EnvRegisters>(),
110            )
111        }
112    }
113}
114
115impl DerefMut for EnvRegisters {
116    /// Implements deref mut.
117    fn deref_mut(&mut self) -> &mut [u8] {
118        unsafe {
119            slice::from_raw_parts_mut(
120                self as *mut EnvRegisters as *mut u8,
121                mem::size_of::<EnvRegisters>(),
122            )
123        }
124    }
125}
126
127#[derive(Clone, Copy, Debug, Default)]
128#[repr(C, packed)]
129pub struct Exception {
130    pub kind: usize,
131    pub code: usize,
132    pub address: usize,
133}
134impl Deref for Exception {
135    type Target = [u8];
136    /// Implements deref.
137    fn deref(&self) -> &[u8] {
138        unsafe {
139            slice::from_raw_parts(
140                self as *const Exception as *const u8,
141                mem::size_of::<Exception>(),
142            )
143        }
144    }
145}
146
147impl DerefMut for Exception {
148    /// Implements deref mut.
149    fn deref_mut(&mut self) -> &mut [u8] {
150        unsafe {
151            slice::from_raw_parts_mut(
152                self as *mut Exception as *mut u8,
153                mem::size_of::<Exception>(),
154            )
155        }
156    }
157}