You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
riscv-pke/kernel/kernel.c

53 lines
1.5 KiB

/*
* Supervisor-mode startup codes
*/
#include "riscv.h"
#include "string.h"
#include "elf.h"
#include "process.h"
#include "spike_interface/spike_utils.h"
// process is a structure defined in kernel/process.h
process user_app;
//
// load the elf, and construct a "process" (with only a trapframe).
// load_bincode_from_host_elf is defined in elf.c
//
void load_user_program(process *proc) {
// USER_TRAP_FRAME is a physical address defined in kernel/config.h
proc->trapframe = (trapframe *)USER_TRAP_FRAME;
memset(proc->trapframe, 0, sizeof(trapframe));
// USER_KSTACK is also a physical address defined in kernel/config.h
proc->kstack = USER_KSTACK;
proc->trapframe->regs.sp = USER_STACK;
// load_bincode_from_host_elf() is defined in kernel/elf.c
load_bincode_from_host_elf(proc);
}
//
// s_start: S-mode entry point of riscv-pke OS kernel.
//
int s_start(void) {
sprint("Enter supervisor mode...\n");
// Note: we use direct (i.e., Bare mode) for memory mapping in lab1.
// which means: Virtual Address = Physical Address
// therefore, we need to set satp to be 0 for now. we will enable paging in lab2_x.
//
// write_csr is a macro defined in kernel/riscv.h
write_csr(satp, 0);
// the application code (elf) is first loaded into memory, and then put into execution
load_user_program(&user_app);
sprint("Switch to user mode...\n");
// switch_to() is defined in kernel/process.c
switch_to(&user_app);
// we should never reach here.
return 0;
}