pgtable.h 11 KB

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