intvec_64.S 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. /*
  2. * Copyright 2011 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Linux interrupt vectors.
  15. */
  16. #include <linux/linkage.h>
  17. #include <linux/errno.h>
  18. #include <linux/unistd.h>
  19. #include <linux/init.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/thread_info.h>
  22. #include <asm/irqflags.h>
  23. #include <asm/asm-offsets.h>
  24. #include <asm/types.h>
  25. #include <asm/traps.h>
  26. #include <asm/signal.h>
  27. #include <hv/hypervisor.h>
  28. #include <arch/abi.h>
  29. #include <arch/interrupts.h>
  30. #include <arch/spr_def.h>
  31. #define PTREGS_PTR(reg, ptreg) addli reg, sp, C_ABI_SAVE_AREA_SIZE + (ptreg)
  32. #define PTREGS_OFFSET_SYSCALL PTREGS_OFFSET_REG(TREG_SYSCALL_NR)
  33. .macro push_reg reg, ptr=sp, delta=-8
  34. {
  35. st \ptr, \reg
  36. addli \ptr, \ptr, \delta
  37. }
  38. .endm
  39. .macro pop_reg reg, ptr=sp, delta=8
  40. {
  41. ld \reg, \ptr
  42. addli \ptr, \ptr, \delta
  43. }
  44. .endm
  45. .macro pop_reg_zero reg, zreg, ptr=sp, delta=8
  46. {
  47. move \zreg, zero
  48. ld \reg, \ptr
  49. addi \ptr, \ptr, \delta
  50. }
  51. .endm
  52. .macro push_extra_callee_saves reg
  53. PTREGS_PTR(\reg, PTREGS_OFFSET_REG(51))
  54. push_reg r51, \reg
  55. push_reg r50, \reg
  56. push_reg r49, \reg
  57. push_reg r48, \reg
  58. push_reg r47, \reg
  59. push_reg r46, \reg
  60. push_reg r45, \reg
  61. push_reg r44, \reg
  62. push_reg r43, \reg
  63. push_reg r42, \reg
  64. push_reg r41, \reg
  65. push_reg r40, \reg
  66. push_reg r39, \reg
  67. push_reg r38, \reg
  68. push_reg r37, \reg
  69. push_reg r36, \reg
  70. push_reg r35, \reg
  71. push_reg r34, \reg, PTREGS_OFFSET_BASE - PTREGS_OFFSET_REG(34)
  72. .endm
  73. .macro panic str
  74. .pushsection .rodata, "a"
  75. 1:
  76. .asciz "\str"
  77. .popsection
  78. {
  79. moveli r0, hw2_last(1b)
  80. }
  81. {
  82. shl16insli r0, r0, hw1(1b)
  83. }
  84. {
  85. shl16insli r0, r0, hw0(1b)
  86. jal panic
  87. }
  88. .endm
  89. /*
  90. * Unalign data exception fast handling: In order to handle
  91. * unaligned data access, a fast JIT version is generated and stored
  92. * in a specific area in user space. We first need to do a quick poke
  93. * to see if the JIT is available. We use certain bits in the fault
  94. * PC (3 to 9 is used for 16KB page size) as index to address the JIT
  95. * code area. The first 64bit word is the fault PC, and the 2nd one is
  96. * the fault bundle itself. If these 2 words both match, then we
  97. * directly "iret" to JIT code. If not, a slow path is invoked to
  98. * generate new JIT code. Note: the current JIT code WILL be
  99. * overwritten if it existed. So, ideally we can handle 128 unalign
  100. * fixups via JIT. For lookup efficiency and to effectively support
  101. * tight loops with multiple unaligned reference, a simple
  102. * direct-mapped cache is used.
  103. *
  104. * SPR_EX_CONTEXT_K_0 is modified to return to JIT code.
  105. * SPR_EX_CONTEXT_K_1 has ICS set.
  106. * SPR_EX_CONTEXT_0_0 is setup to user program's next PC.
  107. * SPR_EX_CONTEXT_0_1 = 0.
  108. */
  109. .macro int_hand_unalign_fast vecnum, vecname
  110. .org (\vecnum << 8)
  111. intvec_\vecname:
  112. /* Put r3 in SPR_SYSTEM_SAVE_K_1. */
  113. mtspr SPR_SYSTEM_SAVE_K_1, r3
  114. mfspr r3, SPR_EX_CONTEXT_K_1
  115. /*
  116. * Examine if exception comes from user without ICS set.
  117. * If not, just go directly to the slow path.
  118. */
  119. bnez r3, hand_unalign_slow_nonuser
  120. mfspr r3, SPR_SYSTEM_SAVE_K_0
  121. /* Get &thread_info->unalign_jit_tmp[0] in r3. */
  122. bfexts r3, r3, 0, CPU_SHIFT-1
  123. mm r3, zero, LOG2_THREAD_SIZE, 63
  124. addli r3, r3, THREAD_INFO_UNALIGN_JIT_TMP_OFFSET
  125. /*
  126. * Save r0, r1, r2 into thread_info array r3 points to
  127. * from low to high memory in order.
  128. */
  129. st_add r3, r0, 8
  130. st_add r3, r1, 8
  131. {
  132. st_add r3, r2, 8
  133. andi r2, sp, 7
  134. }
  135. /* Save stored r3 value so we can revert it on a page fault. */
  136. mfspr r1, SPR_SYSTEM_SAVE_K_1
  137. st r3, r1
  138. {
  139. /* Generate a SIGBUS if sp is not 8-byte aligned. */
  140. bnez r2, hand_unalign_slow_badsp
  141. }
  142. /*
  143. * Get the thread_info in r0; load r1 with pc. Set the low bit of sp
  144. * as an indicator to the page fault code in case we fault.
  145. */
  146. {
  147. ori sp, sp, 1
  148. mfspr r1, SPR_EX_CONTEXT_K_0
  149. }
  150. /* Add the jit_info offset in thread_info; extract r1 [3:9] into r2. */
  151. {
  152. addli r0, r3, THREAD_INFO_UNALIGN_JIT_BASE_OFFSET - \
  153. (THREAD_INFO_UNALIGN_JIT_TMP_OFFSET + (3 * 8))
  154. bfextu r2, r1, 3, (2 + PAGE_SHIFT - UNALIGN_JIT_SHIFT)
  155. }
  156. /* Load the jit_info; multiply r2 by 128. */
  157. {
  158. ld r0, r0
  159. shli r2, r2, UNALIGN_JIT_SHIFT
  160. }
  161. /*
  162. * If r0 is NULL, the JIT page is not mapped, so go to slow path;
  163. * add offset r2 to r0 at the same time.
  164. */
  165. {
  166. beqz r0, hand_unalign_slow
  167. add r2, r0, r2
  168. }
  169. /*
  170. * We are loading from userspace (both the JIT info PC and
  171. * instruction word, and the instruction word we executed)
  172. * and since either could fault while holding the interrupt
  173. * critical section, we must tag this region and check it in
  174. * do_page_fault() to handle it properly.
  175. */
  176. ENTRY(__start_unalign_asm_code)
  177. /* Load first word of JIT in r0 and increment r2 by 8. */
  178. ld_add r0, r2, 8
  179. /*
  180. * Compare the PC with the 1st word in JIT; load the fault bundle
  181. * into r1.
  182. */
  183. {
  184. cmpeq r0, r0, r1
  185. ld r1, r1
  186. }
  187. /* Go to slow path if PC doesn't match. */
  188. beqz r0, hand_unalign_slow
  189. /*
  190. * Load the 2nd word of JIT, which is supposed to be the fault
  191. * bundle for a cache hit. Increment r2; after this bundle r2 will
  192. * point to the potential start of the JIT code we want to run.
  193. */
  194. ld_add r0, r2, 8
  195. /* No further accesses to userspace are done after this point. */
  196. ENTRY(__end_unalign_asm_code)
  197. /* Compare the real bundle with what is saved in the JIT area. */
  198. {
  199. cmpeq r0, r1, r0
  200. mtspr SPR_EX_CONTEXT_0_1, zero
  201. }
  202. /* Go to slow path if the fault bundle does not match. */
  203. beqz r0, hand_unalign_slow
  204. /*
  205. * A cache hit is found.
  206. * r2 points to start of JIT code (3rd word).
  207. * r0 is the fault pc.
  208. * r1 is the fault bundle.
  209. * Reset the low bit of sp.
  210. */
  211. {
  212. mfspr r0, SPR_EX_CONTEXT_K_0
  213. andi sp, sp, ~1
  214. }
  215. /* Write r2 into EX_CONTEXT_K_0 and increment PC. */
  216. {
  217. mtspr SPR_EX_CONTEXT_K_0, r2
  218. addi r0, r0, 8
  219. }
  220. /*
  221. * Set ICS on kernel EX_CONTEXT_K_1 in order to "iret" to
  222. * user with ICS set. This way, if the JIT fixup causes another
  223. * unalign exception (which shouldn't be possible) the user
  224. * process will be terminated with SIGBUS. Also, our fixup will
  225. * run without interleaving with external interrupts.
  226. * Each fixup is at most 14 bundles, so it won't hold ICS for long.
  227. */
  228. {
  229. movei r1, PL_ICS_EX1(USER_PL, 1)
  230. mtspr SPR_EX_CONTEXT_0_0, r0
  231. }
  232. {
  233. mtspr SPR_EX_CONTEXT_K_1, r1
  234. addi r3, r3, -(3 * 8)
  235. }
  236. /* Restore r0..r3. */
  237. ld_add r0, r3, 8
  238. ld_add r1, r3, 8
  239. ld_add r2, r3, 8
  240. ld r3, r3
  241. iret
  242. ENDPROC(intvec_\vecname)
  243. .endm
  244. #ifdef __COLLECT_LINKER_FEEDBACK__
  245. .pushsection .text.intvec_feedback,"ax"
  246. intvec_feedback:
  247. .popsection
  248. #endif
  249. /*
  250. * Default interrupt handler.
  251. *
  252. * vecnum is where we'll put this code.
  253. * c_routine is the C routine we'll call.
  254. *
  255. * The C routine is passed two arguments:
  256. * - A pointer to the pt_regs state.
  257. * - The interrupt vector number.
  258. *
  259. * The "processing" argument specifies the code for processing
  260. * the interrupt. Defaults to "handle_interrupt".
  261. */
  262. .macro __int_hand vecnum, vecname, c_routine,processing=handle_interrupt
  263. intvec_\vecname:
  264. /* Temporarily save a register so we have somewhere to work. */
  265. mtspr SPR_SYSTEM_SAVE_K_1, r0
  266. mfspr r0, SPR_EX_CONTEXT_K_1
  267. /*
  268. * The unalign data fastpath code sets the low bit in sp to
  269. * force us to reset it here on fault.
  270. */
  271. {
  272. blbs sp, 2f
  273. andi r0, r0, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */
  274. }
  275. .ifc \vecnum, INT_DOUBLE_FAULT
  276. /*
  277. * For double-faults from user-space, fall through to the normal
  278. * register save and stack setup path. Otherwise, it's the
  279. * hypervisor giving us one last chance to dump diagnostics, and we
  280. * branch to the kernel_double_fault routine to do so.
  281. */
  282. beqz r0, 1f
  283. j _kernel_double_fault
  284. 1:
  285. .else
  286. /*
  287. * If we're coming from user-space, then set sp to the top of
  288. * the kernel stack. Otherwise, assume sp is already valid.
  289. */
  290. {
  291. bnez r0, 0f
  292. move r0, sp
  293. }
  294. .endif
  295. .ifc \c_routine, do_page_fault
  296. /*
  297. * The page_fault handler may be downcalled directly by the
  298. * hypervisor even when Linux is running and has ICS set.
  299. *
  300. * In this case the contents of EX_CONTEXT_K_1 reflect the
  301. * previous fault and can't be relied on to choose whether or
  302. * not to reinitialize the stack pointer. So we add a test
  303. * to see whether SYSTEM_SAVE_K_2 has the high bit set,
  304. * and if so we don't reinitialize sp, since we must be coming
  305. * from Linux. (In fact the precise case is !(val & ~1),
  306. * but any Linux PC has to have the high bit set.)
  307. *
  308. * Note that the hypervisor *always* sets SYSTEM_SAVE_K_2 for
  309. * any path that turns into a downcall to one of our TLB handlers.
  310. *
  311. * FIXME: if we end up never using this path, perhaps we should
  312. * prevent the hypervisor from generating downcalls in this case.
  313. * The advantage of getting a downcall is we can panic in Linux.
  314. */
  315. mfspr r0, SPR_SYSTEM_SAVE_K_2
  316. {
  317. bltz r0, 0f /* high bit in S_S_1_2 is for a PC to use */
  318. move r0, sp
  319. }
  320. .endif
  321. 2:
  322. /*
  323. * SYSTEM_SAVE_K_0 holds the cpu number in the high bits, and
  324. * the current stack top in the lower bits. So we recover
  325. * our starting stack value by sign-extending the low bits, then
  326. * point sp at the top aligned address on the actual stack page.
  327. */
  328. mfspr r0, SPR_SYSTEM_SAVE_K_0
  329. bfexts r0, r0, 0, CPU_SHIFT-1
  330. 0:
  331. /*
  332. * Align the stack mod 64 so we can properly predict what
  333. * cache lines we need to write-hint to reduce memory fetch
  334. * latency as we enter the kernel. The layout of memory is
  335. * as follows, with cache line 0 at the lowest VA, and cache
  336. * line 8 just below the r0 value this "andi" computes.
  337. * Note that we never write to cache line 8, and we skip
  338. * cache lines 1-3 for syscalls.
  339. *
  340. * cache line 8: ptregs padding (two words)
  341. * cache line 7: sp, lr, pc, ex1, faultnum, orig_r0, flags, cmpexch
  342. * cache line 6: r46...r53 (tp)
  343. * cache line 5: r38...r45
  344. * cache line 4: r30...r37
  345. * cache line 3: r22...r29
  346. * cache line 2: r14...r21
  347. * cache line 1: r6...r13
  348. * cache line 0: 2 x frame, r0..r5
  349. */
  350. #if STACK_TOP_DELTA != 64
  351. #error STACK_TOP_DELTA must be 64 for assumptions here and in task_pt_regs()
  352. #endif
  353. andi r0, r0, -64
  354. /*
  355. * Push the first four registers on the stack, so that we can set
  356. * them to vector-unique values before we jump to the common code.
  357. *
  358. * Registers are pushed on the stack as a struct pt_regs,
  359. * with the sp initially just above the struct, and when we're
  360. * done, sp points to the base of the struct, minus
  361. * C_ABI_SAVE_AREA_SIZE, so we can directly jal to C code.
  362. *
  363. * This routine saves just the first four registers, plus the
  364. * stack context so we can do proper backtracing right away,
  365. * and defers to handle_interrupt to save the rest.
  366. * The backtracer needs pc, ex1, lr, sp, r52, and faultnum,
  367. * and needs sp set to its final location at the bottom of
  368. * the stack frame.
  369. */
  370. addli r0, r0, PTREGS_OFFSET_LR - (PTREGS_SIZE + KSTK_PTREGS_GAP)
  371. wh64 r0 /* cache line 7 */
  372. {
  373. st r0, lr
  374. addli r0, r0, PTREGS_OFFSET_SP - PTREGS_OFFSET_LR
  375. }
  376. {
  377. st r0, sp
  378. addli sp, r0, PTREGS_OFFSET_REG(52) - PTREGS_OFFSET_SP
  379. }
  380. wh64 sp /* cache line 6 */
  381. {
  382. st sp, r52
  383. addli sp, sp, PTREGS_OFFSET_REG(1) - PTREGS_OFFSET_REG(52)
  384. }
  385. wh64 sp /* cache line 0 */
  386. {
  387. st sp, r1
  388. addli sp, sp, PTREGS_OFFSET_REG(2) - PTREGS_OFFSET_REG(1)
  389. }
  390. {
  391. st sp, r2
  392. addli sp, sp, PTREGS_OFFSET_REG(3) - PTREGS_OFFSET_REG(2)
  393. }
  394. {
  395. st sp, r3
  396. addli sp, sp, PTREGS_OFFSET_PC - PTREGS_OFFSET_REG(3)
  397. }
  398. mfspr r0, SPR_EX_CONTEXT_K_0
  399. .ifc \processing,handle_syscall
  400. /*
  401. * Bump the saved PC by one bundle so that when we return, we won't
  402. * execute the same swint instruction again. We need to do this while
  403. * we're in the critical section.
  404. */
  405. addi r0, r0, 8
  406. .endif
  407. {
  408. st sp, r0
  409. addli sp, sp, PTREGS_OFFSET_EX1 - PTREGS_OFFSET_PC
  410. }
  411. mfspr r0, SPR_EX_CONTEXT_K_1
  412. {
  413. st sp, r0
  414. addi sp, sp, PTREGS_OFFSET_FAULTNUM - PTREGS_OFFSET_EX1
  415. /*
  416. * Use r0 for syscalls so it's a temporary; use r1 for interrupts
  417. * so that it gets passed through unchanged to the handler routine.
  418. * Note that the .if conditional confusingly spans bundles.
  419. */
  420. .ifc \processing,handle_syscall
  421. movei r0, \vecnum
  422. }
  423. {
  424. st sp, r0
  425. .else
  426. movei r1, \vecnum
  427. }
  428. {
  429. st sp, r1
  430. .endif
  431. addli sp, sp, PTREGS_OFFSET_REG(0) - PTREGS_OFFSET_FAULTNUM
  432. }
  433. mfspr r0, SPR_SYSTEM_SAVE_K_1 /* Original r0 */
  434. {
  435. st sp, r0
  436. addi sp, sp, -PTREGS_OFFSET_REG(0) - 8
  437. }
  438. {
  439. st sp, zero /* write zero into "Next SP" frame pointer */
  440. addi sp, sp, -8 /* leave SP pointing at bottom of frame */
  441. }
  442. .ifc \processing,handle_syscall
  443. j handle_syscall
  444. .else
  445. /* Capture per-interrupt SPR context to registers. */
  446. .ifc \c_routine, do_page_fault
  447. mfspr r2, SPR_SYSTEM_SAVE_K_3 /* address of page fault */
  448. mfspr r3, SPR_SYSTEM_SAVE_K_2 /* info about page fault */
  449. .else
  450. .ifc \vecnum, INT_ILL_TRANS
  451. mfspr r2, ILL_VA_PC
  452. .else
  453. .ifc \vecnum, INT_DOUBLE_FAULT
  454. mfspr r2, SPR_SYSTEM_SAVE_K_2 /* double fault info from HV */
  455. .else
  456. .ifc \c_routine, do_trap
  457. mfspr r2, GPV_REASON
  458. .else
  459. .ifc \c_routine, op_handle_perf_interrupt
  460. mfspr r2, PERF_COUNT_STS
  461. #if CHIP_HAS_AUX_PERF_COUNTERS()
  462. .else
  463. .ifc \c_routine, op_handle_aux_perf_interrupt
  464. mfspr r2, AUX_PERF_COUNT_STS
  465. .endif
  466. #endif
  467. .endif
  468. .endif
  469. .endif
  470. .endif
  471. .endif
  472. /* Put function pointer in r0 */
  473. moveli r0, hw2_last(\c_routine)
  474. shl16insli r0, r0, hw1(\c_routine)
  475. {
  476. shl16insli r0, r0, hw0(\c_routine)
  477. j \processing
  478. }
  479. .endif
  480. ENDPROC(intvec_\vecname)
  481. #ifdef __COLLECT_LINKER_FEEDBACK__
  482. .pushsection .text.intvec_feedback,"ax"
  483. .org (\vecnum << 5)
  484. FEEDBACK_ENTER_EXPLICIT(intvec_\vecname, .intrpt1, 1 << 8)
  485. jrp lr
  486. .popsection
  487. #endif
  488. .endm
  489. /*
  490. * Save the rest of the registers that we didn't save in the actual
  491. * vector itself. We can't use r0-r10 inclusive here.
  492. */
  493. .macro finish_interrupt_save, function
  494. /* If it's a syscall, save a proper orig_r0, otherwise just zero. */
  495. PTREGS_PTR(r52, PTREGS_OFFSET_ORIG_R0)
  496. {
  497. .ifc \function,handle_syscall
  498. st r52, r0
  499. .else
  500. st r52, zero
  501. .endif
  502. PTREGS_PTR(r52, PTREGS_OFFSET_TP)
  503. }
  504. st r52, tp
  505. {
  506. mfspr tp, CMPEXCH_VALUE
  507. PTREGS_PTR(r52, PTREGS_OFFSET_CMPEXCH)
  508. }
  509. /*
  510. * For ordinary syscalls, we save neither caller- nor callee-
  511. * save registers, since the syscall invoker doesn't expect the
  512. * caller-saves to be saved, and the called kernel functions will
  513. * take care of saving the callee-saves for us.
  514. *
  515. * For interrupts we save just the caller-save registers. Saving
  516. * them is required (since the "caller" can't save them). Again,
  517. * the called kernel functions will restore the callee-save
  518. * registers for us appropriately.
  519. *
  520. * On return, we normally restore nothing special for syscalls,
  521. * and just the caller-save registers for interrupts.
  522. *
  523. * However, there are some important caveats to all this:
  524. *
  525. * - We always save a few callee-save registers to give us
  526. * some scratchpad registers to carry across function calls.
  527. *
  528. * - fork/vfork/etc require us to save all the callee-save
  529. * registers, which we do in PTREGS_SYSCALL_ALL_REGS, below.
  530. *
  531. * - We always save r0..r5 and r10 for syscalls, since we need
  532. * to reload them a bit later for the actual kernel call, and
  533. * since we might need them for -ERESTARTNOINTR, etc.
  534. *
  535. * - Before invoking a signal handler, we save the unsaved
  536. * callee-save registers so they are visible to the
  537. * signal handler or any ptracer.
  538. *
  539. * - If the unsaved callee-save registers are modified, we set
  540. * a bit in pt_regs so we know to reload them from pt_regs
  541. * and not just rely on the kernel function unwinding.
  542. * (Done for ptrace register writes and SA_SIGINFO handler.)
  543. */
  544. {
  545. st r52, tp
  546. PTREGS_PTR(r52, PTREGS_OFFSET_REG(33))
  547. }
  548. wh64 r52 /* cache line 4 */
  549. push_reg r33, r52
  550. push_reg r32, r52
  551. push_reg r31, r52
  552. .ifc \function,handle_syscall
  553. push_reg r30, r52, PTREGS_OFFSET_SYSCALL - PTREGS_OFFSET_REG(30)
  554. push_reg TREG_SYSCALL_NR_NAME, r52, \
  555. PTREGS_OFFSET_REG(5) - PTREGS_OFFSET_SYSCALL
  556. .else
  557. push_reg r30, r52, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(30)
  558. wh64 r52 /* cache line 3 */
  559. push_reg r29, r52
  560. push_reg r28, r52
  561. push_reg r27, r52
  562. push_reg r26, r52
  563. push_reg r25, r52
  564. push_reg r24, r52
  565. push_reg r23, r52
  566. push_reg r22, r52
  567. wh64 r52 /* cache line 2 */
  568. push_reg r21, r52
  569. push_reg r20, r52
  570. push_reg r19, r52
  571. push_reg r18, r52
  572. push_reg r17, r52
  573. push_reg r16, r52
  574. push_reg r15, r52
  575. push_reg r14, r52
  576. wh64 r52 /* cache line 1 */
  577. push_reg r13, r52
  578. push_reg r12, r52
  579. push_reg r11, r52
  580. push_reg r10, r52
  581. push_reg r9, r52
  582. push_reg r8, r52
  583. push_reg r7, r52
  584. push_reg r6, r52
  585. .endif
  586. push_reg r5, r52
  587. st r52, r4
  588. /*
  589. * If we will be returning to the kernel, we will need to
  590. * reset the interrupt masks to the state they had before.
  591. * Set DISABLE_IRQ in flags iff we came from PL1 with irqs disabled.
  592. */
  593. mfspr r32, SPR_EX_CONTEXT_K_1
  594. {
  595. andi r32, r32, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */
  596. PTREGS_PTR(r21, PTREGS_OFFSET_FLAGS)
  597. }
  598. beqzt r32, 1f /* zero if from user space */
  599. IRQS_DISABLED(r32) /* zero if irqs enabled */
  600. #if PT_FLAGS_DISABLE_IRQ != 1
  601. # error Value of IRQS_DISABLED used to set PT_FLAGS_DISABLE_IRQ; fix
  602. #endif
  603. 1:
  604. .ifnc \function,handle_syscall
  605. /* Record the fact that we saved the caller-save registers above. */
  606. ori r32, r32, PT_FLAGS_CALLER_SAVES
  607. .endif
  608. st r21, r32
  609. /*
  610. * we've captured enough state to the stack (including in
  611. * particular our EX_CONTEXT state) that we can now release
  612. * the interrupt critical section and replace it with our
  613. * standard "interrupts disabled" mask value. This allows
  614. * synchronous interrupts (and profile interrupts) to punch
  615. * through from this point onwards.
  616. *
  617. * It's important that no code before this point touch memory
  618. * other than our own stack (to keep the invariant that this
  619. * is all that gets touched under ICS), and that no code after
  620. * this point reference any interrupt-specific SPR, in particular
  621. * the EX_CONTEXT_K_ values.
  622. */
  623. .ifc \function,handle_nmi
  624. IRQ_DISABLE_ALL(r20)
  625. .else
  626. IRQ_DISABLE(r20, r21)
  627. .endif
  628. mtspr INTERRUPT_CRITICAL_SECTION, zero
  629. /* Load tp with our per-cpu offset. */
  630. #ifdef CONFIG_SMP
  631. {
  632. mfspr r20, SPR_SYSTEM_SAVE_K_0
  633. moveli r21, hw2_last(__per_cpu_offset)
  634. }
  635. {
  636. shl16insli r21, r21, hw1(__per_cpu_offset)
  637. bfextu r20, r20, CPU_SHIFT, 63
  638. }
  639. shl16insli r21, r21, hw0(__per_cpu_offset)
  640. shl3add r20, r20, r21
  641. ld tp, r20
  642. #else
  643. move tp, zero
  644. #endif
  645. #ifdef __COLLECT_LINKER_FEEDBACK__
  646. /*
  647. * Notify the feedback routines that we were in the
  648. * appropriate fixed interrupt vector area. Note that we
  649. * still have ICS set at this point, so we can't invoke any
  650. * atomic operations or we will panic. The feedback
  651. * routines internally preserve r0..r10 and r30 up.
  652. */
  653. .ifnc \function,handle_syscall
  654. shli r20, r1, 5
  655. .else
  656. moveli r20, INT_SWINT_1 << 5
  657. .endif
  658. moveli r21, hw2_last(intvec_feedback)
  659. shl16insli r21, r21, hw1(intvec_feedback)
  660. shl16insli r21, r21, hw0(intvec_feedback)
  661. add r20, r20, r21
  662. jalr r20
  663. /* And now notify the feedback routines that we are here. */
  664. FEEDBACK_ENTER(\function)
  665. #endif
  666. /*
  667. * Prepare the first 256 stack bytes to be rapidly accessible
  668. * without having to fetch the background data.
  669. */
  670. addi r52, sp, -64
  671. {
  672. wh64 r52
  673. addi r52, r52, -64
  674. }
  675. {
  676. wh64 r52
  677. addi r52, r52, -64
  678. }
  679. {
  680. wh64 r52
  681. addi r52, r52, -64
  682. }
  683. wh64 r52
  684. #ifdef CONFIG_TRACE_IRQFLAGS
  685. .ifnc \function,handle_nmi
  686. /*
  687. * We finally have enough state set up to notify the irq
  688. * tracing code that irqs were disabled on entry to the handler.
  689. * The TRACE_IRQS_OFF call clobbers registers r0-r29.
  690. * For syscalls, we already have the register state saved away
  691. * on the stack, so we don't bother to do any register saves here,
  692. * and later we pop the registers back off the kernel stack.
  693. * For interrupt handlers, save r0-r3 in callee-saved registers.
  694. */
  695. .ifnc \function,handle_syscall
  696. { move r30, r0; move r31, r1 }
  697. { move r32, r2; move r33, r3 }
  698. .endif
  699. TRACE_IRQS_OFF
  700. .ifnc \function,handle_syscall
  701. { move r0, r30; move r1, r31 }
  702. { move r2, r32; move r3, r33 }
  703. .endif
  704. .endif
  705. #endif
  706. .endm
  707. /*
  708. * Redispatch a downcall.
  709. */
  710. .macro dc_dispatch vecnum, vecname
  711. .org (\vecnum << 8)
  712. intvec_\vecname:
  713. j _hv_downcall_dispatch
  714. ENDPROC(intvec_\vecname)
  715. .endm
  716. /*
  717. * Common code for most interrupts. The C function we're eventually
  718. * going to is in r0, and the faultnum is in r1; the original
  719. * values for those registers are on the stack.
  720. */
  721. .pushsection .text.handle_interrupt,"ax"
  722. handle_interrupt:
  723. finish_interrupt_save handle_interrupt
  724. /* Jump to the C routine; it should enable irqs as soon as possible. */
  725. {
  726. jalr r0
  727. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  728. }
  729. FEEDBACK_REENTER(handle_interrupt)
  730. {
  731. movei r30, 0 /* not an NMI */
  732. j interrupt_return
  733. }
  734. STD_ENDPROC(handle_interrupt)
  735. /*
  736. * This routine takes a boolean in r30 indicating if this is an NMI.
  737. * If so, we also expect a boolean in r31 indicating whether to
  738. * re-enable the oprofile interrupts.
  739. *
  740. * Note that .Lresume_userspace is jumped to directly in several
  741. * places, and we need to make sure r30 is set correctly in those
  742. * callers as well.
  743. */
  744. STD_ENTRY(interrupt_return)
  745. /* If we're resuming to kernel space, don't check thread flags. */
  746. {
  747. bnez r30, .Lrestore_all /* NMIs don't special-case user-space */
  748. PTREGS_PTR(r29, PTREGS_OFFSET_EX1)
  749. }
  750. ld r29, r29
  751. andi r29, r29, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */
  752. {
  753. beqzt r29, .Lresume_userspace
  754. move r29, sp
  755. }
  756. #ifdef CONFIG_PREEMPT
  757. /* Returning to kernel space. Check if we need preemption. */
  758. EXTRACT_THREAD_INFO(r29)
  759. addli r28, r29, THREAD_INFO_FLAGS_OFFSET
  760. {
  761. ld r28, r28
  762. addli r29, r29, THREAD_INFO_PREEMPT_COUNT_OFFSET
  763. }
  764. {
  765. andi r28, r28, _TIF_NEED_RESCHED
  766. ld4s r29, r29
  767. }
  768. beqzt r28, 1f
  769. bnez r29, 1f
  770. jal preempt_schedule_irq
  771. FEEDBACK_REENTER(interrupt_return)
  772. 1:
  773. #endif
  774. /* If we're resuming to _cpu_idle_nap, bump PC forward by 8. */
  775. {
  776. moveli r27, hw2_last(_cpu_idle_nap)
  777. PTREGS_PTR(r29, PTREGS_OFFSET_PC)
  778. }
  779. {
  780. ld r28, r29
  781. shl16insli r27, r27, hw1(_cpu_idle_nap)
  782. }
  783. {
  784. shl16insli r27, r27, hw0(_cpu_idle_nap)
  785. }
  786. {
  787. cmpeq r27, r27, r28
  788. }
  789. {
  790. blbc r27, .Lrestore_all
  791. addi r28, r28, 8
  792. }
  793. st r29, r28
  794. j .Lrestore_all
  795. .Lresume_userspace:
  796. FEEDBACK_REENTER(interrupt_return)
  797. /*
  798. * Use r33 to hold whether we have already loaded the callee-saves
  799. * into ptregs. We don't want to do it twice in this loop, since
  800. * then we'd clobber whatever changes are made by ptrace, etc.
  801. */
  802. {
  803. movei r33, 0
  804. move r32, sp
  805. }
  806. /* Get base of stack in r32. */
  807. EXTRACT_THREAD_INFO(r32)
  808. .Lretry_work_pending:
  809. /*
  810. * Disable interrupts so as to make sure we don't
  811. * miss an interrupt that sets any of the thread flags (like
  812. * need_resched or sigpending) between sampling and the iret.
  813. * Routines like schedule() or do_signal() may re-enable
  814. * interrupts before returning.
  815. */
  816. IRQ_DISABLE(r20, r21)
  817. TRACE_IRQS_OFF /* Note: clobbers registers r0-r29 */
  818. /* Check to see if there is any work to do before returning to user. */
  819. {
  820. addi r29, r32, THREAD_INFO_FLAGS_OFFSET
  821. moveli r1, hw1_last(_TIF_ALLWORK_MASK)
  822. }
  823. {
  824. ld r29, r29
  825. shl16insli r1, r1, hw0(_TIF_ALLWORK_MASK)
  826. }
  827. and r1, r29, r1
  828. beqzt r1, .Lrestore_all
  829. /*
  830. * Make sure we have all the registers saved for signal
  831. * handling or notify-resume. Call out to C code to figure out
  832. * exactly what we need to do for each flag bit, then if
  833. * necessary, reload the flags and recheck.
  834. */
  835. {
  836. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  837. bnez r33, 1f
  838. }
  839. push_extra_callee_saves r0
  840. movei r33, 1
  841. 1: jal do_work_pending
  842. bnez r0, .Lretry_work_pending
  843. /*
  844. * In the NMI case we
  845. * omit the call to single_process_check_nohz, which normally checks
  846. * to see if we should start or stop the scheduler tick, because
  847. * we can't call arbitrary Linux code from an NMI context.
  848. * We always call the homecache TLB deferral code to re-trigger
  849. * the deferral mechanism.
  850. *
  851. * The other chunk of responsibility this code has is to reset the
  852. * interrupt masks appropriately to reset irqs and NMIs. We have
  853. * to call TRACE_IRQS_OFF and TRACE_IRQS_ON to support all the
  854. * lockdep-type stuff, but we can't set ICS until afterwards, since
  855. * ICS can only be used in very tight chunks of code to avoid
  856. * tripping over various assertions that it is off.
  857. */
  858. .Lrestore_all:
  859. PTREGS_PTR(r0, PTREGS_OFFSET_EX1)
  860. {
  861. ld r0, r0
  862. PTREGS_PTR(r32, PTREGS_OFFSET_FLAGS)
  863. }
  864. {
  865. andi r0, r0, SPR_EX_CONTEXT_1_1__PL_MASK
  866. ld r32, r32
  867. }
  868. bnez r0, 1f
  869. j 2f
  870. #if PT_FLAGS_DISABLE_IRQ != 1
  871. # error Assuming PT_FLAGS_DISABLE_IRQ == 1 so we can use blbct below
  872. #endif
  873. 1: blbct r32, 2f
  874. IRQ_DISABLE(r20,r21)
  875. TRACE_IRQS_OFF
  876. movei r0, 1
  877. mtspr INTERRUPT_CRITICAL_SECTION, r0
  878. beqzt r30, .Lrestore_regs
  879. j 3f
  880. 2: TRACE_IRQS_ON
  881. IRQ_ENABLE_LOAD(r20, r21)
  882. movei r0, 1
  883. mtspr INTERRUPT_CRITICAL_SECTION, r0
  884. IRQ_ENABLE_APPLY(r20, r21)
  885. beqzt r30, .Lrestore_regs
  886. 3:
  887. /*
  888. * We now commit to returning from this interrupt, since we will be
  889. * doing things like setting EX_CONTEXT SPRs and unwinding the stack
  890. * frame. No calls should be made to any other code after this point.
  891. * This code should only be entered with ICS set.
  892. * r32 must still be set to ptregs.flags.
  893. * We launch loads to each cache line separately first, so we can
  894. * get some parallelism out of the memory subsystem.
  895. * We start zeroing caller-saved registers throughout, since
  896. * that will save some cycles if this turns out to be a syscall.
  897. */
  898. .Lrestore_regs:
  899. /*
  900. * Rotate so we have one high bit and one low bit to test.
  901. * - low bit says whether to restore all the callee-saved registers,
  902. * or just r30-r33, and r52 up.
  903. * - high bit (i.e. sign bit) says whether to restore all the
  904. * caller-saved registers, or just r0.
  905. */
  906. #if PT_FLAGS_CALLER_SAVES != 2 || PT_FLAGS_RESTORE_REGS != 4
  907. # error Rotate trick does not work :-)
  908. #endif
  909. {
  910. rotli r20, r32, 62
  911. PTREGS_PTR(sp, PTREGS_OFFSET_REG(0))
  912. }
  913. /*
  914. * Load cache lines 0, 4, 6 and 7, in that order, then use
  915. * the last loaded value, which makes it likely that the other
  916. * cache lines have also loaded, at which point we should be
  917. * able to safely read all the remaining words on those cache
  918. * lines without waiting for the memory subsystem.
  919. */
  920. pop_reg r0, sp, PTREGS_OFFSET_REG(30) - PTREGS_OFFSET_REG(0)
  921. pop_reg r30, sp, PTREGS_OFFSET_REG(52) - PTREGS_OFFSET_REG(30)
  922. pop_reg_zero r52, r3, sp, PTREGS_OFFSET_CMPEXCH - PTREGS_OFFSET_REG(52)
  923. pop_reg_zero r21, r27, sp, PTREGS_OFFSET_EX1 - PTREGS_OFFSET_CMPEXCH
  924. pop_reg_zero lr, r2, sp, PTREGS_OFFSET_PC - PTREGS_OFFSET_EX1
  925. {
  926. mtspr CMPEXCH_VALUE, r21
  927. move r4, zero
  928. }
  929. pop_reg r21, sp, PTREGS_OFFSET_REG(31) - PTREGS_OFFSET_PC
  930. {
  931. mtspr SPR_EX_CONTEXT_K_1, lr
  932. andi lr, lr, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */
  933. }
  934. {
  935. mtspr SPR_EX_CONTEXT_K_0, r21
  936. move r5, zero
  937. }
  938. /* Restore callee-saveds that we actually use. */
  939. pop_reg_zero r31, r6
  940. pop_reg_zero r32, r7
  941. pop_reg_zero r33, r8, sp, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(33)
  942. /*
  943. * If we modified other callee-saveds, restore them now.
  944. * This is rare, but could be via ptrace or signal handler.
  945. */
  946. {
  947. move r9, zero
  948. blbs r20, .Lrestore_callees
  949. }
  950. .Lcontinue_restore_regs:
  951. /* Check if we're returning from a syscall. */
  952. {
  953. move r10, zero
  954. bltzt r20, 1f /* no, so go restore callee-save registers */
  955. }
  956. /*
  957. * Check if we're returning to userspace.
  958. * Note that if we're not, we don't worry about zeroing everything.
  959. */
  960. {
  961. addli sp, sp, PTREGS_OFFSET_LR - PTREGS_OFFSET_REG(29)
  962. bnez lr, .Lkernel_return
  963. }
  964. /*
  965. * On return from syscall, we've restored r0 from pt_regs, but we
  966. * clear the remainder of the caller-saved registers. We could
  967. * restore the syscall arguments, but there's not much point,
  968. * and it ensures user programs aren't trying to use the
  969. * caller-saves if we clear them, as well as avoiding leaking
  970. * kernel pointers into userspace.
  971. */
  972. pop_reg_zero lr, r11, sp, PTREGS_OFFSET_TP - PTREGS_OFFSET_LR
  973. pop_reg_zero tp, r12, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_TP
  974. {
  975. ld sp, sp
  976. move r13, zero
  977. move r14, zero
  978. }
  979. { move r15, zero; move r16, zero }
  980. { move r17, zero; move r18, zero }
  981. { move r19, zero; move r20, zero }
  982. { move r21, zero; move r22, zero }
  983. { move r23, zero; move r24, zero }
  984. { move r25, zero; move r26, zero }
  985. /* Set r1 to errno if we are returning an error, otherwise zero. */
  986. {
  987. moveli r29, 4096
  988. sub r1, zero, r0
  989. }
  990. {
  991. move r28, zero
  992. cmpltu r29, r1, r29
  993. }
  994. {
  995. mnz r1, r29, r1
  996. move r29, zero
  997. }
  998. iret
  999. /*
  1000. * Not a syscall, so restore caller-saved registers.
  1001. * First kick off loads for cache lines 1-3, which we're touching
  1002. * for the first time here.
  1003. */
  1004. .align 64
  1005. 1: pop_reg r29, sp, PTREGS_OFFSET_REG(21) - PTREGS_OFFSET_REG(29)
  1006. pop_reg r21, sp, PTREGS_OFFSET_REG(13) - PTREGS_OFFSET_REG(21)
  1007. pop_reg r13, sp, PTREGS_OFFSET_REG(1) - PTREGS_OFFSET_REG(13)
  1008. pop_reg r1
  1009. pop_reg r2
  1010. pop_reg r3
  1011. pop_reg r4
  1012. pop_reg r5
  1013. pop_reg r6
  1014. pop_reg r7
  1015. pop_reg r8
  1016. pop_reg r9
  1017. pop_reg r10
  1018. pop_reg r11
  1019. pop_reg r12, sp, 16
  1020. /* r13 already restored above */
  1021. pop_reg r14
  1022. pop_reg r15
  1023. pop_reg r16
  1024. pop_reg r17
  1025. pop_reg r18
  1026. pop_reg r19
  1027. pop_reg r20, sp, 16
  1028. /* r21 already restored above */
  1029. pop_reg r22
  1030. pop_reg r23
  1031. pop_reg r24
  1032. pop_reg r25
  1033. pop_reg r26
  1034. pop_reg r27
  1035. pop_reg r28, sp, PTREGS_OFFSET_LR - PTREGS_OFFSET_REG(28)
  1036. /* r29 already restored above */
  1037. bnez lr, .Lkernel_return
  1038. pop_reg lr, sp, PTREGS_OFFSET_TP - PTREGS_OFFSET_LR
  1039. pop_reg tp, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_TP
  1040. ld sp, sp
  1041. iret
  1042. /*
  1043. * We can't restore tp when in kernel mode, since a thread might
  1044. * have migrated from another cpu and brought a stale tp value.
  1045. */
  1046. .Lkernel_return:
  1047. pop_reg lr, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_LR
  1048. ld sp, sp
  1049. iret
  1050. /* Restore callee-saved registers from r34 to r51. */
  1051. .Lrestore_callees:
  1052. addli sp, sp, PTREGS_OFFSET_REG(34) - PTREGS_OFFSET_REG(29)
  1053. pop_reg r34
  1054. pop_reg r35
  1055. pop_reg r36
  1056. pop_reg r37
  1057. pop_reg r38
  1058. pop_reg r39
  1059. pop_reg r40
  1060. pop_reg r41
  1061. pop_reg r42
  1062. pop_reg r43
  1063. pop_reg r44
  1064. pop_reg r45
  1065. pop_reg r46
  1066. pop_reg r47
  1067. pop_reg r48
  1068. pop_reg r49
  1069. pop_reg r50
  1070. pop_reg r51, sp, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(51)
  1071. j .Lcontinue_restore_regs
  1072. STD_ENDPROC(interrupt_return)
  1073. /*
  1074. * "NMI" interrupts mask ALL interrupts before calling the
  1075. * handler, and don't check thread flags, etc., on the way
  1076. * back out. In general, the only things we do here for NMIs
  1077. * are register save/restore and dataplane kernel-TLB management.
  1078. * We don't (for example) deal with start/stop of the sched tick.
  1079. */
  1080. .pushsection .text.handle_nmi,"ax"
  1081. handle_nmi:
  1082. finish_interrupt_save handle_nmi
  1083. {
  1084. jalr r0
  1085. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  1086. }
  1087. FEEDBACK_REENTER(handle_nmi)
  1088. {
  1089. movei r30, 1
  1090. move r31, r0
  1091. }
  1092. j interrupt_return
  1093. STD_ENDPROC(handle_nmi)
  1094. /*
  1095. * Parallel code for syscalls to handle_interrupt.
  1096. */
  1097. .pushsection .text.handle_syscall,"ax"
  1098. handle_syscall:
  1099. finish_interrupt_save handle_syscall
  1100. /* Enable irqs. */
  1101. TRACE_IRQS_ON
  1102. IRQ_ENABLE(r20, r21)
  1103. /* Bump the counter for syscalls made on this tile. */
  1104. moveli r20, hw2_last(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)
  1105. shl16insli r20, r20, hw1(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)
  1106. shl16insli r20, r20, hw0(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)
  1107. add r20, r20, tp
  1108. ld4s r21, r20
  1109. {
  1110. addi r21, r21, 1
  1111. move r31, sp
  1112. }
  1113. {
  1114. st4 r20, r21
  1115. EXTRACT_THREAD_INFO(r31)
  1116. }
  1117. /* Trace syscalls, if requested. */
  1118. addi r31, r31, THREAD_INFO_FLAGS_OFFSET
  1119. {
  1120. ld r30, r31
  1121. moveli r32, _TIF_SYSCALL_ENTRY_WORK
  1122. }
  1123. and r30, r30, r32
  1124. {
  1125. addi r30, r31, THREAD_INFO_STATUS_OFFSET - THREAD_INFO_FLAGS_OFFSET
  1126. beqzt r30, .Lrestore_syscall_regs
  1127. }
  1128. {
  1129. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  1130. jal do_syscall_trace_enter
  1131. }
  1132. FEEDBACK_REENTER(handle_syscall)
  1133. /*
  1134. * We always reload our registers from the stack at this
  1135. * point. They might be valid, if we didn't build with
  1136. * TRACE_IRQFLAGS, and this isn't a dataplane tile, and we're not
  1137. * doing syscall tracing, but there are enough cases now that it
  1138. * seems simplest just to do the reload unconditionally.
  1139. */
  1140. .Lrestore_syscall_regs:
  1141. {
  1142. ld r30, r30
  1143. PTREGS_PTR(r11, PTREGS_OFFSET_REG(0))
  1144. }
  1145. pop_reg r0, r11
  1146. pop_reg r1, r11
  1147. pop_reg r2, r11
  1148. pop_reg r3, r11
  1149. pop_reg r4, r11
  1150. pop_reg r5, r11, PTREGS_OFFSET_SYSCALL - PTREGS_OFFSET_REG(5)
  1151. {
  1152. ld TREG_SYSCALL_NR_NAME, r11
  1153. moveli r21, __NR_syscalls
  1154. }
  1155. /* Ensure that the syscall number is within the legal range. */
  1156. {
  1157. moveli r20, hw2(sys_call_table)
  1158. #ifdef CONFIG_COMPAT
  1159. blbs r30, .Lcompat_syscall
  1160. #endif
  1161. }
  1162. {
  1163. cmpltu r21, TREG_SYSCALL_NR_NAME, r21
  1164. shl16insli r20, r20, hw1(sys_call_table)
  1165. }
  1166. {
  1167. blbc r21, .Linvalid_syscall
  1168. shl16insli r20, r20, hw0(sys_call_table)
  1169. }
  1170. .Lload_syscall_pointer:
  1171. shl3add r20, TREG_SYSCALL_NR_NAME, r20
  1172. ld r20, r20
  1173. /* Jump to syscall handler. */
  1174. jalr r20
  1175. .Lhandle_syscall_link: /* value of "lr" after "jalr r20" above */
  1176. /*
  1177. * Write our r0 onto the stack so it gets restored instead
  1178. * of whatever the user had there before.
  1179. * In compat mode, sign-extend r0 before storing it.
  1180. */
  1181. {
  1182. PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))
  1183. blbct r30, 1f
  1184. }
  1185. addxi r0, r0, 0
  1186. 1: st r29, r0
  1187. .Lsyscall_sigreturn_skip:
  1188. FEEDBACK_REENTER(handle_syscall)
  1189. /* Do syscall trace again, if requested. */
  1190. {
  1191. ld r30, r31
  1192. moveli r32, _TIF_SYSCALL_EXIT_WORK
  1193. }
  1194. and r0, r30, r32
  1195. {
  1196. andi r0, r30, _TIF_SINGLESTEP
  1197. beqzt r0, 1f
  1198. }
  1199. {
  1200. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  1201. jal do_syscall_trace_exit
  1202. }
  1203. FEEDBACK_REENTER(handle_syscall)
  1204. andi r0, r30, _TIF_SINGLESTEP
  1205. 1: beqzt r0, 2f
  1206. /* Single stepping -- notify ptrace. */
  1207. {
  1208. movei r0, SIGTRAP
  1209. jal ptrace_notify
  1210. }
  1211. FEEDBACK_REENTER(handle_syscall)
  1212. 2: {
  1213. movei r30, 0 /* not an NMI */
  1214. j .Lresume_userspace /* jump into middle of interrupt_return */
  1215. }
  1216. #ifdef CONFIG_COMPAT
  1217. .Lcompat_syscall:
  1218. /*
  1219. * Load the base of the compat syscall table in r20, and
  1220. * range-check the syscall number (duplicated from 64-bit path).
  1221. * Sign-extend all the user's passed arguments to make them consistent.
  1222. * Also save the original "r(n)" values away in "r(11+n)" in
  1223. * case the syscall table entry wants to validate them.
  1224. */
  1225. moveli r20, hw2(compat_sys_call_table)
  1226. {
  1227. cmpltu r21, TREG_SYSCALL_NR_NAME, r21
  1228. shl16insli r20, r20, hw1(compat_sys_call_table)
  1229. }
  1230. {
  1231. blbc r21, .Linvalid_syscall
  1232. shl16insli r20, r20, hw0(compat_sys_call_table)
  1233. }
  1234. { move r11, r0; addxi r0, r0, 0 }
  1235. { move r12, r1; addxi r1, r1, 0 }
  1236. { move r13, r2; addxi r2, r2, 0 }
  1237. { move r14, r3; addxi r3, r3, 0 }
  1238. { move r15, r4; addxi r4, r4, 0 }
  1239. { move r16, r5; addxi r5, r5, 0 }
  1240. j .Lload_syscall_pointer
  1241. #endif
  1242. .Linvalid_syscall:
  1243. /* Report an invalid syscall back to the user program */
  1244. {
  1245. PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))
  1246. movei r28, -ENOSYS
  1247. }
  1248. st r29, r28
  1249. {
  1250. movei r30, 0 /* not an NMI */
  1251. j .Lresume_userspace /* jump into middle of interrupt_return */
  1252. }
  1253. STD_ENDPROC(handle_syscall)
  1254. /* Return the address for oprofile to suppress in backtraces. */
  1255. STD_ENTRY_SECTION(handle_syscall_link_address, .text.handle_syscall)
  1256. lnk r0
  1257. {
  1258. addli r0, r0, .Lhandle_syscall_link - .
  1259. jrp lr
  1260. }
  1261. STD_ENDPROC(handle_syscall_link_address)
  1262. STD_ENTRY(ret_from_fork)
  1263. jal sim_notify_fork
  1264. jal schedule_tail
  1265. FEEDBACK_REENTER(ret_from_fork)
  1266. {
  1267. movei r30, 0 /* not an NMI */
  1268. j .Lresume_userspace /* jump into middle of interrupt_return */
  1269. }
  1270. STD_ENDPROC(ret_from_fork)
  1271. STD_ENTRY(ret_from_kernel_thread)
  1272. jal sim_notify_fork
  1273. jal schedule_tail
  1274. FEEDBACK_REENTER(ret_from_fork)
  1275. {
  1276. move r0, r31
  1277. jalr r30
  1278. }
  1279. FEEDBACK_REENTER(ret_from_kernel_thread)
  1280. {
  1281. movei r30, 0 /* not an NMI */
  1282. j .Lresume_userspace /* jump into middle of interrupt_return */
  1283. }
  1284. STD_ENDPROC(ret_from_kernel_thread)
  1285. /* Various stub interrupt handlers and syscall handlers */
  1286. STD_ENTRY_LOCAL(_kernel_double_fault)
  1287. mfspr r1, SPR_EX_CONTEXT_K_0
  1288. move r2, lr
  1289. move r3, sp
  1290. move r4, r52
  1291. addi sp, sp, -C_ABI_SAVE_AREA_SIZE
  1292. j kernel_double_fault
  1293. STD_ENDPROC(_kernel_double_fault)
  1294. STD_ENTRY_LOCAL(bad_intr)
  1295. mfspr r2, SPR_EX_CONTEXT_K_0
  1296. panic "Unhandled interrupt %#x: PC %#lx"
  1297. STD_ENDPROC(bad_intr)
  1298. /*
  1299. * Special-case sigreturn to not write r0 to the stack on return.
  1300. * This is technically more efficient, but it also avoids difficulties
  1301. * in the 64-bit OS when handling 32-bit compat code, since we must not
  1302. * sign-extend r0 for the sigreturn return-value case.
  1303. */
  1304. #define PTREGS_SYSCALL_SIGRETURN(x, reg) \
  1305. STD_ENTRY(_##x); \
  1306. addli lr, lr, .Lsyscall_sigreturn_skip - .Lhandle_syscall_link; \
  1307. { \
  1308. PTREGS_PTR(reg, PTREGS_OFFSET_BASE); \
  1309. j x \
  1310. }; \
  1311. STD_ENDPROC(_##x)
  1312. PTREGS_SYSCALL_SIGRETURN(sys_rt_sigreturn, r0)
  1313. #ifdef CONFIG_COMPAT
  1314. PTREGS_SYSCALL_SIGRETURN(compat_sys_rt_sigreturn, r0)
  1315. #endif
  1316. /* Save additional callee-saves to pt_regs and jump to standard function. */
  1317. STD_ENTRY(_sys_clone)
  1318. push_extra_callee_saves r4
  1319. j sys_clone
  1320. STD_ENDPROC(_sys_clone)
  1321. /*
  1322. * Recover r3, r2, r1 and r0 here saved by unalign fast vector.
  1323. * The vector area limit is 32 bundles, so we handle the reload here.
  1324. * r0, r1, r2 are in thread_info from low to high memory in order.
  1325. * r3 points to location the original r3 was saved.
  1326. * We put this code in the __HEAD section so it can be reached
  1327. * via a conditional branch from the fast path.
  1328. */
  1329. __HEAD
  1330. hand_unalign_slow:
  1331. andi sp, sp, ~1
  1332. hand_unalign_slow_badsp:
  1333. addi r3, r3, -(3 * 8)
  1334. ld_add r0, r3, 8
  1335. ld_add r1, r3, 8
  1336. ld r2, r3
  1337. hand_unalign_slow_nonuser:
  1338. mfspr r3, SPR_SYSTEM_SAVE_K_1
  1339. __int_hand INT_UNALIGN_DATA, UNALIGN_DATA_SLOW, int_unalign
  1340. /* The unaligned data support needs to read all the registers. */
  1341. int_unalign:
  1342. push_extra_callee_saves r0
  1343. j do_unaligned
  1344. ENDPROC(hand_unalign_slow)
  1345. /* Fill the return address stack with nonzero entries. */
  1346. STD_ENTRY(fill_ra_stack)
  1347. {
  1348. move r0, lr
  1349. jal 1f
  1350. }
  1351. 1: jal 2f
  1352. 2: jal 3f
  1353. 3: jal 4f
  1354. 4: jrp r0
  1355. STD_ENDPROC(fill_ra_stack)
  1356. .macro int_hand vecnum, vecname, c_routine, processing=handle_interrupt
  1357. .org (\vecnum << 8)
  1358. __int_hand \vecnum, \vecname, \c_routine, \processing
  1359. .endm
  1360. /* Include .intrpt1 array of interrupt vectors */
  1361. .section ".intrpt1", "ax"
  1362. #define op_handle_perf_interrupt bad_intr
  1363. #define op_handle_aux_perf_interrupt bad_intr
  1364. #ifndef CONFIG_HARDWALL
  1365. #define do_hardwall_trap bad_intr
  1366. #endif
  1367. int_hand INT_MEM_ERROR, MEM_ERROR, do_trap
  1368. int_hand INT_SINGLE_STEP_3, SINGLE_STEP_3, bad_intr
  1369. #if CONFIG_KERNEL_PL == 2
  1370. int_hand INT_SINGLE_STEP_2, SINGLE_STEP_2, gx_singlestep_handle
  1371. int_hand INT_SINGLE_STEP_1, SINGLE_STEP_1, bad_intr
  1372. #else
  1373. int_hand INT_SINGLE_STEP_2, SINGLE_STEP_2, bad_intr
  1374. int_hand INT_SINGLE_STEP_1, SINGLE_STEP_1, gx_singlestep_handle
  1375. #endif
  1376. int_hand INT_SINGLE_STEP_0, SINGLE_STEP_0, bad_intr
  1377. int_hand INT_IDN_COMPLETE, IDN_COMPLETE, bad_intr
  1378. int_hand INT_UDN_COMPLETE, UDN_COMPLETE, bad_intr
  1379. int_hand INT_ITLB_MISS, ITLB_MISS, do_page_fault
  1380. int_hand INT_ILL, ILL, do_trap
  1381. int_hand INT_GPV, GPV, do_trap
  1382. int_hand INT_IDN_ACCESS, IDN_ACCESS, do_trap
  1383. int_hand INT_UDN_ACCESS, UDN_ACCESS, do_trap
  1384. int_hand INT_SWINT_3, SWINT_3, do_trap
  1385. int_hand INT_SWINT_2, SWINT_2, do_trap
  1386. int_hand INT_SWINT_1, SWINT_1, SYSCALL, handle_syscall
  1387. int_hand INT_SWINT_0, SWINT_0, do_trap
  1388. int_hand INT_ILL_TRANS, ILL_TRANS, do_trap
  1389. int_hand_unalign_fast INT_UNALIGN_DATA, UNALIGN_DATA
  1390. int_hand INT_DTLB_MISS, DTLB_MISS, do_page_fault
  1391. int_hand INT_DTLB_ACCESS, DTLB_ACCESS, do_page_fault
  1392. int_hand INT_IDN_FIREWALL, IDN_FIREWALL, do_hardwall_trap
  1393. int_hand INT_UDN_FIREWALL, UDN_FIREWALL, do_hardwall_trap
  1394. int_hand INT_TILE_TIMER, TILE_TIMER, do_timer_interrupt
  1395. int_hand INT_IDN_TIMER, IDN_TIMER, bad_intr
  1396. int_hand INT_UDN_TIMER, UDN_TIMER, bad_intr
  1397. int_hand INT_IDN_AVAIL, IDN_AVAIL, bad_intr
  1398. int_hand INT_UDN_AVAIL, UDN_AVAIL, bad_intr
  1399. int_hand INT_IPI_3, IPI_3, bad_intr
  1400. #if CONFIG_KERNEL_PL == 2
  1401. int_hand INT_IPI_2, IPI_2, tile_dev_intr
  1402. int_hand INT_IPI_1, IPI_1, bad_intr
  1403. #else
  1404. int_hand INT_IPI_2, IPI_2, bad_intr
  1405. int_hand INT_IPI_1, IPI_1, tile_dev_intr
  1406. #endif
  1407. int_hand INT_IPI_0, IPI_0, bad_intr
  1408. int_hand INT_PERF_COUNT, PERF_COUNT, \
  1409. op_handle_perf_interrupt, handle_nmi
  1410. int_hand INT_AUX_PERF_COUNT, AUX_PERF_COUNT, \
  1411. op_handle_perf_interrupt, handle_nmi
  1412. int_hand INT_INTCTRL_3, INTCTRL_3, bad_intr
  1413. #if CONFIG_KERNEL_PL == 2
  1414. dc_dispatch INT_INTCTRL_2, INTCTRL_2
  1415. int_hand INT_INTCTRL_1, INTCTRL_1, bad_intr
  1416. #else
  1417. int_hand INT_INTCTRL_2, INTCTRL_2, bad_intr
  1418. dc_dispatch INT_INTCTRL_1, INTCTRL_1
  1419. #endif
  1420. int_hand INT_INTCTRL_0, INTCTRL_0, bad_intr
  1421. int_hand INT_MESSAGE_RCV_DWNCL, MESSAGE_RCV_DWNCL, \
  1422. hv_message_intr
  1423. int_hand INT_DEV_INTR_DWNCL, DEV_INTR_DWNCL, bad_intr
  1424. int_hand INT_I_ASID, I_ASID, bad_intr
  1425. int_hand INT_D_ASID, D_ASID, bad_intr
  1426. int_hand INT_DOUBLE_FAULT, DOUBLE_FAULT, do_trap
  1427. /* Synthetic interrupt delivered only by the simulator */
  1428. int_hand INT_BREAKPOINT, BREAKPOINT, do_breakpoint