entry.S 47 KB

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