Skip to content

Commit

Permalink
Merge branch 'new-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed Mar 5, 2024
2 parents 94bc759 + 87b1ebd commit 3da7dc9
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 170 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ members = [
"kernel",
"subsystems/unwinder",
"subsystems/knet",
"subsystems/shim",
]


Expand Down
2 changes: 1 addition & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ timer = { path = "../subsystems/timer" }
ksync = { path = "../subsystems/ksync" }
knet = { path = "../subsystems/knet" }
gmanager = { path = "../subsystems/gmanager" }

shim = { path = "../subsystems/shim", features = ["kernel"]}



Expand Down
4 changes: 2 additions & 2 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ fn main(hart_id: usize) {
println!("{:#?}", machine_info);
mem::init_memory_system(machine_info.memory.end, true);
interrupt::init_plic(machine_info.plic.start);
drivers::register_task_func(Box::new(DriverTaskImpl));
devices::init_device(Box::new(DriverTaskImpl));
shim::register_task_func(Box::new(DriverTaskImpl));
devices::init_device();
vfs::init_filesystem().expect("init filesystem failed");
trap::init_trap_subsystem();
arch::allow_access_user_memory();
Expand Down
17 changes: 6 additions & 11 deletions kernel/src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ pub use crate::task::task::FsContext;
use alloc::sync::Arc;
use alloc::vec::Vec;
pub use cpu::*;
use devices::DeviceWithTask;
use drivers::{DriverTask, DriverWithTask};
use shim::{KTask, KTaskShim};
use smpscheduler::FifoTask;
use spin::Lazy;
pub use task::{StatisticalData, Task, TaskState};
Expand Down Expand Up @@ -56,7 +55,7 @@ fn kthread_init() {
}
}

impl DriverTask for Task {
impl KTask for Task {
fn to_wait(&self) {
self.update_state(TaskState::Waiting)
}
Expand All @@ -70,33 +69,29 @@ impl DriverTask for Task {
}
}
pub struct DriverTaskImpl;
impl DriverWithTask for DriverTaskImpl {
fn get_task(&self) -> Arc<dyn DriverTask> {
impl KTaskShim for DriverTaskImpl {
fn get_task(&self) -> Arc<dyn KTask> {
let task = current_task().unwrap();
task.clone()
}

fn put_task(&self, task: Arc<dyn DriverTask>) {
fn put_task(&self, task: Arc<dyn KTask>) {
let task = task.downcast_arc::<Task>().map_err(|_| ()).unwrap();
GLOBAL_TASK_MANAGER.add_task(Arc::new(FifoTask::new(task)));
}

fn suspend(&self) {
do_suspend();
}
}

impl DeviceWithTask for DriverTaskImpl {
fn transfer_ptr_raw(&self, ptr: usize) -> usize {
let task = current_task().unwrap();
task.transfer_raw(ptr)
}

fn transfer_buf_raw(&self, src: usize, size: usize) -> Vec<&mut [u8]> {
let task = current_task().unwrap();
task.transfer_buffer(src as *const u8, size)
}
}

// online test has no sort.src
// pub static SORT_SRC: &[u8] = include_bytes!("../../../sdcard/sort.src");
// pub static SORT_SRC: &[u8] = include_bytes!("../../../tests/testbin-second-stage/sort.src");
4 changes: 0 additions & 4 deletions kernel/src/task/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,6 @@ impl Task {
}
}
} else {
// if it is a thread, we should not remove it from parent's children
// if self.access_inner().children.len() == 0 {
// self.access_inner().address_space.lock().release();
// }
}
self.access_inner().state = TaskState::Terminated;
}
Expand Down
2 changes: 1 addition & 1 deletion subsystems/devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ksync = { path = "../ksync" }
interrupt = { path = "../interrupt" }
drivers = { path = "../drivers" }
device_interface = { path = "../device_interface" }

shim = { path = "../shim", features = ["lib"] }
spin = "0"
fdt = { git = "https://github.com/repnop/fdt" }
log = "0"
Expand Down
60 changes: 1 addition & 59 deletions subsystems/devices/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use interrupt::register_device_to_plic;
use log::info;
use platform::println;
pub use rtc::{RTCDevice, RTC_DEVICE};
use spin::Once;
pub use uart::{UARTDevice, UART_DEVICE};
use virtio_drivers::transport::mmio::{MmioTransport, VirtIOHeader};
use virtio_drivers::transport::{DeviceType, Transport};
Expand All @@ -40,68 +39,11 @@ pub struct DeviceInfo {
pub need_register: bool,
}

static TASK_FUNC: Once<Box<dyn DeviceWithTask>> = Once::new();

pub trait DeviceWithTask: Send + Sync {
fn transfer_ptr_raw(&self, ptr: usize) -> usize;
fn transfer_buf_raw(&self, src: usize, size: usize) -> Vec<&mut [u8]>;
}

impl dyn DeviceWithTask {
fn copy_data_to_task<T: 'static + Copy>(&self, src: *const T, dst: *mut T) {
let size = core::mem::size_of::<T>();
let bufs = self.transfer_buf_raw(dst as usize, size);
let src = unsafe { core::slice::from_raw_parts(src as *const u8, size) };
let mut start = 0;
for buffer in bufs {
let len = if start + buffer.len() > size {
size - start
} else {
buffer.len()
};
unsafe {
core::ptr::copy_nonoverlapping(src.as_ptr().add(start), buffer.as_mut_ptr(), len);
}
start += len;
}
}
fn copy_data_from_task<T: 'static + Copy>(&self, src: *const T, dst: *mut T) {
let size = core::mem::size_of::<T>();
let bufs = self.transfer_buf_raw(src as usize, size);
let dst = unsafe { core::slice::from_raw_parts_mut(dst as *mut u8, size) };
let mut start = 0;
for buffer in bufs {
let len = if start + buffer.len() > size {
size - start
} else {
buffer.len()
};
unsafe {
core::ptr::copy_nonoverlapping(buffer.as_ptr(), dst.as_mut_ptr().add(start), len);
}
start += len;
}
}
fn transfer_ptr_mut<T>(&self, ptr: *mut T) -> &'static mut T {
let ptr = ptr as usize;
let ptr = self.transfer_ptr_raw(ptr);
unsafe { &mut *(ptr as *mut T) }
}
fn transfer_ptr<T>(&self, ptr: *const T) -> &'static T {
let ptr = ptr as usize;
let ptr = self.transfer_ptr_raw(ptr);
unsafe { &*(ptr as *const T) }
}
}

