2018-12-11 21:54:06 +08:00
|
|
|
/*
|
2019-01-24 14:11:10 +08:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Western Digital Corporation or its affiliates.
|
2018-12-11 21:54:06 +08:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anup Patel <anup.patel@wdc.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sbi/riscv_asm.h>
|
|
|
|
#include <sbi/riscv_encoding.h>
|
|
|
|
#include <sbi/sbi_console.h>
|
|
|
|
#include <sbi/sbi_ecall.h>
|
|
|
|
#include <sbi/sbi_error.h>
|
|
|
|
#include <sbi/sbi_hart.h>
|
2018-12-24 19:19:01 +08:00
|
|
|
#include <sbi/sbi_illegal_insn.h>
|
2018-12-11 21:54:06 +08:00
|
|
|
#include <sbi/sbi_ipi.h>
|
2018-12-24 19:19:01 +08:00
|
|
|
#include <sbi/sbi_misaligned_ldst.h>
|
2018-12-11 21:54:06 +08:00
|
|
|
#include <sbi/sbi_timer.h>
|
|
|
|
#include <sbi/sbi_trap.h>
|
|
|
|
|
2019-01-14 17:30:15 +08:00
|
|
|
static void __noreturn sbi_trap_error(const char *msg,
|
|
|
|
int rc, u32 hartid,
|
|
|
|
ulong mcause, ulong mtval,
|
|
|
|
struct sbi_trap_regs *regs)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
|
|
|
sbi_printf("%s: hart%d: %s (error %d)\n",
|
|
|
|
__func__, hartid, msg, rc);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: mcause=0x%"PRILX" mtval=0x%"PRILX"\n",
|
2018-12-27 12:36:38 +08:00
|
|
|
__func__, hartid, mcause, mtval);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: mepc=0x%"PRILX" mstatus=0x%"PRILX"\n",
|
2018-12-27 12:36:38 +08:00
|
|
|
__func__, hartid, regs->mepc, regs->mstatus);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "ra", regs->ra, "sp", regs->sp);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "gp", regs->gp, "tp", regs->tp);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "s0", regs->s0, "s1", regs->s1);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "a0", regs->a0, "a1", regs->a1);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "a2", regs->a2, "a3", regs->a3);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "a4", regs->a4, "a5", regs->a5);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "a6", regs->a6, "a7", regs->a7);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "s2", regs->s2, "s3", regs->s3);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "s4", regs->s4, "s5", regs->s5);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "s6", regs->s6, "s7", regs->s7);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "s8", regs->s8, "s9", regs->s9);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "s10", regs->s10, "s11", regs->s11);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "t0", regs->t0, "t1", regs->t1);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "t2", regs->t2, "t3", regs->t3);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX" %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "t4", regs->t4, "t5", regs->t5);
|
2019-02-19 20:36:46 +08:00
|
|
|
sbi_printf("%s: hart%d: %s=0x%"PRILX"\n",
|
2018-12-11 21:54:06 +08:00
|
|
|
__func__, hartid, "t6", regs->t6);
|
|
|
|
|
|
|
|
sbi_hart_hang();
|
|
|
|
}
|
|
|
|
|
2019-01-14 17:30:15 +08:00
|
|
|
/**
|
|
|
|
* Redirect trap to lower privledge mode (S-mode or U-mode)
|
|
|
|
*
|
|
|
|
* @param regs pointer to register state
|
|
|
|
* @param scratch pointer to sbi_scratch of current HART
|
|
|
|
* @param epc error PC for lower privledge mode
|
|
|
|
* @param cause exception cause for lower privledge mode
|
|
|
|
* @param tval trap value for lower privledge mode
|
|
|
|
*
|
2019-01-22 01:53:23 +08:00
|
|
|
* @return 0 on success and negative error code on failure
|
2019-01-14 17:30:15 +08:00
|
|
|
*/
|
2018-12-28 00:09:13 +08:00
|
|
|
int sbi_trap_redirect(struct sbi_trap_regs *regs,
|
|
|
|
struct sbi_scratch *scratch,
|
|
|
|
ulong epc, ulong cause, ulong tval)
|
|
|
|
{
|
|
|
|
ulong new_mstatus, prev_mode;
|
|
|
|
|
|
|
|
/* Sanity check on previous mode */
|
|
|
|
prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
|
|
|
|
if (prev_mode != PRV_S && prev_mode != PRV_U)
|
|
|
|
return SBI_ENOTSUPP;
|
|
|
|
|
|
|
|
/* Update S-mode exception info */
|
2019-02-04 10:49:20 +08:00
|
|
|
csr_write(CSR_STVAL, tval);
|
|
|
|
csr_write(CSR_SEPC, epc);
|
|
|
|
csr_write(CSR_SCAUSE, cause);
|
2018-12-28 00:09:13 +08:00
|
|
|
|
|
|
|
/* Set MEPC to S-mode exception vector base */
|
2019-02-13 10:32:06 +08:00
|
|
|
regs->mepc = csr_read(CSR_STVEC);
|
2018-12-28 00:09:13 +08:00
|
|
|
|
|
|
|
/* Initial value of new MSTATUS */
|
|
|
|
new_mstatus = regs->mstatus;
|
|
|
|
|
|
|
|
/* Clear MPP, SPP, SPIE, and SIE */
|
|
|
|
new_mstatus &= ~(MSTATUS_MPP |
|
|
|
|
MSTATUS_SPP | MSTATUS_SPIE | MSTATUS_SIE);
|
|
|
|
|
|
|
|
/* Set SPP */
|
|
|
|
if (prev_mode == PRV_S)
|
|
|
|
new_mstatus |= (1UL << MSTATUS_SPP_SHIFT);
|
|
|
|
|
|
|
|
/* Set SPIE */
|
|
|
|
if (regs->mstatus & MSTATUS_SIE)
|
|
|
|
new_mstatus |= (1UL << MSTATUS_SPIE_SHIFT);
|
|
|
|
|
|
|
|
/* Set MPP */
|
|
|
|
new_mstatus |= (PRV_S << MSTATUS_MPP_SHIFT);
|
|
|
|
|
|
|
|
/* Set new value in MSTATUS */
|
|
|
|
regs->mstatus = new_mstatus;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-14 17:30:15 +08:00
|
|
|
/**
|
|
|
|
* Handle trap/interrupt
|
|
|
|
*
|
|
|
|
* This function is called by firmware linked to OpenSBI
|
|
|
|
* library for handling trap/interrupt. It expects the
|
|
|
|
* following:
|
|
|
|
* 1. The 'mscratch' CSR is pointing to sbi_scratch of current HART
|
|
|
|
* 2. The 'mcause' CSR is having exception/interrupt cause
|
|
|
|
* 3. The 'mtval' CSR is having additional trap information
|
|
|
|
* 4. Stack pointer (SP) is setup for current HART
|
|
|
|
* 5. Interrupts are disabled in MSTATUS CSR
|
|
|
|
*
|
|
|
|
* @param regs pointer to register state
|
|
|
|
* @param scratch pointer to sbi_scratch of current HART
|
|
|
|
*/
|
2018-12-11 21:54:06 +08:00
|
|
|
void sbi_trap_handler(struct sbi_trap_regs *regs,
|
|
|
|
struct sbi_scratch *scratch)
|
|
|
|
{
|
2018-12-27 12:36:38 +08:00
|
|
|
int rc = SBI_ENOTSUPP;
|
|
|
|
const char *msg = "trap handler failed";
|
2018-12-21 16:06:59 +08:00
|
|
|
u32 hartid = sbi_current_hartid();
|
2019-02-13 10:32:06 +08:00
|
|
|
ulong mcause = csr_read(CSR_MCAUSE);
|
2018-12-11 21:54:06 +08:00
|
|
|
|
|
|
|
if (mcause & (1UL << (__riscv_xlen - 1))) {
|
|
|
|
mcause &= ~(1UL << (__riscv_xlen - 1));
|
|
|
|
switch (mcause) {
|
|
|
|
case IRQ_M_TIMER:
|
2019-01-22 17:17:03 +08:00
|
|
|
sbi_timer_process(scratch);
|
2018-12-11 21:54:06 +08:00
|
|
|
break;
|
|
|
|
case IRQ_M_SOFT:
|
2019-01-22 16:50:59 +08:00
|
|
|
sbi_ipi_process(scratch);
|
2018-12-11 21:54:06 +08:00
|
|
|
break;
|
|
|
|
default:
|
2018-12-27 12:36:38 +08:00
|
|
|
msg = "unhandled external interrupt";
|
|
|
|
goto trap_error;
|
2018-12-11 21:54:06 +08:00
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mcause) {
|
|
|
|
case CAUSE_ILLEGAL_INSTRUCTION:
|
|
|
|
rc = sbi_illegal_insn_handler(hartid, mcause, regs, scratch);
|
|
|
|
msg = "illegal instruction handler failed";
|
|
|
|
break;
|
2018-12-24 19:19:01 +08:00
|
|
|
case CAUSE_MISALIGNED_LOAD:
|
|
|
|
rc = sbi_misaligned_load_handler(hartid, mcause, regs, scratch);
|
|
|
|
msg = "misaligned load handler failed";
|
|
|
|
break;
|
|
|
|
case CAUSE_MISALIGNED_STORE:
|
|
|
|
rc = sbi_misaligned_store_handler(hartid, mcause, regs, scratch);
|
|
|
|
msg = "misaligned store handler failed";
|
|
|
|
break;
|
2018-12-11 21:54:06 +08:00
|
|
|
case CAUSE_SUPERVISOR_ECALL:
|
|
|
|
case CAUSE_HYPERVISOR_ECALL:
|
|
|
|
rc = sbi_ecall_handler(hartid, mcause, regs, scratch);
|
|
|
|
msg = "ecall handler failed";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2018-12-27 12:36:38 +08:00
|
|
|
trap_error:
|
2018-12-11 21:54:06 +08:00
|
|
|
if (rc) {
|
2019-02-04 10:49:20 +08:00
|
|
|
sbi_trap_error(msg, rc, hartid, mcause, csr_read(CSR_MTVAL), regs);
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|
|
|
|
}
|