mmu_context.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This file contains the routines for handling the MMU on those
  4. * PowerPC implementations where the MMU substantially follows the
  5. * architecture specification. This includes the 6xx, 7xx, 7xxx,
  6. * and 8260 implementations but excludes the 8xx and 4xx.
  7. * -- paulus
  8. *
  9. * Derived from arch/ppc/mm/init.c:
  10. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  11. *
  12. * Modifications by Paul Mackerras (PowerMac) ([email protected])
  13. * and Cort Dougan (PReP) ([email protected])
  14. * Copyright (C) 1996 Paul Mackerras
  15. *
  16. * Derived from "arch/i386/mm/init.c"
  17. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  18. */
  19. #include <linux/mm.h>
  20. #include <linux/init.h>
  21. #include <linux/export.h>
  22. #include <asm/mmu_context.h>
  23. /*
  24. * Room for two PTE pointers, usually the kernel and current user pointers
  25. * to their respective root page table.
  26. */
  27. void *abatron_pteptrs[2];
  28. /*
  29. * On 32-bit PowerPC 6xx/7xx/7xxx CPUs, we use a set of 16 VSIDs
  30. * (virtual segment identifiers) for each context. Although the
  31. * hardware supports 24-bit VSIDs, and thus >1 million contexts,
  32. * we only use 32,768 of them. That is ample, since there can be
  33. * at most around 30,000 tasks in the system anyway, and it means
  34. * that we can use a bitmap to indicate which contexts are in use.
  35. * Using a bitmap means that we entirely avoid all of the problems
  36. * that we used to have when the context number overflowed,
  37. * particularly on SMP systems.
  38. * -- paulus.
  39. */
  40. #define NO_CONTEXT ((unsigned long) -1)
  41. #define LAST_CONTEXT 32767
  42. #define FIRST_CONTEXT 1
  43. static unsigned long next_mmu_context;
  44. static unsigned long context_map[LAST_CONTEXT / BITS_PER_LONG + 1];
  45. unsigned long __init_new_context(void)
  46. {
  47. unsigned long ctx = next_mmu_context;
  48. while (test_and_set_bit(ctx, context_map)) {
  49. ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
  50. if (ctx > LAST_CONTEXT)
  51. ctx = 0;
  52. }
  53. next_mmu_context = (ctx + 1) & LAST_CONTEXT;
  54. return ctx;
  55. }
  56. EXPORT_SYMBOL_GPL(__init_new_context);
  57. /*
  58. * Set up the context for a new address space.
  59. */
  60. int init_new_context(struct task_struct *t, struct mm_struct *mm)
  61. {
  62. mm->context.id = __init_new_context();
  63. mm->context.sr0 = CTX_TO_VSID(mm->context.id, 0);
  64. if (!kuep_is_disabled())
  65. mm->context.sr0 |= SR_NX;
  66. if (!kuap_is_disabled())
  67. mm->context.sr0 |= SR_KS;
  68. return 0;
  69. }
  70. /*
  71. * Free a context ID. Make sure to call this with preempt disabled!
  72. */
  73. void __destroy_context(unsigned long ctx)
  74. {
  75. clear_bit(ctx, context_map);
  76. }
  77. EXPORT_SYMBOL_GPL(__destroy_context);
  78. /*
  79. * We're finished using the context for an address space.
  80. */
  81. void destroy_context(struct mm_struct *mm)
  82. {
  83. preempt_disable();
  84. if (mm->context.id != NO_CONTEXT) {
  85. __destroy_context(mm->context.id);
  86. mm->context.id = NO_CONTEXT;
  87. }
  88. preempt_enable();
  89. }
  90. /*
  91. * Initialize the context management stuff.
  92. */
  93. void __init mmu_context_init(void)
  94. {
  95. /* Reserve context 0 for kernel use */
  96. context_map[0] = (1 << FIRST_CONTEXT) - 1;
  97. next_mmu_context = FIRST_CONTEXT;
  98. }
  99. void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk)
  100. {
  101. long id = next->context.id;
  102. if (id < 0)
  103. panic("mm_struct %p has no context ID", next);
  104. isync();
  105. update_user_segments(next->context.sr0);
  106. if (IS_ENABLED(CONFIG_BDI_SWITCH))
  107. abatron_pteptrs[1] = next->pgd;
  108. if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
  109. mtspr(SPRN_SDR1, rol32(__pa(next->pgd), 4) & 0xffff01ff);
  110. mb(); /* sync */
  111. isync();
  112. }
  113. EXPORT_SYMBOL(switch_mmu_context);