entry_64.S 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
  5. * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
  6. * Adapted for Power Macintosh by Paul Mackerras.
  7. * Low-level exception handlers and MMU support
  8. * rewritten by Paul Mackerras.
  9. * Copyright (C) 1996 Paul Mackerras.
  10. * MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
  11. *
  12. * This file contains the system call entry code, context switch
  13. * code, and exception/interrupt return code for PowerPC.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/magic.h>
  23. #include <asm/unistd.h>
  24. #include <asm/processor.h>
  25. #include <asm/page.h>
  26. #include <asm/mmu.h>
  27. #include <asm/thread_info.h>
  28. #include <asm/ppc_asm.h>
  29. #include <asm/asm-offsets.h>
  30. #include <asm/cputable.h>
  31. #include <asm/firmware.h>
  32. #include <asm/bug.h>
  33. #include <asm/ptrace.h>
  34. #include <asm/irqflags.h>
  35. #include <asm/ftrace.h>
  36. #include <asm/hw_irq.h>
  37. #include <asm/context_tracking.h>
  38. #include <asm/tm.h>
  39. #include <asm/ppc-opcode.h>
  40. #include <asm/export.h>
  41. #ifdef CONFIG_PPC_BOOK3S
  42. #include <asm/exception-64s.h>
  43. #else
  44. #include <asm/exception-64e.h>
  45. #endif
  46. /*
  47. * System calls.
  48. */
  49. .section ".toc","aw"
  50. SYS_CALL_TABLE:
  51. .tc sys_call_table[TC],sys_call_table
  52. /* This value is used to mark exception frames on the stack. */
  53. exception_marker:
  54. .tc ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
  55. .section ".text"
  56. .align 7
  57. .globl system_call_common
  58. system_call_common:
  59. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  60. BEGIN_FTR_SECTION
  61. extrdi. r10, r12, 1, (63-MSR_TS_T_LG) /* transaction active? */
  62. bne tabort_syscall
  63. END_FTR_SECTION_IFSET(CPU_FTR_TM)
  64. #endif
  65. andi. r10,r12,MSR_PR
  66. mr r10,r1
  67. addi r1,r1,-INT_FRAME_SIZE
  68. beq- 1f
  69. ld r1,PACAKSAVE(r13)
  70. 1: std r10,0(r1)
  71. std r11,_NIP(r1)
  72. std r12,_MSR(r1)
  73. std r0,GPR0(r1)
  74. std r10,GPR1(r1)
  75. beq 2f /* if from kernel mode */
  76. ACCOUNT_CPU_USER_ENTRY(r13, r10, r11)
  77. 2: std r2,GPR2(r1)
  78. std r3,GPR3(r1)
  79. mfcr r2
  80. std r4,GPR4(r1)
  81. std r5,GPR5(r1)
  82. std r6,GPR6(r1)
  83. std r7,GPR7(r1)
  84. std r8,GPR8(r1)
  85. li r11,0
  86. std r11,GPR9(r1)
  87. std r11,GPR10(r1)
  88. std r11,GPR11(r1)
  89. std r11,GPR12(r1)
  90. std r11,_XER(r1)
  91. std r11,_CTR(r1)
  92. std r9,GPR13(r1)
  93. mflr r10
  94. /*
  95. * This clears CR0.SO (bit 28), which is the error indication on
  96. * return from this system call.
  97. */
  98. rldimi r2,r11,28,(63-28)
  99. li r11,0xc01
  100. std r10,_LINK(r1)
  101. std r11,_TRAP(r1)
  102. std r3,ORIG_GPR3(r1)
  103. std r2,_CCR(r1)
  104. ld r2,PACATOC(r13)
  105. addi r9,r1,STACK_FRAME_OVERHEAD
  106. ld r11,exception_marker@toc(r2)
  107. std r11,-16(r9) /* "regshere" marker */
  108. #if defined(CONFIG_VIRT_CPU_ACCOUNTING_NATIVE) && defined(CONFIG_PPC_SPLPAR)
  109. BEGIN_FW_FTR_SECTION
  110. beq 33f
  111. /* if from user, see if there are any DTL entries to process */
  112. ld r10,PACALPPACAPTR(r13) /* get ptr to VPA */
  113. ld r11,PACA_DTL_RIDX(r13) /* get log read index */
  114. addi r10,r10,LPPACA_DTLIDX
  115. LDX_BE r10,0,r10 /* get log write index */
  116. cmpd cr1,r11,r10
  117. beq+ cr1,33f
  118. bl accumulate_stolen_time
  119. REST_GPR(0,r1)
  120. REST_4GPRS(3,r1)
  121. REST_2GPRS(7,r1)
  122. addi r9,r1,STACK_FRAME_OVERHEAD
  123. 33:
  124. END_FW_FTR_SECTION_IFSET(FW_FEATURE_SPLPAR)
  125. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE && CONFIG_PPC_SPLPAR */
  126. /*
  127. * A syscall should always be called with interrupts enabled
  128. * so we just unconditionally hard-enable here. When some kind
  129. * of irq tracing is used, we additionally check that condition
  130. * is correct
  131. */
  132. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_BUG)
  133. lbz r10,PACASOFTIRQEN(r13)
  134. xori r10,r10,1
  135. 1: tdnei r10,0
  136. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
  137. #endif
  138. #ifdef CONFIG_PPC_BOOK3E
  139. wrteei 1
  140. #else
  141. li r11,MSR_RI
  142. ori r11,r11,MSR_EE
  143. mtmsrd r11,1
  144. #endif /* CONFIG_PPC_BOOK3E */
  145. /* We do need to set SOFTE in the stack frame or the return
  146. * from interrupt will be painful
  147. */
  148. li r10,1
  149. std r10,SOFTE(r1)
  150. CURRENT_THREAD_INFO(r11, r1)
  151. ld r10,TI_FLAGS(r11)
  152. andi. r11,r10,_TIF_SYSCALL_DOTRACE
  153. bne syscall_dotrace /* does not return */
  154. cmpldi 0,r0,NR_syscalls
  155. bge- syscall_enosys
  156. system_call: /* label this so stack traces look sane */
  157. /*
  158. * Need to vector to 32 Bit or default sys_call_table here,
  159. * based on caller's run-mode / personality.
  160. */
  161. ld r11,SYS_CALL_TABLE@toc(2)
  162. andi. r10,r10,_TIF_32BIT
  163. beq 15f
  164. addi r11,r11,8 /* use 32-bit syscall entries */
  165. clrldi r3,r3,32
  166. clrldi r4,r4,32
  167. clrldi r5,r5,32
  168. clrldi r6,r6,32
  169. clrldi r7,r7,32
  170. clrldi r8,r8,32
  171. 15:
  172. slwi r0,r0,4
  173. ldx r12,r11,r0 /* Fetch system call handler [ptr] */
  174. mtctr r12
  175. bctrl /* Call handler */
  176. .Lsyscall_exit:
  177. std r3,RESULT(r1)
  178. CURRENT_THREAD_INFO(r12, r1)
  179. ld r8,_MSR(r1)
  180. #ifdef CONFIG_PPC_BOOK3S
  181. /* No MSR:RI on BookE */
  182. andi. r10,r8,MSR_RI
  183. beq- unrecov_restore
  184. #endif
  185. /*
  186. * Disable interrupts so current_thread_info()->flags can't change,
  187. * and so that we don't get interrupted after loading SRR0/1.
  188. */
  189. #ifdef CONFIG_PPC_BOOK3E
  190. wrteei 0
  191. #else
  192. /*
  193. * For performance reasons we clear RI the same time that we
  194. * clear EE. We only need to clear RI just before we restore r13
  195. * below, but batching it with EE saves us one expensive mtmsrd call.
  196. * We have to be careful to restore RI if we branch anywhere from
  197. * here (eg syscall_exit_work).
  198. */
  199. li r11,0
  200. mtmsrd r11,1
  201. #endif /* CONFIG_PPC_BOOK3E */
  202. ld r9,TI_FLAGS(r12)
  203. li r11,-MAX_ERRNO
  204. andi. r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
  205. bne- syscall_exit_work
  206. andi. r0,r8,MSR_FP
  207. beq 2f
  208. #ifdef CONFIG_ALTIVEC
  209. andis. r0,r8,MSR_VEC@h
  210. bne 3f
  211. #endif
  212. 2: addi r3,r1,STACK_FRAME_OVERHEAD
  213. #ifdef CONFIG_PPC_BOOK3S
  214. li r10,MSR_RI
  215. mtmsrd r10,1 /* Restore RI */
  216. #endif
  217. bl restore_math
  218. #ifdef CONFIG_PPC_BOOK3S
  219. li r11,0
  220. mtmsrd r11,1
  221. #endif
  222. ld r8,_MSR(r1)
  223. ld r3,RESULT(r1)
  224. li r11,-MAX_ERRNO
  225. 3: cmpld r3,r11
  226. ld r5,_CCR(r1)
  227. bge- syscall_error
  228. .Lsyscall_error_cont:
  229. ld r7,_NIP(r1)
  230. BEGIN_FTR_SECTION
  231. stdcx. r0,0,r1 /* to clear the reservation */
  232. END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  233. andi. r6,r8,MSR_PR
  234. ld r4,_LINK(r1)
  235. beq- 1f
  236. ACCOUNT_CPU_USER_EXIT(r13, r11, r12)
  237. BEGIN_FTR_SECTION
  238. HMT_MEDIUM_LOW
  239. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  240. ld r13,GPR13(r1) /* only restore r13 if returning to usermode */
  241. ld r2,GPR2(r1)
  242. ld r1,GPR1(r1)
  243. mtlr r4
  244. mtcr r5
  245. mtspr SPRN_SRR0,r7
  246. mtspr SPRN_SRR1,r8
  247. RFI_TO_USER
  248. b . /* prevent speculative execution */
  249. /* exit to kernel */
  250. 1: ld r2,GPR2(r1)
  251. ld r1,GPR1(r1)
  252. mtlr r4
  253. mtcr r5
  254. mtspr SPRN_SRR0,r7
  255. mtspr SPRN_SRR1,r8
  256. RFI_TO_KERNEL
  257. b . /* prevent speculative execution */
  258. syscall_error:
  259. oris r5,r5,0x1000 /* Set SO bit in CR */
  260. neg r3,r3
  261. std r5,_CCR(r1)
  262. b .Lsyscall_error_cont
  263. /* Traced system call support */
  264. syscall_dotrace:
  265. bl save_nvgprs
  266. addi r3,r1,STACK_FRAME_OVERHEAD
  267. bl do_syscall_trace_enter
  268. /*
  269. * We use the return value of do_syscall_trace_enter() as the syscall
  270. * number. If the syscall was rejected for any reason do_syscall_trace_enter()
  271. * returns an invalid syscall number and the test below against
  272. * NR_syscalls will fail.
  273. */
  274. mr r0,r3
  275. /* Restore argument registers just clobbered and/or possibly changed. */
  276. ld r3,GPR3(r1)
  277. ld r4,GPR4(r1)
  278. ld r5,GPR5(r1)
  279. ld r6,GPR6(r1)
  280. ld r7,GPR7(r1)
  281. ld r8,GPR8(r1)
  282. /* Repopulate r9 and r10 for the system_call path */
  283. addi r9,r1,STACK_FRAME_OVERHEAD
  284. CURRENT_THREAD_INFO(r10, r1)
  285. ld r10,TI_FLAGS(r10)
  286. cmpldi r0,NR_syscalls
  287. blt+ system_call
  288. /* Return code is already in r3 thanks to do_syscall_trace_enter() */
  289. b .Lsyscall_exit
  290. syscall_enosys:
  291. li r3,-ENOSYS
  292. b .Lsyscall_exit
  293. syscall_exit_work:
  294. #ifdef CONFIG_PPC_BOOK3S
  295. li r10,MSR_RI
  296. mtmsrd r10,1 /* Restore RI */
  297. #endif
  298. /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
  299. If TIF_NOERROR is set, just save r3 as it is. */
  300. andi. r0,r9,_TIF_RESTOREALL
  301. beq+ 0f
  302. REST_NVGPRS(r1)
  303. b 2f
  304. 0: cmpld r3,r11 /* r11 is -MAX_ERRNO */
  305. blt+ 1f
  306. andi. r0,r9,_TIF_NOERROR
  307. bne- 1f
  308. ld r5,_CCR(r1)
  309. neg r3,r3
  310. oris r5,r5,0x1000 /* Set SO bit in CR */
  311. std r5,_CCR(r1)
  312. 1: std r3,GPR3(r1)
  313. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  314. beq 4f
  315. /* Clear per-syscall TIF flags if any are set. */
  316. li r11,_TIF_PERSYSCALL_MASK
  317. addi r12,r12,TI_FLAGS
  318. 3: ldarx r10,0,r12
  319. andc r10,r10,r11
  320. stdcx. r10,0,r12
  321. bne- 3b
  322. subi r12,r12,TI_FLAGS
  323. 4: /* Anything else left to do? */
  324. BEGIN_FTR_SECTION
  325. lis r3,INIT_PPR@highest /* Set thread.ppr = 3 */
  326. ld r10,PACACURRENT(r13)
  327. sldi r3,r3,32 /* bits 11-13 are used for ppr */
  328. std r3,TASKTHREADPPR(r10)
  329. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  330. andi. r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP)
  331. beq ret_from_except_lite
  332. /* Re-enable interrupts */
  333. #ifdef CONFIG_PPC_BOOK3E
  334. wrteei 1
  335. #else
  336. li r10,MSR_RI
  337. ori r10,r10,MSR_EE
  338. mtmsrd r10,1
  339. #endif /* CONFIG_PPC_BOOK3E */
  340. bl save_nvgprs
  341. addi r3,r1,STACK_FRAME_OVERHEAD
  342. bl do_syscall_trace_leave
  343. b ret_from_except
  344. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  345. tabort_syscall:
  346. /* Firstly we need to enable TM in the kernel */
  347. mfmsr r10
  348. li r9, 1
  349. rldimi r10, r9, MSR_TM_LG, 63-MSR_TM_LG
  350. mtmsrd r10, 0
  351. /* tabort, this dooms the transaction, nothing else */
  352. li r9, (TM_CAUSE_SYSCALL|TM_CAUSE_PERSISTENT)
  353. TABORT(R9)
  354. /*
  355. * Return directly to userspace. We have corrupted user register state,
  356. * but userspace will never see that register state. Execution will
  357. * resume after the tbegin of the aborted transaction with the
  358. * checkpointed register state.
  359. */
  360. li r9, MSR_RI
  361. andc r10, r10, r9
  362. mtmsrd r10, 1
  363. mtspr SPRN_SRR0, r11
  364. mtspr SPRN_SRR1, r12
  365. RFI_TO_USER
  366. b . /* prevent speculative execution */
  367. #endif
  368. /* Save non-volatile GPRs, if not already saved. */
  369. _GLOBAL(save_nvgprs)
  370. ld r11,_TRAP(r1)
  371. andi. r0,r11,1
  372. beqlr-
  373. SAVE_NVGPRS(r1)
  374. clrrdi r0,r11,1
  375. std r0,_TRAP(r1)
  376. blr
  377. /*
  378. * The sigsuspend and rt_sigsuspend system calls can call do_signal
  379. * and thus put the process into the stopped state where we might
  380. * want to examine its user state with ptrace. Therefore we need
  381. * to save all the nonvolatile registers (r14 - r31) before calling
  382. * the C code. Similarly, fork, vfork and clone need the full
  383. * register state on the stack so that it can be copied to the child.
  384. */
  385. _GLOBAL(ppc_fork)
  386. bl save_nvgprs
  387. bl sys_fork
  388. b .Lsyscall_exit
  389. _GLOBAL(ppc_vfork)
  390. bl save_nvgprs
  391. bl sys_vfork
  392. b .Lsyscall_exit
  393. _GLOBAL(ppc_clone)
  394. bl save_nvgprs
  395. bl sys_clone
  396. b .Lsyscall_exit
  397. _GLOBAL(ppc32_swapcontext)
  398. bl save_nvgprs
  399. bl compat_sys_swapcontext
  400. b .Lsyscall_exit
  401. _GLOBAL(ppc64_swapcontext)
  402. bl save_nvgprs
  403. bl sys_swapcontext
  404. b .Lsyscall_exit
  405. _GLOBAL(ppc_switch_endian)
  406. bl save_nvgprs
  407. bl sys_switch_endian
  408. b .Lsyscall_exit
  409. _GLOBAL(ret_from_fork)
  410. bl schedule_tail
  411. REST_NVGPRS(r1)
  412. li r3,0
  413. b .Lsyscall_exit
  414. _GLOBAL(ret_from_kernel_thread)
  415. bl schedule_tail
  416. REST_NVGPRS(r1)
  417. mtlr r14
  418. mr r3,r15
  419. #ifdef PPC64_ELF_ABI_v2
  420. mr r12,r14
  421. #endif
  422. blrl
  423. li r3,0
  424. b .Lsyscall_exit
  425. /*
  426. * This routine switches between two different tasks. The process
  427. * state of one is saved on its kernel stack. Then the state
  428. * of the other is restored from its kernel stack. The memory
  429. * management hardware is updated to the second process's state.
  430. * Finally, we can return to the second process, via ret_from_except.
  431. * On entry, r3 points to the THREAD for the current task, r4
  432. * points to the THREAD for the new task.
  433. *
  434. * Note: there are two ways to get to the "going out" portion
  435. * of this code; either by coming in via the entry (_switch)
  436. * or via "fork" which must set up an environment equivalent
  437. * to the "_switch" path. If you change this you'll have to change
  438. * the fork code also.
  439. *
  440. * The code which creates the new task context is in 'copy_thread'
  441. * in arch/powerpc/kernel/process.c
  442. */
  443. .align 7
  444. _GLOBAL(_switch)
  445. mflr r0
  446. std r0,16(r1)
  447. stdu r1,-SWITCH_FRAME_SIZE(r1)
  448. /* r3-r13 are caller saved -- Cort */
  449. SAVE_8GPRS(14, r1)
  450. SAVE_10GPRS(22, r1)
  451. std r0,_NIP(r1) /* Return to switch caller */
  452. mfcr r23
  453. std r23,_CCR(r1)
  454. std r1,KSP(r3) /* Set old stack pointer */
  455. #ifdef CONFIG_SMP
  456. /* We need a sync somewhere here to make sure that if the
  457. * previous task gets rescheduled on another CPU, it sees all
  458. * stores it has performed on this one.
  459. */
  460. sync
  461. #endif /* CONFIG_SMP */
  462. /*
  463. * If we optimise away the clear of the reservation in system
  464. * calls because we know the CPU tracks the address of the
  465. * reservation, then we need to clear it here to cover the
  466. * case that the kernel context switch path has no larx
  467. * instructions.
  468. */
  469. BEGIN_FTR_SECTION
  470. ldarx r6,0,r1
  471. END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
  472. BEGIN_FTR_SECTION
  473. /*
  474. * A cp_abort (copy paste abort) here ensures that when context switching, a
  475. * copy from one process can't leak into the paste of another.
  476. */
  477. PPC_CP_ABORT
  478. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  479. #ifdef CONFIG_PPC_BOOK3S
  480. /* Cancel all explict user streams as they will have no use after context
  481. * switch and will stop the HW from creating streams itself
  482. */
  483. DCBT_STOP_ALL_STREAM_IDS(r6)
  484. #endif
  485. addi r6,r4,-THREAD /* Convert THREAD to 'current' */
  486. std r6,PACACURRENT(r13) /* Set new 'current' */
  487. ld r8,KSP(r4) /* new stack pointer */
  488. #ifdef CONFIG_PPC_STD_MMU_64
  489. BEGIN_MMU_FTR_SECTION
  490. b 2f
  491. END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
  492. BEGIN_FTR_SECTION
  493. clrrdi r6,r8,28 /* get its ESID */
  494. clrrdi r9,r1,28 /* get current sp ESID */
  495. FTR_SECTION_ELSE
  496. clrrdi r6,r8,40 /* get its 1T ESID */
  497. clrrdi r9,r1,40 /* get current sp 1T ESID */
  498. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_1T_SEGMENT)
  499. clrldi. r0,r6,2 /* is new ESID c00000000? */
  500. cmpd cr1,r6,r9 /* or is new ESID the same as current ESID? */
  501. cror eq,4*cr1+eq,eq
  502. beq 2f /* if yes, don't slbie it */
  503. /* Bolt in the new stack SLB entry */
  504. ld r7,KSP_VSID(r4) /* Get new stack's VSID */
  505. oris r0,r6,(SLB_ESID_V)@h
  506. ori r0,r0,(SLB_NUM_BOLTED-1)@l
  507. BEGIN_FTR_SECTION
  508. li r9,MMU_SEGSIZE_1T /* insert B field */
  509. oris r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
  510. rldimi r7,r9,SLB_VSID_SSIZE_SHIFT,0
  511. END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
  512. /* Update the last bolted SLB. No write barriers are needed
  513. * here, provided we only update the current CPU's SLB shadow
  514. * buffer.
  515. */
  516. ld r9,PACA_SLBSHADOWPTR(r13)
  517. li r12,0
  518. std r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
  519. li r12,SLBSHADOW_STACKVSID
  520. STDX_BE r7,r12,r9 /* Save VSID */
  521. li r12,SLBSHADOW_STACKESID
  522. STDX_BE r0,r12,r9 /* Save ESID */
  523. /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
  524. * we have 1TB segments, the only CPUs known to have the errata
  525. * only support less than 1TB of system memory and we'll never
  526. * actually hit this code path.
  527. */
  528. isync
  529. slbie r6
  530. slbie r6 /* Workaround POWER5 < DD2.1 issue */
  531. slbmte r7,r0
  532. isync
  533. 2:
  534. #endif /* CONFIG_PPC_STD_MMU_64 */
  535. CURRENT_THREAD_INFO(r7, r8) /* base of new stack */
  536. /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
  537. because we don't need to leave the 288-byte ABI gap at the
  538. top of the kernel stack. */
  539. addi r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
  540. mr r1,r8 /* start using new stack pointer */
  541. std r7,PACAKSAVE(r13)
  542. ld r6,_CCR(r1)
  543. mtcrf 0xFF,r6
  544. /* r3-r13 are destroyed -- Cort */
  545. REST_8GPRS(14, r1)
  546. REST_10GPRS(22, r1)
  547. /* convert old thread to its task_struct for return value */
  548. addi r3,r3,-THREAD
  549. ld r7,_NIP(r1) /* Return to _switch caller in new task */
  550. mtlr r7
  551. addi r1,r1,SWITCH_FRAME_SIZE
  552. blr
  553. .align 7
  554. _GLOBAL(ret_from_except)
  555. ld r11,_TRAP(r1)
  556. andi. r0,r11,1
  557. bne ret_from_except_lite
  558. REST_NVGPRS(r1)
  559. _GLOBAL(ret_from_except_lite)
  560. /*
  561. * Disable interrupts so that current_thread_info()->flags
  562. * can't change between when we test it and when we return
  563. * from the interrupt.
  564. */
  565. #ifdef CONFIG_PPC_BOOK3E
  566. wrteei 0
  567. #else
  568. li r10,MSR_RI
  569. mtmsrd r10,1 /* Update machine state */
  570. #endif /* CONFIG_PPC_BOOK3E */
  571. CURRENT_THREAD_INFO(r9, r1)
  572. ld r3,_MSR(r1)
  573. #ifdef CONFIG_PPC_BOOK3E
  574. ld r10,PACACURRENT(r13)
  575. #endif /* CONFIG_PPC_BOOK3E */
  576. ld r4,TI_FLAGS(r9)
  577. andi. r3,r3,MSR_PR
  578. beq resume_kernel
  579. #ifdef CONFIG_PPC_BOOK3E
  580. lwz r3,(THREAD+THREAD_DBCR0)(r10)
  581. #endif /* CONFIG_PPC_BOOK3E */
  582. /* Check current_thread_info()->flags */
  583. andi. r0,r4,_TIF_USER_WORK_MASK
  584. bne 1f
  585. #ifdef CONFIG_PPC_BOOK3E
  586. /*
  587. * Check to see if the dbcr0 register is set up to debug.
  588. * Use the internal debug mode bit to do this.
  589. */
  590. andis. r0,r3,DBCR0_IDM@h
  591. beq restore
  592. mfmsr r0
  593. rlwinm r0,r0,0,~MSR_DE /* Clear MSR.DE */
  594. mtmsr r0
  595. mtspr SPRN_DBCR0,r3
  596. li r10, -1
  597. mtspr SPRN_DBSR,r10
  598. b restore
  599. #else
  600. addi r3,r1,STACK_FRAME_OVERHEAD
  601. bl restore_math
  602. b restore
  603. #endif
  604. 1: andi. r0,r4,_TIF_NEED_RESCHED
  605. beq 2f
  606. bl restore_interrupts
  607. SCHEDULE_USER
  608. b ret_from_except_lite
  609. 2:
  610. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  611. andi. r0,r4,_TIF_USER_WORK_MASK & ~_TIF_RESTORE_TM
  612. bne 3f /* only restore TM if nothing else to do */
  613. addi r3,r1,STACK_FRAME_OVERHEAD
  614. bl restore_tm_state
  615. b restore
  616. 3:
  617. #endif
  618. bl save_nvgprs
  619. /*
  620. * Use a non volatile GPR to save and restore our thread_info flags
  621. * across the call to restore_interrupts.
  622. */
  623. mr r30,r4
  624. bl restore_interrupts
  625. mr r4,r30
  626. addi r3,r1,STACK_FRAME_OVERHEAD
  627. bl do_notify_resume
  628. b ret_from_except
  629. resume_kernel:
  630. /* check current_thread_info, _TIF_EMULATE_STACK_STORE */
  631. andis. r8,r4,_TIF_EMULATE_STACK_STORE@h
  632. beq+ 1f
  633. addi r8,r1,INT_FRAME_SIZE /* Get the kprobed function entry */
  634. ld r3,GPR1(r1)
  635. subi r3,r3,INT_FRAME_SIZE /* dst: Allocate a trampoline exception frame */
  636. mr r4,r1 /* src: current exception frame */
  637. mr r1,r3 /* Reroute the trampoline frame to r1 */
  638. /* Copy from the original to the trampoline. */
  639. li r5,INT_FRAME_SIZE/8 /* size: INT_FRAME_SIZE */
  640. li r6,0 /* start offset: 0 */
  641. mtctr r5
  642. 2: ldx r0,r6,r4
  643. stdx r0,r6,r3
  644. addi r6,r6,8
  645. bdnz 2b
  646. /* Do real store operation to complete stdu */
  647. ld r5,GPR1(r1)
  648. std r8,0(r5)
  649. /* Clear _TIF_EMULATE_STACK_STORE flag */
  650. lis r11,_TIF_EMULATE_STACK_STORE@h
  651. addi r5,r9,TI_FLAGS
  652. 0: ldarx r4,0,r5
  653. andc r4,r4,r11
  654. stdcx. r4,0,r5
  655. bne- 0b
  656. 1:
  657. #ifdef CONFIG_PREEMPT
  658. /* Check if we need to preempt */
  659. andi. r0,r4,_TIF_NEED_RESCHED
  660. beq+ restore
  661. /* Check that preempt_count() == 0 and interrupts are enabled */
  662. lwz r8,TI_PREEMPT(r9)
  663. cmpwi cr1,r8,0
  664. ld r0,SOFTE(r1)
  665. cmpdi r0,0
  666. crandc eq,cr1*4+eq,eq
  667. bne restore
  668. /*
  669. * Here we are preempting the current task. We want to make
  670. * sure we are soft-disabled first and reconcile irq state.
  671. */
  672. RECONCILE_IRQ_STATE(r3,r4)
  673. 1: bl preempt_schedule_irq
  674. /* Re-test flags and eventually loop */
  675. CURRENT_THREAD_INFO(r9, r1)
  676. ld r4,TI_FLAGS(r9)
  677. andi. r0,r4,_TIF_NEED_RESCHED
  678. bne 1b
  679. /*
  680. * arch_local_irq_restore() from preempt_schedule_irq above may
  681. * enable hard interrupt but we really should disable interrupts
  682. * when we return from the interrupt, and so that we don't get
  683. * interrupted after loading SRR0/1.
  684. */
  685. #ifdef CONFIG_PPC_BOOK3E
  686. wrteei 0
  687. #else
  688. li r10,MSR_RI
  689. mtmsrd r10,1 /* Update machine state */
  690. #endif /* CONFIG_PPC_BOOK3E */
  691. #endif /* CONFIG_PREEMPT */
  692. .globl fast_exc_return_irq
  693. fast_exc_return_irq:
  694. restore:
  695. /*
  696. * This is the main kernel exit path. First we check if we
  697. * are about to re-enable interrupts
  698. */
  699. ld r5,SOFTE(r1)
  700. lbz r6,PACASOFTIRQEN(r13)
  701. cmpwi cr0,r5,0
  702. beq restore_irq_off
  703. /* We are enabling, were we already enabled ? Yes, just return */
  704. cmpwi cr0,r6,1
  705. beq cr0,do_restore
  706. /*
  707. * We are about to soft-enable interrupts (we are hard disabled
  708. * at this point). We check if there's anything that needs to
  709. * be replayed first.
  710. */
  711. lbz r0,PACAIRQHAPPENED(r13)
  712. cmpwi cr0,r0,0
  713. bne- restore_check_irq_replay
  714. /*
  715. * Get here when nothing happened while soft-disabled, just
  716. * soft-enable and move-on. We will hard-enable as a side
  717. * effect of rfi
  718. */
  719. restore_no_replay:
  720. TRACE_ENABLE_INTS
  721. li r0,1
  722. stb r0,PACASOFTIRQEN(r13);
  723. /*
  724. * Final return path. BookE is handled in a different file
  725. */
  726. do_restore:
  727. #ifdef CONFIG_PPC_BOOK3E
  728. b exception_return_book3e
  729. #else
  730. /*
  731. * Clear the reservation. If we know the CPU tracks the address of
  732. * the reservation then we can potentially save some cycles and use
  733. * a larx. On POWER6 and POWER7 this is significantly faster.
  734. */
  735. BEGIN_FTR_SECTION
  736. stdcx. r0,0,r1 /* to clear the reservation */
  737. FTR_SECTION_ELSE
  738. ldarx r4,0,r1
  739. ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  740. /*
  741. * Some code path such as load_up_fpu or altivec return directly
  742. * here. They run entirely hard disabled and do not alter the
  743. * interrupt state. They also don't use lwarx/stwcx. and thus
  744. * are known not to leave dangling reservations.
  745. */
  746. .globl fast_exception_return
  747. fast_exception_return:
  748. ld r3,_MSR(r1)
  749. ld r4,_CTR(r1)
  750. ld r0,_LINK(r1)
  751. mtctr r4
  752. mtlr r0
  753. ld r4,_XER(r1)
  754. mtspr SPRN_XER,r4
  755. REST_8GPRS(5, r1)
  756. andi. r0,r3,MSR_RI
  757. beq- unrecov_restore
  758. /* Load PPR from thread struct before we clear MSR:RI */
  759. BEGIN_FTR_SECTION
  760. ld r2,PACACURRENT(r13)
  761. ld r2,TASKTHREADPPR(r2)
  762. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  763. /*
  764. * Clear RI before restoring r13. If we are returning to
  765. * userspace and we take an exception after restoring r13,
  766. * we end up corrupting the userspace r13 value.
  767. */
  768. li r4,0
  769. mtmsrd r4,1
  770. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  771. /* TM debug */
  772. std r3, PACATMSCRATCH(r13) /* Stash returned-to MSR */
  773. #endif
  774. /*
  775. * r13 is our per cpu area, only restore it if we are returning to
  776. * userspace the value stored in the stack frame may belong to
  777. * another CPU.
  778. */
  779. andi. r0,r3,MSR_PR
  780. beq 1f
  781. BEGIN_FTR_SECTION
  782. mtspr SPRN_PPR,r2 /* Restore PPR */
  783. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  784. ACCOUNT_CPU_USER_EXIT(r13, r2, r4)
  785. REST_GPR(13, r1)
  786. mtspr SPRN_SRR1,r3
  787. ld r2,_CCR(r1)
  788. mtcrf 0xFF,r2
  789. ld r2,_NIP(r1)
  790. mtspr SPRN_SRR0,r2
  791. ld r0,GPR0(r1)
  792. ld r2,GPR2(r1)
  793. ld r3,GPR3(r1)
  794. ld r4,GPR4(r1)
  795. ld r1,GPR1(r1)
  796. RFI_TO_USER
  797. b . /* prevent speculative execution */
  798. 1: mtspr SPRN_SRR1,r3
  799. ld r2,_CCR(r1)
  800. mtcrf 0xFF,r2
  801. ld r2,_NIP(r1)
  802. mtspr SPRN_SRR0,r2
  803. ld r0,GPR0(r1)
  804. ld r2,GPR2(r1)
  805. ld r3,GPR3(r1)
  806. ld r4,GPR4(r1)
  807. ld r1,GPR1(r1)
  808. RFI_TO_KERNEL
  809. b . /* prevent speculative execution */
  810. #endif /* CONFIG_PPC_BOOK3E */
  811. /*
  812. * We are returning to a context with interrupts soft disabled.
  813. *
  814. * However, we may also about to hard enable, so we need to
  815. * make sure that in this case, we also clear PACA_IRQ_HARD_DIS
  816. * or that bit can get out of sync and bad things will happen
  817. */
  818. restore_irq_off:
  819. ld r3,_MSR(r1)
  820. lbz r7,PACAIRQHAPPENED(r13)
  821. andi. r0,r3,MSR_EE
  822. beq 1f
  823. rlwinm r7,r7,0,~PACA_IRQ_HARD_DIS
  824. stb r7,PACAIRQHAPPENED(r13)
  825. 1: li r0,0
  826. stb r0,PACASOFTIRQEN(r13);
  827. TRACE_DISABLE_INTS
  828. b do_restore
  829. /*
  830. * Something did happen, check if a re-emit is needed
  831. * (this also clears paca->irq_happened)
  832. */
  833. restore_check_irq_replay:
  834. /* XXX: We could implement a fast path here where we check
  835. * for irq_happened being just 0x01, in which case we can
  836. * clear it and return. That means that we would potentially
  837. * miss a decrementer having wrapped all the way around.
  838. *
  839. * Still, this might be useful for things like hash_page
  840. */
  841. bl __check_irq_replay
  842. cmpwi cr0,r3,0
  843. beq restore_no_replay
  844. /*
  845. * We need to re-emit an interrupt. We do so by re-using our
  846. * existing exception frame. We first change the trap value,
  847. * but we need to ensure we preserve the low nibble of it
  848. */
  849. ld r4,_TRAP(r1)
  850. clrldi r4,r4,60
  851. or r4,r4,r3
  852. std r4,_TRAP(r1)
  853. /*
  854. * Then find the right handler and call it. Interrupts are
  855. * still soft-disabled and we keep them that way.
  856. */
  857. cmpwi cr0,r3,0x500
  858. bne 1f
  859. addi r3,r1,STACK_FRAME_OVERHEAD;
  860. bl do_IRQ
  861. b ret_from_except
  862. 1: cmpwi cr0,r3,0xe60
  863. bne 1f
  864. addi r3,r1,STACK_FRAME_OVERHEAD;
  865. bl handle_hmi_exception
  866. b ret_from_except
  867. 1: cmpwi cr0,r3,0x900
  868. bne 1f
  869. addi r3,r1,STACK_FRAME_OVERHEAD;
  870. bl timer_interrupt
  871. b ret_from_except
  872. #ifdef CONFIG_PPC_DOORBELL
  873. 1:
  874. #ifdef CONFIG_PPC_BOOK3E
  875. cmpwi cr0,r3,0x280
  876. #else
  877. BEGIN_FTR_SECTION
  878. cmpwi cr0,r3,0xe80
  879. FTR_SECTION_ELSE
  880. cmpwi cr0,r3,0xa00
  881. ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
  882. #endif /* CONFIG_PPC_BOOK3E */
  883. bne 1f
  884. addi r3,r1,STACK_FRAME_OVERHEAD;
  885. bl doorbell_exception
  886. b ret_from_except
  887. #endif /* CONFIG_PPC_DOORBELL */
  888. 1: b ret_from_except /* What else to do here ? */
  889. unrecov_restore:
  890. addi r3,r1,STACK_FRAME_OVERHEAD
  891. bl unrecoverable_exception
  892. b unrecov_restore
  893. #ifdef CONFIG_PPC_RTAS
  894. /*
  895. * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
  896. * called with the MMU off.
  897. *
  898. * In addition, we need to be in 32b mode, at least for now.
  899. *
  900. * Note: r3 is an input parameter to rtas, so don't trash it...
  901. */
  902. _GLOBAL(enter_rtas)
  903. mflr r0
  904. std r0,16(r1)
  905. stdu r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
  906. /* Because RTAS is running in 32b mode, it clobbers the high order half
  907. * of all registers that it saves. We therefore save those registers
  908. * RTAS might touch to the stack. (r0, r3-r13 are caller saved)
  909. */
  910. SAVE_GPR(2, r1) /* Save the TOC */
  911. SAVE_GPR(13, r1) /* Save paca */
  912. SAVE_8GPRS(14, r1) /* Save the non-volatiles */
  913. SAVE_10GPRS(22, r1) /* ditto */
  914. mfcr r4
  915. std r4,_CCR(r1)
  916. mfctr r5
  917. std r5,_CTR(r1)
  918. mfspr r6,SPRN_XER
  919. std r6,_XER(r1)
  920. mfdar r7
  921. std r7,_DAR(r1)
  922. mfdsisr r8
  923. std r8,_DSISR(r1)
  924. /* Temporary workaround to clear CR until RTAS can be modified to
  925. * ignore all bits.
  926. */
  927. li r0,0
  928. mtcr r0
  929. #ifdef CONFIG_BUG
  930. /* There is no way it is acceptable to get here with interrupts enabled,
  931. * check it with the asm equivalent of WARN_ON
  932. */
  933. lbz r0,PACASOFTIRQEN(r13)
  934. 1: tdnei r0,0
  935. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
  936. #endif
  937. /* Hard-disable interrupts */
  938. mfmsr r6
  939. rldicl r7,r6,48,1
  940. rotldi r7,r7,16
  941. mtmsrd r7,1
  942. /* Unfortunately, the stack pointer and the MSR are also clobbered,
  943. * so they are saved in the PACA which allows us to restore
  944. * our original state after RTAS returns.
  945. */
  946. std r1,PACAR1(r13)
  947. std r6,PACASAVEDMSR(r13)
  948. /* Setup our real return addr */
  949. LOAD_REG_ADDR(r4,rtas_return_loc)
  950. clrldi r4,r4,2 /* convert to realmode address */
  951. mtlr r4
  952. li r0,0
  953. ori r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
  954. andc r0,r6,r0
  955. li r9,1
  956. rldicr r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
  957. ori r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI|MSR_LE
  958. andc r6,r0,r9
  959. sync /* disable interrupts so SRR0/1 */
  960. mtmsrd r0 /* don't get trashed */
  961. LOAD_REG_ADDR(r4, rtas)
  962. ld r5,RTASENTRY(r4) /* get the rtas->entry value */
  963. ld r4,RTASBASE(r4) /* get the rtas->base value */
  964. mtspr SPRN_SRR0,r5
  965. mtspr SPRN_SRR1,r6
  966. RFI_TO_KERNEL
  967. b . /* prevent speculative execution */
  968. rtas_return_loc:
  969. FIXUP_ENDIAN
  970. /* relocation is off at this point */
  971. GET_PACA(r4)
  972. clrldi r4,r4,2 /* convert to realmode address */
  973. bcl 20,31,$+4
  974. 0: mflr r3
  975. ld r3,(1f-0b)(r3) /* get &rtas_restore_regs */
  976. mfmsr r6
  977. li r0,MSR_RI
  978. andc r6,r6,r0
  979. sync
  980. mtmsrd r6
  981. ld r1,PACAR1(r4) /* Restore our SP */
  982. ld r4,PACASAVEDMSR(r4) /* Restore our MSR */
  983. mtspr SPRN_SRR0,r3
  984. mtspr SPRN_SRR1,r4
  985. RFI_TO_KERNEL
  986. b . /* prevent speculative execution */
  987. .align 3
  988. 1: .llong rtas_restore_regs
  989. rtas_restore_regs:
  990. /* relocation is on at this point */
  991. REST_GPR(2, r1) /* Restore the TOC */
  992. REST_GPR(13, r1) /* Restore paca */
  993. REST_8GPRS(14, r1) /* Restore the non-volatiles */
  994. REST_10GPRS(22, r1) /* ditto */
  995. GET_PACA(r13)
  996. ld r4,_CCR(r1)
  997. mtcr r4
  998. ld r5,_CTR(r1)
  999. mtctr r5
  1000. ld r6,_XER(r1)
  1001. mtspr SPRN_XER,r6
  1002. ld r7,_DAR(r1)
  1003. mtdar r7
  1004. ld r8,_DSISR(r1)
  1005. mtdsisr r8
  1006. addi r1,r1,RTAS_FRAME_SIZE /* Unstack our frame */
  1007. ld r0,16(r1) /* get return address */
  1008. mtlr r0
  1009. blr /* return to caller */
  1010. #endif /* CONFIG_PPC_RTAS */
  1011. _GLOBAL(enter_prom)
  1012. mflr r0
  1013. std r0,16(r1)
  1014. stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
  1015. /* Because PROM is running in 32b mode, it clobbers the high order half
  1016. * of all registers that it saves. We therefore save those registers
  1017. * PROM might touch to the stack. (r0, r3-r13 are caller saved)
  1018. */
  1019. SAVE_GPR(2, r1)
  1020. SAVE_GPR(13, r1)
  1021. SAVE_8GPRS(14, r1)
  1022. SAVE_10GPRS(22, r1)
  1023. mfcr r10
  1024. mfmsr r11
  1025. std r10,_CCR(r1)
  1026. std r11,_MSR(r1)
  1027. /* Put PROM address in SRR0 */
  1028. mtsrr0 r4
  1029. /* Setup our trampoline return addr in LR */
  1030. bcl 20,31,$+4
  1031. 0: mflr r4
  1032. addi r4,r4,(1f - 0b)
  1033. mtlr r4
  1034. /* Prepare a 32-bit mode big endian MSR
  1035. */
  1036. #ifdef CONFIG_PPC_BOOK3E
  1037. rlwinm r11,r11,0,1,31
  1038. mtsrr1 r11
  1039. rfi
  1040. #else /* CONFIG_PPC_BOOK3E */
  1041. LOAD_REG_IMMEDIATE(r12, MSR_SF | MSR_ISF | MSR_LE)
  1042. andc r11,r11,r12
  1043. mtsrr1 r11
  1044. RFI_TO_KERNEL
  1045. #endif /* CONFIG_PPC_BOOK3E */
  1046. 1: /* Return from OF */
  1047. FIXUP_ENDIAN
  1048. /* Just make sure that r1 top 32 bits didn't get
  1049. * corrupt by OF
  1050. */
  1051. rldicl r1,r1,0,32
  1052. /* Restore the MSR (back to 64 bits) */
  1053. ld r0,_MSR(r1)
  1054. MTMSRD(r0)
  1055. isync
  1056. /* Restore other registers */
  1057. REST_GPR(2, r1)
  1058. REST_GPR(13, r1)
  1059. REST_8GPRS(14, r1)
  1060. REST_10GPRS(22, r1)
  1061. ld r4,_CCR(r1)
  1062. mtcr r4
  1063. addi r1,r1,PROM_FRAME_SIZE
  1064. ld r0,16(r1)
  1065. mtlr r0
  1066. blr
  1067. #ifdef CONFIG_FUNCTION_TRACER
  1068. #ifdef CONFIG_DYNAMIC_FTRACE
  1069. _GLOBAL(mcount)
  1070. _GLOBAL(_mcount)
  1071. EXPORT_SYMBOL(_mcount)
  1072. mflr r12
  1073. mtctr r12
  1074. mtlr r0
  1075. bctr
  1076. #ifndef CC_USING_MPROFILE_KERNEL
  1077. _GLOBAL_TOC(ftrace_caller)
  1078. /* Taken from output of objdump from lib64/glibc */
  1079. mflr r3
  1080. ld r11, 0(r1)
  1081. stdu r1, -112(r1)
  1082. std r3, 128(r1)
  1083. ld r4, 16(r11)
  1084. subi r3, r3, MCOUNT_INSN_SIZE
  1085. .globl ftrace_call
  1086. ftrace_call:
  1087. bl ftrace_stub
  1088. nop
  1089. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1090. .globl ftrace_graph_call
  1091. ftrace_graph_call:
  1092. b ftrace_graph_stub
  1093. _GLOBAL(ftrace_graph_stub)
  1094. #endif
  1095. ld r0, 128(r1)
  1096. mtlr r0
  1097. addi r1, r1, 112
  1098. #else /* CC_USING_MPROFILE_KERNEL */
  1099. /*
  1100. *
  1101. * ftrace_caller() is the function that replaces _mcount() when ftrace is
  1102. * active.
  1103. *
  1104. * We arrive here after a function A calls function B, and we are the trace
  1105. * function for B. When we enter r1 points to A's stack frame, B has not yet
  1106. * had a chance to allocate one yet.
  1107. *
  1108. * Additionally r2 may point either to the TOC for A, or B, depending on
  1109. * whether B did a TOC setup sequence before calling us.
  1110. *
  1111. * On entry the LR points back to the _mcount() call site, and r0 holds the
  1112. * saved LR as it was on entry to B, ie. the original return address at the
  1113. * call site in A.
  1114. *
  1115. * Our job is to save the register state into a struct pt_regs (on the stack)
  1116. * and then arrange for the ftrace function to be called.
  1117. */
  1118. _GLOBAL(ftrace_caller)
  1119. /* Save the original return address in A's stack frame */
  1120. std r0,LRSAVE(r1)
  1121. /* Create our stack frame + pt_regs */
  1122. stdu r1,-SWITCH_FRAME_SIZE(r1)
  1123. /* Save all gprs to pt_regs */
  1124. SAVE_GPR(0, r1)
  1125. SAVE_10GPRS(2, r1)
  1126. SAVE_10GPRS(12, r1)
  1127. SAVE_10GPRS(22, r1)
  1128. /* Save previous stack pointer (r1) */
  1129. addi r8, r1, SWITCH_FRAME_SIZE
  1130. std r8, GPR1(r1)
  1131. /* Load special regs for save below */
  1132. mfmsr r8
  1133. mfctr r9
  1134. mfxer r10
  1135. mfcr r11
  1136. /* Get the _mcount() call site out of LR */
  1137. mflr r7
  1138. /* Save it as pt_regs->nip & pt_regs->link */
  1139. std r7, _NIP(r1)
  1140. std r7, _LINK(r1)
  1141. /* Save callee's TOC in the ABI compliant location */
  1142. std r2, 24(r1)
  1143. ld r2,PACATOC(r13) /* get kernel TOC in r2 */
  1144. addis r3,r2,function_trace_op@toc@ha
  1145. addi r3,r3,function_trace_op@toc@l
  1146. ld r5,0(r3)
  1147. #ifdef CONFIG_LIVEPATCH
  1148. mr r14,r7 /* remember old NIP */
  1149. #endif
  1150. /* Calculate ip from nip-4 into r3 for call below */
  1151. subi r3, r7, MCOUNT_INSN_SIZE
  1152. /* Put the original return address in r4 as parent_ip */
  1153. mr r4, r0
  1154. /* Save special regs */
  1155. std r8, _MSR(r1)
  1156. std r9, _CTR(r1)
  1157. std r10, _XER(r1)
  1158. std r11, _CCR(r1)
  1159. /* Load &pt_regs in r6 for call below */
  1160. addi r6, r1 ,STACK_FRAME_OVERHEAD
  1161. /* ftrace_call(r3, r4, r5, r6) */
  1162. .globl ftrace_call
  1163. ftrace_call:
  1164. bl ftrace_stub
  1165. nop
  1166. /* Load ctr with the possibly modified NIP */
  1167. ld r3, _NIP(r1)
  1168. mtctr r3
  1169. #ifdef CONFIG_LIVEPATCH
  1170. cmpd r14,r3 /* has NIP been altered? */
  1171. #endif
  1172. /* Restore gprs */
  1173. REST_GPR(0,r1)
  1174. REST_10GPRS(2,r1)
  1175. REST_10GPRS(12,r1)
  1176. REST_10GPRS(22,r1)
  1177. /* Restore callee's TOC */
  1178. ld r2, 24(r1)
  1179. /* Pop our stack frame */
  1180. addi r1, r1, SWITCH_FRAME_SIZE
  1181. /* Restore original LR for return to B */
  1182. ld r0, LRSAVE(r1)
  1183. mtlr r0
  1184. #ifdef CONFIG_LIVEPATCH
  1185. /* Based on the cmpd above, if the NIP was altered handle livepatch */
  1186. bne- livepatch_handler
  1187. #endif
  1188. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1189. stdu r1, -112(r1)
  1190. .globl ftrace_graph_call
  1191. ftrace_graph_call:
  1192. b ftrace_graph_stub
  1193. _GLOBAL(ftrace_graph_stub)
  1194. addi r1, r1, 112
  1195. #endif
  1196. ld r0,LRSAVE(r1) /* restore callee's lr at _mcount site */
  1197. mtlr r0
  1198. bctr /* jump after _mcount site */
  1199. #endif /* CC_USING_MPROFILE_KERNEL */
  1200. _GLOBAL(ftrace_stub)
  1201. blr
  1202. #ifdef CONFIG_LIVEPATCH
  1203. /*
  1204. * This function runs in the mcount context, between two functions. As
  1205. * such it can only clobber registers which are volatile and used in
  1206. * function linkage.
  1207. *
  1208. * We get here when a function A, calls another function B, but B has
  1209. * been live patched with a new function C.
  1210. *
  1211. * On entry:
  1212. * - we have no stack frame and can not allocate one
  1213. * - LR points back to the original caller (in A)
  1214. * - CTR holds the new NIP in C
  1215. * - r0 & r12 are free
  1216. *
  1217. * r0 can't be used as the base register for a DS-form load or store, so
  1218. * we temporarily shuffle r1 (stack pointer) into r0 and then put it back.
  1219. */
  1220. livepatch_handler:
  1221. CURRENT_THREAD_INFO(r12, r1)
  1222. /* Save stack pointer into r0 */
  1223. mr r0, r1
  1224. /* Allocate 3 x 8 bytes */
  1225. ld r1, TI_livepatch_sp(r12)
  1226. addi r1, r1, 24
  1227. std r1, TI_livepatch_sp(r12)
  1228. /* Save toc & real LR on livepatch stack */
  1229. std r2, -24(r1)
  1230. mflr r12
  1231. std r12, -16(r1)
  1232. /* Store stack end marker */
  1233. lis r12, STACK_END_MAGIC@h
  1234. ori r12, r12, STACK_END_MAGIC@l
  1235. std r12, -8(r1)
  1236. /* Restore real stack pointer */
  1237. mr r1, r0
  1238. /* Put ctr in r12 for global entry and branch there */
  1239. mfctr r12
  1240. bctrl
  1241. /*
  1242. * Now we are returning from the patched function to the original
  1243. * caller A. We are free to use r0 and r12, and we can use r2 until we
  1244. * restore it.
  1245. */
  1246. CURRENT_THREAD_INFO(r12, r1)
  1247. /* Save stack pointer into r0 */
  1248. mr r0, r1
  1249. ld r1, TI_livepatch_sp(r12)
  1250. /* Check stack marker hasn't been trashed */
  1251. lis r2, STACK_END_MAGIC@h
  1252. ori r2, r2, STACK_END_MAGIC@l
  1253. ld r12, -8(r1)
  1254. 1: tdne r12, r2
  1255. EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
  1256. /* Restore LR & toc from livepatch stack */
  1257. ld r12, -16(r1)
  1258. mtlr r12
  1259. ld r2, -24(r1)
  1260. /* Pop livepatch stack frame */
  1261. CURRENT_THREAD_INFO(r12, r0)
  1262. subi r1, r1, 24
  1263. std r1, TI_livepatch_sp(r12)
  1264. /* Restore real stack pointer */
  1265. mr r1, r0
  1266. /* Return to original caller of live patched function */
  1267. blr
  1268. #endif
  1269. #else
  1270. _GLOBAL_TOC(_mcount)
  1271. EXPORT_SYMBOL(_mcount)
  1272. /* Taken from output of objdump from lib64/glibc */
  1273. mflr r3
  1274. ld r11, 0(r1)
  1275. stdu r1, -112(r1)
  1276. std r3, 128(r1)
  1277. ld r4, 16(r11)
  1278. subi r3, r3, MCOUNT_INSN_SIZE
  1279. LOAD_REG_ADDR(r5,ftrace_trace_function)
  1280. ld r5,0(r5)
  1281. ld r5,0(r5)
  1282. mtctr r5
  1283. bctrl
  1284. nop
  1285. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1286. b ftrace_graph_caller
  1287. #endif
  1288. ld r0, 128(r1)
  1289. mtlr r0
  1290. addi r1, r1, 112
  1291. _GLOBAL(ftrace_stub)
  1292. blr
  1293. #endif /* CONFIG_DYNAMIC_FTRACE */
  1294. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1295. #ifndef CC_USING_MPROFILE_KERNEL
  1296. _GLOBAL(ftrace_graph_caller)
  1297. /* load r4 with local address */
  1298. ld r4, 128(r1)
  1299. subi r4, r4, MCOUNT_INSN_SIZE
  1300. /* Grab the LR out of the caller stack frame */
  1301. ld r11, 112(r1)
  1302. ld r3, 16(r11)
  1303. bl prepare_ftrace_return
  1304. nop
  1305. /*
  1306. * prepare_ftrace_return gives us the address we divert to.
  1307. * Change the LR in the callers stack frame to this.
  1308. */
  1309. ld r11, 112(r1)
  1310. std r3, 16(r11)
  1311. ld r0, 128(r1)
  1312. mtlr r0
  1313. addi r1, r1, 112
  1314. blr
  1315. #else /* CC_USING_MPROFILE_KERNEL */
  1316. _GLOBAL(ftrace_graph_caller)
  1317. /* with -mprofile-kernel, parameter regs are still alive at _mcount */
  1318. std r10, 104(r1)
  1319. std r9, 96(r1)
  1320. std r8, 88(r1)
  1321. std r7, 80(r1)
  1322. std r6, 72(r1)
  1323. std r5, 64(r1)
  1324. std r4, 56(r1)
  1325. std r3, 48(r1)
  1326. /* Save callee's TOC in the ABI compliant location */
  1327. std r2, 24(r1)
  1328. ld r2, PACATOC(r13) /* get kernel TOC in r2 */
  1329. mfctr r4 /* ftrace_caller has moved local addr here */
  1330. std r4, 40(r1)
  1331. mflr r3 /* ftrace_caller has restored LR from stack */
  1332. subi r4, r4, MCOUNT_INSN_SIZE
  1333. bl prepare_ftrace_return
  1334. nop
  1335. /*
  1336. * prepare_ftrace_return gives us the address we divert to.
  1337. * Change the LR to this.
  1338. */
  1339. mtlr r3
  1340. ld r0, 40(r1)
  1341. mtctr r0
  1342. ld r10, 104(r1)
  1343. ld r9, 96(r1)
  1344. ld r8, 88(r1)
  1345. ld r7, 80(r1)
  1346. ld r6, 72(r1)
  1347. ld r5, 64(r1)
  1348. ld r4, 56(r1)
  1349. ld r3, 48(r1)
  1350. /* Restore callee's TOC */
  1351. ld r2, 24(r1)
  1352. addi r1, r1, 112
  1353. mflr r0
  1354. std r0, LRSAVE(r1)
  1355. bctr
  1356. #endif /* CC_USING_MPROFILE_KERNEL */
  1357. _GLOBAL(return_to_handler)
  1358. /* need to save return values */
  1359. std r4, -32(r1)
  1360. std r3, -24(r1)
  1361. /* save TOC */
  1362. std r2, -16(r1)
  1363. std r31, -8(r1)
  1364. mr r31, r1
  1365. stdu r1, -112(r1)
  1366. /*
  1367. * We might be called from a module.
  1368. * Switch to our TOC to run inside the core kernel.
  1369. */
  1370. ld r2, PACATOC(r13)
  1371. bl ftrace_return_to_handler
  1372. nop
  1373. /* return value has real return address */
  1374. mtlr r3
  1375. ld r1, 0(r1)
  1376. ld r4, -32(r1)
  1377. ld r3, -24(r1)
  1378. ld r2, -16(r1)
  1379. ld r31, -8(r1)
  1380. /* Jump back to real return address */
  1381. blr
  1382. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1383. #endif /* CONFIG_FUNCTION_TRACER */