winfixup.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. #error IMMU TSB usage must be fixed
  80. mov TSB_REG, %g1
  81. ldxa [%g1] ASI_IMMU, %g5
  82. #endif
  83. /* This is the same as below, except we handle this a bit special
  84. * since we must preserve %l5 and %l6, see comment above.
  85. */
  86. call do_sparc64_fault
  87. add %sp, PTREGS_OFF, %o0
  88. ba,pt %xcc, rtrap
  89. nop ! yes, nop is correct
  90. /* Be very careful about usage of the alternate globals here.
  91. * You cannot touch %g4/%g5 as that has the fault information
  92. * should this be from usermode. Also be careful for the case
  93. * where we get here from the save instruction in etrap.S when
  94. * coming from either user or kernel (does not matter which, it
  95. * is the same problem in both cases). Essentially this means
  96. * do not touch %g7 or %g2 so we handle the two cases fine.
  97. */
  98. spill_fixup:
  99. ldx [%g6 + TI_FLAGS], %g1
  100. andcc %g1, _TIF_32BIT, %g0
  101. ldub [%g6 + TI_WSAVED], %g1
  102. sll %g1, 3, %g3
  103. add %g6, %g3, %g3
  104. stx %sp, [%g3 + TI_RWIN_SPTRS]
  105. sll %g1, 7, %g3
  106. bne,pt %xcc, 1f
  107. add %g6, %g3, %g3
  108. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  109. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  110. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  111. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  112. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  113. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  114. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  115. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  116. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  117. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  118. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  119. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  120. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  121. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  122. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  123. b,pt %xcc, 2f
  124. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  125. 1: stw %l0, [%g3 + TI_REG_WINDOW + 0x00]
  126. stw %l1, [%g3 + TI_REG_WINDOW + 0x04]
  127. stw %l2, [%g3 + TI_REG_WINDOW + 0x08]
  128. stw %l3, [%g3 + TI_REG_WINDOW + 0x0c]
  129. stw %l4, [%g3 + TI_REG_WINDOW + 0x10]
  130. stw %l5, [%g3 + TI_REG_WINDOW + 0x14]
  131. stw %l6, [%g3 + TI_REG_WINDOW + 0x18]
  132. stw %l7, [%g3 + TI_REG_WINDOW + 0x1c]
  133. stw %i0, [%g3 + TI_REG_WINDOW + 0x20]
  134. stw %i1, [%g3 + TI_REG_WINDOW + 0x24]
  135. stw %i2, [%g3 + TI_REG_WINDOW + 0x28]
  136. stw %i3, [%g3 + TI_REG_WINDOW + 0x2c]
  137. stw %i4, [%g3 + TI_REG_WINDOW + 0x30]
  138. stw %i5, [%g3 + TI_REG_WINDOW + 0x34]
  139. stw %i6, [%g3 + TI_REG_WINDOW + 0x38]
  140. stw %i7, [%g3 + TI_REG_WINDOW + 0x3c]
  141. 2: add %g1, 1, %g1
  142. stb %g1, [%g6 + TI_WSAVED]
  143. rdpr %tstate, %g1
  144. andcc %g1, TSTATE_PRIV, %g0
  145. saved
  146. and %g1, TSTATE_CWP, %g1
  147. be,pn %xcc, window_scheisse_from_user_common
  148. mov FAULT_CODE_WRITE | FAULT_CODE_DTLB | FAULT_CODE_WINFIXUP, %g4
  149. retry
  150. window_scheisse_from_user_common:
  151. stb %g4, [%g6 + TI_FAULT_CODE]
  152. stx %g5, [%g6 + TI_FAULT_ADDR]
  153. wrpr %g1, %cwp
  154. ba,pt %xcc, etrap
  155. rd %pc, %g7
  156. call do_sparc64_fault
  157. add %sp, PTREGS_OFF, %o0
  158. ba,a,pt %xcc, rtrap_clr_l6
  159. .globl winfix_mna, fill_fixup_mna, spill_fixup_mna
  160. winfix_mna:
  161. andn %g3, 0x7f, %g3
  162. add %g3, 0x78, %g3
  163. wrpr %g3, %tnpc
  164. done
  165. fill_fixup_mna:
  166. rdpr %tstate, %g1
  167. andcc %g1, TSTATE_PRIV, %g0
  168. be,pt %xcc, window_mna_from_user_common
  169. and %g1, TSTATE_CWP, %g1
  170. /* Please, see fill_fixup commentary about why we must preserve
  171. * %l5 and %l6 to preserve absolute correct semantics.
  172. */
  173. rdpr %wstate, %g2 ! Grab user mode wstate.
  174. wrpr %g1, %cwp ! Get into the right window.
  175. sll %g2, 3, %g2 ! NORMAL-->OTHER
  176. wrpr %g0, 0x0, %canrestore ! Standard etrap stuff.
  177. wrpr %g2, 0x0, %wstate ! This must be consistent.
  178. wrpr %g0, 0x0, %otherwin ! We know this.
  179. call set_pcontext ! Change contexts...
  180. nop
  181. rdpr %pstate, %l1 ! Prepare to change globals.
  182. mov %g4, %o2 ! Setup args for
  183. mov %g5, %o1 ! final call to mem_address_unaligned.
  184. andn %l1, PSTATE_MM, %l1 ! We want to be in RMO
  185. mov %g6, %o7 ! Stash away current.
  186. wrpr %g0, 0x0, %tl ! Out of trap levels.
  187. wrpr %l1, (PSTATE_IE | PSTATE_AG | PSTATE_RMO), %pstate
  188. mov %o7, %g6 ! Get current back.
  189. ldx [%g6 + TI_TASK], %g4 ! Finish it.
  190. #ifdef CONFIG_SMP
  191. #error IMMU TSB usage must be fixed
  192. mov TSB_REG, %g1
  193. ldxa [%g1] ASI_IMMU, %g5
  194. #endif
  195. call mem_address_unaligned
  196. add %sp, PTREGS_OFF, %o0
  197. b,pt %xcc, rtrap
  198. nop ! yes, the nop is correct
  199. spill_fixup_mna:
  200. ldx [%g6 + TI_FLAGS], %g1
  201. andcc %g1, _TIF_32BIT, %g0
  202. ldub [%g6 + TI_WSAVED], %g1
  203. sll %g1, 3, %g3
  204. add %g6, %g3, %g3
  205. stx %sp, [%g3 + TI_RWIN_SPTRS]
  206. sll %g1, 7, %g3
  207. bne,pt %xcc, 1f
  208. add %g6, %g3, %g3
  209. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  210. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  211. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  212. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  213. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  214. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  215. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  216. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  217. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  218. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  219. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  220. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  221. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  222. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  223. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  224. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  225. b,pt %xcc, 2f
  226. add %g1, 1, %g1
  227. 1: std %l0, [%g3 + TI_REG_WINDOW + 0x00]
  228. std %l2, [%g3 + TI_REG_WINDOW + 0x08]
  229. std %l4, [%g3 + TI_REG_WINDOW + 0x10]
  230. std %l6, [%g3 + TI_REG_WINDOW + 0x18]
  231. std %i0, [%g3 + TI_REG_WINDOW + 0x20]
  232. std %i2, [%g3 + TI_REG_WINDOW + 0x28]
  233. std %i4, [%g3 + TI_REG_WINDOW + 0x30]
  234. std %i6, [%g3 + TI_REG_WINDOW + 0x38]
  235. add %g1, 1, %g1
  236. 2: stb %g1, [%g6 + TI_WSAVED]
  237. rdpr %tstate, %g1
  238. andcc %g1, TSTATE_PRIV, %g0
  239. saved
  240. be,pn %xcc, window_mna_from_user_common
  241. and %g1, TSTATE_CWP, %g1
  242. retry
  243. window_mna_from_user_common:
  244. wrpr %g1, %cwp
  245. sethi %hi(109f), %g7
  246. ba,pt %xcc, etrap
  247. 109: or %g7, %lo(109b), %g7
  248. mov %l4, %o2
  249. mov %l5, %o1
  250. call mem_address_unaligned
  251. add %sp, PTREGS_OFF, %o0
  252. ba,pt %xcc, rtrap
  253. clr %l6
  254. .globl winfix_dax, fill_fixup_dax, spill_fixup_dax
  255. winfix_dax:
  256. andn %g3, 0x7f, %g3
  257. add %g3, 0x74, %g3
  258. wrpr %g3, %tnpc
  259. done
  260. fill_fixup_dax:
  261. rdpr %tstate, %g1
  262. andcc %g1, TSTATE_PRIV, %g0
  263. be,pt %xcc, window_dax_from_user_common
  264. and %g1, TSTATE_CWP, %g1
  265. /* Please, see fill_fixup commentary about why we must preserve
  266. * %l5 and %l6 to preserve absolute correct semantics.
  267. */
  268. rdpr %wstate, %g2 ! Grab user mode wstate.
  269. wrpr %g1, %cwp ! Get into the right window.
  270. sll %g2, 3, %g2 ! NORMAL-->OTHER
  271. wrpr %g0, 0x0, %canrestore ! Standard etrap stuff.
  272. wrpr %g2, 0x0, %wstate ! This must be consistent.
  273. wrpr %g0, 0x0, %otherwin ! We know this.
  274. call set_pcontext ! Change contexts...
  275. nop
  276. rdpr %pstate, %l1 ! Prepare to change globals.
  277. mov %g4, %o1 ! Setup args for
  278. mov %g5, %o2 ! final call to spitfire_data_access_exception.
  279. andn %l1, PSTATE_MM, %l1 ! We want to be in RMO
  280. mov %g6, %o7 ! Stash away current.
  281. wrpr %g0, 0x0, %tl ! Out of trap levels.
  282. wrpr %l1, (PSTATE_IE | PSTATE_AG | PSTATE_RMO), %pstate
  283. mov %o7, %g6 ! Get current back.
  284. ldx [%g6 + TI_TASK], %g4 ! Finish it.
  285. #ifdef CONFIG_SMP
  286. #error IMMU TSB usage must be fixed
  287. mov TSB_REG, %g1
  288. ldxa [%g1] ASI_IMMU, %g5
  289. #endif
  290. call spitfire_data_access_exception
  291. add %sp, PTREGS_OFF, %o0
  292. b,pt %xcc, rtrap
  293. nop ! yes, the nop is correct
  294. spill_fixup_dax:
  295. ldx [%g6 + TI_FLAGS], %g1
  296. andcc %g1, _TIF_32BIT, %g0
  297. ldub [%g6 + TI_WSAVED], %g1
  298. sll %g1, 3, %g3
  299. add %g6, %g3, %g3
  300. stx %sp, [%g3 + TI_RWIN_SPTRS]
  301. sll %g1, 7, %g3
  302. bne,pt %xcc, 1f
  303. add %g6, %g3, %g3
  304. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  305. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  306. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  307. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  308. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  309. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  310. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  311. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  312. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  313. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  314. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  315. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  316. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  317. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  318. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  319. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  320. b,pt %xcc, 2f
  321. add %g1, 1, %g1
  322. 1: std %l0, [%g3 + TI_REG_WINDOW + 0x00]
  323. std %l2, [%g3 + TI_REG_WINDOW + 0x08]
  324. std %l4, [%g3 + TI_REG_WINDOW + 0x10]
  325. std %l6, [%g3 + TI_REG_WINDOW + 0x18]
  326. std %i0, [%g3 + TI_REG_WINDOW + 0x20]
  327. std %i2, [%g3 + TI_REG_WINDOW + 0x28]
  328. std %i4, [%g3 + TI_REG_WINDOW + 0x30]
  329. std %i6, [%g3 + TI_REG_WINDOW + 0x38]
  330. add %g1, 1, %g1
  331. 2: stb %g1, [%g6 + TI_WSAVED]
  332. rdpr %tstate, %g1
  333. andcc %g1, TSTATE_PRIV, %g0
  334. saved
  335. be,pn %xcc, window_dax_from_user_common
  336. and %g1, TSTATE_CWP, %g1
  337. retry
  338. window_dax_from_user_common:
  339. wrpr %g1, %cwp
  340. sethi %hi(109f), %g7
  341. ba,pt %xcc, etrap
  342. 109: or %g7, %lo(109b), %g7
  343. mov %l4, %o1
  344. mov %l5, %o2
  345. call spitfire_data_access_exception
  346. add %sp, PTREGS_OFF, %o0
  347. ba,pt %xcc, rtrap
  348. clr %l6