entry_64.S 26 KB

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