entry_64.S 35 KB

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