xref: /openbmc/linux/arch/powerpc/mm/pgtable_32.c (revision 165f2d28)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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
15  */
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 
27 #include <asm/pgtable.h>
28 #include <asm/pgalloc.h>
29 #include <asm/fixmap.h>
30 #include <asm/setup.h>
31 #include <asm/sections.h>
32 
33 #include <mm/mmu_decl.h>
34 
35 extern char etext[], _stext[], _sinittext[], _einittext[];
36 
37 static void __init *early_alloc_pgtable(unsigned long size)
38 {
39 	void *ptr = memblock_alloc(size, size);
40 
41 	if (!ptr)
42 		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
43 		      __func__, size, size);
44 
45 	return ptr;
46 }
47 
48 static pte_t __init *early_pte_alloc_kernel(pmd_t *pmdp, unsigned long va)
49 {
50 	if (pmd_none(*pmdp)) {
51 		pte_t *ptep = early_alloc_pgtable(PTE_FRAG_SIZE);
52 
53 		pmd_populate_kernel(&init_mm, pmdp, ptep);
54 	}
55 	return pte_offset_kernel(pmdp, va);
56 }
57 
58 
59 int __ref map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot)
60 {
61 	pmd_t *pd;
62 	pte_t *pg;
63 	int err = -ENOMEM;
64 
65 	/* Use upper 10 bits of VA to index the first level map */
66 	pd = pmd_ptr_k(va);
67 	/* Use middle 10 bits of VA to index the second-level map */
68 	if (likely(slab_is_available()))
69 		pg = pte_alloc_kernel(pd, va);
70 	else
71 		pg = early_pte_alloc_kernel(pd, va);
72 	if (pg != 0) {
73 		err = 0;
74 		/* The PTE should never be already set nor present in the
75 		 * hash table
76 		 */
77 		BUG_ON((pte_present(*pg) | pte_hashpte(*pg)) && pgprot_val(prot));
78 		set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, prot));
79 	}
80 	smp_wmb();
81 	return err;
82 }
83 
84 /*
85  * Map in a chunk of physical memory starting at start.
86  */
87 static void __init __mapin_ram_chunk(unsigned long offset, unsigned long top)
88 {
89 	unsigned long v, s;
90 	phys_addr_t p;
91 	int ktext;
92 
93 	s = offset;
94 	v = PAGE_OFFSET + s;
95 	p = memstart_addr + s;
96 	for (; s < top; s += PAGE_SIZE) {
97 		ktext = ((char *)v >= _stext && (char *)v < etext) ||
98 			((char *)v >= _sinittext && (char *)v < _einittext);
99 		map_kernel_page(v, p, ktext ? PAGE_KERNEL_TEXT : PAGE_KERNEL);
100 #ifdef CONFIG_PPC_BOOK3S_32
101 		if (ktext)
102 			hash_preload(&init_mm, v);
103 #endif
104 		v += PAGE_SIZE;
105 		p += PAGE_SIZE;
106 	}
107 }
108 
109 void __init mapin_ram(void)
110 {
111 	struct memblock_region *reg;
112 
113 	for_each_memblock(memory, reg) {
114 		phys_addr_t base = reg->base;
115 		phys_addr_t top = min(base + reg->size, total_lowmem);
116 
117 		if (base >= top)
118 			continue;
119 		base = mmu_mapin_ram(base, top);
120 		__mapin_ram_chunk(base, top);
121 	}
122 }
123 
124 static int __change_page_attr_noflush(struct page *page, pgprot_t prot)
125 {
126 	pte_t *kpte;
127 	unsigned long address;
128 
129 	BUG_ON(PageHighMem(page));
130 	address = (unsigned long)page_address(page);
131 
132 	if (v_block_mapped(address))
133 		return 0;
134 	kpte = virt_to_kpte(address);
135 	if (!kpte)
136 		return -EINVAL;
137 	__set_pte_at(&init_mm, address, kpte, mk_pte(page, prot), 0);
138 
139 	return 0;
140 }
141 
142 /*
143  * Change the page attributes of an page in the linear mapping.
144  *
145  * THIS DOES NOTHING WITH BAT MAPPINGS, DEBUG USE ONLY
146  */
147 static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
148 {
149 	int i, err = 0;
150 	unsigned long flags;
151 	struct page *start = page;
152 
153 	local_irq_save(flags);
154 	for (i = 0; i < numpages; i++, page++) {
155 		err = __change_page_attr_noflush(page, prot);
156 		if (err)
157 			break;
158 	}
159 	wmb();
160 	local_irq_restore(flags);
161 	flush_tlb_kernel_range((unsigned long)page_address(start),
162 			       (unsigned long)page_address(page));
163 	return err;
164 }
165 
166 void mark_initmem_nx(void)
167 {
168 	struct page *page = virt_to_page(_sinittext);
169 	unsigned long numpages = PFN_UP((unsigned long)_einittext) -
170 				 PFN_DOWN((unsigned long)_sinittext);
171 
172 	if (v_block_mapped((unsigned long)_stext + 1))
173 		mmu_mark_initmem_nx();
174 	else
175 		change_page_attr(page, numpages, PAGE_KERNEL);
176 }
177 
178 #ifdef CONFIG_STRICT_KERNEL_RWX
179 void mark_rodata_ro(void)
180 {
181 	struct page *page;
182 	unsigned long numpages;
183 
184 	if (v_block_mapped((unsigned long)_sinittext)) {
185 		mmu_mark_rodata_ro();
186 		ptdump_check_wx();
187 		return;
188 	}
189 
190 	page = virt_to_page(_stext);
191 	numpages = PFN_UP((unsigned long)_etext) -
192 		   PFN_DOWN((unsigned long)_stext);
193 
194 	change_page_attr(page, numpages, PAGE_KERNEL_ROX);
195 	/*
196 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
197 	 * to cover NOTES and EXCEPTION_TABLE.
198 	 */
199 	page = virt_to_page(__start_rodata);
200 	numpages = PFN_UP((unsigned long)__init_begin) -
201 		   PFN_DOWN((unsigned long)__start_rodata);
202 
203 	change_page_attr(page, numpages, PAGE_KERNEL_RO);
204 
205 	// mark_initmem_nx() should have already run by now
206 	ptdump_check_wx();
207 }
208 #endif
209 
210 #ifdef CONFIG_DEBUG_PAGEALLOC
211 void __kernel_map_pages(struct page *page, int numpages, int enable)
212 {
213 	if (PageHighMem(page))
214 		return;
215 
216 	change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
217 }
218 #endif /* CONFIG_DEBUG_PAGEALLOC */
219