switch.S 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #include <linux/objtool.h>
  3. #include <asm/asm-offsets.h>
  4. #include <asm/code-patching-asm.h>
  5. #include <asm/mmu.h>
  6. #include <asm/ppc_asm.h>
  7. #include <asm/kup.h>
  8. #include <asm/thread_info.h>
  9. .section ".text","ax",@progbits
  10. #ifdef CONFIG_PPC_BOOK3S_64
  11. /*
  12. * Cancel all explict user streams as they will have no use after context
  13. * switch and will stop the HW from creating streams itself
  14. */
  15. #define STOP_STREAMS \
  16. DCBT_BOOK3S_STOP_ALL_STREAM_IDS(r6)
  17. #define FLUSH_COUNT_CACHE \
  18. 1: nop; \
  19. patch_site 1b, patch__call_flush_branch_caches1; \
  20. 1: nop; \
  21. patch_site 1b, patch__call_flush_branch_caches2; \
  22. 1: nop; \
  23. patch_site 1b, patch__call_flush_branch_caches3
  24. .macro nops number
  25. .rept \number
  26. nop
  27. .endr
  28. .endm
  29. .balign 32
  30. .global flush_branch_caches
  31. flush_branch_caches:
  32. /* Save LR into r9 */
  33. mflr r9
  34. // Flush the link stack
  35. .rept 64
  36. ANNOTATE_INTRA_FUNCTION_CALL
  37. bl .+4
  38. .endr
  39. b 1f
  40. nops 6
  41. .balign 32
  42. /* Restore LR */
  43. 1: mtlr r9
  44. // If we're just flushing the link stack, return here
  45. 3: nop
  46. patch_site 3b patch__flush_link_stack_return
  47. li r9,0x7fff
  48. mtctr r9
  49. PPC_BCCTR_FLUSH
  50. 2: nop
  51. patch_site 2b patch__flush_count_cache_return
  52. nops 3
  53. .rept 278
  54. .balign 32
  55. PPC_BCCTR_FLUSH
  56. nops 7
  57. .endr
  58. blr
  59. #ifdef CONFIG_PPC_64S_HASH_MMU
  60. .balign 32
  61. /*
  62. * New stack pointer in r8, old stack pointer in r1, must not clobber r3
  63. */
  64. pin_stack_slb:
  65. BEGIN_FTR_SECTION
  66. clrrdi r6,r8,28 /* get its ESID */
  67. clrrdi r9,r1,28 /* get current sp ESID */
  68. FTR_SECTION_ELSE
  69. clrrdi r6,r8,40 /* get its 1T ESID */
  70. clrrdi r9,r1,40 /* get current sp 1T ESID */
  71. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_1T_SEGMENT)
  72. clrldi. r0,r6,2 /* is new ESID c00000000? */
  73. cmpd cr1,r6,r9 /* or is new ESID the same as current ESID? */
  74. cror eq,4*cr1+eq,eq
  75. beq 2f /* if yes, don't slbie it */
  76. /* Bolt in the new stack SLB entry */
  77. ld r7,KSP_VSID(r4) /* Get new stack's VSID */
  78. oris r0,r6,(SLB_ESID_V)@h
  79. ori r0,r0,(SLB_NUM_BOLTED-1)@l
  80. BEGIN_FTR_SECTION
  81. li r9,MMU_SEGSIZE_1T /* insert B field */
  82. oris r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
  83. rldimi r7,r9,SLB_VSID_SSIZE_SHIFT,0
  84. END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
  85. /* Update the last bolted SLB. No write barriers are needed
  86. * here, provided we only update the current CPU's SLB shadow
  87. * buffer.
  88. */
  89. ld r9,PACA_SLBSHADOWPTR(r13)
  90. li r12,0
  91. std r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
  92. li r12,SLBSHADOW_STACKVSID
  93. STDX_BE r7,r12,r9 /* Save VSID */
  94. li r12,SLBSHADOW_STACKESID
  95. STDX_BE r0,r12,r9 /* Save ESID */
  96. /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
  97. * we have 1TB segments, the only CPUs known to have the errata
  98. * only support less than 1TB of system memory and we'll never
  99. * actually hit this code path.
  100. */
  101. isync
  102. slbie r6
  103. BEGIN_FTR_SECTION
  104. slbie r6 /* Workaround POWER5 < DD2.1 issue */
  105. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
  106. slbmte r7,r0
  107. isync
  108. 2: blr
  109. .size pin_stack_slb,.-pin_stack_slb
  110. #endif /* CONFIG_PPC_64S_HASH_MMU */
  111. #else
  112. #define STOP_STREAMS
  113. #define FLUSH_COUNT_CACHE
  114. #endif /* CONFIG_PPC_BOOK3S_64 */
  115. /*
  116. * do_switch_32/64 have the same calling convention as _switch, i.e., r3,r4
  117. * are prev and next thread_struct *, and returns prev task_struct * in r3.
  118. * This switches the stack, current, and does other task switch housekeeping.
  119. */
  120. .macro do_switch_32
  121. tophys(r0,r4)
  122. mtspr SPRN_SPRG_THREAD,r0 /* Update current THREAD phys addr */
  123. lwz r1,KSP(r4) /* Load new stack pointer */
  124. /* save the old current 'last' for return value */
  125. mr r3,r2
  126. addi r2,r4,-THREAD /* Update current */
  127. .endm
  128. .macro do_switch_64
  129. ld r8,KSP(r4) /* Load new stack pointer */
  130. kuap_check_amr r9, r10
  131. FLUSH_COUNT_CACHE /* Clobbers r9, ctr */
  132. STOP_STREAMS /* Clobbers r6 */
  133. addi r3,r3,-THREAD /* old thread -> task_struct for return value */
  134. addi r6,r4,-THREAD /* new thread -> task_struct */
  135. std r6,PACACURRENT(r13) /* Set new task_struct to 'current' */
  136. #if defined(CONFIG_STACKPROTECTOR)
  137. ld r6, TASK_CANARY(r6)
  138. std r6, PACA_CANARY(r13)
  139. #endif
  140. /* Set new PACAKSAVE */
  141. clrrdi r7,r8,THREAD_SHIFT /* base of new stack */
  142. addi r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
  143. std r7,PACAKSAVE(r13)
  144. #ifdef CONFIG_PPC_64S_HASH_MMU
  145. BEGIN_MMU_FTR_SECTION
  146. bl pin_stack_slb
  147. END_MMU_FTR_SECTION_IFCLR(MMU_FTR_TYPE_RADIX)
  148. #endif
  149. /*
  150. * PMU interrupts in radix may come in here. They will use r1, not
  151. * PACAKSAVE, so this stack switch will not cause a problem. They
  152. * will store to the process stack, which may then be migrated to
  153. * another CPU. However the rq lock release on this CPU paired with
  154. * the rq lock acquire on the new CPU before the stack becomes
  155. * active on the new CPU, will order those stores.
  156. */
  157. mr r1,r8 /* start using new stack pointer */
  158. .endm
  159. /*
  160. * This routine switches between two different tasks. The process
  161. * state of one is saved on its kernel stack. Then the state
  162. * of the other is restored from its kernel stack. The memory
  163. * management hardware is updated to the second process's state.
  164. * Finally, we can return to the second process.
  165. * On entry, r3 points to the THREAD for the current task, r4
  166. * points to the THREAD for the new task.
  167. *
  168. * This routine is always called with interrupts disabled.
  169. *
  170. * Note: there are two ways to get to the "going out" portion
  171. * of this code; either by coming in via the entry (_switch)
  172. * or via "fork" which must set up an environment equivalent
  173. * to the "_switch" path. If you change this , you'll have to
  174. * change the fork code also.
  175. *
  176. * The code which creates the new task context is in 'copy_thread'
  177. * in arch/ppc/kernel/process.c
  178. *
  179. * Note: this uses SWITCH_FRAME_SIZE rather than USER_INT_FRAME_SIZE
  180. * because we don't need to leave the redzone ABI gap at the top of
  181. * the kernel stack.
  182. */
  183. _GLOBAL(_switch)
  184. PPC_CREATE_STACK_FRAME(SWITCH_FRAME_SIZE)
  185. PPC_STL r1,KSP(r3) /* Set old stack pointer */
  186. SAVE_NVGPRS(r1) /* volatiles are caller-saved -- Cort */
  187. PPC_STL r0,_NIP(r1) /* Return to switch caller */
  188. mfcr r0
  189. stw r0,_CCR(r1)
  190. /*
  191. * On SMP kernels, care must be taken because a task may be
  192. * scheduled off CPUx and on to CPUy. Memory ordering must be
  193. * considered.
  194. *
  195. * Cacheable stores on CPUx will be visible when the task is
  196. * scheduled on CPUy by virtue of the core scheduler barriers
  197. * (see "Notes on Program-Order guarantees on SMP systems." in
  198. * kernel/sched/core.c).
  199. *
  200. * Uncacheable stores in the case of involuntary preemption must
  201. * be taken care of. The smp_mb__after_spinlock() in __schedule()
  202. * is implemented as hwsync on powerpc, which orders MMIO too. So
  203. * long as there is an hwsync in the context switch path, it will
  204. * be executed on the source CPU after the task has performed
  205. * all MMIO ops on that CPU, and on the destination CPU before the
  206. * task performs any MMIO ops there.
  207. */
  208. /*
  209. * The kernel context switch path must contain a spin_lock,
  210. * which contains larx/stcx, which will clear any reservation
  211. * of the task being switched.
  212. */
  213. #ifdef CONFIG_PPC32
  214. do_switch_32
  215. #else
  216. do_switch_64
  217. #endif
  218. lwz r0,_CCR(r1)
  219. mtcrf 0xFF,r0
  220. REST_NVGPRS(r1) /* volatiles are destroyed -- Cort */
  221. PPC_LL r0,_NIP(r1) /* Return to _switch caller in new task */
  222. mtlr r0
  223. addi r1,r1,SWITCH_FRAME_SIZE
  224. blr