xref: /rk3399_ARM-atf/common/aarch32/debug.S (revision e12cb61f0e7d8678a2cd6ef73f0a38af1d460e08)
1f24307deSSoby Mathew/*
2f24307deSSoby Mathew * Copyright (c) 2016, 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
34*e12cb61fSJeenu Viswambharan	.globl	asm_assert
35f24307deSSoby Mathew	.globl	do_panic
361a0a3f06SYatharth Kochar	.globl	report_exception
37f24307deSSoby Mathew
38*e12cb61fSJeenu Viswambharan/* Since the max decimal input number is 65536 */
39*e12cb61fSJeenu Viswambharan#define MAX_DEC_DIVISOR		10000
40*e12cb61fSJeenu Viswambharan
41*e12cb61fSJeenu Viswambharan/* The offset to add to get ascii for numerals '0 - 9' */
42*e12cb61fSJeenu Viswambharan#define ASCII_OFFSET_NUM	'0'
43*e12cb61fSJeenu Viswambharan
44*e12cb61fSJeenu Viswambharan	.section .rodata.panic_str, "aS"
45*e12cb61fSJeenu Viswambharanpanic_msg:
46*e12cb61fSJeenu Viswambharan	.asciz "PANIC at PC : 0x"
47*e12cb61fSJeenu Viswambharanpanic_end:
48*e12cb61fSJeenu Viswambharan	.asciz "\r\n"
49*e12cb61fSJeenu Viswambharan
50f24307deSSoby Mathew	/***********************************************************
51f24307deSSoby Mathew	 * The common implementation of do_panic for all BL stages
52f24307deSSoby Mathew	 ***********************************************************/
53f24307deSSoby Mathewfunc do_panic
54*e12cb61fSJeenu Viswambharan	/* Have LR copy point to PC at the time of panic */
55*e12cb61fSJeenu Viswambharan	sub	r6, lr, #4
56*e12cb61fSJeenu Viswambharan
57*e12cb61fSJeenu Viswambharan	/* Initialize crash console and verify success */
58*e12cb61fSJeenu Viswambharan	bl	plat_crash_console_init
59*e12cb61fSJeenu Viswambharan	cmp	r0, #0
60*e12cb61fSJeenu Viswambharan	beq	1f
61*e12cb61fSJeenu Viswambharan
62*e12cb61fSJeenu Viswambharan	/* Print panic message */
63*e12cb61fSJeenu Viswambharan	ldr	r4, =panic_msg
64*e12cb61fSJeenu Viswambharan	bl	asm_print_str
65*e12cb61fSJeenu Viswambharan
66*e12cb61fSJeenu Viswambharan	/* Print LR in hex */
67*e12cb61fSJeenu Viswambharan	mov	r4, r6
68*e12cb61fSJeenu Viswambharan	bl	asm_print_hex
69*e12cb61fSJeenu Viswambharan
70*e12cb61fSJeenu Viswambharan	/* Print new line */
71*e12cb61fSJeenu Viswambharan	ldr	r4, =panic_end
72*e12cb61fSJeenu Viswambharan	bl	asm_print_str
73*e12cb61fSJeenu Viswambharan1:
74*e12cb61fSJeenu Viswambharan	mov	lr, r6
75*e12cb61fSJeenu Viswambharan	b	plat_panic_handler
76f24307deSSoby Mathewendfunc do_panic
77f24307deSSoby Mathew
781a0a3f06SYatharth Kochar	/***********************************************************
791a0a3f06SYatharth Kochar	 * This function is called from the vector table for
801a0a3f06SYatharth Kochar	 * unhandled exceptions. It reads the current mode and
811a0a3f06SYatharth Kochar	 * passes it to platform.
821a0a3f06SYatharth Kochar	 ***********************************************************/
831a0a3f06SYatharth Kocharfunc report_exception
841a0a3f06SYatharth Kochar	mrs	r0, cpsr
851a0a3f06SYatharth Kochar	and	r0, #MODE32_MASK
861a0a3f06SYatharth Kochar	bl	plat_report_exception
87a806dad5SJeenu Viswambharan	no_ret	plat_panic_handler
881a0a3f06SYatharth Kocharendfunc report_exception
89*e12cb61fSJeenu Viswambharan
90*e12cb61fSJeenu Viswambharan#if ASM_ASSERTION
91*e12cb61fSJeenu Viswambharan.section .rodata.assert_str, "aS"
92*e12cb61fSJeenu Viswambharanassert_msg1:
93*e12cb61fSJeenu Viswambharan	.asciz "ASSERT: File "
94*e12cb61fSJeenu Viswambharanassert_msg2:
95*e12cb61fSJeenu Viswambharan	.asciz " Line "
96*e12cb61fSJeenu Viswambharan
97*e12cb61fSJeenu Viswambharan/* ---------------------------------------------------------------------------
98*e12cb61fSJeenu Viswambharan * Assertion support in assembly.
99*e12cb61fSJeenu Viswambharan * The below function helps to support assertions in assembly where we do not
100*e12cb61fSJeenu Viswambharan * have a C runtime stack. Arguments to the function are :
101*e12cb61fSJeenu Viswambharan * r0 - File name
102*e12cb61fSJeenu Viswambharan * r1 - Line no
103*e12cb61fSJeenu Viswambharan * Clobber list : lr, r0 - r6
104*e12cb61fSJeenu Viswambharan * ---------------------------------------------------------------------------
105*e12cb61fSJeenu Viswambharan */
106*e12cb61fSJeenu Viswambharanfunc asm_assert
107*e12cb61fSJeenu Viswambharan	/* Stash the parameters already in r0 and r1 */
108*e12cb61fSJeenu Viswambharan	mov	r5, r0
109*e12cb61fSJeenu Viswambharan	mov	r6, r1
110*e12cb61fSJeenu Viswambharan
111*e12cb61fSJeenu Viswambharan	/* Initialize crash console and verify success */
112*e12cb61fSJeenu Viswambharan	bl	plat_crash_console_init
113*e12cb61fSJeenu Viswambharan	cmp	r0, #0
114*e12cb61fSJeenu Viswambharan	beq	1f
115*e12cb61fSJeenu Viswambharan
116*e12cb61fSJeenu Viswambharan	/* Print file name */
117*e12cb61fSJeenu Viswambharan	ldr	r4, =assert_msg1
118*e12cb61fSJeenu Viswambharan	bl	asm_print_str
119*e12cb61fSJeenu Viswambharan	mov	r4, r5
120*e12cb61fSJeenu Viswambharan	bl	asm_print_str
121*e12cb61fSJeenu Viswambharan
122*e12cb61fSJeenu Viswambharan	/* Print line number string */
123*e12cb61fSJeenu Viswambharan	ldr	r4, =assert_msg2
124*e12cb61fSJeenu Viswambharan	bl	asm_print_str
125*e12cb61fSJeenu Viswambharan
126*e12cb61fSJeenu Viswambharan	/* Test for maximum supported line number */
127*e12cb61fSJeenu Viswambharan	ldr	r4, =~0xffff
128*e12cb61fSJeenu Viswambharan	tst	r6, r4
129*e12cb61fSJeenu Viswambharan	bne	1f
130*e12cb61fSJeenu Viswambharan	mov	r4, r6
131*e12cb61fSJeenu Viswambharan
132*e12cb61fSJeenu Viswambharan	/* Print line number in decimal */
133*e12cb61fSJeenu Viswambharan	mov	r6, #10			/* Divide by 10 after every loop iteration */
134*e12cb61fSJeenu Viswambharan	ldr	r5, =MAX_DEC_DIVISOR
135*e12cb61fSJeenu Viswambharandec_print_loop:
136*e12cb61fSJeenu Viswambharan	udiv	r0, r4, r5			/* Quotient */
137*e12cb61fSJeenu Viswambharan	mls	r4, r0, r5, r4			/* Remainder */
138*e12cb61fSJeenu Viswambharan	add	r0, r0, #ASCII_OFFSET_NUM	/* Convert to ASCII */
139*e12cb61fSJeenu Viswambharan	bl	plat_crash_console_putc
140*e12cb61fSJeenu Viswambharan	udiv	r5, r5, r6			/* Reduce divisor */
141*e12cb61fSJeenu Viswambharan	cmp	r5, #0
142*e12cb61fSJeenu Viswambharan	bne	dec_print_loop
143*e12cb61fSJeenu Viswambharan1:
144*e12cb61fSJeenu Viswambharan	no_ret	plat_panic_handler
145*e12cb61fSJeenu Viswambharanendfunc asm_assert
146*e12cb61fSJeenu Viswambharan#endif
147*e12cb61fSJeenu Viswambharan
148*e12cb61fSJeenu Viswambharan/*
149*e12cb61fSJeenu Viswambharan * This function prints a string from address in r4
150*e12cb61fSJeenu Viswambharan * Clobber: lr, r0 - r4
151*e12cb61fSJeenu Viswambharan */
152*e12cb61fSJeenu Viswambharanfunc asm_print_str
153*e12cb61fSJeenu Viswambharan	mov	r3, lr
154*e12cb61fSJeenu Viswambharan1:
155*e12cb61fSJeenu Viswambharan	ldrb	r0, [r4], #0x1
156*e12cb61fSJeenu Viswambharan	cmp	r0, #0
157*e12cb61fSJeenu Viswambharan	beq	2f
158*e12cb61fSJeenu Viswambharan	bl	plat_crash_console_putc
159*e12cb61fSJeenu Viswambharan	b	1b
160*e12cb61fSJeenu Viswambharan2:
161*e12cb61fSJeenu Viswambharan	bx	r3
162*e12cb61fSJeenu Viswambharanendfunc asm_print_str
163*e12cb61fSJeenu Viswambharan
164*e12cb61fSJeenu Viswambharan/*
165*e12cb61fSJeenu Viswambharan * This function prints a hexadecimal number in r4.
166*e12cb61fSJeenu Viswambharan * In: r4 = the hexadecimal to print.
167*e12cb61fSJeenu Viswambharan * Clobber: lr, r0 - r3, r5
168*e12cb61fSJeenu Viswambharan */
169*e12cb61fSJeenu Viswambharanfunc asm_print_hex
170*e12cb61fSJeenu Viswambharan	mov	r3, lr
171*e12cb61fSJeenu Viswambharan	mov	r5, #32  /* No of bits to convert to ascii */
172*e12cb61fSJeenu Viswambharan1:
173*e12cb61fSJeenu Viswambharan	sub	r5, r5, #4
174*e12cb61fSJeenu Viswambharan	lsr	r0, r4, r5
175*e12cb61fSJeenu Viswambharan	and	r0, r0, #0xf
176*e12cb61fSJeenu Viswambharan	cmp	r0, #0xa
177*e12cb61fSJeenu Viswambharan	blo	2f
178*e12cb61fSJeenu Viswambharan	/* Add by 0x27 in addition to ASCII_OFFSET_NUM
179*e12cb61fSJeenu Viswambharan	 * to get ascii for characters 'a - f'.
180*e12cb61fSJeenu Viswambharan	 */
181*e12cb61fSJeenu Viswambharan	add	r0, r0, #0x27
182*e12cb61fSJeenu Viswambharan2:
183*e12cb61fSJeenu Viswambharan	add	r0, r0, #ASCII_OFFSET_NUM
184*e12cb61fSJeenu Viswambharan	bl	plat_crash_console_putc
185*e12cb61fSJeenu Viswambharan	cmp	r5, #0
186*e12cb61fSJeenu Viswambharan	bne	1b
187*e12cb61fSJeenu Viswambharan	bx	r3
188*e12cb61fSJeenu Viswambharanendfunc asm_print_hex
189