1/* 2 * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * Redistributions of source code must retain the above copyright notice, this 8 * list of conditions and the following disclaimer. 9 * 10 * Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * Neither the name of ARM nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific 16 * prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30#include <arch.h> 31#include <asm_macros.S> 32#include <context.h> 33#include <cpu_data.h> 34#include <plat_macros.S> 35#include <platform_def.h> 36 37 .globl report_unhandled_exception 38 .globl report_unhandled_interrupt 39 .globl el3_panic 40 41#if CRASH_REPORTING 42#define REG_SIZE 0x8 43 44 /* ------------------------------------------------------ 45 * The below section deals with dumping the system state 46 * when an unhandled exception is taken in EL3. 47 * The layout and the names of the registers which will 48 * be dumped during a unhandled exception is given below. 49 * ------------------------------------------------------ 50 */ 51.section .rodata.crash_prints, "aS" 52print_spacer: 53 .asciz " =\t\t0x" 54 55cpu_ectlr_reg: 56 .asciz "cpuectlr_el1 =\t\t0x" 57 58gp_regs: 59 .asciz "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",\ 60 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",\ 61 "x16", "x17", "x18", "x19", "x20", "x21", "x22",\ 62 "x23", "x24", "x25", "x26", "x27", "x28", "x29", "" 63el3_sys_regs: 64 .asciz "scr_el3", "sctlr_el3", "cptr_el3", "tcr_el3",\ 65 "daif", "mair_el3", "spsr_el3", "elr_el3", "ttbr0_el3",\ 66 "esr_el3", "far_el3", "" 67 68non_el3_sys_regs: 69 .asciz "spsr_el1", "elr_el1", "spsr_abt", "spsr_und",\ 70 "spsr_irq", "spsr_fiq", "sctlr_el1", "actlr_el1", "cpacr_el1",\ 71 "csselr_el1", "sp_el1", "esr_el1", "ttbr0_el1", "ttbr1_el1",\ 72 "mair_el1", "amair_el1", "tcr_el1", "tpidr_el1", "tpidr_el0",\ 73 "tpidrro_el0", "dacr32_el2", "ifsr32_el2", "par_el1",\ 74 "mpidr_el1", "afsr0_el1", "afsr1_el1", "contextidr_el1",\ 75 "vbar_el1", "cntp_ctl_el0", "cntp_cval_el0", "cntv_ctl_el0",\ 76 "cntv_cval_el0", "cntkctl_el1", "fpexc32_el2", "sp_el0", "" 77 78panic_msg: 79 .asciz "PANIC in EL3 at x30 = 0x" 80excpt_msg: 81 .asciz "Unhandled Exception in EL3.\nx30 =\t\t0x" 82intr_excpt_msg: 83 .asciz "Unhandled Interrupt Exception in EL3.\nx30 =\t\t0x" 84 85 /* 86 * Helper function to print newline to console. 87 */ 88func print_newline 89 mov x0, '\n' 90 b plat_crash_console_putc 91 92 /* 93 * Helper function to print from crash buf. 94 * The print loop is controlled by the buf size and 95 * ascii reg name list which is passed in x6. The 96 * function returns the crash buf address in x0. 97 * Clobbers : x0 - x7, sp 98 */ 99func size_controlled_print 100 /* Save the lr */ 101 mov sp, x30 102 /* load the crash buf address */ 103 mrs x7, tpidr_el3 104test_size_list: 105 /* Calculate x5 always as it will be clobbered by asm_print_hex */ 106 mrs x5, tpidr_el3 107 add x5, x5, #CPU_DATA_CRASH_BUF_SIZE 108 /* Test whether we have reached end of crash buf */ 109 cmp x7, x5 110 b.eq exit_size_print 111 ldrb w4, [x6] 112 /* Test whether we are at end of list */ 113 cbz w4, exit_size_print 114 mov x4, x6 115 /* asm_print_str updates x4 to point to next entry in list */ 116 bl asm_print_str 117 /* update x6 with the updated list pointer */ 118 mov x6, x4 119 adr x4, print_spacer 120 bl asm_print_str 121 ldr x4, [x7], #REG_SIZE 122 bl asm_print_hex 123 bl print_newline 124 b test_size_list 125exit_size_print: 126 mov x30, sp 127 ret 128 129 /* 130 * Helper function to store x8 - x15 registers to 131 * the crash buf. The system registers values are 132 * copied to x8 to x15 by the caller which are then 133 * copied to the crash buf by this function. 134 * x0 points to the crash buf. It then calls 135 * size_controlled_print to print to console. 136 * Clobbers : x0 - x7, sp 137 */ 138func str_in_crash_buf_print 139 /* restore the crash buf address in x0 */ 140 mrs x0, tpidr_el3 141 stp x8, x9, [x0] 142 stp x10, x11, [x0, #REG_SIZE * 2] 143 stp x12, x13, [x0, #REG_SIZE * 4] 144 stp x14, x15, [x0, #REG_SIZE * 6] 145 b size_controlled_print 146 147 /* ------------------------------------------------------ 148 * This macro calculates the offset to crash buf from 149 * cpu_data and stores it in tpidr_el3. It also saves x0 150 * and x1 in the crash buf by using sp as a temporary 151 * register. 152 * ------------------------------------------------------ 153 */ 154 .macro prepare_crash_buf_save_x0_x1 155 /* we can corrupt this reg to free up x0 */ 156 mov sp, x0 157 /* tpidr_el3 contains the address to cpu_data structure */ 158 mrs x0, tpidr_el3 159 /* Calculate the Crash buffer offset in cpu_data */ 160 add x0, x0, #CPU_DATA_CRASH_BUF_OFFSET 161 /* Store crash buffer address in tpidr_el3 */ 162 msr tpidr_el3, x0 163 str x1, [x0, #REG_SIZE] 164 mov x1, sp 165 str x1, [x0] 166 .endm 167 168 /* ----------------------------------------------------- 169 * This function allows to report a crash (if crash 170 * reporting is enabled) when an unhandled exception 171 * occurs. It prints the CPU state via the crash console 172 * making use of the crash buf. This function will 173 * not return. 174 * ----------------------------------------------------- 175 */ 176func report_unhandled_exception 177 prepare_crash_buf_save_x0_x1 178 adr x0, excpt_msg 179 mov sp, x0 180 /* This call will not return */ 181 b do_crash_reporting 182 183 184 /* ----------------------------------------------------- 185 * This function allows to report a crash (if crash 186 * reporting is enabled) when an unhandled interrupt 187 * occurs. It prints the CPU state via the crash console 188 * making use of the crash buf. This function will 189 * not return. 190 * ----------------------------------------------------- 191 */ 192func report_unhandled_interrupt 193 prepare_crash_buf_save_x0_x1 194 adr x0, intr_excpt_msg 195 mov sp, x0 196 /* This call will not return */ 197 b do_crash_reporting 198 199 /* ----------------------------------------------------- 200 * This function allows to report a crash (if crash 201 * reporting is enabled) when panic() is invoked from 202 * C Runtime. It prints the CPU state via the crash 203 * console making use of the crash buf. This function 204 * will not return. 205 * ----------------------------------------------------- 206 */ 207func el3_panic 208 msr spsel, #1 209 prepare_crash_buf_save_x0_x1 210 adr x0, panic_msg 211 mov sp, x0 212 /* This call will not return */ 213 b do_crash_reporting 214 215 /* ------------------------------------------------------------ 216 * The common crash reporting functionality. It requires x0 217 * and x1 has already been stored in crash buf, sp points to 218 * crash message and tpidr_el3 contains the crash buf address. 219 * The function does the following: 220 * - Retrieve the crash buffer from tpidr_el3 221 * - Store x2 to x6 in the crash buffer 222 * - Initialise the crash console. 223 * - Print the crash message by using the address in sp. 224 * - Print x30 value to the crash console. 225 * - Print x0 - x7 from the crash buf to the crash console. 226 * - Print x8 - x29 (in groups of 8 registers) using the 227 * crash buf to the crash console. 228 * - Print el3 sys regs (in groups of 8 registers) using the 229 * crash buf to the crash console. 230 * - Print non el3 sys regs (in groups of 8 registers) using 231 * the crash buf to the crash console. 232 * ------------------------------------------------------------ 233 */ 234func do_crash_reporting 235 /* Retrieve the crash buf from tpidr_el3 */ 236 mrs x0, tpidr_el3 237 /* Store x2 - x6, x30 in the crash buffer */ 238 stp x2, x3, [x0, #REG_SIZE * 2] 239 stp x4, x5, [x0, #REG_SIZE * 4] 240 stp x6, x30, [x0, #REG_SIZE * 6] 241 /* Initialize the crash console */ 242 bl plat_crash_console_init 243 /* Verify the console is initialized */ 244 cbz x0, crash_panic 245 /* Print the crash message. sp points to the crash message */ 246 mov x4, sp 247 bl asm_print_str 248 /* load the crash buf address */ 249 mrs x0, tpidr_el3 250 /* report x30 first from the crash buf */ 251 ldr x4, [x0, #REG_SIZE * 7] 252 bl asm_print_hex 253 bl print_newline 254 /* Load the crash buf address */ 255 mrs x0, tpidr_el3 256 /* Now mov x7 into crash buf */ 257 str x7, [x0, #REG_SIZE * 7] 258 259 /* Report x0 - x29 values stored in crash buf*/ 260 /* Store the ascii list pointer in x6 */ 261 adr x6, gp_regs 262 /* Print x0 to x7 from the crash buf */ 263 bl size_controlled_print 264 /* Store x8 - x15 in crash buf and print */ 265 bl str_in_crash_buf_print 266 /* Load the crash buf address */ 267 mrs x0, tpidr_el3 268 /* Store the rest of gp regs and print */ 269 stp x16, x17, [x0] 270 stp x18, x19, [x0, #REG_SIZE * 2] 271 stp x20, x21, [x0, #REG_SIZE * 4] 272 stp x22, x23, [x0, #REG_SIZE * 6] 273 bl size_controlled_print 274 /* Load the crash buf address */ 275 mrs x0, tpidr_el3 276 stp x24, x25, [x0] 277 stp x26, x27, [x0, #REG_SIZE * 2] 278 stp x28, x29, [x0, #REG_SIZE * 4] 279 bl size_controlled_print 280 281 /* Print the el3 sys registers */ 282 adr x6, el3_sys_regs 283 mrs x8, scr_el3 284 mrs x9, sctlr_el3 285 mrs x10, cptr_el3 286 mrs x11, tcr_el3 287 mrs x12, daif 288 mrs x13, mair_el3 289 mrs x14, spsr_el3 290 mrs x15, elr_el3 291 bl str_in_crash_buf_print 292 mrs x8, ttbr0_el3 293 mrs x9, esr_el3 294 mrs x10, far_el3 295 bl str_in_crash_buf_print 296 297 /* Print the non el3 sys registers */ 298 adr x6, non_el3_sys_regs 299 mrs x8, spsr_el1 300 mrs x9, elr_el1 301 mrs x10, spsr_abt 302 mrs x11, spsr_und 303 mrs x12, spsr_irq 304 mrs x13, spsr_fiq 305 mrs x14, sctlr_el1 306 mrs x15, actlr_el1 307 bl str_in_crash_buf_print 308 mrs x8, cpacr_el1 309 mrs x9, csselr_el1 310 mrs x10, sp_el1 311 mrs x11, esr_el1 312 mrs x12, ttbr0_el1 313 mrs x13, ttbr1_el1 314 mrs x14, mair_el1 315 mrs x15, amair_el1 316 bl str_in_crash_buf_print 317 mrs x8, tcr_el1 318 mrs x9, tpidr_el1 319 mrs x10, tpidr_el0 320 mrs x11, tpidrro_el0 321 mrs x12, dacr32_el2 322 mrs x13, ifsr32_el2 323 mrs x14, par_el1 324 mrs x15, mpidr_el1 325 bl str_in_crash_buf_print 326 mrs x8, afsr0_el1 327 mrs x9, afsr1_el1 328 mrs x10, contextidr_el1 329 mrs x11, vbar_el1 330 mrs x12, cntp_ctl_el0 331 mrs x13, cntp_cval_el0 332 mrs x14, cntv_ctl_el0 333 mrs x15, cntv_cval_el0 334 bl str_in_crash_buf_print 335 mrs x8, cntkctl_el1 336 mrs x9, fpexc32_el2 337 mrs x10, sp_el0 338 bl str_in_crash_buf_print 339 340 /* Print the CPUECTLR_EL1 reg */ 341 mrs x0, midr_el1 342 lsr x0, x0, #MIDR_PN_SHIFT 343 and x0, x0, #MIDR_PN_MASK 344 cmp x0, #MIDR_PN_A57 345 b.eq 1f 346 cmp x0, #MIDR_PN_A53 347 b.ne 2f 3481: 349 adr x4, cpu_ectlr_reg 350 bl asm_print_str 351 mrs x4, CPUECTLR_EL1 352 bl asm_print_hex 353 bl print_newline 3542: 355 356 /* Print the gic registers */ 357 plat_print_gic_regs 358 359 /* Print the interconnect registers */ 360 plat_print_interconnect_regs 361 362 /* Done reporting */ 363 b crash_panic 364 365#else /* CRASH_REPORTING */ 366func report_unhandled_exception 367report_unhandled_interrupt: 368 b crash_panic 369#endif /* CRASH_REPORING */ 370 371 372func crash_panic 373 b crash_panic 374