Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
buhe committed Oct 9, 2021
1 parent 5ea4345 commit 8bafab9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
20 changes: 14 additions & 6 deletions os/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs::{read_dir, File};
use std::io::{Result, Write};
use std::fs::{File, read_dir};

fn main() {
println!("cargo:rerun-if-changed=../user/src/");
Expand All @@ -22,12 +22,16 @@ fn insert_app_data() -> Result<()> {
.collect();
apps.sort();

writeln!(f, r#"
writeln!(
f,
r#"
.align 3
.section .data
.global _num_app
_num_app:
.quad {}"#, apps.len())?;
.quad {}"#,
apps.len()
)?;

for i in 0..apps.len() {
writeln!(f, r#" .quad app_{}_start"#, i)?;
Expand All @@ -36,14 +40,18 @@ _num_app:

for (idx, app) in apps.iter().enumerate() {
println!("app_{}: {}", idx, app);
writeln!(f, r#"
writeln!(
f,
r#"
.section .data
.global app_{0}_start
.global app_{0}_end
.align 3
app_{0}_start:
.incbin "{2}{1}"
app_{0}_end:"#, idx, app, TARGET_PATH)?;
app_{0}_end:"#,
idx, app, TARGET_PATH
)?;
}
Ok(())
}
}
5 changes: 3 additions & 2 deletions os/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ extern "C" fn rust_main(hartid: usize, device_tree_paddr: usize) -> ! {
const DEVICE_TREE_MAGIC: u32 = 0xd00dfeed;
assert_eq!(magic, DEVICE_TREE_MAGIC);
let size = u32::from_be(header.be_size);
let _dtb_data = unsafe { core::slice::from_raw_parts(device_tree_paddr as *const u8, size as usize) };
let _dtb_data =
unsafe { core::slice::from_raw_parts(device_tree_paddr as *const u8, size as usize) };
// let dt = DeviceTree::load(dtb_data).expect("failed to parse device tree");
// DeviceTree::load is not adpator k210
println!("dt size is {:#?}", size);

clear_bss();
heap::init();
mmu::init();
Expand Down
4 changes: 2 additions & 2 deletions os/src/scall_sbi/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use crate::{mmu::translated_byte_buffer, task::current_user_token};
const FD_STDOUT: usize = 1;

pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
match fd {
match fd {
FD_STDOUT => {
// 改为使用虚拟内存
let buffers = translated_byte_buffer(current_user_token(), buf, len);
for buffer in buffers {
print!("{}", core::str::from_utf8(buffer).unwrap());
}
len as isize
},
}
_ => {
panic!("Unsupported fd in sys_write!");
}
Expand Down
27 changes: 16 additions & 11 deletions os/src/task/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{config::{TRAP_CONTEXT, kernel_stack_position}, mmu::{KERNEL_SPACE, MemorySet, PhysPageNum, VirtAddr, MapPermission}, trap::{TrapContext, trap_handler, trap_return}};
use crate::{
config::{kernel_stack_position, TRAP_CONTEXT},
mmu::{MapPermission, MemorySet, PhysPageNum, VirtAddr, KERNEL_SPACE},
trap::{trap_handler, trap_return, TrapContext},
};
use core::{cell::RefCell, usize};
use lazy_static::*;

Expand Down Expand Up @@ -38,13 +42,11 @@ impl AppManagerInner {
// 内核栈单纯用于内核的函数调用
// map a kernel-stack in kernel space
let (kernel_stack_bottom, kernel_stack_top) = kernel_stack_position(0);
KERNEL_SPACE
.lock()
.insert_framed_area(
kernel_stack_bottom.into(),
kernel_stack_top.into(),
MapPermission::R | MapPermission::W,
);
KERNEL_SPACE.lock().insert_framed_area(
kernel_stack_bottom.into(),
kernel_stack_top.into(),
MapPermission::R | MapPermission::W,
);
// 获取 trap context 的指针并赋值
let trap_cx = self.trap_cx_ppn.get_mut();
*trap_cx = TrapContext::app_init_context(
Expand All @@ -69,7 +71,11 @@ lazy_static! {
let app_start_raw: &[usize] =
unsafe { core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) };
app_start[..=num_app].copy_from_slice(app_start_raw);
AppManagerInner { app_start,token: 0, trap_cx_ppn: PhysPageNum(0) }
AppManagerInner {
app_start,
token: 0,
trap_cx_ppn: PhysPageNum(0),
}
}),
};
}
Expand All @@ -91,11 +97,10 @@ pub fn run() -> ! {
trap_return();
}


pub fn current_user_token() -> usize {
APP_MANAGER.inner.borrow().token
}

pub fn current_trap_cx() -> &'static mut TrapContext {
APP_MANAGER.inner.borrow().trap_cx_ppn.get_mut()
}
}
9 changes: 6 additions & 3 deletions os/src/trap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ use riscv::register::{
};
pub use trap_ctx::TrapContext;

use crate::{config::{TRAMPOLINE, TRAP_CONTEXT}, scall_sbi::syscall, task::{current_trap_cx, current_user_token}};
use crate::{
config::{TRAMPOLINE, TRAP_CONTEXT},
scall_sbi::syscall,
task::{current_trap_cx, current_user_token},
};

mod trap_ctx;
global_asm!(include_str!("trap.asm"));


pub fn init() {
set_kernel_trap_entry();
}
Expand All @@ -37,7 +40,7 @@ pub fn trap_return() -> ! {
fn __restore();
}
let restore_va = __restore as usize - __alltraps as usize + TRAMPOLINE;
unsafe {
unsafe {
asm!(
"fence.i",
"jr {restore_va}",
Expand Down
14 changes: 7 additions & 7 deletions os/src/trap/trap_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct TrapContext {
pub x: [usize; 32],
pub sstatus: Sstatus,
pub sepc: usize,
pub kernel_satp: usize,
pub kernel_satp: usize,
pub kernel_sp: usize,
pub trap_handler: usize,
}
Expand All @@ -14,7 +14,7 @@ impl TrapContext {
pub fn set_sp(&mut self, sp: usize) {
self.x[2] = sp;
}
pub fn app_init_context(
pub fn app_init_context(
entry: usize,
sp: usize,
kernel_satp: usize,
Expand All @@ -25,11 +25,11 @@ impl TrapContext {
sstatus.set_spp(SPP::User);
let mut cx = Self {
x: [0; 32],
sstatus,//33
sepc: entry,//34
kernel_satp,//35
kernel_sp,//36
trap_handler,//37
sstatus, //33
sepc: entry, //34
kernel_satp, //35
kernel_sp, //36
trap_handler, //37
};
cx.set_sp(sp);
cx
Expand Down

0 comments on commit 8bafab9

Please sign in to comment.