• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * wof.S: Sparc window overflow handler.
3 *
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 */
6
7#include <asm/contregs.h>
8#include <asm/page.h>
9#include <asm/ptrace.h>
10#include <asm/psr.h>
11#include <asm/smp.h>
12#include <asm/asi.h>
13#include <asm/winmacro.h>
14#include <asm/asmmacro.h>
15#include <asm/thread_info.h>
16
17/* WARNING: This routine is hairy and _very_ complicated, but it
18 *          must be as fast as possible as it handles the allocation
19 *          of register windows to the user and kernel.  If you touch
20 *          this code be _very_ careful as many other pieces of the
21 *          kernel depend upon how this code behaves.  You have been
22 *          duly warned...
23 */
24
25/* We define macro's for registers which have a fixed
26 * meaning throughout this entire routine.  The 'T' in
27 * the comments mean that the register can only be
28 * accessed when in the 'trap' window, 'G' means
29 * accessible in any window.  Do not change these registers
30 * after they have been set, until you are ready to return
31 * from the trap.
32 */
33#define t_psr       l0 /* %psr at trap time                     T */
34#define t_pc        l1 /* PC for trap return                    T */
35#define t_npc       l2 /* NPC for trap return                   T */
36#define t_wim       l3 /* %wim at trap time                     T */
37#define saved_g5    l5 /* Global save register                  T */
38#define saved_g6    l6 /* Global save register                  T */
39#define curptr      g6 /* Gets set to 'current' then stays      G */
40
41/* Now registers whose values can change within the handler.      */
42#define twin_tmp    l4 /* Temp reg, only usable in trap window  T */
43#define glob_tmp    g5 /* Global temporary reg, usable anywhere G */
44
45	.text
46	.align	4
47	/* BEGINNING OF PATCH INSTRUCTIONS */
48	/* On a 7-window Sparc the boot code patches spnwin_*
49	 * instructions with the following ones.
50	 */
51	.globl	spnwin_patch1_7win, spnwin_patch2_7win, spnwin_patch3_7win
52spnwin_patch1_7win:	sll	%t_wim, 6, %glob_tmp
53spnwin_patch2_7win:	and	%glob_tmp, 0x7f, %glob_tmp
54spnwin_patch3_7win:	and	%twin_tmp, 0x7f, %twin_tmp
55	/* END OF PATCH INSTRUCTIONS */
56
57	/* The trap entry point has done the following:
58	 *
59	 * rd    %psr, %l0
60	 * rd    %wim, %l3
61	 * b     spill_window_entry
62	 * andcc %l0, PSR_PS, %g0
63	 */
64
65	/* Datum current_thread_info->uwinmask contains at all times a bitmask
66	 * where if any user windows are active, at least one bit will
67	 * be set in to mask.  If no user windows are active, the bitmask
68	 * will be all zeroes.
69	 */
70	.globl	spill_window_entry
71	.globl	spnwin_patch1, spnwin_patch2, spnwin_patch3
72spill_window_entry:
73	/* LOCATION: Trap Window */
74
75	mov	%g5, %saved_g5		! save away global temp register
76	mov	%g6, %saved_g6		! save away 'current' ptr register
77
78	/* Compute what the new %wim will be if we save the
79	 * window properly in this trap handler.
80	 *
81	 * newwim = ((%wim>>1) | (%wim<<(nwindows - 1)));
82	 */
83		srl	%t_wim, 0x1, %twin_tmp
84spnwin_patch1:	sll	%t_wim, 7, %glob_tmp
85		or	%glob_tmp, %twin_tmp, %glob_tmp
86spnwin_patch2:	and	%glob_tmp, 0xff, %glob_tmp
87
88	/* The trap entry point has set the condition codes
89	 * up for us to see if this is from user or kernel.
90	 * Get the load of 'curptr' out of the way.
91	 */
92	LOAD_CURRENT(curptr, twin_tmp)
93
94	andcc	%t_psr, PSR_PS, %g0
95	be,a	spwin_fromuser				! all user wins, branch
96	 save	%g0, %g0, %g0				! Go where saving will occur
97
98	/* See if any user windows are active in the set. */
99	ld	[%curptr + TI_UWINMASK], %twin_tmp	! grab win mask
100	orcc	%g0, %twin_tmp, %g0			! check for set bits
101	bne	spwin_exist_uwins			! yep, there are some
102	 andn	%twin_tmp, %glob_tmp, %twin_tmp		! compute new uwinmask
103
104	/* Save into the window which must be saved and do it.
105	 * Basically if we are here, this means that we trapped
106	 * from kernel mode with only kernel windows in the register
107	 * file.
108	 */
109	save	%g0, %g0, %g0		! save into the window to stash away
110	wr	%glob_tmp, 0x0, %wim	! set new %wim, this is safe now
111
112spwin_no_userwins_from_kernel:
113	/* LOCATION: Window to be saved */
114
115	STORE_WINDOW(sp)		! stash the window
116	restore	%g0, %g0, %g0		! go back into trap window
117
118	/* LOCATION: Trap window */
119	mov	%saved_g5, %g5		! restore %glob_tmp
120	mov	%saved_g6, %g6		! restore %curptr
121	wr	%t_psr, 0x0, %psr	! restore condition codes in %psr
122	WRITE_PAUSE			! waste some time
123	jmp	%t_pc			! Return from trap
124	rett	%t_npc			! we are done
125
126spwin_exist_uwins:
127	/* LOCATION: Trap window */
128
129	/* Wow, user windows have to be dealt with, this is dirty
130	 * and messy as all hell.  And difficult to follow if you
131	 * are approaching the infamous register window trap handling
132	 * problem for the first time. DON'T LOOK!
133	 *
134	 * Note that how the execution path works out, the new %wim
135	 * will be left for us in the global temporary register,
136	 * %glob_tmp.  We cannot set the new %wim first because we
137	 * need to save into the appropriate window without inducing
138	 * a trap (traps are off, we'd get a watchdog wheee)...
139	 * But first, store the new user window mask calculated
140	 * above.
141	 */
142	st	%twin_tmp, [%curptr + TI_UWINMASK]
143	save	%g0, %g0, %g0		! Go to where the saving will occur
144
145spwin_fromuser:
146	/* LOCATION: Window to be saved */
147	wr	%glob_tmp, 0x0, %wim	! Now it is safe to set new %wim
148
149	/* LOCATION: Window to be saved */
150
151	/* This instruction branches to a routine which will check
152	 * to validity of the users stack pointer by whatever means
153	 * are necessary.  This means that this is architecture
154	 * specific and thus this branch instruction will need to
155	 * be patched at boot time once the machine type is known.
156	 * This routine _shall not_ touch %curptr under any
157	 * circumstances whatsoever!  It will branch back to the
158	 * label 'spwin_good_ustack' if the stack is ok but still
159	 * needs to be dumped (SRMMU for instance will not need to
160	 * do this) or 'spwin_finish_up' if the stack is ok and the
161	 * registers have already been saved.  If the stack is found
162	 * to be bogus for some reason the routine shall branch to
163	 * the label 'spwin_user_stack_is_bolixed' which will take
164	 * care of things at that point.
165	 */
166	b	spwin_srmmu_stackchk
167	 andcc	%sp, 0x7, %g0
168
169spwin_good_ustack:
170	/* LOCATION: Window to be saved */
171
172	/* The users stack is ok and we can safely save it at
173	 * %sp.
174	 */
175	STORE_WINDOW(sp)
176
177spwin_finish_up:
178	restore	%g0, %g0, %g0		/* Back to trap window. */
179
180	/* LOCATION: Trap window */
181
182	/* We have spilled successfully, and we have properly stored
183	 * the appropriate window onto the stack.
184	 */
185
186	/* Restore saved globals */
187	mov	%saved_g5, %g5
188	mov	%saved_g6, %g6
189
190	wr	%t_psr, 0x0, %psr
191	WRITE_PAUSE
192	jmp	%t_pc
193	rett	%t_npc
194
195spwin_user_stack_is_bolixed:
196	/* LOCATION: Window to be saved */
197
198	/* Wheee, user has trashed his/her stack.  We have to decide
199	 * how to proceed based upon whether we came from kernel mode
200	 * or not.  If we came from kernel mode, toss the window into
201	 * a special buffer and proceed, the kernel _needs_ a window
202	 * and we could be in an interrupt handler so timing is crucial.
203	 * If we came from user land we build a full stack frame and call
204	 * c-code to gun down the process.
205	 */
206	rd	%psr, %glob_tmp
207	andcc	%glob_tmp, PSR_PS, %g0
208	bne	spwin_bad_ustack_from_kernel
209	 nop
210
211	/* Oh well, throw this one window into the per-task window
212	 * buffer, the first one.
213	 */
214	st	%sp, [%curptr + TI_RWIN_SPTRS]
215	STORE_WINDOW(curptr + TI_REG_WINDOW)
216	restore	%g0, %g0, %g0
217
218	/* LOCATION: Trap Window */
219
220	/* Back in the trap window, update winbuffer save count. */
221	mov	1, %twin_tmp
222	st	%twin_tmp, [%curptr + TI_W_SAVED]
223
224		/* Compute new user window mask.  What we are basically
225		 * doing is taking two windows, the invalid one at trap
226		 * time and the one we attempted to throw onto the users
227		 * stack, and saying that everything else is an ok user
228		 * window.  umask = ((~(%t_wim | %wim)) & valid_wim_bits)
229		 */
230		rd	%wim, %twin_tmp
231		or	%twin_tmp, %t_wim, %twin_tmp
232		not	%twin_tmp
233spnwin_patch3:	and	%twin_tmp, 0xff, %twin_tmp	! patched on 7win Sparcs
234		st	%twin_tmp, [%curptr + TI_UWINMASK]
235
236#define STACK_OFFSET (THREAD_SIZE - TRACEREG_SZ - STACKFRAME_SZ)
237
238	sethi	%hi(STACK_OFFSET), %sp
239	or	%sp, %lo(STACK_OFFSET), %sp
240	add	%curptr, %sp, %sp
241
242	/* Restore the saved globals and build a pt_regs frame. */
243	mov	%saved_g5, %g5
244	mov	%saved_g6, %g6
245	STORE_PT_ALL(sp, t_psr, t_pc, t_npc, g1)
246
247	sethi	%hi(STACK_OFFSET), %g6
248	or	%g6, %lo(STACK_OFFSET), %g6
249	sub	%sp, %g6, %g6		! curptr
250
251	/* Turn on traps and call c-code to deal with it. */
252	wr	%t_psr, PSR_ET, %psr
253	nop
254	call	window_overflow_fault
255	 nop
256
257	/* Return from trap if C-code actually fixes things, if it
258	 * doesn't then we never get this far as the process will
259	 * be given the look of death from Commander Peanut.
260	 */
261	b	ret_trap_entry
262	 clr	%l6
263
264spwin_bad_ustack_from_kernel:
265	/* LOCATION: Window to be saved */
266
267	/* The kernel provoked a spill window trap, but the window we
268	 * need to save is a user one and the process has trashed its
269	 * stack pointer.  We need to be quick, so we throw it into
270	 * a per-process window buffer until we can properly handle
271	 * this later on.
272	 */
273	SAVE_BOLIXED_USER_STACK(curptr, glob_tmp)
274	restore	%g0, %g0, %g0
275
276	/* LOCATION: Trap window */
277
278	/* Restore globals, condition codes in the %psr and
279	 * return from trap.  Note, restoring %g6 when returning
280	 * to kernel mode is not necessarily these days. ;-)
281	 */
282	mov	%saved_g5, %g5
283	mov	%saved_g6, %g6
284
285	wr	%t_psr, 0x0, %psr
286	WRITE_PAUSE
287
288	jmp	%t_pc
289	rett	%t_npc
290
291/* Undefine the register macros which would only cause trouble
292 * if used below.  This helps find 'stupid' coding errors that
293 * produce 'odd' behavior.  The routines below are allowed to
294 * make usage of glob_tmp and t_psr so we leave them defined.
295 */
296#undef twin_tmp
297#undef curptr
298#undef t_pc
299#undef t_npc
300#undef t_wim
301#undef saved_g5
302#undef saved_g6
303
304/* Now come the per-architecture window overflow stack checking routines.
305 * As noted above %curptr cannot be touched by this routine at all.
306 */
307
308	/* This is a generic SRMMU routine.  As far as I know this
309	 * works for all current v8/srmmu implementations, we'll
310	 * see...
311	 */
312	.globl	spwin_srmmu_stackchk
313spwin_srmmu_stackchk:
314	/* LOCATION: Window to be saved on the stack */
315
316	/* Because of SMP concerns and speed we play a trick.
317	 * We disable fault traps in the MMU control register,
318	 * Execute the stores, then check the fault registers
319	 * to see what happens.  I can hear Linus now
320	 * "disgusting... broken hardware...".
321	 *
322	 * But first, check to see if the users stack has ended
323	 * up in kernel vma, then we would succeed for the 'wrong'
324	 * reason... ;(  Note that the 'sethi' below assumes the
325	 * kernel is page aligned, which should always be the case.
326	 */
327	/* Check results of callers andcc %sp, 0x7, %g0 */
328	bne	spwin_user_stack_is_bolixed
329	 sethi   %hi(PAGE_OFFSET), %glob_tmp
330	cmp	%glob_tmp, %sp
331	bleu	spwin_user_stack_is_bolixed
332	 mov	AC_M_SFSR, %glob_tmp
333
334	/* Clear the fault status and turn on the no_fault bit. */
335LEON_PI(lda	[%glob_tmp] ASI_LEON_MMUREGS, %g0)	! eat SFSR
336SUN_PI_(lda	[%glob_tmp] ASI_M_MMUREGS, %g0)		! eat SFSR
337
338LEON_PI(lda	[%g0] ASI_LEON_MMUREGS, %glob_tmp)	! read MMU control
339SUN_PI_(lda	[%g0] ASI_M_MMUREGS, %glob_tmp)		! read MMU control
340	or	%glob_tmp, 0x2, %glob_tmp		! or in no_fault bit
341LEON_PI(sta	%glob_tmp, [%g0] ASI_LEON_MMUREGS)	! set it
342SUN_PI_(sta	%glob_tmp, [%g0] ASI_M_MMUREGS)		! set it
343
344	/* Dump the registers and cross fingers. */
345	STORE_WINDOW(sp)
346
347	/* Clear the no_fault bit and check the status. */
348	andn	%glob_tmp, 0x2, %glob_tmp
349LEON_PI(sta	%glob_tmp, [%g0] ASI_LEON_MMUREGS)
350SUN_PI_(sta	%glob_tmp, [%g0] ASI_M_MMUREGS)
351
352	mov	AC_M_SFAR, %glob_tmp
353LEON_PI(lda	[%glob_tmp] ASI_LEON_MMUREGS, %g0)
354SUN_PI_(lda	[%glob_tmp] ASI_M_MMUREGS, %g0)
355
356	mov	AC_M_SFSR, %glob_tmp
357LEON_PI(lda	[%glob_tmp] ASI_LEON_MMUREGS, %glob_tmp)
358SUN_PI_(lda	[%glob_tmp] ASI_M_MMUREGS, %glob_tmp)
359	andcc	%glob_tmp, 0x2, %g0			! did we fault?
360	be,a	spwin_finish_up + 0x4			! cool beans, success
361	 restore %g0, %g0, %g0
362
363	rd	%psr, %glob_tmp
364	b	spwin_user_stack_is_bolixed + 0x4	! we faulted, ugh
365	 nop
366