// TODO: CHECK/REPLACE/UPDATE OLD CODE (this file is based on xv6) #include "types.h" #include "param.h" #include "memlayout.h" #include "riscv.h" #include "defs.h" #include "sched.h" void main(); void timerinit(); // entry.S needs one stack per CPU. /*TODO __attribute__ ((aligned (16))) char stack0[4096 * NCPU]; */ uint64 stack0[512*SCHED_CORE_MAX]; // entry.S jumps here in machine mode on stack0. void start() { //consputc(65); // 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); w_sie(r_sie() | SIE_SEIE | SIE_STIE | SIE_SSIE); // configure Physical Memory Protection to give supervisor mode // access to all of physical memory. w_pmpaddr0(0x3fffffffffffffull); w_pmpcfg0(0xf); // ask for clock interrupts. timerinit(); // keep each CPU's hartid in its tp register, for SCHED_CORE_THISNUMBER_NOINTERRUPTS(). int _id = r_mhartid(); sched_cputhreadpointer_set(_id); // switch to supervisor mode and jump to main(). #ifdef _ZCC __asm { mret }; #else asm volatile("mret"); #endif } // ask each hart to generate timer interrupts. void timerinit() { // enable supervisor-mode timer interrupts. w_mie(r_mie() | MIE_STIE); // enable the sstc extension (i.e. stimecmp). w_menvcfg(r_menvcfg() | (1L << 63)); // allow supervisor to use stimecmp and time. w_mcounteren(r_mcounteren() | 2); // ask for the very first timer interrupt. w_stimecmp(r_time() + 1000000); }