fix the logic to start the first process

This commit is contained in:
jiangjianfeng 2022-09-06 11:41:08 +08:00
parent 128ffa0bff
commit 6e11ee39da
6 changed files with 32 additions and 25 deletions

View File

@ -6,7 +6,7 @@
#![feature(const_btree_new)]
#![feature(cstr_from_bytes_until_nul)]
use kxos_frame::println;
use kxos_frame::{println, info, task::Task};
use process::Process;
extern crate alloc;
@ -20,15 +20,26 @@ pub fn init() {
process::fifo_scheduler::init();
}
pub fn run_first_kernel_task() {
Process::spawn_kernel_task(|| {
println!("hello world from kernel");
pub fn init_task() {
println!("[kernel] Hello world from init task!");
let process = Process::spawn_kernel_task(|| {
println!("[kernel] Hello world from kernel!");
});
info!("spawn kernel process, pid = {}", process.pid());
let elf_file_content = read_elf_content();
let process = Process::spawn_from_elf(elf_file_content);
info!("spwan user process, pid = {}", process.pid());
loop {}
}
pub fn run_first_process() {
/// first process never return
pub fn run_first_process() -> ! {
let elf_file_content = read_elf_content();
Process::spawn_from_elf(elf_file_content);
Task::spawn(init_task, None::<u8>, None).expect("Spawn first task failed");
unreachable!()
}
fn read_elf_content() -> &'static [u8] {

View File

@ -1,4 +1,3 @@
use alloc::sync::Arc;
use kxos_frame::vm::{Vaddr, VmPerm, VmSpace};
use super::vm_page::VmPageRange;
@ -8,9 +7,8 @@ pub const USER_STACK_SIZE: usize = 0x1000 * 16; // 64KB
pub struct UserStack {
/// The low address of user stack
pub stack_base: Vaddr,
stack_base: Vaddr,
stack_size: usize,
vm_space: Option<Arc<VmSpace>>,
}
impl UserStack {
@ -19,7 +17,6 @@ impl UserStack {
Self {
stack_base,
stack_size,
vm_space: None,
}
}
@ -28,7 +25,6 @@ impl UserStack {
Self {
stack_base: USER_STACK_BASE,
stack_size: USER_STACK_SIZE,
vm_space: None,
}
}

View File

@ -2,7 +2,7 @@ use core::sync::atomic::{AtomicUsize, Ordering};
use alloc::sync::Arc;
// use kxos_frame::{sync::SpinLock, task::Task, user::UserSpace};
use kxos_frame::{debug, task::Task};
use kxos_frame::task::Task;
use self::task::spawn_user_task_from_elf;
@ -39,8 +39,7 @@ impl Process {
F: Fn() + Send + Sync + 'static,
{
let pid = new_pid();
debug!("pid = {}", pid);
let task = Task::spawn(task_fn, None::<u8>, None).expect("spawn kernel task failed");
let task = Task::spawn(task_fn, pid, None).expect("spawn kernel task failed");
let exit_code = 0;
Self {
pid,
@ -48,6 +47,10 @@ impl Process {
exit_code,
}
}
pub fn pid(&self) -> usize {
self.pid
}
}
/// create a new pid for new process

View File

@ -25,6 +25,7 @@ pub fn spawn_user_task_from_elf(elf_file_content: &[u8]) -> Arc<Task> {
let cur = Task::current();
let user_space = cur.user_space().expect("user task should have user space");
let mut user_mode = UserMode::new(user_space);
debug!("in user task");
loop {
let user_event = user_mode.execute();
debug!("return from user mode");

View File

@ -2,20 +2,18 @@
.section .text
_start:
call print_message
call print_message
call print_message
call print_message
call print_message
call print_message
mov $60, %rax # syscall number of exit
mov $0, %rdi # exit code
mov $0, %rdi # exit code
syscall
print_message:
mov $1, %rax # syscall number of write
mov $1, %rdi # stdout
mov $message, %rsi # address of message
mov $message, %r11
mov $message_end, %r12
sub %r11, %r12 # calculate message len
mov %r12, %rdx # number of bytes
mov $message, %rsi # address of message
mov $message_end, %rdx
sub %rsi, %rdx # calculate message len
syscall
ret
.section .rodata

View File

@ -20,8 +20,6 @@ fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
kxos_std::init();
kxos_std::run_first_process();
loop {}
}
#[cfg(not(test))]
#[panic_handler]