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