pgtable.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #ifndef _ASM_X86_PGTABLE_H
  2. #define _ASM_X86_PGTABLE_H
  3. #define USER_PTRS_PER_PGD ((TASK_SIZE-1)/PGDIR_SIZE+1)
  4. #define FIRST_USER_ADDRESS 0
  5. #define _PAGE_BIT_PRESENT 0
  6. #define _PAGE_BIT_RW 1
  7. #define _PAGE_BIT_USER 2
  8. #define _PAGE_BIT_PWT 3
  9. #define _PAGE_BIT_PCD 4
  10. #define _PAGE_BIT_ACCESSED 5
  11. #define _PAGE_BIT_DIRTY 6
  12. #define _PAGE_BIT_FILE 6
  13. #define _PAGE_BIT_PSE 7 /* 4 MB (or 2MB) page */
  14. #define _PAGE_BIT_GLOBAL 8 /* Global TLB entry PPro+ */
  15. #define _PAGE_BIT_UNUSED1 9 /* available for programmer */
  16. #define _PAGE_BIT_UNUSED2 10
  17. #define _PAGE_BIT_UNUSED3 11
  18. #define _PAGE_BIT_NX 63 /* No execute: only valid after cpuid check */
  19. #define _PAGE_PRESENT (_AC(1, L)<<_PAGE_BIT_PRESENT)
  20. #define _PAGE_RW (_AC(1, L)<<_PAGE_BIT_RW)
  21. #define _PAGE_USER (_AC(1, L)<<_PAGE_BIT_USER)
  22. #define _PAGE_PWT (_AC(1, L)<<_PAGE_BIT_PWT)
  23. #define _PAGE_PCD (_AC(1, L)<<_PAGE_BIT_PCD)
  24. #define _PAGE_ACCESSED (_AC(1, L)<<_PAGE_BIT_ACCESSED)
  25. #define _PAGE_DIRTY (_AC(1, L)<<_PAGE_BIT_DIRTY)
  26. #define _PAGE_PSE (_AC(1, L)<<_PAGE_BIT_PSE) /* 2MB page */
  27. #define _PAGE_GLOBAL (_AC(1, L)<<_PAGE_BIT_GLOBAL) /* Global TLB entry */
  28. #define _PAGE_UNUSED1 (_AC(1, L)<<_PAGE_BIT_UNUSED1)
  29. #define _PAGE_UNUSED2 (_AC(1, L)<<_PAGE_BIT_UNUSED2)
  30. #define _PAGE_UNUSED3 (_AC(1, L)<<_PAGE_BIT_UNUSED3)
  31. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  32. #define _PAGE_NX (_AC(1, ULL) << _PAGE_BIT_NX)
  33. #else
  34. #define _PAGE_NX 0
  35. #endif
  36. /* If _PAGE_PRESENT is clear, we use these: */
  37. #define _PAGE_FILE _PAGE_DIRTY /* nonlinear file mapping, saved PTE; unset:swap */
  38. #define _PAGE_PROTNONE _PAGE_PSE /* if the user mapped it with PROT_NONE;
  39. pte_present gives true */
  40. #define _PAGE_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)
  41. #define _KERNPG_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  42. #define _PAGE_CHG_MASK (PTE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
  43. #define PAGE_NONE __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
  44. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
  45. #define PAGE_SHARED_EXEC __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED)
  46. #define PAGE_COPY_NOEXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
  47. #define PAGE_COPY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
  48. #define PAGE_COPY PAGE_COPY_NOEXEC
  49. #define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
  50. #define PAGE_READONLY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
  51. #ifdef CONFIG_X86_32
  52. #define _PAGE_KERNEL_EXEC \
  53. (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
  54. #define _PAGE_KERNEL (_PAGE_KERNEL_EXEC | _PAGE_NX)
  55. #ifndef __ASSEMBLY__
  56. extern unsigned long long __PAGE_KERNEL, __PAGE_KERNEL_EXEC;
  57. #endif /* __ASSEMBLY__ */
  58. #else
  59. #define __PAGE_KERNEL_EXEC \
  60. (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
  61. #define __PAGE_KERNEL (__PAGE_KERNEL_EXEC | _PAGE_NX)
  62. #endif
  63. #define __PAGE_KERNEL_RO (__PAGE_KERNEL & ~_PAGE_RW)
  64. #define __PAGE_KERNEL_RX (__PAGE_KERNEL_EXEC & ~_PAGE_RW)
  65. #define __PAGE_KERNEL_NOCACHE (__PAGE_KERNEL | _PAGE_PCD | _PAGE_PWT)
  66. #define __PAGE_KERNEL_VSYSCALL (__PAGE_KERNEL_RX | _PAGE_USER)
  67. #define __PAGE_KERNEL_VSYSCALL_NOCACHE (__PAGE_KERNEL_VSYSCALL | _PAGE_PCD | _PAGE_PWT)
  68. #define __PAGE_KERNEL_LARGE (__PAGE_KERNEL | _PAGE_PSE)
  69. #define __PAGE_KERNEL_LARGE_EXEC (__PAGE_KERNEL_EXEC | _PAGE_PSE)
  70. #ifdef CONFIG_X86_32
  71. # define MAKE_GLOBAL(x) __pgprot((x))
  72. #else
  73. # define MAKE_GLOBAL(x) __pgprot((x) | _PAGE_GLOBAL)
  74. #endif
  75. #define PAGE_KERNEL MAKE_GLOBAL(__PAGE_KERNEL)
  76. #define PAGE_KERNEL_RO MAKE_GLOBAL(__PAGE_KERNEL_RO)
  77. #define PAGE_KERNEL_EXEC MAKE_GLOBAL(__PAGE_KERNEL_EXEC)
  78. #define PAGE_KERNEL_RX MAKE_GLOBAL(__PAGE_KERNEL_RX)
  79. #define PAGE_KERNEL_NOCACHE MAKE_GLOBAL(__PAGE_KERNEL_NOCACHE)
  80. #define PAGE_KERNEL_LARGE MAKE_GLOBAL(__PAGE_KERNEL_LARGE)
  81. #define PAGE_KERNEL_LARGE_EXEC MAKE_GLOBAL(__PAGE_KERNEL_LARGE_EXEC)
  82. #define PAGE_KERNEL_VSYSCALL MAKE_GLOBAL(__PAGE_KERNEL_VSYSCALL)
  83. #define PAGE_KERNEL_VSYSCALL_NOCACHE MAKE_GLOBAL(__PAGE_KERNEL_VSYSCALL_NOCACHE)
  84. /* xwr */
  85. #define __P000 PAGE_NONE
  86. #define __P001 PAGE_READONLY
  87. #define __P010 PAGE_COPY
  88. #define __P011 PAGE_COPY
  89. #define __P100 PAGE_READONLY_EXEC
  90. #define __P101 PAGE_READONLY_EXEC
  91. #define __P110 PAGE_COPY_EXEC
  92. #define __P111 PAGE_COPY_EXEC
  93. #define __S000 PAGE_NONE
  94. #define __S001 PAGE_READONLY
  95. #define __S010 PAGE_SHARED
  96. #define __S011 PAGE_SHARED
  97. #define __S100 PAGE_READONLY_EXEC
  98. #define __S101 PAGE_READONLY_EXEC
  99. #define __S110 PAGE_SHARED_EXEC
  100. #define __S111 PAGE_SHARED_EXEC
  101. #ifndef __ASSEMBLY__
  102. /*
  103. * ZERO_PAGE is a global shared page that is always zero: used
  104. * for zero-mapped memory areas etc..
  105. */
  106. extern unsigned long empty_zero_page[PAGE_SIZE/sizeof(unsigned long)];
  107. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  108. /*
  109. * The following only work if pte_present() is true.
  110. * Undefined behaviour if not..
  111. */
  112. static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
  113. static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
  114. static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_RW; }
  115. static inline int pte_file(pte_t pte) { return pte_val(pte) & _PAGE_FILE; }
  116. static inline int pte_huge(pte_t pte) { return pte_val(pte) & _PAGE_PSE; }
  117. static inline int pmd_large(pmd_t pte) {
  118. return (pmd_val(pte) & (_PAGE_PSE|_PAGE_PRESENT)) ==
  119. (_PAGE_PSE|_PAGE_PRESENT);
  120. }
  121. static inline pte_t pte_mkclean(pte_t pte) { return __pte(pte_val(pte) & ~_PAGE_DIRTY); }
  122. static inline pte_t pte_mkold(pte_t pte) { return __pte(pte_val(pte) & ~_PAGE_ACCESSED); }
  123. static inline pte_t pte_wrprotect(pte_t pte) { return __pte(pte_val(pte) & ~_PAGE_RW); }
  124. static inline pte_t pte_mkexec(pte_t pte) { return __pte(pte_val(pte) & ~_PAGE_NX); }
  125. static inline pte_t pte_mkdirty(pte_t pte) { return __pte(pte_val(pte) | _PAGE_DIRTY); }
  126. static inline pte_t pte_mkyoung(pte_t pte) { return __pte(pte_val(pte) | _PAGE_ACCESSED); }
  127. static inline pte_t pte_mkwrite(pte_t pte) { return __pte(pte_val(pte) | _PAGE_RW); }
  128. static inline pte_t pte_mkhuge(pte_t pte) { return __pte(pte_val(pte) | _PAGE_PSE); }
  129. static inline pte_t pte_clrhuge(pte_t pte) { return __pte(pte_val(pte) & ~_PAGE_PSE); }
  130. extern pteval_t __supported_pte_mask;
  131. static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
  132. {
  133. return __pte((((phys_addr_t)page_nr << PAGE_SHIFT) |
  134. pgprot_val(pgprot)) & __supported_pte_mask);
  135. }
  136. static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
  137. {
  138. return __pmd((((phys_addr_t)page_nr << PAGE_SHIFT) |
  139. pgprot_val(pgprot)) & __supported_pte_mask);
  140. }
  141. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  142. {
  143. pteval_t val = pte_val(pte);
  144. /*
  145. * Chop off the NX bit (if present), and add the NX portion of
  146. * the newprot (if present):
  147. */
  148. val &= _PAGE_CHG_MASK & ~_PAGE_NX;
  149. val |= pgprot_val(newprot) & __supported_pte_mask;
  150. return __pte(val);
  151. }
  152. #endif /* __ASSEMBLY__ */
  153. #ifdef CONFIG_X86_32
  154. # include "pgtable_32.h"
  155. #else
  156. # include "pgtable_64.h"
  157. #endif
  158. #ifndef __ASSEMBLY__
  159. #ifndef CONFIG_PARAVIRT
  160. /*
  161. * Rules for using pte_update - it must be called after any PTE update which
  162. * has not been done using the set_pte / clear_pte interfaces. It is used by
  163. * shadow mode hypervisors to resynchronize the shadow page tables. Kernel PTE
  164. * updates should either be sets, clears, or set_pte_atomic for P->P
  165. * transitions, which means this hook should only be called for user PTEs.
  166. * This hook implies a P->P protection or access change has taken place, which
  167. * requires a subsequent TLB flush. The notification can optionally be delayed
  168. * until the TLB flush event by using the pte_update_defer form of the
  169. * interface, but care must be taken to assure that the flush happens while
  170. * still holding the same page table lock so that the shadow and primary pages
  171. * do not become out of sync on SMP.
  172. */
  173. #define pte_update(mm, addr, ptep) do { } while (0)
  174. #define pte_update_defer(mm, addr, ptep) do { } while (0)
  175. #endif
  176. /* local pte updates need not use xchg for locking */
  177. static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
  178. {
  179. pte_t res = *ptep;
  180. /* Pure native function needs no input for mm, addr */
  181. native_pte_clear(NULL, 0, ptep);
  182. return res;
  183. }
  184. /*
  185. * We only update the dirty/accessed state if we set
  186. * the dirty bit by hand in the kernel, since the hardware
  187. * will do the accessed bit for us, and we don't want to
  188. * race with other CPU's that might be updating the dirty
  189. * bit at the same time.
  190. */
  191. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  192. #define ptep_set_access_flags(vma, address, ptep, entry, dirty) \
  193. ({ \
  194. int __changed = !pte_same(*(ptep), entry); \
  195. if (__changed && dirty) { \
  196. *ptep = entry; \
  197. pte_update_defer((vma)->vm_mm, (address), (ptep)); \
  198. flush_tlb_page(vma, address); \
  199. } \
  200. __changed; \
  201. })
  202. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  203. #define ptep_test_and_clear_young(vma, addr, ptep) ({ \
  204. int __ret = 0; \
  205. if (pte_young(*(ptep))) \
  206. __ret = test_and_clear_bit(_PAGE_BIT_ACCESSED, \
  207. &(ptep)->pte); \
  208. if (__ret) \
  209. pte_update((vma)->vm_mm, addr, ptep); \
  210. __ret; \
  211. })
  212. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  213. #define ptep_clear_flush_young(vma, address, ptep) \
  214. ({ \
  215. int __young; \
  216. __young = ptep_test_and_clear_young((vma), (address), (ptep)); \
  217. if (__young) \
  218. flush_tlb_page(vma, address); \
  219. __young; \
  220. })
  221. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  222. static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  223. {
  224. pte_t pte = native_ptep_get_and_clear(ptep);
  225. pte_update(mm, addr, ptep);
  226. return pte;
  227. }
  228. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  229. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, unsigned long addr, pte_t *ptep, int full)
  230. {
  231. pte_t pte;
  232. if (full) {
  233. /*
  234. * Full address destruction in progress; paravirt does not
  235. * care about updates and native needs no locking
  236. */
  237. pte = native_local_ptep_get_and_clear(ptep);
  238. } else {
  239. pte = ptep_get_and_clear(mm, addr, ptep);
  240. }
  241. return pte;
  242. }
  243. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  244. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  245. {
  246. clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
  247. pte_update(mm, addr, ptep);
  248. }
  249. #ifndef CONFIG_PARAVIRT
  250. #define pte_clear(mm, addr, ptep) native_pte_clear(mm, addr, ptep)
  251. #endif /* !CONFIG_PARAVIRT */
  252. #include <asm-generic/pgtable.h>
  253. #endif /* __ASSEMBLY__ */
  254. #endif /* _ASM_X86_PGTABLE_H */