wof.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * wof.S: Sparc window overflow handler.
  3. *
  4. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  5. */
  6. #include <asm/contregs.h>
  7. #include <asm/page.h>
  8. #include <asm/ptrace.h>
  9. #include <asm/psr.h>
  10. #include <asm/smp.h>
  11. #include <asm/asi.h>
  12. #include <asm/winmacro.h>
  13. #include <asm/asmmacro.h>
  14. #include <asm/thread_info.h>
  15. /* WARNING: This routine is hairy and _very_ complicated, but it
  16. * must be as fast as possible as it handles the allocation
  17. * of register windows to the user and kernel. If you touch
  18. * this code be _very_ careful as many other pieces of the
  19. * kernel depend upon how this code behaves. You have been
  20. * duly warned...
  21. */
  22. /* We define macro's for registers which have a fixed
  23. * meaning throughout this entire routine. The 'T' in
  24. * the comments mean that the register can only be
  25. * accessed when in the 'trap' window, 'G' means
  26. * accessible in any window. Do not change these registers
  27. * after they have been set, until you are ready to return
  28. * from the trap.
  29. */
  30. #define t_psr l0 /* %psr at trap time T */
  31. #define t_pc l1 /* PC for trap return T */
  32. #define t_npc l2 /* NPC for trap return T */
  33. #define t_wim l3 /* %wim at trap time T */
  34. #define saved_g5 l5 /* Global save register T */
  35. #define saved_g6 l6 /* Global save register T */
  36. #define curptr g6 /* Gets set to 'current' then stays G */
  37. /* Now registers whose values can change within the handler. */
  38. #define twin_tmp l4 /* Temp reg, only usable in trap window T */
  39. #define glob_tmp g5 /* Global temporary reg, usable anywhere G */
  40. .text
  41. .align 4
  42. /* BEGINNING OF PATCH INSTRUCTIONS */
  43. /* On a 7-window Sparc the boot code patches spnwin_*
  44. * instructions with the following ones.
  45. */
  46. .globl spnwin_patch1_7win, spnwin_patch2_7win, spnwin_patch3_7win
  47. spnwin_patch1_7win: sll %t_wim, 6, %glob_tmp
  48. spnwin_patch2_7win: and %glob_tmp, 0x7f, %glob_tmp
  49. spnwin_patch3_7win: and %twin_tmp, 0x7f, %twin_tmp
  50. /* END OF PATCH INSTRUCTIONS */
  51. /* The trap entry point has done the following:
  52. *
  53. * rd %psr, %l0
  54. * rd %wim, %l3
  55. * b spill_window_entry
  56. * andcc %l0, PSR_PS, %g0
  57. */
  58. /* Datum current_thread_info->uwinmask contains at all times a bitmask
  59. * where if any user windows are active, at least one bit will
  60. * be set in to mask. If no user windows are active, the bitmask
  61. * will be all zeroes.
  62. */
  63. .globl spill_window_entry
  64. .globl spnwin_patch1, spnwin_patch2, spnwin_patch3
  65. spill_window_entry:
  66. /* LOCATION: Trap Window */
  67. mov %g5, %saved_g5 ! save away global temp register
  68. mov %g6, %saved_g6 ! save away 'current' ptr register
  69. /* Compute what the new %wim will be if we save the
  70. * window properly in this trap handler.
  71. *
  72. * newwim = ((%wim>>1) | (%wim<<(nwindows - 1)));
  73. */
  74. srl %t_wim, 0x1, %twin_tmp
  75. spnwin_patch1: sll %t_wim, 7, %glob_tmp
  76. or %glob_tmp, %twin_tmp, %glob_tmp
  77. spnwin_patch2: and %glob_tmp, 0xff, %glob_tmp
  78. /* The trap entry point has set the condition codes
  79. * up for us to see if this is from user or kernel.
  80. * Get the load of 'curptr' out of the way.
  81. */
  82. LOAD_CURRENT(curptr, twin_tmp)
  83. andcc %t_psr, PSR_PS, %g0
  84. be,a spwin_fromuser ! all user wins, branch
  85. save %g0, %g0, %g0 ! Go where saving will occur
  86. /* See if any user windows are active in the set. */
  87. ld [%curptr + TI_UWINMASK], %twin_tmp ! grab win mask
  88. orcc %g0, %twin_tmp, %g0 ! check for set bits
  89. bne spwin_exist_uwins ! yep, there are some
  90. andn %twin_tmp, %glob_tmp, %twin_tmp ! compute new uwinmask
  91. /* Save into the window which must be saved and do it.
  92. * Basically if we are here, this means that we trapped
  93. * from kernel mode with only kernel windows in the register
  94. * file.
  95. */
  96. save %g0, %g0, %g0 ! save into the window to stash away
  97. wr %glob_tmp, 0x0, %wim ! set new %wim, this is safe now
  98. spwin_no_userwins_from_kernel:
  99. /* LOCATION: Window to be saved */
  100. STORE_WINDOW(sp) ! stash the window
  101. restore %g0, %g0, %g0 ! go back into trap window
  102. /* LOCATION: Trap window */
  103. mov %saved_g5, %g5 ! restore %glob_tmp
  104. mov %saved_g6, %g6 ! restore %curptr
  105. wr %t_psr, 0x0, %psr ! restore condition codes in %psr
  106. WRITE_PAUSE ! waste some time
  107. jmp %t_pc ! Return from trap
  108. rett %t_npc ! we are done
  109. spwin_exist_uwins:
  110. /* LOCATION: Trap window */
  111. /* Wow, user windows have to be dealt with, this is dirty
  112. * and messy as all hell. And difficult to follow if you
  113. * are approaching the infamous register window trap handling
  114. * problem for the first time. DON'T LOOK!
  115. *
  116. * Note that how the execution path works out, the new %wim
  117. * will be left for us in the global temporary register,
  118. * %glob_tmp. We cannot set the new %wim first because we
  119. * need to save into the appropriate window without inducing
  120. * a trap (traps are off, we'd get a watchdog wheee)...
  121. * But first, store the new user window mask calculated
  122. * above.
  123. */
  124. st %twin_tmp, [%curptr + TI_UWINMASK]
  125. save %g0, %g0, %g0 ! Go to where the saving will occur
  126. spwin_fromuser:
  127. /* LOCATION: Window to be saved */
  128. wr %glob_tmp, 0x0, %wim ! Now it is safe to set new %wim
  129. /* LOCATION: Window to be saved */
  130. /* This instruction branches to a routine which will check
  131. * to validity of the users stack pointer by whatever means
  132. * are necessary. This means that this is architecture
  133. * specific and thus this branch instruction will need to
  134. * be patched at boot time once the machine type is known.
  135. * This routine _shall not_ touch %curptr under any
  136. * circumstances whatsoever! It will branch back to the
  137. * label 'spwin_good_ustack' if the stack is ok but still
  138. * needs to be dumped (SRMMU for instance will not need to
  139. * do this) or 'spwin_finish_up' if the stack is ok and the
  140. * registers have already been saved. If the stack is found
  141. * to be bogus for some reason the routine shall branch to
  142. * the label 'spwin_user_stack_is_bolixed' which will take
  143. * care of things at that point.
  144. */
  145. b spwin_srmmu_stackchk
  146. andcc %sp, 0x7, %g0
  147. spwin_good_ustack:
  148. /* LOCATION: Window to be saved */
  149. /* The users stack is ok and we can safely save it at
  150. * %sp.
  151. */
  152. STORE_WINDOW(sp)
  153. spwin_finish_up:
  154. restore %g0, %g0, %g0 /* Back to trap window. */
  155. /* LOCATION: Trap window */
  156. /* We have spilled successfully, and we have properly stored
  157. * the appropriate window onto the stack.
  158. */
  159. /* Restore saved globals */
  160. mov %saved_g5, %g5
  161. mov %saved_g6, %g6
  162. wr %t_psr, 0x0, %psr
  163. WRITE_PAUSE
  164. jmp %t_pc
  165. rett %t_npc
  166. spwin_user_stack_is_bolixed:
  167. /* LOCATION: Window to be saved */
  168. /* Wheee, user has trashed his/her stack. We have to decide
  169. * how to proceed based upon whether we came from kernel mode
  170. * or not. If we came from kernel mode, toss the window into
  171. * a special buffer and proceed, the kernel _needs_ a window
  172. * and we could be in an interrupt handler so timing is crucial.
  173. * If we came from user land we build a full stack frame and call
  174. * c-code to gun down the process.
  175. */
  176. rd %psr, %glob_tmp
  177. andcc %glob_tmp, PSR_PS, %g0
  178. bne spwin_bad_ustack_from_kernel
  179. nop
  180. /* Oh well, throw this one window into the per-task window
  181. * buffer, the first one.
  182. */
  183. st %sp, [%curptr + TI_RWIN_SPTRS]
  184. STORE_WINDOW(curptr + TI_REG_WINDOW)
  185. restore %g0, %g0, %g0
  186. /* LOCATION: Trap Window */
  187. /* Back in the trap window, update winbuffer save count. */
  188. mov 1, %twin_tmp
  189. st %twin_tmp, [%curptr + TI_W_SAVED]
  190. /* Compute new user window mask. What we are basically
  191. * doing is taking two windows, the invalid one at trap
  192. * time and the one we attempted to throw onto the users
  193. * stack, and saying that everything else is an ok user
  194. * window. umask = ((~(%t_wim | %wim)) & valid_wim_bits)
  195. */
  196. rd %wim, %twin_tmp
  197. or %twin_tmp, %t_wim, %twin_tmp
  198. not %twin_tmp
  199. spnwin_patch3: and %twin_tmp, 0xff, %twin_tmp ! patched on 7win Sparcs
  200. st %twin_tmp, [%curptr + TI_UWINMASK]
  201. #define STACK_OFFSET (THREAD_SIZE - TRACEREG_SZ - STACKFRAME_SZ)
  202. sethi %hi(STACK_OFFSET), %sp
  203. or %sp, %lo(STACK_OFFSET), %sp
  204. add %curptr, %sp, %sp
  205. /* Restore the saved globals and build a pt_regs frame. */
  206. mov %saved_g5, %g5
  207. mov %saved_g6, %g6
  208. STORE_PT_ALL(sp, t_psr, t_pc, t_npc, g1)
  209. sethi %hi(STACK_OFFSET), %g6
  210. or %g6, %lo(STACK_OFFSET), %g6
  211. sub %sp, %g6, %g6 ! curptr
  212. /* Turn on traps and call c-code to deal with it. */
  213. wr %t_psr, PSR_ET, %psr
  214. nop
  215. call window_overflow_fault
  216. nop
  217. /* Return from trap if C-code actually fixes things, if it
  218. * doesn't then we never get this far as the process will
  219. * be given the look of death from Commander Peanut.
  220. */
  221. b ret_trap_entry
  222. clr %l6
  223. spwin_bad_ustack_from_kernel:
  224. /* LOCATION: Window to be saved */
  225. /* The kernel provoked a spill window trap, but the window we
  226. * need to save is a user one and the process has trashed its
  227. * stack pointer. We need to be quick, so we throw it into
  228. * a per-process window buffer until we can properly handle
  229. * this later on.
  230. */
  231. SAVE_BOLIXED_USER_STACK(curptr, glob_tmp)
  232. restore %g0, %g0, %g0
  233. /* LOCATION: Trap window */
  234. /* Restore globals, condition codes in the %psr and
  235. * return from trap. Note, restoring %g6 when returning
  236. * to kernel mode is not necessarily these days. ;-)
  237. */
  238. mov %saved_g5, %g5
  239. mov %saved_g6, %g6
  240. wr %t_psr, 0x0, %psr
  241. WRITE_PAUSE
  242. jmp %t_pc
  243. rett %t_npc
  244. /* Undefine the register macros which would only cause trouble
  245. * if used below. This helps find 'stupid' coding errors that
  246. * produce 'odd' behavior. The routines below are allowed to
  247. * make usage of glob_tmp and t_psr so we leave them defined.
  248. */
  249. #undef twin_tmp
  250. #undef curptr
  251. #undef t_pc
  252. #undef t_npc
  253. #undef t_wim
  254. #undef saved_g5
  255. #undef saved_g6
  256. /* Now come the per-architecture window overflow stack checking routines.
  257. * As noted above %curptr cannot be touched by this routine at all.
  258. */
  259. /* This is a generic SRMMU routine. As far as I know this
  260. * works for all current v8/srmmu implementations, we'll
  261. * see...
  262. */
  263. .globl spwin_srmmu_stackchk
  264. spwin_srmmu_stackchk:
  265. /* LOCATION: Window to be saved on the stack */
  266. /* Because of SMP concerns and speed we play a trick.
  267. * We disable fault traps in the MMU control register,
  268. * Execute the stores, then check the fault registers
  269. * to see what happens. I can hear Linus now
  270. * "disgusting... broken hardware...".
  271. *
  272. * But first, check to see if the users stack has ended
  273. * up in kernel vma, then we would succeed for the 'wrong'
  274. * reason... ;( Note that the 'sethi' below assumes the
  275. * kernel is page aligned, which should always be the case.
  276. */
  277. /* Check results of callers andcc %sp, 0x7, %g0 */
  278. bne spwin_user_stack_is_bolixed
  279. sethi %hi(PAGE_OFFSET), %glob_tmp
  280. cmp %glob_tmp, %sp
  281. bleu spwin_user_stack_is_bolixed
  282. mov AC_M_SFSR, %glob_tmp
  283. /* Clear the fault status and turn on the no_fault bit. */
  284. LEON_PI(lda [%glob_tmp] ASI_LEON_MMUREGS, %g0) ! eat SFSR
  285. SUN_PI_(lda [%glob_tmp] ASI_M_MMUREGS, %g0) ! eat SFSR
  286. LEON_PI(lda [%g0] ASI_LEON_MMUREGS, %glob_tmp) ! read MMU control
  287. SUN_PI_(lda [%g0] ASI_M_MMUREGS, %glob_tmp) ! read MMU control
  288. or %glob_tmp, 0x2, %glob_tmp ! or in no_fault bit
  289. LEON_PI(sta %glob_tmp, [%g0] ASI_LEON_MMUREGS) ! set it
  290. SUN_PI_(sta %glob_tmp, [%g0] ASI_M_MMUREGS) ! set it
  291. /* Dump the registers and cross fingers. */
  292. STORE_WINDOW(sp)
  293. /* Clear the no_fault bit and check the status. */
  294. andn %glob_tmp, 0x2, %glob_tmp
  295. LEON_PI(sta %glob_tmp, [%g0] ASI_LEON_MMUREGS)
  296. SUN_PI_(sta %glob_tmp, [%g0] ASI_M_MMUREGS)
  297. mov AC_M_SFAR, %glob_tmp
  298. LEON_PI(lda [%glob_tmp] ASI_LEON_MMUREGS, %g0)
  299. SUN_PI_(lda [%glob_tmp] ASI_M_MMUREGS, %g0)
  300. mov AC_M_SFSR, %glob_tmp
  301. LEON_PI(lda [%glob_tmp] ASI_LEON_MMUREGS, %glob_tmp)
  302. SUN_PI_(lda [%glob_tmp] ASI_M_MMUREGS, %glob_tmp)
  303. andcc %glob_tmp, 0x2, %g0 ! did we fault?
  304. be,a spwin_finish_up + 0x4 ! cool beans, success
  305. restore %g0, %g0, %g0
  306. rd %psr, %glob_tmp
  307. b spwin_user_stack_is_bolixed + 0x4 ! we faulted, ugh
  308. nop