wof.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. .globl spwin_mmu_patchme
  146. spwin_mmu_patchme: b spwin_sun4c_stackchk
  147. andcc %sp, 0x7, %g0
  148. spwin_good_ustack:
  149. /* LOCATION: Window to be saved */
  150. /* The users stack is ok and we can safely save it at
  151. * %sp.
  152. */
  153. STORE_WINDOW(sp)
  154. spwin_finish_up:
  155. restore %g0, %g0, %g0 /* Back to trap window. */
  156. /* LOCATION: Trap window */
  157. /* We have spilled successfully, and we have properly stored
  158. * the appropriate window onto the stack.
  159. */
  160. /* Restore saved globals */
  161. mov %saved_g5, %g5
  162. mov %saved_g6, %g6
  163. wr %t_psr, 0x0, %psr
  164. WRITE_PAUSE
  165. jmp %t_pc
  166. rett %t_npc
  167. spwin_user_stack_is_bolixed:
  168. /* LOCATION: Window to be saved */
  169. /* Wheee, user has trashed his/her stack. We have to decide
  170. * how to proceed based upon whether we came from kernel mode
  171. * or not. If we came from kernel mode, toss the window into
  172. * a special buffer and proceed, the kernel _needs_ a window
  173. * and we could be in an interrupt handler so timing is crucial.
  174. * If we came from user land we build a full stack frame and call
  175. * c-code to gun down the process.
  176. */
  177. rd %psr, %glob_tmp
  178. andcc %glob_tmp, PSR_PS, %g0
  179. bne spwin_bad_ustack_from_kernel
  180. nop
  181. /* Oh well, throw this one window into the per-task window
  182. * buffer, the first one.
  183. */
  184. st %sp, [%curptr + TI_RWIN_SPTRS]
  185. STORE_WINDOW(curptr + TI_REG_WINDOW)
  186. restore %g0, %g0, %g0
  187. /* LOCATION: Trap Window */
  188. /* Back in the trap window, update winbuffer save count. */
  189. mov 1, %twin_tmp
  190. st %twin_tmp, [%curptr + TI_W_SAVED]
  191. /* Compute new user window mask. What we are basically
  192. * doing is taking two windows, the invalid one at trap
  193. * time and the one we attempted to throw onto the users
  194. * stack, and saying that everything else is an ok user
  195. * window. umask = ((~(%t_wim | %wim)) & valid_wim_bits)
  196. */
  197. rd %wim, %twin_tmp
  198. or %twin_tmp, %t_wim, %twin_tmp
  199. not %twin_tmp
  200. spnwin_patch3: and %twin_tmp, 0xff, %twin_tmp ! patched on 7win Sparcs
  201. st %twin_tmp, [%curptr + TI_UWINMASK]
  202. #define STACK_OFFSET (THREAD_SIZE - TRACEREG_SZ - STACKFRAME_SZ)
  203. sethi %hi(STACK_OFFSET), %sp
  204. or %sp, %lo(STACK_OFFSET), %sp
  205. add %curptr, %sp, %sp
  206. /* Restore the saved globals and build a pt_regs frame. */
  207. mov %saved_g5, %g5
  208. mov %saved_g6, %g6
  209. STORE_PT_ALL(sp, t_psr, t_pc, t_npc, g1)
  210. sethi %hi(STACK_OFFSET), %g6
  211. or %g6, %lo(STACK_OFFSET), %g6
  212. sub %sp, %g6, %g6 ! curptr
  213. /* Turn on traps and call c-code to deal with it. */
  214. wr %t_psr, PSR_ET, %psr
  215. nop
  216. call window_overflow_fault
  217. nop
  218. /* Return from trap if C-code actually fixes things, if it
  219. * doesn't then we never get this far as the process will
  220. * be given the look of death from Commander Peanut.
  221. */
  222. b ret_trap_entry
  223. clr %l6
  224. spwin_bad_ustack_from_kernel:
  225. /* LOCATION: Window to be saved */
  226. /* The kernel provoked a spill window trap, but the window we
  227. * need to save is a user one and the process has trashed its
  228. * stack pointer. We need to be quick, so we throw it into
  229. * a per-process window buffer until we can properly handle
  230. * this later on.
  231. */
  232. SAVE_BOLIXED_USER_STACK(curptr, glob_tmp)
  233. restore %g0, %g0, %g0
  234. /* LOCATION: Trap window */
  235. /* Restore globals, condition codes in the %psr and
  236. * return from trap. Note, restoring %g6 when returning
  237. * to kernel mode is not necessarily these days. ;-)
  238. */
  239. mov %saved_g5, %g5
  240. mov %saved_g6, %g6
  241. wr %t_psr, 0x0, %psr
  242. WRITE_PAUSE
  243. jmp %t_pc
  244. rett %t_npc
  245. /* Undefine the register macros which would only cause trouble
  246. * if used below. This helps find 'stupid' coding errors that
  247. * produce 'odd' behavior. The routines below are allowed to
  248. * make usage of glob_tmp and t_psr so we leave them defined.
  249. */
  250. #undef twin_tmp
  251. #undef curptr
  252. #undef t_pc
  253. #undef t_npc
  254. #undef t_wim
  255. #undef saved_g5
  256. #undef saved_g6
  257. /* Now come the per-architecture window overflow stack checking routines.
  258. * As noted above %curptr cannot be touched by this routine at all.
  259. */
  260. spwin_sun4c_stackchk:
  261. /* LOCATION: Window to be saved on the stack */
  262. /* See if the stack is in the address space hole but first,
  263. * check results of callers andcc %sp, 0x7, %g0
  264. */
  265. be 1f
  266. sra %sp, 29, %glob_tmp
  267. rd %psr, %glob_tmp
  268. b spwin_user_stack_is_bolixed + 0x4
  269. nop
  270. 1:
  271. add %glob_tmp, 0x1, %glob_tmp
  272. andncc %glob_tmp, 0x1, %g0
  273. be 1f
  274. and %sp, 0xfff, %glob_tmp ! delay slot
  275. rd %psr, %glob_tmp
  276. b spwin_user_stack_is_bolixed + 0x4
  277. nop
  278. /* See if our dump area will be on more than one
  279. * page.
  280. */
  281. 1:
  282. add %glob_tmp, 0x38, %glob_tmp
  283. andncc %glob_tmp, 0xff8, %g0
  284. be spwin_sun4c_onepage ! only one page to check
  285. lda [%sp] ASI_PTE, %glob_tmp ! have to check first page anyways
  286. spwin_sun4c_twopages:
  287. /* Is first page ok permission wise? */
  288. srl %glob_tmp, 29, %glob_tmp
  289. cmp %glob_tmp, 0x6
  290. be 1f
  291. add %sp, 0x38, %glob_tmp /* Is second page in vma hole? */
  292. rd %psr, %glob_tmp
  293. b spwin_user_stack_is_bolixed + 0x4
  294. nop
  295. 1:
  296. sra %glob_tmp, 29, %glob_tmp
  297. add %glob_tmp, 0x1, %glob_tmp
  298. andncc %glob_tmp, 0x1, %g0
  299. be 1f
  300. add %sp, 0x38, %glob_tmp
  301. rd %psr, %glob_tmp
  302. b spwin_user_stack_is_bolixed + 0x4
  303. nop
  304. 1:
  305. lda [%glob_tmp] ASI_PTE, %glob_tmp
  306. spwin_sun4c_onepage:
  307. srl %glob_tmp, 29, %glob_tmp
  308. cmp %glob_tmp, 0x6 ! can user write to it?
  309. be spwin_good_ustack ! success
  310. nop
  311. rd %psr, %glob_tmp
  312. b spwin_user_stack_is_bolixed + 0x4
  313. nop
  314. /* This is a generic SRMMU routine. As far as I know this
  315. * works for all current v8/srmmu implementations, we'll
  316. * see...
  317. */
  318. .globl spwin_srmmu_stackchk
  319. spwin_srmmu_stackchk:
  320. /* LOCATION: Window to be saved on the stack */
  321. /* Because of SMP concerns and speed we play a trick.
  322. * We disable fault traps in the MMU control register,
  323. * Execute the stores, then check the fault registers
  324. * to see what happens. I can hear Linus now
  325. * "disgusting... broken hardware...".
  326. *
  327. * But first, check to see if the users stack has ended
  328. * up in kernel vma, then we would succeed for the 'wrong'
  329. * reason... ;( Note that the 'sethi' below assumes the
  330. * kernel is page aligned, which should always be the case.
  331. */
  332. /* Check results of callers andcc %sp, 0x7, %g0 */
  333. bne spwin_user_stack_is_bolixed
  334. sethi %hi(PAGE_OFFSET), %glob_tmp
  335. cmp %glob_tmp, %sp
  336. bleu spwin_user_stack_is_bolixed
  337. mov AC_M_SFSR, %glob_tmp
  338. /* Clear the fault status and turn on the no_fault bit. */
  339. lda [%glob_tmp] ASI_M_MMUREGS, %g0 ! eat SFSR
  340. lda [%g0] ASI_M_MMUREGS, %glob_tmp ! read MMU control
  341. or %glob_tmp, 0x2, %glob_tmp ! or in no_fault bit
  342. sta %glob_tmp, [%g0] ASI_M_MMUREGS ! set it
  343. /* Dump the registers and cross fingers. */
  344. STORE_WINDOW(sp)
  345. /* Clear the no_fault bit and check the status. */
  346. andn %glob_tmp, 0x2, %glob_tmp
  347. sta %glob_tmp, [%g0] ASI_M_MMUREGS
  348. mov AC_M_SFAR, %glob_tmp
  349. lda [%glob_tmp] ASI_M_MMUREGS, %g0
  350. mov AC_M_SFSR, %glob_tmp
  351. lda [%glob_tmp] ASI_M_MMUREGS, %glob_tmp
  352. andcc %glob_tmp, 0x2, %g0 ! did we fault?
  353. be,a spwin_finish_up + 0x4 ! cool beans, success
  354. restore %g0, %g0, %g0
  355. rd %psr, %glob_tmp
  356. b spwin_user_stack_is_bolixed + 0x4 ! we faulted, ugh
  357. nop