intvec_64.S 42 KB

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