entry_64.S 26 KB

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