pgtable.h 12 KB

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