intvec_32.S 53 KB

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