entry_64.S 26 KB

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