feat: objects: tcb: add tid and implement activate_vspace

This commit is contained in:
Paul Pan 2024-06-15 19:59:28 +08:00
parent 39dab16baa
commit ecb8b7a54f
2 changed files with 27 additions and 14 deletions

View File

@ -79,7 +79,7 @@ impl TrapContextOps for TrapContext {
T::Interrupt(I::SupervisorTimer) => { T::Interrupt(I::SupervisorTimer) => {
Timer::do_tick(); Timer::do_tick();
if !from_kernel { if !from_kernel {
tcb.timer_tick(); tcb.do_tick();
} }
}, },
T::Exception(E::UserEnvCall) => { T::Exception(E::UserEnvCall) => {

View File

@ -1,9 +1,8 @@
use crate::{ use crate::arch::{layout::mmap_phys_to_virt, trap::TrapContext, vspace::*};
arch::{layout::mmap_phys_to_virt, trap::TrapContext}, use crate::{objects::*, plat::trap::TrapContextOps, vspace::*};
objects::*,
plat::trap::TrapContextOps,
};
use core::fmt::Debug; use core::fmt::Debug;
use core::sync::atomic::{AtomicUsize, Ordering};
use log::debug;
use uapi::{cap::ObjectType, error::SysResult, fault::Fault, syscall::MessageInfo}; use uapi::{cap::ObjectType, error::SysResult, fault::Fault, syscall::MessageInfo};
use utils::{ use utils::{
addr::*, addr::*,
@ -11,7 +10,9 @@ use utils::{
LinkHelperImpl, LinkHelperImpl,
}; };
#[derive(Clone, Copy, Debug)] pub static TID_ALLOCATOR: AtomicUsize = AtomicUsize::new(0);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ThreadState { pub enum ThreadState {
Inactive, Inactive,
Running, Running,
@ -33,6 +34,7 @@ pub struct TcbObject {
state: ThreadState, state: ThreadState,
time_tick: usize, time_tick: usize,
tid: usize,
pub link: Link<Self>, pub link: Link<Self>,
} }
@ -53,19 +55,29 @@ impl TcbObject {
fault_handler: CapEntry::new(NullCap::mint()), fault_handler: CapEntry::new(NullCap::mint()),
state: ThreadState::Inactive, state: ThreadState::Inactive,
time_tick: 0, time_tick: 0,
tid: TID_ALLOCATOR.fetch_add(1, Ordering::SeqCst),
link: Link::default(), link: Link::default(),
} }
} }
pub fn configure_idle_thread(&mut self) { pub fn configure_idle_thread(&mut self) {
self.trapframe.configure_idle_thread(); self.trapframe.configure_idle_thread();
self.vspace = CapEntry::new(TableCap::mint(unsafe { get_current_pagetable().paddr() }));
self.state = ThreadState::Idle; self.state = ThreadState::Idle;
} }
pub fn tid(&self) -> usize {
self.tid
}
pub fn state(&self) -> ThreadState { pub fn state(&self) -> ThreadState {
self.state self.state
} }
pub fn schedulable(&self) -> bool {
self.state != ThreadState::Blocked && self.state != ThreadState::Inactive
}
pub fn set_state(&mut self, state: ThreadState) { pub fn set_state(&mut self, state: ThreadState) {
self.state = state; self.state = state;
} }
@ -78,17 +90,18 @@ impl TcbObject {
self.time_tick = timeslice; self.time_tick = timeslice;
} }
pub fn timer_tick(&mut self) { pub fn do_tick(&mut self) {
self.time_tick = self.time_tick.saturating_sub(1); self.time_tick = self.time_tick.saturating_sub(1);
} }
pub fn install_vspace(&self) { pub fn activate_vspace(&self) {
// setup pagetable let cap = TableCap::try_from(&self.vspace).expect("activate_vspace: invalid cap");
todo!("TcbObject::install_vspace"); let paddr = cap.as_object::<Level0>().paddr();
unsafe { install_pagetable(paddr) }
} }
pub fn activate(&mut self) { pub fn activate(&mut self) {
self.install_vspace(); self.activate_vspace();
self.trapframe.restore(); self.trapframe.restore();
self.trapframe.handle_trap(false); self.trapframe.handle_trap(false);
} }
@ -101,9 +114,9 @@ impl TcbObject {
let cap_ptr = self.trapframe.get_reg(1); let cap_ptr = self.trapframe.get_reg(1);
let cap = cspace.resolve_address_bits(cap_ptr, usize::BITS as usize); let cap = cspace.resolve_address_bits(cap_ptr, usize::BITS as usize);
todo!("handle_syscall"); debug!("handle_syscall: info={:?}, cap={:?}", info, cap);
Ok(()) todo!("handle_syscall");
}; };
handle_syscall_inner().unwrap(); handle_syscall_inner().unwrap();