1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kasan.h>
3 #include <linux/sched/task.h>
4 #include <linux/memblock.h>
5 #include <linux/pgtable.h>
6 #include <asm/pgalloc.h>
8 #include <asm/mem_detect.h>
9 #include <asm/processor.h>
11 #include <asm/facility.h>
12 #include <asm/sections.h>
13 #include <asm/setup.h>
16 static unsigned long segment_pos __initdata;
17 static unsigned long segment_low __initdata;
18 static unsigned long pgalloc_pos __initdata;
19 static unsigned long pgalloc_low __initdata;
20 static unsigned long pgalloc_freeable __initdata;
21 static bool has_edat __initdata;
22 static bool has_nx __initdata;
24 #define __sha(x) ((unsigned long)kasan_mem_to_shadow((void *)x))
26 static pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
28 static void __init kasan_early_panic(const char *reason)
30 sclp_early_printk("The Linux kernel failed to boot with the KernelAddressSanitizer:\n");
31 sclp_early_printk(reason);
35 static void * __init kasan_early_alloc_segment(void)
37 segment_pos -= _SEGMENT_SIZE;
39 if (segment_pos < segment_low)
40 kasan_early_panic("out of memory during initialisation\n");
42 return (void *)segment_pos;
45 static void * __init kasan_early_alloc_pages(unsigned int order)
47 pgalloc_pos -= (PAGE_SIZE << order);
49 if (pgalloc_pos < pgalloc_low)
50 kasan_early_panic("out of memory during initialisation\n");
52 return (void *)pgalloc_pos;
55 static void * __init kasan_early_crst_alloc(unsigned long val)
59 table = kasan_early_alloc_pages(CRST_ALLOC_ORDER);
61 crst_table_init(table, val);
65 static pte_t * __init kasan_early_pte_alloc(void)
67 static void *pte_leftover;
70 BUILD_BUG_ON(_PAGE_TABLE_SIZE * 2 != PAGE_SIZE);
73 pte_leftover = kasan_early_alloc_pages(0);
74 pte = pte_leftover + _PAGE_TABLE_SIZE;
79 memset64((u64 *)pte, _PAGE_INVALID, PTRS_PER_PTE);
89 static void __init kasan_early_pgtable_populate(unsigned long address,
91 enum populate_mode mode)
93 unsigned long pgt_prot_zero, pgt_prot, sgt_prot;
100 pgt_prot_zero = pgprot_val(PAGE_KERNEL_RO);
102 pgt_prot_zero &= ~_PAGE_NOEXEC;
103 pgt_prot = pgprot_val(PAGE_KERNEL);
104 sgt_prot = pgprot_val(SEGMENT_KERNEL);
105 if (!has_nx || mode == POPULATE_ONE2ONE) {
106 pgt_prot &= ~_PAGE_NOEXEC;
107 sgt_prot &= ~_SEGMENT_ENTRY_NOEXEC;
111 * The first 1MB of 1:1 mapping is mapped with 4KB pages
113 while (address < end) {
114 pg_dir = pgd_offset_k(address);
115 if (pgd_none(*pg_dir)) {
116 if (mode == POPULATE_ZERO_SHADOW &&
117 IS_ALIGNED(address, PGDIR_SIZE) &&
118 end - address >= PGDIR_SIZE) {
119 pgd_populate(&init_mm, pg_dir,
120 kasan_early_shadow_p4d);
121 address = (address + PGDIR_SIZE) & PGDIR_MASK;
124 p4_dir = kasan_early_crst_alloc(_REGION2_ENTRY_EMPTY);
125 pgd_populate(&init_mm, pg_dir, p4_dir);
128 if (mode == POPULATE_SHALLOW) {
129 address = (address + P4D_SIZE) & P4D_MASK;
133 p4_dir = p4d_offset(pg_dir, address);
134 if (p4d_none(*p4_dir)) {
135 if (mode == POPULATE_ZERO_SHADOW &&
136 IS_ALIGNED(address, P4D_SIZE) &&
137 end - address >= P4D_SIZE) {
138 p4d_populate(&init_mm, p4_dir,
139 kasan_early_shadow_pud);
140 address = (address + P4D_SIZE) & P4D_MASK;
143 pu_dir = kasan_early_crst_alloc(_REGION3_ENTRY_EMPTY);
144 p4d_populate(&init_mm, p4_dir, pu_dir);
147 pu_dir = pud_offset(p4_dir, address);
148 if (pud_none(*pu_dir)) {
149 if (mode == POPULATE_ZERO_SHADOW &&
150 IS_ALIGNED(address, PUD_SIZE) &&
151 end - address >= PUD_SIZE) {
152 pud_populate(&init_mm, pu_dir,
153 kasan_early_shadow_pmd);
154 address = (address + PUD_SIZE) & PUD_MASK;
157 pm_dir = kasan_early_crst_alloc(_SEGMENT_ENTRY_EMPTY);
158 pud_populate(&init_mm, pu_dir, pm_dir);
161 pm_dir = pmd_offset(pu_dir, address);
162 if (pmd_none(*pm_dir)) {
163 if (IS_ALIGNED(address, PMD_SIZE) &&
164 end - address >= PMD_SIZE) {
165 if (mode == POPULATE_ZERO_SHADOW) {
166 pmd_populate(&init_mm, pm_dir, kasan_early_shadow_pte);
167 address = (address + PMD_SIZE) & PMD_MASK;
169 } else if (has_edat && address) {
172 if (mode == POPULATE_ONE2ONE) {
173 page = (void *)address;
175 page = kasan_early_alloc_segment();
176 memset(page, 0, _SEGMENT_SIZE);
178 set_pmd(pm_dir, __pmd(__pa(page) | sgt_prot));
179 address = (address + PMD_SIZE) & PMD_MASK;
183 pt_dir = kasan_early_pte_alloc();
184 pmd_populate(&init_mm, pm_dir, pt_dir);
185 } else if (pmd_large(*pm_dir)) {
186 address = (address + PMD_SIZE) & PMD_MASK;
190 pt_dir = pte_offset_kernel(pm_dir, address);
191 if (pte_none(*pt_dir)) {
195 case POPULATE_ONE2ONE:
196 page = (void *)address;
197 set_pte(pt_dir, __pte(__pa(page) | pgt_prot));
200 page = kasan_early_alloc_pages(0);
201 memset(page, 0, PAGE_SIZE);
202 set_pte(pt_dir, __pte(__pa(page) | pgt_prot));
204 case POPULATE_ZERO_SHADOW:
205 page = kasan_early_shadow_page;
206 set_pte(pt_dir, __pte(__pa(page) | pgt_prot_zero));
208 case POPULATE_SHALLOW:
209 /* should never happen */
213 address += PAGE_SIZE;
217 static void __init kasan_set_pgd(pgd_t *pgd, unsigned long asce_type)
219 unsigned long asce_bits;
221 asce_bits = asce_type | _ASCE_TABLE_LENGTH;
222 S390_lowcore.kernel_asce = (__pa(pgd) & PAGE_MASK) | asce_bits;
223 S390_lowcore.user_asce = S390_lowcore.kernel_asce;
225 __ctl_load(S390_lowcore.kernel_asce, 1, 1);
226 __ctl_load(S390_lowcore.kernel_asce, 7, 7);
227 __ctl_load(S390_lowcore.kernel_asce, 13, 13);
230 static void __init kasan_enable_dat(void)
234 psw.mask = __extract_psw();
235 psw_bits(psw).dat = 1;
236 psw_bits(psw).as = PSW_BITS_AS_HOME;
237 __load_psw_mask(psw.mask);
240 static void __init kasan_early_detect_facilities(void)
242 if (test_facility(8)) {
244 __ctl_set_bit(0, 23);
246 if (!noexec_disabled && test_facility(130)) {
248 __ctl_set_bit(0, 20);
252 void __init kasan_early_init(void)
254 unsigned long shadow_alloc_size;
255 unsigned long initrd_end;
256 unsigned long memsize;
257 unsigned long pgt_prot = pgprot_val(PAGE_KERNEL_RO);
259 pmd_t pmd_z = __pmd(__pa(kasan_early_shadow_pte) | _SEGMENT_ENTRY);
260 pud_t pud_z = __pud(__pa(kasan_early_shadow_pmd) | _REGION3_ENTRY);
261 p4d_t p4d_z = __p4d(__pa(kasan_early_shadow_pud) | _REGION2_ENTRY);
263 kasan_early_detect_facilities();
265 pgt_prot &= ~_PAGE_NOEXEC;
266 pte_z = __pte(__pa(kasan_early_shadow_page) | pgt_prot);
268 memsize = get_mem_detect_end();
270 kasan_early_panic("cannot detect physical memory size\n");
272 * Kasan currently supports standby memory but only if it follows
273 * online memory (default allocation), i.e. no memory holes.
274 * - memsize represents end of online memory
275 * - ident_map_size represents online + standby and memory limits
277 * Kasan maps "memsize" right away.
278 * [0, memsize] - as identity mapping
279 * [__sha(0), __sha(memsize)] - shadow memory for identity mapping
280 * The rest [memsize, ident_map_size] if memsize < ident_map_size
281 * could be mapped/unmapped dynamically later during memory hotplug.
283 memsize = min(memsize, ident_map_size);
285 BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, P4D_SIZE));
286 BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, P4D_SIZE));
287 crst_table_init((unsigned long *)early_pg_dir, _REGION2_ENTRY_EMPTY);
289 /* init kasan zero shadow */
290 crst_table_init((unsigned long *)kasan_early_shadow_p4d,
292 crst_table_init((unsigned long *)kasan_early_shadow_pud,
294 crst_table_init((unsigned long *)kasan_early_shadow_pmd,
296 memset64((u64 *)kasan_early_shadow_pte, pte_val(pte_z), PTRS_PER_PTE);
298 shadow_alloc_size = memsize >> KASAN_SHADOW_SCALE_SHIFT;
299 pgalloc_low = round_up((unsigned long)_end, _SEGMENT_SIZE);
300 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD)) {
302 round_up(initrd_data.start + initrd_data.size, _SEGMENT_SIZE);
303 pgalloc_low = max(pgalloc_low, initrd_end);
306 if (pgalloc_low + shadow_alloc_size > memsize)
307 kasan_early_panic("out of memory during initialisation\n");
310 segment_pos = round_down(memsize, _SEGMENT_SIZE);
311 segment_low = segment_pos - shadow_alloc_size;
312 pgalloc_pos = segment_low;
314 pgalloc_pos = memsize;
316 init_mm.pgd = early_pg_dir;
318 * Current memory layout:
319 * +- 0 -------------+ +- shadow start -+
320 * | 1:1 ram mapping | /| 1/8 ram |
322 * +- end of ram ----+ / +----------------+
323 * | ... gap ... | / | |
325 * +- shadow start --+ | zero |
326 * | 1/8 addr space | | page |
327 * +- shadow end -+ | mapping |
328 * | ... gap ... |\ | (untracked) |
329 * +- vmalloc area -+ \ | |
330 * | vmalloc_size | \ | |
331 * +- modules vaddr -+ \ +----------------+
332 * | 2Gb | \| unmapped | allocated per module
333 * +-----------------+ +- shadow end ---+
335 * Current memory layout (KASAN_VMALLOC):
336 * +- 0 -------------+ +- shadow start -+
337 * | 1:1 ram mapping | /| 1/8 ram |
339 * +- end of ram ----+ / +----------------+
340 * | ... gap ... | / | kasan |
342 * +- shadow start --+ | page |
343 * | 1/8 addr space | | mapping |
344 * +- shadow end -+ | (untracked) |
345 * | ... gap ... |\ | |
346 * +- vmalloc area -+ \ +- vmalloc area -+
347 * | vmalloc_size | \ |shallow populate|
348 * +- modules vaddr -+ \ +- modules area -+
349 * | 2Gb | \|shallow populate|
350 * +-----------------+ +- shadow end ---+
352 /* populate kasan shadow (for identity mapping and zero page mapping) */
353 kasan_early_pgtable_populate(__sha(0), __sha(memsize), POPULATE_MAP);
354 if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
355 /* shallowly populate kasan shadow for vmalloc and modules */
356 kasan_early_pgtable_populate(__sha(VMALLOC_START), __sha(MODULES_END),
359 /* populate kasan shadow for untracked memory */
360 kasan_early_pgtable_populate(__sha(ident_map_size),
361 IS_ENABLED(CONFIG_KASAN_VMALLOC) ?
362 __sha(VMALLOC_START) :
363 __sha(MODULES_VADDR),
364 POPULATE_ZERO_SHADOW);
365 kasan_early_pgtable_populate(__sha(MODULES_END), __sha(_REGION1_SIZE),
366 POPULATE_ZERO_SHADOW);
367 /* memory allocated for identity mapping structs will be freed later */
368 pgalloc_freeable = pgalloc_pos;
369 /* populate identity mapping */
370 kasan_early_pgtable_populate(0, memsize, POPULATE_ONE2ONE);
371 kasan_set_pgd(early_pg_dir, _ASCE_TYPE_REGION2);
374 init_task.kasan_depth = 0;
375 memblock_reserve(pgalloc_pos, memsize - pgalloc_pos);
376 sclp_early_printk("KernelAddressSanitizer initialized\n");
379 void __init kasan_copy_shadow_mapping(void)
382 * At this point we are still running on early pages setup early_pg_dir,
383 * while swapper_pg_dir has just been initialized with identity mapping.
384 * Carry over shadow memory region from early_pg_dir to swapper_pg_dir.
392 pg_dir_src = pgd_offset_raw(early_pg_dir, KASAN_SHADOW_START);
393 pg_dir_dst = pgd_offset_raw(init_mm.pgd, KASAN_SHADOW_START);
394 p4_dir_src = p4d_offset(pg_dir_src, KASAN_SHADOW_START);
395 p4_dir_dst = p4d_offset(pg_dir_dst, KASAN_SHADOW_START);
396 memcpy(p4_dir_dst, p4_dir_src,
397 (KASAN_SHADOW_SIZE >> P4D_SHIFT) * sizeof(p4d_t));
400 void __init kasan_free_early_identity(void)
402 memblock_phys_free(pgalloc_pos, pgalloc_freeable - pgalloc_pos);