intvec_32.S 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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_1 < 32 || INT_INTCTRL_1 >= 48
  33. # error INT_INTCTRL_1 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 SYSTEM_SAVE_1_1, r0
  121. mfspr r0, EX_CONTEXT_1_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_1_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_1_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_1_2 for
  161. * any path that turns into a downcall to one of our TLB handlers.
  162. */
  163. mfspr r0, SYSTEM_SAVE_1_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_1_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, SYSTEM_SAVE_1_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, EX_CONTEXT_1_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, EX_CONTEXT_1_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, SYSTEM_SAVE_1_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, SYSTEM_SAVE_1_3 /* address of page fault */
  292. mfspr r3, SYSTEM_SAVE_1_2 /* info about page fault */
  293. .else
  294. .ifc \vecnum, INT_DOUBLE_FAULT
  295. {
  296. mfspr r2, SYSTEM_SAVE_1_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, SYSTEM_SAVE_1_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, EX_CONTEXT_1_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 EX_CONTEXT_1_0, r21
  889. move r5, zero
  890. }
  891. {
  892. mtspr EX_CONTEXT_1_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, 1024
  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_1 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_1 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_1)
  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_1 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; .Lhandle_syscall_link:
  1228. FEEDBACK_REENTER(handle_syscall)
  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. /* Do syscall trace again, if requested. */
  1236. lw r30, r31
  1237. andi r30, r30, _TIF_SYSCALL_TRACE
  1238. bzt r30, 1f
  1239. jal do_syscall_trace
  1240. FEEDBACK_REENTER(handle_syscall)
  1241. 1: j .Lresume_userspace /* jump into middle of interrupt_return */
  1242. .Linvalid_syscall:
  1243. /* Report an invalid syscall back to the user program */
  1244. {
  1245. PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))
  1246. movei r28, -ENOSYS
  1247. }
  1248. sw r29, r28
  1249. j .Lresume_userspace /* jump into middle of interrupt_return */
  1250. STD_ENDPROC(handle_syscall)
  1251. /* Return the address for oprofile to suppress in backtraces. */
  1252. STD_ENTRY_SECTION(handle_syscall_link_address, .text.handle_syscall)
  1253. lnk r0
  1254. {
  1255. addli r0, r0, .Lhandle_syscall_link - .
  1256. jrp lr
  1257. }
  1258. STD_ENDPROC(handle_syscall_link_address)
  1259. STD_ENTRY(ret_from_fork)
  1260. jal sim_notify_fork
  1261. jal schedule_tail
  1262. FEEDBACK_REENTER(ret_from_fork)
  1263. j .Lresume_userspace /* jump into middle of interrupt_return */
  1264. STD_ENDPROC(ret_from_fork)
  1265. /*
  1266. * Code for ill interrupt.
  1267. */
  1268. .pushsection .text.handle_ill,"ax"
  1269. handle_ill:
  1270. finish_interrupt_save handle_ill
  1271. /*
  1272. * Check for if we are single stepping in user level. If so, then
  1273. * we need to restore the PC.
  1274. */
  1275. check_single_stepping ill, .Ldispatch_normal_ill
  1276. {
  1277. /* See if the PC is the 1st bundle in the buffer */
  1278. seq r25, r27, r26
  1279. /* Point to the 2nd bundle in the buffer */
  1280. addi r26, r26, 8
  1281. }
  1282. {
  1283. /* Point to the original pc */
  1284. addi r24, r29, SINGLESTEP_STATE_ORIG_PC_OFFSET
  1285. /* Branch if the PC is the 1st bundle in the buffer */
  1286. bnz r25, 3f
  1287. }
  1288. {
  1289. /* See if the PC is the 2nd bundle of the buffer */
  1290. seq r25, r27, r26
  1291. /* Set PC to next instruction */
  1292. addi r24, r29, SINGLESTEP_STATE_NEXT_PC_OFFSET
  1293. }
  1294. {
  1295. /* Point to flags */
  1296. addi r25, r29, SINGLESTEP_STATE_FLAGS_OFFSET
  1297. /* Branch if PC is in the second bundle */
  1298. bz r25, 2f
  1299. }
  1300. /* Load flags */
  1301. lw r25, r25
  1302. {
  1303. /*
  1304. * Get the offset for the register to restore
  1305. * Note: the lower bound is 2, so we have implicit scaling by 4.
  1306. * No multiplication of the register number by the size of a register
  1307. * is needed.
  1308. */
  1309. mm r27, r25, zero, SINGLESTEP_STATE_TARGET_LB, \
  1310. SINGLESTEP_STATE_TARGET_UB
  1311. /* Mask Rewrite_LR */
  1312. andi r25, r25, SINGLESTEP_STATE_MASK_UPDATE
  1313. }
  1314. {
  1315. addi r29, r29, SINGLESTEP_STATE_UPDATE_VALUE_OFFSET
  1316. /* Don't rewrite temp register */
  1317. bz r25, 3f
  1318. }
  1319. {
  1320. /* Get the temp value */
  1321. lw r29, r29
  1322. /* Point to where the register is stored */
  1323. add r27, r27, sp
  1324. }
  1325. /* Add in the C ABI save area size to the register offset */
  1326. addi r27, r27, C_ABI_SAVE_AREA_SIZE
  1327. /* Restore the user's register with the temp value */
  1328. sw r27, r29
  1329. j 3f
  1330. 2:
  1331. /* Must be in the third bundle */
  1332. addi r24, r29, SINGLESTEP_STATE_BRANCH_NEXT_PC_OFFSET
  1333. 3:
  1334. /* set PC and continue */
  1335. lw r26, r24
  1336. sw r28, r26
  1337. /* Clear TIF_SINGLESTEP */
  1338. GET_THREAD_INFO(r0)
  1339. addi r1, r0, THREAD_INFO_FLAGS_OFFSET
  1340. {
  1341. lw r2, r1
  1342. addi r0, r0, THREAD_INFO_TASK_OFFSET /* currently a no-op */
  1343. }
  1344. andi r2, r2, ~_TIF_SINGLESTEP
  1345. sw r1, r2
  1346. /* Issue a sigtrap */
  1347. {
  1348. lw r0, r0 /* indirect thru thread_info to get task_info*/
  1349. addi r1, sp, C_ABI_SAVE_AREA_SIZE /* put ptregs pointer into r1 */
  1350. move r2, zero /* load error code into r2 */
  1351. }
  1352. jal send_sigtrap /* issue a SIGTRAP */
  1353. FEEDBACK_REENTER(handle_ill)
  1354. j .Lresume_userspace /* jump into middle of interrupt_return */
  1355. .Ldispatch_normal_ill:
  1356. {
  1357. jalr r0
  1358. PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
  1359. }
  1360. FEEDBACK_REENTER(handle_ill)
  1361. {
  1362. movei r30, 0 /* not an NMI */
  1363. j interrupt_return
  1364. }
  1365. STD_ENDPROC(handle_ill)
  1366. /* Various stub interrupt handlers and syscall handlers */
  1367. STD_ENTRY_LOCAL(_kernel_double_fault)
  1368. mfspr r1, EX_CONTEXT_1_0
  1369. move r2, lr
  1370. move r3, sp
  1371. move r4, r52
  1372. addi sp, sp, -C_ABI_SAVE_AREA_SIZE
  1373. j kernel_double_fault
  1374. STD_ENDPROC(_kernel_double_fault)
  1375. STD_ENTRY_LOCAL(bad_intr)
  1376. mfspr r2, EX_CONTEXT_1_0
  1377. panic "Unhandled interrupt %#x: PC %#lx"
  1378. STD_ENDPROC(bad_intr)
  1379. /* Put address of pt_regs in reg and jump. */
  1380. #define PTREGS_SYSCALL(x, reg) \
  1381. STD_ENTRY(x); \
  1382. { \
  1383. PTREGS_PTR(reg, PTREGS_OFFSET_BASE); \
  1384. j _##x \
  1385. }; \
  1386. STD_ENDPROC(x)
  1387. PTREGS_SYSCALL(sys_execve, r3)
  1388. PTREGS_SYSCALL(sys_sigaltstack, r2)
  1389. PTREGS_SYSCALL(sys_rt_sigreturn, r0)
  1390. /* Save additional callee-saves to pt_regs, put address in reg and jump. */
  1391. #define PTREGS_SYSCALL_ALL_REGS(x, reg) \
  1392. STD_ENTRY(x); \
  1393. push_extra_callee_saves reg; \
  1394. j _##x; \
  1395. STD_ENDPROC(x)
  1396. PTREGS_SYSCALL_ALL_REGS(sys_fork, r0)
  1397. PTREGS_SYSCALL_ALL_REGS(sys_vfork, r0)
  1398. PTREGS_SYSCALL_ALL_REGS(sys_clone, r4)
  1399. PTREGS_SYSCALL_ALL_REGS(sys_cmpxchg_badaddr, r1)
  1400. /*
  1401. * This entrypoint is taken for the cmpxchg and atomic_update fast
  1402. * swints. We may wish to generalize it to other fast swints at some
  1403. * point, but for now there are just two very similar ones, which
  1404. * makes it faster.
  1405. *
  1406. * The fast swint code is designed to have a small footprint. It does
  1407. * not save or restore any GPRs, counting on the caller-save registers
  1408. * to be available to it on entry. It does not modify any callee-save
  1409. * registers (including "lr"). It does not check what PL it is being
  1410. * called at, so you'd better not call it other than at PL0.
  1411. *
  1412. * It does not use the stack, but since it might be re-interrupted by
  1413. * a page fault which would assume the stack was valid, it does
  1414. * save/restore the stack pointer and zero it out to make sure it gets reset.
  1415. * Since we always keep interrupts disabled, the hypervisor won't
  1416. * clobber our EX_CONTEXT_1_x registers, so we don't save/restore them
  1417. * (other than to advance the PC on return).
  1418. *
  1419. * We have to manually validate the user vs kernel address range
  1420. * (since at PL1 we can read/write both), and for performance reasons
  1421. * we don't allow cmpxchg on the fc000000 memory region, since we only
  1422. * validate that the user address is below PAGE_OFFSET.
  1423. *
  1424. * We place it in the __HEAD section to ensure it is relatively
  1425. * near to the intvec_SWINT_1 code (reachable by a conditional branch).
  1426. *
  1427. * Must match register usage in do_page_fault().
  1428. */
  1429. __HEAD
  1430. .align 64
  1431. /* Align much later jump on the start of a cache line. */
  1432. #if !ATOMIC_LOCKS_FOUND_VIA_TABLE()
  1433. nop; nop
  1434. #endif
  1435. ENTRY(sys_cmpxchg)
  1436. /*
  1437. * Save "sp" and set it zero for any possible page fault.
  1438. *
  1439. * HACK: We want to both zero sp and check r0's alignment,
  1440. * so we do both at once. If "sp" becomes nonzero we
  1441. * know r0 is unaligned and branch to the error handler that
  1442. * restores sp, so this is OK.
  1443. *
  1444. * ICS is disabled right now so having a garbage but nonzero
  1445. * sp is OK, since we won't execute any faulting instructions
  1446. * when it is nonzero.
  1447. */
  1448. {
  1449. move r27, sp
  1450. andi sp, r0, 3
  1451. }
  1452. /*
  1453. * Get the lock address in ATOMIC_LOCK_REG, and also validate that the
  1454. * address is less than PAGE_OFFSET, since that won't trap at PL1.
  1455. * We only use bits less than PAGE_SHIFT to avoid having to worry
  1456. * about aliasing among multiple mappings of the same physical page,
  1457. * and we ignore the low 3 bits so we have one lock that covers
  1458. * both a cmpxchg64() and a cmpxchg() on either its low or high word.
  1459. * NOTE: this code must match __atomic_hashed_lock() in lib/atomic.c.
  1460. */
  1461. #if ATOMIC_LOCKS_FOUND_VIA_TABLE()
  1462. {
  1463. /* Check for unaligned input. */
  1464. bnz sp, .Lcmpxchg_badaddr
  1465. mm r25, r0, zero, 3, PAGE_SHIFT-1
  1466. }
  1467. {
  1468. crc32_32 r25, zero, r25
  1469. moveli r21, lo16(atomic_lock_ptr)
  1470. }
  1471. {
  1472. auli r21, r21, ha16(atomic_lock_ptr)
  1473. auli r23, zero, hi16(PAGE_OFFSET) /* hugepage-aligned */
  1474. }
  1475. {
  1476. shri r20, r25, 32 - ATOMIC_HASH_L1_SHIFT
  1477. slt_u r23, r0, r23
  1478. /*
  1479. * Ensure that the TLB is loaded before we take out the lock.
  1480. * On TILEPro, this will start fetching the value all the way
  1481. * into our L1 as well (and if it gets modified before we
  1482. * grab the lock, it will be invalidated from our cache
  1483. * before we reload it). On tile64, we'll start fetching it
  1484. * into our L1 if we're the home, and if we're not, we'll
  1485. * still at least start fetching it into the home's L2.
  1486. */
  1487. lw r26, r0
  1488. }
  1489. {
  1490. s2a r21, r20, r21
  1491. bbns r23, .Lcmpxchg_badaddr
  1492. }
  1493. {
  1494. lw r21, r21
  1495. seqi r23, TREG_SYSCALL_NR_NAME, __NR_FAST_cmpxchg64
  1496. andi r25, r25, ATOMIC_HASH_L2_SIZE - 1
  1497. }
  1498. {
  1499. /* Branch away at this point if we're doing a 64-bit cmpxchg. */
  1500. bbs r23, .Lcmpxchg64
  1501. andi r23, r0, 7 /* Precompute alignment for cmpxchg64. */
  1502. }
  1503. {
  1504. /*
  1505. * We very carefully align the code that actually runs with
  1506. * the lock held (nine bundles) so that we know it is all in
  1507. * the icache when we start. This instruction (the jump) is
  1508. * at the start of the first cache line, address zero mod 64;
  1509. * we jump to somewhere in the second cache line to issue the
  1510. * tns, then jump back to finish up.
  1511. */
  1512. s2a ATOMIC_LOCK_REG_NAME, r25, r21
  1513. j .Lcmpxchg32_tns
  1514. }
  1515. #else /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */
  1516. {
  1517. /* Check for unaligned input. */
  1518. bnz sp, .Lcmpxchg_badaddr
  1519. auli r23, zero, hi16(PAGE_OFFSET) /* hugepage-aligned */
  1520. }
  1521. {
  1522. /*
  1523. * Slide bits into position for 'mm'. We want to ignore
  1524. * the low 3 bits of r0, and consider only the next
  1525. * ATOMIC_HASH_SHIFT bits.
  1526. * Because of C pointer arithmetic, we want to compute this:
  1527. *
  1528. * ((char*)atomic_locks +
  1529. * (((r0 >> 3) & (1 << (ATOMIC_HASH_SIZE - 1))) << 2))
  1530. *
  1531. * Instead of two shifts we just ">> 1", and use 'mm'
  1532. * to ignore the low and high bits we don't want.
  1533. */
  1534. shri r25, r0, 1
  1535. slt_u r23, r0, r23
  1536. /*
  1537. * Ensure that the TLB is loaded before we take out the lock.
  1538. * On tilepro, this will start fetching the value all the way
  1539. * into our L1 as well (and if it gets modified before we
  1540. * grab the lock, it will be invalidated from our cache
  1541. * before we reload it). On tile64, we'll start fetching it
  1542. * into our L1 if we're the home, and if we're not, we'll
  1543. * still at least start fetching it into the home's L2.
  1544. */
  1545. lw r26, r0
  1546. }
  1547. {
  1548. /* atomic_locks is page aligned so this suffices to get its addr. */
  1549. auli r21, zero, hi16(atomic_locks)
  1550. bbns r23, .Lcmpxchg_badaddr
  1551. }
  1552. {
  1553. /*
  1554. * Insert the hash bits into the page-aligned pointer.
  1555. * ATOMIC_HASH_SHIFT is so big that we don't actually hash
  1556. * the unmasked address bits, as that may cause unnecessary
  1557. * collisions.
  1558. */
  1559. mm ATOMIC_LOCK_REG_NAME, r25, r21, 2, (ATOMIC_HASH_SHIFT + 2) - 1
  1560. seqi r23, TREG_SYSCALL_NR_NAME, __NR_FAST_cmpxchg64
  1561. }
  1562. {
  1563. /* Branch away at this point if we're doing a 64-bit cmpxchg. */
  1564. bbs r23, .Lcmpxchg64
  1565. andi r23, r0, 7 /* Precompute alignment for cmpxchg64. */
  1566. }
  1567. {
  1568. /*
  1569. * We very carefully align the code that actually runs with
  1570. * the lock held (nine bundles) so that we know it is all in
  1571. * the icache when we start. This instruction (the jump) is
  1572. * at the start of the first cache line, address zero mod 64;
  1573. * we jump to somewhere in the second cache line to issue the
  1574. * tns, then jump back to finish up.
  1575. */
  1576. j .Lcmpxchg32_tns
  1577. }
  1578. #endif /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */
  1579. ENTRY(__sys_cmpxchg_grab_lock)
  1580. /*
  1581. * Perform the actual cmpxchg or atomic_update.
  1582. * Note that __futex_mark_unlocked() in uClibc relies on
  1583. * atomic_update() to always perform an "mf", so don't make
  1584. * it optional or conditional without modifying that code.
  1585. */
  1586. .Ldo_cmpxchg32:
  1587. {
  1588. lw r21, r0
  1589. seqi r23, TREG_SYSCALL_NR_NAME, __NR_FAST_atomic_update
  1590. move r24, r2
  1591. }
  1592. {
  1593. seq r22, r21, r1 /* See if cmpxchg matches. */
  1594. and r25, r21, r1 /* If atomic_update, compute (*mem & mask) */
  1595. }
  1596. {
  1597. or r22, r22, r23 /* Skip compare branch for atomic_update. */
  1598. add r25, r25, r2 /* Compute (*mem & mask) + addend. */
  1599. }
  1600. {
  1601. mvnz r24, r23, r25 /* Use atomic_update value if appropriate. */
  1602. bbns r22, .Lcmpxchg32_mismatch
  1603. }
  1604. sw r0, r24
  1605. /* Do slow mtspr here so the following "mf" waits less. */
  1606. {
  1607. move sp, r27
  1608. mtspr EX_CONTEXT_1_0, r28
  1609. }
  1610. mf
  1611. /* The following instruction is the start of the second cache line. */
  1612. {
  1613. move r0, r21
  1614. sw ATOMIC_LOCK_REG_NAME, zero
  1615. }
  1616. iret
  1617. /* Duplicated code here in the case where we don't overlap "mf" */
  1618. .Lcmpxchg32_mismatch:
  1619. {
  1620. move r0, r21
  1621. sw ATOMIC_LOCK_REG_NAME, zero
  1622. }
  1623. {
  1624. move sp, r27
  1625. mtspr EX_CONTEXT_1_0, r28
  1626. }
  1627. iret
  1628. /*
  1629. * The locking code is the same for 32-bit cmpxchg/atomic_update,
  1630. * and for 64-bit cmpxchg. We provide it as a macro and put
  1631. * it into both versions. We can't share the code literally
  1632. * since it depends on having the right branch-back address.
  1633. * Note that the first few instructions should share the cache
  1634. * line with the second half of the actual locked code.
  1635. */
  1636. .macro cmpxchg_lock, bitwidth
  1637. /* Lock; if we succeed, jump back up to the read-modify-write. */
  1638. #ifdef CONFIG_SMP
  1639. tns r21, ATOMIC_LOCK_REG_NAME
  1640. #else
  1641. /*
  1642. * Non-SMP preserves all the lock infrastructure, to keep the
  1643. * code simpler for the interesting (SMP) case. However, we do
  1644. * one small optimization here and in atomic_asm.S, which is
  1645. * to fake out acquiring the actual lock in the atomic_lock table.
  1646. */
  1647. movei r21, 0
  1648. #endif
  1649. /* Issue the slow SPR here while the tns result is in flight. */
  1650. mfspr r28, EX_CONTEXT_1_0
  1651. {
  1652. addi r28, r28, 8 /* return to the instruction after the swint1 */
  1653. bzt r21, .Ldo_cmpxchg\bitwidth
  1654. }
  1655. /*
  1656. * The preceding instruction is the last thing that must be
  1657. * on the second cache line.
  1658. */
  1659. #ifdef CONFIG_SMP
  1660. /*
  1661. * We failed to acquire the tns lock on our first try. Now use
  1662. * bounded exponential backoff to retry, like __atomic_spinlock().
  1663. */
  1664. {
  1665. moveli r23, 2048 /* maximum backoff time in cycles */
  1666. moveli r25, 32 /* starting backoff time in cycles */
  1667. }
  1668. 1: mfspr r26, CYCLE_LOW /* get start point for this backoff */
  1669. 2: mfspr r22, CYCLE_LOW /* test to see if we've backed off enough */
  1670. sub r22, r22, r26
  1671. slt r22, r22, r25
  1672. bbst r22, 2b
  1673. {
  1674. shli r25, r25, 1 /* double the backoff; retry the tns */
  1675. tns r21, ATOMIC_LOCK_REG_NAME
  1676. }
  1677. slt r26, r23, r25 /* is the proposed backoff too big? */
  1678. {
  1679. mvnz r25, r26, r23
  1680. bzt r21, .Ldo_cmpxchg\bitwidth
  1681. }
  1682. j 1b
  1683. #endif /* CONFIG_SMP */
  1684. .endm
  1685. .Lcmpxchg32_tns:
  1686. cmpxchg_lock 32
  1687. /*
  1688. * This code is invoked from sys_cmpxchg after most of the
  1689. * preconditions have been checked. We still need to check
  1690. * that r0 is 8-byte aligned, since if it's not we won't
  1691. * actually be atomic. However, ATOMIC_LOCK_REG has the atomic
  1692. * lock pointer and r27/r28 have the saved SP/PC.
  1693. * r23 is holding "r0 & 7" so we can test for alignment.
  1694. * The compare value is in r2/r3; the new value is in r4/r5.
  1695. * On return, we must put the old value in r0/r1.
  1696. */
  1697. .align 64
  1698. .Lcmpxchg64:
  1699. {
  1700. #if ATOMIC_LOCKS_FOUND_VIA_TABLE()
  1701. s2a ATOMIC_LOCK_REG_NAME, r25, r21
  1702. #endif
  1703. bzt r23, .Lcmpxchg64_tns
  1704. }
  1705. j .Lcmpxchg_badaddr
  1706. .Ldo_cmpxchg64:
  1707. {
  1708. lw r21, r0
  1709. addi r25, r0, 4
  1710. }
  1711. {
  1712. lw r1, r25
  1713. }
  1714. seq r26, r21, r2
  1715. {
  1716. bz r26, .Lcmpxchg64_mismatch
  1717. seq r26, r1, r3
  1718. }
  1719. {
  1720. bz r26, .Lcmpxchg64_mismatch
  1721. }
  1722. sw r0, r4
  1723. sw r25, r5
  1724. /*
  1725. * The 32-bit path provides optimized "match" and "mismatch"
  1726. * iret paths, but we don't have enough bundles in this cache line
  1727. * to do that, so we just make even the "mismatch" path do an "mf".
  1728. */
  1729. .Lcmpxchg64_mismatch:
  1730. {
  1731. move sp, r27
  1732. mtspr EX_CONTEXT_1_0, r28
  1733. }
  1734. mf
  1735. {
  1736. move r0, r21
  1737. sw ATOMIC_LOCK_REG_NAME, zero
  1738. }
  1739. iret
  1740. .Lcmpxchg64_tns:
  1741. cmpxchg_lock 64
  1742. /*
  1743. * Reset sp and revector to sys_cmpxchg_badaddr(), which will
  1744. * just raise the appropriate signal and exit. Doing it this
  1745. * way means we don't have to duplicate the code in intvec.S's
  1746. * int_hand macro that locates the top of the stack.
  1747. */
  1748. .Lcmpxchg_badaddr:
  1749. {
  1750. moveli TREG_SYSCALL_NR_NAME, __NR_cmpxchg_badaddr
  1751. move sp, r27
  1752. }
  1753. j intvec_SWINT_1
  1754. ENDPROC(sys_cmpxchg)
  1755. ENTRY(__sys_cmpxchg_end)
  1756. /* The single-step support may need to read all the registers. */
  1757. int_unalign:
  1758. push_extra_callee_saves r0
  1759. j do_trap
  1760. /* Include .intrpt1 array of interrupt vectors */
  1761. .section ".intrpt1", "ax"
  1762. #define op_handle_perf_interrupt bad_intr
  1763. #define op_handle_aux_perf_interrupt bad_intr
  1764. #ifndef CONFIG_HARDWALL
  1765. #define do_hardwall_trap bad_intr
  1766. #endif
  1767. int_hand INT_ITLB_MISS, ITLB_MISS, \
  1768. do_page_fault, handle_interrupt_no_single_step
  1769. int_hand INT_MEM_ERROR, MEM_ERROR, bad_intr
  1770. int_hand INT_ILL, ILL, do_trap, handle_ill
  1771. int_hand INT_GPV, GPV, do_trap
  1772. int_hand INT_SN_ACCESS, SN_ACCESS, do_trap
  1773. int_hand INT_IDN_ACCESS, IDN_ACCESS, do_trap
  1774. int_hand INT_UDN_ACCESS, UDN_ACCESS, do_trap
  1775. int_hand INT_IDN_REFILL, IDN_REFILL, bad_intr
  1776. int_hand INT_UDN_REFILL, UDN_REFILL, bad_intr
  1777. int_hand INT_IDN_COMPLETE, IDN_COMPLETE, bad_intr
  1778. int_hand INT_UDN_COMPLETE, UDN_COMPLETE, bad_intr
  1779. int_hand INT_SWINT_3, SWINT_3, do_trap
  1780. int_hand INT_SWINT_2, SWINT_2, do_trap
  1781. int_hand INT_SWINT_1, SWINT_1, SYSCALL, handle_syscall
  1782. int_hand INT_SWINT_0, SWINT_0, do_trap
  1783. int_hand INT_UNALIGN_DATA, UNALIGN_DATA, int_unalign
  1784. int_hand INT_DTLB_MISS, DTLB_MISS, do_page_fault
  1785. int_hand INT_DTLB_ACCESS, DTLB_ACCESS, do_page_fault
  1786. int_hand INT_DMATLB_MISS, DMATLB_MISS, do_page_fault
  1787. int_hand INT_DMATLB_ACCESS, DMATLB_ACCESS, do_page_fault
  1788. int_hand INT_SNITLB_MISS, SNITLB_MISS, do_page_fault
  1789. int_hand INT_SN_NOTIFY, SN_NOTIFY, bad_intr
  1790. int_hand INT_SN_FIREWALL, SN_FIREWALL, do_hardwall_trap
  1791. int_hand INT_IDN_FIREWALL, IDN_FIREWALL, bad_intr
  1792. int_hand INT_UDN_FIREWALL, UDN_FIREWALL, do_hardwall_trap
  1793. int_hand INT_TILE_TIMER, TILE_TIMER, do_timer_interrupt
  1794. int_hand INT_IDN_TIMER, IDN_TIMER, bad_intr
  1795. int_hand INT_UDN_TIMER, UDN_TIMER, bad_intr
  1796. int_hand INT_DMA_NOTIFY, DMA_NOTIFY, bad_intr
  1797. int_hand INT_IDN_CA, IDN_CA, bad_intr
  1798. int_hand INT_UDN_CA, UDN_CA, bad_intr
  1799. int_hand INT_IDN_AVAIL, IDN_AVAIL, bad_intr
  1800. int_hand INT_UDN_AVAIL, UDN_AVAIL, bad_intr
  1801. int_hand INT_PERF_COUNT, PERF_COUNT, \
  1802. op_handle_perf_interrupt, handle_nmi
  1803. int_hand INT_INTCTRL_3, INTCTRL_3, bad_intr
  1804. int_hand INT_INTCTRL_2, INTCTRL_2, bad_intr
  1805. dc_dispatch INT_INTCTRL_1, INTCTRL_1
  1806. int_hand INT_INTCTRL_0, INTCTRL_0, bad_intr
  1807. int_hand INT_MESSAGE_RCV_DWNCL, MESSAGE_RCV_DWNCL, \
  1808. hv_message_intr, handle_interrupt_downcall
  1809. int_hand INT_DEV_INTR_DWNCL, DEV_INTR_DWNCL, \
  1810. tile_dev_intr, handle_interrupt_downcall
  1811. int_hand INT_I_ASID, I_ASID, bad_intr
  1812. int_hand INT_D_ASID, D_ASID, bad_intr
  1813. int_hand INT_DMATLB_MISS_DWNCL, DMATLB_MISS_DWNCL, \
  1814. do_page_fault, handle_interrupt_downcall
  1815. int_hand INT_SNITLB_MISS_DWNCL, SNITLB_MISS_DWNCL, \
  1816. do_page_fault, handle_interrupt_downcall
  1817. int_hand INT_DMATLB_ACCESS_DWNCL, DMATLB_ACCESS_DWNCL, \
  1818. do_page_fault, handle_interrupt_downcall
  1819. int_hand INT_SN_CPL, SN_CPL, bad_intr
  1820. int_hand INT_DOUBLE_FAULT, DOUBLE_FAULT, do_trap
  1821. #if CHIP_HAS_AUX_PERF_COUNTERS()
  1822. int_hand INT_AUX_PERF_COUNT, AUX_PERF_COUNT, \
  1823. op_handle_aux_perf_interrupt, handle_nmi
  1824. #endif
  1825. /* Synthetic interrupt delivered only by the simulator */
  1826. int_hand INT_BREAKPOINT, BREAKPOINT, do_breakpoint