blob: 5fdaf641733b86d73b808f7e255e201e7c8bc229 [file] [log] [blame]
Soby Mathewbc920122014-07-14 16:58:03 +01001/*
Govindraj Raja7e619ec2023-01-16 15:11:47 +00002 * Copyright (c) 2014-2023 Arm Limited and Contributors. All rights reserved.
Soby Mathewbc920122014-07-14 16:58:03 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewbc920122014-07-14 16:58:03 +01005 */
6
7#include <arch.h>
8#include <asm_macros.S>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00009#include <common/debug.h>
Soby Mathewbc920122014-07-14 16:58:03 +010010
11 .globl asm_print_str
12 .globl asm_print_hex
Alexei Fedorov6c6a4702019-07-29 13:34:07 +010013 .globl asm_print_hex_bits
Justin Chadwell53d7e002019-08-20 10:58:49 +010014 .globl asm_print_newline
Soby Mathewbc920122014-07-14 16:58:03 +010015 .globl asm_assert
16 .globl do_panic
17
18/* Since the max decimal input number is 65536 */
19#define MAX_DEC_DIVISOR 10000
20/* The offset to add to get ascii for numerals '0 - 9' */
21#define ASCII_OFFSET_NUM 0x30
22
Antonio Nino Diaz044bb2f2017-04-20 09:58:28 +010023#if ENABLE_ASSERTIONS
Soby Mathewbc920122014-07-14 16:58:03 +010024.section .rodata.assert_str, "aS"
25assert_msg1:
26 .asciz "ASSERT: File "
27assert_msg2:
28 .asciz " Line "
29
30 /*
31 * This macro is intended to be used to print the
32 * line number in decimal. Used by asm_assert macro.
33 * The max number expected is 65536.
34 * In: x4 = the decimal to print.
35 * Clobber: x30, x0, x1, x2, x5, x6
36 */
37 .macro asm_print_line_dec
38 mov x6, #10 /* Divide by 10 after every loop iteration */
39 mov x5, #MAX_DEC_DIVISOR
Soby Mathewaecc0842014-08-19 11:26:00 +010040dec_print_loop:
Yann Gautier00a55fe2020-09-17 15:15:27 +020041 udiv x0, x4, x5 /* Get the quotient */
42 msub x4, x0, x5, x4 /* Find the remainder */
43 add x0, x0, #ASCII_OFFSET_NUM /* Convert to ascii */
Soby Mathewbc920122014-07-14 16:58:03 +010044 bl plat_crash_console_putc
Yann Gautier00a55fe2020-09-17 15:15:27 +020045 udiv x5, x5, x6 /* Reduce divisor */
Soby Mathewaecc0842014-08-19 11:26:00 +010046 cbnz x5, dec_print_loop
Soby Mathewbc920122014-07-14 16:58:03 +010047 .endm
48
49
50/* ---------------------------------------------------------------------------
51 * Assertion support in assembly.
52 * The below function helps to support assertions in assembly where we do not
53 * have a C runtime stack. Arguments to the function are :
54 * x0 - File name
55 * x1 - Line no
56 * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6.
57 * ---------------------------------------------------------------------------
58 */
59func asm_assert
Antonio Nino Diazcc8b5632017-04-18 15:16:05 +010060#if LOG_LEVEL >= LOG_LEVEL_INFO
61 /*
62 * Only print the output if LOG_LEVEL is higher or equal to
63 * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
64 */
Soby Mathewbc920122014-07-14 16:58:03 +010065 mov x5, x0
66 mov x6, x1
Yann Gautier00a55fe2020-09-17 15:15:27 +020067
Soby Mathewbc920122014-07-14 16:58:03 +010068 /* Ensure the console is initialized */
69 bl plat_crash_console_init
Yann Gautier00a55fe2020-09-17 15:15:27 +020070
Soby Mathewbc920122014-07-14 16:58:03 +010071 /* Check if the console is initialized */
72 cbz x0, _assert_loop
Yann Gautier00a55fe2020-09-17 15:15:27 +020073
Soby Mathewbc920122014-07-14 16:58:03 +010074 /* The console is initialized */
75 adr x4, assert_msg1
76 bl asm_print_str
77 mov x4, x5
78 bl asm_print_str
79 adr x4, assert_msg2
80 bl asm_print_str
Yann Gautier00a55fe2020-09-17 15:15:27 +020081
Soby Mathewbc920122014-07-14 16:58:03 +010082 /* Check if line number higher than max permitted */
83 tst x6, #~0xffff
84 b.ne _assert_loop
85 mov x4, x6
86 asm_print_line_dec
Antonio Nino Diaz801cf932017-02-17 17:11:27 +000087 bl plat_crash_console_flush
Soby Mathewbc920122014-07-14 16:58:03 +010088_assert_loop:
Antonio Nino Diazcc8b5632017-04-18 15:16:05 +010089#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
Antonio Nino Diaz1e09ff92017-02-16 16:49:18 +000090 no_ret plat_panic_handler
Kévin Petit8b779622015-03-24 14:03:57 +000091endfunc asm_assert
Antonio Nino Diaz044bb2f2017-04-20 09:58:28 +010092#endif /* ENABLE_ASSERTIONS */
Soby Mathewbc920122014-07-14 16:58:03 +010093
94/*
95 * This function prints a string from address in x4.
96 * In: x4 = pointer to string.
97 * Clobber: x30, x0, x1, x2, x3
98 */
99func asm_print_str
100 mov x3, x30
1011:
102 ldrb w0, [x4], #0x1
103 cbz x0, 2f
104 bl plat_crash_console_putc
105 b 1b
1062:
107 ret x3
Kévin Petit8b779622015-03-24 14:03:57 +0000108endfunc asm_print_str
Soby Mathewbc920122014-07-14 16:58:03 +0100109
110/*
111 * This function prints a hexadecimal number in x4.
112 * In: x4 = the hexadecimal to print.
Antonio Nino Diaz1c3ea102016-02-01 13:57:25 +0000113 * Clobber: x30, x0 - x3, x5
Soby Mathewbc920122014-07-14 16:58:03 +0100114 */
115func asm_print_hex
Soby Mathewbc920122014-07-14 16:58:03 +0100116 mov x5, #64 /* No of bits to convert to ascii */
Alexei Fedorov6c6a4702019-07-29 13:34:07 +0100117
118 /* Convert to ascii number of bits in x5 */
119asm_print_hex_bits:
120 mov x3, x30
Soby Mathewbc920122014-07-14 16:58:03 +01001211:
122 sub x5, x5, #4
123 lsrv x0, x4, x5
124 and x0, x0, #0xf
125 cmp x0, #0xA
126 b.lo 2f
127 /* Add by 0x27 in addition to ASCII_OFFSET_NUM
128 * to get ascii for characters 'a - f'.
129 */
130 add x0, x0, #0x27
1312:
132 add x0, x0, #ASCII_OFFSET_NUM
133 bl plat_crash_console_putc
134 cbnz x5, 1b
135 ret x3
Kévin Petit8b779622015-03-24 14:03:57 +0000136endfunc asm_print_hex
Soby Mathewbc920122014-07-14 16:58:03 +0100137
Justin Chadwell53d7e002019-08-20 10:58:49 +0100138/*
139 * Helper function to print newline to console
140 * Clobber: x0
141 */
142func asm_print_newline
143 mov x0, '\n'
144 b plat_crash_console_putc
145endfunc asm_print_newline
146
Soby Mathew626ed512014-06-25 10:07:40 +0100147 /***********************************************************
148 * The common implementation of do_panic for all BL stages
149 ***********************************************************/
150
151.section .rodata.panic_str, "aS"
152 panic_msg: .asciz "PANIC at PC : 0x"
153
154/* ---------------------------------------------------------------------------
155 * do_panic assumes that it is invoked from a C Runtime Environment ie a
156 * valid stack exists. This call will not return.
157 * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6
158 * ---------------------------------------------------------------------------
159 */
160
161/* This is for the non el3 BL stages to compile through */
162 .weak el3_panic
163
164func do_panic
165#if CRASH_REPORTING
Alexei Fedorovb4292bc2020-03-03 13:31:58 +0000166 b el3_panic
Alexei Fedorovb4292bc2020-03-03 13:31:58 +0000167#endif /* CRASH_REPORTING */
Soby Mathew626ed512014-06-25 10:07:40 +0100168
169panic_common:
170/*
171 * el3_panic will be redefined by the BL31
172 * crash reporting mechanism (if enabled)
173 */
174el3_panic:
175 mov x6, x30
176 bl plat_crash_console_init
Yann Gautier00a55fe2020-09-17 15:15:27 +0200177
Soby Mathew626ed512014-06-25 10:07:40 +0100178 /* Check if the console is initialized */
Antonio Nino Diaz1c3ea102016-02-01 13:57:25 +0000179 cbz x0, _panic_handler
Yann Gautier00a55fe2020-09-17 15:15:27 +0200180
Soby Mathew626ed512014-06-25 10:07:40 +0100181 /* The console is initialized */
182 adr x4, panic_msg
183 bl asm_print_str
184 mov x4, x6
Yann Gautier00a55fe2020-09-17 15:15:27 +0200185
Soby Mathew626ed512014-06-25 10:07:40 +0100186 /* The panic location is lr -4 */
187 sub x4, x4, #4
188 bl asm_print_hex
Soby Mathew626ed512014-06-25 10:07:40 +0100189
Pali Rohár805f22b2021-02-16 18:05:57 +0100190 /* Print new line */
191 bl asm_print_newline
192
Antonio Nino Diaz801cf932017-02-17 17:11:27 +0000193 bl plat_crash_console_flush
194
Antonio Nino Diaz1c3ea102016-02-01 13:57:25 +0000195_panic_handler:
196 /* Pass to plat_panic_handler the address from where el3_panic was
197 * called, not the address of the call from el3_panic. */
Julius Werner4d918382017-07-27 14:59:34 -0700198 mov x30, x6
199 b plat_panic_handler
Antonio Nino Diaz1c3ea102016-02-01 13:57:25 +0000200endfunc do_panic