1/* 2 * Copyright (c) 2014-2016, 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 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 44#if ASM_ASSERTION 45.section .rodata.assert_str, "aS" 46assert_msg1: 47 .asciz "ASSERT: File " 48assert_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 61dec_print_loop: 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 */ 67 cbnz x5, dec_print_loop 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 */ 80func asm_assert 81 mov x5, x0 82 mov x6, x1 83 /* Ensure the console is initialized */ 84 bl plat_crash_console_init 85 /* Check if the console is initialized */ 86 cbz x0, _assert_loop 87 /* The console is initialized */ 88 adr x4, assert_msg1 89 bl asm_print_str 90 mov x4, x5 91 bl asm_print_str 92 adr x4, assert_msg2 93 bl asm_print_str 94 /* Check if line number higher than max permitted */ 95 tst x6, #~0xffff 96 b.ne _assert_loop 97 mov x4, x6 98 asm_print_line_dec 99_assert_loop: 100 b _assert_loop 101endfunc asm_assert 102#endif 103 104/* 105 * This function prints a string from address in x4. 106 * In: x4 = pointer to string. 107 * Clobber: x30, x0, x1, x2, x3 108 */ 109func asm_print_str 110 mov x3, x30 1111: 112 ldrb w0, [x4], #0x1 113 cbz x0, 2f 114 bl plat_crash_console_putc 115 b 1b 1162: 117 ret x3 118endfunc asm_print_str 119 120/* 121 * This function prints a hexadecimal number in x4. 122 * In: x4 = the hexadecimal to print. 123 * Clobber: x30, x0 - x3, x5 124 */ 125func asm_print_hex 126 mov x3, x30 127 mov x5, #64 /* No of bits to convert to ascii */ 1281: 129 sub x5, x5, #4 130 lsrv x0, x4, x5 131 and x0, x0, #0xf 132 cmp x0, #0xA 133 b.lo 2f 134 /* Add by 0x27 in addition to ASCII_OFFSET_NUM 135 * to get ascii for characters 'a - f'. 136 */ 137 add x0, x0, #0x27 1382: 139 add x0, x0, #ASCII_OFFSET_NUM 140 bl plat_crash_console_putc 141 cbnz x5, 1b 142 ret x3 143endfunc asm_print_hex 144 145 /*********************************************************** 146 * The common implementation of do_panic for all BL stages 147 ***********************************************************/ 148 149.section .rodata.panic_str, "aS" 150 panic_msg: .asciz "PANIC at PC : 0x" 151 152/* --------------------------------------------------------------------------- 153 * do_panic assumes that it is invoked from a C Runtime Environment ie a 154 * valid stack exists. This call will not return. 155 * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6 156 * --------------------------------------------------------------------------- 157 */ 158 159/* This is for the non el3 BL stages to compile through */ 160 .weak el3_panic 161 162func do_panic 163#if CRASH_REPORTING 164 str x0, [sp, #-0x10]! 165 mrs x0, currentel 166 ubfx x0, x0, #2, #2 167 cmp x0, #0x3 168 ldr x0, [sp], #0x10 169 b.eq el3_panic 170#endif 171 172panic_common: 173/* 174 * el3_panic will be redefined by the BL31 175 * crash reporting mechanism (if enabled) 176 */ 177el3_panic: 178 mov x6, x30 179 bl plat_crash_console_init 180 /* Check if the console is initialized */ 181 cbz x0, _panic_handler 182 /* The console is initialized */ 183 adr x4, panic_msg 184 bl asm_print_str 185 mov x4, x6 186 /* The panic location is lr -4 */ 187 sub x4, x4, #4 188 bl asm_print_hex 189 190_panic_handler: 191 /* Pass to plat_panic_handler the address from where el3_panic was 192 * called, not the address of the call from el3_panic. */ 193 mov x30,x6 194 no_ret plat_panic_handler 195endfunc do_panic 196