Skip to content

Commit

Permalink
Remove tid allocator; exit_signal set by sys_clone
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf2019 committed Mar 9, 2023
1 parent 562b7b3 commit 933189f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 115 deletions.
2 changes: 1 addition & 1 deletion kernel/src/arch/riscv64/trap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub fn user_trap_return() -> ! {
let curr_mm = curr.mm.lock();
(
curr_mm.page_table.satp(),
trapframe_base(curr.tid),
trapframe_base(curr.tid.0),
__userret as usize - __uservec as usize + TRAMPOLINE_VA,
)
};
Expand Down
3 changes: 0 additions & 3 deletions kernel/src/config/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ pub const DEFAULT_FD_LIMIT: usize = 0x100;
/// Boot root directory
pub const ROOT_DIR: &str = "/";

/// Main task in the same address space
pub const MAIN_TASK: usize = 0;

/// Absolute path of init task
pub const INIT_TASK_PATH: &str = "hello_world";

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct SyscallArgs(pub SyscallNO, pub [usize; 6]);
pub struct SyscallImpl;

pub fn syscall(args: SyscallArgs) -> SyscallResult {
trace!("[U] Syscall {:X?}", args);
trace!(" SYSCALL {:X?}", args);
let id = args.0;
let args = args.1;
match id {
Expand Down
6 changes: 3 additions & 3 deletions kernel/src/syscall/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ impl SyscallProc for SyscallImpl {
}

fn getpid() -> SyscallResult {
Ok(curr_task().unwrap().pid.0)
Ok(curr_task().unwrap().pid)
}

fn gettid() -> SyscallResult {
Ok(curr_task().unwrap().tid)
Ok(curr_task().unwrap().tid.0)
}

fn set_tid_address(tidptr: usize) -> SyscallResult {
let curr = curr_task().unwrap();
curr.inner().clear_child_tid = tidptr;
Ok(curr.tid)
Ok(curr.tid.0)
}

fn brk(brk: usize) -> SyscallResult {
Expand Down
62 changes: 28 additions & 34 deletions kernel/src/task/fork.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
use core::cell::SyncUnsafeCell;

use alloc::{collections::LinkedList, sync::Arc};
use id_alloc::*;
use kernel_sync::SpinLock;
use log::trace;
use signal_defs::{SigPending, SigSet};
use signal_defs::{SigPending, SigSet, SignalNo};

use crate::{
arch::{
mm::VirtAddr,
trap::{user_trap_return, TrapFrame},
TaskContext,
},
config::MAIN_TASK,
error::{KernelError, KernelResult},
task::{TID, TrapFrameTracker},
};

use super::{
init_trapframe, kstack_alloc, kstack_dealloc, kstack_vm_alloc, pid_alloc, schedule::Scheduler,
Task, TaskInner, TaskLockedInner, TaskState, PID, TASK_MANAGER,
init_trapframe, kstack_alloc, kstack_vm_alloc, schedule::Scheduler,
Task, TaskInner, TaskLockedInner, TaskState, TASK_MANAGER,
};

bitflags::bitflags! {
pub struct CloneFlags: u32 {
/// Signal mask to be sent at exit.
const CSIGNAL = 0x000000ff;
/// Set if vm shared between processes. In particular, memory writes performed by the calling process or
/// by the child process are also visible in the other process.
const CLONE_VM = 0x00000100;
Expand Down Expand Up @@ -82,6 +83,10 @@ pub fn do_clone(
) -> KernelResult<usize> {
trace!("CLONE from {:?} {:?}", &task, flags);

if flags.contains(CloneFlags::CLONE_NEWNS | CloneFlags::CLONE_FS) {
return Err(KernelError::InvalidArgs);
}

/*
* Thread groups must share signals as well, and detached threads
* can only be started up within the thread group.
Expand All @@ -107,44 +112,35 @@ pub fn do_clone(
Arc::new(SpinLock::new(orig.clone()?))
};

// New kernel stack
let kstack = kstack_alloc();
let tid = TID(kstack);
let kstack_base = kstack_vm_alloc(kstack)?;

// Init trapframe
let trapframe_pa = {
let mut mm = mm.lock();
let trapframe_pa = init_trapframe(&mut mm)?;
let trapframe_pa = init_trapframe(&mut mm, kstack)?;
let trapframe = TrapFrame::from(trapframe_pa);
trapframe.copy_from(TrapFrame::from(task.trapframe_pa), flags, stack, tls);
trapframe.copy_from(TrapFrame::from(task.trapframe.0), flags, stack, tls);
trapframe_pa
};
let trapframe = TrapFrameTracker(trapframe_pa); // for unwinding

// New kernel stack
let kstack = kstack_alloc();
let kstack_base = kstack_vm_alloc(kstack);
if kstack_base.is_err() {
kstack_dealloc(kstack);
return Err(kstack_base.unwrap_err());
}

// New task id
let tid = if flags.contains(CloneFlags::CLONE_THREAD) {
task.tid_allocator.lock().alloc()
} else {
MAIN_TASK
};

let new_task = Arc::new(Task {
name: task.name.clone(),
kstack,
tid,
trapframe_pa,
pid: if flags.contains(CloneFlags::CLONE_THREAD) {
task.pid.clone()
task.pid
} else {
Arc::new(PID(pid_alloc()))
kstack
},
tid_allocator: if flags.contains(CloneFlags::CLONE_THREAD) {
task.tid_allocator.clone()
trapframe,
exit_signal: if flags.contains(CloneFlags::CLONE_THREAD) {
SignalNo::ERR
} else {
Arc::new(SpinLock::new(RecycleAllocator::new(MAIN_TASK + 1)))
SignalNo::try_from((flags & CloneFlags::CSIGNAL).bits() as usize).map_err(|_| KernelError::InvalidArgs)?
},
mm,
fd_manager: if flags.contains(CloneFlags::CLONE_FILES) {
Expand Down Expand Up @@ -178,7 +174,7 @@ pub fn do_clone(
}),
inner: SyncUnsafeCell::new(TaskInner {
exit_code: 0,
ctx: TaskContext::new(user_trap_return as usize, kstack_base.unwrap()),
ctx: TaskContext::new(user_trap_return as usize, kstack_base),
set_child_tid: if flags.contains(CloneFlags::CLONE_CHILD_SETTID) {
ctid.value()
} else {
Expand All @@ -194,13 +190,11 @@ pub fn do_clone(
}),
});

let pid = new_task.pid.0;

// Set tid in parent address space
if flags.contains(CloneFlags::CLONE_PARENT_SETTID) {
let mut mm = task.mm.lock();
let ptid = mm.alloc_frame(ptid)?.start_address() + ptid.page_offset();
unsafe { *(ptid.get_mut() as *mut i32) = tid as i32 };
unsafe { *(ptid.get_mut() as *mut i32) = kstack as i32 };
}

// Set tid in child address space (COW)
Expand All @@ -213,7 +207,7 @@ pub fn do_clone(
};
unsafe {
*(ctid.get_mut() as *mut i32) = if flags.contains(CloneFlags::CLONE_CHILD_SETTID) {
tid as i32
kstack as i32
} else {
0
}
Expand All @@ -233,5 +227,5 @@ pub fn do_clone(
}
}

Ok(pid)
Ok(kstack)
}
28 changes: 3 additions & 25 deletions kernel/src/task/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use spin::Lazy;
use crate::{
arch::{get_cpu_id, mm::PAGE_SIZE, TaskContext, __switch},
config::{
ADDR_ALIGN, CPU_NUM, INIT_TASK_PATH, IS_TEST_ENV, KERNEL_STACK_SIZE, MAIN_TASK, ROOT_DIR,
ADDR_ALIGN, CPU_NUM, INIT_TASK_PATH, IS_TEST_ENV, KERNEL_STACK_SIZE, ROOT_DIR,
TRAMPOLINE_VA,
},
error::KernelResult,
Expand All @@ -23,24 +23,6 @@ use super::{
TaskState,
};

/// Global process identification allocator.
static PID_ALLOCATOR: Lazy<SpinLock<RecycleAllocator>> =
Lazy::new(|| SpinLock::new(RecycleAllocator::new(0)));

/// Only provides [`pid_alloc()`] interface.
pub fn pid_alloc() -> usize {
PID_ALLOCATOR.lock().alloc()
}

#[derive(Debug)]
pub struct PID(pub usize);

impl Drop for PID {
fn drop(&mut self) {
PID_ALLOCATOR.lock().dealloc(self.0)
}
}

/// Global kernal stack allocator.
static KSTACK_ALLOCATOR: Lazy<SpinLock<RecycleAllocator>> =
Lazy::new(|| SpinLock::new(RecycleAllocator::new(0)));
Expand Down Expand Up @@ -197,11 +179,7 @@ pub unsafe fn do_exit(exit_code: i32) {
&curr.inner().ctx as *const TaskContext
};

if !IS_TEST_ENV && curr.pid.0 == 0 {
panic!("All task exited!");
} else {
handle_zombie(curr);
}
handle_zombie(curr);

__switch(curr_ctx, idle_ctx());
}
Expand Down Expand Up @@ -267,7 +245,7 @@ pub fn handle_zombie(task: Arc<Task>) {
}
locked_inner.children.clear();
locked_inner.state = TaskState::ZOMBIE;
if IS_TEST_ENV && task.tid == MAIN_TASK {
if IS_TEST_ENV && task.tid.0 == task.pid {
finish_test(task.inner().exit_code, &task.name);
}
}
Loading

0 comments on commit 933189f

Please sign in to comment.