From 91455d163d88e5bf86211c4822b34ced373fef33 Mon Sep 17 00:00:00 2001 From: lcy1996 <992195697@qq.com> Date: Fri, 5 Oct 2018 23:39:28 +0800 Subject: [PATCH] Finish comment riscv context. --- kernel/src/arch/riscv32/context.rs | 38 +++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/kernel/src/arch/riscv32/context.rs b/kernel/src/arch/riscv32/context.rs index 82b3c9d..2a61cf4 100644 --- a/kernel/src/arch/riscv32/context.rs +++ b/kernel/src/arch/riscv32/context.rs @@ -103,6 +103,7 @@ struct ContextData { impl ContextData { fn new(satp: usize) -> Self { + // satp(asid) just like cr3, save the physical address for Page directory? ContextData { ra: __trapret as usize, satp, ..ContextData::default() } } } @@ -177,24 +178,59 @@ impl Context { Context(0) } - + /* + * @param: + * entry: program entry for the thread + * arg: a0 + * kstack_top: kernel stack top + * cr3: cr3 register, save the physical address of Page directory + * @brief: + * generate the content of kernel stack for the new kernel thread and save it's address at kernel stack top - 1 + * @retval: + * a Context struct with the pointer to the kernel stack top - 1 as its only element + */ pub unsafe fn new_kernel_thread(entry: extern fn(usize) -> !, arg: usize, kstack_top: usize, cr3: usize) -> Self { InitStack { context: ContextData::new(cr3), tf: TrapFrame::new_kernel_thread(entry, arg, kstack_top), }.push_at(kstack_top) } + + /* + * @param: + * entry_addr: program entry for the thread + * ustack_top: user stack top + * kstack_top: kernel stack top + * is32: whether the cpu is 32 bit or not + * cr3: cr3 register, save the physical address of Page directory + * @brief: + * generate the content of kernel stack for the new user thread and save it's address at kernel stack top - 1 + * @retval: + * a Context struct with the pointer to the kernel stack top - 1 as its only element + */ pub unsafe fn new_user_thread(entry_addr: usize, ustack_top: usize, kstack_top: usize, is32: bool, cr3: usize) -> Self { InitStack { context: ContextData::new(cr3), tf: TrapFrame::new_user_thread(entry_addr, ustack_top), }.push_at(kstack_top) } + + /* + * @param: + * TrapFrame: the trapframe of the forked process(thread) + * kstack_top: kernel stack top + * cr3: cr3 register, save the physical address of Page directory + * @brief: + * fork and generate a new process(thread) Context according to the TrapFrame and save it's address at kernel stack top - 1 + * @retval: + * a Context struct with the pointer to the kernel stack top - 1 as its only element + */ pub unsafe fn new_fork(tf: &TrapFrame, kstack_top: usize, cr3: usize) -> Self { InitStack { context: ContextData::new(cr3), tf: { let mut tf = tf.clone(); + // fork function's ret value, the new process is 0 tf.x[10] = 0; // a0 tf },