blob: d794d12eadef31ace56ee3dc521ef48dc6021fd6 [file] [log] [blame]
Soby Mathew041f62a2014-07-14 16:58:03 +01001/*
Antonio Nino Diazd3ec5432017-02-17 17:11:27 +00002 * Copyright (c) 2014-2017, 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>
9
10 .globl asm_print_str
11 .globl asm_print_hex
12 .globl asm_assert
13 .globl do_panic
14
15/* Since the max decimal input number is 65536 */
16#define MAX_DEC_DIVISOR 10000
17/* The offset to add to get ascii for numerals '0 - 9' */
18#define ASCII_OFFSET_NUM 0x30
19
Antonio Nino Diaz7c65c1e2017-04-20 09:58:28 +010020#if ENABLE_ASSERTIONS
Soby Mathew041f62a2014-07-14 16:58:03 +010021.section .rodata.assert_str, "aS"
22assert_msg1:
23 .asciz "ASSERT: File "
24assert_msg2:
25 .asciz " Line "
26
27 /*
28 * This macro is intended to be used to print the
29 * line number in decimal. Used by asm_assert macro.
30 * The max number expected is 65536.
31 * In: x4 = the decimal to print.
32 * Clobber: x30, x0, x1, x2, x5, x6
33 */
34 .macro asm_print_line_dec
35 mov x6, #10 /* Divide by 10 after every loop iteration */
36 mov x5, #MAX_DEC_DIVISOR
Soby Mathew1218f112014-08-19 11:26:00 +010037dec_print_loop:
Soby Mathew041f62a2014-07-14 16:58:03 +010038 udiv x0, x4, x5 /* Get the quotient */
39 msub x4, x0, x5, x4 /* Find the remainder */
40 add x0, x0, #ASCII_OFFSET_NUM /* Convert to ascii */
41 bl plat_crash_console_putc
42 udiv x5, x5, x6 /* Reduce divisor */
Soby Mathew1218f112014-08-19 11:26:00 +010043 cbnz x5, dec_print_loop
Soby Mathew041f62a2014-07-14 16:58:03 +010044 .endm
45
46
47/* ---------------------------------------------------------------------------
48 * Assertion support in assembly.
49 * The below function helps to support assertions in assembly where we do not
50 * have a C runtime stack. Arguments to the function are :
51 * x0 - File name
52 * x1 - Line no
53 * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6.
54 * ---------------------------------------------------------------------------
55 */
56func asm_assert
Antonio Nino Diaz808c2292017-04-18 15:16:05 +010057#if LOG_LEVEL >= LOG_LEVEL_INFO
58 /*
59 * Only print the output if LOG_LEVEL is higher or equal to
60 * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
61 */
Soby Mathew041f62a2014-07-14 16:58:03 +010062 mov x5, x0
63 mov x6, x1
64 /* Ensure the console is initialized */
65 bl plat_crash_console_init
66 /* Check if the console is initialized */
67 cbz x0, _assert_loop
68 /* The console is initialized */
69 adr x4, assert_msg1
70 bl asm_print_str
71 mov x4, x5
72 bl asm_print_str
73 adr x4, assert_msg2
74 bl asm_print_str
75 /* Check if line number higher than max permitted */
76 tst x6, #~0xffff
77 b.ne _assert_loop
78 mov x4, x6
79 asm_print_line_dec
Antonio Nino Diazd3ec5432017-02-17 17:11:27 +000080 bl plat_crash_console_flush
Soby Mathew041f62a2014-07-14 16:58:03 +010081_assert_loop:
Antonio Nino Diaz808c2292017-04-18 15:16:05 +010082#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
Antonio Nino Diaze98fa3a2017-02-16 16:49:18 +000083 no_ret plat_panic_handler
Kévin Petita877c252015-03-24 14:03:57 +000084endfunc asm_assert
Antonio Nino Diaz7c65c1e2017-04-20 09:58:28 +010085#endif /* ENABLE_ASSERTIONS */
Soby Mathew041f62a2014-07-14 16:58:03 +010086
87/*
88 * This function prints a string from address in x4.
89 * In: x4 = pointer to string.
90 * Clobber: x30, x0, x1, x2, x3
91 */
92func asm_print_str
93 mov x3, x30
941:
95 ldrb w0, [x4], #0x1
96 cbz x0, 2f
97 bl plat_crash_console_putc
98 b 1b
992:
100 ret x3
Kévin Petita877c252015-03-24 14:03:57 +0000101endfunc asm_print_str
Soby Mathew041f62a2014-07-14 16:58:03 +0100102
103/*
104 * This function prints a hexadecimal number in x4.
105 * In: x4 = the hexadecimal to print.
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000106 * Clobber: x30, x0 - x3, x5
Soby Mathew041f62a2014-07-14 16:58:03 +0100107 */
108func asm_print_hex
109 mov x3, x30
110 mov x5, #64 /* No of bits to convert to ascii */
1111:
112 sub x5, x5, #4
113 lsrv x0, x4, x5
114 and x0, x0, #0xf
115 cmp x0, #0xA
116 b.lo 2f
117 /* Add by 0x27 in addition to ASCII_OFFSET_NUM
118 * to get ascii for characters 'a - f'.
119 */
120 add x0, x0, #0x27
1212:
122 add x0, x0, #ASCII_OFFSET_NUM
123 bl plat_crash_console_putc
124 cbnz x5, 1b
125 ret x3
Kévin Petita877c252015-03-24 14:03:57 +0000126endfunc asm_print_hex
Soby Mathew041f62a2014-07-14 16:58:03 +0100127
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100128 /***********************************************************
129 * The common implementation of do_panic for all BL stages
130 ***********************************************************/
131
132.section .rodata.panic_str, "aS"
133 panic_msg: .asciz "PANIC at PC : 0x"
134
135/* ---------------------------------------------------------------------------
136 * do_panic assumes that it is invoked from a C Runtime Environment ie a
137 * valid stack exists. This call will not return.
138 * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6
139 * ---------------------------------------------------------------------------
140 */
141
142/* This is for the non el3 BL stages to compile through */
143 .weak el3_panic
144
145func do_panic
146#if CRASH_REPORTING
147 str x0, [sp, #-0x10]!
148 mrs x0, currentel
149 ubfx x0, x0, #2, #2
150 cmp x0, #0x3
151 ldr x0, [sp], #0x10
152 b.eq el3_panic
153#endif
154
155panic_common:
156/*
157 * el3_panic will be redefined by the BL31
158 * crash reporting mechanism (if enabled)
159 */
160el3_panic:
161 mov x6, x30
162 bl plat_crash_console_init
163 /* Check if the console is initialized */
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000164 cbz x0, _panic_handler
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100165 /* The console is initialized */
166 adr x4, panic_msg
167 bl asm_print_str
168 mov x4, x6
169 /* The panic location is lr -4 */
170 sub x4, x4, #4
171 bl asm_print_hex
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100172
Antonio Nino Diazd3ec5432017-02-17 17:11:27 +0000173 bl plat_crash_console_flush
174
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000175_panic_handler:
176 /* Pass to plat_panic_handler the address from where el3_panic was
177 * called, not the address of the call from el3_panic. */
Julius Werner67ebde72017-07-27 14:59:34 -0700178 mov x30, x6
179 b plat_panic_handler
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000180endfunc do_panic