xref: /openbmc/linux/arch/sparc/kernel/wuf.S (revision 3d3337de)
1/*
2 * wuf.S: Window underflow trap handler for the Sparc.
3 *
4 * Copyright (C) 1995 David S. Miller
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/* Just like the overflow handler we define macros for registers
18 * with fixed meanings in this routine.
19 */
20#define t_psr       l0
21#define t_pc        l1
22#define t_npc       l2
23#define t_wim       l3
24/* Don't touch the above registers or else you die horribly... */
25
26/* Now macros for the available scratch registers in this routine. */
27#define twin_tmp1    l4
28#define twin_tmp2    l5
29
30#define curptr       g6
31
32	.text
33	.align	4
34
35	/* The trap entry point has executed the following:
36	 *
37	 * rd    %psr, %l0
38	 * rd    %wim, %l3
39	 * b     fill_window_entry
40	 * andcc %l0, PSR_PS, %g0
41	 */
42
43	/* Datum current_thread_info->uwinmask contains at all times a bitmask
44	 * where if any user windows are active, at least one bit will
45	 * be set in to mask.  If no user windows are active, the bitmask
46	 * will be all zeroes.
47	 */
48
49	/* To get an idea of what has just happened to cause this
50	 * trap take a look at this diagram:
51	 *
52	 *      1  2  3  4     <--  Window number
53	 *      ----------
54	 *      T  O  W  I     <--  Symbolic name
55	 *
56	 *      O == the window that execution was in when
57	 *           the restore was attempted
58	 *
59	 *      T == the trap itself has save'd us into this
60	 *           window
61	 *
62	 *      W == this window is the one which is now invalid
63	 *           and must be made valid plus loaded from the
64	 *           stack
65	 *
66	 *      I == this window will be the invalid one when we
67	 *           are done and return from trap if successful
68	 */
69
70	/* BEGINNING OF PATCH INSTRUCTIONS */
71
72	/* On 7-window Sparc the boot code patches fnwin_patch1
73	 * with the following instruction.
74	 */
75	.globl	fnwin_patch1_7win, fnwin_patch2_7win
76fnwin_patch1_7win:	srl	%t_wim, 6, %twin_tmp2
77fnwin_patch2_7win:	and	%twin_tmp1, 0x7f, %twin_tmp1
78	/* END OF PATCH INSTRUCTIONS */
79
80	.globl	fill_window_entry, fnwin_patch1, fnwin_patch2
81fill_window_entry:
82	/* LOCATION: Window 'T' */
83
84	/* Compute what the new %wim is going to be if we retrieve
85	 * the proper window off of the stack.
86	 */
87		sll	%t_wim, 1, %twin_tmp1
88fnwin_patch1:	srl	%t_wim, 7, %twin_tmp2
89		or	%twin_tmp1, %twin_tmp2, %twin_tmp1
90fnwin_patch2:	and	%twin_tmp1, 0xff, %twin_tmp1
91
92	wr	%twin_tmp1, 0x0, %wim	/* Make window 'I' invalid */
93
94	andcc	%t_psr, PSR_PS, %g0
95	be	fwin_from_user
96	 restore	%g0, %g0, %g0		/* Restore to window 'O' */
97
98	/* Trapped from kernel, we trust that the kernel does not
99	 * 'over restore' sorta speak and just grab the window
100	 * from the stack and return.  Easy enough.
101	 */
102fwin_from_kernel:
103	/* LOCATION: Window 'O' */
104
105	restore %g0, %g0, %g0
106
107	/* LOCATION: Window 'W' */
108
109	LOAD_WINDOW(sp)	                /* Load it up */
110
111	/* Spin the wheel... */
112	save	%g0, %g0, %g0
113	save	%g0, %g0, %g0
114	/* I'd like to buy a vowel please... */
115
116	/* LOCATION: Window 'T' */
117
118	/* Now preserve the condition codes in %psr, pause, and
119	 * return from trap.  This is the simplest case of all.
120	 */
121	wr	%t_psr, 0x0, %psr
122	WRITE_PAUSE
123
124	jmp	%t_pc
125	rett	%t_npc
126
127fwin_from_user:
128	/* LOCATION: Window 'O' */
129
130	restore	%g0, %g0, %g0		/* Restore to window 'W' */
131
132	/* LOCATION: Window 'W' */
133
134	/* Branch to the stack validation routine */
135	b	srmmu_fwin_stackchk
136	 andcc	%sp, 0x7, %g0
137
138#define STACK_OFFSET (THREAD_SIZE - TRACEREG_SZ - STACKFRAME_SZ)
139
140fwin_user_stack_is_bolixed:
141	/* LOCATION: Window 'W' */
142
143	/* Place a pt_regs frame on the kernel stack, save back
144	 * to the trap window and call c-code to deal with this.
145	 */
146	LOAD_CURRENT(l4, l5)
147
148	sethi	%hi(STACK_OFFSET), %l5
149	or	%l5, %lo(STACK_OFFSET), %l5
150	add	%l4, %l5, %l5
151
152	/* Store globals into pt_regs frame. */
153	STORE_PT_GLOBALS(l5)
154	STORE_PT_YREG(l5, g3)
155
156	/* Save current in a global while we change windows. */
157	mov	%l4, %curptr
158
159	save	%g0, %g0, %g0
160
161	/* LOCATION: Window 'O' */
162
163	rd	%psr, %g3		/* Read %psr in live user window */
164	mov	%fp, %g4		/* Save bogus frame pointer. */
165
166	save	%g0, %g0, %g0
167
168	/* LOCATION: Window 'T' */
169
170	sethi	%hi(STACK_OFFSET), %l5
171	or	%l5, %lo(STACK_OFFSET), %l5
172	add	%curptr, %l5, %sp
173
174	/* Build rest of pt_regs. */
175	STORE_PT_INS(sp)
176	STORE_PT_PRIV(sp, t_psr, t_pc, t_npc)
177
178	/* re-set trap time %wim value */
179	wr	%t_wim, 0x0, %wim
180
181	/* Fix users window mask and buffer save count. */
182	mov	0x1, %g5
183	sll	%g5, %g3, %g5
184	st	%g5, [%curptr + TI_UWINMASK]		! one live user window still
185	st	%g0, [%curptr + TI_W_SAVED]		! no windows in the buffer
186
187	wr	%t_psr, PSR_ET, %psr			! enable traps
188	nop
189	call	window_underflow_fault
190	 mov	%g4, %o0
191
192	b	ret_trap_entry
193	 clr	%l6
194
195fwin_user_stack_is_ok:
196	/* LOCATION: Window 'W' */
197
198	/* The users stack area is kosher and mapped, load the
199	 * window and fall through to the finish up routine.
200	 */
201	LOAD_WINDOW(sp)
202
203	/* Round and round she goes... */
204	save	%g0, %g0, %g0		/* Save to window 'O' */
205	save	%g0, %g0, %g0		/* Save to window 'T' */
206	/* Where she'll trap nobody knows... */
207
208	/* LOCATION: Window 'T' */
209
210fwin_user_finish_up:
211	/* LOCATION: Window 'T' */
212
213	wr	%t_psr, 0x0, %psr
214	WRITE_PAUSE
215
216	jmp	%t_pc
217	rett	%t_npc
218
219	/* Here come the architecture specific checks for stack.
220	 * mappings.  Note that unlike the window overflow handler
221	 * we only need to check whether the user can read from
222	 * the appropriate addresses.  Also note that we are in
223	 * an invalid window which will be loaded, and this means
224	 * that until we actually load the window up we are free
225	 * to use any of the local registers contained within.
226	 *
227	 * On success these routine branch to fwin_user_stack_is_ok
228	 * if the area at %sp is user readable and the window still
229	 * needs to be loaded, else fwin_user_finish_up if the
230	 * routine has done the loading itself.  On failure (bogus
231	 * user stack) the routine shall branch to the label called
232	 * fwin_user_stack_is_bolixed.
233	 *
234	 * Contrary to the arch-specific window overflow stack
235	 * check routines in wof.S, these routines are free to use
236	 * any of the local registers they want to as this window
237	 * does not belong to anyone at this point, however the
238	 * outs and ins are still verboten as they are part of
239	 * 'someone elses' window possibly.
240	 */
241
242	.globl	srmmu_fwin_stackchk
243srmmu_fwin_stackchk:
244	/* LOCATION: Window 'W' */
245
246	/* Caller did 'andcc %sp, 0x7, %g0' */
247	bne	fwin_user_stack_is_bolixed
248	 sethi   %hi(PAGE_OFFSET), %l5
249
250	/* Check if the users stack is in kernel vma, then our
251	 * trial and error technique below would succeed for
252	 * the 'wrong' reason.
253	 */
254	mov	AC_M_SFSR, %l4
255	cmp	%l5, %sp
256	bleu	fwin_user_stack_is_bolixed
257LEON_PI( lda	[%l4] ASI_LEON_MMUREGS, %g0)	! clear fault status
258SUN_PI_( lda	[%l4] ASI_M_MMUREGS, %g0)	! clear fault status
259
260	/* The technique is, turn off faults on this processor,
261	 * just let the load rip, then check the sfsr to see if
262	 * a fault did occur.  Then we turn on fault traps again
263	 * and branch conditionally based upon what happened.
264	 */
265LEON_PI(lda	[%g0] ASI_LEON_MMUREGS, %l5)	! read mmu-ctrl reg
266SUN_PI_(lda	[%g0] ASI_M_MMUREGS, %l5)	! read mmu-ctrl reg
267	or	%l5, 0x2, %l5			! turn on no-fault bit
268LEON_PI(sta	%l5, [%g0] ASI_LEON_MMUREGS)	! store it
269SUN_PI_(sta	%l5, [%g0] ASI_M_MMUREGS)	! store it
270
271	/* Cross fingers and go for it. */
272	LOAD_WINDOW(sp)
273
274	/* A penny 'saved'... */
275	save	%g0, %g0, %g0
276	save	%g0, %g0, %g0
277	/* Is a BADTRAP earned... */
278
279	/* LOCATION: Window 'T' */
280
281LEON_PI(lda	[%g0] ASI_LEON_MMUREGS, %twin_tmp1)	! load mmu-ctrl again
282SUN_PI_(lda	[%g0] ASI_M_MMUREGS, %twin_tmp1)	! load mmu-ctrl again
283	andn	%twin_tmp1, 0x2, %twin_tmp1		! clear no-fault bit
284LEON_PI(sta	%twin_tmp1, [%g0] ASI_LEON_MMUREGS)	! store it
285SUN_PI_(sta	%twin_tmp1, [%g0] ASI_M_MMUREGS)	! store it
286
287	mov	AC_M_SFAR, %twin_tmp2
288LEON_PI(lda	[%twin_tmp2] ASI_LEON_MMUREGS, %g0)	! read fault address
289SUN_PI_(lda	[%twin_tmp2] ASI_M_MMUREGS, %g0)	! read fault address
290
291	mov	AC_M_SFSR, %twin_tmp2
292LEON_PI(lda	[%twin_tmp2] ASI_LEON_MMUREGS, %twin_tmp2) ! read fault status
293SUN_PI_(lda	[%twin_tmp2] ASI_M_MMUREGS, %twin_tmp2)	   ! read fault status
294	andcc	%twin_tmp2, 0x2, %g0			   ! did fault occur?
295
296	bne	1f					   ! yep, cleanup
297	 nop
298
299	wr	%t_psr, 0x0, %psr
300	nop
301	b	fwin_user_finish_up + 0x4
302	 nop
303
304	/* Did I ever tell you about my window lobotomy?
305	 * anyways... fwin_user_stack_is_bolixed expects
306	 * to be in window 'W' so make it happy or else
307	 * we watchdog badly.
308	 */
3091:
310	restore	%g0, %g0, %g0
311	b	fwin_user_stack_is_bolixed	! oh well
312	 restore	%g0, %g0, %g0
313