mmu_context.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Common implementation of switch_mm_irqs_off
  4. *
  5. * Copyright IBM Corp. 2017
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/cpu.h>
  9. #include <linux/sched/mm.h>
  10. #include <asm/mmu_context.h>
  11. #include <asm/pgalloc.h>
  12. #if defined(CONFIG_PPC32)
  13. static inline void switch_mm_pgdir(struct task_struct *tsk,
  14. struct mm_struct *mm)
  15. {
  16. /* 32-bit keeps track of the current PGDIR in the thread struct */
  17. tsk->thread.pgdir = mm->pgd;
  18. #ifdef CONFIG_PPC_BOOK3S_32
  19. tsk->thread.sr0 = mm->context.sr0;
  20. #endif
  21. #if defined(CONFIG_BOOKE_OR_40x) && defined(CONFIG_PPC_KUAP)
  22. tsk->thread.pid = mm->context.id;
  23. #endif
  24. }
  25. #elif defined(CONFIG_PPC_BOOK3E_64)
  26. static inline void switch_mm_pgdir(struct task_struct *tsk,
  27. struct mm_struct *mm)
  28. {
  29. /* 64-bit Book3E keeps track of current PGD in the PACA */
  30. get_paca()->pgd = mm->pgd;
  31. #ifdef CONFIG_PPC_KUAP
  32. tsk->thread.pid = mm->context.id;
  33. #endif
  34. }
  35. #else
  36. static inline void switch_mm_pgdir(struct task_struct *tsk,
  37. struct mm_struct *mm) { }
  38. #endif
  39. void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  40. struct task_struct *tsk)
  41. {
  42. bool new_on_cpu = false;
  43. /* Mark this context has been used on the new CPU */
  44. if (!cpumask_test_cpu(smp_processor_id(), mm_cpumask(next))) {
  45. cpumask_set_cpu(smp_processor_id(), mm_cpumask(next));
  46. inc_mm_active_cpus(next);
  47. /*
  48. * This full barrier orders the store to the cpumask above vs
  49. * a subsequent load which allows this CPU/MMU to begin loading
  50. * translations for 'next' from page table PTEs into the TLB.
  51. *
  52. * When using the radix MMU, that operation is the load of the
  53. * MMU context id, which is then moved to SPRN_PID.
  54. *
  55. * For the hash MMU it is either the first load from slb_cache
  56. * in switch_slb() to preload the SLBs, or the load of
  57. * get_user_context which loads the context for the VSID hash
  58. * to insert a new SLB, in the SLB fault handler.
  59. *
  60. * On the other side, the barrier is in mm/tlb-radix.c for
  61. * radix which orders earlier stores to clear the PTEs before
  62. * the load of mm_cpumask to check which CPU TLBs should be
  63. * flushed. For hash, pte_xchg to clear the PTE includes the
  64. * barrier.
  65. *
  66. * This full barrier is also needed by membarrier when
  67. * switching between processes after store to rq->curr, before
  68. * user-space memory accesses.
  69. */
  70. smp_mb();
  71. new_on_cpu = true;
  72. }
  73. /* Some subarchs need to track the PGD elsewhere */
  74. switch_mm_pgdir(tsk, next);
  75. /* Nothing else to do if we aren't actually switching */
  76. if (prev == next)
  77. return;
  78. /*
  79. * We must stop all altivec streams before changing the HW
  80. * context
  81. */
  82. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  83. asm volatile (PPC_DSSALL);
  84. if (!new_on_cpu)
  85. membarrier_arch_switch_mm(prev, next, tsk);
  86. /*
  87. * The actual HW switching method differs between the various
  88. * sub architectures. Out of line for now
  89. */
  90. switch_mmu_context(prev, next, tsk);
  91. }
  92. #ifndef CONFIG_PPC_BOOK3S_64
  93. void arch_exit_mmap(struct mm_struct *mm)
  94. {
  95. void *frag = pte_frag_get(&mm->context);
  96. if (frag)
  97. pte_frag_destroy(frag);
  98. }
  99. #endif