entry_64.S 36 KB

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