Skip to content

Commit

Permalink
riscv-sbi/exceptions: handle empty mtval register
Browse files Browse the repository at this point in the history
JIRA: RTOS-859
  • Loading branch information
lukileczo committed Jul 10, 2024
1 parent 5853ca8 commit 5fa5d6f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
19 changes: 13 additions & 6 deletions riscv-sbi/core/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,24 +280,31 @@ static void exceptions_illegalHandler(unsigned int n, exc_context_t *ctx)
{
u64 insn = ctx->mtval;
u64 prevMode = (ctx->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
unsigned long unpriv_insn;

/* Check failing instruction */
if (((insn & 3) == 3) && (((insn & 0x7c) >> 2) == 0x1c)) {
/* Non-compressed, SYSTEM opcode */
exceptions_illSystem(n, ctx);
}
else if (insn == 0) {
/* RV implementation didn't write mtval.
* TODO: get failing instruction through unprivileged access
*/
exceptions_defaultHandler(n, ctx);
}
else {
if (prevMode == PRV_M) {
/* Trapped on illegal instruction in M-mode */
exceptions_defaultHandler(n, ctx);
}
else {
if (insn == 0) {
/* mepc has virtual address, have to load using MPRV */
MPRV_LOAD(lhu, insn, ctx->mepc);
/* Check lowest to bits to determine if we are dealing with compressed insn
* Noncompressed RV instructions have bits [1:0] = 11.
*/
if ((insn & 3) == 3) {
/* Noncompressed, load 2nd part */
MPRV_LOAD(lhu, unpriv_insn, ctx->mepc + 2);
insn = (unpriv_insn << 16) | insn;
}
}
exceptions_redirect(n, insn, ctx);
}
}
Expand Down
20 changes: 20 additions & 0 deletions riscv-sbi/include/hart.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@
do { \
__asm__ volatile ("wfi" ::: "memory"); \
} while (0)


/* Load value using mstatus privilege modification
* This macro allows loads from virtual address in M-Mode.
*/
#define MPRV_LOAD(load_inst, reg, addr) \
__asm__ volatile ( \
/* Set MPRV bit */ \
"li a6, (1 << 17)\n\t" \
"csrs mstatus, a6\n\t" \
".option push\n\t" \
".option norvc\n\t" \
#load_inst " %0, (%1)\n\t" \
".option pop\n\t" /* Clear MPRV bit */ \
"csrc mstatus, a6" \
: "=r"(reg) \
: "r"(addr) \
: "a6", "memory" \
);

/* clang-format on */


Expand Down

0 comments on commit 5fa5d6f

Please sign in to comment.