Skip to content

Commit

Permalink
process implement
Browse files Browse the repository at this point in the history
  • Loading branch information
buhe committed Nov 14, 2021
1 parent 02444a2 commit e0f04b5
Show file tree
Hide file tree
Showing 18 changed files with 377 additions and 62 deletions.
5 changes: 0 additions & 5 deletions os/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ pub const PAGE_SIZE_BITS: usize = 0xc; //12 bit

pub const TRAMPOLINE: usize = usize::MAX - PAGE_SIZE + 1;
pub const TRAP_CONTEXT: usize = TRAMPOLINE - PAGE_SIZE;
pub fn kernel_stack_position(app_id: usize) -> (usize, usize) {
let top = TRAMPOLINE - app_id * (KERNEL_STACK_SIZE + PAGE_SIZE);
let bottom = top - KERNEL_STACK_SIZE;
(bottom, top)
}

pub const CLOCK_FREQ: usize = 403000000 / 62;

Expand Down
2 changes: 1 addition & 1 deletion os/src/driver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod gpio;
// mod ir;
mod lcd;
mod network;
// mod network;
pub fn init() {
gpio::init();
lcd::init();
Expand Down
7 changes: 6 additions & 1 deletion os/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use core::panic::PanicInfo;
use crate::scall_sbi::shutdown;

#[panic_handler]
fn panic(_: &PanicInfo) -> ! {
fn panic(info: &PanicInfo) -> ! {
if let Some(location) = info.location() {
println!("[kernel] Panicked at {}:{} {}", location.file(), location.line(), info.message().unwrap());
} else {
println!("[kernel] Panicked: {}", info.message().unwrap());
}
shutdown();
}
44 changes: 37 additions & 7 deletions os/src/link_app.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,88 @@
.section .data
.global _num_app
_num_app:
.quad 5
.quad 8
.quad app_0_start
.quad app_1_start
.quad app_2_start
.quad app_3_start
.quad app_4_start
.quad app_4_end
.quad app_5_start
.quad app_6_start
.quad app_7_start
.quad app_7_end

.global _app_names
_app_names:
.string "forktest"
.string "hello"
.string "initproc"
.string "power_3"
.string "power_5"
.string "power_7"
.string "sleep"
.string "user_shell"

.section .data
.global app_0_start
.global app_0_end
.align 3
app_0_start:
.incbin "../user/target/riscv64gc-unknown-none-elf/release/hello"
.incbin "../user/target/riscv64gc-unknown-none-elf/release/forktest"
app_0_end:

.section .data
.global app_1_start
.global app_1_end
.align 3
app_1_start:
.incbin "../user/target/riscv64gc-unknown-none-elf/release/power_3"
.incbin "../user/target/riscv64gc-unknown-none-elf/release/hello"
app_1_end:

.section .data
.global app_2_start
.global app_2_end
.align 3
app_2_start:
.incbin "../user/target/riscv64gc-unknown-none-elf/release/power_5"
.incbin "../user/target/riscv64gc-unknown-none-elf/release/initproc"
app_2_end:

.section .data
.global app_3_start
.global app_3_end
.align 3
app_3_start:
.incbin "../user/target/riscv64gc-unknown-none-elf/release/power_7"
.incbin "../user/target/riscv64gc-unknown-none-elf/release/power_3"
app_3_end:

.section .data
.global app_4_start
.global app_4_end
.align 3
app_4_start:
.incbin "../user/target/riscv64gc-unknown-none-elf/release/sleep"
.incbin "../user/target/riscv64gc-unknown-none-elf/release/power_5"
app_4_end:

.section .data
.global app_5_start
.global app_5_end
.align 3
app_5_start:
.incbin "../user/target/riscv64gc-unknown-none-elf/release/power_7"
app_5_end:

.section .data
.global app_6_start
.global app_6_end
.align 3
app_6_start:
.incbin "../user/target/riscv64gc-unknown-none-elf/release/sleep"
app_6_end:

.section .data
.global app_7_start
.global app_7_end
.align 3
app_7_start:
.incbin "../user/target/riscv64gc-unknown-none-elf/release/user_shell"
app_7_end:
2 changes: 1 addition & 1 deletion os/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ pub fn list_apps() {
for app in APP_NAMES.iter() {
println!("{}", app);
}
println!("**************/")
println!("**************/");
}
4 changes: 4 additions & 0 deletions os/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![feature(asm)]
#![feature(global_asm)]
#![feature(alloc_error_handler)]
#![feature(panic_info_message)]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![no_std]
Expand Down Expand Up @@ -70,6 +71,9 @@ extern "C" fn rust_main(_hartid: usize, _: usize) -> ! {
heap::init();
driver::init();
mmu::init();

task::add_initproc();
println!("after initproc!");
trap::init();

println!("{}", logo::LOGO);
Expand Down
28 changes: 27 additions & 1 deletion os/src/scall_sbi/fs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{mmu::translated_byte_buffer, task::current_user_token};
use crate::{mmu::translated_byte_buffer, scall_sbi::console_getchar, task::{current_user_token, suspend_current_and_run_next}};

const FD_STDIN: usize = 0;
const FD_STDOUT: usize = 1;

pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
Expand All @@ -18,3 +19,28 @@ pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
}
}
}

