2 * rtrap.S: Preparing for return from trap on Sparc V9.
4 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
10 #include <asm/pstate.h>
11 #include <asm/ptrace.h>
12 #include <asm/spitfire.h>
14 #include <asm/visasm.h>
15 #include <asm/processor.h>
17 #define RTRAP_PSTATE (PSTATE_TSO|PSTATE_PEF|PSTATE_PRIV|PSTATE_IE)
18 #define RTRAP_PSTATE_IRQOFF (PSTATE_TSO|PSTATE_PEF|PSTATE_PRIV)
19 #define RTRAP_PSTATE_AG_IRQOFF (PSTATE_TSO|PSTATE_PEF|PSTATE_PRIV|PSTATE_AG)
21 #ifdef CONFIG_CONTEXT_TRACKING
22 # define SCHEDULE_USER schedule_user
24 # define SCHEDULE_USER schedule
31 wrpr %g0, RTRAP_PSTATE, %pstate
32 ba,pt %xcc, __handle_preemption_continue
33 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
35 __handle_user_windows:
36 call fault_in_user_windows
37 wrpr %g0, RTRAP_PSTATE, %pstate
38 ba,pt %xcc, __handle_preemption_continue
39 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
43 andcc %l5, FPRS_FEF, %g0
44 sethi %hi(TSTATE_PEF), %o0
45 be,a,pn %icc, __handle_userfpu_continue
47 ba,a,pt %xcc, __handle_userfpu_continue
51 add %sp, PTREGS_OFF, %o0
54 wrpr %g0, RTRAP_PSTATE, %pstate
55 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
57 /* Signal delivery can modify pt_regs tstate, so we must
60 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
61 sethi %hi(0xf << 20), %l4
63 ba,pt %xcc, __handle_preemption_continue
66 /* When returning from a NMI (%pil==15) interrupt we want to
67 * avoid running softirqs, doing IRQ tracing, preempting, etc.
70 rtrap_nmi: ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
71 sethi %hi(0xf << 20), %l4
75 ba,pt %xcc, rtrap_no_irq_enable
79 .globl rtrap_irq, rtrap, irqsz_patchme, rtrap_xcall
82 /* mm/ultra.S:xcall_report_regs KNOWS about this load. */
83 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
85 sethi %hi(0xf << 20), %l4
89 #ifdef CONFIG_TRACE_IRQFLAGS
90 brnz,pn %l4, rtrap_no_irq_enable
92 call trace_hardirqs_on
94 /* Do not actually set the %pil here. We will do that
95 * below after we clear PSTATE_IE in the %pstate register.
96 * If we re-enable interrupts here, we can recurse down
97 * the hardirq stack potentially endlessly, causing a
100 * It is tempting to put this test and trace_hardirqs_on
101 * call at the 'rt_continue' label, but that will not work
102 * as that path hits unconditionally and we do not want to
103 * execute this in NMI return paths, for example.
107 andcc %l1, TSTATE_PRIV, %l3
108 bne,pn %icc, to_kernel
111 /* We must hold IRQs off and atomically test schedule+signal
112 * state, then hold them off all the way back to userspace.
113 * If we are returning to kernel, none of this matters. Note
114 * that we are disabling interrupts via PSTATE_IE, not using
117 * If we do not do this, there is a window where we would do
118 * the tests, later the signal/resched event arrives but we do
119 * not process it since we are still in kernel mode. It would
120 * take until the next local IRQ before the signal/resched
121 * event would be handled.
123 * This also means that if we have to deal with user
124 * windows, we have to redo all of these sched+signal checks
125 * with IRQs disabled.
127 to_user: wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
129 __handle_preemption_continue:
130 ldx [%g6 + TI_FLAGS], %l0
131 sethi %hi(_TIF_USER_WORK_MASK), %o0
132 or %o0, %lo(_TIF_USER_WORK_MASK), %o0
134 sethi %hi(TSTATE_PEF), %o0
135 be,pt %xcc, user_nowork
137 andcc %l0, _TIF_NEED_RESCHED, %g0
138 bne,pn %xcc, __handle_preemption
139 andcc %l0, _TIF_DO_NOTIFY_RESUME_MASK, %g0
140 bne,pn %xcc, __handle_signal
141 ldub [%g6 + TI_WSAVED], %o2
142 brnz,pn %o2, __handle_user_windows
144 sethi %hi(TSTATE_PEF), %o0
147 /* This fpdepth clear is necessary for non-syscall rtraps only */
149 bne,pn %xcc, __handle_userfpu
150 stb %g0, [%g6 + TI_FPDEPTH]
151 __handle_userfpu_continue:
153 rt_continue: ldx [%sp + PTREGS_OFF + PT_V9_G1], %g1
154 ldx [%sp + PTREGS_OFF + PT_V9_G2], %g2
156 ldx [%sp + PTREGS_OFF + PT_V9_G3], %g3
157 ldx [%sp + PTREGS_OFF + PT_V9_G4], %g4
158 ldx [%sp + PTREGS_OFF + PT_V9_G5], %g5
162 /* Must do this before thread reg is clobbered below. */
163 LOAD_PER_CPU_BASE(%g5, %g6, %i0, %i1, %i2)
165 ldx [%sp + PTREGS_OFF + PT_V9_G6], %g6
166 ldx [%sp + PTREGS_OFF + PT_V9_G7], %g7
168 /* Normal globals are restored, go to trap globals. */
169 661: wrpr %g0, RTRAP_PSTATE_AG_IRQOFF, %pstate
171 .section .sun4v_2insn_patch, "ax"
173 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
179 ldx [%sp + PTREGS_OFF + PT_V9_I0], %i0
180 ldx [%sp + PTREGS_OFF + PT_V9_I1], %i1
182 ldx [%sp + PTREGS_OFF + PT_V9_I2], %i2
183 ldx [%sp + PTREGS_OFF + PT_V9_I3], %i3
184 ldx [%sp + PTREGS_OFF + PT_V9_I4], %i4
185 ldx [%sp + PTREGS_OFF + PT_V9_I5], %i5
186 ldx [%sp + PTREGS_OFF + PT_V9_I6], %i6
187 ldx [%sp + PTREGS_OFF + PT_V9_I7], %i7
188 ldx [%sp + PTREGS_OFF + PT_V9_TPC], %l2
189 ldx [%sp + PTREGS_OFF + PT_V9_TNPC], %o2
191 ld [%sp + PTREGS_OFF + PT_V9_Y], %o3
195 andn %l1, TSTATE_SYSCALL, %l1
196 wrpr %l1, %g0, %tstate
200 brnz,pn %l3, kern_rtt
201 mov PRIMARY_CONTEXT, %l7
203 661: ldxa [%l7 + %l7] ASI_DMMU, %l0
204 .section .sun4v_1insn_patch, "ax"
206 ldxa [%l7 + %l7] ASI_MMU, %l0
209 sethi %hi(sparc64_kern_pri_nuc_bits), %l1
210 ldx [%l1 + %lo(sparc64_kern_pri_nuc_bits)], %l1
213 661: stxa %l0, [%l7] ASI_DMMU
214 .section .sun4v_1insn_patch, "ax"
216 stxa %l0, [%l7] ASI_MMU
219 sethi %hi(KERNBASE), %l7
225 wrpr %l2, %g0, %canrestore
226 wrpr %l1, %g0, %wstate
227 brnz,pt %l2, user_rtt_restore
228 wrpr %g0, %g0, %otherwin
230 ldx [%g6 + TI_FLAGS], %g3
231 wr %g0, ASI_AIUP, %asi
233 andcc %g3, _TIF_32BIT, %g0
235 bne,pt %xcc, user_rtt_fill_32bit
237 ba,a,pt %xcc, user_rtt_fill_64bit
246 wrpr %g2, 0x0, %wstate
248 /* We know %canrestore and %otherwin are both zero. */
250 sethi %hi(sparc64_kern_pri_context), %g2
251 ldx [%g2 + %lo(sparc64_kern_pri_context)], %g2
252 mov PRIMARY_CONTEXT, %g1
254 661: stxa %g2, [%g1] ASI_DMMU
255 .section .sun4v_1insn_patch, "ax"
257 stxa %g2, [%g1] ASI_MMU
260 sethi %hi(KERNBASE), %g1
263 or %g4, FAULT_CODE_WINFIXUP, %g4
264 stb %g4, [%g6 + TI_FAULT_CODE]
265 stx %g5, [%g6 + TI_FAULT_ADDR]
271 .section .sun4v_1insn_patch, "ax"
276 wrpr %g0, RTRAP_PSTATE, %pstate
279 ldx [%g6 + TI_TASK], %g4
280 LOAD_PER_CPU_BASE(%g5, %g6, %g1, %g2, %g3)
281 call do_sparc64_fault
282 add %sp, PTREGS_OFF, %o0
286 user_rtt_pre_restore:
292 rdpr %canrestore, %g1
293 wrpr %g1, 0x0, %cleanwin
297 kern_rtt: rdpr %canrestore, %g1
298 brz,pn %g1, kern_rtt_fill
301 stw %g0, [%sp + PTREGS_OFF + PT_V9_MAGIC]
306 #ifdef CONFIG_PREEMPT
307 ldsw [%g6 + TI_PRE_COUNT], %l5
308 brnz %l5, kern_fpucheck
309 ldx [%g6 + TI_FLAGS], %l5
310 andcc %l5, _TIF_NEED_RESCHED, %g0
311 be,pt %xcc, kern_fpucheck
314 bne,pn %xcc, kern_fpucheck
316 call preempt_schedule_irq
320 kern_fpucheck: ldub [%g6 + TI_FPDEPTH], %l5
321 brz,pt %l5, rt_continue
323 add %g6, TI_FPSAVED, %l6
324 ldub [%l6 + %o0], %l2
328 andcc %l2, (FPRS_FEF|FPRS_DU), %g0
330 and %l2, FPRS_DL, %l6
331 andcc %l2, FPRS_FEF, %g0
336 wr %g1, FPRS_FEF, %fprs
338 add %g6, TI_XFSR, %o1
340 add %g6, TI_FPREGS, %o3
342 add %g6, TI_FPREGS+0x40, %o4
345 ldda [%o3 + %o2] ASI_BLK_P, %f0
346 ldda [%o4 + %o2] ASI_BLK_P, %f16
348 1: andcc %l2, FPRS_DU, %g0
353 ldda [%o3 + %o2] ASI_BLK_P, %f32
354 ldda [%o4 + %o2] ASI_BLK_P, %f48
356 ldx [%o1 + %o5], %fsr
357 2: stb %l5, [%g6 + TI_FPDEPTH]
358 ba,pt %xcc, rt_continue
360 5: wr %g0, FPRS_FEF, %fprs
363 add %g6, TI_FPREGS+0x80, %o3
364 add %g6, TI_FPREGS+0xc0, %o4
366 ldda [%o3 + %o2] ASI_BLK_P, %f32
367 ldda [%o4 + %o2] ASI_BLK_P, %f48
369 wr %g0, FPRS_DU, %fprs
370 ba,pt %xcc, rt_continue
371 stb %l5, [%g6 + TI_FPDEPTH]