backtrace.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * linux/arch/arm/lib/backtrace.S
  3. *
  4. * Copyright (C) 1995, 1996 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * 27/03/03 Ian Molton Clean up CONFIG_CPU
  11. *
  12. */
  13. #include <linux/linkage.h>
  14. #include <asm/assembler.h>
  15. .text
  16. @ fp is 0 or stack frame
  17. #define frame r4
  18. #define sv_fp r5
  19. #define sv_pc r6
  20. #define mask r7
  21. #define offset r8
  22. ENTRY(__backtrace)
  23. mov r1, #0x10
  24. mov r0, fp
  25. ENTRY(c_backtrace)
  26. #if !defined(CONFIG_FRAME_POINTER) || !defined(CONFIG_PRINTK)
  27. mov pc, lr
  28. ENDPROC(__backtrace)
  29. ENDPROC(c_backtrace)
  30. #else
  31. stmfd sp!, {r4 - r8, lr} @ Save an extra register so we have a location...
  32. movs frame, r0 @ if frame pointer is zero
  33. beq no_frame @ we have no stack frames
  34. tst r1, #0x10 @ 26 or 32-bit mode?
  35. ARM( moveq mask, #0xfc000003 )
  36. THUMB( moveq mask, #0xfc000000 )
  37. THUMB( orreq mask, #0x03 )
  38. movne mask, #0 @ mask for 32-bit
  39. 1: stmfd sp!, {pc} @ calculate offset of PC stored
  40. ldr r0, [sp], #4 @ by stmfd for this CPU
  41. adr r1, 1b
  42. sub offset, r0, r1
  43. /*
  44. * Stack frame layout:
  45. * optionally saved caller registers (r4 - r10)
  46. * saved fp
  47. * saved sp
  48. * saved lr
  49. * frame => saved pc
  50. * optionally saved arguments (r0 - r3)
  51. * saved sp => <next word>
  52. *
  53. * Functions start with the following code sequence:
  54. * mov ip, sp
  55. * stmfd sp!, {r0 - r3} (optional)
  56. * corrected pc => stmfd sp!, {..., fp, ip, lr, pc}
  57. */
  58. for_each_frame: tst frame, mask @ Check for address exceptions
  59. bne no_frame
  60. 1001: ldr sv_pc, [frame, #0] @ get saved pc
  61. 1002: ldr sv_fp, [frame, #-12] @ get saved fp
  62. sub sv_pc, sv_pc, offset @ Correct PC for prefetching
  63. bic sv_pc, sv_pc, mask @ mask PC/LR for the mode
  64. 1003: ldr r2, [sv_pc, #-4] @ if stmfd sp!, {args} exists,
  65. ldr r3, .Ldsi+4 @ adjust saved 'pc' back one
  66. teq r3, r2, lsr #10 @ instruction
  67. subne r0, sv_pc, #4 @ allow for mov
  68. subeq r0, sv_pc, #8 @ allow for mov + stmia
  69. ldr r1, [frame, #-4] @ get saved lr
  70. mov r2, frame
  71. bic r1, r1, mask @ mask PC/LR for the mode
  72. bl dump_backtrace_entry
  73. ldr r1, [sv_pc, #-4] @ if stmfd sp!, {args} exists,
  74. ldr r3, .Ldsi+4
  75. teq r3, r1, lsr #10
  76. ldreq r0, [frame, #-8] @ get sp
  77. subeq r0, r0, #4 @ point at the last arg
  78. bleq .Ldumpstm @ dump saved registers
  79. 1004: ldr r1, [sv_pc, #0] @ if stmfd sp!, {..., fp, ip, lr, pc}
  80. ldr r3, .Ldsi @ instruction exists,
  81. teq r3, r1, lsr #10
  82. subeq r0, frame, #16
  83. bleq .Ldumpstm @ dump saved registers
  84. teq sv_fp, #0 @ zero saved fp means
  85. beq no_frame @ no further frames
  86. cmp sv_fp, frame @ next frame must be
  87. mov frame, sv_fp @ above the current frame
  88. bhi for_each_frame
  89. 1006: adr r0, .Lbad
  90. mov r1, frame
  91. bl printk
  92. no_frame: ldmfd sp!, {r4 - r8, pc}
  93. ENDPROC(__backtrace)
  94. ENDPROC(c_backtrace)
  95. .pushsection __ex_table,"a"
  96. .align 3
  97. .long 1001b, 1006b
  98. .long 1002b, 1006b
  99. .long 1003b, 1006b
  100. .long 1004b, 1006b
  101. .popsection
  102. #define instr r4
  103. #define reg r5
  104. #define stack r6
  105. .Ldumpstm: stmfd sp!, {instr, reg, stack, r7, lr}
  106. mov stack, r0
  107. mov instr, r1
  108. mov reg, #10
  109. mov r7, #0
  110. 1: mov r3, #1
  111. ARM( tst instr, r3, lsl reg )
  112. THUMB( lsl r3, reg )
  113. THUMB( tst instr, r3 )
  114. beq 2f
  115. add r7, r7, #1
  116. teq r7, #6
  117. moveq r7, #1
  118. moveq r1, #'\n'
  119. movne r1, #' '
  120. ldr r3, [stack], #-4
  121. mov r2, reg
  122. adr r0, .Lfp
  123. bl printk
  124. 2: subs reg, reg, #1
  125. bpl 1b
  126. teq r7, #0
  127. adrne r0, .Lcr
  128. blne printk
  129. ldmfd sp!, {instr, reg, stack, r7, pc}
  130. .Lfp: .asciz "%cr%d:%08x"
  131. .Lcr: .asciz "\n"
  132. .Lbad: .asciz "Backtrace aborted due to bad frame pointer <%p>\n"
  133. .align
  134. .Ldsi: .word 0xe92dd800 >> 10 @ stmfd sp!, {... fp, ip, lr, pc}
  135. .word 0xe92d0000 >> 10 @ stmfd sp!, {}
  136. #endif