winfixup.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* $Id: winfixup.S,v 1.30 2002/02/09 19:49:30 davem Exp $
  2. *
  3. * winfixup.S: Handle cases where user stack pointer is found to be bogus.
  4. *
  5. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  6. */
  7. #include <asm/asi.h>
  8. #include <asm/head.h>
  9. #include <asm/page.h>
  10. #include <asm/ptrace.h>
  11. #include <asm/processor.h>
  12. #include <asm/spitfire.h>
  13. #include <asm/thread_info.h>
  14. .text
  15. set_pcontext:
  16. sethi %hi(sparc64_kern_pri_context), %l1
  17. ldx [%l1 + %lo(sparc64_kern_pri_context)], %l1
  18. mov PRIMARY_CONTEXT, %g1
  19. stxa %l1, [%g1] ASI_DMMU
  20. flush %g6
  21. retl
  22. nop
  23. .align 32
  24. /* Here are the rules, pay attention.
  25. *
  26. * The kernel is disallowed from touching user space while
  27. * the trap level is greater than zero, except for from within
  28. * the window spill/fill handlers. This must be followed
  29. * so that we can easily detect the case where we tried to
  30. * spill/fill with a bogus (or unmapped) user stack pointer.
  31. *
  32. * These are layed out in a special way for cache reasons,
  33. * don't touch...
  34. */
  35. .globl fill_fixup, spill_fixup
  36. fill_fixup:
  37. rdpr %tstate, %g1
  38. andcc %g1, TSTATE_PRIV, %g0
  39. or %g4, FAULT_CODE_WINFIXUP, %g4
  40. be,pt %xcc, window_scheisse_from_user_common
  41. and %g1, TSTATE_CWP, %g1
  42. /* This is the extremely complex case, but it does happen from
  43. * time to time if things are just right. Essentially the restore
  44. * done in rtrap right before going back to user mode, with tl=1
  45. * and that levels trap stack registers all setup, took a fill trap,
  46. * the user stack was not mapped in the tlb, and tlb miss occurred,
  47. * the pte found was not valid, and a simple ref bit watch update
  48. * could not satisfy the miss, so we got here.
  49. *
  50. * We must carefully unwind the state so we get back to tl=0, preserve
  51. * all the register values we were going to give to the user. Luckily
  52. * most things are where they need to be, we also have the address
  53. * which triggered the fault handy as well.
  54. *
  55. * Also note that we must preserve %l5 and %l6. If the user was
  56. * returning from a system call, we must make it look this way
  57. * after we process the fill fault on the users stack.
  58. *
  59. * First, get into the window where the original restore was executed.
  60. */
  61. rdpr %wstate, %g2 ! Grab user mode wstate.
  62. wrpr %g1, %cwp ! Get into the right window.
  63. sll %g2, 3, %g2 ! NORMAL-->OTHER
  64. wrpr %g0, 0x0, %canrestore ! Standard etrap stuff.
  65. wrpr %g2, 0x0, %wstate ! This must be consistent.
  66. wrpr %g0, 0x0, %otherwin ! We know this.
  67. call set_pcontext ! Change contexts...
  68. nop
  69. rdpr %pstate, %l1 ! Prepare to change globals.
  70. mov %g6, %o7 ! Get current.
  71. andn %l1, PSTATE_MM, %l1 ! We want to be in RMO
  72. stb %g4, [%g6 + TI_FAULT_CODE]
  73. stx %g5, [%g6 + TI_FAULT_ADDR]
  74. wrpr %g0, 0x0, %tl ! Out of trap levels.
  75. wrpr %l1, (PSTATE_IE | PSTATE_AG | PSTATE_RMO), %pstate
  76. mov %o7, %g6
  77. ldx [%g6 + TI_TASK], %g4
  78. #ifdef CONFIG_SMP
  79. mov TSB_REG, %g1
  80. ldxa [%g1] ASI_IMMU, %g5
  81. #endif
  82. /* This is the same as below, except we handle this a bit special
  83. * since we must preserve %l5 and %l6, see comment above.
  84. */
  85. call do_sparc64_fault
  86. add %sp, PTREGS_OFF, %o0
  87. ba,pt %xcc, rtrap
  88. nop ! yes, nop is correct
  89. /* Be very careful about usage of the alternate globals here.
  90. * You cannot touch %g4/%g5 as that has the fault information
  91. * should this be from usermode. Also be careful for the case
  92. * where we get here from the save instruction in etrap.S when
  93. * coming from either user or kernel (does not matter which, it
  94. * is the same problem in both cases). Essentially this means
  95. * do not touch %g7 or %g2 so we handle the two cases fine.
  96. */
  97. spill_fixup:
  98. ldx [%g6 + TI_FLAGS], %g1
  99. andcc %g1, _TIF_32BIT, %g0
  100. ldub [%g6 + TI_WSAVED], %g1
  101. sll %g1, 3, %g3
  102. add %g6, %g3, %g3
  103. stx %sp, [%g3 + TI_RWIN_SPTRS]
  104. sll %g1, 7, %g3
  105. bne,pt %xcc, 1f
  106. add %g6, %g3, %g3
  107. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  108. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  109. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  110. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  111. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  112. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  113. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  114. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  115. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  116. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  117. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  118. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  119. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  120. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  121. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  122. b,pt %xcc, 2f
  123. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  124. 1: stw %l0, [%g3 + TI_REG_WINDOW + 0x00]
  125. stw %l1, [%g3 + TI_REG_WINDOW + 0x04]
  126. stw %l2, [%g3 + TI_REG_WINDOW + 0x08]
  127. stw %l3, [%g3 + TI_REG_WINDOW + 0x0c]
  128. stw %l4, [%g3 + TI_REG_WINDOW + 0x10]
  129. stw %l5, [%g3 + TI_REG_WINDOW + 0x14]
  130. stw %l6, [%g3 + TI_REG_WINDOW + 0x18]
  131. stw %l7, [%g3 + TI_REG_WINDOW + 0x1c]
  132. stw %i0, [%g3 + TI_REG_WINDOW + 0x20]
  133. stw %i1, [%g3 + TI_REG_WINDOW + 0x24]
  134. stw %i2, [%g3 + TI_REG_WINDOW + 0x28]
  135. stw %i3, [%g3 + TI_REG_WINDOW + 0x2c]
  136. stw %i4, [%g3 + TI_REG_WINDOW + 0x30]
  137. stw %i5, [%g3 + TI_REG_WINDOW + 0x34]
  138. stw %i6, [%g3 + TI_REG_WINDOW + 0x38]
  139. stw %i7, [%g3 + TI_REG_WINDOW + 0x3c]
  140. 2: add %g1, 1, %g1
  141. stb %g1, [%g6 + TI_WSAVED]
  142. rdpr %tstate, %g1
  143. andcc %g1, TSTATE_PRIV, %g0
  144. saved
  145. and %g1, TSTATE_CWP, %g1
  146. be,pn %xcc, window_scheisse_from_user_common
  147. mov FAULT_CODE_WRITE | FAULT_CODE_DTLB | FAULT_CODE_WINFIXUP, %g4
  148. retry
  149. window_scheisse_from_user_common:
  150. stb %g4, [%g6 + TI_FAULT_CODE]
  151. stx %g5, [%g6 + TI_FAULT_ADDR]
  152. wrpr %g1, %cwp
  153. ba,pt %xcc, etrap
  154. rd %pc, %g7
  155. call do_sparc64_fault
  156. add %sp, PTREGS_OFF, %o0
  157. ba,a,pt %xcc, rtrap_clr_l6
  158. .globl winfix_mna, fill_fixup_mna, spill_fixup_mna
  159. winfix_mna:
  160. andn %g3, 0x7f, %g3
  161. add %g3, 0x78, %g3
  162. wrpr %g3, %tnpc
  163. done
  164. fill_fixup_mna:
  165. rdpr %tstate, %g1
  166. andcc %g1, TSTATE_PRIV, %g0
  167. be,pt %xcc, window_mna_from_user_common
  168. and %g1, TSTATE_CWP, %g1
  169. /* Please, see fill_fixup commentary about why we must preserve
  170. * %l5 and %l6 to preserve absolute correct semantics.
  171. */
  172. rdpr %wstate, %g2 ! Grab user mode wstate.
  173. wrpr %g1, %cwp ! Get into the right window.
  174. sll %g2, 3, %g2 ! NORMAL-->OTHER
  175. wrpr %g0, 0x0, %canrestore ! Standard etrap stuff.
  176. wrpr %g2, 0x0, %wstate ! This must be consistent.
  177. wrpr %g0, 0x0, %otherwin ! We know this.
  178. call set_pcontext ! Change contexts...
  179. nop
  180. rdpr %pstate, %l1 ! Prepare to change globals.
  181. mov %g4, %o2 ! Setup args for
  182. mov %g5, %o1 ! final call to mem_address_unaligned.
  183. andn %l1, PSTATE_MM, %l1 ! We want to be in RMO
  184. mov %g6, %o7 ! Stash away current.
  185. wrpr %g0, 0x0, %tl ! Out of trap levels.
  186. wrpr %l1, (PSTATE_IE | PSTATE_AG | PSTATE_RMO), %pstate
  187. mov %o7, %g6 ! Get current back.
  188. ldx [%g6 + TI_TASK], %g4 ! Finish it.
  189. #ifdef CONFIG_SMP
  190. mov TSB_REG, %g1
  191. ldxa [%g1] ASI_IMMU, %g5
  192. #endif
  193. call mem_address_unaligned
  194. add %sp, PTREGS_OFF, %o0
  195. b,pt %xcc, rtrap
  196. nop ! yes, the nop is correct
  197. spill_fixup_mna:
  198. ldx [%g6 + TI_FLAGS], %g1
  199. andcc %g1, _TIF_32BIT, %g0
  200. ldub [%g6 + TI_WSAVED], %g1
  201. sll %g1, 3, %g3
  202. add %g6, %g3, %g3
  203. stx %sp, [%g3 + TI_RWIN_SPTRS]
  204. sll %g1, 7, %g3
  205. bne,pt %xcc, 1f
  206. add %g6, %g3, %g3
  207. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  208. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  209. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  210. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  211. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  212. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  213. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  214. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  215. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  216. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  217. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  218. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  219. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  220. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  221. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  222. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  223. b,pt %xcc, 2f
  224. add %g1, 1, %g1
  225. 1: std %l0, [%g3 + TI_REG_WINDOW + 0x00]
  226. std %l2, [%g3 + TI_REG_WINDOW + 0x08]
  227. std %l4, [%g3 + TI_REG_WINDOW + 0x10]
  228. std %l6, [%g3 + TI_REG_WINDOW + 0x18]
  229. std %i0, [%g3 + TI_REG_WINDOW + 0x20]
  230. std %i2, [%g3 + TI_REG_WINDOW + 0x28]
  231. std %i4, [%g3 + TI_REG_WINDOW + 0x30]
  232. std %i6, [%g3 + TI_REG_WINDOW + 0x38]
  233. add %g1, 1, %g1
  234. 2: stb %g1, [%g6 + TI_WSAVED]
  235. rdpr %tstate, %g1
  236. andcc %g1, TSTATE_PRIV, %g0
  237. saved
  238. be,pn %xcc, window_mna_from_user_common
  239. and %g1, TSTATE_CWP, %g1
  240. retry
  241. window_mna_from_user_common:
  242. wrpr %g1, %cwp
  243. sethi %hi(109f), %g7
  244. ba,pt %xcc, etrap
  245. 109: or %g7, %lo(109b), %g7
  246. mov %l4, %o2
  247. mov %l5, %o1
  248. call mem_address_unaligned
  249. add %sp, PTREGS_OFF, %o0
  250. ba,pt %xcc, rtrap
  251. clr %l6
  252. /* These are only needed for 64-bit mode processes which
  253. * put their stack pointer into the VPTE area and there
  254. * happens to be a VPTE tlb entry mapped there during
  255. * a spill/fill trap to that stack frame.
  256. */
  257. .globl winfix_dax, fill_fixup_dax, spill_fixup_dax
  258. winfix_dax:
  259. andn %g3, 0x7f, %g3
  260. add %g3, 0x74, %g3
  261. wrpr %g3, %tnpc
  262. done
  263. fill_fixup_dax:
  264. rdpr %tstate, %g1
  265. andcc %g1, TSTATE_PRIV, %g0
  266. be,pt %xcc, window_dax_from_user_common
  267. and %g1, TSTATE_CWP, %g1
  268. /* Please, see fill_fixup commentary about why we must preserve
  269. * %l5 and %l6 to preserve absolute correct semantics.
  270. */
  271. rdpr %wstate, %g2 ! Grab user mode wstate.
  272. wrpr %g1, %cwp ! Get into the right window.
  273. sll %g2, 3, %g2 ! NORMAL-->OTHER
  274. wrpr %g0, 0x0, %canrestore ! Standard etrap stuff.
  275. wrpr %g2, 0x0, %wstate ! This must be consistent.
  276. wrpr %g0, 0x0, %otherwin ! We know this.
  277. call set_pcontext ! Change contexts...
  278. nop
  279. rdpr %pstate, %l1 ! Prepare to change globals.
  280. mov %g4, %o1 ! Setup args for
  281. mov %g5, %o2 ! final call to spitfire_data_access_exception.
  282. andn %l1, PSTATE_MM, %l1 ! We want to be in RMO
  283. mov %g6, %o7 ! Stash away current.
  284. wrpr %g0, 0x0, %tl ! Out of trap levels.
  285. wrpr %l1, (PSTATE_IE | PSTATE_AG | PSTATE_RMO), %pstate
  286. mov %o7, %g6 ! Get current back.
  287. ldx [%g6 + TI_TASK], %g4 ! Finish it.
  288. #ifdef CONFIG_SMP
  289. mov TSB_REG, %g1
  290. ldxa [%g1] ASI_IMMU, %g5
  291. #endif
  292. call spitfire_data_access_exception
  293. add %sp, PTREGS_OFF, %o0
  294. b,pt %xcc, rtrap
  295. nop ! yes, the nop is correct
  296. spill_fixup_dax:
  297. ldx [%g6 + TI_FLAGS], %g1
  298. andcc %g1, _TIF_32BIT, %g0
  299. ldub [%g6 + TI_WSAVED], %g1
  300. sll %g1, 3, %g3
  301. add %g6, %g3, %g3
  302. stx %sp, [%g3 + TI_RWIN_SPTRS]
  303. sll %g1, 7, %g3
  304. bne,pt %xcc, 1f
  305. add %g6, %g3, %g3
  306. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  307. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  308. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  309. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  310. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  311. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  312. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  313. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  314. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  315. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  316. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  317. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  318. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  319. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  320. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  321. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  322. b,pt %xcc, 2f
  323. add %g1, 1, %g1
  324. 1: std %l0, [%g3 + TI_REG_WINDOW + 0x00]
  325. std %l2, [%g3 + TI_REG_WINDOW + 0x08]
  326. std %l4, [%g3 + TI_REG_WINDOW + 0x10]
  327. std %l6, [%g3 + TI_REG_WINDOW + 0x18]
  328. std %i0, [%g3 + TI_REG_WINDOW + 0x20]
  329. std %i2, [%g3 + TI_REG_WINDOW + 0x28]
  330. std %i4, [%g3 + TI_REG_WINDOW + 0x30]
  331. std %i6, [%g3 + TI_REG_WINDOW + 0x38]
  332. add %g1, 1, %g1
  333. 2: stb %g1, [%g6 + TI_WSAVED]
  334. rdpr %tstate, %g1
  335. andcc %g1, TSTATE_PRIV, %g0
  336. saved
  337. be,pn %xcc, window_dax_from_user_common
  338. and %g1, TSTATE_CWP, %g1
  339. retry
  340. window_dax_from_user_common:
  341. wrpr %g1, %cwp
  342. sethi %hi(109f), %g7
  343. ba,pt %xcc, etrap
  344. 109: or %g7, %lo(109b), %g7
  345. mov %l4, %o1
  346. mov %l5, %o2
  347. call spitfire_data_access_exception
  348. add %sp, PTREGS_OFF, %o0
  349. ba,pt %xcc, rtrap
  350. clr %l6