pgtable.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef __ASM_AVR32_PGTABLE_H
  9. #define __ASM_AVR32_PGTABLE_H
  10. #include <asm/addrspace.h>
  11. #ifndef __ASSEMBLY__
  12. #include <linux/sched.h>
  13. #endif /* !__ASSEMBLY__ */
  14. /*
  15. * Use two-level page tables just as the i386 (without PAE)
  16. */
  17. #include <asm/pgtable-2level.h>
  18. /*
  19. * The following code might need some cleanup when the values are
  20. * final...
  21. */
  22. #define PMD_SIZE (1UL << PMD_SHIFT)
  23. #define PMD_MASK (~(PMD_SIZE-1))
  24. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  25. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  26. #define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE)
  27. #define FIRST_USER_ADDRESS 0UL
  28. #ifndef __ASSEMBLY__
  29. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  30. extern void paging_init(void);
  31. /*
  32. * ZERO_PAGE is a global shared page that is always zero: used for
  33. * zero-mapped memory areas etc.
  34. */
  35. extern struct page *empty_zero_page;
  36. #define ZERO_PAGE(vaddr) (empty_zero_page)
  37. /*
  38. * Just any arbitrary offset to the start of the vmalloc VM area: the
  39. * current 8 MiB value just means that there will be a 8 MiB "hole"
  40. * after the uncached physical memory (P2 segment) until the vmalloc
  41. * area starts. That means that any out-of-bounds memory accesses will
  42. * hopefully be caught; we don't know if the end of the P1/P2 segments
  43. * are actually used for anything, but it is anyway safer to let the
  44. * MMU catch these kinds of errors than to rely on the memory bus.
  45. *
  46. * A "hole" of the same size is added to the end of the P3 segment as
  47. * well. It might seem wasteful to use 16 MiB of virtual address space
  48. * on this, but we do have 512 MiB of it...
  49. *
  50. * The vmalloc() routines leave a hole of 4 KiB between each vmalloced
  51. * area for the same reason.
  52. */
  53. #define VMALLOC_OFFSET (8 * 1024 * 1024)
  54. #define VMALLOC_START (P3SEG + VMALLOC_OFFSET)
  55. #define VMALLOC_END (P4SEG - VMALLOC_OFFSET)
  56. #endif /* !__ASSEMBLY__ */
  57. /*
  58. * Page flags. Some of these flags are not directly supported by
  59. * hardware, so we have to emulate them.
  60. */
  61. #define _TLBEHI_BIT_VALID 9
  62. #define _TLBEHI_VALID (1 << _TLBEHI_BIT_VALID)
  63. #define _PAGE_BIT_WT 0 /* W-bit : write-through */
  64. #define _PAGE_BIT_DIRTY 1 /* D-bit : page changed */
  65. #define _PAGE_BIT_SZ0 2 /* SZ0-bit : Size of page */
  66. #define _PAGE_BIT_SZ1 3 /* SZ1-bit : Size of page */
  67. #define _PAGE_BIT_EXECUTE 4 /* X-bit : execute access allowed */
  68. #define _PAGE_BIT_RW 5 /* AP0-bit : write access allowed */
  69. #define _PAGE_BIT_USER 6 /* AP1-bit : user space access allowed */
  70. #define _PAGE_BIT_BUFFER 7 /* B-bit : bufferable */
  71. #define _PAGE_BIT_GLOBAL 8 /* G-bit : global (ignore ASID) */
  72. #define _PAGE_BIT_CACHABLE 9 /* C-bit : cachable */
  73. /* If we drop support for 1K pages, we get two extra bits */
  74. #define _PAGE_BIT_PRESENT 10
  75. #define _PAGE_BIT_ACCESSED 11 /* software: page was accessed */
  76. #define _PAGE_WT (1 << _PAGE_BIT_WT)
  77. #define _PAGE_DIRTY (1 << _PAGE_BIT_DIRTY)
  78. #define _PAGE_EXECUTE (1 << _PAGE_BIT_EXECUTE)
  79. #define _PAGE_RW (1 << _PAGE_BIT_RW)
  80. #define _PAGE_USER (1 << _PAGE_BIT_USER)
  81. #define _PAGE_BUFFER (1 << _PAGE_BIT_BUFFER)
  82. #define _PAGE_GLOBAL (1 << _PAGE_BIT_GLOBAL)
  83. #define _PAGE_CACHABLE (1 << _PAGE_BIT_CACHABLE)
  84. /* Software flags */
  85. #define _PAGE_ACCESSED (1 << _PAGE_BIT_ACCESSED)
  86. #define _PAGE_PRESENT (1 << _PAGE_BIT_PRESENT)
  87. /*
  88. * Page types, i.e. sizes. _PAGE_TYPE_NONE corresponds to what is
  89. * usually called _PAGE_PROTNONE on other architectures.
  90. *
  91. * XXX: Find out if _PAGE_PROTNONE is equivalent with !_PAGE_USER. If
  92. * so, we can encode all possible page sizes (although we can't really
  93. * support 1K pages anyway due to the _PAGE_PRESENT and _PAGE_ACCESSED
  94. * bits)
  95. *
  96. */
  97. #define _PAGE_TYPE_MASK ((1 << _PAGE_BIT_SZ0) | (1 << _PAGE_BIT_SZ1))
  98. #define _PAGE_TYPE_NONE (0 << _PAGE_BIT_SZ0)
  99. #define _PAGE_TYPE_SMALL (1 << _PAGE_BIT_SZ0)
  100. #define _PAGE_TYPE_MEDIUM (2 << _PAGE_BIT_SZ0)
  101. #define _PAGE_TYPE_LARGE (3 << _PAGE_BIT_SZ0)
  102. /*
  103. * Mask which drop software flags. We currently can't handle more than
  104. * 512 MiB of physical memory, so we can use bits 29-31 for other
  105. * stuff. With a fixed 4K page size, we can use bits 10-11 as well as
  106. * bits 2-3 (SZ)
  107. */
  108. #define _PAGE_FLAGS_HARDWARE_MASK 0xfffff3ff
  109. #define _PAGE_FLAGS_CACHE_MASK (_PAGE_CACHABLE | _PAGE_BUFFER | _PAGE_WT)
  110. /* Flags that may be modified by software */
  111. #define _PAGE_CHG_MASK (PTE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY \
  112. | _PAGE_FLAGS_CACHE_MASK)
  113. #define _PAGE_FLAGS_READ (_PAGE_CACHABLE | _PAGE_BUFFER)
  114. #define _PAGE_FLAGS_WRITE (_PAGE_FLAGS_READ | _PAGE_RW | _PAGE_DIRTY)
  115. #define _PAGE_NORMAL(x) __pgprot((x) | _PAGE_PRESENT | _PAGE_TYPE_SMALL \
  116. | _PAGE_ACCESSED)
  117. #define PAGE_NONE (_PAGE_ACCESSED | _PAGE_TYPE_NONE)
  118. #define PAGE_READ (_PAGE_FLAGS_READ | _PAGE_USER)
  119. #define PAGE_EXEC (_PAGE_FLAGS_READ | _PAGE_EXECUTE | _PAGE_USER)
  120. #define PAGE_WRITE (_PAGE_FLAGS_WRITE | _PAGE_USER)
  121. #define PAGE_KERNEL _PAGE_NORMAL(_PAGE_FLAGS_WRITE | _PAGE_EXECUTE | _PAGE_GLOBAL)
  122. #define PAGE_KERNEL_RO _PAGE_NORMAL(_PAGE_FLAGS_READ | _PAGE_EXECUTE | _PAGE_GLOBAL)
  123. #define _PAGE_P(x) _PAGE_NORMAL((x) & ~(_PAGE_RW | _PAGE_DIRTY))
  124. #define _PAGE_S(x) _PAGE_NORMAL(x)
  125. #define PAGE_COPY _PAGE_P(PAGE_WRITE | PAGE_READ)
  126. #define PAGE_SHARED _PAGE_S(PAGE_WRITE | PAGE_READ)
  127. #ifndef __ASSEMBLY__
  128. /*
  129. * The hardware supports flags for write- and execute access. Read is
  130. * always allowed if the page is loaded into the TLB, so the "-w-",
  131. * "--x" and "-wx" mappings are implemented as "rw-", "r-x" and "rwx",
  132. * respectively.
  133. *
  134. * The "---" case is handled by software; the page will simply not be
  135. * loaded into the TLB if the page type is _PAGE_TYPE_NONE.
  136. */
  137. #define __P000 __pgprot(PAGE_NONE)
  138. #define __P001 _PAGE_P(PAGE_READ)
  139. #define __P010 _PAGE_P(PAGE_WRITE)
  140. #define __P011 _PAGE_P(PAGE_WRITE | PAGE_READ)
  141. #define __P100 _PAGE_P(PAGE_EXEC)
  142. #define __P101 _PAGE_P(PAGE_EXEC | PAGE_READ)
  143. #define __P110 _PAGE_P(PAGE_EXEC | PAGE_WRITE)
  144. #define __P111 _PAGE_P(PAGE_EXEC | PAGE_WRITE | PAGE_READ)
  145. #define __S000 __pgprot(PAGE_NONE)
  146. #define __S001 _PAGE_S(PAGE_READ)
  147. #define __S010 _PAGE_S(PAGE_WRITE)
  148. #define __S011 _PAGE_S(PAGE_WRITE | PAGE_READ)
  149. #define __S100 _PAGE_S(PAGE_EXEC)
  150. #define __S101 _PAGE_S(PAGE_EXEC | PAGE_READ)
  151. #define __S110 _PAGE_S(PAGE_EXEC | PAGE_WRITE)
  152. #define __S111 _PAGE_S(PAGE_EXEC | PAGE_WRITE | PAGE_READ)
  153. #define pte_none(x) (!pte_val(x))
  154. #define pte_present(x) (pte_val(x) & _PAGE_PRESENT)
  155. #define pte_clear(mm,addr,xp) \
  156. do { \
  157. set_pte_at(mm, addr, xp, __pte(0)); \
  158. } while (0)
  159. /*
  160. * The following only work if pte_present() is true.
  161. * Undefined behaviour if not..
  162. */
  163. static inline int pte_write(pte_t pte)
  164. {
  165. return pte_val(pte) & _PAGE_RW;
  166. }
  167. static inline int pte_dirty(pte_t pte)
  168. {
  169. return pte_val(pte) & _PAGE_DIRTY;
  170. }
  171. static inline int pte_young(pte_t pte)
  172. {
  173. return pte_val(pte) & _PAGE_ACCESSED;
  174. }
  175. static inline int pte_special(pte_t pte)
  176. {
  177. return 0;
  178. }
  179. /* Mutator functions for PTE bits */
  180. static inline pte_t pte_wrprotect(pte_t pte)
  181. {
  182. set_pte(&pte, __pte(pte_val(pte) & ~_PAGE_RW));
  183. return pte;
  184. }
  185. static inline pte_t pte_mkclean(pte_t pte)
  186. {
  187. set_pte(&pte, __pte(pte_val(pte) & ~_PAGE_DIRTY));
  188. return pte;
  189. }
  190. static inline pte_t pte_mkold(pte_t pte)
  191. {
  192. set_pte(&pte, __pte(pte_val(pte) & ~_PAGE_ACCESSED));
  193. return pte;
  194. }
  195. static inline pte_t pte_mkwrite(pte_t pte)
  196. {
  197. set_pte(&pte, __pte(pte_val(pte) | _PAGE_RW));
  198. return pte;
  199. }
  200. static inline pte_t pte_mkdirty(pte_t pte)
  201. {
  202. set_pte(&pte, __pte(pte_val(pte) | _PAGE_DIRTY));
  203. return pte;
  204. }
  205. static inline pte_t pte_mkyoung(pte_t pte)
  206. {
  207. set_pte(&pte, __pte(pte_val(pte) | _PAGE_ACCESSED));
  208. return pte;
  209. }
  210. static inline pte_t pte_mkspecial(pte_t pte)
  211. {
  212. return pte;
  213. }
  214. #define pmd_none(x) (!pmd_val(x))
  215. #define pmd_present(x) (pmd_val(x))
  216. static inline void pmd_clear(pmd_t *pmdp)
  217. {
  218. set_pmd(pmdp, __pmd(0));
  219. }
  220. #define pmd_bad(x) (pmd_val(x) & ~PAGE_MASK)
  221. /*
  222. * Permanent address of a page. We don't support highmem, so this is
  223. * trivial.
  224. */
  225. #define pages_to_mb(x) ((x) >> (20-PAGE_SHIFT))
  226. #define pte_page(x) (pfn_to_page(pte_pfn(x)))
  227. /*
  228. * Mark the prot value as uncacheable and unbufferable
  229. */
  230. #define pgprot_noncached(prot) \
  231. __pgprot(pgprot_val(prot) & ~(_PAGE_BUFFER | _PAGE_CACHABLE))
  232. /*
  233. * Mark the prot value as uncacheable but bufferable
  234. */
  235. #define pgprot_writecombine(prot) \
  236. __pgprot((pgprot_val(prot) & ~_PAGE_CACHABLE) | _PAGE_BUFFER)
  237. /*
  238. * Conversion functions: convert a page and protection to a page entry,
  239. * and a page entry and page directory to the page they refer to.
  240. *
  241. * extern pte_t mk_pte(struct page *page, pgprot_t pgprot)
  242. */
  243. #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
  244. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  245. {
  246. set_pte(&pte, __pte((pte_val(pte) & _PAGE_CHG_MASK)
  247. | pgprot_val(newprot)));
  248. return pte;
  249. }
  250. #define page_pte(page) page_pte_prot(page, __pgprot(0))
  251. #define pmd_page_vaddr(pmd) pmd_val(pmd)
  252. #define pmd_page(pmd) (virt_to_page(pmd_val(pmd)))
  253. /* to find an entry in a page-table-directory. */
  254. #define pgd_index(address) (((address) >> PGDIR_SHIFT) \
  255. & (PTRS_PER_PGD - 1))
  256. #define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
  257. /* to find an entry in a kernel page-table-directory */
  258. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  259. /* Find an entry in the third-level page table.. */
  260. #define pte_index(address) \
  261. ((address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  262. #define pte_offset(dir, address) \
  263. ((pte_t *) pmd_page_vaddr(*(dir)) + pte_index(address))
  264. #define pte_offset_kernel(dir, address) \
  265. ((pte_t *) pmd_page_vaddr(*(dir)) + pte_index(address))
  266. #define pte_offset_map(dir, address) pte_offset_kernel(dir, address)
  267. #define pte_unmap(pte) do { } while (0)
  268. struct vm_area_struct;
  269. extern void update_mmu_cache(struct vm_area_struct * vma,
  270. unsigned long address, pte_t *ptep);
  271. /*
  272. * Encode and decode a swap entry
  273. *
  274. * Constraints:
  275. * _PAGE_TYPE_* at bits 2-3 (for emulating _PAGE_PROTNONE)
  276. * _PAGE_PRESENT at bit 10
  277. *
  278. * We encode the type into bits 4-9 and offset into bits 11-31. This
  279. * gives us a 21 bits offset, or 2**21 * 4K = 8G usable swap space per
  280. * device, and 64 possible types.
  281. *
  282. * NOTE: We should set ZEROs at the position of _PAGE_PRESENT
  283. * and _PAGE_PROTNONE bits
  284. */
  285. #define __swp_type(x) (((x).val >> 4) & 0x3f)
  286. #define __swp_offset(x) ((x).val >> 11)
  287. #define __swp_entry(type, offset) ((swp_entry_t) { ((type) << 4) | ((offset) << 11) })
  288. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  289. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  290. typedef pte_t *pte_addr_t;
  291. #define kern_addr_valid(addr) (1)
  292. /* No page table caches to initialize (?) */
  293. #define pgtable_cache_init() do { } while(0)
  294. #include <asm-generic/pgtable.h>
  295. #endif /* !__ASSEMBLY__ */
  296. #endif /* __ASM_AVR32_PGTABLE_H */