pgtable.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * OpenRISC Linux
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * OpenRISC implementation:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. * et al.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. */
  18. /* or32 pgtable.h - macros and functions to manipulate page tables
  19. *
  20. * Based on:
  21. * include/asm-cris/pgtable.h
  22. */
  23. #ifndef __ASM_OPENRISC_PGTABLE_H
  24. #define __ASM_OPENRISC_PGTABLE_H
  25. #include <asm-generic/pgtable-nopmd.h>
  26. #ifndef __ASSEMBLY__
  27. #include <asm/mmu.h>
  28. #include <asm/fixmap.h>
  29. /*
  30. * The Linux memory management assumes a three-level page table setup. On
  31. * or32, we use that, but "fold" the mid level into the top-level page
  32. * table. Since the MMU TLB is software loaded through an interrupt, it
  33. * supports any page table structure, so we could have used a three-level
  34. * setup, but for the amounts of memory we normally use, a two-level is
  35. * probably more efficient.
  36. *
  37. * This file contains the functions and defines necessary to modify and use
  38. * the or32 page table tree.
  39. */
  40. extern void paging_init(void);
  41. /* Certain architectures need to do special things when pte's
  42. * within a page table are directly modified. Thus, the following
  43. * hook is made available.
  44. */
  45. #define set_pte(pteptr, pteval) ((*(pteptr)) = (pteval))
  46. #define set_pte_at(mm, addr, ptep, pteval) set_pte(ptep, pteval)
  47. /*
  48. * (pmds are folded into pgds so this doesn't get actually called,
  49. * but the define is needed for a generic inline function.)
  50. */
  51. #define set_pmd(pmdptr, pmdval) (*(pmdptr) = pmdval)
  52. #define PGDIR_SHIFT (PAGE_SHIFT + (PAGE_SHIFT-2))
  53. #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
  54. #define PGDIR_MASK (~(PGDIR_SIZE-1))
  55. /*
  56. * entries per page directory level: we use a two-level, so
  57. * we don't really have any PMD directory physically.
  58. * pointers are 4 bytes so we can use the page size and
  59. * divide it by 4 (shift by 2).
  60. */
  61. #define PTRS_PER_PTE (1UL << (PAGE_SHIFT-2))
  62. #define PTRS_PER_PGD (1UL << (PAGE_SHIFT-2))
  63. /* calculate how many PGD entries a user-level program can use
  64. * the first mappable virtual address is 0
  65. * (TASK_SIZE is the maximum virtual address space)
  66. */
  67. #define USER_PTRS_PER_PGD (TASK_SIZE/PGDIR_SIZE)
  68. #define FIRST_USER_ADDRESS 0UL
  69. /*
  70. * Kernels own virtual memory area.
  71. */
  72. /*
  73. * The size and location of the vmalloc area are chosen so that modules
  74. * placed in this area aren't more than a 28-bit signed offset from any
  75. * kernel functions that they may need. This greatly simplifies handling
  76. * of the relocations for l.j and l.jal instructions as we don't need to
  77. * introduce any trampolines for reaching "distant" code.
  78. *
  79. * 64 MB of vmalloc area is comparable to what's available on other arches.
  80. */
  81. #define VMALLOC_START (PAGE_OFFSET-0x04000000)
  82. #define VMALLOC_END (PAGE_OFFSET)
  83. #define VMALLOC_VMADDR(x) ((unsigned long)(x))
  84. /* Define some higher level generic page attributes.
  85. *
  86. * If you change _PAGE_CI definition be sure to change it in
  87. * io.h for ioremap_nocache() too.
  88. */
  89. /*
  90. * An OR32 PTE looks like this:
  91. *
  92. * | 31 ... 10 | 9 | 8 ... 6 | 5 | 4 | 3 | 2 | 1 | 0 |
  93. * Phys pg.num L PP Index D A WOM WBC CI CC
  94. *
  95. * L : link
  96. * PPI: Page protection index
  97. * D : Dirty
  98. * A : Accessed
  99. * WOM: Weakly ordered memory
  100. * WBC: Write-back cache
  101. * CI : Cache inhibit
  102. * CC : Cache coherent
  103. *
  104. * The protection bits below should correspond to the layout of the actual
  105. * PTE as per above
  106. */
  107. #define _PAGE_CC 0x001 /* software: pte contains a translation */
  108. #define _PAGE_CI 0x002 /* cache inhibit */
  109. #define _PAGE_WBC 0x004 /* write back cache */
  110. #define _PAGE_WOM 0x008 /* weakly ordered memory */
  111. #define _PAGE_A 0x010 /* accessed */
  112. #define _PAGE_D 0x020 /* dirty */
  113. #define _PAGE_URE 0x040 /* user read enable */
  114. #define _PAGE_UWE 0x080 /* user write enable */
  115. #define _PAGE_SRE 0x100 /* superuser read enable */
  116. #define _PAGE_SWE 0x200 /* superuser write enable */
  117. #define _PAGE_EXEC 0x400 /* software: page is executable */
  118. #define _PAGE_U_SHARED 0x800 /* software: page is shared in user space */
  119. /* 0x001 is cache coherency bit, which should always be set to
  120. * 1 - for SMP (when we support it)
  121. * 0 - otherwise
  122. *
  123. * we just reuse this bit in software for _PAGE_PRESENT and
  124. * force it to 0 when loading it into TLB.
  125. */
  126. #define _PAGE_PRESENT _PAGE_CC
  127. #define _PAGE_USER _PAGE_URE
  128. #define _PAGE_WRITE (_PAGE_UWE | _PAGE_SWE)
  129. #define _PAGE_DIRTY _PAGE_D
  130. #define _PAGE_ACCESSED _PAGE_A
  131. #define _PAGE_NO_CACHE _PAGE_CI
  132. #define _PAGE_SHARED _PAGE_U_SHARED
  133. #define _PAGE_READ (_PAGE_URE | _PAGE_SRE)
  134. #define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
  135. #define _PAGE_BASE (_PAGE_PRESENT | _PAGE_ACCESSED)
  136. #define _PAGE_ALL (_PAGE_PRESENT | _PAGE_ACCESSED)
  137. #define _KERNPG_TABLE \
  138. (_PAGE_BASE | _PAGE_SRE | _PAGE_SWE | _PAGE_ACCESSED | _PAGE_DIRTY)
  139. #define PAGE_NONE __pgprot(_PAGE_ALL)
  140. #define PAGE_READONLY __pgprot(_PAGE_ALL | _PAGE_URE | _PAGE_SRE)
  141. #define PAGE_READONLY_X __pgprot(_PAGE_ALL | _PAGE_URE | _PAGE_SRE | _PAGE_EXEC)
  142. #define PAGE_SHARED \
  143. __pgprot(_PAGE_ALL | _PAGE_URE | _PAGE_SRE | _PAGE_UWE | _PAGE_SWE \
  144. | _PAGE_SHARED)
  145. #define PAGE_SHARED_X \
  146. __pgprot(_PAGE_ALL | _PAGE_URE | _PAGE_SRE | _PAGE_UWE | _PAGE_SWE \
  147. | _PAGE_SHARED | _PAGE_EXEC)
  148. #define PAGE_COPY __pgprot(_PAGE_ALL | _PAGE_URE | _PAGE_SRE)
  149. #define PAGE_COPY_X __pgprot(_PAGE_ALL | _PAGE_URE | _PAGE_SRE | _PAGE_EXEC)
  150. #define PAGE_KERNEL \
  151. __pgprot(_PAGE_ALL | _PAGE_SRE | _PAGE_SWE \
  152. | _PAGE_SHARED | _PAGE_DIRTY | _PAGE_EXEC)
  153. #define PAGE_KERNEL_RO \
  154. __pgprot(_PAGE_ALL | _PAGE_SRE \
  155. | _PAGE_SHARED | _PAGE_DIRTY | _PAGE_EXEC)
  156. #define PAGE_KERNEL_NOCACHE \
  157. __pgprot(_PAGE_ALL | _PAGE_SRE | _PAGE_SWE \
  158. | _PAGE_SHARED | _PAGE_DIRTY | _PAGE_EXEC | _PAGE_CI)
  159. #define __P000 PAGE_NONE
  160. #define __P001 PAGE_READONLY_X
  161. #define __P010 PAGE_COPY
  162. #define __P011 PAGE_COPY_X
  163. #define __P100 PAGE_READONLY
  164. #define __P101 PAGE_READONLY_X
  165. #define __P110 PAGE_COPY
  166. #define __P111 PAGE_COPY_X
  167. #define __S000 PAGE_NONE
  168. #define __S001 PAGE_READONLY_X
  169. #define __S010 PAGE_SHARED
  170. #define __S011 PAGE_SHARED_X
  171. #define __S100 PAGE_READONLY
  172. #define __S101 PAGE_READONLY_X
  173. #define __S110 PAGE_SHARED
  174. #define __S111 PAGE_SHARED_X
  175. /* zero page used for uninitialized stuff */
  176. extern unsigned long empty_zero_page[2048];
  177. #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
  178. /* number of bits that fit into a memory pointer */
  179. #define BITS_PER_PTR (8*sizeof(unsigned long))
  180. /* to align the pointer to a pointer address */
  181. #define PTR_MASK (~(sizeof(void *)-1))
  182. /* sizeof(void*)==1<<SIZEOF_PTR_LOG2 */
  183. /* 64-bit machines, beware! SRB. */
  184. #define SIZEOF_PTR_LOG2 2
  185. /* to find an entry in a page-table */
  186. #define PAGE_PTR(address) \
  187. ((unsigned long)(address)>>(PAGE_SHIFT-SIZEOF_PTR_LOG2)&PTR_MASK&~PAGE_MASK)
  188. /* to set the page-dir */
  189. #define SET_PAGE_DIR(tsk, pgdir)
  190. #define pte_none(x) (!pte_val(x))
  191. #define pte_present(x) (pte_val(x) & _PAGE_PRESENT)
  192. #define pte_clear(mm, addr, xp) do { pte_val(*(xp)) = 0; } while (0)
  193. #define pmd_none(x) (!pmd_val(x))
  194. #define pmd_bad(x) ((pmd_val(x) & (~PAGE_MASK)) != _KERNPG_TABLE)
  195. #define pmd_present(x) (pmd_val(x) & _PAGE_PRESENT)
  196. #define pmd_clear(xp) do { pmd_val(*(xp)) = 0; } while (0)
  197. /*
  198. * The following only work if pte_present() is true.
  199. * Undefined behaviour if not..
  200. */
  201. static inline int pte_read(pte_t pte) { return pte_val(pte) & _PAGE_READ; }
  202. static inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_WRITE; }
  203. static inline int pte_exec(pte_t pte) { return pte_val(pte) & _PAGE_EXEC; }
  204. static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
  205. static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
  206. static inline int pte_special(pte_t pte) { return 0; }
  207. static inline pte_t pte_mkspecial(pte_t pte) { return pte; }
  208. static inline pte_t pte_wrprotect(pte_t pte)
  209. {
  210. pte_val(pte) &= ~(_PAGE_WRITE);
  211. return pte;
  212. }
  213. static inline pte_t pte_rdprotect(pte_t pte)
  214. {
  215. pte_val(pte) &= ~(_PAGE_READ);
  216. return pte;
  217. }
  218. static inline pte_t pte_exprotect(pte_t pte)
  219. {
  220. pte_val(pte) &= ~(_PAGE_EXEC);
  221. return pte;
  222. }
  223. static inline pte_t pte_mkclean(pte_t pte)
  224. {
  225. pte_val(pte) &= ~(_PAGE_DIRTY);
  226. return pte;
  227. }
  228. static inline pte_t pte_mkold(pte_t pte)
  229. {
  230. pte_val(pte) &= ~(_PAGE_ACCESSED);
  231. return pte;
  232. }
  233. static inline pte_t pte_mkwrite(pte_t pte)
  234. {
  235. pte_val(pte) |= _PAGE_WRITE;
  236. return pte;
  237. }
  238. static inline pte_t pte_mkread(pte_t pte)
  239. {
  240. pte_val(pte) |= _PAGE_READ;
  241. return pte;
  242. }
  243. static inline pte_t pte_mkexec(pte_t pte)
  244. {
  245. pte_val(pte) |= _PAGE_EXEC;
  246. return pte;
  247. }
  248. static inline pte_t pte_mkdirty(pte_t pte)
  249. {
  250. pte_val(pte) |= _PAGE_DIRTY;
  251. return pte;
  252. }
  253. static inline pte_t pte_mkyoung(pte_t pte)
  254. {
  255. pte_val(pte) |= _PAGE_ACCESSED;
  256. return pte;
  257. }
  258. /*
  259. * Conversion functions: convert a page and protection to a page entry,
  260. * and a page entry and page directory to the page they refer to.
  261. */
  262. /* What actually goes as arguments to the various functions is less than
  263. * obvious, but a rule of thumb is that struct page's goes as struct page *,
  264. * really physical DRAM addresses are unsigned long's, and DRAM "virtual"
  265. * addresses (the 0xc0xxxxxx's) goes as void *'s.
  266. */
  267. static inline pte_t __mk_pte(void *page, pgprot_t pgprot)
  268. {
  269. pte_t pte;
  270. /* the PTE needs a physical address */
  271. pte_val(pte) = __pa(page) | pgprot_val(pgprot);
  272. return pte;
  273. }
  274. #define mk_pte(page, pgprot) __mk_pte(page_address(page), (pgprot))
  275. #define mk_pte_phys(physpage, pgprot) \
  276. ({ \
  277. pte_t __pte; \
  278. \
  279. pte_val(__pte) = (physpage) + pgprot_val(pgprot); \
  280. __pte; \
  281. })
  282. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  283. {
  284. pte_val(pte) = (pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot);
  285. return pte;
  286. }
  287. /*
  288. * pte_val refers to a page in the 0x0xxxxxxx physical DRAM interval
  289. * __pte_page(pte_val) refers to the "virtual" DRAM interval
  290. * pte_pagenr refers to the page-number counted starting from the virtual
  291. * DRAM start
  292. */
  293. static inline unsigned long __pte_page(pte_t pte)
  294. {
  295. /* the PTE contains a physical address */
  296. return (unsigned long)__va(pte_val(pte) & PAGE_MASK);
  297. }
  298. #define pte_pagenr(pte) ((__pte_page(pte) - PAGE_OFFSET) >> PAGE_SHIFT)
  299. /* permanent address of a page */
  300. #define __page_address(page) (PAGE_OFFSET + (((page) - mem_map) << PAGE_SHIFT))
  301. #define pte_page(pte) (mem_map+pte_pagenr(pte))
  302. /*
  303. * only the pte's themselves need to point to physical DRAM (see above)
  304. * the pagetable links are purely handled within the kernel SW and thus
  305. * don't need the __pa and __va transformations.
  306. */
  307. static inline void pmd_set(pmd_t *pmdp, pte_t *ptep)
  308. {
  309. pmd_val(*pmdp) = _KERNPG_TABLE | (unsigned long) ptep;
  310. }
  311. #define pmd_page(pmd) (pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT))
  312. #define pmd_page_kernel(pmd) ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
  313. /* to find an entry in a page-table-directory. */
  314. #define pgd_index(address) ((address >> PGDIR_SHIFT) & (PTRS_PER_PGD-1))
  315. #define __pgd_offset(address) pgd_index(address)
  316. #define pgd_offset(mm, address) ((mm)->pgd+pgd_index(address))
  317. /* to find an entry in a kernel page-table-directory */
  318. #define pgd_offset_k(address) pgd_offset(&init_mm, address)
  319. #define __pmd_offset(address) \
  320. (((address) >> PMD_SHIFT) & (PTRS_PER_PMD-1))
  321. /*
  322. * the pte page can be thought of an array like this: pte_t[PTRS_PER_PTE]
  323. *
  324. * this macro returns the index of the entry in the pte page which would
  325. * control the given virtual address
  326. */
  327. #define __pte_offset(address) \
  328. (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
  329. #define pte_offset_kernel(dir, address) \
  330. ((pte_t *) pmd_page_kernel(*(dir)) + __pte_offset(address))
  331. #define pte_offset_map(dir, address) \
  332. ((pte_t *)page_address(pmd_page(*(dir))) + __pte_offset(address))
  333. #define pte_offset_map_nested(dir, address) \
  334. pte_offset_map(dir, address)
  335. #define pte_unmap(pte) do { } while (0)
  336. #define pte_unmap_nested(pte) do { } while (0)
  337. #define pte_pfn(x) ((unsigned long)(((x).pte)) >> PAGE_SHIFT)
  338. #define pfn_pte(pfn, prot) __pte((((pfn) << PAGE_SHIFT)) | pgprot_val(prot))
  339. #define pte_ERROR(e) \
  340. printk(KERN_ERR "%s:%d: bad pte %p(%08lx).\n", \
  341. __FILE__, __LINE__, &(e), pte_val(e))
  342. #define pgd_ERROR(e) \
  343. printk(KERN_ERR "%s:%d: bad pgd %p(%08lx).\n", \
  344. __FILE__, __LINE__, &(e), pgd_val(e))
  345. extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; /* defined in head.S */
  346. /*
  347. * or32 doesn't have any external MMU info: the kernel page
  348. * tables contain all the necessary information.
  349. *
  350. * Actually I am not sure on what this could be used for.
  351. */
  352. static inline void update_mmu_cache(struct vm_area_struct *vma,
  353. unsigned long address, pte_t *pte)
  354. {
  355. }
  356. /* __PHX__ FIXME, SWAP, this probably doesn't work */
  357. /* Encode and de-code a swap entry (must be !pte_none(e) && !pte_present(e)) */
  358. /* Since the PAGE_PRESENT bit is bit 4, we can use the bits above */
  359. #define __swp_type(x) (((x).val >> 5) & 0x7f)
  360. #define __swp_offset(x) ((x).val >> 12)
  361. #define __swp_entry(type, offset) \
  362. ((swp_entry_t) { ((type) << 5) | ((offset) << 12) })
  363. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  364. #define __swp_entry_to_pte(x) ((pte_t) { (x).val })
  365. #define kern_addr_valid(addr) (1)
  366. #include <asm-generic/pgtable.h>
  367. /*
  368. * No page table caches to initialise
  369. */
  370. #define pgtable_cache_init() do { } while (0)
  371. typedef pte_t *pte_addr_t;
  372. #endif /* __ASSEMBLY__ */
  373. #endif /* __ASM_OPENRISC_PGTABLE_H */