entry_64.S 35 KB

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