pgtable.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * linux/include/asm-xtensa/page.h
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Copyright (C) 2001 - 2005 Tensilica Inc.
  9. */
  10. #ifndef _XTENSA_PGTABLE_H
  11. #define _XTENSA_PGTABLE_H
  12. #include <asm-generic/pgtable-nopmd.h>
  13. #include <asm/page.h>
  14. /* Assertions. */
  15. #ifdef CONFIG_MMU
  16. #if (XCHAL_MMU_RINGS < 2)
  17. # error Linux build assumes at least 2 ring levels.
  18. #endif
  19. #if (XCHAL_MMU_CA_BITS != 4)
  20. # error We assume exactly four bits for CA.
  21. #endif
  22. #if (XCHAL_MMU_SR_BITS != 0)
  23. # error We have no room for SR bits.
  24. #endif
  25. /*
  26. * Use the first min-wired way for mapping page-table pages.
  27. * Page coloring requires a second min-wired way.
  28. */
  29. #if (XCHAL_DTLB_MINWIRED_SETS == 0)
  30. # error Need a min-wired way for mapping page-table pages
  31. #endif
  32. #define DTLB_WAY_PGTABLE XCHAL_DTLB_SET(XCHAL_DTLB_MINWIRED_SET0, WAY)
  33. #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
  34. # if XCHAL_DTLB_SET(XCHAL_DTLB_MINWIRED_SET0, WAYS) >= 2
  35. # define DTLB_WAY_DCACHE_ALIAS0 (DTLB_WAY_PGTABLE + 1)
  36. # define DTLB_WAY_DCACHE_ALIAS1 (DTLB_WAY_PGTABLE + 2)
  37. # else
  38. # error Page coloring requires its own wired dtlb way!
  39. # endif
  40. #endif
  41. #endif /* CONFIG_MMU */
  42. /*
  43. * We only use two ring levels, user and kernel space.
  44. */
  45. #define USER_RING 1 /* user ring level */
  46. #define KERNEL_RING 0 /* kernel ring level */
  47. /*
  48. * The Xtensa architecture port of Linux has a two-level page table system,
  49. * i.e. the logical three-level Linux page table layout are folded.
  50. * Each task has the following memory page tables:
  51. *
  52. * PGD table (page directory), ie. 3rd-level page table:
  53. * One page (4 kB) of 1024 (PTRS_PER_PGD) pointers to PTE tables
  54. * (Architectures that don't have the PMD folded point to the PMD tables)
  55. *
  56. * The pointer to the PGD table for a given task can be retrieved from
  57. * the task structure (struct task_struct*) t, e.g. current():
  58. * (t->mm ? t->mm : t->active_mm)->pgd
  59. *
  60. * PMD tables (page middle-directory), ie. 2nd-level page tables:
  61. * Absent for the Xtensa architecture (folded, PTRS_PER_PMD == 1).
  62. *
  63. * PTE tables (page table entry), ie. 1st-level page tables:
  64. * One page (4 kB) of 1024 (PTRS_PER_PTE) PTEs with a special PTE
  65. * invalid_pte_table for absent mappings.
  66. *
  67. * The individual pages are 4 kB big with special pages for the empty_zero_page.
  68. */
  69. #define PGDIR_SHIFT 22
  70. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  71. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  72. /*
  73. * Entries per page directory level: we use two-level, so
  74. * we don't really have any PMD directory physically.
  75. */
  76. #define PTRS_PER_PTE 1024
  77. #define PTRS_PER_PTE_SHIFT 10
  78. #define PTRS_PER_PMD 1
  79. #define PTRS_PER_PGD 1024
  80. #define PGD_ORDER 0
  81. #define PMD_ORDER 0
  82. #define USER_PTRS_PER_PGD (TASK_SIZE/PGDIR_SIZE)
  83. #define FIRST_USER_ADDRESS XCHAL_SEG_MAPPABLE_VADDR
  84. #define FIRST_USER_PGD_NR (FIRST_USER_ADDRESS >> PGDIR_SHIFT)
  85. /* virtual memory area. We keep a distance to other memory regions to be
  86. * on the safe side. We also use this area for cache aliasing.
  87. */
  88. // FIXME: virtual memory area must be configuration-dependent
  89. #define VMALLOC_START 0xC0000000
  90. #define VMALLOC_END 0xC7FF0000
  91. /* Xtensa Linux config PTE layout (when present):
  92. * 31-12: PPN
  93. * 11-6: Software
  94. * 5-4: RING
  95. * 3-0: CA
  96. *
  97. * Similar to the Alpha and MIPS ports, we need to keep track of the ref
  98. * and mod bits in software. We have a software "you can read
  99. * from this page" bit, and a hardware one which actually lets the
  100. * process read from the page. On the same token we have a software
  101. * writable bit and the real hardware one which actually lets the
  102. * process write to the page.
  103. *
  104. * See further below for PTE layout for swapped-out pages.
  105. */
  106. #define _PAGE_VALID (1<<0) /* hardware: page is accessible */
  107. #define _PAGE_WRENABLE (1<<1) /* hardware: page is writable */
  108. /* None of these cache modes include MP coherency: */
  109. #define _PAGE_NO_CACHE (0<<2) /* bypass, non-speculative */
  110. #if XCHAL_DCACHE_IS_WRITEBACK
  111. # define _PAGE_WRITEBACK (1<<2) /* write back */
  112. # define _PAGE_WRITETHRU (2<<2) /* write through */
  113. #else
  114. # define _PAGE_WRITEBACK (1<<2) /* assume write through */
  115. # define _PAGE_WRITETHRU (1<<2)
  116. #endif
  117. #define _PAGE_NOALLOC (3<<2) /* don't allocate cache,if not cached */
  118. #define _CACHE_MASK (3<<2)
  119. #define _PAGE_USER (1<<4) /* user access (ring=1) */
  120. #define _PAGE_KERNEL (0<<4) /* kernel access (ring=0) */
  121. /* Software */
  122. #define _PAGE_RW (1<<6) /* software: page writable */
  123. #define _PAGE_DIRTY (1<<7) /* software: page dirty */
  124. #define _PAGE_ACCESSED (1<<8) /* software: page accessed (read) */
  125. #define _PAGE_FILE (1<<9) /* nonlinear file mapping*/
  126. #define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _CACHE_MASK | _PAGE_DIRTY)
  127. #define _PAGE_PRESENT ( _PAGE_VALID | _PAGE_WRITEBACK | _PAGE_ACCESSED)
  128. #ifdef CONFIG_MMU
  129. # define PAGE_NONE __pgprot(_PAGE_PRESENT)
  130. # define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_RW)
  131. # define PAGE_COPY __pgprot(_PAGE_PRESENT | _PAGE_USER)
  132. # define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER)
  133. # define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_KERNEL | _PAGE_WRENABLE)
  134. # define PAGE_INVALID __pgprot(_PAGE_USER)
  135. # if (DCACHE_WAY_SIZE > PAGE_SIZE)
  136. # define PAGE_DIRECTORY __pgprot(_PAGE_VALID | _PAGE_ACCESSED | _PAGE_KERNEL)
  137. # else
  138. # define PAGE_DIRECTORY __pgprot(_PAGE_PRESENT | _PAGE_KERNEL)
  139. # endif
  140. #else /* no mmu */
  141. # define PAGE_NONE __pgprot(0)
  142. # define PAGE_SHARED __pgprot(0)
  143. # define PAGE_COPY __pgprot(0)
  144. # define PAGE_READONLY __pgprot(0)
  145. # define PAGE_KERNEL __pgprot(0)
  146. #endif
  147. /*
  148. * On certain configurations of Xtensa MMUs (eg. the initial Linux config),
  149. * the MMU can't do page protection for execute, and considers that the same as
  150. * read. Also, write permissions may imply read permissions.
  151. * What follows is the closest we can get by reasonable means..
  152. * See linux/mm/mmap.c for protection_map[] array that uses these definitions.
  153. */
  154. #define __P000 PAGE_NONE /* private --- */
  155. #define __P001 PAGE_READONLY /* private --r */
  156. #define __P010 PAGE_COPY /* private -w- */
  157. #define __P011 PAGE_COPY /* private -wr */
  158. #define __P100 PAGE_READONLY /* private x-- */
  159. #define __P101 PAGE_READONLY /* private x-r */
  160. #define __P110 PAGE_COPY /* private xw- */
  161. #define __P111 PAGE_COPY /* private xwr */
  162. #define __S000 PAGE_NONE /* shared --- */
  163. #define __S001 PAGE_READONLY /* shared --r */
  164. #define __S010 PAGE_SHARED /* shared -w- */
  165. #define __S011 PAGE_SHARED /* shared -wr */
  166. #define __S100 PAGE_READONLY /* shared x-- */
  167. #define __S101 PAGE_READONLY /* shared x-r */
  168. #define __S110 PAGE_SHARED /* shared xw- */
  169. #define __S111 PAGE_SHARED /* shared xwr */
  170. #ifndef __ASSEMBLY__
  171. #define pte_ERROR(e) \
  172. printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
  173. #define pgd_ERROR(e) \
  174. printk("%s:%d: bad pgd entry %08lx.\n", __FILE__, __LINE__, pgd_val(e))
  175. extern unsigned long empty_zero_page[1024];
  176. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  177. extern pgd_t swapper_pg_dir[PAGE_SIZE/sizeof(pgd_t)];
  178. /*
  179. * The pmd contains the kernel virtual address of the pte page.
  180. */
  181. #define pmd_page_kernel(pmd) ((unsigned long)(pmd_val(pmd) & PAGE_MASK))
  182. #define pmd_page(pmd) virt_to_page(pmd_val(pmd))
  183. /*
  184. * The following only work if pte_present() is true.
  185. */
  186. #define pte_none(pte) (!(pte_val(pte) ^ _PAGE_USER))
  187. #define pte_present(pte) (pte_val(pte) & _PAGE_VALID)
  188. #define pte_clear(mm,addr,ptep) \
  189. do { update_pte(ptep, __pte(_PAGE_USER)); } while(0)
  190. #define pmd_none(pmd) (!pmd_val(pmd))
  191. #define pmd_present(pmd) (pmd_val(pmd) & PAGE_MASK)
  192. #define pmd_clear(pmdp) do { set_pmd(pmdp, __pmd(0)); } while (0)
  193. #define pmd_bad(pmd) (pmd_val(pmd) & ~PAGE_MASK)
  194. /* Note: We use the _PAGE_USER bit to indicate write-protect kernel memory */
  195. static inline int pte_read(pte_t pte) { return pte_val(pte) & _PAGE_USER; }
  196. static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_RW; }
  197. static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
  198. static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
  199. static inline int pte_file(pte_t pte) { return pte_val(pte) & _PAGE_FILE; }
  200. static inline pte_t pte_wrprotect(pte_t pte) { pte_val(pte) &= ~(_PAGE_RW | _PAGE_WRENABLE); return pte; }
  201. static inline pte_t pte_rdprotect(pte_t pte) { pte_val(pte) &= ~_PAGE_USER; return pte; }
  202. static inline pte_t pte_mkclean(pte_t pte) { pte_val(pte) &= ~_PAGE_DIRTY; return pte; }
  203. static inline pte_t pte_mkold(pte_t pte) { pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
  204. static inline pte_t pte_mkread(pte_t pte) { pte_val(pte) |= _PAGE_USER; return pte; }
  205. static inline pte_t pte_mkdirty(pte_t pte) { pte_val(pte) |= _PAGE_DIRTY; return pte; }
  206. static inline pte_t pte_mkyoung(pte_t pte) { pte_val(pte) |= _PAGE_ACCESSED; return pte; }
  207. static inline pte_t pte_mkwrite(pte_t pte) { pte_val(pte) |= _PAGE_RW; return pte; }
  208. /*
  209. * Conversion functions: convert a page and protection to a page entry,
  210. * and a page entry and page directory to the page they refer to.
  211. */
  212. #define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
  213. #define pte_same(a,b) (pte_val(a) == pte_val(b))
  214. #define pte_page(x) pfn_to_page(pte_pfn(x))
  215. #define pfn_pte(pfn, prot) __pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot))
  216. #define mk_pte(page, prot) pfn_pte(page_to_pfn(page), prot)
  217. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  218. {
  219. return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
  220. }
  221. /*
  222. * Certain architectures need to do special things when pte's
  223. * within a page table are directly modified. Thus, the following
  224. * hook is made available.
  225. */
  226. static inline void update_pte(pte_t *ptep, pte_t pteval)
  227. {
  228. *ptep = pteval;
  229. #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
  230. __asm__ __volatile__ ("memw; dhwb %0, 0; dsync" :: "a" (ptep));
  231. #endif
  232. }
  233. struct mm_struct;
  234. static inline void
  235. set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pteval)
  236. {
  237. update_pte(ptep, pteval);
  238. }
  239. static inline void
  240. set_pmd(pmd_t *pmdp, pmd_t pmdval)
  241. {
  242. *pmdp = pmdval;
  243. #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
  244. __asm__ __volatile__ ("memw; dhwb %0, 0; dsync" :: "a" (pmdp));
  245. #endif
  246. }
  247. struct vm_area_struct;
  248. static inline int
  249. ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr,
  250. pte_t *ptep)
  251. {
  252. pte_t pte = *ptep;
  253. if (!pte_young(pte))
  254. return 0;
  255. update_pte(ptep, pte_mkold(pte));
  256. return 1;
  257. }
  258. static inline int
  259. ptep_test_and_clear_dirty(struct vm_area_struct *vma, unsigned long addr,
  260. pte_t *ptep)
  261. {
  262. pte_t pte = *ptep;
  263. if (!pte_dirty(pte))
  264. return 0;
  265. update_pte(ptep, pte_mkclean(pte));
  266. return 1;
  267. }
  268. static inline pte_t
  269. ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  270. {
  271. pte_t pte = *ptep;
  272. pte_clear(mm, addr, ptep);
  273. return pte;
  274. }
  275. static inline void
  276. ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  277. {
  278. pte_t pte = *ptep;
  279. update_pte(ptep, pte_wrprotect(pte));
  280. }
  281. /* to find an entry in a kernel page-table-directory */
  282. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  283. /* to find an entry in a page-table-directory */
  284. #define pgd_offset(mm,address) ((mm)->pgd + pgd_index(address))
  285. #define pgd_index(address) ((address) >> PGDIR_SHIFT)
  286. /* Find an entry in the second-level page table.. */
  287. #define pmd_offset(dir,address) ((pmd_t*)(dir))
  288. /* Find an entry in the third-level page table.. */
  289. #define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  290. #define pte_offset_kernel(dir,addr) \
  291. ((pte_t*) pmd_page_kernel(*(dir)) + pte_index(addr))
  292. #define pte_offset_map(dir,addr) pte_offset_kernel((dir),(addr))
  293. #define pte_offset_map_nested(dir,addr) pte_offset_kernel((dir),(addr))
  294. #define pte_unmap(pte) do { } while (0)
  295. #define pte_unmap_nested(pte) do { } while (0)
  296. /*
  297. * Encode and decode a swap entry.
  298. * Each PTE in a process VM's page table is either:
  299. * "present" -- valid and not swapped out, protection bits are meaningful;
  300. * "not present" -- which further subdivides in these two cases:
  301. * "none" -- no mapping at all; identified by pte_none(), set by pte_clear(
  302. * "swapped out" -- the page is swapped out, and the SWP macros below
  303. * are used to store swap file info in the PTE itself.
  304. *
  305. * In the Xtensa processor MMU, any PTE entries in user space (or anywhere
  306. * in virtual memory that can map differently across address spaces)
  307. * must have a correct ring value that represents the RASID field that
  308. * is changed when switching address spaces. Eg. such PTE entries cannot
  309. * be set to ring zero, because that can cause a (global) kernel ASID
  310. * entry to be created in the TLBs (even with invalid cache attribute),
  311. * potentially causing a multihit exception when going back to another
  312. * address space that mapped the same virtual address at another ring.
  313. *
  314. * SO: we avoid using ring bits (_PAGE_RING_MASK) in "not present" PTEs.
  315. * We also avoid using the _PAGE_VALID bit which must be zero for non-present
  316. * pages.
  317. *
  318. * We end up with the following available bits: 1..3 and 7..31.
  319. * We don't bother with 1..3 for now (we can use them later if needed),
  320. * and chose to allocate 6 bits for SWP_TYPE and the remaining 19 bits
  321. * for SWP_OFFSET. At least 5 bits are needed for SWP_TYPE, because it
  322. * is currently implemented as an index into swap_info[MAX_SWAPFILES]
  323. * and MAX_SWAPFILES is currently defined as 32 in <linux/swap.h>.
  324. * However, for some reason all other architectures in the 2.4 kernel
  325. * reserve either 6, 7, or 8 bits so I'll not detract from that for now. :)
  326. * SWP_OFFSET is an offset into the swap file in page-size units, so
  327. * with 4 kB pages, 19 bits supports a maximum swap file size of 2 GB.
  328. *
  329. * FIXME: 2 GB isn't very big. Other bits can be used to allow
  330. * larger swap sizes. In the meantime, it appears relatively easy to get
  331. * around the 2 GB limitation by simply using multiple swap files.
  332. */
  333. #define __swp_type(entry) (((entry).val >> 7) & 0x3f)
  334. #define __swp_offset(entry) ((entry).val >> 13)
  335. #define __swp_entry(type,offs) ((swp_entry_t) {((type) << 7) | ((offs) << 13)})
  336. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  337. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  338. #define PTE_FILE_MAX_BITS 29
  339. #define pte_to_pgoff(pte) (pte_val(pte) >> 3)
  340. #define pgoff_to_pte(off) ((pte_t) { ((off) << 3) | _PAGE_FILE })
  341. #endif /* !defined (__ASSEMBLY__) */
  342. #ifdef __ASSEMBLY__
  343. /* Assembly macro _PGD_INDEX is the same as C pgd_index(unsigned long),
  344. * _PGD_OFFSET as C pgd_offset(struct mm_struct*, unsigned long),
  345. * _PMD_OFFSET as C pmd_offset(pgd_t*, unsigned long)
  346. * _PTE_OFFSET as C pte_offset(pmd_t*, unsigned long)
  347. *
  348. * Note: We require an additional temporary register which can be the same as
  349. * the register that holds the address.
  350. *
  351. * ((pte_t*) ((unsigned long)(pmd_val(*pmd) & PAGE_MASK)) + pte_index(addr))
  352. *
  353. */
  354. #define _PGD_INDEX(rt,rs) extui rt, rs, PGDIR_SHIFT, 32-PGDIR_SHIFT
  355. #define _PTE_INDEX(rt,rs) extui rt, rs, PAGE_SHIFT, PTRS_PER_PTE_SHIFT
  356. #define _PGD_OFFSET(mm,adr,tmp) l32i mm, mm, MM_PGD; \
  357. _PGD_INDEX(tmp, adr); \
  358. addx4 mm, tmp, mm
  359. #define _PTE_OFFSET(pmd,adr,tmp) _PTE_INDEX(tmp, adr); \
  360. srli pmd, pmd, PAGE_SHIFT; \
  361. slli pmd, pmd, PAGE_SHIFT; \
  362. addx4 pmd, tmp, pmd
  363. #else
  364. extern void paging_init(void);
  365. #define kern_addr_valid(addr) (1)
  366. extern void update_mmu_cache(struct vm_area_struct * vma,
  367. unsigned long address, pte_t pte);
  368. /*
  369. * remap a physical page `pfn' of size `size' with page protection `prot'
  370. * into virtual address `from'
  371. */
  372. #define io_remap_pfn_range(vma,from,pfn,size,prot) \
  373. remap_pfn_range(vma, from, pfn, size, prot)
  374. /* No page table caches to init */
  375. #define pgtable_cache_init() do { } while (0)
  376. typedef pte_t *pte_addr_t;
  377. #endif /* !defined (__ASSEMBLY__) */
  378. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  379. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_DIRTY
  380. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  381. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  382. #define __HAVE_ARCH_PTEP_MKDIRTY
  383. #define __HAVE_ARCH_PTE_SAME
  384. #include <asm-generic/pgtable.h>
  385. #endif /* _XTENSA_PGTABLE_H */