vfphw.S 8.4 KB

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