pgtable.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_POWERPC_NOHASH_PGTABLE_H
  3. #define _ASM_POWERPC_NOHASH_PGTABLE_H
  4. #if defined(CONFIG_PPC64)
  5. #include <asm/nohash/64/pgtable.h>
  6. #else
  7. #include <asm/nohash/32/pgtable.h>
  8. #endif
  9. /* Permission masks used for kernel mappings */
  10. #define PAGE_KERNEL __pgprot(_PAGE_BASE | _PAGE_KERNEL_RW)
  11. #define PAGE_KERNEL_NC __pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | _PAGE_NO_CACHE)
  12. #define PAGE_KERNEL_NCG __pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | _PAGE_NO_CACHE | _PAGE_GUARDED)
  13. #define PAGE_KERNEL_X __pgprot(_PAGE_BASE | _PAGE_KERNEL_RWX)
  14. #define PAGE_KERNEL_RO __pgprot(_PAGE_BASE | _PAGE_KERNEL_RO)
  15. #define PAGE_KERNEL_ROX __pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX)
  16. #ifndef __ASSEMBLY__
  17. /* Generic accessors to PTE bits */
  18. #ifndef pte_write
  19. static inline int pte_write(pte_t pte)
  20. {
  21. return pte_val(pte) & _PAGE_RW;
  22. }
  23. #endif
  24. #ifndef pte_read
  25. static inline int pte_read(pte_t pte) { return 1; }
  26. #endif
  27. static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
  28. static inline int pte_special(pte_t pte) { return pte_val(pte) & _PAGE_SPECIAL; }
  29. static inline int pte_none(pte_t pte) { return (pte_val(pte) & ~_PTE_NONE_MASK) == 0; }
  30. static inline bool pte_hashpte(pte_t pte) { return false; }
  31. static inline bool pte_ci(pte_t pte) { return pte_val(pte) & _PAGE_NO_CACHE; }
  32. static inline bool pte_exec(pte_t pte) { return pte_val(pte) & _PAGE_EXEC; }
  33. #ifdef CONFIG_NUMA_BALANCING
  34. /*
  35. * These work without NUMA balancing but the kernel does not care. See the
  36. * comment in include/linux/pgtable.h . On powerpc, this will only
  37. * work for user pages and always return true for kernel pages.
  38. */
  39. static inline int pte_protnone(pte_t pte)
  40. {
  41. return pte_present(pte) && !pte_user(pte);
  42. }
  43. static inline int pmd_protnone(pmd_t pmd)
  44. {
  45. return pte_protnone(pmd_pte(pmd));
  46. }
  47. #endif /* CONFIG_NUMA_BALANCING */
  48. static inline int pte_present(pte_t pte)
  49. {
  50. return pte_val(pte) & _PAGE_PRESENT;
  51. }
  52. static inline bool pte_hw_valid(pte_t pte)
  53. {
  54. return pte_val(pte) & _PAGE_PRESENT;
  55. }
  56. /*
  57. * Don't just check for any non zero bits in __PAGE_USER, since for book3e
  58. * and PTE_64BIT, PAGE_KERNEL_X contains _PAGE_BAP_SR which is also in
  59. * _PAGE_USER. Need to explicitly match _PAGE_BAP_UR bit in that case too.
  60. */
  61. #ifndef pte_user
  62. static inline bool pte_user(pte_t pte)
  63. {
  64. return (pte_val(pte) & _PAGE_USER) == _PAGE_USER;
  65. }
  66. #endif
  67. /*
  68. * We only find page table entry in the last level
  69. * Hence no need for other accessors
  70. */
  71. #define pte_access_permitted pte_access_permitted
  72. static inline bool pte_access_permitted(pte_t pte, bool write)
  73. {
  74. /*
  75. * A read-only access is controlled by _PAGE_USER bit.
  76. * We have _PAGE_READ set for WRITE and EXECUTE
  77. */
  78. if (!pte_present(pte) || !pte_user(pte) || !pte_read(pte))
  79. return false;
  80. if (write && !pte_write(pte))
  81. return false;
  82. return true;
  83. }
  84. /* Conversion functions: convert a page and protection to a page entry,
  85. * and a page entry and page directory to the page they refer to.
  86. *
  87. * Even if PTEs can be unsigned long long, a PFN is always an unsigned
  88. * long for now.
  89. */
  90. static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot) {
  91. return __pte(((pte_basic_t)(pfn) << PTE_RPN_SHIFT) |
  92. pgprot_val(pgprot)); }
  93. static inline unsigned long pte_pfn(pte_t pte) {
  94. return pte_val(pte) >> PTE_RPN_SHIFT; }
  95. /* Generic modifiers for PTE bits */
  96. static inline pte_t pte_exprotect(pte_t pte)
  97. {
  98. return __pte(pte_val(pte) & ~_PAGE_EXEC);
  99. }
  100. static inline pte_t pte_mkclean(pte_t pte)
  101. {
  102. return __pte(pte_val(pte) & ~_PAGE_DIRTY);
  103. }
  104. static inline pte_t pte_mkold(pte_t pte)
  105. {
  106. return __pte(pte_val(pte) & ~_PAGE_ACCESSED);
  107. }
  108. static inline pte_t pte_mkspecial(pte_t pte)
  109. {
  110. return __pte(pte_val(pte) | _PAGE_SPECIAL);
  111. }
  112. #ifndef pte_mkhuge
  113. static inline pte_t pte_mkhuge(pte_t pte)
  114. {
  115. return __pte(pte_val(pte));
  116. }
  117. #endif
  118. #ifndef pte_mkprivileged
  119. static inline pte_t pte_mkprivileged(pte_t pte)
  120. {
  121. return __pte(pte_val(pte) & ~_PAGE_USER);
  122. }
  123. #endif
  124. #ifndef pte_mkuser
  125. static inline pte_t pte_mkuser(pte_t pte)
  126. {
  127. return __pte(pte_val(pte) | _PAGE_USER);
  128. }
  129. #endif
  130. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  131. {
  132. return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
  133. }
  134. /* Insert a PTE, top-level function is out of line. It uses an inline
  135. * low level function in the respective pgtable-* files
  136. */
  137. extern void set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
  138. pte_t pte);
  139. /* This low level function performs the actual PTE insertion
  140. * Setting the PTE depends on the MMU type and other factors. It's
  141. * an horrible mess that I'm not going to try to clean up now but
  142. * I'm keeping it in one place rather than spread around
  143. */
  144. static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
  145. pte_t *ptep, pte_t pte, int percpu)
  146. {
  147. /* Second case is 32-bit with 64-bit PTE. In this case, we
  148. * can just store as long as we do the two halves in the right order
  149. * with a barrier in between.
  150. * In the percpu case, we also fallback to the simple update
  151. */
  152. if (IS_ENABLED(CONFIG_PPC32) && IS_ENABLED(CONFIG_PTE_64BIT) && !percpu) {
  153. __asm__ __volatile__("\
  154. stw%X0 %2,%0\n\
  155. mbar\n\
  156. stw%X1 %L2,%1"
  157. : "=m" (*ptep), "=m" (*((unsigned char *)ptep+4))
  158. : "r" (pte) : "memory");
  159. return;
  160. }
  161. /* Anything else just stores the PTE normally. That covers all 64-bit
  162. * cases, and 32-bit non-hash with 32-bit PTEs.
  163. */
  164. #if defined(CONFIG_PPC_8xx) && defined(CONFIG_PPC_16K_PAGES)
  165. ptep->pte = ptep->pte1 = ptep->pte2 = ptep->pte3 = pte_val(pte);
  166. #else
  167. *ptep = pte;
  168. #endif
  169. /*
  170. * With hardware tablewalk, a sync is needed to ensure that
  171. * subsequent accesses see the PTE we just wrote. Unlike userspace
  172. * mappings, we can't tolerate spurious faults, so make sure
  173. * the new PTE will be seen the first time.
  174. */
  175. if (IS_ENABLED(CONFIG_PPC_BOOK3E_64) && is_kernel_addr(addr))
  176. mb();
  177. }
  178. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  179. extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address,
  180. pte_t *ptep, pte_t entry, int dirty);
  181. /*
  182. * Macro to mark a page protection value as "uncacheable".
  183. */
  184. #define _PAGE_CACHE_CTL (_PAGE_COHERENT | _PAGE_GUARDED | _PAGE_NO_CACHE | \
  185. _PAGE_WRITETHRU)
  186. #define pgprot_noncached(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
  187. _PAGE_NO_CACHE | _PAGE_GUARDED))
  188. #define pgprot_noncached_wc(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
  189. _PAGE_NO_CACHE))
  190. #define pgprot_cached(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
  191. _PAGE_COHERENT))
  192. #if _PAGE_WRITETHRU != 0
  193. #define pgprot_cached_wthru(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
  194. _PAGE_COHERENT | _PAGE_WRITETHRU))
  195. #else
  196. #define pgprot_cached_wthru(prot) pgprot_noncached(prot)
  197. #endif
  198. #define pgprot_cached_noncoherent(prot) \
  199. (__pgprot(pgprot_val(prot) & ~_PAGE_CACHE_CTL))
  200. #define pgprot_writecombine pgprot_noncached_wc
  201. struct file;
  202. extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  203. unsigned long size, pgprot_t vma_prot);
  204. #define __HAVE_PHYS_MEM_ACCESS_PROT
  205. #ifdef CONFIG_HUGETLB_PAGE
  206. static inline int hugepd_ok(hugepd_t hpd)
  207. {
  208. #ifdef CONFIG_PPC_8xx
  209. return ((hpd_val(hpd) & _PMD_PAGE_MASK) == _PMD_PAGE_8M);
  210. #else
  211. /* We clear the top bit to indicate hugepd */
  212. return (hpd_val(hpd) && (hpd_val(hpd) & PD_HUGE) == 0);
  213. #endif
  214. }
  215. static inline int pmd_huge(pmd_t pmd)
  216. {
  217. return 0;
  218. }
  219. static inline int pud_huge(pud_t pud)
  220. {
  221. return 0;
  222. }
  223. #define is_hugepd(hpd) (hugepd_ok(hpd))
  224. #endif
  225. /*
  226. * This gets called at the end of handling a page fault, when
  227. * the kernel has put a new PTE into the page table for the process.
  228. * We use it to ensure coherency between the i-cache and d-cache
  229. * for the page which has just been mapped in.
  230. */
  231. #if defined(CONFIG_PPC_E500) && defined(CONFIG_HUGETLB_PAGE)
  232. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep);
  233. #else
  234. static inline
  235. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep) {}
  236. #endif
  237. #endif /* __ASSEMBLY__ */
  238. #endif