pgtable.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * CRIS pgtable.h - macros and functions to manipulate page tables.
  3. */
  4. #ifndef _CRIS_PGTABLE_H
  5. #define _CRIS_PGTABLE_H
  6. #include <asm/page.h>
  7. #include <asm-generic/pgtable-nopmd.h>
  8. #ifndef __ASSEMBLY__
  9. #include <linux/sched.h>
  10. #include <asm/mmu.h>
  11. #endif
  12. #include <arch/pgtable.h>
  13. /*
  14. * The Linux memory management assumes a three-level page table setup. On
  15. * CRIS, we use that, but "fold" the mid level into the top-level page
  16. * table. Since the MMU TLB is software loaded through an interrupt, it
  17. * supports any page table structure, so we could have used a three-level
  18. * setup, but for the amounts of memory we normally use, a two-level is
  19. * probably more efficient.
  20. *
  21. * This file contains the functions and defines necessary to modify and use
  22. * the CRIS page table tree.
  23. */
  24. #ifndef __ASSEMBLY__
  25. extern void paging_init(void);
  26. #endif
  27. /* Certain architectures need to do special things when pte's
  28. * within a page table are directly modified. Thus, the following
  29. * hook is made available.
  30. */
  31. #define set_pte(pteptr, pteval) ((*(pteptr)) = (pteval))
  32. #define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
  33. /*
  34. * (pmds are folded into pgds so this doesn't get actually called,
  35. * but the define is needed for a generic inline function.)
  36. */
  37. #define set_pmd(pmdptr, pmdval) (*(pmdptr) = pmdval)
  38. #define set_pgu(pudptr, pudval) (*(pudptr) = pudval)
  39. /* PGDIR_SHIFT determines the size of the area a second-level page table can
  40. * map. It is equal to the page size times the number of PTE's that fit in
  41. * a PMD page. A PTE is 4-bytes in CRIS. Hence the following number.
  42. */
  43. #define PGDIR_SHIFT (PAGE_SHIFT + (PAGE_SHIFT-2))
  44. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  45. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  46. /*
  47. * entries per page directory level: we use a two-level, so
  48. * we don't really have any PMD directory physically.
  49. * pointers are 4 bytes so we can use the page size and
  50. * divide it by 4 (shift by 2).
  51. */
  52. #define PTRS_PER_PTE (1UL << (PAGE_SHIFT-2))
  53. #define PTRS_PER_PGD (1UL << (PAGE_SHIFT-2))
  54. /* calculate how many PGD entries a user-level program can use
  55. * the first mappable virtual address is 0
  56. * (TASK_SIZE is the maximum virtual address space)
  57. */
  58. #define USER_PTRS_PER_PGD (TASK_SIZE/PGDIR_SIZE)
  59. #define FIRST_USER_ADDRESS 0
  60. /* zero page used for uninitialized stuff */
  61. #ifndef __ASSEMBLY__
  62. extern unsigned long empty_zero_page;
  63. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  64. #endif
  65. /* number of bits that fit into a memory pointer */
  66. #define BITS_PER_PTR (8*sizeof(unsigned long))
  67. /* to align the pointer to a pointer address */
  68. #define PTR_MASK (~(sizeof(void*)-1))
  69. /* sizeof(void*)==1<<SIZEOF_PTR_LOG2 */
  70. /* 64-bit machines, beware! SRB. */
  71. #define SIZEOF_PTR_LOG2 2
  72. /* to find an entry in a page-table */
  73. #define PAGE_PTR(address) \
  74. ((unsigned long)(address)>>(PAGE_SHIFT-SIZEOF_PTR_LOG2)&PTR_MASK&~PAGE_MASK)
  75. /* to set the page-dir */
  76. #define SET_PAGE_DIR(tsk,pgdir)
  77. #define pte_none(x) (!pte_val(x))
  78. #define pte_present(x) (pte_val(x) & _PAGE_PRESENT)
  79. #define pte_clear(mm,addr,xp) do { pte_val(*(xp)) = 0; } while (0)
  80. #define pmd_none(x) (!pmd_val(x))
  81. /* by removing the _PAGE_KERNEL bit from the comparison, the same pmd_bad
  82. * works for both _PAGE_TABLE and _KERNPG_TABLE pmd entries.
  83. */
  84. #define pmd_bad(x) ((pmd_val(x) & (~PAGE_MASK & ~_PAGE_KERNEL)) != _PAGE_TABLE)
  85. #define pmd_present(x) (pmd_val(x) & _PAGE_PRESENT)
  86. #define pmd_clear(xp) do { pmd_val(*(xp)) = 0; } while (0)
  87. #ifndef __ASSEMBLY__
  88. /*
  89. * The following only work if pte_present() is true.
  90. * Undefined behaviour if not..
  91. */
  92. static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_WRITE; }
  93. static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_MODIFIED; }
  94. static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
  95. static inline int pte_file(pte_t pte) { return pte_val(pte) & _PAGE_FILE; }
  96. static inline int pte_special(pte_t pte) { return 0; }
  97. static inline pte_t pte_wrprotect(pte_t pte)
  98. {
  99. pte_val(pte) &= ~(_PAGE_WRITE | _PAGE_SILENT_WRITE);
  100. return pte;
  101. }
  102. static inline pte_t pte_mkclean(pte_t pte)
  103. {
  104. pte_val(pte) &= ~(_PAGE_MODIFIED | _PAGE_SILENT_WRITE);
  105. return pte;
  106. }
  107. static inline pte_t pte_mkold(pte_t pte)
  108. {
  109. pte_val(pte) &= ~(_PAGE_ACCESSED | _PAGE_SILENT_READ);
  110. return pte;
  111. }
  112. static inline pte_t pte_mkwrite(pte_t pte)
  113. {
  114. pte_val(pte) |= _PAGE_WRITE;
  115. if (pte_val(pte) & _PAGE_MODIFIED)
  116. pte_val(pte) |= _PAGE_SILENT_WRITE;
  117. return pte;
  118. }
  119. static inline pte_t pte_mkdirty(pte_t pte)
  120. {
  121. pte_val(pte) |= _PAGE_MODIFIED;
  122. if (pte_val(pte) & _PAGE_WRITE)
  123. pte_val(pte) |= _PAGE_SILENT_WRITE;
  124. return pte;
  125. }
  126. static inline pte_t pte_mkyoung(pte_t pte)
  127. {
  128. pte_val(pte) |= _PAGE_ACCESSED;
  129. if (pte_val(pte) & _PAGE_READ)
  130. {
  131. pte_val(pte) |= _PAGE_SILENT_READ;
  132. if ((pte_val(pte) & (_PAGE_WRITE | _PAGE_MODIFIED)) ==
  133. (_PAGE_WRITE | _PAGE_MODIFIED))
  134. pte_val(pte) |= _PAGE_SILENT_WRITE;
  135. }
  136. return pte;
  137. }
  138. static inline pte_t pte_mkspecial(pte_t pte) { return pte; }
  139. /*
  140. * Conversion functions: convert a page and protection to a page entry,
  141. * and a page entry and page directory to the page they refer to.
  142. */
  143. /* What actually goes as arguments to the various functions is less than
  144. * obvious, but a rule of thumb is that struct page's goes as struct page *,
  145. * really physical DRAM addresses are unsigned long's, and DRAM "virtual"
  146. * addresses (the 0xc0xxxxxx's) goes as void *'s.
  147. */
  148. static inline pte_t __mk_pte(void * page, pgprot_t pgprot)
  149. {
  150. pte_t pte;
  151. /* the PTE needs a physical address */
  152. pte_val(pte) = __pa(page) | pgprot_val(pgprot);
  153. return pte;
  154. }
  155. #define mk_pte(page, pgprot) __mk_pte(page_address(page), (pgprot))
  156. #define mk_pte_phys(physpage, pgprot) \
  157. ({ \
  158. pte_t __pte; \
  159. \
  160. pte_val(__pte) = (physpage) + pgprot_val(pgprot); \
  161. __pte; \
  162. })
  163. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  164. { pte_val(pte) = (pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot); return pte; }
  165. #define pgprot_noncached(prot) __pgprot((pgprot_val(prot) | _PAGE_NO_CACHE))
  166. /* pte_val refers to a page in the 0x4xxxxxxx physical DRAM interval
  167. * __pte_page(pte_val) refers to the "virtual" DRAM interval
  168. * pte_pagenr refers to the page-number counted starting from the virtual DRAM start
  169. */
  170. static inline unsigned long __pte_page(pte_t pte)
  171. {
  172. /* the PTE contains a physical address */
  173. return (unsigned long)__va(pte_val(pte) & PAGE_MASK);
  174. }
  175. #define pte_pagenr(pte) ((__pte_page(pte) - PAGE_OFFSET) >> PAGE_SHIFT)
  176. /* permanent address of a page */
  177. #define __page_address(page) (PAGE_OFFSET + (((page) - mem_map) << PAGE_SHIFT))
  178. #define pte_page(pte) (mem_map+pte_pagenr(pte))
  179. /* only the pte's themselves need to point to physical DRAM (see above)
  180. * the pagetable links are purely handled within the kernel SW and thus
  181. * don't need the __pa and __va transformations.
  182. */
  183. static inline void pmd_set(pmd_t * pmdp, pte_t * ptep)
  184. { pmd_val(*pmdp) = _PAGE_TABLE | (unsigned long) ptep; }
  185. #define pmd_page(pmd) (pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT))
  186. #define pmd_page_vaddr(pmd) ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
  187. /* to find an entry in a page-table-directory. */
  188. #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  189. /* to find an entry in a page-table-directory */
  190. static inline pgd_t * pgd_offset(const struct mm_struct *mm, unsigned long address)
  191. {
  192. return mm->pgd + pgd_index(address);
  193. }
  194. /* to find an entry in a kernel page-table-directory */
  195. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  196. /* Find an entry in the third-level page table.. */
  197. #define __pte_offset(address) \
  198. (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  199. #define pte_offset_kernel(dir, address) \
  200. ((pte_t *) pmd_page_vaddr(*(dir)) + __pte_offset(address))
  201. #define pte_offset_map(dir, address) \
  202. ((pte_t *)page_address(pmd_page(*(dir))) + __pte_offset(address))
  203. #define pte_unmap(pte) do { } while (0)
  204. #define pte_pfn(x) ((unsigned long)(__va((x).pte)) >> PAGE_SHIFT)
  205. #define pfn_pte(pfn, prot) __pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot))
  206. #define pte_ERROR(e) \
  207. printk("%s:%d: bad pte %p(%08lx).\n", __FILE__, __LINE__, &(e), pte_val(e))
  208. #define pgd_ERROR(e) \
  209. printk("%s:%d: bad pgd %p(%08lx).\n", __FILE__, __LINE__, &(e), pgd_val(e))
  210. #define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \
  211. remap_pfn_range(vma, vaddr, pfn, size, prot)
  212. extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; /* defined in head.S */
  213. /*
  214. * CRIS doesn't have any external MMU info: the kernel page
  215. * tables contain all the necessary information.
  216. *
  217. * Actually I am not sure on what this could be used for.
  218. */
  219. static inline void update_mmu_cache(struct vm_area_struct * vma,
  220. unsigned long address, pte_t *ptep)
  221. {
  222. }
  223. /* Encode and de-code a swap entry (must be !pte_none(e) && !pte_present(e)) */
  224. /* Since the PAGE_PRESENT bit is bit 4, we can use the bits above */
  225. #define __swp_type(x) (((x).val >> 5) & 0x7f)
  226. #define __swp_offset(x) ((x).val >> 12)
  227. #define __swp_entry(type, offset) ((swp_entry_t) { ((type) << 5) | ((offset) << 12) })
  228. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  229. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  230. #define kern_addr_valid(addr) (1)
  231. #include <asm-generic/pgtable.h>
  232. /*
  233. * No page table caches to initialise
  234. */
  235. #define pgtable_cache_init() do { } while (0)
  236. #define pte_to_pgoff(x) (pte_val(x) >> 6)
  237. #define pgoff_to_pte(x) __pte(((x) << 6) | _PAGE_FILE)
  238. typedef pte_t *pte_addr_t;
  239. #endif /* __ASSEMBLY__ */
  240. #endif /* _CRIS_PGTABLE_H */