1/*
2 * Copyright (c) 2014, 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_print_str
35	.globl	asm_print_hex
36	.globl	asm_assert
37	.globl	do_panic
38
39/* Since the max decimal input number is 65536 */
40#define MAX_DEC_DIVISOR		10000
41/* The offset to add to get ascii for numerals '0 - 9' */
42#define ASCII_OFFSET_NUM	0x30
43
44#if ASM_ASSERTION
45.section .rodata.assert_str, "aS"
46assert_msg1:
47	.asciz "ASSERT: File "
48assert_msg2:
49	.asciz " Line "
50
51	/*
52	 * This macro is intended to be used to print the
53	 * line number in decimal. Used by asm_assert macro.
54	 * The max number expected is 65536.
55	 * In: x4 = the decimal to print.
56	 * Clobber: x30, x0, x1, x2, x5, x6
57	 */
58	.macro asm_print_line_dec
59	mov	x6, #10		/* Divide by 10 after every loop iteration */
60	mov	x5, #MAX_DEC_DIVISOR
61dec_print_loop:
62	udiv	x0, x4, x5		/* Get the quotient */
63	msub	x4, x0, x5, x4		/* Find the remainder */
64	add	x0, x0, #ASCII_OFFSET_NUM		/* Convert to ascii */
65	bl	plat_crash_console_putc
66	udiv	x5, x5, x6		/* Reduce divisor */
67	cbnz	x5, dec_print_loop
68	.endm
69
70
71/* ---------------------------------------------------------------------------
72 * Assertion support in assembly.
73 * The below function helps to support assertions in assembly where we do not
74 * have a C runtime stack. Arguments to the function are :
75 * x0 - File name
76 * x1 - Line no
77 * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6.
78 * ---------------------------------------------------------------------------
79 */
80func asm_assert
81	mov	x5, x0
82	mov	x6, x1
83	/* Ensure the console is initialized */
84	bl	plat_crash_console_init
85	/* Check if the console is initialized */
86	cbz	x0, _assert_loop
87	/* The console is initialized */
88	adr	x4, assert_msg1
89	bl	asm_print_str
90	mov	x4, x5
91	bl	asm_print_str
92	adr	x4, assert_msg2
93	bl	asm_print_str
94	/* Check if line number higher than max permitted */
95	tst	x6, #~0xffff
96	b.ne	_assert_loop
97	mov	x4, x6
98	asm_print_line_dec
99_assert_loop:
100	b	_assert_loop
101#endif
102
103/*
104 * This function prints a string from address in x4.
105 * In: x4 = pointer to string.
106 * Clobber: x30, x0, x1, x2, x3
107 */
108func asm_print_str
109	mov	x3, x30
1101:
111	ldrb	w0, [x4], #0x1
112	cbz	x0, 2f
113	bl	plat_crash_console_putc
114	b	1b
1152:
116	ret	x3
117
118/*
119 * This function prints a hexadecimal number in x4.
120 * In: x4 = the hexadecimal to print.
121 * Clobber: x30, x0, x5, x1, x2, x3
122 */
123func asm_print_hex
124	mov	x3, x30
125	mov	x5, #64  /* No of bits to convert to ascii */
1261:
127	sub	x5, x5, #4
128	lsrv	x0, x4, x5
129	and	x0, x0, #0xf
130	cmp	x0, #0xA
131	b.lo	2f
132	/* Add by 0x27 in addition to ASCII_OFFSET_NUM
133	 * to get ascii for characters 'a - f'.
134	 */
135	add	x0, x0, #0x27
1362:
137	add	x0, x0, #ASCII_OFFSET_NUM
138	bl	plat_crash_console_putc
139	cbnz	x5, 1b
140	ret	x3
141
142	/***********************************************************
143	 * The common implementation of do_panic for all BL stages
144	 ***********************************************************/
145
146.section .rodata.panic_str, "aS"
147	panic_msg: .asciz "PANIC at PC : 0x"
148
149/* ---------------------------------------------------------------------------
150 * do_panic assumes that it is invoked from a C Runtime Environment ie a
151 * valid stack exists. This call will not return.
152 * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6
153 * ---------------------------------------------------------------------------
154 */
155
156/* This is for the non el3 BL stages to compile through */
157	.weak el3_panic
158
159func do_panic
160#if CRASH_REPORTING
161	str	x0, [sp, #-0x10]!
162	mrs	x0, currentel
163	ubfx	x0, x0, #2, #2
164	cmp	x0, #0x3
165	ldr	x0, [sp], #0x10
166	b.eq	el3_panic
167#endif
168
169panic_common:
170/*
171 * el3_panic will be redefined by the BL31
172 * crash reporting mechanism (if enabled)
173 */
174el3_panic:
175	mov	x6, x30
176	bl	plat_crash_console_init
177	/* Check if the console is initialized */
178	cbz	x0, _panic_loop
179	/* The console is initialized */
180	adr	x4, panic_msg
181	bl	asm_print_str
182	mov	x4, x6
183	/* The panic location is lr -4 */
184	sub	x4, x4, #4
185	bl	asm_print_hex
186_panic_loop:
187	b	_panic_loop
188
189