root/arch/sparc/kernel/wuf.S

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

/* [previous][next][first][last][top][bottom][index][help] */