GNU Linux-libre 4.14.303-gnu1
[releases.git] / arch / x86 / power / hibernate_64.c
1 /*
2  * Hibernation support for x86-64
3  *
4  * Distribute under GPLv2
5  *
6  * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
7  * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
8  * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
9  */
10
11 #include <linux/gfp.h>
12 #include <linux/smp.h>
13 #include <linux/suspend.h>
14 #include <linux/scatterlist.h>
15 #include <linux/kdebug.h>
16 #include <linux/cpu.h>
17
18 #include <crypto/hash.h>
19
20 #include <asm/e820/api.h>
21 #include <asm/init.h>
22 #include <asm/proto.h>
23 #include <asm/page.h>
24 #include <asm/pgtable.h>
25 #include <asm/mtrr.h>
26 #include <asm/sections.h>
27 #include <asm/suspend.h>
28 #include <asm/tlbflush.h>
29
30 /* Defined in hibernate_asm_64.S */
31 extern asmlinkage __visible int restore_image(void);
32
33 /*
34  * Address to jump to in the last phase of restore in order to get to the image
35  * kernel's text (this value is passed in the image header).
36  */
37 unsigned long restore_jump_address __visible;
38 unsigned long jump_address_phys;
39
40 /*
41  * Value of the cr3 register from before the hibernation (this value is passed
42  * in the image header).
43  */
44 unsigned long restore_cr3 __visible;
45
46 unsigned long temp_level4_pgt __visible;
47
48 unsigned long relocated_restore_code __visible;
49
50 static int set_up_temporary_text_mapping(pgd_t *pgd)
51 {
52         pmd_t *pmd;
53         pud_t *pud;
54         p4d_t *p4d;
55
56         /*
57          * The new mapping only has to cover the page containing the image
58          * kernel's entry point (jump_address_phys), because the switch over to
59          * it is carried out by relocated code running from a page allocated
60          * specifically for this purpose and covered by the identity mapping, so
61          * the temporary kernel text mapping is only needed for the final jump.
62          * Moreover, in that mapping the virtual address of the image kernel's
63          * entry point must be the same as its virtual address in the image
64          * kernel (restore_jump_address), so the image kernel's
65          * restore_registers() code doesn't find itself in a different area of
66          * the virtual address space after switching over to the original page
67          * tables used by the image kernel.
68          */
69
70         if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
71                 p4d = (p4d_t *)get_safe_page(GFP_ATOMIC);
72                 if (!p4d)
73                         return -ENOMEM;
74         }
75
76         pud = (pud_t *)get_safe_page(GFP_ATOMIC);
77         if (!pud)
78                 return -ENOMEM;
79
80         pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
81         if (!pmd)
82                 return -ENOMEM;
83
84         set_pmd(pmd + pmd_index(restore_jump_address),
85                 __pmd((jump_address_phys & PMD_MASK) | __PAGE_KERNEL_LARGE_EXEC));
86         set_pud(pud + pud_index(restore_jump_address),
87                 __pud(__pa(pmd) | _KERNPG_TABLE));
88         if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
89                 set_p4d(p4d + p4d_index(restore_jump_address), __p4d(__pa(pud) | _KERNPG_TABLE));
90                 set_pgd(pgd + pgd_index(restore_jump_address), __pgd(__pa(p4d) | _KERNPG_TABLE));
91         } else {
92                 /* No p4d for 4-level paging: point the pgd to the pud page table */
93                 set_pgd(pgd + pgd_index(restore_jump_address), __pgd(__pa(pud) | _KERNPG_TABLE));
94         }
95
96         return 0;
97 }
98
99 static void *alloc_pgt_page(void *context)
100 {
101         return (void *)get_safe_page(GFP_ATOMIC);
102 }
103
104 static int set_up_temporary_mappings(void)
105 {
106         struct x86_mapping_info info = {
107                 .alloc_pgt_page = alloc_pgt_page,
108                 .page_flag      = __PAGE_KERNEL_LARGE_EXEC,
109                 .offset         = __PAGE_OFFSET,
110         };
111         unsigned long mstart, mend;
112         pgd_t *pgd;
113         int result;
114         int i;
115
116         pgd = (pgd_t *)get_safe_page(GFP_ATOMIC);
117         if (!pgd)
118                 return -ENOMEM;
119
120         /* Prepare a temporary mapping for the kernel text */
121         result = set_up_temporary_text_mapping(pgd);
122         if (result)
123                 return result;
124
125         /* Set up the direct mapping from scratch */
126         for (i = 0; i < nr_pfn_mapped; i++) {
127                 mstart = pfn_mapped[i].start << PAGE_SHIFT;
128                 mend   = pfn_mapped[i].end << PAGE_SHIFT;
129
130                 result = kernel_ident_mapping_init(&info, pgd, mstart, mend);
131                 if (result)
132                         return result;
133         }
134
135         temp_level4_pgt = __pa(pgd);
136         return 0;
137 }
138
139 static int relocate_restore_code(void)
140 {
141         pgd_t *pgd;
142         p4d_t *p4d;
143         pud_t *pud;
144         pmd_t *pmd;
145         pte_t *pte;
146
147         relocated_restore_code = get_safe_page(GFP_ATOMIC);
148         if (!relocated_restore_code)
149                 return -ENOMEM;
150
151         memcpy((void *)relocated_restore_code, core_restore_code, PAGE_SIZE);
152
153         /* Make the page containing the relocated code executable */
154         pgd = (pgd_t *)__va(read_cr3_pa()) +
155                 pgd_index(relocated_restore_code);
156         p4d = p4d_offset(pgd, relocated_restore_code);
157         if (p4d_large(*p4d)) {
158                 set_p4d(p4d, __p4d(p4d_val(*p4d) & ~_PAGE_NX));
159                 goto out;
160         }
161         pud = pud_offset(p4d, relocated_restore_code);
162         if (pud_large(*pud)) {
163                 set_pud(pud, __pud(pud_val(*pud) & ~_PAGE_NX));
164                 goto out;
165         }
166         pmd = pmd_offset(pud, relocated_restore_code);
167         if (pmd_large(*pmd)) {
168                 set_pmd(pmd, __pmd(pmd_val(*pmd) & ~_PAGE_NX));
169                 goto out;
170         }
171         pte = pte_offset_kernel(pmd, relocated_restore_code);
172         set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_NX));
173 out:
174         __flush_tlb_all();
175         return 0;
176 }
177
178 asmlinkage int swsusp_arch_resume(void)
179 {
180         int error;
181
182         /* We have got enough memory and from now on we cannot recover */
183         error = set_up_temporary_mappings();
184         if (error)
185                 return error;
186
187         error = relocate_restore_code();
188         if (error)
189                 return error;
190
191         restore_image();
192         return 0;
193 }
194
195 /*
196  *      pfn_is_nosave - check if given pfn is in the 'nosave' section
197  */
198
199 int pfn_is_nosave(unsigned long pfn)
200 {
201         unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
202         unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
203         return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
204 }
205
206 #define MD5_DIGEST_SIZE 16
207
208 struct restore_data_record {
209         unsigned long jump_address;
210         unsigned long jump_address_phys;
211         unsigned long cr3;
212         unsigned long magic;
213         u8 e820_digest[MD5_DIGEST_SIZE];
214 };
215
216 #define RESTORE_MAGIC   0x23456789ABCDEF01UL
217
218 #if IS_BUILTIN(CONFIG_CRYPTO_MD5)
219 /**
220  * get_e820_md5 - calculate md5 according to given e820 table
221  *
222  * @table: the e820 table to be calculated
223  * @buf: the md5 result to be stored to
224  */
225 static int get_e820_md5(struct e820_table *table, void *buf)
226 {
227         struct scatterlist sg;
228         struct crypto_ahash *tfm;
229         int size;
230         int ret = 0;
231
232         tfm = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC);
233         if (IS_ERR(tfm))
234                 return -ENOMEM;
235
236         {
237                 AHASH_REQUEST_ON_STACK(req, tfm);
238                 size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry) * table->nr_entries;
239                 ahash_request_set_tfm(req, tfm);
240                 sg_init_one(&sg, (u8 *)table, size);
241                 ahash_request_set_callback(req, 0, NULL, NULL);
242                 ahash_request_set_crypt(req, &sg, buf, size);
243
244                 if (crypto_ahash_digest(req))
245                         ret = -EINVAL;
246                 ahash_request_zero(req);
247         }
248         crypto_free_ahash(tfm);
249
250         return ret;
251 }
252
253 static int hibernation_e820_save(void *buf)
254 {
255         return get_e820_md5(e820_table_firmware, buf);
256 }
257
258 static bool hibernation_e820_mismatch(void *buf)
259 {
260         int ret;
261         u8 result[MD5_DIGEST_SIZE];
262
263         memset(result, 0, MD5_DIGEST_SIZE);
264         /* If there is no digest in suspend kernel, let it go. */
265         if (!memcmp(result, buf, MD5_DIGEST_SIZE))
266                 return false;
267
268         ret = get_e820_md5(e820_table_firmware, result);
269         if (ret)
270                 return true;
271
272         return memcmp(result, buf, MD5_DIGEST_SIZE) ? true : false;
273 }
274 #else
275 static int hibernation_e820_save(void *buf)
276 {
277         return 0;
278 }
279
280 static bool hibernation_e820_mismatch(void *buf)
281 {
282         /* If md5 is not builtin for restore kernel, let it go. */
283         return false;
284 }
285 #endif
286
287 /**
288  *      arch_hibernation_header_save - populate the architecture specific part
289  *              of a hibernation image header
290  *      @addr: address to save the data at
291  */
292 int arch_hibernation_header_save(void *addr, unsigned int max_size)
293 {
294         struct restore_data_record *rdr = addr;
295
296         if (max_size < sizeof(struct restore_data_record))
297                 return -EOVERFLOW;
298         rdr->jump_address = (unsigned long)restore_registers;
299         rdr->jump_address_phys = __pa_symbol(restore_registers);
300
301         /*
302          * The restore code fixes up CR3 and CR4 in the following sequence:
303          *
304          * [in hibernation asm]
305          * 1. CR3 <= temporary page tables
306          * 2. CR4 <= mmu_cr4_features (from the kernel that restores us)
307          * 3. CR3 <= rdr->cr3
308          * 4. CR4 <= mmu_cr4_features (from us, i.e. the image kernel)
309          * [in restore_processor_state()]
310          * 5. CR4 <= saved CR4
311          * 6. CR3 <= saved CR3
312          *
313          * Our mmu_cr4_features has CR4.PCIDE=0, and toggling
314          * CR4.PCIDE while CR3's PCID bits are nonzero is illegal, so
315          * rdr->cr3 needs to point to valid page tables but must not
316          * have any of the PCID bits set.
317          */
318         rdr->cr3 = restore_cr3 & ~CR3_PCID_MASK;
319
320         rdr->magic = RESTORE_MAGIC;
321
322         return hibernation_e820_save(rdr->e820_digest);
323 }
324
325 /**
326  *      arch_hibernation_header_restore - read the architecture specific data
327  *              from the hibernation image header
328  *      @addr: address to read the data from
329  */
330 int arch_hibernation_header_restore(void *addr)
331 {
332         struct restore_data_record *rdr = addr;
333
334         restore_jump_address = rdr->jump_address;
335         jump_address_phys = rdr->jump_address_phys;
336         restore_cr3 = rdr->cr3;
337
338         if (rdr->magic != RESTORE_MAGIC) {
339                 pr_crit("Unrecognized hibernate image header format!\n");
340                 return -EINVAL;
341         }
342
343         if (hibernation_e820_mismatch(rdr->e820_digest)) {
344                 pr_crit("Hibernate inconsistent memory map detected!\n");
345                 return -ENODEV;
346         }
347
348         return 0;
349 }
350
351 int arch_resume_nosmt(void)
352 {
353         int ret = 0;
354         /*
355          * We reached this while coming out of hibernation. This means
356          * that SMT siblings are sleeping in hlt, as mwait is not safe
357          * against control transition during resume (see comment in
358          * hibernate_resume_nonboot_cpu_disable()).
359          *
360          * If the resumed kernel has SMT disabled, we have to take all the
361          * SMT siblings out of hlt, and offline them again so that they
362          * end up in mwait proper.
363          *
364          * Called with hotplug disabled.
365          */
366         cpu_hotplug_enable();
367         if (cpu_smt_control == CPU_SMT_DISABLED ||
368                         cpu_smt_control == CPU_SMT_FORCE_DISABLED) {
369                 enum cpuhp_smt_control old = cpu_smt_control;
370
371                 ret = cpuhp_smt_enable();
372                 if (ret)
373                         goto out;
374                 ret = cpuhp_smt_disable(old);
375                 if (ret)
376                         goto out;
377         }
378 out:
379         cpu_hotplug_disable();
380         return ret;
381 }