xref: /openbmc/linux/arch/x86/power/hibernate.c (revision 2874c5fd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Hibernation support for x86
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 #include <linux/gfp.h>
10 #include <linux/smp.h>
11 #include <linux/suspend.h>
12 #include <linux/scatterlist.h>
13 #include <linux/kdebug.h>
14 
15 #include <crypto/hash.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/pgtable.h>
22 #include <asm/mtrr.h>
23 #include <asm/sections.h>
24 #include <asm/suspend.h>
25 #include <asm/tlbflush.h>
26 
27 /*
28  * Address to jump to in the last phase of restore in order to get to the image
29  * kernel's text (this value is passed in the image header).
30  */
31 unsigned long restore_jump_address __visible;
32 unsigned long jump_address_phys;
33 
34 /*
35  * Value of the cr3 register from before the hibernation (this value is passed
36  * in the image header).
37  */
38 unsigned long restore_cr3 __visible;
39 unsigned long temp_pgt __visible;
40 unsigned long relocated_restore_code __visible;
41 
42 /**
43  *	pfn_is_nosave - check if given pfn is in the 'nosave' section
44  */
45 int pfn_is_nosave(unsigned long pfn)
46 {
47 	unsigned long nosave_begin_pfn;
48 	unsigned long nosave_end_pfn;
49 
50 	nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
51 	nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
52 
53 	return pfn >= nosave_begin_pfn && pfn < nosave_end_pfn;
54 }
55 
56 
57 #define MD5_DIGEST_SIZE 16
58 
59 struct restore_data_record {
60 	unsigned long jump_address;
61 	unsigned long jump_address_phys;
62 	unsigned long cr3;
63 	unsigned long magic;
64 	u8 e820_digest[MD5_DIGEST_SIZE];
65 };
66 
67 #if IS_BUILTIN(CONFIG_CRYPTO_MD5)
68 /**
69  * get_e820_md5 - calculate md5 according to given e820 table
70  *
71  * @table: the e820 table to be calculated
72  * @buf: the md5 result to be stored to
73  */
74 static int get_e820_md5(struct e820_table *table, void *buf)
75 {
76 	struct crypto_shash *tfm;
77 	struct shash_desc *desc;
78 	int size;
79 	int ret = 0;
80 
81 	tfm = crypto_alloc_shash("md5", 0, 0);
82 	if (IS_ERR(tfm))
83 		return -ENOMEM;
84 
85 	desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
86 		       GFP_KERNEL);
87 	if (!desc) {
88 		ret = -ENOMEM;
89 		goto free_tfm;
90 	}
91 
92 	desc->tfm = tfm;
93 
94 	size = offsetof(struct e820_table, entries) +
95 		sizeof(struct e820_entry) * table->nr_entries;
96 
97 	if (crypto_shash_digest(desc, (u8 *)table, size, buf))
98 		ret = -EINVAL;
99 
100 	kzfree(desc);
101 
102 free_tfm:
103 	crypto_free_shash(tfm);
104 	return ret;
105 }
106 
107 static int hibernation_e820_save(void *buf)
108 {
109 	return get_e820_md5(e820_table_firmware, buf);
110 }
111 
112 static bool hibernation_e820_mismatch(void *buf)
113 {
114 	int ret;
115 	u8 result[MD5_DIGEST_SIZE];
116 
117 	memset(result, 0, MD5_DIGEST_SIZE);
118 	/* If there is no digest in suspend kernel, let it go. */
119 	if (!memcmp(result, buf, MD5_DIGEST_SIZE))
120 		return false;
121 
122 	ret = get_e820_md5(e820_table_firmware, result);
123 	if (ret)
124 		return true;
125 
126 	return memcmp(result, buf, MD5_DIGEST_SIZE) ? true : false;
127 }
128 #else
129 static int hibernation_e820_save(void *buf)
130 {
131 	return 0;
132 }
133 
134 static bool hibernation_e820_mismatch(void *buf)
135 {
136 	/* If md5 is not builtin for restore kernel, let it go. */
137 	return false;
138 }
139 #endif
140 
141 #ifdef CONFIG_X86_64
142 #define RESTORE_MAGIC	0x23456789ABCDEF01UL
143 #else
144 #define RESTORE_MAGIC	0x12345678UL
145 #endif
146 
147 /**
148  *	arch_hibernation_header_save - populate the architecture specific part
149  *		of a hibernation image header
150  *	@addr: address to save the data at
151  */
152 int arch_hibernation_header_save(void *addr, unsigned int max_size)
153 {
154 	struct restore_data_record *rdr = addr;
155 
156 	if (max_size < sizeof(struct restore_data_record))
157 		return -EOVERFLOW;
158 	rdr->magic = RESTORE_MAGIC;
159 	rdr->jump_address = (unsigned long)restore_registers;
160 	rdr->jump_address_phys = __pa_symbol(restore_registers);
161 
162 	/*
163 	 * The restore code fixes up CR3 and CR4 in the following sequence:
164 	 *
165 	 * [in hibernation asm]
166 	 * 1. CR3 <= temporary page tables
167 	 * 2. CR4 <= mmu_cr4_features (from the kernel that restores us)
168 	 * 3. CR3 <= rdr->cr3
169 	 * 4. CR4 <= mmu_cr4_features (from us, i.e. the image kernel)
170 	 * [in restore_processor_state()]
171 	 * 5. CR4 <= saved CR4
172 	 * 6. CR3 <= saved CR3
173 	 *
174 	 * Our mmu_cr4_features has CR4.PCIDE=0, and toggling
175 	 * CR4.PCIDE while CR3's PCID bits are nonzero is illegal, so
176 	 * rdr->cr3 needs to point to valid page tables but must not
177 	 * have any of the PCID bits set.
178 	 */
179 	rdr->cr3 = restore_cr3 & ~CR3_PCID_MASK;
180 
181 	return hibernation_e820_save(rdr->e820_digest);
182 }
183 
184 /**
185  *	arch_hibernation_header_restore - read the architecture specific data
186  *		from the hibernation image header
187  *	@addr: address to read the data from
188  */
189 int arch_hibernation_header_restore(void *addr)
190 {
191 	struct restore_data_record *rdr = addr;
192 
193 	if (rdr->magic != RESTORE_MAGIC) {
194 		pr_crit("Unrecognized hibernate image header format!\n");
195 		return -EINVAL;
196 	}
197 
198 	restore_jump_address = rdr->jump_address;
199 	jump_address_phys = rdr->jump_address_phys;
200 	restore_cr3 = rdr->cr3;
201 
202 	if (hibernation_e820_mismatch(rdr->e820_digest)) {
203 		pr_crit("Hibernate inconsistent memory map detected!\n");
204 		return -ENODEV;
205 	}
206 
207 	return 0;
208 }
209 
210 int relocate_restore_code(void)
211 {
212 	pgd_t *pgd;
213 	p4d_t *p4d;
214 	pud_t *pud;
215 	pmd_t *pmd;
216 	pte_t *pte;
217 
218 	relocated_restore_code = get_safe_page(GFP_ATOMIC);
219 	if (!relocated_restore_code)
220 		return -ENOMEM;
221 
222 	memcpy((void *)relocated_restore_code, core_restore_code, PAGE_SIZE);
223 
224 	/* Make the page containing the relocated code executable */
225 	pgd = (pgd_t *)__va(read_cr3_pa()) +
226 		pgd_index(relocated_restore_code);
227 	p4d = p4d_offset(pgd, relocated_restore_code);
228 	if (p4d_large(*p4d)) {
229 		set_p4d(p4d, __p4d(p4d_val(*p4d) & ~_PAGE_NX));
230 		goto out;
231 	}
232 	pud = pud_offset(p4d, relocated_restore_code);
233 	if (pud_large(*pud)) {
234 		set_pud(pud, __pud(pud_val(*pud) & ~_PAGE_NX));
235 		goto out;
236 	}
237 	pmd = pmd_offset(pud, relocated_restore_code);
238 	if (pmd_large(*pmd)) {
239 		set_pmd(pmd, __pmd(pmd_val(*pmd) & ~_PAGE_NX));
240 		goto out;
241 	}
242 	pte = pte_offset_kernel(pmd, relocated_restore_code);
243 	set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_NX));
244 out:
245 	__flush_tlb_all();
246 	return 0;
247 }
248