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) => {
Timer::do_tick();
if !from_kernel {
tcb.timer_tick();
tcb.do_tick();
}
},
T::Exception(E::UserEnvCall) => {

View File

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