Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 1 | /* |
Alexei Fedorov | 813c9f9 | 2020-03-03 13:31:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <asm_macros.S> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | #include <common/debug.h> |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 10 | |
| 11 | .globl asm_print_str |
| 12 | .globl asm_print_hex |
Alexei Fedorov | c4dfb3b | 2019-07-29 13:34:07 +0100 | [diff] [blame] | 13 | .globl asm_print_hex_bits |
Justin Chadwell | 7690382 | 2019-08-20 10:58:49 +0100 | [diff] [blame] | 14 | .globl asm_print_newline |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 15 | .globl asm_assert |
| 16 | .globl do_panic |
| 17 | |
| 18 | /* Since the max decimal input number is 65536 */ |
| 19 | #define MAX_DEC_DIVISOR 10000 |
| 20 | /* The offset to add to get ascii for numerals '0 - 9' */ |
| 21 | #define ASCII_OFFSET_NUM 0x30 |
| 22 | |
Antonio Nino Diaz | 7c65c1e | 2017-04-20 09:58:28 +0100 | [diff] [blame] | 23 | #if ENABLE_ASSERTIONS |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 24 | .section .rodata.assert_str, "aS" |
| 25 | assert_msg1: |
| 26 | .asciz "ASSERT: File " |
| 27 | assert_msg2: |
| 28 | .asciz " Line " |
| 29 | |
| 30 | /* |
| 31 | * This macro is intended to be used to print the |
| 32 | * line number in decimal. Used by asm_assert macro. |
| 33 | * The max number expected is 65536. |
| 34 | * In: x4 = the decimal to print. |
| 35 | * Clobber: x30, x0, x1, x2, x5, x6 |
| 36 | */ |
| 37 | .macro asm_print_line_dec |
| 38 | mov x6, #10 /* Divide by 10 after every loop iteration */ |
| 39 | mov x5, #MAX_DEC_DIVISOR |
Soby Mathew | 1218f11 | 2014-08-19 11:26:00 +0100 | [diff] [blame] | 40 | dec_print_loop: |
Yann Gautier | d04c7ae | 2020-09-17 15:15:27 +0200 | [diff] [blame] | 41 | udiv x0, x4, x5 /* Get the quotient */ |
| 42 | msub x4, x0, x5, x4 /* Find the remainder */ |
| 43 | add x0, x0, #ASCII_OFFSET_NUM /* Convert to ascii */ |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 44 | bl plat_crash_console_putc |
Yann Gautier | d04c7ae | 2020-09-17 15:15:27 +0200 | [diff] [blame] | 45 | udiv x5, x5, x6 /* Reduce divisor */ |
Soby Mathew | 1218f11 | 2014-08-19 11:26:00 +0100 | [diff] [blame] | 46 | cbnz x5, dec_print_loop |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 47 | .endm |
| 48 | |
| 49 | |
| 50 | /* --------------------------------------------------------------------------- |
| 51 | * Assertion support in assembly. |
| 52 | * The below function helps to support assertions in assembly where we do not |
| 53 | * have a C runtime stack. Arguments to the function are : |
| 54 | * x0 - File name |
| 55 | * x1 - Line no |
| 56 | * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6. |
| 57 | * --------------------------------------------------------------------------- |
| 58 | */ |
| 59 | func asm_assert |
Antonio Nino Diaz | 808c229 | 2017-04-18 15:16:05 +0100 | [diff] [blame] | 60 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
| 61 | /* |
| 62 | * Only print the output if LOG_LEVEL is higher or equal to |
| 63 | * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1. |
| 64 | */ |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 65 | mov x5, x0 |
| 66 | mov x6, x1 |
Yann Gautier | d04c7ae | 2020-09-17 15:15:27 +0200 | [diff] [blame] | 67 | |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 68 | /* Ensure the console is initialized */ |
| 69 | bl plat_crash_console_init |
Yann Gautier | d04c7ae | 2020-09-17 15:15:27 +0200 | [diff] [blame] | 70 | |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 71 | /* Check if the console is initialized */ |
| 72 | cbz x0, _assert_loop |
Yann Gautier | d04c7ae | 2020-09-17 15:15:27 +0200 | [diff] [blame] | 73 | |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 74 | /* The console is initialized */ |
| 75 | adr x4, assert_msg1 |
| 76 | bl asm_print_str |
| 77 | mov x4, x5 |
| 78 | bl asm_print_str |
| 79 | adr x4, assert_msg2 |
| 80 | bl asm_print_str |
Yann Gautier | d04c7ae | 2020-09-17 15:15:27 +0200 | [diff] [blame] | 81 | |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 82 | /* Check if line number higher than max permitted */ |
| 83 | tst x6, #~0xffff |
| 84 | b.ne _assert_loop |
| 85 | mov x4, x6 |
| 86 | asm_print_line_dec |
Antonio Nino Diaz | d3ec543 | 2017-02-17 17:11:27 +0000 | [diff] [blame] | 87 | bl plat_crash_console_flush |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 88 | _assert_loop: |
Antonio Nino Diaz | 808c229 | 2017-04-18 15:16:05 +0100 | [diff] [blame] | 89 | #endif /* LOG_LEVEL >= LOG_LEVEL_INFO */ |
Antonio Nino Diaz | e98fa3a | 2017-02-16 16:49:18 +0000 | [diff] [blame] | 90 | no_ret plat_panic_handler |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 91 | endfunc asm_assert |
Antonio Nino Diaz | 7c65c1e | 2017-04-20 09:58:28 +0100 | [diff] [blame] | 92 | #endif /* ENABLE_ASSERTIONS */ |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * This function prints a string from address in x4. |
| 96 | * In: x4 = pointer to string. |
| 97 | * Clobber: x30, x0, x1, x2, x3 |
| 98 | */ |
| 99 | func asm_print_str |
| 100 | mov x3, x30 |
| 101 | 1: |
| 102 | ldrb w0, [x4], #0x1 |
| 103 | cbz x0, 2f |
| 104 | bl plat_crash_console_putc |
| 105 | b 1b |
| 106 | 2: |
| 107 | ret x3 |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 108 | endfunc asm_print_str |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * This function prints a hexadecimal number in x4. |
| 112 | * In: x4 = the hexadecimal to print. |
Antonio Nino Diaz | 1f21bcf | 2016-02-01 13:57:25 +0000 | [diff] [blame] | 113 | * Clobber: x30, x0 - x3, x5 |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 114 | */ |
| 115 | func asm_print_hex |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 116 | mov x5, #64 /* No of bits to convert to ascii */ |
Alexei Fedorov | c4dfb3b | 2019-07-29 13:34:07 +0100 | [diff] [blame] | 117 | |
| 118 | /* Convert to ascii number of bits in x5 */ |
| 119 | asm_print_hex_bits: |
| 120 | mov x3, x30 |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 121 | 1: |
| 122 | sub x5, x5, #4 |
| 123 | lsrv x0, x4, x5 |
| 124 | and x0, x0, #0xf |
| 125 | cmp x0, #0xA |
| 126 | b.lo 2f |
| 127 | /* Add by 0x27 in addition to ASCII_OFFSET_NUM |
| 128 | * to get ascii for characters 'a - f'. |
| 129 | */ |
| 130 | add x0, x0, #0x27 |
| 131 | 2: |
| 132 | add x0, x0, #ASCII_OFFSET_NUM |
| 133 | bl plat_crash_console_putc |
| 134 | cbnz x5, 1b |
| 135 | ret x3 |
Kévin Petit | a877c25 | 2015-03-24 14:03:57 +0000 | [diff] [blame] | 136 | endfunc asm_print_hex |
Soby Mathew | 041f62a | 2014-07-14 16:58:03 +0100 | [diff] [blame] | 137 | |
Justin Chadwell | 7690382 | 2019-08-20 10:58:49 +0100 | [diff] [blame] | 138 | /* |
| 139 | * Helper function to print newline to console |
| 140 | * Clobber: x0 |
| 141 | */ |
| 142 | func asm_print_newline |
| 143 | mov x0, '\n' |
| 144 | b plat_crash_console_putc |
| 145 | endfunc asm_print_newline |
| 146 | |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 147 | /*********************************************************** |
| 148 | * The common implementation of do_panic for all BL stages |
| 149 | ***********************************************************/ |
| 150 | |
| 151 | .section .rodata.panic_str, "aS" |
| 152 | panic_msg: .asciz "PANIC at PC : 0x" |
| 153 | |
| 154 | /* --------------------------------------------------------------------------- |
| 155 | * do_panic assumes that it is invoked from a C Runtime Environment ie a |
| 156 | * valid stack exists. This call will not return. |
| 157 | * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6 |
| 158 | * --------------------------------------------------------------------------- |
| 159 | */ |
| 160 | |
| 161 | /* This is for the non el3 BL stages to compile through */ |
| 162 | .weak el3_panic |
Alexei Fedorov | 813c9f9 | 2020-03-03 13:31:58 +0000 | [diff] [blame] | 163 | .weak elx_panic |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 164 | |
| 165 | func do_panic |
| 166 | #if CRASH_REPORTING |
| 167 | str x0, [sp, #-0x10]! |
| 168 | mrs x0, currentel |
Alexei Fedorov | 813c9f9 | 2020-03-03 13:31:58 +0000 | [diff] [blame] | 169 | ubfx x0, x0, #MODE_EL_SHIFT, #MODE_EL_WIDTH |
| 170 | cmp x0, #MODE_EL3 |
Manish Pandey | 0e3379d | 2022-10-10 11:43:08 +0100 | [diff] [blame] | 171 | #if !HANDLE_EA_EL3_FIRST_NS |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 172 | ldr x0, [sp], #0x10 |
| 173 | b.eq el3_panic |
Alexei Fedorov | 813c9f9 | 2020-03-03 13:31:58 +0000 | [diff] [blame] | 174 | #else |
| 175 | b.ne to_panic_common |
| 176 | |
| 177 | /* Check EL the exception taken from */ |
| 178 | mrs x0, spsr_el3 |
| 179 | ubfx x0, x0, #SPSR_EL_SHIFT, #SPSR_EL_WIDTH |
| 180 | cmp x0, #MODE_EL3 |
| 181 | b.ne elx_panic |
| 182 | ldr x0, [sp], #0x10 |
| 183 | b el3_panic |
| 184 | |
| 185 | to_panic_common: |
| 186 | ldr x0, [sp], #0x10 |
Manish Pandey | 0e3379d | 2022-10-10 11:43:08 +0100 | [diff] [blame] | 187 | #endif /* HANDLE_EA_EL3_FIRST_NS */ |
Alexei Fedorov | 813c9f9 | 2020-03-03 13:31:58 +0000 | [diff] [blame] | 188 | #endif /* CRASH_REPORTING */ |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 189 | |
| 190 | panic_common: |
| 191 | /* |
| 192 | * el3_panic will be redefined by the BL31 |
| 193 | * crash reporting mechanism (if enabled) |
| 194 | */ |
| 195 | el3_panic: |
| 196 | mov x6, x30 |
| 197 | bl plat_crash_console_init |
Yann Gautier | d04c7ae | 2020-09-17 15:15:27 +0200 | [diff] [blame] | 198 | |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 199 | /* Check if the console is initialized */ |
Antonio Nino Diaz | 1f21bcf | 2016-02-01 13:57:25 +0000 | [diff] [blame] | 200 | cbz x0, _panic_handler |
Yann Gautier | d04c7ae | 2020-09-17 15:15:27 +0200 | [diff] [blame] | 201 | |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 202 | /* The console is initialized */ |
| 203 | adr x4, panic_msg |
| 204 | bl asm_print_str |
| 205 | mov x4, x6 |
Yann Gautier | d04c7ae | 2020-09-17 15:15:27 +0200 | [diff] [blame] | 206 | |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 207 | /* The panic location is lr -4 */ |
| 208 | sub x4, x4, #4 |
| 209 | bl asm_print_hex |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 210 | |
Pali Rohár | 7b4e2d6 | 2021-02-16 18:05:57 +0100 | [diff] [blame] | 211 | /* Print new line */ |
| 212 | bl asm_print_newline |
| 213 | |
Antonio Nino Diaz | d3ec543 | 2017-02-17 17:11:27 +0000 | [diff] [blame] | 214 | bl plat_crash_console_flush |
| 215 | |
Antonio Nino Diaz | 1f21bcf | 2016-02-01 13:57:25 +0000 | [diff] [blame] | 216 | _panic_handler: |
| 217 | /* Pass to plat_panic_handler the address from where el3_panic was |
| 218 | * called, not the address of the call from el3_panic. */ |
Julius Werner | 67ebde7 | 2017-07-27 14:59:34 -0700 | [diff] [blame] | 219 | mov x30, x6 |
| 220 | b plat_panic_handler |
Antonio Nino Diaz | 1f21bcf | 2016-02-01 13:57:25 +0000 | [diff] [blame] | 221 | endfunc do_panic |