• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * This file contains the routines for handling the MMU on those
4  * PowerPC implementations where the MMU substantially follows the
5  * architecture specification.  This includes the 6xx, 7xx, 7xxx,
6  * and 8260 implementations but excludes the 8xx and 4xx.
7  *  -- paulus
8  *
9  *  Derived from arch/ppc/mm/init.c:
10  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
11  *
12  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
13  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
14  *    Copyright (C) 1996 Paul Mackerras
15  *
16  *  Derived from "arch/i386/mm/init.c"
17  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
18  */
19 
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/init.h>
23 #include <linux/highmem.h>
24 #include <linux/memblock.h>
25 
26 #include <asm/prom.h>
27 #include <asm/mmu.h>
28 #include <asm/machdep.h>
29 #include <asm/code-patching.h>
30 #include <asm/sections.h>
31 
32 #include <mm/mmu_decl.h>
33 
34 struct hash_pte *Hash;
35 static unsigned long Hash_size, Hash_mask;
36 unsigned long _SDR1;
37 static unsigned int hash_mb, hash_mb2;
38 
39 struct ppc_bat BATS[8][2];	/* 8 pairs of IBAT, DBAT */
40 
41 struct batrange {		/* stores address ranges mapped by BATs */
42 	unsigned long start;
43 	unsigned long limit;
44 	phys_addr_t phys;
45 } bat_addrs[8];
46 
47 /*
48  * Return PA for this VA if it is mapped by a BAT, or 0
49  */
v_block_mapped(unsigned long va)50 phys_addr_t v_block_mapped(unsigned long va)
51 {
52 	int b;
53 	for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
54 		if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
55 			return bat_addrs[b].phys + (va - bat_addrs[b].start);
56 	return 0;
57 }
58 
59 /*
60  * Return VA for a given PA or 0 if not mapped
61  */
p_block_mapped(phys_addr_t pa)62 unsigned long p_block_mapped(phys_addr_t pa)
63 {
64 	int b;
65 	for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
66 		if (pa >= bat_addrs[b].phys
67 	    	    && pa < (bat_addrs[b].limit-bat_addrs[b].start)
68 		              +bat_addrs[b].phys)
69 			return bat_addrs[b].start+(pa-bat_addrs[b].phys);
70 	return 0;
71 }
72 
find_free_bat(void)73 static int find_free_bat(void)
74 {
75 	int b;
76 
77 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_601)) {
78 		for (b = 0; b < 4; b++) {
79 			struct ppc_bat *bat = BATS[b];
80 
81 			if (!(bat[0].batl & 0x40))
82 				return b;
83 		}
84 	} else {
85 		int n = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
86 
87 		for (b = 0; b < n; b++) {
88 			struct ppc_bat *bat = BATS[b];
89 
90 			if (!(bat[1].batu & 3))
91 				return b;
92 		}
93 	}
94 	return -1;
95 }
96 
97 /*
98  * This function calculates the size of the larger block usable to map the
99  * beginning of an area based on the start address and size of that area:
100  * - max block size is 8M on 601 and 256 on other 6xx.
101  * - base address must be aligned to the block size. So the maximum block size
102  *   is identified by the lowest bit set to 1 in the base address (for instance
103  *   if base is 0x16000000, max size is 0x02000000).
104  * - block size has to be a power of two. This is calculated by finding the
105  *   highest bit set to 1.
106  */
block_size(unsigned long base,unsigned long top)107 static unsigned int block_size(unsigned long base, unsigned long top)
108 {
109 	unsigned int max_size = IS_ENABLED(CONFIG_PPC_BOOK3S_601) ? SZ_8M : SZ_256M;
110 	unsigned int base_shift = (ffs(base) - 1) & 31;
111 	unsigned int block_shift = (fls(top - base) - 1) & 31;
112 
113 	return min3(max_size, 1U << base_shift, 1U << block_shift);
114 }
115 
116 /*
117  * Set up one of the IBAT (block address translation) register pairs.
118  * The parameters are not checked; in particular size must be a power
119  * of 2 between 128k and 256M.
120  * Only for 603+ ...
121  */
setibat(int index,unsigned long virt,phys_addr_t phys,unsigned int size,pgprot_t prot)122 static void setibat(int index, unsigned long virt, phys_addr_t phys,
123 		    unsigned int size, pgprot_t prot)
124 {
125 	unsigned int bl = (size >> 17) - 1;
126 	int wimgxpp;
127 	struct ppc_bat *bat = BATS[index];
128 	unsigned long flags = pgprot_val(prot);
129 
130 	if (!cpu_has_feature(CPU_FTR_NEED_COHERENT))
131 		flags &= ~_PAGE_COHERENT;
132 
133 	wimgxpp = (flags & _PAGE_COHERENT) | (_PAGE_EXEC ? BPP_RX : BPP_XX);
134 	bat[0].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
135 	bat[0].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
136 	if (flags & _PAGE_USER)
137 		bat[0].batu |= 1;	/* Vp = 1 */
138 }
139 
clearibat(int index)140 static void clearibat(int index)
141 {
142 	struct ppc_bat *bat = BATS[index];
143 
144 	bat[0].batu = 0;
145 	bat[0].batl = 0;
146 }
147 
__mmu_mapin_ram(unsigned long base,unsigned long top)148 static unsigned long __init __mmu_mapin_ram(unsigned long base, unsigned long top)
149 {
150 	int idx;
151 
152 	while ((idx = find_free_bat()) != -1 && base != top) {
153 		unsigned int size = block_size(base, top);
154 
155 		if (size < 128 << 10)
156 			break;
157 		setbat(idx, PAGE_OFFSET + base, base, size, PAGE_KERNEL_X);
158 		base += size;
159 	}
160 
161 	return base;
162 }
163 
mmu_mapin_ram(unsigned long base,unsigned long top)164 unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
165 {
166 	unsigned long done;
167 	unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
168 
169 	if (__map_without_bats) {
170 		pr_debug("RAM mapped without BATs\n");
171 		return base;
172 	}
173 
174 	if (!strict_kernel_rwx_enabled() || base >= border || top <= border)
175 		return __mmu_mapin_ram(base, top);
176 
177 	done = __mmu_mapin_ram(base, border);
178 	if (done != border)
179 		return done;
180 
181 	return __mmu_mapin_ram(border, top);
182 }
183 
mmu_mark_initmem_nx(void)184 void mmu_mark_initmem_nx(void)
185 {
186 	int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
187 	int i;
188 	unsigned long base = (unsigned long)_stext - PAGE_OFFSET;
189 	unsigned long top = (unsigned long)_etext - PAGE_OFFSET;
190 	unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
191 	unsigned long size;
192 
193 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_601))
194 		return;
195 
196 	for (i = 0; i < nb - 1 && base < top && top - base > (128 << 10);) {
197 		size = block_size(base, top);
198 		setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
199 		base += size;
200 	}
201 	if (base < top) {
202 		size = block_size(base, top);
203 		size = max(size, 128UL << 10);
204 		if ((top - base) > size) {
205 			size <<= 1;
206 			if (strict_kernel_rwx_enabled() && base + size > border)
207 				pr_warn("Some RW data is getting mapped X. "
208 					"Adjust CONFIG_DATA_SHIFT to avoid that.\n");
209 		}
210 		setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
211 		base += size;
212 	}
213 	for (; i < nb; i++)
214 		clearibat(i);
215 
216 	update_bats();
217 
218 	for (i = TASK_SIZE >> 28; i < 16; i++) {
219 		/* Do not set NX on VM space for modules */
220 		if (IS_ENABLED(CONFIG_MODULES) &&
221 		    (VMALLOC_START & 0xf0000000) == i << 28)
222 			break;
223 		mtsrin(mfsrin(i << 28) | 0x10000000, i << 28);
224 	}
225 }
226 
mmu_mark_rodata_ro(void)227 void mmu_mark_rodata_ro(void)
228 {
229 	int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
230 	int i;
231 
232 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_601))
233 		return;
234 
235 	for (i = 0; i < nb; i++) {
236 		struct ppc_bat *bat = BATS[i];
237 
238 		if (bat_addrs[i].start < (unsigned long)__init_begin)
239 			bat[1].batl = (bat[1].batl & ~BPP_RW) | BPP_RX;
240 	}
241 
242 	update_bats();
243 }
244 
245 /*
246  * Set up one of the I/D BAT (block address translation) register pairs.
247  * The parameters are not checked; in particular size must be a power
248  * of 2 between 128k and 256M.
249  * On 603+, only set IBAT when _PAGE_EXEC is set
250  */
setbat(int index,unsigned long virt,phys_addr_t phys,unsigned int size,pgprot_t prot)251 void __init setbat(int index, unsigned long virt, phys_addr_t phys,
252 		   unsigned int size, pgprot_t prot)
253 {
254 	unsigned int bl;
255 	int wimgxpp;
256 	struct ppc_bat *bat = BATS[index];
257 	unsigned long flags = pgprot_val(prot);
258 
259 	if ((flags & _PAGE_NO_CACHE) ||
260 	    (cpu_has_feature(CPU_FTR_NEED_COHERENT) == 0))
261 		flags &= ~_PAGE_COHERENT;
262 
263 	bl = (size >> 17) - 1;
264 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S_601)) {
265 		/* 603, 604, etc. */
266 		/* Do DBAT first */
267 		wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
268 				   | _PAGE_COHERENT | _PAGE_GUARDED);
269 		wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
270 		bat[1].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
271 		bat[1].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
272 		if (flags & _PAGE_USER)
273 			bat[1].batu |= 1; 	/* Vp = 1 */
274 		if (flags & _PAGE_GUARDED) {
275 			/* G bit must be zero in IBATs */
276 			flags &= ~_PAGE_EXEC;
277 		}
278 		if (flags & _PAGE_EXEC)
279 			bat[0] = bat[1];
280 		else
281 			bat[0].batu = bat[0].batl = 0;
282 	} else {
283 		/* 601 cpu */
284 		if (bl > BL_8M)
285 			bl = BL_8M;
286 		wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
287 				   | _PAGE_COHERENT);
288 		wimgxpp |= (flags & _PAGE_RW)?
289 			((flags & _PAGE_USER)? PP_RWRW: PP_RWXX): PP_RXRX;
290 		bat->batu = virt | wimgxpp | 4;	/* Ks=0, Ku=1 */
291 		bat->batl = phys | bl | 0x40;	/* V=1 */
292 	}
293 
294 	bat_addrs[index].start = virt;
295 	bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
296 	bat_addrs[index].phys = phys;
297 }
298 
299 /*
300  * Preload a translation in the hash table
301  */
hash_preload(struct mm_struct * mm,unsigned long ea)302 void hash_preload(struct mm_struct *mm, unsigned long ea)
303 {
304 	pmd_t *pmd;
305 
306 	if (!Hash)
307 		return;
308 	pmd = pmd_offset(pud_offset(pgd_offset(mm, ea), ea), ea);
309 	if (!pmd_none(*pmd))
310 		add_hash_page(mm->context.id, ea, pmd_val(*pmd));
311 }
312 
313 /*
314  * This is called at the end of handling a user page fault, when the
315  * fault has been handled by updating a PTE in the linux page tables.
316  * We use it to preload an HPTE into the hash table corresponding to
317  * the updated linux PTE.
318  *
319  * This must always be called with the pte lock held.
320  */
update_mmu_cache(struct vm_area_struct * vma,unsigned long address,pte_t * ptep)321 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
322 		      pte_t *ptep)
323 {
324 	if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
325 		return;
326 	/*
327 	 * We don't need to worry about _PAGE_PRESENT here because we are
328 	 * called with either mm->page_table_lock held or ptl lock held
329 	 */
330 
331 	/* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
332 	if (!pte_young(*ptep) || address >= TASK_SIZE)
333 		return;
334 
335 	/* We have to test for regs NULL since init will get here first thing at boot */
336 	if (!current->thread.regs)
337 		return;
338 
339 	/* We also avoid filling the hash if not coming from a fault */
340 	if (TRAP(current->thread.regs) != 0x300 && TRAP(current->thread.regs) != 0x400)
341 		return;
342 
343 	hash_preload(vma->vm_mm, address);
344 }
345 
346 /*
347  * Initialize the hash table and patch the instructions in hashtable.S.
348  */
MMU_init_hw(void)349 void __init MMU_init_hw(void)
350 {
351 	unsigned int n_hpteg, lg_n_hpteg;
352 
353 	if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
354 		return;
355 
356 	if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
357 
358 #define LG_HPTEG_SIZE	6		/* 64 bytes per HPTEG */
359 #define SDR1_LOW_BITS	((n_hpteg - 1) >> 10)
360 #define MIN_N_HPTEG	1024		/* min 64kB hash table */
361 
362 	/*
363 	 * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
364 	 * This is less than the recommended amount, but then
365 	 * Linux ain't AIX.
366 	 */
367 	n_hpteg = total_memory / (PAGE_SIZE * 8);
368 	if (n_hpteg < MIN_N_HPTEG)
369 		n_hpteg = MIN_N_HPTEG;
370 	lg_n_hpteg = __ilog2(n_hpteg);
371 	if (n_hpteg & (n_hpteg - 1)) {
372 		++lg_n_hpteg;		/* round up if not power of 2 */
373 		n_hpteg = 1 << lg_n_hpteg;
374 	}
375 	Hash_size = n_hpteg << LG_HPTEG_SIZE;
376 
377 	/*
378 	 * Find some memory for the hash table.
379 	 */
380 	if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
381 	Hash = memblock_alloc(Hash_size, Hash_size);
382 	if (!Hash)
383 		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
384 		      __func__, Hash_size, Hash_size);
385 	_SDR1 = __pa(Hash) | SDR1_LOW_BITS;
386 
387 	pr_info("Total memory = %lldMB; using %ldkB for hash table\n",
388 		(unsigned long long)(total_memory >> 20), Hash_size >> 10);
389 
390 
391 	Hash_mask = n_hpteg - 1;
392 	hash_mb2 = hash_mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
393 	if (lg_n_hpteg > 16)
394 		hash_mb2 = 16 - LG_HPTEG_SIZE;
395 
396 	/*
397 	 * When KASAN is selected, there is already an early temporary hash
398 	 * table and the switch to the final hash table is done later.
399 	 */
400 	if (IS_ENABLED(CONFIG_KASAN))
401 		return;
402 
403 	MMU_init_hw_patch();
404 }
405 
MMU_init_hw_patch(void)406 void __init MMU_init_hw_patch(void)
407 {
408 	unsigned int hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
409 
410 	if (ppc_md.progress)
411 		ppc_md.progress("hash:patch", 0x345);
412 	if (ppc_md.progress)
413 		ppc_md.progress("hash:done", 0x205);
414 
415 	/* WARNING: Make sure nothing can trigger a KASAN check past this point */
416 
417 	/*
418 	 * Patch up the instructions in hashtable.S:create_hpte
419 	 */
420 	modify_instruction_site(&patch__hash_page_A0, 0xffff,
421 				((unsigned int)Hash - PAGE_OFFSET) >> 16);
422 	modify_instruction_site(&patch__hash_page_A1, 0x7c0, hash_mb << 6);
423 	modify_instruction_site(&patch__hash_page_A2, 0x7c0, hash_mb2 << 6);
424 	modify_instruction_site(&patch__hash_page_B, 0xffff, hmask);
425 	modify_instruction_site(&patch__hash_page_C, 0xffff, hmask);
426 
427 	/*
428 	 * Patch up the instructions in hashtable.S:flush_hash_page
429 	 */
430 	modify_instruction_site(&patch__flush_hash_A0, 0xffff,
431 				((unsigned int)Hash - PAGE_OFFSET) >> 16);
432 	modify_instruction_site(&patch__flush_hash_A1, 0x7c0, hash_mb << 6);
433 	modify_instruction_site(&patch__flush_hash_A2, 0x7c0, hash_mb2 << 6);
434 	modify_instruction_site(&patch__flush_hash_B, 0xffff, hmask);
435 }
436 
setup_initial_memory_limit(phys_addr_t first_memblock_base,phys_addr_t first_memblock_size)437 void setup_initial_memory_limit(phys_addr_t first_memblock_base,
438 				phys_addr_t first_memblock_size)
439 {
440 	/* We don't currently support the first MEMBLOCK not mapping 0
441 	 * physical on those processors
442 	 */
443 	BUG_ON(first_memblock_base != 0);
444 
445 	/* 601 can only access 16MB at the moment */
446 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_601))
447 		memblock_set_current_limit(min_t(u64, first_memblock_size, 0x01000000));
448 	else /* Anything else has 256M mapped */
449 		memblock_set_current_limit(min_t(u64, first_memblock_size, 0x10000000));
450 }
451 
print_system_hash_info(void)452 void __init print_system_hash_info(void)
453 {
454 	pr_info("Hash_size         = 0x%lx\n", Hash_size);
455 	if (Hash_mask)
456 		pr_info("Hash_mask         = 0x%lx\n", Hash_mask);
457 }
458 
459 #ifdef CONFIG_PPC_KUEP
setup_kuep(bool disabled)460 void __init setup_kuep(bool disabled)
461 {
462 	pr_info("Activating Kernel Userspace Execution Prevention\n");
463 
464 	if (disabled)
465 		pr_warn("KUEP cannot be disabled yet on 6xx when compiled in\n");
466 }
467 #endif
468 
469 #ifdef CONFIG_PPC_KUAP
setup_kuap(bool disabled)470 void __init setup_kuap(bool disabled)
471 {
472 	pr_info("Activating Kernel Userspace Access Protection\n");
473 
474 	if (disabled)
475 		pr_warn("KUAP cannot be disabled yet on 6xx when compiled in\n");
476 }
477 #endif
478