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]]
name = "os"
version = "0.1.0"
version = "0.3.0"
dependencies = [
"lazy_static",
"riscv",

View File

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

View File

@ -1 +1 @@
pub mod qemu;
pub mod qemu;

View File

@ -1 +1 @@
pub const CLOCK_FREQUENCY: usize = 10000000;
pub const CLOCK_FREQUENCY: usize = 10000000;

View File

@ -5,4 +5,4 @@ pub const MAX_APP_NUM: usize = 4;
pub const APP_BASE_ADDRESS: usize = 0x80400000;
pub const APP_SIZE_LIMIT: usize = 0x20000;
pub use crate::boards::qemu::CLOCK_FREQUENCY;
pub use crate::boards::qemu::CLOCK_FREQUENCY;

View File

@ -35,34 +35,34 @@ macro_rules! println {
#[macro_export]
macro_rules! log_error {
($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_rules! log_warning {
($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_rules! log_information {
($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_rules! log_debug {
($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_rules! log_trace {
($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::{
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 core::arch::asm;
#[repr(align(4096))]
#[derive(Copy, Clone)]
@ -30,7 +31,7 @@ impl KernelStack {
}
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 {
*pointer = trap_context;
@ -85,6 +86,7 @@ pub fn load_apps() {
destination.copy_from_slice(source);
}
log_information!("Load {} applications in memory.", app_num);
unsafe {
asm!("fence.i");
}
@ -93,6 +95,6 @@ pub fn load_apps() {
pub fn initialize_app_context(app_id: usize) -> usize {
KERNEL_STACK[app_id].push_context(TrapContext::init_application_context(
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 core::arch::global_asm;
mod boards;
mod config;
mod console;
mod loader;
mod sbi;
mod sync;
mod syscall;
mod trap;
mod utils;
mod loader;
mod boards;
mod config;
mod task;
mod timer;
mod trap;
mod utils;
global_asm!(include_str!("entry.asm"));
global_asm!(include_str!("link_app.asm"));
@ -22,7 +22,7 @@ global_asm!(include_str!("link_app.asm"));
#[no_mangle]
fn rust_main() -> ! {
clear_bss();
log_information!("[kernel] Hello, rCore!");
log_information!("Hello, rCore!");
trap::init();
loader::load_apps();
task::run_first_task();

View File

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

View File

@ -1,3 +1,4 @@
use crate::log_error;
use crate::syscall::fs::sys_write;
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_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;
@ -11,7 +11,8 @@ pub fn sys_write(fd: usize, buffer: *const u8, len: usize) -> usize {
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::timer::get_time_ms;
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();
unreachable!()

View File

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

View File

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

View File

@ -6,5 +6,3 @@ global_asm!(include_str!("switch.asm"));
extern "C" {
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::sbi::set_timer;
use riscv::register::time;
pub fn get_time() -> usize {
time::read()

View File

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

View File

@ -1,10 +1,14 @@
use crate::println;
use crate::log_warning;
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::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;
@ -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]]);
}
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();
}
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();
}
Trap::Interrupt(Interrupt::SupervisorTimer) => {

View File

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

View File

@ -25,4 +25,4 @@ fn main() -> i32 {
println!("{}^{} = {}(MOD {})", p, iter, s[cur], m);
println!("Test power_3 OK!");
0
}
}

View File

@ -25,4 +25,4 @@ fn main() -> i32 {
println!("{}^{} = {}(MOD {})", p, iter, s[cur], m);
println!("Test power_5 OK!");
0
}
}

View File

@ -25,4 +25,4 @@ fn main() -> i32 {
println!("{}^{} = {}(MOD {})", p, iter, s[cur], m);
println!("Test power_7 OK!");
0
}
}