1 /* SPDX-License-Identifier: GPL-2.0 */
3 * wuf.S: Window underflow trap handler for the Sparc.
5 * Copyright (C) 1995 David S. Miller
8 #include <asm/contregs.h>
10 #include <asm/ptrace.h>
14 #include <asm/winmacro.h>
15 #include <asm/asmmacro.h>
16 #include <asm/thread_info.h>
18 /* Just like the overflow handler we define macros for registers
19 * with fixed meanings in this routine.
25 /* Don't touch the above registers or else you die horribly... */
27 /* Now macros for the available scratch registers in this routine. */
36 /* The trap entry point has executed the following:
41 * andcc %l0, PSR_PS, %g0
44 /* Datum current_thread_info->uwinmask contains at all times a bitmask
45 * where if any user windows are active, at least one bit will
46 * be set in to mask. If no user windows are active, the bitmask
50 /* To get an idea of what has just happened to cause this
51 * trap take a look at this diagram:
53 * 1 2 3 4 <-- Window number
55 * T O W I <-- Symbolic name
57 * O == the window that execution was in when
58 * the restore was attempted
60 * T == the trap itself has save'd us into this
63 * W == this window is the one which is now invalid
64 * and must be made valid plus loaded from the
67 * I == this window will be the invalid one when we
68 * are done and return from trap if successful
71 /* BEGINNING OF PATCH INSTRUCTIONS */
73 /* On 7-window Sparc the boot code patches fnwin_patch1
74 * with the following instruction.
76 .globl fnwin_patch1_7win, fnwin_patch2_7win
77 fnwin_patch1_7win: srl %t_wim, 6, %twin_tmp2
78 fnwin_patch2_7win: and %twin_tmp1, 0x7f, %twin_tmp1
79 /* END OF PATCH INSTRUCTIONS */
81 .globl fill_window_entry, fnwin_patch1, fnwin_patch2
83 /* LOCATION: Window 'T' */
85 /* Compute what the new %wim is going to be if we retrieve
86 * the proper window off of the stack.
88 sll %t_wim, 1, %twin_tmp1
89 fnwin_patch1: srl %t_wim, 7, %twin_tmp2
90 or %twin_tmp1, %twin_tmp2, %twin_tmp1
91 fnwin_patch2: and %twin_tmp1, 0xff, %twin_tmp1
93 wr %twin_tmp1, 0x0, %wim /* Make window 'I' invalid */
95 andcc %t_psr, PSR_PS, %g0
97 restore %g0, %g0, %g0 /* Restore to window 'O' */
99 /* Trapped from kernel, we trust that the kernel does not
100 * 'over restore' sorta speak and just grab the window
101 * from the stack and return. Easy enough.
104 /* LOCATION: Window 'O' */
106 restore %g0, %g0, %g0
108 /* LOCATION: Window 'W' */
110 LOAD_WINDOW(sp) /* Load it up */
112 /* Spin the wheel... */
115 /* I'd like to buy a vowel please... */
117 /* LOCATION: Window 'T' */
119 /* Now preserve the condition codes in %psr, pause, and
120 * return from trap. This is the simplest case of all.
129 /* LOCATION: Window 'O' */
131 restore %g0, %g0, %g0 /* Restore to window 'W' */
133 /* LOCATION: Window 'W' */
135 /* Branch to the stack validation routine */
136 b srmmu_fwin_stackchk
139 #define STACK_OFFSET (THREAD_SIZE - TRACEREG_SZ - STACKFRAME_SZ)
141 fwin_user_stack_is_bolixed:
142 /* LOCATION: Window 'W' */
144 /* Place a pt_regs frame on the kernel stack, save back
145 * to the trap window and call c-code to deal with this.
149 sethi %hi(STACK_OFFSET), %l5
150 or %l5, %lo(STACK_OFFSET), %l5
153 /* Store globals into pt_regs frame. */
155 STORE_PT_YREG(l5, g3)
157 /* Save current in a global while we change windows. */
162 /* LOCATION: Window 'O' */
164 rd %psr, %g3 /* Read %psr in live user window */
165 mov %fp, %g4 /* Save bogus frame pointer. */
169 /* LOCATION: Window 'T' */
171 sethi %hi(STACK_OFFSET), %l5
172 or %l5, %lo(STACK_OFFSET), %l5
173 add %curptr, %l5, %sp
175 /* Build rest of pt_regs. */
177 STORE_PT_PRIV(sp, t_psr, t_pc, t_npc)
179 /* re-set trap time %wim value */
182 /* Fix users window mask and buffer save count. */
185 st %g5, [%curptr + TI_UWINMASK] ! one live user window still
186 st %g0, [%curptr + TI_W_SAVED] ! no windows in the buffer
188 wr %t_psr, PSR_ET, %psr ! enable traps
190 call window_underflow_fault
196 fwin_user_stack_is_ok:
197 /* LOCATION: Window 'W' */
199 /* The users stack area is kosher and mapped, load the
200 * window and fall through to the finish up routine.
204 /* Round and round she goes... */
205 save %g0, %g0, %g0 /* Save to window 'O' */
206 save %g0, %g0, %g0 /* Save to window 'T' */
207 /* Where she'll trap nobody knows... */
209 /* LOCATION: Window 'T' */
212 /* LOCATION: Window 'T' */
220 /* Here come the architecture specific checks for stack.
221 * mappings. Note that unlike the window overflow handler
222 * we only need to check whether the user can read from
223 * the appropriate addresses. Also note that we are in
224 * an invalid window which will be loaded, and this means
225 * that until we actually load the window up we are free
226 * to use any of the local registers contained within.
228 * On success these routine branch to fwin_user_stack_is_ok
229 * if the area at %sp is user readable and the window still
230 * needs to be loaded, else fwin_user_finish_up if the
231 * routine has done the loading itself. On failure (bogus
232 * user stack) the routine shall branch to the label called
233 * fwin_user_stack_is_bolixed.
235 * Contrary to the arch-specific window overflow stack
236 * check routines in wof.S, these routines are free to use
237 * any of the local registers they want to as this window
238 * does not belong to anyone at this point, however the
239 * outs and ins are still verboten as they are part of
240 * 'someone elses' window possibly.
243 .globl srmmu_fwin_stackchk
245 /* LOCATION: Window 'W' */
247 /* Caller did 'andcc %sp, 0x7, %g0' */
248 bne fwin_user_stack_is_bolixed
249 sethi %hi(PAGE_OFFSET), %l5
251 /* Check if the users stack is in kernel vma, then our
252 * trial and error technique below would succeed for
253 * the 'wrong' reason.
257 bleu fwin_user_stack_is_bolixed
258 LEON_PI( lda [%l4] ASI_LEON_MMUREGS, %g0) ! clear fault status
259 SUN_PI_( lda [%l4] ASI_M_MMUREGS, %g0) ! clear fault status
261 /* The technique is, turn off faults on this processor,
262 * just let the load rip, then check the sfsr to see if
263 * a fault did occur. Then we turn on fault traps again
264 * and branch conditionally based upon what happened.
266 LEON_PI(lda [%g0] ASI_LEON_MMUREGS, %l5) ! read mmu-ctrl reg
267 SUN_PI_(lda [%g0] ASI_M_MMUREGS, %l5) ! read mmu-ctrl reg
268 or %l5, 0x2, %l5 ! turn on no-fault bit
269 LEON_PI(sta %l5, [%g0] ASI_LEON_MMUREGS) ! store it
270 SUN_PI_(sta %l5, [%g0] ASI_M_MMUREGS) ! store it
272 /* Cross fingers and go for it. */
275 /* A penny 'saved'... */
278 /* Is a BADTRAP earned... */
280 /* LOCATION: Window 'T' */
282 LEON_PI(lda [%g0] ASI_LEON_MMUREGS, %twin_tmp1) ! load mmu-ctrl again
283 SUN_PI_(lda [%g0] ASI_M_MMUREGS, %twin_tmp1) ! load mmu-ctrl again
284 andn %twin_tmp1, 0x2, %twin_tmp1 ! clear no-fault bit
285 LEON_PI(sta %twin_tmp1, [%g0] ASI_LEON_MMUREGS) ! store it
286 SUN_PI_(sta %twin_tmp1, [%g0] ASI_M_MMUREGS) ! store it
288 mov AC_M_SFAR, %twin_tmp2
289 LEON_PI(lda [%twin_tmp2] ASI_LEON_MMUREGS, %g0) ! read fault address
290 SUN_PI_(lda [%twin_tmp2] ASI_M_MMUREGS, %g0) ! read fault address
292 mov AC_M_SFSR, %twin_tmp2
293 LEON_PI(lda [%twin_tmp2] ASI_LEON_MMUREGS, %twin_tmp2) ! read fault status
294 SUN_PI_(lda [%twin_tmp2] ASI_M_MMUREGS, %twin_tmp2) ! read fault status
295 andcc %twin_tmp2, 0x2, %g0 ! did fault occur?
297 bne 1f ! yep, cleanup
302 b fwin_user_finish_up + 0x4
305 /* Did I ever tell you about my window lobotomy?
306 * anyways... fwin_user_stack_is_bolixed expects
307 * to be in window 'W' so make it happy or else
311 restore %g0, %g0, %g0
312 b fwin_user_stack_is_bolixed ! oh well
313 restore %g0, %g0, %g0