Skip to content

Commit 2267882

Browse files
committed
feat: ready to support multiccore
1 parent 08b6246 commit 2267882

File tree

10 files changed

+65
-164
lines changed

10 files changed

+65
-164
lines changed

.cargo/config.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ rustflags = [
1111
'-Clink-arg=-no-pie',
1212
'--cfg=board="qemu"',
1313
'-Zunstable-options',
14+
'-Ztls-model=local-exec',
1415
# '--cfg=driver="kvirtio,kgoldfish-rtc,ns16550a"',
1516
# '--extern=force:kernel'
1617
# '-Zunstable-options',

Cargo.lock

+8-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ K210-SERIALPORT = /dev/ttyUSB0
5050
K210-BURNER = tools/k210/kflash.py
5151
QEMU_EXEC += -m 1G\
5252
-nographic \
53-
-smp 1 \
53+
-smp 2 \
5454
-D qemu.log -d in_asm,int,pcall,cpu_reset,guest_errors
5555

5656
ifeq ($(RELEASE), release)

kernel/src/main.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ mod tasks;
3030
mod user;
3131

3232
use arch::addr::{PhysPage, VirtPage};
33+
use arch::multicore::MultiCore;
3334
use arch::{
34-
arch_entry, arch_interrupt, disable_irq, enable_irq, get_mem_areas, PageAlloc, TrapFrame,
35-
TrapFrameArgs, TrapType, VIRT_ADDR_START,
35+
disable_irq, enable_irq, get_mem_areas, PageAlloc, TrapFrame, TrapFrameArgs, TrapType,
36+
VIRT_ADDR_START,
3637
};
3738
use devices::{self, get_int_device};
3839
use executor::current_task;
@@ -60,7 +61,7 @@ impl PageAlloc for PageAllocImpl {
6061
}
6162
}
6263

