]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/arm64/kernel/hibernate.c
arm64: hibernate: Resume when hibernate image created on non-boot CPU
[mirror_ubuntu-artful-kernel.git] / arch / arm64 / kernel / hibernate.c
CommitLineData
82869ac5
JM
1/*:
2 * Hibernate support specific for ARM64
3 *
4 * Derived from work on ARM hibernation support by:
5 *
6 * Ubuntu project, hibernation support for mach-dove
7 * Copyright (C) 2010 Nokia Corporation (Hiroshi Doyu)
8 * Copyright (C) 2010 Texas Instruments, Inc. (Teerth Reddy et al.)
9 * https://lkml.org/lkml/2010/6/18/4
10 * https://lists.linux-foundation.org/pipermail/linux-pm/2010-June/027422.html
11 * https://patchwork.kernel.org/patch/96442/
12 *
13 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
14 *
15 * License terms: GNU General Public License (GPL) version 2
16 */
17#define pr_fmt(x) "hibernate: " x
8ec058fd 18#include <linux/cpu.h>
82869ac5
JM
19#include <linux/kvm_host.h>
20#include <linux/mm.h>
1fe492ce 21#include <linux/notifier.h>
82869ac5
JM
22#include <linux/pm.h>
23#include <linux/sched.h>
24#include <linux/suspend.h>
25#include <linux/utsname.h>
26#include <linux/version.h>
27
28#include <asm/barrier.h>
29#include <asm/cacheflush.h>
8ec058fd 30#include <asm/cputype.h>
82869ac5
JM
31#include <asm/irqflags.h>
32#include <asm/memory.h>
33#include <asm/mmu_context.h>
34#include <asm/pgalloc.h>
35#include <asm/pgtable.h>
36#include <asm/pgtable-hwdef.h>
37#include <asm/sections.h>
d74b4e4f 38#include <asm/smp.h>
8ec058fd 39#include <asm/smp_plat.h>
82869ac5 40#include <asm/suspend.h>
0194e760 41#include <asm/sysreg.h>
82869ac5
JM
42#include <asm/virt.h>
43
44/*
45 * Hibernate core relies on this value being 0 on resume, and marks it
46 * __nosavedata assuming it will keep the resume kernel's '0' value. This
47 * doesn't happen with either KASLR.
48 *
49 * defined as "__visible int in_suspend __nosavedata" in
50 * kernel/power/hibernate.c
51 */
52extern int in_suspend;
53
54/* Find a symbols alias in the linear map */
55#define LMADDR(x) phys_to_virt(virt_to_phys(x))
56
57/* Do we need to reset el2? */
58#define el2_reset_needed() (is_hyp_mode_available() && !is_kernel_in_hyp_mode())
59
82869ac5
JM
60/* temporary el2 vectors in the __hibernate_exit_text section. */
61extern char hibernate_el2_vectors[];
62
63/* hyp-stub vectors, used to restore el2 during resume from hibernate. */
64extern char __hyp_stub_vectors[];
65
8ec058fd
JM
66/*
67 * The logical cpu number we should resume on, initialised to a non-cpu
68 * number.
69 */
70static int sleep_cpu = -EINVAL;
71
82869ac5
JM
72/*
73 * Values that may not change over hibernate/resume. We put the build number
74 * and date in here so that we guarantee not to resume with a different
75 * kernel.
76 */
77struct arch_hibernate_hdr_invariants {
78 char uts_version[__NEW_UTS_LEN + 1];
79};
80
81/* These values need to be know across a hibernate/restore. */
82static struct arch_hibernate_hdr {
83 struct arch_hibernate_hdr_invariants invariants;
84
85 /* These are needed to find the relocated kernel if built with kaslr */
86 phys_addr_t ttbr1_el1;
87 void (*reenter_kernel)(void);
88
89 /*
90 * We need to know where the __hyp_stub_vectors are after restore to
91 * re-configure el2.
92 */
93 phys_addr_t __hyp_stub_vectors;
8ec058fd
JM
94
95 u64 sleep_cpu_mpidr;
82869ac5
JM
96} resume_hdr;
97
98static inline void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i)
99{
100 memset(i, 0, sizeof(*i));
101 memcpy(i->uts_version, init_utsname()->version, sizeof(i->uts_version));
102}
103
104int pfn_is_nosave(unsigned long pfn)
105{
106 unsigned long nosave_begin_pfn = virt_to_pfn(&__nosave_begin);
107 unsigned long nosave_end_pfn = virt_to_pfn(&__nosave_end - 1);
108
109 return (pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn);
110}
111
112void notrace save_processor_state(void)
113{
114 WARN_ON(num_online_cpus() != 1);
115}
116
117void notrace restore_processor_state(void)
118{
119}
120
121int arch_hibernation_header_save(void *addr, unsigned int max_size)
122{
123 struct arch_hibernate_hdr *hdr = addr;
124
125 if (max_size < sizeof(*hdr))
126 return -EOVERFLOW;
127
128 arch_hdr_invariants(&hdr->invariants);
129 hdr->ttbr1_el1 = virt_to_phys(swapper_pg_dir);
130 hdr->reenter_kernel = _cpu_resume;
131
132 /* We can't use __hyp_get_vectors() because kvm may still be loaded */
133 if (el2_reset_needed())
134 hdr->__hyp_stub_vectors = virt_to_phys(__hyp_stub_vectors);
135 else
136 hdr->__hyp_stub_vectors = 0;
137
8ec058fd
JM
138 /* Save the mpidr of the cpu we called cpu_suspend() on... */
139 if (sleep_cpu < 0) {
140 pr_err("Failing to hibernate on an unkown CPU.\n");
141 return -ENODEV;
142 }
143 hdr->sleep_cpu_mpidr = cpu_logical_map(sleep_cpu);
144 pr_info("Hibernating on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
145 hdr->sleep_cpu_mpidr);
146
82869ac5
JM
147 return 0;
148}
149EXPORT_SYMBOL(arch_hibernation_header_save);
150
151int arch_hibernation_header_restore(void *addr)
152{
8ec058fd 153 int ret;
82869ac5
JM
154 struct arch_hibernate_hdr_invariants invariants;
155 struct arch_hibernate_hdr *hdr = addr;
156
157 arch_hdr_invariants(&invariants);
158 if (memcmp(&hdr->invariants, &invariants, sizeof(invariants))) {
159 pr_crit("Hibernate image not generated by this kernel!\n");
160 return -EINVAL;
161 }
162
8ec058fd
JM
163 sleep_cpu = get_logical_index(hdr->sleep_cpu_mpidr);
164 pr_info("Hibernated on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
165 hdr->sleep_cpu_mpidr);
166 if (sleep_cpu < 0) {
167 pr_crit("Hibernated on a CPU not known to this kernel!\n");
168 sleep_cpu = -EINVAL;
169 return -EINVAL;
170 }
171 if (!cpu_online(sleep_cpu)) {
172 pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n");
173 ret = cpu_up(sleep_cpu);
174 if (ret) {
175 pr_err("Failed to bring hibernate-CPU up!\n");
176 sleep_cpu = -EINVAL;
177 return ret;
178 }
179 }
180
82869ac5
JM
181 resume_hdr = *hdr;
182
183 return 0;
184}
185EXPORT_SYMBOL(arch_hibernation_header_restore);
186
187/*
188 * Copies length bytes, starting at src_start into an new page,
189 * perform cache maintentance, then maps it at the specified address low
190 * address as executable.
191 *
192 * This is used by hibernate to copy the code it needs to execute when
193 * overwriting the kernel text. This function generates a new set of page
194 * tables, which it loads into ttbr0.
195 *
196 * Length is provided as we probably only want 4K of data, even on a 64K
197 * page system.
198 */
199static int create_safe_exec_page(void *src_start, size_t length,
200 unsigned long dst_addr,
201 phys_addr_t *phys_dst_addr,
202 void *(*allocator)(gfp_t mask),
203 gfp_t mask)
204{
205 int rc = 0;
206 pgd_t *pgd;
207 pud_t *pud;
208 pmd_t *pmd;
209 pte_t *pte;
210 unsigned long dst = (unsigned long)allocator(mask);
211
212 if (!dst) {
213 rc = -ENOMEM;
214 goto out;
215 }
216
217 memcpy((void *)dst, src_start, length);
218 flush_icache_range(dst, dst + length);
219
220 pgd = pgd_offset_raw(allocator(mask), dst_addr);
221 if (pgd_none(*pgd)) {
222 pud = allocator(mask);
223 if (!pud) {
224 rc = -ENOMEM;
225 goto out;
226 }
227 pgd_populate(&init_mm, pgd, pud);
228 }
229
230 pud = pud_offset(pgd, dst_addr);
231 if (pud_none(*pud)) {
232 pmd = allocator(mask);
233 if (!pmd) {
234 rc = -ENOMEM;
235 goto out;
236 }
237 pud_populate(&init_mm, pud, pmd);
238 }
239
240 pmd = pmd_offset(pud, dst_addr);
241 if (pmd_none(*pmd)) {
242 pte = allocator(mask);
243 if (!pte) {
244 rc = -ENOMEM;
245 goto out;
246 }
247 pmd_populate_kernel(&init_mm, pmd, pte);
248 }
249
250 pte = pte_offset_kernel(pmd, dst_addr);
251 set_pte(pte, __pte(virt_to_phys((void *)dst) |
252 pgprot_val(PAGE_KERNEL_EXEC)));
253
0194e760
MR
254 /*
255 * Load our new page tables. A strict BBM approach requires that we
256 * ensure that TLBs are free of any entries that may overlap with the
257 * global mappings we are about to install.
258 *
259 * For a real hibernate/resume cycle TTBR0 currently points to a zero
260 * page, but TLBs may contain stale ASID-tagged entries (e.g. for EFI
261 * runtime services), while for a userspace-driven test_resume cycle it
262 * points to userspace page tables (and we must point it at a zero page
263 * ourselves). Elsewhere we only (un)install the idmap with preemption
264 * disabled, so T0SZ should be as required regardless.
265 */
266 cpu_set_reserved_ttbr0();
267 local_flush_tlb_all();
268 write_sysreg(virt_to_phys(pgd), ttbr0_el1);
269 isb();
82869ac5
JM
270
271 *phys_dst_addr = virt_to_phys((void *)dst);
272
273out:
274 return rc;
275}
276
5ebe3a44 277#define dcache_clean_range(start, end) __flush_dcache_area(start, (end - start))
82869ac5
JM
278
279int swsusp_arch_suspend(void)
280{
281 int ret = 0;
282 unsigned long flags;
283 struct sleep_stack_data state;
284
d74b4e4f
JM
285 if (cpus_are_stuck_in_kernel()) {
286 pr_err("Can't hibernate: no mechanism to offline secondary CPUs.\n");
287 return -EBUSY;
288 }
289
82869ac5
JM
290 local_dbg_save(flags);
291
292 if (__cpu_suspend_enter(&state)) {
8ec058fd 293 sleep_cpu = smp_processor_id();
82869ac5
JM
294 ret = swsusp_save();
295 } else {
5ebe3a44
JM
296 /* Clean kernel core startup/idle code to PoC*/
297 dcache_clean_range(__mmuoff_data_start, __mmuoff_data_end);
298 dcache_clean_range(__idmap_text_start, __idmap_text_end);
299
300 /* Clean kvm setup code to PoC? */
301 if (el2_reset_needed())
302 dcache_clean_range(__hyp_idmap_text_start, __hyp_idmap_text_end);
82869ac5
JM
303
304 /*
305 * Tell the hibernation core that we've just restored
306 * the memory
307 */
308 in_suspend = 0;
309
8ec058fd 310 sleep_cpu = -EINVAL;
82869ac5
JM
311 __cpu_suspend_exit();
312 }
313
314 local_dbg_restore(flags);
315
316 return ret;
317}
318
5ebe3a44
JM
319static void _copy_pte(pte_t *dst_pte, pte_t *src_pte, unsigned long addr)
320{
321 pte_t pte = *src_pte;
322
323 if (pte_valid(pte)) {
324 /*
325 * Resume will overwrite areas that may be marked
326 * read only (code, rodata). Clear the RDONLY bit from
327 * the temporary mappings we use during restore.
328 */
329 set_pte(dst_pte, pte_clear_rdonly(pte));
330 } else if (debug_pagealloc_enabled() && !pte_none(pte)) {
331 /*
332 * debug_pagealloc will removed the PTE_VALID bit if
333 * the page isn't in use by the resume kernel. It may have
334 * been in use by the original kernel, in which case we need
335 * to put it back in our copy to do the restore.
336 *
337 * Before marking this entry valid, check the pfn should
338 * be mapped.
339 */
340 BUG_ON(!pfn_valid(pte_pfn(pte)));
341
342 set_pte(dst_pte, pte_mkpresent(pte_clear_rdonly(pte)));
343 }
344}
345
82869ac5
JM
346static int copy_pte(pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long start,
347 unsigned long end)
348{
349 pte_t *src_pte;
350 pte_t *dst_pte;
351 unsigned long addr = start;
352
353 dst_pte = (pte_t *)get_safe_page(GFP_ATOMIC);
354 if (!dst_pte)
355 return -ENOMEM;
356 pmd_populate_kernel(&init_mm, dst_pmd, dst_pte);
357 dst_pte = pte_offset_kernel(dst_pmd, start);
358
359 src_pte = pte_offset_kernel(src_pmd, start);
360 do {
5ebe3a44 361 _copy_pte(dst_pte, src_pte, addr);
82869ac5
JM
362 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
363
364 return 0;
365}
366
367static int copy_pmd(pud_t *dst_pud, pud_t *src_pud, unsigned long start,
368 unsigned long end)
369{
370 pmd_t *src_pmd;
371 pmd_t *dst_pmd;
372 unsigned long next;
373 unsigned long addr = start;
374
375 if (pud_none(*dst_pud)) {
376 dst_pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
377 if (!dst_pmd)
378 return -ENOMEM;
379 pud_populate(&init_mm, dst_pud, dst_pmd);
380 }
381 dst_pmd = pmd_offset(dst_pud, start);
382
383 src_pmd = pmd_offset(src_pud, start);
384 do {
385 next = pmd_addr_end(addr, end);
386 if (pmd_none(*src_pmd))
387 continue;
388 if (pmd_table(*src_pmd)) {
389 if (copy_pte(dst_pmd, src_pmd, addr, next))
390 return -ENOMEM;
391 } else {
392 set_pmd(dst_pmd,
393 __pmd(pmd_val(*src_pmd) & ~PMD_SECT_RDONLY));
394 }
395 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
396
397 return 0;
398}
399
400static int copy_pud(pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long start,
401 unsigned long end)
402{
403 pud_t *dst_pud;
404 pud_t *src_pud;
405 unsigned long next;
406 unsigned long addr = start;
407
408 if (pgd_none(*dst_pgd)) {
409 dst_pud = (pud_t *)get_safe_page(GFP_ATOMIC);
410 if (!dst_pud)
411 return -ENOMEM;
412 pgd_populate(&init_mm, dst_pgd, dst_pud);
413 }
414 dst_pud = pud_offset(dst_pgd, start);
415
416 src_pud = pud_offset(src_pgd, start);
417 do {
418 next = pud_addr_end(addr, end);
419 if (pud_none(*src_pud))
420 continue;
421 if (pud_table(*(src_pud))) {
422 if (copy_pmd(dst_pud, src_pud, addr, next))
423 return -ENOMEM;
424 } else {
425 set_pud(dst_pud,
426 __pud(pud_val(*src_pud) & ~PMD_SECT_RDONLY));
427 }
428 } while (dst_pud++, src_pud++, addr = next, addr != end);
429
430 return 0;
431}
432
433static int copy_page_tables(pgd_t *dst_pgd, unsigned long start,
434 unsigned long end)
435{
436 unsigned long next;
437 unsigned long addr = start;
438 pgd_t *src_pgd = pgd_offset_k(start);
439
440 dst_pgd = pgd_offset_raw(dst_pgd, start);
441 do {
442 next = pgd_addr_end(addr, end);
443 if (pgd_none(*src_pgd))
444 continue;
445 if (copy_pud(dst_pgd, src_pgd, addr, next))
446 return -ENOMEM;
447 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
448
449 return 0;
450}
451
452/*
453 * Setup then Resume from the hibernate image using swsusp_arch_suspend_exit().
454 *
455 * Memory allocated by get_safe_page() will be dealt with by the hibernate code,
456 * we don't need to free it here.
457 */
458int swsusp_arch_resume(void)
459{
460 int rc = 0;
461 void *zero_page;
462 size_t exit_size;
463 pgd_t *tmp_pg_dir;
464 void *lm_restore_pblist;
465 phys_addr_t phys_hibernate_exit;
466 void __noreturn (*hibernate_exit)(phys_addr_t, phys_addr_t, void *,
467 void *, phys_addr_t, phys_addr_t);
468
dfbca61a
MR
469 /*
470 * Restoring the memory image will overwrite the ttbr1 page tables.
471 * Create a second copy of just the linear map, and use this when
472 * restoring.
473 */
474 tmp_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
475 if (!tmp_pg_dir) {
476 pr_err("Failed to allocate memory for temporary page tables.");
477 rc = -ENOMEM;
478 goto out;
479 }
480 rc = copy_page_tables(tmp_pg_dir, PAGE_OFFSET, 0);
481 if (rc)
482 goto out;
483
484 /*
485 * Since we only copied the linear map, we need to find restore_pblist's
486 * linear map address.
487 */
488 lm_restore_pblist = LMADDR(restore_pblist);
489
490 /*
491 * We need a zero page that is zero before & after resume in order to
492 * to break before make on the ttbr1 page tables.
493 */
494 zero_page = (void *)get_safe_page(GFP_ATOMIC);
495 if (!zero_page) {
496 pr_err("Failed to allocate zero page.");
497 rc = -ENOMEM;
498 goto out;
499 }
500
82869ac5
JM
501 /*
502 * Locate the exit code in the bottom-but-one page, so that *NULL
503 * still has disastrous affects.
504 */
505 hibernate_exit = (void *)PAGE_SIZE;
506 exit_size = __hibernate_exit_text_end - __hibernate_exit_text_start;
507 /*
508 * Copy swsusp_arch_suspend_exit() to a safe page. This will generate
509 * a new set of ttbr0 page tables and load them.
510 */
511 rc = create_safe_exec_page(__hibernate_exit_text_start, exit_size,
512 (unsigned long)hibernate_exit,
513 &phys_hibernate_exit,
514 (void *)get_safe_page, GFP_ATOMIC);
515 if (rc) {
516 pr_err("Failed to create safe executable page for hibernate_exit code.");
517 goto out;
518 }
519
520 /*
521 * The hibernate exit text contains a set of el2 vectors, that will
522 * be executed at el2 with the mmu off in order to reload hyp-stub.
523 */
524 __flush_dcache_area(hibernate_exit, exit_size);
525
82869ac5
JM
526 /*
527 * KASLR will cause the el2 vectors to be in a different location in
528 * the resumed kernel. Load hibernate's temporary copy into el2.
529 *
530 * We can skip this step if we booted at EL1, or are running with VHE.
531 */
532 if (el2_reset_needed()) {
533 phys_addr_t el2_vectors = phys_hibernate_exit; /* base */
534 el2_vectors += hibernate_el2_vectors -
535 __hibernate_exit_text_start; /* offset */
536
537 __hyp_set_vectors(el2_vectors);
538 }
539
82869ac5
JM
540 hibernate_exit(virt_to_phys(tmp_pg_dir), resume_hdr.ttbr1_el1,
541 resume_hdr.reenter_kernel, lm_restore_pblist,
542 resume_hdr.__hyp_stub_vectors, virt_to_phys(zero_page));
543
544out:
545 return rc;
546}
1fe492ce
JM
547
548static int check_boot_cpu_online_pm_callback(struct notifier_block *nb,
549 unsigned long action, void *ptr)
550{
551 if (action == PM_HIBERNATION_PREPARE &&
552 cpumask_first(cpu_online_mask) != 0) {
553 pr_warn("CPU0 is offline.\n");
554 return notifier_from_errno(-ENODEV);
555 }
556
557 return NOTIFY_OK;
558}
559
560static int __init check_boot_cpu_online_init(void)
561{
562 /*
563 * Set this pm_notifier callback with a lower priority than
564 * cpu_hotplug_pm_callback, so that cpu_hotplug_pm_callback will be
565 * called earlier to disable cpu hotplug before the cpu online check.
566 */
567 pm_notifier(check_boot_cpu_online_pm_callback, -INT_MAX);
568
569 return 0;
570}
571core_initcall(check_boot_cpu_online_init);
8ec058fd
JM
572
573int hibernate_resume_nonboot_cpu_disable(void)
574{
575 if (sleep_cpu < 0) {
576 pr_err("Failing to resume from hibernate on an unkown CPU.\n");
577 return -ENODEV;
578 }
579
580 return freeze_secondary_cpus(sleep_cpu);
581}