2019-05-31 13:45:59 +00:00
|
|
|
#include "types.h"
|
2019-06-05 15:42:03 +00:00
|
|
|
#include "param.h"
|
2019-05-31 13:45:59 +00:00
|
|
|
#include "memlayout.h"
|
|
|
|
#include "riscv.h"
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
void main();
|
2019-07-28 17:16:49 +00:00
|
|
|
void timerinit();
|
2019-05-31 13:45:59 +00:00
|
|
|
|
2019-06-05 15:42:03 +00:00
|
|
|
// entry.S needs one stack per CPU.
|
|
|
|
__attribute__ ((aligned (16))) char stack0[4096 * NCPU];
|
2019-05-31 13:45:59 +00:00
|
|
|
|
2020-10-05 19:28:01 +00:00
|
|
|
// a scratch area per CPU for machine-mode timer interrupts.
|
|
|
|
uint64 timer_scratch[NCPU][5];
|
2019-06-04 18:20:37 +00:00
|
|
|
|
2019-07-26 14:17:02 +00:00
|
|
|
// assembly code in kernelvec.S for machine-mode timer interrupt.
|
|
|
|
extern void timervec();
|
2019-06-05 18:05:46 +00:00
|
|
|
|
2019-05-31 13:45:59 +00:00
|
|
|
// entry.S jumps here in machine mode on stack0.
|
|
|
|
void
|
2019-07-23 18:31:12 +00:00
|
|
|
start()
|
2019-05-31 13:45:59 +00:00
|
|
|
{
|
|
|
|
// set M Previous Privilege mode to Supervisor, for mret.
|
|
|
|
unsigned long x = r_mstatus();
|
|
|
|
x &= ~MSTATUS_MPP_MASK;
|
|
|
|
x |= MSTATUS_MPP_S;
|
|
|
|
w_mstatus(x);
|
|
|
|
|
|
|
|
// set M Exception Program Counter to main, for mret.
|
|
|
|
// requires gcc -mcmodel=medany
|
|
|
|
w_mepc((uint64)main);
|
|
|
|
|
|
|
|
// disable paging for now.
|
|
|
|
w_satp(0);
|
|
|
|
|
|
|
|
// delegate all interrupts and exceptions to supervisor mode.
|
|
|
|
w_medeleg(0xffff);
|
|
|
|
w_mideleg(0xffff);
|
2019-10-27 12:03:19 +00:00
|
|
|
w_sie(r_sie() | SIE_SEIE | SIE_STIE | SIE_SSIE);
|
2019-06-04 18:20:37 +00:00
|
|
|
|
2019-07-28 17:16:49 +00:00
|
|
|
// ask for clock interrupts.
|
|
|
|
timerinit();
|
|
|
|
|
|
|
|
// keep each CPU's hartid in its tp register, for cpuid().
|
|
|
|
int id = r_mhartid();
|
|
|
|
w_tp(id);
|
|
|
|
|
|
|
|
// switch to supervisor mode and jump to main().
|
|
|
|
asm volatile("mret");
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up to receive timer interrupts in machine mode,
|
|
|
|
// which arrive at timervec in kernelvec.S,
|
|
|
|
// which turns them into software interrupts for
|
|
|
|
// devintr() in trap.c.
|
|
|
|
void
|
|
|
|
timerinit()
|
|
|
|
{
|
|
|
|
// each CPU has a separate source of timer interrupts.
|
2019-07-26 14:17:02 +00:00
|
|
|
int id = r_mhartid();
|
|
|
|
|
2019-07-25 10:30:49 +00:00
|
|
|
// ask the CLINT for a timer interrupt.
|
|
|
|
int interval = 1000000; // cycles; about 1/10th second in qemu.
|
|
|
|
*(uint64*)CLINT_MTIMECMP(id) = *(uint64*)CLINT_MTIME + interval;
|
2019-07-28 17:16:49 +00:00
|
|
|
|
2019-07-26 14:17:02 +00:00
|
|
|
// prepare information in scratch[] for timervec.
|
2020-10-05 19:28:01 +00:00
|
|
|
// scratch[0..2] : space for timervec to save registers.
|
|
|
|
// scratch[3] : address of CLINT MTIMECMP register.
|
|
|
|
// scratch[4] : desired interval (in cycles) between timer interrupts.
|
|
|
|
uint64 *scratch = &timer_scratch[id][0];
|
|
|
|
scratch[3] = CLINT_MTIMECMP(id);
|
|
|
|
scratch[4] = interval;
|
2019-06-05 15:42:03 +00:00
|
|
|
w_mscratch((uint64)scratch);
|
2019-07-28 17:16:49 +00:00
|
|
|
|
2019-07-25 09:35:03 +00:00
|
|
|
// set the machine-mode trap handler.
|
2019-07-26 14:17:02 +00:00
|
|
|
w_mtvec((uint64)timervec);
|
2019-07-28 17:16:49 +00:00
|
|
|
|
2019-07-25 09:35:03 +00:00
|
|
|
// enable machine-mode interrupts.
|
2019-06-04 18:20:37 +00:00
|
|
|
w_mstatus(r_mstatus() | MSTATUS_MIE);
|
2019-07-28 17:16:49 +00:00
|
|
|
|
2019-07-25 09:35:03 +00:00
|
|
|
// enable machine-mode timer interrupts.
|
|
|
|
w_mie(r_mie() | MIE_MTIE);
|
2019-05-31 13:45:59 +00:00
|
|
|
}
|