1*626ed510SSoby Mathew/* 2*626ed510SSoby Mathew * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved. 3*626ed510SSoby Mathew * 4*626ed510SSoby Mathew * Redistribution and use in source and binary forms, with or without 5*626ed510SSoby Mathew * modification, are permitted provided that the following conditions are met: 6*626ed510SSoby Mathew * 7*626ed510SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8*626ed510SSoby Mathew * list of conditions and the following disclaimer. 9*626ed510SSoby Mathew * 10*626ed510SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11*626ed510SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12*626ed510SSoby Mathew * and/or other materials provided with the distribution. 13*626ed510SSoby Mathew * 14*626ed510SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15*626ed510SSoby Mathew * to endorse or promote products derived from this software without specific 16*626ed510SSoby Mathew * prior written permission. 17*626ed510SSoby Mathew * 18*626ed510SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*626ed510SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*626ed510SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*626ed510SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*626ed510SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*626ed510SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*626ed510SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*626ed510SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*626ed510SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*626ed510SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*626ed510SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29*626ed510SSoby Mathew */ 30*626ed510SSoby Mathew 31*626ed510SSoby Mathew#include <arch.h> 32*626ed510SSoby Mathew#include <asm_macros.S> 33*626ed510SSoby Mathew 34*626ed510SSoby Mathew .globl asm_print_str 35*626ed510SSoby Mathew .globl asm_print_hex 36*626ed510SSoby Mathew .globl asm_assert 37*626ed510SSoby Mathew .globl do_panic 38*626ed510SSoby Mathew 39*626ed510SSoby Mathew/* Since the max decimal input number is 65536 */ 40*626ed510SSoby Mathew#define MAX_DEC_DIVISOR 10000 41*626ed510SSoby Mathew/* The offset to add to get ascii for numerals '0 - 9' */ 42*626ed510SSoby Mathew#define ASCII_OFFSET_NUM 0x30 43*626ed510SSoby Mathew 44*626ed510SSoby Mathew#if ASM_ASSERTION 45*626ed510SSoby Mathew.section .rodata.assert_str, "aS" 46*626ed510SSoby Mathewassert_msg1: 47*626ed510SSoby Mathew .asciz "ASSERT: File " 48*626ed510SSoby Mathewassert_msg2: 49*626ed510SSoby Mathew .asciz " Line " 50*626ed510SSoby Mathew 51*626ed510SSoby Mathew /* 52*626ed510SSoby Mathew * This macro is intended to be used to print the 53*626ed510SSoby Mathew * line number in decimal. Used by asm_assert macro. 54*626ed510SSoby Mathew * The max number expected is 65536. 55*626ed510SSoby Mathew * In: x4 = the decimal to print. 56*626ed510SSoby Mathew * Clobber: x30, x0, x1, x2, x5, x6 57*626ed510SSoby Mathew */ 58*626ed510SSoby Mathew .macro asm_print_line_dec 59*626ed510SSoby Mathew mov x6, #10 /* Divide by 10 after every loop iteration */ 60*626ed510SSoby Mathew mov x5, #MAX_DEC_DIVISOR 61*626ed510SSoby Mathew1: 62*626ed510SSoby Mathew udiv x0, x4, x5 /* Get the quotient */ 63*626ed510SSoby Mathew msub x4, x0, x5, x4 /* Find the remainder */ 64*626ed510SSoby Mathew add x0, x0, #ASCII_OFFSET_NUM /* Convert to ascii */ 65*626ed510SSoby Mathew bl plat_crash_console_putc 66*626ed510SSoby Mathew udiv x5, x5, x6 /* Reduce divisor */ 67*626ed510SSoby Mathew cbnz x5, 1b 68*626ed510SSoby Mathew .endm 69*626ed510SSoby Mathew 70*626ed510SSoby Mathew 71*626ed510SSoby Mathew/* --------------------------------------------------------------------------- 72*626ed510SSoby Mathew * Assertion support in assembly. 73*626ed510SSoby Mathew * The below function helps to support assertions in assembly where we do not 74*626ed510SSoby Mathew * have a C runtime stack. Arguments to the function are : 75*626ed510SSoby Mathew * x0 - File name 76*626ed510SSoby Mathew * x1 - Line no 77*626ed510SSoby Mathew * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6. 78*626ed510SSoby Mathew * --------------------------------------------------------------------------- 79*626ed510SSoby Mathew */ 80*626ed510SSoby Mathewfunc asm_assert 81*626ed510SSoby Mathew mov x5, x0 82*626ed510SSoby Mathew mov x6, x1 83*626ed510SSoby Mathew /* Ensure the console is initialized */ 84*626ed510SSoby Mathew bl plat_crash_console_init 85*626ed510SSoby Mathew /* Check if the console is initialized */ 86*626ed510SSoby Mathew cbz x0, _assert_loop 87*626ed510SSoby Mathew /* The console is initialized */ 88*626ed510SSoby Mathew adr x4, assert_msg1 89*626ed510SSoby Mathew bl asm_print_str 90*626ed510SSoby Mathew mov x4, x5 91*626ed510SSoby Mathew bl asm_print_str 92*626ed510SSoby Mathew adr x4, assert_msg2 93*626ed510SSoby Mathew bl asm_print_str 94*626ed510SSoby Mathew /* Check if line number higher than max permitted */ 95*626ed510SSoby Mathew tst x6, #~0xffff 96*626ed510SSoby Mathew b.ne _assert_loop 97*626ed510SSoby Mathew mov x4, x6 98*626ed510SSoby Mathew asm_print_line_dec 99*626ed510SSoby Mathew_assert_loop: 100*626ed510SSoby Mathew b _assert_loop 101*626ed510SSoby Mathew#endif 102*626ed510SSoby Mathew 103*626ed510SSoby Mathew/* 104*626ed510SSoby Mathew * This function prints a string from address in x4. 105*626ed510SSoby Mathew * In: x4 = pointer to string. 106*626ed510SSoby Mathew * Clobber: x30, x0, x1, x2, x3 107*626ed510SSoby Mathew */ 108*626ed510SSoby Mathewfunc asm_print_str 109*626ed510SSoby Mathew mov x3, x30 110*626ed510SSoby Mathew1: 111*626ed510SSoby Mathew ldrb w0, [x4], #0x1 112*626ed510SSoby Mathew cbz x0, 2f 113*626ed510SSoby Mathew bl plat_crash_console_putc 114*626ed510SSoby Mathew b 1b 115*626ed510SSoby Mathew2: 116*626ed510SSoby Mathew ret x3 117*626ed510SSoby Mathew 118*626ed510SSoby Mathew/* 119*626ed510SSoby Mathew * This function prints a hexadecimal number in x4. 120*626ed510SSoby Mathew * In: x4 = the hexadecimal to print. 121*626ed510SSoby Mathew * Clobber: x30, x0, x5, x1, x2, x3 122*626ed510SSoby Mathew */ 123*626ed510SSoby Mathewfunc asm_print_hex 124*626ed510SSoby Mathew mov x3, x30 125*626ed510SSoby Mathew mov x5, #64 /* No of bits to convert to ascii */ 126*626ed510SSoby Mathew1: 127*626ed510SSoby Mathew sub x5, x5, #4 128*626ed510SSoby Mathew lsrv x0, x4, x5 129*626ed510SSoby Mathew and x0, x0, #0xf 130*626ed510SSoby Mathew cmp x0, #0xA 131*626ed510SSoby Mathew b.lo 2f 132*626ed510SSoby Mathew /* Add by 0x27 in addition to ASCII_OFFSET_NUM 133*626ed510SSoby Mathew * to get ascii for characters 'a - f'. 134*626ed510SSoby Mathew */ 135*626ed510SSoby Mathew add x0, x0, #0x27 136*626ed510SSoby Mathew2: 137*626ed510SSoby Mathew add x0, x0, #ASCII_OFFSET_NUM 138*626ed510SSoby Mathew bl plat_crash_console_putc 139*626ed510SSoby Mathew cbnz x5, 1b 140*626ed510SSoby Mathew ret x3 141*626ed510SSoby Mathew 142*626ed510SSoby Mathew /*********************************************************** 143*626ed510SSoby Mathew * The common implementation of do_panic for all BL stages 144*626ed510SSoby Mathew ***********************************************************/ 145*626ed510SSoby Mathew 146*626ed510SSoby Mathew.section .rodata.panic_str, "aS" 147*626ed510SSoby Mathew panic_msg: .asciz "PANIC at PC : 0x" 148*626ed510SSoby Mathew 149*626ed510SSoby Mathew/* --------------------------------------------------------------------------- 150*626ed510SSoby Mathew * do_panic assumes that it is invoked from a C Runtime Environment ie a 151*626ed510SSoby Mathew * valid stack exists. This call will not return. 152*626ed510SSoby Mathew * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6 153*626ed510SSoby Mathew * --------------------------------------------------------------------------- 154*626ed510SSoby Mathew */ 155*626ed510SSoby Mathew 156*626ed510SSoby Mathew/* This is for the non el3 BL stages to compile through */ 157*626ed510SSoby Mathew .weak el3_panic 158*626ed510SSoby Mathew 159*626ed510SSoby Mathewfunc do_panic 160*626ed510SSoby Mathew#if CRASH_REPORTING 161*626ed510SSoby Mathew str x0, [sp, #-0x10]! 162*626ed510SSoby Mathew mrs x0, currentel 163*626ed510SSoby Mathew ubfx x0, x0, #2, #2 164*626ed510SSoby Mathew cmp x0, #0x3 165*626ed510SSoby Mathew ldr x0, [sp], #0x10 166*626ed510SSoby Mathew b.eq el3_panic 167*626ed510SSoby Mathew#endif 168*626ed510SSoby Mathew 169*626ed510SSoby Mathewpanic_common: 170*626ed510SSoby Mathew/* 171*626ed510SSoby Mathew * el3_panic will be redefined by the BL31 172*626ed510SSoby Mathew * crash reporting mechanism (if enabled) 173*626ed510SSoby Mathew */ 174*626ed510SSoby Mathewel3_panic: 175*626ed510SSoby Mathew mov x6, x30 176*626ed510SSoby Mathew bl plat_crash_console_init 177*626ed510SSoby Mathew /* Check if the console is initialized */ 178*626ed510SSoby Mathew cbz x0, _panic_loop 179*626ed510SSoby Mathew /* The console is initialized */ 180*626ed510SSoby Mathew adr x4, panic_msg 181*626ed510SSoby Mathew bl asm_print_str 182*626ed510SSoby Mathew mov x4, x6 183*626ed510SSoby Mathew /* The panic location is lr -4 */ 184*626ed510SSoby Mathew sub x4, x4, #4 185*626ed510SSoby Mathew bl asm_print_hex 186*626ed510SSoby Mathew_panic_loop: 187*626ed510SSoby Mathew b _panic_loop 188*626ed510SSoby Mathew 189