This source file includes following definitions.
- pfn_is_nosave
- get_e820_md5
- hibernation_e820_save
- hibernation_e820_mismatch
- hibernation_e820_save
- hibernation_e820_mismatch
- arch_hibernation_header_save
- arch_hibernation_header_restore
- relocate_restore_code
- arch_resume_nosmt
1
2
3
4
5
6
7
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 #include <linux/cpu.h>
15
16 #include <crypto/hash.h>
17
18 #include <asm/e820/api.h>
19 #include <asm/init.h>
20 #include <asm/proto.h>
21 #include <asm/page.h>
22 #include <asm/pgtable.h>
23 #include <asm/mtrr.h>
24 #include <asm/sections.h>
25 #include <asm/suspend.h>
26 #include <asm/tlbflush.h>
27
28
29
30
31
32 unsigned long restore_jump_address __visible;
33 unsigned long jump_address_phys;
34
35
36
37
38
39 unsigned long restore_cr3 __visible;
40 unsigned long temp_pgt __visible;
41 unsigned long relocated_restore_code __visible;
42
43
44
45
46 int pfn_is_nosave(unsigned long pfn)
47 {
48 unsigned long nosave_begin_pfn;
49 unsigned long nosave_end_pfn;
50
51 nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
52 nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
53
54 return pfn >= nosave_begin_pfn && pfn < nosave_end_pfn;
55 }
56
57
58 #define MD5_DIGEST_SIZE 16
59
60 struct restore_data_record {
61 unsigned long jump_address;
62 unsigned long jump_address_phys;
63 unsigned long cr3;
64 unsigned long magic;
65 u8 e820_digest[MD5_DIGEST_SIZE];
66 };
67
68 #if IS_BUILTIN(CONFIG_CRYPTO_MD5)
69
70
71
72
73
74
75 static int get_e820_md5(struct e820_table *table, void *buf)
76 {
77 struct crypto_shash *tfm;
78 struct shash_desc *desc;
79 int size;
80 int ret = 0;
81
82 tfm = crypto_alloc_shash("md5", 0, 0);
83 if (IS_ERR(tfm))
84 return -ENOMEM;
85
86 desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
87 GFP_KERNEL);
88 if (!desc) {
89 ret = -ENOMEM;
90 goto free_tfm;
91 }
92
93 desc->tfm = tfm;
94
95 size = offsetof(struct e820_table, entries) +
96 sizeof(struct e820_entry) * table->nr_entries;
97
98 if (crypto_shash_digest(desc, (u8 *)table, size, buf))
99 ret = -EINVAL;
100
101 kzfree(desc);
102
103 free_tfm:
104 crypto_free_shash(tfm);
105 return ret;
106 }
107
108 static int hibernation_e820_save(void *buf)
109 {
110 return get_e820_md5(e820_table_firmware, buf);
111 }
112
113 static bool hibernation_e820_mismatch(void *buf)
114 {
115 int ret;
116 u8 result[MD5_DIGEST_SIZE];
117
118 memset(result, 0, MD5_DIGEST_SIZE);
119
120 if (!memcmp(result, buf, MD5_DIGEST_SIZE))
121 return false;
122
123 ret = get_e820_md5(e820_table_firmware, result);
124 if (ret)
125 return true;
126
127 return memcmp(result, buf, MD5_DIGEST_SIZE) ? true : false;
128 }
129 #else
130 static int hibernation_e820_save(void *buf)
131 {
132 return 0;
133 }
134
135 static bool hibernation_e820_mismatch(void *buf)
136 {
137
138 return false;
139 }
140 #endif
141
142 #ifdef CONFIG_X86_64
143 #define RESTORE_MAGIC 0x23456789ABCDEF01UL
144 #else
145 #define RESTORE_MAGIC 0x12345678UL
146 #endif
147
148
149
150
151
152
153 int arch_hibernation_header_save(void *addr, unsigned int max_size)
154 {
155 struct restore_data_record *rdr = addr;
156
157 if (max_size < sizeof(struct restore_data_record))
158 return -EOVERFLOW;
159 rdr->magic = RESTORE_MAGIC;
160 rdr->jump_address = (unsigned long)restore_registers;
161 rdr->jump_address_phys = __pa_symbol(restore_registers);
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 rdr->cr3 = restore_cr3 & ~CR3_PCID_MASK;
181
182 return hibernation_e820_save(rdr->e820_digest);
183 }
184
185
186
187
188
189
190 int arch_hibernation_header_restore(void *addr)
191 {
192 struct restore_data_record *rdr = addr;
193
194 if (rdr->magic != RESTORE_MAGIC) {
195 pr_crit("Unrecognized hibernate image header format!\n");
196 return -EINVAL;
197 }
198
199 restore_jump_address = rdr->jump_address;
200 jump_address_phys = rdr->jump_address_phys;
201 restore_cr3 = rdr->cr3;
202
203 if (hibernation_e820_mismatch(rdr->e820_digest)) {
204 pr_crit("Hibernate inconsistent memory map detected!\n");
205 return -ENODEV;
206 }
207
208 return 0;
209 }
210
211 int relocate_restore_code(void)
212 {
213 pgd_t *pgd;
214 p4d_t *p4d;
215 pud_t *pud;
216 pmd_t *pmd;
217 pte_t *pte;
218
219 relocated_restore_code = get_safe_page(GFP_ATOMIC);
220 if (!relocated_restore_code)
221 return -ENOMEM;
222
223 memcpy((void *)relocated_restore_code, core_restore_code, PAGE_SIZE);
224
225
226 pgd = (pgd_t *)__va(read_cr3_pa()) +
227 pgd_index(relocated_restore_code);
228 p4d = p4d_offset(pgd, relocated_restore_code);
229 if (p4d_large(*p4d)) {
230 set_p4d(p4d, __p4d(p4d_val(*p4d) & ~_PAGE_NX));
231 goto out;
232 }
233 pud = pud_offset(p4d, relocated_restore_code);
234 if (pud_large(*pud)) {
235 set_pud(pud, __pud(pud_val(*pud) & ~_PAGE_NX));
236 goto out;
237 }
238 pmd = pmd_offset(pud, relocated_restore_code);
239 if (pmd_large(*pmd)) {
240 set_pmd(pmd, __pmd(pmd_val(*pmd) & ~_PAGE_NX));
241 goto out;
242 }
243 pte = pte_offset_kernel(pmd, relocated_restore_code);
244 set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_NX));
245 out:
246 __flush_tlb_all();
247 return 0;
248 }
249
250 int arch_resume_nosmt(void)
251 {
252 int ret = 0;
253
254
255
256
257
258
259
260
261
262
263
264
265 cpu_hotplug_enable();
266 if (cpu_smt_control == CPU_SMT_DISABLED ||
267 cpu_smt_control == CPU_SMT_FORCE_DISABLED) {
268 enum cpuhp_smt_control old = cpu_smt_control;
269
270 ret = cpuhp_smt_enable();
271 if (ret)
272 goto out;
273 ret = cpuhp_smt_disable(old);
274 if (ret)
275 goto out;
276 }
277 out:
278 cpu_hotplug_disable();
279 return ret;
280 }