Fix enter second kernel thread. But page fault now.

master
WangRunji 6 years ago
parent b1a3695dfb
commit 9001ac1f3d

@ -44,13 +44,7 @@ impl InitStack {
} }
extern { extern {
fn trap_ret(); fn __trapret();
}
/// The entry point of new thread
extern fn forkret() {
debug!("forkret");
// Will return to `trapret`
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -63,7 +57,7 @@ struct ContextData {
impl ContextData { impl ContextData {
fn new(satp: usize) -> Self { fn new(satp: usize) -> Self {
ContextData { ra: forkret as usize, satp, ..ContextData::default() } ContextData { ra: __trapret as usize, satp, ..ContextData::default() }
} }
} }
@ -84,8 +78,8 @@ impl Context {
asm!( asm!(
" "
// save from's registers // save from's registers
sub sp, -14*4 addi sp, sp, -4*14
sw sp, (a0) sw sp, 0(a0)
sw ra, 0*4(sp) sw ra, 0*4(sp)
sw s0, 2*4(sp) sw s0, 2*4(sp)
sw s1, 3*4(sp) sw s1, 3*4(sp)
@ -103,7 +97,7 @@ impl Context {
sw s11, 1*4(sp) sw s11, 1*4(sp)
// restore to's registers // restore to's registers
lw sp, (a1) lw sp, 0(a1)
lw s11, 1*4(sp) lw s11, 1*4(sp)
csrrw x0, 0x180, s11 // satp csrrw x0, 0x180, s11 // satp
lw ra, 0*4(sp) lw ra, 0*4(sp)
@ -119,11 +113,11 @@ impl Context {
lw s9, 11*4(sp) lw s9, 11*4(sp)
lw s10, 12*4(sp) lw s10, 12*4(sp)
lw s11, 13*4(sp) lw s11, 13*4(sp)
add sp, 14*4 addi sp, sp, 4*14
sw zero, (a1) sw zero, 0(a1)
ret" ret"
: : : : "intel" "volatile" ) : : : : "volatile" )
} }
pub unsafe fn null() -> Self { pub unsafe fn null() -> Self {

@ -32,14 +32,16 @@ pub unsafe fn restore(flags: usize) {
#[no_mangle] #[no_mangle]
pub extern fn rust_trap(tf: &mut TrapFrame) { pub extern fn rust_trap(tf: &mut TrapFrame) {
use super::riscv::register::scause::{Trap, Interrupt, Exception}; use super::riscv::register::scause::{Trap, Interrupt, Exception};
trace!("Interrupt: {:?}", tf.scause.cause());
match tf.scause.cause() { match tf.scause.cause() {
Trap::Interrupt(SupervisorTimer) => timer(), Trap::Interrupt(SupervisorTimer) => timer(),
_ => panic!("Unhandled interrupt: {:?}\n{:#x?}", tf.scause.cause(), tf), _ => panic!("Unhandled interrupt: {:?}\n{:#x?}", tf.scause.cause(), tf),
} }
::trap::before_return();
} }
fn timer() { fn timer() {
::timer_interrupt(); ::trap::timer();
super::timer::set_next(); super::timer::set_next();
} }

@ -78,7 +78,7 @@ pub extern fn rust_trap(tf: &mut TrapFrame) {
T_IRQ0...63 => { T_IRQ0...63 => {
let irq = tf.trap_num as u8 - T_IRQ0; let irq = tf.trap_num as u8 - T_IRQ0;
match irq { match irq {
IRQ_TIMER => timer(), IRQ_TIMER => ::trap::timer(),
IRQ_KBD => keyboard(), IRQ_KBD => keyboard(),
IRQ_COM1 => com1(), IRQ_COM1 => com1(),
IRQ_COM2 => com2(), IRQ_COM2 => com2(),
@ -97,11 +97,7 @@ pub extern fn rust_trap(tf: &mut TrapFrame) {
T_DIVIDE | T_GPFLT | T_ILLOP => error(tf), T_DIVIDE | T_GPFLT | T_ILLOP => error(tf),
_ => panic!("Unhandled interrupt {:x}", tf.trap_num), _ => panic!("Unhandled interrupt {:x}", tf.trap_num),
} }
::trap::before_return();
use process::PROCESSOR;
if let Some(processor) = PROCESSOR.try() {
processor.lock().schedule();
}
} }
fn breakpoint() { fn breakpoint() {
@ -145,10 +141,6 @@ fn com2() {
COM2.lock().receive(); COM2.lock().receive();
} }
fn timer() {
::timer_interrupt();
}
fn to_user(tf: &mut TrapFrame) { fn to_user(tf: &mut TrapFrame) {
use arch::gdt; use arch::gdt;
info!("\nInterupt: To User"); info!("\nInterupt: To User");

@ -70,6 +70,7 @@ mod syscall;
mod fs; mod fs;
mod thread; mod thread;
mod sync; mod sync;
mod trap;
#[allow(dead_code)] #[allow(dead_code)]
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
@ -80,12 +81,6 @@ mod arch;
#[path = "arch/riscv32/mod.rs"] #[path = "arch/riscv32/mod.rs"]
mod arch; mod arch;
fn timer_interrupt() {
use process::PROCESSOR;
let mut processor = PROCESSOR.try().unwrap().lock();
processor.tick();
}
#[no_mangle] #[no_mangle]
#[cfg(target_arch = "riscv")] #[cfg(target_arch = "riscv")]
pub extern fn rust_main() -> ! { pub extern fn rust_main() -> ! {

@ -15,16 +15,18 @@ pub fn init() {
SpinNoIrqLock::new({ SpinNoIrqLock::new({
let initproc = Process::new_init(); let initproc = Process::new_init();
let idleproc = Process::new("idle", idle_thread, 0); let idleproc = Process::new("idle", idle_thread, 0);
let mut processor = Processor::new(); let mut processor = Processor::new();
processor.add(initproc); processor.add(initproc);
processor.add(idleproc); processor.add(idleproc);
processor processor
})}); })
});
} }
pub static PROCESSOR: Once<SpinNoIrqLock<Processor>> = Once::new(); pub static PROCESSOR: Once<SpinNoIrqLock<Processor>> = Once::new();
extern fn idle_thread(_arg: usize) -> ! { extern fn idle_thread(_arg: usize) -> ! {
println!("Hello, I'm idle.");
loop {} loop {}
} }

@ -0,0 +1,14 @@
use process::PROCESSOR;
pub fn timer() {
let mut processor = PROCESSOR.try().unwrap().lock();
processor.tick();
}
pub fn before_return() {
use process::PROCESSOR;
if let Some(processor) = PROCESSOR.try() {
processor.lock().schedule();
}
}
Loading…
Cancel
Save