Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rick Chen | 6eedd92 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016-17 Microsemi Corporation. |
| 4 | * Padmarao Begari, Microsemi Corporation <padmarao.begari@microsemi.com> |
| 5 | * |
| 6 | * Copyright (C) 2017 Andes Technology Corporation |
| 7 | * Rick Chen, Andes Technology Corporation <rick@andestech.com> |
Sean Anderson | e8b46a1 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 8 | * |
| 9 | * Copyright (C) 2019 Sean Anderson <seanga2@gmail.com> |
Rick Chen | 6eedd92 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 10 | */ |
| 11 | |
Kautuk Consul | c86cb4a | 2022-12-07 17:12:35 +0530 | [diff] [blame] | 12 | #include <linux/compat.h> |
Heinrich Schuchardt | 77efe24 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 13 | #include <efi_loader.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 14 | #include <hang.h> |
Heinrich Schuchardt | 8bf50cd | 2023-10-31 14:55:51 +0200 | [diff] [blame] | 15 | #include <interrupt.h> |
Simon Glass | 9b61c7c | 2019-11-14 12:57:41 -0700 | [diff] [blame] | 16 | #include <irq_func.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Rick Chen | 6eedd92 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 18 | #include <asm/ptrace.h> |
| 19 | #include <asm/system.h> |
| 20 | #include <asm/encoding.h> |
Kautuk Consul | c86cb4a | 2022-12-07 17:12:35 +0530 | [diff] [blame] | 21 | #include <semihosting.h> |
Rick Chen | 6eedd92 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 22 | |
Heinrich Schuchardt | 77efe24 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Heinrich Schuchardt | 8bf50cd | 2023-10-31 14:55:51 +0200 | [diff] [blame] | 25 | static struct resume_data *resume; |
| 26 | |
| 27 | void set_resume(struct resume_data *data) |
| 28 | { |
| 29 | resume = data; |
| 30 | } |
| 31 | |
Heinrich Schuchardt | 77efe24 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 32 | static void show_efi_loaded_images(uintptr_t epc) |
| 33 | { |
| 34 | efi_print_image_infos((void *)epc); |
| 35 | } |
| 36 | |
Sean Anderson | e8b46a1 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 37 | static void show_regs(struct pt_regs *regs) |
| 38 | { |
| 39 | #ifdef CONFIG_SHOW_REGS |
Heinrich Schuchardt | b881ba8 | 2020-12-02 14:36:26 +0100 | [diff] [blame] | 40 | printf("\nSP: " REG_FMT " GP: " REG_FMT " TP: " REG_FMT "\n", |
Heinrich Schuchardt | 77efe24 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 41 | regs->sp, regs->gp, regs->tp); |
| 42 | printf("T0: " REG_FMT " T1: " REG_FMT " T2: " REG_FMT "\n", |
| 43 | regs->t0, regs->t1, regs->t2); |
| 44 | printf("S0: " REG_FMT " S1: " REG_FMT " A0: " REG_FMT "\n", |
| 45 | regs->s0, regs->s1, regs->a0); |
| 46 | printf("A1: " REG_FMT " A2: " REG_FMT " A3: " REG_FMT "\n", |
| 47 | regs->a1, regs->a2, regs->a3); |
| 48 | printf("A4: " REG_FMT " A5: " REG_FMT " A6: " REG_FMT "\n", |
| 49 | regs->a4, regs->a5, regs->a6); |
| 50 | printf("A7: " REG_FMT " S2: " REG_FMT " S3: " REG_FMT "\n", |
| 51 | regs->a7, regs->s2, regs->s3); |
| 52 | printf("S4: " REG_FMT " S5: " REG_FMT " S6: " REG_FMT "\n", |
| 53 | regs->s4, regs->s5, regs->s6); |
| 54 | printf("S7: " REG_FMT " S8: " REG_FMT " S9: " REG_FMT "\n", |
| 55 | regs->s7, regs->s8, regs->s9); |
| 56 | printf("S10: " REG_FMT " S11: " REG_FMT " T3: " REG_FMT "\n", |
| 57 | regs->s10, regs->s11, regs->t3); |
Heinrich Schuchardt | b881ba8 | 2020-12-02 14:36:26 +0100 | [diff] [blame] | 58 | printf("T4: " REG_FMT " T5: " REG_FMT " T6: " REG_FMT "\n", |
Heinrich Schuchardt | 77efe24 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 59 | regs->t4, regs->t5, regs->t6); |
Sean Anderson | e8b46a1 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 60 | #endif |
| 61 | } |
| 62 | |
Ben Dooks | 8a813c1 | 2023-09-05 13:12:53 +0100 | [diff] [blame] | 63 | #if defined(CONFIG_FRAMEPOINTER) || defined(CONFIG_SPL_FRAMEPOINTER) |
| 64 | static void show_backtrace(struct pt_regs *regs) |
| 65 | { |
| 66 | uintptr_t *fp = (uintptr_t *)regs->s0; |
| 67 | unsigned count = 0; |
| 68 | ulong ra; |
| 69 | |
| 70 | printf("backtrace:\n"); |
| 71 | |
| 72 | /* there are a few entry points where the s0 register is |
| 73 | * set to gd, so to avoid changing those, just abort if |
| 74 | * the value is the same */ |
| 75 | while (fp != NULL && fp != (uintptr_t *)gd) { |
| 76 | ra = fp[-1]; |
| 77 | printf("backtrace %2d: FP: " REG_FMT " RA: " REG_FMT, |
| 78 | count, (ulong)fp, ra); |
| 79 | |
| 80 | if (gd && gd->flags & GD_FLG_RELOC) |
| 81 | printf(" - RA: " REG_FMT " reloc adjusted\n", |
| 82 | ra - gd->reloc_off); |
| 83 | else |
| 84 | printf("\n"); |
| 85 | |
| 86 | fp = (uintptr_t *)fp[-2]; |
| 87 | count++; |
| 88 | } |
| 89 | } |
| 90 | #else |
| 91 | static void show_backtrace(struct pt_regs *regs) |
| 92 | { |
| 93 | printf("No backtrace support enabled\n"); |
| 94 | } |
| 95 | #endif |
| 96 | |
Heinrich Schuchardt | 79c6855 | 2021-09-04 10:36:49 +0200 | [diff] [blame] | 97 | /** |
| 98 | * instr_len() - get instruction length |
| 99 | * |
| 100 | * @i: low 16 bits of the instruction |
| 101 | * Return: number of u16 in instruction |
| 102 | */ |
| 103 | static int instr_len(u16 i) |
| 104 | { |
| 105 | if ((i & 0x03) != 0x03) |
| 106 | return 1; |
| 107 | /* Instructions with more than 32 bits are not yet specified */ |
| 108 | return 2; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * show_code() - display code leading to exception |
| 113 | * |
| 114 | * @epc: program counter |
| 115 | */ |
| 116 | static void show_code(ulong epc) |
| 117 | { |
| 118 | u16 *pos = (u16 *)(epc & ~1UL); |
| 119 | int i, len = instr_len(*pos); |
| 120 | |
| 121 | printf("\nCode: "); |
| 122 | for (i = -8; i; ++i) |
| 123 | printf("%04x ", pos[i]); |
| 124 | printf("("); |
| 125 | for (i = 0; i < len; ++i) |
| 126 | printf("%04x%s", pos[i], i + 1 == len ? ")\n" : " "); |
| 127 | } |
| 128 | |
Sean Anderson | e8b46a1 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 129 | static void _exit_trap(ulong code, ulong epc, ulong tval, struct pt_regs *regs) |
Bin Meng | bcc6c74 | 2018-12-12 06:12:44 -0800 | [diff] [blame] | 130 | { |
| 131 | static const char * const exception_code[] = { |
| 132 | "Instruction address misaligned", |
| 133 | "Instruction access fault", |
| 134 | "Illegal instruction", |
| 135 | "Breakpoint", |
| 136 | "Load address misaligned", |
| 137 | "Load access fault", |
| 138 | "Store/AMO address misaligned", |
| 139 | "Store/AMO access fault", |
| 140 | "Environment call from U-mode", |
| 141 | "Environment call from S-mode", |
| 142 | "Reserved", |
| 143 | "Environment call from M-mode", |
| 144 | "Instruction page fault", |
| 145 | "Load page fault", |
| 146 | "Reserved", |
| 147 | "Store/AMO page fault", |
| 148 | }; |
| 149 | |
Heinrich Schuchardt | 8bf50cd | 2023-10-31 14:55:51 +0200 | [diff] [blame] | 150 | if (resume) { |
| 151 | resume->code = code; |
| 152 | longjmp(resume->jump, 1); |
| 153 | } |
| 154 | |
Sean Anderson | e8b46a1 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 155 | if (code < ARRAY_SIZE(exception_code)) |
| 156 | printf("Unhandled exception: %s\n", exception_code[code]); |
| 157 | else |
| 158 | printf("Unhandled exception code: %ld\n", code); |
Bin Meng | bcc6c74 | 2018-12-12 06:12:44 -0800 | [diff] [blame] | 159 | |
Heinrich Schuchardt | 77efe24 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 160 | printf("EPC: " REG_FMT " RA: " REG_FMT " TVAL: " REG_FMT "\n", |
| 161 | epc, regs->ra, tval); |
Sean Anderson | 2c4c7d1 | 2020-09-21 07:51:40 -0400 | [diff] [blame] | 162 | /* Print relocation adjustments, but only if gd is initialized */ |
| 163 | if (gd && gd->flags & GD_FLG_RELOC) |
Heinrich Schuchardt | b881ba8 | 2020-12-02 14:36:26 +0100 | [diff] [blame] | 164 | printf("EPC: " REG_FMT " RA: " REG_FMT " reloc adjusted\n", |
Heinrich Schuchardt | 77efe24 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 165 | epc - gd->reloc_off, regs->ra - gd->reloc_off); |
| 166 | |
Sean Anderson | e8b46a1 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 167 | show_regs(regs); |
Ben Dooks | 8a813c1 | 2023-09-05 13:12:53 +0100 | [diff] [blame] | 168 | show_backtrace(regs); |
Heinrich Schuchardt | 79c6855 | 2021-09-04 10:36:49 +0200 | [diff] [blame] | 169 | show_code(epc); |
Heinrich Schuchardt | 77efe24 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 170 | show_efi_loaded_images(epc); |
Heinrich Schuchardt | b881ba8 | 2020-12-02 14:36:26 +0100 | [diff] [blame] | 171 | panic("\n"); |
Bin Meng | bcc6c74 | 2018-12-12 06:12:44 -0800 | [diff] [blame] | 172 | } |
Rick Chen | 6eedd92 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 173 | |
| 174 | int interrupt_init(void) |
| 175 | { |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * enable interrupts |
| 181 | */ |
| 182 | void enable_interrupts(void) |
| 183 | { |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * disable interrupts |
| 188 | */ |
| 189 | int disable_interrupts(void) |
| 190 | { |
| 191 | return 0; |
| 192 | } |
| 193 | |
Sean Anderson | e8b46a1 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 194 | ulong handle_trap(ulong cause, ulong epc, ulong tval, struct pt_regs *regs) |
Rick Chen | 6eedd92 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 195 | { |
Anup Patel | 89b3934 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 196 | ulong is_irq, irq; |
| 197 | |
Heinrich Schuchardt | fbef54d | 2020-09-26 07:50:36 +0200 | [diff] [blame] | 198 | /* An UEFI application may have changed gd. Restore U-Boot's gd. */ |
| 199 | efi_restore_gd(); |
| 200 | |
Kautuk Consul | c86cb4a | 2022-12-07 17:12:35 +0530 | [diff] [blame] | 201 | if (cause == CAUSE_BREAKPOINT && |
| 202 | CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)) { |
| 203 | ulong pre_addr = epc - 4, post_addr = epc + 4; |
| 204 | |
| 205 | /* Check for prior and post addresses to be in same page. */ |
| 206 | if ((pre_addr & ~(PAGE_SIZE - 1)) == |
| 207 | (post_addr & ~(PAGE_SIZE - 1))) { |
| 208 | u32 pre = *(u32 *)pre_addr; |
| 209 | u32 post = *(u32 *)post_addr; |
| 210 | |
| 211 | /* Check for semihosting, i.e.: |
| 212 | * slli zero,zero,0x1f |
| 213 | * ebreak |
| 214 | * srai zero,zero,0x7 |
| 215 | */ |
| 216 | if (pre == 0x01f01013 && post == 0x40705013) { |
| 217 | disable_semihosting(); |
| 218 | epc += 4; |
| 219 | return epc; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
Anup Patel | 89b3934 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 224 | is_irq = (cause & MCAUSE_INT); |
| 225 | irq = (cause & ~MCAUSE_INT); |
Rick Chen | 6eedd92 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 226 | |
Anup Patel | 89b3934 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 227 | if (is_irq) { |
| 228 | switch (irq) { |
| 229 | case IRQ_M_EXT: |
| 230 | case IRQ_S_EXT: |
| 231 | external_interrupt(0); /* handle external interrupt */ |
| 232 | break; |
| 233 | case IRQ_M_TIMER: |
| 234 | case IRQ_S_TIMER: |
| 235 | timer_interrupt(0); /* handle timer interrupt */ |
| 236 | break; |
| 237 | default: |
Sean Anderson | e8b46a1 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 238 | _exit_trap(cause, epc, tval, regs); |
Anup Patel | 89b3934 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 239 | break; |
| 240 | }; |
| 241 | } else { |
Sean Anderson | e8b46a1 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 242 | _exit_trap(cause, epc, tval, regs); |
Anup Patel | 89b3934 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 243 | } |
Rick Chen | 6eedd92 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 244 | |
| 245 | return epc; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | *Entry Point for PLIC Interrupt Handler |
| 250 | */ |
| 251 | __attribute__((weak)) void external_interrupt(struct pt_regs *regs) |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | __attribute__((weak)) void timer_interrupt(struct pt_regs *regs) |
| 256 | { |
| 257 | } |