xref: /linux/arch/x86/kernel/machine_kexec_64.c (revision 4b99990cdf9560e8a071640baf19f312e6ae02f4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * handle transition of Linux booting another kernel
4  * Copyright (C) 2002-2005 Eric Biederman  <ebiederm@xmission.com>
5  */
6 
7 #define pr_fmt(fmt)	"kexec: " fmt
8 
9 #include <linux/mm.h>
10 #include <linux/kexec.h>
11 #include <linux/string.h>
12 #include <linux/gfp.h>
13 #include <linux/reboot.h>
14 #include <linux/numa.h>
15 #include <linux/ftrace.h>
16 #include <linux/io.h>
17 #include <linux/suspend.h>
18 #include <linux/vmalloc.h>
19 #include <linux/efi.h>
20 #include <linux/cc_platform.h>
21 
22 #include <asm/init.h>
23 #include <asm/tlbflush.h>
24 #include <asm/mmu_context.h>
25 #include <asm/io_apic.h>
26 #include <asm/debugreg.h>
27 #include <asm/kexec-bzimage64.h>
28 #include <asm/setup.h>
29 #include <asm/set_memory.h>
30 #include <asm/cpu.h>
31 #include <asm/efi.h>
32 #include <asm/processor.h>
33 
34 #ifdef CONFIG_ACPI
35 /*
36  * Used while adding mapping for ACPI tables.
37  * Can be reused when other iomem regions need be mapped
38  */
39 struct init_pgtable_data {
40 	struct x86_mapping_info *info;
41 	pgd_t *level4p;
42 };
43 
44 static int mem_region_callback(struct resource *res, void *arg)
45 {
46 	struct init_pgtable_data *data = arg;
47 
48 	return kernel_ident_mapping_init(data->info, data->level4p,
49 					 res->start, res->end + 1);
50 }
51 
52 static int
53 map_acpi_tables(struct x86_mapping_info *info, pgd_t *level4p)
54 {
55 	struct init_pgtable_data data;
56 	unsigned long flags;
57 	int ret;
58 
59 	data.info = info;
60 	data.level4p = level4p;
61 	flags = IORESOURCE_MEM | IORESOURCE_BUSY;
62 
63 	ret = walk_iomem_res_desc(IORES_DESC_ACPI_TABLES, flags, 0, -1,
64 				  &data, mem_region_callback);
65 	if (ret && ret != -EINVAL)
66 		return ret;
67 
68 	/* ACPI tables could be located in ACPI Non-volatile Storage region */
69 	ret = walk_iomem_res_desc(IORES_DESC_ACPI_NV_STORAGE, flags, 0, -1,
70 				  &data, mem_region_callback);
71 	if (ret && ret != -EINVAL)
72 		return ret;
73 
74 	return 0;
75 }
76 #else
77 static int map_acpi_tables(struct x86_mapping_info *info, pgd_t *level4p) { return 0; }
78 #endif
79 
80 static int map_mmio_serial(struct x86_mapping_info *info, pgd_t *level4p)
81 {
82 	unsigned long mstart, mend;
83 
84 	if (!kexec_debug_8250_mmio32)
85 		return 0;
86 
87 	mstart = kexec_debug_8250_mmio32 & PAGE_MASK;
88 	mend = (kexec_debug_8250_mmio32 + PAGE_SIZE + 23) & PAGE_MASK;
89 	pr_info("Map PCI serial at %lx - %lx\n", mstart, mend);
90 	return kernel_ident_mapping_init(info, level4p, mstart, mend);
91 }
92 
93 #ifdef CONFIG_KEXEC_FILE
94 const struct kexec_file_ops * const kexec_file_loaders[] = {
95 		&kexec_bzImage64_ops,
96 		NULL
97 };
98 #endif
99 
100 static int
101 map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p)
102 {
103 #ifdef CONFIG_EFI
104 	unsigned long mstart, mend;
105 	void *kaddr;
106 	int ret;
107 
108 	if (!efi_enabled(EFI_BOOT))
109 		return 0;
110 
111 	mstart = (boot_params.efi_info.efi_systab |
112 			((u64)boot_params.efi_info.efi_systab_hi<<32));
113 
114 	if (efi_enabled(EFI_64BIT))
115 		mend = mstart + sizeof(efi_system_table_64_t);
116 	else
117 		mend = mstart + sizeof(efi_system_table_32_t);
118 
119 	if (!mstart)
120 		return 0;
121 
122 	ret = kernel_ident_mapping_init(info, level4p, mstart, mend);
123 	if (ret)
124 		return ret;
125 
126 	kaddr = memremap(mstart, mend - mstart, MEMREMAP_WB);
127 	if (!kaddr) {
128 		pr_err("Could not map UEFI system table\n");
129 		return -ENOMEM;
130 	}
131 
132 	mstart = efi_config_table;
133 
134 	if (efi_enabled(EFI_64BIT)) {
135 		efi_system_table_64_t *stbl = (efi_system_table_64_t *)kaddr;
136 
137 		mend = mstart + sizeof(efi_config_table_64_t) * stbl->nr_tables;
138 	} else {
139 		efi_system_table_32_t *stbl = (efi_system_table_32_t *)kaddr;
140 
141 		mend = mstart + sizeof(efi_config_table_32_t) * stbl->nr_tables;
142 	}
143 
144 	memunmap(kaddr);
145 
146 	return kernel_ident_mapping_init(info, level4p, mstart, mend);
147 #endif
148 	return 0;
149 }
150 
151 static void free_transition_pgtable(struct kimage *image)
152 {
153 	free_page((unsigned long)image->arch.p4d);
154 	image->arch.p4d = NULL;
155 	free_page((unsigned long)image->arch.pud);
156 	image->arch.pud = NULL;
157 	free_page((unsigned long)image->arch.pmd);
158 	image->arch.pmd = NULL;
159 	free_page((unsigned long)image->arch.pte);
160 	image->arch.pte = NULL;
161 }
162 
163 static int init_transition_pgtable(struct kimage *image, pgd_t *pgd,
164 				   unsigned long control_page)
165 {
166 	pgprot_t prot = PAGE_KERNEL_EXEC_NOENC;
167 	unsigned long vaddr, paddr;
168 	int result = -ENOMEM;
169 	p4d_t *p4d;
170 	pud_t *pud;
171 	pmd_t *pmd;
172 	pte_t *pte;
173 
174 	/*
175 	 * For the transition to the identity mapped page tables, the control
176 	 * code page also needs to be mapped at the virtual address it starts
177 	 * off running from.
178 	 */
179 	vaddr = (unsigned long)__va(control_page);
180 	paddr = control_page;
181 	pgd += pgd_index(vaddr);
182 	if (!pgd_present(*pgd)) {
183 		p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
184 		if (!p4d)
185 			goto err;
186 		image->arch.p4d = p4d;
187 		set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE));
188 	}
189 	p4d = p4d_offset(pgd, vaddr);
190 	if (!p4d_present(*p4d)) {
191 		pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
192 		if (!pud)
193 			goto err;
194 		image->arch.pud = pud;
195 		set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
196 	}
197 	pud = pud_offset(p4d, vaddr);
198 	if (!pud_present(*pud)) {
199 		pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
200 		if (!pmd)
201 			goto err;
202 		image->arch.pmd = pmd;
203 		set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
204 	}
205 	pmd = pmd_offset(pud, vaddr);
206 	if (!pmd_present(*pmd)) {
207 		pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
208 		if (!pte)
209 			goto err;
210 		image->arch.pte = pte;
211 		set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
212 	}
213 	pte = pte_offset_kernel(pmd, vaddr);
214 
215 	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
216 		prot = PAGE_KERNEL_EXEC;
217 
218 	set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
219 	return 0;
220 err:
221 	return result;
222 }
223 
224 static void *alloc_pgt_page(void *data)
225 {
226 	struct kimage *image = (struct kimage *)data;
227 	struct page *page;
228 	void *p = NULL;
229 
230 	page = kimage_alloc_control_pages(image, 0);
231 	if (page) {
232 		p = page_address(page);
233 		clear_page(p);
234 	}
235 
236 	return p;
237 }
238 
239 static int init_pgtable(struct kimage *image, unsigned long control_page)
240 {
241 	struct x86_mapping_info info = {
242 		.alloc_pgt_page	= alloc_pgt_page,
243 		.context	= image,
244 		.page_flag	= __PAGE_KERNEL_LARGE_EXEC,
245 		.kernpg_flag	= _KERNPG_TABLE_NOENC,
246 	};
247 	unsigned long mstart, mend;
248 	int result;
249 	int i;
250 
251 	image->arch.pgd = alloc_pgt_page(image);
252 	if (!image->arch.pgd)
253 		return -ENOMEM;
254 
255 	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
256 		info.page_flag   |= _PAGE_ENC;
257 		info.kernpg_flag |= _PAGE_ENC;
258 	}
259 
260 	if (direct_gbpages)
261 		info.direct_gbpages = true;
262 
263 	for (i = 0; i < nr_pfn_mapped; i++) {
264 		mstart = pfn_mapped[i].start << PAGE_SHIFT;
265 		mend   = pfn_mapped[i].end << PAGE_SHIFT;
266 
267 		result = kernel_ident_mapping_init(&info, image->arch.pgd,
268 						   mstart, mend);
269 		if (result)
270 			return result;
271 	}
272 
273 	/*
274 	 * segments's mem ranges could be outside 0 ~ max_pfn,
275 	 * for example when jump back to original kernel from kexeced kernel.
276 	 * or first kernel is booted with user mem map, and second kernel
277 	 * could be loaded out of that range.
278 	 */
279 	for (i = 0; i < image->nr_segments; i++) {
280 		mstart = image->segment[i].mem;
281 		mend   = mstart + image->segment[i].memsz;
282 
283 		result = kernel_ident_mapping_init(&info, image->arch.pgd,
284 						   mstart, mend);
285 
286 		if (result)
287 			return result;
288 	}
289 
290 	/*
291 	 * Prepare EFI systab and ACPI tables for kexec kernel since they are
292 	 * not covered by pfn_mapped.
293 	 */
294 	result = map_efi_systab(&info, image->arch.pgd);
295 	if (result)
296 		return result;
297 
298 	result = map_acpi_tables(&info, image->arch.pgd);
299 	if (result)
300 		return result;
301 
302 	result = map_mmio_serial(&info, image->arch.pgd);
303 	if (result)
304 		return result;
305 
306 	/*
307 	 * This must be last because the intermediate page table pages it
308 	 * allocates will not be control pages and may overlap the image.
309 	 */
310 	return init_transition_pgtable(image, image->arch.pgd, control_page);
311 }
312 
313 static void load_segments(void)
314 {
315 	__asm__ __volatile__ (
316 		"\tmovl %0,%%ds\n"
317 		"\tmovl %0,%%es\n"
318 		"\tmovl %0,%%ss\n"
319 		"\tmovl %0,%%fs\n"
320 		"\tmovl %0,%%gs\n"
321 		: : "a" (__KERNEL_DS) : "memory"
322 		);
323 }
324 
325 static void prepare_debug_idt(unsigned long control_page, unsigned long vec_ofs)
326 {
327 	gate_desc idtentry = { 0 };
328 	int i;
329 
330 	idtentry.bits.p		= 1;
331 	idtentry.bits.type	= GATE_TRAP;
332 	idtentry.segment	= __KERNEL_CS;
333 	idtentry.offset_low	= (control_page & 0xFFFF) + vec_ofs;
334 	idtentry.offset_middle	= (control_page >> 16) & 0xFFFF;
335 	idtentry.offset_high	= control_page >> 32;
336 
337 	for (i = 0; i < 16; i++) {
338 		kexec_debug_idt[i] = idtentry;
339 		idtentry.offset_low += KEXEC_DEBUG_EXC_HANDLER_SIZE;
340 	}
341 }
342 
343 int machine_kexec_prepare(struct kimage *image)
344 {
345 	void *control_page = page_address(image->control_code_page);
346 	unsigned long reloc_start = (unsigned long)__relocate_kernel_start;
347 	unsigned long reloc_end = (unsigned long)__relocate_kernel_end;
348 	int result;
349 
350 	/* Setup the identity mapped 64bit page table */
351 	result = init_pgtable(image, __pa(control_page));
352 	if (result)
353 		return result;
354 	kexec_va_control_page = (unsigned long)control_page;
355 	kexec_pa_table_page = (unsigned long)__pa(image->arch.pgd);
356 
357 	if (image->type == KEXEC_TYPE_DEFAULT)
358 		kexec_pa_swap_page = page_to_pfn(image->swap_page) << PAGE_SHIFT;
359 
360 	prepare_debug_idt((unsigned long)__pa(control_page),
361 			  (unsigned long)kexec_debug_exc_vectors - reloc_start);
362 
363 	__memcpy(control_page, __relocate_kernel_start, reloc_end - reloc_start);
364 
365 	set_memory_rox((unsigned long)control_page, 1);
366 
367 	return 0;
368 }
369 
370 void machine_kexec_cleanup(struct kimage *image)
371 {
372 	void *control_page = page_address(image->control_code_page);
373 
374 	set_memory_nx((unsigned long)control_page, 1);
375 	set_memory_rw((unsigned long)control_page, 1);
376 
377 	free_transition_pgtable(image);
378 }
379 
380 /*
381  * Do not allocate memory (or fail in any way) in machine_kexec().
382  * We are past the point of no return, committed to rebooting now.
383  */
384 void __nocfi machine_kexec(struct kimage *image)
385 {
386 	unsigned long reloc_start = (unsigned long)__relocate_kernel_start;
387 	relocate_kernel_fn *relocate_kernel_ptr;
388 	unsigned int relocate_kernel_flags;
389 	int save_ftrace_enabled;
390 	void *control_page;
391 
392 #ifdef CONFIG_KEXEC_JUMP
393 	if (image->preserve_context)
394 		save_processor_state();
395 #endif
396 
397 	save_ftrace_enabled = __ftrace_enabled_save();
398 
399 	/* Interrupts aren't acceptable while we reboot */
400 	local_irq_disable();
401 	hw_breakpoint_disable();
402 	cet_disable();
403 
404 	if (image->preserve_context) {
405 #ifdef CONFIG_X86_IO_APIC
406 		/*
407 		 * We need to put APICs in legacy mode so that we can
408 		 * get timer interrupts in second kernel. kexec/kdump
409 		 * paths already have calls to restore_boot_irq_mode()
410 		 * in one form or other. kexec jump path also need one.
411 		 */
412 		clear_IO_APIC();
413 		restore_boot_irq_mode();
414 #endif
415 	}
416 
417 	control_page = page_address(image->control_code_page);
418 
419 	/*
420 	 * Allow for the possibility that relocate_kernel might not be at
421 	 * the very start of the page.
422 	 */
423 	relocate_kernel_ptr = control_page + (unsigned long)relocate_kernel - reloc_start;
424 
425 	relocate_kernel_flags = 0;
426 	if (image->preserve_context)
427 		relocate_kernel_flags |= RELOC_KERNEL_PRESERVE_CONTEXT;
428 
429 	/*
430 	 * This must be done before load_segments() since it resets
431 	 * GS to 0 and percpu data needs the correct GS to work.
432 	 */
433 	if (this_cpu_read(cache_state_incoherent))
434 		relocate_kernel_flags |= RELOC_KERNEL_CACHE_INCOHERENT;
435 
436 	/*
437 	 * The segment registers are funny things, they have both a
438 	 * visible and an invisible part.  Whenever the visible part is
439 	 * set to a specific selector, the invisible part is loaded
440 	 * with from a table in memory.  At no other time is the
441 	 * descriptor table in memory accessed.
442 	 *
443 	 * Take advantage of this here by force loading the segments,
444 	 * before the GDT is zapped with an invalid value.
445 	 *
446 	 * load_segments() resets GS to 0.  Don't make any function call
447 	 * after here since call depth tracking uses percpu variables to
448 	 * operate (relocate_kernel() is explicitly ignored by call depth
449 	 * tracking).
450 	 */
451 	load_segments();
452 
453 	/* now call it */
454 	image->start = relocate_kernel_ptr((unsigned long)image->head,
455 					   virt_to_phys(control_page),
456 					   image->start,
457 					   relocate_kernel_flags);
458 
459 #ifdef CONFIG_KEXEC_JUMP
460 	if (image->preserve_context)
461 		restore_processor_state();
462 #endif
463 
464 	__ftrace_enabled_restore(save_ftrace_enabled);
465 }
466 /*
467  * Handover to the next kernel, no CFI concern.
468  */
469 ANNOTATE_NOCFI_SYM(machine_kexec);
470 
471 /* arch-dependent functionality related to kexec file-based syscall */
472 
473 #ifdef CONFIG_KEXEC_FILE
474 /*
475  * Apply purgatory relocations.
476  *
477  * @pi:		Purgatory to be relocated.
478  * @section:	Section relocations applying to.
479  * @relsec:	Section containing RELAs.
480  * @symtabsec:	Corresponding symtab.
481  *
482  * TODO: Some of the code belongs to generic code. Move that in kexec.c.
483  */
484 int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
485 				     Elf_Shdr *section, const Elf_Shdr *relsec,
486 				     const Elf_Shdr *symtabsec)
487 {
488 	unsigned int i;
489 	Elf64_Rela *rel;
490 	Elf64_Sym *sym;
491 	void *location;
492 	unsigned long address, sec_base, value;
493 	const char *strtab, *name, *shstrtab;
494 	const Elf_Shdr *sechdrs;
495 
496 	/* String & section header string table */
497 	sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
498 	strtab = (char *)pi->ehdr + sechdrs[symtabsec->sh_link].sh_offset;
499 	shstrtab = (char *)pi->ehdr + sechdrs[pi->ehdr->e_shstrndx].sh_offset;
500 
501 	rel = (void *)pi->ehdr + relsec->sh_offset;
502 
503 	pr_debug("Applying relocate section %s to %u\n",
504 		 shstrtab + relsec->sh_name, relsec->sh_info);
505 
506 	for (i = 0; i < relsec->sh_size / sizeof(*rel); i++) {
507 
508 		/*
509 		 * rel[i].r_offset contains byte offset from beginning
510 		 * of section to the storage unit affected.
511 		 *
512 		 * This is location to update. This is temporary buffer
513 		 * where section is currently loaded. This will finally be
514 		 * loaded to a different address later, pointed to by
515 		 * ->sh_addr. kexec takes care of moving it
516 		 *  (kexec_load_segment()).
517 		 */
518 		location = pi->purgatory_buf;
519 		location += section->sh_offset;
520 		location += rel[i].r_offset;
521 
522 		/* Final address of the location */
523 		address = section->sh_addr + rel[i].r_offset;
524 
525 		/*
526 		 * rel[i].r_info contains information about symbol table index
527 		 * w.r.t which relocation must be made and type of relocation
528 		 * to apply. ELF64_R_SYM() and ELF64_R_TYPE() macros get
529 		 * these respectively.
530 		 */
531 		sym = (void *)pi->ehdr + symtabsec->sh_offset;
532 		sym += ELF64_R_SYM(rel[i].r_info);
533 
534 		if (sym->st_name)
535 			name = strtab + sym->st_name;
536 		else
537 			name = shstrtab + sechdrs[sym->st_shndx].sh_name;
538 
539 		pr_debug("Symbol: %s info: %02x shndx: %02x value=%llx size: %llx\n",
540 			 name, sym->st_info, sym->st_shndx, sym->st_value,
541 			 sym->st_size);
542 
543 		if (sym->st_shndx == SHN_UNDEF) {
544 			pr_err("Undefined symbol: %s\n", name);
545 			return -ENOEXEC;
546 		}
547 
548 		if (sym->st_shndx == SHN_COMMON) {
549 			pr_err("symbol '%s' in common section\n", name);
550 			return -ENOEXEC;
551 		}
552 
553 		if (sym->st_shndx == SHN_ABS)
554 			sec_base = 0;
555 		else if (sym->st_shndx >= pi->ehdr->e_shnum) {
556 			pr_err("Invalid section %d for symbol %s\n",
557 			       sym->st_shndx, name);
558 			return -ENOEXEC;
559 		} else
560 			sec_base = pi->sechdrs[sym->st_shndx].sh_addr;
561 
562 		value = sym->st_value;
563 		value += sec_base;
564 		value += rel[i].r_addend;
565 
566 		switch (ELF64_R_TYPE(rel[i].r_info)) {
567 		case R_X86_64_NONE:
568 			break;
569 		case R_X86_64_64:
570 			*(u64 *)location = value;
571 			break;
572 		case R_X86_64_32:
573 			*(u32 *)location = value;
574 			if (value != *(u32 *)location)
575 				goto overflow;
576 			break;
577 		case R_X86_64_32S:
578 			*(s32 *)location = value;
579 			if ((s64)value != *(s32 *)location)
580 				goto overflow;
581 			break;
582 		case R_X86_64_PC32:
583 		case R_X86_64_PLT32:
584 			value -= (u64)address;
585 			*(u32 *)location = value;
586 			break;
587 		default:
588 			pr_err("Unknown rela relocation: %llu\n",
589 			       ELF64_R_TYPE(rel[i].r_info));
590 			return -ENOEXEC;
591 		}
592 	}
593 	return 0;
594 
595 overflow:
596 	pr_err("Overflow in relocation type %d value 0x%lx\n",
597 	       (int)ELF64_R_TYPE(rel[i].r_info), value);
598 	return -ENOEXEC;
599 }
600 
601 int arch_kimage_file_post_load_cleanup(struct kimage *image)
602 {
603 	vfree(image->elf_headers);
604 	image->elf_headers = NULL;
605 	image->elf_headers_sz = 0;
606 
607 	return kexec_image_post_load_cleanup_default(image);
608 }
609 #endif /* CONFIG_KEXEC_FILE */
610 
611 #ifdef CONFIG_CRASH_DUMP
612 
613 static int
614 kexec_mark_range(unsigned long start, unsigned long end, bool protect)
615 {
616 	struct page *page;
617 	unsigned int nr_pages;
618 
619 	/*
620 	 * For physical range: [start, end]. We must skip the unassigned
621 	 * crashk resource with zero-valued "end" member.
622 	 */
623 	if (!end || start > end)
624 		return 0;
625 
626 	page = pfn_to_page(start >> PAGE_SHIFT);
627 	nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
628 	if (protect)
629 		return set_pages_ro(page, nr_pages);
630 	else
631 		return set_pages_rw(page, nr_pages);
632 }
633 
634 static void kexec_mark_crashkres(bool protect)
635 {
636 	unsigned long control;
637 
638 	kexec_mark_range(crashk_low_res.start, crashk_low_res.end, protect);
639 
640 	/* Don't touch the control code page used in crash_kexec().*/
641 	control = PFN_PHYS(page_to_pfn(kexec_crash_image->control_code_page));
642 	kexec_mark_range(crashk_res.start, control - 1, protect);
643 	control += KEXEC_CONTROL_PAGE_SIZE;
644 	kexec_mark_range(control, crashk_res.end, protect);
645 }
646 
647 /* make the memory storing dm crypt keys in/accessible */
648 static void kexec_mark_dm_crypt_keys(bool protect)
649 {
650 	unsigned long start_paddr, end_paddr;
651 	unsigned int nr_pages;
652 
653 	if (kexec_crash_image->dm_crypt_keys_addr) {
654 		start_paddr = kexec_crash_image->dm_crypt_keys_addr;
655 		end_paddr = start_paddr + kexec_crash_image->dm_crypt_keys_sz - 1;
656 		nr_pages = (PAGE_ALIGN(end_paddr) - PAGE_ALIGN_DOWN(start_paddr))/PAGE_SIZE;
657 		if (protect)
658 			set_memory_np((unsigned long)phys_to_virt(start_paddr), nr_pages);
659 		else
660 			set_memory_p((unsigned long)phys_to_virt(start_paddr), nr_pages);
661 	}
662 }
663 
664 void arch_kexec_protect_crashkres(void)
665 {
666 	kexec_mark_crashkres(true);
667 	kexec_mark_dm_crypt_keys(true);
668 }
669 
670 void arch_kexec_unprotect_crashkres(void)
671 {
672 	kexec_mark_dm_crypt_keys(false);
673 	kexec_mark_crashkres(false);
674 }
675 #endif
676 
677 /*
678  * During a traditional boot under SME, SME will encrypt the kernel,
679  * so the SME kexec kernel also needs to be un-encrypted in order to
680  * replicate a normal SME boot.
681  *
682  * During a traditional boot under SEV, the kernel has already been
683  * loaded encrypted, so the SEV kexec kernel needs to be encrypted in
684  * order to replicate a normal SEV boot.
685  */
686 int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gfp_t gfp)
687 {
688 	if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
689 		return 0;
690 
691 	/*
692 	 * If host memory encryption is active we need to be sure that kexec
693 	 * pages are not encrypted because when we boot to the new kernel the
694 	 * pages won't be accessed encrypted (initially).
695 	 */
696 	return set_memory_decrypted((unsigned long)vaddr, pages);
697 }
698 
699 void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages)
700 {
701 	if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
702 		return;
703 
704 	/*
705 	 * If host memory encryption is active we need to reset the pages back
706 	 * to being an encrypted mapping before freeing them.
707 	 */
708 	set_memory_encrypted((unsigned long)vaddr, pages);
709 }
710