entry.S 50 KB

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