|
1 | 1 | //! Structures and functions for user space.
|
2 | 2 |
|
| 3 | +use core::ops::{Deref, DerefMut}; |
| 4 | + |
3 | 5 | use memory_addr::VirtAddr;
|
4 | 6 |
|
5 | 7 | use crate::asm::{read_thread_pointer, write_thread_pointer};
|
| 8 | +use crate::trap::{ExceptionKind, ReturnReason}; |
6 | 9 | use crate::TrapFrame;
|
7 | 10 |
|
8 | 11 | /// Context to enter user space.
|
9 |
| -pub struct UspaceContext(TrapFrame); |
| 12 | +pub struct UserContext(TrapFrame); |
10 | 13 |
|
11 |
| -impl UspaceContext { |
| 14 | +impl UserContext { |
12 | 15 | /// Creates an empty context with all registers set to zero.
|
13 | 16 | pub const fn empty() -> Self {
|
14 | 17 | unsafe { core::mem::MaybeUninit::zeroed().assume_init() }
|
@@ -46,41 +49,11 @@ impl UspaceContext {
|
46 | 49 | ///
|
47 | 50 | /// It restores the user registers and jumps to the user entry point
|
48 | 51 | /// (saved in `rip`).
|
49 |
| - /// When an exception or syscall occurs, the kernel stack pointer is |
50 |
| - /// switched to `kstack_top`. |
51 |
| - /// |
52 |
| - /// # Safety |
53 | 52 | ///
|
54 |
| - /// This function is unsafe because it changes processor mode and the stack. |
55 |
| - pub unsafe fn enter_uspace(&self, kstack_top: VirtAddr) -> ! { |
56 |
| - crate::asm::disable_irqs(); |
57 |
| - assert_eq!(super::gdt::read_tss_rsp0(), kstack_top); |
58 |
| - switch_to_user_fs_base(&self.0); |
59 |
| - unsafe { |
60 |
| - core::arch::asm!(" |
61 |
| - mov rsp, {tf} |
62 |
| - pop rax |
63 |
| - pop rcx |
64 |
| - pop rdx |
65 |
| - pop rbx |
66 |
| - pop rbp |
67 |
| - pop rsi |
68 |
| - pop rdi |
69 |
| - pop r8 |
70 |
| - pop r9 |
71 |
| - pop r10 |
72 |
| - pop r11 |
73 |
| - pop r12 |
74 |
| - pop r13 |
75 |
| - pop r14 |
76 |
| - pop r15 |
77 |
| - add rsp, 32 // skip fs_base, vector, error_code |
78 |
| - swapgs |
79 |
| - iretq", |
80 |
| - tf = in(reg) &self.0, |
81 |
| - options(noreturn), |
82 |
| - ) |
83 |
| - } |
| 53 | + /// This function returns when an exception or syscall occurs. |
| 54 | + pub fn run(&mut self) -> ReturnReason { |
| 55 | + // TODO: implement |
| 56 | + ReturnReason::Unknown |
84 | 57 | }
|
85 | 58 | }
|
86 | 59 |
|
@@ -109,16 +82,26 @@ pub fn switch_to_user_fs_base(tf: &TrapFrame) {
|
109 | 82 | }
|
110 | 83 | }
|
111 | 84 |
|
112 |
| -impl core::ops::Deref for UspaceContext { |
| 85 | +impl Deref for UserContext { |
113 | 86 | type Target = TrapFrame;
|
114 | 87 |
|
115 | 88 | fn deref(&self) -> &Self::Target {
|
116 | 89 | &self.0
|
117 | 90 | }
|
118 | 91 | }
|
119 | 92 |
|
120 |
| -impl core::ops::DerefMut for UspaceContext { |
| 93 | +impl DerefMut for UserContext { |
121 | 94 | fn deref_mut(&mut self) -> &mut Self::Target {
|
122 | 95 | &mut self.0
|
123 | 96 | }
|
124 | 97 | }
|
| 98 | + |
| 99 | +#[derive(Debug, Clone, Copy)] |
| 100 | +pub struct ExceptionInfo {} |
| 101 | + |
| 102 | +impl ExceptionInfo { |
| 103 | + pub fn kind(&self) -> ExceptionKind { |
| 104 | + // TODO: implement |
| 105 | + ExceptionKind::Other |
| 106 | + } |
| 107 | +} |
0 commit comments