blob: 8768a1f749228a1b0329b27156f19d3a65d93966 [file] [log] [blame]
Soby Mathew041f62a2014-07-14 16:58:03 +01001/*
Govindraj Rajaabf9fb92023-01-16 15:11:47 +00002 * Copyright (c) 2014-2023 Arm Limited and Contributors. All rights reserved.
Soby Mathew041f62a2014-07-14 16:58:03 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathew041f62a2014-07-14 16:58:03 +01005 */
6
7#include <arch.h>
8#include <asm_macros.S>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <common/debug.h>
Soby Mathew041f62a2014-07-14 16:58:03 +010010
11 .globl asm_print_str
12 .globl asm_print_hex
Alexei Fedorovc4dfb3b2019-07-29 13:34:07 +010013 .globl asm_print_hex_bits
Justin Chadwell76903822019-08-20 10:58:49 +010014 .globl asm_print_newline
Soby Mathew041f62a2014-07-14 16:58:03 +010015 .globl asm_assert
Govindraj Raja6eeadb32023-01-16 16:44:45 +000016 .globl el3_panic
Govindraj Rajab6709b02023-02-21 17:43:55 +000017 .globl elx_panic
Soby Mathew041f62a2014-07-14 16:58:03 +010018
19/* Since the max decimal input number is 65536 */
20#define MAX_DEC_DIVISOR 10000
21/* The offset to add to get ascii for numerals '0 - 9' */
22#define ASCII_OFFSET_NUM 0x30
23
Antonio Nino Diaz7c65c1e2017-04-20 09:58:28 +010024#if ENABLE_ASSERTIONS
Soby Mathew041f62a2014-07-14 16:58:03 +010025.section .rodata.assert_str, "aS"
26assert_msg1:
27 .asciz "ASSERT: File "
28assert_msg2:
29 .asciz " Line "
30
31 /*
32 * This macro is intended to be used to print the
33 * line number in decimal. Used by asm_assert macro.
34 * The max number expected is 65536.
35 * In: x4 = the decimal to print.
36 * Clobber: x30, x0, x1, x2, x5, x6
37 */
38 .macro asm_print_line_dec
39 mov x6, #10 /* Divide by 10 after every loop iteration */
40 mov x5, #MAX_DEC_DIVISOR
Soby Mathew1218f112014-08-19 11:26:00 +010041dec_print_loop:
Yann Gautierd04c7ae2020-09-17 15:15:27 +020042 udiv x0, x4, x5 /* Get the quotient */
43 msub x4, x0, x5, x4 /* Find the remainder */
44 add x0, x0, #ASCII_OFFSET_NUM /* Convert to ascii */
Soby Mathew041f62a2014-07-14 16:58:03 +010045 bl plat_crash_console_putc
Yann Gautierd04c7ae2020-09-17 15:15:27 +020046 udiv x5, x5, x6 /* Reduce divisor */
Soby Mathew1218f112014-08-19 11:26:00 +010047 cbnz x5, dec_print_loop
Soby Mathew041f62a2014-07-14 16:58:03 +010048 .endm
49
50
51/* ---------------------------------------------------------------------------
52 * Assertion support in assembly.
53 * The below function helps to support assertions in assembly where we do not
54 * have a C runtime stack. Arguments to the function are :
55 * x0 - File name
56 * x1 - Line no
57 * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6.
58 * ---------------------------------------------------------------------------
59 */
60func asm_assert
Antonio Nino Diaz808c2292017-04-18 15:16:05 +010061#if LOG_LEVEL >= LOG_LEVEL_INFO
62 /*
63 * Only print the output if LOG_LEVEL is higher or equal to
64 * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
65 */
Soby Mathew041f62a2014-07-14 16:58:03 +010066 mov x5, x0
67 mov x6, x1
Yann Gautierd04c7ae2020-09-17 15:15:27 +020068
Soby Mathew041f62a2014-07-14 16:58:03 +010069 /* Ensure the console is initialized */
70 bl plat_crash_console_init
Yann Gautierd04c7ae2020-09-17 15:15:27 +020071
Soby Mathew041f62a2014-07-14 16:58:03 +010072 /* Check if the console is initialized */
73 cbz x0, _assert_loop
Yann Gautierd04c7ae2020-09-17 15:15:27 +020074
Soby Mathew041f62a2014-07-14 16:58:03 +010075 /* The console is initialized */
76 adr x4, assert_msg1
77 bl asm_print_str
78 mov x4, x5
79 bl asm_print_str
80 adr x4, assert_msg2
81 bl asm_print_str
Yann Gautierd04c7ae2020-09-17 15:15:27 +020082
Soby Mathew041f62a2014-07-14 16:58:03 +010083 /* Check if line number higher than max permitted */
84 tst x6, #~0xffff
85 b.ne _assert_loop
86 mov x4, x6
87 asm_print_line_dec
Antonio Nino Diazd3ec5432017-02-17 17:11:27 +000088 bl plat_crash_console_flush
Soby Mathew041f62a2014-07-14 16:58:03 +010089_assert_loop:
Antonio Nino Diaz808c2292017-04-18 15:16:05 +010090#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
Antonio Nino Diaze98fa3a2017-02-16 16:49:18 +000091 no_ret plat_panic_handler
Kévin Petita877c252015-03-24 14:03:57 +000092endfunc asm_assert
Antonio Nino Diaz7c65c1e2017-04-20 09:58:28 +010093#endif /* ENABLE_ASSERTIONS */
Soby Mathew041f62a2014-07-14 16:58:03 +010094
95/*
96 * This function prints a string from address in x4.
97 * In: x4 = pointer to string.
98 * Clobber: x30, x0, x1, x2, x3
99 */
100func asm_print_str
101 mov x3, x30
1021:
103 ldrb w0, [x4], #0x1
104 cbz x0, 2f
105 bl plat_crash_console_putc
106 b 1b
1072:
108 ret x3
Kévin Petita877c252015-03-24 14:03:57 +0000109endfunc asm_print_str
Soby Mathew041f62a2014-07-14 16:58:03 +0100110
111/*
112 * This function prints a hexadecimal number in x4.
113 * In: x4 = the hexadecimal to print.
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000114 * Clobber: x30, x0 - x3, x5
Soby Mathew041f62a2014-07-14 16:58:03 +0100115 */
116func asm_print_hex
Soby Mathew041f62a2014-07-14 16:58:03 +0100117 mov x5, #64 /* No of bits to convert to ascii */
Alexei Fedorovc4dfb3b2019-07-29 13:34:07 +0100118
119 /* Convert to ascii number of bits in x5 */
120asm_print_hex_bits:
121 mov x3, x30
Soby Mathew041f62a2014-07-14 16:58:03 +01001221:
123 sub x5, x5, #4
124 lsrv x0, x4, x5
125 and x0, x0, #0xf
126 cmp x0, #0xA
127 b.lo 2f
128 /* Add by 0x27 in addition to ASCII_OFFSET_NUM
129 * to get ascii for characters 'a - f'.
130 */
131 add x0, x0, #0x27
1322:
133 add x0, x0, #ASCII_OFFSET_NUM
134 bl plat_crash_console_putc
135 cbnz x5, 1b
136 ret x3
Kévin Petita877c252015-03-24 14:03:57 +0000137endfunc asm_print_hex
Soby Mathew041f62a2014-07-14 16:58:03 +0100138
Justin Chadwell76903822019-08-20 10:58:49 +0100139/*
140 * Helper function to print newline to console
141 * Clobber: x0
142 */
143func asm_print_newline
144 mov x0, '\n'
145 b plat_crash_console_putc
146endfunc asm_print_newline
147
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100148 /***********************************************************
Govindraj Rajaa796b1b2023-01-16 17:35:07 +0000149 * The common implementation of el3_panic for all BL stages
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100150 ***********************************************************/
151
152.section .rodata.panic_str, "aS"
153 panic_msg: .asciz "PANIC at PC : 0x"
154
Govindraj Rajab6709b02023-02-21 17:43:55 +0000155func elx_panic
156#if CRASH_REPORTING && defined(IMAGE_BL31)
157 b report_elx_panic
158#endif /* CRASH_REPORTING && IMAGE_BL31 */
159
160 b panic_common
161endfunc elx_panic
162
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100163/* ---------------------------------------------------------------------------
Govindraj Rajaa796b1b2023-01-16 17:35:07 +0000164 * el3_panic assumes that it is invoked from a C Runtime Environment ie a
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100165 * valid stack exists. This call will not return.
166 * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6
167 * ---------------------------------------------------------------------------
168 */
169
Govindraj Rajaa796b1b2023-01-16 17:35:07 +0000170func el3_panic
Govindraj Raja6eeadb32023-01-16 16:44:45 +0000171#if CRASH_REPORTING && defined(IMAGE_BL31)
172 b report_el3_panic
173#endif /* CRASH_REPORTING && IMAGE_BL31 */
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100174
Govindraj Rajab6709b02023-02-21 17:43:55 +0000175panic_common:
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100176 mov x6, x30
177 bl plat_crash_console_init
Yann Gautierd04c7ae2020-09-17 15:15:27 +0200178
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100179 /* Check if the console is initialized */
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000180 cbz x0, _panic_handler
Yann Gautierd04c7ae2020-09-17 15:15:27 +0200181
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100182 /* The console is initialized */
183 adr x4, panic_msg
184 bl asm_print_str
185 mov x4, x6
Yann Gautierd04c7ae2020-09-17 15:15:27 +0200186
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100187 /* The panic location is lr -4 */
188 sub x4, x4, #4
189 bl asm_print_hex
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100190
Pali Rohár7b4e2d62021-02-16 18:05:57 +0100191 /* Print new line */
192 bl asm_print_newline
193
Antonio Nino Diazd3ec5432017-02-17 17:11:27 +0000194 bl plat_crash_console_flush
195
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000196_panic_handler:
197 /* Pass to plat_panic_handler the address from where el3_panic was
198 * called, not the address of the call from el3_panic. */
Julius Werner67ebde72017-07-27 14:59:34 -0700199 mov x30, x6
200 b plat_panic_handler
Govindraj Raja6eeadb32023-01-16 16:44:45 +0000201
Govindraj Rajab6709b02023-02-21 17:43:55 +0000202endfunc el3_panic