pgtable.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. * Copyright 2003 PathScale, Inc.
  5. * Derived from include/asm-i386/pgtable.h
  6. */
  7. #ifndef __UM_PGTABLE_H
  8. #define __UM_PGTABLE_H
  9. #include <asm/page.h>
  10. #include <linux/mm_types.h>
  11. #define _PAGE_PRESENT 0x001
  12. #define _PAGE_NEEDSYNC 0x002
  13. #define _PAGE_RW 0x020
  14. #define _PAGE_USER 0x040
  15. #define _PAGE_ACCESSED 0x080
  16. #define _PAGE_DIRTY 0x100
  17. /* If _PAGE_PRESENT is clear, we use these: */
  18. #define _PAGE_PROTNONE 0x010 /* if the user mapped it with PROT_NONE;
  19. pte_present gives true */
  20. /* We borrow bit 10 to store the exclusive marker in swap PTEs. */
  21. #define _PAGE_SWP_EXCLUSIVE 0x400
  22. #if CONFIG_PGTABLE_LEVELS == 4
  23. #include <asm/pgtable-4level.h>
  24. #elif CONFIG_PGTABLE_LEVELS == 2
  25. #include <asm/pgtable-2level.h>
  26. #else
  27. #error "Unsupported number of page table levels"
  28. #endif
  29. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  30. /* zero page used for uninitialized stuff */
  31. extern unsigned long *empty_zero_page;
  32. /* Just any arbitrary offset to the start of the vmalloc VM area: the
  33. * current 8MB value just means that there will be a 8MB "hole" after the
  34. * physical memory until the kernel virtual memory starts. That means that
  35. * any out-of-bounds memory accesses will hopefully be caught.
  36. * The vmalloc() routines leaves a hole of 4kB between each vmalloced
  37. * area for the same reason. ;)
  38. */
  39. #ifndef COMPILE_OFFSETS
  40. #include <as-layout.h> /* for high_physmem */
  41. #endif
  42. #define VMALLOC_OFFSET (__va_space)
  43. #define VMALLOC_START ((high_physmem + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1))
  44. #define VMALLOC_END (TASK_SIZE-2*PAGE_SIZE)
  45. #define MODULES_VADDR VMALLOC_START
  46. #define MODULES_END VMALLOC_END
  47. #define _PAGE_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)
  48. #define _KERNPG_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  49. #define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
  50. #define __PAGE_KERNEL_EXEC \
  51. (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
  52. #define PAGE_NONE __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
  53. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED)
  54. #define PAGE_COPY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
  55. #define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
  56. #define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
  57. #define PAGE_KERNEL_EXEC __pgprot(__PAGE_KERNEL_EXEC)
  58. /*
  59. * The i386 can't do page protection for execute, and considers that the same
  60. * are read.
  61. * Also, write permissions imply read permissions. This is the closest we can
  62. * get..
  63. */
  64. /*
  65. * ZERO_PAGE is a global shared page that is always zero: used
  66. * for zero-mapped memory areas etc..
  67. */
  68. #define ZERO_PAGE(vaddr) virt_to_page(empty_zero_page)
  69. #define pte_clear(mm, addr, xp) pte_set_val(*(xp), (phys_t) 0, __pgprot(_PAGE_NEEDSYNC))
  70. #define pmd_none(x) (!((unsigned long)pmd_val(x) & ~_PAGE_NEEDSYNC))
  71. #define pmd_bad(x) ((pmd_val(x) & (~PAGE_MASK & ~_PAGE_USER)) != _KERNPG_TABLE)
  72. #define pmd_present(x) (pmd_val(x) & _PAGE_PRESENT)
  73. #define pmd_clear(xp) do { pmd_val(*(xp)) = _PAGE_NEEDSYNC; } while (0)
  74. #define pmd_needsync(x) (pmd_val(x) & _PAGE_NEEDSYNC)
  75. #define pmd_mkuptodate(x) (pmd_val(x) &= ~_PAGE_NEEDSYNC)
  76. #define pud_needsync(x) (pud_val(x) & _PAGE_NEEDSYNC)
  77. #define pud_mkuptodate(x) (pud_val(x) &= ~_PAGE_NEEDSYNC)
  78. #define p4d_needsync(x) (p4d_val(x) & _PAGE_NEEDSYNC)
  79. #define p4d_mkuptodate(x) (p4d_val(x) &= ~_PAGE_NEEDSYNC)
  80. #define pmd_pfn(pmd) (pmd_val(pmd) >> PAGE_SHIFT)
  81. #define pmd_page(pmd) phys_to_page(pmd_val(pmd) & PAGE_MASK)
  82. #define pte_page(x) pfn_to_page(pte_pfn(x))
  83. #define pte_present(x) pte_get_bits(x, (_PAGE_PRESENT | _PAGE_PROTNONE))
  84. /*
  85. * =================================
  86. * Flags checking section.
  87. * =================================
  88. */
  89. static inline int pte_none(pte_t pte)
  90. {
  91. return pte_is_zero(pte);
  92. }
  93. /*
  94. * The following only work if pte_present() is true.
  95. * Undefined behaviour if not..
  96. */
  97. static inline int pte_read(pte_t pte)
  98. {
  99. return((pte_get_bits(pte, _PAGE_USER)) &&
  100. !(pte_get_bits(pte, _PAGE_PROTNONE)));
  101. }
  102. static inline int pte_exec(pte_t pte){
  103. return((pte_get_bits(pte, _PAGE_USER)) &&
  104. !(pte_get_bits(pte, _PAGE_PROTNONE)));
  105. }
  106. static inline int pte_write(pte_t pte)
  107. {
  108. return((pte_get_bits(pte, _PAGE_RW)) &&
  109. !(pte_get_bits(pte, _PAGE_PROTNONE)));
  110. }
  111. static inline int pte_dirty(pte_t pte)
  112. {
  113. return pte_get_bits(pte, _PAGE_DIRTY);
  114. }
  115. static inline int pte_young(pte_t pte)
  116. {
  117. return pte_get_bits(pte, _PAGE_ACCESSED);
  118. }
  119. static inline int pte_needsync(pte_t pte)
  120. {
  121. return pte_get_bits(pte, _PAGE_NEEDSYNC);
  122. }
  123. /*
  124. * =================================
  125. * Flags setting section.
  126. * =================================
  127. */
  128. static inline pte_t pte_mkclean(pte_t pte)
  129. {
  130. pte_clear_bits(pte, _PAGE_DIRTY);
  131. return(pte);
  132. }
  133. static inline pte_t pte_mkold(pte_t pte)
  134. {
  135. pte_clear_bits(pte, _PAGE_ACCESSED);
  136. return(pte);
  137. }
  138. static inline pte_t pte_wrprotect(pte_t pte)
  139. {
  140. pte_clear_bits(pte, _PAGE_RW);
  141. return pte;
  142. }
  143. static inline pte_t pte_mkread(pte_t pte)
  144. {
  145. pte_set_bits(pte, _PAGE_USER);
  146. return pte;
  147. }
  148. static inline pte_t pte_mkdirty(pte_t pte)
  149. {
  150. pte_set_bits(pte, _PAGE_DIRTY);
  151. return(pte);
  152. }
  153. static inline pte_t pte_mkyoung(pte_t pte)
  154. {
  155. pte_set_bits(pte, _PAGE_ACCESSED);
  156. return(pte);
  157. }
  158. static inline pte_t pte_mkwrite_novma(pte_t pte)
  159. {
  160. pte_set_bits(pte, _PAGE_RW);
  161. return pte;
  162. }
  163. static inline pte_t pte_mkuptodate(pte_t pte)
  164. {
  165. pte_clear_bits(pte, _PAGE_NEEDSYNC);
  166. return pte;
  167. }
  168. static inline pte_t pte_mkneedsync(pte_t pte)
  169. {
  170. pte_set_bits(pte, _PAGE_NEEDSYNC);
  171. return(pte);
  172. }
  173. static inline void set_pte(pte_t *pteptr, pte_t pteval)
  174. {
  175. pte_copy(*pteptr, pteval);
  176. /* If it's a swap entry, it needs to be marked _PAGE_NEEDSYNC so
  177. * update_pte_range knows to unmap it.
  178. */
  179. *pteptr = pte_mkneedsync(*pteptr);
  180. }
  181. #define PFN_PTE_SHIFT PAGE_SHIFT
  182. static inline void um_tlb_mark_sync(struct mm_struct *mm, unsigned long start,
  183. unsigned long end)
  184. {
  185. guard(spinlock_irqsave)(&mm->context.sync_tlb_lock);
  186. if (!mm->context.sync_tlb_range_to) {
  187. mm->context.sync_tlb_range_from = start;
  188. mm->context.sync_tlb_range_to = end;
  189. } else {
  190. if (start < mm->context.sync_tlb_range_from)
  191. mm->context.sync_tlb_range_from = start;
  192. if (end > mm->context.sync_tlb_range_to)
  193. mm->context.sync_tlb_range_to = end;
  194. }
  195. }
  196. #define set_ptes set_ptes
  197. static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
  198. pte_t *ptep, pte_t pte, int nr)
  199. {
  200. /* Basically the default implementation */
  201. size_t length = nr * PAGE_SIZE;
  202. for (;;) {
  203. set_pte(ptep, pte);
  204. if (--nr == 0)
  205. break;
  206. ptep++;
  207. pte = __pte(pte_val(pte) + (nr << PFN_PTE_SHIFT));
  208. }
  209. um_tlb_mark_sync(mm, addr, addr + length);
  210. }
  211. #define __HAVE_ARCH_PTE_SAME
  212. static inline int pte_same(pte_t pte_a, pte_t pte_b)
  213. {
  214. return !((pte_val(pte_a) ^ pte_val(pte_b)) & ~_PAGE_NEEDSYNC);
  215. }
  216. #define __virt_to_page(virt) phys_to_page(__pa(virt))
  217. #define virt_to_page(addr) __virt_to_page((const unsigned long) addr)
  218. static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot)
  219. {
  220. pte_t pte;
  221. pte_set_val(pte, pfn_to_phys(pfn), pgprot);
  222. return pte;
  223. }
  224. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  225. {
  226. pte_set_val(pte, (pte_val(pte) & _PAGE_CHG_MASK), newprot);
  227. return pte;
  228. }
  229. /*
  230. * the pmd page can be thought of an array like this: pmd_t[PTRS_PER_PMD]
  231. *
  232. * this macro returns the index of the entry in the pmd page which would
  233. * control the given virtual address
  234. */
  235. #define pmd_page_vaddr(pmd) ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
  236. struct mm_struct;
  237. extern pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr);
  238. #define update_mmu_cache(vma,address,ptep) do {} while (0)
  239. #define update_mmu_cache_range(vmf, vma, address, ptep, nr) do {} while (0)
  240. /*
  241. * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
  242. * are !pte_none() && !pte_present().
  243. *
  244. * Format of swap PTEs:
  245. *
  246. * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  247. * 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
  248. * <--------------- offset ----------------> E < type -> 0 0 0 1 0
  249. *
  250. * E is the exclusive marker that is not stored in swap entries.
  251. * _PAGE_NEEDSYNC (bit 1) is always set to 1 in set_pte().
  252. */
  253. #define __swp_type(x) (((x).val >> 5) & 0x1f)
  254. #define __swp_offset(x) ((x).val >> 11)
  255. #define __swp_entry(type, offset) \
  256. ((swp_entry_t) { (((type) & 0x1f) << 5) | ((offset) << 11) })
  257. #define __pte_to_swp_entry(pte) \
  258. ((swp_entry_t) { pte_val(pte_mkuptodate(pte)) })
  259. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  260. static inline bool pte_swp_exclusive(pte_t pte)
  261. {
  262. return pte_get_bits(pte, _PAGE_SWP_EXCLUSIVE);
  263. }
  264. static inline pte_t pte_swp_mkexclusive(pte_t pte)
  265. {
  266. pte_set_bits(pte, _PAGE_SWP_EXCLUSIVE);
  267. return pte;
  268. }
  269. static inline pte_t pte_swp_clear_exclusive(pte_t pte)
  270. {
  271. pte_clear_bits(pte, _PAGE_SWP_EXCLUSIVE);
  272. return pte;
  273. }
  274. #endif