pgtable.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. /*
  20. * Note: we use _AC(1, L) instead of _AC(1, UL) so that we get a
  21. * sign-extended value on 32-bit with all 1's in the upper word,
  22. * which preserves the upper pte values on 64-bit ptes:
  23. */
  24. #define _PAGE_PRESENT (_AC(1, L)<<_PAGE_BIT_PRESENT)
  25. #define _PAGE_RW (_AC(1, L)<<_PAGE_BIT_RW)
  26. #define _PAGE_USER (_AC(1, L)<<_PAGE_BIT_USER)
  27. #define _PAGE_PWT (_AC(1, L)<<_PAGE_BIT_PWT)
  28. #define _PAGE_PCD (_AC(1, L)<<_PAGE_BIT_PCD)
  29. #define _PAGE_ACCESSED (_AC(1, L)<<_PAGE_BIT_ACCESSED)
  30. #define _PAGE_DIRTY (_AC(1, L)<<_PAGE_BIT_DIRTY)
  31. #define _PAGE_PSE (_AC(1, L)<<_PAGE_BIT_PSE) /* 2MB page */
  32. #define _PAGE_GLOBAL (_AC(1, L)<<_PAGE_BIT_GLOBAL) /* Global TLB entry */
  33. #define _PAGE_UNUSED1 (_AC(1, L)<<_PAGE_BIT_UNUSED1)
  34. #define _PAGE_UNUSED2 (_AC(1, L)<<_PAGE_BIT_UNUSED2)
  35. #define _PAGE_UNUSED3 (_AC(1, L)<<_PAGE_BIT_UNUSED3)
  36. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  37. #define _PAGE_NX (_AC(1, ULL) << _PAGE_BIT_NX)
  38. #else
  39. #define _PAGE_NX 0
  40. #endif
  41. /* If _PAGE_PRESENT is clear, we use these: */
  42. #define _PAGE_FILE _PAGE_DIRTY /* nonlinear file mapping, saved PTE; unset:swap */
  43. #define _PAGE_PROTNONE _PAGE_PSE /* if the user mapped it with PROT_NONE;
  44. pte_present gives true */
  45. #define _PAGE_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)
  46. #define _KERNPG_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  47. #define _PAGE_CHG_MASK (PTE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
  48. #define PAGE_NONE __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
  49. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
  50. #define PAGE_SHARED_EXEC __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED)
  51. #define PAGE_COPY_NOEXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
  52. #define PAGE_COPY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
  53. #define PAGE_COPY PAGE_COPY_NOEXEC
  54. #define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
  55. #define PAGE_READONLY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
  56. #ifdef CONFIG_X86_32
  57. #define _PAGE_KERNEL_EXEC \
  58. (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
  59. #define _PAGE_KERNEL (_PAGE_KERNEL_EXEC | _PAGE_NX)
  60. #ifndef __ASSEMBLY__
  61. extern pteval_t __PAGE_KERNEL, __PAGE_KERNEL_EXEC;
  62. #endif /* __ASSEMBLY__ */
  63. #else
  64. #define __PAGE_KERNEL_EXEC \
  65. (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
  66. #define __PAGE_KERNEL (__PAGE_KERNEL_EXEC | _PAGE_NX)
  67. #endif
  68. #define __PAGE_KERNEL_RO (__PAGE_KERNEL & ~_PAGE_RW)
  69. #define __PAGE_KERNEL_RX (__PAGE_KERNEL_EXEC & ~_PAGE_RW)
  70. #define __PAGE_KERNEL_EXEC_NOCACHE (__PAGE_KERNEL_EXEC | _PAGE_PCD | _PAGE_PWT)
  71. #define __PAGE_KERNEL_NOCACHE (__PAGE_KERNEL | _PAGE_PCD | _PAGE_PWT)
  72. #define __PAGE_KERNEL_VSYSCALL (__PAGE_KERNEL_RX | _PAGE_USER)
  73. #define __PAGE_KERNEL_VSYSCALL_NOCACHE (__PAGE_KERNEL_VSYSCALL | _PAGE_PCD | _PAGE_PWT)
  74. #define __PAGE_KERNEL_LARGE (__PAGE_KERNEL | _PAGE_PSE)
  75. #define __PAGE_KERNEL_LARGE_EXEC (__PAGE_KERNEL_EXEC | _PAGE_PSE)
  76. #ifdef CONFIG_X86_32
  77. # define MAKE_GLOBAL(x) __pgprot((x))
  78. #else
  79. # define MAKE_GLOBAL(x) __pgprot((x) | _PAGE_GLOBAL)
  80. #endif
  81. #define PAGE_KERNEL MAKE_GLOBAL(__PAGE_KERNEL)
  82. #define PAGE_KERNEL_RO MAKE_GLOBAL(__PAGE_KERNEL_RO)
  83. #define PAGE_KERNEL_EXEC MAKE_GLOBAL(__PAGE_KERNEL_EXEC)
  84. #define PAGE_KERNEL_RX MAKE_GLOBAL(__PAGE_KERNEL_RX)
  85. #define PAGE_KERNEL_NOCACHE MAKE_GLOBAL(__PAGE_KERNEL_NOCACHE)
  86. #define PAGE_KERNEL_EXEC_NOCACHE MAKE_GLOBAL(__PAGE_KERNEL_EXEC_NOCACHE)
  87. #define PAGE_KERNEL_LARGE MAKE_GLOBAL(__PAGE_KERNEL_LARGE)
  88. #define PAGE_KERNEL_LARGE_EXEC MAKE_GLOBAL(__PAGE_KERNEL_LARGE_EXEC)
  89. #define PAGE_KERNEL_VSYSCALL MAKE_GLOBAL(__PAGE_KERNEL_VSYSCALL)
  90. #define PAGE_KERNEL_VSYSCALL_NOCACHE MAKE_GLOBAL(__PAGE_KERNEL_VSYSCALL_NOCACHE)
  91. /* xwr */
  92. #define __P000 PAGE_NONE
  93. #define __P001 PAGE_READONLY
  94. #define __P010 PAGE_COPY
  95. #define __P011 PAGE_COPY
  96. #define __P100 PAGE_READONLY_EXEC
  97. #define __P101 PAGE_READONLY_EXEC
  98. #define __P110 PAGE_COPY_EXEC
  99. #define __P111 PAGE_COPY_EXEC
  100. #define __S000 PAGE_NONE
  101. #define __S001 PAGE_READONLY
  102. #define __S010 PAGE_SHARED
  103. #define __S011 PAGE_SHARED
  104. #define __S100 PAGE_READONLY_EXEC
  105. #define __S101 PAGE_READONLY_EXEC
  106. #define __S110 PAGE_SHARED_EXEC
  107. #define __S111 PAGE_SHARED_EXEC
  108. #ifndef __ASSEMBLY__
  109. /*
  110. * ZERO_PAGE is a global shared page that is always zero: used
  111. * for zero-mapped memory areas etc..
  112. */
  113. extern unsigned long empty_zero_page[PAGE_SIZE/sizeof(unsigned long)];
  114. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  115. extern spinlock_t pgd_lock;
  116. extern struct list_head pgd_list;
  117. /*
  118. * The following only work if pte_present() is true.
  119. * Undefined behaviour if not..
  120. */
  121. static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
  122. static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
  123. static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_RW; }
  124. static inline int pte_file(pte_t pte) { return pte_val(pte) & _PAGE_FILE; }
  125. static inline int pte_huge(pte_t pte) { return pte_val(pte) & _PAGE_PSE; }
  126. static inline int pte_global(pte_t pte) { return pte_val(pte) & _PAGE_GLOBAL; }
  127. static inline int pte_exec(pte_t pte) { return !(pte_val(pte) & _PAGE_NX); }
  128. static inline int pmd_large(pmd_t pte) {
  129. return (pmd_val(pte) & (_PAGE_PSE|_PAGE_PRESENT)) ==
  130. (_PAGE_PSE|_PAGE_PRESENT);
  131. }
  132. static inline pte_t pte_mkclean(pte_t pte) { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_DIRTY); }
  133. static inline pte_t pte_mkold(pte_t pte) { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_ACCESSED); }
  134. static inline pte_t pte_wrprotect(pte_t pte) { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_RW); }
  135. static inline pte_t pte_mkexec(pte_t pte) { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_NX); }
  136. static inline pte_t pte_mkdirty(pte_t pte) { return __pte(pte_val(pte) | _PAGE_DIRTY); }
  137. static inline pte_t pte_mkyoung(pte_t pte) { return __pte(pte_val(pte) | _PAGE_ACCESSED); }
  138. static inline pte_t pte_mkwrite(pte_t pte) { return __pte(pte_val(pte) | _PAGE_RW); }
  139. static inline pte_t pte_mkhuge(pte_t pte) { return __pte(pte_val(pte) | _PAGE_PSE); }
  140. static inline pte_t pte_clrhuge(pte_t pte) { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_PSE); }
  141. static inline pte_t pte_mkglobal(pte_t pte) { return __pte(pte_val(pte) | _PAGE_GLOBAL); }
  142. static inline pte_t pte_clrglobal(pte_t pte) { return __pte(pte_val(pte) & ~(pteval_t)_PAGE_GLOBAL); }
  143. extern pteval_t __supported_pte_mask;
  144. static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
  145. {
  146. return __pte((((phys_addr_t)page_nr << PAGE_SHIFT) |
  147. pgprot_val(pgprot)) & __supported_pte_mask);
  148. }
  149. static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
  150. {
  151. return __pmd((((phys_addr_t)page_nr << PAGE_SHIFT) |
  152. pgprot_val(pgprot)) & __supported_pte_mask);
  153. }
  154. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  155. {
  156. pteval_t val = pte_val(pte);
  157. /*
  158. * Chop off the NX bit (if present), and add the NX portion of
  159. * the newprot (if present):
  160. */
  161. val &= _PAGE_CHG_MASK & ~_PAGE_NX;
  162. val |= pgprot_val(newprot) & __supported_pte_mask;
  163. return __pte(val);
  164. }
  165. #define pte_pgprot(x) __pgprot(pte_val(x) & (0xfff | _PAGE_NX))
  166. #define canon_pgprot(p) __pgprot(pgprot_val(p) & __supported_pte_mask)
  167. #ifdef CONFIG_PARAVIRT
  168. #include <asm/paravirt.h>
  169. #else /* !CONFIG_PARAVIRT */
  170. #define set_pte(ptep, pte) native_set_pte(ptep, pte)
  171. #define set_pte_at(mm, addr, ptep, pte) native_set_pte_at(mm, addr, ptep, pte)
  172. #define set_pte_present(mm, addr, ptep, pte) \
  173. native_set_pte_present(mm, addr, ptep, pte)
  174. #define set_pte_atomic(ptep, pte) \
  175. native_set_pte_atomic(ptep, pte)
  176. #define set_pmd(pmdp, pmd) native_set_pmd(pmdp, pmd)
  177. #ifndef __PAGETABLE_PUD_FOLDED
  178. #define set_pgd(pgdp, pgd) native_set_pgd(pgdp, pgd)
  179. #define pgd_clear(pgd) native_pgd_clear(pgd)
  180. #endif
  181. #ifndef set_pud
  182. # define set_pud(pudp, pud) native_set_pud(pudp, pud)
  183. #endif
  184. #ifndef __PAGETABLE_PMD_FOLDED
  185. #define pud_clear(pud) native_pud_clear(pud)
  186. #endif
  187. #define pte_clear(mm, addr, ptep) native_pte_clear(mm, addr, ptep)
  188. #define pmd_clear(pmd) native_pmd_clear(pmd)
  189. #define pte_update(mm, addr, ptep) do { } while (0)
  190. #define pte_update_defer(mm, addr, ptep) do { } while (0)
  191. #endif /* CONFIG_PARAVIRT */
  192. #endif /* __ASSEMBLY__ */
  193. #ifdef CONFIG_X86_32
  194. # include "pgtable_32.h"
  195. #else
  196. # include "pgtable_64.h"
  197. #endif
  198. #ifndef __ASSEMBLY__
  199. enum {
  200. PG_LEVEL_NONE,
  201. PG_LEVEL_4K,
  202. PG_LEVEL_2M,
  203. PG_LEVEL_1G,
  204. };
  205. /*
  206. * Helper function that returns the kernel pagetable entry controlling
  207. * the virtual address 'address'. NULL means no pagetable entry present.
  208. * NOTE: the return type is pte_t but if the pmd is PSE then we return it
  209. * as a pte too.
  210. */
  211. extern pte_t *lookup_address(unsigned long address, int *level);
  212. /* local pte updates need not use xchg for locking */
  213. static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
  214. {
  215. pte_t res = *ptep;
  216. /* Pure native function needs no input for mm, addr */
  217. native_pte_clear(NULL, 0, ptep);
  218. return res;
  219. }
  220. static inline void native_set_pte_at(struct mm_struct *mm, unsigned long addr,
  221. pte_t *ptep , pte_t pte)
  222. {
  223. native_set_pte(ptep, pte);
  224. }
  225. #ifndef CONFIG_PARAVIRT
  226. /*
  227. * Rules for using pte_update - it must be called after any PTE update which
  228. * has not been done using the set_pte / clear_pte interfaces. It is used by
  229. * shadow mode hypervisors to resynchronize the shadow page tables. Kernel PTE
  230. * updates should either be sets, clears, or set_pte_atomic for P->P
  231. * transitions, which means this hook should only be called for user PTEs.
  232. * This hook implies a P->P protection or access change has taken place, which
  233. * requires a subsequent TLB flush. The notification can optionally be delayed
  234. * until the TLB flush event by using the pte_update_defer form of the
  235. * interface, but care must be taken to assure that the flush happens while
  236. * still holding the same page table lock so that the shadow and primary pages
  237. * do not become out of sync on SMP.
  238. */
  239. #define pte_update(mm, addr, ptep) do { } while (0)
  240. #define pte_update_defer(mm, addr, ptep) do { } while (0)
  241. #endif
  242. /*
  243. * We only update the dirty/accessed state if we set
  244. * the dirty bit by hand in the kernel, since the hardware
  245. * will do the accessed bit for us, and we don't want to
  246. * race with other CPU's that might be updating the dirty
  247. * bit at the same time.
  248. */
  249. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  250. #define ptep_set_access_flags(vma, address, ptep, entry, dirty) \
  251. ({ \
  252. int __changed = !pte_same(*(ptep), entry); \
  253. if (__changed && dirty) { \
  254. *ptep = entry; \
  255. pte_update_defer((vma)->vm_mm, (address), (ptep)); \
  256. flush_tlb_page(vma, address); \
  257. } \
  258. __changed; \
  259. })
  260. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  261. #define ptep_test_and_clear_young(vma, addr, ptep) ({ \
  262. int __ret = 0; \
  263. if (pte_young(*(ptep))) \
  264. __ret = test_and_clear_bit(_PAGE_BIT_ACCESSED, \
  265. &(ptep)->pte); \
  266. if (__ret) \
  267. pte_update((vma)->vm_mm, addr, ptep); \
  268. __ret; \
  269. })
  270. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  271. #define ptep_clear_flush_young(vma, address, ptep) \
  272. ({ \
  273. int __young; \
  274. __young = ptep_test_and_clear_young((vma), (address), (ptep)); \
  275. if (__young) \
  276. flush_tlb_page(vma, address); \
  277. __young; \
  278. })
  279. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  280. static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  281. {
  282. pte_t pte = native_ptep_get_and_clear(ptep);
  283. pte_update(mm, addr, ptep);
  284. return pte;
  285. }
  286. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  287. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, unsigned long addr, pte_t *ptep, int full)
  288. {
  289. pte_t pte;
  290. if (full) {
  291. /*
  292. * Full address destruction in progress; paravirt does not
  293. * care about updates and native needs no locking
  294. */
  295. pte = native_local_ptep_get_and_clear(ptep);
  296. } else {
  297. pte = ptep_get_and_clear(mm, addr, ptep);
  298. }
  299. return pte;
  300. }
  301. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  302. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  303. {
  304. clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
  305. pte_update(mm, addr, ptep);
  306. }
  307. #include <asm-generic/pgtable.h>
  308. #endif /* __ASSEMBLY__ */
  309. #endif /* _ASM_X86_PGTABLE_H */