1 /* $Id: wof.S,v 1.22 1996/04/03 02:15:10 davem Exp $ 2 * wof.S: Sparc window overflow handler. 3 * 4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu) 5 */ 6 7 #include <asm/cprefix.h> 8 #include <asm/contregs.h> 9 #include <asm/page.h> 10 #include <asm/ptrace.h> 11 #include <asm/psr.h> 12 #include <asm/smp.h> 13 #include <asm/asi.h> 14 #include <asm/winmacro.h> 15 #include <asm/asmmacro.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 52 spnwin_patch1_7win: sll %t_wim, 6, %glob_tmp 53 spnwin_patch2_7win: and %glob_tmp, 0x7f, %glob_tmp 54 spnwin_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->tss.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 72 spill_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 84 spnwin_patch1: sll %t_wim, 7, %glob_tmp 85 or %glob_tmp, %twin_tmp, %glob_tmp 86 spnwin_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 spwin_fromuser ! all user wins, branch 96 nop 97 98 /* See if any user windows are active in the set. */ 99 ld [%curptr + THREAD_UMASK], %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 nop 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 WRITE_PAUSE ! burn cpu cycles due to bad engineering 112 113 spwin_no_userwins_from_kernel: 114 /* LOCATION: Window to be saved */ 115 116 STORE_WINDOW(sp) ! stash the window 117 restore %g0, %g0, %g0 ! go back into trap window 118 119 /* LOCATION: Trap window */ 120 mov %saved_g5, %g5 ! restore %glob_tmp 121 mov %saved_g6, %g6 ! restore %curptr 122 wr %t_psr, 0x0, %psr ! restore condition codes in %psr 123 WRITE_PAUSE ! waste some time 124 jmp %t_pc ! Return from trap 125 rett %t_npc ! we are done 126 127 spwin_exist_uwins: 128 /* LOCATION: Trap window */ 129 130 /* Wow, user windows have to be dealt with, this is dirty 131 * and messy as all hell. And difficult to follow if you 132 * are approaching the infamous register window trap handling 133 * problem for the first time. DON'T LOOK! 134 * 135 * Note that how the execution path works out, the new %wim 136 * will be left for us in the global temporary register, 137 * %glob_tmp. We cannot set the new %wim first because we 138 * need to save into the appropriate window without inducing 139 * a trap (traps are off, we'd get a watchdog wheee)... 140 * But first, store the new user window mask calculated 141 * above. 142 */ 143 andn %twin_tmp, %glob_tmp, %twin_tmp ! compute new umask 144 st %twin_tmp, [%curptr + THREAD_UMASK] 145 146 spwin_fromuser: 147 /* LOCATION: Trap window */ 148 save %g0, %g0, %g0 ! Go to where the saving will occur 149 150 /* LOCATION: Window to be saved */ 151 wr %glob_tmp, 0x0, %wim ! Now it is safe to set new %wim 152 WRITE_PAUSE ! burn baby burn 153 154 /* LOCATION: Window to be saved */ 155 156 /* This instruction branches to a routine which will check 157 * to validity of the users stack pointer by whatever means 158 * are necessary. This means that this is architecture 159 * specific and thus this branch instruction will need to 160 * be patched at boot time once the machine type is known. 161 * This routine _shall not_ touch %curptr under any 162 * circumstances whatsoever! It will branch back to the 163 * label 'spwin_good_ustack' if the stack is ok but still 164 * needs to be dumped (SRMMU for instance will not need to 165 * do this) or 'spwin_finish_up' if the stack is ok and the 166 * registers have already been saved. If the stack is found 167 * to be bogus for some reason the routine shall branch to 168 * the label 'spwin_user_stack_is_bolixed' which will take 169 * care of things at that point. 170 */ 171 .globl C_LABEL(spwin_mmu_patchme) 172 C_LABEL(spwin_mmu_patchme): b C_LABEL(spwin_sun4c_stackchk) 173 andcc %sp, 0x7, %g0 174 175 spwin_good_ustack: 176 /* LOCATION: Window to be saved */ 177 178 /* The users stack is ok and we can safely save it at 179 * %sp. 180 */ 181 STORE_WINDOW(sp) 182 183 spwin_finish_up: 184 restore %g0, %g0, %g0 /* Back to trap window. */ 185 186 /* LOCATION: Trap window */ 187 188 /* We have spilled successfully, and we have properly stored 189 * the appropriate window onto the stack. 190 */ 191 192 /* Restore saved globals */ 193 mov %saved_g5, %g5 194 mov %saved_g6, %g6 195 wr %t_psr, 0x0, %psr 196 WRITE_PAUSE 197 jmp %t_pc 198 rett %t_npc 199 200 spwin_user_stack_is_bolixed: 201 /* LOCATION: Window to be saved */ 202 203 /* Wheee, user has trashed his/her stack. We have to decide 204 * how to proceed based upon whether we came from kernel mode 205 * or not. If we came from kernel mode, toss the window into 206 * a special buffer and proceed, the kernel _needs_ a window 207 * and we could be in an interrupt handler so timing is crucial. 208 * If we came from user land we build a full stack frame and call 209 * c-code to gun down the process. 210 */ 211 rd %psr, %glob_tmp 212 andcc %glob_tmp, PSR_PS, %g0 213 bne spwin_bad_ustack_from_kernel 214 nop 215 216 /* Oh well, throw this one window into the per-task window 217 * buffer, the first one. 218 */ 219 st %sp, [%curptr + THREAD_STACK_PTRS] 220 STORE_WINDOW(curptr + THREAD_REG_WINDOW) 221 restore %g0, %g0, %g0 222 223 /* LOCATION: Trap Window */ 224 225 /* Back in the trap window, update winbuffer save count. */ 226 mov 1, %glob_tmp 227 st %glob_tmp, [%curptr + THREAD_W_SAVED] 228 229 /* Compute new user window mask. What we are basically 230 * doing is taking two windows, the invalid one at trap 231 * time and the one we attempted to throw onto the users 232 * stack, and saying that everything else is an ok user 233 * window. umask = ((~(%t_wim | %wim)) & valid_wim_bits) 234 */ 235 rd %wim, %twin_tmp 236 or %twin_tmp, %t_wim, %twin_tmp 237 not %twin_tmp 238 spnwin_patch3: and %twin_tmp, 0xff, %twin_tmp ! patched on 7win Sparcs 239 st %twin_tmp, [%curptr + THREAD_UMASK] 240 241 /* Jump onto kernel stack for this process... */ 242 ld [%curptr + TASK_SAVED_KSTACK], %sp 243 244 /* Restore the saved globals and build a pt_regs frame. */ 245 mov %saved_g5, %g5 246 mov %saved_g6, %g6 247 STORE_PT_ALL(sp, t_psr, t_pc, t_npc, g1) 248 249 ENTER_SYSCALL 250 251 /* Turn on traps and call c-code to deal with it. */ 252 wr %t_psr, PSR_ET, %psr 253 WRITE_PAUSE 254 255 call C_LABEL(window_overflow_fault) 256 nop 257 258 /* Return from trap if C-code actually fixes things, if it 259 * doesn't then we never get this far as the process will 260 * be given the look of death from Commander Peanut. 261 */ 262 b ret_trap_entry 263 nop 264 265 spwin_bad_ustack_from_kernel: 266 /* LOCATION: Window to be saved */ 267 268 /* The kernel provoked a spill window trap, but the window we 269 * need to save is a user one and the process has trashed its 270 * stack pointer. We need to be quick, so we throw it into 271 * a per-process window buffer until we can properly handle 272 * this later on. 273 */ 274 SAVE_BOLIXED_USER_STACK(curptr, glob_tmp) 275 restore %g0, %g0, %g0 276 277 /* LOCATION: Trap window */ 278 279 /* Restore globals, condition codes in the %psr and 280 * return from trap. 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 .globl C_LABEL(spwin_sun4c_stackchk) 309 C_LABEL(spwin_sun4c_stackchk): 310 /* LOCATION: Window to be saved on the stack */ 311 312 /* See if the stack is in the address space hole but first, 313 * check results of callers andcc %sp, 0x7, %g0 314 */ 315 be 1f 316 sra %sp, 29, %glob_tmp 317 318 b spwin_user_stack_is_bolixed 319 nop 320 321 1: 322 add %glob_tmp, 0x1, %glob_tmp 323 andncc %glob_tmp, 0x1, %g0 324 be 1f 325 and %sp, 0xfff, %glob_tmp ! delay slot 326 327 b spwin_user_stack_is_bolixed 328 nop 329 330 /* See if our dump area will be on more than one 331 * page. 332 */ 333 1: 334 add %glob_tmp, 0x38, %glob_tmp 335 andncc %glob_tmp, 0xff8, %g0 336 be spwin_sun4c_onepage ! only one page to check 337 lda [%sp] ASI_PTE, %glob_tmp ! have to check first page anyways 338 339 spwin_sun4c_twopages: 340 /* Is first page ok permission wise? */ 341 srl %glob_tmp, 29, %glob_tmp 342 cmp %glob_tmp, 0x6 343 be 1f 344 add %sp, 0x38, %glob_tmp /* Is second page in vma hole? */ 345 346 b spwin_user_stack_is_bolixed 347 nop 348 349 1: 350 sra %glob_tmp, 29, %glob_tmp 351 add %glob_tmp, 0x1, %glob_tmp 352 andncc %glob_tmp, 0x1, %g0 353 be 1f 354 add %sp, 0x38, %glob_tmp 355 356 b spwin_user_stack_is_bolixed 357 nop 358 359 1: 360 lda [%glob_tmp] ASI_PTE, %glob_tmp 361 362 spwin_sun4c_onepage: 363 srl %glob_tmp, 29, %glob_tmp 364 cmp %glob_tmp, 0x6 ! can user write to it? 365 be spwin_good_ustack ! success 366 nop 367 368 b spwin_user_stack_is_bolixed 369 nop 370 371 /* This is a generic SRMMU routine. As far as I know this 372 * works for all current v8/srmmu implementations, we'll 373 * see... 374 */ 375 .globl C_LABEL(spwin_srmmu_stackchk) 376 C_LABEL(spwin_srmmu_stackchk): 377 /* LOCATION: Window to be saved on the stack */ 378 379 /* Because of SMP concerns and speed we play a trick. 380 * We disable fault traps in the MMU control register, 381 * Execute the stores, then check the fault registers 382 * to see what happens. I can hear Linus now 383 * "disgusting... broken hardware...". 384 * 385 * But first, check to see if the users stack has ended 386 * up in kernel vma, then we would succeed for the 'wrong' 387 * reason... ;( Note that the 'sethi' below assumes the 388 * kernel is page aligned, which should always be the case. 389 */ 390 /* Check results of callers andcc %sp, 0x7, %g0 */ 391 bne spwin_user_stack_is_bolixed 392 sethi %hi(KERNBASE), %glob_tmp 393 cmp %glob_tmp, %sp 394 bleu spwin_user_stack_is_bolixed 395 mov AC_M_SFSR, %glob_tmp 396 397 /* Clear the fault status and turn on the no_fault bit. */ 398 lda [%glob_tmp] ASI_M_MMUREGS, %g0 ! eat SFSR 399 400 lda [%g0] ASI_M_MMUREGS, %glob_tmp ! read MMU control 401 or %glob_tmp, 0x2, %glob_tmp ! or in no_fault bit 402 sta %glob_tmp, [%g0] ASI_M_MMUREGS ! set it 403 404 /* Dump the registers and cross fingers. */ 405 STORE_WINDOW(sp) 406 407 /* Clear the no_fault bit and check the status. */ 408 andn %glob_tmp, 0x2, %glob_tmp 409 sta %glob_tmp, [%g0] ASI_M_MMUREGS 410 411 mov AC_M_SFAR, %glob_tmp 412 lda [%glob_tmp] ASI_M_MMUREGS, %g0 413 414 mov AC_M_SFSR, %glob_tmp 415 lda [%glob_tmp] ASI_M_MMUREGS, %glob_tmp 416 andcc %glob_tmp, 0x2, %g0 ! did we fault? 417 be spwin_finish_up ! cool beans, success 418 nop 419 420 b spwin_user_stack_is_bolixed ! we faulted, ugh 421 nop