Skip to content

Commit

Permalink
feat: ready to support multiccore
Browse files Browse the repository at this point in the history
  • Loading branch information
yfblock committed Apr 24, 2024
1 parent 08b6246 commit 2267882
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 164 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rustflags = [
'-Clink-arg=-no-pie',
'--cfg=board="qemu"',
'-Zunstable-options',
'-Ztls-model=local-exec',
# '--cfg=driver="kvirtio,kgoldfish-rtc,ns16550a"',
# '--extern=force:kernel'
# '-Zunstable-options',
Expand Down
32 changes: 8 additions & 24 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ K210-SERIALPORT = /dev/ttyUSB0
K210-BURNER = tools/k210/kflash.py
QEMU_EXEC += -m 1G\
-nographic \
-smp 1 \
-smp 2 \
-D qemu.log -d in_asm,int,pcall,cpu_reset,guest_errors

ifeq ($(RELEASE), release)
Expand Down
28 changes: 18 additions & 10 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ mod tasks;
mod user;

use arch::addr::{PhysPage, VirtPage};
use arch::multicore::MultiCore;
use arch::{
arch_entry, arch_interrupt, disable_irq, enable_irq, get_mem_areas, PageAlloc, TrapFrame,
TrapFrameArgs, TrapType, VIRT_ADDR_START,
disable_irq, enable_irq, get_mem_areas, PageAlloc, TrapFrame, TrapFrameArgs, TrapType,
VIRT_ADDR_START,
};
use devices::{self, get_int_device};
use executor::current_task;
Expand Down Expand Up @@ -60,7 +61,7 @@ impl PageAlloc for PageAllocImpl {
}
}

