ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sparc64 / kernel / rtrap.S
1 /* $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
8 #include <linux/config.h>
9
10 #include <asm/asi.h>
11 #include <asm/pstate.h>
12 #include <asm/ptrace.h>
13 #include <asm/spitfire.h>
14 #include <asm/head.h>
15 #include <asm/visasm.h>
16 #include <asm/processor.h>
17
18 #define         RTRAP_PSTATE            (PSTATE_RMO|PSTATE_PEF|PSTATE_PRIV|PSTATE_IE)
19 #define         RTRAP_PSTATE_IRQOFF     (PSTATE_RMO|PSTATE_PEF|PSTATE_PRIV)
20 #define         RTRAP_PSTATE_AG_IRQOFF  (PSTATE_RMO|PSTATE_PEF|PSTATE_PRIV|PSTATE_AG)
21
22                 /* Register %l6 keeps track of whether we are returning
23                  * from a system call or not.  It is cleared if we call
24                  * do_notify_resume, and it must not be otherwise modified
25                  * until we fully commit to returning to userspace.
26                  */
27
28                 .text
29                 .align                  32
30 __handle_softirq:
31                 call                    do_softirq
32                  nop
33                 ba,a,pt                 %xcc, __handle_softirq_continue
34                  nop
35 __handle_preemption:
36                 call                    schedule
37                  wrpr                   %g0, RTRAP_PSTATE, %pstate
38                 ba,pt                   %xcc, __handle_preemption_continue
39                  wrpr                   %g0, RTRAP_PSTATE_IRQOFF, %pstate
40
41 __handle_user_windows:
42                 call                    fault_in_user_windows
43                  wrpr                   %g0, RTRAP_PSTATE, %pstate
44                 wrpr                    %g0, RTRAP_PSTATE_IRQOFF, %pstate
45                 /* Redo sched+sig checks */
46                 ldx                     [%g6 + TI_FLAGS], %l0
47                 andcc                   %l0, _TIF_NEED_RESCHED, %g0
48
49                 be,pt                   %xcc, 1f
50                  nop
51                 call                    schedule
52                  wrpr                   %g0, RTRAP_PSTATE, %pstate
53                 wrpr                    %g0, RTRAP_PSTATE_IRQOFF, %pstate
54                 ldx                     [%g6 + TI_FLAGS], %l0
55
56 1:              andcc                   %l0, (_TIF_NOTIFY_RESUME | _TIF_SIGPENDING), %g0
57                 be,pt                   %xcc, __handle_user_windows_continue
58                  nop
59                 clr                     %o0
60                 mov                     %l5, %o2
61                 mov                     %l6, %o3
62                 add                     %sp, PTREGS_OFF, %o1
63                 mov                     %l0, %o4
64
65                 call                    do_notify_resume
66                  wrpr                   %g0, RTRAP_PSTATE, %pstate
67                 wrpr                    %g0, RTRAP_PSTATE_IRQOFF, %pstate
68                 clr                     %l6
69                 /* Signal delivery can modify pt_regs tstate, so we must
70                  * reload it.
71                  */
72                 ldx                     [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
73                 sethi                   %hi(0xf << 20), %l4
74                 and                     %l1, %l4, %l4
75                 ba,pt                   %xcc, __handle_user_windows_continue
76
77                  andn                   %l1, %l4, %l1
78 __handle_perfctrs:
79                 call                    update_perfctrs
80                  wrpr                   %g0, RTRAP_PSTATE, %pstate
81                 wrpr                    %g0, RTRAP_PSTATE_IRQOFF, %pstate
82                 ldub                    [%g6 + TI_WSAVED], %o2
83                 brz,pt                  %o2, 1f
84                  nop
85                 /* Redo userwin+sched+sig checks */
86                 call                    fault_in_user_windows
87
88                  wrpr                   %g0, RTRAP_PSTATE, %pstate
89                 wrpr                    %g0, RTRAP_PSTATE_IRQOFF, %pstate
90                 ldx                     [%g6 + TI_FLAGS], %l0
91                 andcc                   %l0, _TIF_NEED_RESCHED, %g0
92                 be,pt                   %xcc, 1f
93
94                  nop
95                 call                    schedule
96                  wrpr                   %g0, RTRAP_PSTATE, %pstate
97                 wrpr                    %g0, RTRAP_PSTATE_IRQOFF, %pstate
98                 ldx                     [%g6 + TI_FLAGS], %l0
99 1:              andcc                   %l0, (_TIF_NOTIFY_RESUME | _TIF_SIGPENDING), %g0
100
101                 be,pt                   %xcc, __handle_perfctrs_continue
102                  sethi                  %hi(TSTATE_PEF), %o0
103                 clr                     %o0
104                 mov                     %l5, %o2
105                 mov                     %l6, %o3
106                 add                     %sp, PTREGS_OFF, %o1
107                 mov                     %l0, %o4
108                 call                    do_notify_resume
109
110                  wrpr                   %g0, RTRAP_PSTATE, %pstate
111                 wrpr                    %g0, RTRAP_PSTATE_IRQOFF, %pstate
112                 clr                     %l6
113                 /* Signal delivery can modify pt_regs tstate, so we must
114                  * reload it.
115                  */
116                 ldx                     [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
117                 sethi                   %hi(0xf << 20), %l4
118                 and                     %l1, %l4, %l4
119                 andn                    %l1, %l4, %l1
120                 ba,pt                   %xcc, __handle_perfctrs_continue
121
122                  sethi                  %hi(TSTATE_PEF), %o0
123 __handle_userfpu:
124                 rd                      %fprs, %l5
125                 andcc                   %l5, FPRS_FEF, %g0
126                 sethi                   %hi(TSTATE_PEF), %o0
127                 be,a,pn                 %icc, __handle_userfpu_continue
128                  andn                   %l1, %o0, %l1
129                 ba,a,pt                 %xcc, __handle_userfpu_continue
130
131 __handle_signal:
132                 clr                     %o0
133                 mov                     %l5, %o2
134                 mov                     %l6, %o3
135                 add                     %sp, PTREGS_OFF, %o1
136                 mov                     %l0, %o4
137                 call                    do_notify_resume
138                  wrpr                   %g0, RTRAP_PSTATE, %pstate
139                 wrpr                    %g0, RTRAP_PSTATE_IRQOFF, %pstate
140                 clr                     %l6
141
142                 /* Signal delivery can modify pt_regs tstate, so we must
143                  * reload it.
144                  */
145                 ldx                     [%sp + PTREGS_OFF + PT_V9_TSTATE], %l1
146                 sethi                   %hi(0xf << 20), %l4
147                 and                     %l1, %l4, %l4
148                 ba,pt                   %xcc, __handle_signal_continue
149                  andn                   %l1, %l4, %l1
150
151                 .align                  64
152                 .globl                  rtrap_irq, rtrap_clr_l6, rtrap, irqsz_patchme, rtrap_xcall
153 rtrap_irq:
154 rtrap_clr_l6:   clr                     %l6
155 rtrap:          ldub                    [%g6 + TI_CPU], %l0
156                 sethi                   %hi(irq_stat), %l2      ! &softirq_active
157                 or                      %l2, %lo(irq_stat), %l2 ! &softirq_active
158 irqsz_patchme:  sllx                    %l0, 0, %l0
159                 lduw                    [%l2 + %l0], %l1        ! softirq_pending
160                 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:
166 rtrap_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                  */
187 to_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
199                  andcc                  %l0, (_TIF_NOTIFY_RESUME | _TIF_SIGPENDING), %g0
200                 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 */
214 user_nowork:
215                 bne,pn                  %xcc, __handle_userfpu
216                  stb                    %g0, [%g6 + TI_FPDEPTH]
217 __handle_userfpu_continue:
218
219 rt_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
225                 ldx                     [%sp + PTREGS_OFF + PT_V9_G6], %g6
226                 ldx                     [%sp + PTREGS_OFF + PT_V9_G7], %g7
227                 wrpr                    %g0, RTRAP_PSTATE_AG_IRQOFF, %pstate
228                 ldx                     [%sp + PTREGS_OFF + PT_V9_I0], %i0
229                 ldx                     [%sp + PTREGS_OFF + PT_V9_I1], %i1
230
231                 ldx                     [%sp + PTREGS_OFF + PT_V9_I2], %i2
232                 ldx                     [%sp + PTREGS_OFF + PT_V9_I3], %i3
233                 ldx                     [%sp + PTREGS_OFF + PT_V9_I4], %i4
234                 ldx                     [%sp + PTREGS_OFF + PT_V9_I5], %i5
235                 ldx                     [%sp + PTREGS_OFF + PT_V9_I6], %i6
236                 ldx                     [%sp + PTREGS_OFF + PT_V9_I7], %i7
237                 ldx                     [%sp + PTREGS_OFF + PT_V9_TPC], %l2
238                 ldx                     [%sp + PTREGS_OFF + PT_V9_TNPC], %o2
239
240                 ld                      [%sp + PTREGS_OFF + PT_V9_Y], %o3
241                 wr                      %o3, %g0, %y
242                 srl                     %l4, 20, %l4
243                 wrpr                    %l4, 0x0, %pil
244                 wrpr                    %g0, 0x1, %tl
245                 wrpr                    %l1, %g0, %tstate
246                 wrpr                    %l2, %g0, %tpc
247                 wrpr                    %o2, %g0, %tnpc
248
249                 brnz,pn                 %l3, kern_rtt
250                  mov                    PRIMARY_CONTEXT, %l7
251                 ldxa                    [%l7 + %l7] ASI_DMMU, %l0
252                 stxa                    %l0, [%l7] ASI_DMMU
253                 flush                   %g6
254                 rdpr                    %wstate, %l1
255                 rdpr                    %otherwin, %l2
256                 srl                     %l1, 3, %l1
257
258                 wrpr                    %l2, %g0, %canrestore
259                 wrpr                    %l1, %g0, %wstate
260                 wrpr                    %g0, %g0, %otherwin
261                 restore
262                 rdpr                    %canrestore, %g1
263                 wrpr                    %g1, 0x0, %cleanwin
264                 retry
265                 nop
266
267 kern_rtt:       restore
268                 retry
269 to_kernel:
270 #ifdef CONFIG_PREEMPT
271                 ldsw                    [%g6 + TI_PRE_COUNT], %l5
272                 brnz                    %l5, kern_fpucheck
273                  ldx                    [%g6 + TI_FLAGS], %l5
274                 andcc                   %l5, _TIF_NEED_RESCHED, %g0
275                 be,pt                   %xcc, kern_fpucheck
276                  srl                    %l4, 20, %l5
277                 cmp                     %l5, 0
278                 bne,pn                  %xcc, kern_fpucheck
279                  sethi                  %hi(PREEMPT_ACTIVE), %l6
280                 stw                     %l6, [%g6 + TI_PRE_COUNT]
281                 call                    schedule
282                  nop
283                 ba,pt                   %xcc, rtrap
284                  stw                    %g0, [%g6 + TI_PRE_COUNT]
285 #endif
286 kern_fpucheck:  ldub                    [%g6 + TI_FPDEPTH], %l5
287                 brz,pt                  %l5, rt_continue
288                  srl                    %l5, 1, %o0
289                 add                     %g6, TI_FPSAVED, %l6
290                 ldub                    [%l6 + %o0], %l2
291                 sub                     %l5, 2, %l5
292
293                 add                     %g6, TI_GSR, %o1
294                 andcc                   %l2, (FPRS_FEF|FPRS_DU), %g0
295                 be,pt                   %icc, 2f
296                  and                    %l2, FPRS_DL, %l6
297                 andcc                   %l2, FPRS_FEF, %g0
298                 be,pn                   %icc, 5f
299                  sll                    %o0, 3, %o5
300                 rd                      %fprs, %g5
301
302                 wr                      %g5, FPRS_FEF, %fprs
303                 ldx                     [%o1 + %o5], %g5
304                 add                     %g6, TI_XFSR, %o1
305                 membar                  #StoreLoad | #LoadLoad
306                 sll                     %o0, 8, %o2
307                 add                     %g6, TI_FPREGS, %o3
308                 brz,pn                  %l6, 1f
309                  add                    %g6, TI_FPREGS+0x40, %o4
310
311                 ldda                    [%o3 + %o2] ASI_BLK_P, %f0
312                 ldda                    [%o4 + %o2] ASI_BLK_P, %f16
313 1:              andcc                   %l2, FPRS_DU, %g0
314                 be,pn                   %icc, 1f
315                  wr                     %g5, 0, %gsr
316                 add                     %o2, 0x80, %o2
317                 ldda                    [%o3 + %o2] ASI_BLK_P, %f32
318                 ldda                    [%o4 + %o2] ASI_BLK_P, %f48
319
320 1:              membar                  #Sync
321                 ldx                     [%o1 + %o5], %fsr
322 2:              stb                     %l5, [%g6 + TI_FPDEPTH]
323                 ba,pt                   %xcc, rt_continue
324                  nop
325 5:              wr                      %g0, FPRS_FEF, %fprs
326                 membar                  #StoreLoad | #LoadLoad
327                 sll                     %o0, 8, %o2
328
329                 add                     %g6, TI_FPREGS+0x80, %o3
330                 add                     %g6, TI_FPREGS+0xc0, %o4
331                 ldda                    [%o3 + %o2] ASI_BLK_P, %f32
332                 ldda                    [%o4 + %o2] ASI_BLK_P, %f48
333                 membar                  #Sync
334                 wr                      %g0, FPRS_DU, %fprs
335                 ba,pt                   %xcc, rt_continue
336                  stb                    %l5, [%g6 + TI_FPDEPTH]