Skip to content

Commit 01ca0eb

Browse files
committed
fmt & cleanup
Signed-off-by: YdrMaster <[email protected]>
1 parent c76c8ba commit 01ca0eb

File tree

13 files changed

+133
-158
lines changed

13 files changed

+133
-158
lines changed

Cargo.lock

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch5/src/loader.rs

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
1-
/// 根据应用名加载用户进程
2-
use lazy_static::*;
3-
use alloc::vec::Vec;
4-
5-
lazy_static! {
6-
static ref APP_NAMES: Vec<&'static str> = {
7-
extern "C" {
8-
static apps: utils::AppMeta;
9-
fn app_names();
10-
}
11-
let app_num = unsafe { apps.get_app_num() };
12-
let mut start = app_names as usize as *const u8;
13-
let mut v = Vec::new();
14-
unsafe {
15-
for _ in 0..app_num {
16-
let mut end = start;
17-
while end.read_volatile() != b'\0' {
18-
end = end.add(1);
19-
}
20-
let slice = core::slice::from_raw_parts(start, end as usize - start as usize);
21-
let str = core::str::from_utf8(slice).unwrap();
22-
v.push(str);
23-
start = end.add(1);
24-
}
25-
}
26-
v
27-
};
28-
}
29-
30-
/// 获取应用程序 elf 数据
31-
pub fn get_app_data(app_name: &str) -> Option<&'static [u8]> {
32-
extern "C" {
33-
static apps: utils::AppMeta;
34-
}
35-
let app_num = unsafe { apps.get_app_num() as usize };
36-
(0..app_num).find(|&i| APP_NAMES[i] == app_name).map(|i|
37-
unsafe { apps.iter_elf().nth(i) }
38-
).unwrap()
39-
}
40-
41-
/// 获取 app_list
42-
pub fn list_apps() {
43-
println!("/**** APPS ****");
44-
for app in APP_NAMES.iter() {
45-
println!("{}", app);
46-
}
47-
println!("**************/");
48-
}
49-
1+
use alloc::vec::Vec;
2+
/// 根据应用名加载用户进程
3+
use lazy_static::*;
4+
5+
lazy_static! {
6+
static ref APP_NAMES: Vec<&'static str> = {
7+
extern "C" {
8+
static apps: utils::AppMeta;
9+
fn app_names();
10+
}
11+
let app_num = unsafe { apps.get_app_num() };
12+
let mut start = app_names as usize as *const u8;
13+
let mut v = Vec::new();
14+
unsafe {
15+
for _ in 0..app_num {
16+
let mut end = start;
17+
while end.read_volatile() != b'\0' {
18+
end = end.add(1);
19+
}
20+
let slice = core::slice::from_raw_parts(start, end as usize - start as usize);
21+
let str = core::str::from_utf8(slice).unwrap();
22+
v.push(str);
23+
start = end.add(1);
24+
}
25+
}
26+
v
27+
};
28+
}
29+
30+
/// 获取应用程序 elf 数据
31+
pub fn get_app_data(app_name: &str) -> Option<&'static [u8]> {
32+
extern "C" {
33+
static apps: utils::AppMeta;
34+
}
35+
let app_num = unsafe { apps.get_app_num() as usize };
36+
(0..app_num)
37+
.find(|&i| APP_NAMES[i] == app_name)
38+
.map(|i| unsafe { apps.iter_elf().nth(i) })
39+
.unwrap()
40+
}
41+
42+
/// 获取 app_list
43+
pub fn list_apps() {
44+
println!("/**** APPS ****");
45+
for app in APP_NAMES.iter() {
46+
println!("{}", app);
47+
}
48+
println!("**************/");
49+
}

