entry_64.S 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  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 <linux/err.h>
  22. #include <linux/magic.h>
  23. #include <asm/unistd.h>
  24. #include <asm/processor.h>
  25. #include <asm/page.h>
  26. #include <asm/mmu.h>
  27. #include <asm/thread_info.h>
  28. #include <asm/ppc_asm.h>
  29. #include <asm/asm-offsets.h>
  30. #include <asm/cputable.h>
  31. #include <asm/firmware.h>
  32. #include <asm/bug.h>
  33. #include <asm/ptrace.h>
  34. #include <asm/irqflags.h>
  35. #include <asm/ftrace.h>
  36. #include <asm/hw_irq.h>
  37. #include <asm/context_tracking.h>
  38. #include <asm/tm.h>
  39. #include <asm/ppc-opcode.h>
  40. /*
  41. * System calls.
  42. */
  43. .section ".toc","aw"
  44. SYS_CALL_TABLE:
  45. .tc sys_call_table[TC],sys_call_table
  46. /* This value is used to mark exception frames on the stack. */
  47. exception_marker:
  48. .tc ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
  49. .section ".text"
  50. .align 7
  51. .globl system_call_common
  52. system_call_common:
  53. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  54. BEGIN_FTR_SECTION
  55. extrdi. r10, r12, 1, (63-MSR_TS_T_LG) /* transaction active? */
  56. bne tabort_syscall
  57. END_FTR_SECTION_IFSET(CPU_FTR_TM)
  58. #endif
  59. andi. r10,r12,MSR_PR
  60. mr r10,r1
  61. addi r1,r1,-INT_FRAME_SIZE
  62. beq- 1f
  63. ld r1,PACAKSAVE(r13)
  64. 1: std r10,0(r1)
  65. std r11,_NIP(r1)
  66. std r12,_MSR(r1)
  67. std r0,GPR0(r1)
  68. std r10,GPR1(r1)
  69. beq 2f /* if from kernel mode */
  70. ACCOUNT_CPU_USER_ENTRY(r13, r10, r11)
  71. 2: std r2,GPR2(r1)
  72. std r3,GPR3(r1)
  73. mfcr r2
  74. std r4,GPR4(r1)
  75. std r5,GPR5(r1)
  76. std r6,GPR6(r1)
  77. std r7,GPR7(r1)
  78. std r8,GPR8(r1)
  79. li r11,0
  80. std r11,GPR9(r1)
  81. std r11,GPR10(r1)
  82. std r11,GPR11(r1)
  83. std r11,GPR12(r1)
  84. std r11,_XER(r1)
  85. std r11,_CTR(r1)
  86. std r9,GPR13(r1)
  87. mflr r10
  88. /*
  89. * This clears CR0.SO (bit 28), which is the error indication on
  90. * return from this system call.
  91. */
  92. rldimi r2,r11,28,(63-28)
  93. li r11,0xc01
  94. std r10,_LINK(r1)
  95. std r11,_TRAP(r1)
  96. std r3,ORIG_GPR3(r1)
  97. std r2,_CCR(r1)
  98. ld r2,PACATOC(r13)
  99. addi r9,r1,STACK_FRAME_OVERHEAD
  100. ld r11,exception_marker@toc(r2)
  101. std r11,-16(r9) /* "regshere" marker */
  102. #if defined(CONFIG_VIRT_CPU_ACCOUNTING_NATIVE) && defined(CONFIG_PPC_SPLPAR)
  103. BEGIN_FW_FTR_SECTION
  104. beq 33f
  105. /* if from user, see if there are any DTL entries to process */
  106. ld r10,PACALPPACAPTR(r13) /* get ptr to VPA */
  107. ld r11,PACA_DTL_RIDX(r13) /* get log read index */
  108. addi r10,r10,LPPACA_DTLIDX
  109. LDX_BE r10,0,r10 /* get log write index */
  110. cmpd cr1,r11,r10
  111. beq+ cr1,33f
  112. bl accumulate_stolen_time
  113. REST_GPR(0,r1)
  114. REST_4GPRS(3,r1)
  115. REST_2GPRS(7,r1)
  116. addi r9,r1,STACK_FRAME_OVERHEAD
  117. 33:
  118. END_FW_FTR_SECTION_IFSET(FW_FEATURE_SPLPAR)
  119. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE && CONFIG_PPC_SPLPAR */
  120. /*
  121. * A syscall should always be called with interrupts enabled
  122. * so we just unconditionally hard-enable here. When some kind
  123. * of irq tracing is used, we additionally check that condition
  124. * is correct
  125. */
  126. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_BUG)
  127. lbz r10,PACASOFTIRQEN(r13)
  128. xori r10,r10,1
  129. 1: tdnei r10,0
  130. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
  131. #endif
  132. #ifdef CONFIG_PPC_BOOK3E
  133. wrteei 1
  134. #else
  135. ld r11,PACAKMSR(r13)
  136. ori r11,r11,MSR_EE
  137. mtmsrd r11,1
  138. #endif /* CONFIG_PPC_BOOK3E */
  139. /* We do need to set SOFTE in the stack frame or the return
  140. * from interrupt will be painful
  141. */
  142. li r10,1
  143. std r10,SOFTE(r1)
  144. CURRENT_THREAD_INFO(r11, r1)
  145. ld r10,TI_FLAGS(r11)
  146. andi. r11,r10,_TIF_SYSCALL_DOTRACE
  147. bne syscall_dotrace /* does not return */
  148. cmpldi 0,r0,NR_syscalls
  149. bge- syscall_enosys
  150. system_call: /* label this so stack traces look sane */
  151. /*
  152. * Need to vector to 32 Bit or default sys_call_table here,
  153. * based on caller's run-mode / personality.
  154. */
  155. ld r11,SYS_CALL_TABLE@toc(2)
  156. andi. r10,r10,_TIF_32BIT
  157. beq 15f
  158. addi r11,r11,8 /* use 32-bit syscall entries */
  159. clrldi r3,r3,32
  160. clrldi r4,r4,32
  161. clrldi r5,r5,32
  162. clrldi r6,r6,32
  163. clrldi r7,r7,32
  164. clrldi r8,r8,32
  165. 15:
  166. slwi r0,r0,4
  167. ldx r12,r11,r0 /* Fetch system call handler [ptr] */
  168. mtctr r12
  169. bctrl /* Call handler */
  170. .Lsyscall_exit:
  171. std r3,RESULT(r1)
  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,-MAX_ERRNO
  200. andi. r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
  201. bne- syscall_exit_work
  202. andi. r0,r8,MSR_FP
  203. beq 2f
  204. #ifdef CONFIG_ALTIVEC
  205. andis. r0,r8,MSR_VEC@h
  206. bne 3f
  207. #endif
  208. 2: addi r3,r1,STACK_FRAME_OVERHEAD
  209. #ifdef CONFIG_PPC_BOOK3S
  210. mtmsrd r10,1 /* Restore RI */
  211. #endif
  212. bl restore_math
  213. #ifdef CONFIG_PPC_BOOK3S
  214. ld r10,PACAKMSR(r13)
  215. li r9,MSR_RI
  216. andc r11,r10,r9 /* Re-clear RI */
  217. mtmsrd r11,1
  218. #endif
  219. ld r8,_MSR(r1)
  220. ld r3,RESULT(r1)
  221. li r11,-MAX_ERRNO
  222. 3: cmpld r3,r11
  223. ld r5,_CCR(r1)
  224. bge- syscall_error
  225. .Lsyscall_error_cont:
  226. ld r7,_NIP(r1)
  227. BEGIN_FTR_SECTION
  228. stdcx. r0,0,r1 /* to clear the reservation */
  229. END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  230. andi. r6,r8,MSR_PR
  231. ld r4,_LINK(r1)
  232. beq- 1f
  233. ACCOUNT_CPU_USER_EXIT(r13, r11, r12)
  234. BEGIN_FTR_SECTION
  235. HMT_MEDIUM_LOW
  236. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  237. ld r13,GPR13(r1) /* only restore r13 if returning to usermode */
  238. 1: ld r2,GPR2(r1)
  239. ld r1,GPR1(r1)
  240. mtlr r4
  241. mtcr r5
  242. mtspr SPRN_SRR0,r7
  243. mtspr SPRN_SRR1,r8
  244. RFI
  245. b . /* prevent speculative execution */
  246. syscall_error:
  247. oris r5,r5,0x1000 /* Set SO bit in CR */
  248. neg r3,r3
  249. std r5,_CCR(r1)
  250. b .Lsyscall_error_cont
  251. /* Traced system call support */
  252. syscall_dotrace:
  253. bl save_nvgprs
  254. addi r3,r1,STACK_FRAME_OVERHEAD
  255. bl do_syscall_trace_enter
  256. /*
  257. * We use the return value of do_syscall_trace_enter() as the syscall
  258. * number. If the syscall was rejected for any reason do_syscall_trace_enter()
  259. * returns an invalid syscall number and the test below against
  260. * NR_syscalls will fail.
  261. */
  262. mr r0,r3
  263. /* Restore argument registers just clobbered and/or possibly changed. */
  264. ld r3,GPR3(r1)
  265. ld r4,GPR4(r1)
  266. ld r5,GPR5(r1)
  267. ld r6,GPR6(r1)
  268. ld r7,GPR7(r1)
  269. ld r8,GPR8(r1)
  270. /* Repopulate r9 and r10 for the system_call path */
  271. addi r9,r1,STACK_FRAME_OVERHEAD
  272. CURRENT_THREAD_INFO(r10, r1)
  273. ld r10,TI_FLAGS(r10)
  274. cmpldi r0,NR_syscalls
  275. blt+ system_call
  276. /* Return code is already in r3 thanks to do_syscall_trace_enter() */
  277. b .Lsyscall_exit
  278. syscall_enosys:
  279. li r3,-ENOSYS
  280. b .Lsyscall_exit
  281. syscall_exit_work:
  282. #ifdef CONFIG_PPC_BOOK3S
  283. mtmsrd r10,1 /* Restore RI */
  284. #endif
  285. /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
  286. If TIF_NOERROR is set, just save r3 as it is. */
  287. andi. r0,r9,_TIF_RESTOREALL
  288. beq+ 0f
  289. REST_NVGPRS(r1)
  290. b 2f
  291. 0: cmpld r3,r11 /* r11 is -MAX_ERRNO */
  292. blt+ 1f
  293. andi. r0,r9,_TIF_NOERROR
  294. bne- 1f
  295. ld r5,_CCR(r1)
  296. neg r3,r3
  297. oris r5,r5,0x1000 /* Set SO bit in CR */
  298. std r5,_CCR(r1)
  299. 1: std r3,GPR3(r1)
  300. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  301. beq 4f
  302. /* Clear per-syscall TIF flags if any are set. */
  303. li r11,_TIF_PERSYSCALL_MASK
  304. addi r12,r12,TI_FLAGS
  305. 3: ldarx r10,0,r12
  306. andc r10,r10,r11
  307. stdcx. r10,0,r12
  308. bne- 3b
  309. subi r12,r12,TI_FLAGS
  310. 4: /* Anything else left to do? */
  311. BEGIN_FTR_SECTION
  312. lis r3,INIT_PPR@highest /* Set thread.ppr = 3 */
  313. ld r10,PACACURRENT(r13)
  314. sldi r3,r3,32 /* bits 11-13 are used for ppr */
  315. std r3,TASKTHREADPPR(r10)
  316. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  317. andi. r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP)
  318. beq ret_from_except_lite
  319. /* Re-enable interrupts */
  320. #ifdef CONFIG_PPC_BOOK3E
  321. wrteei 1
  322. #else
  323. ld r10,PACAKMSR(r13)
  324. ori r10,r10,MSR_EE
  325. mtmsrd r10,1
  326. #endif /* CONFIG_PPC_BOOK3E */
  327. bl save_nvgprs
  328. addi r3,r1,STACK_FRAME_OVERHEAD
  329. bl do_syscall_trace_leave
  330. b ret_from_except
  331. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  332. tabort_syscall:
  333. /* Firstly we need to enable TM in the kernel */
  334. mfmsr r10
  335. li r9, 1
  336. rldimi r10, r9, MSR_TM_LG, 63-MSR_TM_LG
  337. mtmsrd r10, 0
  338. /* tabort, this dooms the transaction, nothing else */
  339. li r9, (TM_CAUSE_SYSCALL|TM_CAUSE_PERSISTENT)
  340. TABORT(R9)
  341. /*
  342. * Return directly to userspace. We have corrupted user register state,
  343. * but userspace will never see that register state. Execution will
  344. * resume after the tbegin of the aborted transaction with the
  345. * checkpointed register state.
  346. */
  347. li r9, MSR_RI
  348. andc r10, r10, r9
  349. mtmsrd r10, 1
  350. mtspr SPRN_SRR0, r11
  351. mtspr SPRN_SRR1, r12
  352. rfid
  353. b . /* prevent speculative execution */
  354. #endif
  355. /* Save non-volatile GPRs, if not already saved. */
  356. _GLOBAL(save_nvgprs)
  357. ld r11,_TRAP(r1)
  358. andi. r0,r11,1
  359. beqlr-
  360. SAVE_NVGPRS(r1)
  361. clrrdi r0,r11,1
  362. std r0,_TRAP(r1)
  363. blr
  364. /*
  365. * The sigsuspend and rt_sigsuspend system calls can call do_signal
  366. * and thus put the process into the stopped state where we might
  367. * want to examine its user state with ptrace. Therefore we need
  368. * to save all the nonvolatile registers (r14 - r31) before calling
  369. * the C code. Similarly, fork, vfork and clone need the full
  370. * register state on the stack so that it can be copied to the child.
  371. */
  372. _GLOBAL(ppc_fork)
  373. bl save_nvgprs
  374. bl sys_fork
  375. b .Lsyscall_exit
  376. _GLOBAL(ppc_vfork)
  377. bl save_nvgprs
  378. bl sys_vfork
  379. b .Lsyscall_exit
  380. _GLOBAL(ppc_clone)
  381. bl save_nvgprs
  382. bl sys_clone
  383. b .Lsyscall_exit
  384. _GLOBAL(ppc32_swapcontext)
  385. bl save_nvgprs
  386. bl compat_sys_swapcontext
  387. b .Lsyscall_exit
  388. _GLOBAL(ppc64_swapcontext)
  389. bl save_nvgprs
  390. bl sys_swapcontext
  391. b .Lsyscall_exit
  392. _GLOBAL(ppc_switch_endian)
  393. bl save_nvgprs
  394. bl sys_switch_endian
  395. b .Lsyscall_exit
  396. _GLOBAL(ret_from_fork)
  397. bl schedule_tail
  398. REST_NVGPRS(r1)
  399. li r3,0
  400. b .Lsyscall_exit
  401. _GLOBAL(ret_from_kernel_thread)
  402. bl schedule_tail
  403. REST_NVGPRS(r1)
  404. mtlr r14
  405. mr r3,r15
  406. #ifdef PPC64_ELF_ABI_v2
  407. mr r12,r14
  408. #endif
  409. blrl
  410. li r3,0
  411. b .Lsyscall_exit
  412. /*
  413. * This routine switches between two different tasks. The process
  414. * state of one is saved on its kernel stack. Then the state
  415. * of the other is restored from its kernel stack. The memory
  416. * management hardware is updated to the second process's state.
  417. * Finally, we can return to the second process, via ret_from_except.
  418. * On entry, r3 points to the THREAD for the current task, r4
  419. * points to the THREAD for the new task.
  420. *
  421. * Note: there are two ways to get to the "going out" portion
  422. * of this code; either by coming in via the entry (_switch)
  423. * or via "fork" which must set up an environment equivalent
  424. * to the "_switch" path. If you change this you'll have to change
  425. * the fork code also.
  426. *
  427. * The code which creates the new task context is in 'copy_thread'
  428. * in arch/powerpc/kernel/process.c
  429. */
  430. .align 7
  431. _GLOBAL(_switch)
  432. mflr r0
  433. std r0,16(r1)
  434. stdu r1,-SWITCH_FRAME_SIZE(r1)
  435. /* r3-r13 are caller saved -- Cort */
  436. SAVE_8GPRS(14, r1)
  437. SAVE_10GPRS(22, r1)
  438. std r0,_NIP(r1) /* Return to switch caller */
  439. mfcr r23
  440. std r23,_CCR(r1)
  441. std r1,KSP(r3) /* Set old stack pointer */
  442. #ifdef CONFIG_SMP
  443. /* We need a sync somewhere here to make sure that if the
  444. * previous task gets rescheduled on another CPU, it sees all
  445. * stores it has performed on this one.
  446. */
  447. sync
  448. #endif /* CONFIG_SMP */
  449. /*
  450. * If we optimise away the clear of the reservation in system
  451. * calls because we know the CPU tracks the address of the
  452. * reservation, then we need to clear it here to cover the
  453. * case that the kernel context switch path has no larx
  454. * instructions.
  455. */
  456. BEGIN_FTR_SECTION
  457. ldarx r6,0,r1
  458. END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
  459. BEGIN_FTR_SECTION
  460. /*
  461. * A cp_abort (copy paste abort) here ensures that when context switching, a
  462. * copy from one process can't leak into the paste of another.
  463. */
  464. PPC_CP_ABORT
  465. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  466. #ifdef CONFIG_PPC_BOOK3S
  467. /* Cancel all explict user streams as they will have no use after context
  468. * switch and will stop the HW from creating streams itself
  469. */
  470. DCBT_STOP_ALL_STREAM_IDS(r6)
  471. #endif
  472. addi r6,r4,-THREAD /* Convert THREAD to 'current' */
  473. std r6,PACACURRENT(r13) /* Set new 'current' */
  474. ld r8,KSP(r4) /* new stack pointer */
  475. #ifdef CONFIG_PPC_STD_MMU_64
  476. BEGIN_MMU_FTR_SECTION
  477. b 2f
  478. END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
  479. BEGIN_FTR_SECTION
  480. clrrdi r6,r8,28 /* get its ESID */
  481. clrrdi r9,r1,28 /* get current sp ESID */
  482. FTR_SECTION_ELSE
  483. clrrdi r6,r8,40 /* get its 1T ESID */
  484. clrrdi r9,r1,40 /* get current sp 1T ESID */
  485. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_1T_SEGMENT)
  486. clrldi. r0,r6,2 /* is new ESID c00000000? */
  487. cmpd cr1,r6,r9 /* or is new ESID the same as current ESID? */
  488. cror eq,4*cr1+eq,eq
  489. beq 2f /* if yes, don't slbie it */
  490. /* Bolt in the new stack SLB entry */
  491. ld r7,KSP_VSID(r4) /* Get new stack's VSID */
  492. oris r0,r6,(SLB_ESID_V)@h
  493. ori r0,r0,(SLB_NUM_BOLTED-1)@l
  494. BEGIN_FTR_SECTION
  495. li r9,MMU_SEGSIZE_1T /* insert B field */
  496. oris r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
  497. rldimi r7,r9,SLB_VSID_SSIZE_SHIFT,0
  498. END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
  499. /* Update the last bolted SLB. No write barriers are needed
  500. * here, provided we only update the current CPU's SLB shadow
  501. * buffer.
  502. */
  503. ld r9,PACA_SLBSHADOWPTR(r13)
  504. li r12,0
  505. std r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
  506. li r12,SLBSHADOW_STACKVSID
  507. STDX_BE r7,r12,r9 /* Save VSID */
  508. li r12,SLBSHADOW_STACKESID
  509. STDX_BE r0,r12,r9 /* Save ESID */
  510. /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
  511. * we have 1TB segments, the only CPUs known to have the errata
  512. * only support less than 1TB of system memory and we'll never
  513. * actually hit this code path.
  514. */
  515. slbie r6
  516. slbie r6 /* Workaround POWER5 < DD2.1 issue */
  517. slbmte r7,r0
  518. isync
  519. 2:
  520. #endif /* CONFIG_PPC_STD_MMU_64 */
  521. CURRENT_THREAD_INFO(r7, r8) /* base of new stack */
  522. /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
  523. because we don't need to leave the 288-byte ABI gap at the
  524. top of the kernel stack. */
  525. addi r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
  526. mr r1,r8 /* start using new stack pointer */
  527. std r7,PACAKSAVE(r13)
  528. ld r6,_CCR(r1)
  529. mtcrf 0xFF,r6
  530. /* r3-r13 are destroyed -- Cort */
  531. REST_8GPRS(14, r1)
  532. REST_10GPRS(22, r1)
  533. /* convert old thread to its task_struct for return value */
  534. addi r3,r3,-THREAD
  535. ld r7,_NIP(r1) /* Return to _switch caller in new task */
  536. mtlr r7
  537. addi r1,r1,SWITCH_FRAME_SIZE
  538. blr
  539. .align 7
  540. _GLOBAL(ret_from_except)
  541. ld r11,_TRAP(r1)
  542. andi. r0,r11,1
  543. bne ret_from_except_lite
  544. REST_NVGPRS(r1)
  545. _GLOBAL(ret_from_except_lite)
  546. /*
  547. * Disable interrupts so that current_thread_info()->flags
  548. * can't change between when we test it and when we return
  549. * from the interrupt.
  550. */
  551. #ifdef CONFIG_PPC_BOOK3E
  552. wrteei 0
  553. #else
  554. ld r10,PACAKMSR(r13) /* Get kernel MSR without EE */
  555. mtmsrd r10,1 /* Update machine state */
  556. #endif /* CONFIG_PPC_BOOK3E */
  557. CURRENT_THREAD_INFO(r9, r1)
  558. ld r3,_MSR(r1)
  559. #ifdef CONFIG_PPC_BOOK3E
  560. ld r10,PACACURRENT(r13)
  561. #endif /* CONFIG_PPC_BOOK3E */
  562. ld r4,TI_FLAGS(r9)
  563. andi. r3,r3,MSR_PR
  564. beq resume_kernel
  565. #ifdef CONFIG_PPC_BOOK3E
  566. lwz r3,(THREAD+THREAD_DBCR0)(r10)
  567. #endif /* CONFIG_PPC_BOOK3E */
  568. /* Check current_thread_info()->flags */
  569. andi. r0,r4,_TIF_USER_WORK_MASK
  570. bne 1f
  571. #ifdef CONFIG_PPC_BOOK3E
  572. /*
  573. * Check to see if the dbcr0 register is set up to debug.
  574. * Use the internal debug mode bit to do this.
  575. */
  576. andis. r0,r3,DBCR0_IDM@h
  577. beq restore
  578. mfmsr r0
  579. rlwinm r0,r0,0,~MSR_DE /* Clear MSR.DE */
  580. mtmsr r0
  581. mtspr SPRN_DBCR0,r3
  582. li r10, -1
  583. mtspr SPRN_DBSR,r10
  584. b restore
  585. #else
  586. addi r3,r1,STACK_FRAME_OVERHEAD
  587. bl restore_math
  588. b restore
  589. #endif
  590. 1: andi. r0,r4,_TIF_NEED_RESCHED
  591. beq 2f
  592. bl restore_interrupts
  593. SCHEDULE_USER
  594. b ret_from_except_lite
  595. 2:
  596. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  597. andi. r0,r4,_TIF_USER_WORK_MASK & ~_TIF_RESTORE_TM
  598. bne 3f /* only restore TM if nothing else to do */
  599. addi r3,r1,STACK_FRAME_OVERHEAD
  600. bl restore_tm_state
  601. b restore
  602. 3:
  603. #endif
  604. bl save_nvgprs
  605. /*
  606. * Use a non volatile GPR to save and restore our thread_info flags
  607. * across the call to restore_interrupts.
  608. */
  609. mr r30,r4
  610. bl restore_interrupts
  611. mr r4,r30
  612. addi r3,r1,STACK_FRAME_OVERHEAD
  613. bl do_notify_resume
  614. b ret_from_except
  615. resume_kernel:
  616. /* check current_thread_info, _TIF_EMULATE_STACK_STORE */
  617. andis. r8,r4,_TIF_EMULATE_STACK_STORE@h
  618. beq+ 1f
  619. addi r8,r1,INT_FRAME_SIZE /* Get the kprobed function entry */
  620. lwz r3,GPR1(r1)
  621. subi r3,r3,INT_FRAME_SIZE /* dst: Allocate a trampoline exception frame */
  622. mr r4,r1 /* src: current exception frame */
  623. mr r1,r3 /* Reroute the trampoline frame to r1 */
  624. /* Copy from the original to the trampoline. */
  625. li r5,INT_FRAME_SIZE/8 /* size: INT_FRAME_SIZE */
  626. li r6,0 /* start offset: 0 */
  627. mtctr r5
  628. 2: ldx r0,r6,r4
  629. stdx r0,r6,r3
  630. addi r6,r6,8
  631. bdnz 2b
  632. /* Do real store operation to complete stwu */
  633. lwz r5,GPR1(r1)
  634. std r8,0(r5)
  635. /* Clear _TIF_EMULATE_STACK_STORE flag */
  636. lis r11,_TIF_EMULATE_STACK_STORE@h
  637. addi r5,r9,TI_FLAGS
  638. 0: ldarx r4,0,r5
  639. andc r4,r4,r11
  640. stdcx. r4,0,r5
  641. bne- 0b
  642. 1:
  643. #ifdef CONFIG_PREEMPT
  644. /* Check if we need to preempt */
  645. andi. r0,r4,_TIF_NEED_RESCHED
  646. beq+ restore
  647. /* Check that preempt_count() == 0 and interrupts are enabled */
  648. lwz r8,TI_PREEMPT(r9)
  649. cmpwi cr1,r8,0
  650. ld r0,SOFTE(r1)
  651. cmpdi r0,0
  652. crandc eq,cr1*4+eq,eq
  653. bne restore
  654. /*
  655. * Here we are preempting the current task. We want to make
  656. * sure we are soft-disabled first and reconcile irq state.
  657. */
  658. RECONCILE_IRQ_STATE(r3,r4)
  659. 1: bl preempt_schedule_irq
  660. /* Re-test flags and eventually loop */
  661. CURRENT_THREAD_INFO(r9, r1)
  662. ld r4,TI_FLAGS(r9)
  663. andi. r0,r4,_TIF_NEED_RESCHED
  664. bne 1b
  665. /*
  666. * arch_local_irq_restore() from preempt_schedule_irq above may
  667. * enable hard interrupt but we really should disable interrupts
  668. * when we return from the interrupt, and so that we don't get
  669. * interrupted after loading SRR0/1.
  670. */
  671. #ifdef CONFIG_PPC_BOOK3E
  672. wrteei 0
  673. #else
  674. ld r10,PACAKMSR(r13) /* Get kernel MSR without EE */
  675. mtmsrd r10,1 /* Update machine state */
  676. #endif /* CONFIG_PPC_BOOK3E */
  677. #endif /* CONFIG_PREEMPT */
  678. .globl fast_exc_return_irq
  679. fast_exc_return_irq:
  680. restore:
  681. /*
  682. * This is the main kernel exit path. First we check if we
  683. * are about to re-enable interrupts
  684. */
  685. ld r5,SOFTE(r1)
  686. lbz r6,PACASOFTIRQEN(r13)
  687. cmpwi cr0,r5,0
  688. beq restore_irq_off
  689. /* We are enabling, were we already enabled ? Yes, just return */
  690. cmpwi cr0,r6,1
  691. beq cr0,do_restore
  692. /*
  693. * We are about to soft-enable interrupts (we are hard disabled
  694. * at this point). We check if there's anything that needs to
  695. * be replayed first.
  696. */
  697. lbz r0,PACAIRQHAPPENED(r13)
  698. cmpwi cr0,r0,0
  699. bne- restore_check_irq_replay
  700. /*
  701. * Get here when nothing happened while soft-disabled, just
  702. * soft-enable and move-on. We will hard-enable as a side
  703. * effect of rfi
  704. */
  705. restore_no_replay:
  706. TRACE_ENABLE_INTS
  707. li r0,1
  708. stb r0,PACASOFTIRQEN(r13);
  709. /*
  710. * Final return path. BookE is handled in a different file
  711. */
  712. do_restore:
  713. #ifdef CONFIG_PPC_BOOK3E
  714. b exception_return_book3e
  715. #else
  716. /*
  717. * Clear the reservation. If we know the CPU tracks the address of
  718. * the reservation then we can potentially save some cycles and use
  719. * a larx. On POWER6 and POWER7 this is significantly faster.
  720. */
  721. BEGIN_FTR_SECTION
  722. stdcx. r0,0,r1 /* to clear the reservation */
  723. FTR_SECTION_ELSE
  724. ldarx r4,0,r1
  725. ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  726. /*
  727. * Some code path such as load_up_fpu or altivec return directly
  728. * here. They run entirely hard disabled and do not alter the
  729. * interrupt state. They also don't use lwarx/stwcx. and thus
  730. * are known not to leave dangling reservations.
  731. */
  732. .globl fast_exception_return
  733. fast_exception_return:
  734. ld r3,_MSR(r1)
  735. ld r4,_CTR(r1)
  736. ld r0,_LINK(r1)
  737. mtctr r4
  738. mtlr r0
  739. ld r4,_XER(r1)
  740. mtspr SPRN_XER,r4
  741. REST_8GPRS(5, r1)
  742. andi. r0,r3,MSR_RI
  743. beq- unrecov_restore
  744. /* Load PPR from thread struct before we clear MSR:RI */
  745. BEGIN_FTR_SECTION
  746. ld r2,PACACURRENT(r13)
  747. ld r2,TASKTHREADPPR(r2)
  748. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  749. /*
  750. * Clear RI before restoring r13. If we are returning to
  751. * userspace and we take an exception after restoring r13,
  752. * we end up corrupting the userspace r13 value.
  753. */
  754. ld r4,PACAKMSR(r13) /* Get kernel MSR without EE */
  755. andc r4,r4,r0 /* r0 contains MSR_RI here */
  756. mtmsrd r4,1
  757. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  758. /* TM debug */
  759. std r3, PACATMSCRATCH(r13) /* Stash returned-to MSR */
  760. #endif
  761. /*
  762. * r13 is our per cpu area, only restore it if we are returning to
  763. * userspace the value stored in the stack frame may belong to
  764. * another CPU.
  765. */
  766. andi. r0,r3,MSR_PR
  767. beq 1f
  768. BEGIN_FTR_SECTION
  769. mtspr SPRN_PPR,r2 /* Restore PPR */
  770. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  771. ACCOUNT_CPU_USER_EXIT(r13, r2, r4)
  772. REST_GPR(13, r1)
  773. 1:
  774. mtspr SPRN_SRR1,r3
  775. ld r2,_CCR(r1)
  776. mtcrf 0xFF,r2
  777. ld r2,_NIP(r1)
  778. mtspr SPRN_SRR0,r2
  779. ld r0,GPR0(r1)
  780. ld r2,GPR2(r1)
  781. ld r3,GPR3(r1)
  782. ld r4,GPR4(r1)
  783. ld r1,GPR1(r1)
  784. rfid
  785. b . /* prevent speculative execution */
  786. #endif /* CONFIG_PPC_BOOK3E */
  787. /*
  788. * We are returning to a context with interrupts soft disabled.
  789. *
  790. * However, we may also about to hard enable, so we need to
  791. * make sure that in this case, we also clear PACA_IRQ_HARD_DIS
  792. * or that bit can get out of sync and bad things will happen
  793. */
  794. restore_irq_off:
  795. ld r3,_MSR(r1)
  796. lbz r7,PACAIRQHAPPENED(r13)
  797. andi. r0,r3,MSR_EE
  798. beq 1f
  799. rlwinm r7,r7,0,~PACA_IRQ_HARD_DIS
  800. stb r7,PACAIRQHAPPENED(r13)
  801. 1: li r0,0
  802. stb r0,PACASOFTIRQEN(r13);
  803. TRACE_DISABLE_INTS
  804. b do_restore
  805. /*
  806. * Something did happen, check if a re-emit is needed
  807. * (this also clears paca->irq_happened)
  808. */
  809. restore_check_irq_replay:
  810. /* XXX: We could implement a fast path here where we check
  811. * for irq_happened being just 0x01, in which case we can
  812. * clear it and return. That means that we would potentially
  813. * miss a decrementer having wrapped all the way around.
  814. *
  815. * Still, this might be useful for things like hash_page
  816. */
  817. bl __check_irq_replay
  818. cmpwi cr0,r3,0
  819. beq restore_no_replay
  820. /*
  821. * We need to re-emit an interrupt. We do so by re-using our
  822. * existing exception frame. We first change the trap value,
  823. * but we need to ensure we preserve the low nibble of it
  824. */
  825. ld r4,_TRAP(r1)
  826. clrldi r4,r4,60
  827. or r4,r4,r3
  828. std r4,_TRAP(r1)
  829. /*
  830. * Then find the right handler and call it. Interrupts are
  831. * still soft-disabled and we keep them that way.
  832. */
  833. cmpwi cr0,r3,0x500
  834. bne 1f
  835. addi r3,r1,STACK_FRAME_OVERHEAD;
  836. bl do_IRQ
  837. b ret_from_except
  838. 1: cmpwi cr0,r3,0xe60
  839. bne 1f
  840. addi r3,r1,STACK_FRAME_OVERHEAD;
  841. bl handle_hmi_exception
  842. b ret_from_except
  843. 1: cmpwi cr0,r3,0x900
  844. bne 1f
  845. addi r3,r1,STACK_FRAME_OVERHEAD;
  846. bl timer_interrupt
  847. b ret_from_except
  848. #ifdef CONFIG_PPC_DOORBELL
  849. 1:
  850. #ifdef CONFIG_PPC_BOOK3E
  851. cmpwi cr0,r3,0x280
  852. #else
  853. BEGIN_FTR_SECTION
  854. cmpwi cr0,r3,0xe80
  855. FTR_SECTION_ELSE
  856. cmpwi cr0,r3,0xa00
  857. ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
  858. #endif /* CONFIG_PPC_BOOK3E */
  859. bne 1f
  860. addi r3,r1,STACK_FRAME_OVERHEAD;
  861. bl doorbell_exception
  862. b ret_from_except
  863. #endif /* CONFIG_PPC_DOORBELL */
  864. 1: b ret_from_except /* What else to do here ? */
  865. unrecov_restore:
  866. addi r3,r1,STACK_FRAME_OVERHEAD
  867. bl unrecoverable_exception
  868. b unrecov_restore
  869. #ifdef CONFIG_PPC_RTAS
  870. /*
  871. * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
  872. * called with the MMU off.
  873. *
  874. * In addition, we need to be in 32b mode, at least for now.
  875. *
  876. * Note: r3 is an input parameter to rtas, so don't trash it...
  877. */
  878. _GLOBAL(enter_rtas)
  879. mflr r0
  880. std r0,16(r1)
  881. stdu r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
  882. /* Because RTAS is running in 32b mode, it clobbers the high order half
  883. * of all registers that it saves. We therefore save those registers
  884. * RTAS might touch to the stack. (r0, r3-r13 are caller saved)
  885. */
  886. SAVE_GPR(2, r1) /* Save the TOC */
  887. SAVE_GPR(13, r1) /* Save paca */
  888. SAVE_8GPRS(14, r1) /* Save the non-volatiles */
  889. SAVE_10GPRS(22, r1) /* ditto */
  890. mfcr r4
  891. std r4,_CCR(r1)
  892. mfctr r5
  893. std r5,_CTR(r1)
  894. mfspr r6,SPRN_XER
  895. std r6,_XER(r1)
  896. mfdar r7
  897. std r7,_DAR(r1)
  898. mfdsisr r8
  899. std r8,_DSISR(r1)
  900. /* Temporary workaround to clear CR until RTAS can be modified to
  901. * ignore all bits.
  902. */
  903. li r0,0
  904. mtcr r0
  905. #ifdef CONFIG_BUG
  906. /* There is no way it is acceptable to get here with interrupts enabled,
  907. * check it with the asm equivalent of WARN_ON
  908. */
  909. lbz r0,PACASOFTIRQEN(r13)
  910. 1: tdnei r0,0
  911. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
  912. #endif
  913. /* Hard-disable interrupts */
  914. mfmsr r6
  915. rldicl r7,r6,48,1
  916. rotldi r7,r7,16
  917. mtmsrd r7,1
  918. /* Unfortunately, the stack pointer and the MSR are also clobbered,
  919. * so they are saved in the PACA which allows us to restore
  920. * our original state after RTAS returns.
  921. */
  922. std r1,PACAR1(r13)
  923. std r6,PACASAVEDMSR(r13)
  924. /* Setup our real return addr */
  925. LOAD_REG_ADDR(r4,rtas_return_loc)
  926. clrldi r4,r4,2 /* convert to realmode address */
  927. mtlr r4
  928. li r0,0
  929. ori r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
  930. andc r0,r6,r0
  931. li r9,1
  932. rldicr r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
  933. ori r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI|MSR_LE
  934. andc r6,r0,r9
  935. sync /* disable interrupts so SRR0/1 */
  936. mtmsrd r0 /* don't get trashed */
  937. LOAD_REG_ADDR(r4, rtas)
  938. ld r5,RTASENTRY(r4) /* get the rtas->entry value */
  939. ld r4,RTASBASE(r4) /* get the rtas->base value */
  940. mtspr SPRN_SRR0,r5
  941. mtspr SPRN_SRR1,r6
  942. rfid
  943. b . /* prevent speculative execution */
  944. rtas_return_loc:
  945. FIXUP_ENDIAN
  946. /* relocation is off at this point */
  947. GET_PACA(r4)
  948. clrldi r4,r4,2 /* convert to realmode address */
  949. bcl 20,31,$+4
  950. 0: mflr r3
  951. ld r3,(1f-0b)(r3) /* get &rtas_restore_regs */
  952. mfmsr r6
  953. li r0,MSR_RI
  954. andc r6,r6,r0
  955. sync
  956. mtmsrd r6
  957. ld r1,PACAR1(r4) /* Restore our SP */
  958. ld r4,PACASAVEDMSR(r4) /* Restore our MSR */
  959. mtspr SPRN_SRR0,r3
  960. mtspr SPRN_SRR1,r4
  961. rfid
  962. b . /* prevent speculative execution */
  963. .align 3
  964. 1: .llong rtas_restore_regs
  965. rtas_restore_regs:
  966. /* relocation is on at this point */
  967. REST_GPR(2, r1) /* Restore the TOC */
  968. REST_GPR(13, r1) /* Restore paca */
  969. REST_8GPRS(14, r1) /* Restore the non-volatiles */
  970. REST_10GPRS(22, r1) /* ditto */
  971. GET_PACA(r13)
  972. ld r4,_CCR(r1)
  973. mtcr r4
  974. ld r5,_CTR(r1)
  975. mtctr r5
  976. ld r6,_XER(r1)
  977. mtspr SPRN_XER,r6
  978. ld r7,_DAR(r1)
  979. mtdar r7
  980. ld r8,_DSISR(r1)
  981. mtdsisr r8
  982. addi r1,r1,RTAS_FRAME_SIZE /* Unstack our frame */
  983. ld r0,16(r1) /* get return address */
  984. mtlr r0
  985. blr /* return to caller */
  986. #endif /* CONFIG_PPC_RTAS */
  987. _GLOBAL(enter_prom)
  988. mflr r0
  989. std r0,16(r1)
  990. stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
  991. /* Because PROM is running in 32b mode, it clobbers the high order half
  992. * of all registers that it saves. We therefore save those registers
  993. * PROM might touch to the stack. (r0, r3-r13 are caller saved)
  994. */
  995. SAVE_GPR(2, r1)
  996. SAVE_GPR(13, r1)
  997. SAVE_8GPRS(14, r1)
  998. SAVE_10GPRS(22, r1)
  999. mfcr r10
  1000. mfmsr r11
  1001. std r10,_CCR(r1)
  1002. std r11,_MSR(r1)
  1003. /* Put PROM address in SRR0 */
  1004. mtsrr0 r4
  1005. /* Setup our trampoline return addr in LR */
  1006. bcl 20,31,$+4
  1007. 0: mflr r4
  1008. addi r4,r4,(1f - 0b)
  1009. mtlr r4
  1010. /* Prepare a 32-bit mode big endian MSR
  1011. */
  1012. #ifdef CONFIG_PPC_BOOK3E
  1013. rlwinm r11,r11,0,1,31
  1014. mtsrr1 r11
  1015. rfi
  1016. #else /* CONFIG_PPC_BOOK3E */
  1017. LOAD_REG_IMMEDIATE(r12, MSR_SF | MSR_ISF | MSR_LE)
  1018. andc r11,r11,r12
  1019. mtsrr1 r11
  1020. rfid
  1021. #endif /* CONFIG_PPC_BOOK3E */
  1022. 1: /* Return from OF */
  1023. FIXUP_ENDIAN
  1024. /* Just make sure that r1 top 32 bits didn't get
  1025. * corrupt by OF
  1026. */
  1027. rldicl r1,r1,0,32
  1028. /* Restore the MSR (back to 64 bits) */
  1029. ld r0,_MSR(r1)
  1030. MTMSRD(r0)
  1031. isync
  1032. /* Restore other registers */
  1033. REST_GPR(2, r1)
  1034. REST_GPR(13, r1)
  1035. REST_8GPRS(14, r1)
  1036. REST_10GPRS(22, r1)
  1037. ld r4,_CCR(r1)
  1038. mtcr r4
  1039. addi r1,r1,PROM_FRAME_SIZE
  1040. ld r0,16(r1)
  1041. mtlr r0
  1042. blr
  1043. #ifdef CONFIG_FUNCTION_TRACER
  1044. #ifdef CONFIG_DYNAMIC_FTRACE
  1045. _GLOBAL(mcount)
  1046. _GLOBAL(_mcount)
  1047. mflr r12
  1048. mtctr r12
  1049. mtlr r0
  1050. bctr
  1051. #ifndef CC_USING_MPROFILE_KERNEL
  1052. _GLOBAL_TOC(ftrace_caller)
  1053. /* Taken from output of objdump from lib64/glibc */
  1054. mflr r3
  1055. ld r11, 0(r1)
  1056. stdu r1, -112(r1)
  1057. std r3, 128(r1)
  1058. ld r4, 16(r11)
  1059. subi r3, r3, MCOUNT_INSN_SIZE
  1060. .globl ftrace_call
  1061. ftrace_call:
  1062. bl ftrace_stub
  1063. nop
  1064. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1065. .globl ftrace_graph_call
  1066. ftrace_graph_call:
  1067. b ftrace_graph_stub
  1068. _GLOBAL(ftrace_graph_stub)
  1069. #endif
  1070. ld r0, 128(r1)
  1071. mtlr r0
  1072. addi r1, r1, 112
  1073. #else /* CC_USING_MPROFILE_KERNEL */
  1074. /*
  1075. *
  1076. * ftrace_caller() is the function that replaces _mcount() when ftrace is
  1077. * active.
  1078. *
  1079. * We arrive here after a function A calls function B, and we are the trace
  1080. * function for B. When we enter r1 points to A's stack frame, B has not yet
  1081. * had a chance to allocate one yet.
  1082. *
  1083. * Additionally r2 may point either to the TOC for A, or B, depending on
  1084. * whether B did a TOC setup sequence before calling us.
  1085. *
  1086. * On entry the LR points back to the _mcount() call site, and r0 holds the
  1087. * saved LR as it was on entry to B, ie. the original return address at the
  1088. * call site in A.
  1089. *
  1090. * Our job is to save the register state into a struct pt_regs (on the stack)
  1091. * and then arrange for the ftrace function to be called.
  1092. */
  1093. _GLOBAL(ftrace_caller)
  1094. /* Save the original return address in A's stack frame */
  1095. std r0,LRSAVE(r1)
  1096. /* Create our stack frame + pt_regs */
  1097. stdu r1,-SWITCH_FRAME_SIZE(r1)
  1098. /* Save all gprs to pt_regs */
  1099. SAVE_8GPRS(0,r1)
  1100. SAVE_8GPRS(8,r1)
  1101. SAVE_8GPRS(16,r1)
  1102. SAVE_8GPRS(24,r1)
  1103. /* Load special regs for save below */
  1104. mfmsr r8
  1105. mfctr r9
  1106. mfxer r10
  1107. mfcr r11
  1108. /* Get the _mcount() call site out of LR */
  1109. mflr r7
  1110. /* Save it as pt_regs->nip & pt_regs->link */
  1111. std r7, _NIP(r1)
  1112. std r7, _LINK(r1)
  1113. /* Save callee's TOC in the ABI compliant location */
  1114. std r2, 24(r1)
  1115. ld r2,PACATOC(r13) /* get kernel TOC in r2 */
  1116. addis r3,r2,function_trace_op@toc@ha
  1117. addi r3,r3,function_trace_op@toc@l
  1118. ld r5,0(r3)
  1119. #ifdef CONFIG_LIVEPATCH
  1120. mr r14,r7 /* remember old NIP */
  1121. #endif
  1122. /* Calculate ip from nip-4 into r3 for call below */
  1123. subi r3, r7, MCOUNT_INSN_SIZE
  1124. /* Put the original return address in r4 as parent_ip */
  1125. mr r4, r0
  1126. /* Save special regs */
  1127. std r8, _MSR(r1)
  1128. std r9, _CTR(r1)
  1129. std r10, _XER(r1)
  1130. std r11, _CCR(r1)
  1131. /* Load &pt_regs in r6 for call below */
  1132. addi r6, r1 ,STACK_FRAME_OVERHEAD
  1133. /* ftrace_call(r3, r4, r5, r6) */
  1134. .globl ftrace_call
  1135. ftrace_call:
  1136. bl ftrace_stub
  1137. nop
  1138. /* Load ctr with the possibly modified NIP */
  1139. ld r3, _NIP(r1)
  1140. mtctr r3
  1141. #ifdef CONFIG_LIVEPATCH
  1142. cmpd r14,r3 /* has NIP been altered? */
  1143. #endif
  1144. /* Restore gprs */
  1145. REST_8GPRS(0,r1)
  1146. REST_8GPRS(8,r1)
  1147. REST_8GPRS(16,r1)
  1148. REST_8GPRS(24,r1)
  1149. /* Restore callee's TOC */
  1150. ld r2, 24(r1)
  1151. /* Pop our stack frame */
  1152. addi r1, r1, SWITCH_FRAME_SIZE
  1153. /* Restore original LR for return to B */
  1154. ld r0, LRSAVE(r1)
  1155. mtlr r0
  1156. #ifdef CONFIG_LIVEPATCH
  1157. /* Based on the cmpd above, if the NIP was altered handle livepatch */
  1158. bne- livepatch_handler
  1159. #endif
  1160. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1161. stdu r1, -112(r1)
  1162. .globl ftrace_graph_call
  1163. ftrace_graph_call:
  1164. b ftrace_graph_stub
  1165. _GLOBAL(ftrace_graph_stub)
  1166. addi r1, r1, 112
  1167. #endif
  1168. ld r0,LRSAVE(r1) /* restore callee's lr at _mcount site */
  1169. mtlr r0
  1170. bctr /* jump after _mcount site */
  1171. #endif /* CC_USING_MPROFILE_KERNEL */
  1172. _GLOBAL(ftrace_stub)
  1173. blr
  1174. #ifdef CONFIG_LIVEPATCH
  1175. /*
  1176. * This function runs in the mcount context, between two functions. As
  1177. * such it can only clobber registers which are volatile and used in
  1178. * function linkage.
  1179. *
  1180. * We get here when a function A, calls another function B, but B has
  1181. * been live patched with a new function C.
  1182. *
  1183. * On entry:
  1184. * - we have no stack frame and can not allocate one
  1185. * - LR points back to the original caller (in A)
  1186. * - CTR holds the new NIP in C
  1187. * - r0 & r12 are free
  1188. *
  1189. * r0 can't be used as the base register for a DS-form load or store, so
  1190. * we temporarily shuffle r1 (stack pointer) into r0 and then put it back.
  1191. */
  1192. livepatch_handler:
  1193. CURRENT_THREAD_INFO(r12, r1)
  1194. /* Save stack pointer into r0 */
  1195. mr r0, r1
  1196. /* Allocate 3 x 8 bytes */
  1197. ld r1, TI_livepatch_sp(r12)
  1198. addi r1, r1, 24
  1199. std r1, TI_livepatch_sp(r12)
  1200. /* Save toc & real LR on livepatch stack */
  1201. std r2, -24(r1)
  1202. mflr r12
  1203. std r12, -16(r1)
  1204. /* Store stack end marker */
  1205. lis r12, STACK_END_MAGIC@h
  1206. ori r12, r12, STACK_END_MAGIC@l
  1207. std r12, -8(r1)
  1208. /* Restore real stack pointer */
  1209. mr r1, r0
  1210. /* Put ctr in r12 for global entry and branch there */
  1211. mfctr r12
  1212. bctrl
  1213. /*
  1214. * Now we are returning from the patched function to the original
  1215. * caller A. We are free to use r0 and r12, and we can use r2 until we
  1216. * restore it.
  1217. */
  1218. CURRENT_THREAD_INFO(r12, r1)
  1219. /* Save stack pointer into r0 */
  1220. mr r0, r1
  1221. ld r1, TI_livepatch_sp(r12)
  1222. /* Check stack marker hasn't been trashed */
  1223. lis r2, STACK_END_MAGIC@h
  1224. ori r2, r2, STACK_END_MAGIC@l
  1225. ld r12, -8(r1)
  1226. 1: tdne r12, r2
  1227. EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
  1228. /* Restore LR & toc from livepatch stack */
  1229. ld r12, -16(r1)
  1230. mtlr r12
  1231. ld r2, -24(r1)
  1232. /* Pop livepatch stack frame */
  1233. CURRENT_THREAD_INFO(r12, r0)
  1234. subi r1, r1, 24
  1235. std r1, TI_livepatch_sp(r12)
  1236. /* Restore real stack pointer */
  1237. mr r1, r0
  1238. /* Return to original caller of live patched function */
  1239. blr
  1240. #endif
  1241. #else
  1242. _GLOBAL_TOC(_mcount)
  1243. /* Taken from output of objdump from lib64/glibc */
  1244. mflr r3
  1245. ld r11, 0(r1)
  1246. stdu r1, -112(r1)
  1247. std r3, 128(r1)
  1248. ld r4, 16(r11)
  1249. subi r3, r3, MCOUNT_INSN_SIZE
  1250. LOAD_REG_ADDR(r5,ftrace_trace_function)
  1251. ld r5,0(r5)
  1252. ld r5,0(r5)
  1253. mtctr r5
  1254. bctrl
  1255. nop
  1256. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1257. b ftrace_graph_caller
  1258. #endif
  1259. ld r0, 128(r1)
  1260. mtlr r0
  1261. addi r1, r1, 112
  1262. _GLOBAL(ftrace_stub)
  1263. blr
  1264. #endif /* CONFIG_DYNAMIC_FTRACE */
  1265. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1266. #ifndef CC_USING_MPROFILE_KERNEL
  1267. _GLOBAL(ftrace_graph_caller)
  1268. /* load r4 with local address */
  1269. ld r4, 128(r1)
  1270. subi r4, r4, MCOUNT_INSN_SIZE
  1271. /* Grab the LR out of the caller stack frame */
  1272. ld r11, 112(r1)
  1273. ld r3, 16(r11)
  1274. bl prepare_ftrace_return
  1275. nop
  1276. /*
  1277. * prepare_ftrace_return gives us the address we divert to.
  1278. * Change the LR in the callers stack frame to this.
  1279. */
  1280. ld r11, 112(r1)
  1281. std r3, 16(r11)
  1282. ld r0, 128(r1)
  1283. mtlr r0
  1284. addi r1, r1, 112
  1285. blr
  1286. #else /* CC_USING_MPROFILE_KERNEL */
  1287. _GLOBAL(ftrace_graph_caller)
  1288. /* with -mprofile-kernel, parameter regs are still alive at _mcount */
  1289. std r10, 104(r1)
  1290. std r9, 96(r1)
  1291. std r8, 88(r1)
  1292. std r7, 80(r1)
  1293. std r6, 72(r1)
  1294. std r5, 64(r1)
  1295. std r4, 56(r1)
  1296. std r3, 48(r1)
  1297. /* Save callee's TOC in the ABI compliant location */
  1298. std r2, 24(r1)
  1299. ld r2, PACATOC(r13) /* get kernel TOC in r2 */
  1300. mfctr r4 /* ftrace_caller has moved local addr here */
  1301. std r4, 40(r1)
  1302. mflr r3 /* ftrace_caller has restored LR from stack */
  1303. subi r4, r4, MCOUNT_INSN_SIZE
  1304. bl prepare_ftrace_return
  1305. nop
  1306. /*
  1307. * prepare_ftrace_return gives us the address we divert to.
  1308. * Change the LR to this.
  1309. */
  1310. mtlr r3
  1311. ld r0, 40(r1)
  1312. mtctr r0
  1313. ld r10, 104(r1)
  1314. ld r9, 96(r1)
  1315. ld r8, 88(r1)
  1316. ld r7, 80(r1)
  1317. ld r6, 72(r1)
  1318. ld r5, 64(r1)
  1319. ld r4, 56(r1)
  1320. ld r3, 48(r1)
  1321. /* Restore callee's TOC */
  1322. ld r2, 24(r1)
  1323. addi r1, r1, 112
  1324. mflr r0
  1325. std r0, LRSAVE(r1)
  1326. bctr
  1327. #endif /* CC_USING_MPROFILE_KERNEL */
  1328. _GLOBAL(return_to_handler)
  1329. /* need to save return values */
  1330. std r4, -32(r1)
  1331. std r3, -24(r1)
  1332. /* save TOC */
  1333. std r2, -16(r1)
  1334. std r31, -8(r1)
  1335. mr r31, r1
  1336. stdu r1, -112(r1)
  1337. /*
  1338. * We might be called from a module.
  1339. * Switch to our TOC to run inside the core kernel.
  1340. */
  1341. ld r2, PACATOC(r13)
  1342. bl ftrace_return_to_handler
  1343. nop
  1344. /* return value has real return address */
  1345. mtlr r3
  1346. ld r1, 0(r1)
  1347. ld r4, -32(r1)
  1348. ld r3, -24(r1)
  1349. ld r2, -16(r1)
  1350. ld r31, -8(r1)
  1351. /* Jump back to real return address */
  1352. blr
  1353. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1354. #endif /* CONFIG_FUNCTION_TRACER */