blob: 3522cd66f3bb36e0e1250b4ac7cd9dadebcfb52d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: rtrap.S,v 1.61 2002/02/09 19:49:31 davem Exp $
2 * 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
17#define RTRAP_PSTATE (PSTATE_RMO|PSTATE_PEF|PSTATE_PRIV|PSTATE_IE)
18#define RTRAP_PSTATE_IRQOFF (PSTATE_RMO|PSTATE_PEF|PSTATE_PRIV)
19#define RTRAP_PSTATE_AG_IRQOFF (PSTATE_RMO|PSTATE_PEF|PSTATE_PRIV|PSTATE_AG)
20
21 /* Register %l6 keeps track of whether we are returning
22 * from a system call or not. It is cleared if we call
23 * do_notify_resume, and it must not be otherwise modified
24 * until we fully commit to returning to userspace.
25 */
26
27 .text
28 .align 32
29__handle_softirq:
30 call do_softirq
31 nop
32 ba,a,pt %xcc, __handle_softirq_continue
33 nop
34__handle_preemption:
35 call schedule
36 wrpr %g0, RTRAP_PSTATE, %pstate
37 ba,pt %xcc, __handle_preemption_continue
38 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
39
40__handle_user_windows:
41 call fault_in_user_windows
42 wrpr %g0, RTRAP_PSTATE, %pstate
43 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
44 /* Redo sched+sig checks */
45 ldx [%g6 + TI_FLAGS], %l0
46 andcc %l0, _TIF_NEED_RESCHED, %g0
47
48 be,pt %xcc, 1f
49 nop
50 call schedule
51 wrpr %g0, RTRAP_PSTATE, %pstate
52 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
53 ldx [%g6 + TI_FLAGS], %l0
54
David S. Miller2d7d5f02006-01-19 02:42:49 -0800551: andcc %l0, (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK), %g0
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 be,pt %xcc, __handle_user_windows_continue
57 nop
David S. Miller2d7d5f02006-01-19 02:42:49 -080058 mov %l5, %o1
59 mov %l6, %o2
60 add %sp, PTREGS_OFF, %o0
61 mov %l0, %o3
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 call do_notify_resume
64 wrpr %g0, RTRAP_PSTATE, %pstate
65 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
66 clr %l6
67 /* Signal delivery can modify pt_regs tstate, so we must
68 * reload it.
69 */
70 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
71 sethi %hi(0xf << 20), %l4
72 and %l1, %l4, %l4
73 ba,pt %xcc, __handle_user_windows_continue
74
75 andn %l1, %l4, %l1
76__handle_perfctrs:
77 call update_perfctrs
78 wrpr %g0, RTRAP_PSTATE, %pstate
79 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
80 ldub [%g6 + TI_WSAVED], %o2
81 brz,pt %o2, 1f
82 nop
83 /* Redo userwin+sched+sig checks */
84 call fault_in_user_windows
85
86 wrpr %g0, RTRAP_PSTATE, %pstate
87 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
88 ldx [%g6 + TI_FLAGS], %l0
89 andcc %l0, _TIF_NEED_RESCHED, %g0
90 be,pt %xcc, 1f
91
92 nop
93 call schedule
94 wrpr %g0, RTRAP_PSTATE, %pstate
95 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
96 ldx [%g6 + TI_FLAGS], %l0
David S. Miller2d7d5f02006-01-19 02:42:49 -0800971: andcc %l0, (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK), %g0
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 be,pt %xcc, __handle_perfctrs_continue
100 sethi %hi(TSTATE_PEF), %o0
David S. Miller2d7d5f02006-01-19 02:42:49 -0800101 mov %l5, %o1
102 mov %l6, %o2
103 add %sp, PTREGS_OFF, %o0
104 mov %l0, %o3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 call do_notify_resume
106
107 wrpr %g0, RTRAP_PSTATE, %pstate
108 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
109 clr %l6
110 /* Signal delivery can modify pt_regs tstate, so we must
111 * reload it.
112 */
113 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
114 sethi %hi(0xf << 20), %l4
115 and %l1, %l4, %l4
116 andn %l1, %l4, %l1
117 ba,pt %xcc, __handle_perfctrs_continue
118
119 sethi %hi(TSTATE_PEF), %o0
120__handle_userfpu:
121 rd %fprs, %l5
122 andcc %l5, FPRS_FEF, %g0
123 sethi %hi(TSTATE_PEF), %o0
124 be,a,pn %icc, __handle_userfpu_continue
125 andn %l1, %o0, %l1
126 ba,a,pt %xcc, __handle_userfpu_continue
127
128__handle_signal:
David S. Miller2d7d5f02006-01-19 02:42:49 -0800129 mov %l5, %o1
130 mov %l6, %o2
131 add %sp, PTREGS_OFF, %o0
132 mov %l0, %o3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 call do_notify_resume
134 wrpr %g0, RTRAP_PSTATE, %pstate
135 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
136 clr %l6
137
138 /* Signal delivery can modify pt_regs tstate, so we must
139 * reload it.
140 */
141 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
142 sethi %hi(0xf << 20), %l4
143 and %l1, %l4, %l4
144 ba,pt %xcc, __handle_signal_continue
145 andn %l1, %l4, %l1
146
147 .align 64
148 .globl rtrap_irq, rtrap_clr_l6, rtrap, irqsz_patchme, rtrap_xcall
149rtrap_irq:
150rtrap_clr_l6: clr %l6
151rtrap:
David S. Millerd7ce78f2005-08-29 22:46:43 -0700152#ifndef CONFIG_SMP
153 sethi %hi(per_cpu____cpu_data), %l0
154 lduw [%l0 + %lo(per_cpu____cpu_data)], %l1
155#else
156 sethi %hi(per_cpu____cpu_data), %l0
157 or %l0, %lo(per_cpu____cpu_data), %l0
158 lduw [%l0 + %g5], %l1
159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 cmp %l1, 0
161
162 /* mm/ultra.S:xcall_report_regs KNOWS about this load. */
163 bne,pn %icc, __handle_softirq
164 ldx [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
165__handle_softirq_continue:
166rtrap_xcall:
167 sethi %hi(0xf << 20), %l4
168 andcc %l1, TSTATE_PRIV, %l3
169 and %l1, %l4, %l4
170 bne,pn %icc, to_kernel
171 andn %l1, %l4, %l1
172
173 /* We must hold IRQs off and atomically test schedule+signal
174 * state, then hold them off all the way back to userspace.
175 * If we are returning to kernel, none of this matters.
176 *
177 * If we do not do this, there is a window where we would do
178 * the tests, later the signal/resched event arrives but we do
179 * not process it since we are still in kernel mode. It would
180 * take until the next local IRQ before the signal/resched
181 * event would be handled.
182 *
183 * This also means that if we have to deal with performance
184 * counters or user windows, we have to redo all of these
185 * sched+signal checks with IRQs disabled.
186 */
187to_user: wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
188 wrpr 0, %pil
189__handle_preemption_continue:
190 ldx [%g6 + TI_FLAGS], %l0
191 sethi %hi(_TIF_USER_WORK_MASK), %o0
192 or %o0, %lo(_TIF_USER_WORK_MASK), %o0
193 andcc %l0, %o0, %g0
194 sethi %hi(TSTATE_PEF), %o0
195 be,pt %xcc, user_nowork
196 andcc %l1, %o0, %g0
197 andcc %l0, _TIF_NEED_RESCHED, %g0
198 bne,pn %xcc, __handle_preemption
David S. Miller2d7d5f02006-01-19 02:42:49 -0800199 andcc %l0, (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK), %g0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 bne,pn %xcc, __handle_signal
201__handle_signal_continue:
202 ldub [%g6 + TI_WSAVED], %o2
203 brnz,pn %o2, __handle_user_windows
204 nop
205__handle_user_windows_continue:
206 ldx [%g6 + TI_FLAGS], %l5
207 andcc %l5, _TIF_PERFCTR, %g0
208 sethi %hi(TSTATE_PEF), %o0
209 bne,pn %xcc, __handle_perfctrs
210__handle_perfctrs_continue:
211 andcc %l1, %o0, %g0
212
213 /* This fpdepth clear is necessary for non-syscall rtraps only */
214user_nowork:
215 bne,pn %xcc, __handle_userfpu
216 stb %g0, [%g6 + TI_FPDEPTH]
217__handle_userfpu_continue:
218
219rt_continue: ldx [%sp + PTREGS_OFF + PT_V9_G1], %g1
220 ldx [%sp + PTREGS_OFF + PT_V9_G2], %g2
221
222 ldx [%sp + PTREGS_OFF + PT_V9_G3], %g3
223 ldx [%sp + PTREGS_OFF + PT_V9_G4], %g4
224 ldx [%sp + PTREGS_OFF + PT_V9_G5], %g5
David S. Miller56fb4df2006-02-26 23:24:22 -0800225 brz,pt %l3, 1f
David S. Miller314981a2006-02-05 21:59:03 -0800226 mov %g6, %l2
227
David S. Miller56fb4df2006-02-26 23:24:22 -0800228 /* Must do this before thread reg is clobbered below. */
David S. Millerffe483d2006-02-02 21:55:10 -0800229 LOAD_PER_CPU_BASE(%g5, %g6, %i0, %i1, %i2)
David S. Miller74bf4312006-01-31 18:29:18 -08002301:
231 ldx [%sp + PTREGS_OFF + PT_V9_G6], %g6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 ldx [%sp + PTREGS_OFF + PT_V9_G7], %g7
David S. Miller936f4822006-02-05 21:29:28 -0800233
234 /* Normal globals are restored, go to trap globals. */
235661: wrpr %g0, RTRAP_PSTATE_AG_IRQOFF, %pstate
David S. Milleraf02bec2006-02-16 16:23:45 -0800236 nop
237 .section .sun4v_2insn_patch, "ax"
David S. Miller936f4822006-02-05 21:29:28 -0800238 .word 661b
David S. Milleraf02bec2006-02-16 16:23:45 -0800239 wrpr %g0, RTRAP_PSTATE_IRQOFF, %pstate
David S. Miller936f4822006-02-05 21:29:28 -0800240 SET_GL(1)
241 .previous
242
David S. Miller314981a2006-02-05 21:59:03 -0800243 mov %l2, %g6
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 ldx [%sp + PTREGS_OFF + PT_V9_I0], %i0
246 ldx [%sp + PTREGS_OFF + PT_V9_I1], %i1
247
248 ldx [%sp + PTREGS_OFF + PT_V9_I2], %i2
249 ldx [%sp + PTREGS_OFF + PT_V9_I3], %i3
250 ldx [%sp + PTREGS_OFF + PT_V9_I4], %i4
251 ldx [%sp + PTREGS_OFF + PT_V9_I5], %i5
252 ldx [%sp + PTREGS_OFF + PT_V9_I6], %i6
253 ldx [%sp + PTREGS_OFF + PT_V9_I7], %i7
254 ldx [%sp + PTREGS_OFF + PT_V9_TPC], %l2
255 ldx [%sp + PTREGS_OFF + PT_V9_TNPC], %o2
256
257 ld [%sp + PTREGS_OFF + PT_V9_Y], %o3
258 wr %o3, %g0, %y
259 srl %l4, 20, %l4
260 wrpr %l4, 0x0, %pil
261 wrpr %g0, 0x1, %tl
262 wrpr %l1, %g0, %tstate
263 wrpr %l2, %g0, %tpc
264 wrpr %o2, %g0, %tnpc
265
266 brnz,pn %l3, kern_rtt
267 mov PRIMARY_CONTEXT, %l7
David S. Miller8b11bd12006-02-07 22:13:05 -0800268
269661: ldxa [%l7 + %l7] ASI_DMMU, %l0
270 .section .sun4v_1insn_patch, "ax"
271 .word 661b
272 ldxa [%l7 + %l7] ASI_MMU, %l0
273 .previous
274
David S. Miller0835ae02005-10-04 15:23:20 -0700275 sethi %hi(sparc64_kern_pri_nuc_bits), %l1
276 ldx [%l1 + %lo(sparc64_kern_pri_nuc_bits)], %l1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 or %l0, %l1, %l0
David S. Miller8b11bd12006-02-07 22:13:05 -0800278
279661: stxa %l0, [%l7] ASI_DMMU
280 .section .sun4v_1insn_patch, "ax"
281 .word 661b
282 stxa %l0, [%l7] ASI_MMU
283 .previous
284
David S. Miller4da808c2006-01-31 18:33:00 -0800285 sethi %hi(KERNBASE), %l7
286 flush %l7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 rdpr %wstate, %l1
288 rdpr %otherwin, %l2
289 srl %l1, 3, %l1
290
291 wrpr %l2, %g0, %canrestore
292 wrpr %l1, %g0, %wstate
David S. Miller314ef682006-02-04 00:10:01 -0800293 brnz,pt %l2, user_rtt_restore
294 wrpr %g0, %g0, %otherwin
295
296 ldx [%g6 + TI_FLAGS], %g3
297 wr %g0, ASI_AIUP, %asi
298 rdpr %cwp, %g1
299 andcc %g3, _TIF_32BIT, %g0
300 sub %g1, 1, %g1
301 bne,pt %xcc, user_rtt_fill_32bit
302 wrpr %g1, %cwp
303 ba,a,pt %xcc, user_rtt_fill_64bit
304
305user_rtt_fill_fixup:
306 rdpr %cwp, %g1
307 add %g1, 1, %g1
308 wrpr %g1, 0x0, %cwp
309
310 rdpr %wstate, %g2
311 sll %g2, 3, %g2
312 wrpr %g2, 0x0, %wstate
313
314 /* We know %canrestore and %otherwin are both zero. */
315
316 sethi %hi(sparc64_kern_pri_context), %g2
317 ldx [%g2 + %lo(sparc64_kern_pri_context)], %g2
318 mov PRIMARY_CONTEXT, %g1
David S. Miller8b11bd12006-02-07 22:13:05 -0800319
320661: stxa %g2, [%g1] ASI_DMMU
321 .section .sun4v_1insn_patch, "ax"
322 .word 661b
323 stxa %g2, [%g1] ASI_MMU
324 .previous
325
David S. Miller314ef682006-02-04 00:10:01 -0800326 sethi %hi(KERNBASE), %g1
327 flush %g1
328
329 or %g4, FAULT_CODE_WINFIXUP, %g4
330 stb %g4, [%g6 + TI_FAULT_CODE]
331 stx %g5, [%g6 + TI_FAULT_ADDR]
332
333 mov %g6, %l1
334 wrpr %g0, 0x0, %tl
David S. Miller936f4822006-02-05 21:29:28 -0800335
336661: nop
David S. Millerdf7d6ae2006-02-07 00:00:16 -0800337 .section .sun4v_1insn_patch, "ax"
David S. Miller936f4822006-02-05 21:29:28 -0800338 .word 661b
339 SET_GL(0)
340 .previous
341
David S. Millerfc504922006-02-22 16:15:45 -0800342 wrpr %g0, RTRAP_PSTATE, %pstate
343
David S. Miller314ef682006-02-04 00:10:01 -0800344 mov %l1, %g6
345 ldx [%g6 + TI_TASK], %g4
346 LOAD_PER_CPU_BASE(%g5, %g6, %g1, %g2, %g3)
347 call do_sparc64_fault
348 add %sp, PTREGS_OFF, %o0
349 ba,pt %xcc, rtrap
350 nop
351
352user_rtt_pre_restore:
353 add %g1, 1, %g1
354 wrpr %g1, 0x0, %cwp
355
356user_rtt_restore:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 restore
358 rdpr %canrestore, %g1
359 wrpr %g1, 0x0, %cleanwin
360 retry
361 nop
362
David S. Miller314ef682006-02-04 00:10:01 -0800363kern_rtt: rdpr %canrestore, %g1
364 brz,pn %g1, kern_rtt_fill
365 nop
366kern_rtt_restore:
367 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
377 srl %l4, 20, %l5
378 cmp %l5, 0
379 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]