1use alloc::boxed::Box;
6use core::arch::x86_64::{_fxrstor64, _fxsave64, _xrstor64, _xsave64};
7
8use bitflags::bitflags;
9use cfg_if::cfg_if;
10use ostd_pod::{FromZeros, IntoBytes};
11use spin::Once;
12use x86::bits64::segmentation::{rdfsbase, rdgsbase, swapgs, wrfsbase, wrgsbase};
13use x86_64::registers::{
14 control::{Cr0, Cr0Flags},
15 rflags::RFlags,
16 xcontrol::XCr0,
17};
18
19use crate::{
20 arch::{
21 irq::HwIrqLine,
22 trap::{RawUserContext, TrapFrame},
23 },
24 cpu::PrivilegeLevel,
25 debug,
26 irq::{DisabledLocalIrqGuard, call_irq_callback_functions},
27 mm::Vaddr,
28 user::{ReturnReason, UserContextApi, UserContextApiInternal},
29};
30
31cfg_if! {
32 if #[cfg(feature = "cvm_guest")] {
33 mod tdx;
34
35 use tdx::VirtualizationExceptionHandler;
36 }
37}
38
39#[repr(C)]
41#[derive(Clone, Debug, Default)]
42pub struct UserContext {
43 user_context: RawUserContext,
44 exception: Option<CpuException>,
45}
46
47#[expect(missing_docs)]
49#[repr(C)]
50#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
51pub struct GeneralRegs {
52 pub rax: usize,
53 pub rbx: usize,
54 pub rcx: usize,
55 pub rdx: usize,
56 pub rsi: usize,
57 pub rdi: usize,
58 pub rbp: usize,
59 pub rsp: usize,
60 pub r8: usize,
61 pub r9: usize,
62 pub r10: usize,
63 pub r11: usize,
64 pub r12: usize,
65 pub r13: usize,
66 pub r14: usize,
67 pub r15: usize,
68 pub rip: usize,
69 pub rflags: usize,
70}
71
72#[derive(Clone, Copy, Debug, Default)]
74pub struct FsBase(usize);
75
76impl FsBase {
77 pub fn new(addr: usize) -> Self {
79 Self(addr)
80 }
81
82 pub fn addr(&self) -> usize {
84 self.0
85 }
86
87 pub fn save(&mut self) {
89 self.0 = unsafe { rdfsbase() as usize };
91 }
92
93 pub fn load(&self) {
95 unsafe { wrfsbase(self.0 as u64) }
97 }
98}
99
100#[derive(Clone, Copy, Debug, Default)]
102pub struct GsBase(usize);
103
104impl GsBase {
105 pub fn new(addr: usize) -> Self {
107 Self(addr)
108 }
109
110 pub fn addr(&self) -> usize {
112 self.0
113 }
114
115 pub fn save(&mut self, _guard: &DisabledLocalIrqGuard) {
117 unsafe {
121 swapgs();
122 self.0 = rdgsbase() as usize;
123 swapgs();
124 }
125 }
126
127 pub fn load(&self, _guard: &DisabledLocalIrqGuard) {
129 unsafe {
133 swapgs();
134 wrgsbase(self.0 as u64);
135 swapgs();
136 }
137 }
138}
139
140#[derive(Clone, Copy, Debug, Eq, PartialEq)]
157pub enum CpuException {
158 DivisionError,
160 Debug,
162 NonMaskableInterrupt,
164 BreakPoint,
166 Overflow,
168 BoundRangeExceeded,
170 InvalidOpcode,
172 DeviceNotAvailable,
174 DoubleFault,
176 CoprocessorSegmentOverrun,
178 InvalidTss(SelectorErrorCode),
180 SegmentNotPresent(SelectorErrorCode),
182 StackSegmentFault(SelectorErrorCode),
184 GeneralProtectionFault(Option<SelectorErrorCode>),
186 PageFault(RawPageFaultInfo),
188 X87FloatingPointException,
191 AlignmentCheck,
193 MachineCheck,
195 SIMDFloatingPointException,
197 VirtualizationException,
199 ControlProtectionException,
201 HypervisorInjectionException,
204 VMMCommunicationException,
206 SecurityException,
208 Reserved,
211}
212
213impl CpuException {
214 pub(crate) fn new(trap_num: usize, error_code: usize) -> Option<Self> {
215 let exception = match trap_num {
216 0 => Self::DivisionError,
217 1 => Self::Debug,
218 2 => Self::NonMaskableInterrupt,
219 3 => Self::BreakPoint,
220 4 => Self::Overflow,
221 5 => Self::BoundRangeExceeded,
222 6 => Self::InvalidOpcode,
223 7 => Self::DeviceNotAvailable,
224 8 => {
225 debug_assert_eq!(error_code, 0);
227 Self::DoubleFault
228 }
229 9 => Self::CoprocessorSegmentOverrun,
230 10 => Self::InvalidTss(SelectorErrorCode(error_code)),
231 11 => Self::SegmentNotPresent(SelectorErrorCode(error_code)),
232 12 => Self::StackSegmentFault(SelectorErrorCode(error_code)),
233 13 => {
234 let error_code = if error_code == 0 {
235 None
236 } else {
237 Some(SelectorErrorCode(error_code))
238 };
239 Self::GeneralProtectionFault(error_code)
240 }
241 14 => {
242 let page_fault_addr = x86_64::registers::control::Cr2::read_raw() as usize;
243 Self::PageFault(RawPageFaultInfo {
244 error_code: PageFaultErrorCode::from_bits(error_code).unwrap(),
245 addr: page_fault_addr,
246 })
247 }
248 16 => Self::X87FloatingPointException,
250 17 => Self::AlignmentCheck,
251 18 => Self::MachineCheck,
252 19 => Self::SIMDFloatingPointException,
253 20 => Self::VirtualizationException,
254 21 => Self::ControlProtectionException,
255 28 => Self::HypervisorInjectionException,
257 29 => Self::VMMCommunicationException,
258 30 => Self::SecurityException,
259 15 | 22..=27 | 31 => Self::Reserved,
261 _ => return None,
262 };
263
264 Some(exception)
265 }
266
267 const fn type_(&self) -> CpuExceptionType {
268 match self {
269 Self::Debug => CpuExceptionType::FaultOrTrap,
270 Self::NonMaskableInterrupt => CpuExceptionType::Interrupt,
271 Self::BreakPoint | Self::Overflow => CpuExceptionType::Trap,
272 Self::DoubleFault | Self::MachineCheck => CpuExceptionType::Abort,
273 Self::Reserved => CpuExceptionType::Reserved,
274 _ => CpuExceptionType::Fault,
275 }
276 }
277
278 pub(crate) const fn is_cpu_exception(trap_num: usize) -> bool {
279 trap_num <= 31
280 }
281}
282
283#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
287pub struct SelectorErrorCode(usize);
288
289impl UserContext {
290 pub fn general_regs(&self) -> &GeneralRegs {
292 &self.user_context.general
293 }
294
295 pub fn general_regs_mut(&mut self) -> &mut GeneralRegs {
297 &mut self.user_context.general
298 }
299
300 pub fn take_exception(&mut self) -> Option<CpuException> {
302 self.exception.take()
303 }
304}
305
306impl UserContextApiInternal for UserContext {
307 fn execute<F>(&mut self, mut has_kernel_event: F) -> ReturnReason
308 where
309 F: FnMut() -> bool,
310 {
311 self.user_context.general.rflags |= (RFlags::INTERRUPT_FLAG | RFlags::ID).bits() as usize;
314
315 const SYSCALL_TRAPNUM: usize = 0x100;
316
317 loop {
319 crate::task::scheduler::might_preempt();
320 self.user_context.run();
321
322 let exception =
323 CpuException::new(self.user_context.trap_num, self.user_context.error_code);
324 match exception {
325 #[cfg(feature = "cvm_guest")]
326 Some(CpuException::VirtualizationException) => {
327 let ve_handler = VirtualizationExceptionHandler::new();
328 crate::arch::irq::enable_local();
331 ve_handler.handle(self);
332 }
333 Some(exception) if exception.type_().is_fault_or_trap() => {
334 crate::arch::irq::enable_local();
335 self.exception = Some(exception);
336 return ReturnReason::UserException;
337 }
338 Some(exception) => {
339 panic!(
340 "cannot handle user CPU exception: {:?}, trapframe: {:?}",
341 exception,
342 self.as_trap_frame()
343 );
344 }
345 None if self.user_context.trap_num == SYSCALL_TRAPNUM => {
346 crate::arch::irq::enable_local();
347 return ReturnReason::UserSyscall;
348 }
349 None => {
350 call_irq_callback_functions(
351 &self.as_trap_frame(),
352 &HwIrqLine::new(self.as_trap_frame().trap_num as u8),
353 PrivilegeLevel::User,
354 );
355 crate::arch::irq::enable_local();
356 }
357 }
358
359 if has_kernel_event() {
360 break ReturnReason::KernelEvent;
361 }
362 }
363 }
364
365 fn as_trap_frame(&self) -> TrapFrame {
366 TrapFrame {
367 rax: self.user_context.general.rax,
368 rbx: self.user_context.general.rbx,
369 rcx: self.user_context.general.rcx,
370 rdx: self.user_context.general.rdx,
371 rsi: self.user_context.general.rsi,
372 rdi: self.user_context.general.rdi,
373 rbp: self.user_context.general.rbp,
374 rsp: self.user_context.general.rsp,
375 r8: self.user_context.general.r8,
376 r9: self.user_context.general.r9,
377 r10: self.user_context.general.r10,
378 r11: self.user_context.general.r11,
379 r12: self.user_context.general.r12,
380 r13: self.user_context.general.r13,
381 r14: self.user_context.general.r14,
382 r15: self.user_context.general.r15,
383 trap_num: self.user_context.trap_num,
384 error_code: self.user_context.error_code,
385 rip: self.user_context.general.rip,
386 cs: 0,
387 rflags: self.user_context.general.rflags,
388 }
389 }
390}
391
392#[derive(Clone, Copy, Debug, Eq, PartialEq)]
404pub enum CpuExceptionType {
405 Fault,
407 Trap,
409 FaultOrTrap,
411 Interrupt,
413 Abort,
415 Reserved,
417}
418
419impl CpuExceptionType {
420 pub fn is_fault_or_trap(self) -> bool {
422 match self {
423 CpuExceptionType::Trap | CpuExceptionType::Fault | CpuExceptionType::FaultOrTrap => {
424 true
425 }
426 CpuExceptionType::Abort | CpuExceptionType::Interrupt | CpuExceptionType::Reserved => {
427 false
428 }
429 }
430 }
431}
432
433#[derive(Clone, Copy, Debug, Eq, PartialEq)]
435pub struct RawPageFaultInfo {
436 pub error_code: PageFaultErrorCode,
438 pub addr: Vaddr,
440}
441
442bitflags! {
443 pub struct PageFaultErrorCode : usize{
445 const PRESENT = 1 << 0;
447 const WRITE = 1 << 1;
449 const USER = 1 << 2;
451 const RESERVED = 1 << 3;
454 const INSTRUCTION = 1 << 4;
456 const PROTECTION = 1 << 5;
459 const SHADOW_STACK = 1 << 6;
461 const HLAT = 1 << 7;
463 const SGX = 1 << 15;
466 }
467}
468
469impl UserContextApi for UserContext {
470 fn trap_number(&self) -> usize {
471 self.user_context.trap_num
472 }
473
474 fn trap_error_code(&self) -> usize {
475 self.user_context.error_code
476 }
477
478 fn set_instruction_pointer(&mut self, ip: usize) {
479 self.set_rip(ip);
480 }
481
482 fn set_stack_pointer(&mut self, sp: usize) {
483 self.set_rsp(sp)
484 }
485
486 fn stack_pointer(&self) -> usize {
487 self.rsp()
488 }
489
490 fn instruction_pointer(&self) -> usize {
491 self.rip()
492 }
493}
494
495macro_rules! cpu_context_impl_getter_setter {
496 ( $( [ $field: ident, $setter_name: ident] ),*) => {
497 impl UserContext {
498 $(
499 #[doc = concat!("Gets the value of ", stringify!($field))]
500 #[inline(always)]
501 pub fn $field(&self) -> usize {
502 self.user_context.general.$field
503 }
504
505 #[doc = concat!("Sets the value of ", stringify!(field))]
506 #[inline(always)]
507 pub fn $setter_name(&mut self, $field: usize) {
508 self.user_context.general.$field = $field;
509 }
510 )*
511 }
512 };
513}
514
515cpu_context_impl_getter_setter!(
516 [rax, set_rax],
517 [rbx, set_rbx],
518 [rcx, set_rcx],
519 [rdx, set_rdx],
520 [rsi, set_rsi],
521 [rdi, set_rdi],
522 [rbp, set_rbp],
523 [rsp, set_rsp],
524 [r8, set_r8],
525 [r9, set_r9],
526 [r10, set_r10],
527 [r11, set_r11],
528 [r12, set_r12],
529 [r13, set_r13],
530 [r14, set_r14],
531 [r15, set_r15],
532 [rip, set_rip],
533 [rflags, set_rflags]
534);
535
536#[derive(Debug)]
540pub struct FpuContext {
541 xsave_area: Box<XSaveArea>,
542 area_size: usize,
543}
544
545impl FpuContext {
546 pub fn new() -> Self {
548 let mut area_size = size_of::<FxSaveArea>();
549 if let Some(xsave_area_size) = XSAVE_AREA_SIZE.get() {
550 area_size = area_size.max(*xsave_area_size);
551 }
552
553 Self {
554 xsave_area: Box::new(XSaveArea::new()),
555 area_size,
556 }
557 }
558
559 pub fn save(&mut self) {
561 let mem_addr = self.as_bytes_mut().as_mut_ptr();
562
563 if XSTATE_MAX_FEATURES.is_completed() {
564 unsafe { _xsave64(mem_addr, XFEATURE_MASK_USER_RESTORE) };
565 } else {
566 unsafe { _fxsave64(mem_addr) };
567 }
568
569 debug!("Save FPU context");
570 }
571
572 pub fn load(&self) {
574 let mem_addr = self.as_bytes().as_ptr();
575
576 if let Some(xstate_max_features) = XSTATE_MAX_FEATURES.get() {
577 let rs_mask = XFEATURE_MASK_USER_RESTORE & *xstate_max_features;
578
579 unsafe { _xrstor64(mem_addr, rs_mask) };
580 } else {
581 unsafe { _fxrstor64(mem_addr) };
582 }
583
584 debug!("Load FPU context");
585 }
586
587 pub fn as_bytes(&self) -> &[u8] {
589 &self.xsave_area.as_bytes()[..self.area_size]
590 }
591
592 pub fn as_bytes_mut(&mut self) -> &mut [u8] {
594 &mut self.xsave_area.as_mut_bytes()[..self.area_size]
595 }
596}
597
598impl Default for FpuContext {
599 fn default() -> Self {
600 Self::new()
601 }
602}
603
604impl Clone for FpuContext {
605 fn clone(&self) -> Self {
606 let mut xsave_area = Box::new(XSaveArea::new());
607 xsave_area.fxsave_area = self.xsave_area.fxsave_area;
608 xsave_area.features = self.xsave_area.features;
609 xsave_area.compaction = self.xsave_area.compaction;
610 if self.area_size > size_of::<FxSaveArea>() {
611 let len = self.area_size - size_of::<FxSaveArea>() - 64;
612 xsave_area.extended_state_area[..len]
613 .copy_from_slice(&self.xsave_area.extended_state_area[..len]);
614 }
615
616 Self {
617 xsave_area,
618 area_size: self.area_size,
619 }
620 }
621}
622
623#[repr(C)]
625#[repr(align(64))]
626#[derive(Clone, Copy, Debug, Pod)]
627struct XSaveArea {
628 fxsave_area: FxSaveArea,
629 features: u64,
630 compaction: u64,
631 reserved: [u64; 6],
632 extended_state_area: [u8; MAX_XSAVE_AREA_SIZE - size_of::<FxSaveArea>() - 64],
633}
634
635impl XSaveArea {
636 fn new() -> Self {
637 let features = if let Some(xstate_max_features) = XSTATE_MAX_FEATURES.get() {
638 XCr0::read().bits() & *xstate_max_features
639 } else {
640 0
641 };
642
643 let mut xsave_area = Self::new_zeroed();
644 xsave_area.fxsave_area.control = 0x037F;
647 xsave_area.fxsave_area.tag = 0;
654 xsave_area.fxsave_area.mxcsr = 0x1F80;
655 xsave_area.features = features;
656
657 xsave_area
658 }
659}
660
661#[repr(C)]
663#[repr(align(16))]
664#[derive(Clone, Copy, Debug, Pod)]
665struct FxSaveArea {
666 control: u16, status: u16, tag: u8, reserved1: u8, op: u16, ip: u32, cs: u32, dp: u32, ds: u32, mxcsr: u32, mxcsr_mask: u32, st_space: [u32; 32], xmm_space: [u32; 64], reserved2: [u32; 12], reserved3: [u32; 12], }
682
683static XSTATE_MAX_FEATURES: Once<u64> = Once::new();
685
686const XFEATURE_MASK_USER_RESTORE: u64 = 0b1110_0111;
690
691static XSAVE_AREA_SIZE: Once<usize> = Once::new();
693
694const MAX_XSAVE_AREA_SIZE: usize = 4096;
696
697pub(in crate::arch) fn enable_essential_features() {
698 use super::extension::{IsaExtensions, has_extensions};
699
700 if has_extensions(IsaExtensions::XSAVE) {
701 XSTATE_MAX_FEATURES.call_once(|| super::cpuid::query_xstate_max_features().unwrap());
702 XSAVE_AREA_SIZE.call_once(|| {
703 let xsave_area_size = super::cpuid::query_xsave_area_size().unwrap() as usize;
704 assert!(xsave_area_size <= MAX_XSAVE_AREA_SIZE);
705 xsave_area_size
706 });
707 }
708
709 {
712 let mut cr0 = Cr0::read();
713 cr0.remove(Cr0Flags::TASK_SWITCHED | Cr0Flags::EMULATE_COPROCESSOR);
714
715 unsafe {
716 Cr0::write(cr0);
717 core::arch::asm!("fninit");
719 }
720 }
721}