vriscv/src/cpu/exception.c
2023-10-19 21:00:12 +02:00

26 lines
754 B
C

#include "exception.h"
#include <stdio.h>
void exception_trigger(rv32_cpu_t* cpu, uint32_t scause)
{
// An exception can only be triggered by the CPU itself,
// so we know we already own the mutex
// We are in the CPU thread itself, but we need
// the return of this function to be the beginning of
// the cpu loop
// To achieve that, we can just call cpu_loop (noreturn)
// at the end of this function
// Save execution context, so that 'mret/sret/..' can restore it
// TODO
// Set PC to STVEC, and set SCAUSE
// TODO: If PC cannot be mmu_resolved, throw a 'double fault' ?
cpu->pc = cpu->csr[CSR_STVEC];
cpu->csr[CSR_SCAUSE] = scause;
pthread_mutex_unlock(&cpu0_mutex);
cpu_loop(cpu);
}