pgtable-64.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 2003 Ralf Baechle
  7. * Copyright (C) 1999, 2000, 2001 Silicon Graphics, Inc.
  8. */
  9. #ifndef _ASM_PGTABLE_64_H
  10. #define _ASM_PGTABLE_64_H
  11. #include <linux/compiler.h>
  12. #include <linux/linkage.h>
  13. #include <asm/addrspace.h>
  14. #include <asm/page.h>
  15. #include <asm/cachectl.h>
  16. #include <asm/fixmap.h>
  17. #define __ARCH_USE_5LEVEL_HACK
  18. #if CONFIG_PGTABLE_LEVELS == 2
  19. #include <asm-generic/pgtable-nopmd.h>
  20. #elif CONFIG_PGTABLE_LEVELS == 3
  21. #include <asm-generic/pgtable-nopud.h>
  22. #else
  23. #include <asm-generic/5level-fixup.h>
  24. #endif
  25. /*
  26. * Each address space has 2 4K pages as its page directory, giving 1024
  27. * (== PTRS_PER_PGD) 8 byte pointers to pmd tables. Each pmd table is a
  28. * single 4K page, giving 512 (== PTRS_PER_PMD) 8 byte pointers to page
  29. * tables. Each page table is also a single 4K page, giving 512 (==
  30. * PTRS_PER_PTE) 8 byte ptes. Each pud entry is initialized to point to
  31. * invalid_pmd_table, each pmd entry is initialized to point to
  32. * invalid_pte_table, each pte is initialized to 0.
  33. *
  34. * Kernel mappings: kernel mappings are held in the swapper_pg_table.
  35. * The layout is identical to userspace except it's indexed with the
  36. * fault address - VMALLOC_START.
  37. */
  38. /* PGDIR_SHIFT determines what a third-level page table entry can map */
  39. #ifdef __PAGETABLE_PMD_FOLDED
  40. #define PGDIR_SHIFT (PAGE_SHIFT + PAGE_SHIFT + PTE_ORDER - 3)
  41. #else
  42. /* PMD_SHIFT determines the size of the area a second-level page table can map */
  43. #define PMD_SHIFT (PAGE_SHIFT + (PAGE_SHIFT + PTE_ORDER - 3))
  44. #define PMD_SIZE (1UL << PMD_SHIFT)
  45. #define PMD_MASK (~(PMD_SIZE-1))
  46. # ifdef __PAGETABLE_PUD_FOLDED
  47. # define PGDIR_SHIFT (PMD_SHIFT + (PAGE_SHIFT + PMD_ORDER - 3))
  48. # endif
  49. #endif
  50. #ifndef __PAGETABLE_PUD_FOLDED
  51. #define PUD_SHIFT (PMD_SHIFT + (PAGE_SHIFT + PMD_ORDER - 3))
  52. #define PUD_SIZE (1UL << PUD_SHIFT)
  53. #define PUD_MASK (~(PUD_SIZE-1))
  54. #define PGDIR_SHIFT (PUD_SHIFT + (PAGE_SHIFT + PUD_ORDER - 3))
  55. #endif
  56. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  57. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  58. /*
  59. * For 4kB page size we use a 3 level page tree and an 8kB pud, which
  60. * permits us mapping 40 bits of virtual address space.
  61. *
  62. * We used to implement 41 bits by having an order 1 pmd level but that seemed
  63. * rather pointless.
  64. *
  65. * For 8kB page size we use a 3 level page tree which permits a total of
  66. * 8TB of address space. Alternatively a 33-bit / 8GB organization using
  67. * two levels would be easy to implement.
  68. *
  69. * For 16kB page size we use a 2 level page tree which permits a total of
  70. * 36 bits of virtual address space. We could add a third level but it seems
  71. * like at the moment there's no need for this.
  72. *
  73. * For 64kB page size we use a 2 level page table tree for a total of 42 bits
  74. * of virtual address space.
  75. */
  76. #ifdef CONFIG_PAGE_SIZE_4KB
  77. # ifdef CONFIG_MIPS_VA_BITS_48
  78. # define PGD_ORDER 0
  79. # define PUD_ORDER 0
  80. # else
  81. # define PGD_ORDER 1
  82. # define PUD_ORDER aieeee_attempt_to_allocate_pud
  83. # endif
  84. #define PMD_ORDER 0
  85. #define PTE_ORDER 0
  86. #endif
  87. #ifdef CONFIG_PAGE_SIZE_8KB
  88. #define PGD_ORDER 0
  89. #define PUD_ORDER aieeee_attempt_to_allocate_pud
  90. #define PMD_ORDER 0
  91. #define PTE_ORDER 0
  92. #endif
  93. #ifdef CONFIG_PAGE_SIZE_16KB
  94. #ifdef CONFIG_MIPS_VA_BITS_48
  95. #define PGD_ORDER 1
  96. #else
  97. #define PGD_ORDER 0
  98. #endif
  99. #define PUD_ORDER aieeee_attempt_to_allocate_pud
  100. #define PMD_ORDER 0
  101. #define PTE_ORDER 0
  102. #endif
  103. #ifdef CONFIG_PAGE_SIZE_32KB
  104. #define PGD_ORDER 0
  105. #define PUD_ORDER aieeee_attempt_to_allocate_pud
  106. #define PMD_ORDER 0
  107. #define PTE_ORDER 0
  108. #endif
  109. #ifdef CONFIG_PAGE_SIZE_64KB
  110. #define PGD_ORDER 0
  111. #define PUD_ORDER aieeee_attempt_to_allocate_pud
  112. #ifdef CONFIG_MIPS_VA_BITS_48
  113. #define PMD_ORDER 0
  114. #else
  115. #define PMD_ORDER aieeee_attempt_to_allocate_pmd
  116. #endif
  117. #define PTE_ORDER 0
  118. #endif
  119. #define PTRS_PER_PGD ((PAGE_SIZE << PGD_ORDER) / sizeof(pgd_t))
  120. #ifndef __PAGETABLE_PUD_FOLDED
  121. #define PTRS_PER_PUD ((PAGE_SIZE << PUD_ORDER) / sizeof(pud_t))
  122. #endif
  123. #ifndef __PAGETABLE_PMD_FOLDED
  124. #define PTRS_PER_PMD ((PAGE_SIZE << PMD_ORDER) / sizeof(pmd_t))
  125. #endif
  126. #define PTRS_PER_PTE ((PAGE_SIZE << PTE_ORDER) / sizeof(pte_t))
  127. #define USER_PTRS_PER_PGD ((TASK_SIZE64 / PGDIR_SIZE)?(TASK_SIZE64 / PGDIR_SIZE):1)
  128. #define FIRST_USER_ADDRESS 0UL
  129. /*
  130. * TLB refill handlers also map the vmalloc area into xuseg. Avoid
  131. * the first couple of pages so NULL pointer dereferences will still
  132. * reliably trap.
  133. */
  134. #define VMALLOC_START (MAP_BASE + (2 * PAGE_SIZE))
  135. #define VMALLOC_END \
  136. (MAP_BASE + \
  137. min(PTRS_PER_PGD * PTRS_PER_PUD * PTRS_PER_PMD * PTRS_PER_PTE * PAGE_SIZE, \
  138. (1UL << cpu_vmbits)) - (1UL << 32))
  139. #if defined(CONFIG_MODULES) && defined(KBUILD_64BIT_SYM32) && \
  140. VMALLOC_START != CKSSEG
  141. /* Load modules into 32bit-compatible segment. */
  142. #define MODULE_START CKSSEG
  143. #define MODULE_END (FIXADDR_START-2*PAGE_SIZE)
  144. #endif
  145. #define pte_ERROR(e) \
  146. printk("%s:%d: bad pte %016lx.\n", __FILE__, __LINE__, pte_val(e))
  147. #ifndef __PAGETABLE_PMD_FOLDED
  148. #define pmd_ERROR(e) \
  149. printk("%s:%d: bad pmd %016lx.\n", __FILE__, __LINE__, pmd_val(e))
  150. #endif
  151. #ifndef __PAGETABLE_PUD_FOLDED
  152. #define pud_ERROR(e) \
  153. printk("%s:%d: bad pud %016lx.\n", __FILE__, __LINE__, pud_val(e))
  154. #endif
  155. #define pgd_ERROR(e) \
  156. printk("%s:%d: bad pgd %016lx.\n", __FILE__, __LINE__, pgd_val(e))
  157. extern pte_t invalid_pte_table[PTRS_PER_PTE];
  158. #ifndef __PAGETABLE_PUD_FOLDED
  159. /*
  160. * For 4-level pagetables we defines these ourselves, for 3-level the
  161. * definitions are below, for 2-level the
  162. * definitions are supplied by <asm-generic/pgtable-nopmd.h>.
  163. */
  164. typedef struct { unsigned long pud; } pud_t;
  165. #define pud_val(x) ((x).pud)
  166. #define __pud(x) ((pud_t) { (x) })
  167. extern pud_t invalid_pud_table[PTRS_PER_PUD];
  168. /*
  169. * Empty pgd entries point to the invalid_pud_table.
  170. */
  171. static inline int pgd_none(pgd_t pgd)
  172. {
  173. return pgd_val(pgd) == (unsigned long)invalid_pud_table;
  174. }
  175. static inline int pgd_bad(pgd_t pgd)
  176. {
  177. if (unlikely(pgd_val(pgd) & ~PAGE_MASK))
  178. return 1;
  179. return 0;
  180. }
  181. static inline int pgd_present(pgd_t pgd)
  182. {
  183. return pgd_val(pgd) != (unsigned long)invalid_pud_table;
  184. }
  185. static inline void pgd_clear(pgd_t *pgdp)
  186. {
  187. pgd_val(*pgdp) = (unsigned long)invalid_pud_table;
  188. }
  189. #define pud_index(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD - 1))
  190. static inline unsigned long pgd_page_vaddr(pgd_t pgd)
  191. {
  192. return pgd_val(pgd);
  193. }
  194. #define pgd_phys(pgd) virt_to_phys((void *)pgd_val(pgd))
  195. #define pgd_page(pgd) (pfn_to_page(pgd_phys(pgd) >> PAGE_SHIFT))
  196. static inline pud_t *pud_offset(pgd_t *pgd, unsigned long address)
  197. {
  198. return (pud_t *)pgd_page_vaddr(*pgd) + pud_index(address);
  199. }
  200. static inline void set_pgd(pgd_t *pgd, pgd_t pgdval)
  201. {
  202. *pgd = pgdval;
  203. }
  204. #endif
  205. #ifndef __PAGETABLE_PMD_FOLDED
  206. /*
  207. * For 3-level pagetables we defines these ourselves, for 2-level the
  208. * definitions are supplied by <asm-generic/pgtable-nopmd.h>.
  209. */
  210. typedef struct { unsigned long pmd; } pmd_t;
  211. #define pmd_val(x) ((x).pmd)
  212. #define __pmd(x) ((pmd_t) { (x) } )
  213. extern pmd_t invalid_pmd_table[PTRS_PER_PMD];
  214. #endif
  215. /*
  216. * Empty pgd/pmd entries point to the invalid_pte_table.
  217. */
  218. static inline int pmd_none(pmd_t pmd)
  219. {
  220. return pmd_val(pmd) == (unsigned long) invalid_pte_table;
  221. }
  222. static inline int pmd_bad(pmd_t pmd)
  223. {
  224. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  225. /* pmd_huge(pmd) but inline */
  226. if (unlikely(pmd_val(pmd) & _PAGE_HUGE))
  227. return 0;
  228. #endif
  229. if (unlikely(pmd_val(pmd) & ~PAGE_MASK))
  230. return 1;
  231. return 0;
  232. }
  233. static inline int pmd_present(pmd_t pmd)
  234. {
  235. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  236. if (unlikely(pmd_val(pmd) & _PAGE_HUGE))
  237. return pmd_val(pmd) & _PAGE_PRESENT;
  238. #endif
  239. return pmd_val(pmd) != (unsigned long) invalid_pte_table;
  240. }
  241. static inline void pmd_clear(pmd_t *pmdp)
  242. {
  243. pmd_val(*pmdp) = ((unsigned long) invalid_pte_table);
  244. }
  245. #ifndef __PAGETABLE_PMD_FOLDED
  246. /*
  247. * Empty pud entries point to the invalid_pmd_table.
  248. */
  249. static inline int pud_none(pud_t pud)
  250. {
  251. return pud_val(pud) == (unsigned long) invalid_pmd_table;
  252. }
  253. static inline int pud_bad(pud_t pud)
  254. {
  255. return pud_val(pud) & ~PAGE_MASK;
  256. }
  257. static inline int pud_present(pud_t pud)
  258. {
  259. return pud_val(pud) != (unsigned long) invalid_pmd_table;
  260. }
  261. static inline void pud_clear(pud_t *pudp)
  262. {
  263. pud_val(*pudp) = ((unsigned long) invalid_pmd_table);
  264. }
  265. #endif
  266. #define pte_page(x) pfn_to_page(pte_pfn(x))
  267. #ifdef CONFIG_CPU_VR41XX
  268. #define pte_pfn(x) ((unsigned long)((x).pte >> (PAGE_SHIFT + 2)))
  269. #define pfn_pte(pfn, prot) __pte(((pfn) << (PAGE_SHIFT + 2)) | pgprot_val(prot))
  270. #else
  271. #define pte_pfn(x) ((unsigned long)((x).pte >> _PFN_SHIFT))
  272. #define pfn_pte(pfn, prot) __pte(((pfn) << _PFN_SHIFT) | pgprot_val(prot))
  273. #define pfn_pmd(pfn, prot) __pmd(((pfn) << _PFN_SHIFT) | pgprot_val(prot))
  274. #endif
  275. #define __pgd_offset(address) pgd_index(address)
  276. #define __pud_offset(address) (((address) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
  277. #define __pmd_offset(address) pmd_index(address)
  278. /* to find an entry in a kernel page-table-directory */
  279. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  280. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  281. #define pmd_index(address) (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  282. /* to find an entry in a page-table-directory */
  283. #define pgd_offset(mm, addr) ((mm)->pgd + pgd_index(addr))
  284. #ifndef __PAGETABLE_PMD_FOLDED
  285. static inline unsigned long pud_page_vaddr(pud_t pud)
  286. {
  287. return pud_val(pud);
  288. }
  289. #define pud_phys(pud) virt_to_phys((void *)pud_val(pud))
  290. #define pud_page(pud) (pfn_to_page(pud_phys(pud) >> PAGE_SHIFT))
  291. /* Find an entry in the second-level page table.. */
  292. static inline pmd_t *pmd_offset(pud_t * pud, unsigned long address)
  293. {
  294. return (pmd_t *) pud_page_vaddr(*pud) + pmd_index(address);
  295. }
  296. #endif
  297. /* Find an entry in the third-level page table.. */
  298. #define __pte_offset(address) \
  299. (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  300. #define pte_offset(dir, address) \
  301. ((pte_t *) pmd_page_vaddr(*(dir)) + __pte_offset(address))
  302. #define pte_offset_kernel(dir, address) \
  303. ((pte_t *) pmd_page_vaddr(*(dir)) + __pte_offset(address))
  304. #define pte_offset_map(dir, address) \
  305. ((pte_t *)page_address(pmd_page(*(dir))) + __pte_offset(address))
  306. #define pte_unmap(pte) ((void)(pte))
  307. /*
  308. * Initialize a new pgd / pmd table with invalid pointers.
  309. */
  310. extern void pgd_init(unsigned long page);
  311. extern void pud_init(unsigned long page, unsigned long pagetable);
  312. extern void pmd_init(unsigned long page, unsigned long pagetable);
  313. /*
  314. * Non-present pages: high 40 bits are offset, next 8 bits type,
  315. * low 16 bits zero.
  316. */
  317. static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
  318. { pte_t pte; pte_val(pte) = (type << 16) | (offset << 24); return pte; }
  319. #define __swp_type(x) (((x).val >> 16) & 0xff)
  320. #define __swp_offset(x) ((x).val >> 24)
  321. #define __swp_entry(type, offset) ((swp_entry_t) { pte_val(mk_swap_pte((type), (offset))) })
  322. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  323. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  324. #endif /* _ASM_PGTABLE_64_H */