Skip to content

Commit 4bf40e7

Browse files
committed
Merge branch 'ch9' into main
2 parents 410ee6c + aee2000 commit 4bf40e7

File tree

9 files changed

+99
-9
lines changed

9 files changed

+99
-9
lines changed

.github/workflows/doc-and-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ jobs:
6464
- name: Run usertests
6565
run: cd os && make run TEST=1
6666
timeout-minutes: 10
67+
68+
- name: Build for k210
69+
run: cd os && make build BOARD=k210

os/src/console.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
use crate::drivers::chardev::{CharDevice, UART};
1+
use crate::drivers::chardev::CharDevice;
2+
#[cfg(feature = "board_qemu")]
3+
use crate::drivers::chardev::UART;
4+
#[cfg(feature = "board_k210")]
5+
use crate::sbi::console_putchar;
26
use core::fmt::{self, Write};
37

48
struct Stdout;
59

610
impl Write for Stdout {
711
fn write_str(&mut self, s: &str) -> fmt::Result {
812
for c in s.chars() {
13+
#[cfg(feature = "board_qemu")]
914
UART.write(c as u8);
15+
#[cfg(feature = "board_k210")]
16+
console_putchar(c as usize);
1017
}
1118
Ok(())
1219
}

os/src/drivers/chardev/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
mod ns16550a;
22

3-
pub use ns16550a::NS16550a;
4-
3+
#[cfg(feature = "board_qemu")]
54
use crate::board::CharDeviceImpl;
65
use alloc::sync::Arc;
76
use lazy_static::*;
7+
pub use ns16550a::NS16550a;
88

99
pub trait CharDevice {
1010
fn read(&self) -> u8;
1111
fn write(&self, ch: u8);
1212
fn handle_irq(&self);
1313
}
14-
14+
#[cfg(feature = "board_qemu")]
1515
lazy_static! {
1616
pub static ref UART: Arc<CharDeviceImpl> = Arc::new(CharDeviceImpl::new());
1717
}

os/src/drivers/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
pub mod block;
22
pub mod chardev;
3+
#[cfg(feature = "board_qemu")]
34
pub mod gpu;
5+
#[cfg(feature = "board_qemu")]
46
pub mod input;
57
pub mod plic;
68
pub use block::BLOCK_DEVICE;
9+
#[cfg(feature = "board_qemu")]
710
pub use chardev::UART;
11+
#[cfg(feature = "board_qemu")]
812
pub use gpu::*;
13+
#[cfg(feature = "board_qemu")]
914
pub use input::*;

os/src/fs/stdio.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use super::File;
2-
use crate::drivers::chardev::{CharDevice, UART};
2+
use crate::drivers::chardev::CharDevice;
3+
#[cfg(feature = "board_qemu")]
4+
use crate::drivers::chardev::UART;
35
use crate::mm::UserBuffer;
6+
#[cfg(feature = "board_k210")]
7+
use crate::sbi::console_getchar;
8+
#[cfg(feature = "board_k210")]
9+
use crate::task::suspend_current_and_run_next;
410

511
pub struct Stdin;
612
pub struct Stdout;
@@ -12,6 +18,7 @@ impl File for Stdin {
1218
fn writable(&self) -> bool {
1319
false
1420
}
21+
#[cfg(feature = "board_qemu")]
1522
fn read(&self, mut user_buf: UserBuffer) -> usize {
1623
assert_eq!(user_buf.len(), 1);
1724
//println!("before UART.read() in Stdin::read()");
@@ -21,6 +28,27 @@ impl File for Stdin {
2128
}
2229
1
2330
}
31+
#[cfg(feature = "board_k210")]
32+
fn read(&self, mut user_buf: UserBuffer) -> usize {
33+
assert_eq!(user_buf.len(), 1);
34+
// busy loop
35+
let mut c: usize;
36+
loop {
37+
c = console_getchar();
38+
if c == 0 {
39+
suspend_current_and_run_next();
40+
continue;
41+
} else {
42+
break;
43+
}
44+
}
45+
let ch = c as u8;
46+
unsafe {
47+
user_buf.buffers[0].as_mut_ptr().write_volatile(ch);
48+
}
49+
1
50+
}
51+
2452
fn write(&self, _user_buf: UserBuffer) -> usize {
2553
panic!("Cannot write to stdin!");
2654
}

os/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![no_main]
33
#![feature(panic_info_message)]
44
#![feature(alloc_error_handler)]
5-
5+
#[cfg(feature = "board_qemu")]
66
use crate::drivers::{GPU_DEVICE, KEYBOARD_DEVICE, MOUSE_DEVICE};
77

88
extern crate alloc;
@@ -22,6 +22,7 @@ mod console;
2222
mod config;
2323
mod drivers;
2424
mod fs;
25+
#[cfg(feature = "board_qemu")]
2526
mod gui;
2627
mod lang_items;
2728
mod mm;
@@ -60,10 +61,13 @@ pub fn rust_main() -> ! {
6061
clear_bss();
6162
mm::init();
6263
println!("KERN: init gpu");
64+
#[cfg(feature = "board_qemu")]
6365
GPU_DEVICE.clone();
6466
println!("KERN: init keyboard");
67+
#[cfg(feature = "board_qemu")]
6568
KEYBOARD_DEVICE.clone();
6669
println!("KERN: init mouse");
70+
#[cfg(feature = "board_qemu")]
6771
MOUSE_DEVICE.clone();
6872
println!("KERN: init trap");
6973
trap::init();

os/src/sbi.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ pub fn console_getchar() -> usize {
4040
sbi_call(SBI_CONSOLE_GETCHAR, 0, 0, 0)
4141
}
4242

43+
#[cfg(feature = "board_qemu")]
4344
use crate::board::QEMUExit;
4445
pub fn shutdown(exit_code: usize) -> ! {
45-
//sbi_call(SBI_SHUTDOWN, exit_code, 0, 0);
46+
#[cfg(feature = "board_k210")]
47+
sbi_call(SBI_SHUTDOWN, exit_code, 0, 0);
48+
#[cfg(feature = "board_qemu")]
4649
crate::board::QEMU_EXIT_HANDLE.exit_failure();
50+
#[cfg(feature = "board_k210")]
4751
panic!("It should shutdown!");
4852
}

os/src/syscall/mod.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@ const SYSCALL_CONDVAR_SIGNAL: usize = 1031;
2727
const SYSCALL_CONDVAR_WAIT: usize = 1032;
2828
const SYSCALL_CREATE_DESKTOP: usize = 2000;
2929
mod fs;
30+
#[cfg(feature = "board_qemu")]
3031
mod gui;
3132
mod process;
3233
mod sync;
3334
mod thread;
34-
35+
#[cfg(feature = "board_qemu")]
3536
pub use self::gui::create_desktop;
3637
use fs::*;
38+
39+
#[cfg(feature = "board_qemu")]
3740
pub use gui::PAD;
3841
use process::*;
3942
use sync::*;
4043
use thread::*;
4144

45+
#[cfg(feature = "board_qemu")]
4246
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
4347
match syscall_id {
4448
SYSCALL_DUP => sys_dup(args[0]),
@@ -72,3 +76,37 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
7276
_ => panic!("Unsupported syscall_id: {}", syscall_id),
7377
}
7478
}
79+
80+
#[cfg(feature = "board_k210")]
81+
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
82+
match syscall_id {
83+
SYSCALL_DUP => sys_dup(args[0]),
84+
SYSCALL_OPEN => sys_open(args[0] as *const u8, args[1] as u32),
85+
SYSCALL_CLOSE => sys_close(args[0]),
86+
SYSCALL_PIPE => sys_pipe(args[0] as *mut usize),
87+
SYSCALL_READ => sys_read(args[0], args[1] as *const u8, args[2]),
88+
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
89+
SYSCALL_EXIT => sys_exit(args[0] as i32),
90+
SYSCALL_SLEEP => sys_sleep(args[0]),
91+
SYSCALL_YIELD => sys_yield(),
92+
SYSCALL_KILL => sys_kill(args[0], args[1] as u32),
93+
SYSCALL_GET_TIME => sys_get_time(),
94+
SYSCALL_GETPID => sys_getpid(),
95+
SYSCALL_FORK => sys_fork(),
96+
SYSCALL_EXEC => sys_exec(args[0] as *const u8, args[1] as *const usize),
97+
SYSCALL_WAITPID => sys_waitpid(args[0] as isize, args[1] as *mut i32),
98+
SYSCALL_THREAD_CREATE => sys_thread_create(args[0], args[1]),
99+
SYSCALL_GETTID => sys_gettid(),
100+
SYSCALL_WAITTID => sys_waittid(args[0]) as isize,
101+
SYSCALL_MUTEX_CREATE => sys_mutex_create(args[0] == 1),
102+
SYSCALL_MUTEX_LOCK => sys_mutex_lock(args[0]),
103+
SYSCALL_MUTEX_UNLOCK => sys_mutex_unlock(args[0]),
104+
SYSCALL_SEMAPHORE_CREATE => sys_semaphore_create(args[0]),
105+
SYSCALL_SEMAPHORE_UP => sys_semaphore_up(args[0]),
106+
SYSCALL_SEMAPHORE_DOWN => sys_semaphore_down(args[0]),
107+
SYSCALL_CONDVAR_CREATE => sys_condvar_create(args[0]),
108+
SYSCALL_CONDVAR_SIGNAL => sys_condvar_signal(args[0]),
109+
SYSCALL_CONDVAR_WAIT => sys_condvar_wait(args[0], args[1]),
110+
_ => panic!("Unsupported syscall_id: {}", syscall_id),
111+
}
112+
}

os/src/task/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn block_current_and_run_next() {
5656
let task_cx_ptr = block_current_task();
5757
schedule(task_cx_ptr);
5858
}
59-
59+
#[cfg(feature = "board_qemu")]
6060
use crate::board::QEMUExit;
6161

6262
pub fn exit_current_and_run_next(exit_code: i32) {
@@ -75,6 +75,7 @@ pub fn exit_current_and_run_next(exit_code: i32) {
7575
// the process should terminate at once
7676
if tid == 0 {
7777
let pid = process.getpid();
78+
#[cfg(feature = "board_qemu")]
7879
if pid == IDLE_PID {
7980
println!(
8081
"[kernel] Idle process exit with exit_code {} ...",

0 commit comments

Comments
 (0)