entry_64.S 23 KB

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