pgtable.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #ifndef _I386_PGTABLE_H
  2. #define _I386_PGTABLE_H
  3. /*
  4. * The Linux memory management assumes a three-level page table setup. On
  5. * the i386, we use that, but "fold" the mid level into the top-level page
  6. * table, so that we physically have the same two-level page table as the
  7. * i386 mmu expects.
  8. *
  9. * This file contains the functions and defines necessary to modify and use
  10. * the i386 page table tree.
  11. */
  12. #ifndef __ASSEMBLY__
  13. #include <asm/processor.h>
  14. #include <asm/fixmap.h>
  15. #include <linux/threads.h>
  16. #ifndef _I386_BITOPS_H
  17. #include <asm/bitops.h>
  18. #endif
  19. #include <linux/slab.h>
  20. #include <linux/list.h>
  21. #include <linux/spinlock.h>
  22. struct mm_struct;
  23. struct vm_area_struct;
  24. /*
  25. * ZERO_PAGE is a global shared page that is always zero: used
  26. * for zero-mapped memory areas etc..
  27. */
  28. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  29. extern unsigned long empty_zero_page[1024];
  30. extern pgd_t swapper_pg_dir[1024];
  31. extern kmem_cache_t *pgd_cache;
  32. extern kmem_cache_t *pmd_cache;
  33. extern spinlock_t pgd_lock;
  34. extern struct page *pgd_list;
  35. void pmd_ctor(void *, kmem_cache_t *, unsigned long);
  36. void pgd_ctor(void *, kmem_cache_t *, unsigned long);
  37. void pgd_dtor(void *, kmem_cache_t *, unsigned long);
  38. void pgtable_cache_init(void);
  39. void paging_init(void);
  40. /*
  41. * The Linux x86 paging architecture is 'compile-time dual-mode', it
  42. * implements both the traditional 2-level x86 page tables and the
  43. * newer 3-level PAE-mode page tables.
  44. */
  45. #ifdef CONFIG_X86_PAE
  46. # include <asm/pgtable-3level-defs.h>
  47. # define PMD_SIZE (1UL << PMD_SHIFT)
  48. # define PMD_MASK (~(PMD_SIZE-1))
  49. #else
  50. # include <asm/pgtable-2level-defs.h>
  51. #endif
  52. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  53. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  54. #define USER_PTRS_PER_PGD (TASK_SIZE/PGDIR_SIZE)
  55. #define FIRST_USER_ADDRESS 0
  56. #define USER_PGD_PTRS (PAGE_OFFSET >> PGDIR_SHIFT)
  57. #define KERNEL_PGD_PTRS (PTRS_PER_PGD-USER_PGD_PTRS)
  58. #define TWOLEVEL_PGDIR_SHIFT 22
  59. #define BOOT_USER_PGD_PTRS (__PAGE_OFFSET >> TWOLEVEL_PGDIR_SHIFT)
  60. #define BOOT_KERNEL_PGD_PTRS (1024-BOOT_USER_PGD_PTRS)
  61. /* Just any arbitrary offset to the start of the vmalloc VM area: the
  62. * current 8MB value just means that there will be a 8MB "hole" after the
  63. * physical memory until the kernel virtual memory starts. That means that
  64. * any out-of-bounds memory accesses will hopefully be caught.
  65. * The vmalloc() routines leaves a hole of 4kB between each vmalloced
  66. * area for the same reason. ;)
  67. */
  68. #define VMALLOC_OFFSET (8*1024*1024)
  69. #define VMALLOC_START (((unsigned long) high_memory + vmalloc_earlyreserve + \
  70. 2*VMALLOC_OFFSET-1) & ~(VMALLOC_OFFSET-1))
  71. #ifdef CONFIG_HIGHMEM
  72. # define VMALLOC_END (PKMAP_BASE-2*PAGE_SIZE)
  73. #else
  74. # define VMALLOC_END (FIXADDR_START-2*PAGE_SIZE)
  75. #endif
  76. /*
  77. * _PAGE_PSE set in the page directory entry just means that
  78. * the page directory entry points directly to a 4MB-aligned block of
  79. * memory.
  80. */
  81. #define _PAGE_BIT_PRESENT 0
  82. #define _PAGE_BIT_RW 1
  83. #define _PAGE_BIT_USER 2
  84. #define _PAGE_BIT_PWT 3
  85. #define _PAGE_BIT_PCD 4
  86. #define _PAGE_BIT_ACCESSED 5
  87. #define _PAGE_BIT_DIRTY 6
  88. #define _PAGE_BIT_PSE 7 /* 4 MB (or 2MB) page, Pentium+, if present.. */
  89. #define _PAGE_BIT_GLOBAL 8 /* Global TLB entry PPro+ */
  90. #define _PAGE_BIT_UNUSED1 9 /* available for programmer */
  91. #define _PAGE_BIT_UNUSED2 10
  92. #define _PAGE_BIT_UNUSED3 11
  93. #define _PAGE_BIT_NX 63
  94. #define _PAGE_PRESENT 0x001
  95. #define _PAGE_RW 0x002
  96. #define _PAGE_USER 0x004
  97. #define _PAGE_PWT 0x008
  98. #define _PAGE_PCD 0x010
  99. #define _PAGE_ACCESSED 0x020
  100. #define _PAGE_DIRTY 0x040
  101. #define _PAGE_PSE 0x080 /* 4 MB (or 2MB) page, Pentium+, if present.. */
  102. #define _PAGE_GLOBAL 0x100 /* Global TLB entry PPro+ */
  103. #define _PAGE_UNUSED1 0x200 /* available for programmer */
  104. #define _PAGE_UNUSED2 0x400
  105. #define _PAGE_UNUSED3 0x800
  106. /* If _PAGE_PRESENT is clear, we use these: */
  107. #define _PAGE_FILE 0x040 /* nonlinear file mapping, saved PTE; unset:swap */
  108. #define _PAGE_PROTNONE 0x080 /* if the user mapped it with PROT_NONE;
  109. pte_present gives true */
  110. #ifdef CONFIG_X86_PAE
  111. #define _PAGE_NX (1ULL<<_PAGE_BIT_NX)
  112. #else
  113. #define _PAGE_NX 0
  114. #endif
  115. #define _PAGE_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)
  116. #define _KERNPG_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
  117. #define _PAGE_CHG_MASK (PTE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
  118. #define PAGE_NONE \
  119. __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
  120. #define PAGE_SHARED \
  121. __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED)
  122. #define PAGE_SHARED_EXEC \
  123. __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED)
  124. #define PAGE_COPY_NOEXEC \
  125. __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
  126. #define PAGE_COPY_EXEC \
  127. __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
  128. #define PAGE_COPY \
  129. PAGE_COPY_NOEXEC
  130. #define PAGE_READONLY \
  131. __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED | _PAGE_NX)
  132. #define PAGE_READONLY_EXEC \
  133. __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
  134. #define _PAGE_KERNEL \
  135. (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_NX)
  136. #define _PAGE_KERNEL_EXEC \
  137. (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
  138. extern unsigned long long __PAGE_KERNEL, __PAGE_KERNEL_EXEC;
  139. #define __PAGE_KERNEL_RO (__PAGE_KERNEL & ~_PAGE_RW)
  140. #define __PAGE_KERNEL_NOCACHE (__PAGE_KERNEL | _PAGE_PCD)
  141. #define __PAGE_KERNEL_LARGE (__PAGE_KERNEL | _PAGE_PSE)
  142. #define __PAGE_KERNEL_LARGE_EXEC (__PAGE_KERNEL_EXEC | _PAGE_PSE)
  143. #define PAGE_KERNEL __pgprot(__PAGE_KERNEL)
  144. #define PAGE_KERNEL_RO __pgprot(__PAGE_KERNEL_RO)
  145. #define PAGE_KERNEL_EXEC __pgprot(__PAGE_KERNEL_EXEC)
  146. #define PAGE_KERNEL_NOCACHE __pgprot(__PAGE_KERNEL_NOCACHE)
  147. #define PAGE_KERNEL_LARGE __pgprot(__PAGE_KERNEL_LARGE)
  148. #define PAGE_KERNEL_LARGE_EXEC __pgprot(__PAGE_KERNEL_LARGE_EXEC)
  149. /*
  150. * The i386 can't do page protection for execute, and considers that
  151. * the same are read. Also, write permissions imply read permissions.
  152. * This is the closest we can get..
  153. */
  154. #define __P000 PAGE_NONE
  155. #define __P001 PAGE_READONLY
  156. #define __P010 PAGE_COPY
  157. #define __P011 PAGE_COPY
  158. #define __P100 PAGE_READONLY_EXEC
  159. #define __P101 PAGE_READONLY_EXEC
  160. #define __P110 PAGE_COPY_EXEC
  161. #define __P111 PAGE_COPY_EXEC
  162. #define __S000 PAGE_NONE
  163. #define __S001 PAGE_READONLY
  164. #define __S010 PAGE_SHARED
  165. #define __S011 PAGE_SHARED
  166. #define __S100 PAGE_READONLY_EXEC
  167. #define __S101 PAGE_READONLY_EXEC
  168. #define __S110 PAGE_SHARED_EXEC
  169. #define __S111 PAGE_SHARED_EXEC
  170. /*
  171. * Define this if things work differently on an i386 and an i486:
  172. * it will (on an i486) warn about kernel memory accesses that are
  173. * done without a 'access_ok(VERIFY_WRITE,..)'
  174. */
  175. #undef TEST_ACCESS_OK
  176. /* The boot page tables (all created as a single array) */
  177. extern unsigned long pg0[];
  178. #define pte_present(x) ((x).pte_low & (_PAGE_PRESENT | _PAGE_PROTNONE))
  179. /* To avoid harmful races, pmd_none(x) should check only the lower when PAE */
  180. #define pmd_none(x) (!(unsigned long)pmd_val(x))
  181. #define pmd_present(x) (pmd_val(x) & _PAGE_PRESENT)
  182. #define pmd_bad(x) ((pmd_val(x) & (~PAGE_MASK & ~_PAGE_USER)) != _KERNPG_TABLE)
  183. #define pages_to_mb(x) ((x) >> (20-PAGE_SHIFT))
  184. /*
  185. * The following only work if pte_present() is true.
  186. * Undefined behaviour if not..
  187. */
  188. static inline int pte_user(pte_t pte) { return (pte).pte_low & _PAGE_USER; }
  189. static inline int pte_read(pte_t pte) { return (pte).pte_low & _PAGE_USER; }
  190. static inline int pte_dirty(pte_t pte) { return (pte).pte_low & _PAGE_DIRTY; }
  191. static inline int pte_young(pte_t pte) { return (pte).pte_low & _PAGE_ACCESSED; }
  192. static inline int pte_write(pte_t pte) { return (pte).pte_low & _PAGE_RW; }
  193. static inline int pte_huge(pte_t pte) { return (pte).pte_low & _PAGE_PSE; }
  194. /*
  195. * The following only works if pte_present() is not true.
  196. */
  197. static inline int pte_file(pte_t pte) { return (pte).pte_low & _PAGE_FILE; }
  198. static inline pte_t pte_rdprotect(pte_t pte) { (pte).pte_low &= ~_PAGE_USER; return pte; }
  199. static inline pte_t pte_exprotect(pte_t pte) { (pte).pte_low &= ~_PAGE_USER; return pte; }
  200. static inline pte_t pte_mkclean(pte_t pte) { (pte).pte_low &= ~_PAGE_DIRTY; return pte; }
  201. static inline pte_t pte_mkold(pte_t pte) { (pte).pte_low &= ~_PAGE_ACCESSED; return pte; }
  202. static inline pte_t pte_wrprotect(pte_t pte) { (pte).pte_low &= ~_PAGE_RW; return pte; }
  203. static inline pte_t pte_mkread(pte_t pte) { (pte).pte_low |= _PAGE_USER; return pte; }
  204. static inline pte_t pte_mkexec(pte_t pte) { (pte).pte_low |= _PAGE_USER; return pte; }
  205. static inline pte_t pte_mkdirty(pte_t pte) { (pte).pte_low |= _PAGE_DIRTY; return pte; }
  206. static inline pte_t pte_mkyoung(pte_t pte) { (pte).pte_low |= _PAGE_ACCESSED; return pte; }
  207. static inline pte_t pte_mkwrite(pte_t pte) { (pte).pte_low |= _PAGE_RW; return pte; }
  208. static inline pte_t pte_mkhuge(pte_t pte) { (pte).pte_low |= _PAGE_PSE; return pte; }
  209. #ifdef CONFIG_X86_PAE
  210. # include <asm/pgtable-3level.h>
  211. #else
  212. # include <asm/pgtable-2level.h>
  213. #endif
  214. /*
  215. * We only update the dirty/accessed state if we set
  216. * the dirty bit by hand in the kernel, since the hardware
  217. * will do the accessed bit for us, and we don't want to
  218. * race with other CPU's that might be updating the dirty
  219. * bit at the same time.
  220. */
  221. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  222. #define ptep_set_access_flags(vma, address, ptep, entry, dirty) \
  223. do { \
  224. if (dirty) { \
  225. (ptep)->pte_low = (entry).pte_low; \
  226. flush_tlb_page(vma, address); \
  227. } \
  228. } while (0)
  229. /*
  230. * We don't actually have these, but we want to advertise them so that
  231. * we can encompass the flush here.
  232. */
  233. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_DIRTY
  234. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  235. #define __HAVE_ARCH_PTEP_CLEAR_DIRTY_FLUSH
  236. #define ptep_clear_flush_dirty(vma, address, ptep) \
  237. ({ \
  238. int __dirty; \
  239. __dirty = pte_dirty(*(ptep)); \
  240. if (__dirty) { \
  241. clear_bit(_PAGE_BIT_DIRTY, &(ptep)->pte_low); \
  242. flush_tlb_page(vma, address); \
  243. } \
  244. __dirty; \
  245. })
  246. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  247. #define ptep_clear_flush_young(vma, address, ptep) \
  248. ({ \
  249. int __young; \
  250. __young = pte_young(*(ptep)); \
  251. if (__young) { \
  252. clear_bit(_PAGE_BIT_ACCESSED, &(ptep)->pte_low); \
  253. flush_tlb_page(vma, address); \
  254. } \
  255. __young; \
  256. })
  257. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
  258. static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, unsigned long addr, pte_t *ptep, int full)
  259. {
  260. pte_t pte;
  261. if (full) {
  262. pte = *ptep;
  263. pte_clear(mm, addr, ptep);
  264. } else {
  265. pte = ptep_get_and_clear(mm, addr, ptep);
  266. }
  267. return pte;
  268. }
  269. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  270. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  271. {
  272. clear_bit(_PAGE_BIT_RW, &ptep->pte_low);
  273. }
  274. /*
  275. * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
  276. *
  277. * dst - pointer to pgd range anwhere on a pgd page
  278. * src - ""
  279. * count - the number of pgds to copy.
  280. *
  281. * dst and src can be on the same page, but the range must not overlap,
  282. * and must not cross a page boundary.
  283. */
  284. static inline void clone_pgd_range(pgd_t *dst, pgd_t *src, int count)
  285. {
  286. memcpy(dst, src, count * sizeof(pgd_t));
  287. }
  288. /*
  289. * Macro to mark a page protection value as "uncacheable". On processors which do not support
  290. * it, this is a no-op.
  291. */
  292. #define pgprot_noncached(prot) ((boot_cpu_data.x86 > 3) \
  293. ? (__pgprot(pgprot_val(prot) | _PAGE_PCD | _PAGE_PWT)) : (prot))
  294. /*
  295. * Conversion functions: convert a page and protection to a page entry,
  296. * and a page entry and page directory to the page they refer to.
  297. */
  298. #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
  299. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  300. {
  301. pte.pte_low &= _PAGE_CHG_MASK;
  302. pte.pte_low |= pgprot_val(newprot);
  303. #ifdef CONFIG_X86_PAE
  304. /*
  305. * Chop off the NX bit (if present), and add the NX portion of
  306. * the newprot (if present):
  307. */
  308. pte.pte_high &= ~(1 << (_PAGE_BIT_NX - 32));
  309. pte.pte_high |= (pgprot_val(newprot) >> 32) & \
  310. (__supported_pte_mask >> 32);
  311. #endif
  312. return pte;
  313. }
  314. #define pmd_large(pmd) \
  315. ((pmd_val(pmd) & (_PAGE_PSE|_PAGE_PRESENT)) == (_PAGE_PSE|_PAGE_PRESENT))
  316. /*
  317. * the pgd page can be thought of an array like this: pgd_t[PTRS_PER_PGD]
  318. *
  319. * this macro returns the index of the entry in the pgd page which would
  320. * control the given virtual address
  321. */
  322. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  323. #define pgd_index_k(addr) pgd_index(addr)
  324. /*
  325. * pgd_offset() returns a (pgd_t *)
  326. * pgd_index() is used get the offset into the pgd page's array of pgd_t's;
  327. */
  328. #define pgd_offset(mm, address) ((mm)->pgd+pgd_index(address))
  329. /*
  330. * a shortcut which implies the use of the kernel's pgd, instead
  331. * of a process's
  332. */
  333. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  334. /*
  335. * the pmd page can be thought of an array like this: pmd_t[PTRS_PER_PMD]
  336. *
  337. * this macro returns the index of the entry in the pmd page which would
  338. * control the given virtual address
  339. */
  340. #define pmd_index(address) \
  341. (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  342. /*
  343. * the pte page can be thought of an array like this: pte_t[PTRS_PER_PTE]
  344. *
  345. * this macro returns the index of the entry in the pte page which would
  346. * control the given virtual address
  347. */
  348. #define pte_index(address) \
  349. (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  350. #define pte_offset_kernel(dir, address) \
  351. ((pte_t *) pmd_page_vaddr(*(dir)) + pte_index(address))
  352. #define pmd_page(pmd) (pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT))
  353. #define pmd_page_vaddr(pmd) \
  354. ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
  355. /*
  356. * Helper function that returns the kernel pagetable entry controlling
  357. * the virtual address 'address'. NULL means no pagetable entry present.
  358. * NOTE: the return type is pte_t but if the pmd is PSE then we return it
  359. * as a pte too.
  360. */
  361. extern pte_t *lookup_address(unsigned long address);
  362. /*
  363. * Make a given kernel text page executable/non-executable.
  364. * Returns the previous executability setting of that page (which
  365. * is used to restore the previous state). Used by the SMP bootup code.
  366. * NOTE: this is an __init function for security reasons.
  367. */
  368. #ifdef CONFIG_X86_PAE
  369. extern int set_kernel_exec(unsigned long vaddr, int enable);
  370. #else
  371. static inline int set_kernel_exec(unsigned long vaddr, int enable) { return 0;}
  372. #endif
  373. #if defined(CONFIG_HIGHPTE)
  374. #define pte_offset_map(dir, address) \
  375. ((pte_t *)kmap_atomic(pmd_page(*(dir)),KM_PTE0) + pte_index(address))
  376. #define pte_offset_map_nested(dir, address) \
  377. ((pte_t *)kmap_atomic(pmd_page(*(dir)),KM_PTE1) + pte_index(address))
  378. #define pte_unmap(pte) kunmap_atomic(pte, KM_PTE0)
  379. #define pte_unmap_nested(pte) kunmap_atomic(pte, KM_PTE1)
  380. #else
  381. #define pte_offset_map(dir, address) \
  382. ((pte_t *)page_address(pmd_page(*(dir))) + pte_index(address))
  383. #define pte_offset_map_nested(dir, address) pte_offset_map(dir, address)
  384. #define pte_unmap(pte) do { } while (0)
  385. #define pte_unmap_nested(pte) do { } while (0)
  386. #endif
  387. /* Clear a kernel PTE and flush it from the TLB */
  388. #define kpte_clear_flush(ptep, vaddr) \
  389. do { \
  390. pte_clear(&init_mm, vaddr, ptep); \
  391. __flush_tlb_one(vaddr); \
  392. } while (0)
  393. /*
  394. * The i386 doesn't have any external MMU info: the kernel page
  395. * tables contain all the necessary information.
  396. */
  397. #define update_mmu_cache(vma,address,pte) do { } while (0)
  398. #endif /* !__ASSEMBLY__ */
  399. #ifdef CONFIG_FLATMEM
  400. #define kern_addr_valid(addr) (1)
  401. #endif /* CONFIG_FLATMEM */
  402. #define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \
  403. remap_pfn_range(vma, vaddr, pfn, size, prot)
  404. #define MK_IOSPACE_PFN(space, pfn) (pfn)
  405. #define GET_IOSPACE(pfn) 0
  406. #define GET_PFN(pfn) (pfn)
  407. #include <asm-generic/pgtable.h>
  408. #endif /* _I386_PGTABLE_H */