xref: /linux/arch/x86/power/hibernate_64.c (revision fc2d791a43d3880496d1c729b8bd74d2c19cb4e7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Hibernation support for x86-64
4  *
5  * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
6  * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
7  * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
8  */
9 
10 #include <linux/gfp.h>
11 #include <linux/smp.h>
12 #include <linux/suspend.h>
13 #include <linux/scatterlist.h>
14 #include <linux/kdebug.h>
15 #include <linux/pgtable.h>
16 
17 #include <asm/e820/api.h>
18 #include <asm/init.h>
19 #include <asm/proto.h>
20 #include <asm/page.h>
21 #include <asm/mtrr.h>
22 #include <asm/sections.h>
23 #include <asm/suspend.h>
24 #include <asm/tlbflush.h>
25 
26 static int set_up_temporary_text_mapping(pgd_t *pgd)
27 {
28 	pmd_t *pmd;
29 	pud_t *pud;
30 	p4d_t *p4d = NULL;
31 	pgprot_t pgtable_prot = __pgprot(_KERNPG_TABLE);
32 	pgprot_t pmd_text_prot = __pgprot(__PAGE_KERNEL_LARGE_EXEC);
33 
34 	/* Filter out unsupported __PAGE_KERNEL* bits: */
35 	pgprot_val(pmd_text_prot) &= __default_kernel_pte_mask;
36 	pgprot_val(pgtable_prot)  &= __default_kernel_pte_mask;
37 
38 	/*
39 	 * The new mapping only has to cover the page containing the image
40 	 * kernel's entry point (jump_address_phys), because the switch over to
41 	 * it is carried out by relocated code running from a page allocated
42 	 * specifically for this purpose and covered by the identity mapping, so
43 	 * the temporary kernel text mapping is only needed for the final jump.
44 	 * Moreover, in that mapping the virtual address of the image kernel's
45 	 * entry point must be the same as its virtual address in the image
46 	 * kernel (restore_jump_address), so the image kernel's
47 	 * restore_registers() code doesn't find itself in a different area of
48 	 * the virtual address space after switching over to the original page
49 	 * tables used by the image kernel.
50 	 */
51 
52 	if (pgtable_l5_enabled()) {
53 		p4d = (p4d_t *)get_safe_page(GFP_ATOMIC);
54 		if (!p4d)
55 			return -ENOMEM;
56 	}
57 
58 	pud = (pud_t *)get_safe_page(GFP_ATOMIC);
59 	if (!pud)
60 		return -ENOMEM;
61 
62 	pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
63 	if (!pmd)
64 		return -ENOMEM;
65 
66 	set_pmd(pmd + pmd_index(restore_jump_address),
67 		__pmd((jump_address_phys & PMD_MASK) | pgprot_val(pmd_text_prot)));
68 	set_pud(pud + pud_index(restore_jump_address),
69 		__pud(__pa(pmd) | pgprot_val(pgtable_prot)));
70 	if (p4d) {
71 		p4d_t new_p4d = __p4d(__pa(pud) | pgprot_val(pgtable_prot));
72 		pgd_t new_pgd = __pgd(__pa(p4d) | pgprot_val(pgtable_prot));
73 
74 		set_p4d(p4d + p4d_index(restore_jump_address), new_p4d);
75 		set_pgd(pgd + pgd_index(restore_jump_address), new_pgd);
76 	} else {
77 		/* No p4d for 4-level paging: point the pgd to the pud page table */
78 		pgd_t new_pgd = __pgd(__pa(pud) | pgprot_val(pgtable_prot));
79 		set_pgd(pgd + pgd_index(restore_jump_address), new_pgd);
80 	}
81 
82 	return 0;
83 }
84 
85 static void *alloc_pgt_page(void *context)
86 {
87 	return (void *)get_safe_page(GFP_ATOMIC);
88 }
89 
90 static int set_up_temporary_mappings(void)
91 {
92 	struct x86_mapping_info info = {
93 		.alloc_pgt_page	= alloc_pgt_page,
94 		.page_flag	= __PAGE_KERNEL_LARGE_EXEC,
95 		.offset		= __PAGE_OFFSET,
96 	};
97 	unsigned long mstart, mend;
98 	pgd_t *pgd;
99 	int result;
100 	int i;
101 
102 	pgd = (pgd_t *)get_safe_page(GFP_ATOMIC);
103 	if (!pgd)
104 		return -ENOMEM;
105 
106 	/* Prepare a temporary mapping for the kernel text */
107 	result = set_up_temporary_text_mapping(pgd);
108 	if (result)
109 		return result;
110 
111 	/* Set up the direct mapping from scratch */
112 	for (i = 0; i < nr_pfn_mapped; i++) {
113 		mstart = pfn_mapped[i].start << PAGE_SHIFT;
114 		mend   = pfn_mapped[i].end << PAGE_SHIFT;
115 
116 		result = kernel_ident_mapping_init(&info, pgd, mstart, mend);
117 		if (result)
118 			return result;
119 	}
120 
121 	temp_pgt = __pa(pgd);
122 	return 0;
123 }
124 
125 asmlinkage int swsusp_arch_resume(void)
126 {
127 	int error;
128 
129 	/* We have got enough memory and from now on we cannot recover */
130 	error = set_up_temporary_mappings();
131 	if (error)
132 		return error;
133 
134 	error = relocate_restore_code();
135 	if (error)
136 		return error;
137 
138 	restore_image();
139 	return 0;
140 }
141