Skip to content

Commit

Permalink
feat: use new rtc and syscall-table module
Browse files Browse the repository at this point in the history
1. update rtc driver impl
2. update syscall-table impl
  • Loading branch information
Godones committed Sep 3, 2023
1 parent 7c8d24d commit ccd232a
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 899 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ members = [
"boot",
"modules/gmanager",
"modules/pci-rs",
"modules/syscall-table",
"modules/systable-macro-derive",
"modules/syscall",
"modules/basemachine",
"modules/kernel-sync",
Expand Down
6 changes: 4 additions & 2 deletions boot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use core::hint::spin_loop;
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

use cfg_if::cfg_if;
use kernel::init_init_array;

use basemachine::machine_info_from_dtb;
use kernel::arch::hart_id;
Expand All @@ -43,7 +44,7 @@ use kernel::memory::{init_memory_system, kernel_info};
use kernel::print::init_print;
use kernel::sbi::hart_start;
use kernel::task::init_per_cpu;
use kernel::{config, init_machine_info, println, syscall, task, thread_local_init, timer, trap};
use kernel::{config, init_machine_info, println, task, thread_local_init, timer, trap};

mod entry;

Expand Down Expand Up @@ -103,7 +104,7 @@ pub fn main(_: usize, _: usize) -> ! {
trap::init_trap_subsystem();
init_per_cpu();
init_vfs();
syscall::register_all_syscall();
// syscall::register_all_syscall();
task::init_process();
CPUS.fetch_add(1, Ordering::Release);
STARTED.store(true, Ordering::Relaxed);
Expand All @@ -119,6 +120,7 @@ pub fn main(_: usize, _: usize) -> ! {
trap::init_trap_subsystem();
CPUS.fetch_add(1, Ordering::Release);
}
init_init_array!();
timer::set_next_trigger();
println!("begin run task...");
task::schedule::first_into_user();
Expand Down
6 changes: 3 additions & 3 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ visionfive2-sd = { git = "https://github.com/os-module/visionfive2-sd.git", opti
trace_lib = { git = "https://github.com/os-module/rtrace" }
preprint = "0.1.0"
rslab = { version = "0.2.1" }
syscall-table = { path = "../modules/syscall-table" }
plic = { git = "https://github.com/os-module/plic" }
pager = {git = "https://github.com/os-module/pager", default-features = false, optional = true}
syscall-table = { git = "https://github.com/os-module/syscall-table.git" }
plic = { git = "https://github.com/os-module/plic" }
pager = { git = "https://github.com/os-module/pager", default-features = false, optional = true }
gmanager = { path = "../modules/gmanager" }
kernel-sync = { path = "../modules/kernel-sync", package = "lock" }
syscall_define = { path = "../modules/syscall", package = "syscall" }
Expand Down
245 changes: 119 additions & 126 deletions kernel/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use std::collections::BTreeSet;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::BufRead;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::string::{String, ToString};
use std::vec::Vec;
use std::string::String;
use std::{format, fs};

fn main() {
Expand All @@ -26,8 +21,6 @@ fn main() {
write!(file, "symbol_index:\n").unwrap();
write!(file, "symbol_name:\n").unwrap();
}
scan_and_generate("src/syscall.rs".to_string());

rewrite_config();
}

Expand All @@ -51,122 +44,122 @@ pub fn rewrite_config() {
fs::write(config_file, new_config).unwrap();
}

pub fn scan_and_generate(path: String) {
// read all files in the directory rescursively
let mut target_file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
let mut context = Vec::new();
let import = b"\
//! syscall table registe\n\
use spin::Once;\n\
use syscall_table::{register_syscall, Table};\n\
static SYSCALL_TABLE: Once<Table> = Once::new();\n\
/// Register all system calls\n\
pub fn register_all_syscall(){\n\
\tlet mut table = Table::new();\n\
\tregister_syscall!(table,\n\
";
context.extend_from_slice(import);

let mut import = BTreeSet::new();
scan(&mut import, &mut context, PathBuf::from("src"));
let end = b"\n\
\t);\n\
\tSYSCALL_TABLE.call_once(||table);\n\
}\n\
/// Call the corresponding system call according to the syscall number\n\
pub fn do_syscall(id:usize,args:&[usize])->Option<isize>{\n\
\tlet res = SYSCALL_TABLE.get().unwrap().do_call(id,&args);\n\
\tres\n\
}\n\
";
context.extend_from_slice(end);
import.iter().for_each(|m| {
let import = format!("use {};\n", m);
context.extend_from_slice(import.as_bytes());
});
target_file.write_all(&context).unwrap();
}

fn scan(import: &mut BTreeSet<String>, context: &mut Vec<u8>, dir: PathBuf) {
let entries = fs::read_dir(dir).unwrap();

let paths = entries
.map(|entry| entry.unwrap().path())
.collect::<Vec<PathBuf>>();
// sort the paths
let mut paths = paths.iter().collect::<Vec<&PathBuf>>();
paths.sort();
// pub fn scan_and_generate(path: String) {
// // read all files in the directory rescursively
// let mut target_file = OpenOptions::new()
// .read(true)
// .write(true)
// .create(true)
// .truncate(true)
// .open(path)
// .unwrap();
// let mut context = Vec::new();
// let import = b"\
// //! syscall table registe\n\
// use spin::Once;\n\
// use syscall_table::{register_syscall, Table};\n\
// static SYSCALL_TABLE: Once<Table> = Once::new();\n\
// /// Register all system calls\n\
// pub fn register_all_syscall(){\n\
// \tlet mut table = Table::new();\n\
// \tregister_syscall!(table,\n\
// ";
// context.extend_from_slice(import);
//
// let mut import = BTreeSet::new();
// scan(&mut import, &mut context, PathBuf::from("src"));
// let end = b"\n\
// \t);\n\
// \tSYSCALL_TABLE.call_once(||table);\n\
// }\n\
// /// Call the corresponding system call according to the syscall number\n\
// pub fn do_syscall(id:usize,args:&[usize])->Option<isize>{\n\
// \tlet res = SYSCALL_TABLE.get().unwrap().do_call(id,&args);\n\
// \tres\n\
// }\n\
// ";
// context.extend_from_slice(end);
// import.iter().for_each(|m| {
// let import = format!("use {};\n", m);
// context.extend_from_slice(import.as_bytes());
// });
// target_file.write_all(&context).unwrap();
// }

for path in paths {
// let entry = entry.unwrap();
// let path = entry.path();
if path.is_dir() {
scan(import, context, path.clone());
} else {
if path.extension().unwrap() == "rs" {
let file = File::open(path.clone()).unwrap();
let mut buf_reader = std::io::BufReader::new(file);
let mut line = String::new();
while buf_reader.read_line(&mut line).unwrap() > 0 {
if line.contains("#[syscall_func") && !line.starts_with("//") {
// #[syscall_func(1)]
// find the id from the line
let id = line.split("(").nth(1).unwrap().split(")").next().unwrap();
let id = id.parse::<usize>().unwrap();
// find the function name
let mut func_name = String::new();
if buf_reader.read_line(&mut func_name).unwrap() > 0 {
if func_name.contains("pub fn") {
func_name = func_name
.split("pub fn")
.nth(1)
.unwrap()
.split("(")
.next()
.unwrap()
.to_string();
} else {
panic!("error: the function should be public");
}
} else {
panic!("error: the function name is not found");
}
// find the mod according the path
let mut mod_name = String::from("crate");
let path = path.to_str().unwrap().to_string();
let component = path.split("/").collect::<Vec<&str>>();
let correct = if component.len() == 2 {
component.len()
} else {
component.len() - 1
};
for i in 0..correct {
if component[i] == "src" {
} else {
mod_name.push_str("::");
if component[i].ends_with(".rs") {
mod_name.push_str(component[i].strip_suffix(".rs").unwrap());
} else {
mod_name.push_str(component[i]);
}
}
}
mod_name.push_str("::");
mod_name.push_str(&func_name.trim());
import.insert(mod_name);
// generate the code
let code = format!("\t({},{}),\n", id, func_name);
context.extend_from_slice(code.as_bytes());
}
line.clear();
}
}
}
}
}
// fn scan(import: &mut BTreeSet<String>, context: &mut Vec<u8>, dir: PathBuf) {
// let entries = fs::read_dir(dir).unwrap();
//
// let paths = entries
// .map(|entry| entry.unwrap().path())
// .collect::<Vec<PathBuf>>();
// // sort the paths
// let mut paths = paths.iter().collect::<Vec<&PathBuf>>();
// paths.sort();
//
// for path in paths {
// // let entry = entry.unwrap();
// // let path = entry.path();
// if path.is_dir() {
// scan(import, context, path.clone());
// } else {
// if path.extension().unwrap() == "rs" {
// let file = File::open(path.clone()).unwrap();
// let mut buf_reader = std::io::BufReader::new(file);
// let mut line = String::new();
// while buf_reader.read_line(&mut line).unwrap() > 0 {
// if line.contains("#[syscall_func") && !line.starts_with("//") {
// // #[syscall_func(1)]
// // find the id from the line
// let id = line.split("(").nth(1).unwrap().split(")").next().unwrap();
// let id = id.parse::<usize>().unwrap();
// // find the function name
// let mut func_name = String::new();
// if buf_reader.read_line(&mut func_name).unwrap() > 0 {
// if func_name.contains("pub fn") {
// func_name = func_name
// .split("pub fn")
// .nth(1)
// .unwrap()
// .split("(")
// .next()
// .unwrap()
// .to_string();
// } else {
// panic!("error: the function should be public");
// }
// } else {
// panic!("error: the function name is not found");
// }
// // find the mod according the path
// let mut mod_name = String::from("crate");
// let path = path.to_str().unwrap().to_string();
// let component = path.split("/").collect::<Vec<&str>>();
// let correct = if component.len() == 2 {
// component.len()
// } else {
// component.len() - 1
// };
// for i in 0..correct {
// if component[i] == "src" {
// } else {
// mod_name.push_str("::");
// if component[i].ends_with(".rs") {
// mod_name.push_str(component[i].strip_suffix(".rs").unwrap());
// } else {
// mod_name.push_str(component[i]);
// }
// }
// }
// mod_name.push_str("::");
// mod_name.push_str(&func_name.trim());
// import.insert(mod_name);
// // generate the code
// let code = format!("\t({},{}),\n", id, func_name);
// context.extend_from_slice(code.as_bytes());
// }
// line.clear();
// }
// }
// }
// }
// }
16 changes: 9 additions & 7 deletions kernel/src/device/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use alloc::boxed::Box;
use alloc::sync::Arc;
use core::sync::atomic::Ordering;

use virtio_drivers::transport::mmio::MmioTransport;

pub use block::{BlockDevice, BLOCK_DEVICE};
Expand All @@ -10,14 +9,13 @@ pub use input::{sys_event_get, InputDevice, KEYBOARD_INPUT_DEVICE, MOUSE_INPUT_D

use crate::board::{get_rtc_info, probe_devices_from_dtb};
use crate::driver::hal::HalImpl;
use crate::driver::rtc::Rtc;
use crate::driver::uart::Uart;
use crate::driver::GenericBlockDevice;
use crate::interrupt::register_device_to_plic;
use crate::print::console::UART_FLAG;

// pub use pci::{pci_probe,pci_read,pci_write};
pub use self::rtc::{get_rtc_time, RtcDevice, RtcTime, RTC_DEVICE};
pub use self::rtc::{get_rtc_time, RtcDevice, RTC_DEVICE};
pub use self::uart::{UartDevice, UART_DEVICE};

mod block;
Expand Down Expand Up @@ -71,11 +69,15 @@ fn init_rtc() {
}
let (base_addr, irq) = res.unwrap();
println!("Init rtc, base_addr:{:#x},irq:{}", base_addr, irq);
let rtc = Rtc::new(base_addr, irq as u32);
let current_time = rtc.read_time();
let rtc = Arc::new(rtc);
let rtc: Arc<dyn RtcDevice>;
#[cfg(feature = "qemu")]
{
use crate::driver::rtc::GoldFishRtc;
rtc = Arc::new(GoldFishRtc::new(base_addr))
}
let current_time = rtc.read_time_fmt();
rtc::init_rtc(rtc.clone());
let _ = register_device_to_plic(irq, rtc);
register_device_to_plic(irq, rtc.clone());
println!("init rtc success, current time: {:?}", current_time);
}

Expand Down
Loading

0 comments on commit ccd232a

Please sign in to comment.