pgtable.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * arch/arm/include/asm/pgtable.h
  4. *
  5. * Copyright (C) 1995-2002 Russell King
  6. */
  7. #ifndef _ASMARM_PGTABLE_H
  8. #define _ASMARM_PGTABLE_H
  9. #include <linux/const.h>
  10. #include <asm/proc-fns.h>
  11. #ifndef __ASSEMBLY__
  12. /*
  13. * ZERO_PAGE is a global shared page that is always zero: used
  14. * for zero-mapped memory areas etc..
  15. */
  16. extern struct page *empty_zero_page;
  17. #define ZERO_PAGE(vaddr) (empty_zero_page)
  18. #endif
  19. #ifndef CONFIG_MMU
  20. #include <asm-generic/pgtable-nopud.h>
  21. #include <asm/pgtable-nommu.h>
  22. #else
  23. #include <asm-generic/pgtable-nopud.h>
  24. #include <asm/memory.h>
  25. #include <asm/pgtable-hwdef.h>
  26. #include <asm/tlbflush.h>
  27. #ifdef CONFIG_ARM_LPAE
  28. #include <asm/pgtable-3level.h>
  29. #else
  30. #include <asm/pgtable-2level.h>
  31. #endif
  32. /*
  33. * Just any arbitrary offset to the start of the vmalloc VM area: the
  34. * current 8MB value just means that there will be a 8MB "hole" after the
  35. * physical memory until the kernel virtual memory starts. That means that
  36. * any out-of-bounds memory accesses will hopefully be caught.
  37. * The vmalloc() routines leaves a hole of 4kB between each vmalloced
  38. * area for the same reason. ;)
  39. */
  40. #define VMALLOC_OFFSET (8*1024*1024)
  41. #define VMALLOC_START (((unsigned long)high_memory + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1))
  42. #define VMALLOC_END 0xff800000UL
  43. #define LIBRARY_TEXT_START 0x0c000000
  44. #ifndef __ASSEMBLY__
  45. extern void __pte_error(const char *file, int line, pte_t);
  46. extern void __pmd_error(const char *file, int line, pmd_t);
  47. extern void __pgd_error(const char *file, int line, pgd_t);
  48. #define pte_ERROR(pte) __pte_error(__FILE__, __LINE__, pte)
  49. #define pmd_ERROR(pmd) __pmd_error(__FILE__, __LINE__, pmd)
  50. #define pgd_ERROR(pgd) __pgd_error(__FILE__, __LINE__, pgd)
  51. /*
  52. * This is the lowest virtual address we can permit any user space
  53. * mapping to be mapped at. This is particularly important for
  54. * non-high vector CPUs.
  55. */
  56. #define FIRST_USER_ADDRESS (PAGE_SIZE * 2)
  57. /*
  58. * Use TASK_SIZE as the ceiling argument for free_pgtables() and
  59. * free_pgd_range() to avoid freeing the modules pmd when LPAE is enabled (pmd
  60. * page shared between user and kernel).
  61. */
  62. #ifdef CONFIG_ARM_LPAE
  63. #define USER_PGTABLES_CEILING TASK_SIZE
  64. #endif
  65. /*
  66. * The pgprot_* and protection_map entries will be fixed up in runtime
  67. * to include the cachable and bufferable bits based on memory policy,
  68. * as well as any architecture dependent bits like global/ASID and SMP
  69. * shared mapping bits.
  70. */
  71. #define _L_PTE_DEFAULT L_PTE_PRESENT | L_PTE_YOUNG
  72. extern pgprot_t pgprot_user;
  73. extern pgprot_t pgprot_kernel;
  74. #define _MOD_PROT(p, b) __pgprot(pgprot_val(p) | (b))
  75. #define PAGE_NONE _MOD_PROT(pgprot_user, L_PTE_XN | L_PTE_RDONLY | L_PTE_NONE)
  76. #define PAGE_SHARED _MOD_PROT(pgprot_user, L_PTE_USER | L_PTE_XN)
  77. #define PAGE_SHARED_EXEC _MOD_PROT(pgprot_user, L_PTE_USER)
  78. #define PAGE_COPY _MOD_PROT(pgprot_user, L_PTE_USER | L_PTE_RDONLY | L_PTE_XN)
  79. #define PAGE_COPY_EXEC _MOD_PROT(pgprot_user, L_PTE_USER | L_PTE_RDONLY)
  80. #define PAGE_READONLY _MOD_PROT(pgprot_user, L_PTE_USER | L_PTE_RDONLY | L_PTE_XN)
  81. #define PAGE_READONLY_EXEC _MOD_PROT(pgprot_user, L_PTE_USER | L_PTE_RDONLY)
  82. #define PAGE_KERNEL _MOD_PROT(pgprot_kernel, L_PTE_XN)
  83. #define PAGE_KERNEL_EXEC pgprot_kernel
  84. #define __PAGE_NONE __pgprot(_L_PTE_DEFAULT | L_PTE_RDONLY | L_PTE_XN | L_PTE_NONE)
  85. #define __PAGE_SHARED __pgprot(_L_PTE_DEFAULT | L_PTE_USER | L_PTE_XN)
  86. #define __PAGE_SHARED_EXEC __pgprot(_L_PTE_DEFAULT | L_PTE_USER)
  87. #define __PAGE_COPY __pgprot(_L_PTE_DEFAULT | L_PTE_USER | L_PTE_RDONLY | L_PTE_XN)
  88. #define __PAGE_COPY_EXEC __pgprot(_L_PTE_DEFAULT | L_PTE_USER | L_PTE_RDONLY)
  89. #define __PAGE_READONLY __pgprot(_L_PTE_DEFAULT | L_PTE_USER | L_PTE_RDONLY | L_PTE_XN)
  90. #define __PAGE_READONLY_EXEC __pgprot(_L_PTE_DEFAULT | L_PTE_USER | L_PTE_RDONLY)
  91. #define __pgprot_modify(prot,mask,bits) \
  92. __pgprot((pgprot_val(prot) & ~(mask)) | (bits))
  93. #define pgprot_noncached(prot) \
  94. __pgprot_modify(prot, L_PTE_MT_MASK, L_PTE_MT_UNCACHED)
  95. #define pgprot_writecombine(prot) \
  96. __pgprot_modify(prot, L_PTE_MT_MASK, L_PTE_MT_BUFFERABLE)
  97. #define pgprot_stronglyordered(prot) \
  98. __pgprot_modify(prot, L_PTE_MT_MASK, L_PTE_MT_UNCACHED)
  99. #define pgprot_device(prot) \
  100. __pgprot_modify(prot, L_PTE_MT_MASK, L_PTE_MT_DEV_SHARED | L_PTE_SHARED | L_PTE_DIRTY | L_PTE_XN)
  101. #ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
  102. #define pgprot_dmacoherent(prot) \
  103. __pgprot_modify(prot, L_PTE_MT_MASK, L_PTE_MT_BUFFERABLE | L_PTE_XN)
  104. #define __HAVE_PHYS_MEM_ACCESS_PROT
  105. struct file;
  106. extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  107. unsigned long size, pgprot_t vma_prot);
  108. #else
  109. #define pgprot_dmacoherent(prot) \
  110. __pgprot_modify(prot, L_PTE_MT_MASK, L_PTE_MT_UNCACHED | L_PTE_XN)
  111. #endif
  112. #endif /* __ASSEMBLY__ */
  113. /*
  114. * The table below defines the page protection levels that we insert into our
  115. * Linux page table version. These get translated into the best that the
  116. * architecture can perform. Note that on most ARM hardware:
  117. * 1) We cannot do execute protection
  118. * 2) If we could do execute protection, then read is implied
  119. * 3) write implies read permissions
  120. */
  121. #ifndef __ASSEMBLY__
  122. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  123. #define pud_page(pud) pmd_page(__pmd(pud_val(pud)))
  124. #define pud_write(pud) pmd_write(__pmd(pud_val(pud)))
  125. #define pmd_none(pmd) (!pmd_val(pmd))
  126. static inline pte_t *pmd_page_vaddr(pmd_t pmd)
  127. {
  128. return __va(pmd_val(pmd) & PHYS_MASK & (s32)PAGE_MASK);
  129. }
  130. #define pmd_page(pmd) pfn_to_page(__phys_to_pfn(pmd_val(pmd) & PHYS_MASK))
  131. #define pte_pfn(pte) ((pte_val(pte) & PHYS_MASK) >> PAGE_SHIFT)
  132. #define pfn_pte(pfn,prot) __pte(__pfn_to_phys(pfn) | pgprot_val(prot))
  133. #define pte_page(pte) pfn_to_page(pte_pfn(pte))
  134. #define mk_pte(page,prot) pfn_pte(page_to_pfn(page), prot)
  135. #define pte_clear(mm,addr,ptep) set_pte_ext(ptep, __pte(0), 0)
  136. #define pte_isset(pte, val) ((u32)(val) == (val) ? pte_val(pte) & (val) \
  137. : !!(pte_val(pte) & (val)))
  138. #define pte_isclear(pte, val) (!(pte_val(pte) & (val)))
  139. #define pte_none(pte) (!pte_val(pte))
  140. #define pte_present(pte) (pte_isset((pte), L_PTE_PRESENT))
  141. #define pte_valid(pte) (pte_isset((pte), L_PTE_VALID))
  142. #define pte_accessible(mm, pte) (mm_tlb_flush_pending(mm) ? pte_present(pte) : pte_valid(pte))
  143. #define pte_write(pte) (pte_isclear((pte), L_PTE_RDONLY))
  144. #define pte_dirty(pte) (pte_isset((pte), L_PTE_DIRTY))
  145. #define pte_young(pte) (pte_isset((pte), L_PTE_YOUNG))
  146. #define pte_exec(pte) (pte_isclear((pte), L_PTE_XN))
  147. #define pte_valid_user(pte) \
  148. (pte_valid(pte) && pte_isset((pte), L_PTE_USER) && pte_young(pte))
  149. static inline bool pte_access_permitted(pte_t pte, bool write)
  150. {
  151. pteval_t mask = L_PTE_PRESENT | L_PTE_USER;
  152. pteval_t needed = mask;
  153. if (write)
  154. mask |= L_PTE_RDONLY;
  155. return (pte_val(pte) & mask) == needed;
  156. }
  157. #define pte_access_permitted pte_access_permitted
  158. #if __LINUX_ARM_ARCH__ < 6
  159. static inline void __sync_icache_dcache(pte_t pteval)
  160. {
  161. }
  162. #else
  163. extern void __sync_icache_dcache(pte_t pteval);
  164. #endif
  165. void set_pte_at(struct mm_struct *mm, unsigned long addr,
  166. pte_t *ptep, pte_t pteval);
  167. static inline pte_t clear_pte_bit(pte_t pte, pgprot_t prot)
  168. {
  169. pte_val(pte) &= ~pgprot_val(prot);
  170. return pte;
  171. }
  172. static inline pte_t set_pte_bit(pte_t pte, pgprot_t prot)
  173. {
  174. pte_val(pte) |= pgprot_val(prot);
  175. return pte;
  176. }
  177. static inline pte_t pte_wrprotect(pte_t pte)
  178. {
  179. return set_pte_bit(pte, __pgprot(L_PTE_RDONLY));
  180. }
  181. static inline pte_t pte_mkwrite(pte_t pte)
  182. {
  183. return clear_pte_bit(pte, __pgprot(L_PTE_RDONLY));
  184. }
  185. static inline pte_t pte_mkclean(pte_t pte)
  186. {
  187. return clear_pte_bit(pte, __pgprot(L_PTE_DIRTY));
  188. }
  189. static inline pte_t pte_mkdirty(pte_t pte)
  190. {
  191. return set_pte_bit(pte, __pgprot(L_PTE_DIRTY));
  192. }
  193. static inline pte_t pte_mkold(pte_t pte)
  194. {
  195. return clear_pte_bit(pte, __pgprot(L_PTE_YOUNG));
  196. }
  197. static inline pte_t pte_mkyoung(pte_t pte)
  198. {
  199. return set_pte_bit(pte, __pgprot(L_PTE_YOUNG));
  200. }
  201. static inline pte_t pte_mkexec(pte_t pte)
  202. {
  203. return clear_pte_bit(pte, __pgprot(L_PTE_XN));
  204. }
  205. static inline pte_t pte_mknexec(pte_t pte)
  206. {
  207. return set_pte_bit(pte, __pgprot(L_PTE_XN));
  208. }
  209. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  210. {
  211. const pteval_t mask = L_PTE_XN | L_PTE_RDONLY | L_PTE_USER |
  212. L_PTE_NONE | L_PTE_VALID;
  213. pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask);
  214. return pte;
  215. }
  216. /*
  217. * Encode and decode a swap entry. Swap entries are stored in the Linux
  218. * page tables as follows:
  219. *
  220. * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  221. * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  222. * <--------------- offset ------------------------> < type -> 0 0
  223. *
  224. * This gives us up to 31 swap files and 128GB per swap file. Note that
  225. * the offset field is always non-zero.
  226. */
  227. #define __SWP_TYPE_SHIFT 2
  228. #define __SWP_TYPE_BITS 5
  229. #define __SWP_TYPE_MASK ((1 << __SWP_TYPE_BITS) - 1)
  230. #define __SWP_OFFSET_SHIFT (__SWP_TYPE_BITS + __SWP_TYPE_SHIFT)
  231. #define __swp_type(x) (((x).val >> __SWP_TYPE_SHIFT) & __SWP_TYPE_MASK)
  232. #define __swp_offset(x) ((x).val >> __SWP_OFFSET_SHIFT)
  233. #define __swp_entry(type,offset) ((swp_entry_t) { ((type) << __SWP_TYPE_SHIFT) | ((offset) << __SWP_OFFSET_SHIFT) })
  234. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  235. #define __swp_entry_to_pte(swp) __pte((swp).val | PTE_TYPE_FAULT)
  236. /*
  237. * It is an error for the kernel to have more swap files than we can
  238. * encode in the PTEs. This ensures that we know when MAX_SWAPFILES
  239. * is increased beyond what we presently support.
  240. */
  241. #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > __SWP_TYPE_BITS)
  242. /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
  243. /* FIXME: this is not correct */
  244. #define kern_addr_valid(addr) (1)
  245. /*
  246. * We provide our own arch_get_unmapped_area to cope with VIPT caches.
  247. */
  248. #define HAVE_ARCH_UNMAPPED_AREA
  249. #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  250. #endif /* !__ASSEMBLY__ */
  251. #endif /* CONFIG_MMU */
  252. #endif /* _ASMARM_PGTABLE_H */