blob: 6892cf556f452cb97f3aad4bbf5264cf05424971 [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * rtrap.S: Preparing for return from trap on Sparc V9.
3 *
4 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9#include <asm/asi.h>
10#include <asm/pstate.h>
11#include <asm/ptrace.h>
12#include <asm/spitfire.h>
13#include <asm/head.h>
14#include <asm/visasm.h>
15#include <asm/processor.h>
16
David S. Miller64f2dde2008-10-29 21:25:00 -070017#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 .text
22 .align 32
23__handle_softirq:
24 call do_softirq
25 nop
26 ba,a,pt %xcc, __handle_softirq_continue
27 nop
28__handle_preemption:
29 call schedule
30 wrpr %g0, RTRAP_PSTATE, %pstate
31 ba,pt %xcc, __handle_preemption_continue
32 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
33
34__handle_user_windows:
35 call fault_in_user_windows
36 wrpr %g0, RTRAP_PSTATE, %pstate
37 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
38 /* Redo sched+sig checks */
39 ldx [%g6 + TI_FLAGS], %l0
40 andcc %l0, _TIF_NEED_RESCHED, %g0
41
42 be,pt %xcc, 1f
43 nop
44 call schedule
45 wrpr %g0, RTRAP_PSTATE, %pstate
46 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
47 ldx [%g6 + TI_FLAGS], %l0
48
Roland McGrathe35a8922008-04-20 15:06:49 -0700491: andcc %l0, _TIF_DO_NOTIFY_RESUME_MASK, %g0
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 be,pt %xcc, __handle_user_windows_continue
51 nop
David S. Miller2d7d5f02006-01-19 02:42:49 -080052 mov %l5, %o1
David S. Miller2d7d5f02006-01-19 02:42:49 -080053 add %sp, PTREGS_OFF, %o0
David S. Miller7697daa2008-04-24 03:15:22 -070054 mov %l0, %o2
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 call do_notify_resume
57 wrpr %g0, RTRAP_PSTATE, %pstate
58 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /* Signal delivery can modify pt_regs tstate, so we must
60 * reload it.
61 */
62 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
63 sethi %hi(0xf << 20), %l4
64 and %l1, %l4, %l4
65 ba,pt %xcc, __handle_user_windows_continue
66
67 andn %l1, %l4, %l1
68__handle_perfctrs:
69 call update_perfctrs
70 wrpr %g0, RTRAP_PSTATE, %pstate
71 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
72 ldub [%g6 + TI_WSAVED], %o2
73 brz,pt %o2, 1f
74 nop
75 /* Redo userwin+sched+sig checks */
76 call fault_in_user_windows
77
78 wrpr %g0, RTRAP_PSTATE, %pstate
79 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
80 ldx [%g6 + TI_FLAGS], %l0
81 andcc %l0, _TIF_NEED_RESCHED, %g0
82 be,pt %xcc, 1f
83
84 nop
85 call schedule
86 wrpr %g0, RTRAP_PSTATE, %pstate
87 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
88 ldx [%g6 + TI_FLAGS], %l0
Roland McGrathe35a8922008-04-20 15:06:49 -0700891: andcc %l0, _TIF_DO_NOTIFY_RESUME_MASK, %g0
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 be,pt %xcc, __handle_perfctrs_continue
92 sethi %hi(TSTATE_PEF), %o0
David S. Miller2d7d5f02006-01-19 02:42:49 -080093 mov %l5, %o1
David S. Miller2d7d5f02006-01-19 02:42:49 -080094 add %sp, PTREGS_OFF, %o0
David S. Miller7697daa2008-04-24 03:15:22 -070095 mov %l0, %o2
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 call do_notify_resume
97
98 wrpr %g0, RTRAP_PSTATE, %pstate
99 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* Signal delivery can modify pt_regs tstate, so we must
101 * reload it.
102 */
103 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
104 sethi %hi(0xf << 20), %l4
105 and %l1, %l4, %l4
106 andn %l1, %l4, %l1
107 ba,pt %xcc, __handle_perfctrs_continue
108
109 sethi %hi(TSTATE_PEF), %o0
110__handle_userfpu:
111 rd %fprs, %l5
112 andcc %l5, FPRS_FEF, %g0
113 sethi %hi(TSTATE_PEF), %o0
114 be,a,pn %icc, __handle_userfpu_continue
115 andn %l1, %o0, %l1
116 ba,a,pt %xcc, __handle_userfpu_continue
117
118__handle_signal:
David S. Miller2d7d5f02006-01-19 02:42:49 -0800119 mov %l5, %o1
David S. Miller2d7d5f02006-01-19 02:42:49 -0800120 add %sp, PTREGS_OFF, %o0
David S. Miller7697daa2008-04-24 03:15:22 -0700121 mov %l0, %o2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 call do_notify_resume
123 wrpr %g0, RTRAP_PSTATE, %pstate
124 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /* Signal delivery can modify pt_regs tstate, so we must
127 * reload it.
128 */
129 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
130 sethi %hi(0xf << 20), %l4
131 and %l1, %l4, %l4
132 ba,pt %xcc, __handle_signal_continue
133 andn %l1, %l4, %l1
134
135 .align 64
David S. Miller7697daa2008-04-24 03:15:22 -0700136 .globl rtrap_irq, rtrap, irqsz_patchme, rtrap_xcall
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137rtrap_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138rtrap:
David S. Millerd7ce78f2005-08-29 22:46:43 -0700139#ifndef CONFIG_SMP
140 sethi %hi(per_cpu____cpu_data), %l0
141 lduw [%l0 + %lo(per_cpu____cpu_data)], %l1
142#else
143 sethi %hi(per_cpu____cpu_data), %l0
144 or %l0, %lo(per_cpu____cpu_data), %l0
145 lduw [%l0 + %g5], %l1
146#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 cmp %l1, 0
148
149 /* mm/ultra.S:xcall_report_regs KNOWS about this load. */
150 bne,pn %icc, __handle_softirq
151 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
152__handle_softirq_continue:
153rtrap_xcall:
154 sethi %hi(0xf << 20), %l4
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 and %l1, %l4, %l4
David S. Miller10e26722006-11-16 13:38:57 -0800156 andn %l1, %l4, %l1
157 srl %l4, 20, %l4
158#ifdef CONFIG_TRACE_IRQFLAGS
159 brnz,pn %l4, rtrap_no_irq_enable
160 nop
161 call trace_hardirqs_on
162 nop
163 wrpr %l4, %pil
164rtrap_no_irq_enable:
165#endif
166 andcc %l1, TSTATE_PRIV, %l3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 bne,pn %icc, to_kernel
David S. Miller10e26722006-11-16 13:38:57 -0800168 nop
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* We must hold IRQs off and atomically test schedule+signal
171 * state, then hold them off all the way back to userspace.
David S. Miller10e26722006-11-16 13:38:57 -0800172 * If we are returning to kernel, none of this matters. Note
173 * that we are disabling interrupts via PSTATE_IE, not using
174 * %pil.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
176 * If we do not do this, there is a window where we would do
177 * the tests, later the signal/resched event arrives but we do
178 * not process it since we are still in kernel mode. It would
179 * take until the next local IRQ before the signal/resched
180 * event would be handled.
181 *
182 * This also means that if we have to deal with performance
183 * counters or user windows, we have to redo all of these
184 * sched+signal checks with IRQs disabled.
185 */
186to_user: wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
187 wrpr 0, %pil
188__handle_preemption_continue:
189 ldx [%g6 + TI_FLAGS], %l0
190 sethi %hi(_TIF_USER_WORK_MASK), %o0
191 or %o0, %lo(_TIF_USER_WORK_MASK), %o0
192 andcc %l0, %o0, %g0
193 sethi %hi(TSTATE_PEF), %o0
194 be,pt %xcc, user_nowork
195 andcc %l1, %o0, %g0
196 andcc %l0, _TIF_NEED_RESCHED, %g0
197 bne,pn %xcc, __handle_preemption
Roland McGrathe35a8922008-04-20 15:06:49 -0700198 andcc %l0, _TIF_DO_NOTIFY_RESUME_MASK, %g0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 bne,pn %xcc, __handle_signal
200__handle_signal_continue:
201 ldub [%g6 + TI_WSAVED], %o2
202 brnz,pn %o2, __handle_user_windows
203 nop
204__handle_user_windows_continue:
205 ldx [%g6 + TI_FLAGS], %l5
206 andcc %l5, _TIF_PERFCTR, %g0
207 sethi %hi(TSTATE_PEF), %o0
208 bne,pn %xcc, __handle_perfctrs
209__handle_perfctrs_continue:
210 andcc %l1, %o0, %g0
211
212 /* This fpdepth clear is necessary for non-syscall rtraps only */
213user_nowork:
214 bne,pn %xcc, __handle_userfpu
215 stb %g0, [%g6 + TI_FPDEPTH]
216__handle_userfpu_continue:
217
218rt_continue: ldx [%sp + PTREGS_OFF + PT_V9_G1], %g1
219 ldx [%sp + PTREGS_OFF + PT_V9_G2], %g2
220
221 ldx [%sp + PTREGS_OFF + PT_V9_G3], %g3
222 ldx [%sp + PTREGS_OFF + PT_V9_G4], %g4
223 ldx [%sp + PTREGS_OFF + PT_V9_G5], %g5
David S. Miller56fb4df2006-02-26 23:24:22 -0800224 brz,pt %l3, 1f
David S. Miller314981a2006-02-05 21:59:03 -0800225 mov %g6, %l2
226
David S. Miller56fb4df2006-02-26 23:24:22 -0800227 /* Must do this before thread reg is clobbered below. */
David S. Millerffe483d2006-02-02 21:55:10 -0800228 LOAD_PER_CPU_BASE(%g5, %g6, %i0, %i1, %i2)
David S. Miller74bf4312006-01-31 18:29:18 -08002291:
230 ldx [%sp + PTREGS_OFF + PT_V9_G6], %g6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ldx [%sp + PTREGS_OFF + PT_V9_G7], %g7
David S. Miller936f4822006-02-05 21:29:28 -0800232
233 /* Normal globals are restored, go to trap globals. */
234661: wrpr %g0, RTRAP_PSTATE_AG_IRQOFF, %pstate
David S. Milleraf02bec2006-02-16 16:23:45 -0800235 nop
236 .section .sun4v_2insn_patch, "ax"
David S. Miller936f4822006-02-05 21:29:28 -0800237 .word 661b
David S. Milleraf02bec2006-02-16 16:23:45 -0800238 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
David S. Miller936f4822006-02-05 21:29:28 -0800239 SET_GL(1)
240 .previous
241
David S. Miller314981a2006-02-05 21:59:03 -0800242 mov %l2, %g6
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 ldx [%sp + PTREGS_OFF + PT_V9_I0], %i0
245 ldx [%sp + PTREGS_OFF + PT_V9_I1], %i1
246
247 ldx [%sp + PTREGS_OFF + PT_V9_I2], %i2
248 ldx [%sp + PTREGS_OFF + PT_V9_I3], %i3
249 ldx [%sp + PTREGS_OFF + PT_V9_I4], %i4
250 ldx [%sp + PTREGS_OFF + PT_V9_I5], %i5
251 ldx [%sp + PTREGS_OFF + PT_V9_I6], %i6
252 ldx [%sp + PTREGS_OFF + PT_V9_I7], %i7
253 ldx [%sp + PTREGS_OFF + PT_V9_TPC], %l2
254 ldx [%sp + PTREGS_OFF + PT_V9_TNPC], %o2
255
256 ld [%sp + PTREGS_OFF + PT_V9_Y], %o3
257 wr %o3, %g0, %y
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 wrpr %l4, 0x0, %pil
259 wrpr %g0, 0x1, %tl
David S. Miller28e61032008-05-11 02:07:19 -0700260 andn %l1, TSTATE_SYSCALL, %l1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 wrpr %l1, %g0, %tstate
262 wrpr %l2, %g0, %tpc
263 wrpr %o2, %g0, %tnpc
264
265 brnz,pn %l3, kern_rtt
266 mov PRIMARY_CONTEXT, %l7
David S. Miller8b11bd12006-02-07 22:13:05 -0800267
268661: ldxa [%l7 + %l7] ASI_DMMU, %l0
269 .section .sun4v_1insn_patch, "ax"
270 .word 661b
271 ldxa [%l7 + %l7] ASI_MMU, %l0
272 .previous
273
David S. Miller0835ae02005-10-04 15:23:20 -0700274 sethi %hi(sparc64_kern_pri_nuc_bits), %l1
275 ldx [%l1 + %lo(sparc64_kern_pri_nuc_bits)], %l1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 or %l0, %l1, %l0
David S. Miller8b11bd12006-02-07 22:13:05 -0800277
278661: stxa %l0, [%l7] ASI_DMMU
279 .section .sun4v_1insn_patch, "ax"
280 .word 661b
281 stxa %l0, [%l7] ASI_MMU
282 .previous
283
David S. Miller4da808c2006-01-31 18:33:00 -0800284 sethi %hi(KERNBASE), %l7
285 flush %l7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 rdpr %wstate, %l1
287 rdpr %otherwin, %l2
288 srl %l1, 3, %l1
289
290 wrpr %l2, %g0, %canrestore
291 wrpr %l1, %g0, %wstate
David S. Miller314ef682006-02-04 00:10:01 -0800292 brnz,pt %l2, user_rtt_restore
293 wrpr %g0, %g0, %otherwin
294
295 ldx [%g6 + TI_FLAGS], %g3
296 wr %g0, ASI_AIUP, %asi
297 rdpr %cwp, %g1
298 andcc %g3, _TIF_32BIT, %g0
299 sub %g1, 1, %g1
300 bne,pt %xcc, user_rtt_fill_32bit
301 wrpr %g1, %cwp
302 ba,a,pt %xcc, user_rtt_fill_64bit
303
304user_rtt_fill_fixup:
305 rdpr %cwp, %g1
306 add %g1, 1, %g1
307 wrpr %g1, 0x0, %cwp
308
309 rdpr %wstate, %g2
310 sll %g2, 3, %g2
311 wrpr %g2, 0x0, %wstate
312
313 /* We know %canrestore and %otherwin are both zero. */
314
315 sethi %hi(sparc64_kern_pri_context), %g2
316 ldx [%g2 + %lo(sparc64_kern_pri_context)], %g2
317 mov PRIMARY_CONTEXT, %g1
David S. Miller8b11bd12006-02-07 22:13:05 -0800318
319661: stxa %g2, [%g1] ASI_DMMU
320 .section .sun4v_1insn_patch, "ax"
321 .word 661b
322 stxa %g2, [%g1] ASI_MMU
323 .previous
324
David S. Miller314ef682006-02-04 00:10:01 -0800325 sethi %hi(KERNBASE), %g1
326 flush %g1
327
328 or %g4, FAULT_CODE_WINFIXUP, %g4
329 stb %g4, [%g6 + TI_FAULT_CODE]
330 stx %g5, [%g6 + TI_FAULT_ADDR]
331
332 mov %g6, %l1
333 wrpr %g0, 0x0, %tl
David S. Miller936f4822006-02-05 21:29:28 -0800334
335661: nop
David S. Millerdf7d6ae2006-02-07 00:00:16 -0800336 .section .sun4v_1insn_patch, "ax"
David S. Miller936f4822006-02-05 21:29:28 -0800337 .word 661b
338 SET_GL(0)
339 .previous
340
David S. Millerfc504922006-02-22 16:15:45 -0800341 wrpr %g0, RTRAP_PSTATE, %pstate
342
David S. Miller314ef682006-02-04 00:10:01 -0800343 mov %l1, %g6
344 ldx [%g6 + TI_TASK], %g4
345 LOAD_PER_CPU_BASE(%g5, %g6, %g1, %g2, %g3)
346 call do_sparc64_fault
347 add %sp, PTREGS_OFF, %o0
348 ba,pt %xcc, rtrap
349 nop
350
351user_rtt_pre_restore:
352 add %g1, 1, %g1
353 wrpr %g1, 0x0, %cwp
354
355user_rtt_restore:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 restore
357 rdpr %canrestore, %g1
358 wrpr %g1, 0x0, %cleanwin
359 retry
360 nop
361
David S. Miller314ef682006-02-04 00:10:01 -0800362kern_rtt: rdpr %canrestore, %g1
363 brz,pn %g1, kern_rtt_fill
364 nop
365kern_rtt_restore:
David S. Millerada44a02008-05-21 21:50:01 -0700366 stw %g0, [%sp + PTREGS_OFF + PT_V9_MAGIC]
David S. Miller314ef682006-02-04 00:10:01 -0800367 restore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 retry
David S. Miller314ef682006-02-04 00:10:01 -0800369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370to_kernel:
371#ifdef CONFIG_PREEMPT
372 ldsw [%g6 + TI_PRE_COUNT], %l5
373 brnz %l5, kern_fpucheck
374 ldx [%g6 + TI_FLAGS], %l5
375 andcc %l5, _TIF_NEED_RESCHED, %g0
376 be,pt %xcc, kern_fpucheck
David S. Miller10e26722006-11-16 13:38:57 -0800377 nop
378 cmp %l4, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 bne,pn %xcc, kern_fpucheck
380 sethi %hi(PREEMPT_ACTIVE), %l6
381 stw %l6, [%g6 + TI_PRE_COUNT]
382 call schedule
383 nop
384 ba,pt %xcc, rtrap
385 stw %g0, [%g6 + TI_PRE_COUNT]
386#endif
387kern_fpucheck: ldub [%g6 + TI_FPDEPTH], %l5
388 brz,pt %l5, rt_continue
389 srl %l5, 1, %o0
390 add %g6, TI_FPSAVED, %l6
391 ldub [%l6 + %o0], %l2
392 sub %l5, 2, %l5
393
394 add %g6, TI_GSR, %o1
395 andcc %l2, (FPRS_FEF|FPRS_DU), %g0
396 be,pt %icc, 2f
397 and %l2, FPRS_DL, %l6
398 andcc %l2, FPRS_FEF, %g0
399 be,pn %icc, 5f
400 sll %o0, 3, %o5
401 rd %fprs, %g1
402
403 wr %g1, FPRS_FEF, %fprs
404 ldx [%o1 + %o5], %g1
405 add %g6, TI_XFSR, %o1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 sll %o0, 8, %o2
407 add %g6, TI_FPREGS, %o3
408 brz,pn %l6, 1f
409 add %g6, TI_FPREGS+0x40, %o4
410
David S. Millerba6399332005-10-07 13:30:49 -0700411 membar #Sync
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 ldda [%o3 + %o2] ASI_BLK_P, %f0
413 ldda [%o4 + %o2] ASI_BLK_P, %f16
David S. Millerba6399332005-10-07 13:30:49 -0700414 membar #Sync
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151: andcc %l2, FPRS_DU, %g0
416 be,pn %icc, 1f
417 wr %g1, 0, %gsr
418 add %o2, 0x80, %o2
David S. Millerba6399332005-10-07 13:30:49 -0700419 membar #Sync
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 ldda [%o3 + %o2] ASI_BLK_P, %f32
421 ldda [%o4 + %o2] ASI_BLK_P, %f48
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221: membar #Sync
423 ldx [%o1 + %o5], %fsr
4242: stb %l5, [%g6 + TI_FPDEPTH]
425 ba,pt %xcc, rt_continue
426 nop
4275: wr %g0, FPRS_FEF, %fprs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 sll %o0, 8, %o2
429
430 add %g6, TI_FPREGS+0x80, %o3
431 add %g6, TI_FPREGS+0xc0, %o4
David S. Millerba6399332005-10-07 13:30:49 -0700432 membar #Sync
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 ldda [%o3 + %o2] ASI_BLK_P, %f32
434 ldda [%o4 + %o2] ASI_BLK_P, %f48
435 membar #Sync
436 wr %g0, FPRS_DU, %fprs
437 ba,pt %xcc, rt_continue
438 stb %l5, [%g6 + TI_FPDEPTH]