blob: db603cdd44a4681b48d5a44ba9c6cf0fcede6fe5 [file] [log] [blame]
James Morse82869ac2016-04-27 17:47:12 +01001/*:
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
James Morse8ec058f2016-08-17 13:50:26 +010018#include <linux/cpu.h>
James Morse82869ac2016-04-27 17:47:12 +010019#include <linux/kvm_host.h>
20#include <linux/mm.h>
21#include <linux/pm.h>
22#include <linux/sched.h>
23#include <linux/suspend.h>
24#include <linux/utsname.h>
25#include <linux/version.h>
26
27#include <asm/barrier.h>
28#include <asm/cacheflush.h>
James Morse8ec058f2016-08-17 13:50:26 +010029#include <asm/cputype.h>
James Morse82869ac2016-04-27 17:47:12 +010030#include <asm/irqflags.h>
31#include <asm/memory.h>
32#include <asm/mmu_context.h>
33#include <asm/pgalloc.h>
34#include <asm/pgtable.h>
35#include <asm/pgtable-hwdef.h>
36#include <asm/sections.h>
James Morsed74b4e42016-06-22 10:06:13 +010037#include <asm/smp.h>
James Morse8ec058f2016-08-17 13:50:26 +010038#include <asm/smp_plat.h>
James Morse82869ac2016-04-27 17:47:12 +010039#include <asm/suspend.h>
Mark Rutland0194e762016-08-11 14:11:05 +010040#include <asm/sysreg.h>
James Morse82869ac2016-04-27 17:47:12 +010041#include <asm/virt.h>
42
43/*
44 * Hibernate core relies on this value being 0 on resume, and marks it
45 * __nosavedata assuming it will keep the resume kernel's '0' value. This
46 * doesn't happen with either KASLR.
47 *
48 * defined as "__visible int in_suspend __nosavedata" in
49 * kernel/power/hibernate.c
50 */
51extern int in_suspend;
52
James Morse82869ac2016-04-27 17:47:12 +010053/* Do we need to reset el2? */
54#define el2_reset_needed() (is_hyp_mode_available() && !is_kernel_in_hyp_mode())
55
James Morse82869ac2016-04-27 17:47:12 +010056/* temporary el2 vectors in the __hibernate_exit_text section. */
57extern char hibernate_el2_vectors[];
58
59/* hyp-stub vectors, used to restore el2 during resume from hibernate. */
60extern char __hyp_stub_vectors[];
61
62/*
James Morse8ec058f2016-08-17 13:50:26 +010063 * The logical cpu number we should resume on, initialised to a non-cpu
64 * number.
65 */
66static int sleep_cpu = -EINVAL;
67
68/*
James Morse82869ac2016-04-27 17:47:12 +010069 * Values that may not change over hibernate/resume. We put the build number
70 * and date in here so that we guarantee not to resume with a different
71 * kernel.
72 */
73struct arch_hibernate_hdr_invariants {
74 char uts_version[__NEW_UTS_LEN + 1];
75};
76
77/* These values need to be know across a hibernate/restore. */
78static struct arch_hibernate_hdr {
79 struct arch_hibernate_hdr_invariants invariants;
80
81 /* These are needed to find the relocated kernel if built with kaslr */
82 phys_addr_t ttbr1_el1;
83 void (*reenter_kernel)(void);
84
85 /*
86 * We need to know where the __hyp_stub_vectors are after restore to
87 * re-configure el2.
88 */
89 phys_addr_t __hyp_stub_vectors;
James Morse8ec058f2016-08-17 13:50:26 +010090
91 u64 sleep_cpu_mpidr;
James Morse82869ac2016-04-27 17:47:12 +010092} resume_hdr;
93
94static inline void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i)
95{
96 memset(i, 0, sizeof(*i));
97 memcpy(i->uts_version, init_utsname()->version, sizeof(i->uts_version));
98}
99
100int pfn_is_nosave(unsigned long pfn)
101{
Laura Abbottf8fee94e2017-01-10 13:35:49 -0800102 unsigned long nosave_begin_pfn = sym_to_pfn(&__nosave_begin);
103 unsigned long nosave_end_pfn = sym_to_pfn(&__nosave_end - 1);
James Morse82869ac2016-04-27 17:47:12 +0100104
105 return (pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn);
106}
107
108void notrace save_processor_state(void)
109{
110 WARN_ON(num_online_cpus() != 1);
111}
112
113void notrace restore_processor_state(void)
114{
115}
116
117int arch_hibernation_header_save(void *addr, unsigned int max_size)
118{
119 struct arch_hibernate_hdr *hdr = addr;
120
121 if (max_size < sizeof(*hdr))
122 return -EOVERFLOW;
123
124 arch_hdr_invariants(&hdr->invariants);
Laura Abbottf8fee94e2017-01-10 13:35:49 -0800125 hdr->ttbr1_el1 = __pa_symbol(swapper_pg_dir);
James Morse82869ac2016-04-27 17:47:12 +0100126 hdr->reenter_kernel = _cpu_resume;
127
128 /* We can't use __hyp_get_vectors() because kvm may still be loaded */
129 if (el2_reset_needed())
Laura Abbottf8fee94e2017-01-10 13:35:49 -0800130 hdr->__hyp_stub_vectors = __pa_symbol(__hyp_stub_vectors);
James Morse82869ac2016-04-27 17:47:12 +0100131 else
132 hdr->__hyp_stub_vectors = 0;
133
James Morse8ec058f2016-08-17 13:50:26 +0100134 /* Save the mpidr of the cpu we called cpu_suspend() on... */
135 if (sleep_cpu < 0) {
136 pr_err("Failing to hibernate on an unkown CPU.\n");
137 return -ENODEV;
138 }
139 hdr->sleep_cpu_mpidr = cpu_logical_map(sleep_cpu);
140 pr_info("Hibernating on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
141 hdr->sleep_cpu_mpidr);
142
James Morse82869ac2016-04-27 17:47:12 +0100143 return 0;
144}
145EXPORT_SYMBOL(arch_hibernation_header_save);
146
147int arch_hibernation_header_restore(void *addr)
148{
James Morse8ec058f2016-08-17 13:50:26 +0100149 int ret;
James Morse82869ac2016-04-27 17:47:12 +0100150 struct arch_hibernate_hdr_invariants invariants;
151 struct arch_hibernate_hdr *hdr = addr;
152
153 arch_hdr_invariants(&invariants);
154 if (memcmp(&hdr->invariants, &invariants, sizeof(invariants))) {
155 pr_crit("Hibernate image not generated by this kernel!\n");
156 return -EINVAL;
157 }
158
James Morse8ec058f2016-08-17 13:50:26 +0100159 sleep_cpu = get_logical_index(hdr->sleep_cpu_mpidr);
160 pr_info("Hibernated on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
161 hdr->sleep_cpu_mpidr);
162 if (sleep_cpu < 0) {
163 pr_crit("Hibernated on a CPU not known to this kernel!\n");
164 sleep_cpu = -EINVAL;
165 return -EINVAL;
166 }
167 if (!cpu_online(sleep_cpu)) {
168 pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n");
169 ret = cpu_up(sleep_cpu);
170 if (ret) {
171 pr_err("Failed to bring hibernate-CPU up!\n");
172 sleep_cpu = -EINVAL;
173 return ret;
174 }
175 }
176
James Morse82869ac2016-04-27 17:47:12 +0100177 resume_hdr = *hdr;
178
179 return 0;
180}
181EXPORT_SYMBOL(arch_hibernation_header_restore);
182
183/*
184 * Copies length bytes, starting at src_start into an new page,
185 * perform cache maintentance, then maps it at the specified address low
186 * address as executable.
187 *
188 * This is used by hibernate to copy the code it needs to execute when
189 * overwriting the kernel text. This function generates a new set of page
190 * tables, which it loads into ttbr0.
191 *
192 * Length is provided as we probably only want 4K of data, even on a 64K
193 * page system.
194 */
195static int create_safe_exec_page(void *src_start, size_t length,
196 unsigned long dst_addr,
197 phys_addr_t *phys_dst_addr,
198 void *(*allocator)(gfp_t mask),
199 gfp_t mask)
200{
201 int rc = 0;
202 pgd_t *pgd;
203 pud_t *pud;
204 pmd_t *pmd;
205 pte_t *pte;
206 unsigned long dst = (unsigned long)allocator(mask);
207
208 if (!dst) {
209 rc = -ENOMEM;
210 goto out;
211 }
212
213 memcpy((void *)dst, src_start, length);
214 flush_icache_range(dst, dst + length);
215
216 pgd = pgd_offset_raw(allocator(mask), dst_addr);
217 if (pgd_none(*pgd)) {
218 pud = allocator(mask);
219 if (!pud) {
220 rc = -ENOMEM;
221 goto out;
222 }
223 pgd_populate(&init_mm, pgd, pud);
224 }
225
226 pud = pud_offset(pgd, dst_addr);
227 if (pud_none(*pud)) {
228 pmd = allocator(mask);
229 if (!pmd) {
230 rc = -ENOMEM;
231 goto out;
232 }
233 pud_populate(&init_mm, pud, pmd);
234 }
235
236 pmd = pmd_offset(pud, dst_addr);
237 if (pmd_none(*pmd)) {
238 pte = allocator(mask);
239 if (!pte) {
240 rc = -ENOMEM;
241 goto out;
242 }
243 pmd_populate_kernel(&init_mm, pmd, pte);
244 }
245
246 pte = pte_offset_kernel(pmd, dst_addr);
247 set_pte(pte, __pte(virt_to_phys((void *)dst) |
248 pgprot_val(PAGE_KERNEL_EXEC)));
249
Mark Rutland0194e762016-08-11 14:11:05 +0100250 /*
251 * Load our new page tables. A strict BBM approach requires that we
252 * ensure that TLBs are free of any entries that may overlap with the
253 * global mappings we are about to install.
254 *
255 * For a real hibernate/resume cycle TTBR0 currently points to a zero
256 * page, but TLBs may contain stale ASID-tagged entries (e.g. for EFI
257 * runtime services), while for a userspace-driven test_resume cycle it
258 * points to userspace page tables (and we must point it at a zero page
259 * ourselves). Elsewhere we only (un)install the idmap with preemption
260 * disabled, so T0SZ should be as required regardless.
261 */
262 cpu_set_reserved_ttbr0();
263 local_flush_tlb_all();
264 write_sysreg(virt_to_phys(pgd), ttbr0_el1);
265 isb();
James Morse82869ac2016-04-27 17:47:12 +0100266
267 *phys_dst_addr = virt_to_phys((void *)dst);
268
269out:
270 return rc;
271}
272
James Morse5ebe3a42016-08-24 18:27:30 +0100273#define dcache_clean_range(start, end) __flush_dcache_area(start, (end - start))
James Morse82869ac2016-04-27 17:47:12 +0100274
275int swsusp_arch_suspend(void)
276{
277 int ret = 0;
278 unsigned long flags;
279 struct sleep_stack_data state;
280
James Morsed74b4e42016-06-22 10:06:13 +0100281 if (cpus_are_stuck_in_kernel()) {
282 pr_err("Can't hibernate: no mechanism to offline secondary CPUs.\n");
283 return -EBUSY;
284 }
285
James Morse82869ac2016-04-27 17:47:12 +0100286 local_dbg_save(flags);
287
288 if (__cpu_suspend_enter(&state)) {
James Morse8ec058f2016-08-17 13:50:26 +0100289 sleep_cpu = smp_processor_id();
James Morse82869ac2016-04-27 17:47:12 +0100290 ret = swsusp_save();
291 } else {
James Morse5ebe3a42016-08-24 18:27:30 +0100292 /* Clean kernel core startup/idle code to PoC*/
293 dcache_clean_range(__mmuoff_data_start, __mmuoff_data_end);
294 dcache_clean_range(__idmap_text_start, __idmap_text_end);
295
296 /* Clean kvm setup code to PoC? */
297 if (el2_reset_needed())
298 dcache_clean_range(__hyp_idmap_text_start, __hyp_idmap_text_end);
James Morse82869ac2016-04-27 17:47:12 +0100299
300 /*
301 * Tell the hibernation core that we've just restored
302 * the memory
303 */
304 in_suspend = 0;
305
James Morse8ec058f2016-08-17 13:50:26 +0100306 sleep_cpu = -EINVAL;
James Morse82869ac2016-04-27 17:47:12 +0100307 __cpu_suspend_exit();
Marc Zyngierd8fbc842018-07-20 10:56:28 +0100308
309 /*
310 * Just in case the boot kernel did turn the SSBD
311 * mitigation off behind our back, let's set the state
312 * to what we expect it to be.
313 */
314 switch (arm64_get_ssbd_state()) {
315 case ARM64_SSBD_FORCE_ENABLE:
316 case ARM64_SSBD_KERNEL:
317 arm64_set_ssbd_mitigation(true);
318 }
James Morse82869ac2016-04-27 17:47:12 +0100319 }
320
321 local_dbg_restore(flags);
322
323 return ret;
324}
325
James Morse5ebe3a42016-08-24 18:27:30 +0100326static void _copy_pte(pte_t *dst_pte, pte_t *src_pte, unsigned long addr)
327{
328 pte_t pte = *src_pte;
329
330 if (pte_valid(pte)) {
331 /*
332 * Resume will overwrite areas that may be marked
333 * read only (code, rodata). Clear the RDONLY bit from
334 * the temporary mappings we use during restore.
335 */
336 set_pte(dst_pte, pte_clear_rdonly(pte));
337 } else if (debug_pagealloc_enabled() && !pte_none(pte)) {
338 /*
339 * debug_pagealloc will removed the PTE_VALID bit if
340 * the page isn't in use by the resume kernel. It may have
341 * been in use by the original kernel, in which case we need
342 * to put it back in our copy to do the restore.
343 *
344 * Before marking this entry valid, check the pfn should
345 * be mapped.
346 */
347 BUG_ON(!pfn_valid(pte_pfn(pte)));
348
349 set_pte(dst_pte, pte_mkpresent(pte_clear_rdonly(pte)));
350 }
351}
352
James Morse82869ac2016-04-27 17:47:12 +0100353static int copy_pte(pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long start,
354 unsigned long end)
355{
356 pte_t *src_pte;
357 pte_t *dst_pte;
358 unsigned long addr = start;
359
360 dst_pte = (pte_t *)get_safe_page(GFP_ATOMIC);
361 if (!dst_pte)
362 return -ENOMEM;
363 pmd_populate_kernel(&init_mm, dst_pmd, dst_pte);
364 dst_pte = pte_offset_kernel(dst_pmd, start);
365
366 src_pte = pte_offset_kernel(src_pmd, start);
367 do {
James Morse5ebe3a42016-08-24 18:27:30 +0100368 _copy_pte(dst_pte, src_pte, addr);
James Morse82869ac2016-04-27 17:47:12 +0100369 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
370
371 return 0;
372}
373
374static int copy_pmd(pud_t *dst_pud, pud_t *src_pud, unsigned long start,
375 unsigned long end)
376{
377 pmd_t *src_pmd;
378 pmd_t *dst_pmd;
379 unsigned long next;
380 unsigned long addr = start;
381
382 if (pud_none(*dst_pud)) {
383 dst_pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
384 if (!dst_pmd)
385 return -ENOMEM;
386 pud_populate(&init_mm, dst_pud, dst_pmd);
387 }
388 dst_pmd = pmd_offset(dst_pud, start);
389
390 src_pmd = pmd_offset(src_pud, start);
391 do {
392 next = pmd_addr_end(addr, end);
393 if (pmd_none(*src_pmd))
394 continue;
395 if (pmd_table(*src_pmd)) {
396 if (copy_pte(dst_pmd, src_pmd, addr, next))
397 return -ENOMEM;
398 } else {
399 set_pmd(dst_pmd,
400 __pmd(pmd_val(*src_pmd) & ~PMD_SECT_RDONLY));
401 }
402 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
403
404 return 0;
405}
406
407static int copy_pud(pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long start,
408 unsigned long end)
409{
410 pud_t *dst_pud;
411 pud_t *src_pud;
412 unsigned long next;
413 unsigned long addr = start;
414
415 if (pgd_none(*dst_pgd)) {
416 dst_pud = (pud_t *)get_safe_page(GFP_ATOMIC);
417 if (!dst_pud)
418 return -ENOMEM;
419 pgd_populate(&init_mm, dst_pgd, dst_pud);
420 }
421 dst_pud = pud_offset(dst_pgd, start);
422
423 src_pud = pud_offset(src_pgd, start);
424 do {
425 next = pud_addr_end(addr, end);
426 if (pud_none(*src_pud))
427 continue;
428 if (pud_table(*(src_pud))) {
429 if (copy_pmd(dst_pud, src_pud, addr, next))
430 return -ENOMEM;
431 } else {
432 set_pud(dst_pud,
433 __pud(pud_val(*src_pud) & ~PMD_SECT_RDONLY));
434 }
435 } while (dst_pud++, src_pud++, addr = next, addr != end);
436
437 return 0;
438}
439
440static int copy_page_tables(pgd_t *dst_pgd, unsigned long start,
441 unsigned long end)
442{
443 unsigned long next;
444 unsigned long addr = start;
445 pgd_t *src_pgd = pgd_offset_k(start);
446
447 dst_pgd = pgd_offset_raw(dst_pgd, start);
448 do {
449 next = pgd_addr_end(addr, end);
450 if (pgd_none(*src_pgd))
451 continue;
452 if (copy_pud(dst_pgd, src_pgd, addr, next))
453 return -ENOMEM;
454 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
455
456 return 0;
457}
458
459/*
460 * Setup then Resume from the hibernate image using swsusp_arch_suspend_exit().
461 *
462 * Memory allocated by get_safe_page() will be dealt with by the hibernate code,
463 * we don't need to free it here.
464 */
465int swsusp_arch_resume(void)
466{
467 int rc = 0;
468 void *zero_page;
469 size_t exit_size;
470 pgd_t *tmp_pg_dir;
James Morse82869ac2016-04-27 17:47:12 +0100471 phys_addr_t phys_hibernate_exit;
472 void __noreturn (*hibernate_exit)(phys_addr_t, phys_addr_t, void *,
473 void *, phys_addr_t, phys_addr_t);
474
475 /*
Mark Rutlanddfbca612016-08-11 14:11:06 +0100476 * Restoring the memory image will overwrite the ttbr1 page tables.
477 * Create a second copy of just the linear map, and use this when
478 * restoring.
479 */
480 tmp_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
481 if (!tmp_pg_dir) {
482 pr_err("Failed to allocate memory for temporary page tables.");
483 rc = -ENOMEM;
484 goto out;
485 }
486 rc = copy_page_tables(tmp_pg_dir, PAGE_OFFSET, 0);
487 if (rc)
488 goto out;
489
490 /*
Mark Rutlanddfbca612016-08-11 14:11:06 +0100491 * 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
501 /*
James Morse82869ac2016-04-27 17:47:12 +0100502 * 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
526 /*
James Morse82869ac2016-04-27 17:47:12 +0100527 * 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
James Morse82869ac2016-04-27 17:47:12 +0100540 hibernate_exit(virt_to_phys(tmp_pg_dir), resume_hdr.ttbr1_el1,
Laura Abbottf8fee94e2017-01-10 13:35:49 -0800541 resume_hdr.reenter_kernel, restore_pblist,
James Morse82869ac2016-04-27 17:47:12 +0100542 resume_hdr.__hyp_stub_vectors, virt_to_phys(zero_page));
543
544out:
545 return rc;
546}
James Morse1fe492c2016-04-27 17:47:13 +0100547
James Morse8ec058f2016-08-17 13:50:26 +0100548int hibernate_resume_nonboot_cpu_disable(void)
549{
550 if (sleep_cpu < 0) {
551 pr_err("Failing to resume from hibernate on an unkown CPU.\n");
552 return -ENODEV;
553 }
554
555 return freeze_secondary_cpus(sleep_cpu);
556}