xref: /rk3399_ARM-atf/common/aarch64/debug.S (revision dc2b8e8028c73a4c7a72d138caa26dc447a1d79a)
1626ed510SSoby Mathew/*
27e619eccSGovindraj Raja * Copyright (c) 2014-2023 Arm Limited and Contributors. All rights reserved.
3626ed510SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5626ed510SSoby Mathew */
6626ed510SSoby Mathew
7626ed510SSoby Mathew#include <arch.h>
8626ed510SSoby Mathew#include <asm_macros.S>
909d40e0eSAntonio Nino Diaz#include <common/debug.h>
10626ed510SSoby Mathew
11626ed510SSoby Mathew	.globl	asm_print_str
12626ed510SSoby Mathew	.globl	asm_print_hex
136c6a470fSAlexei Fedorov	.globl	asm_print_hex_bits
1453d7e003SJustin Chadwell	.globl	asm_print_newline
15626ed510SSoby Mathew	.globl	asm_assert
16f300ef66SGovindraj Raja	.globl	el3_panic
17*17d07a55SGovindraj Raja	.globl	elx_panic
18626ed510SSoby Mathew
19626ed510SSoby Mathew/* Since the max decimal input number is 65536 */
20626ed510SSoby Mathew#define MAX_DEC_DIVISOR		10000
21626ed510SSoby Mathew/* The offset to add to get ascii for numerals '0 - 9' */
22626ed510SSoby Mathew#define ASCII_OFFSET_NUM	0x30
23626ed510SSoby Mathew
24044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
25626ed510SSoby Mathew.section .rodata.assert_str, "aS"
26626ed510SSoby Mathewassert_msg1:
27626ed510SSoby Mathew	.asciz "ASSERT: File "
28626ed510SSoby Mathewassert_msg2:
29626ed510SSoby Mathew	.asciz " Line "
30626ed510SSoby Mathew
31626ed510SSoby Mathew	/*
32626ed510SSoby Mathew	 * This macro is intended to be used to print the
33626ed510SSoby Mathew	 * line number in decimal. Used by asm_assert macro.
34626ed510SSoby Mathew	 * The max number expected is 65536.
35626ed510SSoby Mathew	 * In: x4 = the decimal to print.
36626ed510SSoby Mathew	 * Clobber: x30, x0, x1, x2, x5, x6
37626ed510SSoby Mathew	 */
38626ed510SSoby Mathew	.macro asm_print_line_dec
39626ed510SSoby Mathew	mov	x6, #10		/* Divide by 10 after every loop iteration */
40626ed510SSoby Mathew	mov	x5, #MAX_DEC_DIVISOR
41aecc0840SSoby Mathewdec_print_loop:
42626ed510SSoby Mathew	udiv	x0, x4, x5			/* Get the quotient */
43626ed510SSoby Mathew	msub	x4, x0, x5, x4			/* Find the remainder */
44626ed510SSoby Mathew	add	x0, x0, #ASCII_OFFSET_NUM	/* Convert to ascii */
45626ed510SSoby Mathew	bl	plat_crash_console_putc
46626ed510SSoby Mathew	udiv	x5, x5, x6			/* Reduce divisor */
47aecc0840SSoby Mathew	cbnz	x5, dec_print_loop
48626ed510SSoby Mathew	.endm
49626ed510SSoby Mathew
50626ed510SSoby Mathew
51626ed510SSoby Mathew/* ---------------------------------------------------------------------------
52626ed510SSoby Mathew * Assertion support in assembly.
53626ed510SSoby Mathew * The below function helps to support assertions in assembly where we do not
54626ed510SSoby Mathew * have a C runtime stack. Arguments to the function are :
55626ed510SSoby Mathew * x0 - File name
56626ed510SSoby Mathew * x1 - Line no
57626ed510SSoby Mathew * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6.
58626ed510SSoby Mathew * ---------------------------------------------------------------------------
59626ed510SSoby Mathew */
60626ed510SSoby Mathewfunc asm_assert
61cc8b5632SAntonio Nino Diaz#if LOG_LEVEL >= LOG_LEVEL_INFO
62cc8b5632SAntonio Nino Diaz	/*
63cc8b5632SAntonio Nino Diaz	 * Only print the output if LOG_LEVEL is higher or equal to
64cc8b5632SAntonio Nino Diaz	 * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
65cc8b5632SAntonio Nino Diaz	 */
66626ed510SSoby Mathew	mov	x5, x0
67626ed510SSoby Mathew	mov	x6, x1
6800a55fe4SYann Gautier
69626ed510SSoby Mathew	/* Ensure the console is initialized */
70626ed510SSoby Mathew	bl	plat_crash_console_init
7100a55fe4SYann Gautier
72626ed510SSoby Mathew	/* Check if the console is initialized */
73626ed510SSoby Mathew	cbz	x0, _assert_loop
7400a55fe4SYann Gautier
75626ed510SSoby Mathew	/* The console is initialized */
76626ed510SSoby Mathew	adr	x4, assert_msg1
77626ed510SSoby Mathew	bl	asm_print_str
78626ed510SSoby Mathew	mov	x4, x5
79626ed510SSoby Mathew	bl	asm_print_str
80626ed510SSoby Mathew	adr	x4, assert_msg2
81626ed510SSoby Mathew	bl	asm_print_str
8200a55fe4SYann Gautier
83626ed510SSoby Mathew	/* Check if line number higher than max permitted */
84626ed510SSoby Mathew	tst	x6, #~0xffff
85626ed510SSoby Mathew	b.ne	_assert_loop
86626ed510SSoby Mathew	mov	x4, x6
87626ed510SSoby Mathew	asm_print_line_dec
88801cf93cSAntonio Nino Diaz	bl	plat_crash_console_flush
89626ed510SSoby Mathew_assert_loop:
90cc8b5632SAntonio Nino Diaz#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
911e09ff93SAntonio Nino Diaz	no_ret	plat_panic_handler
928b779620SKévin Petitendfunc asm_assert
93044bb2faSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */
94626ed510SSoby Mathew
95626ed510SSoby Mathew/*
96626ed510SSoby Mathew * This function prints a string from address in x4.
97626ed510SSoby Mathew * In: x4 = pointer to string.
98626ed510SSoby Mathew * Clobber: x30, x0, x1, x2, x3
99626ed510SSoby Mathew */
100626ed510SSoby Mathewfunc asm_print_str
101626ed510SSoby Mathew	mov	x3, x30
102626ed510SSoby Mathew1:
103626ed510SSoby Mathew	ldrb	w0, [x4], #0x1
104626ed510SSoby Mathew	cbz	x0, 2f
105626ed510SSoby Mathew	bl	plat_crash_console_putc
106626ed510SSoby Mathew	b	1b
107626ed510SSoby Mathew2:
108626ed510SSoby Mathew	ret	x3
1098b779620SKévin Petitendfunc asm_print_str
110626ed510SSoby Mathew
111626ed510SSoby Mathew/*
112626ed510SSoby Mathew * This function prints a hexadecimal number in x4.
113626ed510SSoby Mathew * In: x4 = the hexadecimal to print.
1141c3ea103SAntonio Nino Diaz * Clobber: x30, x0 - x3, x5
115626ed510SSoby Mathew */
116626ed510SSoby Mathewfunc asm_print_hex
117626ed510SSoby Mathew	mov	x5, #64  /* No of bits to convert to ascii */
1186c6a470fSAlexei Fedorov
1196c6a470fSAlexei Fedorov	/* Convert to ascii number of bits in x5 */
1206c6a470fSAlexei Fedorovasm_print_hex_bits:
1216c6a470fSAlexei Fedorov	mov	x3, x30
122626ed510SSoby Mathew1:
123626ed510SSoby Mathew	sub	x5, x5, #4
124626ed510SSoby Mathew	lsrv	x0, x4, x5
125626ed510SSoby Mathew	and	x0, x0, #0xf
126626ed510SSoby Mathew	cmp	x0, #0xA
127626ed510SSoby Mathew	b.lo	2f
128626ed510SSoby Mathew	/* Add by 0x27 in addition to ASCII_OFFSET_NUM
129626ed510SSoby Mathew	 * to get ascii for characters 'a - f'.
130626ed510SSoby Mathew	 */
131626ed510SSoby Mathew	add	x0, x0, #0x27
132626ed510SSoby Mathew2:
133626ed510SSoby Mathew	add	x0, x0, #ASCII_OFFSET_NUM
134626ed510SSoby Mathew	bl	plat_crash_console_putc
135626ed510SSoby Mathew	cbnz	x5, 1b
136626ed510SSoby Mathew	ret	x3
1378b779620SKévin Petitendfunc asm_print_hex
138626ed510SSoby Mathew
13953d7e003SJustin Chadwell/*
14053d7e003SJustin Chadwell * Helper function to print newline to console
14153d7e003SJustin Chadwell * Clobber: x0
14253d7e003SJustin Chadwell */
14353d7e003SJustin Chadwellfunc asm_print_newline
14453d7e003SJustin Chadwell	mov	x0, '\n'
14553d7e003SJustin Chadwell	b	plat_crash_console_putc
14653d7e003SJustin Chadwellendfunc asm_print_newline
14753d7e003SJustin Chadwell
148626ed510SSoby Mathew	/***********************************************************
149bd62ce98SGovindraj Raja	 * The common implementation of el3_panic for all BL stages
150626ed510SSoby Mathew	 ***********************************************************/
151626ed510SSoby Mathew
152626ed510SSoby Mathew.section .rodata.panic_str, "aS"
153626ed510SSoby Mathew	panic_msg: .asciz "PANIC at PC : 0x"
154626ed510SSoby Mathew
155*17d07a55SGovindraj Rajafunc elx_panic
156*17d07a55SGovindraj Raja#if CRASH_REPORTING && defined(IMAGE_BL31)
157*17d07a55SGovindraj Raja	b	report_elx_panic
158*17d07a55SGovindraj Raja#endif /* CRASH_REPORTING && IMAGE_BL31 */
159*17d07a55SGovindraj Raja
160*17d07a55SGovindraj Raja	b	panic_common
161*17d07a55SGovindraj Rajaendfunc elx_panic
162*17d07a55SGovindraj Raja
163626ed510SSoby Mathew/* ---------------------------------------------------------------------------
164bd62ce98SGovindraj Raja * el3_panic assumes that it is invoked from a C Runtime Environment ie a
165626ed510SSoby Mathew * valid stack exists. This call will not return.
166626ed510SSoby Mathew * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6
167626ed510SSoby Mathew * ---------------------------------------------------------------------------
168626ed510SSoby Mathew */
169626ed510SSoby Mathew
170bd62ce98SGovindraj Rajafunc el3_panic
171f300ef66SGovindraj Raja#if CRASH_REPORTING && defined(IMAGE_BL31)
172f300ef66SGovindraj Raja	b	report_el3_panic
173f300ef66SGovindraj Raja#endif /* CRASH_REPORTING && IMAGE_BL31 */
174626ed510SSoby Mathew
175*17d07a55SGovindraj Rajapanic_common:
176626ed510SSoby Mathew	mov	x6, x30
177626ed510SSoby Mathew	bl	plat_crash_console_init
17800a55fe4SYann Gautier
179626ed510SSoby Mathew	/* Check if the console is initialized */
1801c3ea103SAntonio Nino Diaz	cbz	x0, _panic_handler
18100a55fe4SYann Gautier
182626ed510SSoby Mathew	/* The console is initialized */
183626ed510SSoby Mathew	adr	x4, panic_msg
184626ed510SSoby Mathew	bl	asm_print_str
185626ed510SSoby Mathew	mov	x4, x6
18600a55fe4SYann Gautier
187626ed510SSoby Mathew	/* The panic location is lr -4 */
188626ed510SSoby Mathew	sub	x4, x4, #4
189626ed510SSoby Mathew	bl	asm_print_hex
190626ed510SSoby Mathew
191805f22baSPali Rohár	/* Print new line */
192805f22baSPali Rohár	bl	asm_print_newline
193805f22baSPali Rohár
194801cf93cSAntonio Nino Diaz	bl	plat_crash_console_flush
195801cf93cSAntonio Nino Diaz
1961c3ea103SAntonio Nino Diaz_panic_handler:
1971c3ea103SAntonio Nino Diaz	/* Pass to plat_panic_handler the address from where el3_panic was
1981c3ea103SAntonio Nino Diaz	 * called, not the address of the call from el3_panic. */
1991c3ea103SAntonio Nino Diaz	mov	x30, x6
2004d91838bSJulius Werner	b	plat_panic_handler
201f300ef66SGovindraj Raja
202bd62ce98SGovindraj Rajaendfunc el3_panic
203