fix: use log for kernel output

This commit is contained in:
jackfiled 2024-07-26 14:55:09 +08:00
parent 0d8d1ba8e1
commit 615a4b763c
22 changed files with 64 additions and 53 deletions

2
os/Cargo.lock generated
View File

@ -50,7 +50,7 @@ checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d"
[[package]] [[package]]
name = "os" name = "os"
version = "0.1.0" version = "0.3.0"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"riscv", "riscv",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "os" name = "os"
version = "0.1.0" version = "0.3.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -35,34 +35,34 @@ macro_rules! println {
#[macro_export] #[macro_export]
macro_rules! log_error { macro_rules! log_error {
($fmt: literal $(, $($arg: tt)+)?) => { ($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!("\x1b[31m" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); $crate::console::print(format_args!(concat!("\x1b[31m [kernel]" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?));
} }
} }
#[macro_export] #[macro_export]
macro_rules! log_warning { macro_rules! log_warning {
($fmt: literal $(, $($arg: tt)+)?) => { ($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!("\x1b[93m" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); $crate::console::print(format_args!(concat!("\x1b[93m [kernel]" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?));
} }
} }
#[macro_export] #[macro_export]
macro_rules! log_information { macro_rules! log_information {
($fmt: literal $(, $($arg: tt)+)?) => { ($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!("\x1b[34m" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); $crate::console::print(format_args!(concat!("\x1b[34m [kernel]" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?));
} }
} }
#[macro_export] #[macro_export]
macro_rules! log_debug { macro_rules! log_debug {
($fmt: literal $(, $($arg: tt)+)?) => { ($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!("\x1b[32m" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); $crate::console::print(format_args!(concat!("\x1b[32m [kernel]" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?));
} }
} }
#[macro_export] #[macro_export]
macro_rules! log_trace { macro_rules! log_trace {
($fmt: literal $(, $($arg: tt)+)?) => { ($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!("\x1b[90m" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); $crate::console::print(format_args!(concat!("\x1b[90m [kernel]" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?));
} }
} }

View File

@ -1,8 +1,9 @@
use crate::config::{ use crate::config::{
APP_BASE_ADDRESS, APP_SIZE_LIMIT, KERNEL_STACK_SIZE, MAX_APP_NUM, USER_STACK_SIZE, APP_BASE_ADDRESS, APP_SIZE_LIMIT, KERNEL_STACK_SIZE, MAX_APP_NUM, USER_STACK_SIZE,
}; };
use core::arch::asm; use crate::log_information;
use crate::trap::context::TrapContext; use crate::trap::context::TrapContext;
use core::arch::asm;
#[repr(align(4096))] #[repr(align(4096))]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -30,7 +31,7 @@ impl KernelStack {
} }
pub fn push_context(&self, trap_context: TrapContext) -> usize { pub fn push_context(&self, trap_context: TrapContext) -> usize {
let pointer = (self.get_sp() - core::mem::size_of::<TrapContext>()) as *mut TrapContext; let pointer = (self.get_sp() - size_of::<TrapContext>()) as *mut TrapContext;
unsafe { unsafe {
*pointer = trap_context; *pointer = trap_context;
@ -85,6 +86,7 @@ pub fn load_apps() {
destination.copy_from_slice(source); destination.copy_from_slice(source);
} }
log_information!("Load {} applications in memory.", app_num);
unsafe { unsafe {
asm!("fence.i"); asm!("fence.i");
} }
@ -93,6 +95,6 @@ pub fn load_apps() {
pub fn initialize_app_context(app_id: usize) -> usize { pub fn initialize_app_context(app_id: usize) -> usize {
KERNEL_STACK[app_id].push_context(TrapContext::init_application_context( KERNEL_STACK[app_id].push_context(TrapContext::init_application_context(
APP_BASE_ADDRESS + app_id * APP_SIZE_LIMIT, APP_BASE_ADDRESS + app_id * APP_SIZE_LIMIT,
USER_STACK[app_id].get_sp() USER_STACK[app_id].get_sp(),
)) ))
} }

View File

@ -4,17 +4,17 @@
use crate::utils::clear_bss; use crate::utils::clear_bss;
use core::arch::global_asm; use core::arch::global_asm;
mod boards;
mod config;
mod console; mod console;
mod loader;
mod sbi; mod sbi;
mod sync; mod sync;
mod syscall; mod syscall;
mod trap;
mod utils;
mod loader;
mod boards;
mod config;
mod task; mod task;
mod timer; mod timer;
mod trap;
mod utils;
global_asm!(include_str!("entry.asm")); global_asm!(include_str!("entry.asm"));
global_asm!(include_str!("link_app.asm")); global_asm!(include_str!("link_app.asm"));
@ -22,7 +22,7 @@ global_asm!(include_str!("link_app.asm"));
#[no_mangle] #[no_mangle]
fn rust_main() -> ! { fn rust_main() -> ! {
clear_bss(); clear_bss();
log_information!("[kernel] Hello, rCore!"); log_information!("Hello, rCore!");
trap::init(); trap::init();
loader::load_apps(); loader::load_apps();
task::run_first_task(); task::run_first_task();

View File

@ -8,7 +8,7 @@ pub struct SingleCell<T> {
unsafe impl<T> Sync for SingleCell<T> {} unsafe impl<T> Sync for SingleCell<T> {}
impl<T> SingleCell<T> { impl<T> SingleCell<T> {
pub unsafe fn new(value: T) -> SingleCell<T> { pub fn new(value: T) -> SingleCell<T> {
SingleCell { SingleCell {
inner: RefCell::new(value), inner: RefCell::new(value),
} }

View File

@ -1,3 +1,4 @@
use crate::log_error;
use crate::syscall::fs::sys_write; use crate::syscall::fs::sys_write;
use crate::syscall::process::{sys_exit, sys_get_time, sys_yield}; use crate::syscall::process::{sys_exit, sys_get_time, sys_yield};
@ -16,7 +17,8 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> usize {
SYSCALL_YIELD => sys_yield(), SYSCALL_YIELD => sys_yield(),
SYSCALL_GET_TIME => sys_get_time(), SYSCALL_GET_TIME => sys_get_time(),
_ => { _ => {
panic!("Unsupported syscall: {}", syscall_id) log_error!("Unsupported syscall {} used.", syscall_id);
}, 0
}
} }
} }

View File

@ -1,4 +1,4 @@
use crate::print; use crate::{log_error, print};
const STDOUT_FD: usize = 1; const STDOUT_FD: usize = 1;
@ -11,7 +11,8 @@ pub fn sys_write(fd: usize, buffer: *const u8, len: usize) -> usize {
len len
} }
_ => { _ => {
panic!("Unsupported file descriptor in sys_write"); log_error!("Unsupported file descriptor {} used.", fd);
0
} }
} }
} }

View File

@ -1,9 +1,9 @@
use crate::println; use crate::log_information;
use crate::task::{exit_current_and_run_next, suspend_current_and_run_next}; use crate::task::{exit_current_and_run_next, suspend_current_and_run_next};
use crate::timer::get_time_ms; use crate::timer::get_time_ms;
pub fn sys_exit(exit_state: i32) -> ! { pub fn sys_exit(exit_state: i32) -> ! {
println!("[kernel] Application exited with code {}.", exit_state); log_information!("Application exited with code {}.", exit_state);
exit_current_and_run_next(); exit_current_and_run_next();
unreachable!() unreachable!()

View File

@ -1,3 +1,5 @@
use lazy_static::lazy_static;
use crate::config::MAX_APP_NUM; use crate::config::MAX_APP_NUM;
use crate::loader::{get_app_num, initialize_app_context}; use crate::loader::{get_app_num, initialize_app_context};
use crate::log_information; use crate::log_information;
@ -5,7 +7,6 @@ use crate::sbi::shutdown;
use crate::sync::single_cell::SingleCell; use crate::sync::single_cell::SingleCell;
use crate::task::context::TaskContext; use crate::task::context::TaskContext;
use crate::task::switch::__switch; use crate::task::switch::__switch;
use lazy_static::lazy_static;
mod context; mod context;
mod switch; mod switch;
@ -30,7 +31,7 @@ pub struct TaskManagerInner {
} }
pub struct TaskManager { pub struct TaskManager {
app_numer: usize, app_number: usize,
inner: SingleCell<TaskManagerInner>, inner: SingleCell<TaskManagerInner>,
} }
@ -48,8 +49,8 @@ lazy_static! {
} }
TaskManager { TaskManager {
app_numer: app_num, app_number: app_num,
inner: unsafe { inner: {
SingleCell::new(TaskManagerInner { SingleCell::new(TaskManagerInner {
tasks, tasks,
current_task: 0, current_task: 0,
@ -90,8 +91,8 @@ impl TaskManager {
fn find_next_task(&self) -> Option<usize> { fn find_next_task(&self) -> Option<usize> {
let inner = self.inner.exclusive_borrow(); let inner = self.inner.exclusive_borrow();
(inner.current_task + 1..inner.current_task + self.app_numer + 1) (inner.current_task + 1..inner.current_task + self.app_number + 1)
.map(|id| id % self.app_numer) .map(|id| id % self.app_number)
.find(|id| inner.tasks[*id].status == TaskStatus::Ready) .find(|id| inner.tasks[*id].status == TaskStatus::Ready)
} }
@ -116,7 +117,7 @@ impl TaskManager {
} }
} }
fn run_first_task(&self) -> ! { fn run_first_task(&self) {
let mut inner = self.inner.exclusive_borrow(); let mut inner = self.inner.exclusive_borrow();
let task0 = &mut inner.tasks[0]; let task0 = &mut inner.tasks[0];
task0.status = TaskStatus::Running; task0.status = TaskStatus::Running;
@ -127,7 +128,5 @@ impl TaskManager {
unsafe { unsafe {
__switch(&mut unused as *mut TaskContext, next_task_context); __switch(&mut unused as *mut TaskContext, next_task_context);
} }
unreachable!();
} }
} }

View File

@ -1,10 +1,9 @@
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
#[repr(C)] #[repr(C)]
pub struct TaskContext { pub struct TaskContext {
ra: usize, ra: usize,
sp: usize, sp: usize,
s: [usize; 12] s: [usize; 12],
} }
impl TaskContext { impl TaskContext {
@ -12,7 +11,7 @@ impl TaskContext {
Self { Self {
ra: 0, ra: 0,
sp: 0, sp: 0,
s: [0; 12] s: [0; 12],
} }
} }
@ -25,7 +24,7 @@ impl TaskContext {
Self { Self {
ra: __restore as usize, ra: __restore as usize,
sp: kernel_stack_pointer, sp: kernel_stack_pointer,
s: [0; 12] s: [0; 12],
} }
} }
} }

View File

@ -6,5 +6,3 @@ global_asm!(include_str!("switch.asm"));
extern "C" { extern "C" {
pub fn __switch(current_task_context: *mut TaskContext, next_task_context: *const TaskContext); pub fn __switch(current_task_context: *mut TaskContext, next_task_context: *const TaskContext);
} }

View File

@ -1,6 +1,6 @@
use riscv::register::time;
use crate::config::CLOCK_FREQUENCY; use crate::config::CLOCK_FREQUENCY;
use crate::sbi::set_timer; use crate::sbi::set_timer;
use riscv::register::time;
pub fn get_time() -> usize { pub fn get_time() -> usize {
time::read() time::read()

View File

@ -59,4 +59,5 @@ __restore:
addi sp, sp, 34*8 addi sp, sp, 34*8
# now sp->kernel stack, sscratch->user stack # now sp->kernel stack, sscratch->user stack
csrrw sp, sscratch, sp csrrw sp, sscratch, sp
# now sp->user stack, sscratch->kernel stack
sret sret

View File

@ -1,10 +1,14 @@
use crate::println; use crate::log_warning;
use crate::syscall::syscall; use crate::syscall::syscall;
use crate::trap::context::TrapContext;
use core::arch::global_asm;
use riscv::register::{mtvec::TrapMode, scause::{self, Exception, Trap, Interrupt}, sie, stval, stvec};
use crate::task::{exit_current_and_run_next, suspend_current_and_run_next}; use crate::task::{exit_current_and_run_next, suspend_current_and_run_next};
use crate::timer::set_next_trigger; use crate::timer::set_next_trigger;
use crate::trap::context::TrapContext;
use core::arch::global_asm;
use riscv::register::{
mtvec::TrapMode,
scause::{self, Exception, Interrupt, Trap},
sie, stval, stvec,
};
pub mod context; pub mod context;
@ -35,11 +39,15 @@ pub fn trap_handler(context: &mut TrapContext) -> &mut TrapContext {
context.x[10] = syscall(context.x[17], [context.x[10], context.x[11], context.x[12]]); context.x[10] = syscall(context.x[17], [context.x[10], context.x[11], context.x[12]]);
} }
Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) => { Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) => {
println!("[kernel] PageFault in application, kernel will kill it."); log_warning!("PageFault in application, kernel will kill it.");
exit_current_and_run_next(); exit_current_and_run_next();
} }
Trap::Exception(Exception::IllegalInstruction) => { Trap::Exception(Exception::IllegalInstruction) => {
println!("[kernel] Illegal instruction in application, kernel will kill it."); log_warning!("Illegal instruction in application, kernel will kill it.");
exit_current_and_run_next();
}
Trap::Exception(Exception::LoadFault) => {
log_warning!("Illegal memory access in application, kernel will kill it.");
exit_current_and_run_next(); exit_current_and_run_next();
} }
Trap::Interrupt(Interrupt::SupervisorTimer) => { Trap::Interrupt(Interrupt::SupervisorTimer) => {

View File

@ -11,6 +11,7 @@ fn main() -> i32 {
let current_time = get_time(); let current_time = get_time();
let wait_time = current_time + 3000; let wait_time = current_time + 3000;
println!("Sleep~~~~");
while get_time() < wait_time { while get_time() < wait_time {
sys_yield(); sys_yield();
} }