xv6-riscv-kernel/kernel/start.c

89 lines
2.3 KiB
C
Raw Permalink Normal View History

2019-05-31 15:45:59 +02:00
#include "types.h"
2019-06-05 17:42:03 +02:00
#include "param.h"
2019-05-31 15:45:59 +02:00
#include "memlayout.h"
#include "riscv.h"
void main();
void timerinit();
2019-05-31 15:45:59 +02:00
2024-08-07 14:33:58 +02:00
// Entry.S needs one stack per CPU.
2024-06-15 16:55:06 +02:00
__attribute__((aligned(16))) char stack0[4096 * NCPU];
2019-05-31 15:45:59 +02:00
2024-08-07 14:33:58 +02:00
// A scratch area per CPU for machine-mode timer interrupts.
2024-05-24 11:26:40 +02:00
u64 timer_scratch[NCPU][5];
2024-08-07 14:33:58 +02:00
// Assembly code in kernelvec.S for machine-mode timer interrupt.
2019-07-26 16:17:02 +02:00
extern void timervec();
2024-08-07 14:33:58 +02:00
// Entry.S jumps here in machine mode on stack0.
2019-05-31 15:45:59 +02:00
void
2019-07-23 20:31:12 +02:00
start()
2019-05-31 15:45:59 +02:00
{
2024-08-07 14:33:58 +02:00
// Set M Previous Privilege mode to Supervisor, for mret.
2019-05-31 15:45:59 +02:00
unsigned long x = r_mstatus();
x &= ~MSTATUS_MPP_MASK;
x |= MSTATUS_MPP_S;
w_mstatus(x);
2024-08-07 14:33:58 +02:00
// Set M Exception Program Counter to main, for mret.
// Requires gcc -mcmodel=medany
2024-05-24 11:26:40 +02:00
w_mepc((u64)main);
2019-05-31 15:45:59 +02:00
2024-08-07 14:33:58 +02:00
// Disable paging for now.
2019-05-31 15:45:59 +02:00
w_satp(0);
2024-08-07 14:33:58 +02:00
// Delegate all interrupts and exceptions to supervisor mode.
2019-05-31 15:45:59 +02:00
w_medeleg(0xffff);
w_mideleg(0xffff);
2019-10-27 13:03:19 +01:00
w_sie(r_sie() | SIE_SEIE | SIE_STIE | SIE_SSIE);
2024-08-07 14:33:58 +02:00
// Configure Physical Memory Protection to give supervisor mode
// Access to all of physical memory.
2021-08-30 22:27:52 +02:00
w_pmpaddr0(0x3fffffffffffffull);
w_pmpcfg0(0xf);
2024-08-07 14:33:58 +02:00
// Ask for clock interrupts.
timerinit();
2024-08-07 14:33:58 +02:00
// Keep each CPU's hartid in its tp register, for cpuid().
int id = r_mhartid();
w_tp(id);
2024-08-07 14:33:58 +02:00
// Switch to supervisor mode and jump to main().
asm volatile("mret");
}
2024-08-07 14:33:58 +02:00
// Arrange to receive timer interrupts.
// They will arrive in machine mode at
2022-08-09 20:17:46 +02:00
// at timervec in kernelvec.S,
// which turns them into software interrupts for
// devintr() in trap.c.
void
timerinit()
{
2024-08-07 14:33:58 +02:00
// Each CPU has a separate source of timer interrupts.
2019-07-26 16:17:02 +02:00
int id = r_mhartid();
2024-08-07 14:33:58 +02:00
// Ask the CLINT for a timer interrupt.
int interval = 1000000; // Cycles; about 1/10th second in qemu.
2024-06-15 16:55:06 +02:00
*(u64 *)CLINT_MTIMECMP(id) = *(u64 *)CLINT_MTIME + interval;
2024-08-07 14:33:58 +02:00
// Prepare information in scratch[] for timervec.
// 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.
2024-05-24 11:26:40 +02:00
u64 *scratch = &timer_scratch[id][0];
scratch[3] = CLINT_MTIMECMP(id);
scratch[4] = interval;
2024-05-24 11:26:40 +02:00
w_mscratch((u64)scratch);
2024-08-07 14:33:58 +02:00
// Set the machine-mode trap handler.
2024-05-24 11:26:40 +02:00
w_mtvec((u64)timervec);
2024-08-07 14:33:58 +02:00
// Enable machine-mode interrupts.
w_mstatus(r_mstatus() | MSTATUS_MIE);
2024-08-07 14:33:58 +02:00
// Enable machine-mode timer interrupts.
2019-07-25 11:35:03 +02:00
w_mie(r_mie() | MIE_MTIE);
2019-05-31 15:45:59 +02:00
}