blob: 079159e97bca8df97e831d63e68c26b03f52ae53 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * This file contains the routines setting up the linux page tables.
4 * -- paulus
5 *
6 * Derived from arch/ppc/mm/init.c:
7 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
8 *
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
12 *
13 * Derived from "arch/i386/mm/init.c"
14 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/mm.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>
26
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027#include <asm/pgalloc.h>
28#include <asm/fixmap.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029#include <asm/setup.h>
30#include <asm/sections.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020031#include <asm/early_ioremap.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032
David Brazdil0f672f62019-12-10 10:32:29 +000033#include <mm/mmu_decl.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034
35extern char etext[], _stext[], _sinittext[], _einittext[];
36
Olivier Deprez157378f2022-04-04 15:47:50 +020037static u8 early_fixmap_pagetable[FIXMAP_PTE_SIZE] __page_aligned_data;
38
39notrace void __init early_ioremap_init(void)
40{
41 unsigned long addr = ALIGN_DOWN(FIXADDR_START, PGDIR_SIZE);
42 pte_t *ptep = (pte_t *)early_fixmap_pagetable;
43 pmd_t *pmdp = pmd_off_k(addr);
44
45 for (; (s32)(FIXADDR_TOP - addr) > 0;
46 addr += PGDIR_SIZE, ptep += PTRS_PER_PTE, pmdp++)
47 pmd_populate_kernel(&init_mm, pmdp, ptep);
48
49 early_ioremap_setup();
50}
51
David Brazdil0f672f62019-12-10 10:32:29 +000052static void __init *early_alloc_pgtable(unsigned long size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053{
David Brazdil0f672f62019-12-10 10:32:29 +000054 void *ptr = memblock_alloc(size, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055
David Brazdil0f672f62019-12-10 10:32:29 +000056 if (!ptr)
57 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
58 __func__, size, size);
59
60 return ptr;
61}
62
Olivier Deprez157378f2022-04-04 15:47:50 +020063pte_t __init *early_pte_alloc_kernel(pmd_t *pmdp, unsigned long va)
David Brazdil0f672f62019-12-10 10:32:29 +000064{
65 if (pmd_none(*pmdp)) {
66 pte_t *ptep = early_alloc_pgtable(PTE_FRAG_SIZE);
67
68 pmd_populate_kernel(&init_mm, pmdp, ptep);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069 }
David Brazdil0f672f62019-12-10 10:32:29 +000070 return pte_offset_kernel(pmdp, va);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071}
72
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073
David Brazdil0f672f62019-12-10 10:32:29 +000074int __ref map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075{
76 pmd_t *pd;
77 pte_t *pg;
78 int err = -ENOMEM;
79
80 /* Use upper 10 bits of VA to index the first level map */
Olivier Deprez157378f2022-04-04 15:47:50 +020081 pd = pmd_off_k(va);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082 /* Use middle 10 bits of VA to index the second-level map */
David Brazdil0f672f62019-12-10 10:32:29 +000083 if (likely(slab_is_available()))
84 pg = pte_alloc_kernel(pd, va);
85 else
86 pg = early_pte_alloc_kernel(pd, va);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000087 if (pg != 0) {
88 err = 0;
89 /* The PTE should never be already set nor present in the
90 * hash table
91 */
David Brazdil0f672f62019-12-10 10:32:29 +000092 BUG_ON((pte_present(*pg) | pte_hashpte(*pg)) && pgprot_val(prot));
93 set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, prot));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094 }
95 smp_wmb();
96 return err;
97}
98
99/*
100 * Map in a chunk of physical memory starting at start.
101 */
102static void __init __mapin_ram_chunk(unsigned long offset, unsigned long top)
103{
David Brazdil0f672f62019-12-10 10:32:29 +0000104 unsigned long v, s;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105 phys_addr_t p;
106 int ktext;
107
108 s = offset;
109 v = PAGE_OFFSET + s;
110 p = memstart_addr + s;
111 for (; s < top; s += PAGE_SIZE) {
112 ktext = ((char *)v >= _stext && (char *)v < etext) ||
113 ((char *)v >= _sinittext && (char *)v < _einittext);
David Brazdil0f672f62019-12-10 10:32:29 +0000114 map_kernel_page(v, p, ktext ? PAGE_KERNEL_TEXT : PAGE_KERNEL);
115#ifdef CONFIG_PPC_BOOK3S_32
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116 if (ktext)
David Brazdil0f672f62019-12-10 10:32:29 +0000117 hash_preload(&init_mm, v);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118#endif
119 v += PAGE_SIZE;
120 p += PAGE_SIZE;
121 }
122}
123
124void __init mapin_ram(void)
125{
Olivier Deprez157378f2022-04-04 15:47:50 +0200126 phys_addr_t base, end;
127 u64 i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128
Olivier Deprez157378f2022-04-04 15:47:50 +0200129 for_each_mem_range(i, &base, &end) {
130 phys_addr_t top = min(end, total_lowmem);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131
David Brazdil0f672f62019-12-10 10:32:29 +0000132 if (base >= top)
133 continue;
134 base = mmu_mapin_ram(base, top);
Olivier Deprez157378f2022-04-04 15:47:50 +0200135 __mapin_ram_chunk(base, top);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137}
138
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000139static int __change_page_attr_noflush(struct page *page, pgprot_t prot)
140{
141 pte_t *kpte;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000142 unsigned long address;
143
144 BUG_ON(PageHighMem(page));
145 address = (unsigned long)page_address(page);
146
147 if (v_block_mapped(address))
148 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200149 kpte = virt_to_kpte(address);
150 if (!kpte)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000151 return -EINVAL;
152 __set_pte_at(&init_mm, address, kpte, mk_pte(page, prot), 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153
154 return 0;
155}
156
157/*
158 * Change the page attributes of an page in the linear mapping.
159 *
160 * THIS DOES NOTHING WITH BAT MAPPINGS, DEBUG USE ONLY
161 */
162static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
163{
164 int i, err = 0;
165 unsigned long flags;
166 struct page *start = page;
167
168 local_irq_save(flags);
169 for (i = 0; i < numpages; i++, page++) {
170 err = __change_page_attr_noflush(page, prot);
171 if (err)
172 break;
173 }
174 wmb();
175 local_irq_restore(flags);
176 flush_tlb_kernel_range((unsigned long)page_address(start),
177 (unsigned long)page_address(page));
178 return err;
179}
180
181void mark_initmem_nx(void)
182{
183 struct page *page = virt_to_page(_sinittext);
184 unsigned long numpages = PFN_UP((unsigned long)_einittext) -
185 PFN_DOWN((unsigned long)_sinittext);
186
Olivier Deprez0e641232021-09-23 10:07:05 +0200187 if (v_block_mapped((unsigned long)_sinittext))
David Brazdil0f672f62019-12-10 10:32:29 +0000188 mmu_mark_initmem_nx();
189 else
190 change_page_attr(page, numpages, PAGE_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000191}
192
193#ifdef CONFIG_STRICT_KERNEL_RWX
194void mark_rodata_ro(void)
195{
196 struct page *page;
197 unsigned long numpages;
198
Olivier Deprez0e641232021-09-23 10:07:05 +0200199 if (v_block_mapped((unsigned long)_stext + 1)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000200 mmu_mark_rodata_ro();
Olivier Deprez0e641232021-09-23 10:07:05 +0200201 ptdump_check_wx();
David Brazdil0f672f62019-12-10 10:32:29 +0000202 return;
203 }
204
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000205 page = virt_to_page(_stext);
206 numpages = PFN_UP((unsigned long)_etext) -
207 PFN_DOWN((unsigned long)_stext);
208
209 change_page_attr(page, numpages, PAGE_KERNEL_ROX);
210 /*
211 * mark .rodata as read only. Use __init_begin rather than __end_rodata
212 * to cover NOTES and EXCEPTION_TABLE.
213 */
214 page = virt_to_page(__start_rodata);
215 numpages = PFN_UP((unsigned long)__init_begin) -
216 PFN_DOWN((unsigned long)__start_rodata);
217
218 change_page_attr(page, numpages, PAGE_KERNEL_RO);
David Brazdil0f672f62019-12-10 10:32:29 +0000219
220 // mark_initmem_nx() should have already run by now
221 ptdump_check_wx();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222}
223#endif
224
225#ifdef CONFIG_DEBUG_PAGEALLOC
226void __kernel_map_pages(struct page *page, int numpages, int enable)
227{
228 if (PageHighMem(page))
229 return;
230
231 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
232}
233#endif /* CONFIG_DEBUG_PAGEALLOC */