2022-04-30 16:04:58 +08:00
|
|
|
//!Implementation of [`PidAllocator`]
|
2022-01-21 14:56:19 -08:00
|
|
|
use crate::config::{KERNEL_STACK_SIZE, PAGE_SIZE, TRAMPOLINE};
|
2025-02-19 21:27:17 +08:00
|
|
|
use crate::mm::{KERNEL_SPACE, MapPermission, VirtAddr};
|
2022-01-21 14:56:19 -08:00
|
|
|
use crate::sync::UPSafeCell;
|
2020-12-08 17:17:28 +08:00
|
|
|
use alloc::vec::Vec;
|
|
|
|
use lazy_static::*;
|
2022-04-30 16:04:58 +08:00
|
|
|
///Pid Allocator struct
|
|
|
|
pub struct PidAllocator {
|
2020-12-08 17:17:28 +08:00
|
|
|
current: usize,
|
|
|
|
recycled: Vec<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PidAllocator {
|
2022-04-30 16:04:58 +08:00
|
|
|
///Create an empty `PidAllocator`
|
2020-12-08 17:17:28 +08:00
|
|
|
pub fn new() -> Self {
|
|
|
|
PidAllocator {
|
|
|
|
current: 0,
|
|
|
|
recycled: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
2022-04-30 16:04:58 +08:00
|
|
|
///Allocate a pid
|
2020-12-08 17:17:28 +08:00
|
|
|
pub fn alloc(&mut self) -> PidHandle {
|
|
|
|
if let Some(pid) = self.recycled.pop() {
|
|
|
|
PidHandle(pid)
|
|
|
|
} else {
|
|
|
|
self.current += 1;
|
|
|
|
PidHandle(self.current - 1)
|
|
|
|
}
|
|
|
|
}
|
2022-04-30 16:04:58 +08:00
|
|
|
///Recycle a pid
|
2020-12-08 17:17:28 +08:00
|
|
|
pub fn dealloc(&mut self, pid: usize) {
|
|
|
|
assert!(pid < self.current);
|
|
|
|
assert!(
|
2022-01-24 17:50:49 -08:00
|
|
|
!self.recycled.iter().any(|ppid| *ppid == pid),
|
2022-01-21 14:56:19 -08:00
|
|
|
"pid {} has been deallocated!",
|
|
|
|
pid
|
2020-12-08 17:17:28 +08:00
|
|
|
);
|
|
|
|
self.recycled.push(pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
2022-04-30 16:04:58 +08:00
|
|
|
pub static ref PID_ALLOCATOR: UPSafeCell<PidAllocator> =
|
2022-01-21 14:56:19 -08:00
|
|
|
unsafe { UPSafeCell::new(PidAllocator::new()) };
|
2020-12-08 17:17:28 +08:00
|
|
|
}
|
2022-04-30 16:04:58 +08:00
|
|
|
///Bind pid lifetime to `PidHandle`
|
2020-12-08 17:17:28 +08:00
|
|
|
pub struct PidHandle(pub usize);
|
|
|
|
|
|
|
|
impl Drop for PidHandle {
|
|
|
|
fn drop(&mut self) {
|
2020-12-10 11:57:26 +08:00
|
|
|
//println!("drop pid {}", self.0);
|
2021-07-20 22:10:22 +08:00
|
|
|
PID_ALLOCATOR.exclusive_access().dealloc(self.0);
|
2020-12-08 17:17:28 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-30 16:04:58 +08:00
|
|
|
///Allocate a pid from PID_ALLOCATOR
|
2020-12-08 17:17:28 +08:00
|
|
|
pub fn pid_alloc() -> PidHandle {
|
2021-07-20 22:10:22 +08:00
|
|
|
PID_ALLOCATOR.exclusive_access().alloc()
|
2020-12-08 17:17:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return (bottom, top) of a kernel stack in kernel space.
|
|
|
|
pub fn kernel_stack_position(app_id: usize) -> (usize, usize) {
|
|
|
|
let top = TRAMPOLINE - app_id * (KERNEL_STACK_SIZE + PAGE_SIZE);
|
|
|
|
let bottom = top - KERNEL_STACK_SIZE;
|
|
|
|
(bottom, top)
|
|
|
|
}
|
2022-05-20 08:50:52 +08:00
|
|
|
///Kernelstack for app
|
2020-12-08 17:17:28 +08:00
|
|
|
pub struct KernelStack {
|
|
|
|
pid: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KernelStack {
|
2022-04-30 16:04:58 +08:00
|
|
|
///Create a kernelstack from pid
|
2020-12-08 17:17:28 +08:00
|
|
|
pub fn new(pid_handle: &PidHandle) -> Self {
|
|
|
|
let pid = pid_handle.0;
|
|
|
|
let (kernel_stack_bottom, kernel_stack_top) = kernel_stack_position(pid);
|
2022-01-21 14:56:19 -08:00
|
|
|
KERNEL_SPACE.exclusive_access().insert_framed_area(
|
|
|
|
kernel_stack_bottom.into(),
|
|
|
|
kernel_stack_top.into(),
|
|
|
|
MapPermission::R | MapPermission::W,
|
|
|
|
);
|
|
|
|
KernelStack { pid: pid_handle.0 }
|
2020-12-08 17:17:28 +08:00
|
|
|
}
|
2021-07-20 22:10:22 +08:00
|
|
|
#[allow(unused)]
|
2022-04-30 16:04:58 +08:00
|
|
|
///Push a value on top of kernelstack
|
2022-01-21 14:56:19 -08:00
|
|
|
pub fn push_on_top<T>(&self, value: T) -> *mut T
|
|
|
|
where
|
|
|
|
T: Sized,
|
|
|
|
{
|
2020-12-10 11:57:26 +08:00
|
|
|
let kernel_stack_top = self.get_top();
|
2020-12-08 17:17:28 +08:00
|
|
|
let ptr_mut = (kernel_stack_top - core::mem::size_of::<T>()) as *mut T;
|
2022-01-21 14:56:19 -08:00
|
|
|
unsafe {
|
|
|
|
*ptr_mut = value;
|
|
|
|
}
|
2020-12-08 17:17:28 +08:00
|
|
|
ptr_mut
|
|
|
|
}
|
2022-04-30 16:04:58 +08:00
|
|
|
///Get the value on the top of kernelstack
|
2020-12-08 17:17:28 +08:00
|
|
|
pub fn get_top(&self) -> usize {
|
|
|
|
let (_, kernel_stack_top) = kernel_stack_position(self.pid);
|
|
|
|
kernel_stack_top
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for KernelStack {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let (kernel_stack_bottom, _) = kernel_stack_position(self.pid);
|
|
|
|
let kernel_stack_bottom_va: VirtAddr = kernel_stack_bottom.into();
|
|
|
|
KERNEL_SPACE
|
2021-07-20 22:10:22 +08:00
|
|
|
.exclusive_access()
|
2020-12-08 17:17:28 +08:00
|
|
|
.remove_area_with_start_vpn(kernel_stack_bottom_va.into());
|
|
|
|
}
|
2022-01-21 14:56:19 -08:00
|
|
|
}
|