entry.S 46 KB

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