Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright IBM Corp. 2011 |
| 4 | * Author(s): Jan Glauber <jang@linux.vnet.ibm.com> |
| 5 | */ |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 6 | #include <linux/hugetlb.h> |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 7 | #include <linux/mm.h> |
Martin Schwidefsky | 638ad34 | 2011-10-30 15:17:13 +0100 | [diff] [blame] | 8 | #include <asm/cacheflush.h> |
Heiko Carstens | cfb0b24 | 2014-09-23 21:29:20 +0200 | [diff] [blame] | 9 | #include <asm/facility.h> |
Heiko Carstens | a01ef30 | 2017-06-16 17:51:15 +0200 | [diff] [blame] | 10 | #include <asm/pgalloc.h> |
Sven Schnelle | e41ba11 | 2021-07-28 21:02:53 +0200 | [diff] [blame] | 11 | #include <asm/kfence.h> |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 12 | #include <asm/page.h> |
Laura Abbott | e6c7c63 | 2017-05-08 15:58:08 -0700 | [diff] [blame] | 13 | #include <asm/set_memory.h> |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 14 | |
Heiko Carstens | f7f8d7e | 2013-03-14 16:46:05 +0100 | [diff] [blame] | 15 | static inline unsigned long sske_frame(unsigned long addr, unsigned char skey) |
| 16 | { |
Dominik Dingel | 964c2c0 | 2018-07-13 11:28:25 +0100 | [diff] [blame] | 17 | asm volatile(".insn rrf,0xb22b0000,%[skey],%[addr],1,0" |
Heiko Carstens | f7f8d7e | 2013-03-14 16:46:05 +0100 | [diff] [blame] | 18 | : [addr] "+a" (addr) : [skey] "d" (skey)); |
| 19 | return addr; |
| 20 | } |
| 21 | |
Martin Schwidefsky | 127c1fe | 2013-10-07 12:12:32 +0200 | [diff] [blame] | 22 | void __storage_key_init_range(unsigned long start, unsigned long end) |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 23 | { |
Heiko Carstens | f7f8d7e | 2013-03-14 16:46:05 +0100 | [diff] [blame] | 24 | unsigned long boundary, size; |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 25 | |
| 26 | while (start < end) { |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 27 | if (MACHINE_HAS_EDAT1) { |
| 28 | /* set storage keys for a 1MB frame */ |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 29 | size = 1UL << 20; |
| 30 | boundary = (start + size) & ~(size - 1); |
| 31 | if (boundary <= end) { |
| 32 | do { |
Heiko Carstens | f7f8d7e | 2013-03-14 16:46:05 +0100 | [diff] [blame] | 33 | start = sske_frame(start, PAGE_DEFAULT_KEY); |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 34 | } while (start < boundary); |
| 35 | continue; |
| 36 | } |
| 37 | } |
Dominik Dingel | 964c2c0 | 2018-07-13 11:28:25 +0100 | [diff] [blame] | 38 | page_set_storage_key(start, PAGE_DEFAULT_KEY, 1); |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 39 | start += PAGE_SIZE; |
| 40 | } |
| 41 | } |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 42 | |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_PROC_FS |
| 44 | atomic_long_t direct_pages_count[PG_DIRECT_MAP_MAX]; |
| 45 | |
| 46 | void arch_report_meminfo(struct seq_file *m) |
| 47 | { |
| 48 | seq_printf(m, "DirectMap4k: %8lu kB\n", |
| 49 | atomic_long_read(&direct_pages_count[PG_DIRECT_MAP_4K]) << 2); |
| 50 | seq_printf(m, "DirectMap1M: %8lu kB\n", |
| 51 | atomic_long_read(&direct_pages_count[PG_DIRECT_MAP_1M]) << 10); |
| 52 | seq_printf(m, "DirectMap2G: %8lu kB\n", |
| 53 | atomic_long_read(&direct_pages_count[PG_DIRECT_MAP_2G]) << 21); |
| 54 | } |
| 55 | #endif /* CONFIG_PROC_FS */ |
| 56 | |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 57 | static void pgt_set(unsigned long *old, unsigned long new, unsigned long addr, |
| 58 | unsigned long dtt) |
Heiko Carstens | 5b1ba9e | 2012-10-01 16:18:46 +0200 | [diff] [blame] | 59 | { |
Alexander Gordeev | 273cd17 | 2021-01-11 11:01:55 +0100 | [diff] [blame] | 60 | unsigned long *table, mask; |
Heiko Carstens | 5b1ba9e | 2012-10-01 16:18:46 +0200 | [diff] [blame] | 61 | |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 62 | mask = 0; |
| 63 | if (MACHINE_HAS_EDAT2) { |
| 64 | switch (dtt) { |
| 65 | case CRDTE_DTT_REGION3: |
| 66 | mask = ~(PTRS_PER_PUD * sizeof(pud_t) - 1); |
| 67 | break; |
| 68 | case CRDTE_DTT_SEGMENT: |
| 69 | mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1); |
| 70 | break; |
| 71 | case CRDTE_DTT_PAGE: |
| 72 | mask = ~(PTRS_PER_PTE * sizeof(pte_t) - 1); |
| 73 | break; |
| 74 | } |
Alexander Gordeev | 273cd17 | 2021-01-11 11:01:55 +0100 | [diff] [blame] | 75 | table = (unsigned long *)((unsigned long)old & mask); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 76 | crdte(*old, new, table, dtt, addr, S390_lowcore.kernel_asce); |
| 77 | } else if (MACHINE_HAS_IDTE) { |
| 78 | cspg(old, *old, new); |
| 79 | } else { |
| 80 | csp((unsigned int *)old + 1, *old, new); |
| 81 | } |
Heiko Carstens | 5b1ba9e | 2012-10-01 16:18:46 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 84 | static int walk_pte_level(pmd_t *pmdp, unsigned long addr, unsigned long end, |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 85 | unsigned long flags) |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 86 | { |
| 87 | pte_t *ptep, new; |
| 88 | |
Heiko Carstens | c4f0e5c | 2021-08-24 15:21:44 +0200 | [diff] [blame] | 89 | if (flags == SET_MEMORY_4K) |
Heiko Carstens | b3e1a00 | 2021-07-28 21:02:51 +0200 | [diff] [blame] | 90 | return 0; |
Mike Rapoport | 974b9b2 | 2020-06-08 21:33:10 -0700 | [diff] [blame] | 91 | ptep = pte_offset_kernel(pmdp, addr); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 92 | do { |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 93 | new = *ptep; |
| 94 | if (pte_none(new)) |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 95 | return -EINVAL; |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 96 | if (flags & SET_MEMORY_RO) |
| 97 | new = pte_wrprotect(new); |
| 98 | else if (flags & SET_MEMORY_RW) |
| 99 | new = pte_mkwrite(pte_mkdirty(new)); |
Heiko Carstens | 1366def | 2017-04-24 15:27:35 +0200 | [diff] [blame] | 100 | if (flags & SET_MEMORY_NX) |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 101 | pte_val(new) |= _PAGE_NOEXEC; |
| 102 | else if (flags & SET_MEMORY_X) |
| 103 | pte_val(new) &= ~_PAGE_NOEXEC; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 104 | pgt_set((unsigned long *)ptep, pte_val(new), addr, CRDTE_DTT_PAGE); |
| 105 | ptep++; |
Jan Glauber | e4c031b | 2011-04-20 10:15:32 +0200 | [diff] [blame] | 106 | addr += PAGE_SIZE; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 107 | cond_resched(); |
| 108 | } while (addr < end); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int split_pmd_page(pmd_t *pmdp, unsigned long addr) |
| 113 | { |
| 114 | unsigned long pte_addr, prot; |
| 115 | pte_t *pt_dir, *ptep; |
| 116 | pmd_t new; |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 117 | int i, ro, nx; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 118 | |
| 119 | pt_dir = vmem_pte_alloc(); |
| 120 | if (!pt_dir) |
| 121 | return -ENOMEM; |
| 122 | pte_addr = pmd_pfn(*pmdp) << PAGE_SHIFT; |
| 123 | ro = !!(pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT); |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 124 | nx = !!(pmd_val(*pmdp) & _SEGMENT_ENTRY_NOEXEC); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 125 | prot = pgprot_val(ro ? PAGE_KERNEL_RO : PAGE_KERNEL); |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 126 | if (!nx) |
| 127 | prot &= ~_PAGE_NOEXEC; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 128 | ptep = pt_dir; |
| 129 | for (i = 0; i < PTRS_PER_PTE; i++) { |
| 130 | pte_val(*ptep) = pte_addr | prot; |
| 131 | pte_addr += PAGE_SIZE; |
| 132 | ptep++; |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 133 | } |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 134 | pmd_val(new) = __pa(pt_dir) | _SEGMENT_ENTRY; |
| 135 | pgt_set((unsigned long *)pmdp, pmd_val(new), addr, CRDTE_DTT_SEGMENT); |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 136 | update_page_count(PG_DIRECT_MAP_4K, PTRS_PER_PTE); |
| 137 | update_page_count(PG_DIRECT_MAP_1M, -1); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 141 | static void modify_pmd_page(pmd_t *pmdp, unsigned long addr, |
| 142 | unsigned long flags) |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 143 | { |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 144 | pmd_t new = *pmdp; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 145 | |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 146 | if (flags & SET_MEMORY_RO) |
| 147 | new = pmd_wrprotect(new); |
| 148 | else if (flags & SET_MEMORY_RW) |
| 149 | new = pmd_mkwrite(pmd_mkdirty(new)); |
Heiko Carstens | 1366def | 2017-04-24 15:27:35 +0200 | [diff] [blame] | 150 | if (flags & SET_MEMORY_NX) |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 151 | pmd_val(new) |= _SEGMENT_ENTRY_NOEXEC; |
| 152 | else if (flags & SET_MEMORY_X) |
| 153 | pmd_val(new) &= ~_SEGMENT_ENTRY_NOEXEC; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 154 | pgt_set((unsigned long *)pmdp, pmd_val(new), addr, CRDTE_DTT_SEGMENT); |
| 155 | } |
| 156 | |
| 157 | static int walk_pmd_level(pud_t *pudp, unsigned long addr, unsigned long end, |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 158 | unsigned long flags) |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 159 | { |
| 160 | unsigned long next; |
Heiko Carstens | b3e1a00 | 2021-07-28 21:02:51 +0200 | [diff] [blame] | 161 | int need_split; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 162 | pmd_t *pmdp; |
| 163 | int rc = 0; |
| 164 | |
| 165 | pmdp = pmd_offset(pudp, addr); |
| 166 | do { |
| 167 | if (pmd_none(*pmdp)) |
| 168 | return -EINVAL; |
| 169 | next = pmd_addr_end(addr, end); |
| 170 | if (pmd_large(*pmdp)) { |
Heiko Carstens | b3e1a00 | 2021-07-28 21:02:51 +0200 | [diff] [blame] | 171 | need_split = !!(flags & SET_MEMORY_4K); |
| 172 | need_split |= !!(addr & ~PMD_MASK); |
| 173 | need_split |= !!(addr + PMD_SIZE > next); |
| 174 | if (need_split) { |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 175 | rc = split_pmd_page(pmdp, addr); |
| 176 | if (rc) |
| 177 | return rc; |
| 178 | continue; |
| 179 | } |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 180 | modify_pmd_page(pmdp, addr, flags); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 181 | } else { |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 182 | rc = walk_pte_level(pmdp, addr, next, flags); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 183 | if (rc) |
| 184 | return rc; |
| 185 | } |
| 186 | pmdp++; |
| 187 | addr = next; |
| 188 | cond_resched(); |
| 189 | } while (addr < end); |
| 190 | return rc; |
| 191 | } |
| 192 | |
| 193 | static int split_pud_page(pud_t *pudp, unsigned long addr) |
| 194 | { |
| 195 | unsigned long pmd_addr, prot; |
| 196 | pmd_t *pm_dir, *pmdp; |
| 197 | pud_t new; |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 198 | int i, ro, nx; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 199 | |
Heiko Carstens | a01ef30 | 2017-06-16 17:51:15 +0200 | [diff] [blame] | 200 | pm_dir = vmem_crst_alloc(_SEGMENT_ENTRY_EMPTY); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 201 | if (!pm_dir) |
| 202 | return -ENOMEM; |
| 203 | pmd_addr = pud_pfn(*pudp) << PAGE_SHIFT; |
| 204 | ro = !!(pud_val(*pudp) & _REGION_ENTRY_PROTECT); |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 205 | nx = !!(pud_val(*pudp) & _REGION_ENTRY_NOEXEC); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 206 | prot = pgprot_val(ro ? SEGMENT_KERNEL_RO : SEGMENT_KERNEL); |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 207 | if (!nx) |
| 208 | prot &= ~_SEGMENT_ENTRY_NOEXEC; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 209 | pmdp = pm_dir; |
| 210 | for (i = 0; i < PTRS_PER_PMD; i++) { |
| 211 | pmd_val(*pmdp) = pmd_addr | prot; |
| 212 | pmd_addr += PMD_SIZE; |
| 213 | pmdp++; |
| 214 | } |
| 215 | pud_val(new) = __pa(pm_dir) | _REGION3_ENTRY; |
| 216 | pgt_set((unsigned long *)pudp, pud_val(new), addr, CRDTE_DTT_REGION3); |
Heiko Carstens | 37cd944 | 2016-05-20 08:08:14 +0200 | [diff] [blame] | 217 | update_page_count(PG_DIRECT_MAP_1M, PTRS_PER_PMD); |
| 218 | update_page_count(PG_DIRECT_MAP_2G, -1); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 222 | static void modify_pud_page(pud_t *pudp, unsigned long addr, |
| 223 | unsigned long flags) |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 224 | { |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 225 | pud_t new = *pudp; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 226 | |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 227 | if (flags & SET_MEMORY_RO) |
| 228 | new = pud_wrprotect(new); |
| 229 | else if (flags & SET_MEMORY_RW) |
| 230 | new = pud_mkwrite(pud_mkdirty(new)); |
Heiko Carstens | 1366def | 2017-04-24 15:27:35 +0200 | [diff] [blame] | 231 | if (flags & SET_MEMORY_NX) |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 232 | pud_val(new) |= _REGION_ENTRY_NOEXEC; |
| 233 | else if (flags & SET_MEMORY_X) |
| 234 | pud_val(new) &= ~_REGION_ENTRY_NOEXEC; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 235 | pgt_set((unsigned long *)pudp, pud_val(new), addr, CRDTE_DTT_REGION3); |
| 236 | } |
| 237 | |
Martin Schwidefsky | 1aea9b3 | 2017-04-24 18:19:10 +0200 | [diff] [blame] | 238 | static int walk_pud_level(p4d_t *p4d, unsigned long addr, unsigned long end, |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 239 | unsigned long flags) |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 240 | { |
| 241 | unsigned long next; |
Heiko Carstens | b3e1a00 | 2021-07-28 21:02:51 +0200 | [diff] [blame] | 242 | int need_split; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 243 | pud_t *pudp; |
| 244 | int rc = 0; |
| 245 | |
Martin Schwidefsky | 1aea9b3 | 2017-04-24 18:19:10 +0200 | [diff] [blame] | 246 | pudp = pud_offset(p4d, addr); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 247 | do { |
| 248 | if (pud_none(*pudp)) |
| 249 | return -EINVAL; |
| 250 | next = pud_addr_end(addr, end); |
| 251 | if (pud_large(*pudp)) { |
Heiko Carstens | b3e1a00 | 2021-07-28 21:02:51 +0200 | [diff] [blame] | 252 | need_split = !!(flags & SET_MEMORY_4K); |
| 253 | need_split |= !!(addr & ~PUD_MASK); |
| 254 | need_split |= !!(addr + PUD_SIZE > next); |
| 255 | if (need_split) { |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 256 | rc = split_pud_page(pudp, addr); |
| 257 | if (rc) |
| 258 | break; |
| 259 | continue; |
| 260 | } |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 261 | modify_pud_page(pudp, addr, flags); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 262 | } else { |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 263 | rc = walk_pmd_level(pudp, addr, next, flags); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 264 | } |
| 265 | pudp++; |
| 266 | addr = next; |
| 267 | cond_resched(); |
| 268 | } while (addr < end && !rc); |
| 269 | return rc; |
| 270 | } |
| 271 | |
Martin Schwidefsky | 1aea9b3 | 2017-04-24 18:19:10 +0200 | [diff] [blame] | 272 | static int walk_p4d_level(pgd_t *pgd, unsigned long addr, unsigned long end, |
| 273 | unsigned long flags) |
| 274 | { |
| 275 | unsigned long next; |
| 276 | p4d_t *p4dp; |
| 277 | int rc = 0; |
| 278 | |
| 279 | p4dp = p4d_offset(pgd, addr); |
| 280 | do { |
| 281 | if (p4d_none(*p4dp)) |
| 282 | return -EINVAL; |
| 283 | next = p4d_addr_end(addr, end); |
| 284 | rc = walk_pud_level(p4dp, addr, next, flags); |
| 285 | p4dp++; |
| 286 | addr = next; |
| 287 | cond_resched(); |
| 288 | } while (addr < end && !rc); |
| 289 | return rc; |
| 290 | } |
| 291 | |
Heiko Carstens | da1694a | 2020-09-07 17:20:05 +0200 | [diff] [blame] | 292 | DEFINE_MUTEX(cpa_mutex); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 293 | |
| 294 | static int change_page_attr(unsigned long addr, unsigned long end, |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 295 | unsigned long flags) |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 296 | { |
| 297 | unsigned long next; |
| 298 | int rc = -EINVAL; |
| 299 | pgd_t *pgdp; |
| 300 | |
Heiko Carstens | 4d81aaa | 2016-08-09 12:26:28 +0200 | [diff] [blame] | 301 | if (addr == end) |
| 302 | return 0; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 303 | if (end >= MODULES_END) |
| 304 | return -EINVAL; |
| 305 | mutex_lock(&cpa_mutex); |
| 306 | pgdp = pgd_offset_k(addr); |
| 307 | do { |
| 308 | if (pgd_none(*pgdp)) |
| 309 | break; |
| 310 | next = pgd_addr_end(addr, end); |
Martin Schwidefsky | 1aea9b3 | 2017-04-24 18:19:10 +0200 | [diff] [blame] | 311 | rc = walk_p4d_level(pgdp, addr, next, flags); |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 312 | if (rc) |
| 313 | break; |
| 314 | cond_resched(); |
| 315 | } while (pgdp++, addr = next, addr < end && !rc); |
| 316 | mutex_unlock(&cpa_mutex); |
| 317 | return rc; |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 320 | int __set_memory(unsigned long addr, int numpages, unsigned long flags) |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 321 | { |
Heiko Carstens | 1366def | 2017-04-24 15:27:35 +0200 | [diff] [blame] | 322 | if (!MACHINE_HAS_NX) |
| 323 | flags &= ~(SET_MEMORY_NX | SET_MEMORY_X); |
| 324 | if (!flags) |
| 325 | return 0; |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 326 | addr &= PAGE_MASK; |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 327 | return change_page_attr(addr, addr + numpages * PAGE_SIZE, flags); |
Jan Glauber | 448694a | 2011-05-19 16:55:26 -0600 | [diff] [blame] | 328 | } |
Heiko Carstens | 0a4ccc9 | 2012-11-02 13:28:48 +0100 | [diff] [blame] | 329 | |
Sven Schnelle | e41ba11 | 2021-07-28 21:02:53 +0200 | [diff] [blame] | 330 | #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KFENCE) |
Heiko Carstens | cfb0b24 | 2014-09-23 21:29:20 +0200 | [diff] [blame] | 331 | |
| 332 | static void ipte_range(pte_t *pte, unsigned long address, int nr) |
| 333 | { |
| 334 | int i; |
| 335 | |
Heiko Carstens | 5a79859a | 2015-02-12 13:08:27 +0100 | [diff] [blame] | 336 | if (test_facility(13)) { |
Martin Schwidefsky | 34eeaf3 | 2016-06-14 12:38:40 +0200 | [diff] [blame] | 337 | __ptep_ipte_range(address, nr - 1, pte, IPTE_GLOBAL); |
Heiko Carstens | cfb0b24 | 2014-09-23 21:29:20 +0200 | [diff] [blame] | 338 | return; |
| 339 | } |
| 340 | for (i = 0; i < nr; i++) { |
Martin Schwidefsky | 28c807e | 2016-07-26 16:00:22 +0200 | [diff] [blame] | 341 | __ptep_ipte(address, pte, 0, 0, IPTE_GLOBAL); |
Heiko Carstens | cfb0b24 | 2014-09-23 21:29:20 +0200 | [diff] [blame] | 342 | address += PAGE_SIZE; |
| 343 | pte++; |
| 344 | } |
| 345 | } |
| 346 | |
Joonsoo Kim | 031bc57 | 2014-12-12 16:55:52 -0800 | [diff] [blame] | 347 | void __kernel_map_pages(struct page *page, int numpages, int enable) |
Heiko Carstens | 0a4ccc9 | 2012-11-02 13:28:48 +0100 | [diff] [blame] | 348 | { |
| 349 | unsigned long address; |
Heiko Carstens | cfb0b24 | 2014-09-23 21:29:20 +0200 | [diff] [blame] | 350 | int nr, i, j; |
Heiko Carstens | 0a4ccc9 | 2012-11-02 13:28:48 +0100 | [diff] [blame] | 351 | pte_t *pte; |
Heiko Carstens | 0a4ccc9 | 2012-11-02 13:28:48 +0100 | [diff] [blame] | 352 | |
Heiko Carstens | cfb0b24 | 2014-09-23 21:29:20 +0200 | [diff] [blame] | 353 | for (i = 0; i < numpages;) { |
Heiko Carstens | cfafad6 | 2021-08-03 14:42:29 +0200 | [diff] [blame] | 354 | address = (unsigned long)page_to_virt(page + i); |
Mike Rapoport | e05c7b1 | 2020-06-08 21:33:05 -0700 | [diff] [blame] | 355 | pte = virt_to_kpte(address); |
Heiko Carstens | cfb0b24 | 2014-09-23 21:29:20 +0200 | [diff] [blame] | 356 | nr = (unsigned long)pte >> ilog2(sizeof(long)); |
| 357 | nr = PTRS_PER_PTE - (nr & (PTRS_PER_PTE - 1)); |
| 358 | nr = min(numpages - i, nr); |
| 359 | if (enable) { |
| 360 | for (j = 0; j < nr; j++) { |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 361 | pte_val(*pte) &= ~_PAGE_INVALID; |
Heiko Carstens | cfb0b24 | 2014-09-23 21:29:20 +0200 | [diff] [blame] | 362 | address += PAGE_SIZE; |
| 363 | pte++; |
| 364 | } |
| 365 | } else { |
| 366 | ipte_range(pte, address, nr); |
Heiko Carstens | 0a4ccc9 | 2012-11-02 13:28:48 +0100 | [diff] [blame] | 367 | } |
Heiko Carstens | cfb0b24 | 2014-09-23 21:29:20 +0200 | [diff] [blame] | 368 | i += nr; |
Heiko Carstens | 0a4ccc9 | 2012-11-02 13:28:48 +0100 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Heiko Carstens | 0a4ccc9 | 2012-11-02 13:28:48 +0100 | [diff] [blame] | 372 | #endif /* CONFIG_DEBUG_PAGEALLOC */ |