Initializes syscall handler on Rust side (#1075)

This commit is contained in:
Putta Khunchalee 2024-10-27 23:42:07 +07:00 committed by GitHub
parent c8a220bea2
commit 4e2d4c3991
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 9 deletions

View File

@ -11,6 +11,7 @@ use core::ops::Deref;
///
/// This type work by making itself not [Send] and [Sync], which prevent the caller from storing it
/// at a global level.
#[repr(transparent)]
pub struct BorrowedArc<T>(*const T); // A pointer make this type automatically !Send and !Sync.
impl<T> BorrowedArc<T> {

View File

@ -52,7 +52,11 @@ pub unsafe fn run_with_context(
pub fn current_thread() -> BorrowedArc<Thread> {
// It does not matter if we are on a different CPU after we load the Context::thread because it
// is going to be the same one since it represent the current thread.
unsafe { BorrowedArc::new(Context::load_fixed_ptr::<{ offset_of!(Base, thread) }, _>()) }
unsafe { BorrowedArc::new(Context::load_fixed_ptr::<{ current_thread_offset() }, _>()) }
}
pub const fn current_thread_offset() -> usize {
offset_of!(Base, thread)
}
/// # Interrupt safety

View File

@ -16,10 +16,11 @@ mod cell;
/// Do not try to access any [`PrivateCell`] fields from interrupt handler because it might
/// currently locked, which will can cause a panic.
pub struct Thread {
active_pins: AtomicU8, // td_critnest
active_interrupts: AtomicU8, // td_intr_nesting_level
active_mutexes: PrivateCell<u16>, // td_locks
sleeping: Gutex<usize>, // td_wchan
active_pins: AtomicU8, // td_critnest
active_interrupts: AtomicU8, // td_intr_nesting_level
active_mutexes: PrivateCell<u16>, // td_locks
sleeping: Gutex<usize>, // td_wchan
profiling_ticks: PrivateCell<u32>, // td_pticks
}
impl Thread {
@ -39,6 +40,7 @@ impl Thread {
active_interrupts: AtomicU8::new(0),
active_mutexes: PrivateCell::new(0),
sleeping: gg.spawn(0),
profiling_ticks: PrivateCell::new(0),
}
}
@ -76,4 +78,8 @@ impl Thread {
pub fn sleeping_mut(&self) -> GutexWriteGuard<usize> {
self.sleeping.write()
}
pub fn profiling_ticks_mut(&self) -> RefMut<u32> {
unsafe { self.profiling_ticks.borrow_mut(self) }
}
}

View File

@ -1,5 +1,6 @@
use crate::config::boot_env;
use crate::context::current_thread;
use crate::context::{current_thread, BorrowedArc};
use crate::proc::Thread;
use core::sync::atomic::Ordering;
use obconf::BootEnv;
@ -22,6 +23,17 @@ pub extern "C" fn interrupt_handler(frame: &mut TrapFrame) {
unsafe { td.active_interrupts().fetch_sub(1, Ordering::Relaxed) };
}
/// Main entry point for `syscall` instruction.
///
/// This will be called by an inline assembly.
///
/// See `amd64_syscall` function on the PS4 for a reference.
pub extern "C" fn syscall_handler(td: BorrowedArc<Thread>) {
*td.profiling_ticks_mut() = 0;
todo!()
}
/// Predefined interrupt vector number.
#[allow(dead_code)] // Used by inline assembly.
#[repr(u32)]

View File

@ -1,5 +1,7 @@
use crate::context::{current_trap_rsp_offset, current_user_rsp_offset, ContextArgs};
use crate::trap::interrupt_handler;
use crate::context::{
current_thread_offset, current_trap_rsp_offset, current_user_rsp_offset, ContextArgs,
};
use crate::trap::{interrupt_handler, syscall_handler};
use bitfield_struct::bitfield;
use core::arch::{asm, global_asm};
use core::mem::{transmute, zeroed};
@ -202,9 +204,13 @@ global_asm!(
"swapgs",
"mov gs:[{user_rsp}], rsp", // Save user RSP.
"mov rsp, gs:[{trap_rsp}]",
"mov rdi, gs:[{td}]",
"call {handler}",
"ud2",
user_rsp = const current_user_rsp_offset(),
trap_rsp = const current_trap_rsp_offset()
trap_rsp = const current_trap_rsp_offset(),
td = const current_thread_offset(),
handler = sym syscall_handler
);
// See Xfast_syscall32 on the PS4 for a reference.