1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#![allow(non_snake_case)]
use crate::symbols::{NCPUS, SCHEDULER_INTERVAL};
use crate::println;
use crate::arch::{hart_id, sp};
pub const CLINT_BASE: usize = 0x200_0000;
pub const CLINT_MTIMECMP_BASE: usize = CLINT_BASE + 0x4000;
pub const fn CLINT_MTIMECMP(hart: usize) -> usize { CLINT_MTIMECMP_BASE + 8 * hart }
pub const CLINT_MTIME_BASE: usize = CLINT_BASE + 0xBFF8;
static mut MSCRATCH0: [[u64; 8]; NCPUS] = [[0; 8]; NCPUS];
pub unsafe fn timer_init() {
use riscv::register::*;
let id = mhartid::read();
let interval = SCHEDULER_INTERVAL as u64;
let mtimecmp = CLINT_MTIMECMP(id) as *mut u64;
let mtime = CLINT_MTIME_BASE as *const u64;
mtimecmp.write_volatile(mtime.read_volatile() + interval);
let scratch = &mut MSCRATCH0[id];
mscratch::write(scratch.as_mut_ptr() as usize);
scratch[3] = mtime as u64;
scratch[4] = mtimecmp as u64;
scratch[5] = interval;
mtvec::write(crate::symbols::timervec as usize, mtvec::TrapMode::Direct);
mstatus::set_mie();
mie::set_mtimer();
}
pub fn debug() {
unsafe {
println!("0x{:x} 0x{:x}, {}", MSCRATCH0.as_mut_ptr() as usize, MSCRATCH0[hart_id()][4], MSCRATCH0[hart_id()][5]);
}
}