entry.S 45 KB

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