pgtable.h 10 KB

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