entry.S 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  1. /*
  2. * arch/xtensa/kernel/entry.S
  3. *
  4. * Low-level exception handling
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2004 - 2008 by Tensilica Inc.
  11. *
  12. * Chris Zankel <chris@zankel.net>
  13. *
  14. */
  15. #include <linux/linkage.h>
  16. #include <asm/asm-offsets.h>
  17. #include <asm/processor.h>
  18. #include <asm/coprocessor.h>
  19. #include <asm/thread_info.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/unistd.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/current.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/page.h>
  26. #include <asm/signal.h>
  27. #include <asm/tlbflush.h>
  28. #include <variant/tie-asm.h>
  29. /* Unimplemented features. */
  30. #undef KERNEL_STACK_OVERFLOW_CHECK
  31. /* Not well tested.
  32. *
  33. * - fast_coprocessor
  34. */
  35. /*
  36. * Macro to find first bit set in WINDOWBASE from the left + 1
  37. *
  38. * 100....0 -> 1
  39. * 010....0 -> 2
  40. * 000....1 -> WSBITS
  41. */
  42. .macro ffs_ws bit mask
  43. #if XCHAL_HAVE_NSA
  44. nsau \bit, \mask # 32-WSBITS ... 31 (32 iff 0)
  45. addi \bit, \bit, WSBITS - 32 + 1 # uppest bit set -> return 1
  46. #else
  47. movi \bit, WSBITS
  48. #if WSBITS > 16
  49. _bltui \mask, 0x10000, 99f
  50. addi \bit, \bit, -16
  51. extui \mask, \mask, 16, 16
  52. #endif
  53. #if WSBITS > 8
  54. 99: _bltui \mask, 0x100, 99f
  55. addi \bit, \bit, -8
  56. srli \mask, \mask, 8
  57. #endif
  58. 99: _bltui \mask, 0x10, 99f
  59. addi \bit, \bit, -4
  60. srli \mask, \mask, 4
  61. 99: _bltui \mask, 0x4, 99f
  62. addi \bit, \bit, -2
  63. srli \mask, \mask, 2
  64. 99: _bltui \mask, 0x2, 99f
  65. addi \bit, \bit, -1
  66. 99:
  67. #endif
  68. .endm
  69. /* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */
  70. /*
  71. * First-level exception handler for user exceptions.
  72. * Save some special registers, extra states and all registers in the AR
  73. * register file that were in use in the user task, and jump to the common
  74. * exception code.
  75. * We save SAR (used to calculate WMASK), and WB and WS (we don't have to
  76. * save them for kernel exceptions).
  77. *
  78. * Entry condition for user_exception:
  79. *
  80. * a0: trashed, original value saved on stack (PT_AREG0)
  81. * a1: a1
  82. * a2: new stack pointer, original value in depc
  83. * a3: a3
  84. * depc: a2, original value saved on stack (PT_DEPC)
  85. * excsave1: dispatch table
  86. *
  87. * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
  88. * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
  89. *
  90. * Entry condition for _user_exception:
  91. *
  92. * a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
  93. * excsave has been restored, and
  94. * stack pointer (a1) has been set.
  95. *
  96. * Note: _user_exception might be at an odd address. Don't use call0..call12
  97. */
  98. ENTRY(user_exception)
  99. /* Save a1, a2, a3, and set SP. */
  100. rsr a0, depc
  101. s32i a1, a2, PT_AREG1
  102. s32i a0, a2, PT_AREG2
  103. s32i a3, a2, PT_AREG3
  104. mov a1, a2
  105. .globl _user_exception
  106. _user_exception:
  107. /* Save SAR and turn off single stepping */
  108. movi a2, 0
  109. rsr a3, sar
  110. xsr a2, icountlevel
  111. s32i a3, a1, PT_SAR
  112. s32i a2, a1, PT_ICOUNTLEVEL
  113. #if XCHAL_HAVE_THREADPTR
  114. rur a2, threadptr
  115. s32i a2, a1, PT_THREADPTR
  116. #endif
  117. /* Rotate ws so that the current windowbase is at bit0. */
  118. /* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
  119. rsr a2, windowbase
  120. rsr a3, windowstart
  121. ssr a2
  122. s32i a2, a1, PT_WINDOWBASE
  123. s32i a3, a1, PT_WINDOWSTART
  124. slli a2, a3, 32-WSBITS
  125. src a2, a3, a2
  126. srli a2, a2, 32-WSBITS
  127. s32i a2, a1, PT_WMASK # needed for restoring registers
  128. /* Save only live registers. */
  129. _bbsi.l a2, 1, 1f
  130. s32i a4, a1, PT_AREG4
  131. s32i a5, a1, PT_AREG5
  132. s32i a6, a1, PT_AREG6
  133. s32i a7, a1, PT_AREG7
  134. _bbsi.l a2, 2, 1f
  135. s32i a8, a1, PT_AREG8
  136. s32i a9, a1, PT_AREG9
  137. s32i a10, a1, PT_AREG10
  138. s32i a11, a1, PT_AREG11
  139. _bbsi.l a2, 3, 1f
  140. s32i a12, a1, PT_AREG12
  141. s32i a13, a1, PT_AREG13
  142. s32i a14, a1, PT_AREG14
  143. s32i a15, a1, PT_AREG15
  144. _bnei a2, 1, 1f # only one valid frame?
  145. /* Only one valid frame, skip saving regs. */
  146. j 2f
  147. /* Save the remaining registers.
  148. * We have to save all registers up to the first '1' from
  149. * the right, except the current frame (bit 0).
  150. * Assume a2 is: 001001000110001
  151. * All register frames starting from the top field to the marked '1'
  152. * must be saved.
  153. */
  154. 1: addi a3, a2, -1 # eliminate '1' in bit 0: yyyyxxww0
  155. neg a3, a3 # yyyyxxww0 -> YYYYXXWW1+1
  156. and a3, a3, a2 # max. only one bit is set
  157. /* Find number of frames to save */
  158. ffs_ws a0, a3 # number of frames to the '1' from left
  159. /* Store information into WMASK:
  160. * bits 0..3: xxx1 masked lower 4 bits of the rotated windowstart,
  161. * bits 4...: number of valid 4-register frames
  162. */
  163. slli a3, a0, 4 # number of frames to save in bits 8..4
  164. extui a2, a2, 0, 4 # mask for the first 16 registers
  165. or a2, a3, a2
  166. s32i a2, a1, PT_WMASK # needed when we restore the reg-file
  167. /* Save 4 registers at a time */
  168. 1: rotw -1
  169. s32i a0, a5, PT_AREG_END - 16
  170. s32i a1, a5, PT_AREG_END - 12
  171. s32i a2, a5, PT_AREG_END - 8
  172. s32i a3, a5, PT_AREG_END - 4
  173. addi a0, a4, -1
  174. addi a1, a5, -16
  175. _bnez a0, 1b
  176. /* WINDOWBASE still in SAR! */
  177. rsr a2, sar # original WINDOWBASE
  178. movi a3, 1
  179. ssl a2
  180. sll a3, a3
  181. wsr a3, windowstart # set corresponding WINDOWSTART bit
  182. wsr a2, windowbase # and WINDOWSTART
  183. rsync
  184. /* We are back to the original stack pointer (a1) */
  185. 2: /* Now, jump to the common exception handler. */
  186. j common_exception
  187. ENDPROC(user_exception)
  188. /*
  189. * First-level exit handler for kernel exceptions
  190. * Save special registers and the live window frame.
  191. * Note: Even though we changes the stack pointer, we don't have to do a
  192. * MOVSP here, as we do that when we return from the exception.
  193. * (See comment in the kernel exception exit code)
  194. *
  195. * Entry condition for kernel_exception:
  196. *
  197. * a0: trashed, original value saved on stack (PT_AREG0)
  198. * a1: a1
  199. * a2: new stack pointer, original in DEPC
  200. * a3: a3
  201. * depc: a2, original value saved on stack (PT_DEPC)
  202. * excsave_1: dispatch table
  203. *
  204. * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
  205. * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
  206. *
  207. * Entry condition for _kernel_exception:
  208. *
  209. * a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
  210. * excsave has been restored, and
  211. * stack pointer (a1) has been set.
  212. *
  213. * Note: _kernel_exception might be at an odd address. Don't use call0..call12
  214. */
  215. ENTRY(kernel_exception)
  216. /* Save a1, a2, a3, and set SP. */
  217. rsr a0, depc # get a2
  218. s32i a1, a2, PT_AREG1
  219. s32i a0, a2, PT_AREG2
  220. s32i a3, a2, PT_AREG3
  221. mov a1, a2
  222. .globl _kernel_exception
  223. _kernel_exception:
  224. /* Save SAR and turn off single stepping */
  225. movi a2, 0
  226. rsr a3, sar
  227. xsr a2, icountlevel
  228. s32i a3, a1, PT_SAR
  229. s32i a2, a1, PT_ICOUNTLEVEL
  230. /* Rotate ws so that the current windowbase is at bit0. */
  231. /* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
  232. rsr a2, windowbase # don't need to save these, we only
  233. rsr a3, windowstart # need shifted windowstart: windowmask
  234. ssr a2
  235. slli a2, a3, 32-WSBITS
  236. src a2, a3, a2
  237. srli a2, a2, 32-WSBITS
  238. s32i a2, a1, PT_WMASK # needed for kernel_exception_exit
  239. /* Save only the live window-frame */
  240. _bbsi.l a2, 1, 1f
  241. s32i a4, a1, PT_AREG4
  242. s32i a5, a1, PT_AREG5
  243. s32i a6, a1, PT_AREG6
  244. s32i a7, a1, PT_AREG7
  245. _bbsi.l a2, 2, 1f
  246. s32i a8, a1, PT_AREG8
  247. s32i a9, a1, PT_AREG9
  248. s32i a10, a1, PT_AREG10
  249. s32i a11, a1, PT_AREG11
  250. _bbsi.l a2, 3, 1f
  251. s32i a12, a1, PT_AREG12
  252. s32i a13, a1, PT_AREG13
  253. s32i a14, a1, PT_AREG14
  254. s32i a15, a1, PT_AREG15
  255. 1:
  256. #ifdef KERNEL_STACK_OVERFLOW_CHECK
  257. /* Stack overflow check, for debugging */
  258. extui a2, a1, TASK_SIZE_BITS,XX
  259. movi a3, SIZE??
  260. _bge a2, a3, out_of_stack_panic
  261. #endif
  262. /*
  263. * This is the common exception handler.
  264. * We get here from the user exception handler or simply by falling through
  265. * from the kernel exception handler.
  266. * Save the remaining special registers, switch to kernel mode, and jump
  267. * to the second-level exception handler.
  268. *
  269. */
  270. common_exception:
  271. /* Save some registers, disable loops and clear the syscall flag. */
  272. rsr a2, debugcause
  273. rsr a3, epc1
  274. s32i a2, a1, PT_DEBUGCAUSE
  275. s32i a3, a1, PT_PC
  276. movi a2, -1
  277. rsr a3, excvaddr
  278. s32i a2, a1, PT_SYSCALL
  279. movi a2, 0
  280. s32i a3, a1, PT_EXCVADDR
  281. xsr a2, lcount
  282. s32i a2, a1, PT_LCOUNT
  283. /* It is now save to restore the EXC_TABLE_FIXUP variable. */
  284. rsr a0, exccause
  285. movi a3, 0
  286. rsr a2, excsave1
  287. s32i a0, a1, PT_EXCCAUSE
  288. s32i a3, a2, EXC_TABLE_FIXUP
  289. /* All unrecoverable states are saved on stack, now, and a1 is valid,
  290. * so we can allow exceptions and interrupts (*) again.
  291. * Set PS(EXCM = 0, UM = 0, RING = 0, OWB = 0, WOE = 1, INTLEVEL = X)
  292. *
  293. * (*) We only allow interrupts if they were previously enabled and
  294. * we're not handling an IRQ
  295. */
  296. rsr a3, ps
  297. addi a0, a0, -EXCCAUSE_LEVEL1_INTERRUPT
  298. movi a2, LOCKLEVEL
  299. extui a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
  300. # a3 = PS.INTLEVEL
  301. moveqz a3, a2, a0 # a3 = LOCKLEVEL iff interrupt
  302. movi a2, 1 << PS_WOE_BIT
  303. or a3, a3, a2
  304. rsr a0, exccause
  305. xsr a3, ps
  306. s32i a3, a1, PT_PS # save ps
  307. /* Save lbeg, lend */
  308. rsr a2, lbeg
  309. rsr a3, lend
  310. s32i a2, a1, PT_LBEG
  311. s32i a3, a1, PT_LEND
  312. /* Save SCOMPARE1 */
  313. #if XCHAL_HAVE_S32C1I
  314. rsr a2, scompare1
  315. s32i a2, a1, PT_SCOMPARE1
  316. #endif
  317. /* Save optional registers. */
  318. save_xtregs_opt a1 a2 a4 a5 a6 a7 PT_XTREGS_OPT
  319. #ifdef CONFIG_TRACE_IRQFLAGS
  320. l32i a4, a1, PT_DEPC
  321. /* Double exception means we came here with an exception
  322. * while PS.EXCM was set, i.e. interrupts disabled.
  323. */
  324. bgeui a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
  325. l32i a4, a1, PT_EXCCAUSE
  326. bnei a4, EXCCAUSE_LEVEL1_INTERRUPT, 1f
  327. /* We came here with an interrupt means interrupts were enabled
  328. * and we've just disabled them.
  329. */
  330. movi a4, trace_hardirqs_off
  331. callx4 a4
  332. 1:
  333. #endif
  334. /* Go to second-level dispatcher. Set up parameters to pass to the
  335. * exception handler and call the exception handler.
  336. */
  337. rsr a4, excsave1
  338. mov a6, a1 # pass stack frame
  339. mov a7, a0 # pass EXCCAUSE
  340. addx4 a4, a0, a4
  341. l32i a4, a4, EXC_TABLE_DEFAULT # load handler
  342. /* Call the second-level handler */
  343. callx4 a4
  344. /* Jump here for exception exit */
  345. .global common_exception_return
  346. common_exception_return:
  347. 1:
  348. rsil a2, LOCKLEVEL
  349. /* Jump if we are returning from kernel exceptions. */
  350. l32i a3, a1, PT_PS
  351. GET_THREAD_INFO(a2, a1)
  352. l32i a4, a2, TI_FLAGS
  353. _bbci.l a3, PS_UM_BIT, 6f
  354. /* Specific to a user exception exit:
  355. * We need to check some flags for signal handling and rescheduling,
  356. * and have to restore WB and WS, extra states, and all registers
  357. * in the register file that were in use in the user task.
  358. * Note that we don't disable interrupts here.
  359. */
  360. _bbsi.l a4, TIF_NEED_RESCHED, 3f
  361. _bbsi.l a4, TIF_NOTIFY_RESUME, 2f
  362. _bbci.l a4, TIF_SIGPENDING, 5f
  363. 2: l32i a4, a1, PT_DEPC
  364. bgeui a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
  365. /* Call do_signal() */
  366. rsil a2, 0
  367. movi a4, do_notify_resume # int do_notify_resume(struct pt_regs*)
  368. mov a6, a1
  369. callx4 a4
  370. j 1b
  371. 3: /* Reschedule */
  372. rsil a2, 0
  373. movi a4, schedule # void schedule (void)
  374. callx4 a4
  375. j 1b
  376. #ifdef CONFIG_PREEMPT
  377. 6:
  378. _bbci.l a4, TIF_NEED_RESCHED, 4f
  379. /* Check current_thread_info->preempt_count */
  380. l32i a4, a2, TI_PRE_COUNT
  381. bnez a4, 4f
  382. movi a4, preempt_schedule_irq
  383. callx4 a4
  384. j 1b
  385. #endif
  386. 5:
  387. #ifdef CONFIG_DEBUG_TLB_SANITY
  388. l32i a4, a1, PT_DEPC
  389. bgeui a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
  390. movi a4, check_tlb_sanity
  391. callx4 a4
  392. #endif
  393. 6:
  394. 4:
  395. #ifdef CONFIG_TRACE_IRQFLAGS
  396. l32i a4, a1, PT_DEPC
  397. /* Double exception means we came here with an exception
  398. * while PS.EXCM was set, i.e. interrupts disabled.
  399. */
  400. bgeui a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
  401. l32i a4, a1, PT_EXCCAUSE
  402. bnei a4, EXCCAUSE_LEVEL1_INTERRUPT, 1f
  403. /* We came here with an interrupt means interrupts were enabled
  404. * and we'll reenable them on return.
  405. */
  406. movi a4, trace_hardirqs_on
  407. callx4 a4
  408. 1:
  409. #endif
  410. /* Restore optional registers. */
  411. load_xtregs_opt a1 a2 a4 a5 a6 a7 PT_XTREGS_OPT
  412. /* Restore SCOMPARE1 */
  413. #if XCHAL_HAVE_S32C1I
  414. l32i a2, a1, PT_SCOMPARE1
  415. wsr a2, scompare1
  416. #endif
  417. wsr a3, ps /* disable interrupts */
  418. _bbci.l a3, PS_UM_BIT, kernel_exception_exit
  419. user_exception_exit:
  420. /* Restore the state of the task and return from the exception. */
  421. /* Switch to the user thread WINDOWBASE. Save SP temporarily in DEPC */
  422. l32i a2, a1, PT_WINDOWBASE
  423. l32i a3, a1, PT_WINDOWSTART
  424. wsr a1, depc # use DEPC as temp storage
  425. wsr a3, windowstart # restore WINDOWSTART
  426. ssr a2 # preserve user's WB in the SAR
  427. wsr a2, windowbase # switch to user's saved WB
  428. rsync
  429. rsr a1, depc # restore stack pointer
  430. l32i a2, a1, PT_WMASK # register frames saved (in bits 4...9)
  431. rotw -1 # we restore a4..a7
  432. _bltui a6, 16, 1f # only have to restore current window?
  433. /* The working registers are a0 and a3. We are restoring to
  434. * a4..a7. Be careful not to destroy what we have just restored.
  435. * Note: wmask has the format YYYYM:
  436. * Y: number of registers saved in groups of 4
  437. * M: 4 bit mask of first 16 registers
  438. */
  439. mov a2, a6
  440. mov a3, a5
  441. 2: rotw -1 # a0..a3 become a4..a7
  442. addi a3, a7, -4*4 # next iteration
  443. addi a2, a6, -16 # decrementing Y in WMASK
  444. l32i a4, a3, PT_AREG_END + 0
  445. l32i a5, a3, PT_AREG_END + 4
  446. l32i a6, a3, PT_AREG_END + 8
  447. l32i a7, a3, PT_AREG_END + 12
  448. _bgeui a2, 16, 2b
  449. /* Clear unrestored registers (don't leak anything to user-land */
  450. 1: rsr a0, windowbase
  451. rsr a3, sar
  452. sub a3, a0, a3
  453. beqz a3, 2f
  454. extui a3, a3, 0, WBBITS
  455. 1: rotw -1
  456. addi a3, a7, -1
  457. movi a4, 0
  458. movi a5, 0
  459. movi a6, 0
  460. movi a7, 0
  461. bgei a3, 1, 1b
  462. /* We are back were we were when we started.
  463. * Note: a2 still contains WMASK (if we've returned to the original
  464. * frame where we had loaded a2), or at least the lower 4 bits
  465. * (if we have restored WSBITS-1 frames).
  466. */
  467. #if XCHAL_HAVE_THREADPTR
  468. l32i a3, a1, PT_THREADPTR
  469. wur a3, threadptr
  470. #endif
  471. 2: j common_exception_exit
  472. /* This is the kernel exception exit.
  473. * We avoided to do a MOVSP when we entered the exception, but we
  474. * have to do it here.
  475. */
  476. kernel_exception_exit:
  477. /* Check if we have to do a movsp.
  478. *
  479. * We only have to do a movsp if the previous window-frame has
  480. * been spilled to the *temporary* exception stack instead of the
  481. * task's stack. This is the case if the corresponding bit in
  482. * WINDOWSTART for the previous window-frame was set before
  483. * (not spilled) but is zero now (spilled).
  484. * If this bit is zero, all other bits except the one for the
  485. * current window frame are also zero. So, we can use a simple test:
  486. * 'and' WINDOWSTART and WINDOWSTART-1:
  487. *
  488. * (XXXXXX1[0]* - 1) AND XXXXXX1[0]* = XXXXXX0[0]*
  489. *
  490. * The result is zero only if one bit was set.
  491. *
  492. * (Note: We might have gone through several task switches before
  493. * we come back to the current task, so WINDOWBASE might be
  494. * different from the time the exception occurred.)
  495. */
  496. /* Test WINDOWSTART before and after the exception.
  497. * We actually have WMASK, so we only have to test if it is 1 or not.
  498. */
  499. l32i a2, a1, PT_WMASK
  500. _beqi a2, 1, common_exception_exit # Spilled before exception,jump
  501. /* Test WINDOWSTART now. If spilled, do the movsp */
  502. rsr a3, windowstart
  503. addi a0, a3, -1
  504. and a3, a3, a0
  505. _bnez a3, common_exception_exit
  506. /* Do a movsp (we returned from a call4, so we have at least a0..a7) */
  507. addi a0, a1, -16
  508. l32i a3, a0, 0
  509. l32i a4, a0, 4
  510. s32i a3, a1, PT_SIZE+0
  511. s32i a4, a1, PT_SIZE+4
  512. l32i a3, a0, 8
  513. l32i a4, a0, 12
  514. s32i a3, a1, PT_SIZE+8
  515. s32i a4, a1, PT_SIZE+12
  516. /* Common exception exit.
  517. * We restore the special register and the current window frame, and
  518. * return from the exception.
  519. *
  520. * Note: We expect a2 to hold PT_WMASK
  521. */
  522. common_exception_exit:
  523. /* Restore address registers. */
  524. _bbsi.l a2, 1, 1f
  525. l32i a4, a1, PT_AREG4
  526. l32i a5, a1, PT_AREG5
  527. l32i a6, a1, PT_AREG6
  528. l32i a7, a1, PT_AREG7
  529. _bbsi.l a2, 2, 1f
  530. l32i a8, a1, PT_AREG8
  531. l32i a9, a1, PT_AREG9
  532. l32i a10, a1, PT_AREG10
  533. l32i a11, a1, PT_AREG11
  534. _bbsi.l a2, 3, 1f
  535. l32i a12, a1, PT_AREG12
  536. l32i a13, a1, PT_AREG13
  537. l32i a14, a1, PT_AREG14
  538. l32i a15, a1, PT_AREG15
  539. /* Restore PC, SAR */
  540. 1: l32i a2, a1, PT_PC
  541. l32i a3, a1, PT_SAR
  542. wsr a2, epc1
  543. wsr a3, sar
  544. /* Restore LBEG, LEND, LCOUNT */
  545. l32i a2, a1, PT_LBEG
  546. l32i a3, a1, PT_LEND
  547. wsr a2, lbeg
  548. l32i a2, a1, PT_LCOUNT
  549. wsr a3, lend
  550. wsr a2, lcount
  551. /* We control single stepping through the ICOUNTLEVEL register. */
  552. l32i a2, a1, PT_ICOUNTLEVEL
  553. movi a3, -2
  554. wsr a2, icountlevel
  555. wsr a3, icount
  556. /* Check if it was double exception. */
  557. l32i a0, a1, PT_DEPC
  558. l32i a3, a1, PT_AREG3
  559. l32i a2, a1, PT_AREG2
  560. _bgeui a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
  561. /* Restore a0...a3 and return */
  562. l32i a0, a1, PT_AREG0
  563. l32i a1, a1, PT_AREG1
  564. rfe
  565. 1: wsr a0, depc
  566. l32i a0, a1, PT_AREG0
  567. l32i a1, a1, PT_AREG1
  568. rfde
  569. ENDPROC(kernel_exception)
  570. /*
  571. * Debug exception handler.
  572. *
  573. * Currently, we don't support KGDB, so only user application can be debugged.
  574. *
  575. * When we get here, a0 is trashed and saved to excsave[debuglevel]
  576. */
  577. ENTRY(debug_exception)
  578. rsr a0, SREG_EPS + XCHAL_DEBUGLEVEL
  579. bbsi.l a0, PS_EXCM_BIT, 1f # exception mode
  580. /* Set EPC1 and EXCCAUSE */
  581. wsr a2, depc # save a2 temporarily
  582. rsr a2, SREG_EPC + XCHAL_DEBUGLEVEL
  583. wsr a2, epc1
  584. movi a2, EXCCAUSE_MAPPED_DEBUG
  585. wsr a2, exccause
  586. /* Restore PS to the value before the debug exc but with PS.EXCM set.*/
  587. movi a2, 1 << PS_EXCM_BIT
  588. or a2, a0, a2
  589. movi a0, debug_exception # restore a3, debug jump vector
  590. wsr a2, ps
  591. xsr a0, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
  592. /* Switch to kernel/user stack, restore jump vector, and save a0 */
  593. bbsi.l a2, PS_UM_BIT, 2f # jump if user mode
  594. addi a2, a1, -16-PT_SIZE # assume kernel stack
  595. s32i a0, a2, PT_AREG0
  596. movi a0, 0
  597. s32i a1, a2, PT_AREG1
  598. s32i a0, a2, PT_DEPC # mark it as a regular exception
  599. xsr a0, depc
  600. s32i a3, a2, PT_AREG3
  601. s32i a0, a2, PT_AREG2
  602. mov a1, a2
  603. j _kernel_exception
  604. 2: rsr a2, excsave1
  605. l32i a2, a2, EXC_TABLE_KSTK # load kernel stack pointer
  606. s32i a0, a2, PT_AREG0
  607. movi a0, 0
  608. s32i a1, a2, PT_AREG1
  609. s32i a0, a2, PT_DEPC
  610. xsr a0, depc
  611. s32i a3, a2, PT_AREG3
  612. s32i a0, a2, PT_AREG2
  613. mov a1, a2
  614. j _user_exception
  615. /* Debug exception while in exception mode. */
  616. 1: j 1b // FIXME!!
  617. ENDPROC(debug_exception)
  618. /*
  619. * We get here in case of an unrecoverable exception.
  620. * The only thing we can do is to be nice and print a panic message.
  621. * We only produce a single stack frame for panic, so ???
  622. *
  623. *
  624. * Entry conditions:
  625. *
  626. * - a0 contains the caller address; original value saved in excsave1.
  627. * - the original a0 contains a valid return address (backtrace) or 0.
  628. * - a2 contains a valid stackpointer
  629. *
  630. * Notes:
  631. *
  632. * - If the stack pointer could be invalid, the caller has to setup a
  633. * dummy stack pointer (e.g. the stack of the init_task)
  634. *
  635. * - If the return address could be invalid, the caller has to set it
  636. * to 0, so the backtrace would stop.
  637. *
  638. */
  639. .align 4
  640. unrecoverable_text:
  641. .ascii "Unrecoverable error in exception handler\0"
  642. ENTRY(unrecoverable_exception)
  643. movi a0, 1
  644. movi a1, 0
  645. wsr a0, windowstart
  646. wsr a1, windowbase
  647. rsync
  648. movi a1, (1 << PS_WOE_BIT) | LOCKLEVEL
  649. wsr a1, ps
  650. rsync
  651. movi a1, init_task
  652. movi a0, 0
  653. addi a1, a1, PT_REGS_OFFSET
  654. movi a4, panic
  655. movi a6, unrecoverable_text
  656. callx4 a4
  657. 1: j 1b
  658. ENDPROC(unrecoverable_exception)
  659. /* -------------------------- FAST EXCEPTION HANDLERS ----------------------- */
  660. /*
  661. * Fast-handler for alloca exceptions
  662. *
  663. * The ALLOCA handler is entered when user code executes the MOVSP
  664. * instruction and the caller's frame is not in the register file.
  665. *
  666. * This algorithm was taken from the Ross Morley's RTOS Porting Layer:
  667. *
  668. * /home/ross/rtos/porting/XtensaRTOS-PortingLayer-20090507/xtensa_vectors.S
  669. *
  670. * It leverages the existing window spill/fill routines and their support for
  671. * double exceptions. The 'movsp' instruction will only cause an exception if
  672. * the next window needs to be loaded. In fact this ALLOCA exception may be
  673. * replaced at some point by changing the hardware to do a underflow exception
  674. * of the proper size instead.
  675. *
  676. * This algorithm simply backs out the register changes started by the user
  677. * excpetion handler, makes it appear that we have started a window underflow
  678. * by rotating the window back and then setting the old window base (OWB) in
  679. * the 'ps' register with the rolled back window base. The 'movsp' instruction
  680. * will be re-executed and this time since the next window frames is in the
  681. * active AR registers it won't cause an exception.
  682. *
  683. * If the WindowUnderflow code gets a TLB miss the page will get mapped
  684. * the the partial windeowUnderflow will be handeled in the double exception
  685. * handler.
  686. *
  687. * Entry condition:
  688. *
  689. * a0: trashed, original value saved on stack (PT_AREG0)
  690. * a1: a1
  691. * a2: new stack pointer, original in DEPC
  692. * a3: a3
  693. * depc: a2, original value saved on stack (PT_DEPC)
  694. * excsave_1: dispatch table
  695. *
  696. * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
  697. * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
  698. */
  699. ENTRY(fast_alloca)
  700. rsr a0, windowbase
  701. rotw -1
  702. rsr a2, ps
  703. extui a3, a2, PS_OWB_SHIFT, PS_OWB_WIDTH
  704. xor a3, a3, a4
  705. l32i a4, a6, PT_AREG0
  706. l32i a1, a6, PT_DEPC
  707. rsr a6, depc
  708. wsr a1, depc
  709. slli a3, a3, PS_OWB_SHIFT
  710. xor a2, a2, a3
  711. wsr a2, ps
  712. rsync
  713. _bbci.l a4, 31, 4f
  714. rotw -1
  715. _bbci.l a8, 30, 8f
  716. rotw -1
  717. j _WindowUnderflow12
  718. 8: j _WindowUnderflow8
  719. 4: j _WindowUnderflow4
  720. ENDPROC(fast_alloca)
  721. /*
  722. * fast system calls.
  723. *
  724. * WARNING: The kernel doesn't save the entire user context before
  725. * handling a fast system call. These functions are small and short,
  726. * usually offering some functionality not available to user tasks.
  727. *
  728. * BE CAREFUL TO PRESERVE THE USER'S CONTEXT.
  729. *
  730. * Entry condition:
  731. *
  732. * a0: trashed, original value saved on stack (PT_AREG0)
  733. * a1: a1
  734. * a2: new stack pointer, original in DEPC
  735. * a3: a3
  736. * depc: a2, original value saved on stack (PT_DEPC)
  737. * excsave_1: dispatch table
  738. */
  739. ENTRY(fast_syscall_kernel)
  740. /* Skip syscall. */
  741. rsr a0, epc1
  742. addi a0, a0, 3
  743. wsr a0, epc1
  744. l32i a0, a2, PT_DEPC
  745. bgeui a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
  746. rsr a0, depc # get syscall-nr
  747. _beqz a0, fast_syscall_spill_registers
  748. _beqi a0, __NR_xtensa, fast_syscall_xtensa
  749. j kernel_exception
  750. ENDPROC(fast_syscall_kernel)
  751. ENTRY(fast_syscall_user)
  752. /* Skip syscall. */
  753. rsr a0, epc1
  754. addi a0, a0, 3
  755. wsr a0, epc1
  756. l32i a0, a2, PT_DEPC
  757. bgeui a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
  758. rsr a0, depc # get syscall-nr
  759. _beqz a0, fast_syscall_spill_registers
  760. _beqi a0, __NR_xtensa, fast_syscall_xtensa
  761. j user_exception
  762. ENDPROC(fast_syscall_user)
  763. ENTRY(fast_syscall_unrecoverable)
  764. /* Restore all states. */
  765. l32i a0, a2, PT_AREG0 # restore a0
  766. xsr a2, depc # restore a2, depc
  767. wsr a0, excsave1
  768. movi a0, unrecoverable_exception
  769. callx0 a0
  770. ENDPROC(fast_syscall_unrecoverable)
  771. /*
  772. * sysxtensa syscall handler
  773. *
  774. * int sysxtensa (SYS_XTENSA_ATOMIC_SET, ptr, val, unused);
  775. * int sysxtensa (SYS_XTENSA_ATOMIC_ADD, ptr, val, unused);
  776. * int sysxtensa (SYS_XTENSA_ATOMIC_EXG_ADD, ptr, val, unused);
  777. * int sysxtensa (SYS_XTENSA_ATOMIC_CMP_SWP, ptr, oldval, newval);
  778. * a2 a6 a3 a4 a5
  779. *
  780. * Entry condition:
  781. *
  782. * a0: a2 (syscall-nr), original value saved on stack (PT_AREG0)
  783. * a1: a1
  784. * a2: new stack pointer, original in a0 and DEPC
  785. * a3: a3
  786. * a4..a15: unchanged
  787. * depc: a2, original value saved on stack (PT_DEPC)
  788. * excsave_1: dispatch table
  789. *
  790. * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
  791. * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
  792. *
  793. * Note: we don't have to save a2; a2 holds the return value
  794. *
  795. * We use the two macros TRY and CATCH:
  796. *
  797. * TRY adds an entry to the __ex_table fixup table for the immediately
  798. * following instruction.
  799. *
  800. * CATCH catches any exception that occurred at one of the preceding TRY
  801. * statements and continues from there
  802. *
  803. * Usage TRY l32i a0, a1, 0
  804. * <other code>
  805. * done: rfe
  806. * CATCH <set return code>
  807. * j done
  808. */
  809. #define TRY \
  810. .section __ex_table, "a"; \
  811. .word 66f, 67f; \
  812. .text; \
  813. 66:
  814. #define CATCH \
  815. 67:
  816. ENTRY(fast_syscall_xtensa)
  817. s32i a7, a2, PT_AREG7 # we need an additional register
  818. movi a7, 4 # sizeof(unsigned int)
  819. access_ok a3, a7, a0, a2, .Leac # a0: scratch reg, a2: sp
  820. addi a6, a6, -1 # assuming SYS_XTENSA_ATOMIC_SET = 1
  821. _bgeui a6, SYS_XTENSA_COUNT - 1, .Lill
  822. _bnei a6, SYS_XTENSA_ATOMIC_CMP_SWP - 1, .Lnswp
  823. /* Fall through for ATOMIC_CMP_SWP. */
  824. .Lswp: /* Atomic compare and swap */
  825. TRY l32i a0, a3, 0 # read old value
  826. bne a0, a4, 1f # same as old value? jump
  827. TRY s32i a5, a3, 0 # different, modify value
  828. l32i a7, a2, PT_AREG7 # restore a7
  829. l32i a0, a2, PT_AREG0 # restore a0
  830. movi a2, 1 # and return 1
  831. addi a6, a6, 1 # restore a6 (really necessary?)
  832. rfe
  833. 1: l32i a7, a2, PT_AREG7 # restore a7
  834. l32i a0, a2, PT_AREG0 # restore a0
  835. movi a2, 0 # return 0 (note that we cannot set
  836. addi a6, a6, 1 # restore a6 (really necessary?)
  837. rfe
  838. .Lnswp: /* Atomic set, add, and exg_add. */
  839. TRY l32i a7, a3, 0 # orig
  840. add a0, a4, a7 # + arg
  841. moveqz a0, a4, a6 # set
  842. TRY s32i a0, a3, 0 # write new value
  843. mov a0, a2
  844. mov a2, a7
  845. l32i a7, a0, PT_AREG7 # restore a7
  846. l32i a0, a0, PT_AREG0 # restore a0
  847. addi a6, a6, 1 # restore a6 (really necessary?)
  848. rfe
  849. CATCH
  850. .Leac: l32i a7, a2, PT_AREG7 # restore a7
  851. l32i a0, a2, PT_AREG0 # restore a0
  852. movi a2, -EFAULT
  853. rfe
  854. .Lill: l32i a7, a2, PT_AREG0 # restore a7
  855. l32i a0, a2, PT_AREG0 # restore a0
  856. movi a2, -EINVAL
  857. rfe
  858. ENDPROC(fast_syscall_xtensa)
  859. /* fast_syscall_spill_registers.
  860. *
  861. * Entry condition:
  862. *
  863. * a0: trashed, original value saved on stack (PT_AREG0)
  864. * a1: a1
  865. * a2: new stack pointer, original in DEPC
  866. * a3: a3
  867. * depc: a2, original value saved on stack (PT_DEPC)
  868. * excsave_1: dispatch table
  869. *
  870. * Note: We assume the stack pointer is EXC_TABLE_KSTK in the fixup handler.
  871. */
  872. ENTRY(fast_syscall_spill_registers)
  873. /* Register a FIXUP handler (pass current wb as a parameter) */
  874. xsr a3, excsave1
  875. movi a0, fast_syscall_spill_registers_fixup
  876. s32i a0, a3, EXC_TABLE_FIXUP
  877. rsr a0, windowbase
  878. s32i a0, a3, EXC_TABLE_PARAM
  879. xsr a3, excsave1 # restore a3 and excsave_1
  880. /* Save a3, a4 and SAR on stack. */
  881. rsr a0, sar
  882. s32i a3, a2, PT_AREG3
  883. s32i a4, a2, PT_AREG4
  884. s32i a0, a2, PT_AREG5 # store SAR to PT_AREG5
  885. /* The spill routine might clobber a7, a11, and a15. */
  886. s32i a7, a2, PT_AREG7
  887. s32i a11, a2, PT_AREG11
  888. s32i a15, a2, PT_AREG15
  889. call0 _spill_registers # destroys a3, a4, and SAR
  890. /* Advance PC, restore registers and SAR, and return from exception. */
  891. l32i a3, a2, PT_AREG5
  892. l32i a4, a2, PT_AREG4
  893. l32i a0, a2, PT_AREG0
  894. wsr a3, sar
  895. l32i a3, a2, PT_AREG3
  896. /* Restore clobbered registers. */
  897. l32i a7, a2, PT_AREG7
  898. l32i a11, a2, PT_AREG11
  899. l32i a15, a2, PT_AREG15
  900. movi a2, 0
  901. rfe
  902. ENDPROC(fast_syscall_spill_registers)
  903. /* Fixup handler.
  904. *
  905. * We get here if the spill routine causes an exception, e.g. tlb miss.
  906. * We basically restore WINDOWBASE and WINDOWSTART to the condition when
  907. * we entered the spill routine and jump to the user exception handler.
  908. *
  909. * a0: value of depc, original value in depc
  910. * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
  911. * a3: exctable, original value in excsave1
  912. */
  913. fast_syscall_spill_registers_fixup:
  914. rsr a2, windowbase # get current windowbase (a2 is saved)
  915. xsr a0, depc # restore depc and a0
  916. ssl a2 # set shift (32 - WB)
  917. /* We need to make sure the current registers (a0-a3) are preserved.
  918. * To do this, we simply set the bit for the current window frame
  919. * in WS, so that the exception handlers save them to the task stack.
  920. */
  921. xsr a3, excsave1 # get spill-mask
  922. slli a2, a3, 1 # shift left by one
  923. slli a3, a2, 32-WSBITS
  924. src a2, a2, a3 # a1 = xxwww1yyxxxwww1yy......
  925. wsr a2, windowstart # set corrected windowstart
  926. rsr a3, excsave1
  927. l32i a2, a3, EXC_TABLE_DOUBLE_SAVE # restore a2
  928. l32i a3, a3, EXC_TABLE_PARAM # original WB (in user task)
  929. /* Return to the original (user task) WINDOWBASE.
  930. * We leave the following frame behind:
  931. * a0, a1, a2 same
  932. * a3: trashed (saved in excsave_1)
  933. * depc: depc (we have to return to that address)
  934. * excsave_1: a3
  935. */
  936. wsr a3, windowbase
  937. rsync
  938. /* We are now in the original frame when we entered _spill_registers:
  939. * a0: return address
  940. * a1: used, stack pointer
  941. * a2: kernel stack pointer
  942. * a3: available, saved in EXCSAVE_1
  943. * depc: exception address
  944. * excsave: a3
  945. * Note: This frame might be the same as above.
  946. */
  947. /* Setup stack pointer. */
  948. addi a2, a2, -PT_USER_SIZE
  949. s32i a0, a2, PT_AREG0
  950. /* Make sure we return to this fixup handler. */
  951. movi a3, fast_syscall_spill_registers_fixup_return
  952. s32i a3, a2, PT_DEPC # setup depc
  953. /* Jump to the exception handler. */
  954. rsr a3, excsave1
  955. rsr a0, exccause
  956. addx4 a0, a0, a3 # find entry in table
  957. l32i a0, a0, EXC_TABLE_FAST_USER # load handler
  958. jx a0
  959. fast_syscall_spill_registers_fixup_return:
  960. /* When we return here, all registers have been restored (a2: DEPC) */
  961. wsr a2, depc # exception address
  962. /* Restore fixup handler. */
  963. xsr a3, excsave1
  964. movi a2, fast_syscall_spill_registers_fixup
  965. s32i a2, a3, EXC_TABLE_FIXUP
  966. s32i a0, a3, EXC_TABLE_DOUBLE_SAVE
  967. rsr a2, windowbase
  968. s32i a2, a3, EXC_TABLE_PARAM
  969. l32i a2, a3, EXC_TABLE_KSTK
  970. /* Load WB at the time the exception occurred. */
  971. rsr a3, sar # WB is still in SAR
  972. neg a3, a3
  973. wsr a3, windowbase
  974. rsync
  975. rfde
  976. /*
  977. * spill all registers.
  978. *
  979. * This is not a real function. The following conditions must be met:
  980. *
  981. * - must be called with call0.
  982. * - uses a3, a4 and SAR.
  983. * - the last 'valid' register of each frame are clobbered.
  984. * - the caller must have registered a fixup handler
  985. * (or be inside a critical section)
  986. * - PS_EXCM must be set (PS_WOE cleared?)
  987. */
  988. ENTRY(_spill_registers)
  989. /*
  990. * Rotate ws so that the current windowbase is at bit 0.
  991. * Assume ws = xxxwww1yy (www1 current window frame).
  992. * Rotate ws right so that a4 = yyxxxwww1.
  993. */
  994. rsr a4, windowbase
  995. rsr a3, windowstart # a3 = xxxwww1yy
  996. ssr a4 # holds WB
  997. slli a4, a3, WSBITS
  998. or a3, a3, a4 # a3 = xxxwww1yyxxxwww1yy
  999. srl a3, a3 # a3 = 00xxxwww1yyxxxwww1
  1000. /* We are done if there are no more than the current register frame. */
  1001. extui a3, a3, 1, WSBITS-1 # a3 = 0yyxxxwww
  1002. movi a4, (1 << (WSBITS-1))
  1003. _beqz a3, .Lnospill # only one active frame? jump
  1004. /* We want 1 at the top, so that we return to the current windowbase */
  1005. or a3, a3, a4 # 1yyxxxwww
  1006. /* Skip empty frames - get 'oldest' WINDOWSTART-bit. */
  1007. wsr a3, windowstart # save shifted windowstart
  1008. neg a4, a3
  1009. and a3, a4, a3 # first bit set from right: 000010000
  1010. ffs_ws a4, a3 # a4: shifts to skip empty frames
  1011. movi a3, WSBITS
  1012. sub a4, a3, a4 # WSBITS-a4:number of 0-bits from right
  1013. ssr a4 # save in SAR for later.
  1014. rsr a3, windowbase
  1015. add a3, a3, a4
  1016. wsr a3, windowbase
  1017. rsync
  1018. rsr a3, windowstart
  1019. srl a3, a3 # shift windowstart
  1020. /* WB is now just one frame below the oldest frame in the register
  1021. window. WS is shifted so the oldest frame is in bit 0, thus, WB
  1022. and WS differ by one 4-register frame. */
  1023. /* Save frames. Depending what call was used (call4, call8, call12),
  1024. * we have to save 4,8. or 12 registers.
  1025. */
  1026. _bbsi.l a3, 1, .Lc4
  1027. _bbsi.l a3, 2, .Lc8
  1028. /* Special case: we have a call12-frame starting at a4. */
  1029. _bbci.l a3, 3, .Lc12 # bit 3 shouldn't be zero! (Jump to Lc12 first)
  1030. s32e a4, a1, -16 # a1 is valid with an empty spill area
  1031. l32e a4, a5, -12
  1032. s32e a8, a4, -48
  1033. mov a8, a4
  1034. l32e a4, a1, -16
  1035. j .Lc12c
  1036. .Lnospill:
  1037. ret
  1038. .Lloop: _bbsi.l a3, 1, .Lc4
  1039. _bbci.l a3, 2, .Lc12
  1040. .Lc8: s32e a4, a13, -16
  1041. l32e a4, a5, -12
  1042. s32e a8, a4, -32
  1043. s32e a5, a13, -12
  1044. s32e a6, a13, -8
  1045. s32e a7, a13, -4
  1046. s32e a9, a4, -28
  1047. s32e a10, a4, -24
  1048. s32e a11, a4, -20
  1049. srli a11, a3, 2 # shift windowbase by 2
  1050. rotw 2
  1051. _bnei a3, 1, .Lloop
  1052. .Lexit: /* Done. Do the final rotation, set WS, and return. */
  1053. rotw 1
  1054. rsr a3, windowbase
  1055. ssl a3
  1056. movi a3, 1
  1057. sll a3, a3
  1058. wsr a3, windowstart
  1059. ret
  1060. .Lc4: s32e a4, a9, -16
  1061. s32e a5, a9, -12
  1062. s32e a6, a9, -8
  1063. s32e a7, a9, -4
  1064. srli a7, a3, 1
  1065. rotw 1
  1066. _bnei a3, 1, .Lloop
  1067. j .Lexit
  1068. .Lc12: _bbci.l a3, 3, .Linvalid_mask # bit 2 shouldn't be zero!
  1069. /* 12-register frame (call12) */
  1070. l32e a2, a5, -12
  1071. s32e a8, a2, -48
  1072. mov a8, a2
  1073. .Lc12c: s32e a9, a8, -44
  1074. s32e a10, a8, -40
  1075. s32e a11, a8, -36
  1076. s32e a12, a8, -32
  1077. s32e a13, a8, -28
  1078. s32e a14, a8, -24
  1079. s32e a15, a8, -20
  1080. srli a15, a3, 3
  1081. /* The stack pointer for a4..a7 is out of reach, so we rotate the
  1082. * window, grab the stackpointer, and rotate back.
  1083. * Alternatively, we could also use the following approach, but that
  1084. * makes the fixup routine much more complicated:
  1085. * rotw 1
  1086. * s32e a0, a13, -16
  1087. * ...
  1088. * rotw 2
  1089. */
  1090. rotw 1
  1091. mov a5, a13
  1092. rotw -1
  1093. s32e a4, a9, -16
  1094. s32e a5, a9, -12
  1095. s32e a6, a9, -8
  1096. s32e a7, a9, -4
  1097. rotw 3
  1098. _beqi a3, 1, .Lexit
  1099. j .Lloop
  1100. .Linvalid_mask:
  1101. /* We get here because of an unrecoverable error in the window
  1102. * registers. If we are in user space, we kill the application,
  1103. * however, this condition is unrecoverable in kernel space.
  1104. */
  1105. rsr a0, ps
  1106. _bbci.l a0, PS_UM_BIT, 1f
  1107. /* User space: Setup a dummy frame and kill application.
  1108. * Note: We assume EXC_TABLE_KSTK contains a valid stack pointer.
  1109. */
  1110. movi a0, 1
  1111. movi a1, 0
  1112. wsr a0, windowstart
  1113. wsr a1, windowbase
  1114. rsync
  1115. movi a0, 0
  1116. rsr a3, excsave1
  1117. l32i a1, a3, EXC_TABLE_KSTK
  1118. movi a4, (1 << PS_WOE_BIT) | LOCKLEVEL
  1119. wsr a4, ps
  1120. rsync
  1121. movi a6, SIGSEGV
  1122. movi a4, do_exit
  1123. callx4 a4
  1124. 1: /* Kernel space: PANIC! */
  1125. wsr a0, excsave1
  1126. movi a0, unrecoverable_exception
  1127. callx0 a0 # should not return
  1128. 1: j 1b
  1129. ENDPROC(_spill_registers)
  1130. #ifdef CONFIG_MMU
  1131. /*
  1132. * We should never get here. Bail out!
  1133. */
  1134. ENTRY(fast_second_level_miss_double_kernel)
  1135. 1: movi a0, unrecoverable_exception
  1136. callx0 a0 # should not return
  1137. 1: j 1b
  1138. ENDPROC(fast_second_level_miss_double_kernel)
  1139. /* First-level entry handler for user, kernel, and double 2nd-level
  1140. * TLB miss exceptions. Note that for now, user and kernel miss
  1141. * exceptions share the same entry point and are handled identically.
  1142. *
  1143. * An old, less-efficient C version of this function used to exist.
  1144. * We include it below, interleaved as comments, for reference.
  1145. *
  1146. * Entry condition:
  1147. *
  1148. * a0: trashed, original value saved on stack (PT_AREG0)
  1149. * a1: a1
  1150. * a2: new stack pointer, original in DEPC
  1151. * a3: a3
  1152. * depc: a2, original value saved on stack (PT_DEPC)
  1153. * excsave_1: dispatch table
  1154. *
  1155. * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
  1156. * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
  1157. */
  1158. ENTRY(fast_second_level_miss)
  1159. /* Save a1 and a3. Note: we don't expect a double exception. */
  1160. s32i a1, a2, PT_AREG1
  1161. s32i a3, a2, PT_AREG3
  1162. /* We need to map the page of PTEs for the user task. Find
  1163. * the pointer to that page. Also, it's possible for tsk->mm
  1164. * to be NULL while tsk->active_mm is nonzero if we faulted on
  1165. * a vmalloc address. In that rare case, we must use
  1166. * active_mm instead to avoid a fault in this handler. See
  1167. *
  1168. * http://mail.nl.linux.org/linux-mm/2002-08/msg00258.html
  1169. * (or search Internet on "mm vs. active_mm")
  1170. *
  1171. * if (!mm)
  1172. * mm = tsk->active_mm;
  1173. * pgd = pgd_offset (mm, regs->excvaddr);
  1174. * pmd = pmd_offset (pgd, regs->excvaddr);
  1175. * pmdval = *pmd;
  1176. */
  1177. GET_CURRENT(a1,a2)
  1178. l32i a0, a1, TASK_MM # tsk->mm
  1179. beqz a0, 9f
  1180. 8: rsr a3, excvaddr # fault address
  1181. _PGD_OFFSET(a0, a3, a1)
  1182. l32i a0, a0, 0 # read pmdval
  1183. beqz a0, 2f
  1184. /* Read ptevaddr and convert to top of page-table page.
  1185. *
  1186. * vpnval = read_ptevaddr_register() & PAGE_MASK;
  1187. * vpnval += DTLB_WAY_PGTABLE;
  1188. * pteval = mk_pte (virt_to_page(pmd_val(pmdval)), PAGE_KERNEL);
  1189. * write_dtlb_entry (pteval, vpnval);
  1190. *
  1191. * The messy computation for 'pteval' above really simplifies
  1192. * into the following:
  1193. *
  1194. * pteval = ((pmdval - PAGE_OFFSET) & PAGE_MASK) | PAGE_DIRECTORY
  1195. */
  1196. movi a1, (-PAGE_OFFSET) & 0xffffffff
  1197. add a0, a0, a1 # pmdval - PAGE_OFFSET
  1198. extui a1, a0, 0, PAGE_SHIFT # ... & PAGE_MASK
  1199. xor a0, a0, a1
  1200. movi a1, _PAGE_DIRECTORY
  1201. or a0, a0, a1 # ... | PAGE_DIRECTORY
  1202. /*
  1203. * We utilize all three wired-ways (7-9) to hold pmd translations.
  1204. * Memory regions are mapped to the DTLBs according to bits 28 and 29.
  1205. * This allows to map the three most common regions to three different
  1206. * DTLBs:
  1207. * 0,1 -> way 7 program (0040.0000) and virtual (c000.0000)
  1208. * 2 -> way 8 shared libaries (2000.0000)
  1209. * 3 -> way 0 stack (3000.0000)
  1210. */
  1211. extui a3, a3, 28, 2 # addr. bit 28 and 29 0,1,2,3
  1212. rsr a1, ptevaddr
  1213. addx2 a3, a3, a3 # -> 0,3,6,9
  1214. srli a1, a1, PAGE_SHIFT
  1215. extui a3, a3, 2, 2 # -> 0,0,1,2
  1216. slli a1, a1, PAGE_SHIFT # ptevaddr & PAGE_MASK
  1217. addi a3, a3, DTLB_WAY_PGD
  1218. add a1, a1, a3 # ... + way_number
  1219. 3: wdtlb a0, a1
  1220. dsync
  1221. /* Exit critical section. */
  1222. 4: rsr a3, excsave1
  1223. movi a0, 0
  1224. s32i a0, a3, EXC_TABLE_FIXUP
  1225. /* Restore the working registers, and return. */
  1226. l32i a0, a2, PT_AREG0
  1227. l32i a1, a2, PT_AREG1
  1228. l32i a3, a2, PT_AREG3
  1229. l32i a2, a2, PT_DEPC
  1230. bgeui a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
  1231. /* Restore excsave1 and return. */
  1232. rsr a2, depc
  1233. rfe
  1234. /* Return from double exception. */
  1235. 1: xsr a2, depc
  1236. esync
  1237. rfde
  1238. 9: l32i a0, a1, TASK_ACTIVE_MM # unlikely case mm == 0
  1239. j 8b
  1240. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  1241. 2: /* Special case for cache aliasing.
  1242. * We (should) only get here if a clear_user_page, copy_user_page
  1243. * or the aliased cache flush functions got preemptively interrupted
  1244. * by another task. Re-establish temporary mapping to the
  1245. * TLBTEMP_BASE areas.
  1246. */
  1247. /* We shouldn't be in a double exception */
  1248. l32i a0, a2, PT_DEPC
  1249. bgeui a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 2f
  1250. /* Make sure the exception originated in the special functions */
  1251. movi a0, __tlbtemp_mapping_start
  1252. rsr a3, epc1
  1253. bltu a3, a0, 2f
  1254. movi a0, __tlbtemp_mapping_end
  1255. bgeu a3, a0, 2f
  1256. /* Check if excvaddr was in one of the TLBTEMP_BASE areas. */
  1257. movi a3, TLBTEMP_BASE_1
  1258. rsr a0, excvaddr
  1259. bltu a0, a3, 2f
  1260. addi a1, a0, -(2 << (DCACHE_ALIAS_ORDER + PAGE_SHIFT))
  1261. bgeu a1, a3, 2f
  1262. /* Check if we have to restore an ITLB mapping. */
  1263. movi a1, __tlbtemp_mapping_itlb
  1264. rsr a3, epc1
  1265. sub a3, a3, a1
  1266. /* Calculate VPN */
  1267. movi a1, PAGE_MASK
  1268. and a1, a1, a0
  1269. /* Jump for ITLB entry */
  1270. bgez a3, 1f
  1271. /* We can use up to two TLBTEMP areas, one for src and one for dst. */
  1272. extui a3, a0, PAGE_SHIFT + DCACHE_ALIAS_ORDER, 1
  1273. add a1, a3, a1
  1274. /* PPN is in a6 for the first TLBTEMP area and in a7 for the second. */
  1275. mov a0, a6
  1276. movnez a0, a7, a3
  1277. j 3b
  1278. /* ITLB entry. We only use dst in a6. */
  1279. 1: witlb a6, a1
  1280. isync
  1281. j 4b
  1282. #endif // DCACHE_WAY_SIZE > PAGE_SIZE
  1283. 2: /* Invalid PGD, default exception handling */
  1284. rsr a1, depc
  1285. s32i a1, a2, PT_AREG2
  1286. mov a1, a2
  1287. rsr a2, ps
  1288. bbsi.l a2, PS_UM_BIT, 1f
  1289. j _kernel_exception
  1290. 1: j _user_exception
  1291. ENDPROC(fast_second_level_miss)
  1292. /*
  1293. * StoreProhibitedException
  1294. *
  1295. * Update the pte and invalidate the itlb mapping for this pte.
  1296. *
  1297. * Entry condition:
  1298. *
  1299. * a0: trashed, original value saved on stack (PT_AREG0)
  1300. * a1: a1
  1301. * a2: new stack pointer, original in DEPC
  1302. * a3: a3
  1303. * depc: a2, original value saved on stack (PT_DEPC)
  1304. * excsave_1: dispatch table
  1305. *
  1306. * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
  1307. * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
  1308. */
  1309. ENTRY(fast_store_prohibited)
  1310. /* Save a1 and a3. */
  1311. s32i a1, a2, PT_AREG1
  1312. s32i a3, a2, PT_AREG3
  1313. GET_CURRENT(a1,a2)
  1314. l32i a0, a1, TASK_MM # tsk->mm
  1315. beqz a0, 9f
  1316. 8: rsr a1, excvaddr # fault address
  1317. _PGD_OFFSET(a0, a1, a3)
  1318. l32i a0, a0, 0
  1319. beqz a0, 2f
  1320. /*
  1321. * Note that we test _PAGE_WRITABLE_BIT only if PTE is present
  1322. * and is not PAGE_NONE. See pgtable.h for possible PTE layouts.
  1323. */
  1324. _PTE_OFFSET(a0, a1, a3)
  1325. l32i a3, a0, 0 # read pteval
  1326. movi a1, _PAGE_CA_INVALID
  1327. ball a3, a1, 2f
  1328. bbci.l a3, _PAGE_WRITABLE_BIT, 2f
  1329. movi a1, _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_HW_WRITE
  1330. or a3, a3, a1
  1331. rsr a1, excvaddr
  1332. s32i a3, a0, 0
  1333. /* We need to flush the cache if we have page coloring. */
  1334. #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
  1335. dhwb a0, 0
  1336. #endif
  1337. pdtlb a0, a1
  1338. wdtlb a3, a0
  1339. /* Exit critical section. */
  1340. movi a0, 0
  1341. rsr a3, excsave1
  1342. s32i a0, a3, EXC_TABLE_FIXUP
  1343. /* Restore the working registers, and return. */
  1344. l32i a3, a2, PT_AREG3
  1345. l32i a1, a2, PT_AREG1
  1346. l32i a0, a2, PT_AREG0
  1347. l32i a2, a2, PT_DEPC
  1348. bgeui a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
  1349. rsr a2, depc
  1350. rfe
  1351. /* Double exception. Restore FIXUP handler and return. */
  1352. 1: xsr a2, depc
  1353. esync
  1354. rfde
  1355. 9: l32i a0, a1, TASK_ACTIVE_MM # unlikely case mm == 0
  1356. j 8b
  1357. 2: /* If there was a problem, handle fault in C */
  1358. rsr a3, depc # still holds a2
  1359. s32i a3, a2, PT_AREG2
  1360. mov a1, a2
  1361. rsr a2, ps
  1362. bbsi.l a2, PS_UM_BIT, 1f
  1363. j _kernel_exception
  1364. 1: j _user_exception
  1365. ENDPROC(fast_store_prohibited)
  1366. #endif /* CONFIG_MMU */
  1367. /*
  1368. * System Calls.
  1369. *
  1370. * void system_call (struct pt_regs* regs, int exccause)
  1371. * a2 a3
  1372. */
  1373. ENTRY(system_call)
  1374. entry a1, 32
  1375. /* regs->syscall = regs->areg[2] */
  1376. l32i a3, a2, PT_AREG2
  1377. mov a6, a2
  1378. movi a4, do_syscall_trace_enter
  1379. s32i a3, a2, PT_SYSCALL
  1380. callx4 a4
  1381. /* syscall = sys_call_table[syscall_nr] */
  1382. movi a4, sys_call_table;
  1383. movi a5, __NR_syscall_count
  1384. movi a6, -ENOSYS
  1385. bgeu a3, a5, 1f
  1386. addx4 a4, a3, a4
  1387. l32i a4, a4, 0
  1388. movi a5, sys_ni_syscall;
  1389. beq a4, a5, 1f
  1390. /* Load args: arg0 - arg5 are passed via regs. */
  1391. l32i a6, a2, PT_AREG6
  1392. l32i a7, a2, PT_AREG3
  1393. l32i a8, a2, PT_AREG4
  1394. l32i a9, a2, PT_AREG5
  1395. l32i a10, a2, PT_AREG8
  1396. l32i a11, a2, PT_AREG9
  1397. /* Pass one additional argument to the syscall: pt_regs (on stack) */
  1398. s32i a2, a1, 0
  1399. callx4 a4
  1400. 1: /* regs->areg[2] = return_value */
  1401. s32i a6, a2, PT_AREG2
  1402. movi a4, do_syscall_trace_leave
  1403. mov a6, a2
  1404. callx4 a4
  1405. retw
  1406. ENDPROC(system_call)
  1407. /*
  1408. * Task switch.
  1409. *
  1410. * struct task* _switch_to (struct task* prev, struct task* next)
  1411. * a2 a2 a3
  1412. */
  1413. ENTRY(_switch_to)
  1414. entry a1, 16
  1415. mov a12, a2 # preserve 'prev' (a2)
  1416. mov a13, a3 # and 'next' (a3)
  1417. l32i a4, a2, TASK_THREAD_INFO
  1418. l32i a5, a3, TASK_THREAD_INFO
  1419. save_xtregs_user a4 a6 a8 a9 a10 a11 THREAD_XTREGS_USER
  1420. s32i a0, a12, THREAD_RA # save return address
  1421. s32i a1, a12, THREAD_SP # save stack pointer
  1422. /* Disable ints while we manipulate the stack pointer. */
  1423. movi a14, (1 << PS_EXCM_BIT) | LOCKLEVEL
  1424. xsr a14, ps
  1425. rsr a3, excsave1
  1426. rsync
  1427. s32i a3, a3, EXC_TABLE_FIXUP /* enter critical section */
  1428. /* Switch CPENABLE */
  1429. #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
  1430. l32i a3, a5, THREAD_CPENABLE
  1431. xsr a3, cpenable
  1432. s32i a3, a4, THREAD_CPENABLE
  1433. #endif
  1434. /* Flush register file. */
  1435. call0 _spill_registers # destroys a3, a4, and SAR
  1436. /* Set kernel stack (and leave critical section)
  1437. * Note: It's save to set it here. The stack will not be overwritten
  1438. * because the kernel stack will only be loaded again after
  1439. * we return from kernel space.
  1440. */
  1441. rsr a3, excsave1 # exc_table
  1442. movi a6, 0
  1443. addi a7, a5, PT_REGS_OFFSET
  1444. s32i a6, a3, EXC_TABLE_FIXUP
  1445. s32i a7, a3, EXC_TABLE_KSTK
  1446. /* restore context of the task 'next' */
  1447. l32i a0, a13, THREAD_RA # restore return address
  1448. l32i a1, a13, THREAD_SP # restore stack pointer
  1449. load_xtregs_user a5 a6 a8 a9 a10 a11 THREAD_XTREGS_USER
  1450. wsr a14, ps
  1451. mov a2, a12 # return 'prev'
  1452. rsync
  1453. retw
  1454. ENDPROC(_switch_to)
  1455. ENTRY(ret_from_fork)
  1456. /* void schedule_tail (struct task_struct *prev)
  1457. * Note: prev is still in a6 (return value from fake call4 frame)
  1458. */
  1459. movi a4, schedule_tail
  1460. callx4 a4
  1461. movi a4, do_syscall_trace_leave
  1462. mov a6, a1
  1463. callx4 a4
  1464. j common_exception_return
  1465. ENDPROC(ret_from_fork)
  1466. /*
  1467. * Kernel thread creation helper
  1468. * On entry, set up by copy_thread: a2 = thread_fn, a3 = thread_fn arg
  1469. * left from _switch_to: a6 = prev
  1470. */
  1471. ENTRY(ret_from_kernel_thread)
  1472. call4 schedule_tail
  1473. mov a6, a3
  1474. callx4 a2
  1475. j common_exception_return
  1476. ENDPROC(ret_from_kernel_thread)