mmu.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 ARM Ltd.
  4. */
  5. #ifndef __ASM_MMU_H
  6. #define __ASM_MMU_H
  7. #include <asm/cputype.h>
  8. #define MMCF_AARCH32 0x1 /* mm context flag for AArch32 executables */
  9. #define USER_ASID_BIT 48
  10. #define USER_ASID_FLAG (UL(1) << USER_ASID_BIT)
  11. #define TTBR_ASID_MASK (UL(0xffff) << 48)
  12. #ifndef __ASSEMBLY__
  13. #include <linux/refcount.h>
  14. #include <asm/cpufeature.h>
  15. typedef struct {
  16. atomic64_t id;
  17. #ifdef CONFIG_COMPAT
  18. void *sigpage;
  19. #endif
  20. refcount_t pinned;
  21. void *vdso;
  22. unsigned long flags;
  23. u8 pkey_allocation_map;
  24. } mm_context_t;
  25. /*
  26. * We use atomic64_read() here because the ASID for an 'mm_struct' can
  27. * be reallocated when scheduling one of its threads following a
  28. * rollover event (see new_context() and flush_context()). In this case,
  29. * a concurrent TLBI (e.g. via try_to_unmap_one() and ptep_clear_flush())
  30. * may use a stale ASID. This is fine in principle as the new ASID is
  31. * guaranteed to be clean in the TLB, but the TLBI routines have to take
  32. * care to handle the following race:
  33. *
  34. * CPU 0 CPU 1 CPU 2
  35. *
  36. * // ptep_clear_flush(mm)
  37. * xchg_relaxed(pte, 0)
  38. * DSB ISHST
  39. * old = ASID(mm)
  40. * | <rollover>
  41. * | new = new_context(mm)
  42. * \-----------------> atomic_set(mm->context.id, new)
  43. * cpu_switch_mm(mm)
  44. * // Hardware walk of pte using new ASID
  45. * TLBI(old)
  46. *
  47. * In this scenario, the barrier on CPU 0 and the dependency on CPU 1
  48. * ensure that the page-table walker on CPU 1 *must* see the invalid PTE
  49. * written by CPU 0.
  50. */
  51. #define ASID(mm) (atomic64_read(&(mm)->context.id) & 0xffff)
  52. static inline bool arm64_kernel_unmapped_at_el0(void)
  53. {
  54. return alternative_has_cap_unlikely(ARM64_UNMAP_KERNEL_AT_EL0);
  55. }
  56. extern void arm64_memblock_init(void);
  57. extern void paging_init(void);
  58. extern void bootmem_init(void);
  59. extern void create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
  60. phys_addr_t size, pgprot_t prot);
  61. extern void create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
  62. unsigned long virt, phys_addr_t size,
  63. pgprot_t prot, bool page_mappings_only);
  64. extern void *fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot);
  65. extern void mark_linear_text_alias_ro(void);
  66. /*
  67. * This check is triggered during the early boot before the cpufeature
  68. * is initialised. Checking the status on the local CPU allows the boot
  69. * CPU to detect the need for non-global mappings and thus avoiding a
  70. * pagetable re-write after all the CPUs are booted. This check will be
  71. * anyway run on individual CPUs, allowing us to get the consistent
  72. * state once the SMP CPUs are up and thus make the switch to non-global
  73. * mappings if required.
  74. */
  75. static inline bool kaslr_requires_kpti(void)
  76. {
  77. /*
  78. * E0PD does a similar job to KPTI so can be used instead
  79. * where available.
  80. */
  81. if (IS_ENABLED(CONFIG_ARM64_E0PD)) {
  82. u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);
  83. if (cpuid_feature_extract_unsigned_field(mmfr2,
  84. ID_AA64MMFR2_EL1_E0PD_SHIFT))
  85. return false;
  86. }
  87. /*
  88. * Systems affected by Cavium erratum 24756 are incompatible
  89. * with KPTI.
  90. */
  91. if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) {
  92. extern const struct midr_range cavium_erratum_27456_cpus[];
  93. if (is_midr_in_range_list(read_cpuid_id(),
  94. cavium_erratum_27456_cpus))
  95. return false;
  96. }
  97. return true;
  98. }
  99. #define INIT_MM_CONTEXT(name) \
  100. .pgd = swapper_pg_dir,
  101. #endif /* !__ASSEMBLY__ */
  102. #endif