1 /* SPDX-License-Identifier: GPL-2.0 */
3 * rtrap.S: Preparing for return from trap on Sparc V9.
5 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
11 #include <asm/pstate.h>
12 #include <asm/ptrace.h>
13 #include <asm/spitfire.h>
15 #include <asm/visasm.h>
16 #include <asm/processor.h>
18 #ifdef CONFIG_CONTEXT_TRACKING
19 # define SCHEDULE_USER schedule_user
21 # define SCHEDULE_USER schedule
28 661: wrpr %g0, RTRAP_PSTATE, %pstate
29 /* If userspace is using ADI, it could potentially pass
30 * a pointer with version tag embedded in it. To maintain
31 * the ADI security, we must re-enable PSTATE.mcde before
32 * we continue execution in the kernel for another thread.
34 .section .sun_m7_1insn_patch, "ax"
36 wrpr %g0, RTRAP_PSTATE|PSTATE_MCDE, %pstate
38 ba,pt %xcc, __handle_preemption_continue
39 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
41 __handle_user_windows:
42 add %sp, PTREGS_OFF, %o0
43 call fault_in_user_windows
44 661: wrpr %g0, RTRAP_PSTATE, %pstate
45 /* If userspace is using ADI, it could potentially pass
46 * a pointer with version tag embedded in it. To maintain
47 * the ADI security, we must re-enable PSTATE.mcde before
48 * we continue execution in the kernel for another thread.
50 .section .sun_m7_1insn_patch, "ax"
52 wrpr %g0, RTRAP_PSTATE|PSTATE_MCDE, %pstate
54 ba,pt %xcc, __handle_preemption_continue
55 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
59 andcc %l5, FPRS_FEF, %g0
60 sethi %hi(TSTATE_PEF), %o0
61 be,a,pn %icc, __handle_userfpu_continue
63 ba,a,pt %xcc, __handle_userfpu_continue
67 add %sp, PTREGS_OFF, %o0
70 661: wrpr %g0, RTRAP_PSTATE, %pstate
71 /* If userspace is using ADI, it could potentially pass
72 * a pointer with version tag embedded in it. To maintain
73 * the ADI security, we must re-enable PSTATE.mcde before
74 * we continue execution in the kernel for another thread.
76 .section .sun_m7_1insn_patch, "ax"
78 wrpr %g0, RTRAP_PSTATE|PSTATE_MCDE, %pstate
80 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
82 /* Signal delivery can modify pt_regs tstate, so we must
85 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
86 sethi %hi(0xf << 20), %l4
89 ba,pt %xcc, __handle_preemption_continue
92 /* When returning from a NMI (%pil==15) interrupt we want to
93 * avoid running softirqs, doing IRQ tracing, preempting, etc.
96 rtrap_nmi: ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
97 sethi %hi(0xf << 20), %l4
101 ba,pt %xcc, rtrap_no_irq_enable
103 /* Do not actually set the %pil here. We will do that
104 * below after we clear PSTATE_IE in the %pstate register.
105 * If we re-enable interrupts here, we can recurse down
106 * the hardirq stack potentially endlessly, causing a
111 .globl rtrap_irq, rtrap, irqsz_patchme, rtrap_xcall
114 /* mm/ultra.S:xcall_report_regs KNOWS about this load. */
115 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
117 sethi %hi(0xf << 20), %l4
121 #ifdef CONFIG_TRACE_IRQFLAGS
122 brnz,pn %l4, rtrap_no_irq_enable
124 call trace_hardirqs_on
126 /* Do not actually set the %pil here. We will do that
127 * below after we clear PSTATE_IE in the %pstate register.
128 * If we re-enable interrupts here, we can recurse down
129 * the hardirq stack potentially endlessly, causing a
132 * It is tempting to put this test and trace_hardirqs_on
133 * call at the 'rt_continue' label, but that will not work
134 * as that path hits unconditionally and we do not want to
135 * execute this in NMI return paths, for example.
139 andcc %l1, TSTATE_PRIV, %l3
140 bne,pn %icc, to_kernel
143 /* We must hold IRQs off and atomically test schedule+signal
144 * state, then hold them off all the way back to userspace.
145 * If we are returning to kernel, none of this matters. Note
146 * that we are disabling interrupts via PSTATE_IE, not using
149 * If we do not do this, there is a window where we would do
150 * the tests, later the signal/resched event arrives but we do
151 * not process it since we are still in kernel mode. It would
152 * take until the next local IRQ before the signal/resched
153 * event would be handled.
155 * This also means that if we have to deal with user
156 * windows, we have to redo all of these sched+signal checks
157 * with IRQs disabled.
159 to_user: wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
161 __handle_preemption_continue:
162 ldx [%g6 + TI_FLAGS], %l0
163 sethi %hi(_TIF_USER_WORK_MASK), %o0
164 or %o0, %lo(_TIF_USER_WORK_MASK), %o0
166 sethi %hi(TSTATE_PEF), %o0
167 be,pt %xcc, user_nowork
169 andcc %l0, _TIF_NEED_RESCHED, %g0
170 bne,pn %xcc, __handle_preemption
171 andcc %l0, _TIF_DO_NOTIFY_RESUME_MASK, %g0
172 bne,pn %xcc, __handle_signal
173 ldub [%g6 + TI_WSAVED], %o2
174 brnz,pn %o2, __handle_user_windows
176 sethi %hi(TSTATE_PEF), %o0
179 /* This fpdepth clear is necessary for non-syscall rtraps only */
181 bne,pn %xcc, __handle_userfpu
182 stb %g0, [%g6 + TI_FPDEPTH]
183 __handle_userfpu_continue:
185 rt_continue: ldx [%sp + PTREGS_OFF + PT_V9_G1], %g1
186 ldx [%sp + PTREGS_OFF + PT_V9_G2], %g2
188 ldx [%sp + PTREGS_OFF + PT_V9_G3], %g3
189 ldx [%sp + PTREGS_OFF + PT_V9_G4], %g4
190 ldx [%sp + PTREGS_OFF + PT_V9_G5], %g5
194 /* Must do this before thread reg is clobbered below. */
195 LOAD_PER_CPU_BASE(%g5, %g6, %i0, %i1, %i2)
197 ldx [%sp + PTREGS_OFF + PT_V9_G6], %g6
198 ldx [%sp + PTREGS_OFF + PT_V9_G7], %g7
200 /* Normal globals are restored, go to trap globals. */
201 661: wrpr %g0, RTRAP_PSTATE_AG_IRQOFF, %pstate
203 .section .sun4v_2insn_patch, "ax"
205 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
211 ldx [%sp + PTREGS_OFF + PT_V9_I0], %i0
212 ldx [%sp + PTREGS_OFF + PT_V9_I1], %i1
214 ldx [%sp + PTREGS_OFF + PT_V9_I2], %i2
215 ldx [%sp + PTREGS_OFF + PT_V9_I3], %i3
216 ldx [%sp + PTREGS_OFF + PT_V9_I4], %i4
217 ldx [%sp + PTREGS_OFF + PT_V9_I5], %i5
218 ldx [%sp + PTREGS_OFF + PT_V9_I6], %i6
219 ldx [%sp + PTREGS_OFF + PT_V9_I7], %i7
220 ldx [%sp + PTREGS_OFF + PT_V9_TPC], %l2
221 ldx [%sp + PTREGS_OFF + PT_V9_TNPC], %o2
223 ld [%sp + PTREGS_OFF + PT_V9_Y], %o3
227 andn %l1, TSTATE_SYSCALL, %l1
228 wrpr %l1, %g0, %tstate
232 brnz,pn %l3, kern_rtt
233 mov PRIMARY_CONTEXT, %l7
235 661: ldxa [%l7 + %l7] ASI_DMMU, %l0
236 .section .sun4v_1insn_patch, "ax"
238 ldxa [%l7 + %l7] ASI_MMU, %l0
241 sethi %hi(sparc64_kern_pri_nuc_bits), %l1
242 ldx [%l1 + %lo(sparc64_kern_pri_nuc_bits)], %l1
245 661: stxa %l0, [%l7] ASI_DMMU
246 .section .sun4v_1insn_patch, "ax"
248 stxa %l0, [%l7] ASI_MMU
251 sethi %hi(KERNBASE), %l7
257 661: wrpr %l2, %g0, %canrestore
258 .section .fast_win_ctrl_1insn_patch, "ax"
260 .word 0x89880000 ! normalw
263 wrpr %l1, %g0, %wstate
264 brnz,pt %l2, user_rtt_restore
265 661: wrpr %g0, %g0, %otherwin
266 .section .fast_win_ctrl_1insn_patch, "ax"
271 ldx [%g6 + TI_FLAGS], %g3
272 wr %g0, ASI_AIUP, %asi
274 andcc %g3, _TIF_32BIT, %g0
276 bne,pt %xcc, user_rtt_fill_32bit
278 ba,a,pt %xcc, user_rtt_fill_64bit
281 user_rtt_fill_fixup_dax:
282 ba,pt %xcc, user_rtt_fill_fixup_common
285 user_rtt_fill_fixup_mna:
286 ba,pt %xcc, user_rtt_fill_fixup_common
290 ba,pt %xcc, user_rtt_fill_fixup_common
293 user_rtt_pre_restore:
299 rdpr %canrestore, %g1
300 wrpr %g1, 0x0, %cleanwin
304 kern_rtt: rdpr %canrestore, %g1
305 brz,pn %g1, kern_rtt_fill
308 stw %g0, [%sp + PTREGS_OFF + PT_V9_MAGIC]
313 #ifdef CONFIG_PREEMPTION
314 ldsw [%g6 + TI_PRE_COUNT], %l5
315 brnz %l5, kern_fpucheck
316 ldx [%g6 + TI_FLAGS], %l5
317 andcc %l5, _TIF_NEED_RESCHED, %g0
318 be,pt %xcc, kern_fpucheck
321 bne,pn %xcc, kern_fpucheck
323 call preempt_schedule_irq
327 kern_fpucheck: ldub [%g6 + TI_FPDEPTH], %l5
328 brz,pt %l5, rt_continue
330 add %g6, TI_FPSAVED, %l6
331 ldub [%l6 + %o0], %l2
335 andcc %l2, (FPRS_FEF|FPRS_DU), %g0
337 and %l2, FPRS_DL, %l6
338 andcc %l2, FPRS_FEF, %g0
343 wr %g1, FPRS_FEF, %fprs
345 add %g6, TI_XFSR, %o1
347 add %g6, TI_FPREGS, %o3
349 add %g6, TI_FPREGS+0x40, %o4
352 ldda [%o3 + %o2] ASI_BLK_P, %f0
353 ldda [%o4 + %o2] ASI_BLK_P, %f16
355 1: andcc %l2, FPRS_DU, %g0
360 ldda [%o3 + %o2] ASI_BLK_P, %f32
361 ldda [%o4 + %o2] ASI_BLK_P, %f48
363 ldx [%o1 + %o5], %fsr
364 2: stb %l5, [%g6 + TI_FPDEPTH]
365 ba,pt %xcc, rt_continue
367 5: wr %g0, FPRS_FEF, %fprs
370 add %g6, TI_FPREGS+0x80, %o3
371 add %g6, TI_FPREGS+0xc0, %o4
373 ldda [%o3 + %o2] ASI_BLK_P, %f32
374 ldda [%o4 + %o2] ASI_BLK_P, %f48
376 wr %g0, FPRS_DU, %fprs
377 ba,pt %xcc, rt_continue
378 stb %l5, [%g6 + TI_FPDEPTH]