mmu.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/mm/mmu.c
  4. *
  5. * Copyright (C) 1995-2005 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #include <linux/cache.h>
  9. #include <linux/export.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/init.h>
  13. #include <linux/ioport.h>
  14. #include <linux/kexec.h>
  15. #include <linux/libfdt.h>
  16. #include <linux/mman.h>
  17. #include <linux/nodemask.h>
  18. #include <linux/memblock.h>
  19. #include <linux/memory.h>
  20. #include <linux/fs.h>
  21. #include <linux/io.h>
  22. #include <linux/mm.h>
  23. #include <linux/vmalloc.h>
  24. #include <asm/barrier.h>
  25. #include <asm/cputype.h>
  26. #include <asm/fixmap.h>
  27. #include <asm/kasan.h>
  28. #include <asm/kernel-pgtable.h>
  29. #include <asm/sections.h>
  30. #include <asm/setup.h>
  31. #include <linux/sizes.h>
  32. #include <asm/tlb.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/ptdump.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/pgalloc.h>
  37. #define NO_BLOCK_MAPPINGS BIT(0)
  38. #define NO_CONT_MAPPINGS BIT(1)
  39. u64 idmap_t0sz = TCR_T0SZ(VA_BITS_MIN);
  40. u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
  41. u64 __section(".mmuoff.data.write") vabits_actual;
  42. EXPORT_SYMBOL(vabits_actual);
  43. u64 kimage_voffset __ro_after_init;
  44. EXPORT_SYMBOL(kimage_voffset);
  45. /*
  46. * Empty_zero_page is a special page that is used for zero-initialized data
  47. * and COW.
  48. */
  49. unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
  50. EXPORT_SYMBOL(empty_zero_page);
  51. static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
  52. static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
  53. static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
  54. static DEFINE_SPINLOCK(swapper_pgdir_lock);
  55. static DEFINE_MUTEX(fixmap_lock);
  56. void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
  57. {
  58. pgd_t *fixmap_pgdp;
  59. spin_lock(&swapper_pgdir_lock);
  60. fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
  61. WRITE_ONCE(*fixmap_pgdp, pgd);
  62. /*
  63. * We need dsb(ishst) here to ensure the page-table-walker sees
  64. * our new entry before set_p?d() returns. The fixmap's
  65. * flush_tlb_kernel_range() via clear_fixmap() does this for us.
  66. */
  67. pgd_clear_fixmap();
  68. spin_unlock(&swapper_pgdir_lock);
  69. }
  70. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  71. unsigned long size, pgprot_t vma_prot)
  72. {
  73. if (!pfn_valid(pfn))
  74. return pgprot_noncached(vma_prot);
  75. else if (file->f_flags & O_SYNC)
  76. return pgprot_writecombine(vma_prot);
  77. return vma_prot;
  78. }
  79. EXPORT_SYMBOL(phys_mem_access_prot);
  80. static phys_addr_t __init early_pgtable_alloc(int shift)
  81. {
  82. phys_addr_t phys;
  83. void *ptr;
  84. phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
  85. if (!phys)
  86. panic("Failed to allocate page table page\n");
  87. /*
  88. * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
  89. * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
  90. * any level of table.
  91. */
  92. ptr = pte_set_fixmap(phys);
  93. memset(ptr, 0, PAGE_SIZE);
  94. /*
  95. * Implicit barriers also ensure the zeroed page is visible to the page
  96. * table walker
  97. */
  98. pte_clear_fixmap();
  99. return phys;
  100. }
  101. static bool pgattr_change_is_safe(u64 old, u64 new)
  102. {
  103. /*
  104. * The following mapping attributes may be updated in live
  105. * kernel mappings without the need for break-before-make.
  106. */
  107. pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
  108. /* creating or taking down mappings is always safe */
  109. if (old == 0 || new == 0)
  110. return true;
  111. /* live contiguous mappings may not be manipulated at all */
  112. if ((old | new) & PTE_CONT)
  113. return false;
  114. /* Transitioning from Non-Global to Global is unsafe */
  115. if (old & ~new & PTE_NG)
  116. return false;
  117. /*
  118. * Changing the memory type between Normal and Normal-Tagged is safe
  119. * since Tagged is considered a permission attribute from the
  120. * mismatched attribute aliases perspective.
  121. */
  122. if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
  123. (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) &&
  124. ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
  125. (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)))
  126. mask |= PTE_ATTRINDX_MASK;
  127. return ((old ^ new) & ~mask) == 0;
  128. }
  129. static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
  130. phys_addr_t phys, pgprot_t prot)
  131. {
  132. pte_t *ptep;
  133. ptep = pte_set_fixmap_offset(pmdp, addr);
  134. do {
  135. pte_t old_pte = READ_ONCE(*ptep);
  136. set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
  137. /*
  138. * After the PTE entry has been populated once, we
  139. * only allow updates to the permission attributes.
  140. */
  141. BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
  142. READ_ONCE(pte_val(*ptep))));
  143. phys += PAGE_SIZE;
  144. } while (ptep++, addr += PAGE_SIZE, addr != end);
  145. pte_clear_fixmap();
  146. }
  147. static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
  148. unsigned long end, phys_addr_t phys,
  149. pgprot_t prot,
  150. phys_addr_t (*pgtable_alloc)(int),
  151. int flags)
  152. {
  153. unsigned long next;
  154. pmd_t pmd = READ_ONCE(*pmdp);
  155. BUG_ON(pmd_sect(pmd));
  156. if (pmd_none(pmd)) {
  157. phys_addr_t pte_phys;
  158. BUG_ON(!pgtable_alloc);
  159. pte_phys = pgtable_alloc(PAGE_SHIFT);
  160. __pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
  161. pmd = READ_ONCE(*pmdp);
  162. }
  163. BUG_ON(pmd_bad(pmd));
  164. do {
  165. pgprot_t __prot = prot;
  166. next = pte_cont_addr_end(addr, end);
  167. /* use a contiguous mapping if the range is suitably aligned */
  168. if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
  169. (flags & NO_CONT_MAPPINGS) == 0)
  170. __prot = __pgprot(pgprot_val(prot) | PTE_CONT);
  171. init_pte(pmdp, addr, next, phys, __prot);
  172. phys += next - addr;
  173. } while (addr = next, addr != end);
  174. }
  175. static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,
  176. phys_addr_t phys, pgprot_t prot,
  177. phys_addr_t (*pgtable_alloc)(int), int flags)
  178. {
  179. unsigned long next;
  180. pmd_t *pmdp;
  181. pmdp = pmd_set_fixmap_offset(pudp, addr);
  182. do {
  183. pmd_t old_pmd = READ_ONCE(*pmdp);
  184. next = pmd_addr_end(addr, end);
  185. /* try section mapping first */
  186. if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
  187. (flags & NO_BLOCK_MAPPINGS) == 0) {
  188. pmd_set_huge(pmdp, phys, prot);
  189. /*
  190. * After the PMD entry has been populated once, we
  191. * only allow updates to the permission attributes.
  192. */
  193. BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
  194. READ_ONCE(pmd_val(*pmdp))));
  195. } else {
  196. alloc_init_cont_pte(pmdp, addr, next, phys, prot,
  197. pgtable_alloc, flags);
  198. BUG_ON(pmd_val(old_pmd) != 0 &&
  199. pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
  200. }
  201. phys += next - addr;
  202. } while (pmdp++, addr = next, addr != end);
  203. pmd_clear_fixmap();
  204. }
  205. static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
  206. unsigned long end, phys_addr_t phys,
  207. pgprot_t prot,
  208. phys_addr_t (*pgtable_alloc)(int), int flags)
  209. {
  210. unsigned long next;
  211. pud_t pud = READ_ONCE(*pudp);
  212. /*
  213. * Check for initial section mappings in the pgd/pud.
  214. */
  215. BUG_ON(pud_sect(pud));
  216. if (pud_none(pud)) {
  217. phys_addr_t pmd_phys;
  218. BUG_ON(!pgtable_alloc);
  219. pmd_phys = pgtable_alloc(PMD_SHIFT);
  220. __pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
  221. pud = READ_ONCE(*pudp);
  222. }
  223. BUG_ON(pud_bad(pud));
  224. do {
  225. pgprot_t __prot = prot;
  226. next = pmd_cont_addr_end(addr, end);
  227. /* use a contiguous mapping if the range is suitably aligned */
  228. if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
  229. (flags & NO_CONT_MAPPINGS) == 0)
  230. __prot = __pgprot(pgprot_val(prot) | PTE_CONT);
  231. init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags);
  232. phys += next - addr;
  233. } while (addr = next, addr != end);
  234. }
  235. static inline bool use_1G_block(unsigned long addr, unsigned long next,
  236. unsigned long phys)
  237. {
  238. if (PAGE_SHIFT != 12)
  239. return false;
  240. if (((addr | next | phys) & ~PUD_MASK) != 0)
  241. return false;
  242. return true;
  243. }
  244. static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,
  245. phys_addr_t phys, pgprot_t prot,
  246. phys_addr_t (*pgtable_alloc)(int),
  247. int flags)
  248. {
  249. unsigned long next;
  250. pud_t *pudp;
  251. p4d_t *p4dp = p4d_offset(pgdp, addr);
  252. p4d_t p4d = READ_ONCE(*p4dp);
  253. if (p4d_none(p4d)) {
  254. phys_addr_t pud_phys;
  255. BUG_ON(!pgtable_alloc);
  256. pud_phys = pgtable_alloc(PUD_SHIFT);
  257. __p4d_populate(p4dp, pud_phys, PUD_TYPE_TABLE);
  258. p4d = READ_ONCE(*p4dp);
  259. }
  260. BUG_ON(p4d_bad(p4d));
  261. /*
  262. * No need for locking during early boot. And it doesn't work as
  263. * expected with KASLR enabled.
  264. */
  265. if (system_state != SYSTEM_BOOTING)
  266. mutex_lock(&fixmap_lock);
  267. pudp = pud_set_fixmap_offset(p4dp, addr);
  268. do {
  269. pud_t old_pud = READ_ONCE(*pudp);
  270. next = pud_addr_end(addr, end);
  271. /*
  272. * For 4K granule only, attempt to put down a 1GB block
  273. */
  274. if (use_1G_block(addr, next, phys) &&
  275. (flags & NO_BLOCK_MAPPINGS) == 0) {
  276. pud_set_huge(pudp, phys, prot);
  277. /*
  278. * After the PUD entry has been populated once, we
  279. * only allow updates to the permission attributes.
  280. */
  281. BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
  282. READ_ONCE(pud_val(*pudp))));
  283. } else {
  284. alloc_init_cont_pmd(pudp, addr, next, phys, prot,
  285. pgtable_alloc, flags);
  286. BUG_ON(pud_val(old_pud) != 0 &&
  287. pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
  288. }
  289. phys += next - addr;
  290. } while (pudp++, addr = next, addr != end);
  291. pud_clear_fixmap();
  292. if (system_state != SYSTEM_BOOTING)
  293. mutex_unlock(&fixmap_lock);
  294. }
  295. static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
  296. unsigned long virt, phys_addr_t size,
  297. pgprot_t prot,
  298. phys_addr_t (*pgtable_alloc)(int),
  299. int flags)
  300. {
  301. unsigned long addr, end, next;
  302. pgd_t *pgdp = pgd_offset_pgd(pgdir, virt);
  303. /*
  304. * If the virtual and physical address don't have the same offset
  305. * within a page, we cannot map the region as the caller expects.
  306. */
  307. if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
  308. return;
  309. phys &= PAGE_MASK;
  310. addr = virt & PAGE_MASK;
  311. end = PAGE_ALIGN(virt + size);
  312. do {
  313. next = pgd_addr_end(addr, end);
  314. alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc,
  315. flags);
  316. phys += next - addr;
  317. } while (pgdp++, addr = next, addr != end);
  318. }
  319. static phys_addr_t __pgd_pgtable_alloc(int shift)
  320. {
  321. void *ptr = (void *)__get_free_page(GFP_PGTABLE_KERNEL);
  322. BUG_ON(!ptr);
  323. /* Ensure the zeroed page is visible to the page table walker */
  324. dsb(ishst);
  325. return __pa(ptr);
  326. }
  327. static phys_addr_t pgd_pgtable_alloc(int shift)
  328. {
  329. phys_addr_t pa = __pgd_pgtable_alloc(shift);
  330. /*
  331. * Call proper page table ctor in case later we need to
  332. * call core mm functions like apply_to_page_range() on
  333. * this pre-allocated page table.
  334. *
  335. * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is
  336. * folded, and if so pgtable_pmd_page_ctor() becomes nop.
  337. */
  338. if (shift == PAGE_SHIFT)
  339. BUG_ON(!pgtable_pte_page_ctor(phys_to_page(pa)));
  340. else if (shift == PMD_SHIFT)
  341. BUG_ON(!pgtable_pmd_page_ctor(phys_to_page(pa)));
  342. return pa;
  343. }
  344. /*
  345. * This function can only be used to modify existing table entries,
  346. * without allocating new levels of table. Note that this permits the
  347. * creation of new section or page entries.
  348. */
  349. static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
  350. phys_addr_t size, pgprot_t prot)
  351. {
  352. if ((virt >= PAGE_END) && (virt < VMALLOC_START)) {
  353. pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
  354. &phys, virt);
  355. return;
  356. }
  357. __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
  358. NO_CONT_MAPPINGS);
  359. }
  360. void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
  361. unsigned long virt, phys_addr_t size,
  362. pgprot_t prot, bool page_mappings_only)
  363. {
  364. int flags = 0;
  365. BUG_ON(mm == &init_mm);
  366. if (page_mappings_only)
  367. flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  368. __create_pgd_mapping(mm->pgd, phys, virt, size, prot,
  369. pgd_pgtable_alloc, flags);
  370. }
  371. static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
  372. phys_addr_t size, pgprot_t prot)
  373. {
  374. if ((virt >= PAGE_END) && (virt < VMALLOC_START)) {
  375. pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
  376. &phys, virt);
  377. return;
  378. }
  379. __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
  380. NO_CONT_MAPPINGS);
  381. /* flush the TLBs after updating live kernel mappings */
  382. flush_tlb_kernel_range(virt, virt + size);
  383. }
  384. static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
  385. phys_addr_t end, pgprot_t prot, int flags)
  386. {
  387. __create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
  388. prot, early_pgtable_alloc, flags);
  389. }
  390. void __init mark_linear_text_alias_ro(void)
  391. {
  392. /*
  393. * Remove the write permissions from the linear alias of .text/.rodata
  394. */
  395. update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
  396. (unsigned long)__init_begin - (unsigned long)_text,
  397. PAGE_KERNEL_RO);
  398. }
  399. static bool crash_mem_map __initdata;
  400. static int __init enable_crash_mem_map(char *arg)
  401. {
  402. /*
  403. * Proper parameter parsing is done by reserve_crashkernel(). We only
  404. * need to know if the linear map has to avoid block mappings so that
  405. * the crashkernel reservations can be unmapped later.
  406. */
  407. crash_mem_map = true;
  408. return 0;
  409. }
  410. early_param("crashkernel", enable_crash_mem_map);
  411. static void __init map_mem(pgd_t *pgdp)
  412. {
  413. phys_addr_t kernel_start = __pa_symbol(_text);
  414. phys_addr_t kernel_end = __pa_symbol(__init_begin);
  415. phys_addr_t start, end;
  416. int flags = 0;
  417. u64 i;
  418. if (rodata_full || debug_pagealloc_enabled() ||
  419. IS_ENABLED(CONFIG_KFENCE))
  420. flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  421. /*
  422. * Take care not to create a writable alias for the
  423. * read-only text and rodata sections of the kernel image.
  424. * So temporarily mark them as NOMAP to skip mappings in
  425. * the following for-loop
  426. */
  427. memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
  428. #ifdef CONFIG_KEXEC_CORE
  429. if (crash_mem_map) {
  430. if (IS_ENABLED(CONFIG_ZONE_DMA) ||
  431. IS_ENABLED(CONFIG_ZONE_DMA32))
  432. flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  433. else if (crashk_res.end)
  434. memblock_mark_nomap(crashk_res.start,
  435. resource_size(&crashk_res));
  436. }
  437. #endif
  438. /* map all the memory banks */
  439. for_each_mem_range(i, &start, &end) {
  440. if (start >= end)
  441. break;
  442. /*
  443. * The linear map must allow allocation tags reading/writing
  444. * if MTE is present. Otherwise, it has the same attributes as
  445. * PAGE_KERNEL.
  446. */
  447. __map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL),
  448. flags);
  449. }
  450. /*
  451. * Map the linear alias of the [_text, __init_begin) interval
  452. * as non-executable now, and remove the write permission in
  453. * mark_linear_text_alias_ro() below (which will be called after
  454. * alternative patching has completed). This makes the contents
  455. * of the region accessible to subsystems such as hibernate,
  456. * but protects it from inadvertent modification or execution.
  457. * Note that contiguous mappings cannot be remapped in this way,
  458. * so we should avoid them here.
  459. */
  460. __map_memblock(pgdp, kernel_start, kernel_end,
  461. PAGE_KERNEL, NO_CONT_MAPPINGS);
  462. memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
  463. /*
  464. * Use page-level mappings here so that we can shrink the region
  465. * in page granularity and put back unused memory to buddy system
  466. * through /sys/kernel/kexec_crash_size interface.
  467. */
  468. #ifdef CONFIG_KEXEC_CORE
  469. if (crash_mem_map &&
  470. !IS_ENABLED(CONFIG_ZONE_DMA) && !IS_ENABLED(CONFIG_ZONE_DMA32)) {
  471. if (crashk_res.end) {
  472. __map_memblock(pgdp, crashk_res.start,
  473. crashk_res.end + 1,
  474. PAGE_KERNEL,
  475. NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
  476. memblock_clear_nomap(crashk_res.start,
  477. resource_size(&crashk_res));
  478. }
  479. }
  480. #endif
  481. }
  482. void mark_rodata_ro(void)
  483. {
  484. unsigned long section_size;
  485. /*
  486. * mark .rodata as read only. Use __init_begin rather than __end_rodata
  487. * to cover NOTES and EXCEPTION_TABLE.
  488. */
  489. section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
  490. update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
  491. section_size, PAGE_KERNEL_RO);
  492. debug_checkwx();
  493. }
  494. static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
  495. pgprot_t prot, struct vm_struct *vma,
  496. int flags, unsigned long vm_flags)
  497. {
  498. phys_addr_t pa_start = __pa_symbol(va_start);
  499. unsigned long size = va_end - va_start;
  500. BUG_ON(!PAGE_ALIGNED(pa_start));
  501. BUG_ON(!PAGE_ALIGNED(size));
  502. __create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
  503. early_pgtable_alloc, flags);
  504. if (!(vm_flags & VM_NO_GUARD))
  505. size += PAGE_SIZE;
  506. vma->addr = va_start;
  507. vma->phys_addr = pa_start;
  508. vma->size = size;
  509. vma->flags = VM_MAP | vm_flags;
  510. vma->caller = __builtin_return_address(0);
  511. vm_area_add_early(vma);
  512. }
  513. static int __init parse_rodata(char *arg)
  514. {
  515. int ret = strtobool(arg, &rodata_enabled);
  516. if (!ret) {
  517. rodata_full = false;
  518. return 0;
  519. }
  520. /* permit 'full' in addition to boolean options */
  521. if (strcmp(arg, "full"))
  522. return -EINVAL;
  523. rodata_enabled = true;
  524. rodata_full = true;
  525. return 0;
  526. }
  527. early_param("rodata", parse_rodata);
  528. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  529. static int __init map_entry_trampoline(void)
  530. {
  531. int i;
  532. pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
  533. phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
  534. /* The trampoline is always mapped and can therefore be global */
  535. pgprot_val(prot) &= ~PTE_NG;
  536. /* Map only the text into the trampoline page table */
  537. memset(tramp_pg_dir, 0, PGD_SIZE);
  538. __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS,
  539. entry_tramp_text_size(), prot,
  540. __pgd_pgtable_alloc, NO_BLOCK_MAPPINGS);
  541. /* Map both the text and data into the kernel page table */
  542. for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++)
  543. __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
  544. pa_start + i * PAGE_SIZE, prot);
  545. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  546. extern char __entry_tramp_data_start[];
  547. __set_fixmap(FIX_ENTRY_TRAMP_DATA,
  548. __pa_symbol(__entry_tramp_data_start),
  549. PAGE_KERNEL_RO);
  550. }
  551. return 0;
  552. }
  553. core_initcall(map_entry_trampoline);
  554. #endif
  555. /*
  556. * Open coded check for BTI, only for use to determine configuration
  557. * for early mappings for before the cpufeature code has run.
  558. */
  559. static bool arm64_early_this_cpu_has_bti(void)
  560. {
  561. u64 pfr1;
  562. if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
  563. return false;
  564. pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
  565. return cpuid_feature_extract_unsigned_field(pfr1,
  566. ID_AA64PFR1_BT_SHIFT);
  567. }
  568. /*
  569. * Create fine-grained mappings for the kernel.
  570. */
  571. static void __init map_kernel(pgd_t *pgdp)
  572. {
  573. static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
  574. vmlinux_initdata, vmlinux_data;
  575. /*
  576. * External debuggers may need to write directly to the text
  577. * mapping to install SW breakpoints. Allow this (only) when
  578. * explicitly requested with rodata=off.
  579. */
  580. pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
  581. /*
  582. * If we have a CPU that supports BTI and a kernel built for
  583. * BTI then mark the kernel executable text as guarded pages
  584. * now so we don't have to rewrite the page tables later.
  585. */
  586. if (arm64_early_this_cpu_has_bti())
  587. text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP);
  588. /*
  589. * Only rodata will be remapped with different permissions later on,
  590. * all other segments are allowed to use contiguous mappings.
  591. */
  592. map_kernel_segment(pgdp, _text, _etext, text_prot, &vmlinux_text, 0,
  593. VM_NO_GUARD);
  594. map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
  595. &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
  596. map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
  597. &vmlinux_inittext, 0, VM_NO_GUARD);
  598. map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
  599. &vmlinux_initdata, 0, VM_NO_GUARD);
  600. map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
  601. if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) {
  602. /*
  603. * The fixmap falls in a separate pgd to the kernel, and doesn't
  604. * live in the carveout for the swapper_pg_dir. We can simply
  605. * re-use the existing dir for the fixmap.
  606. */
  607. set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START),
  608. READ_ONCE(*pgd_offset_k(FIXADDR_START)));
  609. } else if (CONFIG_PGTABLE_LEVELS > 3) {
  610. pgd_t *bm_pgdp;
  611. p4d_t *bm_p4dp;
  612. pud_t *bm_pudp;
  613. /*
  614. * The fixmap shares its top level pgd entry with the kernel
  615. * mapping. This can really only occur when we are running
  616. * with 16k/4 levels, so we can simply reuse the pud level
  617. * entry instead.
  618. */
  619. BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
  620. bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START);
  621. bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START);
  622. bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START);
  623. pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd));
  624. pud_clear_fixmap();
  625. } else {
  626. BUG();
  627. }
  628. kasan_copy_shadow(pgdp);
  629. }
  630. void __init paging_init(void)
  631. {
  632. pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
  633. map_kernel(pgdp);
  634. map_mem(pgdp);
  635. pgd_clear_fixmap();
  636. cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
  637. init_mm.pgd = swapper_pg_dir;
  638. memblock_free(__pa_symbol(init_pg_dir),
  639. __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
  640. memblock_allow_resize();
  641. }
  642. /*
  643. * Check whether a kernel address is valid (derived from arch/x86/).
  644. */
  645. int kern_addr_valid(unsigned long addr)
  646. {
  647. pgd_t *pgdp;
  648. p4d_t *p4dp;
  649. pud_t *pudp, pud;
  650. pmd_t *pmdp, pmd;
  651. pte_t *ptep, pte;
  652. addr = arch_kasan_reset_tag(addr);
  653. if ((((long)addr) >> VA_BITS) != -1UL)
  654. return 0;
  655. pgdp = pgd_offset_k(addr);
  656. if (pgd_none(READ_ONCE(*pgdp)))
  657. return 0;
  658. p4dp = p4d_offset(pgdp, addr);
  659. if (p4d_none(READ_ONCE(*p4dp)))
  660. return 0;
  661. pudp = pud_offset(p4dp, addr);
  662. pud = READ_ONCE(*pudp);
  663. if (pud_none(pud))
  664. return 0;
  665. if (pud_sect(pud))
  666. return pfn_valid(pud_pfn(pud));
  667. pmdp = pmd_offset(pudp, addr);
  668. pmd = READ_ONCE(*pmdp);
  669. if (pmd_none(pmd))
  670. return 0;
  671. if (pmd_sect(pmd))
  672. return pfn_valid(pmd_pfn(pmd));
  673. ptep = pte_offset_kernel(pmdp, addr);
  674. pte = READ_ONCE(*ptep);
  675. if (pte_none(pte))
  676. return 0;
  677. return pfn_valid(pte_pfn(pte));
  678. }
  679. #ifdef CONFIG_MEMORY_HOTPLUG
  680. static void free_hotplug_page_range(struct page *page, size_t size,
  681. struct vmem_altmap *altmap)
  682. {
  683. if (altmap) {
  684. vmem_altmap_free(altmap, size >> PAGE_SHIFT);
  685. } else {
  686. WARN_ON(PageReserved(page));
  687. free_pages((unsigned long)page_address(page), get_order(size));
  688. }
  689. }
  690. static void free_hotplug_pgtable_page(struct page *page)
  691. {
  692. free_hotplug_page_range(page, PAGE_SIZE, NULL);
  693. }
  694. static bool pgtable_range_aligned(unsigned long start, unsigned long end,
  695. unsigned long floor, unsigned long ceiling,
  696. unsigned long mask)
  697. {
  698. start &= mask;
  699. if (start < floor)
  700. return false;
  701. if (ceiling) {
  702. ceiling &= mask;
  703. if (!ceiling)
  704. return false;
  705. }
  706. if (end - 1 > ceiling - 1)
  707. return false;
  708. return true;
  709. }
  710. static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
  711. unsigned long end, bool free_mapped,
  712. struct vmem_altmap *altmap)
  713. {
  714. pte_t *ptep, pte;
  715. do {
  716. ptep = pte_offset_kernel(pmdp, addr);
  717. pte = READ_ONCE(*ptep);
  718. if (pte_none(pte))
  719. continue;
  720. WARN_ON(!pte_present(pte));
  721. pte_clear(&init_mm, addr, ptep);
  722. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  723. if (free_mapped)
  724. free_hotplug_page_range(pte_page(pte),
  725. PAGE_SIZE, altmap);
  726. } while (addr += PAGE_SIZE, addr < end);
  727. }
  728. static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
  729. unsigned long end, bool free_mapped,
  730. struct vmem_altmap *altmap)
  731. {
  732. unsigned long next;
  733. pmd_t *pmdp, pmd;
  734. do {
  735. next = pmd_addr_end(addr, end);
  736. pmdp = pmd_offset(pudp, addr);
  737. pmd = READ_ONCE(*pmdp);
  738. if (pmd_none(pmd))
  739. continue;
  740. WARN_ON(!pmd_present(pmd));
  741. if (pmd_sect(pmd)) {
  742. pmd_clear(pmdp);
  743. /*
  744. * One TLBI should be sufficient here as the PMD_SIZE
  745. * range is mapped with a single block entry.
  746. */
  747. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  748. if (free_mapped)
  749. free_hotplug_page_range(pmd_page(pmd),
  750. PMD_SIZE, altmap);
  751. continue;
  752. }
  753. WARN_ON(!pmd_table(pmd));
  754. unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap);
  755. } while (addr = next, addr < end);
  756. }
  757. static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
  758. unsigned long end, bool free_mapped,
  759. struct vmem_altmap *altmap)
  760. {
  761. unsigned long next;
  762. pud_t *pudp, pud;
  763. do {
  764. next = pud_addr_end(addr, end);
  765. pudp = pud_offset(p4dp, addr);
  766. pud = READ_ONCE(*pudp);
  767. if (pud_none(pud))
  768. continue;
  769. WARN_ON(!pud_present(pud));
  770. if (pud_sect(pud)) {
  771. pud_clear(pudp);
  772. /*
  773. * One TLBI should be sufficient here as the PUD_SIZE
  774. * range is mapped with a single block entry.
  775. */
  776. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  777. if (free_mapped)
  778. free_hotplug_page_range(pud_page(pud),
  779. PUD_SIZE, altmap);
  780. continue;
  781. }
  782. WARN_ON(!pud_table(pud));
  783. unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap);
  784. } while (addr = next, addr < end);
  785. }
  786. static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
  787. unsigned long end, bool free_mapped,
  788. struct vmem_altmap *altmap)
  789. {
  790. unsigned long next;
  791. p4d_t *p4dp, p4d;
  792. do {
  793. next = p4d_addr_end(addr, end);
  794. p4dp = p4d_offset(pgdp, addr);
  795. p4d = READ_ONCE(*p4dp);
  796. if (p4d_none(p4d))
  797. continue;
  798. WARN_ON(!p4d_present(p4d));
  799. unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap);
  800. } while (addr = next, addr < end);
  801. }
  802. static void unmap_hotplug_range(unsigned long addr, unsigned long end,
  803. bool free_mapped, struct vmem_altmap *altmap)
  804. {
  805. unsigned long next;
  806. pgd_t *pgdp, pgd;
  807. /*
  808. * altmap can only be used as vmemmap mapping backing memory.
  809. * In case the backing memory itself is not being freed, then
  810. * altmap is irrelevant. Warn about this inconsistency when
  811. * encountered.
  812. */
  813. WARN_ON(!free_mapped && altmap);
  814. do {
  815. next = pgd_addr_end(addr, end);
  816. pgdp = pgd_offset_k(addr);
  817. pgd = READ_ONCE(*pgdp);
  818. if (pgd_none(pgd))
  819. continue;
  820. WARN_ON(!pgd_present(pgd));
  821. unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
  822. } while (addr = next, addr < end);
  823. }
  824. static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr,
  825. unsigned long end, unsigned long floor,
  826. unsigned long ceiling)
  827. {
  828. pte_t *ptep, pte;
  829. unsigned long i, start = addr;
  830. do {
  831. ptep = pte_offset_kernel(pmdp, addr);
  832. pte = READ_ONCE(*ptep);
  833. /*
  834. * This is just a sanity check here which verifies that
  835. * pte clearing has been done by earlier unmap loops.
  836. */
  837. WARN_ON(!pte_none(pte));
  838. } while (addr += PAGE_SIZE, addr < end);
  839. if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK))
  840. return;
  841. /*
  842. * Check whether we can free the pte page if the rest of the
  843. * entries are empty. Overlap with other regions have been
  844. * handled by the floor/ceiling check.
  845. */
  846. ptep = pte_offset_kernel(pmdp, 0UL);
  847. for (i = 0; i < PTRS_PER_PTE; i++) {
  848. if (!pte_none(READ_ONCE(ptep[i])))
  849. return;
  850. }
  851. pmd_clear(pmdp);
  852. __flush_tlb_kernel_pgtable(start);
  853. free_hotplug_pgtable_page(virt_to_page(ptep));
  854. }
  855. static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
  856. unsigned long end, unsigned long floor,
  857. unsigned long ceiling)
  858. {
  859. pmd_t *pmdp, pmd;
  860. unsigned long i, next, start = addr;
  861. do {
  862. next = pmd_addr_end(addr, end);
  863. pmdp = pmd_offset(pudp, addr);
  864. pmd = READ_ONCE(*pmdp);
  865. if (pmd_none(pmd))
  866. continue;
  867. WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd));
  868. free_empty_pte_table(pmdp, addr, next, floor, ceiling);
  869. } while (addr = next, addr < end);
  870. if (CONFIG_PGTABLE_LEVELS <= 2)
  871. return;
  872. if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
  873. return;
  874. /*
  875. * Check whether we can free the pmd page if the rest of the
  876. * entries are empty. Overlap with other regions have been
  877. * handled by the floor/ceiling check.
  878. */
  879. pmdp = pmd_offset(pudp, 0UL);
  880. for (i = 0; i < PTRS_PER_PMD; i++) {
  881. if (!pmd_none(READ_ONCE(pmdp[i])))
  882. return;
  883. }
  884. pud_clear(pudp);
  885. __flush_tlb_kernel_pgtable(start);
  886. free_hotplug_pgtable_page(virt_to_page(pmdp));
  887. }
  888. static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,
  889. unsigned long end, unsigned long floor,
  890. unsigned long ceiling)
  891. {
  892. pud_t *pudp, pud;
  893. unsigned long i, next, start = addr;
  894. do {
  895. next = pud_addr_end(addr, end);
  896. pudp = pud_offset(p4dp, addr);
  897. pud = READ_ONCE(*pudp);
  898. if (pud_none(pud))
  899. continue;
  900. WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud));
  901. free_empty_pmd_table(pudp, addr, next, floor, ceiling);
  902. } while (addr = next, addr < end);
  903. if (CONFIG_PGTABLE_LEVELS <= 3)
  904. return;
  905. if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
  906. return;
  907. /*
  908. * Check whether we can free the pud page if the rest of the
  909. * entries are empty. Overlap with other regions have been
  910. * handled by the floor/ceiling check.
  911. */
  912. pudp = pud_offset(p4dp, 0UL);
  913. for (i = 0; i < PTRS_PER_PUD; i++) {
  914. if (!pud_none(READ_ONCE(pudp[i])))
  915. return;
  916. }
  917. p4d_clear(p4dp);
  918. __flush_tlb_kernel_pgtable(start);
  919. free_hotplug_pgtable_page(virt_to_page(pudp));
  920. }
  921. static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
  922. unsigned long end, unsigned long floor,
  923. unsigned long ceiling)
  924. {
  925. unsigned long next;
  926. p4d_t *p4dp, p4d;
  927. do {
  928. next = p4d_addr_end(addr, end);
  929. p4dp = p4d_offset(pgdp, addr);
  930. p4d = READ_ONCE(*p4dp);
  931. if (p4d_none(p4d))
  932. continue;
  933. WARN_ON(!p4d_present(p4d));
  934. free_empty_pud_table(p4dp, addr, next, floor, ceiling);
  935. } while (addr = next, addr < end);
  936. }
  937. static void free_empty_tables(unsigned long addr, unsigned long end,
  938. unsigned long floor, unsigned long ceiling)
  939. {
  940. unsigned long next;
  941. pgd_t *pgdp, pgd;
  942. do {
  943. next = pgd_addr_end(addr, end);
  944. pgdp = pgd_offset_k(addr);
  945. pgd = READ_ONCE(*pgdp);
  946. if (pgd_none(pgd))
  947. continue;
  948. WARN_ON(!pgd_present(pgd));
  949. free_empty_p4d_table(pgdp, addr, next, floor, ceiling);
  950. } while (addr = next, addr < end);
  951. }
  952. #endif
  953. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  954. #if !ARM64_SWAPPER_USES_SECTION_MAPS
  955. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  956. struct vmem_altmap *altmap)
  957. {
  958. return vmemmap_populate_basepages(start, end, node, altmap);
  959. }
  960. #else /* !ARM64_SWAPPER_USES_SECTION_MAPS */
  961. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  962. struct vmem_altmap *altmap)
  963. {
  964. unsigned long addr = start;
  965. unsigned long next;
  966. pgd_t *pgdp;
  967. p4d_t *p4dp;
  968. pud_t *pudp;
  969. pmd_t *pmdp;
  970. do {
  971. next = pmd_addr_end(addr, end);
  972. pgdp = vmemmap_pgd_populate(addr, node);
  973. if (!pgdp)
  974. return -ENOMEM;
  975. p4dp = vmemmap_p4d_populate(pgdp, addr, node);
  976. if (!p4dp)
  977. return -ENOMEM;
  978. pudp = vmemmap_pud_populate(p4dp, addr, node);
  979. if (!pudp)
  980. return -ENOMEM;
  981. pmdp = pmd_offset(pudp, addr);
  982. if (pmd_none(READ_ONCE(*pmdp))) {
  983. void *p = NULL;
  984. p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
  985. if (!p) {
  986. if (vmemmap_populate_basepages(addr, next, node, altmap))
  987. return -ENOMEM;
  988. continue;
  989. }
  990. pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
  991. } else
  992. vmemmap_verify((pte_t *)pmdp, node, addr, next);
  993. } while (addr = next, addr != end);
  994. return 0;
  995. }
  996. #endif /* !ARM64_SWAPPER_USES_SECTION_MAPS */
  997. void vmemmap_free(unsigned long start, unsigned long end,
  998. struct vmem_altmap *altmap)
  999. {
  1000. #ifdef CONFIG_MEMORY_HOTPLUG
  1001. WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
  1002. unmap_hotplug_range(start, end, true, altmap);
  1003. free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END);
  1004. #endif
  1005. }
  1006. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  1007. static inline pud_t * fixmap_pud(unsigned long addr)
  1008. {
  1009. pgd_t *pgdp = pgd_offset_k(addr);
  1010. p4d_t *p4dp = p4d_offset(pgdp, addr);
  1011. p4d_t p4d = READ_ONCE(*p4dp);
  1012. BUG_ON(p4d_none(p4d) || p4d_bad(p4d));
  1013. return pud_offset_kimg(p4dp, addr);
  1014. }
  1015. static inline pmd_t * fixmap_pmd(unsigned long addr)
  1016. {
  1017. pud_t *pudp = fixmap_pud(addr);
  1018. pud_t pud = READ_ONCE(*pudp);
  1019. BUG_ON(pud_none(pud) || pud_bad(pud));
  1020. return pmd_offset_kimg(pudp, addr);
  1021. }
  1022. static inline pte_t * fixmap_pte(unsigned long addr)
  1023. {
  1024. return &bm_pte[pte_index(addr)];
  1025. }
  1026. /*
  1027. * The p*d_populate functions call virt_to_phys implicitly so they can't be used
  1028. * directly on kernel symbols (bm_p*d). This function is called too early to use
  1029. * lm_alias so __p*d_populate functions must be used to populate with the
  1030. * physical address from __pa_symbol.
  1031. */
  1032. void __init early_fixmap_init(void)
  1033. {
  1034. pgd_t *pgdp;
  1035. p4d_t *p4dp, p4d;
  1036. pud_t *pudp;
  1037. pmd_t *pmdp;
  1038. unsigned long addr = FIXADDR_START;
  1039. pgdp = pgd_offset_k(addr);
  1040. p4dp = p4d_offset(pgdp, addr);
  1041. p4d = READ_ONCE(*p4dp);
  1042. if (CONFIG_PGTABLE_LEVELS > 3 &&
  1043. !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) {
  1044. /*
  1045. * We only end up here if the kernel mapping and the fixmap
  1046. * share the top level pgd entry, which should only happen on
  1047. * 16k/4 levels configurations.
  1048. */
  1049. BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
  1050. pudp = pud_offset_kimg(p4dp, addr);
  1051. } else {
  1052. if (p4d_none(p4d))
  1053. __p4d_populate(p4dp, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
  1054. pudp = fixmap_pud(addr);
  1055. }
  1056. if (pud_none(READ_ONCE(*pudp)))
  1057. __pud_populate(pudp, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
  1058. pmdp = fixmap_pmd(addr);
  1059. __pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
  1060. /*
  1061. * The boot-ioremap range spans multiple pmds, for which
  1062. * we are not prepared:
  1063. */
  1064. BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
  1065. != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
  1066. if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
  1067. || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
  1068. WARN_ON(1);
  1069. pr_warn("pmdp %p != %p, %p\n",
  1070. pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
  1071. fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
  1072. pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  1073. fix_to_virt(FIX_BTMAP_BEGIN));
  1074. pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
  1075. fix_to_virt(FIX_BTMAP_END));
  1076. pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  1077. pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
  1078. }
  1079. }
  1080. /*
  1081. * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
  1082. * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
  1083. */
  1084. void __set_fixmap(enum fixed_addresses idx,
  1085. phys_addr_t phys, pgprot_t flags)
  1086. {
  1087. unsigned long addr = __fix_to_virt(idx);
  1088. pte_t *ptep;
  1089. BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
  1090. ptep = fixmap_pte(addr);
  1091. if (pgprot_val(flags)) {
  1092. set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
  1093. } else {
  1094. pte_clear(&init_mm, addr, ptep);
  1095. flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
  1096. }
  1097. }
  1098. void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
  1099. {
  1100. const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
  1101. int offset;
  1102. void *dt_virt;
  1103. /*
  1104. * Check whether the physical FDT address is set and meets the minimum
  1105. * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
  1106. * at least 8 bytes so that we can always access the magic and size
  1107. * fields of the FDT header after mapping the first chunk, double check
  1108. * here if that is indeed the case.
  1109. */
  1110. BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
  1111. if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
  1112. return NULL;
  1113. /*
  1114. * Make sure that the FDT region can be mapped without the need to
  1115. * allocate additional translation table pages, so that it is safe
  1116. * to call create_mapping_noalloc() this early.
  1117. *
  1118. * On 64k pages, the FDT will be mapped using PTEs, so we need to
  1119. * be in the same PMD as the rest of the fixmap.
  1120. * On 4k pages, we'll use section mappings for the FDT so we only
  1121. * have to be in the same PUD.
  1122. */
  1123. BUILD_BUG_ON(dt_virt_base % SZ_2M);
  1124. BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
  1125. __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
  1126. offset = dt_phys % SWAPPER_BLOCK_SIZE;
  1127. dt_virt = (void *)dt_virt_base + offset;
  1128. /* map the first chunk so we can read the size from the header */
  1129. create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
  1130. dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
  1131. if (fdt_magic(dt_virt) != FDT_MAGIC)
  1132. return NULL;
  1133. *size = fdt_totalsize(dt_virt);
  1134. if (*size > MAX_FDT_SIZE)
  1135. return NULL;
  1136. if (offset + *size > SWAPPER_BLOCK_SIZE)
  1137. create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
  1138. round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
  1139. return dt_virt;
  1140. }
  1141. int __init arch_ioremap_p4d_supported(void)
  1142. {
  1143. return 0;
  1144. }
  1145. int __init arch_ioremap_pud_supported(void)
  1146. {
  1147. /*
  1148. * Only 4k granule supports level 1 block mappings.
  1149. * SW table walks can't handle removal of intermediate entries.
  1150. */
  1151. return IS_ENABLED(CONFIG_ARM64_4K_PAGES) &&
  1152. !IS_ENABLED(CONFIG_PTDUMP_DEBUGFS);
  1153. }
  1154. int __init arch_ioremap_pmd_supported(void)
  1155. {
  1156. /* See arch_ioremap_pud_supported() */
  1157. return !IS_ENABLED(CONFIG_PTDUMP_DEBUGFS);
  1158. }
  1159. int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
  1160. {
  1161. pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot));
  1162. /* Only allow permission changes for now */
  1163. if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
  1164. pud_val(new_pud)))
  1165. return 0;
  1166. VM_BUG_ON(phys & ~PUD_MASK);
  1167. set_pud(pudp, new_pud);
  1168. return 1;
  1169. }
  1170. int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
  1171. {
  1172. pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot));
  1173. /* Only allow permission changes for now */
  1174. if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
  1175. pmd_val(new_pmd)))
  1176. return 0;
  1177. VM_BUG_ON(phys & ~PMD_MASK);
  1178. set_pmd(pmdp, new_pmd);
  1179. return 1;
  1180. }
  1181. int pud_clear_huge(pud_t *pudp)
  1182. {
  1183. if (!pud_sect(READ_ONCE(*pudp)))
  1184. return 0;
  1185. pud_clear(pudp);
  1186. return 1;
  1187. }
  1188. int pmd_clear_huge(pmd_t *pmdp)
  1189. {
  1190. if (!pmd_sect(READ_ONCE(*pmdp)))
  1191. return 0;
  1192. pmd_clear(pmdp);
  1193. return 1;
  1194. }
  1195. int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
  1196. {
  1197. pte_t *table;
  1198. pmd_t pmd;
  1199. pmd = READ_ONCE(*pmdp);
  1200. if (!pmd_table(pmd)) {
  1201. VM_WARN_ON(1);
  1202. return 1;
  1203. }
  1204. table = pte_offset_kernel(pmdp, addr);
  1205. pmd_clear(pmdp);
  1206. __flush_tlb_kernel_pgtable(addr);
  1207. pte_free_kernel(NULL, table);
  1208. return 1;
  1209. }
  1210. int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
  1211. {
  1212. pmd_t *table;
  1213. pmd_t *pmdp;
  1214. pud_t pud;
  1215. unsigned long next, end;
  1216. pud = READ_ONCE(*pudp);
  1217. if (!pud_table(pud)) {
  1218. VM_WARN_ON(1);
  1219. return 1;
  1220. }
  1221. table = pmd_offset(pudp, addr);
  1222. pmdp = table;
  1223. next = addr;
  1224. end = addr + PUD_SIZE;
  1225. do {
  1226. pmd_free_pte_page(pmdp, next);
  1227. } while (pmdp++, next += PMD_SIZE, next != end);
  1228. pud_clear(pudp);
  1229. __flush_tlb_kernel_pgtable(addr);
  1230. pmd_free(NULL, table);
  1231. return 1;
  1232. }
  1233. int p4d_free_pud_page(p4d_t *p4d, unsigned long addr)
  1234. {
  1235. return 0; /* Don't attempt a block mapping */
  1236. }
  1237. #ifdef CONFIG_MEMORY_HOTPLUG
  1238. static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
  1239. {
  1240. unsigned long end = start + size;
  1241. WARN_ON(pgdir != init_mm.pgd);
  1242. WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END));
  1243. unmap_hotplug_range(start, end, false, NULL);
  1244. free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
  1245. }
  1246. static bool inside_linear_region(u64 start, u64 size)
  1247. {
  1248. u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual));
  1249. u64 end_linear_pa = __pa(PAGE_END - 1);
  1250. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  1251. /*
  1252. * Check for a wrap, it is possible because of randomized linear
  1253. * mapping the start physical address is actually bigger than
  1254. * the end physical address. In this case set start to zero
  1255. * because [0, end_linear_pa] range must still be able to cover
  1256. * all addressable physical addresses.
  1257. */
  1258. if (start_linear_pa > end_linear_pa)
  1259. start_linear_pa = 0;
  1260. }
  1261. WARN_ON(start_linear_pa > end_linear_pa);
  1262. /*
  1263. * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
  1264. * accommodating both its ends but excluding PAGE_END. Max physical
  1265. * range which can be mapped inside this linear mapping range, must
  1266. * also be derived from its end points.
  1267. */
  1268. return start >= start_linear_pa && (start + size - 1) <= end_linear_pa;
  1269. }
  1270. int arch_add_memory(int nid, u64 start, u64 size,
  1271. struct mhp_params *params)
  1272. {
  1273. int ret, flags = 0;
  1274. if (!inside_linear_region(start, size)) {
  1275. pr_err("[%llx %llx] is outside linear mapping region\n", start, start + size);
  1276. return -EINVAL;
  1277. }
  1278. /*
  1279. * KFENCE requires linear map to be mapped at page granularity, so that
  1280. * it is possible to protect/unprotect single pages in the KFENCE pool.
  1281. */
  1282. if (rodata_full || debug_pagealloc_enabled() ||
  1283. IS_ENABLED(CONFIG_KFENCE))
  1284. flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  1285. __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
  1286. size, params->pgprot, __pgd_pgtable_alloc,
  1287. flags);
  1288. memblock_clear_nomap(start, size);
  1289. ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
  1290. params);
  1291. if (ret)
  1292. __remove_pgd_mapping(swapper_pg_dir,
  1293. __phys_to_virt(start), size);
  1294. else {
  1295. max_pfn = PFN_UP(start + size);
  1296. max_low_pfn = max_pfn;
  1297. }
  1298. return ret;
  1299. }
  1300. void arch_remove_memory(int nid, u64 start, u64 size,
  1301. struct vmem_altmap *altmap)
  1302. {
  1303. unsigned long start_pfn = start >> PAGE_SHIFT;
  1304. unsigned long nr_pages = size >> PAGE_SHIFT;
  1305. __remove_pages(start_pfn, nr_pages, altmap);
  1306. __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
  1307. }
  1308. int check_range_driver_managed(u64 start, u64 size, const char *resource_name)
  1309. {
  1310. struct mem_section *ms;
  1311. unsigned long pfn = __phys_to_pfn(start);
  1312. unsigned long end_pfn = __phys_to_pfn(start + size);
  1313. struct resource *res;
  1314. unsigned long flags;
  1315. res = lookup_resource(&iomem_resource, start);
  1316. if (!res) {
  1317. pr_err("%s: couldn't find memory resource for start 0x%llx\n",
  1318. __func__, start);
  1319. return -EINVAL;
  1320. }
  1321. flags = res->flags;
  1322. if (!(flags & IORESOURCE_SYSRAM_DRIVER_MANAGED) ||
  1323. strstr(resource_name, "System RAM (") != resource_name)
  1324. return -EINVAL;
  1325. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1326. ms = __pfn_to_section(pfn);
  1327. if (early_section(ms))
  1328. return -EINVAL;
  1329. }
  1330. return 0;
  1331. }
  1332. int populate_range_driver_managed(u64 start, u64 size,
  1333. const char *resource_name)
  1334. {
  1335. unsigned long virt = (unsigned long)phys_to_virt(start);
  1336. int flags = 0;
  1337. if (check_range_driver_managed(start, size, resource_name))
  1338. return -EINVAL;
  1339. /*
  1340. * When rodata_full is enabled, memory is mapped at page size granule,
  1341. * as opposed to block mapping.
  1342. */
  1343. if (rodata_full || debug_pagealloc_enabled())
  1344. flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  1345. __create_pgd_mapping(init_mm.pgd, start, virt, size,
  1346. PAGE_KERNEL, NULL, flags);
  1347. return 0;
  1348. }
  1349. EXPORT_SYMBOL_GPL(populate_range_driver_managed);
  1350. int depopulate_range_driver_managed(u64 start, u64 size,
  1351. const char *resource_name)
  1352. {
  1353. if (check_range_driver_managed(start, size, resource_name))
  1354. return -EINVAL;
  1355. unmap_hotplug_range(start, start + size, false, NULL);
  1356. return 0;
  1357. }
  1358. EXPORT_SYMBOL_GPL(depopulate_range_driver_managed);
  1359. /*
  1360. * This memory hotplug notifier helps prevent boot memory from being
  1361. * inadvertently removed as it blocks pfn range offlining process in
  1362. * __offline_pages(). Hence this prevents both offlining as well as
  1363. * removal process for boot memory which is initially always online.
  1364. * In future if and when boot memory could be removed, this notifier
  1365. * should be dropped and free_hotplug_page_range() should handle any
  1366. * reserved pages allocated during boot.
  1367. */
  1368. static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
  1369. unsigned long action, void *data)
  1370. {
  1371. struct mem_section *ms;
  1372. struct memory_notify *arg = data;
  1373. unsigned long end_pfn = arg->start_pfn + arg->nr_pages;
  1374. unsigned long pfn = arg->start_pfn;
  1375. if (action != MEM_GOING_OFFLINE)
  1376. return NOTIFY_OK;
  1377. for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  1378. ms = __pfn_to_section(pfn);
  1379. if (early_section(ms))
  1380. return NOTIFY_BAD;
  1381. }
  1382. return NOTIFY_OK;
  1383. }
  1384. static struct notifier_block prevent_bootmem_remove_nb = {
  1385. .notifier_call = prevent_bootmem_remove_notifier,
  1386. };
  1387. static int __init prevent_bootmem_remove_init(void)
  1388. {
  1389. return register_memory_notifier(&prevent_bootmem_remove_nb);
  1390. }
  1391. device_initcall(prevent_bootmem_remove_init);
  1392. #endif