entry_64.S 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. /*
  35. * System calls.
  36. */
  37. .section ".toc","aw"
  38. .SYS_CALL_TABLE:
  39. .tc .sys_call_table[TC],.sys_call_table
  40. /* This value is used to mark exception frames on the stack. */
  41. exception_marker:
  42. .tc ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
  43. .section ".text"
  44. .align 7
  45. #undef SHOW_SYSCALLS
  46. .globl system_call_common
  47. system_call_common:
  48. andi. r10,r12,MSR_PR
  49. mr r10,r1
  50. addi r1,r1,-INT_FRAME_SIZE
  51. beq- 1f
  52. ld r1,PACAKSAVE(r13)
  53. 1: std r10,0(r1)
  54. crclr so
  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. std r4,GPR4(r1)
  63. std r5,GPR5(r1)
  64. std r6,GPR6(r1)
  65. std r7,GPR7(r1)
  66. std r8,GPR8(r1)
  67. li r11,0
  68. std r11,GPR9(r1)
  69. std r11,GPR10(r1)
  70. std r11,GPR11(r1)
  71. std r11,GPR12(r1)
  72. std r9,GPR13(r1)
  73. mfcr r9
  74. mflr r10
  75. li r11,0xc01
  76. std r9,_CCR(r1)
  77. std r10,_LINK(r1)
  78. std r11,_TRAP(r1)
  79. mfxer r9
  80. mfctr r10
  81. std r9,_XER(r1)
  82. std r10,_CTR(r1)
  83. std r3,ORIG_GPR3(r1)
  84. ld r2,PACATOC(r13)
  85. addi r9,r1,STACK_FRAME_OVERHEAD
  86. ld r11,exception_marker@toc(r2)
  87. std r11,-16(r9) /* "regshere" marker */
  88. #ifdef CONFIG_TRACE_IRQFLAGS
  89. bl .trace_hardirqs_on
  90. REST_GPR(0,r1)
  91. REST_4GPRS(3,r1)
  92. REST_2GPRS(7,r1)
  93. addi r9,r1,STACK_FRAME_OVERHEAD
  94. ld r12,_MSR(r1)
  95. #endif /* CONFIG_TRACE_IRQFLAGS */
  96. li r10,1
  97. stb r10,PACASOFTIRQEN(r13)
  98. stb r10,PACAHARDIRQEN(r13)
  99. std r10,SOFTE(r1)
  100. #ifdef CONFIG_PPC_ISERIES
  101. BEGIN_FW_FTR_SECTION
  102. /* Hack for handling interrupts when soft-enabling on iSeries */
  103. cmpdi cr1,r0,0x5555 /* syscall 0x5555 */
  104. andi. r10,r12,MSR_PR /* from kernel */
  105. crand 4*cr0+eq,4*cr1+eq,4*cr0+eq
  106. bne 2f
  107. b hardware_interrupt_entry
  108. 2:
  109. END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
  110. #endif /* CONFIG_PPC_ISERIES */
  111. mfmsr r11
  112. ori r11,r11,MSR_EE
  113. mtmsrd r11,1
  114. #ifdef SHOW_SYSCALLS
  115. bl .do_show_syscall
  116. REST_GPR(0,r1)
  117. REST_4GPRS(3,r1)
  118. REST_2GPRS(7,r1)
  119. addi r9,r1,STACK_FRAME_OVERHEAD
  120. #endif
  121. clrrdi r11,r1,THREAD_SHIFT
  122. ld r10,TI_FLAGS(r11)
  123. andi. r11,r10,_TIF_SYSCALL_T_OR_A
  124. bne- syscall_dotrace
  125. syscall_dotrace_cont:
  126. cmpldi 0,r0,NR_syscalls
  127. bge- syscall_enosys
  128. system_call: /* label this so stack traces look sane */
  129. /*
  130. * Need to vector to 32 Bit or default sys_call_table here,
  131. * based on caller's run-mode / personality.
  132. */
  133. ld r11,.SYS_CALL_TABLE@toc(2)
  134. andi. r10,r10,_TIF_32BIT
  135. beq 15f
  136. addi r11,r11,8 /* use 32-bit syscall entries */
  137. clrldi r3,r3,32
  138. clrldi r4,r4,32
  139. clrldi r5,r5,32
  140. clrldi r6,r6,32
  141. clrldi r7,r7,32
  142. clrldi r8,r8,32
  143. 15:
  144. slwi r0,r0,4
  145. ldx r10,r11,r0 /* Fetch system call handler [ptr] */
  146. mtctr r10
  147. bctrl /* Call handler */
  148. syscall_exit:
  149. std r3,RESULT(r1)
  150. #ifdef SHOW_SYSCALLS
  151. bl .do_show_syscall_exit
  152. ld r3,RESULT(r1)
  153. #endif
  154. clrrdi r12,r1,THREAD_SHIFT
  155. /* disable interrupts so current_thread_info()->flags can't change,
  156. and so that we don't get interrupted after loading SRR0/1. */
  157. ld r8,_MSR(r1)
  158. andi. r10,r8,MSR_RI
  159. beq- unrecov_restore
  160. mfmsr r10
  161. rldicl r10,r10,48,1
  162. rotldi r10,r10,16
  163. mtmsrd r10,1
  164. ld r9,TI_FLAGS(r12)
  165. li r11,-_LAST_ERRNO
  166. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
  167. bne- syscall_exit_work
  168. cmpld r3,r11
  169. ld r5,_CCR(r1)
  170. bge- syscall_error
  171. syscall_error_cont:
  172. ld r7,_NIP(r1)
  173. stdcx. r0,0,r1 /* to clear the reservation */
  174. andi. r6,r8,MSR_PR
  175. ld r4,_LINK(r1)
  176. /*
  177. * Clear RI before restoring r13. If we are returning to
  178. * userspace and we take an exception after restoring r13,
  179. * we end up corrupting the userspace r13 value.
  180. */
  181. li r12,MSR_RI
  182. andc r11,r10,r12
  183. mtmsrd r11,1 /* clear MSR.RI */
  184. beq- 1f
  185. ACCOUNT_CPU_USER_EXIT(r11, r12)
  186. ld r13,GPR13(r1) /* only restore r13 if returning to usermode */
  187. 1: ld r2,GPR2(r1)
  188. ld r1,GPR1(r1)
  189. mtlr r4
  190. mtcr r5
  191. mtspr SPRN_SRR0,r7
  192. mtspr SPRN_SRR1,r8
  193. rfid
  194. b . /* prevent speculative execution */
  195. syscall_error:
  196. oris r5,r5,0x1000 /* Set SO bit in CR */
  197. neg r3,r3
  198. std r5,_CCR(r1)
  199. b syscall_error_cont
  200. /* Traced system call support */
  201. syscall_dotrace:
  202. bl .save_nvgprs
  203. addi r3,r1,STACK_FRAME_OVERHEAD
  204. bl .do_syscall_trace_enter
  205. /*
  206. * Restore argument registers possibly just changed.
  207. * We use the return value of do_syscall_trace_enter
  208. * for the call number to look up in the table (r0).
  209. */
  210. mr r0,r3
  211. ld r3,GPR3(r1)
  212. ld r4,GPR4(r1)
  213. ld r5,GPR5(r1)
  214. ld r6,GPR6(r1)
  215. ld r7,GPR7(r1)
  216. ld r8,GPR8(r1)
  217. addi r9,r1,STACK_FRAME_OVERHEAD
  218. clrrdi r10,r1,THREAD_SHIFT
  219. ld r10,TI_FLAGS(r10)
  220. b syscall_dotrace_cont
  221. syscall_enosys:
  222. li r3,-ENOSYS
  223. b syscall_exit
  224. syscall_exit_work:
  225. /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
  226. If TIF_NOERROR is set, just save r3 as it is. */
  227. andi. r0,r9,_TIF_RESTOREALL
  228. beq+ 0f
  229. REST_NVGPRS(r1)
  230. b 2f
  231. 0: cmpld r3,r11 /* r10 is -LAST_ERRNO */
  232. blt+ 1f
  233. andi. r0,r9,_TIF_NOERROR
  234. bne- 1f
  235. ld r5,_CCR(r1)
  236. neg r3,r3
  237. oris r5,r5,0x1000 /* Set SO bit in CR */
  238. std r5,_CCR(r1)
  239. 1: std r3,GPR3(r1)
  240. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  241. beq 4f
  242. /* Clear per-syscall TIF flags if any are set. */
  243. li r11,_TIF_PERSYSCALL_MASK
  244. addi r12,r12,TI_FLAGS
  245. 3: ldarx r10,0,r12
  246. andc r10,r10,r11
  247. stdcx. r10,0,r12
  248. bne- 3b
  249. subi r12,r12,TI_FLAGS
  250. 4: /* Anything else left to do? */
  251. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
  252. beq .ret_from_except_lite
  253. /* Re-enable interrupts */
  254. mfmsr r10
  255. ori r10,r10,MSR_EE
  256. mtmsrd r10,1
  257. bl .save_nvgprs
  258. addi r3,r1,STACK_FRAME_OVERHEAD
  259. bl .do_syscall_trace_leave
  260. b .ret_from_except
  261. /* Save non-volatile GPRs, if not already saved. */
  262. _GLOBAL(save_nvgprs)
  263. ld r11,_TRAP(r1)
  264. andi. r0,r11,1
  265. beqlr-
  266. SAVE_NVGPRS(r1)
  267. clrrdi r0,r11,1
  268. std r0,_TRAP(r1)
  269. blr
  270. /*
  271. * The sigsuspend and rt_sigsuspend system calls can call do_signal
  272. * and thus put the process into the stopped state where we might
  273. * want to examine its user state with ptrace. Therefore we need
  274. * to save all the nonvolatile registers (r14 - r31) before calling
  275. * the C code. Similarly, fork, vfork and clone need the full
  276. * register state on the stack so that it can be copied to the child.
  277. */
  278. _GLOBAL(ppc_fork)
  279. bl .save_nvgprs
  280. bl .sys_fork
  281. b syscall_exit
  282. _GLOBAL(ppc_vfork)
  283. bl .save_nvgprs
  284. bl .sys_vfork
  285. b syscall_exit
  286. _GLOBAL(ppc_clone)
  287. bl .save_nvgprs
  288. bl .sys_clone
  289. b syscall_exit
  290. _GLOBAL(ppc32_swapcontext)
  291. bl .save_nvgprs
  292. bl .compat_sys_swapcontext
  293. b syscall_exit
  294. _GLOBAL(ppc64_swapcontext)
  295. bl .save_nvgprs
  296. bl .sys_swapcontext
  297. b syscall_exit
  298. _GLOBAL(ret_from_fork)
  299. bl .schedule_tail
  300. REST_NVGPRS(r1)
  301. li r3,0
  302. b syscall_exit
  303. /*
  304. * This routine switches between two different tasks. The process
  305. * state of one is saved on its kernel stack. Then the state
  306. * of the other is restored from its kernel stack. The memory
  307. * management hardware is updated to the second process's state.
  308. * Finally, we can return to the second process, via ret_from_except.
  309. * On entry, r3 points to the THREAD for the current task, r4
  310. * points to the THREAD for the new task.
  311. *
  312. * Note: there are two ways to get to the "going out" portion
  313. * of this code; either by coming in via the entry (_switch)
  314. * or via "fork" which must set up an environment equivalent
  315. * to the "_switch" path. If you change this you'll have to change
  316. * the fork code also.
  317. *
  318. * The code which creates the new task context is in 'copy_thread'
  319. * in arch/powerpc/kernel/process.c
  320. */
  321. .align 7
  322. _GLOBAL(_switch)
  323. mflr r0
  324. std r0,16(r1)
  325. stdu r1,-SWITCH_FRAME_SIZE(r1)
  326. /* r3-r13 are caller saved -- Cort */
  327. SAVE_8GPRS(14, r1)
  328. SAVE_10GPRS(22, r1)
  329. mflr r20 /* Return to switch caller */
  330. mfmsr r22
  331. li r0, MSR_FP
  332. #ifdef CONFIG_VSX
  333. BEGIN_FTR_SECTION
  334. oris r0,r0,MSR_VSX@h /* Disable VSX */
  335. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  336. #endif /* CONFIG_VSX */
  337. #ifdef CONFIG_ALTIVEC
  338. BEGIN_FTR_SECTION
  339. oris r0,r0,MSR_VEC@h /* Disable altivec */
  340. mfspr r24,SPRN_VRSAVE /* save vrsave register value */
  341. std r24,THREAD_VRSAVE(r3)
  342. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  343. #endif /* CONFIG_ALTIVEC */
  344. and. r0,r0,r22
  345. beq+ 1f
  346. andc r22,r22,r0
  347. mtmsrd r22
  348. isync
  349. 1: std r20,_NIP(r1)
  350. mfcr r23
  351. std r23,_CCR(r1)
  352. std r1,KSP(r3) /* Set old stack pointer */
  353. #ifdef CONFIG_SMP
  354. /* We need a sync somewhere here to make sure that if the
  355. * previous task gets rescheduled on another CPU, it sees all
  356. * stores it has performed on this one.
  357. */
  358. sync
  359. #endif /* CONFIG_SMP */
  360. addi r6,r4,-THREAD /* Convert THREAD to 'current' */
  361. std r6,PACACURRENT(r13) /* Set new 'current' */
  362. ld r8,KSP(r4) /* new stack pointer */
  363. BEGIN_FTR_SECTION
  364. BEGIN_FTR_SECTION_NESTED(95)
  365. clrrdi r6,r8,28 /* get its ESID */
  366. clrrdi r9,r1,28 /* get current sp ESID */
  367. FTR_SECTION_ELSE_NESTED(95)
  368. clrrdi r6,r8,40 /* get its 1T ESID */
  369. clrrdi r9,r1,40 /* get current sp 1T ESID */
  370. ALT_FTR_SECTION_END_NESTED_IFCLR(CPU_FTR_1T_SEGMENT, 95)
  371. FTR_SECTION_ELSE
  372. b 2f
  373. ALT_FTR_SECTION_END_IFSET(CPU_FTR_SLB)
  374. clrldi. r0,r6,2 /* is new ESID c00000000? */
  375. cmpd cr1,r6,r9 /* or is new ESID the same as current ESID? */
  376. cror eq,4*cr1+eq,eq
  377. beq 2f /* if yes, don't slbie it */
  378. /* Bolt in the new stack SLB entry */
  379. ld r7,KSP_VSID(r4) /* Get new stack's VSID */
  380. oris r0,r6,(SLB_ESID_V)@h
  381. ori r0,r0,(SLB_NUM_BOLTED-1)@l
  382. BEGIN_FTR_SECTION
  383. li r9,MMU_SEGSIZE_1T /* insert B field */
  384. oris r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
  385. rldimi r7,r9,SLB_VSID_SSIZE_SHIFT,0
  386. END_FTR_SECTION_IFSET(CPU_FTR_1T_SEGMENT)
  387. /* Update the last bolted SLB. No write barriers are needed
  388. * here, provided we only update the current CPU's SLB shadow
  389. * buffer.
  390. */
  391. ld r9,PACA_SLBSHADOWPTR(r13)
  392. li r12,0
  393. std r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
  394. std r7,SLBSHADOW_STACKVSID(r9) /* Save VSID */
  395. std r0,SLBSHADOW_STACKESID(r9) /* Save ESID */
  396. /* No need to check for CPU_FTR_NO_SLBIE_B here, since when
  397. * we have 1TB segments, the only CPUs known to have the errata
  398. * only support less than 1TB of system memory and we'll never
  399. * actually hit this code path.
  400. */
  401. slbie r6
  402. slbie r6 /* Workaround POWER5 < DD2.1 issue */
  403. slbmte r7,r0
  404. isync
  405. 2:
  406. clrrdi r7,r8,THREAD_SHIFT /* base of new stack */
  407. /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
  408. because we don't need to leave the 288-byte ABI gap at the
  409. top of the kernel stack. */
  410. addi r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
  411. mr r1,r8 /* start using new stack pointer */
  412. std r7,PACAKSAVE(r13)
  413. ld r6,_CCR(r1)
  414. mtcrf 0xFF,r6
  415. #ifdef CONFIG_ALTIVEC
  416. BEGIN_FTR_SECTION
  417. ld r0,THREAD_VRSAVE(r4)
  418. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  419. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  420. #endif /* CONFIG_ALTIVEC */
  421. /* r3-r13 are destroyed -- Cort */
  422. REST_8GPRS(14, r1)
  423. REST_10GPRS(22, r1)
  424. /* convert old thread to its task_struct for return value */
  425. addi r3,r3,-THREAD
  426. ld r7,_NIP(r1) /* Return to _switch caller in new task */
  427. mtlr r7
  428. addi r1,r1,SWITCH_FRAME_SIZE
  429. blr
  430. .align 7
  431. _GLOBAL(ret_from_except)
  432. ld r11,_TRAP(r1)
  433. andi. r0,r11,1
  434. bne .ret_from_except_lite
  435. REST_NVGPRS(r1)
  436. _GLOBAL(ret_from_except_lite)
  437. /*
  438. * Disable interrupts so that current_thread_info()->flags
  439. * can't change between when we test it and when we return
  440. * from the interrupt.
  441. */
  442. mfmsr r10 /* Get current interrupt state */
  443. rldicl r9,r10,48,1 /* clear MSR_EE */
  444. rotldi r9,r9,16
  445. mtmsrd r9,1 /* Update machine state */
  446. #ifdef CONFIG_PREEMPT
  447. clrrdi r9,r1,THREAD_SHIFT /* current_thread_info() */
  448. li r0,_TIF_NEED_RESCHED /* bits to check */
  449. ld r3,_MSR(r1)
  450. ld r4,TI_FLAGS(r9)
  451. /* Move MSR_PR bit in r3 to _TIF_SIGPENDING position in r0 */
  452. rlwimi r0,r3,32+TIF_SIGPENDING-MSR_PR_LG,_TIF_SIGPENDING
  453. and. r0,r4,r0 /* check NEED_RESCHED and maybe SIGPENDING */
  454. bne do_work
  455. #else /* !CONFIG_PREEMPT */
  456. ld r3,_MSR(r1) /* Returning to user mode? */
  457. andi. r3,r3,MSR_PR
  458. beq restore /* if not, just restore regs and return */
  459. /* Check current_thread_info()->flags */
  460. clrrdi r9,r1,THREAD_SHIFT
  461. ld r4,TI_FLAGS(r9)
  462. andi. r0,r4,_TIF_USER_WORK_MASK
  463. bne do_work
  464. #endif
  465. restore:
  466. ld r5,SOFTE(r1)
  467. #ifdef CONFIG_PPC_ISERIES
  468. BEGIN_FW_FTR_SECTION
  469. cmpdi 0,r5,0
  470. beq 4f
  471. /* Check for pending interrupts (iSeries) */
  472. ld r3,PACALPPACAPTR(r13)
  473. ld r3,LPPACAANYINT(r3)
  474. cmpdi r3,0
  475. beq+ 4f /* skip do_IRQ if no interrupts */
  476. li r3,0
  477. stb r3,PACASOFTIRQEN(r13) /* ensure we are soft-disabled */
  478. #ifdef CONFIG_TRACE_IRQFLAGS
  479. bl .trace_hardirqs_off
  480. mfmsr r10
  481. #endif
  482. ori r10,r10,MSR_EE
  483. mtmsrd r10 /* hard-enable again */
  484. addi r3,r1,STACK_FRAME_OVERHEAD
  485. bl .do_IRQ
  486. b .ret_from_except_lite /* loop back and handle more */
  487. 4:
  488. END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
  489. #endif
  490. TRACE_AND_RESTORE_IRQ(r5);
  491. /* extract EE bit and use it to restore paca->hard_enabled */
  492. ld r3,_MSR(r1)
  493. rldicl r4,r3,49,63 /* r0 = (r3 >> 15) & 1 */
  494. stb r4,PACAHARDIRQEN(r13)
  495. ld r4,_CTR(r1)
  496. ld r0,_LINK(r1)
  497. mtctr r4
  498. mtlr r0
  499. ld r4,_XER(r1)
  500. mtspr SPRN_XER,r4
  501. REST_8GPRS(5, r1)
  502. andi. r0,r3,MSR_RI
  503. beq- unrecov_restore
  504. stdcx. r0,0,r1 /* to clear the reservation */
  505. /*
  506. * Clear RI before restoring r13. If we are returning to
  507. * userspace and we take an exception after restoring r13,
  508. * we end up corrupting the userspace r13 value.
  509. */
  510. mfmsr r4
  511. andc r4,r4,r0 /* r0 contains MSR_RI here */
  512. mtmsrd r4,1
  513. /*
  514. * r13 is our per cpu area, only restore it if we are returning to
  515. * userspace
  516. */
  517. andi. r0,r3,MSR_PR
  518. beq 1f
  519. ACCOUNT_CPU_USER_EXIT(r2, r4)
  520. REST_GPR(13, r1)
  521. 1:
  522. mtspr SPRN_SRR1,r3
  523. ld r2,_CCR(r1)
  524. mtcrf 0xFF,r2
  525. ld r2,_NIP(r1)
  526. mtspr SPRN_SRR0,r2
  527. ld r0,GPR0(r1)
  528. ld r2,GPR2(r1)
  529. ld r3,GPR3(r1)
  530. ld r4,GPR4(r1)
  531. ld r1,GPR1(r1)
  532. rfid
  533. b . /* prevent speculative execution */
  534. do_work:
  535. #ifdef CONFIG_PREEMPT
  536. andi. r0,r3,MSR_PR /* Returning to user mode? */
  537. bne user_work
  538. /* Check that preempt_count() == 0 and interrupts are enabled */
  539. lwz r8,TI_PREEMPT(r9)
  540. cmpwi cr1,r8,0
  541. ld r0,SOFTE(r1)
  542. cmpdi r0,0
  543. crandc eq,cr1*4+eq,eq
  544. bne restore
  545. /* here we are preempting the current task */
  546. 1:
  547. #ifdef CONFIG_TRACE_IRQFLAGS
  548. bl .trace_hardirqs_on
  549. /* Note: we just clobbered r10 which used to contain the previous
  550. * MSR before the hard-disabling done by the caller of do_work.
  551. * We don't have that value anymore, but it doesn't matter as
  552. * we will hard-enable unconditionally, we can just reload the
  553. * current MSR into r10
  554. */
  555. mfmsr r10
  556. #endif /* CONFIG_TRACE_IRQFLAGS */
  557. li r0,1
  558. stb r0,PACASOFTIRQEN(r13)
  559. stb r0,PACAHARDIRQEN(r13)
  560. ori r10,r10,MSR_EE
  561. mtmsrd r10,1 /* reenable interrupts */
  562. bl .preempt_schedule
  563. mfmsr r10
  564. clrrdi r9,r1,THREAD_SHIFT
  565. rldicl r10,r10,48,1 /* disable interrupts again */
  566. rotldi r10,r10,16
  567. mtmsrd r10,1
  568. ld r4,TI_FLAGS(r9)
  569. andi. r0,r4,_TIF_NEED_RESCHED
  570. bne 1b
  571. b restore
  572. user_work:
  573. #endif
  574. /* Enable interrupts */
  575. ori r10,r10,MSR_EE
  576. mtmsrd r10,1
  577. andi. r0,r4,_TIF_NEED_RESCHED
  578. beq 1f
  579. bl .schedule
  580. b .ret_from_except_lite
  581. 1: bl .save_nvgprs
  582. addi r3,r1,STACK_FRAME_OVERHEAD
  583. bl .do_signal
  584. b .ret_from_except
  585. unrecov_restore:
  586. addi r3,r1,STACK_FRAME_OVERHEAD
  587. bl .unrecoverable_exception
  588. b unrecov_restore
  589. #ifdef CONFIG_PPC_RTAS
  590. /*
  591. * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
  592. * called with the MMU off.
  593. *
  594. * In addition, we need to be in 32b mode, at least for now.
  595. *
  596. * Note: r3 is an input parameter to rtas, so don't trash it...
  597. */
  598. _GLOBAL(enter_rtas)
  599. mflr r0
  600. std r0,16(r1)
  601. stdu r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
  602. /* Because RTAS is running in 32b mode, it clobbers the high order half
  603. * of all registers that it saves. We therefore save those registers
  604. * RTAS might touch to the stack. (r0, r3-r13 are caller saved)
  605. */
  606. SAVE_GPR(2, r1) /* Save the TOC */
  607. SAVE_GPR(13, r1) /* Save paca */
  608. SAVE_8GPRS(14, r1) /* Save the non-volatiles */
  609. SAVE_10GPRS(22, r1) /* ditto */
  610. mfcr r4
  611. std r4,_CCR(r1)
  612. mfctr r5
  613. std r5,_CTR(r1)
  614. mfspr r6,SPRN_XER
  615. std r6,_XER(r1)
  616. mfdar r7
  617. std r7,_DAR(r1)
  618. mfdsisr r8
  619. std r8,_DSISR(r1)
  620. mfsrr0 r9
  621. std r9,_SRR0(r1)
  622. mfsrr1 r10
  623. std r10,_SRR1(r1)
  624. /* Temporary workaround to clear CR until RTAS can be modified to
  625. * ignore all bits.
  626. */
  627. li r0,0
  628. mtcr r0
  629. #ifdef CONFIG_BUG
  630. /* There is no way it is acceptable to get here with interrupts enabled,
  631. * check it with the asm equivalent of WARN_ON
  632. */
  633. lbz r0,PACASOFTIRQEN(r13)
  634. 1: tdnei r0,0
  635. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
  636. #endif
  637. /* Hard-disable interrupts */
  638. mfmsr r6
  639. rldicl r7,r6,48,1
  640. rotldi r7,r7,16
  641. mtmsrd r7,1
  642. /* Unfortunately, the stack pointer and the MSR are also clobbered,
  643. * so they are saved in the PACA which allows us to restore
  644. * our original state after RTAS returns.
  645. */
  646. std r1,PACAR1(r13)
  647. std r6,PACASAVEDMSR(r13)
  648. /* Setup our real return addr */
  649. LOAD_REG_ADDR(r4,.rtas_return_loc)
  650. clrldi r4,r4,2 /* convert to realmode address */
  651. mtlr r4
  652. li r0,0
  653. ori r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
  654. andc r0,r6,r0
  655. li r9,1
  656. rldicr r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
  657. ori r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP
  658. andc r6,r0,r9
  659. ori r6,r6,MSR_RI
  660. sync /* disable interrupts so SRR0/1 */
  661. mtmsrd r0 /* don't get trashed */
  662. LOAD_REG_ADDR(r4, rtas)
  663. ld r5,RTASENTRY(r4) /* get the rtas->entry value */
  664. ld r4,RTASBASE(r4) /* get the rtas->base value */
  665. mtspr SPRN_SRR0,r5
  666. mtspr SPRN_SRR1,r6
  667. rfid
  668. b . /* prevent speculative execution */
  669. _STATIC(rtas_return_loc)
  670. /* relocation is off at this point */
  671. mfspr r4,SPRN_SPRG3 /* Get PACA */
  672. clrldi r4,r4,2 /* convert to realmode address */
  673. mfmsr r6
  674. li r0,MSR_RI
  675. andc r6,r6,r0
  676. sync
  677. mtmsrd r6
  678. ld r1,PACAR1(r4) /* Restore our SP */
  679. LOAD_REG_IMMEDIATE(r3,.rtas_restore_regs)
  680. ld r4,PACASAVEDMSR(r4) /* Restore our MSR */
  681. mtspr SPRN_SRR0,r3
  682. mtspr SPRN_SRR1,r4
  683. rfid
  684. b . /* prevent speculative execution */
  685. _STATIC(rtas_restore_regs)
  686. /* relocation is on at this point */
  687. REST_GPR(2, r1) /* Restore the TOC */
  688. REST_GPR(13, r1) /* Restore paca */
  689. REST_8GPRS(14, r1) /* Restore the non-volatiles */
  690. REST_10GPRS(22, r1) /* ditto */
  691. mfspr r13,SPRN_SPRG3
  692. ld r4,_CCR(r1)
  693. mtcr r4
  694. ld r5,_CTR(r1)
  695. mtctr r5
  696. ld r6,_XER(r1)
  697. mtspr SPRN_XER,r6
  698. ld r7,_DAR(r1)
  699. mtdar r7
  700. ld r8,_DSISR(r1)
  701. mtdsisr r8
  702. ld r9,_SRR0(r1)
  703. mtsrr0 r9
  704. ld r10,_SRR1(r1)
  705. mtsrr1 r10
  706. addi r1,r1,RTAS_FRAME_SIZE /* Unstack our frame */
  707. ld r0,16(r1) /* get return address */
  708. mtlr r0
  709. blr /* return to caller */
  710. #endif /* CONFIG_PPC_RTAS */
  711. _GLOBAL(enter_prom)
  712. mflr r0
  713. std r0,16(r1)
  714. stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
  715. /* Because PROM is running in 32b mode, it clobbers the high order half
  716. * of all registers that it saves. We therefore save those registers
  717. * PROM might touch to the stack. (r0, r3-r13 are caller saved)
  718. */
  719. SAVE_8GPRS(2, r1)
  720. SAVE_GPR(13, r1)
  721. SAVE_8GPRS(14, r1)
  722. SAVE_10GPRS(22, r1)
  723. mfcr r4
  724. std r4,_CCR(r1)
  725. mfctr r5
  726. std r5,_CTR(r1)
  727. mfspr r6,SPRN_XER
  728. std r6,_XER(r1)
  729. mfdar r7
  730. std r7,_DAR(r1)
  731. mfdsisr r8
  732. std r8,_DSISR(r1)
  733. mfsrr0 r9
  734. std r9,_SRR0(r1)
  735. mfsrr1 r10
  736. std r10,_SRR1(r1)
  737. mfmsr r11
  738. std r11,_MSR(r1)
  739. /* Get the PROM entrypoint */
  740. ld r0,GPR4(r1)
  741. mtlr r0
  742. /* Switch MSR to 32 bits mode
  743. */
  744. mfmsr r11
  745. li r12,1
  746. rldicr r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
  747. andc r11,r11,r12
  748. li r12,1
  749. rldicr r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
  750. andc r11,r11,r12
  751. mtmsrd r11
  752. isync
  753. /* Restore arguments & enter PROM here... */
  754. ld r3,GPR3(r1)
  755. blrl
  756. /* Just make sure that r1 top 32 bits didn't get
  757. * corrupt by OF
  758. */
  759. rldicl r1,r1,0,32
  760. /* Restore the MSR (back to 64 bits) */
  761. ld r0,_MSR(r1)
  762. mtmsrd r0
  763. isync
  764. /* Restore other registers */
  765. REST_GPR(2, r1)
  766. REST_GPR(13, r1)
  767. REST_8GPRS(14, r1)
  768. REST_10GPRS(22, r1)
  769. ld r4,_CCR(r1)
  770. mtcr r4
  771. ld r5,_CTR(r1)
  772. mtctr r5
  773. ld r6,_XER(r1)
  774. mtspr SPRN_XER,r6
  775. ld r7,_DAR(r1)
  776. mtdar r7
  777. ld r8,_DSISR(r1)
  778. mtdsisr r8
  779. ld r9,_SRR0(r1)
  780. mtsrr0 r9
  781. ld r10,_SRR1(r1)
  782. mtsrr1 r10
  783. addi r1,r1,PROM_FRAME_SIZE
  784. ld r0,16(r1)
  785. mtlr r0
  786. blr
  787. #ifdef CONFIG_FTRACE
  788. #ifdef CONFIG_DYNAMIC_FTRACE
  789. _GLOBAL(mcount)
  790. _GLOBAL(_mcount)
  791. /* Taken from output of objdump from lib64/glibc */
  792. mflr r3
  793. stdu r1, -112(r1)
  794. std r3, 128(r1)
  795. subi r3, r3, MCOUNT_INSN_SIZE
  796. .globl mcount_call
  797. mcount_call:
  798. bl ftrace_stub
  799. nop
  800. ld r0, 128(r1)
  801. mtlr r0
  802. addi r1, r1, 112
  803. blr
  804. _GLOBAL(ftrace_caller)
  805. /* Taken from output of objdump from lib64/glibc */
  806. mflr r3
  807. ld r11, 0(r1)
  808. stdu r1, -112(r1)
  809. std r3, 128(r1)
  810. ld r4, 16(r11)
  811. subi r3, r3, MCOUNT_INSN_SIZE
  812. .globl ftrace_call
  813. ftrace_call:
  814. bl ftrace_stub
  815. nop
  816. ld r0, 128(r1)
  817. mtlr r0
  818. addi r1, r1, 112
  819. _GLOBAL(ftrace_stub)
  820. blr
  821. #else
  822. _GLOBAL(mcount)
  823. blr
  824. _GLOBAL(_mcount)
  825. /* Taken from output of objdump from lib64/glibc */
  826. mflr r3
  827. ld r11, 0(r1)
  828. stdu r1, -112(r1)
  829. std r3, 128(r1)
  830. ld r4, 16(r11)
  831. subi r3, r3, MCOUNT_INSN_SIZE
  832. LOAD_REG_ADDR(r5,ftrace_trace_function)
  833. ld r5,0(r5)
  834. ld r5,0(r5)
  835. mtctr r5
  836. bctrl
  837. nop
  838. ld r0, 128(r1)
  839. mtlr r0
  840. addi r1, r1, 112
  841. _GLOBAL(ftrace_stub)
  842. blr
  843. #endif
  844. #endif