pgtable.h 14 KB

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