entry.S 47 KB

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