pgtable.h 9.4 KB

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