/// Probe all devices from device tree and init them.
/// # Warning
/// Before init device, we should init platform first.
///
/// todo!(The task_func should be replaced)
pub fn init_device(task_func: Box<dyn DeviceWithTask>) {
TASK_FUNC.call_once(|| task_func);

pub fn init_device() {
let dtb_ptr = platform::platform_dtb_ptr();

let dtb = unsafe { Fdt::from_ptr(dtb_ptr as *const u8).unwrap() };
Expand Down
8 changes: 1 addition & 7 deletions subsystems/devices/src/rtc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::TASK_FUNC;
use alloc::format;
use alloc::sync::Arc;
use constants::io::{RtcTime, TeletypeCommand};
Expand Down Expand Up @@ -51,16 +50,11 @@ impl VfsFile for RTCDevice {
todo!()
}
fn ioctl(&self, cmd: u32, arg: usize) -> VfsResult<usize> {
// let task = current_task().unwrap();
// let mut task_inner = task.access_inner();
let cmd = TeletypeCommand::try_from(cmd).map_err(|_| VfsError::Invalid)?;
match cmd {
TeletypeCommand::RTC_RD_TIME => {
let time = self.device.read_time();
TASK_FUNC
.get()
.unwrap()
.copy_data_to_task(&time, arg as *mut RtcTime);
shim::copy_data_to_task(&time, arg as *mut RtcTime);
}
_ => return Err(VfsError::Invalid),
}
Expand Down
35 changes: 6 additions & 29 deletions subsystems/devices/src/uart.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::TASK_FUNC;
use alloc::sync::Arc;
use constants::io::{LocalModes, TeletypeCommand, Termios, WinSize};
use constants::DeviceId;
Expand Down Expand Up @@ -95,53 +94,31 @@ impl VfsFile for UARTDevice {
fn ioctl(&self, cmd: u32, arg: usize) -> VfsResult<usize> {
let mut io = self.io.lock();
let cmd = TeletypeCommand::try_from(cmd).unwrap();

// let task = current_task().unwrap();
// let mut task_inner = task.access_inner();

return match cmd {
TeletypeCommand::TCGETS | TeletypeCommand::TCGETA => {
// task_inner.copy_to_user(&io.termios, arg as *mut Termios);
TASK_FUNC
.get()
.unwrap()
.copy_data_to_task(&io.termios, arg as *mut Termios);
shim::copy_data_to_task(&io.termios, arg as *mut Termios);
Ok(0)
}
TeletypeCommand::TCSETS | TeletypeCommand::TCSETSW | TeletypeCommand::TCSETSF => {
// task_inner.copy_from_user(arg as *const Termios, &mut io.termios);
TASK_FUNC
.get()
.unwrap()
.copy_data_from_task(arg as *const Termios, &mut io.termios);
shim::copy_data_from_task(arg as *const Termios, &mut io.termios);
Ok(0)
}
TeletypeCommand::TIOCGPGRP => {
// let word = task_inner.transfer_raw_ptr_mut(arg as *mut u32);
let word = TASK_FUNC.get().unwrap().transfer_ptr_mut(arg as *mut u32);
let word = shim::transfer_ptr_mut(arg as *mut u32);
*word = io.foreground_pgid;
Ok(0)
}
TeletypeCommand::TIOCSPGRP => {
// let word = task_inner.transfer_raw_ptr(arg as *const u32);
let word = TASK_FUNC.get().unwrap().transfer_ptr(arg as *const u32);
let word = shim::transfer_ptr(arg as *const u32);
io.foreground_pgid = *word;
Ok(0)
}
TeletypeCommand::TIOCGWINSZ => {
// task_inner.copy_to_user(&io.winsize, arg as *mut WinSize);
TASK_FUNC
.get()
.unwrap()
.copy_data_to_task(&io.winsize, arg as *mut WinSize);
shim::copy_data_to_task(&io.winsize, arg as *mut WinSize);
Ok(0)
}
TeletypeCommand::TIOCSWINSZ => {
// task_inner.copy_from_user(arg as *const WinSize, &mut io.winsize);
TASK_FUNC
.get()
.unwrap()
.copy_data_from_task(arg as *const WinSize, &mut io.winsize);
shim::copy_data_from_task(arg as *const WinSize, &mut io.winsize);
Ok(0)
}
_ => {
Expand Down
7 changes: 4 additions & 3 deletions subsystems/drivers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ mem = { path = "../mem" }
log = "0"
timer = { path = "../timer" }
platform = { path = "../platform" }
spin = "0"
shim = { path = "../shim", features = ["lib"] }


spin = "0"
virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers",rev = "de1c3b1" }
rtc = { git = "https://github.com/os-module/rtc.git" }
lru = "0.10.0"
lru = "0"

# uart
uart16550 = { version = "0.0.1"}
Expand All @@ -31,5 +33,4 @@ virtio-net = { git = "https://github.com/os-module/simple-net" }
netcore = { git = "https://github.com/os-module/simple-net" }

visionfive2-sd = { git = "https://github.com/os-module/visionfive2-sd.git" }
downcast-rs = { version = "1.2.0", default-features = false }

16 changes: 5 additions & 11 deletions subsystems/drivers/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use virtio_drivers::device::input::VirtIOInput;
use virtio_drivers::transport::mmio::{MmioTransport, VirtIOHeader};

use crate::hal::HalImpl;
use crate::{DriverTask, DRIVER_TASK};
use ksync::Mutex;
use shim::KTask;

pub struct VirtIOInputDriver {
inner: Mutex<InputDriverInner>,
Expand All @@ -23,7 +23,7 @@ struct InputDriverInner {
max_events: u32,
driver: VirtIOInput<HalImpl, MmioTransport>,
events: VecDeque<u64>,
wait_queue: VecDeque<Arc<dyn DriverTask>>,
wait_queue: VecDeque<Arc<dyn KTask>>,
}

impl VirtIOInputDriver {
Expand Down Expand Up @@ -67,15 +67,11 @@ impl InputDevice for VirtIOInputDriver {
if let Some(event) = inner.events.pop_front() {
return event;
}
// let process = current_task().unwrap();
// process.update_state(TaskState::Waiting);
// inner.wait_queue.push_back(process.clone());
let task = DRIVER_TASK.get().unwrap().get_task();
let task = shim::current_task();
task.to_wait();
inner.wait_queue.push_back(task);
} // drop the lock
// schedule();
DRIVER_TASK.get().unwrap().suspend();
shim::suspend(); // yield current task
}
}

Expand Down Expand Up @@ -103,10 +99,8 @@ impl DeviceBase for VirtIOInputDriver {
}
while !inner.wait_queue.is_empty() && count > 0 {
let task = inner.wait_queue.pop_front().unwrap();
// process.update_state(TaskState::Ready);
// GLOBAL_TASK_MANAGER.add_task(Arc::new(FifoTask::new(process)));
task.to_wakeup();
DRIVER_TASK.get().unwrap().put_task(task);
shim::put_task(task);
count -= 1;
}
info!("read {} events", count);
Expand Down
Loading

0 comments on commit 3da7dc9

Please sign in to comment.