pgtable.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * include/asm-xtensa/pgtable.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 version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Copyright (C) 2001 - 2007 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. /*
  15. * We only use two ring levels, user and kernel space.
  16. */
  17. #define USER_RING 1 /* user ring level */
  18. #define KERNEL_RING 0 /* kernel ring level */
  19. /*
  20. * The Xtensa architecture port of Linux has a two-level page table system,
  21. * i.e. the logical three-level Linux page table layout is folded.
  22. * Each task has the following memory page tables:
  23. *
  24. * PGD table (page directory), ie. 3rd-level page table:
  25. * One page (4 kB) of 1024 (PTRS_PER_PGD) pointers to PTE tables
  26. * (Architectures that don't have the PMD folded point to the PMD tables)
  27. *
  28. * The pointer to the PGD table for a given task can be retrieved from
  29. * the task structure (struct task_struct*) t, e.g. current():
  30. * (t->mm ? t->mm : t->active_mm)->pgd
  31. *
  32. * PMD tables (page middle-directory), ie. 2nd-level page tables:
  33. * Absent for the Xtensa architecture (folded, PTRS_PER_PMD == 1).
  34. *
  35. * PTE tables (page table entry), ie. 1st-level page tables:
  36. * One page (4 kB) of 1024 (PTRS_PER_PTE) PTEs with a special PTE
  37. * invalid_pte_table for absent mappings.
  38. *
  39. * The individual pages are 4 kB big with special pages for the empty_zero_page.
  40. */
  41. #define PGDIR_SHIFT 22
  42. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  43. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  44. /*
  45. * Entries per page directory level: we use two-level, so
  46. * we don't really have any PMD directory physically.
  47. */
  48. #define PTRS_PER_PTE 1024
  49. #define PTRS_PER_PTE_SHIFT 10
  50. #define PTRS_PER_PGD 1024
  51. #define PGD_ORDER 0
  52. #define USER_PTRS_PER_PGD (TASK_SIZE/PGDIR_SIZE)
  53. #define FIRST_USER_ADDRESS 0
  54. #define FIRST_USER_PGD_NR (FIRST_USER_ADDRESS >> PGDIR_SHIFT)
  55. /*
  56. * Virtual memory area. We keep a distance to other memory regions to be
  57. * on the safe side. We also use this area for cache aliasing.
  58. */
  59. #define VMALLOC_START 0xC0000000
  60. #define VMALLOC_END 0xC7FEFFFF
  61. #define TLBTEMP_BASE_1 0xC7FF0000
  62. #define TLBTEMP_BASE_2 (TLBTEMP_BASE_1 + DCACHE_WAY_SIZE)
  63. #if 2 * DCACHE_WAY_SIZE > ICACHE_WAY_SIZE
  64. #define TLBTEMP_SIZE (2 * DCACHE_WAY_SIZE)
  65. #else
  66. #define TLBTEMP_SIZE ICACHE_WAY_SIZE
  67. #endif
  68. /*
  69. * Xtensa Linux config PTE layout (when present):
  70. * 31-12: PPN
  71. * 11-6: Software
  72. * 5-4: RING
  73. * 3-0: CA
  74. *
  75. * Similar to the Alpha and MIPS ports, we need to keep track of the ref
  76. * and mod bits in software. We have a software "you can read
  77. * from this page" bit, and a hardware one which actually lets the
  78. * process read from the page. On the same token we have a software
  79. * writable bit and the real hardware one which actually lets the
  80. * process write to the page.
  81. *
  82. * See further below for PTE layout for swapped-out pages.
  83. */
  84. #define _PAGE_HW_EXEC (1<<0) /* hardware: page is executable */
  85. #define _PAGE_HW_WRITE (1<<1) /* hardware: page is writable */
  86. #define _PAGE_FILE (1<<1) /* non-linear mapping, if !present */
  87. #define _PAGE_PROTNONE (3<<0) /* special case for VM_PROT_NONE */
  88. /* None of these cache modes include MP coherency: */
  89. #define _PAGE_CA_BYPASS (0<<2) /* bypass, non-speculative */
  90. #define _PAGE_CA_WB (1<<2) /* write-back */
  91. #define _PAGE_CA_WT (2<<2) /* write-through */
  92. #define _PAGE_CA_MASK (3<<2)
  93. #define _PAGE_INVALID (3<<2)
  94. #define _PAGE_USER (1<<4) /* user access (ring=1) */
  95. /* Software */
  96. #define _PAGE_WRITABLE_BIT 6
  97. #define _PAGE_WRITABLE (1<<6) /* software: page writable */
  98. #define _PAGE_DIRTY (1<<7) /* software: page dirty */
  99. #define _PAGE_ACCESSED (1<<8) /* software: page accessed (read) */
  100. /* On older HW revisions, we always have to set bit 0 */
  101. #if XCHAL_HW_VERSION_MAJOR < 2000
  102. # define _PAGE_VALID (1<<0)
  103. #else
  104. # define _PAGE_VALID 0
  105. #endif
  106. #define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
  107. #define _PAGE_PRESENT (_PAGE_VALID | _PAGE_CA_WB | _PAGE_ACCESSED)
  108. #ifdef CONFIG_MMU
  109. #define PAGE_NONE __pgprot(_PAGE_INVALID | _PAGE_USER | _PAGE_PROTNONE)
  110. #define PAGE_COPY __pgprot(_PAGE_PRESENT | _PAGE_USER)
  111. #define PAGE_COPY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
  112. #define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER)
  113. #define PAGE_READONLY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
  114. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE)
  115. #define PAGE_SHARED_EXEC \
  116. __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE | _PAGE_HW_EXEC)
  117. #define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_HW_WRITE)
  118. #define PAGE_KERNEL_EXEC __pgprot(_PAGE_PRESENT|_PAGE_HW_WRITE|_PAGE_HW_EXEC)
  119. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  120. # define _PAGE_DIRECTORY (_PAGE_VALID | _PAGE_ACCESSED)
  121. #else
  122. # define _PAGE_DIRECTORY (_PAGE_VALID | _PAGE_ACCESSED | _PAGE_CA_WB)
  123. #endif
  124. #else /* no mmu */
  125. # define PAGE_NONE __pgprot(0)
  126. # define PAGE_SHARED __pgprot(0)
  127. # define PAGE_COPY __pgprot(0)
  128. # define PAGE_READONLY __pgprot(0)
  129. # define PAGE_KERNEL __pgprot(0)
  130. #endif
  131. /*
  132. * On certain configurations of Xtensa MMUs (eg. the initial Linux config),
  133. * the MMU can't do page protection for execute, and considers that the same as
  134. * read. Also, write permissions may imply read permissions.
  135. * What follows is the closest we can get by reasonable means..
  136. * See linux/mm/mmap.c for protection_map[] array that uses these definitions.
  137. */
  138. #define __P000 PAGE_NONE /* private --- */
  139. #define __P001 PAGE_READONLY /* private --r */
  140. #define __P010 PAGE_COPY /* private -w- */
  141. #define __P011 PAGE_COPY /* private -wr */
  142. #define __P100 PAGE_READONLY_EXEC /* private x-- */
  143. #define __P101 PAGE_READONLY_EXEC /* private x-r */
  144. #define __P110 PAGE_COPY_EXEC /* private xw- */
  145. #define __P111 PAGE_COPY_EXEC /* private xwr */
  146. #define __S000 PAGE_NONE /* shared --- */
  147. #define __S001 PAGE_READONLY /* shared --r */
  148. #define __S010 PAGE_SHARED /* shared -w- */
  149. #define __S011 PAGE_SHARED /* shared -wr */
  150. #define __S100 PAGE_READONLY_EXEC /* shared x-- */
  151. #define __S101 PAGE_READONLY_EXEC /* shared x-r */
  152. #define __S110 PAGE_SHARED_EXEC /* shared xw- */
  153. #define __S111 PAGE_SHARED_EXEC /* shared xwr */
  154. #ifndef __ASSEMBLY__
  155. #define pte_ERROR(e) \
  156. printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
  157. #define pgd_ERROR(e) \
  158. printk("%s:%d: bad pgd entry %08lx.\n", __FILE__, __LINE__, pgd_val(e))
  159. extern unsigned long empty_zero_page[1024];
  160. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  161. #ifdef CONFIG_MMU
  162. extern pgd_t swapper_pg_dir[PAGE_SIZE/sizeof(pgd_t)];
  163. extern void paging_init(void);
  164. extern void pgtable_cache_init(void);
  165. #else
  166. # define swapper_pg_dir NULL
  167. static inline void paging_init(void) { }
  168. static inline void pgtable_cache_init(void) { }
  169. #endif
  170. /*
  171. * The pmd contains the kernel virtual address of the pte page.
  172. */
  173. #define pmd_page_vaddr(pmd) ((unsigned long)(pmd_val(pmd) & PAGE_MASK))
  174. #define pmd_page(pmd) virt_to_page(pmd_val(pmd))
  175. /*
  176. * pte status.
  177. */
  178. #define pte_none(pte) (pte_val(pte) == _PAGE_INVALID)
  179. #define pte_present(pte) \
  180. (((pte_val(pte) & _PAGE_CA_MASK) != _PAGE_INVALID) \
  181. || ((pte_val(pte) & _PAGE_PROTNONE) == _PAGE_PROTNONE))
  182. #define pte_clear(mm,addr,ptep) \
  183. do { update_pte(ptep, __pte(_PAGE_INVALID)); } while(0)
  184. #define pmd_none(pmd) (!pmd_val(pmd))
  185. #define pmd_present(pmd) (pmd_val(pmd) & PAGE_MASK)
  186. #define pmd_bad(pmd) (pmd_val(pmd) & ~PAGE_MASK)
  187. #define pmd_clear(pmdp) do { set_pmd(pmdp, __pmd(0)); } while (0)
  188. static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_WRITABLE; }
  189. static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
  190. static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
  191. static inline int pte_file(pte_t pte) { return pte_val(pte) & _PAGE_FILE; }
  192. static inline int pte_special(pte_t pte) { return 0; }
  193. static inline pte_t pte_wrprotect(pte_t pte)
  194. { pte_val(pte) &= ~(_PAGE_WRITABLE | _PAGE_HW_WRITE); return pte; }
  195. static inline pte_t pte_mkclean(pte_t pte)
  196. { pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_HW_WRITE); return pte; }
  197. static inline pte_t pte_mkold(pte_t pte)
  198. { pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
  199. static inline pte_t pte_mkdirty(pte_t pte)
  200. { pte_val(pte) |= _PAGE_DIRTY; return pte; }
  201. static inline pte_t pte_mkyoung(pte_t pte)
  202. { pte_val(pte) |= _PAGE_ACCESSED; return pte; }
  203. static inline pte_t pte_mkwrite(pte_t pte)
  204. { pte_val(pte) |= _PAGE_WRITABLE; return pte; }
  205. static inline pte_t pte_mkspecial(pte_t pte)
  206. { return pte; }
  207. /*
  208. * Conversion functions: convert a page and protection to a page entry,
  209. * and a page entry and page directory to the page they refer to.
  210. */
  211. #define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
  212. #define pte_same(a,b) (pte_val(a) == pte_val(b))
  213. #define pte_page(x) pfn_to_page(pte_pfn(x))
  214. #define pfn_pte(pfn, prot) __pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot))
  215. #define mk_pte(page, prot) pfn_pte(page_to_pfn(page), prot)
  216. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  217. {
  218. return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
  219. }
  220. /*
  221. * Certain architectures need to do special things when pte's
  222. * within a page table are directly modified. Thus, the following
  223. * hook is made available.
  224. */
  225. static inline void update_pte(pte_t *ptep, pte_t pteval)
  226. {
  227. *ptep = pteval;
  228. #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
  229. __asm__ __volatile__ ("dhwb %0, 0" :: "a" (ptep));
  230. #endif
  231. }
  232. struct mm_struct;
  233. static inline void
  234. set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pteval)
  235. {
  236. update_pte(ptep, pteval);
  237. }
  238. static inline void
  239. set_pmd(pmd_t *pmdp, pmd_t pmdval)
  240. {
  241. *pmdp = pmdval;
  242. }
  243. struct vm_area_struct;
  244. static inline int
  245. ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr,
  246. pte_t *ptep)
  247. {
  248. pte_t pte = *ptep;
  249. if (!pte_young(pte))
  250. return 0;
  251. update_pte(ptep, pte_mkold(pte));
  252. return 1;
  253. }
  254. static inline pte_t
  255. ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  256. {
  257. pte_t pte = *ptep;
  258. pte_clear(mm, addr, ptep);
  259. return pte;
  260. }
  261. static inline void
  262. ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  263. {
  264. pte_t pte = *ptep;
  265. update_pte(ptep, pte_wrprotect(pte));
  266. }
  267. /* to find an entry in a kernel page-table-directory */
  268. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  269. /* to find an entry in a page-table-directory */
  270. #define pgd_offset(mm,address) ((mm)->pgd + pgd_index(address))
  271. #define pgd_index(address) ((address) >> PGDIR_SHIFT)
  272. /* Find an entry in the second-level page table.. */
  273. #define pmd_offset(dir,address) ((pmd_t*)(dir))
  274. /* Find an entry in the third-level page table.. */
  275. #define pte_index(address) (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  276. #define pte_offset_kernel(dir,addr) \
  277. ((pte_t*) pmd_page_vaddr(*(dir)) + pte_index(addr))
  278. #define pte_offset_map(dir,addr) pte_offset_kernel((dir),(addr))
  279. #define pte_unmap(pte) do { } while (0)
  280. /*
  281. * Encode and decode a swap entry.
  282. *
  283. * Format of swap pte:
  284. * bit 0 MBZ
  285. * bit 1 page-file (must be zero)
  286. * bits 2 - 3 page hw access mode (must be 11: _PAGE_INVALID)
  287. * bits 4 - 5 ring protection (must be 01: _PAGE_USER)
  288. * bits 6 - 10 swap type (5 bits -> 32 types)
  289. * bits 11 - 31 swap offset / PAGE_SIZE (21 bits -> 8GB)
  290. * Format of file pte:
  291. * bit 0 MBZ
  292. * bit 1 page-file (must be one: _PAGE_FILE)
  293. * bits 2 - 3 page hw access mode (must be 11: _PAGE_INVALID)
  294. * bits 4 - 5 ring protection (must be 01: _PAGE_USER)
  295. * bits 6 - 31 file offset / PAGE_SIZE
  296. */
  297. #define __swp_type(entry) (((entry).val >> 6) & 0x1f)
  298. #define __swp_offset(entry) ((entry).val >> 11)
  299. #define __swp_entry(type,offs) \
  300. ((swp_entry_t) {((type) << 6) | ((offs) << 11) | _PAGE_INVALID})
  301. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  302. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  303. #define PTE_FILE_MAX_BITS 28
  304. #define pte_to_pgoff(pte) (pte_val(pte) >> 4)
  305. #define pgoff_to_pte(off) \
  306. ((pte_t) { ((off) << 4) | _PAGE_INVALID | _PAGE_FILE })
  307. #endif /* !defined (__ASSEMBLY__) */
  308. #ifdef __ASSEMBLY__
  309. /* Assembly macro _PGD_INDEX is the same as C pgd_index(unsigned long),
  310. * _PGD_OFFSET as C pgd_offset(struct mm_struct*, unsigned long),
  311. * _PMD_OFFSET as C pmd_offset(pgd_t*, unsigned long)
  312. * _PTE_OFFSET as C pte_offset(pmd_t*, unsigned long)
  313. *
  314. * Note: We require an additional temporary register which can be the same as
  315. * the register that holds the address.
  316. *
  317. * ((pte_t*) ((unsigned long)(pmd_val(*pmd) & PAGE_MASK)) + pte_index(addr))
  318. *
  319. */
  320. #define _PGD_INDEX(rt,rs) extui rt, rs, PGDIR_SHIFT, 32-PGDIR_SHIFT
  321. #define _PTE_INDEX(rt,rs) extui rt, rs, PAGE_SHIFT, PTRS_PER_PTE_SHIFT
  322. #define _PGD_OFFSET(mm,adr,tmp) l32i mm, mm, MM_PGD; \
  323. _PGD_INDEX(tmp, adr); \
  324. addx4 mm, tmp, mm
  325. #define _PTE_OFFSET(pmd,adr,tmp) _PTE_INDEX(tmp, adr); \
  326. srli pmd, pmd, PAGE_SHIFT; \
  327. slli pmd, pmd, PAGE_SHIFT; \
  328. addx4 pmd, tmp, pmd
  329. #else
  330. #define kern_addr_valid(addr) (1)
  331. extern void update_mmu_cache(struct vm_area_struct * vma,
  332. unsigned long address, pte_t *ptep);
  333. /*
  334. * remap a physical page `pfn' of size `size' with page protection `prot'
  335. * into virtual address `from'
  336. */
  337. #define io_remap_pfn_range(vma,from,pfn,size,prot) \
  338. remap_pfn_range(vma, from, pfn, size, prot)
  339. typedef pte_t *pte_addr_t;
  340. #endif /* !defined (__ASSEMBLY__) */
  341. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  342. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  343. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  344. #define __HAVE_ARCH_PTEP_MKDIRTY
  345. #define __HAVE_ARCH_PTE_SAME
  346. #include <asm-generic/pgtable.h>
  347. #endif /* _XTENSA_PGTABLE_H */