lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1 | // 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 Sota | 9ac2ac8 | 2020-02-03 16:04:50 -0500 | [diff] [blame] | 5 | #include "sw/device/lib/handler.h" |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 6 | |
Miguel Young de la Sota | bacc071 | 2019-12-18 13:43:14 -0600 | [diff] [blame] | 7 | #include "sw/device/lib/base/stdasm.h" |
Miguel Young de la Sota | 4e14265 | 2020-09-21 12:31:24 -0400 | [diff] [blame] | 8 | #include "sw/device/lib/runtime/log.h" |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 9 | |
| 10 | /** |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 11 | * Return value of mtval |
| 12 | */ |
| 13 | static 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 | */ |
| 24 | static void print_exc_msg(const char *msg) { |
Miguel Young de la Sota | 4e14265 | 2020-09-21 12:31:24 -0400 | [diff] [blame] | 25 | LOG_INFO("%s", msg); |
| 26 | LOG_INFO("MTVAL value is 0x%x", get_mtval()); |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 27 | while (1) { |
| 28 | }; |
| 29 | } |
| 30 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 31 | // Below functions are default weak exception handlers meant to be overriden |
Sam Elliott | 2463e2d | 2019-12-11 17:52:23 +0000 | [diff] [blame] | 32 | __attribute__((weak)) void handler_exception(void) { |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 33 | 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 Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Sam Elliott | 2463e2d | 2019-12-11 17:52:23 +0000 | [diff] [blame] | 67 | __attribute__((weak)) void handler_irq_software(void) { |
Miguel Young de la Sota | 4e14265 | 2020-09-21 12:31:24 -0400 | [diff] [blame] | 68 | LOG_INFO("Software IRQ triggered!"); |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 69 | while (1) { |
| 70 | } |
| 71 | } |
| 72 | |
Sam Elliott | 2463e2d | 2019-12-11 17:52:23 +0000 | [diff] [blame] | 73 | __attribute__((weak)) void handler_irq_timer(void) { |
Miguel Young de la Sota | 4e14265 | 2020-09-21 12:31:24 -0400 | [diff] [blame] | 74 | LOG_INFO("Timer IRQ triggered!"); |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 75 | while (1) { |
| 76 | } |
| 77 | } |
| 78 | |
Sam Elliott | 2463e2d | 2019-12-11 17:52:23 +0000 | [diff] [blame] | 79 | __attribute__((weak)) void handler_irq_external(void) { |
Miguel Young de la Sota | 4e14265 | 2020-09-21 12:31:24 -0400 | [diff] [blame] | 80 | LOG_INFO("External IRQ triggered!"); |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 81 | while (1) { |
| 82 | } |
| 83 | } |
| 84 | |
Sam Elliott | 2463e2d | 2019-12-11 17:52:23 +0000 | [diff] [blame] | 85 | __attribute__((weak)) void handler_instr_acc_fault(void) { |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 86 | const char fault_msg[] = |
Miguel Young de la Sota | 4e14265 | 2020-09-21 12:31:24 -0400 | [diff] [blame] | 87 | "Instruction access fault, mtval shows fault address"; |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 88 | print_exc_msg(fault_msg); |
| 89 | } |
| 90 | |
Sam Elliott | 2463e2d | 2019-12-11 17:52:23 +0000 | [diff] [blame] | 91 | __attribute__((weak)) void handler_instr_ill_fault(void) { |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 92 | const char fault_msg[] = |
Miguel Young de la Sota | 4e14265 | 2020-09-21 12:31:24 -0400 | [diff] [blame] | 93 | "Illegal Instruction fault, mtval shows instruction content"; |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 94 | print_exc_msg(fault_msg); |
| 95 | } |
| 96 | |
Sam Elliott | 2463e2d | 2019-12-11 17:52:23 +0000 | [diff] [blame] | 97 | __attribute__((weak)) void handler_bkpt(void) { |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 98 | const char exc_msg[] = |
Miguel Young de la Sota | 4e14265 | 2020-09-21 12:31:24 -0400 | [diff] [blame] | 99 | "Breakpoint triggerd, mtval shows the breakpoint address"; |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 100 | print_exc_msg(exc_msg); |
| 101 | } |
| 102 | |
Sam Elliott | 2463e2d | 2019-12-11 17:52:23 +0000 | [diff] [blame] | 103 | __attribute__((weak)) void handler_lsu_fault(void) { |
Miguel Young de la Sota | 4e14265 | 2020-09-21 12:31:24 -0400 | [diff] [blame] | 104 | const char exc_msg[] = "Load/Store fault, mtval shows the fault address"; |
Timothy Chen | 25eb480 | 2019-09-20 17:23:26 -0700 | [diff] [blame] | 105 | print_exc_msg(exc_msg); |
| 106 | } |
| 107 | |
Sam Elliott | 2463e2d | 2019-12-11 17:52:23 +0000 | [diff] [blame] | 108 | __attribute__((weak)) void handler_ecall(void) { |
Miguel Young de la Sota | 4e14265 | 2020-09-21 12:31:24 -0400 | [diff] [blame] | 109 | LOG_INFO("Environment call encountered"); |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 110 | while (1) { |
| 111 | } |
| 112 | } |