entry_64.S 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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 <asm/unistd.h>
  22. #include <asm/processor.h>
  23. #include <asm/page.h>
  24. #include <asm/mmu.h>
  25. #include <asm/thread_info.h>
  26. #include <asm/ppc_asm.h>
  27. #include <asm/asm-offsets.h>
  28. #include <asm/cputable.h>
  29. #include <asm/firmware.h>
  30. #include <asm/bug.h>
  31. #include <asm/ptrace.h>
  32. #include <asm/irqflags.h>
  33. #include <asm/ftrace.h>
  34. #include <asm/hw_irq.h>
  35. /*
  36. * System calls.
  37. */
  38. .section ".toc","aw"
  39. .SYS_CALL_TABLE:
  40. .tc .sys_call_table[TC],.sys_call_table
  41. /* This value is used to mark exception frames on the stack. */
  42. exception_marker:
  43. .tc ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
  44. .section ".text"
  45. .align 7
  46. #undef SHOW_SYSCALLS
  47. .globl system_call_common
  48. system_call_common:
  49. andi. r10,r12,MSR_PR
  50. mr r10,r1
  51. addi r1,r1,-INT_FRAME_SIZE
  52. beq- 1f
  53. ld r1,PACAKSAVE(r13)
  54. 1: std r10,0(r1)
  55. std r11,_NIP(r1)
  56. std r12,_MSR(r1)
  57. std r0,GPR0(r1)
  58. std r10,GPR1(r1)
  59. ACCOUNT_CPU_USER_ENTRY(r10, r11)
  60. std r2,GPR2(r1)
  61. std r3,GPR3(r1)
  62. mfcr r2
  63. std r4,GPR4(r1)
  64. std r5,GPR5(r1)
  65. std r6,GPR6(r1)
  66. std r7,GPR7(r1)
  67. std r8,GPR8(r1)
  68. li r11,0
  69. std r11,GPR9(r1)
  70. std r11,GPR10(r1)
  71. std r11,GPR11(r1)
  72. std r11,GPR12(r1)
  73. std r11,_XER(r1)
  74. std r11,_CTR(r1)
  75. std r9,GPR13(r1)
  76. mflr r10
  77. /*
  78. * This clears CR0.SO (bit 28), which is the error indication on
  79. * return from this system call.
  80. */
  81. rldimi r2,r11,28,(63-28)
  82. li r11,0xc01
  83. std r10,_LINK(r1)
  84. std r11,_TRAP(r1)
  85. std r3,ORIG_GPR3(r1)
  86. std r2,_CCR(r1)
  87. ld r2,PACATOC(r13)
  88. addi r9,r1,STACK_FRAME_OVERHEAD
  89. ld r11,exception_marker@toc(r2)
  90. std r11,-16(r9) /* "regshere" marker */
  91. #if defined(CONFIG_VIRT_CPU_ACCOUNTING) && defined(CONFIG_PPC_SPLPAR)
  92. BEGIN_FW_FTR_SECTION
  93. beq 33f
  94. /* if from user, see if there are any DTL entries to process */
  95. ld r10,PACALPPACAPTR(r13) /* get ptr to VPA */
  96. ld r11,PACA_DTL_RIDX(r13) /* get log read index */
  97. ld r10,LPPACA_DTLIDX(r10) /* get log write index */
  98. cmpd cr1,r11,r10
  99. beq+ cr1,33f
  100. bl .accumulate_stolen_time
  101. REST_GPR(0,r1)
  102. REST_4GPRS(3,r1)
  103. REST_2GPRS(7,r1)
  104. addi r9,r1,STACK_FRAME_OVERHEAD
  105. 33:
  106. END_FW_FTR_SECTION_IFSET(FW_FEATURE_SPLPAR)
  107. #endif /* CONFIG_VIRT_CPU_ACCOUNTING && CONFIG_PPC_SPLPAR */
  108. /*
  109. * A syscall should always be called with interrupts enabled
  110. * so we just unconditionally hard-enable here. When some kind
  111. * of irq tracing is used, we additionally check that condition
  112. * is correct
  113. */
  114. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_BUG)
  115. lbz r10,PACASOFTIRQEN(r13)
  116. xori r10,r10,1
  117. 1: tdnei r10,0
  118. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
  119. #endif
  120. #ifdef CONFIG_PPC_BOOK3E
  121. wrteei 1
  122. #else
  123. ld r11,PACAKMSR(r13)
  124. ori r11,r11,MSR_EE
  125. mtmsrd r11,1
  126. #endif /* CONFIG_PPC_BOOK3E */
  127. /* We do need to set SOFTE in the stack frame or the return
  128. * from interrupt will be painful
  129. */
  130. li r10,1
  131. std r10,SOFTE(r1)
  132. #ifdef SHOW_SYSCALLS
  133. bl .do_show_syscall
  134. REST_GPR(0,r1)
  135. REST_4GPRS(3,r1)
  136. REST_2GPRS(7,r1)
  137. addi r9,r1,STACK_FRAME_OVERHEAD
  138. #endif
  139. CURRENT_THREAD_INFO(r11, r1)
  140. ld r10,TI_FLAGS(r11)
  141. andi. r11,r10,_TIF_SYSCALL_T_OR_A
  142. bne- syscall_dotrace
  143. .Lsyscall_dotrace_cont:
  144. cmpldi 0,r0,NR_syscalls
  145. bge- syscall_enosys
  146. system_call: /* label this so stack traces look sane */
  147. /*
  148. * Need to vector to 32 Bit or default sys_call_table here,
  149. * based on caller's run-mode / personality.
  150. */
  151. ld r11,.SYS_CALL_TABLE@toc(2)
  152. andi. r10,r10,_TIF_32BIT
  153. beq 15f
  154. addi r11,r11,8 /* use 32-bit syscall entries */
  155. clrldi r3,r3,32
  156. clrldi r4,r4,32
  157. clrldi r5,r5,32
  158. clrldi r6,r6,32
  159. clrldi r7,r7,32
  160. clrldi r8,r8,32
  161. 15:
  162. slwi r0,r0,4
  163. ldx r10,r11,r0 /* Fetch system call handler [ptr] */
  164. mtctr r10
  165. bctrl /* Call handler */
  166. syscall_exit:
  167. std r3,RESULT(r1)
  168. #ifdef SHOW_SYSCALLS
  169. bl .do_show_syscall_exit
  170. ld r3,RESULT(r1)
  171. #endif
  172. CURRENT_THREAD_INFO(r12, r1)
  173. ld r8,_MSR(r1)
  174. #ifdef CONFIG_PPC_BOOK3S
  175. /* No MSR:RI on BookE */
  176. andi. r10,r8,MSR_RI
  177. beq- unrecov_restore
  178. #endif
  179. /*
  180. * Disable interrupts so current_thread_info()->flags can't change,
  181. * and so that we don't get interrupted after loading SRR0/1.
  182. */
  183. #ifdef CONFIG_PPC_BOOK3E
  184. wrteei 0
  185. #else
  186. ld r10,PACAKMSR(r13)
  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 r9,MSR_RI
  195. andc r11,r10,r9
  196. mtmsrd r11,1
  197. #endif /* CONFIG_PPC_BOOK3E */
  198. ld r9,TI_FLAGS(r12)
  199. li r11,-_LAST_ERRNO
  200. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
  201. bne- syscall_exit_work
  202. cmpld r3,r11
  203. ld r5,_CCR(r1)
  204. bge- syscall_error
  205. .Lsyscall_error_cont:
  206. ld r7,_NIP(r1)
  207. BEGIN_FTR_SECTION
  208. stdcx. r0,0,r1 /* to clear the reservation */
  209. END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  210. andi. r6,r8,MSR_PR
  211. ld r4,_LINK(r1)
  212. beq- 1f
  213. ACCOUNT_CPU_USER_EXIT(r11, r12)
  214. ld r13,GPR13(r1) /* only restore r13 if returning to usermode */
  215. 1: ld r2,GPR2(r1)
  216. ld r1,GPR1(r1)
  217. mtlr r4
  218. mtcr r5
  219. mtspr SPRN_SRR0,r7
  220. mtspr SPRN_SRR1,r8
  221. RFI
  222. b . /* prevent speculative execution */
  223. syscall_error:
  224. oris r5,r5,0x1000 /* Set SO bit in CR */
  225. neg r3,r3
  226. std r5,_CCR(r1)
  227. b .Lsyscall_error_cont
  228. /* Traced system call support */
  229. syscall_dotrace:
  230. bl .save_nvgprs
  231. addi r3,r1,STACK_FRAME_OVERHEAD
  232. bl .do_syscall_trace_enter
  233. /*
  234. * Restore argument registers possibly just changed.
  235. * We use the return value of do_syscall_trace_enter
  236. * for the call number to look up in the table (r0).
  237. */
  238. mr r0,r3
  239. ld r3,GPR3(r1)
  240. ld r4,GPR4(r1)
  241. ld r5,GPR5(r1)
  242. ld r6,GPR6(r1)
  243. ld r7,GPR7(r1)
  244. ld r8,GPR8(r1)
  245. addi r9,r1,STACK_FRAME_OVERHEAD
  246. CURRENT_THREAD_INFO(r10, r1)
  247. ld r10,TI_FLAGS(r10)
  248. b .Lsyscall_dotrace_cont
  249. syscall_enosys:
  250. li r3,-ENOSYS
  251. b syscall_exit
  252. syscall_exit_work:
  253. #ifdef CONFIG_PPC_BOOK3S
  254. mtmsrd r10,1 /* Restore RI */
  255. #endif
  256. /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
  257. If TIF_NOERROR is set, just save r3 as it is. */
  258. andi. r0,r9,_TIF_RESTOREALL
  259. beq+ 0f
  260. REST_NVGPRS(r1)
  261. b 2f
  262. 0: cmpld r3,r11 /* r10 is -LAST_ERRNO */
  263. blt+ 1f
  264. andi. r0,r9,_TIF_NOERROR
  265. bne- 1f
  266. ld r5,_CCR(r1)
  267. neg r3,r3
  268. oris r5,r5,0x1000 /* Set SO bit in CR */
  269. std r5,_CCR(r1)
  270. 1: std r3,GPR3(r1)
  271. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  272. beq 4f
  273. /* Clear per-syscall TIF flags if any are set. */
  274. li r11,_TIF_PERSYSCALL_MASK
  275. addi r12,r12,TI_FLAGS
  276. 3: ldarx r10,0,r12
  277. andc r10,r10,r11
  278. stdcx. r10,0,r12
  279. bne- 3b
  280. subi r12,r12,TI_FLAGS
  281. 4: /* Anything else left to do? */
  282. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
  283. beq .ret_from_except_lite
  284. /* Re-enable interrupts */
  285. #ifdef CONFIG_PPC_BOOK3E
  286. wrteei 1
  287. #else
  288. ld r10,PACAKMSR(r13)
  289. ori r10,r10,MSR_EE
  290. mtmsrd r10,1
  291. #endif /* CONFIG_PPC_BOOK3E */
  292. bl .save_nvgprs
  293. addi r3,r1,STACK_FRAME_OVERHEAD
  294. bl .do_syscall_trace_leave
  295. b .ret_from_except
  296. /* Save non-volatile GPRs, if not already saved. */
  297. _GLOBAL(save_nvgprs)
  298. ld r11,_TRAP(r1)
  299. andi. r0,r11,1
  300. beqlr-
  301. SAVE_NVGPRS(r1)
  302. clrrdi r0,r11,1
  303. std r0,_TRAP(r1)
  304. blr
  305. /*
  306. * The sigsuspend and rt_sigsuspend system calls can call do_signal
  307. * and thus put the process into the stopped state where we might
  308. * want to examine its user state with ptrace. Therefore we need
  309. * to save all the nonvolatile registers (r14 - r31) before calling
  310. * the C code. Similarly, fork, vfork and clone need the full
  311. * register state on the stack so that it can be copied to the child.
  312. */
  313. _GLOBAL(ppc_fork)
  314. bl .save_nvgprs
  315. bl .sys_fork
  316. b syscall_exit
  317. _GLOBAL(ppc_vfork)
  318. bl .save_nvgprs
  319. bl .sys_vfork
  320. b syscall_exit
  321. _GLOBAL(ppc_clone)
  322. bl .save_nvgprs
  323. bl .sys_clone
  324. b syscall_exit
  325. _GLOBAL(ppc32_swapcontext)
  326. bl .save_nvgprs
  327. bl .compat_sys_swapcontext
  328. b syscall_exit
  329. _GLOBAL(ppc64_swapcontext)
  330. bl .save_nvgprs
  331. bl .sys_swapcontext
  332. b syscall_exit
  333. _GLOBAL(ret_from_fork)
  334. bl .schedule_tail
  335. REST_NVGPRS(r1)
  336. li r3,0
  337. b syscall_exit
  338. _GLOBAL(ret_from_kernel_thread)
  339. bl .schedule_tail
  340. REST_NVGPRS(r1)
  341. REST_GPR(2,r1)
  342. mtlr r14
  343. mr r3,r15
  344. blrl
  345. li r3,0
  346. b .do_exit # no return
  347. .section ".toc","aw"
  348. DSCR_DEFAULT:
  349. .tc dscr_default[TC],dscr_default
  350. .section ".text"
  351. /*
  352. * This routine switches between two different tasks. The process
  353. * state of one is saved on its kernel stack. Then the state
  354. * of the other is restored from its kernel stack. The memory
  355. * management hardware is updated to the second process's state.
  356. * Finally, we can return to the second process, via ret_from_except.
  357. * On entry, r3 points to the THREAD for the current task, r4
  358. * points to the THREAD for the new task.
  359. *
  360. * Note: there are two ways to get to the "going out" portion
  361. * of this code; either by coming in via the entry (_switch)
  362. * or via "fork" which must set up an environment equivalent
  363. * to the "_switch" path. If you change this you'll have to change
  364. * the fork code also.
  365. *
  366. * The code which creates the new task context is in 'copy_thread'
  367. * in arch/powerpc/kernel/process.c
  368. */
  369. .align 7
  370. _GLOBAL(_switch)
  371. mflr r0
  372. std r0,16(r1)
  373. stdu r1,-SWITCH_FRAME_SIZE(r1)
  374. /* r3-r13 are caller saved -- Cort */
  375. SAVE_8GPRS(14, r1)
  376. SAVE_10GPRS(22, r1)
  377. mflr r20 /* Return to switch caller */
  378. mfmsr r22
  379. li r0, MSR_FP
  380. #ifdef CONFIG_VSX
  381. BEGIN_FTR_SECTION
  382. oris r0,r0,MSR_VSX@h /* Disable VSX */
  383. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  384. #endif /* CONFIG_VSX */
  385. #ifdef CONFIG_ALTIVEC
  386. BEGIN_FTR_SECTION
  387. oris r0,r0,MSR_VEC@h /* Disable altivec */
  388. mfspr r24,SPRN_VRSAVE /* save vrsave register value */
  389. std r24,THREAD_VRSAVE(r3)
  390. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  391. #endif /* CONFIG_ALTIVEC */
  392. #ifdef CONFIG_PPC64
  393. BEGIN_FTR_SECTION
  394. mfspr r25,SPRN_DSCR
  395. std r25,THREAD_DSCR(r3)
  396. END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
  397. #endif
  398. and. r0,r0,r22
  399. beq+ 1f
  400. andc r22,r22,r0
  401. MTMSRD(r22)
  402. isync
  403. 1: std r20,_NIP(r1)
  404. mfcr r23
  405. std r23,_CCR(r1)
  406. std r1,KSP(r3) /* Set old stack pointer */
  407. #ifdef CONFIG_SMP
  408. /* We need a sync somewhere here to make sure that if the
  409. * previous task gets rescheduled on another CPU, it sees all
  410. * stores it has performed on this one.
  411. */
  412. sync
  413. #endif /* CONFIG_SMP */
  414. /*
  415. * If we optimise away the clear of the reservation in system
  416. * calls because we know the CPU tracks the address of the
  417. * reservation, then we need to clear it here to cover the
  418. * case that the kernel context switch path has no larx
  419. * instructions.
  420. */
  421. BEGIN_FTR_SECTION
  422. ldarx r6,0,r1
  423. END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
  424. addi r6,r4,-THREAD /* Convert THREAD to 'current' */
  425. std r6,PACACURRENT(r13) /* Set new 'current' */
  426. ld r8,KSP(r4) /* new stack pointer */
  427. #ifdef CONFIG_PPC_BOOK3S
  428. BEGIN_FTR_SECTION
  429. BEGIN_FTR_SECTION_NESTED(95)
  430. clrrdi r6,r8,28 /* get its ESID */
  431. clrrdi r9,r1,28 /* get current sp ESID */
  432. FTR_SECTION_ELSE_NESTED(95)
  433. clrrdi r6,r8,40 /* get its 1T ESID */
  434. clrrdi r9,r1,40 /* get current sp 1T ESID */
  435. ALT_MMU_FTR_SECTION_END_NESTED_IFCLR(MMU_FTR_1T_SEGMENT, 95)
  436. FTR_SECTION_ELSE
  437. b 2f
  438. ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_SLB)
  439. clrldi. r0,r6,2 /* is new ESID c00000000? */
  440. cmpd cr1,r6,r9 /* or is new ESID the same as current ESID? */
  441. cror eq,4*cr1+eq,eq
  442. beq 2f /* if yes, don't slbie it */
  443. /* Bolt in the new stack SLB entry */
  444. ld r7,KSP_VSID(r4) /* Get new stack's VSID */
  445. oris r0,r6,(SLB_ESID_V)@h
  446. ori r0,r0,(SLB_NUM_BOLTED-1)@l
  447. BEGIN_FTR_SECTION
  448. li r9,MMU_SEGSIZE_1T /* insert B field */
  449. oris r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
  450. rldimi r7,r9,SLB_VSID_SSIZE_SHIFT,0
  451. END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
  452. /* Update the last bolted SLB. No write barriers are needed
  453. * here, provided we only update the current CPU's SLB shadow
  454. * buffer.
  455. */
  456. ld r9,PACA_SLBSHADOWPTR(r13)
  457. li r12,0
  458. std r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
  459. std r7,SLBSHADOW_STACKVSID(r9) /* Save VSID */
  460. std r0,SLBSHADOW_STACKESID(r9) /* Save ESID */
  461. /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
  462. * we have 1TB segments, the only CPUs known to have the errata
  463. * only support less than 1TB of system memory and we'll never
  464. * actually hit this code path.
  465. */
  466. slbie r6
  467. slbie r6 /* Workaround POWER5 < DD2.1 issue */
  468. slbmte r7,r0
  469. isync
  470. 2:
  471. #endif /* !CONFIG_PPC_BOOK3S */
  472. CURRENT_THREAD_INFO(r7, r8) /* base of new stack */
  473. /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
  474. because we don't need to leave the 288-byte ABI gap at the
  475. top of the kernel stack. */
  476. addi r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
  477. mr r1,r8 /* start using new stack pointer */
  478. std r7,PACAKSAVE(r13)
  479. #ifdef CONFIG_ALTIVEC
  480. BEGIN_FTR_SECTION
  481. ld r0,THREAD_VRSAVE(r4)
  482. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  483. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  484. #endif /* CONFIG_ALTIVEC */
  485. #ifdef CONFIG_PPC64
  486. BEGIN_FTR_SECTION
  487. lwz r6,THREAD_DSCR_INHERIT(r4)
  488. ld r7,DSCR_DEFAULT@toc(2)
  489. ld r0,THREAD_DSCR(r4)
  490. cmpwi r6,0
  491. bne 1f
  492. ld r0,0(r7)
  493. 1: cmpd r0,r25
  494. beq 2f
  495. mtspr SPRN_DSCR,r0
  496. 2:
  497. END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
  498. #endif
  499. ld r6,_CCR(r1)
  500. mtcrf 0xFF,r6
  501. /* r3-r13 are destroyed -- Cort */
  502. REST_8GPRS(14, r1)
  503. REST_10GPRS(22, r1)
  504. /* convert old thread to its task_struct for return value */
  505. addi r3,r3,-THREAD
  506. ld r7,_NIP(r1) /* Return to _switch caller in new task */
  507. mtlr r7
  508. addi r1,r1,SWITCH_FRAME_SIZE
  509. blr
  510. .align 7
  511. _GLOBAL(ret_from_except)
  512. ld r11,_TRAP(r1)
  513. andi. r0,r11,1
  514. bne .ret_from_except_lite
  515. REST_NVGPRS(r1)
  516. _GLOBAL(ret_from_except_lite)
  517. /*
  518. * Disable interrupts so that current_thread_info()->flags
  519. * can't change between when we test it and when we return
  520. * from the interrupt.
  521. */
  522. #ifdef CONFIG_PPC_BOOK3E
  523. wrteei 0
  524. #else
  525. ld r10,PACAKMSR(r13) /* Get kernel MSR without EE */
  526. mtmsrd r10,1 /* Update machine state */
  527. #endif /* CONFIG_PPC_BOOK3E */
  528. CURRENT_THREAD_INFO(r9, r1)
  529. ld r3,_MSR(r1)
  530. ld r4,TI_FLAGS(r9)
  531. andi. r3,r3,MSR_PR
  532. beq resume_kernel
  533. /* Check current_thread_info()->flags */
  534. andi. r0,r4,_TIF_USER_WORK_MASK
  535. beq restore
  536. andi. r0,r4,_TIF_NEED_RESCHED
  537. beq 1f
  538. bl .restore_interrupts
  539. bl .schedule
  540. b .ret_from_except_lite
  541. 1: bl .save_nvgprs
  542. bl .restore_interrupts
  543. addi r3,r1,STACK_FRAME_OVERHEAD
  544. bl .do_notify_resume
  545. b .ret_from_except
  546. resume_kernel:
  547. #ifdef CONFIG_PREEMPT
  548. /* Check if we need to preempt */
  549. andi. r0,r4,_TIF_NEED_RESCHED
  550. beq+ restore
  551. /* Check that preempt_count() == 0 and interrupts are enabled */
  552. lwz r8,TI_PREEMPT(r9)
  553. cmpwi cr1,r8,0
  554. ld r0,SOFTE(r1)
  555. cmpdi r0,0
  556. crandc eq,cr1*4+eq,eq
  557. bne restore
  558. /*
  559. * Here we are preempting the current task. We want to make
  560. * sure we are soft-disabled first
  561. */
  562. SOFT_DISABLE_INTS(r3,r4)
  563. 1: bl .preempt_schedule_irq
  564. /* Re-test flags and eventually loop */
  565. CURRENT_THREAD_INFO(r9, r1)
  566. ld r4,TI_FLAGS(r9)
  567. andi. r0,r4,_TIF_NEED_RESCHED
  568. bne 1b
  569. #endif /* CONFIG_PREEMPT */
  570. .globl fast_exc_return_irq
  571. fast_exc_return_irq:
  572. restore:
  573. /*
  574. * This is the main kernel exit path. First we check if we
  575. * are about to re-enable interrupts
  576. */
  577. ld r5,SOFTE(r1)
  578. lbz r6,PACASOFTIRQEN(r13)
  579. cmpwi cr0,r5,0
  580. beq restore_irq_off
  581. /* We are enabling, were we already enabled ? Yes, just return */
  582. cmpwi cr0,r6,1
  583. beq cr0,do_restore
  584. /*
  585. * We are about to soft-enable interrupts (we are hard disabled
  586. * at this point). We check if there's anything that needs to
  587. * be replayed first.
  588. */
  589. lbz r0,PACAIRQHAPPENED(r13)
  590. cmpwi cr0,r0,0
  591. bne- restore_check_irq_replay
  592. /*
  593. * Get here when nothing happened while soft-disabled, just
  594. * soft-enable and move-on. We will hard-enable as a side
  595. * effect of rfi
  596. */
  597. restore_no_replay:
  598. TRACE_ENABLE_INTS
  599. li r0,1
  600. stb r0,PACASOFTIRQEN(r13);
  601. /*
  602. * Final return path. BookE is handled in a different file
  603. */
  604. do_restore:
  605. #ifdef CONFIG_PPC_BOOK3E
  606. b .exception_return_book3e
  607. #else
  608. /*
  609. * Clear the reservation. If we know the CPU tracks the address of
  610. * the reservation then we can potentially save some cycles and use
  611. * a larx. On POWER6 and POWER7 this is significantly faster.
  612. */
  613. BEGIN_FTR_SECTION
  614. stdcx. r0,0,r1 /* to clear the reservation */
  615. FTR_SECTION_ELSE
  616. ldarx r4,0,r1
  617. ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  618. /*
  619. * Some code path such as load_up_fpu or altivec return directly
  620. * here. They run entirely hard disabled and do not alter the
  621. * interrupt state. They also don't use lwarx/stwcx. and thus
  622. * are known not to leave dangling reservations.
  623. */
  624. .globl fast_exception_return
  625. fast_exception_return:
  626. ld r3,_MSR(r1)
  627. ld r4,_CTR(r1)
  628. ld r0,_LINK(r1)
  629. mtctr r4
  630. mtlr r0
  631. ld r4,_XER(r1)
  632. mtspr SPRN_XER,r4
  633. REST_8GPRS(5, r1)
  634. andi. r0,r3,MSR_RI
  635. beq- unrecov_restore
  636. /*
  637. * Clear RI before restoring r13. If we are returning to
  638. * userspace and we take an exception after restoring r13,
  639. * we end up corrupting the userspace r13 value.
  640. */
  641. ld r4,PACAKMSR(r13) /* Get kernel MSR without EE */
  642. andc r4,r4,r0 /* r0 contains MSR_RI here */
  643. mtmsrd r4,1
  644. /*
  645. * r13 is our per cpu area, only restore it if we are returning to
  646. * userspace the value stored in the stack frame may belong to
  647. * another CPU.
  648. */
  649. andi. r0,r3,MSR_PR
  650. beq 1f
  651. ACCOUNT_CPU_USER_EXIT(r2, r4)
  652. REST_GPR(13, r1)
  653. 1:
  654. mtspr SPRN_SRR1,r3
  655. ld r2,_CCR(r1)
  656. mtcrf 0xFF,r2
  657. ld r2,_NIP(r1)
  658. mtspr SPRN_SRR0,r2
  659. ld r0,GPR0(r1)
  660. ld r2,GPR2(r1)
  661. ld r3,GPR3(r1)
  662. ld r4,GPR4(r1)
  663. ld r1,GPR1(r1)
  664. rfid
  665. b . /* prevent speculative execution */
  666. #endif /* CONFIG_PPC_BOOK3E */
  667. /*
  668. * We are returning to a context with interrupts soft disabled.
  669. *
  670. * However, we may also about to hard enable, so we need to
  671. * make sure that in this case, we also clear PACA_IRQ_HARD_DIS
  672. * or that bit can get out of sync and bad things will happen
  673. */
  674. restore_irq_off:
  675. ld r3,_MSR(r1)
  676. lbz r7,PACAIRQHAPPENED(r13)
  677. andi. r0,r3,MSR_EE
  678. beq 1f
  679. rlwinm r7,r7,0,~PACA_IRQ_HARD_DIS
  680. stb r7,PACAIRQHAPPENED(r13)
  681. 1: li r0,0
  682. stb r0,PACASOFTIRQEN(r13);
  683. TRACE_DISABLE_INTS
  684. b do_restore
  685. /*
  686. * Something did happen, check if a re-emit is needed
  687. * (this also clears paca->irq_happened)
  688. */
  689. restore_check_irq_replay:
  690. /* XXX: We could implement a fast path here where we check
  691. * for irq_happened being just 0x01, in which case we can
  692. * clear it and return. That means that we would potentially
  693. * miss a decrementer having wrapped all the way around.
  694. *
  695. * Still, this might be useful for things like hash_page
  696. */
  697. bl .__check_irq_replay
  698. cmpwi cr0,r3,0
  699. beq restore_no_replay
  700. /*
  701. * We need to re-emit an interrupt. We do so by re-using our
  702. * existing exception frame. We first change the trap value,
  703. * but we need to ensure we preserve the low nibble of it
  704. */
  705. ld r4,_TRAP(r1)
  706. clrldi r4,r4,60
  707. or r4,r4,r3
  708. std r4,_TRAP(r1)
  709. /*
  710. * Then find the right handler and call it. Interrupts are
  711. * still soft-disabled and we keep them that way.
  712. */
  713. cmpwi cr0,r3,0x500
  714. bne 1f
  715. addi r3,r1,STACK_FRAME_OVERHEAD;
  716. bl .do_IRQ
  717. b .ret_from_except
  718. 1: cmpwi cr0,r3,0x900
  719. bne 1f
  720. addi r3,r1,STACK_FRAME_OVERHEAD;
  721. bl .timer_interrupt
  722. b .ret_from_except
  723. #ifdef CONFIG_PPC_BOOK3E
  724. 1: cmpwi cr0,r3,0x280
  725. bne 1f
  726. addi r3,r1,STACK_FRAME_OVERHEAD;
  727. bl .doorbell_exception
  728. b .ret_from_except
  729. #endif /* CONFIG_PPC_BOOK3E */
  730. 1: b .ret_from_except /* What else to do here ? */
  731. unrecov_restore:
  732. addi r3,r1,STACK_FRAME_OVERHEAD
  733. bl .unrecoverable_exception
  734. b unrecov_restore
  735. #ifdef CONFIG_PPC_RTAS
  736. /*
  737. * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
  738. * called with the MMU off.
  739. *
  740. * In addition, we need to be in 32b mode, at least for now.
  741. *
  742. * Note: r3 is an input parameter to rtas, so don't trash it...
  743. */
  744. _GLOBAL(enter_rtas)
  745. mflr r0
  746. std r0,16(r1)
  747. stdu r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
  748. /* Because RTAS is running in 32b mode, it clobbers the high order half
  749. * of all registers that it saves. We therefore save those registers
  750. * RTAS might touch to the stack. (r0, r3-r13 are caller saved)
  751. */
  752. SAVE_GPR(2, r1) /* Save the TOC */
  753. SAVE_GPR(13, r1) /* Save paca */
  754. SAVE_8GPRS(14, r1) /* Save the non-volatiles */
  755. SAVE_10GPRS(22, r1) /* ditto */
  756. mfcr r4
  757. std r4,_CCR(r1)
  758. mfctr r5
  759. std r5,_CTR(r1)
  760. mfspr r6,SPRN_XER
  761. std r6,_XER(r1)
  762. mfdar r7
  763. std r7,_DAR(r1)
  764. mfdsisr r8
  765. std r8,_DSISR(r1)
  766. /* Temporary workaround to clear CR until RTAS can be modified to
  767. * ignore all bits.
  768. */
  769. li r0,0
  770. mtcr r0
  771. #ifdef CONFIG_BUG
  772. /* There is no way it is acceptable to get here with interrupts enabled,
  773. * check it with the asm equivalent of WARN_ON
  774. */
  775. lbz r0,PACASOFTIRQEN(r13)
  776. 1: tdnei r0,0
  777. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
  778. #endif
  779. /* Hard-disable interrupts */
  780. mfmsr r6
  781. rldicl r7,r6,48,1
  782. rotldi r7,r7,16
  783. mtmsrd r7,1
  784. /* Unfortunately, the stack pointer and the MSR are also clobbered,
  785. * so they are saved in the PACA which allows us to restore
  786. * our original state after RTAS returns.
  787. */
  788. std r1,PACAR1(r13)
  789. std r6,PACASAVEDMSR(r13)
  790. /* Setup our real return addr */
  791. LOAD_REG_ADDR(r4,.rtas_return_loc)
  792. clrldi r4,r4,2 /* convert to realmode address */
  793. mtlr r4
  794. li r0,0
  795. ori r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
  796. andc r0,r6,r0
  797. li r9,1
  798. rldicr r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
  799. ori r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI
  800. andc r6,r0,r9
  801. sync /* disable interrupts so SRR0/1 */
  802. mtmsrd r0 /* don't get trashed */
  803. LOAD_REG_ADDR(r4, rtas)
  804. ld r5,RTASENTRY(r4) /* get the rtas->entry value */
  805. ld r4,RTASBASE(r4) /* get the rtas->base value */
  806. mtspr SPRN_SRR0,r5
  807. mtspr SPRN_SRR1,r6
  808. rfid
  809. b . /* prevent speculative execution */
  810. _STATIC(rtas_return_loc)
  811. /* relocation is off at this point */
  812. GET_PACA(r4)
  813. clrldi r4,r4,2 /* convert to realmode address */
  814. bcl 20,31,$+4
  815. 0: mflr r3
  816. ld r3,(1f-0b)(r3) /* get &.rtas_restore_regs */
  817. mfmsr r6
  818. li r0,MSR_RI
  819. andc r6,r6,r0
  820. sync
  821. mtmsrd r6
  822. ld r1,PACAR1(r4) /* Restore our SP */
  823. ld r4,PACASAVEDMSR(r4) /* Restore our MSR */
  824. mtspr SPRN_SRR0,r3
  825. mtspr SPRN_SRR1,r4
  826. rfid
  827. b . /* prevent speculative execution */
  828. .align 3
  829. 1: .llong .rtas_restore_regs
  830. _STATIC(rtas_restore_regs)
  831. /* relocation is on at this point */
  832. REST_GPR(2, r1) /* Restore the TOC */
  833. REST_GPR(13, r1) /* Restore paca */
  834. REST_8GPRS(14, r1) /* Restore the non-volatiles */
  835. REST_10GPRS(22, r1) /* ditto */
  836. GET_PACA(r13)
  837. ld r4,_CCR(r1)
  838. mtcr r4
  839. ld r5,_CTR(r1)
  840. mtctr r5
  841. ld r6,_XER(r1)
  842. mtspr SPRN_XER,r6
  843. ld r7,_DAR(r1)
  844. mtdar r7
  845. ld r8,_DSISR(r1)
  846. mtdsisr r8
  847. addi r1,r1,RTAS_FRAME_SIZE /* Unstack our frame */
  848. ld r0,16(r1) /* get return address */
  849. mtlr r0
  850. blr /* return to caller */
  851. #endif /* CONFIG_PPC_RTAS */
  852. _GLOBAL(enter_prom)
  853. mflr r0
  854. std r0,16(r1)
  855. stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
  856. /* Because PROM is running in 32b mode, it clobbers the high order half
  857. * of all registers that it saves. We therefore save those registers
  858. * PROM might touch to the stack. (r0, r3-r13 are caller saved)
  859. */
  860. SAVE_GPR(2, r1)
  861. SAVE_GPR(13, r1)
  862. SAVE_8GPRS(14, r1)
  863. SAVE_10GPRS(22, r1)
  864. mfcr r10
  865. mfmsr r11
  866. std r10,_CCR(r1)
  867. std r11,_MSR(r1)
  868. /* Get the PROM entrypoint */
  869. mtlr r4
  870. /* Switch MSR to 32 bits mode
  871. */
  872. #ifdef CONFIG_PPC_BOOK3E
  873. rlwinm r11,r11,0,1,31
  874. mtmsr r11
  875. #else /* CONFIG_PPC_BOOK3E */
  876. mfmsr r11
  877. li r12,1
  878. rldicr r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
  879. andc r11,r11,r12
  880. li r12,1
  881. rldicr r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
  882. andc r11,r11,r12
  883. mtmsrd r11
  884. #endif /* CONFIG_PPC_BOOK3E */
  885. isync
  886. /* Enter PROM here... */
  887. blrl
  888. /* Just make sure that r1 top 32 bits didn't get
  889. * corrupt by OF
  890. */
  891. rldicl r1,r1,0,32
  892. /* Restore the MSR (back to 64 bits) */
  893. ld r0,_MSR(r1)
  894. MTMSRD(r0)
  895. isync
  896. /* Restore other registers */
  897. REST_GPR(2, r1)
  898. REST_GPR(13, r1)
  899. REST_8GPRS(14, r1)
  900. REST_10GPRS(22, r1)
  901. ld r4,_CCR(r1)
  902. mtcr r4
  903. addi r1,r1,PROM_FRAME_SIZE
  904. ld r0,16(r1)
  905. mtlr r0
  906. blr
  907. #ifdef CONFIG_FUNCTION_TRACER
  908. #ifdef CONFIG_DYNAMIC_FTRACE
  909. _GLOBAL(mcount)
  910. _GLOBAL(_mcount)
  911. blr
  912. _GLOBAL(ftrace_caller)
  913. /* Taken from output of objdump from lib64/glibc */
  914. mflr r3
  915. ld r11, 0(r1)
  916. stdu r1, -112(r1)
  917. std r3, 128(r1)
  918. ld r4, 16(r11)
  919. subi r3, r3, MCOUNT_INSN_SIZE
  920. .globl ftrace_call
  921. ftrace_call:
  922. bl ftrace_stub
  923. nop
  924. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  925. .globl ftrace_graph_call
  926. ftrace_graph_call:
  927. b ftrace_graph_stub
  928. _GLOBAL(ftrace_graph_stub)
  929. #endif
  930. ld r0, 128(r1)
  931. mtlr r0
  932. addi r1, r1, 112
  933. _GLOBAL(ftrace_stub)
  934. blr
  935. #else
  936. _GLOBAL(mcount)
  937. blr
  938. _GLOBAL(_mcount)
  939. /* Taken from output of objdump from lib64/glibc */
  940. mflr r3
  941. ld r11, 0(r1)
  942. stdu r1, -112(r1)
  943. std r3, 128(r1)
  944. ld r4, 16(r11)
  945. subi r3, r3, MCOUNT_INSN_SIZE
  946. LOAD_REG_ADDR(r5,ftrace_trace_function)
  947. ld r5,0(r5)
  948. ld r5,0(r5)
  949. mtctr r5
  950. bctrl
  951. nop
  952. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  953. b ftrace_graph_caller
  954. #endif
  955. ld r0, 128(r1)
  956. mtlr r0
  957. addi r1, r1, 112
  958. _GLOBAL(ftrace_stub)
  959. blr
  960. #endif /* CONFIG_DYNAMIC_FTRACE */
  961. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  962. _GLOBAL(ftrace_graph_caller)
  963. /* load r4 with local address */
  964. ld r4, 128(r1)
  965. subi r4, r4, MCOUNT_INSN_SIZE
  966. /* get the parent address */
  967. ld r11, 112(r1)
  968. addi r3, r11, 16
  969. bl .prepare_ftrace_return
  970. nop
  971. ld r0, 128(r1)
  972. mtlr r0
  973. addi r1, r1, 112
  974. blr
  975. _GLOBAL(return_to_handler)
  976. /* need to save return values */
  977. std r4, -24(r1)
  978. std r3, -16(r1)
  979. std r31, -8(r1)
  980. mr r31, r1
  981. stdu r1, -112(r1)
  982. bl .ftrace_return_to_handler
  983. nop
  984. /* return value has real return address */
  985. mtlr r3
  986. ld r1, 0(r1)
  987. ld r4, -24(r1)
  988. ld r3, -16(r1)
  989. ld r31, -8(r1)
  990. /* Jump back to real return address */
  991. blr
  992. _GLOBAL(mod_return_to_handler)
  993. /* need to save return values */
  994. std r4, -32(r1)
  995. std r3, -24(r1)
  996. /* save TOC */
  997. std r2, -16(r1)
  998. std r31, -8(r1)
  999. mr r31, r1
  1000. stdu r1, -112(r1)
  1001. /*
  1002. * We are in a module using the module's TOC.
  1003. * Switch to our TOC to run inside the core kernel.
  1004. */
  1005. ld r2, PACATOC(r13)
  1006. bl .ftrace_return_to_handler
  1007. nop
  1008. /* return value has real return address */
  1009. mtlr r3
  1010. ld r1, 0(r1)
  1011. ld r4, -32(r1)
  1012. ld r3, -24(r1)
  1013. ld r2, -16(r1)
  1014. ld r31, -8(r1)
  1015. /* Jump back to real return address */
  1016. blr
  1017. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1018. #endif /* CONFIG_FUNCTION_TRACER */