exception_asm.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2008 by Duane Ellis
  3. *
  4. * All rights reserved.
  5. *
  6. * The original code had been released as part of the LoastARM Project
  7. * under GPL Version 2 and is published here under the following license
  8. * with kind permission from the author:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the copyright holders nor the names of
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  33. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * For additional information see http://lostarm.sourceforge.net/
  37. */
  38. /*!
  39. * \file include/arch/arm/exception_asm.h
  40. *
  41. * \verbatim
  42. * $Id: exception_asm.h 4124 2012-04-16 13:08:48Z olereinhardt $
  43. * \endverbatim
  44. */
  45. /* -*- mode: asm -*- */
  46. #if __ASSEMBLER__
  47. .macro ARM7TDMI_IRQ_BUG
  48. /** BUG - ARM7TDMI **/
  49. /* See: ATMEL doc DOC1156 */
  50. /* fix/adjust lr */
  51. sub lr,lr,#4
  52. stmfd sp!,{lr}
  53. mrs lr,spsr
  54. /* BUG IS - due to pipeline the IRQ bit may be */
  55. /* set right (disableing irqs) but it gets lost */
  56. /* inside the pipeline, software must fix this */
  57. ands lr,lr,#ARM_CPSR_I_BIT
  58. ldmnefd sp!,{pc}^
  59. ldmfd sp!,{lr}
  60. /* undo the add above cause below we will do
  61. * the sub again below it would be nicer to
  62. * 'clean this up'
  63. */
  64. add lr,lr,#4
  65. .endm
  66. .macro exception_enter, whatmode, frametype
  67. .if ((\whatmode) != ARM_SVC_MODE)
  68. sub lr,lr,#4
  69. .endif
  70. /* switch to system mode */
  71. msr cpsr_c,#(ARM_SYS_MODE | ARM_CPSR_F_BIT | ARM_CPSR_I_BIT)
  72. /* we are now in system mode */
  73. /* one fallacy - we always assume
  74. * sysmode SP is not messed up
  75. */
  76. /* make room for PTRACE frame */
  77. sub sp,sp,#(PTRACE_FRAME_size*4)
  78. /* save the world */
  79. stmia sp,{r0-r14}
  80. /* save the return value here */
  81. str r0,[sp,#(PTRACE_R0_retval_idx*4)]
  82. /* Need to go back to old mode and pickup things
  83. * When we get there, we will need the sys stack
  84. */
  85. mov r0,sp
  86. /* step over to the dark side */
  87. msr cpsr_c,#((\whatmode) | ARM_CPSR_F_BIT | ARM_CPSR_I_BIT)
  88. /* back in offending mode */
  89. /* save the exception address */
  90. str lr,[r0,#(4*PTRACE_R15_idx)]
  91. /* And the saved PSR */
  92. mrs r1,spsr
  93. str r1,[r0,#(4*PTRACE_CPSR_idx)]
  94. /* and back to supervisor mode */
  95. msr cpsr_c,#(ARM_SYS_MODE | ARM_CPSR_F_BIT | ARM_CPSR_I_BIT)
  96. /* done, w/ IRQ & FIQ disabled */
  97. /* APP can decide to re-enable if *IT* wants to */
  98. .if ((\frametype) != 0)
  99. mov r1,#(\frametype)
  100. str r1,[r0,#(4*PTRACE_FRAMETYPE_idx)]
  101. .endif
  102. .endm
  103. .macro exception_exit, whatmode
  104. /*
  105. * SP = the saved state
  106. * we always restore via SYSMODE
  107. */
  108. /* pre-position some values */
  109. ldr r0,[sp,#(PTRACE_R15_idx*4)]
  110. ldr r1,[sp,#(PTRACE_CPSR_idx*4)]
  111. /* go to the offending mode */
  112. msr cpsr_c,#((\whatmode) | ARM_CPSR_F_BIT | ARM_CPSR_I_BIT)
  113. mov lr,r0
  114. msr spsr,r1
  115. /* back to the main mode */
  116. msr cpsr_c,#(ARM_SYS_MODE | ARM_CPSR_F_BIT | ARM_CPSR_I_BIT)
  117. /* restore the world */
  118. ldmia sp,{r0-r14}
  119. .if ((\whatmode) == ARM_SVC_MODE)
  120. ldr r0,[sp,#(PTRACE_R0_retval_idx*4)]
  121. .endif
  122. add sp,sp,#(PTRACE_FRAME_size*4)
  123. /* back to the offending mode */
  124. msr cpsr_c,#((\whatmode) | ARM_CPSR_F_BIT | ARM_CPSR_I_BIT)
  125. /* and effect the return */
  126. movs pc,lr
  127. .endm
  128. #endif