debug.S 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2014-2023 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 el3_panic
  15. .globl elx_panic
  16. /* Since the max decimal input number is 65536 */
  17. #define MAX_DEC_DIVISOR 10000
  18. /* The offset to add to get ascii for numerals '0 - 9' */
  19. #define ASCII_OFFSET_NUM 0x30
  20. #if ENABLE_ASSERTIONS
  21. .section .rodata.assert_str, "aS"
  22. assert_msg1:
  23. .asciz "ASSERT: File "
  24. assert_msg2:
  25. .asciz " Line "
  26. /*
  27. * This macro is intended to be used to print the
  28. * line number in decimal. Used by asm_assert macro.
  29. * The max number expected is 65536.
  30. * In: x4 = the decimal to print.
  31. * Clobber: x30, x0, x1, x2, x5, x6
  32. */
  33. .macro asm_print_line_dec
  34. mov x6, #10 /* Divide by 10 after every loop iteration */
  35. mov x5, #MAX_DEC_DIVISOR
  36. dec_print_loop:
  37. udiv x0, x4, x5 /* Get the quotient */
  38. msub x4, x0, x5, x4 /* Find the remainder */
  39. add x0, x0, #ASCII_OFFSET_NUM /* Convert to ascii */
  40. bl plat_crash_console_putc
  41. udiv x5, x5, x6 /* Reduce divisor */
  42. cbnz x5, dec_print_loop
  43. .endm
  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. #if LOG_LEVEL >= LOG_LEVEL_INFO
  55. /*
  56. * Only print the output if LOG_LEVEL is higher or equal to
  57. * LOG_LEVEL_INFO, which is the default value for builds with DEBUG=1.
  58. */
  59. mov x5, x0
  60. mov x6, x1
  61. /* Ensure the console is initialized */
  62. bl plat_crash_console_init
  63. /* Check if the console is initialized */
  64. cbz x0, _assert_loop
  65. /* The console is initialized */
  66. adr x4, assert_msg1
  67. bl asm_print_str
  68. mov x4, x5
  69. bl asm_print_str
  70. adr x4, assert_msg2
  71. bl asm_print_str
  72. /* Check if line number higher than max permitted */
  73. tst x6, #~0xffff
  74. b.ne _assert_loop
  75. mov x4, x6
  76. asm_print_line_dec
  77. bl plat_crash_console_flush
  78. _assert_loop:
  79. #endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
  80. no_ret plat_panic_handler
  81. endfunc asm_assert
  82. #endif /* ENABLE_ASSERTIONS */
  83. /*
  84. * This function prints a string from address in x4.
  85. * In: x4 = pointer to string.
  86. * Clobber: x30, x0, x1, x2, x3
  87. */
  88. func asm_print_str
  89. mov x3, x30
  90. 1:
  91. ldrb w0, [x4], #0x1
  92. cbz x0, 2f
  93. bl plat_crash_console_putc
  94. b 1b
  95. 2:
  96. ret x3
  97. endfunc asm_print_str
  98. /*
  99. * This function prints a hexadecimal number in x4.
  100. * In: x4 = the hexadecimal to print.
  101. * Clobber: x30, x0 - x3, x5
  102. */
  103. func asm_print_hex
  104. mov x5, #64 /* No of bits to convert to ascii */
  105. /* Convert to ascii number of bits in x5 */
  106. asm_print_hex_bits:
  107. mov x3, x30
  108. 1:
  109. sub x5, x5, #4
  110. lsrv x0, x4, x5
  111. and x0, x0, #0xf
  112. cmp x0, #0xA
  113. b.lo 2f
  114. /* Add by 0x27 in addition to ASCII_OFFSET_NUM
  115. * to get ascii for characters 'a - f'.
  116. */
  117. add x0, x0, #0x27
  118. 2:
  119. add x0, x0, #ASCII_OFFSET_NUM
  120. bl plat_crash_console_putc
  121. cbnz x5, 1b
  122. ret x3
  123. endfunc asm_print_hex
  124. /*
  125. * Helper function to print newline to console
  126. * Clobber: x0
  127. */
  128. func asm_print_newline
  129. mov x0, '\n'
  130. b plat_crash_console_putc
  131. endfunc asm_print_newline
  132. /***********************************************************
  133. * The common implementation of el3_panic for all BL stages
  134. ***********************************************************/
  135. .section .rodata.panic_str, "aS"
  136. panic_msg: .asciz "PANIC at PC : 0x"
  137. func elx_panic
  138. #if CRASH_REPORTING && defined(IMAGE_BL31)
  139. b report_elx_panic
  140. #endif /* CRASH_REPORTING && IMAGE_BL31 */
  141. b panic_common
  142. endfunc elx_panic
  143. /* ---------------------------------------------------------------------------
  144. * el3_panic assumes that it is invoked from a C Runtime Environment ie a
  145. * valid stack exists. This call will not return.
  146. * Clobber list : if CRASH_REPORTING is not enabled then x30, x0 - x6
  147. * ---------------------------------------------------------------------------
  148. */
  149. func el3_panic
  150. #if CRASH_REPORTING && defined(IMAGE_BL31)
  151. b report_el3_panic
  152. #endif /* CRASH_REPORTING && IMAGE_BL31 */
  153. panic_common:
  154. mov x6, x30
  155. bl plat_crash_console_init
  156. /* Check if the console is initialized */
  157. cbz x0, _panic_handler
  158. /* The console is initialized */
  159. adr x4, panic_msg
  160. bl asm_print_str
  161. mov x4, x6
  162. /* The panic location is lr -4 */
  163. sub x4, x4, #4
  164. bl asm_print_hex
  165. /* Print new line */
  166. bl asm_print_newline
  167. bl plat_crash_console_flush
  168. _panic_handler:
  169. /* Pass to plat_panic_handler the address from where el3_panic was
  170. * called, not the address of the call from el3_panic. */
  171. mov x30, x6
  172. b plat_panic_handler
  173. endfunc el3_panic