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