Quellcode durchsuchen

hal: set int state and user mode in TrapContext::new

We always need to specify interrupt enabled state and whether we're in
user mode after we create a trap context otherwise the behavior may
alter on different platforms. So make it default.

Signed-off-by: greatbridf <greatbridf@icloud.com>
greatbridf vor 1 Woche
Ursprung
Commit
0fefeff6ab

+ 20 - 1
crates/eonix_hal/eonix_hal_traits/src/trap.rs

@@ -17,7 +17,26 @@ pub trait RawTrapContext: Copy {
     type FIrq: FnOnce(fn(irqno: usize));
     type FTimer: FnOnce(fn());
 
-    fn new() -> Self;
+    /// **Don't use this function unless you know what you're doing**
+    ///
+    /// Create a blank trap context.
+    ///
+    /// The context should be in a state that is ready to be used but whether
+    /// the interrupt is enabled or the context is in user mode is unspecified.
+    fn blank() -> Self;
+
+    /// Create a new trap context.
+    ///
+    /// The context will be in a state that is ready to be used. Whether the
+    /// interrupt is enabled or the context is in user mode is specified by
+    /// the arguments.
+    fn new(int_enabled: bool, user: bool) -> Self {
+        let mut me = Self::blank();
+        me.set_interrupt_enabled(int_enabled);
+        me.set_user_mode(user);
+
+        me
+    }
 
     fn trap_type(&self) -> TrapType<Self::FIrq, Self::FTimer>;
 

+ 1 - 1
crates/eonix_hal/src/arch/riscv64/trap/trap_context.rs

@@ -122,7 +122,7 @@ impl RawTrapContext for TrapContext {
     type FIrq = fn(handler: fn(irqno: usize));
     type FTimer = fn(handler: fn());
 
-    fn new() -> Self {
+    fn blank() -> Self {
         let mut sstatus = Sstatus::from_bits(0);
         sstatus.set_fs(FS::Initial);
         sstatus.set_sum(true);

+ 1 - 1
crates/eonix_hal/src/arch/x86_64/trap/trap_context.rs

@@ -109,7 +109,7 @@ impl RawTrapContext for TrapContext {
     type FIrq = impl FnOnce(fn(irqno: usize));
     type FTimer = fn(handler: fn());
 
-    fn new() -> Self {
+    fn blank() -> Self {
         Self {
             ..Default::default()
         }

+ 1 - 3
src/kernel/syscall/procops.rs

@@ -245,10 +245,8 @@ async fn execve(
     thread.set_name(dentry.get_name());
 
     let mut trap_ctx = thread.trap_ctx.borrow();
-    *trap_ctx = TrapContext::new();
+    *trap_ctx = TrapContext::new(true, true);
 
-    trap_ctx.set_user_mode(true);
-    trap_ctx.set_interrupt_enabled(true);
     trap_ctx.set_program_counter(load_info.entry_ip.addr());
     trap_ctx.set_stack_pointer(load_info.sp.addr());
 

+ 1 - 3
src/kernel/task.rs

@@ -196,10 +196,8 @@ where
 
     let mut output = UnsafeCell::new(None);
 
-    let mut trap_ctx = TrapContext::new();
+    let mut trap_ctx = TrapContext::new(true, false);
 
-    trap_ctx.set_user_mode(false);
-    trap_ctx.set_interrupt_enabled(true);
     trap_ctx.set_kernel_call_frame(
         symbol_addr!(execute::<F>),
         &stack,

+ 1 - 3
src/kernel/task/thread.rs

@@ -200,11 +200,9 @@ impl ThreadBuilder {
     }
 
     pub fn entry(mut self, entry: VAddr, stack_pointer: VAddr) -> Self {
-        let mut trap_ctx = TrapContext::new();
-        trap_ctx.set_user_mode(true);
+        let mut trap_ctx = TrapContext::new(true, true);
         trap_ctx.set_program_counter(entry.addr());
         trap_ctx.set_stack_pointer(stack_pointer.addr());
-        trap_ctx.set_interrupt_enabled(true);
 
         self.trap_ctx = Some(trap_ctx);
         self

+ 2 - 6
src/lib.rs

@@ -66,11 +66,9 @@ fn kernel_init(mut data: eonix_hal::bootstrap::BootStrapData) -> ! {
 
     drop(data);
 
-    let mut ctx = TrapContext::new();
+    let mut ctx = TrapContext::new(true, false);
     let stack = KernelStack::new();
 
-    ctx.set_interrupt_enabled(true);
-    ctx.set_user_mode(false);
     ctx.set_kernel_call_frame(symbol_addr!(standard_main), &stack, None, &[]);
 
     core::mem::forget(stack);
@@ -89,11 +87,9 @@ fn kernel_ap_main(_stack_range: PRange) -> ! {
 
     println_debug!("AP{} started", CPU::local().cpuid());
 
-    let mut ctx = TrapContext::new();
+    let mut ctx = TrapContext::new(true, false);
     let stack = KernelStack::new();
 
-    ctx.set_interrupt_enabled(true);
-    ctx.set_user_mode(false);
     ctx.set_kernel_call_frame(symbol_addr!(standard_main), &stack, None, &[]);
 
     core::mem::forget(stack);