xref: /rk3399_ARM-atf/common/aarch64/debug.S (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
1626ed510SSoby Mathew/*
2801cf93cSAntonio Nino Diaz * Copyright (c) 2014-2017, 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>
9*09d40e0eSAntonio Nino Diaz#include <common/debug.h>
10626ed510SSoby Mathew
11626ed510SSoby Mathew	.globl	asm_print_str
12626ed510SSoby Mathew	.globl	asm_print_hex
13626ed510SSoby Mathew	.globl	asm_assert
14626ed510SSoby Mathew	.globl	do_panic
15626ed510SSoby Mathew
16626ed510SSoby Mathew/* Since the max decimal input number is 65536 */
17626ed510SSoby Mathew#define MAX_DEC_DIVISOR		10000
18626ed510SSoby Mathew/* The offset to add to get ascii for numerals '0 - 9' */
19626ed510SSoby Mathew#define ASCII_OFFSET_NUM	0x30
20626ed510SSoby Mathew
21044bb2faSAntonio Nino Diaz#if ENABLE_ASSERTIONS
22626ed510SSoby Mathew.section .rodata.assert_str, "aS"
23626ed510SSoby Mathewassert_msg1:
24626ed510SSoby Mathew	.asciz "ASSERT: File "
25626ed510SSoby Mathewassert_msg2:
26626ed510SSoby Mathew	.asciz " Line "
27626ed510SSoby Mathew
28626ed510SSoby Mathew	/*
29626ed510SSoby Mathew	 * This macro is intended to be used to print the
30626ed510SSoby Mathew	 * line number in decimal. Used by asm_assert macro.
31626ed510SSoby Mathew	 * The max number expected is 65536.
32626ed510SSoby Mathew	 * In: x4 = the decimal to print.
33626ed510SSoby Mathew	 * Clobber: x30, x0, x1, x2, x5, x6
34626ed510SSoby Mathew	 */
35626ed510SSoby Mathew	.macro asm_print_line_dec
36626ed510SSoby Mathew	mov	x6, #10		/* Divide by 10 after every loop iteration */
37626ed510SSoby Mathew	mov	x5, #MAX_DEC_DIVISOR
38aecc0840SSoby Mathewdec_print_loop:
39626ed510SSoby Mathew	udiv	x0, x4, x5		/* Get the quotient */
40626ed510SSoby Mathew	msub	x4, x0, x5, x4		/* Find the remainder */
41626ed510SSoby Mathew	add	x0, x0, #ASCII_OFFSET_NUM		/* Convert to ascii */
42626ed510SSoby Mathew	bl	plat_crash_console_putc
43626ed510SSoby Mathew	udiv	x5, x5, x6		/* Reduce divisor */
44aecc0840SSoby Mathew	cbnz	x5, dec_print_loop
45626ed510SSoby Mathew	.endm
46626ed510SSoby Mathew
47626ed510SSoby Mathew
48626ed510SSoby Mathew/* ---------------------------------------------------------------------------
49626ed510SSoby Mathew * Assertion support in assembly.
50626ed510SSoby Mathew * The below function helps to support assertions in assembly where we do not
51626ed510SSoby Mathew * have a C runtime stack. Arguments to the function are :
52626ed510SSoby Mathew * x0 - File name
53626ed510SSoby Mathew * x1 - Line no
54626ed510SSoby Mathew * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6.
55626ed510SSoby Mathew * ---------------------------------------------------------------------------
56626ed510SSoby Mathew */
57626ed510SSoby Mathewfunc asm_assert
58cc8b5632SAntonio Nino Diaz#if LOG_LEVEL >= LOG_LEVEL_INFO
59cc8b5632SAntonio Nino Diaz	/*
60cc8b5632SAntonio Nino Diaz	 * Only print the output if LOG_LEVEL is higher or equal to
61cc8b5632SAntonio Nino Diaz	 * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
62cc8b5632SAntonio Nino Diaz	 */
63626ed510SSoby Mathew	mov	x5, x0
64626ed510SSoby Mathew	mov	x6, x1
65626ed510SSoby Mathew	/* Ensure the console is initialized */
66626ed510SSoby Mathew	bl	plat_crash_console_init
67626ed510SSoby Mathew	/* Check if the console is initialized */
68626ed510SSoby Mathew	cbz	x0, _assert_loop
69626ed510SSoby Mathew	/* The console is initialized */
70626ed510SSoby Mathew	adr	x4, assert_msg1
71626ed510SSoby Mathew	bl	asm_print_str
72626ed510SSoby Mathew	mov	x4, x5
73626ed510SSoby Mathew	bl	asm_print_str
74626ed510SSoby Mathew	adr	x4, assert_msg2
75626ed510SSoby Mathew	bl	asm_print_str
76626ed510SSoby Mathew	/* Check if line number higher than max permitted */
77626ed510SSoby Mathew	tst	x6, #~0xffff
78626ed510SSoby Mathew	b.ne	_assert_loop
79626ed510SSoby Mathew	mov	x4, x6
80626ed510SSoby Mathew	asm_print_line_dec
81801cf93cSAntonio Nino Diaz	bl	plat_crash_console_flush
82626ed510SSoby Mathew_assert_loop:
83cc8b5632SAntonio Nino Diaz#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
841e09ff93SAntonio Nino Diaz	no_ret	plat_panic_handler
858b779620SKévin Petitendfunc asm_assert
86044bb2faSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */
87626ed510SSoby Mathew
88626ed510SSoby Mathew/*
89626ed510SSoby Mathew * This function prints a string from address in x4.
90626ed510SSoby Mathew * In: x4 = pointer to string.
91626ed510SSoby Mathew * Clobber: x30, x0, x1, x2, x3
92626ed510SSoby Mathew */
93626ed510SSoby Mathewfunc asm_print_str
94626ed510SSoby Mathew	mov	x3, x30
95626ed510SSoby Mathew1:
96626ed510SSoby Mathew	ldrb	w0, [x4], #0x1
97626ed510SSoby Mathew	cbz	x0, 2f
98626ed510SSoby Mathew	bl	plat_crash_console_putc
99626ed510SSoby Mathew	b	1b
100626ed510SSoby Mathew2:
101626ed510SSoby Mathew	ret	x3
1028b779620SKévin Petitendfunc asm_print_str
103626ed510SSoby Mathew
104626ed510SSoby Mathew/*
105626ed510SSoby Mathew * This function prints a hexadecimal number in x4.
106626ed510SSoby Mathew * In: x4 = the hexadecimal to print.
1071c3ea103SAntonio Nino Diaz * Clobber: x30, x0 - x3, x5
108626ed510SSoby Mathew */
109626ed510SSoby Mathewfunc asm_print_hex
110626ed510SSoby Mathew	mov	x3, x30
111626ed510SSoby Mathew	mov	x5, #64  /* No of bits to convert to ascii */
112626ed510SSoby Mathew1:
113626ed510SSoby Mathew	sub	x5, x5, #4
114626ed510SSoby Mathew	lsrv	x0, x4, x5
115626ed510SSoby Mathew	and	x0, x0, #0xf
116626ed510SSoby Mathew	cmp	x0, #0xA
117626ed510SSoby Mathew	b.lo	2f
118626ed510SSoby Mathew	/* Add by 0x27 in addition to ASCII_OFFSET_NUM
119626ed510SSoby Mathew	 * to get ascii for characters 'a - f'.
120626ed510SSoby Mathew	 */
121626ed510SSoby Mathew	add	x0, x0, #0x27
122626ed510SSoby Mathew2:
123626ed510SSoby Mathew	add	x0, x0, #ASCII_OFFSET_NUM
124626ed510SSoby Mathew	bl	plat_crash_console_putc
125626ed510SSoby Mathew	cbnz	x5, 1b
126626ed510SSoby Mathew	ret	x3
1278b779620SKévin Petitendfunc asm_print_hex
128626ed510SSoby Mathew
129626ed510SSoby Mathew	/***********************************************************
130626ed510SSoby Mathew	 * The common implementation of do_panic for all BL stages
131626ed510SSoby Mathew	 ***********************************************************/
132626ed510SSoby Mathew
133626ed510SSoby Mathew.section .rodata.panic_str, "aS"
134626ed510SSoby Mathew	panic_msg: .asciz "PANIC at PC : 0x"
135626ed510SSoby Mathew
136626ed510SSoby Mathew/* ---------------------------------------------------------------------------
137626ed510SSoby Mathew * do_panic assumes that it is invoked from a C Runtime Environment ie a
138626ed510SSoby Mathew * valid stack exists. This call will not return.
139626ed510SSoby Mathew * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6
140626ed510SSoby Mathew * ---------------------------------------------------------------------------
141626ed510SSoby Mathew */
142626ed510SSoby Mathew
143626ed510SSoby Mathew/* This is for the non el3 BL stages to compile through */
144626ed510SSoby Mathew	.weak el3_panic
145626ed510SSoby Mathew
146626ed510SSoby Mathewfunc do_panic
147626ed510SSoby Mathew#if CRASH_REPORTING
148626ed510SSoby Mathew	str	x0, [sp, #-0x10]!
149626ed510SSoby Mathew	mrs	x0, currentel
150626ed510SSoby Mathew	ubfx	x0, x0, #2, #2
151626ed510SSoby Mathew	cmp	x0, #0x3
152626ed510SSoby Mathew	ldr	x0, [sp], #0x10
153626ed510SSoby Mathew	b.eq	el3_panic
154626ed510SSoby Mathew#endif
155626ed510SSoby Mathew
156626ed510SSoby Mathewpanic_common:
157626ed510SSoby Mathew/*
158626ed510SSoby Mathew * el3_panic will be redefined by the BL31
159626ed510SSoby Mathew * crash reporting mechanism (if enabled)
160626ed510SSoby Mathew */
161626ed510SSoby Mathewel3_panic:
162626ed510SSoby Mathew	mov	x6, x30
163626ed510SSoby Mathew	bl	plat_crash_console_init
164626ed510SSoby Mathew	/* Check if the console is initialized */
1651c3ea103SAntonio Nino Diaz	cbz	x0, _panic_handler
166626ed510SSoby Mathew	/* The console is initialized */
167626ed510SSoby Mathew	adr	x4, panic_msg
168626ed510SSoby Mathew	bl	asm_print_str
169626ed510SSoby Mathew	mov	x4, x6
170626ed510SSoby Mathew	/* The panic location is lr -4 */
171626ed510SSoby Mathew	sub	x4, x4, #4
172626ed510SSoby Mathew	bl	asm_print_hex
173626ed510SSoby Mathew
174801cf93cSAntonio Nino Diaz	bl	plat_crash_console_flush
175801cf93cSAntonio Nino Diaz
1761c3ea103SAntonio Nino Diaz_panic_handler:
1771c3ea103SAntonio Nino Diaz	/* Pass to plat_panic_handler the address from where el3_panic was
1781c3ea103SAntonio Nino Diaz	 * called, not the address of the call from el3_panic. */
1791c3ea103SAntonio Nino Diaz	mov	x30, x6
1804d91838bSJulius Werner	b	plat_panic_handler
1811c3ea103SAntonio Nino Diazendfunc do_panic
182