entry.S 45 KB

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