entry_64.S 19 KB

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