1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * This file contains the routines setting up the linux page tables.
6 * Derived from arch/ppc/mm/init.c:
7 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
9 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
10 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
11 * Copyright (C) 1996 Paul Mackerras
13 * Derived from "arch/i386/mm/init.c"
14 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
21 #include <linux/vmalloc.h>
22 #include <linux/init.h>
23 #include <linux/highmem.h>
24 #include <linux/memblock.h>
25 #include <linux/slab.h>
27 #include <asm/pgtable.h>
28 #include <asm/pgalloc.h>
29 #include <asm/fixmap.h>
31 #include <asm/setup.h>
32 #include <asm/sections.h>
34 #include <mm/mmu_decl.h>
36 unsigned long ioremap_bot;
37 EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */
39 extern char etext[], _stext[], _sinittext[], _einittext[];
42 ioremap(phys_addr_t addr, unsigned long size)
44 pgprot_t prot = pgprot_noncached(PAGE_KERNEL);
46 return __ioremap_caller(addr, size, prot, __builtin_return_address(0));
48 EXPORT_SYMBOL(ioremap);
51 ioremap_wc(phys_addr_t addr, unsigned long size)
53 pgprot_t prot = pgprot_noncached_wc(PAGE_KERNEL);
55 return __ioremap_caller(addr, size, prot, __builtin_return_address(0));
57 EXPORT_SYMBOL(ioremap_wc);
60 ioremap_wt(phys_addr_t addr, unsigned long size)
62 pgprot_t prot = pgprot_cached_wthru(PAGE_KERNEL);
64 return __ioremap_caller(addr, size, prot, __builtin_return_address(0));
66 EXPORT_SYMBOL(ioremap_wt);
69 ioremap_coherent(phys_addr_t addr, unsigned long size)
71 pgprot_t prot = pgprot_cached(PAGE_KERNEL);
73 return __ioremap_caller(addr, size, prot, __builtin_return_address(0));
75 EXPORT_SYMBOL(ioremap_coherent);
78 ioremap_prot(phys_addr_t addr, unsigned long size, unsigned long flags)
80 pte_t pte = __pte(flags);
82 /* writeable implies dirty for kernel addresses */
84 pte = pte_mkdirty(pte);
86 /* we don't want to let _PAGE_USER and _PAGE_EXEC leak out */
87 pte = pte_exprotect(pte);
88 pte = pte_mkprivileged(pte);
90 return __ioremap_caller(addr, size, pte_pgprot(pte), __builtin_return_address(0));
92 EXPORT_SYMBOL(ioremap_prot);
95 __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
97 return __ioremap_caller(addr, size, __pgprot(flags), __builtin_return_address(0));
101 __ioremap_caller(phys_addr_t addr, unsigned long size, pgprot_t prot, void *caller)
108 * Choose an address to map it to.
109 * Once the vmalloc system is running, we use it.
110 * Before then, we use space going down from IOREMAP_TOP
111 * (ioremap_bot records where we're up to).
113 p = addr & PAGE_MASK;
114 size = PAGE_ALIGN(addr + size) - p;
117 * If the address lies within the first 16 MB, assume it's in ISA
120 if (p < 16*1024*1024)
123 #ifndef CONFIG_CRASH_DUMP
125 * Don't allow anybody to remap normal RAM that we're using.
126 * mem_init() sets high_memory so only do the check after that.
128 if (slab_is_available() && p <= virt_to_phys(high_memory - 1) &&
129 page_is_ram(__phys_to_pfn(p))) {
130 printk("__ioremap(): phys addr 0x%llx is RAM lr %ps\n",
131 (unsigned long long)p, __builtin_return_address(0));
140 * Is it already mapped? Perhaps overlapped by a previous
143 v = p_block_mapped(p);
147 if (slab_is_available()) {
148 struct vm_struct *area;
149 area = get_vm_area_caller(size, VM_IOREMAP, caller);
153 v = (unsigned long) area->addr;
155 v = (ioremap_bot -= size);
159 * Should check if it is a candidate for a BAT mapping
163 for (i = 0; i < size && err == 0; i += PAGE_SIZE)
164 err = map_kernel_page(v + i, p + i, prot);
166 if (slab_is_available())
172 return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
174 EXPORT_SYMBOL(__ioremap);
176 void iounmap(volatile void __iomem *addr)
179 * If mapped by BATs then there is nothing to do.
180 * Calling vfree() generates a benign warning.
182 if (v_block_mapped((unsigned long)addr))
185 if (addr > high_memory && (unsigned long) addr < ioremap_bot)
186 vunmap((void *) (PAGE_MASK & (unsigned long)addr));
188 EXPORT_SYMBOL(iounmap);
190 static void __init *early_alloc_pgtable(unsigned long size)
192 void *ptr = memblock_alloc(size, size);
195 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
196 __func__, size, size);
201 static pte_t __init *early_pte_alloc_kernel(pmd_t *pmdp, unsigned long va)
203 if (pmd_none(*pmdp)) {
204 pte_t *ptep = early_alloc_pgtable(PTE_FRAG_SIZE);
206 pmd_populate_kernel(&init_mm, pmdp, ptep);
208 return pte_offset_kernel(pmdp, va);
212 int __ref map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot)
218 /* Use upper 10 bits of VA to index the first level map */
219 pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va);
220 /* Use middle 10 bits of VA to index the second-level map */
221 if (likely(slab_is_available()))
222 pg = pte_alloc_kernel(pd, va);
224 pg = early_pte_alloc_kernel(pd, va);
227 /* The PTE should never be already set nor present in the
230 BUG_ON((pte_present(*pg) | pte_hashpte(*pg)) && pgprot_val(prot));
231 set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, prot));
238 * Map in a chunk of physical memory starting at start.
240 static void __init __mapin_ram_chunk(unsigned long offset, unsigned long top)
248 p = memstart_addr + s;
249 for (; s < top; s += PAGE_SIZE) {
250 ktext = ((char *)v >= _stext && (char *)v < etext) ||
251 ((char *)v >= _sinittext && (char *)v < _einittext);
252 map_kernel_page(v, p, ktext ? PAGE_KERNEL_TEXT : PAGE_KERNEL);
253 #ifdef CONFIG_PPC_BOOK3S_32
255 hash_preload(&init_mm, v, false, 0x300);
262 void __init mapin_ram(void)
264 struct memblock_region *reg;
266 for_each_memblock(memory, reg) {
267 phys_addr_t base = reg->base;
268 phys_addr_t top = min(base + reg->size, total_lowmem);
272 base = mmu_mapin_ram(base, top);
273 if (IS_ENABLED(CONFIG_BDI_SWITCH))
274 __mapin_ram_chunk(reg->base, top);
276 __mapin_ram_chunk(base, top);
280 /* Scan the real Linux page tables and return a PTE pointer for
281 * a virtual address in a context.
282 * Returns true (1) if PTE was found, zero otherwise. The pointer to
283 * the PTE pointer is unmodified if PTE is not found.
286 get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
294 pgd = pgd_offset(mm, addr & PAGE_MASK);
296 pud = pud_offset(pgd, addr & PAGE_MASK);
297 if (pud && pud_present(*pud)) {
298 pmd = pmd_offset(pud, addr & PAGE_MASK);
299 if (pmd_present(*pmd)) {
300 pte = pte_offset_map(pmd, addr & PAGE_MASK);
306 /* XXX caller needs to do pte_unmap, yuck */
314 static int __change_page_attr_noflush(struct page *page, pgprot_t prot)
318 unsigned long address;
320 BUG_ON(PageHighMem(page));
321 address = (unsigned long)page_address(page);
323 if (v_block_mapped(address))
325 if (!get_pteptr(&init_mm, address, &kpte, &kpmd))
327 __set_pte_at(&init_mm, address, kpte, mk_pte(page, prot), 0);
334 * Change the page attributes of an page in the linear mapping.
336 * THIS DOES NOTHING WITH BAT MAPPINGS, DEBUG USE ONLY
338 static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
342 struct page *start = page;
344 local_irq_save(flags);
345 for (i = 0; i < numpages; i++, page++) {
346 err = __change_page_attr_noflush(page, prot);
351 local_irq_restore(flags);
352 flush_tlb_kernel_range((unsigned long)page_address(start),
353 (unsigned long)page_address(page));
357 void mark_initmem_nx(void)
359 struct page *page = virt_to_page(_sinittext);
360 unsigned long numpages = PFN_UP((unsigned long)_einittext) -
361 PFN_DOWN((unsigned long)_sinittext);
363 if (v_block_mapped((unsigned long)_stext + 1))
364 mmu_mark_initmem_nx();
366 change_page_attr(page, numpages, PAGE_KERNEL);
369 #ifdef CONFIG_STRICT_KERNEL_RWX
370 void mark_rodata_ro(void)
373 unsigned long numpages;
375 if (v_block_mapped((unsigned long)_sinittext)) {
376 mmu_mark_rodata_ro();
380 page = virt_to_page(_stext);
381 numpages = PFN_UP((unsigned long)_etext) -
382 PFN_DOWN((unsigned long)_stext);
384 change_page_attr(page, numpages, PAGE_KERNEL_ROX);
386 * mark .rodata as read only. Use __init_begin rather than __end_rodata
387 * to cover NOTES and EXCEPTION_TABLE.
389 page = virt_to_page(__start_rodata);
390 numpages = PFN_UP((unsigned long)__init_begin) -
391 PFN_DOWN((unsigned long)__start_rodata);
393 change_page_attr(page, numpages, PAGE_KERNEL_RO);
395 // mark_initmem_nx() should have already run by now
400 #ifdef CONFIG_DEBUG_PAGEALLOC
401 void __kernel_map_pages(struct page *page, int numpages, int enable)
403 if (PageHighMem(page))
406 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
408 #endif /* CONFIG_DEBUG_PAGEALLOC */