entry_64.S 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. /*
  339. * This routine switches between two different tasks. The process
  340. * state of one is saved on its kernel stack. Then the state
  341. * of the other is restored from its kernel stack. The memory
  342. * management hardware is updated to the second process's state.
  343. * Finally, we can return to the second process, via ret_from_except.
  344. * On entry, r3 points to the THREAD for the current task, r4
  345. * points to the THREAD for the new task.
  346. *
  347. * Note: there are two ways to get to the "going out" portion
  348. * of this code; either by coming in via the entry (_switch)
  349. * or via "fork" which must set up an environment equivalent
  350. * to the "_switch" path. If you change this you'll have to change
  351. * the fork code also.
  352. *
  353. * The code which creates the new task context is in 'copy_thread'
  354. * in arch/powerpc/kernel/process.c
  355. */
  356. .align 7
  357. _GLOBAL(_switch)
  358. mflr r0
  359. std r0,16(r1)
  360. stdu r1,-SWITCH_FRAME_SIZE(r1)
  361. /* r3-r13 are caller saved -- Cort */
  362. SAVE_8GPRS(14, r1)
  363. SAVE_10GPRS(22, r1)
  364. mflr r20 /* Return to switch caller */
  365. mfmsr r22
  366. li r0, MSR_FP
  367. #ifdef CONFIG_VSX
  368. BEGIN_FTR_SECTION
  369. oris r0,r0,MSR_VSX@h /* Disable VSX */
  370. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  371. #endif /* CONFIG_VSX */
  372. #ifdef CONFIG_ALTIVEC
  373. BEGIN_FTR_SECTION
  374. oris r0,r0,MSR_VEC@h /* Disable altivec */
  375. mfspr r24,SPRN_VRSAVE /* save vrsave register value */
  376. std r24,THREAD_VRSAVE(r3)
  377. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  378. #endif /* CONFIG_ALTIVEC */
  379. #ifdef CONFIG_PPC64
  380. BEGIN_FTR_SECTION
  381. mfspr r25,SPRN_DSCR
  382. std r25,THREAD_DSCR(r3)
  383. END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
  384. #endif
  385. and. r0,r0,r22
  386. beq+ 1f
  387. andc r22,r22,r0
  388. MTMSRD(r22)
  389. isync
  390. 1: std r20,_NIP(r1)
  391. mfcr r23
  392. std r23,_CCR(r1)
  393. std r1,KSP(r3) /* Set old stack pointer */
  394. #ifdef CONFIG_SMP
  395. /* We need a sync somewhere here to make sure that if the
  396. * previous task gets rescheduled on another CPU, it sees all
  397. * stores it has performed on this one.
  398. */
  399. sync
  400. #endif /* CONFIG_SMP */
  401. /*
  402. * If we optimise away the clear of the reservation in system
  403. * calls because we know the CPU tracks the address of the
  404. * reservation, then we need to clear it here to cover the
  405. * case that the kernel context switch path has no larx
  406. * instructions.
  407. */
  408. BEGIN_FTR_SECTION
  409. ldarx r6,0,r1
  410. END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
  411. addi r6,r4,-THREAD /* Convert THREAD to 'current' */
  412. std r6,PACACURRENT(r13) /* Set new 'current' */
  413. ld r8,KSP(r4) /* new stack pointer */
  414. #ifdef CONFIG_PPC_BOOK3S
  415. BEGIN_FTR_SECTION
  416. BEGIN_FTR_SECTION_NESTED(95)
  417. clrrdi r6,r8,28 /* get its ESID */
  418. clrrdi r9,r1,28 /* get current sp ESID */
  419. FTR_SECTION_ELSE_NESTED(95)
  420. clrrdi r6,r8,40 /* get its 1T ESID */
  421. clrrdi r9,r1,40 /* get current sp 1T ESID */
  422. ALT_MMU_FTR_SECTION_END_NESTED_IFCLR(MMU_FTR_1T_SEGMENT, 95)
  423. FTR_SECTION_ELSE
  424. b 2f
  425. ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_SLB)
  426. clrldi. r0,r6,2 /* is new ESID c00000000? */
  427. cmpd cr1,r6,r9 /* or is new ESID the same as current ESID? */
  428. cror eq,4*cr1+eq,eq
  429. beq 2f /* if yes, don't slbie it */
  430. /* Bolt in the new stack SLB entry */
  431. ld r7,KSP_VSID(r4) /* Get new stack's VSID */
  432. oris r0,r6,(SLB_ESID_V)@h
  433. ori r0,r0,(SLB_NUM_BOLTED-1)@l
  434. BEGIN_FTR_SECTION
  435. li r9,MMU_SEGSIZE_1T /* insert B field */
  436. oris r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
  437. rldimi r7,r9,SLB_VSID_SSIZE_SHIFT,0
  438. END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
  439. /* Update the last bolted SLB. No write barriers are needed
  440. * here, provided we only update the current CPU's SLB shadow
  441. * buffer.
  442. */
  443. ld r9,PACA_SLBSHADOWPTR(r13)
  444. li r12,0
  445. std r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
  446. std r7,SLBSHADOW_STACKVSID(r9) /* Save VSID */
  447. std r0,SLBSHADOW_STACKESID(r9) /* Save ESID */
  448. /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
  449. * we have 1TB segments, the only CPUs known to have the errata
  450. * only support less than 1TB of system memory and we'll never
  451. * actually hit this code path.
  452. */
  453. slbie r6
  454. slbie r6 /* Workaround POWER5 < DD2.1 issue */
  455. slbmte r7,r0
  456. isync
  457. 2:
  458. #endif /* !CONFIG_PPC_BOOK3S */
  459. CURRENT_THREAD_INFO(r7, r8) /* base of new stack */
  460. /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
  461. because we don't need to leave the 288-byte ABI gap at the
  462. top of the kernel stack. */
  463. addi r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
  464. mr r1,r8 /* start using new stack pointer */
  465. std r7,PACAKSAVE(r13)
  466. ld r6,_CCR(r1)
  467. mtcrf 0xFF,r6
  468. #ifdef CONFIG_ALTIVEC
  469. BEGIN_FTR_SECTION
  470. ld r0,THREAD_VRSAVE(r4)
  471. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  472. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  473. #endif /* CONFIG_ALTIVEC */
  474. #ifdef CONFIG_PPC64
  475. BEGIN_FTR_SECTION
  476. ld r0,THREAD_DSCR(r4)
  477. cmpd r0,r25
  478. beq 1f
  479. mtspr SPRN_DSCR,r0
  480. 1:
  481. END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
  482. #endif
  483. /* r3-r13 are destroyed -- Cort */
  484. REST_8GPRS(14, r1)
  485. REST_10GPRS(22, r1)
  486. /* convert old thread to its task_struct for return value */
  487. addi r3,r3,-THREAD
  488. ld r7,_NIP(r1) /* Return to _switch caller in new task */
  489. mtlr r7
  490. addi r1,r1,SWITCH_FRAME_SIZE
  491. blr
  492. .align 7
  493. _GLOBAL(ret_from_except)
  494. ld r11,_TRAP(r1)
  495. andi. r0,r11,1
  496. bne .ret_from_except_lite
  497. REST_NVGPRS(r1)
  498. _GLOBAL(ret_from_except_lite)
  499. /*
  500. * Disable interrupts so that current_thread_info()->flags
  501. * can't change between when we test it and when we return
  502. * from the interrupt.
  503. */
  504. #ifdef CONFIG_PPC_BOOK3E
  505. wrteei 0
  506. #else
  507. ld r10,PACAKMSR(r13) /* Get kernel MSR without EE */
  508. mtmsrd r10,1 /* Update machine state */
  509. #endif /* CONFIG_PPC_BOOK3E */
  510. CURRENT_THREAD_INFO(r9, r1)
  511. ld r3,_MSR(r1)
  512. ld r4,TI_FLAGS(r9)
  513. andi. r3,r3,MSR_PR
  514. beq resume_kernel
  515. /* Check current_thread_info()->flags */
  516. andi. r0,r4,_TIF_USER_WORK_MASK
  517. beq restore
  518. andi. r0,r4,_TIF_NEED_RESCHED
  519. beq 1f
  520. bl .restore_interrupts
  521. bl .schedule
  522. b .ret_from_except_lite
  523. 1: bl .save_nvgprs
  524. bl .restore_interrupts
  525. addi r3,r1,STACK_FRAME_OVERHEAD
  526. bl .do_notify_resume
  527. b .ret_from_except
  528. resume_kernel:
  529. #ifdef CONFIG_PREEMPT
  530. /* Check if we need to preempt */
  531. andi. r0,r4,_TIF_NEED_RESCHED
  532. beq+ restore
  533. /* Check that preempt_count() == 0 and interrupts are enabled */
  534. lwz r8,TI_PREEMPT(r9)
  535. cmpwi cr1,r8,0
  536. ld r0,SOFTE(r1)
  537. cmpdi r0,0
  538. crandc eq,cr1*4+eq,eq
  539. bne restore
  540. /*
  541. * Here we are preempting the current task. We want to make
  542. * sure we are soft-disabled first
  543. */
  544. SOFT_DISABLE_INTS(r3,r4)
  545. 1: bl .preempt_schedule_irq
  546. /* Re-test flags and eventually loop */
  547. CURRENT_THREAD_INFO(r9, r1)
  548. ld r4,TI_FLAGS(r9)
  549. andi. r0,r4,_TIF_NEED_RESCHED
  550. bne 1b
  551. #endif /* CONFIG_PREEMPT */
  552. .globl fast_exc_return_irq
  553. fast_exc_return_irq:
  554. restore:
  555. /*
  556. * This is the main kernel exit path. First we check if we
  557. * are about to re-enable interrupts
  558. */
  559. ld r5,SOFTE(r1)
  560. lbz r6,PACASOFTIRQEN(r13)
  561. cmpwi cr0,r5,0
  562. beq restore_irq_off
  563. /* We are enabling, were we already enabled ? Yes, just return */
  564. cmpwi cr0,r6,1
  565. beq cr0,do_restore
  566. /*
  567. * We are about to soft-enable interrupts (we are hard disabled
  568. * at this point). We check if there's anything that needs to
  569. * be replayed first.
  570. */
  571. lbz r0,PACAIRQHAPPENED(r13)
  572. cmpwi cr0,r0,0
  573. bne- restore_check_irq_replay
  574. /*
  575. * Get here when nothing happened while soft-disabled, just
  576. * soft-enable and move-on. We will hard-enable as a side
  577. * effect of rfi
  578. */
  579. restore_no_replay:
  580. TRACE_ENABLE_INTS
  581. li r0,1
  582. stb r0,PACASOFTIRQEN(r13);
  583. /*
  584. * Final return path. BookE is handled in a different file
  585. */
  586. do_restore:
  587. #ifdef CONFIG_PPC_BOOK3E
  588. b .exception_return_book3e
  589. #else
  590. /*
  591. * Clear the reservation. If we know the CPU tracks the address of
  592. * the reservation then we can potentially save some cycles and use
  593. * a larx. On POWER6 and POWER7 this is significantly faster.
  594. */
  595. BEGIN_FTR_SECTION
  596. stdcx. r0,0,r1 /* to clear the reservation */
  597. FTR_SECTION_ELSE
  598. ldarx r4,0,r1
  599. ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  600. /*
  601. * Some code path such as load_up_fpu or altivec return directly
  602. * here. They run entirely hard disabled and do not alter the
  603. * interrupt state. They also don't use lwarx/stwcx. and thus
  604. * are known not to leave dangling reservations.
  605. */
  606. .globl fast_exception_return
  607. fast_exception_return:
  608. ld r3,_MSR(r1)
  609. ld r4,_CTR(r1)
  610. ld r0,_LINK(r1)
  611. mtctr r4
  612. mtlr r0
  613. ld r4,_XER(r1)
  614. mtspr SPRN_XER,r4
  615. REST_8GPRS(5, r1)
  616. andi. r0,r3,MSR_RI
  617. beq- unrecov_restore
  618. /*
  619. * Clear RI before restoring r13. If we are returning to
  620. * userspace and we take an exception after restoring r13,
  621. * we end up corrupting the userspace r13 value.
  622. */
  623. ld r4,PACAKMSR(r13) /* Get kernel MSR without EE */
  624. andc r4,r4,r0 /* r0 contains MSR_RI here */
  625. mtmsrd r4,1
  626. /*
  627. * r13 is our per cpu area, only restore it if we are returning to
  628. * userspace the value stored in the stack frame may belong to
  629. * another CPU.
  630. */
  631. andi. r0,r3,MSR_PR
  632. beq 1f
  633. ACCOUNT_CPU_USER_EXIT(r2, r4)
  634. REST_GPR(13, r1)
  635. 1:
  636. mtspr SPRN_SRR1,r3
  637. ld r2,_CCR(r1)
  638. mtcrf 0xFF,r2
  639. ld r2,_NIP(r1)
  640. mtspr SPRN_SRR0,r2
  641. ld r0,GPR0(r1)
  642. ld r2,GPR2(r1)
  643. ld r3,GPR3(r1)
  644. ld r4,GPR4(r1)
  645. ld r1,GPR1(r1)
  646. rfid
  647. b . /* prevent speculative execution */
  648. #endif /* CONFIG_PPC_BOOK3E */
  649. /*
  650. * We are returning to a context with interrupts soft disabled.
  651. *
  652. * However, we may also about to hard enable, so we need to
  653. * make sure that in this case, we also clear PACA_IRQ_HARD_DIS
  654. * or that bit can get out of sync and bad things will happen
  655. */
  656. restore_irq_off:
  657. ld r3,_MSR(r1)
  658. lbz r7,PACAIRQHAPPENED(r13)
  659. andi. r0,r3,MSR_EE
  660. beq 1f
  661. rlwinm r7,r7,0,~PACA_IRQ_HARD_DIS
  662. stb r7,PACAIRQHAPPENED(r13)
  663. 1: li r0,0
  664. stb r0,PACASOFTIRQEN(r13);
  665. TRACE_DISABLE_INTS
  666. b do_restore
  667. /*
  668. * Something did happen, check if a re-emit is needed
  669. * (this also clears paca->irq_happened)
  670. */
  671. restore_check_irq_replay:
  672. /* XXX: We could implement a fast path here where we check
  673. * for irq_happened being just 0x01, in which case we can
  674. * clear it and return. That means that we would potentially
  675. * miss a decrementer having wrapped all the way around.
  676. *
  677. * Still, this might be useful for things like hash_page
  678. */
  679. bl .__check_irq_replay
  680. cmpwi cr0,r3,0
  681. beq restore_no_replay
  682. /*
  683. * We need to re-emit an interrupt. We do so by re-using our
  684. * existing exception frame. We first change the trap value,
  685. * but we need to ensure we preserve the low nibble of it
  686. */
  687. ld r4,_TRAP(r1)
  688. clrldi r4,r4,60
  689. or r4,r4,r3
  690. std r4,_TRAP(r1)
  691. /*
  692. * Then find the right handler and call it. Interrupts are
  693. * still soft-disabled and we keep them that way.
  694. */
  695. cmpwi cr0,r3,0x500
  696. bne 1f
  697. addi r3,r1,STACK_FRAME_OVERHEAD;
  698. bl .do_IRQ
  699. b .ret_from_except
  700. 1: cmpwi cr0,r3,0x900
  701. bne 1f
  702. addi r3,r1,STACK_FRAME_OVERHEAD;
  703. bl .timer_interrupt
  704. b .ret_from_except
  705. #ifdef CONFIG_PPC_BOOK3E
  706. 1: cmpwi cr0,r3,0x280
  707. bne 1f
  708. addi r3,r1,STACK_FRAME_OVERHEAD;
  709. bl .doorbell_exception
  710. b .ret_from_except
  711. #endif /* CONFIG_PPC_BOOK3E */
  712. 1: b .ret_from_except /* What else to do here ? */
  713. unrecov_restore:
  714. addi r3,r1,STACK_FRAME_OVERHEAD
  715. bl .unrecoverable_exception
  716. b unrecov_restore
  717. #ifdef CONFIG_PPC_RTAS
  718. /*
  719. * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
  720. * called with the MMU off.
  721. *
  722. * In addition, we need to be in 32b mode, at least for now.
  723. *
  724. * Note: r3 is an input parameter to rtas, so don't trash it...
  725. */
  726. _GLOBAL(enter_rtas)
  727. mflr r0
  728. std r0,16(r1)
  729. stdu r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
  730. /* Because RTAS is running in 32b mode, it clobbers the high order half
  731. * of all registers that it saves. We therefore save those registers
  732. * RTAS might touch to the stack. (r0, r3-r13 are caller saved)
  733. */
  734. SAVE_GPR(2, r1) /* Save the TOC */
  735. SAVE_GPR(13, r1) /* Save paca */
  736. SAVE_8GPRS(14, r1) /* Save the non-volatiles */
  737. SAVE_10GPRS(22, r1) /* ditto */
  738. mfcr r4
  739. std r4,_CCR(r1)
  740. mfctr r5
  741. std r5,_CTR(r1)
  742. mfspr r6,SPRN_XER
  743. std r6,_XER(r1)
  744. mfdar r7
  745. std r7,_DAR(r1)
  746. mfdsisr r8
  747. std r8,_DSISR(r1)
  748. /* Temporary workaround to clear CR until RTAS can be modified to
  749. * ignore all bits.
  750. */
  751. li r0,0
  752. mtcr r0
  753. #ifdef CONFIG_BUG
  754. /* There is no way it is acceptable to get here with interrupts enabled,
  755. * check it with the asm equivalent of WARN_ON
  756. */
  757. lbz r0,PACASOFTIRQEN(r13)
  758. 1: tdnei r0,0
  759. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
  760. #endif
  761. /* Hard-disable interrupts */
  762. mfmsr r6
  763. rldicl r7,r6,48,1
  764. rotldi r7,r7,16
  765. mtmsrd r7,1
  766. /* Unfortunately, the stack pointer and the MSR are also clobbered,
  767. * so they are saved in the PACA which allows us to restore
  768. * our original state after RTAS returns.
  769. */
  770. std r1,PACAR1(r13)
  771. std r6,PACASAVEDMSR(r13)
  772. /* Setup our real return addr */
  773. LOAD_REG_ADDR(r4,.rtas_return_loc)
  774. clrldi r4,r4,2 /* convert to realmode address */
  775. mtlr r4
  776. li r0,0
  777. ori r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
  778. andc r0,r6,r0
  779. li r9,1
  780. rldicr r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
  781. ori r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI
  782. andc r6,r0,r9
  783. sync /* disable interrupts so SRR0/1 */
  784. mtmsrd r0 /* don't get trashed */
  785. LOAD_REG_ADDR(r4, rtas)
  786. ld r5,RTASENTRY(r4) /* get the rtas->entry value */
  787. ld r4,RTASBASE(r4) /* get the rtas->base value */
  788. mtspr SPRN_SRR0,r5
  789. mtspr SPRN_SRR1,r6
  790. rfid
  791. b . /* prevent speculative execution */
  792. _STATIC(rtas_return_loc)
  793. /* relocation is off at this point */
  794. GET_PACA(r4)
  795. clrldi r4,r4,2 /* convert to realmode address */
  796. bcl 20,31,$+4
  797. 0: mflr r3
  798. ld r3,(1f-0b)(r3) /* get &.rtas_restore_regs */
  799. mfmsr r6
  800. li r0,MSR_RI
  801. andc r6,r6,r0
  802. sync
  803. mtmsrd r6
  804. ld r1,PACAR1(r4) /* Restore our SP */
  805. ld r4,PACASAVEDMSR(r4) /* Restore our MSR */
  806. mtspr SPRN_SRR0,r3
  807. mtspr SPRN_SRR1,r4
  808. rfid
  809. b . /* prevent speculative execution */
  810. .align 3
  811. 1: .llong .rtas_restore_regs
  812. _STATIC(rtas_restore_regs)
  813. /* relocation is on at this point */
  814. REST_GPR(2, r1) /* Restore the TOC */
  815. REST_GPR(13, r1) /* Restore paca */
  816. REST_8GPRS(14, r1) /* Restore the non-volatiles */
  817. REST_10GPRS(22, r1) /* ditto */
  818. GET_PACA(r13)
  819. ld r4,_CCR(r1)
  820. mtcr r4
  821. ld r5,_CTR(r1)
  822. mtctr r5
  823. ld r6,_XER(r1)
  824. mtspr SPRN_XER,r6
  825. ld r7,_DAR(r1)
  826. mtdar r7
  827. ld r8,_DSISR(r1)
  828. mtdsisr r8
  829. addi r1,r1,RTAS_FRAME_SIZE /* Unstack our frame */
  830. ld r0,16(r1) /* get return address */
  831. mtlr r0
  832. blr /* return to caller */
  833. #endif /* CONFIG_PPC_RTAS */
  834. _GLOBAL(enter_prom)
  835. mflr r0
  836. std r0,16(r1)
  837. stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
  838. /* Because PROM is running in 32b mode, it clobbers the high order half
  839. * of all registers that it saves. We therefore save those registers
  840. * PROM might touch to the stack. (r0, r3-r13 are caller saved)
  841. */
  842. SAVE_GPR(2, r1)
  843. SAVE_GPR(13, r1)
  844. SAVE_8GPRS(14, r1)
  845. SAVE_10GPRS(22, r1)
  846. mfcr r10
  847. mfmsr r11
  848. std r10,_CCR(r1)
  849. std r11,_MSR(r1)
  850. /* Get the PROM entrypoint */
  851. mtlr r4
  852. /* Switch MSR to 32 bits mode
  853. */
  854. #ifdef CONFIG_PPC_BOOK3E
  855. rlwinm r11,r11,0,1,31
  856. mtmsr r11
  857. #else /* CONFIG_PPC_BOOK3E */
  858. mfmsr r11
  859. li r12,1
  860. rldicr r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
  861. andc r11,r11,r12
  862. li r12,1
  863. rldicr r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
  864. andc r11,r11,r12
  865. mtmsrd r11
  866. #endif /* CONFIG_PPC_BOOK3E */
  867. isync
  868. /* Enter PROM here... */
  869. blrl
  870. /* Just make sure that r1 top 32 bits didn't get
  871. * corrupt by OF
  872. */
  873. rldicl r1,r1,0,32
  874. /* Restore the MSR (back to 64 bits) */
  875. ld r0,_MSR(r1)
  876. MTMSRD(r0)
  877. isync
  878. /* Restore other registers */
  879. REST_GPR(2, r1)
  880. REST_GPR(13, r1)
  881. REST_8GPRS(14, r1)
  882. REST_10GPRS(22, r1)
  883. ld r4,_CCR(r1)
  884. mtcr r4
  885. addi r1,r1,PROM_FRAME_SIZE
  886. ld r0,16(r1)
  887. mtlr r0
  888. blr
  889. #ifdef CONFIG_FUNCTION_TRACER
  890. #ifdef CONFIG_DYNAMIC_FTRACE
  891. _GLOBAL(mcount)
  892. _GLOBAL(_mcount)
  893. blr
  894. _GLOBAL(ftrace_caller)
  895. /* Taken from output of objdump from lib64/glibc */
  896. mflr r3
  897. ld r11, 0(r1)
  898. stdu r1, -112(r1)
  899. std r3, 128(r1)
  900. ld r4, 16(r11)
  901. subi r3, r3, MCOUNT_INSN_SIZE
  902. .globl ftrace_call
  903. ftrace_call:
  904. bl ftrace_stub
  905. nop
  906. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  907. .globl ftrace_graph_call
  908. ftrace_graph_call:
  909. b ftrace_graph_stub
  910. _GLOBAL(ftrace_graph_stub)
  911. #endif
  912. ld r0, 128(r1)
  913. mtlr r0
  914. addi r1, r1, 112
  915. _GLOBAL(ftrace_stub)
  916. blr
  917. #else
  918. _GLOBAL(mcount)
  919. blr
  920. _GLOBAL(_mcount)
  921. /* Taken from output of objdump from lib64/glibc */
  922. mflr r3
  923. ld r11, 0(r1)
  924. stdu r1, -112(r1)
  925. std r3, 128(r1)
  926. ld r4, 16(r11)
  927. subi r3, r3, MCOUNT_INSN_SIZE
  928. LOAD_REG_ADDR(r5,ftrace_trace_function)
  929. ld r5,0(r5)
  930. ld r5,0(r5)
  931. mtctr r5
  932. bctrl
  933. nop
  934. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  935. b ftrace_graph_caller
  936. #endif
  937. ld r0, 128(r1)
  938. mtlr r0
  939. addi r1, r1, 112
  940. _GLOBAL(ftrace_stub)
  941. blr
  942. #endif /* CONFIG_DYNAMIC_FTRACE */
  943. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  944. _GLOBAL(ftrace_graph_caller)
  945. /* load r4 with local address */
  946. ld r4, 128(r1)
  947. subi r4, r4, MCOUNT_INSN_SIZE
  948. /* get the parent address */
  949. ld r11, 112(r1)
  950. addi r3, r11, 16
  951. bl .prepare_ftrace_return
  952. nop
  953. ld r0, 128(r1)
  954. mtlr r0
  955. addi r1, r1, 112
  956. blr
  957. _GLOBAL(return_to_handler)
  958. /* need to save return values */
  959. std r4, -24(r1)
  960. std r3, -16(r1)
  961. std r31, -8(r1)
  962. mr r31, r1
  963. stdu r1, -112(r1)
  964. bl .ftrace_return_to_handler
  965. nop
  966. /* return value has real return address */
  967. mtlr r3
  968. ld r1, 0(r1)
  969. ld r4, -24(r1)
  970. ld r3, -16(r1)
  971. ld r31, -8(r1)
  972. /* Jump back to real return address */
  973. blr
  974. _GLOBAL(mod_return_to_handler)
  975. /* need to save return values */
  976. std r4, -32(r1)
  977. std r3, -24(r1)
  978. /* save TOC */
  979. std r2, -16(r1)
  980. std r31, -8(r1)
  981. mr r31, r1
  982. stdu r1, -112(r1)
  983. /*
  984. * We are in a module using the module's TOC.
  985. * Switch to our TOC to run inside the core kernel.
  986. */
  987. ld r2, PACATOC(r13)
  988. bl .ftrace_return_to_handler
  989. nop
  990. /* return value has real return address */
  991. mtlr r3
  992. ld r1, 0(r1)
  993. ld r4, -32(r1)
  994. ld r3, -24(r1)
  995. ld r2, -16(r1)
  996. ld r31, -8(r1)
  997. /* Jump back to real return address */
  998. blr
  999. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1000. #endif /* CONFIG_FUNCTION_TRACER */