pgtable-64.h 11 KB

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