pub fn sys_read(fd: usize, buf: *const u8, len: usize) -> isize {
match fd {
FD_STDIN => {
assert_eq!(len, 1, "Only support len = 1 in sys_read!");
let mut c: usize;
loop {
c = console_getchar();
if c == 0 {
suspend_current_and_run_next();
continue;
} else {
break;
}
}
let ch = c as u8;
let mut buffers = translated_byte_buffer(current_user_token(), buf, len);
unsafe { buffers[0].as_mut_ptr().write_volatile(ch); }
1
}
_ => {
panic!("Unsupported fd in sys_read!");
}
}
}
51 changes: 16 additions & 35 deletions os/src/scall_sbi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,33 @@
#[inline(always)]
fn scall(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
let mut ret;
unsafe {
asm!("ecall",
in( "x10") arg0,
in("x11") arg1,
in ("x12") arg2,
in("x17") which,
lateout("x10") ret,
options(nostack)
);
}
ret
}

const SBI_SET_TIMER: usize = 0;
const SBI_CONSOLE_PUTCHAR: usize = 1;
const SBI_SHUTDOWN: usize = 8;

pub fn shutdown() -> ! {
scall(SBI_SHUTDOWN, 0, 0, 0);
panic!("It should shutdown!");
}
mod proxy;
pub use proxy::*;
mod fs;
mod process;

pub fn put_char(c: usize) {
scall(SBI_CONSOLE_PUTCHAR, c, 0, 0);
}
use fs::*;
use process::*;

pub fn set_timer(timer: usize) {
scall(SBI_SET_TIMER, timer, 0, 0);
}
const SYSCALL_READ: usize = 63;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_YIELD: usize = 124;
const SYSCALL_GET_TIME: usize = 169;
const SYSCALL_GETPID: usize = 172;
const SYSCALL_FORK: usize = 220;
const SYSCALL_EXEC: usize = 221;
const SYSCALL_WAITPID: usize = 260;

mod fs;
mod process;

use fs::*;
use process::*;
// 处理来自 apps 的 trap
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
match syscall_id {
SYSCALL_READ => sys_read(args[0], args[1] as *const u8, args[2]),
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
SYSCALL_EXIT => sys_exit(args[0] as i32),
SYSCALL_YIELD => sys_yield(),
SYSCALL_GET_TIME => sys_get_time(),
SYSCALL_GETPID => sys_getpid(),
SYSCALL_FORK => sys_fork(),
SYSCALL_EXEC => sys_exec(args[0] as *const u8),
SYSCALL_WAITPID => sys_waitpid(args[0] as isize, args[1] as *mut i32),
_ => panic!("Unsupported syscall_id: {}", syscall_id),
}
}
38 changes: 38 additions & 0 deletions os/src/scall_sbi/proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[inline(always)]
fn scall(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
let mut ret;
unsafe {
asm!("ecall",
in( "x10") arg0,
in("x11") arg1,
in ("x12") arg2,
in("x17") which,
lateout("x10") ret,
options(nostack)
);
}
ret
}

const SBI_SET_TIMER: usize = 0;
const SBI_CONSOLE_PUTCHAR: usize = 1;
const SBI_CONSOLE_GETCHAR: usize = 2;
const SBI_SHUTDOWN: usize = 8;

pub fn shutdown() -> ! {
scall(SBI_SHUTDOWN, 0, 0, 0);
panic!("It should shutdown!");
}

pub fn put_char(c: usize) {
scall(SBI_CONSOLE_PUTCHAR, c, 0, 0);
}

pub fn set_timer(timer: usize) {
scall(SBI_SET_TIMER, timer, 0, 0);
}


pub fn console_getchar() -> usize {
scall(SBI_CONSOLE_GETCHAR, 0, 0, 0)
}
18 changes: 18 additions & 0 deletions user/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion user/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
[dependencies]
buddy_system_allocator = "0.6"
34 changes: 34 additions & 0 deletions user/src/bin/forktest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#![no_std]
#![no_main]

#[macro_use]
extern crate user;

use user::{fork, wait, exit};

const MAX_CHILD: usize = 40;

#[no_mangle]
pub fn main() -> i32 {
for i in 0..MAX_CHILD {
let pid = fork();
if pid == 0 {
println!("I am child {}", i);
exit(0);
} else {
println!("forked child pid = {}", pid);
}
assert!(pid > 0);
}
let mut exit_code: i32 = 0;
for _ in 0..MAX_CHILD {
if wait(&mut exit_code) <= 0 {
panic!("wait stopped early");
}
}
if wait(&mut exit_code) > 0 {
panic!("wait got too many");
}
println!("forktest pass.");
0
}
Loading

0 comments on commit e0f04b5

Please sign in to comment.