فهرست منبع

Merge pull request #162 from yizishun/main

fix: hsm should return invalid when hartid is invalid
Luo Jia / Zhouqi Jiang 4 روز پیش
والد
کامیت
1936f6bdf5
3فایلهای تغییر یافته به همراه25 افزوده شده و 1 حذف شده
  1. 9 0
      prototyper/prototyper/src/main.rs
  2. 4 1
      prototyper/prototyper/src/platform/mod.rs
  3. 12 0
      prototyper/prototyper/src/sbi/hsm.rs

+ 9 - 0
prototyper/prototyper/src/main.rs

@@ -20,6 +20,7 @@ mod sbi;
 
 use core::arch::{asm, naked_asm};
 
+use crate::platform::CPU_ENABLED;
 use crate::platform::PLATFORM;
 use crate::riscv::csr::menvcfg;
 use crate::riscv::current_hartid;
@@ -53,11 +54,19 @@ extern "C" fn rust_main(_hart_id: usize, opaque: usize, nonstandard_a2: usize) {
         MPP::Supervisor => {
             if !misa::read().unwrap().has_extension('S') {
                 fail::stop();
+            } else {
+                unsafe {
+                    CPU_ENABLED[current_hartid()] = true;
+                }
             }
         }
         MPP::User => {
             if !misa::read().unwrap().has_extension('U') {
                 fail::stop();
+            } else {
+                unsafe {
+                    CPU_ENABLED[current_hartid()] = true;
+                }
             }
         }
         _ => {}

+ 4 - 1
prototyper/prototyper/src/platform/mod.rs

@@ -36,6 +36,7 @@ use crate::sbi::suspend::SbiSuspend;
 mod clint;
 mod console;
 mod reset;
+pub static mut CPU_ENABLED: [bool; NUM_HART_MAX] = [false; NUM_HART_MAX];
 
 type BaseAddress = usize;
 
@@ -264,7 +265,9 @@ impl Platform {
             let cpu = cpu_iter.deserialize::<Cpu>();
             let hart_id = cpu.reg.iter().next().unwrap().0.start;
             if let Some(x) = cpu_list.get_mut(hart_id) {
-                *x = true;
+                unsafe {
+                    *x = CPU_ENABLED[hart_id];
+                }
             } else {
                 error!(
                     "The maximum supported hart id is {}, but the hart id {} was obtained. Please check the config!",

+ 12 - 0
prototyper/prototyper/src/sbi/hsm.rs

@@ -205,6 +205,12 @@ pub(crate) struct SbiHsm;
 impl rustsbi::Hsm for SbiHsm {
     /// Starts execution on a stopped hart.
     fn hart_start(&self, hartid: usize, start_addr: usize, opaque: usize) -> SbiRet {
+        let hart_enable = unsafe { PLATFORM.info.cpu_enabled.unwrap() };
+        let enabled = hart_enable.get(hartid).copied().unwrap_or(false);
+        if !enabled {
+            return SbiRet::invalid_param();
+        }
+
         match remote_hsm(hartid) {
             Some(remote) => {
                 if remote.start(NextStage {
@@ -238,6 +244,12 @@ impl rustsbi::Hsm for SbiHsm {
     /// Gets the current state of a hart.
     #[inline]
     fn hart_get_status(&self, hartid: usize) -> SbiRet {
+        let hart_enable = unsafe { PLATFORM.info.cpu_enabled.unwrap() };
+        let enabled = hart_enable.get(hartid).copied().unwrap_or(false);
+        if !enabled {
+            return SbiRet::invalid_param();
+        }
+
         match remote_hsm(hartid) {
             Some(remote) => SbiRet::success(remote.get_status()),
             None => SbiRet::invalid_param(),