pgtable.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. #ifndef _ASM_X86_PGTABLE_H
  2. #define _ASM_X86_PGTABLE_H
  3. #include <asm/page.h>
  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_PSE 7 /* 4 MB (or 2MB) page */
  13. #define _PAGE_BIT_PAT 7 /* on 4KB pages */
  14. #define _PAGE_BIT_GLOBAL 8 /* Global TLB entry PPro+ */
  15. #define _PAGE_BIT_UNUSED1 9 /* available for programmer */
  16. #define _PAGE_BIT_IOMAP 10 /* flag used to indicate IO mapping */
  17. #define _PAGE_BIT_UNUSED3 11
  18. #define _PAGE_BIT_PAT_LARGE 12 /* On 2MB or 1GB pages */
  19. #define _PAGE_BIT_SPECIAL _PAGE_BIT_UNUSED1
  20. #define _PAGE_BIT_CPA_TEST _PAGE_BIT_UNUSED1
  21. #define _PAGE_BIT_NX 63 /* No execute: only valid after cpuid check */
  22. /* If _PAGE_BIT_PRESENT is clear, we use these: */
  23. /* - if the user mapped it with PROT_NONE; pte_present gives true */
  24. #define _PAGE_BIT_PROTNONE _PAGE_BIT_GLOBAL
  25. /* - set: nonlinear file mapping, saved PTE; unset:swap */
  26. #define _PAGE_BIT_FILE _PAGE_BIT_DIRTY
  27. #define _PAGE_PRESENT (_AT(pteval_t, 1) << _PAGE_BIT_PRESENT)
  28. #define _PAGE_RW (_AT(pteval_t, 1) << _PAGE_BIT_RW)
  29. #define _PAGE_USER (_AT(pteval_t, 1) << _PAGE_BIT_USER)
  30. #define _PAGE_PWT (_AT(pteval_t, 1) << _PAGE_BIT_PWT)
  31. #define _PAGE_PCD (_AT(pteval_t, 1) << _PAGE_BIT_PCD)
  32. #define _PAGE_ACCESSED (_AT(pteval_t, 1) << _PAGE_BIT_ACCESSED)
  33. #define _PAGE_DIRTY (_AT(pteval_t, 1) << _PAGE_BIT_DIRTY)
  34. #define _PAGE_PSE (_AT(pteval_t, 1) << _PAGE_BIT_PSE)
  35. #define _PAGE_GLOBAL (_AT(pteval_t, 1) << _PAGE_BIT_GLOBAL)
  36. #define _PAGE_UNUSED1 (_AT(pteval_t, 1) << _PAGE_BIT_UNUSED1)
  37. #define _PAGE_IOMAP (_AT(pteval_t, 1) << _PAGE_BIT_IOMAP)
  38. #define _PAGE_UNUSED3 (_AT(pteval_t, 1) << _PAGE_BIT_UNUSED3)
  39. #define _PAGE_PAT (_AT(pteval_t, 1) << _PAGE_BIT_PAT)
  40. #define _PAGE_PAT_LARGE (_AT(pteval_t, 1) << _PAGE_BIT_PAT_LARGE)
  41. #define _PAGE_SPECIAL (_AT(pteval_t, 1) << _PAGE_BIT_SPECIAL)
  42. #define _PAGE_CPA_TEST (_AT(pteval_t, 1) << _PAGE_BIT_CPA_TEST)
  43. #define __HAVE_ARCH_PTE_SPECIAL
  44. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  45. #define _PAGE_NX (_AT(pteval_t, 1) << _PAGE_BIT_NX)
  46. #else
  47. #define _PAGE_NX (_AT(pteval_t, 0))
  48. #endif
  49. #define _PAGE_FILE (_AT(pteval_t, 1) << _PAGE_BIT_FILE)
  50. #define _PAGE_PROTNONE (_AT(pteval_t, 1) << _PAGE_BIT_PROTNONE)
  51. #define _PAGE_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | \
  52. _PAGE_ACCESSED | _PAGE_DIRTY)
  53. #define _KERNPG_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | \
  54. _PAGE_DIRTY)
  55. /* Set of bits not changed in pte_modify */
  56. #define _PAGE_CHG_MASK (PTE_PFN_MASK | _PAGE_PCD | _PAGE_PWT | \
  57. _PAGE_SPECIAL | _PAGE_ACCESSED | _PAGE_DIRTY)
  58. #define _PAGE_CACHE_MASK (_PAGE_PCD | _PAGE_PWT)
  59. #define _PAGE_CACHE_WB (0)
  60. #define _PAGE_CACHE_WC (_PAGE_PWT)
  61. #define _PAGE_CACHE_UC_MINUS (_PAGE_PCD)
  62. #define _PAGE_CACHE_UC (_PAGE_PCD | _PAGE_PWT)
  63. #define PAGE_NONE __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
  64. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | \
  65. _PAGE_ACCESSED | _PAGE_NX)
  66. #define PAGE_SHARED_EXEC __pgprot(_PAGE_PRESENT | _PAGE_RW | \
  67. _PAGE_USER | _PAGE_ACCESSED)
  68. #define PAGE_COPY_NOEXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  69. _PAGE_ACCESSED | _PAGE_NX)
  70. #define PAGE_COPY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  71. _PAGE_ACCESSED)
  72. #define PAGE_COPY PAGE_COPY_NOEXEC
  73. #define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  74. _PAGE_ACCESSED | _PAGE_NX)
  75. #define PAGE_READONLY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | \
  76. _PAGE_ACCESSED)
  77. #define __PAGE_KERNEL_EXEC \
  78. (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_GLOBAL)
  79. #define __PAGE_KERNEL (__PAGE_KERNEL_EXEC | _PAGE_NX)
  80. #define __PAGE_KERNEL_RO (__PAGE_KERNEL & ~_PAGE_RW)
  81. #define __PAGE_KERNEL_RX (__PAGE_KERNEL_EXEC & ~_PAGE_RW)
  82. #define __PAGE_KERNEL_EXEC_NOCACHE (__PAGE_KERNEL_EXEC | _PAGE_PCD | _PAGE_PWT)
  83. #define __PAGE_KERNEL_WC (__PAGE_KERNEL | _PAGE_CACHE_WC)
  84. #define __PAGE_KERNEL_NOCACHE (__PAGE_KERNEL | _PAGE_PCD | _PAGE_PWT)
  85. #define __PAGE_KERNEL_UC_MINUS (__PAGE_KERNEL | _PAGE_PCD)
  86. #define __PAGE_KERNEL_VSYSCALL (__PAGE_KERNEL_RX | _PAGE_USER)
  87. #define __PAGE_KERNEL_VSYSCALL_NOCACHE (__PAGE_KERNEL_VSYSCALL | _PAGE_PCD | _PAGE_PWT)
  88. #define __PAGE_KERNEL_LARGE (__PAGE_KERNEL | _PAGE_PSE)
  89. #define __PAGE_KERNEL_LARGE_NOCACHE (__PAGE_KERNEL | _PAGE_CACHE_UC | _PAGE_PSE)
  90. #define __PAGE_KERNEL_LARGE_EXEC (__PAGE_KERNEL_EXEC | _PAGE_PSE)
  91. #define __PAGE_KERNEL_IO (__PAGE_KERNEL | _PAGE_IOMAP)
  92. #define __PAGE_KERNEL_IO_NOCACHE (__PAGE_KERNEL_NOCACHE | _PAGE_IOMAP)
  93. #define __PAGE_KERNEL_IO_UC_MINUS (__PAGE_KERNEL_UC_MINUS | _PAGE_IOMAP)
  94. #define __PAGE_KERNEL_IO_WC (__PAGE_KERNEL_WC | _PAGE_IOMAP)
  95. #define PAGE_KERNEL __pgprot(__PAGE_KERNEL)
  96. #define PAGE_KERNEL_RO __pgprot(__PAGE_KERNEL_RO)
  97. #define PAGE_KERNEL_EXEC __pgprot(__PAGE_KERNEL_EXEC)
  98. #define PAGE_KERNEL_RX __pgprot(__PAGE_KERNEL_RX)
  99. #define PAGE_KERNEL_WC __pgprot(__PAGE_KERNEL_WC)
  100. #define PAGE_KERNEL_NOCACHE __pgprot(__PAGE_KERNEL_NOCACHE)
  101. #define PAGE_KERNEL_UC_MINUS __pgprot(__PAGE_KERNEL_UC_MINUS)
  102. #define PAGE_KERNEL_EXEC_NOCACHE __pgprot(__PAGE_KERNEL_EXEC_NOCACHE)
  103. #define PAGE_KERNEL_LARGE __pgprot(__PAGE_KERNEL_LARGE)
  104. #define PAGE_KERNEL_LARGE_NOCACHE __pgprot(__PAGE_KERNEL_LARGE_NOCACHE)
  105. #define PAGE_KERNEL_LARGE_EXEC __pgprot(__PAGE_KERNEL_LARGE_EXEC)
  106. #define PAGE_KERNEL_VSYSCALL __pgprot(__PAGE_KERNEL_VSYSCALL)
  107. #define PAGE_KERNEL_VSYSCALL_NOCACHE __pgprot(__PAGE_KERNEL_VSYSCALL_NOCACHE)
  108. #define PAGE_KERNEL_IO __pgprot(__PAGE_KERNEL_IO)
  109. #define PAGE_KERNEL_IO_NOCACHE __pgprot(__PAGE_KERNEL_IO_NOCACHE)
  110. #define PAGE_KERNEL_IO_UC_MINUS __pgprot(__PAGE_KERNEL_IO_UC_MINUS)
  111. #define PAGE_KERNEL_IO_WC __pgprot(__PAGE_KERNEL_IO_WC)
  112. /* xwr */
  113. #define __P000 PAGE_NONE
  114. #define __P001 PAGE_READONLY
  115. #define __P010 PAGE_COPY
  116. #define __P011 PAGE_COPY
  117. #define __P100 PAGE_READONLY_EXEC
  118. #define __P101 PAGE_READONLY_EXEC
  119. #define __P110 PAGE_COPY_EXEC
  120. #define __P111 PAGE_COPY_EXEC
  121. #define __S000 PAGE_NONE
  122. #define __S001 PAGE_READONLY
  123. #define __S010 PAGE_SHARED
  124. #define __S011 PAGE_SHARED
  125. #define __S100 PAGE_READONLY_EXEC
  126. #define __S101 PAGE_READONLY_EXEC
  127. #define __S110 PAGE_SHARED_EXEC
  128. #define __S111 PAGE_SHARED_EXEC
  129. /*
  130. * early identity mapping pte attrib macros.
  131. */
  132. #ifdef CONFIG_X86_64
  133. #define __PAGE_KERNEL_IDENT_LARGE_EXEC __PAGE_KERNEL_LARGE_EXEC
  134. #else
  135. /*
  136. * For PDE_IDENT_ATTR include USER bit. As the PDE and PTE protection
  137. * bits are combined, this will alow user to access the high address mapped
  138. * VDSO in the presence of CONFIG_COMPAT_VDSO
  139. */
  140. #define PTE_IDENT_ATTR 0x003 /* PRESENT+RW */
  141. #define PDE_IDENT_ATTR 0x067 /* PRESENT+RW+USER+DIRTY+ACCESSED */
  142. #define PGD_IDENT_ATTR 0x001 /* PRESENT (no other attributes) */
  143. #endif
  144. /*
  145. * Macro to mark a page protection value as UC-
  146. */
  147. #define pgprot_noncached(prot) \
  148. ((boot_cpu_data.x86 > 3) \
  149. ? (__pgprot(pgprot_val(prot) | _PAGE_CACHE_UC_MINUS)) \
  150. : (prot))
  151. #ifndef __ASSEMBLY__
  152. #define pgprot_writecombine pgprot_writecombine
  153. extern pgprot_t pgprot_writecombine(pgprot_t prot);
  154. /*
  155. * ZERO_PAGE is a global shared page that is always zero: used
  156. * for zero-mapped memory areas etc..
  157. */
  158. extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
  159. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  160. extern spinlock_t pgd_lock;
  161. extern struct list_head pgd_list;
  162. /*
  163. * The following only work if pte_present() is true.
  164. * Undefined behaviour if not..
  165. */
  166. static inline int pte_dirty(pte_t pte)
  167. {
  168. return pte_flags(pte) & _PAGE_DIRTY;
  169. }
  170. static inline int pte_young(pte_t pte)
  171. {
  172. return pte_flags(pte) & _PAGE_ACCESSED;
  173. }
  174. static inline int pte_write(pte_t pte)
  175. {
  176. return pte_flags(pte) & _PAGE_RW;
  177. }
  178. static inline int pte_file(pte_t pte)
  179. {
  180. return pte_flags(pte) & _PAGE_FILE;
  181. }
  182. static inline int pte_huge(pte_t pte)
  183. {
  184. return pte_flags(pte) & _PAGE_PSE;
  185. }
  186. static inline int pte_global(pte_t pte)
  187. {
  188. return pte_flags(pte) & _PAGE_GLOBAL;
  189. }
  190. static inline int pte_exec(pte_t pte)
  191. {
  192. return !(pte_flags(pte) & _PAGE_NX);
  193. }
  194. static inline int pte_special(pte_t pte)
  195. {
  196. return pte_flags(pte) & _PAGE_SPECIAL;
  197. }
  198. static inline unsigned long pte_pfn(pte_t pte)
  199. {
  200. return (pte_val(pte) & PTE_PFN_MASK) >> PAGE_SHIFT;
  201. }
  202. #define pte_page(pte) pfn_to_page(pte_pfn(pte))
  203. static inline int pmd_large(pmd_t pte)
  204. {
  205. return (pmd_flags(pte) & (_PAGE_PSE | _PAGE_PRESENT)) ==
  206. (_PAGE_PSE | _PAGE_PRESENT);
  207. }
  208. static inline pte_t pte_mkclean(pte_t pte)
  209. {
  210. return __pte(pte_val(pte) & ~_PAGE_DIRTY);
  211. }
  212. static inline pte_t pte_mkold(pte_t pte)
  213. {
  214. return __pte(pte_val(pte) & ~_PAGE_ACCESSED);
  215. }
  216. static inline pte_t pte_wrprotect(pte_t pte)
  217. {
  218. return __pte(pte_val(pte) & ~_PAGE_RW);
  219. }
  220. static inline pte_t pte_mkexec(pte_t pte)
  221. {
  222. return __pte(pte_val(pte) & ~_PAGE_NX);
  223. }
  224. static inline pte_t pte_mkdirty(pte_t pte)
  225. {
  226. return __pte(pte_val(pte) | _PAGE_DIRTY);
  227. }
  228. static inline pte_t pte_mkyoung(pte_t pte)
  229. {
  230. return __pte(pte_val(pte) | _PAGE_ACCESSED);
  231. }
  232. static inline pte_t pte_mkwrite(pte_t pte)
  233. {
  234. return __pte(pte_val(pte) | _PAGE_RW);
  235. }
  236. static inline pte_t pte_mkhuge(pte_t pte)
  237. {
  238. return __pte(pte_val(pte) | _PAGE_PSE);
  239. }
  240. static inline pte_t pte_clrhuge(pte_t pte)
  241. {
  242. return __pte(pte_val(pte) & ~_PAGE_PSE);
  243. }
  244. static inline pte_t pte_mkglobal(pte_t pte)
  245. {
  246. return __pte(pte_val(pte) | _PAGE_GLOBAL);
  247. }
  248. static inline pte_t pte_clrglobal(pte_t pte)
  249. {
  250. return __pte(pte_val(pte) & ~_PAGE_GLOBAL);
  251. }
  252. static inline pte_t pte_mkspecial(pte_t pte)
  253. {
  254. return __pte(pte_val(pte) | _PAGE_SPECIAL);
  255. }
  256. extern pteval_t __supported_pte_mask;
  257. static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
  258. {
  259. return __pte((((phys_addr_t)page_nr << PAGE_SHIFT) |
  260. pgprot_val(pgprot)) & __supported_pte_mask);
  261. }
  262. static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
  263. {
  264. return __pmd((((phys_addr_t)page_nr << PAGE_SHIFT) |
  265. pgprot_val(pgprot)) & __supported_pte_mask);
  266. }
  267. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  268. {
  269. pteval_t val = pte_val(pte);
  270. /*
  271. * Chop off the NX bit (if present), and add the NX portion of
  272. * the newprot (if present):
  273. */
  274. val &= _PAGE_CHG_MASK;
  275. val |= pgprot_val(newprot) & (~_PAGE_CHG_MASK) & __supported_pte_mask;
  276. return __pte(val);
  277. }
  278. /* mprotect needs to preserve PAT bits when updating vm_page_prot */
  279. #define pgprot_modify pgprot_modify
  280. static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
  281. {
  282. pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK;
  283. pgprotval_t addbits = pgprot_val(newprot);
  284. return __pgprot(preservebits | addbits);
  285. }
  286. #define pte_pgprot(x) __pgprot(pte_flags(x) & PTE_FLAGS_MASK)
  287. #define canon_pgprot(p) __pgprot(pgprot_val(p) & __supported_pte_mask)
  288. static inline int is_new_memtype_allowed(unsigned long flags,
  289. unsigned long new_flags)
  290. {
  291. /*
  292. * Certain new memtypes are not allowed with certain
  293. * requested memtype:
  294. * - request is uncached, return cannot be write-back
  295. * - request is write-combine, return cannot be write-back
  296. */
  297. if ((flags == _PAGE_CACHE_UC_MINUS &&
  298. new_flags == _PAGE_CACHE_WB) ||
  299. (flags == _PAGE_CACHE_WC &&
  300. new_flags == _PAGE_CACHE_WB)) {
  301. return 0;
  302. }
  303. return 1;
  304. }
  305. #ifndef __ASSEMBLY__
  306. /* Indicate that x86 has its own track and untrack pfn vma functions */
  307. #define __HAVE_PFNMAP_TRACKING
  308. #define __HAVE_PHYS_MEM_ACCESS_PROT
  309. struct file;
  310. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  311. unsigned long size, pgprot_t vma_prot);
  312. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  313. unsigned long size, pgprot_t *vma_prot);
  314. #endif
  315. /* Install a pte for a particular vaddr in kernel space. */
  316. void set_pte_vaddr(unsigned long vaddr, pte_t pte);
  317. #ifdef CONFIG_X86_32
  318. extern void native_pagetable_setup_start(pgd_t *base);
  319. extern void native_pagetable_setup_done(pgd_t *base);
  320. #else
  321. static inline void native_pagetable_setup_start(pgd_t *base) {}
  322. static inline void native_pagetable_setup_done(pgd_t *base) {}
  323. #endif
  324. struct seq_file;
  325. extern void arch_report_meminfo(struct seq_file *m);
  326. #ifdef CONFIG_PARAVIRT
  327. #include <asm/paravirt.h>
  328. #else /* !CONFIG_PARAVIRT */
  329. #define set_pte(ptep, pte) native_set_pte(ptep, pte)
  330. #define set_pte_at(mm, addr, ptep, pte) native_set_pte_at(mm, addr, ptep, pte)
  331. #define set_pte_present(mm, addr, ptep, pte) \
  332. native_set_pte_present(mm, addr, ptep, pte)
  333. #define set_pte_atomic(ptep, pte) \
  334. native_set_pte_atomic(ptep, pte)
  335. #define set_pmd(pmdp, pmd) native_set_pmd(pmdp, pmd)
  336. #ifndef __PAGETABLE_PUD_FOLDED
  337. #define set_pgd(pgdp, pgd) native_set_pgd(pgdp, pgd)
  338. #define pgd_clear(pgd) native_pgd_clear(pgd)
  339. #endif
  340. #ifndef set_pud
  341. # define set_pud(pudp, pud) native_set_pud(pudp, pud)
  342. #endif
  343. #ifndef __PAGETABLE_PMD_FOLDED
  344. #define pud_clear(pud) native_pud_clear(pud)
  345. #endif
  346. #define pte_clear(mm, addr, ptep) native_pte_clear(mm, addr, ptep)
  347. #define pmd_clear(pmd) native_pmd_clear(pmd)
  348. #define pte_update(mm, addr, ptep) do { } while (0)
  349. #define pte_update_defer(mm, addr, ptep) do { } while (0)
  350. static inline void __init paravirt_pagetable_setup_start(pgd_t *base)
  351. {
  352. native_pagetable_setup_start(base);
  353. }
  354. static inline void __init paravirt_pagetable_setup_done(pgd_t *base)
  355. {
  356. native_pagetable_setup_done(base);
  357. }
  358. #endif /* CONFIG_PARAVIRT */
  359. #endif /* __ASSEMBLY__ */
  360. #ifdef CONFIG_X86_32
  361. # include "pgtable_32.h"
  362. #else
  363. # include "pgtable_64.h"
  364. #endif
  365. #ifndef __ASSEMBLY__
  366. #include <linux/mm_types.h>
  367. static inline int pte_none(pte_t pte)
  368. {
  369. return !pte.pte;
  370. }
  371. #define __HAVE_ARCH_PTE_SAME
  372. static inline int pte_same(pte_t a, pte_t b)
  373. {
  374. return a.pte == b.pte;
  375. }
  376. static inline int pte_present(pte_t a)
  377. {
  378. return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE);
  379. }
  380. static inline int pmd_present(pmd_t pmd)
  381. {
  382. return pmd_flags(pmd) & _PAGE_PRESENT;
  383. }
  384. static inline int pmd_none(pmd_t pmd)
  385. {
  386. /* Only check low word on 32-bit platforms, since it might be
  387. out of sync with upper half. */
  388. return (unsigned long)native_pmd_val(pmd) == 0;
  389. }
  390. static inline unsigned long pmd_page_vaddr(pmd_t pmd)
  391. {
  392. return (unsigned long)__va(pmd_val(pmd) & PTE_PFN_MASK);
  393. }
  394. /*
  395. * Currently stuck as a macro due to indirect forward reference to
  396. * linux/mmzone.h's __section_mem_map_addr() definition:
  397. */
  398. #define pmd_page(pmd) pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT)
  399. /*
  400. * the pmd page can be thought of an array like this: pmd_t[PTRS_PER_PMD]
  401. *
  402. * this macro returns the index of the entry in the pmd page which would
  403. * control the given virtual address
  404. */
  405. static inline unsigned pmd_index(unsigned long address)
  406. {
  407. return (address >> PMD_SHIFT) & (PTRS_PER_PMD - 1);
  408. }
  409. /*
  410. * Conversion functions: convert a page and protection to a page entry,
  411. * and a page entry and page directory to the page they refer to.
  412. *
  413. * (Currently stuck as a macro because of indirect forward reference
  414. * to linux/mm.h:page_to_nid())
  415. */
  416. #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
  417. /*
  418. * the pte page can be thought of an array like this: pte_t[PTRS_PER_PTE]
  419. *
  420. * this function returns the index of the entry in the pte page which would
  421. * control the given virtual address
  422. */
  423. static inline unsigned pte_index(unsigned long address)
  424. {
  425. return (address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  426. }
  427. static inline pte_t *pte_offset_kernel(pmd_t *pmd, unsigned long address)
  428. {
  429. return (pte_t *)pmd_page_vaddr(*pmd) + pte_index(address);
  430. }
  431. static inline int pmd_bad(pmd_t pmd)
  432. {
  433. return (pmd_flags(pmd) & ~_PAGE_USER) != _KERNPG_TABLE;
  434. }
  435. static inline unsigned long pages_to_mb(unsigned long npg)
  436. {
  437. return npg >> (20 - PAGE_SHIFT);
  438. }
  439. #define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \
  440. remap_pfn_range(vma, vaddr, pfn, size, prot)
  441. #if PAGETABLE_LEVELS == 2
  442. static inline int pud_large(pud_t pud)
  443. {
  444. return 0;
  445. }
  446. #endif
  447. #if PAGETABLE_LEVELS > 2
  448. static inline int pud_none(pud_t pud)
  449. {
  450. return native_pud_val(pud) == 0;
  451. }
  452. static inline int pud_present(pud_t pud)
  453. {
  454. return pud_flags(pud) & _PAGE_PRESENT;
  455. }
  456. static inline unsigned long pud_page_vaddr(pud_t pud)
  457. {
  458. return (unsigned long)__va((unsigned long)pud_val(pud) & PTE_PFN_MASK);
  459. }
  460. /*
  461. * Currently stuck as a macro due to indirect forward reference to
  462. * linux/mmzone.h's __section_mem_map_addr() definition:
  463. */
  464. #define pud_page(pud) pfn_to_page(pud_val(pud) >> PAGE_SHIFT)
  465. /* Find an entry in the second-level page table.. */
  466. static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
  467. {
  468. return (pmd_t *)pud_page_vaddr(*pud) + pmd_index(address);
  469. }
  470. static inline unsigned long pmd_pfn(pmd_t pmd)
  471. {
  472. return (pmd_val(pmd) & PTE_PFN_MASK) >> PAGE_SHIFT;
  473. }
  474. static inline int pud_large(pud_t pud)
  475. {
  476. return (pud_flags(pud) & (_PAGE_PSE | _PAGE_PRESENT)) ==
  477. (_PAGE_PSE | _PAGE_PRESENT);
  478. }
  479. static inline int pud_bad(pud_t pud)
  480. {
  481. return (pud_flags(pud) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0;
  482. }
  483. #endif /* PAGETABLE_LEVELS > 2 */
  484. #if PAGETABLE_LEVELS > 3
  485. static inline int pgd_present(pgd_t pgd)
  486. {
  487. return pgd_flags(pgd) & _PAGE_PRESENT;
  488. }
  489. static inline unsigned long pgd_page_vaddr(pgd_t pgd)
  490. {
  491. return (unsigned long)__va((unsigned long)pgd_val(pgd) & PTE_PFN_MASK);
  492. }
  493. /*
  494. * Currently stuck as a macro due to indirect forward reference to
  495. * linux/mmzone.h's __section_mem_map_addr() definition:
  496. */
  497. #define pgd_page(pgd) pfn_to_page(pgd_val(pgd) >> PAGE_SHIFT)
  498. /* to find an entry in a page-table-directory. */
  499. static inline unsigned pud_index(unsigned long address)
  500. {
  501. return (address >> PUD_SHIFT) & (PTRS_PER_PUD - 1);
  502. }
  503. static inline pud_t *pud_offset(pgd_t *pgd, unsigned long address)
  504. {
  505. return (pud_t *)pgd_page_vaddr(*pgd) + pud_index(address);
  506. }
  507. static inline int pgd_bad(pgd_t pgd)
  508. {
  509. return (pgd_flags(pgd) & ~_PAGE_USER) != _KERNPG_TABLE;
  510. }
  511. static inline int pgd_none(pgd_t pgd)
  512. {
  513. return !native_pgd_val(pgd);
  514. }
  515. #endif /* PAGETABLE_LEVELS > 3 */
  516. #endif /* __ASSEMBLY__ */
  517. /*
  518. * the pgd page can be thought of an array like this: pgd_t[PTRS_PER_PGD]
  519. *
  520. * this macro returns the index of the entry in the pgd page which would
  521. * control the given virtual address
  522. */
  523. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
  524. /*
  525. * pgd_offset() returns a (pgd_t *)
  526. * pgd_index() is used get the offset into the pgd page's array of pgd_t's;
  527. */
  528. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index((address)))
  529. /*
  530. * a shortcut which implies the use of the kernel's pgd, instead
  531. * of a process's
  532. */
  533. #define pgd_offset_k(address) pgd_offset(&init_mm, (address))
  534. #define KERNEL_PGD_BOUNDARY pgd_index(PAGE_OFFSET)
  535. #define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_BOUNDARY)
  536. #ifndef __ASSEMBLY__
  537. enum {
  538. PG_LEVEL_NONE,
  539. PG_LEVEL_4K,
  540. PG_LEVEL_2M,
  541. PG_LEVEL_1G,
  542. PG_LEVEL_NUM
  543. };
  544. #ifdef CONFIG_PROC_FS
  545. extern void update_page_count(int level, unsigned long pages);
  546. #else
  547. static inline void update_page_count(int level, unsigned long pages) { }
  548. #endif
  549. /*
  550. * Helper function that returns the kernel pagetable entry controlling
  551. * the virtual address 'address'. NULL means no pagetable entry present.
  552. * NOTE: the return type is pte_t but if the pmd is PSE then we return it
  553. * as a pte too.
  554. */
  555. extern pte_t *lookup_address(unsigned long address, unsigned int *level);
  556. /* local pte updates need not use xchg for locking */
  557. static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
  558. {
  559. pte_t res = *ptep;
  560. /* Pure native function needs no input for mm, addr */
  561. native_pte_clear(NULL, 0, ptep);
  562. return res;
  563. }
  564. static inline void native_set_pte_at(struct mm_struct *mm, unsigned long addr,
  565. pte_t *ptep , pte_t pte)
  566. {
  567. native_set_pte(ptep, pte);
  568. }
  569. #ifndef CONFIG_PARAVIRT
  570. /*
  571. * Rules for using pte_update - it must be called after any PTE update which
  572. * has not been done using the set_pte / clear_pte interfaces. It is used by
  573. * shadow mode hypervisors to resynchronize the shadow page tables. Kernel PTE
  574. * updates should either be sets, clears, or set_pte_atomic for P->P
  575. * transitions, which means this hook should only be called for user PTEs.
  576. * This hook implies a P->P protection or access change has taken place, which
  577. * requires a subsequent TLB flush. The notification can optionally be delayed
  578. * until the TLB flush event by using the pte_update_defer form of the
  579. * interface, but care must be taken to assure that the flush happens while
  580. * still holding the same page table lock so that the shadow and primary pages
  581. * do not become out of sync on SMP.
  582. */
  583. #define pte_update(mm, addr, ptep) do { } while (0)
  584. #define pte_update_defer(mm, addr, ptep) do { } while (0)
  585. #endif
  586. /*
  587. * We only update the dirty/accessed state if we set
  588. * the dirty bit by hand in the kernel, since the hardware
  589. * will do the accessed bit for us, and we don't want to
  590. * race with other CPU's that might be updating the dirty
  591. * bit at the same time.
  592. */
  593. struct vm_area_struct;
  594. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  595. extern int ptep_set_access_flags(struct vm_area_struct *vma,
  596. unsigned long address, pte_t *ptep,
  597. pte_t entry, int dirty);
  598. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  599. extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
  600. unsigned long addr, pte_t *ptep);
  601. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  602. extern int ptep_clear_flush_young(struct vm_area_struct *vma,
  603. unsigned long address, pte_t *ptep);
  604. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  605. static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  606. pte_t *ptep)
  607. {
  608. pte_t pte = native_ptep_get_and_clear(ptep);
  609. pte_update(mm, addr, ptep);
  610. return pte;
  611. }
  612. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  613. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
  614. unsigned long addr, pte_t *ptep,
  615. int full)
  616. {
  617. pte_t pte;
  618. if (full) {
  619. /*
  620. * Full address destruction in progress; paravirt does not
  621. * care about updates and native needs no locking
  622. */
  623. pte = native_local_ptep_get_and_clear(ptep);
  624. } else {
  625. pte = ptep_get_and_clear(mm, addr, ptep);
  626. }
  627. return pte;
  628. }
  629. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  630. static inline void ptep_set_wrprotect(struct mm_struct *mm,
  631. unsigned long addr, pte_t *ptep)
  632. {
  633. clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
  634. pte_update(mm, addr, ptep);
  635. }
  636. /*
  637. * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
  638. *
  639. * dst - pointer to pgd range anwhere on a pgd page
  640. * src - ""
  641. * count - the number of pgds to copy.
  642. *
  643. * dst and src can be on the same page, but the range must not overlap,
  644. * and must not cross a page boundary.
  645. */
  646. static inline void clone_pgd_range(pgd_t *dst, pgd_t *src, int count)
  647. {
  648. memcpy(dst, src, count * sizeof(pgd_t));
  649. }
  650. #include <asm-generic/pgtable.h>
  651. #endif /* __ASSEMBLY__ */
  652. #endif /* _ASM_X86_PGTABLE_H */