abstract-machine(nemu): support context switch

This commit is contained in:
xinyangli 2024-07-25 16:47:29 +08:00
parent ef51673020
commit 385d448746
Signed by: xin
SSH key fingerprint: SHA256:qZ/tzd8lYRtUFSrfBDBMcUqV4GHKxqeqRA3huItgvbk
22 changed files with 236 additions and 87 deletions

View file

@ -6,7 +6,7 @@ int main(const char *args);
Area heap = RANGE(&_heap_start, PMEM_END);
#ifndef MAINARGS
#define MAINARGS "5"
#define MAINARGS "i"
#endif
static const char mainargs[] = MAINARGS;

View file

@ -1,14 +1,21 @@
#include "arch/riscv.h"
#include <am.h>
#include <riscv/riscv.h>
#include <klib.h>
#include <riscv/riscv.h>
#include <stdint.h>
static Context* (*user_handler)(Event, Context*) = NULL;
static Context *(*user_handler)(Event, Context *) = NULL;
Context* __am_irq_handle(Context *c) {
Context *__am_irq_handle(Context *c) {
if (user_handler) {
Event ev = {0};
switch (c->mcause) {
default: ev.event = EVENT_ERROR; break;
case CauseEnvironmentCallFromMMode:
ev.event = EVENT_YIELD;
break;
default:
ev.event = EVENT_ERROR;
break;
}
c = user_handler(ev, c);
@ -20,7 +27,7 @@ Context* __am_irq_handle(Context *c) {
extern void __am_asm_trap(void);
bool cte_init(Context*(*handler)(Event, Context*)) {
bool cte_init(Context *(*handler)(Event, Context *)) {
// initialize exception entry
asm volatile("csrw mtvec, %0" : : "r"(__am_asm_trap));
@ -31,7 +38,10 @@ bool cte_init(Context*(*handler)(Event, Context*)) {
}
Context *kcontext(Area kstack, void (*entry)(void *), void *arg) {
return NULL;
Context *c = kstack.end - sizeof(Context);
c->mepc = (uintptr_t)entry;
c->gpr[10] = (uintptr_t)arg;
return c;
}
void yield() {
@ -42,9 +52,6 @@ void yield() {
#endif
}
bool ienabled() {
return false;
}
bool ienabled() { return false; }
void iset(bool enable) {
}
void iset(bool enable) {}

View file

@ -60,6 +60,8 @@ __am_asm_trap:
mv a0, sp
jal __am_irq_handle
mv sp, a0
LOAD t1, OFFSET_STATUS(sp)
LOAD t2, OFFSET_EPC(sp)
csrw mstatus, t1