traps.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * (C) Copyright 2003, Psyent Corporation <www.psyent.com>
  3. * Scott McNutt <smcnutt@psyent.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <config.h>
  24. /*************************************************************************
  25. * Register window underflow
  26. *
  27. * The register window underflow exception occurs whenever the lowest
  28. * valid register window is in use (CWP=LO_LIMIT) and a save instruction
  29. * is issued. The save moves CWP below LO_LIMIT, %sp is set as normal,
  30. * then the exception is generated prior to executing the instruction
  31. * after the save.
  32. ************************************************************************/
  33. .text
  34. .global _cwp_lolimit
  35. .align 4
  36. _cwp_lolimit:
  37. /* Sixteen words are always allocated by the compiler in every
  38. * procedure's stack frame, always starting at %sp, for saving
  39. * 'in' and 'local' registers on a window overflow.
  40. *
  41. * Save the 'global' and 'in' regs on stack. They are restored
  42. * at cwp = HI_LIMIT. The 'local' regs aren't in-use at this point.
  43. */
  44. sts [%sp,0], %g0 /* Save 'global' regs*/
  45. sts [%sp,1], %g1
  46. sts [%sp,2], %g2
  47. sts [%sp,3], %g3
  48. sts [%sp,4], %g4
  49. sts [%sp,5], %g5
  50. sts [%sp,6], %g6
  51. sts [%sp,7], %g7
  52. sts [%sp,8], %i0 /* Save 'in' regs */
  53. sts [%sp,9], %i1
  54. sts [%sp,10], %i2
  55. sts [%sp,11], %i3
  56. sts [%sp,12], %i4
  57. sts [%sp,13], %i5
  58. sts [%sp,14], %i6
  59. sts [%sp,15], %i7
  60. /* Save current %sp and return address in a global so they are
  61. * available at cwp = HI_LIMIT ... where the 'global'/'in' regs
  62. * are restored. NOTE: %sp changes with cwp.
  63. */
  64. mov %g7, %o7
  65. mov %g6, %sp
  66. /* Get LO_LIMIT/HI_LIMIT to know where to start & stop. Note: in
  67. * the underflow exception, cwp is __NOT__ guaranteed to be zero.
  68. * If the OCI debug module is enabled the reset value for LO_LIMIT
  69. * is 2, not 1 -- so cwp can be 1 or 0.
  70. */
  71. pfx 2 /* WVALID */
  72. rdctl %g1
  73. mov %g2, %g1
  74. pfx 0
  75. and %g1, 0x1f /* g1 <- LO_LIMIT */
  76. lsri %g2, 5
  77. pfx 0
  78. and %g2,0x1f /* g2 <- HI_LIMIT */
  79. /* Set istatus so cwp = HI_LIMIT after tret
  80. */
  81. movi %g5, 0x1f
  82. lsli %g5, 4
  83. not %g5 /* mask to clr cwp */
  84. pfx 1 /* istatus */
  85. rdctl %g0
  86. and %g0, %g5 /* clear cwp field */
  87. mov %g4, %g2
  88. lsli %g4, 4
  89. or %g0, %g4 /* cwp = HI_LIMIT */
  90. pfx 1
  91. wrctl %g0 /* update istatus */
  92. /* Now move up the register file, saving as we go. When loop
  93. * is first entered, %g1 is at LO_LIMIT.
  94. */
  95. 0:
  96. restore /* cwp++ */
  97. sts [%sp,0], %l0 /* Save "local" regs*/
  98. sts [%sp,1], %l1
  99. sts [%sp,2], %l2
  100. sts [%sp,3], %l3
  101. sts [%sp,4], %l4
  102. sts [%sp,5], %l5
  103. sts [%sp,6], %l6
  104. sts [%sp,7], %l7
  105. sts [%sp,8], %i0 /* Save 'in' regs */
  106. sts [%sp,9], %i1
  107. sts [%sp,10], %i2
  108. sts [%sp,11], %i3
  109. sts [%sp,12], %i4
  110. sts [%sp,13], %i5
  111. sts [%sp,14], %i6
  112. sts [%sp,15], %i7
  113. cmp %g1, %g2 /* cwp == HI_LIMIT ? */
  114. skps cc_ne /* if so, we're done */
  115. br 1f
  116. nop /* delay slot */
  117. inc %g1 /* g1 <- cwp++ */
  118. br 0b
  119. nop /* delay slot */
  120. /* At this point cwp = HI_LIMIT, so the global/in regs that were
  121. * in place when the underflow occurred must be restored using
  122. * the original stack pointer (saved in g6).
  123. */
  124. 1:
  125. mov %o7, %g7 /* restore return addr */
  126. mov %sp, %g6 /* Restore original sp */
  127. lds %g0, [%sp,0] /* Restore 'global' regs*/
  128. lds %g1, [%sp,1]
  129. lds %g2, [%sp,2]
  130. lds %g3, [%sp,3]
  131. lds %g4, [%sp,4]
  132. lds %g5, [%sp,5]
  133. lds %g6, [%sp,6]
  134. lds %g7, [%sp,7]
  135. lds %i0, [%sp,8] /* Restore 'in' regs*/
  136. lds %i1, [%sp,9]
  137. lds %i2, [%sp,10]
  138. lds %i3, [%sp,11]
  139. lds %i4, [%sp,12]
  140. lds %i5, [%sp,13]
  141. lds %i6, [%sp,14]
  142. lds %i7, [%sp,15]
  143. tret %o7 /* All done */
  144. /*************************************************************************
  145. * Register window overflow
  146. *
  147. * The register window overflow exception occurs whenever the highest
  148. * valid register window is in use (cwp = HI_LIMIT) and a restore
  149. * instruction is issued. Control is transferred to the overflow handler
  150. * before the instruction following restore is executed.
  151. *
  152. * When a register window overflow exception is taken, the exception
  153. * handler sees cwp at HI_LIMIT.
  154. ************************************************************************/
  155. .text
  156. .global _cwp_hilimit
  157. .align 4
  158. _cwp_hilimit:
  159. /* Save 'global'/'in' regs on the stack -- will restore when cwp
  160. * is at LO_LIMIT. Locals don't need saving as they are going away.
  161. */
  162. sts [%sp,0], %g0 /* Save "global" regs*/
  163. sts [%sp,1], %g1
  164. sts [%sp,2], %g2
  165. sts [%sp,3], %g3
  166. sts [%sp,4], %g4
  167. sts [%sp,5], %g5
  168. sts [%sp,6], %g6
  169. sts [%sp,7], %g7
  170. sts [%sp,8], %i0 /* Save 'in' regs */
  171. sts [%sp,9], %i1
  172. sts [%sp,10], %i2
  173. sts [%sp,11], %i3
  174. sts [%sp,12], %i4
  175. sts [%sp,13], %i5
  176. sts [%sp,14], %i6
  177. sts [%sp,15], %i7
  178. /* The current %sp must be available in global to restore regs
  179. * saved on stack. Need return addr as well ;-)
  180. */
  181. mov %g7, %o7
  182. mov %g6, %sp
  183. /* Get HI_LIMIT & LO_LIMIT
  184. */
  185. pfx 2 /* WVALID */
  186. rdctl %g1
  187. mov %g2, %g1
  188. pfx 0
  189. and %g1, 0x1f /* g1 <- LO_LIMIT */
  190. lsri %g2, 5
  191. pfx 0
  192. and %g2,0x1f /* g2 <- HI_LIMIT */
  193. /* Set istatus so cwp = LO_LIMIT after tret
  194. */
  195. movi %g5, 0x1f
  196. lsli %g5, 4
  197. not %g5 /* mask to clr cwp */
  198. pfx 1 /* istatus */
  199. rdctl %g0
  200. and %g0, %g5 /* clear cwp field */
  201. mov %g4, %g1 /* g4 <- LO_LIMIT */
  202. lsli %g4, 4
  203. or %g0, %g4 /* cwp = LO_LIMIT */
  204. pfx 1
  205. wrctl %g0 /* update istatus */
  206. /* Move to cwp = LO_LIMIT-1 and restore 'in' regs.
  207. */
  208. subi %g4,(1 << 4) /* g4 <- LO_LIMIT - 1 */
  209. rdctl %g0
  210. and %g0, %g5 /* clear cwp field */
  211. or %g0, %g4 /* cwp = LO_LIMIT - 1 */
  212. wrctl %g0 /* update status */
  213. nop
  214. mov %sp, %g6 /* Restore sp */
  215. lds %i0, [%sp,8] /* Restore 'in' regs */
  216. lds %i1, [%sp,9]
  217. lds %i2, [%sp,10]
  218. lds %i3, [%sp,11]
  219. lds %i4, [%sp,12]
  220. lds %i5, [%sp,13]
  221. lds %i6, [%sp,14] /* sp in next window */
  222. lds %i7, [%sp,15]
  223. /* Starting at LO_LIMIT-1, move up the register file, restoring
  224. * along the way.
  225. */
  226. 0:
  227. restore /* cwp++ */
  228. lds %l0, [%sp,0] /* Restore 'local' regs*/
  229. lds %l1, [%sp,1]
  230. lds %l2, [%sp,2]
  231. lds %l3, [%sp,3]
  232. lds %l4, [%sp,4]
  233. lds %l5, [%sp,5]
  234. lds %l6, [%sp,6]
  235. lds %l7, [%sp,7]
  236. lds %i0, [%sp,8] /* Restore 'in' regs */
  237. lds %i1, [%sp,9]
  238. lds %i2, [%sp,10]
  239. lds %i3, [%sp,11]
  240. lds %i4, [%sp,12]
  241. lds %i5, [%sp,13]
  242. lds %i6, [%sp,14] /* sp in next window */
  243. lds %i7, [%sp,15]
  244. cmp %g1, %g2 /* cwp == HI_LIMIT ? */
  245. skps cc_ne /* if so, we're done */
  246. br 1f
  247. nop /* delay slot */
  248. inc %g1 /* cwp++ */
  249. br 0b
  250. nop /* delay slot */
  251. /* All windows have been updated at this point, but the globals
  252. * still need to be restored. Go to cwp = LO_LIMIT-1 to get
  253. * some registers to use.
  254. */
  255. 1:
  256. rdctl %g0
  257. and %g0, %g5 /* clear cwp field */
  258. or %g0, %g4 /* cwp = LO_LIMIT - 1 */
  259. wrctl %g0 /* update status */
  260. nop
  261. /* Now there are some registers available to use in restoring
  262. * the globals.
  263. */
  264. mov %sp, %g6
  265. mov %o7, %g7
  266. lds %g0, [%sp,0] /* Restore "global" regs*/
  267. lds %g1, [%sp,1]
  268. lds %g2, [%sp,2]
  269. lds %g3, [%sp,3]
  270. lds %g4, [%sp,4]
  271. lds %g5, [%sp,5]
  272. lds %g6, [%sp,6]
  273. lds %g7, [%sp,7]
  274. /* The tret moves istatus -> status. istatus was already set for
  275. * cwp = LO_LIMIT.
  276. */
  277. tret %o7 /* done */
  278. /*************************************************************************
  279. * Default exception handler
  280. *
  281. * The default handler passes control to external_interrupt(). So trap
  282. * or hardware interrupt hanlders can be installed using the familiar
  283. * irq_install_handler().
  284. *
  285. * Here, the stack is fixed-up and cwp is incremented prior to calling
  286. * external_interrupt(). This lets the underflow and overflow handlers
  287. * operate normally during the exception.
  288. ************************************************************************/
  289. .text
  290. .global _def_xhandler
  291. .align 4
  292. _def_xhandler:
  293. /* Allocate some stack space: 16 words at %sp to accomodate
  294. * a reg window underflow, 8 words to save interrupted task's
  295. * 'out' regs (which are now the 'in' regs), 8 words to preserve
  296. * the 'global' regs and 3 words to save the return address,
  297. * status and istatus. istatus must be saved in the event an
  298. * underflow occurs in a dispatched handler. status is saved so
  299. * a handler can access it on stack.
  300. */
  301. pfx %hi((16+16+3) * 4)
  302. subi %fp, %lo((16+16+3) * 4)
  303. mov %sp, %fp
  304. /* Save the 'global' regs and the interrupted task's 'out' regs
  305. * (our 'in' regs) along with the return addr, status & istatus.
  306. * First 16 words are for underflow exception.
  307. */
  308. rdctl %l0 /* status */
  309. pfx 1 /* istatus */
  310. rdctl %l1
  311. sts [%sp,16+0], %g0 /* Save 'global' regs*/
  312. sts [%sp,16+1], %g1
  313. sts [%sp,16+2], %g2
  314. sts [%sp,16+3], %g3
  315. sts [%sp,16+4], %g4
  316. sts [%sp,16+5], %g5
  317. sts [%sp,16+6], %g6
  318. sts [%sp,16+7], %g7
  319. sts [%sp,16+8], %i0 /* Save 'in' regs */
  320. sts [%sp,16+9], %i1
  321. sts [%sp,16+10], %i2
  322. sts [%sp,16+11], %i3
  323. sts [%sp,16+12], %i4
  324. sts [%sp,16+13], %i5
  325. sts [%sp,16+14], %i6
  326. sts [%sp,16+15], %i7
  327. sts [%sp,16+16], %l0 /* status */
  328. sts [%sp,16+17], %l1 /* istatus */
  329. sts [%sp,16+18], %o7 /* return addr */
  330. /* Move to cwp+1 ... this guarantees cwp is at or above LO_LIMIT.
  331. * Need to set IPRI=3 and IE=1 to enable underflow exceptions.
  332. * NOTE: only the 'out' regs have been saved ... can't touch
  333. * the 'in' or 'local' here.
  334. */
  335. restore /* cwp++ */
  336. rdctl %o0 /* o0 <- status */
  337. pfx %hi(0x7e00)
  338. movi %o1, %lo(0x7e00)
  339. not %o1
  340. and %o0, %o1 /* clear IPRI */
  341. pfx %hi(0x8600)
  342. movi %o1, %lo(0x8600)
  343. or %o0, %o1 /* IPRI=3, IE=1 */
  344. wrctl %o0 /* o0 -> status */
  345. nop
  346. /* It's ok to call a C routine now since cwp >= LO_LIMIT,
  347. * interrupt task's registers are/will be preserved, and
  348. * underflow exceptions can be handled.
  349. */
  350. pfx %hi(external_interrupt@h)
  351. movi %o1, %lo(external_interrupt@h)
  352. pfx %xhi(external_interrupt@h)
  353. movhi %o1, %xlo(external_interrupt@h)
  354. bgen %o0, 4+2 /* 16 * 4 */
  355. add %o0, %sp /* Ptr to regs */
  356. call %o1
  357. nop
  358. /* Move back to the exception register window, restore the 'out'
  359. * registers, then return from exception.
  360. */
  361. rdctl %o0 /* o0 <- status */
  362. subi %o0, 16
  363. wrctl %o0 /* cwp-- */
  364. nop
  365. mov %sp, %fp
  366. lds %g0, [%sp,16+0] /* Restore 'global' regs*/
  367. lds %g1, [%sp,16+1]
  368. lds %g2, [%sp,16+2]
  369. lds %g3, [%sp,16+3]
  370. lds %g4, [%sp,16+4]
  371. lds %g5, [%sp,16+5]
  372. lds %g6, [%sp,16+6]
  373. lds %g7, [%sp,16+7]
  374. lds %i0, [%sp,16+8] /* Restore 'in' regs*/
  375. lds %i1, [%sp,16+9]
  376. lds %i2, [%sp,16+10]
  377. lds %i3, [%sp,16+11]
  378. lds %i4, [%sp,16+12]
  379. lds %i5, [%sp,16+13]
  380. lds %i6, [%sp,16+14]
  381. lds %i7, [%sp,16+15]
  382. lds %l0, [%sp,16+16] /* status */
  383. lds %l1, [%sp,16+17] /* istatus */
  384. lds %o7, [%sp,16+18] /* return addr */
  385. pfx 1
  386. wrctl %l1 /* restore istatus */
  387. pfx %hi((16+16+3) * 4)
  388. addi %sp, %lo((16+16+3) * 4)
  389. mov %fp, %sp
  390. tret %o7 /* Done */
  391. /*************************************************************************
  392. * Timebase Timer Interrupt -- This has identical structure to above,
  393. * but calls timer_interrupt(). Doing it this way keeps things similar
  394. * to other architectures (e.g. ppc).
  395. ************************************************************************/
  396. .text
  397. .global _timebase_int
  398. .align 4
  399. _timebase_int:
  400. /* Allocate stack space.
  401. */
  402. pfx %hi((16+16+3) * 4)
  403. subi %fp, %lo((16+16+3) * 4)
  404. mov %sp, %fp
  405. /* Save the 'global' regs & 'out' regs (our 'in' regs)
  406. */
  407. rdctl %l0 /* status */
  408. pfx 1 /* istatus */
  409. rdctl %l1
  410. sts [%sp,16+0], %g0 /* Save 'global' regs*/
  411. sts [%sp,16+1], %g1
  412. sts [%sp,16+2], %g2
  413. sts [%sp,16+3], %g3
  414. sts [%sp,16+4], %g4
  415. sts [%sp,16+5], %g5
  416. sts [%sp,16+6], %g6
  417. sts [%sp,16+7], %g7
  418. sts [%sp,16+8], %i0 /* Save 'in' regs */
  419. sts [%sp,16+9], %i1
  420. sts [%sp,16+10], %i2
  421. sts [%sp,16+11], %i3
  422. sts [%sp,16+12], %i4
  423. sts [%sp,16+13], %i5
  424. sts [%sp,16+14], %i6
  425. sts [%sp,16+15], %i7
  426. sts [%sp,16+16], %l0 /* status */
  427. sts [%sp,16+17], %l1 /* istatus */
  428. sts [%sp,16+18], %o7 /* return addr */
  429. /* Move to cwp+1.
  430. */
  431. restore /* cwp++ */
  432. rdctl %o0 /* o0 <- status */
  433. pfx %hi(0x7e00)
  434. movi %o1, %lo(0x7e00)
  435. not %o1
  436. and %o0, %o1 /* clear IPRI */
  437. pfx %hi(0x8600)
  438. movi %o1, %lo(0x8600)
  439. or %o0, %o1 /* IPRI=3, IE=1 */
  440. wrctl %o0 /* o0 -> status */
  441. nop
  442. /* Call timer_interrupt()
  443. */
  444. pfx %hi(timer_interrupt@h)
  445. movi %o1, %lo(timer_interrupt@h)
  446. pfx %xhi(timer_interrupt@h)
  447. movhi %o1, %xlo(timer_interrupt@h)
  448. bgen %o0, 4+2 /* 16 * 4 */
  449. add %o0, %sp /* Ptr to regs */
  450. call %o1
  451. nop
  452. /* Move back to the exception register window, restore the 'out'
  453. * registers, then return from exception.
  454. */
  455. rdctl %o0 /* o0 <- status */
  456. subi %o0, 16
  457. wrctl %o0 /* cwp-- */
  458. nop
  459. mov %sp, %fp
  460. lds %g0, [%sp,16+0] /* Restore 'global' regs*/
  461. lds %g1, [%sp,16+1]
  462. lds %g2, [%sp,16+2]
  463. lds %g3, [%sp,16+3]
  464. lds %g4, [%sp,16+4]
  465. lds %g5, [%sp,16+5]
  466. lds %g6, [%sp,16+6]
  467. lds %g7, [%sp,16+7]
  468. lds %i0, [%sp,16+8] /* Restore 'in' regs*/
  469. lds %i1, [%sp,16+9]
  470. lds %i2, [%sp,16+10]
  471. lds %i3, [%sp,16+11]
  472. lds %i4, [%sp,16+12]
  473. lds %i5, [%sp,16+13]
  474. lds %i6, [%sp,16+14]
  475. lds %i7, [%sp,16+15]
  476. lds %l0, [%sp,16+16] /* status */
  477. lds %l1, [%sp,16+17] /* istatus */
  478. lds %o7, [%sp,16+18] /* return addr */
  479. pfx 1
  480. wrctl %l1 /* restore istatus */
  481. pfx %hi((16+16+3) * 4)
  482. addi %sp, %lo((16+16+3) * 4)
  483. mov %fp, %sp
  484. tret %o7 /* Done */