winfixup.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. TRAP_LOAD_THREAD_REG
  38. rdpr %tstate, %g1
  39. andcc %g1, TSTATE_PRIV, %g0
  40. or %g4, FAULT_CODE_WINFIXUP, %g4
  41. be,pt %xcc, window_scheisse_from_user_common
  42. and %g1, TSTATE_CWP, %g1
  43. /* This is the extremely complex case, but it does happen from
  44. * time to time if things are just right. Essentially the restore
  45. * done in rtrap right before going back to user mode, with tl=1
  46. * and that levels trap stack registers all setup, took a fill trap,
  47. * the user stack was not mapped in the tlb, and tlb miss occurred,
  48. * the pte found was not valid, and a simple ref bit watch update
  49. * could not satisfy the miss, so we got here.
  50. *
  51. * We must carefully unwind the state so we get back to tl=0, preserve
  52. * all the register values we were going to give to the user. Luckily
  53. * most things are where they need to be, we also have the address
  54. * which triggered the fault handy as well.
  55. *
  56. * Also note that we must preserve %l5 and %l6. If the user was
  57. * returning from a system call, we must make it look this way
  58. * after we process the fill fault on the users stack.
  59. *
  60. * First, get into the window where the original restore was executed.
  61. */
  62. rdpr %wstate, %g2 ! Grab user mode wstate.
  63. wrpr %g1, %cwp ! Get into the right window.
  64. sll %g2, 3, %g2 ! NORMAL-->OTHER
  65. wrpr %g0, 0x0, %canrestore ! Standard etrap stuff.
  66. wrpr %g2, 0x0, %wstate ! This must be consistent.
  67. wrpr %g0, 0x0, %otherwin ! We know this.
  68. call set_pcontext ! Change contexts...
  69. nop
  70. rdpr %pstate, %l1 ! Prepare to change globals.
  71. mov %g6, %o7 ! Get current.
  72. andn %l1, PSTATE_MM, %l1 ! We want to be in RMO
  73. stb %g4, [%g6 + TI_FAULT_CODE]
  74. stx %g5, [%g6 + TI_FAULT_ADDR]
  75. wrpr %g0, 0x0, %tl ! Out of trap levels.
  76. wrpr %l1, (PSTATE_IE | PSTATE_AG | PSTATE_RMO), %pstate
  77. mov %o7, %g6
  78. ldx [%g6 + TI_TASK], %g4
  79. LOAD_PER_CPU_BASE(%g1, %g2)
  80. /* This is the same as below, except we handle this a bit special
  81. * since we must preserve %l5 and %l6, see comment above.
  82. */
  83. call do_sparc64_fault
  84. add %sp, PTREGS_OFF, %o0
  85. ba,pt %xcc, rtrap
  86. nop ! yes, nop is correct
  87. /* Be very careful about usage of the alternate globals here.
  88. * You cannot touch %g4/%g5 as that has the fault information
  89. * should this be from usermode. Also be careful for the case
  90. * where we get here from the save instruction in etrap.S when
  91. * coming from either user or kernel (does not matter which, it
  92. * is the same problem in both cases). Essentially this means
  93. * do not touch %g7 or %g2 so we handle the two cases fine.
  94. */
  95. spill_fixup:
  96. TRAP_LOAD_THREAD_REG
  97. ldx [%g6 + TI_FLAGS], %g1
  98. andcc %g1, _TIF_32BIT, %g0
  99. ldub [%g6 + TI_WSAVED], %g1
  100. sll %g1, 3, %g3
  101. add %g6, %g3, %g3
  102. stx %sp, [%g3 + TI_RWIN_SPTRS]
  103. sll %g1, 7, %g3
  104. bne,pt %xcc, 1f
  105. add %g6, %g3, %g3
  106. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  107. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  108. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  109. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  110. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  111. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  112. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  113. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  114. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  115. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  116. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  117. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  118. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  119. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  120. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  121. b,pt %xcc, 2f
  122. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  123. 1: stw %l0, [%g3 + TI_REG_WINDOW + 0x00]
  124. stw %l1, [%g3 + TI_REG_WINDOW + 0x04]
  125. stw %l2, [%g3 + TI_REG_WINDOW + 0x08]
  126. stw %l3, [%g3 + TI_REG_WINDOW + 0x0c]
  127. stw %l4, [%g3 + TI_REG_WINDOW + 0x10]
  128. stw %l5, [%g3 + TI_REG_WINDOW + 0x14]
  129. stw %l6, [%g3 + TI_REG_WINDOW + 0x18]
  130. stw %l7, [%g3 + TI_REG_WINDOW + 0x1c]
  131. stw %i0, [%g3 + TI_REG_WINDOW + 0x20]
  132. stw %i1, [%g3 + TI_REG_WINDOW + 0x24]
  133. stw %i2, [%g3 + TI_REG_WINDOW + 0x28]
  134. stw %i3, [%g3 + TI_REG_WINDOW + 0x2c]
  135. stw %i4, [%g3 + TI_REG_WINDOW + 0x30]
  136. stw %i5, [%g3 + TI_REG_WINDOW + 0x34]
  137. stw %i6, [%g3 + TI_REG_WINDOW + 0x38]
  138. stw %i7, [%g3 + TI_REG_WINDOW + 0x3c]
  139. 2: add %g1, 1, %g1
  140. stb %g1, [%g6 + TI_WSAVED]
  141. rdpr %tstate, %g1
  142. andcc %g1, TSTATE_PRIV, %g0
  143. saved
  144. and %g1, TSTATE_CWP, %g1
  145. be,pn %xcc, window_scheisse_from_user_common
  146. mov FAULT_CODE_WRITE | FAULT_CODE_DTLB | FAULT_CODE_WINFIXUP, %g4
  147. retry
  148. window_scheisse_from_user_common:
  149. stb %g4, [%g6 + TI_FAULT_CODE]
  150. stx %g5, [%g6 + TI_FAULT_ADDR]
  151. wrpr %g1, %cwp
  152. ba,pt %xcc, etrap
  153. rd %pc, %g7
  154. call do_sparc64_fault
  155. add %sp, PTREGS_OFF, %o0
  156. ba,a,pt %xcc, rtrap_clr_l6
  157. .globl winfix_mna, fill_fixup_mna, spill_fixup_mna
  158. winfix_mna:
  159. andn %g3, 0x7f, %g3
  160. add %g3, 0x78, %g3
  161. wrpr %g3, %tnpc
  162. done
  163. fill_fixup_mna:
  164. TRAP_LOAD_THREAD_REG
  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. LOAD_PER_CPU_BASE(%g1, %g2)
  190. call mem_address_unaligned
  191. add %sp, PTREGS_OFF, %o0
  192. b,pt %xcc, rtrap
  193. nop ! yes, the nop is correct
  194. spill_fixup_mna:
  195. TRAP_LOAD_THREAD_REG
  196. ldx [%g6 + TI_FLAGS], %g1
  197. andcc %g1, _TIF_32BIT, %g0
  198. ldub [%g6 + TI_WSAVED], %g1
  199. sll %g1, 3, %g3
  200. add %g6, %g3, %g3
  201. stx %sp, [%g3 + TI_RWIN_SPTRS]
  202. sll %g1, 7, %g3
  203. bne,pt %xcc, 1f
  204. add %g6, %g3, %g3
  205. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  206. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  207. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  208. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  209. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  210. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  211. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  212. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  213. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  214. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  215. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  216. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  217. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  218. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  219. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  220. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  221. b,pt %xcc, 2f
  222. add %g1, 1, %g1
  223. 1: std %l0, [%g3 + TI_REG_WINDOW + 0x00]
  224. std %l2, [%g3 + TI_REG_WINDOW + 0x08]
  225. std %l4, [%g3 + TI_REG_WINDOW + 0x10]
  226. std %l6, [%g3 + TI_REG_WINDOW + 0x18]
  227. std %i0, [%g3 + TI_REG_WINDOW + 0x20]
  228. std %i2, [%g3 + TI_REG_WINDOW + 0x28]
  229. std %i4, [%g3 + TI_REG_WINDOW + 0x30]
  230. std %i6, [%g3 + TI_REG_WINDOW + 0x38]
  231. add %g1, 1, %g1
  232. 2: stb %g1, [%g6 + TI_WSAVED]
  233. rdpr %tstate, %g1
  234. andcc %g1, TSTATE_PRIV, %g0
  235. saved
  236. be,pn %xcc, window_mna_from_user_common
  237. and %g1, TSTATE_CWP, %g1
  238. retry
  239. window_mna_from_user_common:
  240. wrpr %g1, %cwp
  241. sethi %hi(109f), %g7
  242. ba,pt %xcc, etrap
  243. 109: or %g7, %lo(109b), %g7
  244. mov %l4, %o2
  245. mov %l5, %o1
  246. call mem_address_unaligned
  247. add %sp, PTREGS_OFF, %o0
  248. ba,pt %xcc, rtrap
  249. clr %l6
  250. .globl winfix_dax, fill_fixup_dax, spill_fixup_dax
  251. winfix_dax:
  252. andn %g3, 0x7f, %g3
  253. add %g3, 0x74, %g3
  254. wrpr %g3, %tnpc
  255. done
  256. fill_fixup_dax:
  257. TRAP_LOAD_THREAD_REG
  258. rdpr %tstate, %g1
  259. andcc %g1, TSTATE_PRIV, %g0
  260. be,pt %xcc, window_dax_from_user_common
  261. and %g1, TSTATE_CWP, %g1
  262. /* Please, see fill_fixup commentary about why we must preserve
  263. * %l5 and %l6 to preserve absolute correct semantics.
  264. */
  265. rdpr %wstate, %g2 ! Grab user mode wstate.
  266. wrpr %g1, %cwp ! Get into the right window.
  267. sll %g2, 3, %g2 ! NORMAL-->OTHER
  268. wrpr %g0, 0x0, %canrestore ! Standard etrap stuff.
  269. wrpr %g2, 0x0, %wstate ! This must be consistent.
  270. wrpr %g0, 0x0, %otherwin ! We know this.
  271. call set_pcontext ! Change contexts...
  272. nop
  273. rdpr %pstate, %l1 ! Prepare to change globals.
  274. mov %g4, %o1 ! Setup args for
  275. mov %g5, %o2 ! final call to spitfire_data_access_exception.
  276. andn %l1, PSTATE_MM, %l1 ! We want to be in RMO
  277. mov %g6, %o7 ! Stash away current.
  278. wrpr %g0, 0x0, %tl ! Out of trap levels.
  279. wrpr %l1, (PSTATE_IE | PSTATE_AG | PSTATE_RMO), %pstate
  280. mov %o7, %g6 ! Get current back.
  281. ldx [%g6 + TI_TASK], %g4 ! Finish it.
  282. LOAD_PER_CPU_BASE(%g1, %g2)
  283. call spitfire_data_access_exception
  284. add %sp, PTREGS_OFF, %o0
  285. b,pt %xcc, rtrap
  286. nop ! yes, the nop is correct
  287. spill_fixup_dax:
  288. TRAP_LOAD_THREAD_REG
  289. ldx [%g6 + TI_FLAGS], %g1
  290. andcc %g1, _TIF_32BIT, %g0
  291. ldub [%g6 + TI_WSAVED], %g1
  292. sll %g1, 3, %g3
  293. add %g6, %g3, %g3
  294. stx %sp, [%g3 + TI_RWIN_SPTRS]
  295. sll %g1, 7, %g3
  296. bne,pt %xcc, 1f
  297. add %g6, %g3, %g3
  298. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  299. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  300. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  301. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  302. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  303. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  304. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  305. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  306. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  307. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  308. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  309. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  310. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  311. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  312. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  313. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  314. b,pt %xcc, 2f
  315. add %g1, 1, %g1
  316. 1: std %l0, [%g3 + TI_REG_WINDOW + 0x00]
  317. std %l2, [%g3 + TI_REG_WINDOW + 0x08]
  318. std %l4, [%g3 + TI_REG_WINDOW + 0x10]
  319. std %l6, [%g3 + TI_REG_WINDOW + 0x18]
  320. std %i0, [%g3 + TI_REG_WINDOW + 0x20]
  321. std %i2, [%g3 + TI_REG_WINDOW + 0x28]
  322. std %i4, [%g3 + TI_REG_WINDOW + 0x30]
  323. std %i6, [%g3 + TI_REG_WINDOW + 0x38]
  324. add %g1, 1, %g1
  325. 2: stb %g1, [%g6 + TI_WSAVED]
  326. rdpr %tstate, %g1
  327. andcc %g1, TSTATE_PRIV, %g0
  328. saved
  329. be,pn %xcc, window_dax_from_user_common
  330. and %g1, TSTATE_CWP, %g1
  331. retry
  332. window_dax_from_user_common:
  333. wrpr %g1, %cwp
  334. sethi %hi(109f), %g7
  335. ba,pt %xcc, etrap
  336. 109: or %g7, %lo(109b), %g7
  337. mov %l4, %o1
  338. mov %l5, %o2
  339. call spitfire_data_access_exception
  340. add %sp, PTREGS_OFF, %o0
  341. ba,pt %xcc, rtrap
  342. clr %l6