pgtable-64.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #if CONFIG_PGTABLE_LEVELS == 2
  18. #include <asm-generic/pgtable-nopmd.h>
  19. #elif CONFIG_PGTABLE_LEVELS == 3
  20. #include <asm-generic/pgtable-nopud.h>
  21. #else
  22. #include <asm-generic/pgtable-nop4d.h>
  23. #endif
  24. /*
  25. * Each address space has 2 4K pages as its page directory, giving 1024
  26. * (== PTRS_PER_PGD) 8 byte pointers to pmd tables. Each pmd table is a
  27. * single 4K page, giving 512 (== PTRS_PER_PMD) 8 byte pointers to page
  28. * tables. Each page table is also a single 4K page, giving 512 (==
  29. * PTRS_PER_PTE) 8 byte ptes. Each pud entry is initialized to point to
  30. * invalid_pmd_table, each pmd entry is initialized to point to
  31. * invalid_pte_table, each pte is initialized to 0.
  32. *
  33. * Kernel mappings: kernel mappings are held in the swapper_pg_table.
  34. * The layout is identical to userspace except it's indexed with the
  35. * fault address - VMALLOC_START.
  36. */
  37. /* PGDIR_SHIFT determines what a third-level page table entry can map */
  38. #ifdef __PAGETABLE_PMD_FOLDED
  39. #define PGDIR_SHIFT (PAGE_SHIFT + PAGE_SHIFT - 3)
  40. #else
  41. /* PMD_SHIFT determines the size of the area a second-level page table can map */
  42. #define PMD_SHIFT (PAGE_SHIFT + (PAGE_SHIFT - 3))
  43. #define PMD_SIZE (1UL << PMD_SHIFT)
  44. #define PMD_MASK (~(PMD_SIZE-1))
  45. # ifdef __PAGETABLE_PUD_FOLDED
  46. # define PGDIR_SHIFT (PMD_SHIFT + (PAGE_SHIFT + PMD_TABLE_ORDER - 3))
  47. # endif
  48. #endif
  49. #ifndef __PAGETABLE_PUD_FOLDED
  50. #define PUD_SHIFT (PMD_SHIFT + (PAGE_SHIFT + PMD_TABLE_ORDER - 3))
  51. #define PUD_SIZE (1UL << PUD_SHIFT)
  52. #define PUD_MASK (~(PUD_SIZE-1))
  53. #define PGDIR_SHIFT (PUD_SHIFT + (PAGE_SHIFT + PUD_TABLE_ORDER - 3))
  54. #endif
  55. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  56. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  57. /*
  58. * For 4kB page size we use a 3 level page tree and an 8kB pud, which
  59. * permits us mapping 40 bits of virtual address space.
  60. *
  61. * We used to implement 41 bits by having an order 1 pmd level but that seemed
  62. * rather pointless.
  63. *
  64. * For 8kB page size we use a 3 level page tree which permits a total of
  65. * 8TB of address space. Alternatively a 33-bit / 8GB organization using
  66. * two levels would be easy to implement.
  67. *
  68. * For 16kB page size we use a 2 level page tree which permits a total of
  69. * 36 bits of virtual address space. We could add a third level but it seems
  70. * like at the moment there's no need for this.
  71. *
  72. * For 64kB page size we use a 2 level page table tree for a total of 42 bits
  73. * of virtual address space.
  74. */
  75. #ifdef CONFIG_PAGE_SIZE_4KB
  76. # ifdef CONFIG_MIPS_VA_BITS_48
  77. # define PGD_TABLE_ORDER 0
  78. # define PUD_TABLE_ORDER 0
  79. # else
  80. # define PGD_TABLE_ORDER 1
  81. # define PUD_TABLE_ORDER aieeee_attempt_to_allocate_pud
  82. # endif
  83. #define PMD_TABLE_ORDER 0
  84. #endif
  85. #ifdef CONFIG_PAGE_SIZE_8KB
  86. #define PGD_TABLE_ORDER 0
  87. #define PUD_TABLE_ORDER aieeee_attempt_to_allocate_pud
  88. #define PMD_TABLE_ORDER 0
  89. #endif
  90. #ifdef CONFIG_PAGE_SIZE_16KB
  91. #ifdef CONFIG_MIPS_VA_BITS_48
  92. #define PGD_TABLE_ORDER 1
  93. #else
  94. #define PGD_TABLE_ORDER 0
  95. #endif
  96. #define PUD_TABLE_ORDER aieeee_attempt_to_allocate_pud
  97. #define PMD_TABLE_ORDER 0
  98. #endif
  99. #ifdef CONFIG_PAGE_SIZE_32KB
  100. #define PGD_TABLE_ORDER 0
  101. #define PUD_TABLE_ORDER aieeee_attempt_to_allocate_pud
  102. #define PMD_TABLE_ORDER 0
  103. #endif
  104. #ifdef CONFIG_PAGE_SIZE_64KB
  105. #define PGD_TABLE_ORDER 0
  106. #define PUD_TABLE_ORDER aieeee_attempt_to_allocate_pud
  107. #ifdef CONFIG_MIPS_VA_BITS_48
  108. #define PMD_TABLE_ORDER 0
  109. #else
  110. #define PMD_TABLE_ORDER aieeee_attempt_to_allocate_pmd
  111. #endif
  112. #endif
  113. #define PTRS_PER_PGD ((PAGE_SIZE << PGD_TABLE_ORDER) / sizeof(pgd_t))
  114. #ifndef __PAGETABLE_PUD_FOLDED
  115. #define PTRS_PER_PUD ((PAGE_SIZE << PUD_TABLE_ORDER) / sizeof(pud_t))
  116. #endif
  117. #ifndef __PAGETABLE_PMD_FOLDED
  118. #define PTRS_PER_PMD ((PAGE_SIZE << PMD_TABLE_ORDER) / sizeof(pmd_t))
  119. #endif
  120. #define PTRS_PER_PTE (PAGE_SIZE / sizeof(pte_t))
  121. #define USER_PTRS_PER_PGD ((TASK_SIZE64 / PGDIR_SIZE)?(TASK_SIZE64 / PGDIR_SIZE):1)
  122. /*
  123. * TLB refill handlers also map the vmalloc area into xuseg. Avoid
  124. * the first couple of pages so NULL pointer dereferences will still
  125. * reliably trap.
  126. */
  127. #define VMALLOC_START (MAP_BASE + (2 * PAGE_SIZE))
  128. #define VMALLOC_END \
  129. (MAP_BASE + \
  130. min(PTRS_PER_PGD * PTRS_PER_PUD * PTRS_PER_PMD * PTRS_PER_PTE * PAGE_SIZE, \
  131. (1UL << cpu_vmbits)) - (1UL << 32))
  132. #if defined(CONFIG_MODULES) && defined(KBUILD_64BIT_SYM32) && \
  133. VMALLOC_START != CKSSEG
  134. /* Load modules into 32bit-compatible segment. */
  135. #define MODULE_START CKSSEG
  136. #define MODULE_END (FIXADDR_START-2*PAGE_SIZE)
  137. #endif
  138. #define pte_ERROR(e) \
  139. printk("%s:%d: bad pte %016lx.\n", __FILE__, __LINE__, pte_val(e))
  140. #ifndef __PAGETABLE_PMD_FOLDED
  141. #define pmd_ERROR(e) \
  142. printk("%s:%d: bad pmd %016lx.\n", __FILE__, __LINE__, pmd_val(e))
  143. #endif
  144. #ifndef __PAGETABLE_PUD_FOLDED
  145. #define pud_ERROR(e) \
  146. printk("%s:%d: bad pud %016lx.\n", __FILE__, __LINE__, pud_val(e))
  147. #endif
  148. #define pgd_ERROR(e) \
  149. printk("%s:%d: bad pgd %016lx.\n", __FILE__, __LINE__, pgd_val(e))
  150. extern pte_t invalid_pte_table[PTRS_PER_PTE];
  151. #ifndef __PAGETABLE_PUD_FOLDED
  152. /*
  153. * For 4-level pagetables we defines these ourselves, for 3-level the
  154. * definitions are below, for 2-level the
  155. * definitions are supplied by <asm-generic/pgtable-nopmd.h>.
  156. */
  157. typedef struct { unsigned long pud; } pud_t;
  158. #define pud_val(x) ((x).pud)
  159. #define __pud(x) ((pud_t) { (x) })
  160. extern pud_t invalid_pud_table[PTRS_PER_PUD];
  161. /*
  162. * Empty pgd entries point to the invalid_pud_table.
  163. */
  164. static inline int p4d_none(p4d_t p4d)
  165. {
  166. return p4d_val(p4d) == (unsigned long)invalid_pud_table;
  167. }
  168. static inline int p4d_bad(p4d_t p4d)
  169. {
  170. if (unlikely(p4d_val(p4d) & ~PAGE_MASK))
  171. return 1;
  172. return 0;
  173. }
  174. static inline int p4d_present(p4d_t p4d)
  175. {
  176. return p4d_val(p4d) != (unsigned long)invalid_pud_table;
  177. }
  178. static inline void p4d_clear(p4d_t *p4dp)
  179. {
  180. p4d_val(*p4dp) = (unsigned long)invalid_pud_table;
  181. }
  182. static inline pud_t *p4d_pgtable(p4d_t p4d)
  183. {
  184. return (pud_t *)p4d_val(p4d);
  185. }
  186. #define p4d_phys(p4d) virt_to_phys((void *)p4d_val(p4d))
  187. #define p4d_page(p4d) (pfn_to_page(p4d_phys(p4d) >> PAGE_SHIFT))
  188. #define p4d_index(address) (((address) >> P4D_SHIFT) & (PTRS_PER_P4D - 1))
  189. static inline void set_p4d(p4d_t *p4d, p4d_t p4dval)
  190. {
  191. *p4d = p4dval;
  192. }
  193. #endif
  194. #ifndef __PAGETABLE_PMD_FOLDED
  195. /*
  196. * For 3-level pagetables we defines these ourselves, for 2-level the
  197. * definitions are supplied by <asm-generic/pgtable-nopmd.h>.
  198. */
  199. typedef struct { unsigned long pmd; } pmd_t;
  200. #define pmd_val(x) ((x).pmd)
  201. #define __pmd(x) ((pmd_t) { (x) } )
  202. extern pmd_t invalid_pmd_table[PTRS_PER_PMD];
  203. #endif
  204. /*
  205. * Empty pgd/pmd entries point to the invalid_pte_table.
  206. */
  207. static inline int pmd_none(pmd_t pmd)
  208. {
  209. return pmd_val(pmd) == (unsigned long) invalid_pte_table;
  210. }
  211. static inline int pmd_bad(pmd_t pmd)
  212. {
  213. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  214. /* pmd_huge(pmd) but inline */
  215. if (unlikely(pmd_val(pmd) & _PAGE_HUGE))
  216. return 0;
  217. #endif
  218. if (unlikely(pmd_val(pmd) & ~PAGE_MASK))
  219. return 1;
  220. return 0;
  221. }
  222. static inline int pmd_present(pmd_t pmd)
  223. {
  224. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  225. if (unlikely(pmd_val(pmd) & _PAGE_HUGE))
  226. return pmd_val(pmd) & _PAGE_PRESENT;
  227. #endif
  228. return pmd_val(pmd) != (unsigned long) invalid_pte_table;
  229. }
  230. static inline void pmd_clear(pmd_t *pmdp)
  231. {
  232. pmd_val(*pmdp) = ((unsigned long) invalid_pte_table);
  233. }
  234. #ifndef __PAGETABLE_PMD_FOLDED
  235. /*
  236. * Empty pud entries point to the invalid_pmd_table.
  237. */
  238. static inline int pud_none(pud_t pud)
  239. {
  240. return pud_val(pud) == (unsigned long) invalid_pmd_table;
  241. }
  242. static inline int pud_bad(pud_t pud)
  243. {
  244. return pud_val(pud) & ~PAGE_MASK;
  245. }
  246. static inline int pud_present(pud_t pud)
  247. {
  248. return pud_val(pud) != (unsigned long) invalid_pmd_table;
  249. }
  250. static inline void pud_clear(pud_t *pudp)
  251. {
  252. pud_val(*pudp) = ((unsigned long) invalid_pmd_table);
  253. }
  254. #endif
  255. #define pte_page(x) pfn_to_page(pte_pfn(x))
  256. #define pte_pfn(x) ((unsigned long)((x).pte >> _PFN_SHIFT))
  257. #define pfn_pte(pfn, prot) __pte(((pfn) << _PFN_SHIFT) | pgprot_val(prot))
  258. #define pfn_pmd(pfn, prot) __pmd(((pfn) << _PFN_SHIFT) | pgprot_val(prot))
  259. #ifndef __PAGETABLE_PMD_FOLDED
  260. static inline pmd_t *pud_pgtable(pud_t pud)
  261. {
  262. return (pmd_t *)pud_val(pud);
  263. }
  264. #define pud_phys(pud) virt_to_phys((void *)pud_val(pud))
  265. #define pud_page(pud) (pfn_to_page(pud_phys(pud) >> PAGE_SHIFT))
  266. #endif
  267. /*
  268. * Initialize a new pgd / pmd table with invalid pointers.
  269. */
  270. extern void pgd_init(unsigned long page);
  271. extern void pud_init(unsigned long page, unsigned long pagetable);
  272. extern void pmd_init(unsigned long page, unsigned long pagetable);
  273. /*
  274. * Non-present pages: high 40 bits are offset, next 8 bits type,
  275. * low 16 bits zero.
  276. */
  277. static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
  278. { pte_t pte; pte_val(pte) = (type << 16) | (offset << 24); return pte; }
  279. #define __swp_type(x) (((x).val >> 16) & 0xff)
  280. #define __swp_offset(x) ((x).val >> 24)
  281. #define __swp_entry(type, offset) ((swp_entry_t) { pte_val(mk_swap_pte((type), (offset))) })
  282. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  283. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  284. #endif /* _ASM_PGTABLE_64_H */