entry_64.S 31 KB

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