pgtable.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_M32R_PGTABLE_H
  3. #define _ASM_M32R_PGTABLE_H
  4. #include <asm-generic/4level-fixup.h>
  5. #ifdef __KERNEL__
  6. /*
  7. * The Linux memory management assumes a three-level page table setup. On
  8. * the M32R, we use that, but "fold" the mid level into the top-level page
  9. * table, so that we physically have the same two-level page table as the
  10. * M32R mmu expects.
  11. *
  12. * This file contains the functions and defines necessary to modify and use
  13. * the M32R page table tree.
  14. */
  15. /* CAUTION!: If you change macro definitions in this file, you might have to
  16. * change arch/m32r/mmu.S manually.
  17. */
  18. #ifndef __ASSEMBLY__
  19. #include <linux/threads.h>
  20. #include <linux/bitops.h>
  21. #include <asm/processor.h>
  22. #include <asm/addrspace.h>
  23. #include <asm/page.h>
  24. struct mm_struct;
  25. struct vm_area_struct;
  26. extern pgd_t swapper_pg_dir[1024];
  27. extern void paging_init(void);
  28. /*
  29. * ZERO_PAGE is a global shared page that is always zero: used
  30. * for zero-mapped memory areas etc..
  31. */
  32. extern unsigned long empty_zero_page[1024];
  33. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  34. #endif /* !__ASSEMBLY__ */
  35. #ifndef __ASSEMBLY__
  36. #include <asm/pgtable-2level.h>
  37. #endif
  38. #define pgtable_cache_init() do { } while (0)
  39. #define PMD_SIZE (1UL << PMD_SHIFT)
  40. #define PMD_MASK (~(PMD_SIZE - 1))
  41. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  42. #define PGDIR_MASK (~(PGDIR_SIZE - 1))
  43. #define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE)
  44. #define FIRST_USER_ADDRESS 0UL
  45. #ifndef __ASSEMBLY__
  46. /* Just any arbitrary offset to the start of the vmalloc VM area: the
  47. * current 8MB value just means that there will be a 8MB "hole" after the
  48. * physical memory until the kernel virtual memory starts. That means that
  49. * any out-of-bounds memory accesses will hopefully be caught.
  50. * The vmalloc() routines leaves a hole of 4kB between each vmalloced
  51. * area for the same reason. ;)
  52. */
  53. #define VMALLOC_START KSEG2
  54. #define VMALLOC_END KSEG3
  55. /*
  56. * M32R TLB format
  57. *
  58. * [0] [1:19] [20:23] [24:31]
  59. * +-----------------------+----+-------------+
  60. * | VPN |0000| ASID |
  61. * +-----------------------+----+-------------+
  62. * +-+---------------------+----+-+---+-+-+-+-+
  63. * |0 PPN |0000|N|AC |L|G|V| |
  64. * +-+---------------------+----+-+---+-+-+-+-+
  65. * RWX
  66. */
  67. #define _PAGE_BIT_DIRTY 0 /* software: page changed */
  68. #define _PAGE_BIT_PRESENT 1 /* Valid: page is valid */
  69. #define _PAGE_BIT_GLOBAL 2 /* Global */
  70. #define _PAGE_BIT_LARGE 3 /* Large */
  71. #define _PAGE_BIT_EXEC 4 /* Execute */
  72. #define _PAGE_BIT_WRITE 5 /* Write */
  73. #define _PAGE_BIT_READ 6 /* Read */
  74. #define _PAGE_BIT_NONCACHABLE 7 /* Non cachable */
  75. #define _PAGE_BIT_ACCESSED 8 /* software: page referenced */
  76. #define _PAGE_BIT_PROTNONE 9 /* software: if not present */
  77. #define _PAGE_DIRTY (1UL << _PAGE_BIT_DIRTY)
  78. #define _PAGE_PRESENT (1UL << _PAGE_BIT_PRESENT)
  79. #define _PAGE_GLOBAL (1UL << _PAGE_BIT_GLOBAL)
  80. #define _PAGE_LARGE (1UL << _PAGE_BIT_LARGE)
  81. #define _PAGE_EXEC (1UL << _PAGE_BIT_EXEC)
  82. #define _PAGE_WRITE (1UL << _PAGE_BIT_WRITE)
  83. #define _PAGE_READ (1UL << _PAGE_BIT_READ)
  84. #define _PAGE_NONCACHABLE (1UL << _PAGE_BIT_NONCACHABLE)
  85. #define _PAGE_ACCESSED (1UL << _PAGE_BIT_ACCESSED)
  86. #define _PAGE_PROTNONE (1UL << _PAGE_BIT_PROTNONE)
  87. #define _PAGE_TABLE \
  88. ( _PAGE_PRESENT | _PAGE_WRITE | _PAGE_READ | _PAGE_ACCESSED \
  89. | _PAGE_DIRTY )
  90. #define _KERNPG_TABLE \
  91. ( _PAGE_PRESENT | _PAGE_WRITE | _PAGE_READ | _PAGE_ACCESSED \
  92. | _PAGE_DIRTY )
  93. #define _PAGE_CHG_MASK \
  94. ( PTE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY )
  95. #ifdef CONFIG_MMU
  96. #define PAGE_NONE \
  97. __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
  98. #define PAGE_SHARED \
  99. __pgprot(_PAGE_PRESENT | _PAGE_WRITE | _PAGE_READ | _PAGE_ACCESSED)
  100. #define PAGE_SHARED_EXEC \
  101. __pgprot(_PAGE_PRESENT | _PAGE_EXEC | _PAGE_WRITE | _PAGE_READ \
  102. | _PAGE_ACCESSED)
  103. #define PAGE_COPY \
  104. __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_ACCESSED)
  105. #define PAGE_COPY_EXEC \
  106. __pgprot(_PAGE_PRESENT | _PAGE_EXEC | _PAGE_READ | _PAGE_ACCESSED)
  107. #define PAGE_READONLY \
  108. __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_ACCESSED)
  109. #define PAGE_READONLY_EXEC \
  110. __pgprot(_PAGE_PRESENT | _PAGE_EXEC | _PAGE_READ | _PAGE_ACCESSED)
  111. #define __PAGE_KERNEL \
  112. ( _PAGE_PRESENT | _PAGE_EXEC | _PAGE_WRITE | _PAGE_READ | _PAGE_DIRTY \
  113. | _PAGE_ACCESSED )
  114. #define __PAGE_KERNEL_RO ( __PAGE_KERNEL & ~_PAGE_WRITE )
  115. #define __PAGE_KERNEL_NOCACHE ( __PAGE_KERNEL | _PAGE_NONCACHABLE)
  116. #define MAKE_GLOBAL(x) __pgprot((x) | _PAGE_GLOBAL)
  117. #define PAGE_KERNEL MAKE_GLOBAL(__PAGE_KERNEL)
  118. #define PAGE_KERNEL_RO MAKE_GLOBAL(__PAGE_KERNEL_RO)
  119. #define PAGE_KERNEL_NOCACHE MAKE_GLOBAL(__PAGE_KERNEL_NOCACHE)
  120. #else
  121. #define PAGE_NONE __pgprot(0)
  122. #define PAGE_SHARED __pgprot(0)
  123. #define PAGE_SHARED_EXEC __pgprot(0)
  124. #define PAGE_COPY __pgprot(0)
  125. #define PAGE_COPY_EXEC __pgprot(0)
  126. #define PAGE_READONLY __pgprot(0)
  127. #define PAGE_READONLY_EXEC __pgprot(0)
  128. #define PAGE_KERNEL __pgprot(0)
  129. #define PAGE_KERNEL_RO __pgprot(0)
  130. #define PAGE_KERNEL_NOCACHE __pgprot(0)
  131. #endif /* CONFIG_MMU */
  132. /* xwr */
  133. #define __P000 PAGE_NONE
  134. #define __P001 PAGE_READONLY
  135. #define __P010 PAGE_COPY
  136. #define __P011 PAGE_COPY
  137. #define __P100 PAGE_READONLY_EXEC
  138. #define __P101 PAGE_READONLY_EXEC
  139. #define __P110 PAGE_COPY_EXEC
  140. #define __P111 PAGE_COPY_EXEC
  141. #define __S000 PAGE_NONE
  142. #define __S001 PAGE_READONLY
  143. #define __S010 PAGE_SHARED
  144. #define __S011 PAGE_SHARED
  145. #define __S100 PAGE_READONLY_EXEC
  146. #define __S101 PAGE_READONLY_EXEC
  147. #define __S110 PAGE_SHARED_EXEC
  148. #define __S111 PAGE_SHARED_EXEC
  149. /* page table for 0-4MB for everybody */
  150. #define pte_present(x) (pte_val(x) & (_PAGE_PRESENT | _PAGE_PROTNONE))
  151. #define pte_clear(mm,addr,xp) do { set_pte_at(mm, addr, xp, __pte(0)); } while (0)
  152. #define pmd_none(x) (!pmd_val(x))
  153. #define pmd_present(x) (pmd_val(x) & _PAGE_PRESENT)
  154. #define pmd_clear(xp) do { set_pmd(xp, __pmd(0)); } while (0)
  155. #define pmd_bad(x) ((pmd_val(x) & ~PAGE_MASK) != _KERNPG_TABLE)
  156. #define pages_to_mb(x) ((x) >> (20 - PAGE_SHIFT))
  157. /*
  158. * The following only work if pte_present() is true.
  159. * Undefined behaviour if not..
  160. */
  161. static inline int pte_dirty(pte_t pte)
  162. {
  163. return pte_val(pte) & _PAGE_DIRTY;
  164. }
  165. static inline int pte_young(pte_t pte)
  166. {
  167. return pte_val(pte) & _PAGE_ACCESSED;
  168. }
  169. static inline int pte_write(pte_t pte)
  170. {
  171. return pte_val(pte) & _PAGE_WRITE;
  172. }
  173. static inline int pte_special(pte_t pte)
  174. {
  175. return 0;
  176. }
  177. static inline pte_t pte_mkclean(pte_t pte)
  178. {
  179. pte_val(pte) &= ~_PAGE_DIRTY;
  180. return pte;
  181. }
  182. static inline pte_t pte_mkold(pte_t pte)
  183. {
  184. pte_val(pte) &= ~_PAGE_ACCESSED;
  185. return pte;
  186. }
  187. static inline pte_t pte_wrprotect(pte_t pte)
  188. {
  189. pte_val(pte) &= ~_PAGE_WRITE;
  190. return pte;
  191. }
  192. static inline pte_t pte_mkdirty(pte_t pte)
  193. {
  194. pte_val(pte) |= _PAGE_DIRTY;
  195. return pte;
  196. }
  197. static inline pte_t pte_mkyoung(pte_t pte)
  198. {
  199. pte_val(pte) |= _PAGE_ACCESSED;
  200. return pte;
  201. }
  202. static inline pte_t pte_mkwrite(pte_t pte)
  203. {
  204. pte_val(pte) |= _PAGE_WRITE;
  205. return pte;
  206. }
  207. static inline pte_t pte_mkspecial(pte_t pte)
  208. {
  209. return pte;
  210. }
  211. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
  212. {
  213. return test_and_clear_bit(_PAGE_BIT_ACCESSED, ptep);
  214. }
  215. static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  216. {
  217. clear_bit(_PAGE_BIT_WRITE, ptep);
  218. }
  219. /*
  220. * Macro and implementation to make a page protection as uncachable.
  221. */
  222. static inline pgprot_t pgprot_noncached(pgprot_t _prot)
  223. {
  224. unsigned long prot = pgprot_val(_prot);
  225. prot |= _PAGE_NONCACHABLE;
  226. return __pgprot(prot);
  227. }
  228. #define pgprot_writecombine(prot) pgprot_noncached(prot)
  229. /*
  230. * Conversion functions: convert a page and protection to a page entry,
  231. * and a page entry and page directory to the page they refer to.
  232. */
  233. #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), pgprot)
  234. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  235. {
  236. set_pte(&pte, __pte((pte_val(pte) & _PAGE_CHG_MASK) \
  237. | pgprot_val(newprot)));
  238. return pte;
  239. }
  240. /*
  241. * Conversion functions: convert a page and protection to a page entry,
  242. * and a page entry and page directory to the page they refer to.
  243. */
  244. static inline void pmd_set(pmd_t * pmdp, pte_t * ptep)
  245. {
  246. pmd_val(*pmdp) = (((unsigned long) ptep) & PAGE_MASK);
  247. }
  248. #define pmd_page_vaddr(pmd) \
  249. ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
  250. #ifndef CONFIG_DISCONTIGMEM
  251. #define pmd_page(pmd) (mem_map + ((pmd_val(pmd) >> PAGE_SHIFT) - PFN_BASE))
  252. #endif /* !CONFIG_DISCONTIGMEM */
  253. /* to find an entry in a page-table-directory. */
  254. #define pgd_index(address) \
  255. (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
  256. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
  257. /* to find an entry in a kernel page-table-directory */
  258. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  259. #define pmd_index(address) \
  260. (((address) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))
  261. #define pte_index(address) \
  262. (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  263. #define pte_offset_kernel(dir, address) \
  264. ((pte_t *)pmd_page_vaddr(*(dir)) + pte_index(address))
  265. #define pte_offset_map(dir, address) \
  266. ((pte_t *)page_address(pmd_page(*(dir))) + pte_index(address))
  267. #define pte_unmap(pte) do { } while (0)
  268. /* Encode and de-code a swap entry */
  269. #define __swp_type(x) (((x).val >> 2) & 0x1f)
  270. #define __swp_offset(x) ((x).val >> 10)
  271. #define __swp_entry(type, offset) \
  272. ((swp_entry_t) { ((type) << 2) | ((offset) << 10) })
  273. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  274. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  275. #endif /* !__ASSEMBLY__ */
  276. /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
  277. #define kern_addr_valid(addr) (1)
  278. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  279. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  280. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  281. #define __HAVE_ARCH_PTE_SAME
  282. #include <asm-generic/pgtable.h>
  283. #endif /* __KERNEL__ */
  284. #endif /* _ASM_M32R_PGTABLE_H */