mmu.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /*
  2. * Based on arch/arm/mm/mmu.c
  3. *
  4. * Copyright (C) 1995-2005 Russell King
  5. * Copyright (C) 2012 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/cache.h>
  20. #include <linux/export.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/init.h>
  24. #include <linux/ioport.h>
  25. #include <linux/kexec.h>
  26. #include <linux/libfdt.h>
  27. #include <linux/mman.h>
  28. #include <linux/nodemask.h>
  29. #include <linux/memblock.h>
  30. #include <linux/fs.h>
  31. #include <linux/io.h>
  32. #include <linux/mm.h>
  33. #include <linux/vmalloc.h>
  34. #include <asm/barrier.h>
  35. #include <asm/cputype.h>
  36. #include <asm/fixmap.h>
  37. #include <asm/kasan.h>
  38. #include <asm/kernel-pgtable.h>
  39. #include <asm/sections.h>
  40. #include <asm/setup.h>
  41. #include <asm/sizes.h>
  42. #include <asm/tlb.h>
  43. #include <asm/memblock.h>
  44. #include <asm/mmu_context.h>
  45. #include <asm/ptdump.h>
  46. #include <asm/tlbflush.h>
  47. #define NO_BLOCK_MAPPINGS BIT(0)
  48. #define NO_CONT_MAPPINGS BIT(1)
  49. u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
  50. u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
  51. u64 kimage_voffset __ro_after_init;
  52. EXPORT_SYMBOL(kimage_voffset);
  53. /*
  54. * Empty_zero_page is a special page that is used for zero-initialized data
  55. * and COW.
  56. */
  57. unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
  58. EXPORT_SYMBOL(empty_zero_page);
  59. static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
  60. static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
  61. static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
  62. static DEFINE_SPINLOCK(swapper_pgdir_lock);
  63. void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
  64. {
  65. pgd_t *fixmap_pgdp;
  66. spin_lock(&swapper_pgdir_lock);
  67. fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
  68. WRITE_ONCE(*fixmap_pgdp, pgd);
  69. /*
  70. * We need dsb(ishst) here to ensure the page-table-walker sees
  71. * our new entry before set_p?d() returns. The fixmap's
  72. * flush_tlb_kernel_range() via clear_fixmap() does this for us.
  73. */
  74. pgd_clear_fixmap();
  75. spin_unlock(&swapper_pgdir_lock);
  76. }
  77. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  78. unsigned long size, pgprot_t vma_prot)
  79. {
  80. if (!pfn_valid(pfn))
  81. return pgprot_noncached(vma_prot);
  82. else if (file->f_flags & O_SYNC)
  83. return pgprot_writecombine(vma_prot);
  84. return vma_prot;
  85. }
  86. EXPORT_SYMBOL(phys_mem_access_prot);
  87. static phys_addr_t __init early_pgtable_alloc(void)
  88. {
  89. phys_addr_t phys;
  90. void *ptr;
  91. phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
  92. /*
  93. * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
  94. * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
  95. * any level of table.
  96. */
  97. ptr = pte_set_fixmap(phys);
  98. memset(ptr, 0, PAGE_SIZE);
  99. /*
  100. * Implicit barriers also ensure the zeroed page is visible to the page
  101. * table walker
  102. */
  103. pte_clear_fixmap();
  104. return phys;
  105. }
  106. static bool pgattr_change_is_safe(u64 old, u64 new)
  107. {
  108. /*
  109. * The following mapping attributes may be updated in live
  110. * kernel mappings without the need for break-before-make.
  111. */
  112. static const pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
  113. /* creating or taking down mappings is always safe */
  114. if (old == 0 || new == 0)
  115. return true;
  116. /* live contiguous mappings may not be manipulated at all */
  117. if ((old | new) & PTE_CONT)
  118. return false;
  119. /* Transitioning from Non-Global to Global is unsafe */
  120. if (old & ~new & PTE_NG)
  121. return false;
  122. return ((old ^ new) & ~mask) == 0;
  123. }
  124. static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
  125. phys_addr_t phys, pgprot_t prot)
  126. {
  127. pte_t *ptep;
  128. ptep = pte_set_fixmap_offset(pmdp, addr);
  129. do {
  130. pte_t old_pte = READ_ONCE(*ptep);
  131. set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
  132. /*
  133. * After the PTE entry has been populated once, we
  134. * only allow updates to the permission attributes.
  135. */
  136. BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
  137. READ_ONCE(pte_val(*ptep))));
  138. phys += PAGE_SIZE;
  139. } while (ptep++, addr += PAGE_SIZE, addr != end);
  140. pte_clear_fixmap();
  141. }
  142. static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
  143. unsigned long end, phys_addr_t phys,
  144. pgprot_t prot,
  145. phys_addr_t (*pgtable_alloc)(void),
  146. int flags)
  147. {
  148. unsigned long next;
  149. pmd_t pmd = READ_ONCE(*pmdp);
  150. BUG_ON(pmd_sect(pmd));
  151. if (pmd_none(pmd)) {
  152. phys_addr_t pte_phys;
  153. BUG_ON(!pgtable_alloc);
  154. pte_phys = pgtable_alloc();
  155. __pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
  156. pmd = READ_ONCE(*pmdp);
  157. }
  158. BUG_ON(pmd_bad(pmd));
  159. do {
  160. pgprot_t __prot = prot;
  161. next = pte_cont_addr_end(addr, end);
  162. /* use a contiguous mapping if the range is suitably aligned */
  163. if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
  164. (flags & NO_CONT_MAPPINGS) == 0)
  165. __prot = __pgprot(pgprot_val(prot) | PTE_CONT);
  166. init_pte(pmdp, addr, next, phys, __prot);
  167. phys += next - addr;
  168. } while (addr = next, addr != end);
  169. }
  170. static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,
  171. phys_addr_t phys, pgprot_t prot,
  172. phys_addr_t (*pgtable_alloc)(void), int flags)
  173. {
  174. unsigned long next;
  175. pmd_t *pmdp;
  176. pmdp = pmd_set_fixmap_offset(pudp, addr);
  177. do {
  178. pmd_t old_pmd = READ_ONCE(*pmdp);
  179. next = pmd_addr_end(addr, end);
  180. /* try section mapping first */
  181. if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
  182. (flags & NO_BLOCK_MAPPINGS) == 0) {
  183. pmd_set_huge(pmdp, phys, prot);
  184. /*
  185. * After the PMD entry has been populated once, we
  186. * only allow updates to the permission attributes.
  187. */
  188. BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
  189. READ_ONCE(pmd_val(*pmdp))));
  190. } else {
  191. alloc_init_cont_pte(pmdp, addr, next, phys, prot,
  192. pgtable_alloc, flags);
  193. BUG_ON(pmd_val(old_pmd) != 0 &&
  194. pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
  195. }
  196. phys += next - addr;
  197. } while (pmdp++, addr = next, addr != end);
  198. pmd_clear_fixmap();
  199. }
  200. static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
  201. unsigned long end, phys_addr_t phys,
  202. pgprot_t prot,
  203. phys_addr_t (*pgtable_alloc)(void), int flags)
  204. {
  205. unsigned long next;
  206. pud_t pud = READ_ONCE(*pudp);
  207. /*
  208. * Check for initial section mappings in the pgd/pud.
  209. */
  210. BUG_ON(pud_sect(pud));
  211. if (pud_none(pud)) {
  212. phys_addr_t pmd_phys;
  213. BUG_ON(!pgtable_alloc);
  214. pmd_phys = pgtable_alloc();
  215. __pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
  216. pud = READ_ONCE(*pudp);
  217. }
  218. BUG_ON(pud_bad(pud));
  219. do {
  220. pgprot_t __prot = prot;
  221. next = pmd_cont_addr_end(addr, end);
  222. /* use a contiguous mapping if the range is suitably aligned */
  223. if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
  224. (flags & NO_CONT_MAPPINGS) == 0)
  225. __prot = __pgprot(pgprot_val(prot) | PTE_CONT);
  226. init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags);
  227. phys += next - addr;
  228. } while (addr = next, addr != end);
  229. }
  230. static inline bool use_1G_block(unsigned long addr, unsigned long next,
  231. unsigned long phys)
  232. {
  233. if (PAGE_SHIFT != 12)
  234. return false;
  235. if (((addr | next | phys) & ~PUD_MASK) != 0)
  236. return false;
  237. return true;
  238. }
  239. static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,
  240. phys_addr_t phys, pgprot_t prot,
  241. phys_addr_t (*pgtable_alloc)(void),
  242. int flags)
  243. {
  244. unsigned long next;
  245. pud_t *pudp;
  246. pgd_t pgd = READ_ONCE(*pgdp);
  247. if (pgd_none(pgd)) {
  248. phys_addr_t pud_phys;
  249. BUG_ON(!pgtable_alloc);
  250. pud_phys = pgtable_alloc();
  251. __pgd_populate(pgdp, pud_phys, PUD_TYPE_TABLE);
  252. pgd = READ_ONCE(*pgdp);
  253. }
  254. BUG_ON(pgd_bad(pgd));
  255. pudp = pud_set_fixmap_offset(pgdp, addr);
  256. do {
  257. pud_t old_pud = READ_ONCE(*pudp);
  258. next = pud_addr_end(addr, end);
  259. /*
  260. * For 4K granule only, attempt to put down a 1GB block
  261. */
  262. if (use_1G_block(addr, next, phys) &&
  263. (flags & NO_BLOCK_MAPPINGS) == 0) {
  264. pud_set_huge(pudp, phys, prot);
  265. /*
  266. * After the PUD entry has been populated once, we
  267. * only allow updates to the permission attributes.
  268. */
  269. BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
  270. READ_ONCE(pud_val(*pudp))));
  271. } else {
  272. alloc_init_cont_pmd(pudp, addr, next, phys, prot,
  273. pgtable_alloc, flags);
  274. BUG_ON(pud_val(old_pud) != 0 &&
  275. pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
  276. }
  277. phys += next - addr;
  278. } while (pudp++, addr = next, addr != end);
  279. pud_clear_fixmap();
  280. }
  281. static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
  282. unsigned long virt, phys_addr_t size,
  283. pgprot_t prot,
  284. phys_addr_t (*pgtable_alloc)(void),
  285. int flags)
  286. {
  287. unsigned long addr, length, end, next;
  288. pgd_t *pgdp = pgd_offset_raw(pgdir, virt);
  289. /*
  290. * If the virtual and physical address don't have the same offset
  291. * within a page, we cannot map the region as the caller expects.
  292. */
  293. if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
  294. return;
  295. phys &= PAGE_MASK;
  296. addr = virt & PAGE_MASK;
  297. length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
  298. end = addr + length;
  299. do {
  300. next = pgd_addr_end(addr, end);
  301. alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc,
  302. flags);
  303. phys += next - addr;
  304. } while (pgdp++, addr = next, addr != end);
  305. }
  306. static phys_addr_t pgd_pgtable_alloc(void)
  307. {
  308. void *ptr = (void *)__get_free_page(PGALLOC_GFP);
  309. if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))
  310. BUG();
  311. /* Ensure the zeroed page is visible to the page table walker */
  312. dsb(ishst);
  313. return __pa(ptr);
  314. }
  315. /*
  316. * This function can only be used to modify existing table entries,
  317. * without allocating new levels of table. Note that this permits the
  318. * creation of new section or page entries.
  319. */
  320. static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
  321. phys_addr_t size, pgprot_t prot)
  322. {
  323. if (virt < VMALLOC_START) {
  324. pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
  325. &phys, virt);
  326. return;
  327. }
  328. __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
  329. NO_CONT_MAPPINGS);
  330. }
  331. void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
  332. unsigned long virt, phys_addr_t size,
  333. pgprot_t prot, bool page_mappings_only)
  334. {
  335. int flags = 0;
  336. BUG_ON(mm == &init_mm);
  337. if (page_mappings_only)
  338. flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  339. __create_pgd_mapping(mm->pgd, phys, virt, size, prot,
  340. pgd_pgtable_alloc, flags);
  341. }
  342. static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
  343. phys_addr_t size, pgprot_t prot)
  344. {
  345. if (virt < VMALLOC_START) {
  346. pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
  347. &phys, virt);
  348. return;
  349. }
  350. __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
  351. NO_CONT_MAPPINGS);
  352. /* flush the TLBs after updating live kernel mappings */
  353. flush_tlb_kernel_range(virt, virt + size);
  354. }
  355. static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
  356. phys_addr_t end, pgprot_t prot, int flags)
  357. {
  358. __create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
  359. prot, early_pgtable_alloc, flags);
  360. }
  361. void __init mark_linear_text_alias_ro(void)
  362. {
  363. /*
  364. * Remove the write permissions from the linear alias of .text/.rodata
  365. */
  366. update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
  367. (unsigned long)__init_begin - (unsigned long)_text,
  368. PAGE_KERNEL_RO);
  369. }
  370. static void __init map_mem(pgd_t *pgdp)
  371. {
  372. phys_addr_t kernel_start = __pa_symbol(_text);
  373. phys_addr_t kernel_end = __pa_symbol(__init_begin);
  374. struct memblock_region *reg;
  375. int flags = 0;
  376. if (debug_pagealloc_enabled())
  377. flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
  378. /*
  379. * Take care not to create a writable alias for the
  380. * read-only text and rodata sections of the kernel image.
  381. * So temporarily mark them as NOMAP to skip mappings in
  382. * the following for-loop
  383. */
  384. memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
  385. #ifdef CONFIG_KEXEC_CORE
  386. if (crashk_res.end)
  387. memblock_mark_nomap(crashk_res.start,
  388. resource_size(&crashk_res));
  389. #endif
  390. /* map all the memory banks */
  391. for_each_memblock(memory, reg) {
  392. phys_addr_t start = reg->base;
  393. phys_addr_t end = start + reg->size;
  394. if (start >= end)
  395. break;
  396. if (memblock_is_nomap(reg))
  397. continue;
  398. __map_memblock(pgdp, start, end, PAGE_KERNEL, flags);
  399. }
  400. /*
  401. * Map the linear alias of the [_text, __init_begin) interval
  402. * as non-executable now, and remove the write permission in
  403. * mark_linear_text_alias_ro() below (which will be called after
  404. * alternative patching has completed). This makes the contents
  405. * of the region accessible to subsystems such as hibernate,
  406. * but protects it from inadvertent modification or execution.
  407. * Note that contiguous mappings cannot be remapped in this way,
  408. * so we should avoid them here.
  409. */
  410. __map_memblock(pgdp, kernel_start, kernel_end,
  411. PAGE_KERNEL, NO_CONT_MAPPINGS);
  412. memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
  413. #ifdef CONFIG_KEXEC_CORE
  414. /*
  415. * Use page-level mappings here so that we can shrink the region
  416. * in page granularity and put back unused memory to buddy system
  417. * through /sys/kernel/kexec_crash_size interface.
  418. */
  419. if (crashk_res.end) {
  420. __map_memblock(pgdp, crashk_res.start, crashk_res.end + 1,
  421. PAGE_KERNEL,
  422. NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
  423. memblock_clear_nomap(crashk_res.start,
  424. resource_size(&crashk_res));
  425. }
  426. #endif
  427. }
  428. void mark_rodata_ro(void)
  429. {
  430. unsigned long section_size;
  431. /*
  432. * mark .rodata as read only. Use __init_begin rather than __end_rodata
  433. * to cover NOTES and EXCEPTION_TABLE.
  434. */
  435. section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
  436. update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
  437. section_size, PAGE_KERNEL_RO);
  438. debug_checkwx();
  439. }
  440. static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
  441. pgprot_t prot, struct vm_struct *vma,
  442. int flags, unsigned long vm_flags)
  443. {
  444. phys_addr_t pa_start = __pa_symbol(va_start);
  445. unsigned long size = va_end - va_start;
  446. BUG_ON(!PAGE_ALIGNED(pa_start));
  447. BUG_ON(!PAGE_ALIGNED(size));
  448. __create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
  449. early_pgtable_alloc, flags);
  450. if (!(vm_flags & VM_NO_GUARD))
  451. size += PAGE_SIZE;
  452. vma->addr = va_start;
  453. vma->phys_addr = pa_start;
  454. vma->size = size;
  455. vma->flags = VM_MAP | vm_flags;
  456. vma->caller = __builtin_return_address(0);
  457. vm_area_add_early(vma);
  458. }
  459. static int __init parse_rodata(char *arg)
  460. {
  461. return strtobool(arg, &rodata_enabled);
  462. }
  463. early_param("rodata", parse_rodata);
  464. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  465. static int __init map_entry_trampoline(void)
  466. {
  467. pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
  468. phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
  469. /* The trampoline is always mapped and can therefore be global */
  470. pgprot_val(prot) &= ~PTE_NG;
  471. /* Map only the text into the trampoline page table */
  472. memset(tramp_pg_dir, 0, PGD_SIZE);
  473. __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE,
  474. prot, pgd_pgtable_alloc, 0);
  475. /* Map both the text and data into the kernel page table */
  476. __set_fixmap(FIX_ENTRY_TRAMP_TEXT, pa_start, prot);
  477. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  478. extern char __entry_tramp_data_start[];
  479. __set_fixmap(FIX_ENTRY_TRAMP_DATA,
  480. __pa_symbol(__entry_tramp_data_start),
  481. PAGE_KERNEL_RO);
  482. }
  483. return 0;
  484. }
  485. core_initcall(map_entry_trampoline);
  486. #endif
  487. /*
  488. * Create fine-grained mappings for the kernel.
  489. */
  490. static void __init map_kernel(pgd_t *pgdp)
  491. {
  492. static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
  493. vmlinux_initdata, vmlinux_data;
  494. /*
  495. * External debuggers may need to write directly to the text
  496. * mapping to install SW breakpoints. Allow this (only) when
  497. * explicitly requested with rodata=off.
  498. */
  499. pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
  500. /*
  501. * Only rodata will be remapped with different permissions later on,
  502. * all other segments are allowed to use contiguous mappings.
  503. */
  504. map_kernel_segment(pgdp, _text, _etext, text_prot, &vmlinux_text, 0,
  505. VM_NO_GUARD);
  506. map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
  507. &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
  508. map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
  509. &vmlinux_inittext, 0, VM_NO_GUARD);
  510. map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
  511. &vmlinux_initdata, 0, VM_NO_GUARD);
  512. map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
  513. if (!READ_ONCE(pgd_val(*pgd_offset_raw(pgdp, FIXADDR_START)))) {
  514. /*
  515. * The fixmap falls in a separate pgd to the kernel, and doesn't
  516. * live in the carveout for the swapper_pg_dir. We can simply
  517. * re-use the existing dir for the fixmap.
  518. */
  519. set_pgd(pgd_offset_raw(pgdp, FIXADDR_START),
  520. READ_ONCE(*pgd_offset_k(FIXADDR_START)));
  521. } else if (CONFIG_PGTABLE_LEVELS > 3) {
  522. /*
  523. * The fixmap shares its top level pgd entry with the kernel
  524. * mapping. This can really only occur when we are running
  525. * with 16k/4 levels, so we can simply reuse the pud level
  526. * entry instead.
  527. */
  528. BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
  529. pud_populate(&init_mm,
  530. pud_set_fixmap_offset(pgdp, FIXADDR_START),
  531. lm_alias(bm_pmd));
  532. pud_clear_fixmap();
  533. } else {
  534. BUG();
  535. }
  536. kasan_copy_shadow(pgdp);
  537. }
  538. /*
  539. * paging_init() sets up the page tables, initialises the zone memory
  540. * maps and sets up the zero page.
  541. */
  542. void __init paging_init(void)
  543. {
  544. pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
  545. map_kernel(pgdp);
  546. map_mem(pgdp);
  547. pgd_clear_fixmap();
  548. cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
  549. init_mm.pgd = swapper_pg_dir;
  550. memblock_free(__pa_symbol(init_pg_dir),
  551. __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
  552. memblock_allow_resize();
  553. }
  554. /*
  555. * Check whether a kernel address is valid (derived from arch/x86/).
  556. */
  557. int kern_addr_valid(unsigned long addr)
  558. {
  559. pgd_t *pgdp;
  560. pud_t *pudp, pud;
  561. pmd_t *pmdp, pmd;
  562. pte_t *ptep, pte;
  563. if ((((long)addr) >> VA_BITS) != -1UL)
  564. return 0;
  565. pgdp = pgd_offset_k(addr);
  566. if (pgd_none(READ_ONCE(*pgdp)))
  567. return 0;
  568. pudp = pud_offset(pgdp, addr);
  569. pud = READ_ONCE(*pudp);
  570. if (pud_none(pud))
  571. return 0;
  572. if (pud_sect(pud))
  573. return pfn_valid(pud_pfn(pud));
  574. pmdp = pmd_offset(pudp, addr);
  575. pmd = READ_ONCE(*pmdp);
  576. if (pmd_none(pmd))
  577. return 0;
  578. if (pmd_sect(pmd))
  579. return pfn_valid(pmd_pfn(pmd));
  580. ptep = pte_offset_kernel(pmdp, addr);
  581. pte = READ_ONCE(*ptep);
  582. if (pte_none(pte))
  583. return 0;
  584. return pfn_valid(pte_pfn(pte));
  585. }
  586. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  587. #if !ARM64_SWAPPER_USES_SECTION_MAPS
  588. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  589. struct vmem_altmap *altmap)
  590. {
  591. return vmemmap_populate_basepages(start, end, node);
  592. }
  593. #else /* !ARM64_SWAPPER_USES_SECTION_MAPS */
  594. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  595. struct vmem_altmap *altmap)
  596. {
  597. unsigned long addr = start;
  598. unsigned long next;
  599. pgd_t *pgdp;
  600. pud_t *pudp;
  601. pmd_t *pmdp;
  602. do {
  603. next = pmd_addr_end(addr, end);
  604. pgdp = vmemmap_pgd_populate(addr, node);
  605. if (!pgdp)
  606. return -ENOMEM;
  607. pudp = vmemmap_pud_populate(pgdp, addr, node);
  608. if (!pudp)
  609. return -ENOMEM;
  610. pmdp = pmd_offset(pudp, addr);
  611. if (pmd_none(READ_ONCE(*pmdp))) {
  612. void *p = NULL;
  613. p = vmemmap_alloc_block_buf(PMD_SIZE, node);
  614. if (!p)
  615. return -ENOMEM;
  616. pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
  617. } else
  618. vmemmap_verify((pte_t *)pmdp, node, addr, next);
  619. } while (addr = next, addr != end);
  620. return 0;
  621. }
  622. #endif /* CONFIG_ARM64_64K_PAGES */
  623. void vmemmap_free(unsigned long start, unsigned long end,
  624. struct vmem_altmap *altmap)
  625. {
  626. }
  627. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  628. static inline pud_t * fixmap_pud(unsigned long addr)
  629. {
  630. pgd_t *pgdp = pgd_offset_k(addr);
  631. pgd_t pgd = READ_ONCE(*pgdp);
  632. BUG_ON(pgd_none(pgd) || pgd_bad(pgd));
  633. return pud_offset_kimg(pgdp, addr);
  634. }
  635. static inline pmd_t * fixmap_pmd(unsigned long addr)
  636. {
  637. pud_t *pudp = fixmap_pud(addr);
  638. pud_t pud = READ_ONCE(*pudp);
  639. BUG_ON(pud_none(pud) || pud_bad(pud));
  640. return pmd_offset_kimg(pudp, addr);
  641. }
  642. static inline pte_t * fixmap_pte(unsigned long addr)
  643. {
  644. return &bm_pte[pte_index(addr)];
  645. }
  646. /*
  647. * The p*d_populate functions call virt_to_phys implicitly so they can't be used
  648. * directly on kernel symbols (bm_p*d). This function is called too early to use
  649. * lm_alias so __p*d_populate functions must be used to populate with the
  650. * physical address from __pa_symbol.
  651. */
  652. void __init early_fixmap_init(void)
  653. {
  654. pgd_t *pgdp, pgd;
  655. pud_t *pudp;
  656. pmd_t *pmdp;
  657. unsigned long addr = FIXADDR_START;
  658. pgdp = pgd_offset_k(addr);
  659. pgd = READ_ONCE(*pgdp);
  660. if (CONFIG_PGTABLE_LEVELS > 3 &&
  661. !(pgd_none(pgd) || pgd_page_paddr(pgd) == __pa_symbol(bm_pud))) {
  662. /*
  663. * We only end up here if the kernel mapping and the fixmap
  664. * share the top level pgd entry, which should only happen on
  665. * 16k/4 levels configurations.
  666. */
  667. BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
  668. pudp = pud_offset_kimg(pgdp, addr);
  669. } else {
  670. if (pgd_none(pgd))
  671. __pgd_populate(pgdp, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
  672. pudp = fixmap_pud(addr);
  673. }
  674. if (pud_none(READ_ONCE(*pudp)))
  675. __pud_populate(pudp, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
  676. pmdp = fixmap_pmd(addr);
  677. __pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
  678. /*
  679. * The boot-ioremap range spans multiple pmds, for which
  680. * we are not prepared:
  681. */
  682. BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
  683. != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
  684. if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
  685. || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
  686. WARN_ON(1);
  687. pr_warn("pmdp %p != %p, %p\n",
  688. pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
  689. fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
  690. pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  691. fix_to_virt(FIX_BTMAP_BEGIN));
  692. pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
  693. fix_to_virt(FIX_BTMAP_END));
  694. pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  695. pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
  696. }
  697. }
  698. /*
  699. * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
  700. * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
  701. */
  702. void __set_fixmap(enum fixed_addresses idx,
  703. phys_addr_t phys, pgprot_t flags)
  704. {
  705. unsigned long addr = __fix_to_virt(idx);
  706. pte_t *ptep;
  707. BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
  708. ptep = fixmap_pte(addr);
  709. if (pgprot_val(flags)) {
  710. set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
  711. } else {
  712. pte_clear(&init_mm, addr, ptep);
  713. flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
  714. }
  715. }
  716. void *__init __fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
  717. {
  718. const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
  719. int offset;
  720. void *dt_virt;
  721. /*
  722. * Check whether the physical FDT address is set and meets the minimum
  723. * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
  724. * at least 8 bytes so that we can always access the magic and size
  725. * fields of the FDT header after mapping the first chunk, double check
  726. * here if that is indeed the case.
  727. */
  728. BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
  729. if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
  730. return NULL;
  731. /*
  732. * Make sure that the FDT region can be mapped without the need to
  733. * allocate additional translation table pages, so that it is safe
  734. * to call create_mapping_noalloc() this early.
  735. *
  736. * On 64k pages, the FDT will be mapped using PTEs, so we need to
  737. * be in the same PMD as the rest of the fixmap.
  738. * On 4k pages, we'll use section mappings for the FDT so we only
  739. * have to be in the same PUD.
  740. */
  741. BUILD_BUG_ON(dt_virt_base % SZ_2M);
  742. BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
  743. __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
  744. offset = dt_phys % SWAPPER_BLOCK_SIZE;
  745. dt_virt = (void *)dt_virt_base + offset;
  746. /* map the first chunk so we can read the size from the header */
  747. create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
  748. dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
  749. if (fdt_magic(dt_virt) != FDT_MAGIC)
  750. return NULL;
  751. *size = fdt_totalsize(dt_virt);
  752. if (*size > MAX_FDT_SIZE)
  753. return NULL;
  754. if (offset + *size > SWAPPER_BLOCK_SIZE)
  755. create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
  756. round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
  757. return dt_virt;
  758. }
  759. void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
  760. {
  761. void *dt_virt;
  762. int size;
  763. dt_virt = __fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
  764. if (!dt_virt)
  765. return NULL;
  766. memblock_reserve(dt_phys, size);
  767. return dt_virt;
  768. }
  769. int __init arch_ioremap_pud_supported(void)
  770. {
  771. /* only 4k granule supports level 1 block mappings */
  772. return IS_ENABLED(CONFIG_ARM64_4K_PAGES);
  773. }
  774. int __init arch_ioremap_pmd_supported(void)
  775. {
  776. return 1;
  777. }
  778. int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
  779. {
  780. pgprot_t sect_prot = __pgprot(PUD_TYPE_SECT |
  781. pgprot_val(mk_sect_prot(prot)));
  782. pud_t new_pud = pfn_pud(__phys_to_pfn(phys), sect_prot);
  783. /* Only allow permission changes for now */
  784. if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
  785. pud_val(new_pud)))
  786. return 0;
  787. BUG_ON(phys & ~PUD_MASK);
  788. set_pud(pudp, new_pud);
  789. return 1;
  790. }
  791. int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
  792. {
  793. pgprot_t sect_prot = __pgprot(PMD_TYPE_SECT |
  794. pgprot_val(mk_sect_prot(prot)));
  795. pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), sect_prot);
  796. /* Only allow permission changes for now */
  797. if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
  798. pmd_val(new_pmd)))
  799. return 0;
  800. BUG_ON(phys & ~PMD_MASK);
  801. set_pmd(pmdp, new_pmd);
  802. return 1;
  803. }
  804. int pud_clear_huge(pud_t *pudp)
  805. {
  806. if (!pud_sect(READ_ONCE(*pudp)))
  807. return 0;
  808. pud_clear(pudp);
  809. return 1;
  810. }
  811. int pmd_clear_huge(pmd_t *pmdp)
  812. {
  813. if (!pmd_sect(READ_ONCE(*pmdp)))
  814. return 0;
  815. pmd_clear(pmdp);
  816. return 1;
  817. }
  818. int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
  819. {
  820. pte_t *table;
  821. pmd_t pmd;
  822. pmd = READ_ONCE(*pmdp);
  823. if (!pmd_present(pmd))
  824. return 1;
  825. if (!pmd_table(pmd)) {
  826. VM_WARN_ON(!pmd_table(pmd));
  827. return 1;
  828. }
  829. table = pte_offset_kernel(pmdp, addr);
  830. pmd_clear(pmdp);
  831. __flush_tlb_kernel_pgtable(addr);
  832. pte_free_kernel(NULL, table);
  833. return 1;
  834. }
  835. int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
  836. {
  837. pmd_t *table;
  838. pmd_t *pmdp;
  839. pud_t pud;
  840. unsigned long next, end;
  841. pud = READ_ONCE(*pudp);
  842. if (!pud_present(pud))
  843. return 1;
  844. if (!pud_table(pud)) {
  845. VM_WARN_ON(!pud_table(pud));
  846. return 1;
  847. }
  848. table = pmd_offset(pudp, addr);
  849. pmdp = table;
  850. next = addr;
  851. end = addr + PUD_SIZE;
  852. do {
  853. pmd_free_pte_page(pmdp, next);
  854. } while (pmdp++, next += PMD_SIZE, next != end);
  855. pud_clear(pudp);
  856. __flush_tlb_kernel_pgtable(addr);
  857. pmd_free(NULL, table);
  858. return 1;
  859. }