63-
#[arch_interrupt]
64+
#[arch::arch_interrupt]
6465
/// Handle kernel interrupt
6566
fn kernel_interrupt(cx_ref: &mut TrapFrame, trap_type: TrapType) {
6667
match trap_type {
@@ -71,7 +72,7 @@ fn kernel_interrupt(cx_ref: &mut TrapFrame, trap_type: TrapType) {
7172
panic!("kernel error: {:#x}", addr);
7273
}
7374
// judge whether it is trigger by a user_task handler.
74-
if let Some(task) = current_task().as_any().downcast::<UserTask>().ok() {
75+
if let Some(task) = current_task().downcast_arc::<UserTask>().ok() {
7576
let cx_ref = task.force_cx_ref();
7677
if task.pcb.is_locked() {
7778
// task.pcb.force_unlock();
@@ -81,7 +82,7 @@ fn kernel_interrupt(cx_ref: &mut TrapFrame, trap_type: TrapType) {
8182
}
8283
user_cow_int(task, cx_ref, addr);
8384
} else {
84-
panic!("page fault: {:?}", trap_type);
85+
panic!("page fault: {:#x?}", trap_type);
8586
}
8687
}
8788
TrapType::IllegalInstruction(addr) => {
@@ -129,8 +130,8 @@ fn kernel_interrupt(cx_ref: &mut TrapFrame, trap_type: TrapType) {
129130
};
130131
}
131132

132-
#[arch_entry]
133133
/// The kernel entry
134+
#[arch::arch_entry]
134135
fn main(hart_id: usize) {
135136
disable_irq();
136137
if hart_id == 0 {
@@ -156,6 +157,9 @@ fn main(hart_id: usize) {
156157

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

160+
// Boot all application core.
161+
MultiCore::boot_all();
162+
159163
devices::prepare_drivers();
160164

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

211215
// init kernel threads and async executor
212216
tasks::init();
217+
loop {
218+
arch::wfi()
219+
}
220+
tasks::run_tasks();
213221

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

221229
// enable_irq();
222-
223-
loop {
224-
info!("aux core");
225-
}
230+
enable_irq();
231+
// loop { arch::wfi() }
232+
tasks::run_tasks();
233+
info!("shutdown ap core");
226234
}
227235
}

kernel/src/syscall/fd.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use vfscore::FileType;
77
use alloc::sync::Arc;
88
use arch::addr::VirtAddr;
99
use bit_field::BitArray;
10-
use executor::{yield_now, AsyncTask};
10+
use executor::yield_now;
1111
use fs::pipe::create_pipe;
1212
use fs::{OpenFlags, PollEvent, PollFd, SeekFrom, Stat, StatFS, StatMode, TimeSpec, UTIME_NOW};
1313
use log::debug;
@@ -479,7 +479,7 @@ impl UserTaskContainer {
479479
);
480480
let cmd = FromPrimitive::from_usize(cmd).ok_or(LinuxError::EINVAL)?;
481481
let file = self.task.get_fd(fd).ok_or(LinuxError::EBADF)?;
482-
debug!("[task {}] fcntl: {:?}", self.task.get_task_id(), cmd);
482+
debug!("[task {}] fcntl: {:?}", self.tid, cmd);
483483
match cmd {
484484
FcntlCmd::DUPFD | FcntlCmd::DUPFDCLOEXEC => self.sys_dup(fd).await,
485485
FcntlCmd::GETFD => Ok(1),

kernel/src/syscall/socket.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::net::{Ipv4Addr, SocketAddrV4};
33

44
use alloc::sync::Arc;
55
use devices::get_net_device;
6-
use executor::{yield_now, AsyncTask};
6+
use executor::yield_now;
77
use log::{debug, warn};
88
use lose_net_stack::connection::NetServer;
99
use lose_net_stack::net_trait::NetInterface;
@@ -340,11 +340,7 @@ impl UserTaskContainer {
340340
socket_addr.family = 2;
341341
socket_addr.addr = *socket_address.ip();
342342
socket_addr.in_port = socket_address.port().to_be();
343-
debug!(
344-
"[task {}] socket address: {:?}",
345-
self.task.get_task_id(),
346-
socket_address
347-
);
343+
debug!("[task {}] socket address: {:?}", self.tid, socket_address);
348344
}
349345
Ok(0)
350346
}

kernel/src/syscall/task.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,10 @@ impl UserTaskContainer {
476476
new_tcb.cx[TrapFrameArgs::TLS] = tls;
477477
}
478478
if flags.contains(CloneFlags::CLONE_PARENT_SETTID) {
479-
*ptid.get_mut() = new_task.get_task_id() as _;
479+
*ptid.get_mut() = new_task.task_id as _;
480480
}
481481
if flags.contains(CloneFlags::CLONE_CHILD_SETTID) && ctid.is_valid() {
482-
*ctid.get_mut() = new_task.get_task_id() as _;
482+
*ctid.get_mut() = new_task.task_id as _;
483483
}
484484
new_tcb.exit_signal = sig as u8;
485485
drop(new_tcb);
@@ -529,14 +529,13 @@ impl UserTaskContainer {
529529

530530
debug!(
531531
"wait ok: {} waiter: {}",
532-
child_task.get_task_id(),
533-
self.task.get_task_id()
532+
child_task.task_id, self.task.task_id
534533
);
535534
self.task
536535
.pcb
537536
.lock()
538537
.children
539-
.retain(|x| x.task_id != child_task.get_task_id());
538+
.retain(|x| x.task_id != child_task.task_id);
540539
debug!("wait pid: {}", child_task.exit_code().unwrap());
541540

542541
if status.is_valid() {
@@ -565,7 +564,7 @@ impl UserTaskContainer {
565564
*status.get_mut() = (t1 as i32) << 8;
566565
}
567566
// TIPS: This is a small change.
568-
Ok(child_task.get_task_id())
567+
Ok(child_task.task_id)
569568
// Ok(0)
570569
}
571570
None => Ok(0),
@@ -616,7 +615,7 @@ impl UserTaskContainer {
616615
.parent
617616
.read()
618617
.upgrade()
619-
.map(|x| x.get_task_id())
618+
.map(|x| x.task_id)
620619
.ok_or(LinuxError::EPERM)
621620
}
622621

@@ -786,11 +785,11 @@ impl UserTaskContainer {
786785
false => TASK_QUEUE
787786
.lock()
788787
.iter()
789-
.find(|x| x.get_task_id() == pid)
790-
.map(|x| x.clone())
788+
.find(|x| x.task.get_task_id() == pid)
791789
.ok_or(LinuxError::ESRCH)?
792-
.as_any()
793-
.downcast::<UserTask>()
790+
.task
791+
.clone()
792+
.downcast_arc::<UserTask>()
794793
.ok(),
795794
};
796795

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

813812
if let Some(parent) = parent.upgrade() {
814-
parent
815-
.pcb
816-
.lock()
817-
.children
818-
.retain(|x| x.get_task_id() != self.task.get_task_id());
813+
parent.pcb.lock().children.retain(|x| x.task_id != self.tid);
819814
*self.task.parent.write() = Weak::<UserTask>::new();
820815
}
821816
Ok(0)

kernel/src/tasks/initproc.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use alloc::{
55
vec::Vec,
66
};
77
use arch::debug::DebugConsole;
8-
use executor::{current_task, yield_now, FUTURE_LIST, TASK_QUEUE};
8+
use executor::{current_task, task::TaskType, yield_now, TASK_QUEUE};
99
use frame_allocator::get_free_pages;
1010
use fs::{
1111
dentry::{dentry_open, dentry_root, DentryNode},
@@ -58,17 +58,7 @@ fn clear() {
5858
async fn kill_all_tasks() {
5959
TASK_QUEUE
6060
.lock()
61-
.iter()
62-
.for_each(|x| match x.clone().as_any().downcast::<UserTask>() {
63-
Ok(user_task) => {
64-
user_task.exit(0);
65-
FUTURE_LIST.lock().remove(&user_task.task_id);
66-
}
67-
Err(_) => {}
68-
});
69-
TASK_QUEUE
70-
.lock()
71-
.retain(|x| x.clone().as_any().downcast::<UserTask>().is_err());
61+
.retain(|x| x.task_type != TaskType::MonolithicTask);
7262
}
7363

7464
async fn run_libc_test() -> bool {
@@ -143,7 +133,7 @@ async fn file_command(cmd: &str) {
143133
if TASK_QUEUE
144134
.lock()
145135
.iter()
146-
.find(|x| x.get_task_id() == task_id)
136+
.find(|x| x.task.get_task_id() == task_id)
147137
.is_none()
148138
{
149139
break;

0 commit comments

Comments
 (0)