pgtable.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * include/asm-xtensa/pgtable.h
  4. *
  5. * Copyright (C) 2001 - 2013 Tensilica Inc.
  6. */
  7. #ifndef _XTENSA_PGTABLE_H
  8. #define _XTENSA_PGTABLE_H
  9. #include <asm/page.h>
  10. #include <asm/kmem_layout.h>
  11. #include <asm-generic/pgtable-nopmd.h>
  12. /*
  13. * We only use two ring levels, user and kernel space.
  14. */
  15. #ifdef CONFIG_MMU
  16. #define USER_RING 1 /* user ring level */
  17. #else
  18. #define USER_RING 0
  19. #endif
  20. #define KERNEL_RING 0 /* kernel ring level */
  21. /*
  22. * The Xtensa architecture port of Linux has a two-level page table system,
  23. * i.e. the logical three-level Linux page table layout is folded.
  24. * Each task has the following memory page tables:
  25. *
  26. * PGD table (page directory), ie. 3rd-level page table:
  27. * One page (4 kB) of 1024 (PTRS_PER_PGD) pointers to PTE tables
  28. * (Architectures that don't have the PMD folded point to the PMD tables)
  29. *
  30. * The pointer to the PGD table for a given task can be retrieved from
  31. * the task structure (struct task_struct*) t, e.g. current():
  32. * (t->mm ? t->mm : t->active_mm)->pgd
  33. *
  34. * PMD tables (page middle-directory), ie. 2nd-level page tables:
  35. * Absent for the Xtensa architecture (folded, PTRS_PER_PMD == 1).
  36. *
  37. * PTE tables (page table entry), ie. 1st-level page tables:
  38. * One page (4 kB) of 1024 (PTRS_PER_PTE) PTEs with a special PTE
  39. * invalid_pte_table for absent mappings.
  40. *
  41. * The individual pages are 4 kB big with special pages for the empty_zero_page.
  42. */
  43. #define PGDIR_SHIFT 22
  44. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  45. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  46. /*
  47. * Entries per page directory level: we use two-level, so
  48. * we don't really have any PMD directory physically.
  49. */
  50. #define PTRS_PER_PTE 1024
  51. #define PTRS_PER_PTE_SHIFT 10
  52. #define PTRS_PER_PGD 1024
  53. #define USER_PTRS_PER_PGD (TASK_SIZE/PGDIR_SIZE)
  54. #define FIRST_USER_PGD_NR (FIRST_USER_ADDRESS >> PGDIR_SHIFT)
  55. #ifdef CONFIG_MMU
  56. /*
  57. * Virtual memory area. We keep a distance to other memory regions to be
  58. * on the safe side. We also use this area for cache aliasing.
  59. */
  60. #define VMALLOC_START (XCHAL_KSEG_CACHED_VADDR - 0x10000000)
  61. #define VMALLOC_END (VMALLOC_START + 0x07FEFFFF)
  62. #define TLBTEMP_BASE_1 (VMALLOC_START + 0x08000000)
  63. #define TLBTEMP_BASE_2 (TLBTEMP_BASE_1 + DCACHE_WAY_SIZE)
  64. #if 2 * DCACHE_WAY_SIZE > ICACHE_WAY_SIZE
  65. #define TLBTEMP_SIZE (2 * DCACHE_WAY_SIZE)
  66. #else
  67. #define TLBTEMP_SIZE ICACHE_WAY_SIZE
  68. #endif
  69. #else
  70. #define VMALLOC_START __XTENSA_UL_CONST(0)
  71. #define VMALLOC_END __XTENSA_UL_CONST(0xffffffff)
  72. #endif
  73. /*
  74. * For the Xtensa architecture, the PTE layout is as follows:
  75. *
  76. * 31------12 11 10-9 8-6 5-4 3-2 1-0
  77. * +-----------------------------------------+
  78. * | | Software | HARDWARE |
  79. * | PPN | ADW | RI |Attribute|
  80. * +-----------------------------------------+
  81. * pte_none | MBZ | 01 | 11 | 00 |
  82. * +-----------------------------------------+
  83. * present | PPN | 0 | 00 | ADW | RI | CA | wx |
  84. * +- - - - - - - - - - - - - - - - - - - - -+
  85. * (PAGE_NONE)| PPN | 0 | 00 | ADW | 01 | 11 | 11 |
  86. * +-----------------------------------------+
  87. * swap | index | type | 01 | 11 | 00 |
  88. * +-----------------------------------------+
  89. *
  90. * For T1050 hardware and earlier the layout differs for present and (PAGE_NONE)
  91. * +-----------------------------------------+
  92. * present | PPN | 0 | 00 | ADW | RI | CA | w1 |
  93. * +-----------------------------------------+
  94. * (PAGE_NONE)| PPN | 0 | 00 | ADW | 01 | 01 | 00 |
  95. * +-----------------------------------------+
  96. *
  97. * Legend:
  98. * PPN Physical Page Number
  99. * ADW software: accessed (young) / dirty / writable
  100. * RI ring (0=privileged, 1=user, 2 and 3 are unused)
  101. * CA cache attribute: 00 bypass, 01 writeback, 10 writethrough
  102. * (11 is invalid and used to mark pages that are not present)
  103. * w page is writable (hw)
  104. * x page is executable (hw)
  105. * index swap offset / PAGE_SIZE (bit 11-31: 21 bits -> 8 GB)
  106. * (note that the index is always non-zero)
  107. * type swap type (5 bits -> 32 types)
  108. *
  109. * Notes:
  110. * - (PROT_NONE) is a special case of 'present' but causes an exception for
  111. * any access (read, write, and execute).
  112. * - 'multihit-exception' has the highest priority of all MMU exceptions,
  113. * so the ring must be set to 'RING_USER' even for 'non-present' pages.
  114. * - on older hardware, the exectuable flag was not supported and
  115. * used as a 'valid' flag, so it needs to be always set.
  116. * - we need to keep track of certain flags in software (dirty and young)
  117. * to do this, we use write exceptions and have a separate software w-flag.
  118. * - attribute value 1101 (and 1111 on T1050 and earlier) is reserved
  119. */
  120. #define _PAGE_ATTRIB_MASK 0xf
  121. #define _PAGE_HW_EXEC (1<<0) /* hardware: page is executable */
  122. #define _PAGE_HW_WRITE (1<<1) /* hardware: page is writable */
  123. #define _PAGE_CA_BYPASS (0<<2) /* bypass, non-speculative */
  124. #define _PAGE_CA_WB (1<<2) /* write-back */
  125. #define _PAGE_CA_WT (2<<2) /* write-through */
  126. #define _PAGE_CA_MASK (3<<2)
  127. #define _PAGE_CA_INVALID (3<<2)
  128. /* We use invalid attribute values to distinguish special pte entries */
  129. #if XCHAL_HW_VERSION_MAJOR < 2000
  130. #define _PAGE_HW_VALID 0x01 /* older HW needed this bit set */
  131. #define _PAGE_NONE 0x04
  132. #else
  133. #define _PAGE_HW_VALID 0x00
  134. #define _PAGE_NONE 0x0f
  135. #endif
  136. #define _PAGE_USER (1<<4) /* user access (ring=1) */
  137. /* Software */
  138. #define _PAGE_WRITABLE_BIT 6
  139. #define _PAGE_WRITABLE (1<<6) /* software: page writable */
  140. #define _PAGE_DIRTY (1<<7) /* software: page dirty */
  141. #define _PAGE_ACCESSED (1<<8) /* software: page accessed (read) */
  142. #ifdef CONFIG_MMU
  143. #define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
  144. #define _PAGE_PRESENT (_PAGE_HW_VALID | _PAGE_CA_WB | _PAGE_ACCESSED)
  145. #define PAGE_NONE __pgprot(_PAGE_NONE | _PAGE_USER)
  146. #define PAGE_COPY __pgprot(_PAGE_PRESENT | _PAGE_USER)
  147. #define PAGE_COPY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
  148. #define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER)
  149. #define PAGE_READONLY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
  150. #define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE)
  151. #define PAGE_SHARED_EXEC \
  152. __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE | _PAGE_HW_EXEC)
  153. #define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_HW_WRITE)
  154. #define PAGE_KERNEL_RO __pgprot(_PAGE_PRESENT)
  155. #define PAGE_KERNEL_EXEC __pgprot(_PAGE_PRESENT|_PAGE_HW_WRITE|_PAGE_HW_EXEC)
  156. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  157. # define _PAGE_DIRECTORY (_PAGE_HW_VALID | _PAGE_ACCESSED | _PAGE_CA_BYPASS)
  158. #else
  159. # define _PAGE_DIRECTORY (_PAGE_HW_VALID | _PAGE_ACCESSED | _PAGE_CA_WB)
  160. #endif
  161. #else /* no mmu */
  162. # define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
  163. # define PAGE_NONE __pgprot(0)
  164. # define PAGE_SHARED __pgprot(0)
  165. # define PAGE_COPY __pgprot(0)
  166. # define PAGE_READONLY __pgprot(0)
  167. # define PAGE_KERNEL __pgprot(0)
  168. #endif
  169. /*
  170. * On certain configurations of Xtensa MMUs (eg. the initial Linux config),
  171. * the MMU can't do page protection for execute, and considers that the same as
  172. * read. Also, write permissions may imply read permissions.
  173. * What follows is the closest we can get by reasonable means..
  174. * See linux/mm/mmap.c for protection_map[] array that uses these definitions.
  175. */
  176. #ifndef __ASSEMBLY__
  177. #define pte_ERROR(e) \
  178. printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
  179. #define pgd_ERROR(e) \
  180. printk("%s:%d: bad pgd entry %08lx.\n", __FILE__, __LINE__, pgd_val(e))
  181. extern unsigned long empty_zero_page[1024];
  182. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  183. #ifdef CONFIG_MMU
  184. extern pgd_t swapper_pg_dir[PAGE_SIZE/sizeof(pgd_t)];
  185. extern void paging_init(void);
  186. #else
  187. # define swapper_pg_dir NULL
  188. static inline void paging_init(void) { }
  189. #endif
  190. /*
  191. * The pmd contains the kernel virtual address of the pte page.
  192. */
  193. #define pmd_page_vaddr(pmd) ((unsigned long)(pmd_val(pmd) & PAGE_MASK))
  194. #define pmd_pfn(pmd) (__pa(pmd_val(pmd)) >> PAGE_SHIFT)
  195. #define pmd_page(pmd) virt_to_page(pmd_val(pmd))
  196. /*
  197. * pte status.
  198. */
  199. # define pte_none(pte) (pte_val(pte) == (_PAGE_CA_INVALID | _PAGE_USER))
  200. #if XCHAL_HW_VERSION_MAJOR < 2000
  201. # define pte_present(pte) ((pte_val(pte) & _PAGE_CA_MASK) != _PAGE_CA_INVALID)
  202. #else
  203. # define pte_present(pte) \
  204. (((pte_val(pte) & _PAGE_CA_MASK) != _PAGE_CA_INVALID) \
  205. || ((pte_val(pte) & _PAGE_ATTRIB_MASK) == _PAGE_NONE))
  206. #endif
  207. #define pte_clear(mm,addr,ptep) \
  208. do { update_pte(ptep, __pte(_PAGE_CA_INVALID | _PAGE_USER)); } while (0)
  209. #define pmd_none(pmd) (!pmd_val(pmd))
  210. #define pmd_present(pmd) (pmd_val(pmd) & PAGE_MASK)
  211. #define pmd_bad(pmd) (pmd_val(pmd) & ~PAGE_MASK)
  212. #define pmd_clear(pmdp) do { set_pmd(pmdp, __pmd(0)); } while (0)
  213. static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_WRITABLE; }
  214. static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
  215. static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
  216. static inline pte_t pte_wrprotect(pte_t pte)
  217. { pte_val(pte) &= ~(_PAGE_WRITABLE | _PAGE_HW_WRITE); return pte; }
  218. static inline pte_t pte_mkclean(pte_t pte)
  219. { pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_HW_WRITE); return pte; }
  220. static inline pte_t pte_mkold(pte_t pte)
  221. { pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
  222. static inline pte_t pte_mkdirty(pte_t pte)
  223. { pte_val(pte) |= _PAGE_DIRTY; return pte; }
  224. static inline pte_t pte_mkyoung(pte_t pte)
  225. { pte_val(pte) |= _PAGE_ACCESSED; return pte; }
  226. static inline pte_t pte_mkwrite(pte_t pte)
  227. { pte_val(pte) |= _PAGE_WRITABLE; return pte; }
  228. #define pgprot_noncached(prot) \
  229. ((__pgprot((pgprot_val(prot) & ~_PAGE_CA_MASK) | \
  230. _PAGE_CA_BYPASS)))
  231. /*
  232. * Conversion functions: convert a page and protection to a page entry,
  233. * and a page entry and page directory to the page they refer to.
  234. */
  235. #define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
  236. #define pte_same(a,b) (pte_val(a) == pte_val(b))
  237. #define pte_page(x) pfn_to_page(pte_pfn(x))
  238. #define pfn_pte(pfn, prot) __pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot))
  239. #define mk_pte(page, prot) pfn_pte(page_to_pfn(page), prot)
  240. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  241. {
  242. return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
  243. }
  244. /*
  245. * Certain architectures need to do special things when pte's
  246. * within a page table are directly modified. Thus, the following
  247. * hook is made available.
  248. */
  249. static inline void update_pte(pte_t *ptep, pte_t pteval)
  250. {
  251. *ptep = pteval;
  252. #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
  253. __asm__ __volatile__ ("dhwb %0, 0" :: "a" (ptep));
  254. #endif
  255. }
  256. struct mm_struct;
  257. static inline void
  258. set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pteval)
  259. {
  260. update_pte(ptep, pteval);
  261. }
  262. static inline void set_pte(pte_t *ptep, pte_t pteval)
  263. {
  264. update_pte(ptep, pteval);
  265. }
  266. static inline void
  267. set_pmd(pmd_t *pmdp, pmd_t pmdval)
  268. {
  269. *pmdp = pmdval;
  270. }
  271. struct vm_area_struct;
  272. static inline int
  273. ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr,
  274. pte_t *ptep)
  275. {
  276. pte_t pte = *ptep;
  277. if (!pte_young(pte))
  278. return 0;
  279. update_pte(ptep, pte_mkold(pte));
  280. return 1;
  281. }
  282. static inline pte_t
  283. ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  284. {
  285. pte_t pte = *ptep;
  286. pte_clear(mm, addr, ptep);
  287. return pte;
  288. }
  289. static inline void
  290. ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  291. {
  292. pte_t pte = *ptep;
  293. update_pte(ptep, pte_wrprotect(pte));
  294. }
  295. /*
  296. * Encode and decode a swap and file entry.
  297. */
  298. #define SWP_TYPE_BITS 5
  299. #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS)
  300. #define __swp_type(entry) (((entry).val >> 6) & 0x1f)
  301. #define __swp_offset(entry) ((entry).val >> 11)
  302. #define __swp_entry(type,offs) \
  303. ((swp_entry_t){((type) << 6) | ((offs) << 11) | \
  304. _PAGE_CA_INVALID | _PAGE_USER})
  305. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  306. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  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. typedef pte_t *pte_addr_t;
  334. void update_mmu_tlb(struct vm_area_struct *vma,
  335. unsigned long address, pte_t *ptep);
  336. #define __HAVE_ARCH_UPDATE_MMU_TLB
  337. #endif /* !defined (__ASSEMBLY__) */
  338. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  339. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  340. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  341. #define __HAVE_ARCH_PTEP_MKDIRTY
  342. #define __HAVE_ARCH_PTE_SAME
  343. /* We provide our own get_unmapped_area to cope with
  344. * SHM area cache aliasing for userland.
  345. */
  346. #define HAVE_ARCH_UNMAPPED_AREA
  347. #endif /* _XTENSA_PGTABLE_H */