pgtable.h 12 KB

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