Skip to content

Commit 443465c

Browse files
wyfcyxwangrunji0408
authored andcommitted
chk-ch9-part1
1 parent 86abde4 commit 443465c

File tree

9 files changed

+139
-14
lines changed

9 files changed

+139
-14
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
os/target
22
usr/rust/target
3+
usr/build
34
os/src/link_user.S
45
Cargo.lock

os/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] }
1111
spin = "0.5.2"
1212
buddy_system_allocator = "0.3"
1313
xmas-elf = "0.6"
14+
rcore-fs = { git = "https://github.com/rcore-os/rcore-fs", rev = "d8d61190" }
15+
rcore-fs-sfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "d8d61190" }
16+
17+
[dependencies.lazy_static]
18+
version = "1.0"
19+
features = ["spin_no_std"]

os/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ env:
1313
rustup component add llvm-tools-preview rustfmt
1414
rustup target add $(target)
1515

16-
export USER_IMG = ../usr/rust/target/$(target)/debug/hello_world
16+
export USER_IMG = ../usr/build/riscv64.img
1717

1818
kernel:
1919
cargo build

os/src/fs/device.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use spin::RwLock;
2+
use rcore_fs::dev::*;
3+
4+
pub struct MemBuf(RwLock<&'static mut [u8]>);
5+
6+
impl MemBuf {
7+
pub unsafe fn new(begin: usize, end: usize) -> Self {
8+
use core::slice;
9+
MemBuf(
10+
RwLock::new(
11+
slice::from_raw_parts_mut(
12+
begin as *mut u8,
13+
end - begin
14+
)
15+
)
16+
)
17+
}
18+
}
19+
20+
21+
impl Device for MemBuf {
22+
fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> {
23+
let slice = self.0.read();
24+
let len = buf.len().min(slice.len() - offset);
25+
buf[..len].copy_from_slice(&slice[offset..offset + len]);
26+
Ok(len)
27+
}
28+
fn write_at(&self, offset: usize, buf: &[u8]) -> Result<usize> {
29+
let mut slice = self.0.write();
30+
let len = buf.len().min(slice.len() - offset);
31+
slice[offset..offset + len].copy_from_slice(&buf[..len]);
32+
Ok(len)
33+
}
34+
fn sync(&self) -> Result<()> {
35+
Ok(())
36+
}
37+
}

os/src/fs/mod.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
mod device;
2+
3+
use lazy_static::*;
4+
use rcore_fs::vfs::*;
5+
use rcore_fs_sfs::SimpleFileSystem;
6+
use alloc::{ sync::Arc, vec::Vec };
7+
8+
lazy_static! {
9+
pub static ref ROOT_INODE: Arc<dyn INode> = {
10+
let device = {
11+
extern "C" {
12+
fn _user_img_start();
13+
fn _user_img_end();
14+
};
15+
let start = _user_img_start as usize;
16+
let end = _user_img_end as usize;
17+
Arc::new(unsafe { device::MemBuf::new(start, end) })
18+
};
19+
let sfs = SimpleFileSystem::open(device).expect("failed to open SFS");
20+
sfs.root_inode()
21+
22+
};
23+
}
24+
25+
pub trait INodeExt {
26+
fn read_as_vec(&self) -> Result<Vec<u8>>;
27+
}
28+
29+
impl INodeExt for dyn INode {
30+
fn read_as_vec(&self) -> Result<Vec<u8>> {
31+
let size = self.metadata()?.size;
32+
let mut buf = Vec::with_capacity(size);
33+
unsafe {
34+
buf.set_len(size);
35+
}
36+
self.read_at(0, buf.as_mut_slice())?;
37+
Ok(buf)
38+
}
39+
}
40+
41+
pub fn init() {
42+
println!("available programs in rust/ are:");
43+
let mut id = 0;
44+
let mut rust_dir = ROOT_INODE.lookup("rust").unwrap();
45+
while let Ok(name) = rust_dir.get_entry(id) {
46+
id += 1;
47+
println!(" {}", name);
48+
}
49+
println!("++++ setup fs! ++++")
50+
}

os/src/init.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub extern "C" fn rust_main() -> ! {
1818
((end as usize - KERNEL_BEGIN_VADDR + KERNEL_BEGIN_PADDR) >> 12) + 1,
1919
PHYSICAL_MEMORY_END >> 12
2020
);
21+
crate::fs::init();
2122
crate::process::init();
2223
crate::timer::init();
2324
crate::process::run();

os/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ mod consts;
1919
mod memory;
2020
mod process;
2121
mod syscall;
22+
mod fs;

os/src/process/mod.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use processor::Processor;
88
use scheduler::RRScheduler;
99
use thread_pool::ThreadPool;
1010
use alloc::boxed::Box;
11-
11+
use crate::fs::{
12+
ROOT_INODE,
13+
INodeExt
14+
};
1215

1316
pub type Tid = usize;
1417
pub type ExitCode = usize;
@@ -17,7 +20,7 @@ pub type ExitCode = usize;
1720
static CPU: Processor = Processor::new();
1821

1922
pub fn init() {
20-
let scheduler = RRScheduler::new(2);
23+
let scheduler = RRScheduler::new(1);
2124
let thread_pool = ThreadPool::new(100, Box::new(scheduler));
2225
let idle = Thread::new_kernel(Processor::idle_main as usize);
2326
idle.append_initial_arguments([&CPU as *const Processor as usize, 0, 0]);
@@ -31,17 +34,12 @@ pub fn init() {
3134
});
3235
}
3336

34-
extern "C" {
35-
fn _user_img_start();
36-
fn _user_img_end();
37-
}
38-
let data = unsafe {
39-
core::slice::from_raw_parts(
40-
_user_img_start as *const u8,
41-
_user_img_end as usize - _user_img_start as usize,
42-
)
43-
};
44-
let user_thread = unsafe { Thread::new_user(data) };
37+
let data = ROOT_INODE
38+
.lookup("rust/hello_world")
39+
.unwrap()
40+
.read_as_vec()
41+
.unwrap();
42+
let user_thread = unsafe { Thread::new_user(data.as_slice()) };
4543
CPU.add_thread(user_thread);
4644

4745
println!("++++ setup process! ++++");

usr/Makefile

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
target := riscv64imac-unknown-none-elf
2+
mode := debug
3+
rust_src_dir := rust/src/bin
4+
rust_target_dir := rust/target/$(target)/$(mode)
5+
rust_srcs := $(wildcard $(rust_src_dir)/*.rs)
6+
rust_targets := $(patsubst $(rust_src_dir)/%.rs, $(rust_target_dir)/%, $(rust_srcs))
7+
out_dir := build/riscv64
8+
sfsimg := build/riscv64.img
9+
.PHONY: rcore-fs-fuse rust user_img clean
10+
11+
12+
rcore-fs-fuse:
13+
ifeq ($(shell which rcore-fs-fuse),)
14+
@echo Installing rcore-fs-fuse
15+
@cargo install rcore-fs-fuse --git https://github.com/rcore-os/rcore-fs --rev d8d61190
16+
endif
17+
18+
rust:
19+
@cd rust && cargo build
20+
@echo targets includes $(rust_targets)
21+
@rm -rf $(out_dir)/rust && mkdir -p $(out_dir)/rust
22+
@rm -f $(sfsimg)
23+
@cp $(rust_targets) $(out_dir)/rust
24+
25+
$(sfsimg): rcore-fs-fuse rust
26+
@rcore-fs-fuse --fs sfs $@ $(out_dir) zip
27+
28+
user_img: $(sfsimg)
29+
30+
clean:
31+
@rm -rf build/

0 commit comments

Comments
 (0)