Browse Source

fix(riscv/process): 把riscv的调度时钟节拍率与HZ同步,并且修复切换到用户态的时候忘了在内核态关中断的bug (#780)

LoGin 10 months ago
parent
commit
942cf26b48

+ 1 - 0
kernel/src/arch/riscv64/asm/csr.rs

@@ -1,3 +1,4 @@
+#![allow(dead_code)]
 pub const CSR_SSTATUS: usize = 0x100;
 pub const CSR_SSCRATCH: usize = 0x140;
 pub const CSR_SEPC: usize = 0x141;

+ 2 - 1
kernel/src/arch/riscv64/cpu.rs

@@ -62,6 +62,7 @@ pub(super) struct LocalContext {
     pub user_sp: usize,
 }
 
+#[allow(dead_code)]
 impl LocalContext {
     pub fn new(cpu: ProcessorId) -> Self {
         Self {
@@ -131,7 +132,7 @@ pub(super) fn init_local_context() {
 }
 
 impl SmpCpuManager {
-    pub fn arch_init(boot_cpu: ProcessorId) {
+    pub fn arch_init(_boot_cpu: ProcessorId) {
         // todo: 读取所有可用的CPU
     }
 }

+ 2 - 13
kernel/src/arch/riscv64/process/mod.rs

@@ -1,8 +1,4 @@
-use alloc::{
-    string::String,
-    sync::{Arc, Weak},
-    vec::Vec,
-};
+use alloc::sync::{Arc, Weak};
 use core::{
     arch::asm,
     intrinsics::unlikely,
@@ -28,7 +24,6 @@ use crate::{
         PROCESS_SWITCH_RESULT,
     },
     smp::cpu::ProcessorId,
-    syscall::Syscall,
 };
 
 use super::{
@@ -61,12 +56,12 @@ pub unsafe fn arch_switch_to_user(trap_frame: TrapFrame) -> ! {
     let trap_frame_vaddr = VirtAddr::new(
         current_pcb.kernel_stack().stack_max_address().data() - core::mem::size_of::<TrapFrame>(),
     );
+
     let new_pc = VirtAddr::new(ret_from_exception as usize);
 
     let mut arch_guard = current_pcb.arch_info_irqsave();
     arch_guard.ksp = trap_frame_vaddr.data();
 
-    arch_guard.ra = new_pc.data();
     drop(arch_guard);
 
     drop(current_pcb);
@@ -315,12 +310,6 @@ unsafe extern "C" fn switch_to_inner(prev: *mut ArchPCBInfo, next: *mut ArchPCBI
 
 /// 在切换上下文完成后的钩子函数(必须在这里加一个跳转函数,否则会出现relocation truncated to fit: R_RISCV_JAL错误)
 unsafe extern "C" fn before_switch_finish_hook() {
-    let pcb = ProcessManager::current_pcb();
-    // kdebug!(
-    //     "before_switch_finish_hook, pid: {:?}, name: {:?}",
-    //     pcb.pid(),
-    //     pcb.basic().name()
-    // );
     switch_finish_hook();
 }
 

+ 2 - 2
kernel/src/arch/riscv64/process/syscall.rs

@@ -102,7 +102,6 @@ impl Syscall {
         regs.epc = load_result.entry_point().data();
         regs.status.update_spp(SPP::User);
         regs.status.update_fs(FS::Clean);
-        regs.status.update_sie(true);
         regs.status.update_sum(true);
 
         drop(param);
@@ -111,7 +110,8 @@ impl Syscall {
     }
 
     /// ## 用于控制和查询与体系结构相关的进程特定选项
-    pub fn arch_prctl(option: usize, arg2: usize) -> Result<usize, SystemError> {
+    #[allow(dead_code)]
+    pub fn arch_prctl(_option: usize, _arg2: usize) -> Result<usize, SystemError> {
         unimplemented!("Syscall::arch_prctl")
     }
 }

+ 1 - 1
kernel/src/arch/riscv64/smp/mod.rs

@@ -17,7 +17,7 @@ impl SMPArch for RiscV64SMPArch {
         Ok(())
     }
 
-    fn start_cpu(cpu_id: ProcessorId, hp_state: &CpuHpCpuState) -> Result<(), SystemError> {
+    fn start_cpu(_cpu_id: ProcessorId, _hp_state: &CpuHpCpuState) -> Result<(), SystemError> {
         kwarn!("RiscV64SMPArch::start_cpu() is not implemented");
         Ok(())
     }

+ 4 - 3
kernel/src/driver/acpi/pmtmr.rs

@@ -1,6 +1,4 @@
-use crate::driver::clocksource::acpi_pm::{acpi_pm_read_verified, PMTMR_IO_PORT};
-use core::sync::atomic::Ordering;
-
+#[allow(dead_code)]
 pub const ACPI_PM_OVERRUN: u64 = 1 << 24;
 
 /// Number of PMTMR ticks expected during calibration run
@@ -12,6 +10,8 @@ pub const ACPI_PM_MASK: u64 = 0xffffff;
 #[inline(always)]
 #[cfg(target_arch = "x86_64")]
 pub fn acpi_pm_read_early() -> u32 {
+    use crate::driver::clocksource::acpi_pm::{acpi_pm_read_verified, PMTMR_IO_PORT};
+    use core::sync::atomic::Ordering;
     let port = unsafe { PMTMR_IO_PORT.load(Ordering::SeqCst) };
 
     // 如果端口为零直接返回
@@ -25,6 +25,7 @@ pub fn acpi_pm_read_early() -> u32 {
 
 #[inline(always)]
 #[cfg(not(target_arch = "x86_64"))]
+#[allow(dead_code)]
 pub fn acpi_pm_read_early() -> u32 {
     return 0;
 }

+ 4 - 0
kernel/src/driver/clocksource/acpi_pm.rs

@@ -34,6 +34,7 @@ fn read_pmtmr() -> u32 {
 ///
 /// ## 返回值
 /// - u32: 读取到的acpi_pmtmr值
+#[allow(dead_code)]
 pub fn acpi_pm_read_verified() -> u32 {
     let mut v2: u32;
 
@@ -167,6 +168,7 @@ const PMTMR_EXPECTED_RATE: u64 =
 /// ## 返回值
 /// - i32:如果为0则表示在预期范围内,否则不在
 #[cfg(not(target_arch = "x86_64"))]
+#[allow(dead_code)]
 fn verify_pmtmr_rate() -> bool {
     let mut count: u32 = 0;
 
@@ -216,6 +218,8 @@ fn find_acpi_pm_clock() -> Result<(), SystemError> {
 
 /// # 初始化ACPI PM Timer作为系统时钟源
 // #[unified_init(INITCALL_FS)]
+#[inline(never)]
+#[allow(dead_code)]
 pub fn init_acpi_pm_clocksource() -> Result<(), SystemError> {
     let acpi_pm = Acpipm::new();
     unsafe {

+ 3 - 3
kernel/src/driver/clocksource/timer_riscv.rs

@@ -19,7 +19,7 @@ use crate::{
     mm::percpu::PerCpu,
     process::ProcessManager,
     smp::core::smp_get_processor_id,
-    time::TimeArch,
+    time::{clocksource::HZ, TimeArch},
 };
 
 pub struct RiscVSbiTimer;
@@ -48,6 +48,7 @@ impl RiscVSbiTimer {
         unsafe { riscv::register::sie::set_stimer() };
     }
 
+    #[allow(dead_code)]
     fn disable() {
         unsafe { riscv::register::sie::clear_stimer() };
     }
@@ -59,8 +60,7 @@ pub fn riscv_sbi_timer_init_local() {
     assert_eq!(CurrentIrqArch::is_irq_enabled(), false);
 
     if unsafe { INTERVAL_CNT } == 0 {
-        // todo: 将来正式实现时,需要除以HZ
-        let new = riscv_time_base_freq() / 3;
+        let new = riscv_time_base_freq() / HZ as usize;
         if new == 0 {
             panic!("riscv_sbi_timer_init: failed to get timebase-frequency");
         }

+ 1 - 0
kernel/src/driver/disk/mod.rs

@@ -1 +1,2 @@
+#[cfg(target_arch = "x86_64")]
 pub mod ahci;

+ 4 - 3
kernel/src/filesystem/vfs/core.rs

@@ -1,10 +1,10 @@
 use core::{hint::spin_loop, sync::atomic::Ordering};
 
-use alloc::{string::ToString, sync::Arc};
+use alloc::sync::Arc;
 use system_error::SystemError;
 
 use crate::{
-    driver::{base::block::disk_info::Partition, disk::ahci},
+    driver::base::block::disk_info::Partition,
     filesystem::{
         devfs::devfs_init,
         fat::fs::FATFileSystem,
@@ -116,7 +116,8 @@ fn migrate_virtual_filesystem(new_fs: Arc<dyn FileSystem>) -> Result<(), SystemE
 fn root_partition() -> Arc<Partition> {
     #[cfg(target_arch = "x86_64")]
     {
-        return ahci::get_disks_by_name("ahci_disk_0".to_string())
+        use alloc::string::ToString;
+        return crate::driver::disk::ahci::get_disks_by_name("ahci_disk_0".to_string())
             .unwrap()
             .0
             .lock()

+ 1 - 1
kernel/src/init/initial_kthread.rs

@@ -74,7 +74,6 @@ fn switch_to_user() -> ! {
 
     let mut trap_frame = TrapFrame::new();
     // 逐个尝试运行init进程
-
     if try_to_run_init_process("/bin/dragonreach", &mut trap_frame).is_err()
         && try_to_run_init_process("/bin/init", &mut trap_frame).is_err()
         && try_to_run_init_process("/bin/sh", &mut trap_frame).is_err()
@@ -83,6 +82,7 @@ fn switch_to_user() -> ! {
     }
 
     // 需要确保执行到这里之后,上面所有的资源都已经释放(比如arc之类的)
+    compiler_fence(Ordering::SeqCst);
 
     unsafe { arch_switch_to_user(trap_frame) };
 }