|
| 1 | +// Licensed under the Apache License, Version 2.0 or the MIT License. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | +// Copyright Tock Contributors 2024. |
| 4 | + |
| 5 | +use core::fmt::Write; |
| 6 | + |
| 7 | +use crate::registers::bits32::eflags::{EFlags, EFLAGS}; |
| 8 | + |
| 9 | +use kernel::process::FunctionCall; |
| 10 | +use kernel::syscall::{ContextSwitchReason, Syscall, SyscallReturn, UserspaceKernelBoundary}; |
| 11 | +use kernel::ErrorCode; |
| 12 | + |
| 13 | +use crate::interrupts::{IDT_RESERVED_EXCEPTIONS, SYSCALL_VECTOR}; |
| 14 | +use crate::segmentation::{USER_CODE, USER_DATA}; |
| 15 | + |
| 16 | +use super::UserContext; |
| 17 | + |
| 18 | +/// Defines the usermode-kernelmode ABI for x86 platforms. |
| 19 | +pub struct Boundary; |
| 20 | + |
| 21 | +impl Default for Boundary { |
| 22 | + fn default() -> Self { |
| 23 | + Self::new() |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl Boundary { |
| 28 | + /// Minimum required size for initial process memory. |
| 29 | + /// |
| 30 | + /// Need at least 5 dwords of initial stack space for CRT 0: |
| 31 | + /// |
| 32 | + /// - 1 dword for initial upcall return address (although this will be zero for init_fn) |
| 33 | + /// - 4 dwords of scratch space for invoking memop syscalls |
| 34 | + const MIN_APP_BRK: u32 = 5 * core::mem::size_of::<usize>() as u32; |
| 35 | + |
| 36 | + /// Constructs a new instance of `SysCall`. |
| 37 | + pub fn new() -> Self { |
| 38 | + Self |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl UserspaceKernelBoundary for Boundary { |
| 43 | + type StoredState = UserContext; |
| 44 | + |
| 45 | + fn initial_process_app_brk_size(&self) -> usize { |
| 46 | + Self::MIN_APP_BRK as usize |
| 47 | + } |
| 48 | + |
| 49 | + unsafe fn initialize_process( |
| 50 | + &self, |
| 51 | + accessible_memory_start: *const u8, |
| 52 | + app_brk: *const u8, |
| 53 | + state: &mut Self::StoredState, |
| 54 | + ) -> Result<(), ()> { |
| 55 | + if (app_brk as u32 - accessible_memory_start as u32) < Self::MIN_APP_BRK { |
| 56 | + return Err(()); |
| 57 | + } |
| 58 | + |
| 59 | + // We pre-allocate 16 bytes on the stack for initial upcall arguments. |
| 60 | + let esp = (app_brk as u32) - 16; |
| 61 | + |
| 62 | + let mut eflags = EFlags::new(); |
| 63 | + eflags.0.modify(EFLAGS::FLAGS_IF::SET); |
| 64 | + |
| 65 | + state.eax = 0; |
| 66 | + state.ebx = 0; |
| 67 | + state.ecx = 0; |
| 68 | + state.edx = 0; |
| 69 | + state.esi = 0; |
| 70 | + state.edi = 0; |
| 71 | + state.ebp = 0; |
| 72 | + state.esp = esp; |
| 73 | + state.eip = 0; |
| 74 | + state.eflags = eflags.0.get(); |
| 75 | + state.cs = USER_CODE.bits() as u32; |
| 76 | + state.ss = USER_DATA.bits() as u32; |
| 77 | + state.ds = USER_DATA.bits() as u32; |
| 78 | + state.es = USER_DATA.bits() as u32; |
| 79 | + state.fs = USER_DATA.bits() as u32; |
| 80 | + state.gs = USER_DATA.bits() as u32; |
| 81 | + |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | + |
| 85 | + unsafe fn set_syscall_return_value( |
| 86 | + &self, |
| 87 | + _accessible_memory_start: *const u8, |
| 88 | + _app_brk: *const u8, |
| 89 | + state: &mut Self::StoredState, |
| 90 | + return_value: SyscallReturn, |
| 91 | + ) -> Result<(), ()> { |
| 92 | + kernel::utilities::arch_helpers::encode_syscall_return_trd104( |
| 93 | + &kernel::utilities::arch_helpers::TRD104SyscallReturn::from_syscall_return( |
| 94 | + return_value, |
| 95 | + ), |
| 96 | + &mut state.ebx, |
| 97 | + &mut state.ecx, |
| 98 | + &mut state.edx, |
| 99 | + &mut state.edi, |
| 100 | + ); |
| 101 | + |
| 102 | + Ok(()) |
| 103 | + } |
| 104 | + |
| 105 | + unsafe fn set_process_function( |
| 106 | + &self, |
| 107 | + _accessible_memory_start: *const u8, |
| 108 | + _app_brk: *const u8, |
| 109 | + state: &mut Self::StoredState, |
| 110 | + upcall: FunctionCall, |
| 111 | + ) -> Result<(), ()> { |
| 112 | + state.ebx = upcall.argument0 as u32; |
| 113 | + state.ecx = upcall.argument1 as u32; |
| 114 | + state.edx = upcall.argument2 as u32; |
| 115 | + state.edi = upcall.argument3.as_usize() as u32; |
| 116 | + |
| 117 | + // The next time we switch to this process, we will directly jump to the upcall. When the |
| 118 | + // upcall issues `ret`, it will return to wherever the yield syscall was invoked. |
| 119 | + state.eip = upcall.pc.addr() as u32; |
| 120 | + |
| 121 | + Ok(()) |
| 122 | + } |
| 123 | + |
| 124 | + unsafe fn switch_to_process( |
| 125 | + &self, |
| 126 | + _accessible_memory_start: *const u8, |
| 127 | + _app_brk: *const u8, |
| 128 | + state: &mut Self::StoredState, |
| 129 | + ) -> (ContextSwitchReason, Option<*const u8>) { |
| 130 | + // Sanity check: don't try to run a faulted app |
| 131 | + if state.exception != 0 || state.err_code != 0 { |
| 132 | + let stack_ptr = state.esp as *mut u8; |
| 133 | + return (ContextSwitchReason::Fault, Some(stack_ptr)); |
| 134 | + } |
| 135 | + |
| 136 | + let mut err_code = 0; |
| 137 | + let int_num = unsafe { super::switch_to_user(state, &mut err_code) }; |
| 138 | + |
| 139 | + let reason = match int_num as u8 { |
| 140 | + 0..IDT_RESERVED_EXCEPTIONS => { |
| 141 | + state.exception = int_num as u8; |
| 142 | + state.err_code = err_code; |
| 143 | + ContextSwitchReason::Fault |
| 144 | + } |
| 145 | + |
| 146 | + SYSCALL_VECTOR => { |
| 147 | + let num = state.eax as u8; |
| 148 | + |
| 149 | + let arg0 = state.ebx; |
| 150 | + let arg1 = state.ecx; |
| 151 | + let arg2 = state.edx; |
| 152 | + let arg3 = state.edi; |
| 153 | + |
| 154 | + Syscall::from_register_arguments( |
| 155 | + num, |
| 156 | + arg0 as usize, |
| 157 | + (arg1 as usize).into(), |
| 158 | + (arg2 as usize).into(), |
| 159 | + (arg3 as usize).into(), |
| 160 | + ) |
| 161 | + .map_or(ContextSwitchReason::Fault, |syscall| { |
| 162 | + ContextSwitchReason::SyscallFired { syscall } |
| 163 | + }) |
| 164 | + } |
| 165 | + _ => ContextSwitchReason::Interrupted, |
| 166 | + }; |
| 167 | + |
| 168 | + let stack_ptr = state.esp as *const u8; |
| 169 | + |
| 170 | + (reason, Some(stack_ptr)) |
| 171 | + } |
| 172 | + |
| 173 | + unsafe fn print_context( |
| 174 | + &self, |
| 175 | + _accessible_memory_start: *const u8, |
| 176 | + _app_brk: *const u8, |
| 177 | + state: &Self::StoredState, |
| 178 | + writer: &mut dyn Write, |
| 179 | + ) { |
| 180 | + let _ = writeln!(writer, "{}", state); |
| 181 | + } |
| 182 | + |
| 183 | + fn store_context( |
| 184 | + &self, |
| 185 | + _state: &Self::StoredState, |
| 186 | + _out: &mut [u8], |
| 187 | + ) -> Result<usize, ErrorCode> { |
| 188 | + unimplemented!() |
| 189 | + } |
| 190 | +} |
0 commit comments