intvec_64.S 42 KB

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