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