winfixup.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. sethi %hi(KERNBASE), %l1
  21. flush %l1
  22. retl
  23. nop
  24. .align 32
  25. /* Here are the rules, pay attention.
  26. *
  27. * The kernel is disallowed from touching user space while
  28. * the trap level is greater than zero, except for from within
  29. * the window spill/fill handlers. This must be followed
  30. * so that we can easily detect the case where we tried to
  31. * spill/fill with a bogus (or unmapped) user stack pointer.
  32. *
  33. * These are layed out in a special way for cache reasons,
  34. * don't touch...
  35. */
  36. .globl fill_fixup, spill_fixup
  37. fill_fixup:
  38. TRAP_LOAD_THREAD_REG
  39. rdpr %tstate, %g1
  40. andcc %g1, TSTATE_PRIV, %g0
  41. or %g4, FAULT_CODE_WINFIXUP, %g4
  42. be,pt %xcc, window_scheisse_from_user_common
  43. and %g1, TSTATE_CWP, %g1
  44. /* This is the extremely complex case, but it does happen from
  45. * time to time if things are just right. Essentially the restore
  46. * done in rtrap right before going back to user mode, with tl=1
  47. * and that levels trap stack registers all setup, took a fill trap,
  48. * the user stack was not mapped in the tlb, and tlb miss occurred,
  49. * the pte found was not valid, and a simple ref bit watch update
  50. * could not satisfy the miss, so we got here.
  51. *
  52. * We must carefully unwind the state so we get back to tl=0, preserve
  53. * all the register values we were going to give to the user. Luckily
  54. * most things are where they need to be, we also have the address
  55. * which triggered the fault handy as well.
  56. *
  57. * Also note that we must preserve %l5 and %l6. If the user was
  58. * returning from a system call, we must make it look this way
  59. * after we process the fill fault on the users stack.
  60. *
  61. * First, get into the window where the original restore was executed.
  62. */
  63. rdpr %wstate, %g2 ! Grab user mode wstate.
  64. wrpr %g1, %cwp ! Get into the right window.
  65. sll %g2, 3, %g2 ! NORMAL-->OTHER
  66. wrpr %g0, 0x0, %canrestore ! Standard etrap stuff.
  67. wrpr %g2, 0x0, %wstate ! This must be consistent.
  68. wrpr %g0, 0x0, %otherwin ! We know this.
  69. call set_pcontext ! Change contexts...
  70. nop
  71. rdpr %pstate, %l1 ! Prepare to change globals.
  72. mov %g6, %o7 ! Get current.
  73. andn %l1, PSTATE_MM, %l1 ! We want to be in RMO
  74. stb %g4, [%g6 + TI_FAULT_CODE]
  75. stx %g5, [%g6 + TI_FAULT_ADDR]
  76. wrpr %g0, 0x0, %tl ! Out of trap levels.
  77. wrpr %l1, (PSTATE_IE | PSTATE_AG | PSTATE_RMO), %pstate
  78. mov %o7, %g6
  79. ldx [%g6 + TI_TASK], %g4
  80. LOAD_PER_CPU_BASE(%g1, %g2)
  81. /* This is the same as below, except we handle this a bit special
  82. * since we must preserve %l5 and %l6, see comment above.
  83. */
  84. call do_sparc64_fault
  85. add %sp, PTREGS_OFF, %o0
  86. ba,pt %xcc, rtrap
  87. nop ! yes, nop is correct
  88. /* Be very careful about usage of the alternate globals here.
  89. * You cannot touch %g4/%g5 as that has the fault information
  90. * should this be from usermode. Also be careful for the case
  91. * where we get here from the save instruction in etrap.S when
  92. * coming from either user or kernel (does not matter which, it
  93. * is the same problem in both cases). Essentially this means
  94. * do not touch %g7 or %g2 so we handle the two cases fine.
  95. */
  96. spill_fixup:
  97. TRAP_LOAD_THREAD_REG
  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. TRAP_LOAD_THREAD_REG
  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. LOAD_PER_CPU_BASE(%g1, %g2)
  191. call mem_address_unaligned
  192. add %sp, PTREGS_OFF, %o0
  193. b,pt %xcc, rtrap
  194. nop ! yes, the nop is correct
  195. spill_fixup_mna:
  196. TRAP_LOAD_THREAD_REG
  197. ldx [%g6 + TI_FLAGS], %g1
  198. andcc %g1, _TIF_32BIT, %g0
  199. ldub [%g6 + TI_WSAVED], %g1
  200. sll %g1, 3, %g3
  201. add %g6, %g3, %g3
  202. stx %sp, [%g3 + TI_RWIN_SPTRS]
  203. sll %g1, 7, %g3
  204. bne,pt %xcc, 1f
  205. add %g6, %g3, %g3
  206. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  207. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  208. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  209. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  210. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  211. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  212. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  213. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  214. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  215. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  216. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  217. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  218. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  219. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  220. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  221. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  222. b,pt %xcc, 2f
  223. add %g1, 1, %g1
  224. 1: std %l0, [%g3 + TI_REG_WINDOW + 0x00]
  225. std %l2, [%g3 + TI_REG_WINDOW + 0x08]
  226. std %l4, [%g3 + TI_REG_WINDOW + 0x10]
  227. std %l6, [%g3 + TI_REG_WINDOW + 0x18]
  228. std %i0, [%g3 + TI_REG_WINDOW + 0x20]
  229. std %i2, [%g3 + TI_REG_WINDOW + 0x28]
  230. std %i4, [%g3 + TI_REG_WINDOW + 0x30]
  231. std %i6, [%g3 + TI_REG_WINDOW + 0x38]
  232. add %g1, 1, %g1
  233. 2: stb %g1, [%g6 + TI_WSAVED]
  234. rdpr %tstate, %g1
  235. andcc %g1, TSTATE_PRIV, %g0
  236. saved
  237. be,pn %xcc, window_mna_from_user_common
  238. and %g1, TSTATE_CWP, %g1
  239. retry
  240. window_mna_from_user_common:
  241. wrpr %g1, %cwp
  242. sethi %hi(109f), %g7
  243. ba,pt %xcc, etrap
  244. 109: or %g7, %lo(109b), %g7
  245. mov %l4, %o2
  246. mov %l5, %o1
  247. call mem_address_unaligned
  248. add %sp, PTREGS_OFF, %o0
  249. ba,pt %xcc, rtrap
  250. clr %l6
  251. .globl winfix_dax, fill_fixup_dax, spill_fixup_dax
  252. winfix_dax:
  253. andn %g3, 0x7f, %g3
  254. add %g3, 0x74, %g3
  255. wrpr %g3, %tnpc
  256. done
  257. fill_fixup_dax:
  258. TRAP_LOAD_THREAD_REG
  259. rdpr %tstate, %g1
  260. andcc %g1, TSTATE_PRIV, %g0
  261. be,pt %xcc, window_dax_from_user_common
  262. and %g1, TSTATE_CWP, %g1
  263. /* Please, see fill_fixup commentary about why we must preserve
  264. * %l5 and %l6 to preserve absolute correct semantics.
  265. */
  266. rdpr %wstate, %g2 ! Grab user mode wstate.
  267. wrpr %g1, %cwp ! Get into the right window.
  268. sll %g2, 3, %g2 ! NORMAL-->OTHER
  269. wrpr %g0, 0x0, %canrestore ! Standard etrap stuff.
  270. wrpr %g2, 0x0, %wstate ! This must be consistent.
  271. wrpr %g0, 0x0, %otherwin ! We know this.
  272. call set_pcontext ! Change contexts...
  273. nop
  274. rdpr %pstate, %l1 ! Prepare to change globals.
  275. mov %g4, %o1 ! Setup args for
  276. mov %g5, %o2 ! final call to spitfire_data_access_exception.
  277. andn %l1, PSTATE_MM, %l1 ! We want to be in RMO
  278. mov %g6, %o7 ! Stash away current.
  279. wrpr %g0, 0x0, %tl ! Out of trap levels.
  280. wrpr %l1, (PSTATE_IE | PSTATE_AG | PSTATE_RMO), %pstate
  281. mov %o7, %g6 ! Get current back.
  282. ldx [%g6 + TI_TASK], %g4 ! Finish it.
  283. LOAD_PER_CPU_BASE(%g1, %g2)
  284. call spitfire_data_access_exception
  285. add %sp, PTREGS_OFF, %o0
  286. b,pt %xcc, rtrap
  287. nop ! yes, the nop is correct
  288. spill_fixup_dax:
  289. TRAP_LOAD_THREAD_REG
  290. ldx [%g6 + TI_FLAGS], %g1
  291. andcc %g1, _TIF_32BIT, %g0
  292. ldub [%g6 + TI_WSAVED], %g1
  293. sll %g1, 3, %g3
  294. add %g6, %g3, %g3
  295. stx %sp, [%g3 + TI_RWIN_SPTRS]
  296. sll %g1, 7, %g3
  297. bne,pt %xcc, 1f
  298. add %g6, %g3, %g3
  299. stx %l0, [%g3 + TI_REG_WINDOW + 0x00]
  300. stx %l1, [%g3 + TI_REG_WINDOW + 0x08]
  301. stx %l2, [%g3 + TI_REG_WINDOW + 0x10]
  302. stx %l3, [%g3 + TI_REG_WINDOW + 0x18]
  303. stx %l4, [%g3 + TI_REG_WINDOW + 0x20]
  304. stx %l5, [%g3 + TI_REG_WINDOW + 0x28]
  305. stx %l6, [%g3 + TI_REG_WINDOW + 0x30]
  306. stx %l7, [%g3 + TI_REG_WINDOW + 0x38]
  307. stx %i0, [%g3 + TI_REG_WINDOW + 0x40]
  308. stx %i1, [%g3 + TI_REG_WINDOW + 0x48]
  309. stx %i2, [%g3 + TI_REG_WINDOW + 0x50]
  310. stx %i3, [%g3 + TI_REG_WINDOW + 0x58]
  311. stx %i4, [%g3 + TI_REG_WINDOW + 0x60]
  312. stx %i5, [%g3 + TI_REG_WINDOW + 0x68]
  313. stx %i6, [%g3 + TI_REG_WINDOW + 0x70]
  314. stx %i7, [%g3 + TI_REG_WINDOW + 0x78]
  315. b,pt %xcc, 2f
  316. add %g1, 1, %g1
  317. 1: std %l0, [%g3 + TI_REG_WINDOW + 0x00]
  318. std %l2, [%g3 + TI_REG_WINDOW + 0x08]
  319. std %l4, [%g3 + TI_REG_WINDOW + 0x10]
  320. std %l6, [%g3 + TI_REG_WINDOW + 0x18]
  321. std %i0, [%g3 + TI_REG_WINDOW + 0x20]
  322. std %i2, [%g3 + TI_REG_WINDOW + 0x28]
  323. std %i4, [%g3 + TI_REG_WINDOW + 0x30]
  324. std %i6, [%g3 + TI_REG_WINDOW + 0x38]
  325. add %g1, 1, %g1
  326. 2: stb %g1, [%g6 + TI_WSAVED]
  327. rdpr %tstate, %g1
  328. andcc %g1, TSTATE_PRIV, %g0
  329. saved
  330. be,pn %xcc, window_dax_from_user_common
  331. and %g1, TSTATE_CWP, %g1
  332. retry
  333. window_dax_from_user_common:
  334. wrpr %g1, %cwp
  335. sethi %hi(109f), %g7
  336. ba,pt %xcc, etrap
  337. 109: or %g7, %lo(109b), %g7
  338. mov %l4, %o1
  339. mov %l5, %o2
  340. call spitfire_data_access_exception
  341. add %sp, PTREGS_OFF, %o0
  342. ba,pt %xcc, rtrap
  343. clr %l6