JLINKDCC_HandleDataAbort.s 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*********************************************************************
  2. * SEGGER Microcontroller GmbH *
  3. * The Embedded Experts *
  4. **********************************************************************
  5. * *
  6. * (c) 2006 - 2019 SEGGER Microcontroller GmbH *
  7. * *
  8. * www.segger.com Support: support@segger.com *
  9. * *
  10. **********************************************************************
  11. * *
  12. * All rights reserved. *
  13. * *
  14. * Redistribution and use in source and binary forms, with or *
  15. * without modification, are permitted provided that the following *
  16. * condition is met: *
  17. * *
  18. * o Redistributions of source code must retain the above copyright *
  19. * notice, this condition and the following disclaimer. *
  20. * *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
  22. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
  23. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
  25. * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
  26. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
  29. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
  32. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
  33. * DAMAGE. *
  34. * *
  35. **********************************************************************
  36. -------------------------- END-OF-HEADER -----------------------------
  37. File : JLINKDCC_HandleDataAbort.s79
  38. Purpose : Data abort handler for ARM J-Link type communication via DCC
  39. Notes : (1) How to use
  40. In order to use the DCC abort handler, 3 things need to be done:
  41. * Place a branch to DCC_Abort at address 0x10 ("vector" used for data aborts)
  42. * Initialize the Abort-mode stack pointer to an area of at least 8 bytes of stack
  43. memory required by the handler
  44. * Add the DCC abort handler assembly file to the application
  45. (2) Compatibility
  46. The J-Link ARM DCC handler is compatible to the DCC communication
  47. protocol used by IAR in the embedded workbench for ARM and allows
  48. using the live data window in C-Spy
  49. */
  50. IRQ_MODE DEFINE 0x12 ; Interrupt Request mode
  51. SVC_MODE DEFINE 0x13 ; Supervisor mode
  52. ABT_MODE DEFINE 0x17 ; Abort mode
  53. UND_MODE DEFINE 0x1B ; Undefined mode
  54. SYS_MODE DEFINE 0x1F ; System mode
  55. FLAG_I DEFINE 0x80 ; IRQ disable flag
  56. FLAG_F DEFINE 0x40 ; FIQ disable flag
  57. EXTERN JLINKDCC_IsInHandler
  58. EXTERN JLINKDCC_AbortOccurred
  59. SECTION `.bss`:DATA:NOROOT(2)
  60. SaveArea:
  61. DS8 8
  62. SECTION .text:CODE:NOROOT(2)
  63. CODE32
  64. /*********************************************************************
  65. *
  66. * Public code
  67. *
  68. **********************************************************************
  69. */
  70. PUBLIC JLINKDCC_HandleDataAbort
  71. PUBLIC Abort_Handler
  72. /*********************************************************************
  73. *
  74. * JLINKDCC_HandleDataAbort
  75. *
  76. * Function description
  77. * Data abort handler for J-Link ARM type DCC communication.
  78. *
  79. * Notes
  80. * (1) Abort mode
  81. * This handler is supposed to be executed in abort mode by simply placing a
  82. * jump to the handler at address 0x10.
  83. * Just like an interrupt service routine, it needs to restore all registers
  84. * for other modes and may only permanently modify registers of abort mode (SPSR_ABT, R14_ABT)
  85. *
  86. * (2) Stack, mode switching
  87. * In a lot of applications, SP_ABT is not loaded. We therefor switch to UND mode so we do not need an ABT stack.
  88. */
  89. Abort_Handler:
  90. JLINKDCC_HandleDataAbort:
  91. //
  92. // Switch to Undef mode so we have registers to "play" with
  93. //
  94. msr CPSR_c, #(UND_MODE | FLAG_F | FLAG_I) // Switch to UND mode & disable IRQ and FIQ
  95. LDR R14, =SaveArea
  96. STR R0, [R14, #0]
  97. STR R1, [R14, #4]
  98. //
  99. // Check if exception stemmed from JLINKARM DCC communication. Otherwise, user abort handler is called
  100. //
  101. LDR R0, =JLINKDCC_IsInHandler
  102. LDRB R1, [R0, #0]
  103. TST R1, R1 // Check if abort occurred during execution of DCC handler
  104. BEQ UserAbortHandler
  105. //
  106. // Set JLINKDCC_AbortOccurred so PC (via J-Link) has a chance to find out that memory operation was aborted
  107. //
  108. LDR R14, =JLINKDCC_AbortOccurred
  109. STRB R1, [R14, #0] // Set abort occurred state
  110. //
  111. // Remember T-Flag in Z-Flag
  112. //
  113. msr CPSR_c, #(ABT_MODE | FLAG_F | FLAG_I) // Switch to ABT mode & disable IRQ and FIQ
  114. MRS R0, SPSR
  115. TST R0, #0x20 // Check thumb bit
  116. //
  117. // Restore modified registers
  118. //
  119. LDR R0, =SaveArea
  120. LDR R1, [R0, #4]
  121. LDR R0, [R0, #0]
  122. //
  123. // Continue code execution right after the instruction which caused the abort.
  124. //
  125. SUBEQS PC, R14, #4 // Return after the aborted instruction (ARM mode)
  126. SUBS PC, R14, #6 // Return after the aborted instruction (Thumb mode)
  127. /*********************************************************************
  128. *
  129. * UserAbortHandler
  130. *
  131. * Function description
  132. * User defined abort handler.
  133. * This function is called if a data abort occurs for other reasons than JLINK DCC comunication.
  134. * Feel free to modify and jump to your own handler as required.
  135. */
  136. UserAbortHandler:
  137. B UserAbortHandler // Modify this line to jump to your own handler if desired
  138. END
  139. /*************************** end of file ****************************/