Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <asm_macros.S> |
| 9 | |
| 10 | #if ENABLE_ASSERTIONS |
| 11 | |
| 12 | .globl asm_assert |
| 13 | |
| 14 | /* Since the max decimal input number is 65536 */ |
| 15 | #define MAX_DEC_DIVISOR 10000 |
| 16 | /* The offset to add to get ascii for numerals '0 - 9' */ |
| 17 | #define ASCII_OFFSET_NUM 0x30 |
| 18 | |
| 19 | .section .rodata.assert_str, "aS" |
| 20 | assert_msg1: |
| 21 | .asciz "ASSERT: File " |
| 22 | assert_msg2: |
| 23 | .asciz " Line " |
| 24 | |
| 25 | /* |
| 26 | * This macro is intended to be used to print the |
| 27 | * line number in decimal. Used by asm_assert macro. |
| 28 | * The max number expected is 65536. |
| 29 | * In: x4 = the decimal to print. |
| 30 | * Clobber: x30, x0, x1, x2, x5, x6 |
| 31 | */ |
| 32 | .macro asm_print_line_dec |
| 33 | mov x6, #10 /* Divide by 10 after every loop iteration */ |
| 34 | mov x5, #MAX_DEC_DIVISOR |
| 35 | dec_print_loop: |
| 36 | udiv x0, x4, x5 /* Get the quotient */ |
| 37 | msub x4, x0, x5, x4 /* Find the remainder */ |
| 38 | add x0, x0, #ASCII_OFFSET_NUM /* Convert to ascii */ |
| 39 | bl plat_crash_console_putc |
| 40 | udiv x5, x5, x6 /* Reduce divisor */ |
| 41 | cbnz x5, dec_print_loop |
| 42 | .endm |
| 43 | |
| 44 | /* --------------------------------------------------------------------------- |
| 45 | * Assertion support in assembly. |
| 46 | * The below function helps to support assertions in assembly where we do not |
| 47 | * have a C runtime stack. Arguments to the function are : |
| 48 | * x0 - File name |
| 49 | * x1 - Line no |
| 50 | * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6. |
| 51 | * --------------------------------------------------------------------------- |
| 52 | */ |
| 53 | func asm_assert |
| 54 | mov x5, x0 |
| 55 | mov x6, x1 |
| 56 | /* Ensure the console is initialized */ |
| 57 | bl plat_crash_console_init |
| 58 | /* Check if the console is initialized */ |
| 59 | cbz x0, _assert_loop |
| 60 | /* The console is initialized */ |
| 61 | adr x4, assert_msg1 |
| 62 | bl asm_print_str |
| 63 | mov x4, x5 |
| 64 | bl asm_print_str |
| 65 | adr x4, assert_msg2 |
| 66 | bl asm_print_str |
| 67 | /* Check if line number higher than max permitted */ |
| 68 | tst x6, #~0xffff |
| 69 | b.ne _assert_loop |
| 70 | mov x4, x6 |
| 71 | asm_print_line_dec |
| 72 | bl plat_crash_console_flush |
| 73 | _assert_loop: |
| 74 | wfi |
| 75 | b _assert_loop |
| 76 | endfunc asm_assert |
| 77 | |
| 78 | /* |
| 79 | * This function prints a string from address in x4. |
| 80 | * In: x4 = pointer to string. |
| 81 | * Clobber: x30, x0, x1, x2, x3 |
| 82 | */ |
| 83 | func asm_print_str |
| 84 | mov x3, x30 |
| 85 | 1: |
| 86 | ldrb w0, [x4], #0x1 |
| 87 | cbz x0, 2f |
| 88 | bl plat_crash_console_putc |
| 89 | b 1b |
| 90 | 2: |
| 91 | ret x3 |
| 92 | endfunc asm_print_str |
| 93 | |
| 94 | /* |
| 95 | * This function prints a hexadecimal number in x4. |
| 96 | * In: x4 = the hexadecimal to print. |
| 97 | * Clobber: x30, x0 - x3, x5 |
| 98 | */ |
| 99 | func asm_print_hex |
| 100 | mov x3, x30 |
| 101 | mov x5, #64 /* No of bits to convert to ascii */ |
| 102 | 1: |
| 103 | sub x5, x5, #4 |
| 104 | lsrv x0, x4, x5 |
| 105 | and x0, x0, #0xf |
| 106 | cmp x0, #0xA |
| 107 | b.lo 2f |
| 108 | /* Add by 0x27 in addition to ASCII_OFFSET_NUM |
| 109 | * to get ascii for characters 'a - f'. |
| 110 | */ |
| 111 | add x0, x0, #0x27 |
| 112 | 2: |
| 113 | add x0, x0, #ASCII_OFFSET_NUM |
| 114 | bl plat_crash_console_putc |
| 115 | cbnz x5, 1b |
| 116 | ret x3 |
| 117 | endfunc asm_print_hex |
| 118 | |
| 119 | #endif /* ENABLE_ASSERTIONS */ |