ch5/src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ extern "C" fn rust_main() -> ! {
7575
mm::test();
7676
// 异界传送门
7777
let portal = ForeignPortal::new();
78-
unsafe { PROCESSOR.set_portal(portal); }
78+
unsafe {
79+
PROCESSOR.set_portal(portal);
80+
}
7981

8082
// 建立内核地址空间
8183
let mut ks = kernel_space();
8284
let tramp = (
83-
PPN::<Sv39>::new(unsafe {&PROCESSOR.portal } as *const _ as usize >> Sv39::PAGE_BITS),
85+
PPN::<Sv39>::new(unsafe { &PROCESSOR.portal } as *const _ as usize >> Sv39::PAGE_BITS),
8486
VmFlags::build_from_str("XWRV"),
8587
);
8688
// 传送门映射到所有地址空间
@@ -98,7 +100,7 @@ extern "C" fn rust_main() -> ! {
98100
const PROTAL_TRANSIT: usize = VPN::<Sv39>::MAX.base().val();
99101
loop {
100102
if let Some(task) = unsafe { PROCESSOR.find_next() } {
101-
task.execute(unsafe {&mut PROCESSOR.portal }, PROTAL_TRANSIT);
103+
task.execute(unsafe { &mut PROCESSOR.portal }, PROTAL_TRANSIT);
102104
match scause::read().cause() {
103105
scause::Trap::Exception(scause::Exception::UserEnvCall) => {
104106
use syscall::{SyscallId as Id, SyscallResult as Ret};
@@ -112,7 +114,7 @@ extern "C" fn rust_main() -> ! {
112114
PROCESSOR.make_current_exited();
113115
},
114116
_ => {
115-
let ctx = &mut task.context.context ;
117+
let ctx = &mut task.context.context;
116118
*ctx.a_mut(0) = ret as _;
117119
unsafe {
118120
PROCESSOR.make_current_suspend();

ch5/src/process.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use crate::{mm::PAGE, Sv39Manager};
1+
use crate::{mm::PAGE, Sv39Manager};
2+
use alloc::vec::Vec;
3+
use core::sync::atomic::{AtomicUsize, Ordering};
24
use core::{alloc::Layout, str::FromStr};
3-
use kernel_context::{foreign::ForeignContext, LocalContext, foreign::ForeignPortal};
5+
use kernel_context::{foreign::ForeignContext, foreign::ForeignPortal, LocalContext};
46
use kernel_vm::{
57
page_table::{MmuMeta, Sv39, VAddr, VmFlags, PPN, VPN},
68
AddressSpace,
@@ -9,8 +11,6 @@ use xmas_elf::{
911
header::{self, HeaderPt2, Machine},
1012
program, ElfFile,
1113
};
12-
use alloc::vec::Vec;
13-
use core::sync::atomic::{AtomicUsize, Ordering};
1414

1515
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash, Ord, PartialOrd)]
1616
pub struct TaskId(usize);
@@ -29,7 +29,7 @@ impl TaskId {
2929

3030
pub fn get_val(&self) -> usize {
3131
self.0
32-
}
32+
}
3333
}
3434

3535
/// 进程。
@@ -44,7 +44,6 @@ pub struct Process {
4444
}
4545

4646
impl Process {
47-
4847
pub fn exec(&mut self, elf: ElfFile) {
4948
let proc = Process::from_elf(elf).unwrap();
5049
let tramp = self.address_space.tramp;
@@ -53,7 +52,6 @@ impl Process {
5352
self.context = proc.context;
5453
}
5554

56-
5755
pub fn fork(&mut self) -> Option<Process> {
5856
// 子进程 pid
5957
let pid = TaskId::generate();
@@ -64,12 +62,9 @@ impl Process {
6462
// 复制父进程上下文
6563
let context = self.context.context.clone();
6664
let satp = (8 << 60) | address_space.root_ppn().val();
67-
let foreign_ctx = ForeignContext {
68-
context,
69-
satp,
70-
};
65+
let foreign_ctx = ForeignContext { context, satp };
7166
self.children.push(pid);
72-
Some( Self {
67+
Some(Self {
7368
pid,
7469
parent: self.pid,
7570
children: Vec::new(),
@@ -148,4 +143,4 @@ impl Process {
148143
pub fn execute(&mut self, portal: &mut ForeignPortal, portal_transit: usize) {
149144
unsafe { self.context.execute(portal, portal_transit) };
150145
}
151-
}
146+
}

ch6/src/main.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ unsafe extern "C" fn _start() -> ! {
6363
static mut KERNEL_SPACE: Once<AddressSpace<Sv39, Sv39Manager>> = Once::new();
6464
static mut PROCESSOR: Processor<Process, TaskId> = Processor::new();
6565

66-
6766
extern "C" fn rust_main() -> ! {
6867
// bss 段清零
6968
utils::zero_bss();
@@ -84,9 +83,11 @@ extern "C" fn rust_main() -> ! {
8483
// 异界传送门
8584
// 可以直接放在栈上
8685
let portal = ForeignPortal::new();
87-
unsafe { PROCESSOR.set_portal(portal); }
86+
unsafe {
87+
PROCESSOR.set_portal(portal);
88+
}
8889
let tramp = (
89-
PPN::<Sv39>::new(unsafe {&PROCESSOR.portal } as *const _ as usize >> Sv39::PAGE_BITS),
90+
PPN::<Sv39>::new(unsafe { &PROCESSOR.portal } as *const _ as usize >> Sv39::PAGE_BITS),
9091
VmFlags::build_from_str("XWRV"),
9192
);
9293
// 传送门映射到所有地址空间
@@ -111,7 +112,7 @@ extern "C" fn rust_main() -> ! {
111112
const PROTAL_TRANSIT: usize = VPN::<Sv39>::MAX.base().val();
112113
loop {
113114
if let Some(task) = unsafe { PROCESSOR.find_next() } {
114-
task.execute(unsafe {&mut PROCESSOR.portal }, PROTAL_TRANSIT);
115+
task.execute(unsafe { &mut PROCESSOR.portal }, PROTAL_TRANSIT);
115116
match scause::read().cause() {
116117
scause::Trap::Exception(scause::Exception::UserEnvCall) => {
117118
use syscall::{SyscallId as Id, SyscallResult as Ret};
@@ -125,7 +126,7 @@ extern "C" fn rust_main() -> ! {
125126
PROCESSOR.make_current_exited();
126127
},
127128
_ => {
128-
let ctx = &mut task.context.context ;
129+
let ctx = &mut task.context.context;
129130
*ctx.a_mut(0) = ret as _;
130131
unsafe {
131132
PROCESSOR.make_current_suspend();

kernel-vm/src/space/mod.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
mod mapper;
1+
mod mapper;
22
mod visitor;
33

44
extern crate alloc;
55

66
use crate::PageManager;
7-
use core::{fmt, marker::PhantomData, ops::Range, ptr::NonNull};
87
use alloc::vec::Vec;
8+
use core::{fmt, marker::PhantomData, ops::Range, ptr::NonNull};
99
use mapper::Mapper;
1010
use page_table::{PageTable, PageTableFormatter, Pos, VAddr, VmFlags, VmMeta, PPN, VPN};
1111
use visitor::Visitor;
@@ -24,9 +24,9 @@ impl<Meta: VmMeta, M: PageManager<Meta>> AddressSpace<Meta, M> {
2424
/// 创建新地址空间。
2525
#[inline]
2626
pub fn new() -> Self {
27-
Self{
27+
Self {
2828
areas: Vec::new(),
29-
page_manager: M::new_root(),
29+
page_manager: M::new_root(),
3030
phantom_data: PhantomData,
3131
tramp: (PPN::INVALID, VmFlags::ZERO),
3232
}
@@ -123,22 +123,15 @@ impl<Meta: VmMeta, M: PageManager<Meta>> AddressSpace<Meta, M> {
123123
.ans()
124124
.filter(|pte| pte.is_valid())
125125
.map(|pte| {
126-
(
127-
pte.flags(),
128-
unsafe {
129-
NonNull::new_unchecked(
130-
self.page_manager
131-
.p_to_v::<u8>(pte.ppn())
132-
.as_ptr()
133-
)
134-
}
135-
)
136-
}
137-
).unwrap();
126+
(pte.flags(), unsafe {
127+
NonNull::new_unchecked(self.page_manager.p_to_v::<u8>(pte.ppn()).as_ptr())
128+
})
129+
})
130+
.unwrap();
138131
let vpn_range = range.start..range.end;
139132
// 虚拟地址块中页数量
140133
let count = range.end.val() - range.start.val();
141-
let size = count << Meta::PAGE_BITS;
134+
let size = count << Meta::PAGE_BITS;
142135
// 分配 count 个 flags 属性的物理页面
143136
let paddr = new_addrspace.page_manager.allocate(count, &mut flags);
144137
let ppn = new_addrspace.page_manager.v_to_p(paddr);

task-manage/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
buddy-allocator = { git = "https://github.com/YdrMaster/buddy-allocator", rev = "fae1979" }
10-
# console = { path = "../console"}
11-
kernel-context ={ path = "../kernel-context"}
9+
kernel-context = { path = "../kernel-context" }

task-manage/src/lib.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
//! 任务管理 lib
2-
3-
#![no_std]
4-
#![deny(warnings, missing_docs)]
5-
#![feature(const_btree_new, const_mut_refs)]
6-
7-
mod processor;
8-
mod scheduler;
9-
mod manager;
10-
mod task;
11-
12-
pub use manager::TaskManager;
13-
pub use processor::Processor;
14-
pub use task::Execute;
15-
16-
extern crate alloc;
17-
18-
// extern crate console;
19-
20-
1+
//! 任务管理 lib
2+
3+
#![no_std]
4+
#![deny(warnings, missing_docs)]
5+
#![feature(const_btree_new, const_mut_refs)]
6+
7+
mod manager;
8+
mod processor;
9+
mod scheduler;
10+
mod task;
11+
12+
pub use manager::TaskManager;
13+
pub use processor::Processor;
14+
pub use task::Execute;
15+
16+
extern crate alloc;

task-manage/src/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct TaskManager<T, I: Copy + Ord> {
1111
impl<T, I: Copy + Ord> TaskManager<T, I> {
1212
/// 新建任务管理器
1313
pub const fn new() -> Self {
14-
Self {
14+
Self {
1515
tasks: BTreeMap::new(),
1616
}
1717
}

0 commit comments

Comments
 (0)