pgtable.h 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 ARM Ltd.
  4. */
  5. #ifndef __ASM_PGTABLE_H
  6. #define __ASM_PGTABLE_H
  7. #include <asm/bug.h>
  8. #include <asm/proc-fns.h>
  9. #include <asm/memory.h>
  10. #include <asm/mte.h>
  11. #include <asm/pgtable-hwdef.h>
  12. #include <asm/pgtable-prot.h>
  13. #include <asm/tlbflush.h>
  14. /*
  15. * VMALLOC range.
  16. *
  17. * VMALLOC_START: beginning of the kernel vmalloc space
  18. * VMALLOC_END: extends to the available space below vmemmap
  19. */
  20. #define VMALLOC_START (MODULES_END)
  21. #if VA_BITS == VA_BITS_MIN
  22. #define VMALLOC_END (VMEMMAP_START - SZ_8M)
  23. #else
  24. #define VMEMMAP_UNUSED_NPAGES ((_PAGE_OFFSET(vabits_actual) - PAGE_OFFSET) >> PAGE_SHIFT)
  25. #define VMALLOC_END (VMEMMAP_START + VMEMMAP_UNUSED_NPAGES * sizeof(struct page) - SZ_8M)
  26. #endif
  27. #define vmemmap ((struct page *)VMEMMAP_START - (memstart_addr >> PAGE_SHIFT))
  28. #ifndef __ASSEMBLER__
  29. #include <asm/cmpxchg.h>
  30. #include <asm/fixmap.h>
  31. #include <asm/por.h>
  32. #include <linux/mmdebug.h>
  33. #include <linux/mm_types.h>
  34. #include <linux/sched.h>
  35. #include <linux/page_table_check.h>
  36. static inline void emit_pte_barriers(void)
  37. {
  38. /*
  39. * These barriers are emitted under certain conditions after a pte entry
  40. * was modified (see e.g. __set_pte_complete()). The dsb makes the store
  41. * visible to the table walker. The isb ensures that any previous
  42. * speculative "invalid translation" marker that is in the CPU's
  43. * pipeline gets cleared, so that any access to that address after
  44. * setting the pte to valid won't cause a spurious fault. If the thread
  45. * gets preempted after storing to the pgtable but before emitting these
  46. * barriers, __switch_to() emits a dsb which ensure the walker gets to
  47. * see the store. There is no guarantee of an isb being issued though.
  48. * This is safe because it will still get issued (albeit on a
  49. * potentially different CPU) when the thread starts running again,
  50. * before any access to the address.
  51. */
  52. dsb(ishst);
  53. isb();
  54. }
  55. static inline void queue_pte_barriers(void)
  56. {
  57. if (is_lazy_mmu_mode_active()) {
  58. /* Avoid the atomic op if already set. */
  59. if (!test_thread_flag(TIF_LAZY_MMU_PENDING))
  60. set_thread_flag(TIF_LAZY_MMU_PENDING);
  61. } else {
  62. emit_pte_barriers();
  63. }
  64. }
  65. static inline void arch_enter_lazy_mmu_mode(void) {}
  66. static inline void arch_flush_lazy_mmu_mode(void)
  67. {
  68. if (test_and_clear_thread_flag(TIF_LAZY_MMU_PENDING))
  69. emit_pte_barriers();
  70. }
  71. static inline void arch_leave_lazy_mmu_mode(void)
  72. {
  73. arch_flush_lazy_mmu_mode();
  74. }
  75. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  76. #define __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
  77. /* Set stride and tlb_level in flush_*_tlb_range */
  78. #define flush_pmd_tlb_range(vma, addr, end) \
  79. __flush_tlb_range(vma, addr, end, PMD_SIZE, false, 2)
  80. #define flush_pud_tlb_range(vma, addr, end) \
  81. __flush_tlb_range(vma, addr, end, PUD_SIZE, false, 1)
  82. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  83. /*
  84. * We use local TLB invalidation instruction when reusing page in
  85. * write protection fault handler to avoid TLBI broadcast in the hot
  86. * path. This will cause spurious page faults if stale read-only TLB
  87. * entries exist.
  88. */
  89. #define flush_tlb_fix_spurious_fault(vma, address, ptep) \
  90. local_flush_tlb_page_nonotify(vma, address)
  91. #define flush_tlb_fix_spurious_fault_pmd(vma, address, pmdp) \
  92. local_flush_tlb_page_nonotify(vma, address)
  93. /*
  94. * ZERO_PAGE is a global shared page that is always zero: used
  95. * for zero-mapped memory areas etc..
  96. */
  97. extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
  98. #define ZERO_PAGE(vaddr) phys_to_page(__pa_symbol(empty_zero_page))
  99. #define pte_ERROR(e) \
  100. pr_err("%s:%d: bad pte %016llx.\n", __FILE__, __LINE__, pte_val(e))
  101. #ifdef CONFIG_ARM64_PA_BITS_52
  102. static inline phys_addr_t __pte_to_phys(pte_t pte)
  103. {
  104. pte_val(pte) &= ~PTE_MAYBE_SHARED;
  105. return (pte_val(pte) & PTE_ADDR_LOW) |
  106. ((pte_val(pte) & PTE_ADDR_HIGH) << PTE_ADDR_HIGH_SHIFT);
  107. }
  108. static inline pteval_t __phys_to_pte_val(phys_addr_t phys)
  109. {
  110. return (phys | (phys >> PTE_ADDR_HIGH_SHIFT)) & PHYS_TO_PTE_ADDR_MASK;
  111. }
  112. #else
  113. static inline phys_addr_t __pte_to_phys(pte_t pte)
  114. {
  115. return pte_val(pte) & PTE_ADDR_LOW;
  116. }
  117. static inline pteval_t __phys_to_pte_val(phys_addr_t phys)
  118. {
  119. return phys;
  120. }
  121. #endif
  122. #define pte_pfn(pte) (__pte_to_phys(pte) >> PAGE_SHIFT)
  123. #define pfn_pte(pfn,prot) \
  124. __pte(__phys_to_pte_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot))
  125. #define pte_none(pte) (!pte_val(pte))
  126. #define pte_page(pte) (pfn_to_page(pte_pfn(pte)))
  127. /*
  128. * The following only work if pte_present(). Undefined behaviour otherwise.
  129. */
  130. #define pte_present(pte) (pte_valid(pte) || pte_present_invalid(pte))
  131. #define pte_young(pte) (!!(pte_val(pte) & PTE_AF))
  132. #define pte_special(pte) (!!(pte_val(pte) & PTE_SPECIAL))
  133. #define pte_write(pte) (!!(pte_val(pte) & PTE_WRITE))
  134. #define pte_rdonly(pte) (!!(pte_val(pte) & PTE_RDONLY))
  135. #define pte_user(pte) (!!(pte_val(pte) & PTE_USER))
  136. #define pte_user_exec(pte) (!(pte_val(pte) & PTE_UXN))
  137. #define pte_cont(pte) (!!(pte_val(pte) & PTE_CONT))
  138. #define pte_tagged(pte) ((pte_val(pte) & PTE_ATTRINDX_MASK) == \
  139. PTE_ATTRINDX(MT_NORMAL_TAGGED))
  140. #define pte_cont_addr_end(addr, end) \
  141. ({ unsigned long __boundary = ((addr) + CONT_PTE_SIZE) & CONT_PTE_MASK; \
  142. (__boundary - 1 < (end) - 1) ? __boundary : (end); \
  143. })
  144. #define pmd_cont_addr_end(addr, end) \
  145. ({ unsigned long __boundary = ((addr) + CONT_PMD_SIZE) & CONT_PMD_MASK; \
  146. (__boundary - 1 < (end) - 1) ? __boundary : (end); \
  147. })
  148. #define pte_hw_dirty(pte) (pte_write(pte) && !pte_rdonly(pte))
  149. #define pte_sw_dirty(pte) (!!(pte_val(pte) & PTE_DIRTY))
  150. #define pte_dirty(pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte))
  151. #define pte_valid(pte) (!!(pte_val(pte) & PTE_VALID))
  152. #define pte_present_invalid(pte) \
  153. ((pte_val(pte) & (PTE_VALID | PTE_PRESENT_INVALID)) == PTE_PRESENT_INVALID)
  154. /*
  155. * Execute-only user mappings do not have the PTE_USER bit set. All valid
  156. * kernel mappings have the PTE_UXN bit set.
  157. */
  158. #define pte_valid_not_user(pte) \
  159. ((pte_val(pte) & (PTE_VALID | PTE_USER | PTE_UXN)) == (PTE_VALID | PTE_UXN))
  160. /*
  161. * Returns true if the pte is valid and has the contiguous bit set.
  162. */
  163. #define pte_valid_cont(pte) (pte_valid(pte) && pte_cont(pte))
  164. /*
  165. * Could the pte be present in the TLB? We must check mm_tlb_flush_pending
  166. * so that we don't erroneously return false for pages that have been
  167. * remapped as PROT_NONE but are yet to be flushed from the TLB.
  168. * Note that we can't make any assumptions based on the state of the access
  169. * flag, since __ptep_clear_flush_young() elides a DSB when invalidating the
  170. * TLB.
  171. */
  172. #define pte_accessible(mm, pte) \
  173. (mm_tlb_flush_pending(mm) ? pte_present(pte) : pte_valid(pte))
  174. static inline bool por_el0_allows_pkey(u8 pkey, bool write, bool execute)
  175. {
  176. u64 por;
  177. if (!system_supports_poe())
  178. return true;
  179. por = read_sysreg_s(SYS_POR_EL0);
  180. if (write)
  181. return por_elx_allows_write(por, pkey);
  182. if (execute)
  183. return por_elx_allows_exec(por, pkey);
  184. return por_elx_allows_read(por, pkey);
  185. }
  186. /*
  187. * p??_access_permitted() is true for valid user mappings (PTE_USER
  188. * bit set, subject to the write permission check). For execute-only
  189. * mappings, like PROT_EXEC with EPAN (both PTE_USER and PTE_UXN bits
  190. * not set) must return false. PROT_NONE mappings do not have the
  191. * PTE_VALID bit set.
  192. */
  193. #define pte_access_permitted_no_overlay(pte, write) \
  194. (((pte_val(pte) & (PTE_VALID | PTE_USER)) == (PTE_VALID | PTE_USER)) && (!(write) || pte_write(pte)))
  195. #define pte_access_permitted(pte, write) \
  196. (pte_access_permitted_no_overlay(pte, write) && \
  197. por_el0_allows_pkey(FIELD_GET(PTE_PO_IDX_MASK, pte_val(pte)), write, false))
  198. #define pmd_access_permitted(pmd, write) \
  199. (pte_access_permitted(pmd_pte(pmd), (write)))
  200. #define pud_access_permitted(pud, write) \
  201. (pte_access_permitted(pud_pte(pud), (write)))
  202. static inline pte_t clear_pte_bit(pte_t pte, pgprot_t prot)
  203. {
  204. pte_val(pte) &= ~pgprot_val(prot);
  205. return pte;
  206. }
  207. static inline pte_t set_pte_bit(pte_t pte, pgprot_t prot)
  208. {
  209. pte_val(pte) |= pgprot_val(prot);
  210. return pte;
  211. }
  212. static inline pmd_t clear_pmd_bit(pmd_t pmd, pgprot_t prot)
  213. {
  214. pmd_val(pmd) &= ~pgprot_val(prot);
  215. return pmd;
  216. }
  217. static inline pmd_t set_pmd_bit(pmd_t pmd, pgprot_t prot)
  218. {
  219. pmd_val(pmd) |= pgprot_val(prot);
  220. return pmd;
  221. }
  222. static inline pte_t pte_mkwrite_novma(pte_t pte)
  223. {
  224. pte = set_pte_bit(pte, __pgprot(PTE_WRITE));
  225. if (pte_sw_dirty(pte))
  226. pte = clear_pte_bit(pte, __pgprot(PTE_RDONLY));
  227. return pte;
  228. }
  229. static inline pte_t pte_mkclean(pte_t pte)
  230. {
  231. pte = clear_pte_bit(pte, __pgprot(PTE_DIRTY));
  232. pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
  233. return pte;
  234. }
  235. static inline pte_t pte_mkdirty(pte_t pte)
  236. {
  237. pte = set_pte_bit(pte, __pgprot(PTE_DIRTY));
  238. if (pte_write(pte))
  239. pte = clear_pte_bit(pte, __pgprot(PTE_RDONLY));
  240. return pte;
  241. }
  242. static inline pte_t pte_wrprotect(pte_t pte)
  243. {
  244. /*
  245. * If hardware-dirty (PTE_WRITE/DBM bit set and PTE_RDONLY
  246. * clear), set the PTE_DIRTY bit.
  247. */
  248. if (pte_hw_dirty(pte))
  249. pte = set_pte_bit(pte, __pgprot(PTE_DIRTY));
  250. pte = clear_pte_bit(pte, __pgprot(PTE_WRITE));
  251. pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
  252. return pte;
  253. }
  254. static inline pte_t pte_mkold(pte_t pte)
  255. {
  256. return clear_pte_bit(pte, __pgprot(PTE_AF));
  257. }
  258. static inline pte_t pte_mkyoung(pte_t pte)
  259. {
  260. return set_pte_bit(pte, __pgprot(PTE_AF));
  261. }
  262. static inline pte_t pte_mkspecial(pte_t pte)
  263. {
  264. return set_pte_bit(pte, __pgprot(PTE_SPECIAL));
  265. }
  266. static inline pte_t pte_mkcont(pte_t pte)
  267. {
  268. return set_pte_bit(pte, __pgprot(PTE_CONT));
  269. }
  270. static inline pte_t pte_mknoncont(pte_t pte)
  271. {
  272. return clear_pte_bit(pte, __pgprot(PTE_CONT));
  273. }
  274. static inline pte_t pte_mkvalid(pte_t pte)
  275. {
  276. return set_pte_bit(pte, __pgprot(PTE_VALID));
  277. }
  278. static inline pte_t pte_mkinvalid(pte_t pte)
  279. {
  280. pte = set_pte_bit(pte, __pgprot(PTE_PRESENT_INVALID));
  281. pte = clear_pte_bit(pte, __pgprot(PTE_VALID));
  282. return pte;
  283. }
  284. static inline pmd_t pmd_mkcont(pmd_t pmd)
  285. {
  286. return __pmd(pmd_val(pmd) | PMD_SECT_CONT);
  287. }
  288. static inline pmd_t pmd_mknoncont(pmd_t pmd)
  289. {
  290. return __pmd(pmd_val(pmd) & ~PMD_SECT_CONT);
  291. }
  292. #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
  293. static inline int pte_uffd_wp(pte_t pte)
  294. {
  295. return !!(pte_val(pte) & PTE_UFFD_WP);
  296. }
  297. static inline pte_t pte_mkuffd_wp(pte_t pte)
  298. {
  299. return pte_wrprotect(set_pte_bit(pte, __pgprot(PTE_UFFD_WP)));
  300. }
  301. static inline pte_t pte_clear_uffd_wp(pte_t pte)
  302. {
  303. return clear_pte_bit(pte, __pgprot(PTE_UFFD_WP));
  304. }
  305. #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
  306. static inline void __set_pte_nosync(pte_t *ptep, pte_t pte)
  307. {
  308. WRITE_ONCE(*ptep, pte);
  309. }
  310. static inline void __set_pte_complete(pte_t pte)
  311. {
  312. /*
  313. * Only if the new pte is valid and kernel, otherwise TLB maintenance
  314. * has the necessary barriers.
  315. */
  316. if (pte_valid_not_user(pte))
  317. queue_pte_barriers();
  318. }
  319. static inline void __set_pte(pte_t *ptep, pte_t pte)
  320. {
  321. __set_pte_nosync(ptep, pte);
  322. __set_pte_complete(pte);
  323. }
  324. static inline pte_t __ptep_get(pte_t *ptep)
  325. {
  326. return READ_ONCE(*ptep);
  327. }
  328. extern void __sync_icache_dcache(pte_t pteval);
  329. bool pgattr_change_is_safe(pteval_t old, pteval_t new);
  330. /*
  331. * PTE bits configuration in the presence of hardware Dirty Bit Management
  332. * (PTE_WRITE == PTE_DBM):
  333. *
  334. * Dirty Writable | PTE_RDONLY PTE_WRITE PTE_DIRTY (sw)
  335. * 0 0 | 1 0 0
  336. * 0 1 | 1 1 0
  337. * 1 0 | 1 0 1
  338. * 1 1 | 0 1 x
  339. *
  340. * When hardware DBM is not present, the software PTE_DIRTY bit is updated via
  341. * the page fault mechanism. Checking the dirty status of a pte becomes:
  342. *
  343. * PTE_DIRTY || (PTE_WRITE && !PTE_RDONLY)
  344. */
  345. static inline void __check_safe_pte_update(struct mm_struct *mm, pte_t *ptep,
  346. pte_t pte)
  347. {
  348. pte_t old_pte;
  349. if (!IS_ENABLED(CONFIG_DEBUG_VM))
  350. return;
  351. old_pte = __ptep_get(ptep);
  352. if (!pte_valid(old_pte) || !pte_valid(pte))
  353. return;
  354. if (mm != current->active_mm && atomic_read(&mm->mm_users) <= 1)
  355. return;
  356. /*
  357. * Check for potential race with hardware updates of the pte
  358. * (__ptep_set_access_flags safely changes valid ptes without going
  359. * through an invalid entry).
  360. */
  361. VM_WARN_ONCE(!pte_young(pte),
  362. "%s: racy access flag clearing: 0x%016llx -> 0x%016llx",
  363. __func__, pte_val(old_pte), pte_val(pte));
  364. VM_WARN_ONCE(pte_write(old_pte) && !pte_dirty(pte),
  365. "%s: racy dirty state clearing: 0x%016llx -> 0x%016llx",
  366. __func__, pte_val(old_pte), pte_val(pte));
  367. VM_WARN_ONCE(!pgattr_change_is_safe(pte_val(old_pte), pte_val(pte)),
  368. "%s: unsafe attribute change: 0x%016llx -> 0x%016llx",
  369. __func__, pte_val(old_pte), pte_val(pte));
  370. }
  371. static inline void __sync_cache_and_tags(pte_t pte, unsigned int nr_pages)
  372. {
  373. if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte))
  374. __sync_icache_dcache(pte);
  375. /*
  376. * If the PTE would provide user space access to the tags associated
  377. * with it then ensure that the MTE tags are synchronised. Although
  378. * pte_access_permitted_no_overlay() returns false for exec only
  379. * mappings, they don't expose tags (instruction fetches don't check
  380. * tags).
  381. */
  382. if (system_supports_mte() && pte_access_permitted_no_overlay(pte, false) &&
  383. !pte_special(pte) && pte_tagged(pte))
  384. mte_sync_tags(pte, nr_pages);
  385. }
  386. /*
  387. * Select all bits except the pfn
  388. */
  389. #define pte_pgprot pte_pgprot
  390. static inline pgprot_t pte_pgprot(pte_t pte)
  391. {
  392. unsigned long pfn = pte_pfn(pte);
  393. return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte));
  394. }
  395. #define pte_advance_pfn pte_advance_pfn
  396. static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr)
  397. {
  398. return pfn_pte(pte_pfn(pte) + nr, pte_pgprot(pte));
  399. }
  400. /*
  401. * Hugetlb definitions.
  402. */
  403. #define HUGE_MAX_HSTATE 4
  404. #define HPAGE_SHIFT PMD_SHIFT
  405. #define HPAGE_SIZE (_AC(1, UL) << HPAGE_SHIFT)
  406. #define HPAGE_MASK (~(HPAGE_SIZE - 1))
  407. #define HUGETLB_PAGE_ORDER (HPAGE_SHIFT - PAGE_SHIFT)
  408. static inline pte_t pgd_pte(pgd_t pgd)
  409. {
  410. return __pte(pgd_val(pgd));
  411. }
  412. static inline pte_t p4d_pte(p4d_t p4d)
  413. {
  414. return __pte(p4d_val(p4d));
  415. }
  416. static inline pte_t pud_pte(pud_t pud)
  417. {
  418. return __pte(pud_val(pud));
  419. }
  420. static inline pud_t pte_pud(pte_t pte)
  421. {
  422. return __pud(pte_val(pte));
  423. }
  424. static inline pmd_t pud_pmd(pud_t pud)
  425. {
  426. return __pmd(pud_val(pud));
  427. }
  428. static inline pte_t pmd_pte(pmd_t pmd)
  429. {
  430. return __pte(pmd_val(pmd));
  431. }
  432. static inline pmd_t pte_pmd(pte_t pte)
  433. {
  434. return __pmd(pte_val(pte));
  435. }
  436. static inline pgprot_t mk_pud_sect_prot(pgprot_t prot)
  437. {
  438. return __pgprot((pgprot_val(prot) & ~PUD_TYPE_MASK) | PUD_TYPE_SECT);
  439. }
  440. static inline pgprot_t mk_pmd_sect_prot(pgprot_t prot)
  441. {
  442. return __pgprot((pgprot_val(prot) & ~PMD_TYPE_MASK) | PMD_TYPE_SECT);
  443. }
  444. static inline pte_t pte_swp_mkexclusive(pte_t pte)
  445. {
  446. return set_pte_bit(pte, __pgprot(PTE_SWP_EXCLUSIVE));
  447. }
  448. static inline bool pte_swp_exclusive(pte_t pte)
  449. {
  450. return pte_val(pte) & PTE_SWP_EXCLUSIVE;
  451. }
  452. static inline pte_t pte_swp_clear_exclusive(pte_t pte)
  453. {
  454. return clear_pte_bit(pte, __pgprot(PTE_SWP_EXCLUSIVE));
  455. }
  456. #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
  457. static inline pte_t pte_swp_mkuffd_wp(pte_t pte)
  458. {
  459. return set_pte_bit(pte, __pgprot(PTE_SWP_UFFD_WP));
  460. }
  461. static inline int pte_swp_uffd_wp(pte_t pte)
  462. {
  463. return !!(pte_val(pte) & PTE_SWP_UFFD_WP);
  464. }
  465. static inline pte_t pte_swp_clear_uffd_wp(pte_t pte)
  466. {
  467. return clear_pte_bit(pte, __pgprot(PTE_SWP_UFFD_WP));
  468. }
  469. #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
  470. #ifdef CONFIG_NUMA_BALANCING
  471. /*
  472. * See the comment in include/linux/pgtable.h
  473. */
  474. static inline int pte_protnone(pte_t pte)
  475. {
  476. /*
  477. * pte_present_invalid() tells us that the pte is invalid from HW
  478. * perspective but present from SW perspective, so the fields are to be
  479. * interpreted as per the HW layout. The second 2 checks are the unique
  480. * encoding that we use for PROT_NONE. It is insufficient to only use
  481. * the first check because we share the same encoding scheme with pmds
  482. * which support pmd_mkinvalid(), so can be present-invalid without
  483. * being PROT_NONE.
  484. */
  485. return pte_present_invalid(pte) && !pte_user(pte) && !pte_user_exec(pte);
  486. }
  487. static inline int pmd_protnone(pmd_t pmd)
  488. {
  489. return pte_protnone(pmd_pte(pmd));
  490. }
  491. #endif
  492. #define pmd_present(pmd) pte_present(pmd_pte(pmd))
  493. #define pmd_dirty(pmd) pte_dirty(pmd_pte(pmd))
  494. #define pmd_young(pmd) pte_young(pmd_pte(pmd))
  495. #define pmd_valid(pmd) pte_valid(pmd_pte(pmd))
  496. #define pmd_user(pmd) pte_user(pmd_pte(pmd))
  497. #define pmd_user_exec(pmd) pte_user_exec(pmd_pte(pmd))
  498. #define pmd_cont(pmd) pte_cont(pmd_pte(pmd))
  499. #define pmd_wrprotect(pmd) pte_pmd(pte_wrprotect(pmd_pte(pmd)))
  500. #define pmd_mkold(pmd) pte_pmd(pte_mkold(pmd_pte(pmd)))
  501. #define pmd_mkwrite_novma(pmd) pte_pmd(pte_mkwrite_novma(pmd_pte(pmd)))
  502. #define pmd_mkclean(pmd) pte_pmd(pte_mkclean(pmd_pte(pmd)))
  503. #define pmd_mkdirty(pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd)))
  504. #define pmd_mkyoung(pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd)))
  505. #define pmd_mkinvalid(pmd) pte_pmd(pte_mkinvalid(pmd_pte(pmd)))
  506. #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
  507. #define pmd_uffd_wp(pmd) pte_uffd_wp(pmd_pte(pmd))
  508. #define pmd_mkuffd_wp(pmd) pte_pmd(pte_mkuffd_wp(pmd_pte(pmd)))
  509. #define pmd_clear_uffd_wp(pmd) pte_pmd(pte_clear_uffd_wp(pmd_pte(pmd)))
  510. #define pmd_swp_uffd_wp(pmd) pte_swp_uffd_wp(pmd_pte(pmd))
  511. #define pmd_swp_mkuffd_wp(pmd) pte_pmd(pte_swp_mkuffd_wp(pmd_pte(pmd)))
  512. #define pmd_swp_clear_uffd_wp(pmd) \
  513. pte_pmd(pte_swp_clear_uffd_wp(pmd_pte(pmd)))
  514. #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
  515. #define pmd_write(pmd) pte_write(pmd_pte(pmd))
  516. static inline pmd_t pmd_mkhuge(pmd_t pmd)
  517. {
  518. /*
  519. * It's possible that the pmd is present-invalid on entry
  520. * and in that case it needs to remain present-invalid on
  521. * exit. So ensure the VALID bit does not get modified.
  522. */
  523. pmdval_t mask = PMD_TYPE_MASK & ~PTE_VALID;
  524. pmdval_t val = PMD_TYPE_SECT & ~PTE_VALID;
  525. return __pmd((pmd_val(pmd) & ~mask) | val);
  526. }
  527. #ifdef CONFIG_ARCH_SUPPORTS_PMD_PFNMAP
  528. #define pmd_special(pte) (!!((pmd_val(pte) & PTE_SPECIAL)))
  529. static inline pmd_t pmd_mkspecial(pmd_t pmd)
  530. {
  531. return set_pmd_bit(pmd, __pgprot(PTE_SPECIAL));
  532. }
  533. #endif
  534. #define __pmd_to_phys(pmd) __pte_to_phys(pmd_pte(pmd))
  535. #define __phys_to_pmd_val(phys) __phys_to_pte_val(phys)
  536. #define pmd_pfn(pmd) ((__pmd_to_phys(pmd) & PMD_MASK) >> PAGE_SHIFT)
  537. #define pfn_pmd(pfn,prot) __pmd(__phys_to_pmd_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot))
  538. #define pud_young(pud) pte_young(pud_pte(pud))
  539. #define pud_mkyoung(pud) pte_pud(pte_mkyoung(pud_pte(pud)))
  540. #define pud_write(pud) pte_write(pud_pte(pud))
  541. static inline pud_t pud_mkhuge(pud_t pud)
  542. {
  543. /*
  544. * It's possible that the pud is present-invalid on entry
  545. * and in that case it needs to remain present-invalid on
  546. * exit. So ensure the VALID bit does not get modified.
  547. */
  548. pudval_t mask = PUD_TYPE_MASK & ~PTE_VALID;
  549. pudval_t val = PUD_TYPE_SECT & ~PTE_VALID;
  550. return __pud((pud_val(pud) & ~mask) | val);
  551. }
  552. #define __pud_to_phys(pud) __pte_to_phys(pud_pte(pud))
  553. #define __phys_to_pud_val(phys) __phys_to_pte_val(phys)
  554. #define pud_pfn(pud) ((__pud_to_phys(pud) & PUD_MASK) >> PAGE_SHIFT)
  555. #define pfn_pud(pfn,prot) __pud(__phys_to_pud_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot))
  556. #define pmd_pgprot pmd_pgprot
  557. static inline pgprot_t pmd_pgprot(pmd_t pmd)
  558. {
  559. unsigned long pfn = pmd_pfn(pmd);
  560. return __pgprot(pmd_val(pfn_pmd(pfn, __pgprot(0))) ^ pmd_val(pmd));
  561. }
  562. #define pud_pgprot pud_pgprot
  563. static inline pgprot_t pud_pgprot(pud_t pud)
  564. {
  565. unsigned long pfn = pud_pfn(pud);
  566. return __pgprot(pud_val(pfn_pud(pfn, __pgprot(0))) ^ pud_val(pud));
  567. }
  568. static inline void __set_ptes_anysz(struct mm_struct *mm, unsigned long addr,
  569. pte_t *ptep, pte_t pte, unsigned int nr,
  570. unsigned long pgsize)
  571. {
  572. unsigned long stride = pgsize >> PAGE_SHIFT;
  573. switch (pgsize) {
  574. case PAGE_SIZE:
  575. page_table_check_ptes_set(mm, addr, ptep, pte, nr);
  576. break;
  577. case PMD_SIZE:
  578. page_table_check_pmds_set(mm, addr, (pmd_t *)ptep,
  579. pte_pmd(pte), nr);
  580. break;
  581. #ifndef __PAGETABLE_PMD_FOLDED
  582. case PUD_SIZE:
  583. page_table_check_puds_set(mm, addr, (pud_t *)ptep,
  584. pte_pud(pte), nr);
  585. break;
  586. #endif
  587. default:
  588. VM_WARN_ON(1);
  589. }
  590. __sync_cache_and_tags(pte, nr * stride);
  591. for (;;) {
  592. __check_safe_pte_update(mm, ptep, pte);
  593. __set_pte_nosync(ptep, pte);
  594. if (--nr == 0)
  595. break;
  596. ptep++;
  597. pte = pte_advance_pfn(pte, stride);
  598. }
  599. __set_pte_complete(pte);
  600. }
  601. static inline void __set_ptes(struct mm_struct *mm, unsigned long addr,
  602. pte_t *ptep, pte_t pte, unsigned int nr)
  603. {
  604. __set_ptes_anysz(mm, addr, ptep, pte, nr, PAGE_SIZE);
  605. }
  606. static inline void __set_pmds(struct mm_struct *mm, unsigned long addr,
  607. pmd_t *pmdp, pmd_t pmd, unsigned int nr)
  608. {
  609. __set_ptes_anysz(mm, addr, (pte_t *)pmdp, pmd_pte(pmd), nr, PMD_SIZE);
  610. }
  611. #define set_pmd_at(mm, addr, pmdp, pmd) __set_pmds(mm, addr, pmdp, pmd, 1)
  612. static inline void __set_puds(struct mm_struct *mm, unsigned long addr,
  613. pud_t *pudp, pud_t pud, unsigned int nr)
  614. {
  615. __set_ptes_anysz(mm, addr, (pte_t *)pudp, pud_pte(pud), nr, PUD_SIZE);
  616. }
  617. #define set_pud_at(mm, addr, pudp, pud) __set_puds(mm, addr, pudp, pud, 1)
  618. #define __p4d_to_phys(p4d) __pte_to_phys(p4d_pte(p4d))
  619. #define __phys_to_p4d_val(phys) __phys_to_pte_val(phys)
  620. #define __pgd_to_phys(pgd) __pte_to_phys(pgd_pte(pgd))
  621. #define __phys_to_pgd_val(phys) __phys_to_pte_val(phys)
  622. #define __pgprot_modify(prot,mask,bits) \
  623. __pgprot((pgprot_val(prot) & ~(mask)) | (bits))
  624. #define pgprot_nx(prot) \
  625. __pgprot_modify(prot, PTE_MAYBE_GP, PTE_PXN)
  626. #define pgprot_decrypted(prot) \
  627. __pgprot_modify(prot, PROT_NS_SHARED, PROT_NS_SHARED)
  628. #define pgprot_encrypted(prot) \
  629. __pgprot_modify(prot, PROT_NS_SHARED, 0)
  630. /*
  631. * Mark the prot value as uncacheable and unbufferable.
  632. */
  633. #define pgprot_noncached(prot) \
  634. __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_DEVICE_nGnRnE) | PTE_PXN | PTE_UXN)
  635. #define pgprot_writecombine(prot) \
  636. __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_NORMAL_NC) | PTE_PXN | PTE_UXN)
  637. #define pgprot_device(prot) \
  638. __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_DEVICE_nGnRE) | PTE_PXN | PTE_UXN)
  639. #define pgprot_tagged(prot) \
  640. __pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_NORMAL_TAGGED))
  641. #define pgprot_mhp pgprot_tagged
  642. /*
  643. * DMA allocations for non-coherent devices use what the Arm architecture calls
  644. * "Normal non-cacheable" memory, which permits speculation, unaligned accesses
  645. * and merging of writes. This is different from "Device-nGnR[nE]" memory which
  646. * is intended for MMIO and thus forbids speculation, preserves access size,
  647. * requires strict alignment and can also force write responses to come from the
  648. * endpoint.
  649. */
  650. #define pgprot_dmacoherent(prot) \
  651. __pgprot_modify(prot, PTE_ATTRINDX_MASK, \
  652. PTE_ATTRINDX(MT_NORMAL_NC) | PTE_PXN | PTE_UXN)
  653. #define __HAVE_PHYS_MEM_ACCESS_PROT
  654. struct file;
  655. extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  656. unsigned long size, pgprot_t vma_prot);
  657. #define pmd_none(pmd) (!pmd_val(pmd))
  658. #define pmd_table(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \
  659. PMD_TYPE_TABLE)
  660. #define pmd_sect(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \
  661. PMD_TYPE_SECT)
  662. #define pmd_leaf(pmd) (pmd_present(pmd) && !pmd_table(pmd))
  663. #define pmd_bad(pmd) (!pmd_table(pmd))
  664. #define pmd_leaf_size(pmd) (pmd_cont(pmd) ? CONT_PMD_SIZE : PMD_SIZE)
  665. #define pte_leaf_size(pte) (pte_cont(pte) ? CONT_PTE_SIZE : PAGE_SIZE)
  666. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  667. static inline int pmd_trans_huge(pmd_t pmd)
  668. {
  669. /*
  670. * If pmd is present-invalid, pmd_table() won't detect it
  671. * as a table, so force the valid bit for the comparison.
  672. */
  673. return pmd_present(pmd) && !pmd_table(__pmd(pmd_val(pmd) | PTE_VALID));
  674. }
  675. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  676. #if defined(CONFIG_ARM64_64K_PAGES) || CONFIG_PGTABLE_LEVELS < 3
  677. static inline bool pud_sect(pud_t pud) { return false; }
  678. static inline bool pud_table(pud_t pud) { return true; }
  679. #else
  680. #define pud_sect(pud) ((pud_val(pud) & PUD_TYPE_MASK) == \
  681. PUD_TYPE_SECT)
  682. #define pud_table(pud) ((pud_val(pud) & PUD_TYPE_MASK) == \
  683. PUD_TYPE_TABLE)
  684. #endif
  685. extern pgd_t swapper_pg_dir[];
  686. extern pgd_t idmap_pg_dir[];
  687. extern pgd_t tramp_pg_dir[];
  688. extern pgd_t reserved_pg_dir[];
  689. extern void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd);
  690. static inline bool in_swapper_pgdir(void *addr)
  691. {
  692. return ((unsigned long)addr & PAGE_MASK) ==
  693. ((unsigned long)swapper_pg_dir & PAGE_MASK);
  694. }
  695. static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
  696. {
  697. #ifdef __PAGETABLE_PMD_FOLDED
  698. if (in_swapper_pgdir(pmdp)) {
  699. set_swapper_pgd((pgd_t *)pmdp, __pgd(pmd_val(pmd)));
  700. return;
  701. }
  702. #endif /* __PAGETABLE_PMD_FOLDED */
  703. WRITE_ONCE(*pmdp, pmd);
  704. if (pmd_valid(pmd))
  705. queue_pte_barriers();
  706. }
  707. static inline void pmd_clear(pmd_t *pmdp)
  708. {
  709. set_pmd(pmdp, __pmd(0));
  710. }
  711. static inline phys_addr_t pmd_page_paddr(pmd_t pmd)
  712. {
  713. return __pmd_to_phys(pmd);
  714. }
  715. static inline unsigned long pmd_page_vaddr(pmd_t pmd)
  716. {
  717. return (unsigned long)__va(pmd_page_paddr(pmd));
  718. }
  719. /* Find an entry in the third-level page table. */
  720. #define pte_offset_phys(dir,addr) (pmd_page_paddr(READ_ONCE(*(dir))) + pte_index(addr) * sizeof(pte_t))
  721. #define pte_set_fixmap(addr) ((pte_t *)set_fixmap_offset(FIX_PTE, addr))
  722. #define pte_set_fixmap_offset(pmd, addr) pte_set_fixmap(pte_offset_phys(pmd, addr))
  723. #define pte_clear_fixmap() clear_fixmap(FIX_PTE)
  724. #define pmd_page(pmd) phys_to_page(__pmd_to_phys(pmd))
  725. /* use ONLY for statically allocated translation tables */
  726. #define pte_offset_kimg(dir,addr) ((pte_t *)__phys_to_kimg(pte_offset_phys((dir), (addr))))
  727. #if CONFIG_PGTABLE_LEVELS > 2
  728. #define pmd_ERROR(e) \
  729. pr_err("%s:%d: bad pmd %016llx.\n", __FILE__, __LINE__, pmd_val(e))
  730. #define pud_none(pud) (!pud_val(pud))
  731. #define pud_bad(pud) ((pud_val(pud) & PUD_TYPE_MASK) != \
  732. PUD_TYPE_TABLE)
  733. #define pud_present(pud) pte_present(pud_pte(pud))
  734. #ifndef __PAGETABLE_PMD_FOLDED
  735. #define pud_leaf(pud) (pud_present(pud) && !pud_table(pud))
  736. #else
  737. #define pud_leaf(pud) false
  738. #endif
  739. #define pud_valid(pud) pte_valid(pud_pte(pud))
  740. #define pud_user(pud) pte_user(pud_pte(pud))
  741. #define pud_user_exec(pud) pte_user_exec(pud_pte(pud))
  742. static inline bool pgtable_l4_enabled(void);
  743. static inline void set_pud(pud_t *pudp, pud_t pud)
  744. {
  745. if (!pgtable_l4_enabled() && in_swapper_pgdir(pudp)) {
  746. set_swapper_pgd((pgd_t *)pudp, __pgd(pud_val(pud)));
  747. return;
  748. }
  749. WRITE_ONCE(*pudp, pud);
  750. if (pud_valid(pud))
  751. queue_pte_barriers();
  752. }
  753. static inline void pud_clear(pud_t *pudp)
  754. {
  755. set_pud(pudp, __pud(0));
  756. }
  757. static inline phys_addr_t pud_page_paddr(pud_t pud)
  758. {
  759. return __pud_to_phys(pud);
  760. }
  761. static inline pmd_t *pud_pgtable(pud_t pud)
  762. {
  763. return (pmd_t *)__va(pud_page_paddr(pud));
  764. }
  765. /* Find an entry in the second-level page table. */
  766. #define pmd_offset_phys(dir, addr) (pud_page_paddr(READ_ONCE(*(dir))) + pmd_index(addr) * sizeof(pmd_t))
  767. #define pmd_set_fixmap(addr) ((pmd_t *)set_fixmap_offset(FIX_PMD, addr))
  768. #define pmd_set_fixmap_offset(pud, addr) pmd_set_fixmap(pmd_offset_phys(pud, addr))
  769. #define pmd_clear_fixmap() clear_fixmap(FIX_PMD)
  770. #define pud_page(pud) phys_to_page(__pud_to_phys(pud))
  771. /* use ONLY for statically allocated translation tables */
  772. #define pmd_offset_kimg(dir,addr) ((pmd_t *)__phys_to_kimg(pmd_offset_phys((dir), (addr))))
  773. #else
  774. #define pud_valid(pud) false
  775. #define pud_page_paddr(pud) ({ BUILD_BUG(); 0; })
  776. #define pud_user_exec(pud) pud_user(pud) /* Always 0 with folding */
  777. /* Match pmd_offset folding in <asm/generic/pgtable-nopmd.h> */
  778. #define pmd_set_fixmap(addr) NULL
  779. #define pmd_set_fixmap_offset(pudp, addr) ((pmd_t *)pudp)
  780. #define pmd_clear_fixmap()
  781. #define pmd_offset_kimg(dir,addr) ((pmd_t *)dir)
  782. #endif /* CONFIG_PGTABLE_LEVELS > 2 */
  783. #if CONFIG_PGTABLE_LEVELS > 3
  784. static __always_inline bool pgtable_l4_enabled(void)
  785. {
  786. if (CONFIG_PGTABLE_LEVELS > 4 || !IS_ENABLED(CONFIG_ARM64_LPA2))
  787. return true;
  788. if (!alternative_has_cap_likely(ARM64_ALWAYS_BOOT))
  789. return vabits_actual == VA_BITS;
  790. return alternative_has_cap_unlikely(ARM64_HAS_VA52);
  791. }
  792. static inline bool mm_pud_folded(const struct mm_struct *mm)
  793. {
  794. return !pgtable_l4_enabled();
  795. }
  796. #define mm_pud_folded mm_pud_folded
  797. #define pud_ERROR(e) \
  798. pr_err("%s:%d: bad pud %016llx.\n", __FILE__, __LINE__, pud_val(e))
  799. #define p4d_none(p4d) (pgtable_l4_enabled() && !p4d_val(p4d))
  800. #define p4d_bad(p4d) (pgtable_l4_enabled() && \
  801. ((p4d_val(p4d) & P4D_TYPE_MASK) != \
  802. P4D_TYPE_TABLE))
  803. #define p4d_present(p4d) (!p4d_none(p4d))
  804. static inline void set_p4d(p4d_t *p4dp, p4d_t p4d)
  805. {
  806. if (in_swapper_pgdir(p4dp)) {
  807. set_swapper_pgd((pgd_t *)p4dp, __pgd(p4d_val(p4d)));
  808. return;
  809. }
  810. WRITE_ONCE(*p4dp, p4d);
  811. queue_pte_barriers();
  812. }
  813. static inline void p4d_clear(p4d_t *p4dp)
  814. {
  815. if (pgtable_l4_enabled())
  816. set_p4d(p4dp, __p4d(0));
  817. }
  818. static inline phys_addr_t p4d_page_paddr(p4d_t p4d)
  819. {
  820. return __p4d_to_phys(p4d);
  821. }
  822. #define pud_index(addr) (((addr) >> PUD_SHIFT) & (PTRS_PER_PUD - 1))
  823. static inline pud_t *p4d_to_folded_pud(p4d_t *p4dp, unsigned long addr)
  824. {
  825. /* Ensure that 'p4dp' indexes a page table according to 'addr' */
  826. VM_BUG_ON(((addr >> P4D_SHIFT) ^ ((u64)p4dp >> 3)) % PTRS_PER_P4D);
  827. return (pud_t *)PTR_ALIGN_DOWN(p4dp, PAGE_SIZE) + pud_index(addr);
  828. }
  829. static inline pud_t *p4d_pgtable(p4d_t p4d)
  830. {
  831. return (pud_t *)__va(p4d_page_paddr(p4d));
  832. }
  833. static inline phys_addr_t pud_offset_phys(p4d_t *p4dp, unsigned long addr)
  834. {
  835. BUG_ON(!pgtable_l4_enabled());
  836. return p4d_page_paddr(READ_ONCE(*p4dp)) + pud_index(addr) * sizeof(pud_t);
  837. }
  838. static inline
  839. pud_t *pud_offset_lockless(p4d_t *p4dp, p4d_t p4d, unsigned long addr)
  840. {
  841. if (!pgtable_l4_enabled())
  842. return p4d_to_folded_pud(p4dp, addr);
  843. return (pud_t *)__va(p4d_page_paddr(p4d)) + pud_index(addr);
  844. }
  845. #define pud_offset_lockless pud_offset_lockless
  846. static inline pud_t *pud_offset(p4d_t *p4dp, unsigned long addr)
  847. {
  848. return pud_offset_lockless(p4dp, READ_ONCE(*p4dp), addr);
  849. }
  850. #define pud_offset pud_offset
  851. static inline pud_t *pud_set_fixmap(unsigned long addr)
  852. {
  853. if (!pgtable_l4_enabled())
  854. return NULL;
  855. return (pud_t *)set_fixmap_offset(FIX_PUD, addr);
  856. }
  857. static inline pud_t *pud_set_fixmap_offset(p4d_t *p4dp, unsigned long addr)
  858. {
  859. if (!pgtable_l4_enabled())
  860. return p4d_to_folded_pud(p4dp, addr);
  861. return pud_set_fixmap(pud_offset_phys(p4dp, addr));
  862. }
  863. static inline void pud_clear_fixmap(void)
  864. {
  865. if (pgtable_l4_enabled())
  866. clear_fixmap(FIX_PUD);
  867. }
  868. /* use ONLY for statically allocated translation tables */
  869. static inline pud_t *pud_offset_kimg(p4d_t *p4dp, u64 addr)
  870. {
  871. if (!pgtable_l4_enabled())
  872. return p4d_to_folded_pud(p4dp, addr);
  873. return (pud_t *)__phys_to_kimg(pud_offset_phys(p4dp, addr));
  874. }
  875. #define p4d_page(p4d) pfn_to_page(__phys_to_pfn(__p4d_to_phys(p4d)))
  876. #else
  877. static inline bool pgtable_l4_enabled(void) { return false; }
  878. #define p4d_page_paddr(p4d) ({ BUILD_BUG(); 0;})
  879. /* Match pud_offset folding in <asm/generic/pgtable-nopud.h> */
  880. #define pud_set_fixmap(addr) NULL
  881. #define pud_set_fixmap_offset(pgdp, addr) ((pud_t *)pgdp)
  882. #define pud_clear_fixmap()
  883. #define pud_offset_kimg(dir,addr) ((pud_t *)dir)
  884. #endif /* CONFIG_PGTABLE_LEVELS > 3 */
  885. #if CONFIG_PGTABLE_LEVELS > 4
  886. static __always_inline bool pgtable_l5_enabled(void)
  887. {
  888. if (!alternative_has_cap_likely(ARM64_ALWAYS_BOOT))
  889. return vabits_actual == VA_BITS;
  890. return alternative_has_cap_unlikely(ARM64_HAS_VA52);
  891. }
  892. static inline bool mm_p4d_folded(const struct mm_struct *mm)
  893. {
  894. return !pgtable_l5_enabled();
  895. }
  896. #define mm_p4d_folded mm_p4d_folded
  897. #define p4d_ERROR(e) \
  898. pr_err("%s:%d: bad p4d %016llx.\n", __FILE__, __LINE__, p4d_val(e))
  899. #define pgd_none(pgd) (pgtable_l5_enabled() && !pgd_val(pgd))
  900. #define pgd_bad(pgd) (pgtable_l5_enabled() && \
  901. ((pgd_val(pgd) & PGD_TYPE_MASK) != \
  902. PGD_TYPE_TABLE))
  903. #define pgd_present(pgd) (!pgd_none(pgd))
  904. static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)
  905. {
  906. if (in_swapper_pgdir(pgdp)) {
  907. set_swapper_pgd(pgdp, __pgd(pgd_val(pgd)));
  908. return;
  909. }
  910. WRITE_ONCE(*pgdp, pgd);
  911. queue_pte_barriers();
  912. }
  913. static inline void pgd_clear(pgd_t *pgdp)
  914. {
  915. if (pgtable_l5_enabled())
  916. set_pgd(pgdp, __pgd(0));
  917. }
  918. static inline phys_addr_t pgd_page_paddr(pgd_t pgd)
  919. {
  920. return __pgd_to_phys(pgd);
  921. }
  922. #define p4d_index(addr) (((addr) >> P4D_SHIFT) & (PTRS_PER_P4D - 1))
  923. static inline p4d_t *pgd_to_folded_p4d(pgd_t *pgdp, unsigned long addr)
  924. {
  925. /* Ensure that 'pgdp' indexes a page table according to 'addr' */
  926. VM_BUG_ON(((addr >> PGDIR_SHIFT) ^ ((u64)pgdp >> 3)) % PTRS_PER_PGD);
  927. return (p4d_t *)PTR_ALIGN_DOWN(pgdp, PAGE_SIZE) + p4d_index(addr);
  928. }
  929. static inline phys_addr_t p4d_offset_phys(pgd_t *pgdp, unsigned long addr)
  930. {
  931. BUG_ON(!pgtable_l5_enabled());
  932. return pgd_page_paddr(READ_ONCE(*pgdp)) + p4d_index(addr) * sizeof(p4d_t);
  933. }
  934. static inline
  935. p4d_t *p4d_offset_lockless(pgd_t *pgdp, pgd_t pgd, unsigned long addr)
  936. {
  937. if (!pgtable_l5_enabled())
  938. return pgd_to_folded_p4d(pgdp, addr);
  939. return (p4d_t *)__va(pgd_page_paddr(pgd)) + p4d_index(addr);
  940. }
  941. #define p4d_offset_lockless p4d_offset_lockless
  942. static inline p4d_t *p4d_offset(pgd_t *pgdp, unsigned long addr)
  943. {
  944. return p4d_offset_lockless(pgdp, READ_ONCE(*pgdp), addr);
  945. }
  946. static inline p4d_t *p4d_set_fixmap(unsigned long addr)
  947. {
  948. if (!pgtable_l5_enabled())
  949. return NULL;
  950. return (p4d_t *)set_fixmap_offset(FIX_P4D, addr);
  951. }
  952. static inline p4d_t *p4d_set_fixmap_offset(pgd_t *pgdp, unsigned long addr)
  953. {
  954. if (!pgtable_l5_enabled())
  955. return pgd_to_folded_p4d(pgdp, addr);
  956. return p4d_set_fixmap(p4d_offset_phys(pgdp, addr));
  957. }
  958. static inline void p4d_clear_fixmap(void)
  959. {
  960. if (pgtable_l5_enabled())
  961. clear_fixmap(FIX_P4D);
  962. }
  963. /* use ONLY for statically allocated translation tables */
  964. static inline p4d_t *p4d_offset_kimg(pgd_t *pgdp, u64 addr)
  965. {
  966. if (!pgtable_l5_enabled())
  967. return pgd_to_folded_p4d(pgdp, addr);
  968. return (p4d_t *)__phys_to_kimg(p4d_offset_phys(pgdp, addr));
  969. }
  970. #define pgd_page(pgd) pfn_to_page(__phys_to_pfn(__pgd_to_phys(pgd)))
  971. #else
  972. static inline bool pgtable_l5_enabled(void) { return false; }
  973. #define p4d_index(addr) (((addr) >> P4D_SHIFT) & (PTRS_PER_P4D - 1))
  974. /* Match p4d_offset folding in <asm/generic/pgtable-nop4d.h> */
  975. #define p4d_set_fixmap(addr) NULL
  976. #define p4d_set_fixmap_offset(p4dp, addr) ((p4d_t *)p4dp)
  977. #define p4d_clear_fixmap()
  978. #define p4d_offset_kimg(dir,addr) ((p4d_t *)dir)
  979. static inline
  980. p4d_t *p4d_offset_lockless_folded(pgd_t *pgdp, pgd_t pgd, unsigned long addr)
  981. {
  982. /*
  983. * With runtime folding of the pud, pud_offset_lockless() passes
  984. * the 'pgd_t *' we return here to p4d_to_folded_pud(), which
  985. * will offset the pointer assuming that it points into
  986. * a page-table page. However, the fast GUP path passes us a
  987. * pgd_t allocated on the stack and so we must use the original
  988. * pointer in 'pgdp' to construct the p4d pointer instead of
  989. * using the generic p4d_offset_lockless() implementation.
  990. *
  991. * Note: reusing the original pointer means that we may
  992. * dereference the same (live) page-table entry multiple times.
  993. * This is safe because it is still only loaded once in the
  994. * context of each level and the CPU guarantees same-address
  995. * read-after-read ordering.
  996. */
  997. return p4d_offset(pgdp, addr);
  998. }
  999. #define p4d_offset_lockless p4d_offset_lockless_folded
  1000. #endif /* CONFIG_PGTABLE_LEVELS > 4 */
  1001. #define pgd_ERROR(e) \
  1002. pr_err("%s:%d: bad pgd %016llx.\n", __FILE__, __LINE__, pgd_val(e))
  1003. #define pgd_set_fixmap(addr) ((pgd_t *)set_fixmap_offset(FIX_PGD, addr))
  1004. #define pgd_clear_fixmap() clear_fixmap(FIX_PGD)
  1005. static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  1006. {
  1007. /*
  1008. * Normal and Normal-Tagged are two different memory types and indices
  1009. * in MAIR_EL1. The mask below has to include PTE_ATTRINDX_MASK.
  1010. */
  1011. const pteval_t mask = PTE_USER | PTE_PXN | PTE_UXN | PTE_RDONLY |
  1012. PTE_PRESENT_INVALID | PTE_VALID | PTE_WRITE |
  1013. PTE_GP | PTE_ATTRINDX_MASK | PTE_PO_IDX_MASK;
  1014. /* preserve the hardware dirty information */
  1015. if (pte_hw_dirty(pte))
  1016. pte = set_pte_bit(pte, __pgprot(PTE_DIRTY));
  1017. pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask);
  1018. /*
  1019. * If we end up clearing hw dirtiness for a sw-dirty PTE, set hardware
  1020. * dirtiness again.
  1021. */
  1022. if (pte_sw_dirty(pte))
  1023. pte = pte_mkdirty(pte);
  1024. return pte;
  1025. }
  1026. static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
  1027. {
  1028. return pte_pmd(pte_modify(pmd_pte(pmd), newprot));
  1029. }
  1030. extern int __ptep_set_access_flags(struct vm_area_struct *vma,
  1031. unsigned long address, pte_t *ptep,
  1032. pte_t entry, int dirty);
  1033. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1034. #define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
  1035. static inline int pmdp_set_access_flags(struct vm_area_struct *vma,
  1036. unsigned long address, pmd_t *pmdp,
  1037. pmd_t entry, int dirty)
  1038. {
  1039. return __ptep_set_access_flags(vma, address, (pte_t *)pmdp,
  1040. pmd_pte(entry), dirty);
  1041. }
  1042. #endif
  1043. #ifdef CONFIG_PAGE_TABLE_CHECK
  1044. static inline bool pte_user_accessible_page(pte_t pte, unsigned long addr)
  1045. {
  1046. return pte_valid(pte) && (pte_user(pte) || pte_user_exec(pte));
  1047. }
  1048. static inline bool pmd_user_accessible_page(pmd_t pmd, unsigned long addr)
  1049. {
  1050. return pmd_valid(pmd) && !pmd_table(pmd) && (pmd_user(pmd) || pmd_user_exec(pmd));
  1051. }
  1052. static inline bool pud_user_accessible_page(pud_t pud, unsigned long addr)
  1053. {
  1054. return pud_valid(pud) && !pud_table(pud) && (pud_user(pud) || pud_user_exec(pud));
  1055. }
  1056. #endif
  1057. /*
  1058. * Atomic pte/pmd modifications.
  1059. */
  1060. static inline void __pte_clear(struct mm_struct *mm,
  1061. unsigned long addr, pte_t *ptep)
  1062. {
  1063. __set_pte(ptep, __pte(0));
  1064. }
  1065. static inline int __ptep_test_and_clear_young(struct vm_area_struct *vma,
  1066. unsigned long address,
  1067. pte_t *ptep)
  1068. {
  1069. pte_t old_pte, pte;
  1070. pte = __ptep_get(ptep);
  1071. do {
  1072. old_pte = pte;
  1073. pte = pte_mkold(pte);
  1074. pte_val(pte) = cmpxchg_relaxed(&pte_val(*ptep),
  1075. pte_val(old_pte), pte_val(pte));
  1076. } while (pte_val(pte) != pte_val(old_pte));
  1077. return pte_young(pte);
  1078. }
  1079. static inline int __ptep_clear_flush_young(struct vm_area_struct *vma,
  1080. unsigned long address, pte_t *ptep)
  1081. {
  1082. int young = __ptep_test_and_clear_young(vma, address, ptep);
  1083. if (young) {
  1084. /*
  1085. * We can elide the trailing DSB here since the worst that can
  1086. * happen is that a CPU continues to use the young entry in its
  1087. * TLB and we mistakenly reclaim the associated page. The
  1088. * window for such an event is bounded by the next
  1089. * context-switch, which provides a DSB to complete the TLB
  1090. * invalidation.
  1091. */
  1092. flush_tlb_page_nosync(vma, address);
  1093. }
  1094. return young;
  1095. }
  1096. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG)
  1097. #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
  1098. static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  1099. unsigned long address,
  1100. pmd_t *pmdp)
  1101. {
  1102. /* Operation applies to PMD table entry only if FEAT_HAFT is enabled */
  1103. VM_WARN_ON(pmd_table(READ_ONCE(*pmdp)) && !system_supports_haft());
  1104. return __ptep_test_and_clear_young(vma, address, (pte_t *)pmdp);
  1105. }
  1106. #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */
  1107. static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm,
  1108. unsigned long address,
  1109. pte_t *ptep,
  1110. unsigned long pgsize)
  1111. {
  1112. pte_t pte = __pte(xchg_relaxed(&pte_val(*ptep), 0));
  1113. switch (pgsize) {
  1114. case PAGE_SIZE:
  1115. page_table_check_pte_clear(mm, address, pte);
  1116. break;
  1117. case PMD_SIZE:
  1118. page_table_check_pmd_clear(mm, address, pte_pmd(pte));
  1119. break;
  1120. #ifndef __PAGETABLE_PMD_FOLDED
  1121. case PUD_SIZE:
  1122. page_table_check_pud_clear(mm, address, pte_pud(pte));
  1123. break;
  1124. #endif
  1125. default:
  1126. VM_WARN_ON(1);
  1127. }
  1128. return pte;
  1129. }
  1130. static inline pte_t __ptep_get_and_clear(struct mm_struct *mm,
  1131. unsigned long address, pte_t *ptep)
  1132. {
  1133. return __ptep_get_and_clear_anysz(mm, address, ptep, PAGE_SIZE);
  1134. }
  1135. static inline void __clear_full_ptes(struct mm_struct *mm, unsigned long addr,
  1136. pte_t *ptep, unsigned int nr, int full)
  1137. {
  1138. for (;;) {
  1139. __ptep_get_and_clear(mm, addr, ptep);
  1140. if (--nr == 0)
  1141. break;
  1142. ptep++;
  1143. addr += PAGE_SIZE;
  1144. }
  1145. }
  1146. static inline pte_t __get_and_clear_full_ptes(struct mm_struct *mm,
  1147. unsigned long addr, pte_t *ptep,
  1148. unsigned int nr, int full)
  1149. {
  1150. pte_t pte, tmp_pte;
  1151. pte = __ptep_get_and_clear(mm, addr, ptep);
  1152. while (--nr) {
  1153. ptep++;
  1154. addr += PAGE_SIZE;
  1155. tmp_pte = __ptep_get_and_clear(mm, addr, ptep);
  1156. if (pte_dirty(tmp_pte))
  1157. pte = pte_mkdirty(pte);
  1158. if (pte_young(tmp_pte))
  1159. pte = pte_mkyoung(pte);
  1160. }
  1161. return pte;
  1162. }
  1163. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1164. #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
  1165. static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
  1166. unsigned long address, pmd_t *pmdp)
  1167. {
  1168. return pte_pmd(__ptep_get_and_clear_anysz(mm, address, (pte_t *)pmdp, PMD_SIZE));
  1169. }
  1170. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1171. static inline void ___ptep_set_wrprotect(struct mm_struct *mm,
  1172. unsigned long address, pte_t *ptep,
  1173. pte_t pte)
  1174. {
  1175. pte_t old_pte;
  1176. do {
  1177. old_pte = pte;
  1178. pte = pte_wrprotect(pte);
  1179. pte_val(pte) = cmpxchg_relaxed(&pte_val(*ptep),
  1180. pte_val(old_pte), pte_val(pte));
  1181. } while (pte_val(pte) != pte_val(old_pte));
  1182. }
  1183. /*
  1184. * __ptep_set_wrprotect - mark read-only while transferring potential hardware
  1185. * dirty status (PTE_DBM && !PTE_RDONLY) to the software PTE_DIRTY bit.
  1186. */
  1187. static inline void __ptep_set_wrprotect(struct mm_struct *mm,
  1188. unsigned long address, pte_t *ptep)
  1189. {
  1190. ___ptep_set_wrprotect(mm, address, ptep, __ptep_get(ptep));
  1191. }
  1192. static inline void __wrprotect_ptes(struct mm_struct *mm, unsigned long address,
  1193. pte_t *ptep, unsigned int nr)
  1194. {
  1195. unsigned int i;
  1196. for (i = 0; i < nr; i++, address += PAGE_SIZE, ptep++)
  1197. __ptep_set_wrprotect(mm, address, ptep);
  1198. }
  1199. static inline void __clear_young_dirty_pte(struct vm_area_struct *vma,
  1200. unsigned long addr, pte_t *ptep,
  1201. pte_t pte, cydp_t flags)
  1202. {
  1203. pte_t old_pte;
  1204. do {
  1205. old_pte = pte;
  1206. if (flags & CYDP_CLEAR_YOUNG)
  1207. pte = pte_mkold(pte);
  1208. if (flags & CYDP_CLEAR_DIRTY)
  1209. pte = pte_mkclean(pte);
  1210. pte_val(pte) = cmpxchg_relaxed(&pte_val(*ptep),
  1211. pte_val(old_pte), pte_val(pte));
  1212. } while (pte_val(pte) != pte_val(old_pte));
  1213. }
  1214. static inline void __clear_young_dirty_ptes(struct vm_area_struct *vma,
  1215. unsigned long addr, pte_t *ptep,
  1216. unsigned int nr, cydp_t flags)
  1217. {
  1218. pte_t pte;
  1219. for (;;) {
  1220. pte = __ptep_get(ptep);
  1221. if (flags == (CYDP_CLEAR_YOUNG | CYDP_CLEAR_DIRTY))
  1222. __set_pte(ptep, pte_mkclean(pte_mkold(pte)));
  1223. else
  1224. __clear_young_dirty_pte(vma, addr, ptep, pte, flags);
  1225. if (--nr == 0)
  1226. break;
  1227. ptep++;
  1228. addr += PAGE_SIZE;
  1229. }
  1230. }
  1231. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1232. #define __HAVE_ARCH_PMDP_SET_WRPROTECT
  1233. static inline void pmdp_set_wrprotect(struct mm_struct *mm,
  1234. unsigned long address, pmd_t *pmdp)
  1235. {
  1236. __ptep_set_wrprotect(mm, address, (pte_t *)pmdp);
  1237. }
  1238. #define pmdp_establish pmdp_establish
  1239. static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
  1240. unsigned long address, pmd_t *pmdp, pmd_t pmd)
  1241. {
  1242. page_table_check_pmd_set(vma->vm_mm, address, pmdp, pmd);
  1243. return __pmd(xchg_relaxed(&pmd_val(*pmdp), pmd_val(pmd)));
  1244. }
  1245. #endif
  1246. /*
  1247. * Encode and decode a swap entry:
  1248. * bits 0-1: present (must be zero)
  1249. * bits 2: remember PG_anon_exclusive
  1250. * bit 3: remember uffd-wp state
  1251. * bits 6-10: swap type
  1252. * bit 11: PTE_PRESENT_INVALID (must be zero)
  1253. * bits 12-61: swap offset
  1254. */
  1255. #define __SWP_TYPE_SHIFT 6
  1256. #define __SWP_TYPE_BITS 5
  1257. #define __SWP_TYPE_MASK ((1 << __SWP_TYPE_BITS) - 1)
  1258. #define __SWP_OFFSET_SHIFT 12
  1259. #define __SWP_OFFSET_BITS 50
  1260. #define __SWP_OFFSET_MASK ((1UL << __SWP_OFFSET_BITS) - 1)
  1261. #define __swp_type(x) (((x).val >> __SWP_TYPE_SHIFT) & __SWP_TYPE_MASK)
  1262. #define __swp_offset(x) (((x).val >> __SWP_OFFSET_SHIFT) & __SWP_OFFSET_MASK)
  1263. #define __swp_entry(type,offset) ((swp_entry_t) { ((type) << __SWP_TYPE_SHIFT) | ((offset) << __SWP_OFFSET_SHIFT) })
  1264. #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
  1265. #define __swp_entry_to_pte(swp) ((pte_t) { (swp).val })
  1266. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  1267. #define __pmd_to_swp_entry(pmd) ((swp_entry_t) { pmd_val(pmd) })
  1268. #define __swp_entry_to_pmd(swp) __pmd((swp).val)
  1269. #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */
  1270. /*
  1271. * Ensure that there are not more swap files than can be encoded in the kernel
  1272. * PTEs.
  1273. */
  1274. #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > __SWP_TYPE_BITS)
  1275. #ifdef CONFIG_ARM64_MTE
  1276. #define __HAVE_ARCH_PREPARE_TO_SWAP
  1277. extern int arch_prepare_to_swap(struct folio *folio);
  1278. #define __HAVE_ARCH_SWAP_INVALIDATE
  1279. static inline void arch_swap_invalidate_page(int type, pgoff_t offset)
  1280. {
  1281. if (system_supports_mte())
  1282. mte_invalidate_tags(type, offset);
  1283. }
  1284. static inline void arch_swap_invalidate_area(int type)
  1285. {
  1286. if (system_supports_mte())
  1287. mte_invalidate_tags_area(type);
  1288. }
  1289. #define __HAVE_ARCH_SWAP_RESTORE
  1290. extern void arch_swap_restore(swp_entry_t entry, struct folio *folio);
  1291. #endif /* CONFIG_ARM64_MTE */
  1292. /*
  1293. * On AArch64, the cache coherency is handled via the __set_ptes() function.
  1294. */
  1295. static inline void update_mmu_cache_range(struct vm_fault *vmf,
  1296. struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
  1297. unsigned int nr)
  1298. {
  1299. /*
  1300. * We don't do anything here, so there's a very small chance of
  1301. * us retaking a user fault which we just fixed up. The alternative
  1302. * is doing a dsb(ishst), but that penalises the fastpath.
  1303. */
  1304. }
  1305. #define update_mmu_cache(vma, addr, ptep) \
  1306. update_mmu_cache_range(NULL, vma, addr, ptep, 1)
  1307. #define update_mmu_cache_pmd(vma, address, pmd) do { } while (0)
  1308. #ifdef CONFIG_ARM64_PA_BITS_52
  1309. #define phys_to_ttbr(addr) (((addr) | ((addr) >> 46)) & TTBR_BADDR_MASK_52)
  1310. #else
  1311. #define phys_to_ttbr(addr) (addr)
  1312. #endif
  1313. /*
  1314. * On arm64 without hardware Access Flag, copying from user will fail because
  1315. * the pte is old and cannot be marked young. So we always end up with zeroed
  1316. * page after fork() + CoW for pfn mappings. We don't always have a
  1317. * hardware-managed access flag on arm64.
  1318. */
  1319. #define arch_has_hw_pte_young cpu_has_hw_af
  1320. #ifdef CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG
  1321. #define arch_has_hw_nonleaf_pmd_young system_supports_haft
  1322. #endif
  1323. /*
  1324. * Experimentally, it's cheap to set the access flag in hardware and we
  1325. * benefit from prefaulting mappings as 'old' to start with.
  1326. */
  1327. #define arch_wants_old_prefaulted_pte cpu_has_hw_af
  1328. /*
  1329. * Request exec memory is read into pagecache in at least 64K folios. This size
  1330. * can be contpte-mapped when 4K base pages are in use (16 pages into 1 iTLB
  1331. * entry), and HPA can coalesce it (4 pages into 1 TLB entry) when 16K base
  1332. * pages are in use.
  1333. */
  1334. #define exec_folio_order() ilog2(SZ_64K >> PAGE_SHIFT)
  1335. static inline bool pud_sect_supported(void)
  1336. {
  1337. return PAGE_SIZE == SZ_4K;
  1338. }
  1339. #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  1340. #define ptep_modify_prot_start ptep_modify_prot_start
  1341. extern pte_t ptep_modify_prot_start(struct vm_area_struct *vma,
  1342. unsigned long addr, pte_t *ptep);
  1343. #define ptep_modify_prot_commit ptep_modify_prot_commit
  1344. extern void ptep_modify_prot_commit(struct vm_area_struct *vma,
  1345. unsigned long addr, pte_t *ptep,
  1346. pte_t old_pte, pte_t new_pte);
  1347. #define modify_prot_start_ptes modify_prot_start_ptes
  1348. extern pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
  1349. unsigned long addr, pte_t *ptep,
  1350. unsigned int nr);
  1351. #define modify_prot_commit_ptes modify_prot_commit_ptes
  1352. extern void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
  1353. pte_t *ptep, pte_t old_pte, pte_t pte,
  1354. unsigned int nr);
  1355. #ifdef CONFIG_ARM64_CONTPTE
  1356. /*
  1357. * The contpte APIs are used to transparently manage the contiguous bit in ptes
  1358. * where it is possible and makes sense to do so. The PTE_CONT bit is considered
  1359. * a private implementation detail of the public ptep API (see below).
  1360. */
  1361. extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,
  1362. pte_t *ptep, pte_t pte);
  1363. extern void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
  1364. pte_t *ptep, pte_t pte);
  1365. extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte);
  1366. extern pte_t contpte_ptep_get_lockless(pte_t *orig_ptep);
  1367. extern void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,
  1368. pte_t *ptep, pte_t pte, unsigned int nr);
  1369. extern void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,
  1370. pte_t *ptep, unsigned int nr, int full);
  1371. extern pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm,
  1372. unsigned long addr, pte_t *ptep,
  1373. unsigned int nr, int full);
  1374. int contpte_test_and_clear_young_ptes(struct vm_area_struct *vma,
  1375. unsigned long addr, pte_t *ptep, unsigned int nr);
  1376. int contpte_clear_flush_young_ptes(struct vm_area_struct *vma,
  1377. unsigned long addr, pte_t *ptep, unsigned int nr);
  1378. extern void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
  1379. pte_t *ptep, unsigned int nr);
  1380. extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
  1381. unsigned long addr, pte_t *ptep,
  1382. pte_t entry, int dirty);
  1383. extern void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma,
  1384. unsigned long addr, pte_t *ptep,
  1385. unsigned int nr, cydp_t flags);
  1386. static __always_inline void contpte_try_fold(struct mm_struct *mm,
  1387. unsigned long addr, pte_t *ptep, pte_t pte)
  1388. {
  1389. /*
  1390. * Only bother trying if both the virtual and physical addresses are
  1391. * aligned and correspond to the last entry in a contig range. The core
  1392. * code mostly modifies ranges from low to high, so this is the likely
  1393. * the last modification in the contig range, so a good time to fold.
  1394. * We can't fold special mappings, because there is no associated folio.
  1395. */
  1396. const unsigned long contmask = CONT_PTES - 1;
  1397. bool valign = ((addr >> PAGE_SHIFT) & contmask) == contmask;
  1398. if (unlikely(valign)) {
  1399. bool palign = (pte_pfn(pte) & contmask) == contmask;
  1400. if (unlikely(palign &&
  1401. pte_valid(pte) && !pte_cont(pte) && !pte_special(pte)))
  1402. __contpte_try_fold(mm, addr, ptep, pte);
  1403. }
  1404. }
  1405. static __always_inline void contpte_try_unfold(struct mm_struct *mm,
  1406. unsigned long addr, pte_t *ptep, pte_t pte)
  1407. {
  1408. if (unlikely(pte_valid_cont(pte)))
  1409. __contpte_try_unfold(mm, addr, ptep, pte);
  1410. }
  1411. #define pte_batch_hint pte_batch_hint
  1412. static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte)
  1413. {
  1414. if (!pte_valid_cont(pte))
  1415. return 1;
  1416. return CONT_PTES - (((unsigned long)ptep >> 3) & (CONT_PTES - 1));
  1417. }
  1418. /*
  1419. * The below functions constitute the public API that arm64 presents to the
  1420. * core-mm to manipulate PTE entries within their page tables (or at least this
  1421. * is the subset of the API that arm64 needs to implement). These public
  1422. * versions will automatically and transparently apply the contiguous bit where
  1423. * it makes sense to do so. Therefore any users that are contig-aware (e.g.
  1424. * hugetlb, kernel mapper) should NOT use these APIs, but instead use the
  1425. * private versions, which are prefixed with double underscore. All of these
  1426. * APIs except for ptep_get_lockless() are expected to be called with the PTL
  1427. * held. Although the contiguous bit is considered private to the
  1428. * implementation, it is deliberately allowed to leak through the getters (e.g.
  1429. * ptep_get()), back to core code. This is required so that pte_leaf_size() can
  1430. * provide an accurate size for perf_get_pgtable_size(). But this leakage means
  1431. * its possible a pte will be passed to a setter with the contiguous bit set, so
  1432. * we explicitly clear the contiguous bit in those cases to prevent accidentally
  1433. * setting it in the pgtable.
  1434. */
  1435. #define ptep_get ptep_get
  1436. static inline pte_t ptep_get(pte_t *ptep)
  1437. {
  1438. pte_t pte = __ptep_get(ptep);
  1439. if (likely(!pte_valid_cont(pte)))
  1440. return pte;
  1441. return contpte_ptep_get(ptep, pte);
  1442. }
  1443. #define ptep_get_lockless ptep_get_lockless
  1444. static inline pte_t ptep_get_lockless(pte_t *ptep)
  1445. {
  1446. pte_t pte = __ptep_get(ptep);
  1447. if (likely(!pte_valid_cont(pte)))
  1448. return pte;
  1449. return contpte_ptep_get_lockless(ptep);
  1450. }
  1451. static inline void set_pte(pte_t *ptep, pte_t pte)
  1452. {
  1453. /*
  1454. * We don't have the mm or vaddr so cannot unfold contig entries (since
  1455. * it requires tlb maintenance). set_pte() is not used in core code, so
  1456. * this should never even be called. Regardless do our best to service
  1457. * any call and emit a warning if there is any attempt to set a pte on
  1458. * top of an existing contig range.
  1459. */
  1460. pte_t orig_pte = __ptep_get(ptep);
  1461. WARN_ON_ONCE(pte_valid_cont(orig_pte));
  1462. __set_pte(ptep, pte_mknoncont(pte));
  1463. }
  1464. #define set_ptes set_ptes
  1465. static __always_inline void set_ptes(struct mm_struct *mm, unsigned long addr,
  1466. pte_t *ptep, pte_t pte, unsigned int nr)
  1467. {
  1468. pte = pte_mknoncont(pte);
  1469. if (likely(nr == 1)) {
  1470. contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep));
  1471. __set_ptes(mm, addr, ptep, pte, 1);
  1472. contpte_try_fold(mm, addr, ptep, pte);
  1473. } else {
  1474. contpte_set_ptes(mm, addr, ptep, pte, nr);
  1475. }
  1476. }
  1477. static inline void pte_clear(struct mm_struct *mm,
  1478. unsigned long addr, pte_t *ptep)
  1479. {
  1480. contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep));
  1481. __pte_clear(mm, addr, ptep);
  1482. }
  1483. #define clear_full_ptes clear_full_ptes
  1484. static inline void clear_full_ptes(struct mm_struct *mm, unsigned long addr,
  1485. pte_t *ptep, unsigned int nr, int full)
  1486. {
  1487. if (likely(nr == 1)) {
  1488. contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep));
  1489. __clear_full_ptes(mm, addr, ptep, nr, full);
  1490. } else {
  1491. contpte_clear_full_ptes(mm, addr, ptep, nr, full);
  1492. }
  1493. }
  1494. #define get_and_clear_full_ptes get_and_clear_full_ptes
  1495. static inline pte_t get_and_clear_full_ptes(struct mm_struct *mm,
  1496. unsigned long addr, pte_t *ptep,
  1497. unsigned int nr, int full)
  1498. {
  1499. pte_t pte;
  1500. if (likely(nr == 1)) {
  1501. contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep));
  1502. pte = __get_and_clear_full_ptes(mm, addr, ptep, nr, full);
  1503. } else {
  1504. pte = contpte_get_and_clear_full_ptes(mm, addr, ptep, nr, full);
  1505. }
  1506. return pte;
  1507. }
  1508. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  1509. static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
  1510. unsigned long addr, pte_t *ptep)
  1511. {
  1512. contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep));
  1513. return __ptep_get_and_clear(mm, addr, ptep);
  1514. }
  1515. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  1516. static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
  1517. unsigned long addr, pte_t *ptep)
  1518. {
  1519. pte_t orig_pte = __ptep_get(ptep);
  1520. if (likely(!pte_valid_cont(orig_pte)))
  1521. return __ptep_test_and_clear_young(vma, addr, ptep);
  1522. return contpte_test_and_clear_young_ptes(vma, addr, ptep, 1);
  1523. }
  1524. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  1525. static inline int ptep_clear_flush_young(struct vm_area_struct *vma,
  1526. unsigned long addr, pte_t *ptep)
  1527. {
  1528. pte_t orig_pte = __ptep_get(ptep);
  1529. if (likely(!pte_valid_cont(orig_pte)))
  1530. return __ptep_clear_flush_young(vma, addr, ptep);
  1531. return contpte_clear_flush_young_ptes(vma, addr, ptep, 1);
  1532. }
  1533. #define clear_flush_young_ptes clear_flush_young_ptes
  1534. static inline int clear_flush_young_ptes(struct vm_area_struct *vma,
  1535. unsigned long addr, pte_t *ptep,
  1536. unsigned int nr)
  1537. {
  1538. if (likely(nr == 1 && !pte_cont(__ptep_get(ptep))))
  1539. return __ptep_clear_flush_young(vma, addr, ptep);
  1540. return contpte_clear_flush_young_ptes(vma, addr, ptep, nr);
  1541. }
  1542. #define wrprotect_ptes wrprotect_ptes
  1543. static __always_inline void wrprotect_ptes(struct mm_struct *mm,
  1544. unsigned long addr, pte_t *ptep, unsigned int nr)
  1545. {
  1546. if (likely(nr == 1)) {
  1547. /*
  1548. * Optimization: wrprotect_ptes() can only be called for present
  1549. * ptes so we only need to check contig bit as condition for
  1550. * unfold, and we can remove the contig bit from the pte we read
  1551. * to avoid re-reading. This speeds up fork() which is sensitive
  1552. * for order-0 folios. Equivalent to contpte_try_unfold().
  1553. */
  1554. pte_t orig_pte = __ptep_get(ptep);
  1555. if (unlikely(pte_cont(orig_pte))) {
  1556. __contpte_try_unfold(mm, addr, ptep, orig_pte);
  1557. orig_pte = pte_mknoncont(orig_pte);
  1558. }
  1559. ___ptep_set_wrprotect(mm, addr, ptep, orig_pte);
  1560. } else {
  1561. contpte_wrprotect_ptes(mm, addr, ptep, nr);
  1562. }
  1563. }
  1564. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  1565. static inline void ptep_set_wrprotect(struct mm_struct *mm,
  1566. unsigned long addr, pte_t *ptep)
  1567. {
  1568. wrprotect_ptes(mm, addr, ptep, 1);
  1569. }
  1570. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  1571. static inline int ptep_set_access_flags(struct vm_area_struct *vma,
  1572. unsigned long addr, pte_t *ptep,
  1573. pte_t entry, int dirty)
  1574. {
  1575. pte_t orig_pte = __ptep_get(ptep);
  1576. entry = pte_mknoncont(entry);
  1577. if (likely(!pte_valid_cont(orig_pte)))
  1578. return __ptep_set_access_flags(vma, addr, ptep, entry, dirty);
  1579. return contpte_ptep_set_access_flags(vma, addr, ptep, entry, dirty);
  1580. }
  1581. #define clear_young_dirty_ptes clear_young_dirty_ptes
  1582. static inline void clear_young_dirty_ptes(struct vm_area_struct *vma,
  1583. unsigned long addr, pte_t *ptep,
  1584. unsigned int nr, cydp_t flags)
  1585. {
  1586. if (likely(nr == 1 && !pte_cont(__ptep_get(ptep))))
  1587. __clear_young_dirty_ptes(vma, addr, ptep, nr, flags);
  1588. else
  1589. contpte_clear_young_dirty_ptes(vma, addr, ptep, nr, flags);
  1590. }
  1591. #else /* CONFIG_ARM64_CONTPTE */
  1592. #define ptep_get __ptep_get
  1593. #define set_pte __set_pte
  1594. #define set_ptes __set_ptes
  1595. #define pte_clear __pte_clear
  1596. #define clear_full_ptes __clear_full_ptes
  1597. #define get_and_clear_full_ptes __get_and_clear_full_ptes
  1598. #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
  1599. #define ptep_get_and_clear __ptep_get_and_clear
  1600. #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  1601. #define ptep_test_and_clear_young __ptep_test_and_clear_young
  1602. #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
  1603. #define ptep_clear_flush_young __ptep_clear_flush_young
  1604. #define __HAVE_ARCH_PTEP_SET_WRPROTECT
  1605. #define ptep_set_wrprotect __ptep_set_wrprotect
  1606. #define wrprotect_ptes __wrprotect_ptes
  1607. #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
  1608. #define ptep_set_access_flags __ptep_set_access_flags
  1609. #define clear_young_dirty_ptes __clear_young_dirty_ptes
  1610. #endif /* CONFIG_ARM64_CONTPTE */
  1611. #endif /* !__ASSEMBLER__ */
  1612. #endif /* __ASM_PGTABLE_H */