blob: 4b6a93ce11d66565c17913dc6ec6607ae6674888 [file] [log] [blame]
lowRISC Contributors802543a2019-08-31 12:12:56 +01001// Copyright lowRISC contributors.
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
Miguel Young de la Sota9ac2ac82020-02-03 16:04:50 -05005#include "sw/device/lib/handler.h"
Timothy Chen25eb4802019-09-20 17:23:26 -07006
Miguel Young de la Sotabacc0712019-12-18 13:43:14 -06007#include "sw/device/lib/base/stdasm.h"
Miguel Young de la Sota4e142652020-09-21 12:31:24 -04008#include "sw/device/lib/runtime/log.h"
lowRISC Contributors802543a2019-08-31 12:12:56 +01009
10/**
Timothy Chen25eb4802019-09-20 17:23:26 -070011 * Return value of mtval
12 */
13static uint32_t get_mtval(void) {
14 uint32_t mtval;
15 asm volatile("csrr %0, mtval" : "=r"(mtval) : :);
16 return mtval;
17}
18
19/**
20 * Default Error Handling
21 * @param error message supplied by caller
22 * TODO - this will be soon by a real print formatting
23 */
24static void print_exc_msg(const char *msg) {
Miguel Young de la Sota4e142652020-09-21 12:31:24 -040025 LOG_INFO("%s", msg);
26 LOG_INFO("MTVAL value is 0x%x", get_mtval());
Timothy Chen25eb4802019-09-20 17:23:26 -070027 while (1) {
28 };
29}
30
lowRISC Contributors802543a2019-08-31 12:12:56 +010031// Below functions are default weak exception handlers meant to be overriden
Sam Elliott2463e2d2019-12-11 17:52:23 +000032__attribute__((weak)) void handler_exception(void) {
Timothy Chen25eb4802019-09-20 17:23:26 -070033 uint32_t mcause;
34 exc_id_t exc_cause;
35
36 asm volatile("csrr %0 , mcause" : "=r"(mcause) : :);
37 exc_cause = (exc_id_t)(mcause & kIdMax);
38
39 switch (exc_cause) {
40 case kInstMisa:
41 handler_instr_acc_fault();
42 break;
43 case kInstAccFault:
44 handler_instr_acc_fault();
45 break;
46 case kInstIllegalFault:
47 handler_instr_ill_fault();
48 break;
49 case kBkpt:
50 handler_bkpt();
51 break;
52 case kLoadAccFault:
53 handler_lsu_fault();
54 break;
55 case kStrAccFault:
56 handler_lsu_fault();
57 break;
58 case kECall:
59 handler_ecall();
60 break;
61 default:
62 while (1) {
63 };
lowRISC Contributors802543a2019-08-31 12:12:56 +010064 }
65}
66
Sam Elliott2463e2d2019-12-11 17:52:23 +000067__attribute__((weak)) void handler_irq_software(void) {
Miguel Young de la Sota4e142652020-09-21 12:31:24 -040068 LOG_INFO("Software IRQ triggered!");
lowRISC Contributors802543a2019-08-31 12:12:56 +010069 while (1) {
70 }
71}
72
Sam Elliott2463e2d2019-12-11 17:52:23 +000073__attribute__((weak)) void handler_irq_timer(void) {
Miguel Young de la Sota4e142652020-09-21 12:31:24 -040074 LOG_INFO("Timer IRQ triggered!");
lowRISC Contributors802543a2019-08-31 12:12:56 +010075 while (1) {
76 }
77}
78
Sam Elliott2463e2d2019-12-11 17:52:23 +000079__attribute__((weak)) void handler_irq_external(void) {
Miguel Young de la Sota4e142652020-09-21 12:31:24 -040080 LOG_INFO("External IRQ triggered!");
Timothy Chen25eb4802019-09-20 17:23:26 -070081 while (1) {
82 }
83}
84
Sam Elliott2463e2d2019-12-11 17:52:23 +000085__attribute__((weak)) void handler_instr_acc_fault(void) {
Timothy Chen25eb4802019-09-20 17:23:26 -070086 const char fault_msg[] =
Miguel Young de la Sota4e142652020-09-21 12:31:24 -040087 "Instruction access fault, mtval shows fault address";
Timothy Chen25eb4802019-09-20 17:23:26 -070088 print_exc_msg(fault_msg);
89}
90
Sam Elliott2463e2d2019-12-11 17:52:23 +000091__attribute__((weak)) void handler_instr_ill_fault(void) {
Timothy Chen25eb4802019-09-20 17:23:26 -070092 const char fault_msg[] =
Miguel Young de la Sota4e142652020-09-21 12:31:24 -040093 "Illegal Instruction fault, mtval shows instruction content";
Timothy Chen25eb4802019-09-20 17:23:26 -070094 print_exc_msg(fault_msg);
95}
96
Sam Elliott2463e2d2019-12-11 17:52:23 +000097__attribute__((weak)) void handler_bkpt(void) {
Timothy Chen25eb4802019-09-20 17:23:26 -070098 const char exc_msg[] =
Miguel Young de la Sota4e142652020-09-21 12:31:24 -040099 "Breakpoint triggerd, mtval shows the breakpoint address";
Timothy Chen25eb4802019-09-20 17:23:26 -0700100 print_exc_msg(exc_msg);
101}
102
Sam Elliott2463e2d2019-12-11 17:52:23 +0000103__attribute__((weak)) void handler_lsu_fault(void) {
Miguel Young de la Sota4e142652020-09-21 12:31:24 -0400104 const char exc_msg[] = "Load/Store fault, mtval shows the fault address";
Timothy Chen25eb4802019-09-20 17:23:26 -0700105 print_exc_msg(exc_msg);
106}
107
Sam Elliott2463e2d2019-12-11 17:52:23 +0000108__attribute__((weak)) void handler_ecall(void) {
Miguel Young de la Sota4e142652020-09-21 12:31:24 -0400109 LOG_INFO("Environment call encountered");
lowRISC Contributors802543a2019-08-31 12:12:56 +0100110 while (1) {
111 }
112}