rtrap_64.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * rtrap.S: Preparing for return from trap on Sparc V9.
  4. *
  5. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  7. */
  8. #include <asm/asi.h>
  9. #include <asm/pstate.h>
  10. #include <asm/ptrace.h>
  11. #include <asm/spitfire.h>
  12. #include <asm/head.h>
  13. #include <asm/visasm.h>
  14. #include <asm/processor.h>
  15. #ifdef CONFIG_CONTEXT_TRACKING
  16. # define SCHEDULE_USER schedule_user
  17. #else
  18. # define SCHEDULE_USER schedule
  19. #endif
  20. .text
  21. .align 32
  22. __handle_preemption:
  23. call SCHEDULE_USER
  24. 661: wrpr %g0, RTRAP_PSTATE, %pstate
  25. /* If userspace is using ADI, it could potentially pass
  26. * a pointer with version tag embedded in it. To maintain
  27. * the ADI security, we must re-enable PSTATE.mcde before
  28. * we continue execution in the kernel for another thread.
  29. */
  30. .section .sun_m7_1insn_patch, "ax"
  31. .word 661b
  32. wrpr %g0, RTRAP_PSTATE|PSTATE_MCDE, %pstate
  33. .previous
  34. ba,pt %xcc, __handle_preemption_continue
  35. wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
  36. __handle_user_windows:
  37. call fault_in_user_windows
  38. 661: wrpr %g0, RTRAP_PSTATE, %pstate
  39. /* If userspace is using ADI, it could potentially pass
  40. * a pointer with version tag embedded in it. To maintain
  41. * the ADI security, we must re-enable PSTATE.mcde before
  42. * we continue execution in the kernel for another thread.
  43. */
  44. .section .sun_m7_1insn_patch, "ax"
  45. .word 661b
  46. wrpr %g0, RTRAP_PSTATE|PSTATE_MCDE, %pstate
  47. .previous
  48. ba,pt %xcc, __handle_preemption_continue
  49. wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
  50. __handle_userfpu:
  51. rd %fprs, %l5
  52. andcc %l5, FPRS_FEF, %g0
  53. sethi %hi(TSTATE_PEF), %o0
  54. be,a,pn %icc, __handle_userfpu_continue
  55. andn %l1, %o0, %l1
  56. ba,a,pt %xcc, __handle_userfpu_continue
  57. __handle_signal:
  58. mov %l5, %o1
  59. add %sp, PTREGS_OFF, %o0
  60. mov %l0, %o2
  61. call do_notify_resume
  62. 661: wrpr %g0, RTRAP_PSTATE, %pstate
  63. /* If userspace is using ADI, it could potentially pass
  64. * a pointer with version tag embedded in it. To maintain
  65. * the ADI security, we must re-enable PSTATE.mcde before
  66. * we continue execution in the kernel for another thread.
  67. */
  68. .section .sun_m7_1insn_patch, "ax"
  69. .word 661b
  70. wrpr %g0, RTRAP_PSTATE|PSTATE_MCDE, %pstate
  71. .previous
  72. wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
  73. /* Signal delivery can modify pt_regs tstate, so we must
  74. * reload it.
  75. */
  76. ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
  77. sethi %hi(0xf << 20), %l4
  78. and %l1, %l4, %l4
  79. ba,pt %xcc, __handle_preemption_continue
  80. andn %l1, %l4, %l1
  81. /* When returning from a NMI (%pil==15) interrupt we want to
  82. * avoid running softirqs, doing IRQ tracing, preempting, etc.
  83. */
  84. .globl rtrap_nmi
  85. rtrap_nmi: ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
  86. sethi %hi(0xf << 20), %l4
  87. and %l1, %l4, %l4
  88. andn %l1, %l4, %l1
  89. srl %l4, 20, %l4
  90. ba,pt %xcc, rtrap_no_irq_enable
  91. nop
  92. /* Do not actually set the %pil here. We will do that
  93. * below after we clear PSTATE_IE in the %pstate register.
  94. * If we re-enable interrupts here, we can recurse down
  95. * the hardirq stack potentially endlessly, causing a
  96. * stack overflow.
  97. */
  98. .align 64
  99. .globl rtrap_irq, rtrap, irqsz_patchme, rtrap_xcall
  100. rtrap_irq:
  101. rtrap:
  102. /* mm/ultra.S:xcall_report_regs KNOWS about this load. */
  103. ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
  104. rtrap_xcall:
  105. sethi %hi(0xf << 20), %l4
  106. and %l1, %l4, %l4
  107. andn %l1, %l4, %l1
  108. srl %l4, 20, %l4
  109. #ifdef CONFIG_TRACE_IRQFLAGS
  110. brnz,pn %l4, rtrap_no_irq_enable
  111. nop
  112. call trace_hardirqs_on
  113. nop
  114. /* Do not actually set the %pil here. We will do that
  115. * below after we clear PSTATE_IE in the %pstate register.
  116. * If we re-enable interrupts here, we can recurse down
  117. * the hardirq stack potentially endlessly, causing a
  118. * stack overflow.
  119. *
  120. * It is tempting to put this test and trace_hardirqs_on
  121. * call at the 'rt_continue' label, but that will not work
  122. * as that path hits unconditionally and we do not want to
  123. * execute this in NMI return paths, for example.
  124. */
  125. #endif
  126. rtrap_no_irq_enable:
  127. andcc %l1, TSTATE_PRIV, %l3
  128. bne,pn %icc, to_kernel
  129. nop
  130. /* We must hold IRQs off and atomically test schedule+signal
  131. * state, then hold them off all the way back to userspace.
  132. * If we are returning to kernel, none of this matters. Note
  133. * that we are disabling interrupts via PSTATE_IE, not using
  134. * %pil.
  135. *
  136. * If we do not do this, there is a window where we would do
  137. * the tests, later the signal/resched event arrives but we do
  138. * not process it since we are still in kernel mode. It would
  139. * take until the next local IRQ before the signal/resched
  140. * event would be handled.
  141. *
  142. * This also means that if we have to deal with user
  143. * windows, we have to redo all of these sched+signal checks
  144. * with IRQs disabled.
  145. */
  146. to_user: wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
  147. wrpr 0, %pil
  148. __handle_preemption_continue:
  149. ldx [%g6 + TI_FLAGS], %l0
  150. sethi %hi(_TIF_USER_WORK_MASK), %o0
  151. or %o0, %lo(_TIF_USER_WORK_MASK), %o0
  152. andcc %l0, %o0, %g0
  153. sethi %hi(TSTATE_PEF), %o0
  154. be,pt %xcc, user_nowork
  155. andcc %l1, %o0, %g0
  156. andcc %l0, _TIF_NEED_RESCHED, %g0
  157. bne,pn %xcc, __handle_preemption
  158. andcc %l0, _TIF_DO_NOTIFY_RESUME_MASK, %g0
  159. bne,pn %xcc, __handle_signal
  160. ldub [%g6 + TI_WSAVED], %o2
  161. brnz,pn %o2, __handle_user_windows
  162. nop
  163. sethi %hi(TSTATE_PEF), %o0
  164. andcc %l1, %o0, %g0
  165. /* This fpdepth clear is necessary for non-syscall rtraps only */
  166. user_nowork:
  167. bne,pn %xcc, __handle_userfpu
  168. stb %g0, [%g6 + TI_FPDEPTH]
  169. __handle_userfpu_continue:
  170. rt_continue: ldx [%sp + PTREGS_OFF + PT_V9_G1], %g1
  171. ldx [%sp + PTREGS_OFF + PT_V9_G2], %g2
  172. ldx [%sp + PTREGS_OFF + PT_V9_G3], %g3
  173. ldx [%sp + PTREGS_OFF + PT_V9_G4], %g4
  174. ldx [%sp + PTREGS_OFF + PT_V9_G5], %g5
  175. brz,pt %l3, 1f
  176. mov %g6, %l2
  177. /* Must do this before thread reg is clobbered below. */
  178. LOAD_PER_CPU_BASE(%g5, %g6, %i0, %i1, %i2)
  179. 1:
  180. ldx [%sp + PTREGS_OFF + PT_V9_G6], %g6
  181. ldx [%sp + PTREGS_OFF + PT_V9_G7], %g7
  182. /* Normal globals are restored, go to trap globals. */
  183. 661: wrpr %g0, RTRAP_PSTATE_AG_IRQOFF, %pstate
  184. nop
  185. .section .sun4v_2insn_patch, "ax"
  186. .word 661b
  187. wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
  188. SET_GL(1)
  189. .previous
  190. mov %l2, %g6
  191. ldx [%sp + PTREGS_OFF + PT_V9_I0], %i0
  192. ldx [%sp + PTREGS_OFF + PT_V9_I1], %i1
  193. ldx [%sp + PTREGS_OFF + PT_V9_I2], %i2
  194. ldx [%sp + PTREGS_OFF + PT_V9_I3], %i3
  195. ldx [%sp + PTREGS_OFF + PT_V9_I4], %i4
  196. ldx [%sp + PTREGS_OFF + PT_V9_I5], %i5
  197. ldx [%sp + PTREGS_OFF + PT_V9_I6], %i6
  198. ldx [%sp + PTREGS_OFF + PT_V9_I7], %i7
  199. ldx [%sp + PTREGS_OFF + PT_V9_TPC], %l2
  200. ldx [%sp + PTREGS_OFF + PT_V9_TNPC], %o2
  201. ld [%sp + PTREGS_OFF + PT_V9_Y], %o3
  202. wr %o3, %g0, %y
  203. wrpr %l4, 0x0, %pil
  204. wrpr %g0, 0x1, %tl
  205. andn %l1, TSTATE_SYSCALL, %l1
  206. wrpr %l1, %g0, %tstate
  207. wrpr %l2, %g0, %tpc
  208. wrpr %o2, %g0, %tnpc
  209. brnz,pn %l3, kern_rtt
  210. mov PRIMARY_CONTEXT, %l7
  211. 661: ldxa [%l7 + %l7] ASI_DMMU, %l0
  212. .section .sun4v_1insn_patch, "ax"
  213. .word 661b
  214. ldxa [%l7 + %l7] ASI_MMU, %l0
  215. .previous
  216. sethi %hi(sparc64_kern_pri_nuc_bits), %l1
  217. ldx [%l1 + %lo(sparc64_kern_pri_nuc_bits)], %l1
  218. or %l0, %l1, %l0
  219. 661: stxa %l0, [%l7] ASI_DMMU
  220. .section .sun4v_1insn_patch, "ax"
  221. .word 661b
  222. stxa %l0, [%l7] ASI_MMU
  223. .previous
  224. sethi %hi(KERNBASE), %l7
  225. flush %l7
  226. rdpr %wstate, %l1
  227. rdpr %otherwin, %l2
  228. srl %l1, 3, %l1
  229. 661: wrpr %l2, %g0, %canrestore
  230. .section .fast_win_ctrl_1insn_patch, "ax"
  231. .word 661b
  232. .word 0x89880000 ! normalw
  233. .previous
  234. wrpr %l1, %g0, %wstate
  235. brnz,pt %l2, user_rtt_restore
  236. 661: wrpr %g0, %g0, %otherwin
  237. .section .fast_win_ctrl_1insn_patch, "ax"
  238. .word 661b
  239. nop
  240. .previous
  241. ldx [%g6 + TI_FLAGS], %g3
  242. wr %g0, ASI_AIUP, %asi
  243. rdpr %cwp, %g1
  244. andcc %g3, _TIF_32BIT, %g0
  245. sub %g1, 1, %g1
  246. bne,pt %xcc, user_rtt_fill_32bit
  247. wrpr %g1, %cwp
  248. ba,a,pt %xcc, user_rtt_fill_64bit
  249. nop
  250. user_rtt_fill_fixup_dax:
  251. ba,pt %xcc, user_rtt_fill_fixup_common
  252. mov 1, %g3
  253. user_rtt_fill_fixup_mna:
  254. ba,pt %xcc, user_rtt_fill_fixup_common
  255. mov 2, %g3
  256. user_rtt_fill_fixup:
  257. ba,pt %xcc, user_rtt_fill_fixup_common
  258. clr %g3
  259. user_rtt_pre_restore:
  260. add %g1, 1, %g1
  261. wrpr %g1, 0x0, %cwp
  262. user_rtt_restore:
  263. restore
  264. rdpr %canrestore, %g1
  265. wrpr %g1, 0x0, %cleanwin
  266. retry
  267. nop
  268. kern_rtt: rdpr %canrestore, %g1
  269. brz,pn %g1, kern_rtt_fill
  270. nop
  271. kern_rtt_restore:
  272. stw %g0, [%sp + PTREGS_OFF + PT_V9_MAGIC]
  273. restore
  274. retry
  275. to_kernel:
  276. #ifdef CONFIG_PREEMPT
  277. ldsw [%g6 + TI_PRE_COUNT], %l5
  278. brnz %l5, kern_fpucheck
  279. ldx [%g6 + TI_FLAGS], %l5
  280. andcc %l5, _TIF_NEED_RESCHED, %g0
  281. be,pt %xcc, kern_fpucheck
  282. nop
  283. cmp %l4, 0
  284. bne,pn %xcc, kern_fpucheck
  285. nop
  286. call preempt_schedule_irq
  287. nop
  288. ba,pt %xcc, rtrap
  289. #endif
  290. kern_fpucheck: ldub [%g6 + TI_FPDEPTH], %l5
  291. brz,pt %l5, rt_continue
  292. srl %l5, 1, %o0
  293. add %g6, TI_FPSAVED, %l6
  294. ldub [%l6 + %o0], %l2
  295. sub %l5, 2, %l5
  296. add %g6, TI_GSR, %o1
  297. andcc %l2, (FPRS_FEF|FPRS_DU), %g0
  298. be,pt %icc, 2f
  299. and %l2, FPRS_DL, %l6
  300. andcc %l2, FPRS_FEF, %g0
  301. be,pn %icc, 5f
  302. sll %o0, 3, %o5
  303. rd %fprs, %g1
  304. wr %g1, FPRS_FEF, %fprs
  305. ldx [%o1 + %o5], %g1
  306. add %g6, TI_XFSR, %o1
  307. sll %o0, 8, %o2
  308. add %g6, TI_FPREGS, %o3
  309. brz,pn %l6, 1f
  310. add %g6, TI_FPREGS+0x40, %o4
  311. membar #Sync
  312. ldda [%o3 + %o2] ASI_BLK_P, %f0
  313. ldda [%o4 + %o2] ASI_BLK_P, %f16
  314. membar #Sync
  315. 1: andcc %l2, FPRS_DU, %g0
  316. be,pn %icc, 1f
  317. wr %g1, 0, %gsr
  318. add %o2, 0x80, %o2
  319. membar #Sync
  320. ldda [%o3 + %o2] ASI_BLK_P, %f32
  321. ldda [%o4 + %o2] ASI_BLK_P, %f48
  322. 1: membar #Sync
  323. ldx [%o1 + %o5], %fsr
  324. 2: stb %l5, [%g6 + TI_FPDEPTH]
  325. ba,pt %xcc, rt_continue
  326. nop
  327. 5: wr %g0, FPRS_FEF, %fprs
  328. sll %o0, 8, %o2
  329. add %g6, TI_FPREGS+0x80, %o3
  330. add %g6, TI_FPREGS+0xc0, %o4
  331. membar #Sync
  332. ldda [%o3 + %o2] ASI_BLK_P, %f32
  333. ldda [%o4 + %o2] ASI_BLK_P, %f48
  334. membar #Sync
  335. wr %g0, FPRS_DU, %fprs
  336. ba,pt %xcc, rt_continue
  337. stb %l5, [%g6 + TI_FPDEPTH]