Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 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, |
Michael Ellerman | 0f36910 | 2014-07-10 12:29:24 +1000 | [diff] [blame] | 6 | * and 8260 implementations but excludes the 8xx and 4xx. |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 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 |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 15 | * |
| 16 | * Derived from "arch/i386/mm/init.c" |
| 17 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 18 | */ |
| 19 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 20 | #include <linux/kernel.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/highmem.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 24 | #include <linux/memblock.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 25 | |
| 26 | #include <asm/prom.h> |
| 27 | #include <asm/mmu.h> |
| 28 | #include <asm/machdep.h> |
Christophe Leroy | 9efc74f | 2018-11-09 17:33:24 +0000 | [diff] [blame] | 29 | #include <asm/code-patching.h> |
Christophe Leroy | 63b2bc6 | 2019-02-21 19:08:49 +0000 | [diff] [blame] | 30 | #include <asm/sections.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 31 | |
Christophe Leroy | 9d9f2cc | 2019-03-29 09:59:59 +0000 | [diff] [blame] | 32 | #include <mm/mmu_decl.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 33 | |
Christophe Leroy | 57e0491 | 2019-04-26 16:36:36 +0000 | [diff] [blame] | 34 | struct hash_pte *Hash; |
Christophe Leroy | e4dccf9 | 2019-04-26 16:36:39 +0000 | [diff] [blame] | 35 | static unsigned long Hash_size, Hash_mask; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 36 | unsigned long _SDR1; |
Christophe Leroy | 72f208c | 2019-04-26 16:23:35 +0000 | [diff] [blame] | 37 | static unsigned int hash_mb, hash_mb2; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 38 | |
Becky Bruce | 316a405 | 2008-06-14 09:41:43 +1000 | [diff] [blame] | 39 | struct ppc_bat BATS[8][2]; /* 8 pairs of IBAT, DBAT */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 40 | |
| 41 | struct batrange { /* stores address ranges mapped by BATs */ |
| 42 | unsigned long start; |
| 43 | unsigned long limit; |
Becky Bruce | 7c5c432 | 2008-06-14 09:41:42 +1000 | [diff] [blame] | 44 | phys_addr_t phys; |
Jon Loeliger | ee0339f | 2006-06-17 17:52:44 -0500 | [diff] [blame] | 45 | } bat_addrs[8]; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Return PA for this VA if it is mapped by a BAT, or 0 |
| 49 | */ |
Christophe Leroy | 3084cdb | 2016-02-09 17:07:58 +0100 | [diff] [blame] | 50 | phys_addr_t v_block_mapped(unsigned long va) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 51 | { |
| 52 | int b; |
Christophe Leroy | e93ba1b | 2018-11-16 17:27:42 +0000 | [diff] [blame] | 53 | for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 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 | */ |
Christophe Leroy | 3084cdb | 2016-02-09 17:07:58 +0100 | [diff] [blame] | 62 | unsigned long p_block_mapped(phys_addr_t pa) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 63 | { |
| 64 | int b; |
Christophe Leroy | e93ba1b | 2018-11-16 17:27:42 +0000 | [diff] [blame] | 65 | for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 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 | |
Christophe Leroy | e4d6654 | 2019-02-21 19:08:39 +0000 | [diff] [blame] | 73 | static int find_free_bat(void) |
| 74 | { |
| 75 | int b; |
| 76 | |
| 77 | if (cpu_has_feature(CPU_FTR_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 | |
Christophe Leroy | 12f3635 | 2019-04-30 16:11:59 +0000 | [diff] [blame] | 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 | */ |
Christophe Leroy | e4d6654 | 2019-02-21 19:08:39 +0000 | [diff] [blame] | 107 | static unsigned int block_size(unsigned long base, unsigned long top) |
| 108 | { |
| 109 | unsigned int max_size = (cpu_has_feature(CPU_FTR_601) ? 8 : 256) << 20; |
Christophe Leroy | 12f3635 | 2019-04-30 16:11:59 +0000 | [diff] [blame] | 110 | unsigned int base_shift = (ffs(base) - 1) & 31; |
Christophe Leroy | e4d6654 | 2019-02-21 19:08:39 +0000 | [diff] [blame] | 111 | unsigned int block_shift = (fls(top - base) - 1) & 31; |
| 112 | |
| 113 | return min3(max_size, 1U << base_shift, 1U << block_shift); |
| 114 | } |
| 115 | |
Christophe Leroy | 5e04ae8 | 2019-02-21 19:08:48 +0000 | [diff] [blame] | 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 | */ |
| 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 | |
| 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 | |
Christophe Leroy | 63b2bc6 | 2019-02-21 19:08:49 +0000 | [diff] [blame] | 148 | static unsigned long __init __mmu_mapin_ram(unsigned long base, unsigned long top) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 149 | { |
Christophe Leroy | e4d6654 | 2019-02-21 19:08:39 +0000 | [diff] [blame] | 150 | int idx; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 151 | |
Christophe Leroy | e4d6654 | 2019-02-21 19:08:39 +0000 | [diff] [blame] | 152 | while ((idx = find_free_bat()) != -1 && base != top) { |
| 153 | unsigned int size = block_size(base, top); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 154 | |
Christophe Leroy | e4d6654 | 2019-02-21 19:08:39 +0000 | [diff] [blame] | 155 | if (size < 128 << 10) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 156 | break; |
Christophe Leroy | e4d6654 | 2019-02-21 19:08:39 +0000 | [diff] [blame] | 157 | setbat(idx, PAGE_OFFSET + base, base, size, PAGE_KERNEL_X); |
| 158 | base += size; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 159 | } |
| 160 | |
Christophe Leroy | e4d6654 | 2019-02-21 19:08:39 +0000 | [diff] [blame] | 161 | return base; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 162 | } |
| 163 | |
Christophe Leroy | 63b2bc6 | 2019-02-21 19:08:49 +0000 | [diff] [blame] | 164 | unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top) |
| 165 | { |
Christophe Leroy | 12f3635 | 2019-04-30 16:11:59 +0000 | [diff] [blame] | 166 | unsigned long done; |
Christophe Leroy | 63b2bc6 | 2019-02-21 19:08:49 +0000 | [diff] [blame] | 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); |
Christophe Leroy | 12f3635 | 2019-04-30 16:11:59 +0000 | [diff] [blame] | 178 | if (done != border) |
Christophe Leroy | 63b2bc6 | 2019-02-21 19:08:49 +0000 | [diff] [blame] | 179 | return done; |
| 180 | |
Christophe Leroy | 12f3635 | 2019-04-30 16:11:59 +0000 | [diff] [blame] | 181 | return __mmu_mapin_ram(border, top); |
Christophe Leroy | 63b2bc6 | 2019-02-21 19:08:49 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 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 size; |
| 191 | |
| 192 | if (cpu_has_feature(CPU_FTR_601)) |
| 193 | return; |
| 194 | |
| 195 | for (i = 0; i < nb - 1 && base < top && top - base > (128 << 10);) { |
| 196 | size = block_size(base, top); |
| 197 | setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT); |
| 198 | base += size; |
| 199 | } |
| 200 | if (base < top) { |
| 201 | size = block_size(base, top); |
| 202 | size = max(size, 128UL << 10); |
| 203 | if ((top - base) > size) { |
| 204 | if (strict_kernel_rwx_enabled()) |
| 205 | pr_warn("Kernel _etext not properly aligned\n"); |
| 206 | size <<= 1; |
| 207 | } |
| 208 | setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT); |
| 209 | base += size; |
| 210 | } |
| 211 | for (; i < nb; i++) |
| 212 | clearibat(i); |
| 213 | |
| 214 | update_bats(); |
| 215 | |
| 216 | for (i = TASK_SIZE >> 28; i < 16; i++) { |
| 217 | /* Do not set NX on VM space for modules */ |
| 218 | if (IS_ENABLED(CONFIG_MODULES) && |
| 219 | (VMALLOC_START & 0xf0000000) == i << 28) |
| 220 | break; |
| 221 | mtsrin(mfsrin(i << 28) | 0x10000000, i << 28); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void mmu_mark_rodata_ro(void) |
| 226 | { |
| 227 | int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4; |
| 228 | int i; |
| 229 | |
| 230 | if (cpu_has_feature(CPU_FTR_601)) |
| 231 | return; |
| 232 | |
| 233 | for (i = 0; i < nb; i++) { |
| 234 | struct ppc_bat *bat = BATS[i]; |
| 235 | |
| 236 | if (bat_addrs[i].start < (unsigned long)__init_begin) |
| 237 | bat[1].batl = (bat[1].batl & ~BPP_RW) | BPP_RX; |
| 238 | } |
| 239 | |
| 240 | update_bats(); |
| 241 | } |
| 242 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 243 | /* |
| 244 | * Set up one of the I/D BAT (block address translation) register pairs. |
| 245 | * The parameters are not checked; in particular size must be a power |
| 246 | * of 2 between 128k and 256M. |
Christophe Leroy | df25f86 | 2019-02-21 19:08:43 +0000 | [diff] [blame] | 247 | * On 603+, only set IBAT when _PAGE_EXEC is set |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 248 | */ |
Becky Bruce | 7c5c432 | 2008-06-14 09:41:42 +1000 | [diff] [blame] | 249 | void __init setbat(int index, unsigned long virt, phys_addr_t phys, |
Michael Ellerman | 5dd4e4f | 2015-03-25 20:11:55 +1100 | [diff] [blame] | 250 | unsigned int size, pgprot_t prot) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 251 | { |
| 252 | unsigned int bl; |
| 253 | int wimgxpp; |
Becky Bruce | 316a405 | 2008-06-14 09:41:43 +1000 | [diff] [blame] | 254 | struct ppc_bat *bat = BATS[index]; |
Michael Ellerman | 5dd4e4f | 2015-03-25 20:11:55 +1100 | [diff] [blame] | 255 | unsigned long flags = pgprot_val(prot); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 256 | |
Gerhard Pircher | 4c456a6 | 2009-01-23 06:51:28 +0000 | [diff] [blame] | 257 | if ((flags & _PAGE_NO_CACHE) || |
| 258 | (cpu_has_feature(CPU_FTR_NEED_COHERENT) == 0)) |
| 259 | flags &= ~_PAGE_COHERENT; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 260 | |
| 261 | bl = (size >> 17) - 1; |
| 262 | if (PVR_VER(mfspr(SPRN_PVR)) != 1) { |
| 263 | /* 603, 604, etc. */ |
| 264 | /* Do DBAT first */ |
| 265 | wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
| 266 | | _PAGE_COHERENT | _PAGE_GUARDED); |
| 267 | wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX; |
Becky Bruce | 316a405 | 2008-06-14 09:41:43 +1000 | [diff] [blame] | 268 | bat[1].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */ |
| 269 | bat[1].batl = BAT_PHYS_ADDR(phys) | wimgxpp; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 270 | if (flags & _PAGE_USER) |
Becky Bruce | 316a405 | 2008-06-14 09:41:43 +1000 | [diff] [blame] | 271 | bat[1].batu |= 1; /* Vp = 1 */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 272 | if (flags & _PAGE_GUARDED) { |
| 273 | /* G bit must be zero in IBATs */ |
Christophe Leroy | df25f86 | 2019-02-21 19:08:43 +0000 | [diff] [blame] | 274 | flags &= ~_PAGE_EXEC; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 275 | } |
Christophe Leroy | df25f86 | 2019-02-21 19:08:43 +0000 | [diff] [blame] | 276 | if (flags & _PAGE_EXEC) |
| 277 | bat[0] = bat[1]; |
| 278 | else |
| 279 | bat[0].batu = bat[0].batl = 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 280 | } else { |
| 281 | /* 601 cpu */ |
| 282 | if (bl > BL_8M) |
| 283 | bl = BL_8M; |
| 284 | wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
| 285 | | _PAGE_COHERENT); |
| 286 | wimgxpp |= (flags & _PAGE_RW)? |
| 287 | ((flags & _PAGE_USER)? PP_RWRW: PP_RWXX): PP_RXRX; |
Becky Bruce | 316a405 | 2008-06-14 09:41:43 +1000 | [diff] [blame] | 288 | bat->batu = virt | wimgxpp | 4; /* Ks=0, Ku=1 */ |
| 289 | bat->batl = phys | bl | 0x40; /* V=1 */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | bat_addrs[index].start = virt; |
| 293 | bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1; |
| 294 | bat_addrs[index].phys = phys; |
| 295 | } |
| 296 | |
| 297 | /* |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 298 | * Preload a translation in the hash table |
| 299 | */ |
| 300 | void hash_preload(struct mm_struct *mm, unsigned long ea, |
Christophe Leroy | 34eb138 | 2018-10-09 13:51:54 +0000 | [diff] [blame] | 301 | bool is_exec, unsigned long trap) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 302 | { |
| 303 | pmd_t *pmd; |
| 304 | |
Mathieu Malaterre | d873152 | 2018-04-13 20:41:43 +0200 | [diff] [blame] | 305 | if (!Hash) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 306 | return; |
David Gibson | f1a1eb2 | 2007-05-09 15:20:37 +1000 | [diff] [blame] | 307 | pmd = pmd_offset(pud_offset(pgd_offset(mm, ea), ea), ea); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 308 | if (!pmd_none(*pmd)) |
Paul Mackerras | 6218a76 | 2006-06-11 14:15:17 +1000 | [diff] [blame] | 309 | add_hash_page(mm->context.id, ea, pmd_val(*pmd)); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
Christophe Leroy | e5a1edb | 2019-08-16 05:41:42 +0000 | [diff] [blame^] | 313 | * This is called at the end of handling a user page fault, when the |
| 314 | * fault has been handled by updating a PTE in the linux page tables. |
| 315 | * We use it to preload an HPTE into the hash table corresponding to |
| 316 | * the updated linux PTE. |
| 317 | * |
| 318 | * This must always be called with the pte lock held. |
| 319 | */ |
| 320 | void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, |
| 321 | pte_t *ptep) |
| 322 | { |
| 323 | /* |
| 324 | * We don't need to worry about _PAGE_PRESENT here because we are |
| 325 | * called with either mm->page_table_lock held or ptl lock held |
| 326 | */ |
| 327 | unsigned long trap; |
| 328 | bool is_exec; |
| 329 | |
| 330 | /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */ |
| 331 | if (!pte_young(*ptep) || address >= TASK_SIZE) |
| 332 | return; |
| 333 | |
| 334 | /* |
| 335 | * We try to figure out if we are coming from an instruction |
| 336 | * access fault and pass that down to __hash_page so we avoid |
| 337 | * double-faulting on execution of fresh text. We have to test |
| 338 | * for regs NULL since init will get here first thing at boot. |
| 339 | * |
| 340 | * We also avoid filling the hash if not coming from a fault. |
| 341 | */ |
| 342 | |
| 343 | trap = current->thread.regs ? TRAP(current->thread.regs) : 0UL; |
| 344 | switch (trap) { |
| 345 | case 0x300: |
| 346 | is_exec = false; |
| 347 | break; |
| 348 | case 0x400: |
| 349 | is_exec = true; |
| 350 | break; |
| 351 | default: |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | hash_preload(vma->vm_mm, address, is_exec, trap); |
| 356 | } |
| 357 | |
| 358 | /* |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 359 | * Initialize the hash table and patch the instructions in hashtable.S. |
| 360 | */ |
| 361 | void __init MMU_init_hw(void) |
| 362 | { |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 363 | unsigned int n_hpteg, lg_n_hpteg; |
| 364 | |
Christophe Leroy | 4a3a224 | 2018-11-09 17:33:22 +0000 | [diff] [blame] | 365 | if (!mmu_has_feature(MMU_FTR_HPTE_TABLE)) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 366 | return; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 367 | |
| 368 | if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105); |
| 369 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 370 | #define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */ |
| 371 | #define SDR1_LOW_BITS ((n_hpteg - 1) >> 10) |
| 372 | #define MIN_N_HPTEG 1024 /* min 64kB hash table */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 373 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 374 | /* |
| 375 | * Allow 1 HPTE (1/8 HPTEG) for each page of memory. |
| 376 | * This is less than the recommended amount, but then |
| 377 | * Linux ain't AIX. |
| 378 | */ |
| 379 | n_hpteg = total_memory / (PAGE_SIZE * 8); |
| 380 | if (n_hpteg < MIN_N_HPTEG) |
| 381 | n_hpteg = MIN_N_HPTEG; |
| 382 | lg_n_hpteg = __ilog2(n_hpteg); |
| 383 | if (n_hpteg & (n_hpteg - 1)) { |
| 384 | ++lg_n_hpteg; /* round up if not power of 2 */ |
| 385 | n_hpteg = 1 << lg_n_hpteg; |
| 386 | } |
| 387 | Hash_size = n_hpteg << LG_HPTEG_SIZE; |
| 388 | |
| 389 | /* |
| 390 | * Find some memory for the hash table. |
| 391 | */ |
| 392 | if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322); |
Mike Rapoport | b63a07d | 2019-03-07 16:31:06 -0800 | [diff] [blame] | 393 | Hash = memblock_alloc(Hash_size, Hash_size); |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 394 | if (!Hash) |
| 395 | panic("%s: Failed to allocate %lu bytes align=0x%lx\n", |
| 396 | __func__, Hash_size, Hash_size); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 397 | _SDR1 = __pa(Hash) | SDR1_LOW_BITS; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 398 | |
Christophe Leroy | 8f156c2 | 2019-04-26 16:36:37 +0000 | [diff] [blame] | 399 | pr_info("Total memory = %lldMB; using %ldkB for hash table\n", |
| 400 | (unsigned long long)(total_memory >> 20), Hash_size >> 10); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 401 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 402 | |
Christophe Leroy | 72f208c | 2019-04-26 16:23:35 +0000 | [diff] [blame] | 403 | Hash_mask = n_hpteg - 1; |
| 404 | hash_mb2 = hash_mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg; |
| 405 | if (lg_n_hpteg > 16) |
| 406 | hash_mb2 = 16 - LG_HPTEG_SIZE; |
Christophe Leroy | 9d6d712 | 2019-08-14 10:02:20 +0000 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * When KASAN is selected, there is already an early temporary hash |
| 410 | * table and the switch to the final hash table is done later. |
| 411 | */ |
| 412 | if (IS_ENABLED(CONFIG_KASAN)) |
| 413 | return; |
| 414 | |
| 415 | MMU_init_hw_patch(); |
Christophe Leroy | 72f208c | 2019-04-26 16:23:35 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | void __init MMU_init_hw_patch(void) |
| 419 | { |
| 420 | unsigned int hmask = Hash_mask >> (16 - LG_HPTEG_SIZE); |
| 421 | |
| 422 | if (ppc_md.progress) |
| 423 | ppc_md.progress("hash:patch", 0x345); |
| 424 | if (ppc_md.progress) |
| 425 | ppc_md.progress("hash:done", 0x205); |
| 426 | |
| 427 | /* WARNING: Make sure nothing can trigger a KASAN check past this point */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * Patch up the instructions in hashtable.S:create_hpte |
| 431 | */ |
Christophe Leroy | 6790dae | 2019-02-21 10:37:57 +0000 | [diff] [blame] | 432 | modify_instruction_site(&patch__hash_page_A0, 0xffff, |
| 433 | ((unsigned int)Hash - PAGE_OFFSET) >> 16); |
Christophe Leroy | 72f208c | 2019-04-26 16:23:35 +0000 | [diff] [blame] | 434 | modify_instruction_site(&patch__hash_page_A1, 0x7c0, hash_mb << 6); |
| 435 | modify_instruction_site(&patch__hash_page_A2, 0x7c0, hash_mb2 << 6); |
Christophe Leroy | 9efc74f | 2018-11-09 17:33:24 +0000 | [diff] [blame] | 436 | modify_instruction_site(&patch__hash_page_B, 0xffff, hmask); |
| 437 | modify_instruction_site(&patch__hash_page_C, 0xffff, hmask); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 438 | |
| 439 | /* |
| 440 | * Patch up the instructions in hashtable.S:flush_hash_page |
| 441 | */ |
Christophe Leroy | 6790dae | 2019-02-21 10:37:57 +0000 | [diff] [blame] | 442 | modify_instruction_site(&patch__flush_hash_A0, 0xffff, |
| 443 | ((unsigned int)Hash - PAGE_OFFSET) >> 16); |
Christophe Leroy | 72f208c | 2019-04-26 16:23:35 +0000 | [diff] [blame] | 444 | modify_instruction_site(&patch__flush_hash_A1, 0x7c0, hash_mb << 6); |
| 445 | modify_instruction_site(&patch__flush_hash_A2, 0x7c0, hash_mb2 << 6); |
Christophe Leroy | 9efc74f | 2018-11-09 17:33:24 +0000 | [diff] [blame] | 446 | modify_instruction_site(&patch__flush_hash_B, 0xffff, hmask); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 447 | } |
Benjamin Herrenschmidt | cd3db0c | 2010-07-06 15:39:02 -0700 | [diff] [blame] | 448 | |
| 449 | void setup_initial_memory_limit(phys_addr_t first_memblock_base, |
| 450 | phys_addr_t first_memblock_size) |
| 451 | { |
| 452 | /* We don't currently support the first MEMBLOCK not mapping 0 |
| 453 | * physical on those processors |
| 454 | */ |
| 455 | BUG_ON(first_memblock_base != 0); |
| 456 | |
| 457 | /* 601 can only access 16MB at the moment */ |
| 458 | if (PVR_VER(mfspr(SPRN_PVR)) == 1) |
| 459 | memblock_set_current_limit(min_t(u64, first_memblock_size, 0x01000000)); |
| 460 | else /* Anything else has 256M mapped */ |
| 461 | memblock_set_current_limit(min_t(u64, first_memblock_size, 0x10000000)); |
| 462 | } |
Christophe Leroy | 31ed2b1 | 2019-03-11 08:30:35 +0000 | [diff] [blame] | 463 | |
Christophe Leroy | e4dccf9 | 2019-04-26 16:36:39 +0000 | [diff] [blame] | 464 | void __init print_system_hash_info(void) |
| 465 | { |
| 466 | pr_info("Hash_size = 0x%lx\n", Hash_size); |
| 467 | if (Hash_mask) |
| 468 | pr_info("Hash_mask = 0x%lx\n", Hash_mask); |
| 469 | } |
| 470 | |
Christophe Leroy | 31ed2b1 | 2019-03-11 08:30:35 +0000 | [diff] [blame] | 471 | #ifdef CONFIG_PPC_KUEP |
| 472 | void __init setup_kuep(bool disabled) |
| 473 | { |
| 474 | pr_info("Activating Kernel Userspace Execution Prevention\n"); |
| 475 | |
| 476 | if (cpu_has_feature(CPU_FTR_601)) |
| 477 | pr_warn("KUEP is not working on powerpc 601 (No NX bit in Seg Regs)\n"); |
| 478 | |
| 479 | if (disabled) |
| 480 | pr_warn("KUEP cannot be disabled yet on 6xx when compiled in\n"); |
| 481 | } |
| 482 | #endif |
Christophe Leroy | a68c31f | 2019-03-11 08:30:38 +0000 | [diff] [blame] | 483 | |
| 484 | #ifdef CONFIG_PPC_KUAP |
| 485 | void __init setup_kuap(bool disabled) |
| 486 | { |
| 487 | pr_info("Activating Kernel Userspace Access Protection\n"); |
| 488 | |
| 489 | if (disabled) |
| 490 | pr_warn("KUAP cannot be disabled yet on 6xx when compiled in\n"); |
| 491 | } |
| 492 | #endif |