xref: /rk3399_ARM-atf/common/aarch32/debug.S (revision 801cf93c48585a83fda5d576b25c7b7f3cd59259)
1f24307deSSoby Mathew/*
2*801cf93cSAntonio Nino Diaz * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
3f24307deSSoby Mathew *
4f24307deSSoby Mathew * Redistribution and use in source and binary forms, with or without
5f24307deSSoby Mathew * modification, are permitted provided that the following conditions are met:
6f24307deSSoby Mathew *
7f24307deSSoby Mathew * Redistributions of source code must retain the above copyright notice, this
8f24307deSSoby Mathew * list of conditions and the following disclaimer.
9f24307deSSoby Mathew *
10f24307deSSoby Mathew * Redistributions in binary form must reproduce the above copyright notice,
11f24307deSSoby Mathew * this list of conditions and the following disclaimer in the documentation
12f24307deSSoby Mathew * and/or other materials provided with the distribution.
13f24307deSSoby Mathew *
14f24307deSSoby Mathew * Neither the name of ARM nor the names of its contributors may be used
15f24307deSSoby Mathew * to endorse or promote products derived from this software without specific
16f24307deSSoby Mathew * prior written permission.
17f24307deSSoby Mathew *
18f24307deSSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19f24307deSSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20f24307deSSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21f24307deSSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22f24307deSSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23f24307deSSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24f24307deSSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25f24307deSSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26f24307deSSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27f24307deSSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28f24307deSSoby Mathew * POSSIBILITY OF SUCH DAMAGE.
29f24307deSSoby Mathew */
30f24307deSSoby Mathew
31f24307deSSoby Mathew#include <arch.h>
32f24307deSSoby Mathew#include <asm_macros.S>
33f24307deSSoby Mathew
34e12cb61fSJeenu Viswambharan	.globl	asm_assert
35f24307deSSoby Mathew	.globl	do_panic
361a0a3f06SYatharth Kochar	.globl	report_exception
37f24307deSSoby Mathew
38e12cb61fSJeenu Viswambharan/* Since the max decimal input number is 65536 */
39e12cb61fSJeenu Viswambharan#define MAX_DEC_DIVISOR		10000
40e12cb61fSJeenu Viswambharan
41e12cb61fSJeenu Viswambharan/* The offset to add to get ascii for numerals '0 - 9' */
42e12cb61fSJeenu Viswambharan#define ASCII_OFFSET_NUM	'0'
43e12cb61fSJeenu Viswambharan
44e12cb61fSJeenu Viswambharan	.section .rodata.panic_str, "aS"
45e12cb61fSJeenu Viswambharanpanic_msg:
46e12cb61fSJeenu Viswambharan	.asciz "PANIC at PC : 0x"
47e12cb61fSJeenu Viswambharanpanic_end:
48e12cb61fSJeenu Viswambharan	.asciz "\r\n"
49e12cb61fSJeenu Viswambharan
50f24307deSSoby Mathew	/***********************************************************
51f24307deSSoby Mathew	 * The common implementation of do_panic for all BL stages
52f24307deSSoby Mathew	 ***********************************************************/
53f24307deSSoby Mathewfunc do_panic
54e12cb61fSJeenu Viswambharan	/* Have LR copy point to PC at the time of panic */
55e12cb61fSJeenu Viswambharan	sub	r6, lr, #4
56e12cb61fSJeenu Viswambharan
57e12cb61fSJeenu Viswambharan	/* Initialize crash console and verify success */
58e12cb61fSJeenu Viswambharan	bl	plat_crash_console_init
59e12cb61fSJeenu Viswambharan	cmp	r0, #0
60e12cb61fSJeenu Viswambharan	beq	1f
61e12cb61fSJeenu Viswambharan
62e12cb61fSJeenu Viswambharan	/* Print panic message */
63e12cb61fSJeenu Viswambharan	ldr	r4, =panic_msg
64e12cb61fSJeenu Viswambharan	bl	asm_print_str
65e12cb61fSJeenu Viswambharan
66e12cb61fSJeenu Viswambharan	/* Print LR in hex */
67e12cb61fSJeenu Viswambharan	mov	r4, r6
68e12cb61fSJeenu Viswambharan	bl	asm_print_hex
69e12cb61fSJeenu Viswambharan
70e12cb61fSJeenu Viswambharan	/* Print new line */
71e12cb61fSJeenu Viswambharan	ldr	r4, =panic_end
72e12cb61fSJeenu Viswambharan	bl	asm_print_str
73*801cf93cSAntonio Nino Diaz
74*801cf93cSAntonio Nino Diaz	bl	plat_crash_console_flush
75*801cf93cSAntonio Nino Diaz
76e12cb61fSJeenu Viswambharan1:
77e12cb61fSJeenu Viswambharan	mov	lr, r6
78e12cb61fSJeenu Viswambharan	b	plat_panic_handler
79f24307deSSoby Mathewendfunc do_panic
80f24307deSSoby Mathew
811a0a3f06SYatharth Kochar	/***********************************************************
821a0a3f06SYatharth Kochar	 * This function is called from the vector table for
831a0a3f06SYatharth Kochar	 * unhandled exceptions. It reads the current mode and
841a0a3f06SYatharth Kochar	 * passes it to platform.
851a0a3f06SYatharth Kochar	 ***********************************************************/
861a0a3f06SYatharth Kocharfunc report_exception
871a0a3f06SYatharth Kochar	mrs	r0, cpsr
881a0a3f06SYatharth Kochar	and	r0, #MODE32_MASK
891a0a3f06SYatharth Kochar	bl	plat_report_exception
90a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
911a0a3f06SYatharth Kocharendfunc report_exception
92e12cb61fSJeenu Viswambharan
93e12cb61fSJeenu Viswambharan#if ASM_ASSERTION
94e12cb61fSJeenu Viswambharan.section .rodata.assert_str, "aS"
95e12cb61fSJeenu Viswambharanassert_msg1:
96e12cb61fSJeenu Viswambharan	.asciz "ASSERT: File "
97e12cb61fSJeenu Viswambharanassert_msg2:
98e12cb61fSJeenu Viswambharan	.asciz " Line "
99e12cb61fSJeenu Viswambharan
100e12cb61fSJeenu Viswambharan/* ---------------------------------------------------------------------------
101e12cb61fSJeenu Viswambharan * Assertion support in assembly.
102e12cb61fSJeenu Viswambharan * The below function helps to support assertions in assembly where we do not
103e12cb61fSJeenu Viswambharan * have a C runtime stack. Arguments to the function are :
104e12cb61fSJeenu Viswambharan * r0 - File name
105e12cb61fSJeenu Viswambharan * r1 - Line no
106e12cb61fSJeenu Viswambharan * Clobber list : lr, r0 - r6
107e12cb61fSJeenu Viswambharan * ---------------------------------------------------------------------------
108e12cb61fSJeenu Viswambharan */
109e12cb61fSJeenu Viswambharanfunc asm_assert
110e12cb61fSJeenu Viswambharan	/* Stash the parameters already in r0 and r1 */
111e12cb61fSJeenu Viswambharan	mov	r5, r0
112e12cb61fSJeenu Viswambharan	mov	r6, r1
113e12cb61fSJeenu Viswambharan
114e12cb61fSJeenu Viswambharan	/* Initialize crash console and verify success */
115e12cb61fSJeenu Viswambharan	bl	plat_crash_console_init
116e12cb61fSJeenu Viswambharan	cmp	r0, #0
117e12cb61fSJeenu Viswambharan	beq	1f
118e12cb61fSJeenu Viswambharan
119e12cb61fSJeenu Viswambharan	/* Print file name */
120e12cb61fSJeenu Viswambharan	ldr	r4, =assert_msg1
121e12cb61fSJeenu Viswambharan	bl	asm_print_str
122e12cb61fSJeenu Viswambharan	mov	r4, r5
123e12cb61fSJeenu Viswambharan	bl	asm_print_str
124e12cb61fSJeenu Viswambharan
125e12cb61fSJeenu Viswambharan	/* Print line number string */
126e12cb61fSJeenu Viswambharan	ldr	r4, =assert_msg2
127e12cb61fSJeenu Viswambharan	bl	asm_print_str
128e12cb61fSJeenu Viswambharan
129e12cb61fSJeenu Viswambharan	/* Test for maximum supported line number */
130e12cb61fSJeenu Viswambharan	ldr	r4, =~0xffff
131e12cb61fSJeenu Viswambharan	tst	r6, r4
132e12cb61fSJeenu Viswambharan	bne	1f
133e12cb61fSJeenu Viswambharan	mov	r4, r6
134e12cb61fSJeenu Viswambharan
135e12cb61fSJeenu Viswambharan	/* Print line number in decimal */
136e12cb61fSJeenu Viswambharan	mov	r6, #10			/* Divide by 10 after every loop iteration */
137e12cb61fSJeenu Viswambharan	ldr	r5, =MAX_DEC_DIVISOR
138e12cb61fSJeenu Viswambharandec_print_loop:
139e12cb61fSJeenu Viswambharan	udiv	r0, r4, r5			/* Quotient */
140e12cb61fSJeenu Viswambharan	mls	r4, r0, r5, r4			/* Remainder */
141e12cb61fSJeenu Viswambharan	add	r0, r0, #ASCII_OFFSET_NUM	/* Convert to ASCII */
142e12cb61fSJeenu Viswambharan	bl	plat_crash_console_putc
143e12cb61fSJeenu Viswambharan	udiv	r5, r5, r6			/* Reduce divisor */
144e12cb61fSJeenu Viswambharan	cmp	r5, #0
145e12cb61fSJeenu Viswambharan	bne	dec_print_loop
146*801cf93cSAntonio Nino Diaz
147*801cf93cSAntonio Nino Diaz	bl	plat_crash_console_flush
148*801cf93cSAntonio Nino Diaz
149e12cb61fSJeenu Viswambharan1:
150e12cb61fSJeenu Viswambharan	no_ret	plat_panic_handler
151e12cb61fSJeenu Viswambharanendfunc asm_assert
152e12cb61fSJeenu Viswambharan#endif
153e12cb61fSJeenu Viswambharan
154e12cb61fSJeenu Viswambharan/*
155e12cb61fSJeenu Viswambharan * This function prints a string from address in r4
156e12cb61fSJeenu Viswambharan * Clobber: lr, r0 - r4
157e12cb61fSJeenu Viswambharan */
158e12cb61fSJeenu Viswambharanfunc asm_print_str
159e12cb61fSJeenu Viswambharan	mov	r3, lr
160e12cb61fSJeenu Viswambharan1:
161e12cb61fSJeenu Viswambharan	ldrb	r0, [r4], #0x1
162e12cb61fSJeenu Viswambharan	cmp	r0, #0
163e12cb61fSJeenu Viswambharan	beq	2f
164e12cb61fSJeenu Viswambharan	bl	plat_crash_console_putc
165e12cb61fSJeenu Viswambharan	b	1b
166e12cb61fSJeenu Viswambharan2:
167e12cb61fSJeenu Viswambharan	bx	r3
168e12cb61fSJeenu Viswambharanendfunc asm_print_str
169e12cb61fSJeenu Viswambharan
170e12cb61fSJeenu Viswambharan/*
171e12cb61fSJeenu Viswambharan * This function prints a hexadecimal number in r4.
172e12cb61fSJeenu Viswambharan * In: r4 = the hexadecimal to print.
173e12cb61fSJeenu Viswambharan * Clobber: lr, r0 - r3, r5
174e12cb61fSJeenu Viswambharan */
175e12cb61fSJeenu Viswambharanfunc asm_print_hex
176e12cb61fSJeenu Viswambharan	mov	r3, lr
177e12cb61fSJeenu Viswambharan	mov	r5, #32  /* No of bits to convert to ascii */
178e12cb61fSJeenu Viswambharan1:
179e12cb61fSJeenu Viswambharan	sub	r5, r5, #4
180e12cb61fSJeenu Viswambharan	lsr	r0, r4, r5
181e12cb61fSJeenu Viswambharan	and	r0, r0, #0xf
182e12cb61fSJeenu Viswambharan	cmp	r0, #0xa
183e12cb61fSJeenu Viswambharan	blo	2f
184e12cb61fSJeenu Viswambharan	/* Add by 0x27 in addition to ASCII_OFFSET_NUM
185e12cb61fSJeenu Viswambharan	 * to get ascii for characters 'a - f'.
186e12cb61fSJeenu Viswambharan	 */
187e12cb61fSJeenu Viswambharan	add	r0, r0, #0x27
188e12cb61fSJeenu Viswambharan2:
189e12cb61fSJeenu Viswambharan	add	r0, r0, #ASCII_OFFSET_NUM
190e12cb61fSJeenu Viswambharan	bl	plat_crash_console_putc
191e12cb61fSJeenu Viswambharan	cmp	r5, #0
192e12cb61fSJeenu Viswambharan	bne	1b
193e12cb61fSJeenu Viswambharan	bx	r3
194e12cb61fSJeenu Viswambharanendfunc asm_print_hex
195