/arch/unicore32/include/asm/pgtable.h

http://github.com/mirrors/linux · C Header · 287 lines · 159 code · 48 blank · 80 comment · 5 complexity · 105b56a7221e8ed49eed0eeaf495a7b6 MD5 · raw file

  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * linux/arch/unicore32/include/asm/pgtable.h
  4. *
  5. * Code specific to PKUnity SoC and UniCore ISA
  6. *
  7. * Copyright (C) 2001-2010 GUAN Xue-tao
  8. */
  9. #ifndef __UNICORE_PGTABLE_H__
  10. #define __UNICORE_PGTABLE_H__
  11. #define __ARCH_USE_5LEVEL_HACK
  12. #include <asm-generic/pgtable-nopmd.h>
  13. #include <asm/cpu-single.h>
  14. #include <asm/memory.h>
  15. #include <asm/pgtable-hwdef.h>
  16. /*
  17. * Just any arbitrary offset to the start of the vmalloc VM area: the
  18. * current 8MB value just means that there will be a 8MB "hole" after the
  19. * physical memory until the kernel virtual memory starts. That means that
  20. * any out-of-bounds memory accesses will hopefully be caught.
  21. * The vmalloc() routines leaves a hole of 4kB between each vmalloced
  22. * area for the same reason. ;)
  23. *
  24. * Note that platforms may override VMALLOC_START, but they must provide
  25. * VMALLOC_END. VMALLOC_END defines the (exclusive) limit of this space,
  26. * which may not overlap IO space.
  27. */
  28. #ifndef VMALLOC_START
  29. #define VMALLOC_OFFSET SZ_8M
  30. #define VMALLOC_START (((unsigned long)high_memory + VMALLOC_OFFSET) \
  31. & ~(VMALLOC_OFFSET-1))
  32. #define VMALLOC_END (0xff000000UL)
  33. #endif
  34. #define PTRS_PER_PTE 1024
  35. #define PTRS_PER_PGD 1024
  36. /*
  37. * PGDIR_SHIFT determines what a third-level page table entry can map
  38. */
  39. #define PGDIR_SHIFT 22
  40. #ifndef __ASSEMBLY__
  41. extern void __pte_error(const char *file, int line, unsigned long val);
  42. extern void __pgd_error(const char *file, int line, unsigned long val);
  43. #define pte_ERROR(pte) __pte_error(__FILE__, __LINE__, pte_val(pte))
  44. #define pgd_ERROR(pgd) __pgd_error(__FILE__, __LINE__, pgd_val(pgd))
  45. #endif /* !__ASSEMBLY__ */
  46. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  47. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  48. /*
  49. * This is the lowest virtual address we can permit any user space
  50. * mapping to be mapped at. This is particularly important for
  51. * non-high vector CPUs.
  52. */
  53. #define FIRST_USER_ADDRESS PAGE_SIZE
  54. #define FIRST_USER_PGD_NR 1
  55. #define USER_PTRS_PER_PGD ((TASK_SIZE/PGDIR_SIZE) - FIRST_USER_PGD_NR)
  56. /*
  57. * section address mask and size definitions.
  58. */
  59. #define SECTION_SHIFT 22
  60. #define SECTION_SIZE (1UL << SECTION_SHIFT)
  61. #define SECTION_MASK (~(SECTION_SIZE-1))
  62. #ifndef __ASSEMBLY__
  63. /*
  64. * The pgprot_* and protection_map entries will be fixed up in runtime
  65. * to include the cachable bits based on memory policy, as well as any
  66. * architecture dependent bits.
  67. */
  68. #define _PTE_DEFAULT (PTE_PRESENT | PTE_YOUNG | PTE_CACHEABLE)
  69. extern pgprot_t pgprot_user;
  70. extern pgprot_t pgprot_kernel;
  71. #define PAGE_NONE pgprot_user
  72. #define PAGE_SHARED __pgprot(pgprot_val(pgprot_user | PTE_READ \
  73. | PTE_WRITE))
  74. #define PAGE_SHARED_EXEC __pgprot(pgprot_val(pgprot_user | PTE_READ \
  75. | PTE_WRITE \
  76. | PTE_EXEC))
  77. #define PAGE_COPY __pgprot(pgprot_val(pgprot_user | PTE_READ)
  78. #define PAGE_COPY_EXEC __pgprot(pgprot_val(pgprot_user | PTE_READ \
  79. | PTE_EXEC))
  80. #define PAGE_READONLY __pgprot(pgprot_val(pgprot_user | PTE_READ))
  81. #define PAGE_READONLY_EXEC __pgprot(pgprot_val(pgprot_user | PTE_READ \
  82. | PTE_EXEC))
  83. #define PAGE_KERNEL pgprot_kernel
  84. #define PAGE_KERNEL_EXEC __pgprot(pgprot_val(pgprot_kernel | PTE_EXEC))
  85. #define __PAGE_NONE __pgprot(_PTE_DEFAULT)
  86. #define __PAGE_SHARED __pgprot(_PTE_DEFAULT | PTE_READ \
  87. | PTE_WRITE)
  88. #define __PAGE_SHARED_EXEC __pgprot(_PTE_DEFAULT | PTE_READ \
  89. | PTE_WRITE \
  90. | PTE_EXEC)
  91. #define __PAGE_COPY __pgprot(_PTE_DEFAULT | PTE_READ)
  92. #define __PAGE_COPY_EXEC __pgprot(_PTE_DEFAULT | PTE_READ \
  93. | PTE_EXEC)
  94. #define __PAGE_READONLY __pgprot(_PTE_DEFAULT | PTE_READ)
  95. #define __PAGE_READONLY_EXEC __pgprot(_PTE_DEFAULT | PTE_READ \
  96. | PTE_EXEC)
  97. #endif /* __ASSEMBLY__ */
  98. /*
  99. * The table below defines the page protection levels that we insert into our
  100. * Linux page table version. These get translated into the best that the
  101. * architecture can perform. Note that on UniCore hardware:
  102. * 1) We cannot do execute protection
  103. * 2) If we could do execute protection, then read is implied
  104. * 3) write implies read permissions
  105. */
  106. #define __P000 __PAGE_NONE
  107. #define __P001 __PAGE_READONLY
  108. #define __P010 __PAGE_COPY
  109. #define __P011 __PAGE_COPY
  110. #define __P100 __PAGE_READONLY_EXEC
  111. #define __P101 __PAGE_READONLY_EXEC
  112. #define __P110 __PAGE_COPY_EXEC
  113. #define __P111 __PAGE_COPY_EXEC
  114. #define __S000 __PAGE_NONE
  115. #define __S001 __PAGE_READONLY
  116. #define __S010 __PAGE_SHARED
  117. #define __S011 __PAGE_SHARED
  118. #define __S100 __PAGE_READONLY_EXEC
  119. #define __S101 __PAGE_READONLY_EXEC
  120. #define __S110 __PAGE_SHARED_EXEC
  121. #define __S111 __PAGE_SHARED_EXEC
  122. #ifndef __ASSEMBLY__
  123. /*
  124. * ZERO_PAGE is a global shared page that is always zero: used
  125. * for zero-mapped memory areas etc..
  126. */
  127. extern struct page *empty_zero_page;
  128. #define ZERO_PAGE(vaddr) (empty_zero_page)
  129. #define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)
  130. #define pfn_pte(pfn, prot) (__pte(((pfn) << PAGE_SHIFT) \
  131. | pgprot_val(prot)))
  132. #define pte_none(pte) (!pte_val(pte))
  133. #define pte_clear(mm, addr, ptep) set_pte(ptep, __pte(0))
  134. #define pte_page(pte) (pfn_to_page(pte_pfn(pte)))
  135. #define pte_offset_kernel(dir, addr) (pmd_page_vaddr(*(dir)) \
  136. + __pte_index(addr))
  137. #define pte_offset_map(dir, addr) (pmd_page_vaddr(*(dir)) \
  138. + __pte_index(addr))
  139. #define pte_unmap(pte) do { } while (0)
  140. #define set_pte(ptep, pte) cpu_set_pte(ptep, pte)
  141. #define set_pte_at(mm, addr, ptep, pteval) \
  142. do { \
  143. set_pte(ptep, pteval); \
  144. } while (0)
  145. /*
  146. * The following only work if pte_present() is true.
  147. * Undefined behaviour if not..
  148. */
  149. #define pte_present(pte) (pte_val(pte) & PTE_PRESENT)
  150. #define pte_write(pte) (pte_val(pte) & PTE_WRITE)
  151. #define pte_dirty(pte) (pte_val(pte) & PTE_DIRTY)
  152. #define pte_young(pte) (pte_val(pte) & PTE_YOUNG)
  153. #define pte_exec(pte) (pte_val(pte) & PTE_EXEC)
  154. #define PTE_BIT_FUNC(fn, op) \
  155. static inline pte_t pte_##fn(pte_t pte) { pte_val(pte) op; return pte; }
  156. PTE_BIT_FUNC(wrprotect, &= ~PTE_WRITE);
  157. PTE_BIT_FUNC(mkwrite, |= PTE_WRITE);
  158. PTE_BIT_FUNC(mkclean, &= ~PTE_DIRTY);
  159. PTE_BIT_FUNC(mkdirty, |= PTE_DIRTY);
  160. PTE_BIT_FUNC(mkold, &= ~PTE_YOUNG);
  161. PTE_BIT_FUNC(mkyoung, |= PTE_YOUNG);
  162. /*
  163. * Mark the prot value as uncacheable.
  164. */
  165. #define pgprot_noncached(prot) \
  166. __pgprot(pgprot_val(prot) & ~PTE_CACHEABLE)
  167. #define pgprot_writecombine(prot) \
  168. __pgprot(pgprot_val(prot) & ~PTE_CACHEABLE)
  169. #define pmd_none(pmd) (!pmd_val(pmd))
  170. #define pmd_present(pmd) (pmd_val(pmd) & PMD_PRESENT)
  171. #define pmd_bad(pmd) (((pmd_val(pmd) & \
  172. (PMD_PRESENT | PMD_TYPE_MASK)) \
  173. != (PMD_PRESENT | PMD_TYPE_TABLE)))
  174. #define set_pmd(pmdpd, pmdval) \
  175. do { \
  176. *(pmdpd) = pmdval; \
  177. } while (0)
  178. #define pmd_clear(pmdp) \
  179. do { \
  180. set_pmd(pmdp, __pmd(0));\
  181. clean_pmd_entry(pmdp); \
  182. } while (0)
  183. #define pmd_page_vaddr(pmd) ((pte_t *)__va(pmd_val(pmd) & PAGE_MASK))
  184. #define pmd_page(pmd) pfn_to_page(__phys_to_pfn(pmd_val(pmd)))
  185. /*
  186. * Conversion functions: convert a page and protection to a page entry,
  187. * and a page entry and page directory to the page they refer to.
  188. */
  189. #define mk_pte(page, prot) pfn_pte(page_to_pfn(page), prot)
  190. /* to find an entry in a page-table-directory */
  191. #define pgd_index(addr) ((addr) >> PGDIR_SHIFT)
  192. #define pgd_offset(mm, addr) ((mm)->pgd+pgd_index(addr))
  193. /* to find an entry in a kernel page-table-directory */
  194. #define pgd_offset_k(addr) pgd_offset(&init_mm, addr)
  195. /* Find an entry in the third-level page table.. */
  196. #define __pte_index(addr) (((addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  197. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  198. {
  199. const unsigned long mask = PTE_EXEC | PTE_WRITE | PTE_READ;
  200. pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask);
  201. return pte;
  202. }
  203. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  204. /*
  205. * Encode and decode a swap entry. Swap entries are stored in the Linux
  206. * page tables as follows:
  207. *
  208. * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  209. * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  210. * <--------------- offset --------------> <--- type --> 0 0 0 0 0
  211. *
  212. * This gives us up to 127 swap files and 32GB per swap file. Note that
  213. * the offset field is always non-zero.
  214. */
  215. #define __SWP_TYPE_SHIFT 5
  216. #define __SWP_TYPE_BITS 7
  217. #define __SWP_TYPE_MASK ((1 << __SWP_TYPE_BITS) - 1)
  218. #define __SWP_OFFSET_SHIFT (__SWP_TYPE_BITS + __SWP_TYPE_SHIFT)
  219. #define __swp_type(x) (((x).val >> __SWP_TYPE_SHIFT) \
  220. & __SWP_TYPE_MASK)
  221. #define __swp_offset(x) ((x).val >> __SWP_OFFSET_SHIFT)
  222. #define __swp_entry(type, offset) ((swp_entry_t) { \
  223. ((type) << __SWP_TYPE_SHIFT) | \
  224. ((offset) << __SWP_OFFSET_SHIFT) })
  225. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  226. #define __swp_entry_to_pte(swp) ((pte_t) { (swp).val })
  227. /*
  228. * It is an error for the kernel to have more swap files than we can
  229. * encode in the PTEs. This ensures that we know when MAX_SWAPFILES
  230. * is increased beyond what we presently support.
  231. */
  232. #define MAX_SWAPFILES_CHECK() \
  233. BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > __SWP_TYPE_BITS)
  234. /* Needs to be defined here and not in linux/mm.h, as it is arch dependent */
  235. /* FIXME: this is not correct */
  236. #define kern_addr_valid(addr) (1)
  237. #include <asm-generic/pgtable.h>
  238. #endif /* !__ASSEMBLY__ */
  239. #endif /* __UNICORE_PGTABLE_H__ */