Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 |
| 4 | * David Feng <fenghua@phytium.com.cn> |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Sean Anderson | 064c857 | 2022-03-22 16:59:31 -0400 | [diff] [blame] | 7 | #include <asm/esr.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 8 | #include <asm/global_data.h> |
Simon Glass | 6b9f010 | 2020-05-10 11:40:06 -0600 | [diff] [blame] | 9 | #include <asm/ptrace.h> |
Simon Glass | 9b61c7c | 2019-11-14 12:57:41 -0700 | [diff] [blame] | 10 | #include <irq_func.h> |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 11 | #include <linux/compiler.h> |
Alexander Graf | 5817786 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 12 | #include <efi_loader.h> |
Sean Anderson | 064c857 | 2022-03-22 16:59:31 -0400 | [diff] [blame] | 13 | #include <semihosting.h> |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 14 | |
Peng Fan | c3c9b33 | 2017-11-28 10:08:08 +0800 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 16 | |
| 17 | int interrupt_init(void) |
| 18 | { |
Ovidiu Panait | 8998e71 | 2020-04-20 10:31:44 +0300 | [diff] [blame] | 19 | enable_interrupts(); |
| 20 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | void enable_interrupts(void) |
| 25 | { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | int disable_interrupts(void) |
| 30 | { |
| 31 | return 0; |
| 32 | } |
| 33 | |
Heinrich Schuchardt | b7c0c38 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 34 | static void show_efi_loaded_images(struct pt_regs *regs) |
| 35 | { |
| 36 | efi_print_image_infos((void *)regs->elr); |
| 37 | } |
| 38 | |
Pavel Skripkin | 23c722d | 2023-04-02 19:27:34 +0300 | [diff] [blame] | 39 | static void dump_far(unsigned long esr) |
| 40 | { |
| 41 | unsigned long el, far; |
| 42 | |
| 43 | switch ((esr >> 26) & 0b111111) { |
| 44 | case 0x20: |
| 45 | case 0x21: |
| 46 | case 0x24: |
| 47 | case 0x25: |
| 48 | case 0x22: |
| 49 | case 0x34: |
| 50 | case 0x35: |
| 51 | break; |
| 52 | default: |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | asm("mrs %0, CurrentEl": "=r" (el)); |
| 57 | |
| 58 | switch (el >> 2) { |
| 59 | case 1: |
| 60 | asm("mrs %0, FAR_EL1": "=r" (far)); |
| 61 | break; |
| 62 | case 2: |
| 63 | asm("mrs %0, FAR_EL2": "=r" (far)); |
| 64 | break; |
| 65 | default: |
| 66 | /* don't print anything to make output pretty */ |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | printf(", far 0x%lx", far); |
| 71 | } |
| 72 | |
Heinrich Schuchardt | 11efc1a | 2019-09-12 19:09:26 +0200 | [diff] [blame] | 73 | static void dump_instr(struct pt_regs *regs) |
| 74 | { |
| 75 | u32 *addr = (u32 *)(regs->elr & ~3UL); |
| 76 | int i; |
| 77 | |
| 78 | printf("Code: "); |
| 79 | for (i = -4; i < 1; i++) |
| 80 | printf(i == 0 ? "(%08x) " : "%08x ", addr[i]); |
| 81 | printf("\n"); |
| 82 | } |
| 83 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 84 | void show_regs(struct pt_regs *regs) |
| 85 | { |
| 86 | int i; |
| 87 | |
Karl Beldan | b33ccd1 | 2018-02-20 23:30:08 +0000 | [diff] [blame] | 88 | if (gd->flags & GD_FLG_RELOC) |
| 89 | printf("elr: %016lx lr : %016lx (reloc)\n", |
| 90 | regs->elr - gd->reloc_off, |
| 91 | regs->regs[30] - gd->reloc_off); |
| 92 | printf("elr: %016lx lr : %016lx\n", regs->elr, regs->regs[30]); |
| 93 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 94 | for (i = 0; i < 29; i += 2) |
| 95 | printf("x%-2d: %016lx x%-2d: %016lx\n", |
| 96 | i, regs->regs[i], i+1, regs->regs[i+1]); |
| 97 | printf("\n"); |
Heinrich Schuchardt | 11efc1a | 2019-09-12 19:09:26 +0200 | [diff] [blame] | 98 | dump_instr(regs); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
Sean Anderson | 064c857 | 2022-03-22 16:59:31 -0400 | [diff] [blame] | 102 | * Try to "emulate" a semihosting call in the event that we don't have a |
| 103 | * debugger attached. |
| 104 | */ |
| 105 | static bool smh_emulate_trap(struct pt_regs *regs) |
| 106 | { |
| 107 | int size; |
| 108 | |
| 109 | if (ESR_ELx_EC(regs->esr) != ESR_ELx_EC_UNKNOWN) |
| 110 | return false; |
| 111 | |
| 112 | if (regs->spsr & PSR_MODE32_BIT) { |
| 113 | if (regs->spsr & PSR_AA32_T_BIT) { |
| 114 | u16 *insn = (u16 *)ALIGN_DOWN(regs->elr, 2); |
| 115 | |
| 116 | if (*insn != SMH_T32_SVC && *insn != SMH_T32_HLT) |
| 117 | return false; |
| 118 | size = 2; |
| 119 | } else { |
| 120 | u32 *insn = (u32 *)ALIGN_DOWN(regs->elr, 4); |
| 121 | |
| 122 | if (*insn != SMH_A32_SVC && *insn != SMH_A32_HLT) |
| 123 | return false; |
| 124 | size = 4; |
| 125 | } |
| 126 | } else { |
| 127 | u32 *insn = (u32 *)ALIGN_DOWN(regs->elr, 4); |
| 128 | |
| 129 | if (*insn != SMH_A64_HLT) |
| 130 | return false; |
| 131 | size = 4; |
| 132 | } |
| 133 | |
| 134 | /* Avoid future semihosting calls */ |
| 135 | disable_semihosting(); |
| 136 | |
| 137 | /* Just pretend the call failed */ |
| 138 | regs->regs[0] = -1; |
| 139 | regs->elr += size; |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | /* |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 144 | * do_bad_sync handles the impossible case in the Synchronous Abort vector. |
| 145 | */ |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 146 | void do_bad_sync(struct pt_regs *pt_regs) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 147 | { |
Alexander Graf | 5817786 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 148 | efi_restore_gd(); |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 149 | printf("Bad mode in \"Synchronous Abort\" handler, esr 0x%08lx\n", |
| 150 | pt_regs->esr); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 151 | show_regs(pt_regs); |
Heinrich Schuchardt | b7c0c38 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 152 | show_efi_loaded_images(pt_regs); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 153 | panic("Resetting CPU ...\n"); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * do_bad_irq handles the impossible case in the Irq vector. |
| 158 | */ |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 159 | void do_bad_irq(struct pt_regs *pt_regs) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 160 | { |
Alexander Graf | 5817786 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 161 | efi_restore_gd(); |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 162 | printf("Bad mode in \"Irq\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 163 | show_regs(pt_regs); |
Heinrich Schuchardt | b7c0c38 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 164 | show_efi_loaded_images(pt_regs); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 165 | panic("Resetting CPU ...\n"); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * do_bad_fiq handles the impossible case in the Fiq vector. |
| 170 | */ |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 171 | void do_bad_fiq(struct pt_regs *pt_regs) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 172 | { |
Alexander Graf | 5817786 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 173 | efi_restore_gd(); |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 174 | printf("Bad mode in \"Fiq\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 175 | show_regs(pt_regs); |
Heinrich Schuchardt | b7c0c38 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 176 | show_efi_loaded_images(pt_regs); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 177 | panic("Resetting CPU ...\n"); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * do_bad_error handles the impossible case in the Error vector. |
| 182 | */ |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 183 | void do_bad_error(struct pt_regs *pt_regs) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 184 | { |
Alexander Graf | 5817786 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 185 | efi_restore_gd(); |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 186 | printf("Bad mode in \"Error\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 187 | show_regs(pt_regs); |
Heinrich Schuchardt | b7c0c38 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 188 | show_efi_loaded_images(pt_regs); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 189 | panic("Resetting CPU ...\n"); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * do_sync handles the Synchronous Abort exception. |
| 194 | */ |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 195 | void do_sync(struct pt_regs *pt_regs) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 196 | { |
Sean Anderson | 064c857 | 2022-03-22 16:59:31 -0400 | [diff] [blame] | 197 | if (CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK) && |
| 198 | smh_emulate_trap(pt_regs)) |
| 199 | return; |
Alexander Graf | 5817786 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 200 | efi_restore_gd(); |
Pavel Skripkin | 23c722d | 2023-04-02 19:27:34 +0300 | [diff] [blame] | 201 | printf("\"Synchronous Abort\" handler, esr 0x%08lx", pt_regs->esr); |
| 202 | dump_far(pt_regs->esr); |
| 203 | printf("\n"); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 204 | show_regs(pt_regs); |
Heinrich Schuchardt | b7c0c38 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 205 | show_efi_loaded_images(pt_regs); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 206 | panic("Resetting CPU ...\n"); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * do_irq handles the Irq exception. |
| 211 | */ |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 212 | void do_irq(struct pt_regs *pt_regs) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 213 | { |
Alexander Graf | 5817786 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 214 | efi_restore_gd(); |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 215 | printf("\"Irq\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 216 | show_regs(pt_regs); |
Heinrich Schuchardt | b7c0c38 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 217 | show_efi_loaded_images(pt_regs); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 218 | panic("Resetting CPU ...\n"); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * do_fiq handles the Fiq exception. |
| 223 | */ |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 224 | void do_fiq(struct pt_regs *pt_regs) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 225 | { |
Alexander Graf | 5817786 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 226 | efi_restore_gd(); |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 227 | printf("\"Fiq\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 228 | show_regs(pt_regs); |
Heinrich Schuchardt | b7c0c38 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 229 | show_efi_loaded_images(pt_regs); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 230 | panic("Resetting CPU ...\n"); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * do_error handles the Error exception. |
| 235 | * Errors are more likely to be processor specific, |
| 236 | * it is defined with weak attribute and can be redefined |
| 237 | * in processor specific code. |
| 238 | */ |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 239 | void __weak do_error(struct pt_regs *pt_regs) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 240 | { |
Alexander Graf | 5817786 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 241 | efi_restore_gd(); |
Sean Anderson | 2d75549 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 242 | printf("\"Error\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 243 | show_regs(pt_regs); |
Heinrich Schuchardt | b7c0c38 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 244 | show_efi_loaded_images(pt_regs); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 245 | panic("Resetting CPU ...\n"); |
| 246 | } |