entry_64.S 27 KB

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