blob: 7db24396e1071263ec65bbcf2c50b2659f02c9e2 [file] [log] [blame]
Soby Mathew041f62a2014-07-14 16:58:03 +01001/*
Alexei Fedorov813c9f92020-03-03 13:31:58 +00002 * Copyright (c) 2014-2020, 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
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 Diaz7c65c1e2017-04-20 09:58:28 +010023#if ENABLE_ASSERTIONS
Soby Mathew041f62a2014-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 Mathew1218f112014-08-19 11:26:00 +010040dec_print_loop:
Soby Mathew041f62a2014-07-14 16:58:03 +010041 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 */
44 bl plat_crash_console_putc
45 udiv x5, x5, x6 /* Reduce divisor */
Soby Mathew1218f112014-08-19 11:26:00 +010046 cbnz x5, dec_print_loop
Soby Mathew041f62a2014-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 Diaz808c2292017-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 Mathew041f62a2014-07-14 16:58:03 +010065 mov x5, x0
66 mov x6, x1
67 /* Ensure the console is initialized */
68 bl plat_crash_console_init
69 /* Check if the console is initialized */
70 cbz x0, _assert_loop
71 /* The console is initialized */
72 adr x4, assert_msg1
73 bl asm_print_str
74 mov x4, x5
75 bl asm_print_str
76 adr x4, assert_msg2
77 bl asm_print_str
78 /* Check if line number higher than max permitted */
79 tst x6, #~0xffff
80 b.ne _assert_loop
81 mov x4, x6
82 asm_print_line_dec
Antonio Nino Diazd3ec5432017-02-17 17:11:27 +000083 bl plat_crash_console_flush
Soby Mathew041f62a2014-07-14 16:58:03 +010084_assert_loop:
Antonio Nino Diaz808c2292017-04-18 15:16:05 +010085#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
Antonio Nino Diaze98fa3a2017-02-16 16:49:18 +000086 no_ret plat_panic_handler
Kévin Petita877c252015-03-24 14:03:57 +000087endfunc asm_assert
Antonio Nino Diaz7c65c1e2017-04-20 09:58:28 +010088#endif /* ENABLE_ASSERTIONS */
Soby Mathew041f62a2014-07-14 16:58:03 +010089
90/*
91 * This function prints a string from address in x4.
92 * In: x4 = pointer to string.
93 * Clobber: x30, x0, x1, x2, x3
94 */
95func asm_print_str
96 mov x3, x30
971:
98 ldrb w0, [x4], #0x1
99 cbz x0, 2f
100 bl plat_crash_console_putc
101 b 1b
1022:
103 ret x3
Kévin Petita877c252015-03-24 14:03:57 +0000104endfunc asm_print_str
Soby Mathew041f62a2014-07-14 16:58:03 +0100105
106/*
107 * This function prints a hexadecimal number in x4.
108 * In: x4 = the hexadecimal to print.
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000109 * Clobber: x30, x0 - x3, x5
Soby Mathew041f62a2014-07-14 16:58:03 +0100110 */
111func asm_print_hex
Soby Mathew041f62a2014-07-14 16:58:03 +0100112 mov x5, #64 /* No of bits to convert to ascii */
Alexei Fedorovc4dfb3b2019-07-29 13:34:07 +0100113
114 /* Convert to ascii number of bits in x5 */
115asm_print_hex_bits:
116 mov x3, x30
Soby Mathew041f62a2014-07-14 16:58:03 +01001171:
118 sub x5, x5, #4
119 lsrv x0, x4, x5
120 and x0, x0, #0xf
121 cmp x0, #0xA
122 b.lo 2f
123 /* Add by 0x27 in addition to ASCII_OFFSET_NUM
124 * to get ascii for characters 'a - f'.
125 */
126 add x0, x0, #0x27
1272:
128 add x0, x0, #ASCII_OFFSET_NUM
129 bl plat_crash_console_putc
130 cbnz x5, 1b
131 ret x3
Kévin Petita877c252015-03-24 14:03:57 +0000132endfunc asm_print_hex
Soby Mathew041f62a2014-07-14 16:58:03 +0100133
Justin Chadwell76903822019-08-20 10:58:49 +0100134/*
135 * Helper function to print newline to console
136 * Clobber: x0
137 */
138func asm_print_newline
139 mov x0, '\n'
140 b plat_crash_console_putc
141endfunc asm_print_newline
142
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100143 /***********************************************************
144 * The common implementation of do_panic for all BL stages
145 ***********************************************************/
146
147.section .rodata.panic_str, "aS"
148 panic_msg: .asciz "PANIC at PC : 0x"
149
150/* ---------------------------------------------------------------------------
151 * do_panic assumes that it is invoked from a C Runtime Environment ie a
152 * valid stack exists. This call will not return.
153 * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6
154 * ---------------------------------------------------------------------------
155 */
156
157/* This is for the non el3 BL stages to compile through */
158 .weak el3_panic
Alexei Fedorov813c9f92020-03-03 13:31:58 +0000159 .weak elx_panic
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100160
161func do_panic
162#if CRASH_REPORTING
163 str x0, [sp, #-0x10]!
164 mrs x0, currentel
Alexei Fedorov813c9f92020-03-03 13:31:58 +0000165 ubfx x0, x0, #MODE_EL_SHIFT, #MODE_EL_WIDTH
166 cmp x0, #MODE_EL3
167#if !HANDLE_EA_EL3_FIRST
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100168 ldr x0, [sp], #0x10
169 b.eq el3_panic
Alexei Fedorov813c9f92020-03-03 13:31:58 +0000170#else
171 b.ne to_panic_common
172
173 /* Check EL the exception taken from */
174 mrs x0, spsr_el3
175 ubfx x0, x0, #SPSR_EL_SHIFT, #SPSR_EL_WIDTH
176 cmp x0, #MODE_EL3
177 b.ne elx_panic
178 ldr x0, [sp], #0x10
179 b el3_panic
180
181to_panic_common:
182 ldr x0, [sp], #0x10
183#endif /* HANDLE_EA_EL3_FIRST */
184#endif /* CRASH_REPORTING */
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100185
186panic_common:
187/*
188 * el3_panic will be redefined by the BL31
189 * crash reporting mechanism (if enabled)
190 */
191el3_panic:
192 mov x6, x30
193 bl plat_crash_console_init
194 /* Check if the console is initialized */
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000195 cbz x0, _panic_handler
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100196 /* The console is initialized */
197 adr x4, panic_msg
198 bl asm_print_str
199 mov x4, x6
200 /* The panic location is lr -4 */
201 sub x4, x4, #4
202 bl asm_print_hex
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100203
Antonio Nino Diazd3ec5432017-02-17 17:11:27 +0000204 bl plat_crash_console_flush
205
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000206_panic_handler:
207 /* Pass to plat_panic_handler the address from where el3_panic was
208 * called, not the address of the call from el3_panic. */
Julius Werner67ebde72017-07-27 14:59:34 -0700209 mov x30, x6
210 b plat_panic_handler
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000211endfunc do_panic