1626ed510SSoby Mathew/* 2626ed510SSoby Mathew * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved. 3626ed510SSoby Mathew * 4626ed510SSoby Mathew * Redistribution and use in source and binary forms, with or without 5626ed510SSoby Mathew * modification, are permitted provided that the following conditions are met: 6626ed510SSoby Mathew * 7626ed510SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8626ed510SSoby Mathew * list of conditions and the following disclaimer. 9626ed510SSoby Mathew * 10626ed510SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11626ed510SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12626ed510SSoby Mathew * and/or other materials provided with the distribution. 13626ed510SSoby Mathew * 14626ed510SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15626ed510SSoby Mathew * to endorse or promote products derived from this software without specific 16626ed510SSoby Mathew * prior written permission. 17626ed510SSoby Mathew * 18626ed510SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19626ed510SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20626ed510SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21626ed510SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22626ed510SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23626ed510SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24626ed510SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25626ed510SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26626ed510SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27626ed510SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28626ed510SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29626ed510SSoby Mathew */ 30626ed510SSoby Mathew 31626ed510SSoby Mathew#include <arch.h> 32626ed510SSoby Mathew#include <asm_macros.S> 33626ed510SSoby Mathew 34626ed510SSoby Mathew .globl asm_print_str 35626ed510SSoby Mathew .globl asm_print_hex 36626ed510SSoby Mathew .globl asm_assert 37626ed510SSoby Mathew .globl do_panic 38626ed510SSoby Mathew 39626ed510SSoby Mathew/* Since the max decimal input number is 65536 */ 40626ed510SSoby Mathew#define MAX_DEC_DIVISOR 10000 41626ed510SSoby Mathew/* The offset to add to get ascii for numerals '0 - 9' */ 42626ed510SSoby Mathew#define ASCII_OFFSET_NUM 0x30 43626ed510SSoby Mathew 44626ed510SSoby Mathew#if ASM_ASSERTION 45626ed510SSoby Mathew.section .rodata.assert_str, "aS" 46626ed510SSoby Mathewassert_msg1: 47626ed510SSoby Mathew .asciz "ASSERT: File " 48626ed510SSoby Mathewassert_msg2: 49626ed510SSoby Mathew .asciz " Line " 50626ed510SSoby Mathew 51626ed510SSoby Mathew /* 52626ed510SSoby Mathew * This macro is intended to be used to print the 53626ed510SSoby Mathew * line number in decimal. Used by asm_assert macro. 54626ed510SSoby Mathew * The max number expected is 65536. 55626ed510SSoby Mathew * In: x4 = the decimal to print. 56626ed510SSoby Mathew * Clobber: x30, x0, x1, x2, x5, x6 57626ed510SSoby Mathew */ 58626ed510SSoby Mathew .macro asm_print_line_dec 59626ed510SSoby Mathew mov x6, #10 /* Divide by 10 after every loop iteration */ 60626ed510SSoby Mathew mov x5, #MAX_DEC_DIVISOR 61*aecc0840SSoby Mathewdec_print_loop: 62626ed510SSoby Mathew udiv x0, x4, x5 /* Get the quotient */ 63626ed510SSoby Mathew msub x4, x0, x5, x4 /* Find the remainder */ 64626ed510SSoby Mathew add x0, x0, #ASCII_OFFSET_NUM /* Convert to ascii */ 65626ed510SSoby Mathew bl plat_crash_console_putc 66626ed510SSoby Mathew udiv x5, x5, x6 /* Reduce divisor */ 67*aecc0840SSoby Mathew cbnz x5, dec_print_loop 68626ed510SSoby Mathew .endm 69626ed510SSoby Mathew 70626ed510SSoby Mathew 71626ed510SSoby Mathew/* --------------------------------------------------------------------------- 72626ed510SSoby Mathew * Assertion support in assembly. 73626ed510SSoby Mathew * The below function helps to support assertions in assembly where we do not 74626ed510SSoby Mathew * have a C runtime stack. Arguments to the function are : 75626ed510SSoby Mathew * x0 - File name 76626ed510SSoby Mathew * x1 - Line no 77626ed510SSoby Mathew * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6. 78626ed510SSoby Mathew * --------------------------------------------------------------------------- 79626ed510SSoby Mathew */ 80626ed510SSoby Mathewfunc asm_assert 81626ed510SSoby Mathew mov x5, x0 82626ed510SSoby Mathew mov x6, x1 83626ed510SSoby Mathew /* Ensure the console is initialized */ 84626ed510SSoby Mathew bl plat_crash_console_init 85626ed510SSoby Mathew /* Check if the console is initialized */ 86626ed510SSoby Mathew cbz x0, _assert_loop 87626ed510SSoby Mathew /* The console is initialized */ 88626ed510SSoby Mathew adr x4, assert_msg1 89626ed510SSoby Mathew bl asm_print_str 90626ed510SSoby Mathew mov x4, x5 91626ed510SSoby Mathew bl asm_print_str 92626ed510SSoby Mathew adr x4, assert_msg2 93626ed510SSoby Mathew bl asm_print_str 94626ed510SSoby Mathew /* Check if line number higher than max permitted */ 95626ed510SSoby Mathew tst x6, #~0xffff 96626ed510SSoby Mathew b.ne _assert_loop 97626ed510SSoby Mathew mov x4, x6 98626ed510SSoby Mathew asm_print_line_dec 99626ed510SSoby Mathew_assert_loop: 100626ed510SSoby Mathew b _assert_loop 101626ed510SSoby Mathew#endif 102626ed510SSoby Mathew 103626ed510SSoby Mathew/* 104626ed510SSoby Mathew * This function prints a string from address in x4. 105626ed510SSoby Mathew * In: x4 = pointer to string. 106626ed510SSoby Mathew * Clobber: x30, x0, x1, x2, x3 107626ed510SSoby Mathew */ 108626ed510SSoby Mathewfunc asm_print_str 109626ed510SSoby Mathew mov x3, x30 110626ed510SSoby Mathew1: 111626ed510SSoby Mathew ldrb w0, [x4], #0x1 112626ed510SSoby Mathew cbz x0, 2f 113626ed510SSoby Mathew bl plat_crash_console_putc 114626ed510SSoby Mathew b 1b 115626ed510SSoby Mathew2: 116626ed510SSoby Mathew ret x3 117626ed510SSoby Mathew 118626ed510SSoby Mathew/* 119626ed510SSoby Mathew * This function prints a hexadecimal number in x4. 120626ed510SSoby Mathew * In: x4 = the hexadecimal to print. 121626ed510SSoby Mathew * Clobber: x30, x0, x5, x1, x2, x3 122626ed510SSoby Mathew */ 123626ed510SSoby Mathewfunc asm_print_hex 124626ed510SSoby Mathew mov x3, x30 125626ed510SSoby Mathew mov x5, #64 /* No of bits to convert to ascii */ 126626ed510SSoby Mathew1: 127626ed510SSoby Mathew sub x5, x5, #4 128626ed510SSoby Mathew lsrv x0, x4, x5 129626ed510SSoby Mathew and x0, x0, #0xf 130626ed510SSoby Mathew cmp x0, #0xA 131626ed510SSoby Mathew b.lo 2f 132626ed510SSoby Mathew /* Add by 0x27 in addition to ASCII_OFFSET_NUM 133626ed510SSoby Mathew * to get ascii for characters 'a - f'. 134626ed510SSoby Mathew */ 135626ed510SSoby Mathew add x0, x0, #0x27 136626ed510SSoby Mathew2: 137626ed510SSoby Mathew add x0, x0, #ASCII_OFFSET_NUM 138626ed510SSoby Mathew bl plat_crash_console_putc 139626ed510SSoby Mathew cbnz x5, 1b 140626ed510SSoby Mathew ret x3 141626ed510SSoby Mathew 142626ed510SSoby Mathew /*********************************************************** 143626ed510SSoby Mathew * The common implementation of do_panic for all BL stages 144626ed510SSoby Mathew ***********************************************************/ 145626ed510SSoby Mathew 146626ed510SSoby Mathew.section .rodata.panic_str, "aS" 147626ed510SSoby Mathew panic_msg: .asciz "PANIC at PC : 0x" 148626ed510SSoby Mathew 149626ed510SSoby Mathew/* --------------------------------------------------------------------------- 150626ed510SSoby Mathew * do_panic assumes that it is invoked from a C Runtime Environment ie a 151626ed510SSoby Mathew * valid stack exists. This call will not return. 152626ed510SSoby Mathew * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6 153626ed510SSoby Mathew * --------------------------------------------------------------------------- 154626ed510SSoby Mathew */ 155626ed510SSoby Mathew 156626ed510SSoby Mathew/* This is for the non el3 BL stages to compile through */ 157626ed510SSoby Mathew .weak el3_panic 158626ed510SSoby Mathew 159626ed510SSoby Mathewfunc do_panic 160626ed510SSoby Mathew#if CRASH_REPORTING 161626ed510SSoby Mathew str x0, [sp, #-0x10]! 162626ed510SSoby Mathew mrs x0, currentel 163626ed510SSoby Mathew ubfx x0, x0, #2, #2 164626ed510SSoby Mathew cmp x0, #0x3 165626ed510SSoby Mathew ldr x0, [sp], #0x10 166626ed510SSoby Mathew b.eq el3_panic 167626ed510SSoby Mathew#endif 168626ed510SSoby Mathew 169626ed510SSoby Mathewpanic_common: 170626ed510SSoby Mathew/* 171626ed510SSoby Mathew * el3_panic will be redefined by the BL31 172626ed510SSoby Mathew * crash reporting mechanism (if enabled) 173626ed510SSoby Mathew */ 174626ed510SSoby Mathewel3_panic: 175626ed510SSoby Mathew mov x6, x30 176626ed510SSoby Mathew bl plat_crash_console_init 177626ed510SSoby Mathew /* Check if the console is initialized */ 178626ed510SSoby Mathew cbz x0, _panic_loop 179626ed510SSoby Mathew /* The console is initialized */ 180626ed510SSoby Mathew adr x4, panic_msg 181626ed510SSoby Mathew bl asm_print_str 182626ed510SSoby Mathew mov x4, x6 183626ed510SSoby Mathew /* The panic location is lr -4 */ 184626ed510SSoby Mathew sub x4, x4, #4 185626ed510SSoby Mathew bl asm_print_hex 186626ed510SSoby Mathew_panic_loop: 187626ed510SSoby Mathew b _panic_loop 188626ed510SSoby Mathew 189