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