1f24307deSSoby Mathew/* 26dc5979aSYann Gautier * Copyright (c) 2016-2022, ARM Limited and Contributors. All rights reserved. 3f24307deSSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5f24307deSSoby Mathew */ 6f24307deSSoby Mathew 7f24307deSSoby Mathew#include <arch.h> 8f24307deSSoby Mathew#include <asm_macros.S> 900a55fe4SYann Gautier#include <common/debug.h> 10f24307deSSoby Mathew 1100a55fe4SYann Gautier .globl asm_print_str 1200a55fe4SYann Gautier .globl asm_print_hex 1300a55fe4SYann Gautier .globl asm_print_hex_bits 14e12cb61fSJeenu Viswambharan .globl asm_assert 15*bd62ce98SGovindraj Raja .globl el3_panic 161a0a3f06SYatharth Kochar .globl report_exception 176dc5979aSYann Gautier .globl report_prefetch_abort 186dc5979aSYann Gautier .globl report_data_abort 19f24307deSSoby Mathew 20e12cb61fSJeenu Viswambharan/* Since the max decimal input number is 65536 */ 21e12cb61fSJeenu Viswambharan#define MAX_DEC_DIVISOR 10000 22e12cb61fSJeenu Viswambharan/* The offset to add to get ascii for numerals '0 - 9' */ 23e12cb61fSJeenu Viswambharan#define ASCII_OFFSET_NUM '0' 24e12cb61fSJeenu Viswambharan 25044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS 26e12cb61fSJeenu Viswambharan.section .rodata.assert_str, "aS" 27e12cb61fSJeenu Viswambharanassert_msg1: 28e12cb61fSJeenu Viswambharan .asciz "ASSERT: File " 29e12cb61fSJeenu Viswambharanassert_msg2: 3064cc6e91SEtienne Carriere#if ARM_ARCH_MAJOR == 7 && !defined(ARMV7_SUPPORTS_VIRTUALIZATION) 3164cc6e91SEtienne Carriere /****************************************************************** 3264cc6e91SEtienne Carriere * Virtualization comes with the UDIV/SDIV instructions. If missing 3364cc6e91SEtienne Carriere * write file line number in hexadecimal format. 3464cc6e91SEtienne Carriere ******************************************************************/ 3564cc6e91SEtienne Carriere .asciz " Line 0x" 3664cc6e91SEtienne Carriere#else 37e12cb61fSJeenu Viswambharan .asciz " Line " 3800a55fe4SYann Gautier 3900a55fe4SYann Gautier /* 4000a55fe4SYann Gautier * This macro is intended to be used to print the 4100a55fe4SYann Gautier * line number in decimal. Used by asm_assert macro. 4200a55fe4SYann Gautier * The max number expected is 65536. 4300a55fe4SYann Gautier * In: r4 = the decimal to print. 4400a55fe4SYann Gautier * Clobber: lr, r0, r1, r2, r5, r6 4500a55fe4SYann Gautier */ 4600a55fe4SYann Gautier .macro asm_print_line_dec 4700a55fe4SYann Gautier mov r6, #10 /* Divide by 10 after every loop iteration */ 4800a55fe4SYann Gautier ldr r5, =MAX_DEC_DIVISOR 4900a55fe4SYann Gautierdec_print_loop: 5000a55fe4SYann Gautier udiv r0, r4, r5 /* Get the quotient */ 5100a55fe4SYann Gautier mls r4, r0, r5, r4 /* Find the remainder */ 5200a55fe4SYann Gautier add r0, r0, #ASCII_OFFSET_NUM /* Convert to ascii */ 5300a55fe4SYann Gautier bl plat_crash_console_putc 5400a55fe4SYann Gautier udiv r5, r5, r6 /* Reduce divisor */ 5500a55fe4SYann Gautier cmp r5, #0 5600a55fe4SYann Gautier bne dec_print_loop 5700a55fe4SYann Gautier .endm 5864cc6e91SEtienne Carriere#endif 59e12cb61fSJeenu Viswambharan 60e12cb61fSJeenu Viswambharan/* --------------------------------------------------------------------------- 61e12cb61fSJeenu Viswambharan * Assertion support in assembly. 62e12cb61fSJeenu Viswambharan * The below function helps to support assertions in assembly where we do not 63e12cb61fSJeenu Viswambharan * have a C runtime stack. Arguments to the function are : 64e12cb61fSJeenu Viswambharan * r0 - File name 65e12cb61fSJeenu Viswambharan * r1 - Line no 66e12cb61fSJeenu Viswambharan * Clobber list : lr, r0 - r6 67e12cb61fSJeenu Viswambharan * --------------------------------------------------------------------------- 68e12cb61fSJeenu Viswambharan */ 69e12cb61fSJeenu Viswambharanfunc asm_assert 70cc8b5632SAntonio Nino Diaz#if LOG_LEVEL >= LOG_LEVEL_INFO 71cc8b5632SAntonio Nino Diaz /* 72cc8b5632SAntonio Nino Diaz * Only print the output if LOG_LEVEL is higher or equal to 73cc8b5632SAntonio Nino Diaz * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1. 74cc8b5632SAntonio Nino Diaz */ 75e12cb61fSJeenu Viswambharan /* Stash the parameters already in r0 and r1 */ 76e12cb61fSJeenu Viswambharan mov r5, r0 77e12cb61fSJeenu Viswambharan mov r6, r1 78e12cb61fSJeenu Viswambharan 7900a55fe4SYann Gautier /* Ensure the console is initialized */ 80e12cb61fSJeenu Viswambharan bl plat_crash_console_init 81e12cb61fSJeenu Viswambharan 8200a55fe4SYann Gautier /* Check if the console is initialized */ 8300a55fe4SYann Gautier cmp r0, #0 8400a55fe4SYann Gautier beq _assert_loop 8500a55fe4SYann Gautier 8600a55fe4SYann Gautier /* The console is initialized */ 87e12cb61fSJeenu Viswambharan ldr r4, =assert_msg1 88e12cb61fSJeenu Viswambharan bl asm_print_str 89e12cb61fSJeenu Viswambharan mov r4, r5 90e12cb61fSJeenu Viswambharan bl asm_print_str 91e12cb61fSJeenu Viswambharan ldr r4, =assert_msg2 92e12cb61fSJeenu Viswambharan bl asm_print_str 93e12cb61fSJeenu Viswambharan 9400a55fe4SYann Gautier /* Check if line number higher than max permitted */ 95e12cb61fSJeenu Viswambharan ldr r4, =~0xffff 96e12cb61fSJeenu Viswambharan tst r6, r4 9700a55fe4SYann Gautier bne _assert_loop 98e12cb61fSJeenu Viswambharan mov r4, r6 99e12cb61fSJeenu Viswambharan 10064cc6e91SEtienne Carriere#if ARM_ARCH_MAJOR == 7 && !defined(ARMV7_SUPPORTS_VIRTUALIZATION) 10164cc6e91SEtienne Carriere /****************************************************************** 10264cc6e91SEtienne Carriere * Virtualization comes with the UDIV/SDIV instructions. If missing 10364cc6e91SEtienne Carriere * write file line number in hexadecimal format. 10464cc6e91SEtienne Carriere ******************************************************************/ 10564cc6e91SEtienne Carriere bl asm_print_hex 10664cc6e91SEtienne Carriere#else 10700a55fe4SYann Gautier asm_print_line_dec 10864cc6e91SEtienne Carriere#endif 109801cf93cSAntonio Nino Diaz bl plat_crash_console_flush 11000a55fe4SYann Gautier_assert_loop: 111cc8b5632SAntonio Nino Diaz#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */ 112e12cb61fSJeenu Viswambharan no_ret plat_panic_handler 113e12cb61fSJeenu Viswambharanendfunc asm_assert 114044bb2faSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */ 115e12cb61fSJeenu Viswambharan 116e12cb61fSJeenu Viswambharan/* 117e12cb61fSJeenu Viswambharan * This function prints a string from address in r4 118e12cb61fSJeenu Viswambharan * Clobber: lr, r0 - r4 119e12cb61fSJeenu Viswambharan */ 120e12cb61fSJeenu Viswambharanfunc asm_print_str 121e12cb61fSJeenu Viswambharan mov r3, lr 122e12cb61fSJeenu Viswambharan1: 123e12cb61fSJeenu Viswambharan ldrb r0, [r4], #0x1 124e12cb61fSJeenu Viswambharan cmp r0, #0 125e12cb61fSJeenu Viswambharan beq 2f 126e12cb61fSJeenu Viswambharan bl plat_crash_console_putc 127e12cb61fSJeenu Viswambharan b 1b 128e12cb61fSJeenu Viswambharan2: 129e12cb61fSJeenu Viswambharan bx r3 130e12cb61fSJeenu Viswambharanendfunc asm_print_str 131e12cb61fSJeenu Viswambharan 132e12cb61fSJeenu Viswambharan/* 133e12cb61fSJeenu Viswambharan * This function prints a hexadecimal number in r4. 134e12cb61fSJeenu Viswambharan * In: r4 = the hexadecimal to print. 135e12cb61fSJeenu Viswambharan * Clobber: lr, r0 - r3, r5 136e12cb61fSJeenu Viswambharan */ 137e12cb61fSJeenu Viswambharanfunc asm_print_hex 138e12cb61fSJeenu Viswambharan mov r5, #32 /* No of bits to convert to ascii */ 13900a55fe4SYann Gautier 14000a55fe4SYann Gautier /* Convert to ascii number of bits in r5 */ 14100a55fe4SYann Gautierasm_print_hex_bits: 14200a55fe4SYann Gautier mov r3, lr 143e12cb61fSJeenu Viswambharan1: 144e12cb61fSJeenu Viswambharan sub r5, r5, #4 145e12cb61fSJeenu Viswambharan lsr r0, r4, r5 146e12cb61fSJeenu Viswambharan and r0, r0, #0xf 147e12cb61fSJeenu Viswambharan cmp r0, #0xa 148e12cb61fSJeenu Viswambharan blo 2f 149e12cb61fSJeenu Viswambharan /* Add by 0x27 in addition to ASCII_OFFSET_NUM 150e12cb61fSJeenu Viswambharan * to get ascii for characters 'a - f'. 151e12cb61fSJeenu Viswambharan */ 152e12cb61fSJeenu Viswambharan add r0, r0, #0x27 153e12cb61fSJeenu Viswambharan2: 154e12cb61fSJeenu Viswambharan add r0, r0, #ASCII_OFFSET_NUM 155e12cb61fSJeenu Viswambharan bl plat_crash_console_putc 156e12cb61fSJeenu Viswambharan cmp r5, #0 157e12cb61fSJeenu Viswambharan bne 1b 158e12cb61fSJeenu Viswambharan bx r3 159e12cb61fSJeenu Viswambharanendfunc asm_print_hex 16000a55fe4SYann Gautier 16100a55fe4SYann Gautier /*********************************************************** 162*bd62ce98SGovindraj Raja * The common implementation of el3_panic for all BL stages 16300a55fe4SYann Gautier ***********************************************************/ 16400a55fe4SYann Gautier 16500a55fe4SYann Gautier.section .rodata.panic_str, "aS" 16600a55fe4SYann Gautier panic_msg: .asciz "PANIC at PC : 0x" 16700a55fe4SYann Gautier panic_end: .asciz "\r\n" 16800a55fe4SYann Gautier 169*bd62ce98SGovindraj Rajafunc el3_panic 17000a55fe4SYann Gautier /* Have LR copy point to PC at the time of panic */ 17100a55fe4SYann Gautier sub r6, lr, #4 17200a55fe4SYann Gautier 17300a55fe4SYann Gautier /* Initialize crash console and verify success */ 17400a55fe4SYann Gautier bl plat_crash_console_init 17500a55fe4SYann Gautier 17600a55fe4SYann Gautier /* Check if the console is initialized */ 17700a55fe4SYann Gautier cmp r0, #0 17800a55fe4SYann Gautier beq _panic_handler 17900a55fe4SYann Gautier 18000a55fe4SYann Gautier /* The console is initialized */ 18100a55fe4SYann Gautier ldr r4, =panic_msg 18200a55fe4SYann Gautier bl asm_print_str 18300a55fe4SYann Gautier 18400a55fe4SYann Gautier /* Print LR in hex */ 18500a55fe4SYann Gautier mov r4, r6 18600a55fe4SYann Gautier bl asm_print_hex 18700a55fe4SYann Gautier 18800a55fe4SYann Gautier /* Print new line */ 18900a55fe4SYann Gautier ldr r4, =panic_end 19000a55fe4SYann Gautier bl asm_print_str 19100a55fe4SYann Gautier 19200a55fe4SYann Gautier bl plat_crash_console_flush 19300a55fe4SYann Gautier 19400a55fe4SYann Gautier_panic_handler: 19500a55fe4SYann Gautier mov lr, r6 19600a55fe4SYann Gautier b plat_panic_handler 197*bd62ce98SGovindraj Rajaendfunc el3_panic 19800a55fe4SYann Gautier 19900a55fe4SYann Gautier /*********************************************************** 20000a55fe4SYann Gautier * This function is called from the vector table for 20100a55fe4SYann Gautier * unhandled exceptions. It reads the current mode and 20200a55fe4SYann Gautier * passes it to platform. 20300a55fe4SYann Gautier ***********************************************************/ 20400a55fe4SYann Gautierfunc report_exception 20500a55fe4SYann Gautier mrs r0, cpsr 20600a55fe4SYann Gautier and r0, #MODE32_MASK 20700a55fe4SYann Gautier bl plat_report_exception 20800a55fe4SYann Gautier no_ret plat_panic_handler 20900a55fe4SYann Gautierendfunc report_exception 2106dc5979aSYann Gautier 2116dc5979aSYann Gautier /*********************************************************** 2126dc5979aSYann Gautier * This function is called from the vector table for 2136dc5979aSYann Gautier * unhandled exceptions. The lr_abt is given as an 2146dc5979aSYann Gautier * argument to platform handler. 2156dc5979aSYann Gautier ***********************************************************/ 2166dc5979aSYann Gautierfunc report_prefetch_abort 2176dc5979aSYann Gautier#if ARM_ARCH_MAJOR == 7 && !defined(ARMV7_SUPPORTS_VIRTUALIZATION) 2186dc5979aSYann Gautier b report_exception 2196dc5979aSYann Gautier#else 2206dc5979aSYann Gautier mrs r0, lr_abt 2216dc5979aSYann Gautier bl plat_report_prefetch_abort 2226dc5979aSYann Gautier no_ret plat_panic_handler 2236dc5979aSYann Gautier#endif 2246dc5979aSYann Gautierendfunc report_prefetch_abort 2256dc5979aSYann Gautier 2266dc5979aSYann Gautier /*********************************************************** 2276dc5979aSYann Gautier * This function is called from the vector table for 2286dc5979aSYann Gautier * unhandled exceptions. The lr_abt is given as an 2296dc5979aSYann Gautier * argument to platform handler. 2306dc5979aSYann Gautier ***********************************************************/ 2316dc5979aSYann Gautierfunc report_data_abort 2326dc5979aSYann Gautier#if ARM_ARCH_MAJOR == 7 && !defined(ARMV7_SUPPORTS_VIRTUALIZATION) 2336dc5979aSYann Gautier b report_exception 2346dc5979aSYann Gautier#else 2356dc5979aSYann Gautier mrs r0, lr_abt 2366dc5979aSYann Gautier bl plat_report_data_abort 2376dc5979aSYann Gautier no_ret plat_panic_handler 2386dc5979aSYann Gautier#endif 2396dc5979aSYann Gautierendfunc report_data_abort 240