entry_64.S 20 KB

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