Skip to content

Commit

Permalink
feat: add multicore
Browse files Browse the repository at this point in the history
  • Loading branch information
yfblock committed Apr 27, 2024
1 parent 2267882 commit ed76271
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 164 deletions.
16 changes: 3 additions & 13 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ serde_derive = "1.0.136"

[dependencies]
allocator = { git = "https://github.com/Byte-OS/allocator.git", rev = "c6ce949146d5feab1d406502b19b035f5d392c35"}
crate_interface = { git = "https://github.com/Byte-OS/crate_interface.git" }
frame_allocator = { git = "https://github.com/Byte-OS/bit_frame_allocator.git" }
logging = { git = "https://github.com/Byte-OS/logging.git", features = []}
log = "0.4"
Expand Down
16 changes: 8 additions & 8 deletions kernel/linker.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ SECTIONS

_load_end = .;

.bss ALIGN(4K): {
*(.bss.stack)
_sbss = .;
*(.bss .bss.*)
*(.sbss .sbss.*)
_ebss = .;
}

. = ALIGN(4K);
_percpu_start = .;
.percpu 0x0 : AT(_percpu_start) {
Expand All @@ -62,6 +54,14 @@ SECTIONS
. = _percpu_start + SIZEOF(.percpu);
_percpu_end = .;

.bss ALIGN(4K): {
*(.bss.stack)
_sbss = .;
*(.bss .bss.*)
*(.sbss .sbss.*)
_ebss = .;
}

PROVIDE(end = .);
/DISCARD/ : {
*(.comment) *(.gnu*) *(.note*) *(.eh_frame*)
Expand Down
4 changes: 1 addition & 3 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,7 @@ fn main(hart_id: usize) {

// init kernel threads and async executor
tasks::init();
loop {
arch::wfi()
}
// loop { arch::wfi() }
tasks::run_tasks();

println!("Task All Finished!");
Expand Down
18 changes: 16 additions & 2 deletions kernel/src/panic.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
use arch::shutdown;
use arch::{hart_id, shutdown};
// use backtrace::backtrace;
use core::panic::PanicInfo;

// 程序遇到错误
#[panic_handler]
fn panic_handler(info: &PanicInfo) -> ! {
println!("\x1b[1;31mpanic: '{}'\x1b[0m", info.message().unwrap());
if let Some(location) = info.location() {
println!(
"\x1b[1;31m[Core {}][{}:{}]panic: '{}'\x1b[0m",
hart_id(),
location.file(),
location.line(),
info.message().unwrap()
);
} else {
println!(
"\x1b[1;31m[Core {}]panic: '{}'\x1b[0m",
hart_id(),
info.message().unwrap()
);
}
// backtrace();
println!("!TEST FINISH!");
// loop {}
Expand Down
38 changes: 17 additions & 21 deletions kernel/src/syscall/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use arch::{
};
use async_recursion::async_recursion;
use core::cmp;
use executor::{select, yield_now, AsyncTask, TASK_QUEUE};
use executor::{select, thread, tid2task, yield_now, AsyncTask};
use frame_allocator::{ceil_div, frame_alloc_much, FrameTracker};
use fs::dentry::{dentry_open, dentry_root};
use fs::TimeSpec;
Expand Down Expand Up @@ -228,8 +228,10 @@ pub async fn exec_with_process(
Ok(user_task)
} else {
drop(caches);
let file = dentry_open(dentry_root(), &path, OpenFlags::O_RDONLY).map_err(from_vfs)?;
let file = file.node.clone();
let file = dentry_open(dentry_root(), &path, OpenFlags::O_RDONLY)
.map_err(from_vfs)?
.node
.clone();
debug!("file: {:#x?}", file.metadata().unwrap());
let file_size = file.metadata().unwrap().size;
let frame_ppn = frame_alloc_much(ceil_div(file_size, PAGE_SIZE));
Expand Down Expand Up @@ -454,10 +456,10 @@ impl UserTaskContainer {
);

let new_task = match flags.contains(CloneFlags::CLONE_THREAD) {
true => self.task.clone().thread_clone(user_entry()),
true => self.task.clone().thread_clone(),
// false => curr_task.clone().fork(user_entry()),
// use cow(Copy On Write) to save memory.
false => self.task.clone().cow_fork(user_entry()),
false => self.task.clone().cow_fork(),
};

let clear_child_tid = flags
Expand All @@ -484,6 +486,7 @@ impl UserTaskContainer {
new_tcb.exit_signal = sig as u8;
drop(new_tcb);
yield_now().await;
thread::spawn(new_task.clone(), user_entry());
Ok(new_task.task_id)
}

Expand Down Expand Up @@ -531,11 +534,13 @@ impl UserTaskContainer {
"wait ok: {} waiter: {}",
child_task.task_id, self.task.task_id
);
// release the task resources
self.task
.pcb
.lock()
.children
.retain(|x| x.task_id != child_task.task_id);
child_task.release();
debug!("wait pid: {}", child_task.exit_code().unwrap());

if status.is_valid() {
Expand All @@ -555,11 +560,13 @@ impl UserTaskContainer {
match exit {
Some(t1) => {
let child_task = child_task.unwrap();
// Release task.
self.task
.pcb
.lock()
.children
.retain(|x| x.task_id != child_task.task_id);
child_task.release();
if status.is_valid() {
*status.get_mut() = (t1 as i32) << 8;
}
Expand Down Expand Up @@ -780,23 +787,12 @@ impl UserTaskContainer {
self.tid, pid, signal
);

let user_task = match pid == self.tid {
true => Some(self.task.clone()),
false => TASK_QUEUE
.lock()
.iter()
.find(|x| x.task.get_task_id() == pid)
.ok_or(LinuxError::ESRCH)?
.task
.clone()
let user_task = match tid2task(pid) {
Some(task) => task
.downcast_arc::<UserTask>()
.ok(),
};

let user_task = match user_task {
Some(t) => t,
None => return Err(LinuxError::ESRCH),
};
.map_err(|_| LinuxError::ESRCH),
None => Err(LinuxError::ESRCH),
}?;

user_task.tcb.write().signal.add_signal(signal.clone());

Expand Down
1 change: 1 addition & 0 deletions kernel/src/tasks/async_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::{cmp, future::Future, pin::Pin, task::Poll};

use alloc::{sync::Arc, vec::Vec};
use arch::time::Time;
use executor::AsyncTask;
use sync::Mutex;

use crate::syscall::consts::LinuxError;
Expand Down
44 changes: 29 additions & 15 deletions kernel/src/tasks/initproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use alloc::{
string::{String, ToString},
vec::Vec,
};
use arch::debug::DebugConsole;
use executor::{current_task, task::TaskType, yield_now, TASK_QUEUE};
use arch::{debug::DebugConsole, hart_id, shutdown};
use executor::{current_task, release_task, task::TaskType, tid2task, yield_now, TASK_MAP};
use frame_allocator::get_free_pages;
use fs::{
dentry::{dentry_open, dentry_root, DentryNode},
Expand Down Expand Up @@ -56,9 +56,9 @@ fn clear() {
}

async fn kill_all_tasks() {
TASK_QUEUE
.lock()
.retain(|x| x.task_type != TaskType::MonolithicTask);
TASK_MAP.lock().values().into_iter().for_each(|task| {
task.upgrade().inspect(|x| x.exit(100));
});
}

async fn run_libc_test() -> bool {
Expand Down Expand Up @@ -129,13 +129,10 @@ async fn file_command(cmd: &str) {
args_extend.extend(args.into_iter());
// args.into_iter().for_each(|x| args_extend.push(x));
let task_id = add_user_task(&filename, args_extend, Vec::new()).await;
let task = tid2task(task_id).unwrap();
loop {
if TASK_QUEUE
.lock()
.iter()
.find(|x| x.task.get_task_id() == task_id)
.is_none()
{
if task.exit_code().is_some() {
release_task(task_id);
break;
}
yield_now().await;
Expand Down Expand Up @@ -225,6 +222,7 @@ pub async fn initproc() {
// }

println!("start kernel tasks");

// command("ls").await;
// command("entry-static.exe crypt").await;
// command("./runtest.exe -w entry-dynamic.exe dlopen").await;
Expand Down Expand Up @@ -283,10 +281,12 @@ pub async fn initproc() {
// command("busybox echo run libctest_testcode.sh").await;
// command("busybox sh libctest_testcode.sh").await;

// command("busybox sh ./run-static.sh").await;
// command("./runtest.exe -w entry-dynamic.exe pthread_robust_detach").await;
// command("busybox echo 123").await;
command("qjs.static test.js").await;
// command("qjs.static test.js").await;
// command("qjs.static").await;
command("busybox sh").await;
// command("busybox sh").await;
// command("busybox echo run lua_testcode.sh").await;
// command("busybox sh lua_testcode.sh").await;

Expand All @@ -306,8 +306,8 @@ pub async fn initproc() {
// command("busybox echo run lmbench_testcode.sh").await;
// command("busybox sh lmbench_testcode.sh").await;

// command("busybox echo run unixbench_testcode.sh").await;
// command("busybox sh unixbench_testcode.sh").await;
command("busybox echo run unixbench_testcode.sh").await;
command("busybox sh unixbench_testcode.sh").await;

// command("copy-file-range-test-1").await;
// command("copy-file-range-test-2").await;
Expand Down Expand Up @@ -380,4 +380,18 @@ pub async fn initproc() {

// switch_to_kernel_page_table();
println!("!TEST FINISH!");

// Shutdown if there just have blankkernel task.
if TASK_MAP
.lock()
.values()
.find(|x| {
x.upgrade()
.map(|x| x.get_task_type() != TaskType::BlankKernel)
.unwrap_or(false)
})
.is_none()
{
shutdown();
}
}
12 changes: 4 additions & 8 deletions kernel/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloc::sync::Weak;
use alloc::{sync::Arc, vec::Vec};
use arch::get_cpu_num;
use devices::get_net_device;
use executor::{current_task, thread, yield_now, AsyncTask, TaskId, DEFAULT_EXECUTOR, TASK_QUEUE};
use executor::{current_task, thread, yield_now, AsyncTask, TaskId, DEFAULT_EXECUTOR};
use hal::{ITimerVal, TimeVal};

use crate::syscall::{exec_with_process, NET_SERVER};
Expand Down Expand Up @@ -74,9 +74,6 @@ pub async fn handle_net() {
let mut buffer = vec![0u8; 2048];
// #[cfg(feature = "net")]
loop {
if TASK_QUEUE.lock().len() == 0 {
break;
}
let res = get_net_device(0).recv(&mut buffer);
if let Ok(rlen) = res {
NET_SERVER.analysis_net_data(&buffer[..rlen]);
Expand All @@ -86,11 +83,10 @@ pub async fn handle_net() {
}

pub fn init() {
DEFAULT_EXECUTOR.init(get_cpu_num());
thread::spawn_blank(initproc());
#[cfg(feature = "net")]
thread::spawn_blank(KernelTask::new(handle_net()));

DEFAULT_EXECUTOR.init(get_cpu_num());
}

pub fn run_tasks() {
Expand All @@ -99,8 +95,7 @@ pub fn run_tasks() {

pub async fn add_user_task(filename: &str, args: Vec<&str>, envp: Vec<&str>) -> TaskId {
let curr_task = current_task();
let task = UserTask::new(user_entry(), Weak::new(), initproc::USER_WORK_DIR);

let task = UserTask::new(Weak::new(), initproc::USER_WORK_DIR);
task.before_run();
exec_with_process(
task.clone(),
Expand All @@ -111,6 +106,7 @@ pub async fn add_user_task(filename: &str, args: Vec<&str>, envp: Vec<&str>) ->
.await
.expect("can't add task to excutor");
curr_task.before_run();
thread::spawn(task.clone(), user_entry());

task.get_task_id()
}
Expand Down
Loading

0 comments on commit ed76271

Please sign in to comment.