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