#[arch_interrupt]
#[arch::arch_interrupt]
/// Handle kernel interrupt
fn kernel_interrupt(cx_ref: &mut TrapFrame, trap_type: TrapType) {
match trap_type {
Expand All @@ -71,7 +72,7 @@ fn kernel_interrupt(cx_ref: &mut TrapFrame, trap_type: TrapType) {
panic!("kernel error: {:#x}", addr);
}
// judge whether it is trigger by a user_task handler.
if let Some(task) = current_task().as_any().downcast::<UserTask>().ok() {
if let Some(task) = current_task().downcast_arc::<UserTask>().ok() {
let cx_ref = task.force_cx_ref();
if task.pcb.is_locked() {
// task.pcb.force_unlock();
Expand All @@ -81,7 +82,7 @@ fn kernel_interrupt(cx_ref: &mut TrapFrame, trap_type: TrapType) {
}
user_cow_int(task, cx_ref, addr);
} else {
panic!("page fault: {:?}", trap_type);
panic!("page fault: {:#x?}", trap_type);
}
}
TrapType::IllegalInstruction(addr) => {
Expand Down Expand Up @@ -129,8 +130,8 @@ fn kernel_interrupt(cx_ref: &mut TrapFrame, trap_type: TrapType) {
};
}

#[arch_entry]
/// The kernel entry
#[arch::arch_entry]
fn main(hart_id: usize) {
disable_irq();
if hart_id == 0 {
Expand All @@ -156,6 +157,9 @@ fn main(hart_id: usize) {

info!("program size: {}KB", (end as usize - start as usize) / 1024);

// Boot all application core.
MultiCore::boot_all();

devices::prepare_drivers();

if let Some(fdt) = arch::get_fdt() {
Expand Down Expand Up @@ -210,6 +214,10 @@ fn main(hart_id: usize) {

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

println!("Task All Finished!");
} else {
Expand All @@ -219,9 +227,9 @@ fn main(hart_id: usize) {
hal::interrupt::init();

// enable_irq();

loop {
info!("aux core");
}
enable_irq();
// loop { arch::wfi() }
tasks::run_tasks();
info!("shutdown ap core");
}
}
4 changes: 2 additions & 2 deletions kernel/src/syscall/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use vfscore::FileType;
use alloc::sync::Arc;
use arch::addr::VirtAddr;
use bit_field::BitArray;
use executor::{yield_now, AsyncTask};
use executor::yield_now;
use fs::pipe::create_pipe;
use fs::{OpenFlags, PollEvent, PollFd, SeekFrom, Stat, StatFS, StatMode, TimeSpec, UTIME_NOW};
use log::debug;
Expand Down Expand Up @@ -479,7 +479,7 @@ impl UserTaskContainer {
);
let cmd = FromPrimitive::from_usize(cmd).ok_or(LinuxError::EINVAL)?;
let file = self.task.get_fd(fd).ok_or(LinuxError::EBADF)?;
debug!("[task {}] fcntl: {:?}", self.task.get_task_id(), cmd);
debug!("[task {}] fcntl: {:?}", self.tid, cmd);
match cmd {
FcntlCmd::DUPFD | FcntlCmd::DUPFDCLOEXEC => self.sys_dup(fd).await,
FcntlCmd::GETFD => Ok(1),
Expand Down
8 changes: 2 additions & 6 deletions kernel/src/syscall/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::net::{Ipv4Addr, SocketAddrV4};

use alloc::sync::Arc;
use devices::get_net_device;
use executor::{yield_now, AsyncTask};
use executor::yield_now;
use log::{debug, warn};
use lose_net_stack::connection::NetServer;
use lose_net_stack::net_trait::NetInterface;
Expand Down Expand Up @@ -340,11 +340,7 @@ impl UserTaskContainer {
socket_addr.family = 2;
socket_addr.addr = *socket_address.ip();
socket_addr.in_port = socket_address.port().to_be();
debug!(
"[task {}] socket address: {:?}",
self.task.get_task_id(),
socket_address
);
debug!("[task {}] socket address: {:?}", self.tid, socket_address);
}
Ok(0)
}
Expand Down
27 changes: 11 additions & 16 deletions kernel/src/syscall/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,10 @@ impl UserTaskContainer {
new_tcb.cx[TrapFrameArgs::TLS] = tls;
}
if flags.contains(CloneFlags::CLONE_PARENT_SETTID) {
*ptid.get_mut() = new_task.get_task_id() as _;
*ptid.get_mut() = new_task.task_id as _;
}
if flags.contains(CloneFlags::CLONE_CHILD_SETTID) && ctid.is_valid() {
*ctid.get_mut() = new_task.get_task_id() as _;
*ctid.get_mut() = new_task.task_id as _;
}
new_tcb.exit_signal = sig as u8;
drop(new_tcb);
Expand Down Expand Up @@ -529,14 +529,13 @@ impl UserTaskContainer {

debug!(
"wait ok: {} waiter: {}",
child_task.get_task_id(),
self.task.get_task_id()
child_task.task_id, self.task.task_id
);
self.task
.pcb
.lock()
.children
.retain(|x| x.task_id != child_task.get_task_id());
.retain(|x| x.task_id != child_task.task_id);
debug!("wait pid: {}", child_task.exit_code().unwrap());

if status.is_valid() {
Expand Down Expand Up @@ -565,7 +564,7 @@ impl UserTaskContainer {
*status.get_mut() = (t1 as i32) << 8;
}
// TIPS: This is a small change.
Ok(child_task.get_task_id())
Ok(child_task.task_id)
// Ok(0)
}
None => Ok(0),
Expand Down Expand Up @@ -616,7 +615,7 @@ impl UserTaskContainer {
.parent
.read()
.upgrade()
.map(|x| x.get_task_id())
.map(|x| x.task_id)
.ok_or(LinuxError::EPERM)
}

Expand Down Expand Up @@ -786,11 +785,11 @@ impl UserTaskContainer {
false => TASK_QUEUE
.lock()
.iter()
.find(|x| x.get_task_id() == pid)
.map(|x| x.clone())
.find(|x| x.task.get_task_id() == pid)
.ok_or(LinuxError::ESRCH)?
.as_any()
.downcast::<UserTask>()
.task
.clone()
.downcast_arc::<UserTask>()
.ok(),
};

Expand All @@ -811,11 +810,7 @@ impl UserTaskContainer {
let parent = self.task.parent.read().clone();

if let Some(parent) = parent.upgrade() {
parent
.pcb
.lock()
.children
.retain(|x| x.get_task_id() != self.task.get_task_id());
parent.pcb.lock().children.retain(|x| x.task_id != self.tid);
*self.task.parent.write() = Weak::<UserTask>::new();
}
Ok(0)
Expand Down
16 changes: 3 additions & 13 deletions kernel/src/tasks/initproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::{
vec::Vec,
};
use arch::debug::DebugConsole;
use executor::{current_task, yield_now, FUTURE_LIST, TASK_QUEUE};
use executor::{current_task, task::TaskType, yield_now, TASK_QUEUE};
use frame_allocator::get_free_pages;
use fs::{
dentry::{dentry_open, dentry_root, DentryNode},
Expand Down Expand Up @@ -58,17 +58,7 @@ fn clear() {
async fn kill_all_tasks() {
TASK_QUEUE
.lock()
.iter()
.for_each(|x| match x.clone().as_any().downcast::<UserTask>() {
Ok(user_task) => {
user_task.exit(0);
FUTURE_LIST.lock().remove(&user_task.task_id);
}
Err(_) => {}
});
TASK_QUEUE
.lock()
.retain(|x| x.clone().as_any().downcast::<UserTask>().is_err());
.retain(|x| x.task_type != TaskType::MonolithicTask);
}

async fn run_libc_test() -> bool {
Expand Down Expand Up @@ -143,7 +133,7 @@ async fn file_command(cmd: &str) {
if TASK_QUEUE
.lock()
.iter()
.find(|x| x.get_task_id() == task_id)
.find(|x| x.task.get_task_id() == task_id)
.is_none()
{
break;
Expand Down
Loading

0 comments on commit 2267882

Please sign in to comment.