xref: /rk3399_ARM-atf/common/aarch64/debug.S (revision bd62ce98d2c9ef164456c6477b2e21172165dc11)
1/*
2 * Copyright (c) 2014-2023 Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch.h>
8#include <asm_macros.S>
9#include <common/debug.h>
10
11	.globl	asm_print_str
12	.globl	asm_print_hex
13	.globl	asm_print_hex_bits
14	.globl	asm_print_newline
15	.globl	asm_assert
16	.globl	el3_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
23#if ENABLE_ASSERTIONS
24.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
40dec_print_loop:
41	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 */
46	cbnz	x5, dec_print_loop
47	.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
60#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	 */
65	mov	x5, x0
66	mov	x6, x1
67
68	/* Ensure the console is initialized */
69	bl	plat_crash_console_init
70
71	/* Check if the console is initialized */
72	cbz	x0, _assert_loop
73
74	/* 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
81
82	/* 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
87	bl	plat_crash_console_flush
88_assert_loop:
89#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
90	no_ret	plat_panic_handler
91endfunc asm_assert
92#endif /* ENABLE_ASSERTIONS */
93
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
108endfunc asm_print_str
109
110/*
111 * This function prints a hexadecimal number in x4.
112 * In: x4 = the hexadecimal to print.
113 * Clobber: x30, x0 - x3, x5
114 */
115func asm_print_hex
116	mov	x5, #64  /* No of bits to convert to ascii */
117
118	/* Convert to ascii number of bits in x5 */
119asm_print_hex_bits:
120	mov	x3, x30
1211:
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
136endfunc asm_print_hex
137
138/*
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
147	/***********************************************************
148	 * The common implementation of el3_panic for all BL stages
149	 ***********************************************************/
150
151.section .rodata.panic_str, "aS"
152	panic_msg: .asciz "PANIC at PC : 0x"
153
154/* ---------------------------------------------------------------------------
155 * el3_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
161func el3_panic
162#if CRASH_REPORTING && defined(IMAGE_BL31)
163	b	report_el3_panic
164#endif /* CRASH_REPORTING && IMAGE_BL31 */
165
166	mov	x6, x30
167	bl	plat_crash_console_init
168
169	/* Check if the console is initialized */
170	cbz	x0, _panic_handler
171
172	/* The console is initialized */
173	adr	x4, panic_msg
174	bl	asm_print_str
175	mov	x4, x6
176
177	/* The panic location is lr -4 */
178	sub	x4, x4, #4
179	bl	asm_print_hex
180
181	/* Print new line */
182	bl	asm_print_newline
183
184	bl	plat_crash_console_flush
185
186_panic_handler:
187	/* Pass to plat_panic_handler the address from where el3_panic was
188	 * called, not the address of the call from el3_panic. */
189	mov	x30, x6
190	b	plat_panic_handler
191
192endfunc el3_panic