debug.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch.h>
  7. #include <asm_macros.S>
  8. #include <common/debug.h>
  9. .globl asm_print_str
  10. .globl asm_print_hex
  11. .globl asm_print_hex_bits
  12. .globl asm_print_newline
  13. .globl asm_assert
  14. .globl do_panic
  15. /* Since the max decimal input number is 65536 */
  16. #define MAX_DEC_DIVISOR 10000
  17. /* The offset to add to get ascii for numerals '0 - 9' */
  18. #define ASCII_OFFSET_NUM 0x30
  19. #if ENABLE_ASSERTIONS
  20. .section .rodata.assert_str, "aS"
  21. assert_msg1:
  22. .asciz "ASSERT: File "
  23. assert_msg2:
  24. .asciz " Line "
  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. * Assertion support in assembly.
  45. * The below function helps to support assertions in assembly where we do not
  46. * have a C runtime stack. Arguments to the function are :
  47. * x0 - File name
  48. * x1 - Line no
  49. * Clobber list : x30, x0, x1, x2, x3, x4, x5, x6.
  50. * ---------------------------------------------------------------------------
  51. */
  52. func asm_assert
  53. #if LOG_LEVEL >= LOG_LEVEL_INFO
  54. /*
  55. * Only print the output if LOG_LEVEL is higher or equal to
  56. * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
  57. */
  58. mov x5, x0
  59. mov x6, x1
  60. /* Ensure the console is initialized */
  61. bl plat_crash_console_init
  62. /* Check if the console is initialized */
  63. cbz x0, _assert_loop
  64. /* The console is initialized */
  65. adr x4, assert_msg1
  66. bl asm_print_str
  67. mov x4, x5
  68. bl asm_print_str
  69. adr x4, assert_msg2
  70. bl asm_print_str
  71. /* Check if line number higher than max permitted */
  72. tst x6, #~0xffff
  73. b.ne _assert_loop
  74. mov x4, x6
  75. asm_print_line_dec
  76. bl plat_crash_console_flush
  77. _assert_loop:
  78. #endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
  79. no_ret plat_panic_handler
  80. endfunc asm_assert
  81. #endif /* ENABLE_ASSERTIONS */
  82. /*
  83. * This function prints a string from address in x4.
  84. * In: x4 = pointer to string.
  85. * Clobber: x30, x0, x1, x2, x3
  86. */
  87. func asm_print_str
  88. mov x3, x30
  89. 1:
  90. ldrb w0, [x4], #0x1
  91. cbz x0, 2f
  92. bl plat_crash_console_putc
  93. b 1b
  94. 2:
  95. ret x3
  96. endfunc asm_print_str
  97. /*
  98. * This function prints a hexadecimal number in x4.
  99. * In: x4 = the hexadecimal to print.
  100. * Clobber: x30, x0 - x3, x5
  101. */
  102. func asm_print_hex
  103. mov x5, #64 /* No of bits to convert to ascii */
  104. /* Convert to ascii number of bits in x5 */
  105. asm_print_hex_bits:
  106. mov x3, x30
  107. 1:
  108. sub x5, x5, #4
  109. lsrv x0, x4, x5
  110. and x0, x0, #0xf
  111. cmp x0, #0xA
  112. b.lo 2f
  113. /* Add by 0x27 in addition to ASCII_OFFSET_NUM
  114. * to get ascii for characters 'a - f'.
  115. */
  116. add x0, x0, #0x27
  117. 2:
  118. add x0, x0, #ASCII_OFFSET_NUM
  119. bl plat_crash_console_putc
  120. cbnz x5, 1b
  121. ret x3
  122. endfunc asm_print_hex
  123. /*
  124. * Helper function to print newline to console
  125. * Clobber: x0
  126. */
  127. func asm_print_newline
  128. mov x0, '\n'
  129. b plat_crash_console_putc
  130. endfunc asm_print_newline
  131. /***********************************************************
  132. * The common implementation of do_panic for all BL stages
  133. ***********************************************************/
  134. .section .rodata.panic_str, "aS"
  135. panic_msg: .asciz "PANIC at PC : 0x"
  136. /* ---------------------------------------------------------------------------
  137. * do_panic assumes that it is invoked from a C Runtime Environment ie a
  138. * valid stack exists. This call will not return.
  139. * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6
  140. * ---------------------------------------------------------------------------
  141. */
  142. /* This is for the non el3 BL stages to compile through */
  143. .weak el3_panic
  144. .weak elx_panic
  145. func do_panic
  146. #if CRASH_REPORTING
  147. str x0, [sp, #-0x10]!
  148. mrs x0, currentel
  149. ubfx x0, x0, #MODE_EL_SHIFT, #MODE_EL_WIDTH
  150. cmp x0, #MODE_EL3
  151. #if !HANDLE_EA_EL3_FIRST
  152. ldr x0, [sp], #0x10
  153. b.eq el3_panic
  154. #else
  155. b.ne to_panic_common
  156. /* Check EL the exception taken from */
  157. mrs x0, spsr_el3
  158. ubfx x0, x0, #SPSR_EL_SHIFT, #SPSR_EL_WIDTH
  159. cmp x0, #MODE_EL3
  160. b.ne elx_panic
  161. ldr x0, [sp], #0x10
  162. b el3_panic
  163. to_panic_common:
  164. ldr x0, [sp], #0x10
  165. #endif /* HANDLE_EA_EL3_FIRST */
  166. #endif /* CRASH_REPORTING */
  167. panic_common:
  168. /*
  169. * el3_panic will be redefined by the BL31
  170. * crash reporting mechanism (if enabled)
  171. */
  172. el3_panic:
  173. mov x6, x30
  174. bl plat_crash_console_init
  175. /* Check if the console is initialized */
  176. cbz x0, _panic_handler
  177. /* The console is initialized */
  178. adr x4, panic_msg
  179. bl asm_print_str
  180. mov x4, x6
  181. /* The panic location is lr -4 */
  182. sub x4, x4, #4
  183. bl asm_print_hex
  184. /* Print new line */
  185. bl asm_print_newline
  186. bl plat_crash_console_flush
  187. _panic_handler:
  188. /* Pass to plat_panic_handler the address from where el3_panic was
  189. * called, not the address of the call from el3_panic. */
  190. mov x30, x6
  191. b plat_panic_handler
  192. endfunc do_panic