vfphw.S 8.6 KB

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