Browse Source

Merge pull request #103 from woshiluo/toml-config

feat(prototyper): read const in cfg.rs from toml file
guttatus 1 month ago
parent
commit
c9ccda0c67

+ 1 - 0
prototyper/prototyper/Cargo.toml

@@ -24,6 +24,7 @@ serde-device-tree = { git = "https://github.com/rustsbi/serde-device-tree", defa
 uart_xilinx = { git = "https://github.com/duskmoon314/uart-rs/" }
 xuantie-riscv = { git= "https://github.com/rustsbi/xuantie" }
 bouffalo-hal = { git = "https://github.com/rustsbi/bouffalo-hal", rev = "968b949", features = ["bl808"] }
+static-toml = "1"
 
 [[bin]]
 name = "rustsbi-prototyper"

+ 7 - 0
prototyper/prototyper/config/default.toml

@@ -0,0 +1,7 @@
+num_hart_max = 8
+stack_size_per_hart = 16384 # 16 * 1024
+heap_size = 32768 # 32 * 1024
+page_size = 4096
+log_level = "INFO"
+jump_address = 0x80200000
+tlb_flush_limit = 16384 # page_size * 4

+ 7 - 0
prototyper/prototyper/config/sipeed_m1s_dock.toml

@@ -0,0 +1,7 @@
+num_hart_max = 8
+stack_size_per_hart = 16384 # 16 * 1024
+heap_size = 32768 # 32 * 1024
+page_size = 4096
+log_level = "INFO"
+jump_address = 0x50000000
+tlb_flush_limit = 16384 # page_size * 4

+ 17 - 8
prototyper/prototyper/src/cfg.rs

@@ -1,16 +1,25 @@
+use static_toml::static_toml;
+
 /// The address where the SBI link start.
 pub const SBI_LINK_START_ADDRESS: usize = 0x80000000;
+
+static_toml! {
+    const CONFIG = include_toml!("../../target/config.toml");
+}
+
 /// Maximum number of supported harts.
-pub const NUM_HART_MAX: usize = 8;
+pub const NUM_HART_MAX: usize = CONFIG.num_hart_max as usize;
 /// Stack size per hart (hardware thread) in bytes.
-pub const LEN_STACK_PER_HART: usize = 16 * 1024;
+pub const STACK_SIZE_PER_HART: usize = CONFIG.stack_size_per_hart as usize;
 /// Heap Size of SBI firmware.
-pub const HEAP_SIZE: usize = 32 * 1024;
+pub const HEAP_SIZE: usize = CONFIG.heap_size as usize;
 /// Platform page size.
-pub const PAGE_SIZE: usize = 4096;
+pub const PAGE_SIZE: usize = CONFIG.page_size as usize;
+/// Log Level.
+pub const LOG_LEVEL: &'static str = CONFIG.log_level;
+/// Address for jump mode.
+#[cfg(feature = "jump")]
+pub const JUMP_ADDRESS: usize = CONFIG.jump_address as usize;
 /// TLB_FLUSH_LIMIT defines the TLB refresh range limit.
 /// If the TLB refresh range is greater than TLB_FLUSH_LIMIT, the entire TLB is refreshed.
-pub const TLB_FLUSH_LIMIT: usize = 4 * PAGE_SIZE;
-
-#[cfg(feature = "jump")]
-pub const JUMP_ADDRESS: usize = 0x50000000;
+pub const TLB_FLUSH_LIMIT: usize = CONFIG.tlb_flush_limit as usize;

+ 2 - 4
prototyper/prototyper/src/sbi/logger.rs

@@ -7,10 +7,8 @@ pub struct Logger;
 impl Logger {
     /// Initialize the logger with log level from RUST_LOG env var or default to Info.
     pub fn init() -> Result<(), log::SetLoggerError> {
-        // Set max log level from RUST_LOG env var if present, otherwise use Info
-        let max_level = option_env!("RUST_LOG")
-            .and_then(|s| LevelFilter::from_str(s).ok())
-            .unwrap_or(LevelFilter::Info);
+        // Set max log level from LOG_LEVEL from config file, otherwise use Info
+        let max_level = LevelFilter::from_str(crate::cfg::LOG_LEVEL).unwrap_or(LevelFilter::Info);
 
         log::set_max_level(max_level);
         log::set_logger(&Logger)

+ 4 - 4
prototyper/prototyper/src/sbi/trap_stack.rs

@@ -1,4 +1,4 @@
-use crate::cfg::{LEN_STACK_PER_HART, NUM_HART_MAX};
+use crate::cfg::{NUM_HART_MAX, STACK_SIZE_PER_HART};
 use crate::riscv::current_hartid;
 use crate::sbi::hart_context::HartContext;
 use crate::sbi::trap::fast_handler;
@@ -26,7 +26,7 @@ pub(crate) unsafe extern "C" fn locate() {
             call t1, {move_stack}       // Call stack reuse function
             ret                         // Return
         ",
-            per_hart_stack_size = const LEN_STACK_PER_HART,
+            per_hart_stack_size = const STACK_SIZE_PER_HART,
             stack               =   sym ROOT_STACK,
             move_stack          =   sym fast_trap::reuse_stack_for_trap,
         )
@@ -51,10 +51,10 @@ pub(crate) fn prepare_for_trap() {
 ///
 /// Each hart has a single stack that contains both its context and working space.
 #[repr(C, align(128))]
-pub(crate) struct Stack([u8; LEN_STACK_PER_HART]);
+pub(crate) struct Stack([u8; STACK_SIZE_PER_HART]);
 
 impl Stack {
-    const ZERO: Self = Self([0; LEN_STACK_PER_HART]);
+    const ZERO: Self = Self([0; STACK_SIZE_PER_HART]);
 
     /// Gets mutable reference to hart context at bottom of stack.
     #[inline]

+ 1 - 0
rustfmt.toml

@@ -1,3 +1,4 @@
 # Use rustfmt to format code
 
+edition = "2024"
 # Empty file.

+ 2 - 0
xtask/Cargo.toml

@@ -10,3 +10,5 @@ publish = false
 clap = { version = "4.5.4", features = ["derive", "env", "suggestions"] }
 log = "0.4.21"
 clap-verbosity-flag = "3.0.2"
+serde = "1.0"
+toml = "0.8.20"

+ 28 - 5
xtask/src/prototyper.rs

@@ -1,5 +1,6 @@
 use std::{
     env, fs,
+    path::PathBuf,
     process::{Command, ExitStatus},
 };
 
@@ -22,8 +23,8 @@ pub struct PrototyperArg {
     #[clap(long)]
     pub jump: bool,
 
-    #[clap(long, default_value = "INFO")]
-    pub log_level: String,
+    #[clap(long, short = 'c')]
+    pub config_file: Option<PathBuf>,
 }
 
 #[must_use]
@@ -33,13 +34,36 @@ pub fn run(arg: &PrototyperArg) -> Option<ExitStatus> {
     let fdt = arg.fdt.clone();
     let payload = arg.payload.clone();
     let jump = arg.jump;
+
     let current_dir = env::current_dir();
-    let target_dir = current_dir
+    let raw_target_dir = current_dir
         .as_ref()
         .unwrap()
-        .join("target")
+        .join("target");
+    let target_dir = raw_target_dir
         .join(arch)
         .join("release");
+    let target_config_toml = raw_target_dir.join("config.toml");
+
+    let default_config_file = current_dir
+        .as_ref()
+        .unwrap()
+        .join("prototyper")
+        .join("prototyper")
+        .join("config")
+        .join("default.toml");
+    let config_file = arg.config_file.clone().unwrap_or(default_config_file);
+
+    if fs::exists(&target_config_toml).ok()? {
+        info!("Delete old config");
+        fs::remove_file(&target_config_toml).ok()?;
+    }
+
+    info!("Copy config");
+    fs::copy(
+        &config_file,
+        target_config_toml
+    ).ok()?;
 
     info!("Building Protoyper");
     cargo::Cargo::new("build")
@@ -59,7 +83,6 @@ pub fn run(arg: &PrototyperArg) -> Option<ExitStatus> {
         .optional(jump, |cargo| {
             cargo.features(["jump".to_string()])
         })
-        .env("RUST_LOG", &arg.log_level)
         .release()
         .status()
         .ok()?;