vfphw.S 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * linux/arch/arm/vfp/vfphw.S
  3. *
  4. * Copyright (C) 2004 ARM Limited.
  5. * Written by Deep Blue Solutions Limited.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This code is called from the kernel's undefined instruction trap.
  12. * r9 holds the return address for successful handling.
  13. * lr holds the return address for unrecognised instructions.
  14. * r10 points at the start of the private FP workspace in the thread structure
  15. * sp points to a struct pt_regs (as defined in include/asm/proc/ptrace.h)
  16. */
  17. #include <asm/thread_info.h>
  18. #include <asm/vfpmacros.h>
  19. #include <linux/kern_levels.h>
  20. #include "../kernel/entry-header.S"
  21. .macro DBGSTR, str
  22. #ifdef DEBUG
  23. stmfd sp!, {r0-r3, ip, lr}
  24. add r0, pc, #4
  25. bl printk
  26. b 1f
  27. .asciz KERN_DEBUG "VFP: \str\n"
  28. .balign 4
  29. 1: ldmfd sp!, {r0-r3, ip, lr}
  30. #endif
  31. .endm
  32. .macro DBGSTR1, str, arg
  33. #ifdef DEBUG
  34. stmfd sp!, {r0-r3, ip, lr}
  35. mov r1, \arg
  36. add r0, pc, #4
  37. bl printk
  38. b 1f
  39. .asciz KERN_DEBUG "VFP: \str\n"
  40. .balign 4
  41. 1: ldmfd sp!, {r0-r3, ip, lr}
  42. #endif
  43. .endm
  44. .macro DBGSTR3, str, arg1, arg2, arg3
  45. #ifdef DEBUG
  46. stmfd sp!, {r0-r3, ip, lr}
  47. mov r3, \arg3
  48. mov r2, \arg2
  49. mov r1, \arg1
  50. add r0, pc, #4
  51. bl printk
  52. b 1f
  53. .asciz KERN_DEBUG "VFP: \str\n"
  54. .balign 4
  55. 1: ldmfd sp!, {r0-r3, ip, lr}
  56. #endif
  57. .endm
  58. @ VFP hardware support entry point.
  59. @
  60. @ r0 = faulted instruction
  61. @ r2 = faulted PC+4
  62. @ r9 = successful return
  63. @ r10 = vfp_state union
  64. @ r11 = CPU number
  65. @ lr = failure return
  66. ENTRY(vfp_support_entry)
  67. DBGSTR3 "instr %08x pc %08x state %p", r0, r2, r10
  68. VFPFMRX r1, FPEXC @ Is the VFP enabled?
  69. DBGSTR1 "fpexc %08x", r1
  70. tst r1, #FPEXC_EN
  71. bne look_for_VFP_exceptions @ VFP is already enabled
  72. DBGSTR1 "enable %x", r10
  73. ldr r3, vfp_current_hw_state_address
  74. orr r1, r1, #FPEXC_EN @ user FPEXC has the enable bit set
  75. ldr r4, [r3, r11, lsl #2] @ vfp_current_hw_state pointer
  76. bic r5, r1, #FPEXC_EX @ make sure exceptions are disabled
  77. cmp r4, r10 @ this thread owns the hw context?
  78. #ifndef CONFIG_SMP
  79. @ For UP, checking that this thread owns the hw context is
  80. @ sufficient to determine that the hardware state is valid.
  81. beq vfp_hw_state_valid
  82. @ On UP, we lazily save the VFP context. As a different
  83. @ thread wants ownership of the VFP hardware, save the old
  84. @ state if there was a previous (valid) owner.
  85. VFPFMXR FPEXC, r5 @ enable VFP, disable any pending
  86. @ exceptions, so we can get at the
  87. @ rest of it
  88. DBGSTR1 "save old state %p", r4
  89. cmp r4, #0 @ if the vfp_current_hw_state is NULL
  90. beq vfp_reload_hw @ then the hw state needs reloading
  91. VFPFSTMIA r4, r5 @ save the working registers
  92. VFPFMRX r5, FPSCR @ current status
  93. #ifndef CONFIG_CPU_FEROCEON
  94. tst r1, #FPEXC_EX @ is there additional state to save?
  95. beq 1f
  96. VFPFMRX r6, FPINST @ FPINST (only if FPEXC.EX is set)
  97. tst r1, #FPEXC_FP2V @ is there an FPINST2 to read?
  98. beq 1f
  99. VFPFMRX r8, FPINST2 @ FPINST2 if needed (and present)
  100. 1:
  101. #endif
  102. stmia r4, {r1, r5, r6, r8} @ save FPEXC, FPSCR, FPINST, FPINST2
  103. vfp_reload_hw:
  104. #else
  105. @ For SMP, if this thread does not own the hw context, then we
  106. @ need to reload it. No need to save the old state as on SMP,
  107. @ we always save the state when we switch away from a thread.
  108. bne vfp_reload_hw
  109. @ This thread has ownership of the current hardware context.
  110. @ However, it may have been migrated to another CPU, in which
  111. @ case the saved state is newer than the hardware context.
  112. @ Check this by looking at the CPU number which the state was
  113. @ last loaded onto.
  114. ldr ip, [r10, #VFP_CPU]
  115. teq ip, r11
  116. beq vfp_hw_state_valid
  117. vfp_reload_hw:
  118. @ We're loading this threads state into the VFP hardware. Update
  119. @ the CPU number which contains the most up to date VFP context.
  120. str r11, [r10, #VFP_CPU]
  121. VFPFMXR FPEXC, r5 @ enable VFP, disable any pending
  122. @ exceptions, so we can get at the
  123. @ rest of it
  124. #endif
  125. DBGSTR1 "load state %p", r10
  126. str r10, [r3, r11, lsl #2] @ update the vfp_current_hw_state pointer
  127. @ Load the saved state back into the VFP
  128. VFPFLDMIA r10, r5 @ reload the working registers while
  129. @ FPEXC is in a safe state
  130. ldmia r10, {r1, r5, r6, r8} @ load FPEXC, FPSCR, FPINST, FPINST2
  131. #ifndef CONFIG_CPU_FEROCEON
  132. tst r1, #FPEXC_EX @ is there additional state to restore?
  133. beq 1f
  134. VFPFMXR FPINST, r6 @ restore FPINST (only if FPEXC.EX is set)
  135. tst r1, #FPEXC_FP2V @ is there an FPINST2 to write?
  136. beq 1f
  137. VFPFMXR FPINST2, r8 @ FPINST2 if needed (and present)
  138. 1:
  139. #endif
  140. VFPFMXR FPSCR, r5 @ restore status
  141. @ The context stored in the VFP hardware is up to date with this thread
  142. vfp_hw_state_valid:
  143. tst r1, #FPEXC_EX
  144. bne process_exception @ might as well handle the pending
  145. @ exception before retrying branch
  146. @ out before setting an FPEXC that
  147. @ stops us reading stuff
  148. VFPFMXR FPEXC, r1 @ restore FPEXC last
  149. sub r2, r2, #4
  150. str r2, [sp, #S_PC] @ retry the instruction
  151. #ifdef CONFIG_PREEMPT
  152. get_thread_info r10
  153. ldr r4, [r10, #TI_PREEMPT] @ get preempt count
  154. sub r11, r4, #1 @ decrement it
  155. str r11, [r10, #TI_PREEMPT]
  156. #endif
  157. mov pc, r9 @ we think we have handled things
  158. look_for_VFP_exceptions:
  159. @ Check for synchronous or asynchronous exception
  160. tst r1, #FPEXC_EX | FPEXC_DEX
  161. bne process_exception
  162. @ On some implementations of the VFP subarch 1, setting FPSCR.IXE
  163. @ causes all the CDP instructions to be bounced synchronously without
  164. @ setting the FPEXC.EX bit
  165. VFPFMRX r5, FPSCR
  166. tst r5, #FPSCR_IXE
  167. bne process_exception
  168. @ Fall into hand on to next handler - appropriate coproc instr
  169. @ not recognised by VFP
  170. DBGSTR "not VFP"
  171. #ifdef CONFIG_PREEMPT
  172. get_thread_info r10
  173. ldr r4, [r10, #TI_PREEMPT] @ get preempt count
  174. sub r11, r4, #1 @ decrement it
  175. str r11, [r10, #TI_PREEMPT]
  176. #endif
  177. mov pc, lr
  178. process_exception:
  179. DBGSTR "bounce"
  180. mov r2, sp @ nothing stacked - regdump is at TOS
  181. mov lr, r9 @ setup for a return to the user code.
  182. @ Now call the C code to package up the bounce to the support code
  183. @ r0 holds the trigger instruction
  184. @ r1 holds the FPEXC value
  185. @ r2 pointer to register dump
  186. b VFP_bounce @ we have handled this - the support
  187. @ code will raise an exception if
  188. @ required. If not, the user code will
  189. @ retry the faulted instruction
  190. ENDPROC(vfp_support_entry)
  191. ENTRY(vfp_save_state)
  192. @ Save the current VFP state
  193. @ r0 - save location
  194. @ r1 - FPEXC
  195. DBGSTR1 "save VFP state %p", r0
  196. VFPFSTMIA r0, r2 @ save the working registers
  197. VFPFMRX r2, FPSCR @ current status
  198. tst r1, #FPEXC_EX @ is there additional state to save?
  199. beq 1f
  200. VFPFMRX r3, FPINST @ FPINST (only if FPEXC.EX is set)
  201. tst r1, #FPEXC_FP2V @ is there an FPINST2 to read?
  202. beq 1f
  203. VFPFMRX r12, FPINST2 @ FPINST2 if needed (and present)
  204. 1:
  205. stmia r0, {r1, r2, r3, r12} @ save FPEXC, FPSCR, FPINST, FPINST2
  206. mov pc, lr
  207. ENDPROC(vfp_save_state)
  208. .align
  209. vfp_current_hw_state_address:
  210. .word vfp_current_hw_state
  211. .macro tbl_branch, base, tmp, shift
  212. #ifdef CONFIG_THUMB2_KERNEL
  213. adr \tmp, 1f
  214. add \tmp, \tmp, \base, lsl \shift
  215. mov pc, \tmp
  216. #else
  217. add pc, pc, \base, lsl \shift
  218. mov r0, r0
  219. #endif
  220. 1:
  221. .endm
  222. ENTRY(vfp_get_float)
  223. tbl_branch r0, r3, #3
  224. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  225. 1: mrc p10, 0, r0, c\dr, c0, 0 @ fmrs r0, s0
  226. mov pc, lr
  227. .org 1b + 8
  228. 1: mrc p10, 0, r0, c\dr, c0, 4 @ fmrs r0, s1
  229. mov pc, lr
  230. .org 1b + 8
  231. .endr
  232. ENDPROC(vfp_get_float)
  233. ENTRY(vfp_put_float)
  234. tbl_branch r1, r3, #3
  235. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  236. 1: mcr p10, 0, r0, c\dr, c0, 0 @ fmsr r0, s0
  237. mov pc, lr
  238. .org 1b + 8
  239. 1: mcr p10, 0, r0, c\dr, c0, 4 @ fmsr r0, s1
  240. mov pc, lr
  241. .org 1b + 8
  242. .endr
  243. ENDPROC(vfp_put_float)
  244. ENTRY(vfp_get_double)
  245. tbl_branch r0, r3, #3
  246. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  247. 1: fmrrd r0, r1, d\dr
  248. mov pc, lr
  249. .org 1b + 8
  250. .endr
  251. #ifdef CONFIG_VFPv3
  252. @ d16 - d31 registers
  253. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  254. 1: mrrc p11, 3, r0, r1, c\dr @ fmrrd r0, r1, d\dr
  255. mov pc, lr
  256. .org 1b + 8
  257. .endr
  258. #endif
  259. @ virtual register 16 (or 32 if VFPv3) for compare with zero
  260. mov r0, #0
  261. mov r1, #0
  262. mov pc, lr
  263. ENDPROC(vfp_get_double)
  264. ENTRY(vfp_put_double)
  265. tbl_branch r2, r3, #3
  266. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  267. 1: fmdrr d\dr, r0, r1
  268. mov pc, lr
  269. .org 1b + 8
  270. .endr
  271. #ifdef CONFIG_VFPv3
  272. @ d16 - d31 registers
  273. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  274. 1: mcrr p11, 3, r0, r1, c\dr @ fmdrr r0, r1, d\dr
  275. mov pc, lr
  276. .org 1b + 8
  277. .endr
  278. #endif
  279. ENDPROC(vfp_put_double)