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>
7 #include <asm/kasan.h>
8 #include <asm/mem_detect.h>
9 #include <asm/processor.h>
10 #include <asm/sclp.h>
11 #include <asm/facility.h>
12 #include <asm/sections.h>
13 #include <asm/setup.h>
14 #include <asm/uv.h>
15 
16 unsigned long kasan_vmax;
17 static unsigned long segment_pos __initdata;
18 static unsigned long segment_low __initdata;
19 static unsigned long pgalloc_pos __initdata;
20 static unsigned long pgalloc_low __initdata;
21 static unsigned long pgalloc_freeable __initdata;
22 static bool has_edat __initdata;
23 static bool has_nx __initdata;
24 
25 #define __sha(x) ((unsigned long)kasan_mem_to_shadow((void *)x))
26 
27 static pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
28 
kasan_early_panic(const char * reason)29 static void __init kasan_early_panic(const char *reason)
30 {
31 	sclp_early_printk("The Linux kernel failed to boot with the KernelAddressSanitizer:\n");
32 	sclp_early_printk(reason);
33 	disabled_wait();
34 }
35 
kasan_early_alloc_segment(void)36 static void * __init kasan_early_alloc_segment(void)
37 {
38 	segment_pos -= _SEGMENT_SIZE;
39 
40 	if (segment_pos < segment_low)
41 		kasan_early_panic("out of memory during initialisation\n");
42 
43 	return (void *)segment_pos;
44 }
45 
kasan_early_alloc_pages(unsigned int order)46 static void * __init kasan_early_alloc_pages(unsigned int order)
47 {
48 	pgalloc_pos -= (PAGE_SIZE << order);
49 
50 	if (pgalloc_pos < pgalloc_low)
51 		kasan_early_panic("out of memory during initialisation\n");
52 
53 	return (void *)pgalloc_pos;
54 }
55 
kasan_early_crst_alloc(unsigned long val)56 static void * __init kasan_early_crst_alloc(unsigned long val)
57 {
58 	unsigned long *table;
59 
60 	table = kasan_early_alloc_pages(CRST_ALLOC_ORDER);
61 	if (table)
62 		crst_table_init(table, val);
63 	return table;
64 }
65 
kasan_early_pte_alloc(void)66 static pte_t * __init kasan_early_pte_alloc(void)
67 {
68 	static void *pte_leftover;
69 	pte_t *pte;
70 
71 	BUILD_BUG_ON(_PAGE_TABLE_SIZE * 2 != PAGE_SIZE);
72 
73 	if (!pte_leftover) {
74 		pte_leftover = kasan_early_alloc_pages(0);
75 		pte = pte_leftover + _PAGE_TABLE_SIZE;
76 	} else {
77 		pte = pte_leftover;
78 		pte_leftover = NULL;
79 	}
80 	memset64((u64 *)pte, _PAGE_INVALID, PTRS_PER_PTE);
81 	return pte;
82 }
83 
84 enum populate_mode {
85 	POPULATE_ONE2ONE,
86 	POPULATE_MAP,
87 	POPULATE_ZERO_SHADOW,
88 	POPULATE_SHALLOW
89 };
kasan_early_vmemmap_populate(unsigned long address,unsigned long end,enum populate_mode mode)90 static void __init kasan_early_vmemmap_populate(unsigned long address,
91 						unsigned long end,
92 						enum populate_mode mode)
93 {
94 	unsigned long pgt_prot_zero, pgt_prot, sgt_prot;
95 	pgd_t *pg_dir;
96 	p4d_t *p4_dir;
97 	pud_t *pu_dir;
98 	pmd_t *pm_dir;
99 	pte_t *pt_dir;
100 
101 	pgt_prot_zero = pgprot_val(PAGE_KERNEL_RO);
102 	if (!has_nx)
103 		pgt_prot_zero &= ~_PAGE_NOEXEC;
104 	pgt_prot = pgprot_val(PAGE_KERNEL);
105 	sgt_prot = pgprot_val(SEGMENT_KERNEL);
106 	if (!has_nx || mode == POPULATE_ONE2ONE) {
107 		pgt_prot &= ~_PAGE_NOEXEC;
108 		sgt_prot &= ~_SEGMENT_ENTRY_NOEXEC;
109 	}
110 
111 	while (address < end) {
112 		pg_dir = pgd_offset_k(address);
113 		if (pgd_none(*pg_dir)) {
114 			if (mode == POPULATE_ZERO_SHADOW &&
115 			    IS_ALIGNED(address, PGDIR_SIZE) &&
116 			    end - address >= PGDIR_SIZE) {
117 				pgd_populate(&init_mm, pg_dir,
118 						kasan_early_shadow_p4d);
119 				address = (address + PGDIR_SIZE) & PGDIR_MASK;
120 				continue;
121 			}
122 			p4_dir = kasan_early_crst_alloc(_REGION2_ENTRY_EMPTY);
123 			pgd_populate(&init_mm, pg_dir, p4_dir);
124 		}
125 
126 		if (IS_ENABLED(CONFIG_KASAN_S390_4_LEVEL_PAGING) &&
127 		    mode == POPULATE_SHALLOW) {
128 			address = (address + P4D_SIZE) & P4D_MASK;
129 			continue;
130 		}
131 
132 		p4_dir = p4d_offset(pg_dir, address);
133 		if (p4d_none(*p4_dir)) {
134 			if (mode == POPULATE_ZERO_SHADOW &&
135 			    IS_ALIGNED(address, P4D_SIZE) &&
136 			    end - address >= P4D_SIZE) {
137 				p4d_populate(&init_mm, p4_dir,
138 						kasan_early_shadow_pud);
139 				address = (address + P4D_SIZE) & P4D_MASK;
140 				continue;
141 			}
142 			pu_dir = kasan_early_crst_alloc(_REGION3_ENTRY_EMPTY);
143 			p4d_populate(&init_mm, p4_dir, pu_dir);
144 		}
145 
146 		if (!IS_ENABLED(CONFIG_KASAN_S390_4_LEVEL_PAGING) &&
147 		    mode == POPULATE_SHALLOW) {
148 			address = (address + PUD_SIZE) & PUD_MASK;
149 			continue;
150 		}
151 
152 		pu_dir = pud_offset(p4_dir, address);
153 		if (pud_none(*pu_dir)) {
154 			if (mode == POPULATE_ZERO_SHADOW &&
155 			    IS_ALIGNED(address, PUD_SIZE) &&
156 			    end - address >= PUD_SIZE) {
157 				pud_populate(&init_mm, pu_dir,
158 						kasan_early_shadow_pmd);
159 				address = (address + PUD_SIZE) & PUD_MASK;
160 				continue;
161 			}
162 			pm_dir = kasan_early_crst_alloc(_SEGMENT_ENTRY_EMPTY);
163 			pud_populate(&init_mm, pu_dir, pm_dir);
164 		}
165 
166 		pm_dir = pmd_offset(pu_dir, address);
167 		if (pmd_none(*pm_dir)) {
168 			if (mode == POPULATE_ZERO_SHADOW &&
169 			    IS_ALIGNED(address, PMD_SIZE) &&
170 			    end - address >= PMD_SIZE) {
171 				pmd_populate(&init_mm, pm_dir,
172 						kasan_early_shadow_pte);
173 				address = (address + PMD_SIZE) & PMD_MASK;
174 				continue;
175 			}
176 			/* the first megabyte of 1:1 is mapped with 4k pages */
177 			if (has_edat && address && end - address >= PMD_SIZE &&
178 			    mode != POPULATE_ZERO_SHADOW) {
179 				void *page;
180 
181 				if (mode == POPULATE_ONE2ONE) {
182 					page = (void *)address;
183 				} else {
184 					page = kasan_early_alloc_segment();
185 					memset(page, 0, _SEGMENT_SIZE);
186 				}
187 				pmd_val(*pm_dir) = __pa(page) | sgt_prot;
188 				address = (address + PMD_SIZE) & PMD_MASK;
189 				continue;
190 			}
191 
192 			pt_dir = kasan_early_pte_alloc();
193 			pmd_populate(&init_mm, pm_dir, pt_dir);
194 		} else if (pmd_large(*pm_dir)) {
195 			address = (address + PMD_SIZE) & PMD_MASK;
196 			continue;
197 		}
198 
199 		pt_dir = pte_offset_kernel(pm_dir, address);
200 		if (pte_none(*pt_dir)) {
201 			void *page;
202 
203 			switch (mode) {
204 			case POPULATE_ONE2ONE:
205 				page = (void *)address;
206 				pte_val(*pt_dir) = __pa(page) | pgt_prot;
207 				break;
208 			case POPULATE_MAP:
209 				page = kasan_early_alloc_pages(0);
210 				memset(page, 0, PAGE_SIZE);
211 				pte_val(*pt_dir) = __pa(page) | pgt_prot;
212 				break;
213 			case POPULATE_ZERO_SHADOW:
214 				page = kasan_early_shadow_page;
215 				pte_val(*pt_dir) = __pa(page) | pgt_prot_zero;
216 				break;
217 			case POPULATE_SHALLOW:
218 				/* should never happen */
219 				break;
220 			}
221 		}
222 		address += PAGE_SIZE;
223 	}
224 }
225 
kasan_set_pgd(pgd_t * pgd,unsigned long asce_type)226 static void __init kasan_set_pgd(pgd_t *pgd, unsigned long asce_type)
227 {
228 	unsigned long asce_bits;
229 
230 	asce_bits = asce_type | _ASCE_TABLE_LENGTH;
231 	S390_lowcore.kernel_asce = (__pa(pgd) & PAGE_MASK) | asce_bits;
232 	S390_lowcore.user_asce = S390_lowcore.kernel_asce;
233 
234 	__ctl_load(S390_lowcore.kernel_asce, 1, 1);
235 	__ctl_load(S390_lowcore.kernel_asce, 7, 7);
236 	__ctl_load(S390_lowcore.kernel_asce, 13, 13);
237 }
238 
kasan_enable_dat(void)239 static void __init kasan_enable_dat(void)
240 {
241 	psw_t psw;
242 
243 	psw.mask = __extract_psw();
244 	psw_bits(psw).dat = 1;
245 	psw_bits(psw).as = PSW_BITS_AS_HOME;
246 	__load_psw_mask(psw.mask);
247 }
248 
kasan_early_detect_facilities(void)249 static void __init kasan_early_detect_facilities(void)
250 {
251 	if (test_facility(8)) {
252 		has_edat = true;
253 		__ctl_set_bit(0, 23);
254 	}
255 	if (!noexec_disabled && test_facility(130)) {
256 		has_nx = true;
257 		__ctl_set_bit(0, 20);
258 	}
259 }
260 
has_uv_sec_stor_limit(void)261 static bool __init has_uv_sec_stor_limit(void)
262 {
263 	/*
264 	 * keep these conditions in line with setup_uv()
265 	 */
266 	if (!is_prot_virt_host())
267 		return false;
268 
269 	if (is_prot_virt_guest())
270 		return false;
271 
272 	if (!test_facility(158))
273 		return false;
274 
275 	return !!uv_info.max_sec_stor_addr;
276 }
277 
kasan_early_init(void)278 void __init kasan_early_init(void)
279 {
280 	unsigned long untracked_mem_end;
281 	unsigned long shadow_alloc_size;
282 	unsigned long vmax_unlimited;
283 	unsigned long initrd_end;
284 	unsigned long asce_type;
285 	unsigned long memsize;
286 	unsigned long pgt_prot = pgprot_val(PAGE_KERNEL_RO);
287 	pte_t pte_z;
288 	pmd_t pmd_z = __pmd(__pa(kasan_early_shadow_pte) | _SEGMENT_ENTRY);
289 	pud_t pud_z = __pud(__pa(kasan_early_shadow_pmd) | _REGION3_ENTRY);
290 	p4d_t p4d_z = __p4d(__pa(kasan_early_shadow_pud) | _REGION2_ENTRY);
291 
292 	kasan_early_detect_facilities();
293 	if (!has_nx)
294 		pgt_prot &= ~_PAGE_NOEXEC;
295 	pte_z = __pte(__pa(kasan_early_shadow_page) | pgt_prot);
296 
297 	memsize = get_mem_detect_end();
298 	if (!memsize)
299 		kasan_early_panic("cannot detect physical memory size\n");
300 	/* respect mem= cmdline parameter */
301 	if (memory_end_set && memsize > memory_end)
302 		memsize = memory_end;
303 	if (IS_ENABLED(CONFIG_CRASH_DUMP) && OLDMEM_BASE)
304 		memsize = min(memsize, OLDMEM_SIZE);
305 	memsize = min(memsize, KASAN_SHADOW_START);
306 
307 	if (IS_ENABLED(CONFIG_KASAN_S390_4_LEVEL_PAGING)) {
308 		/* 4 level paging */
309 		BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, P4D_SIZE));
310 		BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, P4D_SIZE));
311 		crst_table_init((unsigned long *)early_pg_dir,
312 				_REGION2_ENTRY_EMPTY);
313 		untracked_mem_end = kasan_vmax = vmax_unlimited = _REGION1_SIZE;
314 		if (has_uv_sec_stor_limit())
315 			kasan_vmax = min(vmax_unlimited, uv_info.max_sec_stor_addr);
316 		asce_type = _ASCE_TYPE_REGION2;
317 	} else {
318 		/* 3 level paging */
319 		BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PUD_SIZE));
320 		BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PUD_SIZE));
321 		crst_table_init((unsigned long *)early_pg_dir,
322 				_REGION3_ENTRY_EMPTY);
323 		untracked_mem_end = kasan_vmax = vmax_unlimited = _REGION2_SIZE;
324 		asce_type = _ASCE_TYPE_REGION3;
325 	}
326 
327 	/* init kasan zero shadow */
328 	crst_table_init((unsigned long *)kasan_early_shadow_p4d,
329 				p4d_val(p4d_z));
330 	crst_table_init((unsigned long *)kasan_early_shadow_pud,
331 				pud_val(pud_z));
332 	crst_table_init((unsigned long *)kasan_early_shadow_pmd,
333 				pmd_val(pmd_z));
334 	memset64((u64 *)kasan_early_shadow_pte, pte_val(pte_z), PTRS_PER_PTE);
335 
336 	shadow_alloc_size = memsize >> KASAN_SHADOW_SCALE_SHIFT;
337 	pgalloc_low = round_up((unsigned long)_end, _SEGMENT_SIZE);
338 	if (IS_ENABLED(CONFIG_BLK_DEV_INITRD)) {
339 		initrd_end =
340 		    round_up(INITRD_START + INITRD_SIZE, _SEGMENT_SIZE);
341 		pgalloc_low = max(pgalloc_low, initrd_end);
342 	}
343 
344 	if (pgalloc_low + shadow_alloc_size > memsize)
345 		kasan_early_panic("out of memory during initialisation\n");
346 
347 	if (has_edat) {
348 		segment_pos = round_down(memsize, _SEGMENT_SIZE);
349 		segment_low = segment_pos - shadow_alloc_size;
350 		pgalloc_pos = segment_low;
351 	} else {
352 		pgalloc_pos = memsize;
353 	}
354 	init_mm.pgd = early_pg_dir;
355 	/*
356 	 * Current memory layout:
357 	 * +- 0 -------------+	   +- shadow start -+
358 	 * | 1:1 ram mapping |	  /| 1/8 ram	    |
359 	 * |		     |	 / |		    |
360 	 * +- end of ram ----+	/  +----------------+
361 	 * | ... gap ...     | /   |		    |
362 	 * |		     |/    |	kasan	    |
363 	 * +- shadow start --+	   |	zero	    |
364 	 * | 1/8 addr space  |	   |	page	    |
365 	 * +- shadow end    -+	   |	mapping	    |
366 	 * | ... gap ...     |\    |  (untracked)   |
367 	 * +- vmalloc area  -+ \   |		    |
368 	 * | vmalloc_size    |	\  |		    |
369 	 * +- modules vaddr -+	 \ +----------------+
370 	 * | 2Gb	     |	  \|	  unmapped  | allocated per module
371 	 * +-----------------+	   +- shadow end ---+
372 	 *
373 	 * Current memory layout (KASAN_VMALLOC):
374 	 * +- 0 -------------+	   +- shadow start -+
375 	 * | 1:1 ram mapping |	  /| 1/8 ram	    |
376 	 * |		     |	 / |		    |
377 	 * +- end of ram ----+	/  +----------------+
378 	 * | ... gap ...     | /   |	kasan	    |
379 	 * |		     |/    |	zero	    |
380 	 * +- shadow start --+	   |	page	    |
381 	 * | 1/8 addr space  |	   |	mapping     |
382 	 * +- shadow end    -+	   |  (untracked)   |
383 	 * | ... gap ...     |\    |		    |
384 	 * +- vmalloc area  -+ \   +- vmalloc area -+
385 	 * | vmalloc_size    |	\  |shallow populate|
386 	 * +- modules vaddr -+	 \ +- modules area -+
387 	 * | 2Gb	     |	  \|shallow populate|
388 	 * +-----------------+	   +- shadow end ---+
389 	 */
390 	/* populate kasan shadow (for identity mapping and zero page mapping) */
391 	kasan_early_vmemmap_populate(__sha(0), __sha(memsize), POPULATE_MAP);
392 	if (IS_ENABLED(CONFIG_MODULES))
393 		untracked_mem_end = kasan_vmax - MODULES_LEN;
394 	if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
395 		untracked_mem_end = kasan_vmax - vmalloc_size - MODULES_LEN;
396 		/* shallowly populate kasan shadow for vmalloc and modules */
397 		kasan_early_vmemmap_populate(__sha(untracked_mem_end),
398 					     __sha(kasan_vmax), POPULATE_SHALLOW);
399 	}
400 	/* populate kasan shadow for untracked memory */
401 	kasan_early_vmemmap_populate(__sha(max_physmem_end),
402 				     __sha(untracked_mem_end),
403 				     POPULATE_ZERO_SHADOW);
404 	kasan_early_vmemmap_populate(__sha(kasan_vmax),
405 				     __sha(vmax_unlimited),
406 				     POPULATE_ZERO_SHADOW);
407 	/* memory allocated for identity mapping structs will be freed later */
408 	pgalloc_freeable = pgalloc_pos;
409 	/* populate identity mapping */
410 	kasan_early_vmemmap_populate(0, memsize, POPULATE_ONE2ONE);
411 	kasan_set_pgd(early_pg_dir, asce_type);
412 	kasan_enable_dat();
413 	/* enable kasan */
414 	init_task.kasan_depth = 0;
415 	memblock_reserve(pgalloc_pos, memsize - pgalloc_pos);
416 	sclp_early_printk("KernelAddressSanitizer initialized\n");
417 }
418 
kasan_copy_shadow(pgd_t * pg_dir)419 void __init kasan_copy_shadow(pgd_t *pg_dir)
420 {
421 	/*
422 	 * At this point we are still running on early pages setup early_pg_dir,
423 	 * while swapper_pg_dir has just been initialized with identity mapping.
424 	 * Carry over shadow memory region from early_pg_dir to swapper_pg_dir.
425 	 */
426 
427 	pgd_t *pg_dir_src;
428 	pgd_t *pg_dir_dst;
429 	p4d_t *p4_dir_src;
430 	p4d_t *p4_dir_dst;
431 	pud_t *pu_dir_src;
432 	pud_t *pu_dir_dst;
433 
434 	pg_dir_src = pgd_offset_raw(early_pg_dir, KASAN_SHADOW_START);
435 	pg_dir_dst = pgd_offset_raw(pg_dir, KASAN_SHADOW_START);
436 	p4_dir_src = p4d_offset(pg_dir_src, KASAN_SHADOW_START);
437 	p4_dir_dst = p4d_offset(pg_dir_dst, KASAN_SHADOW_START);
438 	if (!p4d_folded(*p4_dir_src)) {
439 		/* 4 level paging */
440 		memcpy(p4_dir_dst, p4_dir_src,
441 		       (KASAN_SHADOW_SIZE >> P4D_SHIFT) * sizeof(p4d_t));
442 		return;
443 	}
444 	/* 3 level paging */
445 	pu_dir_src = pud_offset(p4_dir_src, KASAN_SHADOW_START);
446 	pu_dir_dst = pud_offset(p4_dir_dst, KASAN_SHADOW_START);
447 	memcpy(pu_dir_dst, pu_dir_src,
448 	       (KASAN_SHADOW_SIZE >> PUD_SHIFT) * sizeof(pud_t));
449 }
450 
kasan_free_early_identity(void)451 void __init kasan_free_early_identity(void)
452 {
453 	memblock_free(pgalloc_pos, pgalloc_freeable - pgalloc_pos);
454 }
455