entry.S 46 KB

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