intvec_32.S 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /*
  2. * Copyright 2010 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/init.h>
  19. #include <linux/unistd.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/thread_info.h>
  22. #include <asm/irqflags.h>
  23. #include <asm/atomic.h>
  24. #include <asm/asm-offsets.h>
  25. #include <hv/hypervisor.h>
  26. #include <arch/abi.h>
  27. #include <arch/interrupts.h>
  28. #include <arch/spr_def.h>
  29. #ifdef CONFIG_PREEMPT
  30. # error "No support for kernel preemption currently"
  31. #endif
  32. #if INT_INTCTRL_K < 32 || INT_INTCTRL_K >= 48
  33. # error INT_INTCTRL_K coded to set high interrupt mask
  34. #endif
  35. #define PTREGS_PTR(reg, ptreg) addli reg, sp, C_ABI_SAVE_AREA_SIZE + (ptreg)
  36. #define PTREGS_OFFSET_SYSCALL PTREGS_OFFSET_REG(TREG_SYSCALL_NR)
  37. #if !CHIP_HAS_WH64()
  38. /* By making this an empty macro, we can use wh64 in the code. */
  39. .macro wh64 reg
  40. .endm
  41. #endif
  42. .macro push_reg reg, ptr=sp, delta=-4
  43. {
  44. sw \ptr, \reg
  45. addli \ptr, \ptr, \delta
  46. }
  47. .endm
  48. .macro pop_reg reg, ptr=sp, delta=4
  49. {
  50. lw \reg, \ptr
  51. addli \ptr, \ptr, \delta
  52. }
  53. .endm
  54. .macro pop_reg_zero reg, zreg, ptr=sp, delta=4
  55. {
  56. move \zreg, zero
  57. lw \reg, \ptr
  58. addi \ptr, \ptr, \delta
  59. }
  60. .endm
  61. .macro push_extra_callee_saves reg
  62. PTREGS_PTR(\reg, PTREGS_OFFSET_REG(51))
  63. push_reg r51, \reg
  64. push_reg r50, \reg
  65. push_reg r49, \reg
  66. push_reg r48, \reg
  67. push_reg r47, \reg
  68. push_reg r46, \reg
  69. push_reg r45, \reg
  70. push_reg r44, \reg
  71. push_reg r43, \reg
  72. push_reg r42, \reg
  73. push_reg r41, \reg
  74. push_reg r40, \reg
  75. push_reg r39, \reg
  76. push_reg r38, \reg
  77. push_reg r37, \reg
  78. push_reg r36, \reg
  79. push_reg r35, \reg
  80. push_reg r34, \reg, PTREGS_OFFSET_BASE - PTREGS_OFFSET_REG(34)
  81. .endm
  82. .macro panic str
  83. .pushsection .rodata, "a"
  84. 1:
  85. .asciz "\str"
  86. .popsection
  87. {
  88. moveli r0, lo16(1b)
  89. }
  90. {
  91. auli r0, r0, ha16(1b)
  92. jal panic
  93. }
  94. .endm
  95. #ifdef __COLLECT_LINKER_FEEDBACK__
  96. .pushsection .text.intvec_feedback,"ax"
  97. intvec_feedback:
  98. .popsection
  99. #endif
  100. /*
  101. * Default interrupt handler.
  102. *
  103. * vecnum is where we'll put this code.
  104. * c_routine is the C routine we'll call.
  105. *
  106. * The C routine is passed two arguments:
  107. * - A pointer to the pt_regs state.
  108. * - The interrupt vector number.
  109. *
  110. * The "processing" argument specifies the code for processing
  111. * the interrupt. Defaults to "handle_interrupt".
  112. */
  113. .macro int_hand vecnum, vecname, c_routine, processing=handle_interrupt
  114. .org (\vecnum << 8)
  115. intvec_\vecname:
  116. .ifc \vecnum, INT_SWINT_1
  117. blz TREG_SYSCALL_NR_NAME, sys_cmpxchg
  118. .endif
  119. /* Temporarily save a register so we have somewhere to work. */
  120. mtspr SPR_SYSTEM_SAVE_K_1, r0
  121. mfspr r0, SPR_EX_CONTEXT_K_1
  122. /* The cmpxchg code clears sp to force us to reset it here on fault. */
  123. {
  124. bz sp, 2f
  125. andi r0, r0, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */
  126. }
  127. .ifc \vecnum, INT_DOUBLE_FAULT
  128. /*
  129. * For double-faults from user-space, fall through to the normal
  130. * register save and stack setup path. Otherwise, it's the
  131. * hypervisor giving us one last chance to dump diagnostics, and we
  132. * branch to the kernel_double_fault routine to do so.
  133. */
  134. bz r0, 1f
  135. j _kernel_double_fault
  136. 1:
  137. .else
  138. /*
  139. * If we're coming from user-space, then set sp to the top of
  140. * the kernel stack. Otherwise, assume sp is already valid.
  141. */
  142. {
  143. bnz r0, 0f
  144. move r0, sp
  145. }
  146. .endif
  147. .ifc \c_routine, do_page_fault
  148. /*
  149. * The page_fault handler may be downcalled directly by the
  150. * hypervisor even when Linux is running and has ICS set.
  151. *
  152. * In this case the contents of EX_CONTEXT_K_1 reflect the
  153. * previous fault and can't be relied on to choose whether or
  154. * not to reinitialize the stack pointer. So we add a test
  155. * to see whether SYSTEM_SAVE_K_2 has the high bit set,
  156. * and if so we don't reinitialize sp, since we must be coming
  157. * from Linux. (In fact the precise case is !(val & ~1),
  158. * but any Linux PC has to have the high bit set.)
  159. *
  160. * Note that the hypervisor *always* sets SYSTEM_SAVE_K_2 for
  161. * any path that turns into a downcall to one of our TLB handlers.
  162. */
  163. mfspr r0, SPR_SYSTEM_SAVE_K_2
  164. {
  165. blz r0, 0f /* high bit in S_S_1_2 is for a PC to use */
  166. move r0, sp
  167. }
  168. .endif
  169. 2:
  170. /*
  171. * SYSTEM_SAVE_K_0 holds the cpu number in the low bits, and
  172. * the current stack top in the higher bits. So we recover
  173. * our stack top by just masking off the low bits, then
  174. * point sp at the top aligned address on the actual stack page.
  175. */
  176. mfspr r0, SPR_SYSTEM_SAVE_K_0
  177. mm r0, r0, zero, LOG2_THREAD_SIZE, 31
  178. 0:
  179. /*
  180. * Align the stack mod 64 so we can properly predict what
  181. * cache lines we need to write-hint to reduce memory fetch
  182. * latency as we enter the kernel. The layout of memory is
  183. * as follows, with cache line 0 at the lowest VA, and cache
  184. * line 4 just below the r0 value this "andi" computes.
  185. * Note that we never write to cache line 4, and we skip
  186. * cache line 1 for syscalls.
  187. *
  188. * cache line 4: ptregs padding (two words)
  189. * cache line 3: r46...lr, pc, ex1, faultnum, orig_r0, flags, pad
  190. * cache line 2: r30...r45
  191. * cache line 1: r14...r29
  192. * cache line 0: 2 x frame, r0..r13
  193. */
  194. andi r0, r0, -64
  195. /*
  196. * Push the first four registers on the stack, so that we can set
  197. * them to vector-unique values before we jump to the common code.
  198. *
  199. * Registers are pushed on the stack as a struct pt_regs,
  200. * with the sp initially just above the struct, and when we're
  201. * done, sp points to the base of the struct, minus
  202. * C_ABI_SAVE_AREA_SIZE, so we can directly jal to C code.
  203. *
  204. * This routine saves just the first four registers, plus the
  205. * stack context so we can do proper backtracing right away,
  206. * and defers to handle_interrupt to save the rest.
  207. * The backtracer needs pc, ex1, lr, sp, r52, and faultnum.
  208. */
  209. addli r0, r0, PTREGS_OFFSET_LR - (PTREGS_SIZE + KSTK_PTREGS_GAP)
  210. wh64 r0 /* cache line 3 */
  211. {
  212. sw r0, lr
  213. addli r0, r0, PTREGS_OFFSET_SP - PTREGS_OFFSET_LR
  214. }
  215. {
  216. sw r0, sp
  217. addli sp, r0, PTREGS_OFFSET_REG(52) - PTREGS_OFFSET_SP
  218. }
  219. {
  220. sw sp, r52
  221. addli sp, sp, PTREGS_OFFSET_REG(1) - PTREGS_OFFSET_REG(52)
  222. }
  223. wh64 sp /* cache line 0 */
  224. {
  225. sw sp, r1
  226. addli sp, sp, PTREGS_OFFSET_REG(2) - PTREGS_OFFSET_REG(1)
  227. }
  228. {
  229. sw sp, r2
  230. addli sp, sp, PTREGS_OFFSET_REG(3) - PTREGS_OFFSET_REG(2)
  231. }
  232. {
  233. sw sp, r3
  234. addli sp, sp, PTREGS_OFFSET_PC - PTREGS_OFFSET_REG(3)
  235. }
  236. mfspr r0, SPR_EX_CONTEXT_K_0
  237. .ifc \processing,handle_syscall
  238. /*
  239. * Bump the saved PC by one bundle so that when we return, we won't
  240. * execute the same swint instruction again. We need to do this while
  241. * we're in the critical section.
  242. */
  243. addi r0, r0, 8
  244. .endif
  245. {
  246. sw sp, r0
  247. addli sp, sp, PTREGS_OFFSET_EX1 - PTREGS_OFFSET_PC
  248. }
  249. mfspr r0, SPR_EX_CONTEXT_K_1
  250. {
  251. sw sp, r0
  252. addi sp, sp, PTREGS_OFFSET_FAULTNUM - PTREGS_OFFSET_EX1
  253. /*
  254. * Use r0 for syscalls so it's a temporary; use r1 for interrupts
  255. * so that it gets passed through unchanged to the handler routine.
  256. * Note that the .if conditional confusingly spans bundles.
  257. */
  258. .ifc \processing,handle_syscall
  259. movei r0, \vecnum
  260. }
  261. {
  262. sw sp, r0
  263. .else
  264. movei r1, \vecnum
  265. }
  266. {
  267. sw sp, r1
  268. .endif
  269. addli sp, sp, PTREGS_OFFSET_REG(0) - PTREGS_OFFSET_FAULTNUM
  270. }
  271. mfspr r0, SPR_SYSTEM_SAVE_K_1 /* Original r0 */
  272. {
  273. sw sp, r0
  274. addi sp, sp, -PTREGS_OFFSET_REG(0) - 4
  275. }
  276. {
  277. sw sp, zero /* write zero into "Next SP" frame pointer */
  278. addi sp, sp, -4 /* leave SP pointing at bottom of frame */
  279. }
  280. .ifc \processing,handle_syscall
  281. j handle_syscall
  282. .else
  283. /*
  284. * Capture per-interrupt SPR context to registers.
  285. * We overload the meaning of r3 on this path such that if its bit 31
  286. * is set, we have to mask all interrupts including NMIs before
  287. * clearing the interrupt critical section bit.
  288. * See discussion below at "finish_interrupt_save".
  289. */
  290. .ifc \c_routine, do_page_fault
  291. mfspr r2, SPR_SYSTEM_SAVE_K_3 /* address of page fault */
  292. mfspr r3, SPR_SYSTEM_SAVE_K_2 /* info about page fault */
  293. .else
  294. .ifc \vecnum, INT_DOUBLE_FAULT
  295. {
  296. mfspr r2, SPR_SYSTEM_SAVE_K_2 /* double fault info from HV */
  297. movei r3, 0
  298. }
  299. .else
  300. .ifc \c_routine, do_trap
  301. {
  302. mfspr r2, GPV_REASON
  303. movei r3, 0
  304. }
  305. .else
  306. .ifc \c_routine, op_handle_perf_interrupt
  307. {
  308. mfspr r2, PERF_COUNT_STS
  309. movei r3, -1 /* not used, but set for consistency */
  310. }
  311. .else
  312. #if CHIP_HAS_AUX_PERF_COUNTERS()
  313. .ifc \c_routine, op_handle_aux_perf_interrupt
  314. {
  315. mfspr r2, AUX_PERF_COUNT_STS
  316. movei r3, -1 /* not used, but set for consistency */
  317. }
  318. .else
  319. #endif
  320. movei r3, 0
  321. #if CHIP_HAS_AUX_PERF_COUNTERS()
  322. .endif
  323. #endif
  324. .endif
  325. .endif
  326. .endif
  327. .endif
  328. /* Put function pointer in r0 */
  329. moveli r0, lo16(\c_routine)
  330. {
  331. auli r0, r0, ha16(\c_routine)
  332. j \processing
  333. }
  334. .endif
  335. ENDPROC(intvec_\vecname)
  336. #ifdef __COLLECT_LINKER_FEEDBACK__
  337. .pushsection .text.intvec_feedback,"ax"
  338. .org (\vecnum << 5)
  339. FEEDBACK_ENTER_EXPLICIT(intvec_\vecname, .intrpt1, 1 << 8)
  340. jrp lr
  341. .popsection
  342. #endif
  343. .endm
  344. /*
  345. * Save the rest of the registers that we didn't save in the actual
  346. * vector itself. We can't use r0-r10 inclusive here.
  347. */
  348. .macro finish_interrupt_save, function
  349. /* If it's a syscall, save a proper orig_r0, otherwise just zero. */
  350. PTREGS_PTR(r52, PTREGS_OFFSET_ORIG_R0)
  351. {
  352. .ifc \function,handle_syscall
  353. sw r52, r0
  354. .else
  355. sw r52, zero
  356. .endif
  357. PTREGS_PTR(r52, PTREGS_OFFSET_TP)
  358. }
  359. /*
  360. * For ordinary syscalls, we save neither caller- nor callee-
  361. * save registers, since the syscall invoker doesn't expect the
  362. * caller-saves to be saved, and the called kernel functions will
  363. * take care of saving the callee-saves for us.
  364. *
  365. * For interrupts we save just the caller-save registers. Saving
  366. * them is required (since the "caller" can't save them). Again,
  367. * the called kernel functions will restore the callee-save
  368. * registers for us appropriately.
  369. *
  370. * On return, we normally restore nothing special for syscalls,
  371. * and just the caller-save registers for interrupts.
  372. *
  373. * However, there are some important caveats to all this:
  374. *
  375. * - We always save a few callee-save registers to give us
  376. * some scratchpad registers to carry across function calls.
  377. *
  378. * - fork/vfork/etc require us to save all the callee-save
  379. * registers, which we do in PTREGS_SYSCALL_ALL_REGS, below.
  380. *
  381. * - We always save r0..r5 and r10 for syscalls, since we need
  382. * to reload them a bit later for the actual kernel call, and
  383. * since we might need them for -ERESTARTNOINTR, etc.
  384. *
  385. * - Before invoking a signal handler, we save the unsaved
  386. * callee-save registers so they are visible to the
  387. * signal handler or any ptracer.
  388. *
  389. * - If the unsaved callee-save registers are modified, we set
  390. * a bit in pt_regs so we know to reload them from pt_regs
  391. * and not just rely on the kernel function unwinding.
  392. * (Done for ptrace register writes and SA_SIGINFO handler.)
  393. */
  394. {
  395. sw r52, tp
  396. PTREGS_PTR(r52, PTREGS_OFFSET_REG(33))
  397. }
  398. wh64 r52 /* cache line 2 */
  399. push_reg r33, r52
  400. push_reg r32, r52
  401. push_reg r31, r52
  402. .ifc \function,handle_syscall
  403. push_reg r30, r52, PTREGS_OFFSET_SYSCALL - PTREGS_OFFSET_REG(30)
  404. push_reg TREG_SYSCALL_NR_NAME, r52, \
  405. PTREGS_OFFSET_REG(5) - PTREGS_OFFSET_SYSCALL
  406. .else
  407. push_reg r30, r52, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(30)
  408. wh64 r52 /* cache line 1 */
  409. push_reg r29, r52
  410. push_reg r28, r52
  411. push_reg r27, r52
  412. push_reg r26, r52
  413. push_reg r25, r52
  414. push_reg r24, r52
  415. push_reg r23, r52
  416. push_reg r22, r52
  417. push_reg r21, r52
  418. push_reg r20, r52
  419. push_reg r19, r52
  420. push_reg r18, r52
  421. push_reg r17, r52
  422. push_reg r16, r52
  423. push_reg r15, r52
  424. push_reg r14, r52
  425. push_reg r13, r52
  426. push_reg r12, r52
  427. push_reg r11, r52
  428. push_reg r10, r52
  429. push_reg r9, r52
  430. push_reg r8, r52
  431. push_reg r7, r52
  432. push_reg r6, r52
  433. .endif
  434. push_reg r5, r52
  435. sw r52, r4
  436. /* Load tp with our per-cpu offset. */
  437. #ifdef CONFIG_SMP
  438. {
  439. mfspr r20, SPR_SYSTEM_SAVE_K_0
  440. moveli r21, lo16(__per_cpu_offset)
  441. }
  442. {
  443. auli r21, r21, ha16(__per_cpu_offset)
  444. mm r20, r20, zero, 0, LOG2_THREAD_SIZE-1
  445. }
  446. s2a r20, r20, r21
  447. lw tp, r20
  448. #else
  449. move tp, zero
  450. #endif
  451. /*
  452. * If we will be returning to the kernel, we will need to
  453. * reset the interrupt masks to the state they had before.
  454. * Set DISABLE_IRQ in flags iff we came from PL1 with irqs disabled.
  455. * We load flags in r32 here so we can jump to .Lrestore_regs
  456. * directly after do_page_fault_ics() if necessary.
  457. */
  458. mfspr r32, SPR_EX_CONTEXT_K_1
  459. {
  460. andi r32, r32, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */
  461. PTREGS_PTR(r21, PTREGS_OFFSET_FLAGS)
  462. }
  463. bzt r32, 1f /* zero if from user space */
  464. IRQS_DISABLED(r32) /* zero if irqs enabled */
  465. #if PT_FLAGS_DISABLE_IRQ != 1
  466. # error Value of IRQS_DISABLED used to set PT_FLAGS_DISABLE_IRQ; fix
  467. #endif
  468. 1:
  469. .ifnc \function,handle_syscall
  470. /* Record the fact that we saved the caller-save registers above. */
  471. ori r32, r32, PT_FLAGS_CALLER_SAVES
  472. .endif
  473. sw r21, r32
  474. #ifdef __COLLECT_LINKER_FEEDBACK__
  475. /*
  476. * Notify the feedback routines that we were in the
  477. * appropriate fixed interrupt vector area. Note that we
  478. * still have ICS set at this point, so we can't invoke any
  479. * atomic operations or we will panic. The feedback
  480. * routines internally preserve r0..r10 and r30 up.
  481. */
  482. .ifnc \function,handle_syscall
  483. shli r20, r1, 5
  484. .else
  485. moveli r20, INT_SWINT_1 << 5
  486. .endif
  487. addli r20, r20, lo16(intvec_feedback)
  488. auli r20, r20, ha16(intvec_feedback)
  489. jalr r20
  490. /* And now notify the feedback routines that we are here. */
  491. FEEDBACK_ENTER(\function)
  492. #endif
  493. /*
  494. * we've captured enough state to the stack (including in
  495. * particular our EX_CONTEXT state) that we can now release
  496. * the interrupt critical section and replace it with our
  497. * standard "interrupts disabled" mask value. This allows
  498. * synchronous interrupts (and profile interrupts) to punch
  499. * through from this point onwards.
  500. *
  501. * If bit 31 of r3 is set during a non-NMI interrupt, we know we
  502. * are on the path where the hypervisor has punched through our
  503. * ICS with a page fault, so we call out to do_page_fault_ics()
  504. * to figure out what to do with it. If the fault was in
  505. * an atomic op, we unlock the atomic lock, adjust the
  506. * saved register state a little, and return "zero" in r4,
  507. * falling through into the normal page-fault interrupt code.
  508. * If the fault was in a kernel-space atomic operation, then
  509. * do_page_fault_ics() resolves it itself, returns "one" in r4,
  510. * and as a result goes directly to restoring registers and iret,
  511. * without trying to adjust the interrupt masks at all.
  512. * The do_page_fault_ics() API involves passing and returning
  513. * a five-word struct (in registers) to avoid writing the
  514. * save and restore code here.
  515. */
  516. .ifc \function,handle_nmi
  517. IRQ_DISABLE_ALL(r20)
  518. .else
  519. .ifnc \function,handle_syscall
  520. bgezt r3, 1f
  521. {
  522. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  523. jal do_page_fault_ics
  524. }
  525. FEEDBACK_REENTER(\function)
  526. bzt r4, 1f
  527. j .Lrestore_regs
  528. 1:
  529. .endif
  530. IRQ_DISABLE(r20, r21)
  531. .endif
  532. mtspr INTERRUPT_CRITICAL_SECTION, zero
  533. #if CHIP_HAS_WH64()
  534. /*
  535. * Prepare the first 256 stack bytes to be rapidly accessible
  536. * without having to fetch the background data. We don't really
  537. * know how far to write-hint, but kernel stacks generally
  538. * aren't that big, and write-hinting here does take some time.
  539. */
  540. addi r52, sp, -64
  541. {
  542. wh64 r52
  543. addi r52, r52, -64
  544. }
  545. {
  546. wh64 r52
  547. addi r52, r52, -64
  548. }
  549. {
  550. wh64 r52
  551. addi r52, r52, -64
  552. }
  553. wh64 r52
  554. #endif
  555. #ifdef CONFIG_TRACE_IRQFLAGS
  556. .ifnc \function,handle_nmi
  557. /*
  558. * We finally have enough state set up to notify the irq
  559. * tracing code that irqs were disabled on entry to the handler.
  560. * The TRACE_IRQS_OFF call clobbers registers r0-r29.
  561. * For syscalls, we already have the register state saved away
  562. * on the stack, so we don't bother to do any register saves here,
  563. * and later we pop the registers back off the kernel stack.
  564. * For interrupt handlers, save r0-r3 in callee-saved registers.
  565. */
  566. .ifnc \function,handle_syscall
  567. { move r30, r0; move r31, r1 }
  568. { move r32, r2; move r33, r3 }
  569. .endif
  570. TRACE_IRQS_OFF
  571. .ifnc \function,handle_syscall
  572. { move r0, r30; move r1, r31 }
  573. { move r2, r32; move r3, r33 }
  574. .endif
  575. .endif
  576. #endif
  577. .endm
  578. .macro check_single_stepping, kind, not_single_stepping
  579. /*
  580. * Check for single stepping in user-level priv
  581. * kind can be "normal", "ill", or "syscall"
  582. * At end, if fall-thru
  583. * r29: thread_info->step_state
  584. * r28: &pt_regs->pc
  585. * r27: pt_regs->pc
  586. * r26: thread_info->step_state->buffer
  587. */
  588. /* Check for single stepping */
  589. GET_THREAD_INFO(r29)
  590. {
  591. /* Get pointer to field holding step state */
  592. addi r29, r29, THREAD_INFO_STEP_STATE_OFFSET
  593. /* Get pointer to EX1 in register state */
  594. PTREGS_PTR(r27, PTREGS_OFFSET_EX1)
  595. }
  596. {
  597. /* Get pointer to field holding PC */
  598. PTREGS_PTR(r28, PTREGS_OFFSET_PC)
  599. /* Load the pointer to the step state */
  600. lw r29, r29
  601. }
  602. /* Load EX1 */
  603. lw r27, r27
  604. {
  605. /* Points to flags */
  606. addi r23, r29, SINGLESTEP_STATE_FLAGS_OFFSET
  607. /* No single stepping if there is no step state structure */
  608. bzt r29, \not_single_stepping
  609. }
  610. {
  611. /* mask off ICS and any other high bits */
  612. andi r27, r27, SPR_EX_CONTEXT_1_1__PL_MASK
  613. /* Load pointer to single step instruction buffer */
  614. lw r26, r29
  615. }
  616. /* Check priv state */
  617. bnz r27, \not_single_stepping
  618. /* Get flags */
  619. lw r22, r23
  620. {
  621. /* Branch if single-step mode not enabled */
  622. bbnst r22, \not_single_stepping
  623. /* Clear enabled flag */
  624. andi r22, r22, ~SINGLESTEP_STATE_MASK_IS_ENABLED
  625. }
  626. .ifc \kind,normal
  627. {
  628. /* Load PC */
  629. lw r27, r28
  630. /* Point to the entry containing the original PC */
  631. addi r24, r29, SINGLESTEP_STATE_ORIG_PC_OFFSET
  632. }
  633. {
  634. /* Disable single stepping flag */
  635. sw r23, r22
  636. }
  637. {
  638. /* Get the original pc */
  639. lw r24, r24
  640. /* See if the PC is at the start of the single step buffer */
  641. seq r25, r26, r27
  642. }
  643. /*
  644. * NOTE: it is really expected that the PC be in the single step buffer
  645. * at this point
  646. */
  647. bzt r25, \not_single_stepping
  648. /* Restore the original PC */
  649. sw r28, r24
  650. .else
  651. .ifc \kind,syscall
  652. {
  653. /* Load PC */
  654. lw r27, r28
  655. /* Point to the entry containing the next PC */
  656. addi r24, r29, SINGLESTEP_STATE_NEXT_PC_OFFSET
  657. }
  658. {
  659. /* Increment the stopped PC by the bundle size */
  660. addi r26, r26, 8
  661. /* Disable single stepping flag */
  662. sw r23, r22
  663. }
  664. {
  665. /* Get the next pc */
  666. lw r24, r24
  667. /*
  668. * See if the PC is one bundle past the start of the
  669. * single step buffer
  670. */
  671. seq r25, r26, r27
  672. }
  673. {
  674. /*
  675. * NOTE: it is really expected that the PC be in the
  676. * single step buffer at this point
  677. */
  678. bzt r25, \not_single_stepping
  679. }
  680. /* Set to the next PC */
  681. sw r28, r24
  682. .else
  683. {
  684. /* Point to 3rd bundle in buffer */
  685. addi r25, r26, 16
  686. /* Load PC */
  687. lw r27, r28
  688. }
  689. {
  690. /* Disable single stepping flag */
  691. sw r23, r22
  692. /* See if the PC is in the single step buffer */
  693. slte_u r24, r26, r27
  694. }
  695. {
  696. slte_u r25, r27, r25
  697. /*
  698. * NOTE: it is really expected that the PC be in the
  699. * single step buffer at this point
  700. */
  701. bzt r24, \not_single_stepping
  702. }
  703. bzt r25, \not_single_stepping
  704. .endif
  705. .endif
  706. .endm
  707. /*
  708. * Redispatch a downcall.
  709. */
  710. .macro dc_dispatch vecnum, vecname
  711. .org (\vecnum << 8)
  712. intvec_\vecname:
  713. j hv_downcall_dispatch
  714. ENDPROC(intvec_\vecname)
  715. .endm
  716. /*
  717. * Common code for most interrupts. The C function we're eventually
  718. * going to is in r0, and the faultnum is in r1; the original
  719. * values for those registers are on the stack.
  720. */
  721. .pushsection .text.handle_interrupt,"ax"
  722. handle_interrupt:
  723. finish_interrupt_save handle_interrupt
  724. /*
  725. * Check for if we are single stepping in user level. If so, then
  726. * we need to restore the PC.
  727. */
  728. check_single_stepping normal, .Ldispatch_interrupt
  729. .Ldispatch_interrupt:
  730. /* Jump to the C routine; it should enable irqs as soon as possible. */
  731. {
  732. jalr r0
  733. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  734. }
  735. FEEDBACK_REENTER(handle_interrupt)
  736. {
  737. movei r30, 0 /* not an NMI */
  738. j interrupt_return
  739. }
  740. STD_ENDPROC(handle_interrupt)
  741. /*
  742. * This routine takes a boolean in r30 indicating if this is an NMI.
  743. * If so, we also expect a boolean in r31 indicating whether to
  744. * re-enable the oprofile interrupts.
  745. */
  746. STD_ENTRY(interrupt_return)
  747. /* If we're resuming to kernel space, don't check thread flags. */
  748. {
  749. bnz r30, .Lrestore_all /* NMIs don't special-case user-space */
  750. PTREGS_PTR(r29, PTREGS_OFFSET_EX1)
  751. }
  752. lw r29, r29
  753. andi r29, r29, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */
  754. {
  755. bzt r29, .Lresume_userspace
  756. PTREGS_PTR(r29, PTREGS_OFFSET_PC)
  757. }
  758. /* If we're resuming to _cpu_idle_nap, bump PC forward by 8. */
  759. {
  760. lw r28, r29
  761. moveli r27, lo16(_cpu_idle_nap)
  762. }
  763. {
  764. auli r27, r27, ha16(_cpu_idle_nap)
  765. }
  766. {
  767. seq r27, r27, r28
  768. }
  769. {
  770. bbns r27, .Lrestore_all
  771. addi r28, r28, 8
  772. }
  773. sw r29, r28
  774. j .Lrestore_all
  775. .Lresume_userspace:
  776. FEEDBACK_REENTER(interrupt_return)
  777. /*
  778. * Disable interrupts so as to make sure we don't
  779. * miss an interrupt that sets any of the thread flags (like
  780. * need_resched or sigpending) between sampling and the iret.
  781. * Routines like schedule() or do_signal() may re-enable
  782. * interrupts before returning.
  783. */
  784. IRQ_DISABLE(r20, r21)
  785. TRACE_IRQS_OFF /* Note: clobbers registers r0-r29 */
  786. /* Get base of stack in r32; note r30/31 are used as arguments here. */
  787. GET_THREAD_INFO(r32)
  788. /* Check to see if there is any work to do before returning to user. */
  789. {
  790. addi r29, r32, THREAD_INFO_FLAGS_OFFSET
  791. moveli r28, lo16(_TIF_ALLWORK_MASK)
  792. }
  793. {
  794. lw r29, r29
  795. auli r28, r28, ha16(_TIF_ALLWORK_MASK)
  796. }
  797. and r28, r29, r28
  798. bnz r28, .Lwork_pending
  799. /*
  800. * In the NMI case we
  801. * omit the call to single_process_check_nohz, which normally checks
  802. * to see if we should start or stop the scheduler tick, because
  803. * we can't call arbitrary Linux code from an NMI context.
  804. * We always call the homecache TLB deferral code to re-trigger
  805. * the deferral mechanism.
  806. *
  807. * The other chunk of responsibility this code has is to reset the
  808. * interrupt masks appropriately to reset irqs and NMIs. We have
  809. * to call TRACE_IRQS_OFF and TRACE_IRQS_ON to support all the
  810. * lockdep-type stuff, but we can't set ICS until afterwards, since
  811. * ICS can only be used in very tight chunks of code to avoid
  812. * tripping over various assertions that it is off.
  813. *
  814. * (There is what looks like a window of vulnerability here since
  815. * we might take a profile interrupt between the two SPR writes
  816. * that set the mask, but since we write the low SPR word first,
  817. * and our interrupt entry code checks the low SPR word, any
  818. * profile interrupt will actually disable interrupts in both SPRs
  819. * before returning, which is OK.)
  820. */
  821. .Lrestore_all:
  822. PTREGS_PTR(r0, PTREGS_OFFSET_EX1)
  823. {
  824. lw r0, r0
  825. PTREGS_PTR(r32, PTREGS_OFFSET_FLAGS)
  826. }
  827. {
  828. andi r0, r0, SPR_EX_CONTEXT_1_1__PL_MASK
  829. lw r32, r32
  830. }
  831. bnz r0, 1f
  832. j 2f
  833. #if PT_FLAGS_DISABLE_IRQ != 1
  834. # error Assuming PT_FLAGS_DISABLE_IRQ == 1 so we can use bbnst below
  835. #endif
  836. 1: bbnst r32, 2f
  837. IRQ_DISABLE(r20,r21)
  838. TRACE_IRQS_OFF
  839. movei r0, 1
  840. mtspr INTERRUPT_CRITICAL_SECTION, r0
  841. bzt r30, .Lrestore_regs
  842. j 3f
  843. 2: TRACE_IRQS_ON
  844. movei r0, 1
  845. mtspr INTERRUPT_CRITICAL_SECTION, r0
  846. IRQ_ENABLE(r20, r21)
  847. bzt r30, .Lrestore_regs
  848. 3:
  849. /*
  850. * We now commit to returning from this interrupt, since we will be
  851. * doing things like setting EX_CONTEXT SPRs and unwinding the stack
  852. * frame. No calls should be made to any other code after this point.
  853. * This code should only be entered with ICS set.
  854. * r32 must still be set to ptregs.flags.
  855. * We launch loads to each cache line separately first, so we can
  856. * get some parallelism out of the memory subsystem.
  857. * We start zeroing caller-saved registers throughout, since
  858. * that will save some cycles if this turns out to be a syscall.
  859. */
  860. .Lrestore_regs:
  861. FEEDBACK_REENTER(interrupt_return) /* called from elsewhere */
  862. /*
  863. * Rotate so we have one high bit and one low bit to test.
  864. * - low bit says whether to restore all the callee-saved registers,
  865. * or just r30-r33, and r52 up.
  866. * - high bit (i.e. sign bit) says whether to restore all the
  867. * caller-saved registers, or just r0.
  868. */
  869. #if PT_FLAGS_CALLER_SAVES != 2 || PT_FLAGS_RESTORE_REGS != 4
  870. # error Rotate trick does not work :-)
  871. #endif
  872. {
  873. rli r20, r32, 30
  874. PTREGS_PTR(sp, PTREGS_OFFSET_REG(0))
  875. }
  876. /*
  877. * Load cache lines 0, 2, and 3 in that order, then use
  878. * the last loaded value, which makes it likely that the other
  879. * cache lines have also loaded, at which point we should be
  880. * able to safely read all the remaining words on those cache
  881. * lines without waiting for the memory subsystem.
  882. */
  883. pop_reg_zero r0, r28, sp, PTREGS_OFFSET_REG(30) - PTREGS_OFFSET_REG(0)
  884. pop_reg_zero r30, r2, sp, PTREGS_OFFSET_PC - PTREGS_OFFSET_REG(30)
  885. pop_reg_zero r21, r3, sp, PTREGS_OFFSET_EX1 - PTREGS_OFFSET_PC
  886. pop_reg_zero lr, r4, sp, PTREGS_OFFSET_REG(52) - PTREGS_OFFSET_EX1
  887. {
  888. mtspr SPR_EX_CONTEXT_K_0, r21
  889. move r5, zero
  890. }
  891. {
  892. mtspr SPR_EX_CONTEXT_K_1, lr
  893. andi lr, lr, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */
  894. }
  895. /* Restore callee-saveds that we actually use. */
  896. pop_reg_zero r52, r6, sp, PTREGS_OFFSET_REG(31) - PTREGS_OFFSET_REG(52)
  897. pop_reg_zero r31, r7
  898. pop_reg_zero r32, r8
  899. pop_reg_zero r33, r9, sp, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(33)
  900. /*
  901. * If we modified other callee-saveds, restore them now.
  902. * This is rare, but could be via ptrace or signal handler.
  903. */
  904. {
  905. move r10, zero
  906. bbs r20, .Lrestore_callees
  907. }
  908. .Lcontinue_restore_regs:
  909. /* Check if we're returning from a syscall. */
  910. {
  911. move r11, zero
  912. blzt r20, 1f /* no, so go restore callee-save registers */
  913. }
  914. /*
  915. * Check if we're returning to userspace.
  916. * Note that if we're not, we don't worry about zeroing everything.
  917. */
  918. {
  919. addli sp, sp, PTREGS_OFFSET_LR - PTREGS_OFFSET_REG(29)
  920. bnz lr, .Lkernel_return
  921. }
  922. /*
  923. * On return from syscall, we've restored r0 from pt_regs, but we
  924. * clear the remainder of the caller-saved registers. We could
  925. * restore the syscall arguments, but there's not much point,
  926. * and it ensures user programs aren't trying to use the
  927. * caller-saves if we clear them, as well as avoiding leaking
  928. * kernel pointers into userspace.
  929. */
  930. pop_reg_zero lr, r12, sp, PTREGS_OFFSET_TP - PTREGS_OFFSET_LR
  931. pop_reg_zero tp, r13, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_TP
  932. {
  933. lw sp, sp
  934. move r14, zero
  935. move r15, zero
  936. }
  937. { move r16, zero; move r17, zero }
  938. { move r18, zero; move r19, zero }
  939. { move r20, zero; move r21, zero }
  940. { move r22, zero; move r23, zero }
  941. { move r24, zero; move r25, zero }
  942. { move r26, zero; move r27, zero }
  943. /* Set r1 to errno if we are returning an error, otherwise zero. */
  944. {
  945. moveli r29, 4096
  946. sub r1, zero, r0
  947. }
  948. slt_u r29, r1, r29
  949. {
  950. mnz r1, r29, r1
  951. move r29, zero
  952. }
  953. iret
  954. /*
  955. * Not a syscall, so restore caller-saved registers.
  956. * First kick off a load for cache line 1, which we're touching
  957. * for the first time here.
  958. */
  959. .align 64
  960. 1: pop_reg r29, sp, PTREGS_OFFSET_REG(1) - PTREGS_OFFSET_REG(29)
  961. pop_reg r1
  962. pop_reg r2
  963. pop_reg r3
  964. pop_reg r4
  965. pop_reg r5
  966. pop_reg r6
  967. pop_reg r7
  968. pop_reg r8
  969. pop_reg r9
  970. pop_reg r10
  971. pop_reg r11
  972. pop_reg r12
  973. pop_reg r13
  974. pop_reg r14
  975. pop_reg r15
  976. pop_reg r16
  977. pop_reg r17
  978. pop_reg r18
  979. pop_reg r19
  980. pop_reg r20
  981. pop_reg r21
  982. pop_reg r22
  983. pop_reg r23
  984. pop_reg r24
  985. pop_reg r25
  986. pop_reg r26
  987. pop_reg r27
  988. pop_reg r28, sp, PTREGS_OFFSET_LR - PTREGS_OFFSET_REG(28)
  989. /* r29 already restored above */
  990. bnz lr, .Lkernel_return
  991. pop_reg lr, sp, PTREGS_OFFSET_TP - PTREGS_OFFSET_LR
  992. pop_reg tp, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_TP
  993. lw sp, sp
  994. iret
  995. /*
  996. * We can't restore tp when in kernel mode, since a thread might
  997. * have migrated from another cpu and brought a stale tp value.
  998. */
  999. .Lkernel_return:
  1000. pop_reg lr, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_LR
  1001. lw sp, sp
  1002. iret
  1003. /* Restore callee-saved registers from r34 to r51. */
  1004. .Lrestore_callees:
  1005. addli sp, sp, PTREGS_OFFSET_REG(34) - PTREGS_OFFSET_REG(29)
  1006. pop_reg r34
  1007. pop_reg r35
  1008. pop_reg r36
  1009. pop_reg r37
  1010. pop_reg r38
  1011. pop_reg r39
  1012. pop_reg r40
  1013. pop_reg r41
  1014. pop_reg r42
  1015. pop_reg r43
  1016. pop_reg r44
  1017. pop_reg r45
  1018. pop_reg r46
  1019. pop_reg r47
  1020. pop_reg r48
  1021. pop_reg r49
  1022. pop_reg r50
  1023. pop_reg r51, sp, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(51)
  1024. j .Lcontinue_restore_regs
  1025. .Lwork_pending:
  1026. /* Mask the reschedule flag */
  1027. andi r28, r29, _TIF_NEED_RESCHED
  1028. {
  1029. /*
  1030. * If the NEED_RESCHED flag is called, we call schedule(), which
  1031. * may drop this context right here and go do something else.
  1032. * On return, jump back to .Lresume_userspace and recheck.
  1033. */
  1034. bz r28, .Lasync_tlb
  1035. /* Mask the async-tlb flag */
  1036. andi r28, r29, _TIF_ASYNC_TLB
  1037. }
  1038. jal schedule
  1039. FEEDBACK_REENTER(interrupt_return)
  1040. /* Reload the flags and check again */
  1041. j .Lresume_userspace
  1042. .Lasync_tlb:
  1043. {
  1044. bz r28, .Lneed_sigpending
  1045. /* Mask the sigpending flag */
  1046. andi r28, r29, _TIF_SIGPENDING
  1047. }
  1048. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  1049. jal do_async_page_fault
  1050. FEEDBACK_REENTER(interrupt_return)
  1051. /*
  1052. * Go restart the "resume userspace" process. We may have
  1053. * fired a signal, and we need to disable interrupts again.
  1054. */
  1055. j .Lresume_userspace
  1056. .Lneed_sigpending:
  1057. /*
  1058. * At this point we are either doing signal handling or single-step,
  1059. * so either way make sure we have all the registers saved.
  1060. */
  1061. push_extra_callee_saves r0
  1062. {
  1063. /* If no signal pending, skip to singlestep check */
  1064. bz r28, .Lneed_singlestep
  1065. /* Mask the singlestep flag */
  1066. andi r28, r29, _TIF_SINGLESTEP
  1067. }
  1068. jal do_signal
  1069. FEEDBACK_REENTER(interrupt_return)
  1070. /* Reload the flags and check again */
  1071. j .Lresume_userspace
  1072. .Lneed_singlestep:
  1073. {
  1074. /* Get a pointer to the EX1 field */
  1075. PTREGS_PTR(r29, PTREGS_OFFSET_EX1)
  1076. /* If we get here, our bit must be set. */
  1077. bz r28, .Lwork_confusion
  1078. }
  1079. /* If we are in priv mode, don't single step */
  1080. lw r28, r29
  1081. andi r28, r28, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */
  1082. bnz r28, .Lrestore_all
  1083. /* Allow interrupts within the single step code */
  1084. TRACE_IRQS_ON /* Note: clobbers registers r0-r29 */
  1085. IRQ_ENABLE(r20, r21)
  1086. /* try to single-step the current instruction */
  1087. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  1088. jal single_step_once
  1089. FEEDBACK_REENTER(interrupt_return)
  1090. /* Re-disable interrupts. TRACE_IRQS_OFF in .Lrestore_all. */
  1091. IRQ_DISABLE(r20,r21)
  1092. j .Lrestore_all
  1093. .Lwork_confusion:
  1094. move r0, r28
  1095. panic "thread_info allwork flags unhandled on userspace resume: %#x"
  1096. STD_ENDPROC(interrupt_return)
  1097. /*
  1098. * This interrupt variant clears the INT_INTCTRL_K interrupt mask bit
  1099. * before returning, so we can properly get more downcalls.
  1100. */
  1101. .pushsection .text.handle_interrupt_downcall,"ax"
  1102. handle_interrupt_downcall:
  1103. finish_interrupt_save handle_interrupt_downcall
  1104. check_single_stepping normal, .Ldispatch_downcall
  1105. .Ldispatch_downcall:
  1106. /* Clear INTCTRL_K from the set of interrupts we ever enable. */
  1107. GET_INTERRUPTS_ENABLED_MASK_PTR(r30)
  1108. {
  1109. addi r30, r30, 4
  1110. movei r31, INT_MASK(INT_INTCTRL_K)
  1111. }
  1112. {
  1113. lw r20, r30
  1114. nor r21, r31, zero
  1115. }
  1116. and r20, r20, r21
  1117. sw r30, r20
  1118. {
  1119. jalr r0
  1120. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  1121. }
  1122. FEEDBACK_REENTER(handle_interrupt_downcall)
  1123. /* Allow INTCTRL_K to be enabled next time we enable interrupts. */
  1124. lw r20, r30
  1125. or r20, r20, r31
  1126. sw r30, r20
  1127. {
  1128. movei r30, 0 /* not an NMI */
  1129. j interrupt_return
  1130. }
  1131. STD_ENDPROC(handle_interrupt_downcall)
  1132. /*
  1133. * Some interrupts don't check for single stepping
  1134. */
  1135. .pushsection .text.handle_interrupt_no_single_step,"ax"
  1136. handle_interrupt_no_single_step:
  1137. finish_interrupt_save handle_interrupt_no_single_step
  1138. {
  1139. jalr r0
  1140. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  1141. }
  1142. FEEDBACK_REENTER(handle_interrupt_no_single_step)
  1143. {
  1144. movei r30, 0 /* not an NMI */
  1145. j interrupt_return
  1146. }
  1147. STD_ENDPROC(handle_interrupt_no_single_step)
  1148. /*
  1149. * "NMI" interrupts mask ALL interrupts before calling the
  1150. * handler, and don't check thread flags, etc., on the way
  1151. * back out. In general, the only things we do here for NMIs
  1152. * are the register save/restore, fixing the PC if we were
  1153. * doing single step, and the dataplane kernel-TLB management.
  1154. * We don't (for example) deal with start/stop of the sched tick.
  1155. */
  1156. .pushsection .text.handle_nmi,"ax"
  1157. handle_nmi:
  1158. finish_interrupt_save handle_nmi
  1159. check_single_stepping normal, .Ldispatch_nmi
  1160. .Ldispatch_nmi:
  1161. {
  1162. jalr r0
  1163. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  1164. }
  1165. FEEDBACK_REENTER(handle_nmi)
  1166. j interrupt_return
  1167. STD_ENDPROC(handle_nmi)
  1168. /*
  1169. * Parallel code for syscalls to handle_interrupt.
  1170. */
  1171. .pushsection .text.handle_syscall,"ax"
  1172. handle_syscall:
  1173. finish_interrupt_save handle_syscall
  1174. /*
  1175. * Check for if we are single stepping in user level. If so, then
  1176. * we need to restore the PC.
  1177. */
  1178. check_single_stepping syscall, .Ldispatch_syscall
  1179. .Ldispatch_syscall:
  1180. /* Enable irqs. */
  1181. TRACE_IRQS_ON
  1182. IRQ_ENABLE(r20, r21)
  1183. /* Bump the counter for syscalls made on this tile. */
  1184. moveli r20, lo16(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)
  1185. auli r20, r20, ha16(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)
  1186. add r20, r20, tp
  1187. lw r21, r20
  1188. addi r21, r21, 1
  1189. sw r20, r21
  1190. /* Trace syscalls, if requested. */
  1191. GET_THREAD_INFO(r31)
  1192. addi r31, r31, THREAD_INFO_FLAGS_OFFSET
  1193. lw r30, r31
  1194. andi r30, r30, _TIF_SYSCALL_TRACE
  1195. bzt r30, .Lrestore_syscall_regs
  1196. jal do_syscall_trace
  1197. FEEDBACK_REENTER(handle_syscall)
  1198. /*
  1199. * We always reload our registers from the stack at this
  1200. * point. They might be valid, if we didn't build with
  1201. * TRACE_IRQFLAGS, and this isn't a dataplane tile, and we're not
  1202. * doing syscall tracing, but there are enough cases now that it
  1203. * seems simplest just to do the reload unconditionally.
  1204. */
  1205. .Lrestore_syscall_regs:
  1206. PTREGS_PTR(r11, PTREGS_OFFSET_REG(0))
  1207. pop_reg r0, r11
  1208. pop_reg r1, r11
  1209. pop_reg r2, r11
  1210. pop_reg r3, r11
  1211. pop_reg r4, r11
  1212. pop_reg r5, r11, PTREGS_OFFSET_SYSCALL - PTREGS_OFFSET_REG(5)
  1213. pop_reg TREG_SYSCALL_NR_NAME, r11
  1214. /* Ensure that the syscall number is within the legal range. */
  1215. moveli r21, __NR_syscalls
  1216. {
  1217. slt_u r21, TREG_SYSCALL_NR_NAME, r21
  1218. moveli r20, lo16(sys_call_table)
  1219. }
  1220. {
  1221. bbns r21, .Linvalid_syscall
  1222. auli r20, r20, ha16(sys_call_table)
  1223. }
  1224. s2a r20, TREG_SYSCALL_NR_NAME, r20
  1225. lw r20, r20
  1226. /* Jump to syscall handler. */
  1227. jalr r20
  1228. .Lhandle_syscall_link: /* value of "lr" after "jalr r20" above */
  1229. /*
  1230. * Write our r0 onto the stack so it gets restored instead
  1231. * of whatever the user had there before.
  1232. */
  1233. PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))
  1234. sw r29, r0
  1235. .Lsyscall_sigreturn_skip:
  1236. FEEDBACK_REENTER(handle_syscall)
  1237. /* Do syscall trace again, if requested. */
  1238. lw r30, r31
  1239. andi r30, r30, _TIF_SYSCALL_TRACE
  1240. bzt r30, 1f
  1241. jal do_syscall_trace
  1242. FEEDBACK_REENTER(handle_syscall)
  1243. 1: j .Lresume_userspace /* jump into middle of interrupt_return */
  1244. .Linvalid_syscall:
  1245. /* Report an invalid syscall back to the user program */
  1246. {
  1247. PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))
  1248. movei r28, -ENOSYS
  1249. }
  1250. sw r29, r28
  1251. j .Lresume_userspace /* jump into middle of interrupt_return */
  1252. STD_ENDPROC(handle_syscall)
  1253. /* Return the address for oprofile to suppress in backtraces. */
  1254. STD_ENTRY_SECTION(handle_syscall_link_address, .text.handle_syscall)
  1255. lnk r0
  1256. {
  1257. addli r0, r0, .Lhandle_syscall_link - .
  1258. jrp lr
  1259. }
  1260. STD_ENDPROC(handle_syscall_link_address)
  1261. STD_ENTRY(ret_from_fork)
  1262. jal sim_notify_fork
  1263. jal schedule_tail
  1264. FEEDBACK_REENTER(ret_from_fork)
  1265. j .Lresume_userspace /* jump into middle of interrupt_return */
  1266. STD_ENDPROC(ret_from_fork)
  1267. /*
  1268. * Code for ill interrupt.
  1269. */
  1270. .pushsection .text.handle_ill,"ax"
  1271. handle_ill:
  1272. finish_interrupt_save handle_ill
  1273. /*
  1274. * Check for if we are single stepping in user level. If so, then
  1275. * we need to restore the PC.
  1276. */
  1277. check_single_stepping ill, .Ldispatch_normal_ill
  1278. {
  1279. /* See if the PC is the 1st bundle in the buffer */
  1280. seq r25, r27, r26
  1281. /* Point to the 2nd bundle in the buffer */
  1282. addi r26, r26, 8
  1283. }
  1284. {
  1285. /* Point to the original pc */
  1286. addi r24, r29, SINGLESTEP_STATE_ORIG_PC_OFFSET
  1287. /* Branch if the PC is the 1st bundle in the buffer */
  1288. bnz r25, 3f
  1289. }
  1290. {
  1291. /* See if the PC is the 2nd bundle of the buffer */
  1292. seq r25, r27, r26
  1293. /* Set PC to next instruction */
  1294. addi r24, r29, SINGLESTEP_STATE_NEXT_PC_OFFSET
  1295. }
  1296. {
  1297. /* Point to flags */
  1298. addi r25, r29, SINGLESTEP_STATE_FLAGS_OFFSET
  1299. /* Branch if PC is in the second bundle */
  1300. bz r25, 2f
  1301. }
  1302. /* Load flags */
  1303. lw r25, r25
  1304. {
  1305. /*
  1306. * Get the offset for the register to restore
  1307. * Note: the lower bound is 2, so we have implicit scaling by 4.
  1308. * No multiplication of the register number by the size of a register
  1309. * is needed.
  1310. */
  1311. mm r27, r25, zero, SINGLESTEP_STATE_TARGET_LB, \
  1312. SINGLESTEP_STATE_TARGET_UB
  1313. /* Mask Rewrite_LR */
  1314. andi r25, r25, SINGLESTEP_STATE_MASK_UPDATE
  1315. }
  1316. {
  1317. addi r29, r29, SINGLESTEP_STATE_UPDATE_VALUE_OFFSET
  1318. /* Don't rewrite temp register */
  1319. bz r25, 3f
  1320. }
  1321. {
  1322. /* Get the temp value */
  1323. lw r29, r29
  1324. /* Point to where the register is stored */
  1325. add r27, r27, sp
  1326. }
  1327. /* Add in the C ABI save area size to the register offset */
  1328. addi r27, r27, C_ABI_SAVE_AREA_SIZE
  1329. /* Restore the user's register with the temp value */
  1330. sw r27, r29
  1331. j 3f
  1332. 2:
  1333. /* Must be in the third bundle */
  1334. addi r24, r29, SINGLESTEP_STATE_BRANCH_NEXT_PC_OFFSET
  1335. 3:
  1336. /* set PC and continue */
  1337. lw r26, r24
  1338. sw r28, r26
  1339. /*
  1340. * Clear TIF_SINGLESTEP to prevent recursion if we execute an ill.
  1341. * The normal non-arch flow redundantly clears TIF_SINGLESTEP, but we
  1342. * need to clear it here and can't really impose on all other arches.
  1343. * So what's another write between friends?
  1344. */
  1345. GET_THREAD_INFO(r0)
  1346. addi r1, r0, THREAD_INFO_FLAGS_OFFSET
  1347. {
  1348. lw r2, r1
  1349. addi r0, r0, THREAD_INFO_TASK_OFFSET /* currently a no-op */
  1350. }
  1351. andi r2, r2, ~_TIF_SINGLESTEP
  1352. sw r1, r2
  1353. /* Issue a sigtrap */
  1354. {
  1355. lw r0, r0 /* indirect thru thread_info to get task_info*/
  1356. addi r1, sp, C_ABI_SAVE_AREA_SIZE /* put ptregs pointer into r1 */
  1357. move r2, zero /* load error code into r2 */
  1358. }
  1359. jal send_sigtrap /* issue a SIGTRAP */
  1360. FEEDBACK_REENTER(handle_ill)
  1361. j .Lresume_userspace /* jump into middle of interrupt_return */
  1362. .Ldispatch_normal_ill:
  1363. {
  1364. jalr r0
  1365. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  1366. }
  1367. FEEDBACK_REENTER(handle_ill)
  1368. {
  1369. movei r30, 0 /* not an NMI */
  1370. j interrupt_return
  1371. }
  1372. STD_ENDPROC(handle_ill)
  1373. /* Various stub interrupt handlers and syscall handlers */
  1374. STD_ENTRY_LOCAL(_kernel_double_fault)
  1375. mfspr r1, SPR_EX_CONTEXT_K_0
  1376. move r2, lr
  1377. move r3, sp
  1378. move r4, r52
  1379. addi sp, sp, -C_ABI_SAVE_AREA_SIZE
  1380. j kernel_double_fault
  1381. STD_ENDPROC(_kernel_double_fault)
  1382. STD_ENTRY_LOCAL(bad_intr)
  1383. mfspr r2, SPR_EX_CONTEXT_K_0
  1384. panic "Unhandled interrupt %#x: PC %#lx"
  1385. STD_ENDPROC(bad_intr)
  1386. /* Put address of pt_regs in reg and jump. */
  1387. #define PTREGS_SYSCALL(x, reg) \
  1388. STD_ENTRY(_##x); \
  1389. { \
  1390. PTREGS_PTR(reg, PTREGS_OFFSET_BASE); \
  1391. j x \
  1392. }; \
  1393. STD_ENDPROC(_##x)
  1394. /*
  1395. * Special-case sigreturn to not write r0 to the stack on return.
  1396. * This is technically more efficient, but it also avoids difficulties
  1397. * in the 64-bit OS when handling 32-bit compat code, since we must not
  1398. * sign-extend r0 for the sigreturn return-value case.
  1399. */
  1400. #define PTREGS_SYSCALL_SIGRETURN(x, reg) \
  1401. STD_ENTRY(_##x); \
  1402. addli lr, lr, .Lsyscall_sigreturn_skip - .Lhandle_syscall_link; \
  1403. { \
  1404. PTREGS_PTR(reg, PTREGS_OFFSET_BASE); \
  1405. j x \
  1406. }; \
  1407. STD_ENDPROC(_##x)
  1408. PTREGS_SYSCALL(sys_execve, r3)
  1409. PTREGS_SYSCALL(sys_sigaltstack, r2)
  1410. PTREGS_SYSCALL_SIGRETURN(sys_rt_sigreturn, r0)
  1411. PTREGS_SYSCALL(sys_cmpxchg_badaddr, r1)
  1412. /* Save additional callee-saves to pt_regs, put address in r4 and jump. */
  1413. STD_ENTRY(_sys_clone)
  1414. push_extra_callee_saves r4
  1415. j sys_clone
  1416. STD_ENDPROC(_sys_clone)
  1417. /*
  1418. * This entrypoint is taken for the cmpxchg and atomic_update fast
  1419. * swints. We may wish to generalize it to other fast swints at some
  1420. * point, but for now there are just two very similar ones, which
  1421. * makes it faster.
  1422. *
  1423. * The fast swint code is designed to have a small footprint. It does
  1424. * not save or restore any GPRs, counting on the caller-save registers
  1425. * to be available to it on entry. It does not modify any callee-save
  1426. * registers (including "lr"). It does not check what PL it is being
  1427. * called at, so you'd better not call it other than at PL0.
  1428. * The <atomic.h> wrapper assumes it only clobbers r20-r29, so if
  1429. * it ever is necessary to use more registers, be aware.
  1430. *
  1431. * It does not use the stack, but since it might be re-interrupted by
  1432. * a page fault which would assume the stack was valid, it does
  1433. * save/restore the stack pointer and zero it out to make sure it gets reset.
  1434. * Since we always keep interrupts disabled, the hypervisor won't
  1435. * clobber our EX_CONTEXT_K_x registers, so we don't save/restore them
  1436. * (other than to advance the PC on return).
  1437. *
  1438. * We have to manually validate the user vs kernel address range
  1439. * (since at PL1 we can read/write both), and for performance reasons
  1440. * we don't allow cmpxchg on the fc000000 memory region, since we only
  1441. * validate that the user address is below PAGE_OFFSET.
  1442. *
  1443. * We place it in the __HEAD section to ensure it is relatively
  1444. * near to the intvec_SWINT_1 code (reachable by a conditional branch).
  1445. *
  1446. * Must match register usage in do_page_fault().
  1447. */
  1448. __HEAD
  1449. .align 64
  1450. /* Align much later jump on the start of a cache line. */
  1451. #if !ATOMIC_LOCKS_FOUND_VIA_TABLE()
  1452. nop; nop
  1453. #endif
  1454. ENTRY(sys_cmpxchg)
  1455. /*
  1456. * Save "sp" and set it zero for any possible page fault.
  1457. *
  1458. * HACK: We want to both zero sp and check r0's alignment,
  1459. * so we do both at once. If "sp" becomes nonzero we
  1460. * know r0 is unaligned and branch to the error handler that
  1461. * restores sp, so this is OK.
  1462. *
  1463. * ICS is disabled right now so having a garbage but nonzero
  1464. * sp is OK, since we won't execute any faulting instructions
  1465. * when it is nonzero.
  1466. */
  1467. {
  1468. move r27, sp
  1469. andi sp, r0, 3
  1470. }
  1471. /*
  1472. * Get the lock address in ATOMIC_LOCK_REG, and also validate that the
  1473. * address is less than PAGE_OFFSET, since that won't trap at PL1.
  1474. * We only use bits less than PAGE_SHIFT to avoid having to worry
  1475. * about aliasing among multiple mappings of the same physical page,
  1476. * and we ignore the low 3 bits so we have one lock that covers
  1477. * both a cmpxchg64() and a cmpxchg() on either its low or high word.
  1478. * NOTE: this code must match __atomic_hashed_lock() in lib/atomic.c.
  1479. */
  1480. #if ATOMIC_LOCKS_FOUND_VIA_TABLE()
  1481. {
  1482. /* Check for unaligned input. */
  1483. bnz sp, .Lcmpxchg_badaddr
  1484. mm r25, r0, zero, 3, PAGE_SHIFT-1
  1485. }
  1486. {
  1487. crc32_32 r25, zero, r25
  1488. moveli r21, lo16(atomic_lock_ptr)
  1489. }
  1490. {
  1491. auli r21, r21, ha16(atomic_lock_ptr)
  1492. auli r23, zero, hi16(PAGE_OFFSET) /* hugepage-aligned */
  1493. }
  1494. {
  1495. shri r20, r25, 32 - ATOMIC_HASH_L1_SHIFT
  1496. slt_u r23, r0, r23
  1497. /*
  1498. * Ensure that the TLB is loaded before we take out the lock.
  1499. * On TILEPro, this will start fetching the value all the way
  1500. * into our L1 as well (and if it gets modified before we
  1501. * grab the lock, it will be invalidated from our cache
  1502. * before we reload it). On tile64, we'll start fetching it
  1503. * into our L1 if we're the home, and if we're not, we'll
  1504. * still at least start fetching it into the home's L2.
  1505. */
  1506. lw r26, r0
  1507. }
  1508. {
  1509. s2a r21, r20, r21
  1510. bbns r23, .Lcmpxchg_badaddr
  1511. }
  1512. {
  1513. lw r21, r21
  1514. seqi r23, TREG_SYSCALL_NR_NAME, __NR_FAST_cmpxchg64
  1515. andi r25, r25, ATOMIC_HASH_L2_SIZE - 1
  1516. }
  1517. {
  1518. /* Branch away at this point if we're doing a 64-bit cmpxchg. */
  1519. bbs r23, .Lcmpxchg64
  1520. andi r23, r0, 7 /* Precompute alignment for cmpxchg64. */
  1521. }
  1522. {
  1523. /*
  1524. * We very carefully align the code that actually runs with
  1525. * the lock held (nine bundles) so that we know it is all in
  1526. * the icache when we start. This instruction (the jump) is
  1527. * at the start of the first cache line, address zero mod 64;
  1528. * we jump to somewhere in the second cache line to issue the
  1529. * tns, then jump back to finish up.
  1530. */
  1531. s2a ATOMIC_LOCK_REG_NAME, r25, r21
  1532. j .Lcmpxchg32_tns
  1533. }
  1534. #else /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */
  1535. {
  1536. /* Check for unaligned input. */
  1537. bnz sp, .Lcmpxchg_badaddr
  1538. auli r23, zero, hi16(PAGE_OFFSET) /* hugepage-aligned */
  1539. }
  1540. {
  1541. /*
  1542. * Slide bits into position for 'mm'. We want to ignore
  1543. * the low 3 bits of r0, and consider only the next
  1544. * ATOMIC_HASH_SHIFT bits.
  1545. * Because of C pointer arithmetic, we want to compute this:
  1546. *
  1547. * ((char*)atomic_locks +
  1548. * (((r0 >> 3) & (1 << (ATOMIC_HASH_SIZE - 1))) << 2))
  1549. *
  1550. * Instead of two shifts we just ">> 1", and use 'mm'
  1551. * to ignore the low and high bits we don't want.
  1552. */
  1553. shri r25, r0, 1
  1554. slt_u r23, r0, r23
  1555. /*
  1556. * Ensure that the TLB is loaded before we take out the lock.
  1557. * On tilepro, this will start fetching the value all the way
  1558. * into our L1 as well (and if it gets modified before we
  1559. * grab the lock, it will be invalidated from our cache
  1560. * before we reload it). On tile64, we'll start fetching it
  1561. * into our L1 if we're the home, and if we're not, we'll
  1562. * still at least start fetching it into the home's L2.
  1563. */
  1564. lw r26, r0
  1565. }
  1566. {
  1567. /* atomic_locks is page aligned so this suffices to get its addr. */
  1568. auli r21, zero, hi16(atomic_locks)
  1569. bbns r23, .Lcmpxchg_badaddr
  1570. }
  1571. {
  1572. /*
  1573. * Insert the hash bits into the page-aligned pointer.
  1574. * ATOMIC_HASH_SHIFT is so big that we don't actually hash
  1575. * the unmasked address bits, as that may cause unnecessary
  1576. * collisions.
  1577. */
  1578. mm ATOMIC_LOCK_REG_NAME, r25, r21, 2, (ATOMIC_HASH_SHIFT + 2) - 1
  1579. seqi r23, TREG_SYSCALL_NR_NAME, __NR_FAST_cmpxchg64
  1580. }
  1581. {
  1582. /* Branch away at this point if we're doing a 64-bit cmpxchg. */
  1583. bbs r23, .Lcmpxchg64
  1584. andi r23, r0, 7 /* Precompute alignment for cmpxchg64. */
  1585. }
  1586. {
  1587. /*
  1588. * We very carefully align the code that actually runs with
  1589. * the lock held (nine bundles) so that we know it is all in
  1590. * the icache when we start. This instruction (the jump) is
  1591. * at the start of the first cache line, address zero mod 64;
  1592. * we jump to somewhere in the second cache line to issue the
  1593. * tns, then jump back to finish up.
  1594. */
  1595. j .Lcmpxchg32_tns
  1596. }
  1597. #endif /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */
  1598. ENTRY(__sys_cmpxchg_grab_lock)
  1599. /*
  1600. * Perform the actual cmpxchg or atomic_update.
  1601. * Note that __futex_mark_unlocked() in uClibc relies on
  1602. * atomic_update() to always perform an "mf", so don't make
  1603. * it optional or conditional without modifying that code.
  1604. */
  1605. .Ldo_cmpxchg32:
  1606. {
  1607. lw r21, r0
  1608. seqi r23, TREG_SYSCALL_NR_NAME, __NR_FAST_atomic_update
  1609. move r24, r2
  1610. }
  1611. {
  1612. seq r22, r21, r1 /* See if cmpxchg matches. */
  1613. and r25, r21, r1 /* If atomic_update, compute (*mem & mask) */
  1614. }
  1615. {
  1616. or r22, r22, r23 /* Skip compare branch for atomic_update. */
  1617. add r25, r25, r2 /* Compute (*mem & mask) + addend. */
  1618. }
  1619. {
  1620. mvnz r24, r23, r25 /* Use atomic_update value if appropriate. */
  1621. bbns r22, .Lcmpxchg32_mismatch
  1622. }
  1623. sw r0, r24
  1624. /* Do slow mtspr here so the following "mf" waits less. */
  1625. {
  1626. move sp, r27
  1627. mtspr SPR_EX_CONTEXT_K_0, r28
  1628. }
  1629. mf
  1630. /* The following instruction is the start of the second cache line. */
  1631. {
  1632. move r0, r21
  1633. sw ATOMIC_LOCK_REG_NAME, zero
  1634. }
  1635. iret
  1636. /* Duplicated code here in the case where we don't overlap "mf" */
  1637. .Lcmpxchg32_mismatch:
  1638. {
  1639. move r0, r21
  1640. sw ATOMIC_LOCK_REG_NAME, zero
  1641. }
  1642. {
  1643. move sp, r27
  1644. mtspr SPR_EX_CONTEXT_K_0, r28
  1645. }
  1646. iret
  1647. /*
  1648. * The locking code is the same for 32-bit cmpxchg/atomic_update,
  1649. * and for 64-bit cmpxchg. We provide it as a macro and put
  1650. * it into both versions. We can't share the code literally
  1651. * since it depends on having the right branch-back address.
  1652. * Note that the first few instructions should share the cache
  1653. * line with the second half of the actual locked code.
  1654. */
  1655. .macro cmpxchg_lock, bitwidth
  1656. /* Lock; if we succeed, jump back up to the read-modify-write. */
  1657. #ifdef CONFIG_SMP
  1658. tns r21, ATOMIC_LOCK_REG_NAME
  1659. #else
  1660. /*
  1661. * Non-SMP preserves all the lock infrastructure, to keep the
  1662. * code simpler for the interesting (SMP) case. However, we do
  1663. * one small optimization here and in atomic_asm.S, which is
  1664. * to fake out acquiring the actual lock in the atomic_lock table.
  1665. */
  1666. movei r21, 0
  1667. #endif
  1668. /* Issue the slow SPR here while the tns result is in flight. */
  1669. mfspr r28, SPR_EX_CONTEXT_K_0
  1670. {
  1671. addi r28, r28, 8 /* return to the instruction after the swint1 */
  1672. bzt r21, .Ldo_cmpxchg\bitwidth
  1673. }
  1674. /*
  1675. * The preceding instruction is the last thing that must be
  1676. * on the second cache line.
  1677. */
  1678. #ifdef CONFIG_SMP
  1679. /*
  1680. * We failed to acquire the tns lock on our first try. Now use
  1681. * bounded exponential backoff to retry, like __atomic_spinlock().
  1682. */
  1683. {
  1684. moveli r23, 2048 /* maximum backoff time in cycles */
  1685. moveli r25, 32 /* starting backoff time in cycles */
  1686. }
  1687. 1: mfspr r26, CYCLE_LOW /* get start point for this backoff */
  1688. 2: mfspr r22, CYCLE_LOW /* test to see if we've backed off enough */
  1689. sub r22, r22, r26
  1690. slt r22, r22, r25
  1691. bbst r22, 2b
  1692. {
  1693. shli r25, r25, 1 /* double the backoff; retry the tns */
  1694. tns r21, ATOMIC_LOCK_REG_NAME
  1695. }
  1696. slt r26, r23, r25 /* is the proposed backoff too big? */
  1697. {
  1698. mvnz r25, r26, r23
  1699. bzt r21, .Ldo_cmpxchg\bitwidth
  1700. }
  1701. j 1b
  1702. #endif /* CONFIG_SMP */
  1703. .endm
  1704. .Lcmpxchg32_tns:
  1705. cmpxchg_lock 32
  1706. /*
  1707. * This code is invoked from sys_cmpxchg after most of the
  1708. * preconditions have been checked. We still need to check
  1709. * that r0 is 8-byte aligned, since if it's not we won't
  1710. * actually be atomic. However, ATOMIC_LOCK_REG has the atomic
  1711. * lock pointer and r27/r28 have the saved SP/PC.
  1712. * r23 is holding "r0 & 7" so we can test for alignment.
  1713. * The compare value is in r2/r3; the new value is in r4/r5.
  1714. * On return, we must put the old value in r0/r1.
  1715. */
  1716. .align 64
  1717. .Lcmpxchg64:
  1718. {
  1719. #if ATOMIC_LOCKS_FOUND_VIA_TABLE()
  1720. s2a ATOMIC_LOCK_REG_NAME, r25, r21
  1721. #endif
  1722. bzt r23, .Lcmpxchg64_tns
  1723. }
  1724. j .Lcmpxchg_badaddr
  1725. .Ldo_cmpxchg64:
  1726. {
  1727. lw r21, r0
  1728. addi r25, r0, 4
  1729. }
  1730. {
  1731. lw r1, r25
  1732. }
  1733. seq r26, r21, r2
  1734. {
  1735. bz r26, .Lcmpxchg64_mismatch
  1736. seq r26, r1, r3
  1737. }
  1738. {
  1739. bz r26, .Lcmpxchg64_mismatch
  1740. }
  1741. sw r0, r4
  1742. sw r25, r5
  1743. /*
  1744. * The 32-bit path provides optimized "match" and "mismatch"
  1745. * iret paths, but we don't have enough bundles in this cache line
  1746. * to do that, so we just make even the "mismatch" path do an "mf".
  1747. */
  1748. .Lcmpxchg64_mismatch:
  1749. {
  1750. move sp, r27
  1751. mtspr SPR_EX_CONTEXT_K_0, r28
  1752. }
  1753. mf
  1754. {
  1755. move r0, r21
  1756. sw ATOMIC_LOCK_REG_NAME, zero
  1757. }
  1758. iret
  1759. .Lcmpxchg64_tns:
  1760. cmpxchg_lock 64
  1761. /*
  1762. * Reset sp and revector to sys_cmpxchg_badaddr(), which will
  1763. * just raise the appropriate signal and exit. Doing it this
  1764. * way means we don't have to duplicate the code in intvec.S's
  1765. * int_hand macro that locates the top of the stack.
  1766. */
  1767. .Lcmpxchg_badaddr:
  1768. {
  1769. moveli TREG_SYSCALL_NR_NAME, __NR_cmpxchg_badaddr
  1770. move sp, r27
  1771. }
  1772. j intvec_SWINT_1
  1773. ENDPROC(sys_cmpxchg)
  1774. ENTRY(__sys_cmpxchg_end)
  1775. /* The single-step support may need to read all the registers. */
  1776. int_unalign:
  1777. push_extra_callee_saves r0
  1778. j do_trap
  1779. /* Include .intrpt1 array of interrupt vectors */
  1780. .section ".intrpt1", "ax"
  1781. #define op_handle_perf_interrupt bad_intr
  1782. #define op_handle_aux_perf_interrupt bad_intr
  1783. #ifndef CONFIG_HARDWALL
  1784. #define do_hardwall_trap bad_intr
  1785. #endif
  1786. int_hand INT_ITLB_MISS, ITLB_MISS, \
  1787. do_page_fault, handle_interrupt_no_single_step
  1788. int_hand INT_MEM_ERROR, MEM_ERROR, bad_intr
  1789. int_hand INT_ILL, ILL, do_trap, handle_ill
  1790. int_hand INT_GPV, GPV, do_trap
  1791. int_hand INT_SN_ACCESS, SN_ACCESS, do_trap
  1792. int_hand INT_IDN_ACCESS, IDN_ACCESS, do_trap
  1793. int_hand INT_UDN_ACCESS, UDN_ACCESS, do_trap
  1794. int_hand INT_IDN_REFILL, IDN_REFILL, bad_intr
  1795. int_hand INT_UDN_REFILL, UDN_REFILL, bad_intr
  1796. int_hand INT_IDN_COMPLETE, IDN_COMPLETE, bad_intr
  1797. int_hand INT_UDN_COMPLETE, UDN_COMPLETE, bad_intr
  1798. int_hand INT_SWINT_3, SWINT_3, do_trap
  1799. int_hand INT_SWINT_2, SWINT_2, do_trap
  1800. int_hand INT_SWINT_1, SWINT_1, SYSCALL, handle_syscall
  1801. int_hand INT_SWINT_0, SWINT_0, do_trap
  1802. int_hand INT_UNALIGN_DATA, UNALIGN_DATA, int_unalign
  1803. int_hand INT_DTLB_MISS, DTLB_MISS, do_page_fault
  1804. int_hand INT_DTLB_ACCESS, DTLB_ACCESS, do_page_fault
  1805. int_hand INT_DMATLB_MISS, DMATLB_MISS, do_page_fault
  1806. int_hand INT_DMATLB_ACCESS, DMATLB_ACCESS, do_page_fault
  1807. int_hand INT_SNITLB_MISS, SNITLB_MISS, do_page_fault
  1808. int_hand INT_SN_NOTIFY, SN_NOTIFY, bad_intr
  1809. int_hand INT_SN_FIREWALL, SN_FIREWALL, do_hardwall_trap
  1810. int_hand INT_IDN_FIREWALL, IDN_FIREWALL, bad_intr
  1811. int_hand INT_UDN_FIREWALL, UDN_FIREWALL, do_hardwall_trap
  1812. int_hand INT_TILE_TIMER, TILE_TIMER, do_timer_interrupt
  1813. int_hand INT_IDN_TIMER, IDN_TIMER, bad_intr
  1814. int_hand INT_UDN_TIMER, UDN_TIMER, bad_intr
  1815. int_hand INT_DMA_NOTIFY, DMA_NOTIFY, bad_intr
  1816. int_hand INT_IDN_CA, IDN_CA, bad_intr
  1817. int_hand INT_UDN_CA, UDN_CA, bad_intr
  1818. int_hand INT_IDN_AVAIL, IDN_AVAIL, bad_intr
  1819. int_hand INT_UDN_AVAIL, UDN_AVAIL, bad_intr
  1820. int_hand INT_PERF_COUNT, PERF_COUNT, \
  1821. op_handle_perf_interrupt, handle_nmi
  1822. int_hand INT_INTCTRL_3, INTCTRL_3, bad_intr
  1823. #if CONFIG_KERNEL_PL == 2
  1824. dc_dispatch INT_INTCTRL_2, INTCTRL_2
  1825. int_hand INT_INTCTRL_1, INTCTRL_1, bad_intr
  1826. #else
  1827. int_hand INT_INTCTRL_2, INTCTRL_2, bad_intr
  1828. dc_dispatch INT_INTCTRL_1, INTCTRL_1
  1829. #endif
  1830. int_hand INT_INTCTRL_0, INTCTRL_0, bad_intr
  1831. int_hand INT_MESSAGE_RCV_DWNCL, MESSAGE_RCV_DWNCL, \
  1832. hv_message_intr, handle_interrupt_downcall
  1833. int_hand INT_DEV_INTR_DWNCL, DEV_INTR_DWNCL, \
  1834. tile_dev_intr, handle_interrupt_downcall
  1835. int_hand INT_I_ASID, I_ASID, bad_intr
  1836. int_hand INT_D_ASID, D_ASID, bad_intr
  1837. int_hand INT_DMATLB_MISS_DWNCL, DMATLB_MISS_DWNCL, \
  1838. do_page_fault, handle_interrupt_downcall
  1839. int_hand INT_SNITLB_MISS_DWNCL, SNITLB_MISS_DWNCL, \
  1840. do_page_fault, handle_interrupt_downcall
  1841. int_hand INT_DMATLB_ACCESS_DWNCL, DMATLB_ACCESS_DWNCL, \
  1842. do_page_fault, handle_interrupt_downcall
  1843. int_hand INT_SN_CPL, SN_CPL, bad_intr
  1844. int_hand INT_DOUBLE_FAULT, DOUBLE_FAULT, do_trap
  1845. #if CHIP_HAS_AUX_PERF_COUNTERS()
  1846. int_hand INT_AUX_PERF_COUNT, AUX_PERF_COUNT, \
  1847. op_handle_aux_perf_interrupt, handle_nmi
  1848. #endif
  1849. /* Synthetic interrupt delivered only by the simulator */
  1850. int_hand INT_BREAKPOINT, BREAKPOINT, do_breakpoint