1a43d431bSSoby Mathew/* 2c424b91eSImre Kis * Copyright (c) 2014-2019, ARM Limited and Contributors. All rights reserved. 3a43d431bSSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5a43d431bSSoby Mathew */ 609d40e0eSAntonio Nino Diaz 709d40e0eSAntonio Nino Diaz#include <plat_macros.S> 809d40e0eSAntonio Nino Diaz#include <platform_def.h> 909d40e0eSAntonio Nino Diaz 10a43d431bSSoby Mathew#include <arch.h> 11a43d431bSSoby Mathew#include <asm_macros.S> 12a43d431bSSoby Mathew#include <context.h> 1309d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h> 1409d40e0eSAntonio Nino Diaz#include <lib/utils_def.h> 15a43d431bSSoby Mathew 16626ed510SSoby Mathew .globl report_unhandled_exception 17626ed510SSoby Mathew .globl report_unhandled_interrupt 18626ed510SSoby Mathew .globl el3_panic 19a43d431bSSoby Mathew 209c22b323SAndrew Thoelke#if CRASH_REPORTING 21626ed510SSoby Mathew 22a43d431bSSoby Mathew /* ------------------------------------------------------ 23a43d431bSSoby Mathew * The below section deals with dumping the system state 24a43d431bSSoby Mathew * when an unhandled exception is taken in EL3. 25a43d431bSSoby Mathew * The layout and the names of the registers which will 26a43d431bSSoby Mathew * be dumped during a unhandled exception is given below. 27a43d431bSSoby Mathew * ------------------------------------------------------ 28a43d431bSSoby Mathew */ 29626ed510SSoby Mathew.section .rodata.crash_prints, "aS" 30626ed510SSoby Mathewprint_spacer: 316c6a470fSAlexei Fedorov .asciz " = 0x" 32a43d431bSSoby Mathew 33626ed510SSoby Mathewgp_regs: 34626ed510SSoby Mathew .asciz "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",\ 35626ed510SSoby Mathew "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",\ 36626ed510SSoby Mathew "x16", "x17", "x18", "x19", "x20", "x21", "x22",\ 37626ed510SSoby Mathew "x23", "x24", "x25", "x26", "x27", "x28", "x29", "" 38626ed510SSoby Mathewel3_sys_regs: 39626ed510SSoby Mathew .asciz "scr_el3", "sctlr_el3", "cptr_el3", "tcr_el3",\ 40626ed510SSoby Mathew "daif", "mair_el3", "spsr_el3", "elr_el3", "ttbr0_el3",\ 41626ed510SSoby Mathew "esr_el3", "far_el3", "" 42a43d431bSSoby Mathew 43626ed510SSoby Mathewnon_el3_sys_regs: 44626ed510SSoby Mathew .asciz "spsr_el1", "elr_el1", "spsr_abt", "spsr_und",\ 45a43d431bSSoby Mathew "spsr_irq", "spsr_fiq", "sctlr_el1", "actlr_el1", "cpacr_el1",\ 46a43d431bSSoby Mathew "csselr_el1", "sp_el1", "esr_el1", "ttbr0_el1", "ttbr1_el1",\ 47626ed510SSoby Mathew "mair_el1", "amair_el1", "tcr_el1", "tpidr_el1", "tpidr_el0",\ 48c424b91eSImre Kis "tpidrro_el0", "par_el1", "mpidr_el1", "afsr0_el1", "afsr1_el1",\ 49c424b91eSImre Kis "contextidr_el1", "vbar_el1", "cntp_ctl_el0", "cntp_cval_el0",\ 50c424b91eSImre Kis "cntv_ctl_el0", "cntv_cval_el0", "cntkctl_el1", "sp_el0", "isr_el1", "" 51c424b91eSImre Kis 52c424b91eSImre Kis#if CTX_INCLUDE_AARCH32_REGS 53c424b91eSImre Kisaarch32_regs: 54c424b91eSImre Kis .asciz "dacr32_el2", "ifsr32_el2", "" 55c424b91eSImre Kis#endif /* CTX_INCLUDE_AARCH32_REGS */ 56a43d431bSSoby Mathew 57626ed510SSoby Mathewpanic_msg: 586c6a470fSAlexei Fedorov .asciz "PANIC in EL3.\nx30" 59626ed510SSoby Mathewexcpt_msg: 606c6a470fSAlexei Fedorov .asciz "Unhandled Exception in EL3.\nx30" 61626ed510SSoby Mathewintr_excpt_msg: 626c6a470fSAlexei Fedorov .asciz "Unhandled Interrupt Exception in EL3.\nx30" 63626ed510SSoby Mathew 64626ed510SSoby Mathew /* 65626ed510SSoby Mathew * Helper function to print from crash buf. 66626ed510SSoby Mathew * The print loop is controlled by the buf size and 67626ed510SSoby Mathew * ascii reg name list which is passed in x6. The 68626ed510SSoby Mathew * function returns the crash buf address in x0. 69626ed510SSoby Mathew * Clobbers : x0 - x7, sp 70626ed510SSoby Mathew */ 71626ed510SSoby Mathewfunc size_controlled_print 72626ed510SSoby Mathew /* Save the lr */ 73626ed510SSoby Mathew mov sp, x30 74626ed510SSoby Mathew /* load the crash buf address */ 75626ed510SSoby Mathew mrs x7, tpidr_el3 76626ed510SSoby Mathewtest_size_list: 77626ed510SSoby Mathew /* Calculate x5 always as it will be clobbered by asm_print_hex */ 78626ed510SSoby Mathew mrs x5, tpidr_el3 79626ed510SSoby Mathew add x5, x5, #CPU_DATA_CRASH_BUF_SIZE 80626ed510SSoby Mathew /* Test whether we have reached end of crash buf */ 81626ed510SSoby Mathew cmp x7, x5 82626ed510SSoby Mathew b.eq exit_size_print 83626ed510SSoby Mathew ldrb w4, [x6] 84626ed510SSoby Mathew /* Test whether we are at end of list */ 85626ed510SSoby Mathew cbz w4, exit_size_print 86626ed510SSoby Mathew mov x4, x6 87626ed510SSoby Mathew /* asm_print_str updates x4 to point to next entry in list */ 88626ed510SSoby Mathew bl asm_print_str 896c6a470fSAlexei Fedorov /* x0 = number of symbols printed + 1 */ 906c6a470fSAlexei Fedorov sub x0, x4, x6 91626ed510SSoby Mathew /* update x6 with the updated list pointer */ 92626ed510SSoby Mathew mov x6, x4 936c6a470fSAlexei Fedorov bl print_alignment 94155a1006SJulius Werner ldr x4, [x7], #REGSZ 95626ed510SSoby Mathew bl asm_print_hex 96*53d7e003SJustin Chadwell bl asm_print_newline 97626ed510SSoby Mathew b test_size_list 98626ed510SSoby Mathewexit_size_print: 99626ed510SSoby Mathew mov x30, sp 100626ed510SSoby Mathew ret 1018b779620SKévin Petitendfunc size_controlled_print 102626ed510SSoby Mathew 1036c6a470fSAlexei Fedorov /* ----------------------------------------------------- 1046c6a470fSAlexei Fedorov * This function calculates and prints required number 1056c6a470fSAlexei Fedorov * of space characters followed by "= 0x", based on the 1066c6a470fSAlexei Fedorov * length of ascii register name. 1076c6a470fSAlexei Fedorov * x0: length of ascii register name + 1 1086c6a470fSAlexei Fedorov * ------------------------------------------------------ 1096c6a470fSAlexei Fedorov */ 1106c6a470fSAlexei Fedorovfunc print_alignment 1116c6a470fSAlexei Fedorov /* The minimum ascii length is 3, e.g. for "x0" */ 1126c6a470fSAlexei Fedorov adr x4, print_spacer - 3 1136c6a470fSAlexei Fedorov add x4, x4, x0 1146c6a470fSAlexei Fedorov b asm_print_str 1156c6a470fSAlexei Fedorovendfunc print_alignment 1166c6a470fSAlexei Fedorov 117626ed510SSoby Mathew /* 118626ed510SSoby Mathew * Helper function to store x8 - x15 registers to 119626ed510SSoby Mathew * the crash buf. The system registers values are 120626ed510SSoby Mathew * copied to x8 to x15 by the caller which are then 121626ed510SSoby Mathew * copied to the crash buf by this function. 122626ed510SSoby Mathew * x0 points to the crash buf. It then calls 123626ed510SSoby Mathew * size_controlled_print to print to console. 124626ed510SSoby Mathew * Clobbers : x0 - x7, sp 125626ed510SSoby Mathew */ 126626ed510SSoby Mathewfunc str_in_crash_buf_print 127626ed510SSoby Mathew /* restore the crash buf address in x0 */ 128626ed510SSoby Mathew mrs x0, tpidr_el3 129626ed510SSoby Mathew stp x8, x9, [x0] 130155a1006SJulius Werner stp x10, x11, [x0, #REGSZ * 2] 131155a1006SJulius Werner stp x12, x13, [x0, #REGSZ * 4] 132155a1006SJulius Werner stp x14, x15, [x0, #REGSZ * 6] 133626ed510SSoby Mathew b size_controlled_print 1348b779620SKévin Petitendfunc str_in_crash_buf_print 135626ed510SSoby Mathew 136626ed510SSoby Mathew /* ------------------------------------------------------ 137626ed510SSoby Mathew * This macro calculates the offset to crash buf from 138626ed510SSoby Mathew * cpu_data and stores it in tpidr_el3. It also saves x0 139626ed510SSoby Mathew * and x1 in the crash buf by using sp as a temporary 140626ed510SSoby Mathew * register. 141626ed510SSoby Mathew * ------------------------------------------------------ 142626ed510SSoby Mathew */ 143626ed510SSoby Mathew .macro prepare_crash_buf_save_x0_x1 144626ed510SSoby Mathew /* we can corrupt this reg to free up x0 */ 145626ed510SSoby Mathew mov sp, x0 146626ed510SSoby Mathew /* tpidr_el3 contains the address to cpu_data structure */ 147626ed510SSoby Mathew mrs x0, tpidr_el3 148626ed510SSoby Mathew /* Calculate the Crash buffer offset in cpu_data */ 149626ed510SSoby Mathew add x0, x0, #CPU_DATA_CRASH_BUF_OFFSET 150626ed510SSoby Mathew /* Store crash buffer address in tpidr_el3 */ 151626ed510SSoby Mathew msr tpidr_el3, x0 152155a1006SJulius Werner str x1, [x0, #REGSZ] 153626ed510SSoby Mathew mov x1, sp 154626ed510SSoby Mathew str x1, [x0] 155626ed510SSoby Mathew .endm 156a43d431bSSoby Mathew 157a43d431bSSoby Mathew /* ----------------------------------------------------- 158626ed510SSoby Mathew * This function allows to report a crash (if crash 159626ed510SSoby Mathew * reporting is enabled) when an unhandled exception 160626ed510SSoby Mathew * occurs. It prints the CPU state via the crash console 161626ed510SSoby Mathew * making use of the crash buf. This function will 162626ed510SSoby Mathew * not return. 163a43d431bSSoby Mathew * ----------------------------------------------------- 164a43d431bSSoby Mathew */ 165626ed510SSoby Mathewfunc report_unhandled_exception 166626ed510SSoby Mathew prepare_crash_buf_save_x0_x1 167626ed510SSoby Mathew adr x0, excpt_msg 168626ed510SSoby Mathew mov sp, x0 169626ed510SSoby Mathew /* This call will not return */ 170626ed510SSoby Mathew b do_crash_reporting 1718b779620SKévin Petitendfunc report_unhandled_exception 172a43d431bSSoby Mathew 173a43d431bSSoby Mathew 174626ed510SSoby Mathew /* ----------------------------------------------------- 175626ed510SSoby Mathew * This function allows to report a crash (if crash 176626ed510SSoby Mathew * reporting is enabled) when an unhandled interrupt 177626ed510SSoby Mathew * occurs. It prints the CPU state via the crash console 178626ed510SSoby Mathew * making use of the crash buf. This function will 179626ed510SSoby Mathew * not return. 180626ed510SSoby Mathew * ----------------------------------------------------- 181626ed510SSoby Mathew */ 182626ed510SSoby Mathewfunc report_unhandled_interrupt 183626ed510SSoby Mathew prepare_crash_buf_save_x0_x1 184626ed510SSoby Mathew adr x0, intr_excpt_msg 185626ed510SSoby Mathew mov sp, x0 186626ed510SSoby Mathew /* This call will not return */ 187626ed510SSoby Mathew b do_crash_reporting 1888b779620SKévin Petitendfunc report_unhandled_interrupt 189a43d431bSSoby Mathew 190626ed510SSoby Mathew /* ----------------------------------------------------- 191626ed510SSoby Mathew * This function allows to report a crash (if crash 192626ed510SSoby Mathew * reporting is enabled) when panic() is invoked from 193626ed510SSoby Mathew * C Runtime. It prints the CPU state via the crash 194626ed510SSoby Mathew * console making use of the crash buf. This function 195626ed510SSoby Mathew * will not return. 196626ed510SSoby Mathew * ----------------------------------------------------- 197626ed510SSoby Mathew */ 198626ed510SSoby Mathewfunc el3_panic 1996c6a470fSAlexei Fedorov msr spsel, #MODE_SP_ELX 200626ed510SSoby Mathew prepare_crash_buf_save_x0_x1 201626ed510SSoby Mathew adr x0, panic_msg 202626ed510SSoby Mathew mov sp, x0 203626ed510SSoby Mathew /* This call will not return */ 204626ed510SSoby Mathew b do_crash_reporting 2058b779620SKévin Petitendfunc el3_panic 206a43d431bSSoby Mathew 207626ed510SSoby Mathew /* ------------------------------------------------------------ 208626ed510SSoby Mathew * The common crash reporting functionality. It requires x0 209626ed510SSoby Mathew * and x1 has already been stored in crash buf, sp points to 210626ed510SSoby Mathew * crash message and tpidr_el3 contains the crash buf address. 211626ed510SSoby Mathew * The function does the following: 212626ed510SSoby Mathew * - Retrieve the crash buffer from tpidr_el3 213626ed510SSoby Mathew * - Store x2 to x6 in the crash buffer 214626ed510SSoby Mathew * - Initialise the crash console. 215626ed510SSoby Mathew * - Print the crash message by using the address in sp. 216626ed510SSoby Mathew * - Print x30 value to the crash console. 217626ed510SSoby Mathew * - Print x0 - x7 from the crash buf to the crash console. 218626ed510SSoby Mathew * - Print x8 - x29 (in groups of 8 registers) using the 219626ed510SSoby Mathew * crash buf to the crash console. 220626ed510SSoby Mathew * - Print el3 sys regs (in groups of 8 registers) using the 221626ed510SSoby Mathew * crash buf to the crash console. 222626ed510SSoby Mathew * - Print non el3 sys regs (in groups of 8 registers) using 223626ed510SSoby Mathew * the crash buf to the crash console. 224626ed510SSoby Mathew * ------------------------------------------------------------ 225626ed510SSoby Mathew */ 226626ed510SSoby Mathewfunc do_crash_reporting 227626ed510SSoby Mathew /* Retrieve the crash buf from tpidr_el3 */ 228626ed510SSoby Mathew mrs x0, tpidr_el3 229626ed510SSoby Mathew /* Store x2 - x6, x30 in the crash buffer */ 230155a1006SJulius Werner stp x2, x3, [x0, #REGSZ * 2] 231155a1006SJulius Werner stp x4, x5, [x0, #REGSZ * 4] 232155a1006SJulius Werner stp x6, x30, [x0, #REGSZ * 6] 233626ed510SSoby Mathew /* Initialize the crash console */ 234626ed510SSoby Mathew bl plat_crash_console_init 235626ed510SSoby Mathew /* Verify the console is initialized */ 236626ed510SSoby Mathew cbz x0, crash_panic 237626ed510SSoby Mathew /* Print the crash message. sp points to the crash message */ 238626ed510SSoby Mathew mov x4, sp 239626ed510SSoby Mathew bl asm_print_str 2406c6a470fSAlexei Fedorov /* Print spaces to align "x30" string */ 2416c6a470fSAlexei Fedorov mov x0, #4 2426c6a470fSAlexei Fedorov bl print_alignment 243626ed510SSoby Mathew /* load the crash buf address */ 244626ed510SSoby Mathew mrs x0, tpidr_el3 245626ed510SSoby Mathew /* report x30 first from the crash buf */ 246155a1006SJulius Werner ldr x4, [x0, #REGSZ * 7] 247626ed510SSoby Mathew bl asm_print_hex 248*53d7e003SJustin Chadwell bl asm_print_newline 249626ed510SSoby Mathew /* Load the crash buf address */ 250626ed510SSoby Mathew mrs x0, tpidr_el3 251626ed510SSoby Mathew /* Now mov x7 into crash buf */ 252155a1006SJulius Werner str x7, [x0, #REGSZ * 7] 253a43d431bSSoby Mathew 254626ed510SSoby Mathew /* Report x0 - x29 values stored in crash buf*/ 255626ed510SSoby Mathew /* Store the ascii list pointer in x6 */ 256626ed510SSoby Mathew adr x6, gp_regs 257626ed510SSoby Mathew /* Print x0 to x7 from the crash buf */ 258626ed510SSoby Mathew bl size_controlled_print 259626ed510SSoby Mathew /* Store x8 - x15 in crash buf and print */ 260626ed510SSoby Mathew bl str_in_crash_buf_print 261626ed510SSoby Mathew /* Load the crash buf address */ 262626ed510SSoby Mathew mrs x0, tpidr_el3 263626ed510SSoby Mathew /* Store the rest of gp regs and print */ 264626ed510SSoby Mathew stp x16, x17, [x0] 265155a1006SJulius Werner stp x18, x19, [x0, #REGSZ * 2] 266155a1006SJulius Werner stp x20, x21, [x0, #REGSZ * 4] 267155a1006SJulius Werner stp x22, x23, [x0, #REGSZ * 6] 268626ed510SSoby Mathew bl size_controlled_print 269626ed510SSoby Mathew /* Load the crash buf address */ 270626ed510SSoby Mathew mrs x0, tpidr_el3 271626ed510SSoby Mathew stp x24, x25, [x0] 272155a1006SJulius Werner stp x26, x27, [x0, #REGSZ * 2] 273155a1006SJulius Werner stp x28, x29, [x0, #REGSZ * 4] 274626ed510SSoby Mathew bl size_controlled_print 275a43d431bSSoby Mathew 276626ed510SSoby Mathew /* Print the el3 sys registers */ 277626ed510SSoby Mathew adr x6, el3_sys_regs 278626ed510SSoby Mathew mrs x8, scr_el3 279626ed510SSoby Mathew mrs x9, sctlr_el3 280626ed510SSoby Mathew mrs x10, cptr_el3 281626ed510SSoby Mathew mrs x11, tcr_el3 282626ed510SSoby Mathew mrs x12, daif 283626ed510SSoby Mathew mrs x13, mair_el3 284626ed510SSoby Mathew mrs x14, spsr_el3 285626ed510SSoby Mathew mrs x15, elr_el3 286626ed510SSoby Mathew bl str_in_crash_buf_print 287626ed510SSoby Mathew mrs x8, ttbr0_el3 288626ed510SSoby Mathew mrs x9, esr_el3 289626ed510SSoby Mathew mrs x10, far_el3 290626ed510SSoby Mathew bl str_in_crash_buf_print 291a43d431bSSoby Mathew 292626ed510SSoby Mathew /* Print the non el3 sys registers */ 293626ed510SSoby Mathew adr x6, non_el3_sys_regs 294626ed510SSoby Mathew mrs x8, spsr_el1 295626ed510SSoby Mathew mrs x9, elr_el1 296626ed510SSoby Mathew mrs x10, spsr_abt 297626ed510SSoby Mathew mrs x11, spsr_und 298626ed510SSoby Mathew mrs x12, spsr_irq 299626ed510SSoby Mathew mrs x13, spsr_fiq 300626ed510SSoby Mathew mrs x14, sctlr_el1 301626ed510SSoby Mathew mrs x15, actlr_el1 302626ed510SSoby Mathew bl str_in_crash_buf_print 303626ed510SSoby Mathew mrs x8, cpacr_el1 304626ed510SSoby Mathew mrs x9, csselr_el1 305a43d431bSSoby Mathew mrs x10, sp_el1 306a43d431bSSoby Mathew mrs x11, esr_el1 307a43d431bSSoby Mathew mrs x12, ttbr0_el1 308a43d431bSSoby Mathew mrs x13, ttbr1_el1 309a43d431bSSoby Mathew mrs x14, mair_el1 310a43d431bSSoby Mathew mrs x15, amair_el1 311626ed510SSoby Mathew bl str_in_crash_buf_print 312626ed510SSoby Mathew mrs x8, tcr_el1 313626ed510SSoby Mathew mrs x9, tpidr_el1 314626ed510SSoby Mathew mrs x10, tpidr_el0 315626ed510SSoby Mathew mrs x11, tpidrro_el0 316c424b91eSImre Kis mrs x12, par_el1 317c424b91eSImre Kis mrs x13, mpidr_el1 318c424b91eSImre Kis mrs x14, afsr0_el1 319c424b91eSImre Kis mrs x15, afsr1_el1 320626ed510SSoby Mathew bl str_in_crash_buf_print 321c424b91eSImre Kis mrs x8, contextidr_el1 322c424b91eSImre Kis mrs x9, vbar_el1 323c424b91eSImre Kis mrs x10, cntp_ctl_el0 324c424b91eSImre Kis mrs x11, cntp_cval_el0 325c424b91eSImre Kis mrs x12, cntv_ctl_el0 326c424b91eSImre Kis mrs x13, cntv_cval_el0 327c424b91eSImre Kis mrs x14, cntkctl_el1 328c424b91eSImre Kis mrs x15, sp_el0 329626ed510SSoby Mathew bl str_in_crash_buf_print 330c424b91eSImre Kis mrs x8, isr_el1 331626ed510SSoby Mathew bl str_in_crash_buf_print 332a43d431bSSoby Mathew 333c424b91eSImre Kis#if CTX_INCLUDE_AARCH32_REGS 334c424b91eSImre Kis /* Print the AArch32 registers */ 335c424b91eSImre Kis adr x6, aarch32_regs 336c424b91eSImre Kis mrs x8, dacr32_el2 337c424b91eSImre Kis mrs x9, ifsr32_el2 338c424b91eSImre Kis bl str_in_crash_buf_print 339c424b91eSImre Kis#endif /* CTX_INCLUDE_AARCH32_REGS */ 340c424b91eSImre Kis 341d3f70af6SSoby Mathew /* Get the cpu specific registers to report */ 342d3f70af6SSoby Mathew bl do_cpu_reg_dump 343d3f70af6SSoby Mathew bl str_in_crash_buf_print 3448c106902SSoby Mathew 3459ff67fa6SGerald Lejeune /* Print some platform registers */ 3469ff67fa6SGerald Lejeune plat_crash_print_regs 3478c106902SSoby Mathew 348801cf93cSAntonio Nino Diaz bl plat_crash_console_flush 349801cf93cSAntonio Nino Diaz 350626ed510SSoby Mathew /* Done reporting */ 351a806dad5SJeenu Viswambharan no_ret plat_panic_handler 3528b779620SKévin Petitendfunc do_crash_reporting 353a43d431bSSoby Mathew 354626ed510SSoby Mathew#else /* CRASH_REPORTING */ 355626ed510SSoby Mathewfunc report_unhandled_exception 356626ed510SSoby Mathewreport_unhandled_interrupt: 357a806dad5SJeenu Viswambharan no_ret plat_panic_handler 3588b779620SKévin Petitendfunc report_unhandled_exception 3591645d3eeSSandrine Bailleux#endif /* CRASH_REPORTING */ 3609c22b323SAndrew Thoelke 361a43d431bSSoby Mathew 362626ed510SSoby Mathewfunc crash_panic 363a806dad5SJeenu Viswambharan no_ret plat_panic_handler 3648b779620SKévin Petitendfunc crash_panic 365