entry.S 46 KB

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