xref: /rk3399_ARM-atf/common/aarch32/debug.S (revision b10d44995eb652675863c2cc6a7726683613da0d)
1/*
2 * Copyright (c) 2016-2017, 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
73
74	bl	plat_crash_console_flush
75
761:
77	mov	lr, r6
78	no_ret	plat_panic_handler
79endfunc do_panic
80
81	/***********************************************************
82	 * This function is called from the vector table for
83	 * unhandled exceptions. It reads the current mode and
84	 * passes it to platform.
85	 ***********************************************************/
86func report_exception
87	mrs	r0, cpsr
88	and	r0, #MODE32_MASK
89	bl	plat_report_exception
90	no_ret	plat_panic_handler
91endfunc report_exception
92
93#if ENABLE_ASSERTIONS
94.section .rodata.assert_str, "aS"
95assert_msg1:
96	.asciz "ASSERT: File "
97assert_msg2:
98	.asciz " Line "
99
100/* ---------------------------------------------------------------------------
101 * Assertion support in assembly.
102 * The below function helps to support assertions in assembly where we do not
103 * have a C runtime stack. Arguments to the function are :
104 * r0 - File name
105 * r1 - Line no
106 * Clobber list : lr, r0 - r6
107 * ---------------------------------------------------------------------------
108 */
109func asm_assert
110#if LOG_LEVEL >= LOG_LEVEL_INFO
111	/*
112	 * Only print the output if LOG_LEVEL is higher or equal to
113	 * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
114	 */
115	/* Stash the parameters already in r0 and r1 */
116	mov	r5, r0
117	mov	r6, r1
118
119	/* Initialize crash console and verify success */
120	bl	plat_crash_console_init
121	cmp	r0, #0
122	beq	1f
123
124	/* Print file name */
125	ldr	r4, =assert_msg1
126	bl	asm_print_str
127	mov	r4, r5
128	bl	asm_print_str
129
130	/* Print line number string */
131	ldr	r4, =assert_msg2
132	bl	asm_print_str
133
134	/* Test for maximum supported line number */
135	ldr	r4, =~0xffff
136	tst	r6, r4
137	bne	1f
138	mov	r4, r6
139
140	/* Print line number in decimal */
141	mov	r6, #10			/* Divide by 10 after every loop iteration */
142	ldr	r5, =MAX_DEC_DIVISOR
143dec_print_loop:
144	udiv	r0, r4, r5			/* Quotient */
145	mls	r4, r0, r5, r4			/* Remainder */
146	add	r0, r0, #ASCII_OFFSET_NUM	/* Convert to ASCII */
147	bl	plat_crash_console_putc
148	udiv	r5, r5, r6			/* Reduce divisor */
149	cmp	r5, #0
150	bne	dec_print_loop
151
152	bl	plat_crash_console_flush
153
1541:
155#endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
156	no_ret	plat_panic_handler
157endfunc asm_assert
158#endif /* ENABLE_ASSERTIONS */
159
160/*
161 * This function prints a string from address in r4
162 * Clobber: lr, r0 - r4
163 */
164func asm_print_str
165	mov	r3, lr
1661:
167	ldrb	r0, [r4], #0x1
168	cmp	r0, #0
169	beq	2f
170	bl	plat_crash_console_putc
171	b	1b
1722:
173	bx	r3
174endfunc asm_print_str
175
176/*
177 * This function prints a hexadecimal number in r4.
178 * In: r4 = the hexadecimal to print.
179 * Clobber: lr, r0 - r3, r5
180 */
181func asm_print_hex
182	mov	r3, lr
183	mov	r5, #32  /* No of bits to convert to ascii */
1841:
185	sub	r5, r5, #4
186	lsr	r0, r4, r5
187	and	r0, r0, #0xf
188	cmp	r0, #0xa
189	blo	2f
190	/* Add by 0x27 in addition to ASCII_OFFSET_NUM
191	 * to get ascii for characters 'a - f'.
192	 */
193	add	r0, r0, #0x27
1942:
195	add	r0, r0, #ASCII_OFFSET_NUM
196	bl	plat_crash_console_putc
197	cmp	r5, #0
198	bne	1b
199	bx	r3
200endfunc asm_print_hex
201