entry_64.S 37 KB

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