entry_64.S 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * PowerPC version
  4. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  5. * Rewritten by Cort Dougan ([email protected]) for PReP
  6. * Copyright (C) 1996 Cort Dougan <[email protected]>
  7. * Adapted for Power Macintosh by Paul Mackerras.
  8. * Low-level exception handlers and MMU support
  9. * rewritten by Paul Mackerras.
  10. * Copyright (C) 1996 Paul Mackerras.
  11. * MPC8xx modifications Copyright (C) 1997 Dan Malek ([email protected]).
  12. *
  13. * This file contains the system call entry code, context switch
  14. * code, and exception/interrupt return code for PowerPC.
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/err.h>
  18. #include <asm/cache.h>
  19. #include <asm/unistd.h>
  20. #include <asm/processor.h>
  21. #include <asm/page.h>
  22. #include <asm/mmu.h>
  23. #include <asm/thread_info.h>
  24. #include <asm/code-patching-asm.h>
  25. #include <asm/ppc_asm.h>
  26. #include <asm/asm-offsets.h>
  27. #include <asm/cputable.h>
  28. #include <asm/firmware.h>
  29. #include <asm/bug.h>
  30. #include <asm/ptrace.h>
  31. #include <asm/irqflags.h>
  32. #include <asm/hw_irq.h>
  33. #include <asm/context_tracking.h>
  34. #include <asm/ppc-opcode.h>
  35. #include <asm/barrier.h>
  36. #include <asm/export.h>
  37. #include <asm/asm-compat.h>
  38. #ifdef CONFIG_PPC_BOOK3S
  39. #include <asm/exception-64s.h>
  40. #else
  41. #include <asm/exception-64e.h>
  42. #endif
  43. #include <asm/feature-fixups.h>
  44. #include <asm/kup.h>
  45. /*
  46. * System calls.
  47. */
  48. .section ".text"
  49. #ifdef CONFIG_PPC_BOOK3S_64
  50. #define FLUSH_COUNT_CACHE \
  51. 1: nop; \
  52. patch_site 1b, patch__call_flush_branch_caches1; \
  53. 1: nop; \
  54. patch_site 1b, patch__call_flush_branch_caches2; \
  55. 1: nop; \
  56. patch_site 1b, patch__call_flush_branch_caches3
  57. .macro nops number
  58. .rept \number
  59. nop
  60. .endr
  61. .endm
  62. .balign 32
  63. .global flush_branch_caches
  64. flush_branch_caches:
  65. /* Save LR into r9 */
  66. mflr r9
  67. // Flush the link stack
  68. .rept 64
  69. bl .+4
  70. .endr
  71. b 1f
  72. nops 6
  73. .balign 32
  74. /* Restore LR */
  75. 1: mtlr r9
  76. // If we're just flushing the link stack, return here
  77. 3: nop
  78. patch_site 3b patch__flush_link_stack_return
  79. li r9,0x7fff
  80. mtctr r9
  81. PPC_BCCTR_FLUSH
  82. 2: nop
  83. patch_site 2b patch__flush_count_cache_return
  84. nops 3
  85. .rept 278
  86. .balign 32
  87. PPC_BCCTR_FLUSH
  88. nops 7
  89. .endr
  90. blr
  91. #else
  92. #define FLUSH_COUNT_CACHE
  93. #endif /* CONFIG_PPC_BOOK3S_64 */
  94. /*
  95. * This routine switches between two different tasks. The process
  96. * state of one is saved on its kernel stack. Then the state
  97. * of the other is restored from its kernel stack. The memory
  98. * management hardware is updated to the second process's state.
  99. * Finally, we can return to the second process, via interrupt_return.
  100. * On entry, r3 points to the THREAD for the current task, r4
  101. * points to the THREAD for the new task.
  102. *
  103. * Note: there are two ways to get to the "going out" portion
  104. * of this code; either by coming in via the entry (_switch)
  105. * or via "fork" which must set up an environment equivalent
  106. * to the "_switch" path. If you change this you'll have to change
  107. * the fork code also.
  108. *
  109. * The code which creates the new task context is in 'copy_thread'
  110. * in arch/powerpc/kernel/process.c
  111. */
  112. .align 7
  113. _GLOBAL(_switch)
  114. mflr r0
  115. std r0,16(r1)
  116. stdu r1,-SWITCH_FRAME_SIZE(r1)
  117. /* r3-r13 are caller saved -- Cort */
  118. SAVE_NVGPRS(r1)
  119. std r0,_NIP(r1) /* Return to switch caller */
  120. mfcr r23
  121. std r23,_CCR(r1)
  122. std r1,KSP(r3) /* Set old stack pointer */
  123. kuap_check_amr r9, r10
  124. FLUSH_COUNT_CACHE /* Clobbers r9, ctr */
  125. /*
  126. * On SMP kernels, care must be taken because a task may be
  127. * scheduled off CPUx and on to CPUy. Memory ordering must be
  128. * considered.
  129. *
  130. * Cacheable stores on CPUx will be visible when the task is
  131. * scheduled on CPUy by virtue of the core scheduler barriers
  132. * (see "Notes on Program-Order guarantees on SMP systems." in
  133. * kernel/sched/core.c).
  134. *
  135. * Uncacheable stores in the case of involuntary preemption must
  136. * be taken care of. The smp_mb__after_spinlock() in __schedule()
  137. * is implemented as hwsync on powerpc, which orders MMIO too. So
  138. * long as there is an hwsync in the context switch path, it will
  139. * be executed on the source CPU after the task has performed
  140. * all MMIO ops on that CPU, and on the destination CPU before the
  141. * task performs any MMIO ops there.
  142. */
  143. /*
  144. * The kernel context switch path must contain a spin_lock,
  145. * which contains larx/stcx, which will clear any reservation
  146. * of the task being switched.
  147. */
  148. #ifdef CONFIG_PPC_BOOK3S
  149. /* Cancel all explict user streams as they will have no use after context
  150. * switch and will stop the HW from creating streams itself
  151. */
  152. DCBT_BOOK3S_STOP_ALL_STREAM_IDS(r6)
  153. #endif
  154. addi r6,r4,-THREAD /* Convert THREAD to 'current' */
  155. std r6,PACACURRENT(r13) /* Set new 'current' */
  156. #if defined(CONFIG_STACKPROTECTOR)
  157. ld r6, TASK_CANARY(r6)
  158. std r6, PACA_CANARY(r13)
  159. #endif
  160. ld r8,KSP(r4) /* new stack pointer */
  161. #ifdef CONFIG_PPC_64S_HASH_MMU
  162. BEGIN_MMU_FTR_SECTION
  163. b 2f
  164. END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
  165. BEGIN_FTR_SECTION
  166. clrrdi r6,r8,28 /* get its ESID */
  167. clrrdi r9,r1,28 /* get current sp ESID */
  168. FTR_SECTION_ELSE
  169. clrrdi r6,r8,40 /* get its 1T ESID */
  170. clrrdi r9,r1,40 /* get current sp 1T ESID */
  171. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_1T_SEGMENT)
  172. clrldi. r0,r6,2 /* is new ESID c00000000? */
  173. cmpd cr1,r6,r9 /* or is new ESID the same as current ESID? */
  174. cror eq,4*cr1+eq,eq
  175. beq 2f /* if yes, don't slbie it */
  176. /* Bolt in the new stack SLB entry */
  177. ld r7,KSP_VSID(r4) /* Get new stack's VSID */
  178. oris r0,r6,(SLB_ESID_V)@h
  179. ori r0,r0,(SLB_NUM_BOLTED-1)@l
  180. BEGIN_FTR_SECTION
  181. li r9,MMU_SEGSIZE_1T /* insert B field */
  182. oris r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
  183. rldimi r7,r9,SLB_VSID_SSIZE_SHIFT,0
  184. END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
  185. /* Update the last bolted SLB. No write barriers are needed
  186. * here, provided we only update the current CPU's SLB shadow
  187. * buffer.
  188. */
  189. ld r9,PACA_SLBSHADOWPTR(r13)
  190. li r12,0
  191. std r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
  192. li r12,SLBSHADOW_STACKVSID
  193. STDX_BE r7,r12,r9 /* Save VSID */
  194. li r12,SLBSHADOW_STACKESID
  195. STDX_BE r0,r12,r9 /* Save ESID */
  196. /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
  197. * we have 1TB segments, the only CPUs known to have the errata
  198. * only support less than 1TB of system memory and we'll never
  199. * actually hit this code path.
  200. */
  201. isync
  202. slbie r6
  203. BEGIN_FTR_SECTION
  204. slbie r6 /* Workaround POWER5 < DD2.1 issue */
  205. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
  206. slbmte r7,r0
  207. isync
  208. 2:
  209. #endif /* CONFIG_PPC_64S_HASH_MMU */
  210. clrrdi r7, r8, THREAD_SHIFT /* base of new stack */
  211. /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
  212. because we don't need to leave the 288-byte ABI gap at the
  213. top of the kernel stack. */
  214. addi r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
  215. /*
  216. * PMU interrupts in radix may come in here. They will use r1, not
  217. * PACAKSAVE, so this stack switch will not cause a problem. They
  218. * will store to the process stack, which may then be migrated to
  219. * another CPU. However the rq lock release on this CPU paired with
  220. * the rq lock acquire on the new CPU before the stack becomes
  221. * active on the new CPU, will order those stores.
  222. */
  223. mr r1,r8 /* start using new stack pointer */
  224. std r7,PACAKSAVE(r13)
  225. ld r6,_CCR(r1)
  226. mtcrf 0xFF,r6
  227. /* r3-r13 are destroyed -- Cort */
  228. REST_NVGPRS(r1)
  229. /* convert old thread to its task_struct for return value */
  230. addi r3,r3,-THREAD
  231. ld r7,_NIP(r1) /* Return to _switch caller in new task */
  232. mtlr r7
  233. addi r1,r1,SWITCH_FRAME_SIZE
  234. blr
  235. _GLOBAL(enter_prom)
  236. mflr r0
  237. std r0,16(r1)
  238. stdu r1,-SWITCH_FRAME_SIZE(r1) /* Save SP and create stack space */
  239. /* Because PROM is running in 32b mode, it clobbers the high order half
  240. * of all registers that it saves. We therefore save those registers
  241. * PROM might touch to the stack. (r0, r3-r13 are caller saved)
  242. */
  243. SAVE_GPR(2, r1)
  244. SAVE_GPR(13, r1)
  245. SAVE_NVGPRS(r1)
  246. mfcr r10
  247. mfmsr r11
  248. std r10,_CCR(r1)
  249. std r11,_MSR(r1)
  250. /* Put PROM address in SRR0 */
  251. mtsrr0 r4
  252. /* Setup our trampoline return addr in LR */
  253. bcl 20,31,$+4
  254. 0: mflr r4
  255. addi r4,r4,(1f - 0b)
  256. mtlr r4
  257. /* Prepare a 32-bit mode big endian MSR
  258. */
  259. #ifdef CONFIG_PPC_BOOK3E_64
  260. rlwinm r11,r11,0,1,31
  261. mtsrr1 r11
  262. rfi
  263. #else /* CONFIG_PPC_BOOK3E_64 */
  264. LOAD_REG_IMMEDIATE(r12, MSR_SF | MSR_LE)
  265. andc r11,r11,r12
  266. mtsrr1 r11
  267. RFI_TO_KERNEL
  268. #endif /* CONFIG_PPC_BOOK3E_64 */
  269. 1: /* Return from OF */
  270. FIXUP_ENDIAN
  271. /* Just make sure that r1 top 32 bits didn't get
  272. * corrupt by OF
  273. */
  274. rldicl r1,r1,0,32
  275. /* Restore the MSR (back to 64 bits) */
  276. ld r0,_MSR(r1)
  277. MTMSRD(r0)
  278. isync
  279. /* Restore other registers */
  280. REST_GPR(2, r1)
  281. REST_GPR(13, r1)
  282. REST_NVGPRS(r1)
  283. ld r4,_CCR(r1)
  284. mtcr r4
  285. addi r1,r1,SWITCH_FRAME_SIZE
  286. ld r0,16(r1)
  287. mtlr r0
  288. blr