1 // SPDX-License-Identifier: GPL-2.0-only
2 /*:
3 * Hibernate support specific for ARM64
4 *
5 * Derived from work on ARM hibernation support by:
6 *
7 * Ubuntu project, hibernation support for mach-dove
8 * Copyright (C) 2010 Nokia Corporation (Hiroshi Doyu)
9 * Copyright (C) 2010 Texas Instruments, Inc. (Teerth Reddy et al.)
10 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
11 */
12 #define pr_fmt(x) "hibernate: " x
13 #include <linux/cpu.h>
14 #include <linux/kvm_host.h>
15 #include <linux/pm.h>
16 #include <linux/sched.h>
17 #include <linux/suspend.h>
18 #include <linux/utsname.h>
19
20 #include <asm/barrier.h>
21 #include <asm/cacheflush.h>
22 #include <asm/cputype.h>
23 #include <asm/daifflags.h>
24 #include <asm/irqflags.h>
25 #include <asm/kexec.h>
26 #include <asm/memory.h>
27 #include <asm/mmu_context.h>
28 #include <asm/mte.h>
29 #include <asm/sections.h>
30 #include <asm/smp.h>
31 #include <asm/smp_plat.h>
32 #include <asm/suspend.h>
33 #include <asm/sysreg.h>
34 #include <asm/trans_pgd.h>
35 #include <asm/virt.h>
36 #include <trace/hooks/bl_hib.h>
37
38 /*
39 * Hibernate core relies on this value being 0 on resume, and marks it
40 * __nosavedata assuming it will keep the resume kernel's '0' value. This
41 * doesn't happen with either KASLR.
42 *
43 * defined as "__visible int in_suspend __nosavedata" in
44 * kernel/power/hibernate.c
45 */
46 extern int in_suspend;
47
48 /* Do we need to reset el2? */
49 #define el2_reset_needed() (is_hyp_nvhe())
50
51 /* hyp-stub vectors, used to restore el2 during resume from hibernate. */
52 extern char __hyp_stub_vectors[];
53
54 /*
55 * The logical cpu number we should resume on, initialised to a non-cpu
56 * number.
57 */
58 static int sleep_cpu = -EINVAL;
59
60 /*
61 * Values that may not change over hibernate/resume. We put the build number
62 * and date in here so that we guarantee not to resume with a different
63 * kernel.
64 */
65 struct arch_hibernate_hdr_invariants {
66 char uts_version[__NEW_UTS_LEN + 1];
67 };
68
69 /* These values need to be know across a hibernate/restore. */
70 static struct arch_hibernate_hdr {
71 struct arch_hibernate_hdr_invariants invariants;
72
73 /* These are needed to find the relocated kernel if built with kaslr */
74 phys_addr_t ttbr1_el1;
75 void (*reenter_kernel)(void);
76
77 /*
78 * We need to know where the __hyp_stub_vectors are after restore to
79 * re-configure el2.
80 */
81 phys_addr_t __hyp_stub_vectors;
82
83 u64 sleep_cpu_mpidr;
84
85 ANDROID_VENDOR_DATA(1);
86 } resume_hdr;
87
arch_hdr_invariants(struct arch_hibernate_hdr_invariants * i)88 static inline void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i)
89 {
90 memset(i, 0, sizeof(*i));
91 memcpy(i->uts_version, init_utsname()->version, sizeof(i->uts_version));
92 }
93
pfn_is_nosave(unsigned long pfn)94 int pfn_is_nosave(unsigned long pfn)
95 {
96 unsigned long nosave_begin_pfn = sym_to_pfn(&__nosave_begin);
97 unsigned long nosave_end_pfn = sym_to_pfn(&__nosave_end - 1);
98
99 return ((pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn)) ||
100 crash_is_nosave(pfn);
101 }
102
save_processor_state(void)103 void notrace save_processor_state(void)
104 {
105 }
106
restore_processor_state(void)107 void notrace restore_processor_state(void)
108 {
109 }
110
arch_hibernation_header_save(void * addr,unsigned int max_size)111 int arch_hibernation_header_save(void *addr, unsigned int max_size)
112 {
113 struct arch_hibernate_hdr *hdr = addr;
114
115 if (max_size < sizeof(*hdr))
116 return -EOVERFLOW;
117
118 arch_hdr_invariants(&hdr->invariants);
119 hdr->ttbr1_el1 = __pa_symbol(swapper_pg_dir);
120 hdr->reenter_kernel = _cpu_resume;
121
122 #ifdef CONFIG_ANDROID_VENDOR_OEM_DATA
123 trace_android_vh_save_cpu_resume(&hdr->android_vendor_data1,
124 __pa(cpu_resume));
125 #endif
126
127 /* We can't use __hyp_get_vectors() because kvm may still be loaded */
128 if (el2_reset_needed())
129 hdr->__hyp_stub_vectors = __pa_symbol(__hyp_stub_vectors);
130 else
131 hdr->__hyp_stub_vectors = 0;
132
133 /* Save the mpidr of the cpu we called cpu_suspend() on... */
134 if (sleep_cpu < 0) {
135 pr_err("Failing to hibernate on an unknown CPU.\n");
136 return -ENODEV;
137 }
138 hdr->sleep_cpu_mpidr = cpu_logical_map(sleep_cpu);
139 pr_info("Hibernating on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
140 hdr->sleep_cpu_mpidr);
141
142 return 0;
143 }
144 EXPORT_SYMBOL(arch_hibernation_header_save);
145
arch_hibernation_header_restore(void * addr)146 int arch_hibernation_header_restore(void *addr)
147 {
148 int ret;
149 struct arch_hibernate_hdr_invariants invariants;
150 struct arch_hibernate_hdr *hdr = addr;
151
152 arch_hdr_invariants(&invariants);
153 if (memcmp(&hdr->invariants, &invariants, sizeof(invariants))) {
154 pr_crit("Hibernate image not generated by this kernel!\n");
155 return -EINVAL;
156 }
157
158 sleep_cpu = get_logical_index(hdr->sleep_cpu_mpidr);
159 pr_info("Hibernated on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
160 hdr->sleep_cpu_mpidr);
161 if (sleep_cpu < 0) {
162 pr_crit("Hibernated on a CPU not known to this kernel!\n");
163 sleep_cpu = -EINVAL;
164 return -EINVAL;
165 }
166
167 ret = bringup_hibernate_cpu(sleep_cpu);
168 if (ret) {
169 sleep_cpu = -EINVAL;
170 return ret;
171 }
172
173 resume_hdr = *hdr;
174
175 return 0;
176 }
177 EXPORT_SYMBOL(arch_hibernation_header_restore);
178
hibernate_page_alloc(void * arg)179 static void *hibernate_page_alloc(void *arg)
180 {
181 return (void *)get_safe_page((__force gfp_t)(unsigned long)arg);
182 }
183
184 /*
185 * Copies length bytes, starting at src_start into an new page,
186 * perform cache maintenance, then maps it at the specified address low
187 * address as executable.
188 *
189 * This is used by hibernate to copy the code it needs to execute when
190 * overwriting the kernel text. This function generates a new set of page
191 * tables, which it loads into ttbr0.
192 *
193 * Length is provided as we probably only want 4K of data, even on a 64K
194 * page system.
195 */
create_safe_exec_page(void * src_start,size_t length,phys_addr_t * phys_dst_addr)196 static int create_safe_exec_page(void *src_start, size_t length,
197 phys_addr_t *phys_dst_addr)
198 {
199 struct trans_pgd_info trans_info = {
200 .trans_alloc_page = hibernate_page_alloc,
201 .trans_alloc_arg = (__force void *)GFP_ATOMIC,
202 };
203
204 void *page = (void *)get_safe_page(GFP_ATOMIC);
205 phys_addr_t trans_ttbr0;
206 unsigned long t0sz;
207 int rc;
208
209 if (!page)
210 return -ENOMEM;
211
212 memcpy(page, src_start, length);
213 caches_clean_inval_pou((unsigned long)page, (unsigned long)page + length);
214 rc = trans_pgd_idmap_page(&trans_info, &trans_ttbr0, &t0sz, page);
215 if (rc)
216 return rc;
217
218 cpu_install_ttbr0(trans_ttbr0, t0sz);
219 *phys_dst_addr = virt_to_phys(page);
220
221 return 0;
222 }
223
224 #ifdef CONFIG_ARM64_MTE
225
226 static DEFINE_XARRAY(mte_pages);
227
save_tags(struct page * page,unsigned long pfn)228 static int save_tags(struct page *page, unsigned long pfn)
229 {
230 void *tag_storage, *ret;
231
232 tag_storage = mte_allocate_tag_storage();
233 if (!tag_storage)
234 return -ENOMEM;
235
236 mte_save_page_tags(page_address(page), tag_storage);
237
238 ret = xa_store(&mte_pages, pfn, tag_storage, GFP_KERNEL);
239 if (WARN(xa_is_err(ret), "Failed to store MTE tags")) {
240 mte_free_tag_storage(tag_storage);
241 return xa_err(ret);
242 } else if (WARN(ret, "swsusp: %s: Duplicate entry", __func__)) {
243 mte_free_tag_storage(ret);
244 }
245
246 return 0;
247 }
248
swsusp_mte_free_storage(void)249 static void swsusp_mte_free_storage(void)
250 {
251 XA_STATE(xa_state, &mte_pages, 0);
252 void *tags;
253
254 xa_lock(&mte_pages);
255 xas_for_each(&xa_state, tags, ULONG_MAX) {
256 mte_free_tag_storage(tags);
257 }
258 xa_unlock(&mte_pages);
259
260 xa_destroy(&mte_pages);
261 }
262
swsusp_mte_save_tags(void)263 static int swsusp_mte_save_tags(void)
264 {
265 struct zone *zone;
266 unsigned long pfn, max_zone_pfn;
267 int ret = 0;
268 int n = 0;
269
270 if (!system_supports_mte())
271 return 0;
272
273 for_each_populated_zone(zone) {
274 max_zone_pfn = zone_end_pfn(zone);
275 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
276 struct page *page = pfn_to_online_page(pfn);
277
278 if (!page)
279 continue;
280
281 if (!page_mte_tagged(page))
282 continue;
283
284 ret = save_tags(page, pfn);
285 if (ret) {
286 swsusp_mte_free_storage();
287 goto out;
288 }
289
290 n++;
291 }
292 }
293 pr_info("Saved %d MTE pages\n", n);
294
295 out:
296 return ret;
297 }
298
swsusp_mte_restore_tags(void)299 static void swsusp_mte_restore_tags(void)
300 {
301 XA_STATE(xa_state, &mte_pages, 0);
302 int n = 0;
303 void *tags;
304
305 xa_lock(&mte_pages);
306 xas_for_each(&xa_state, tags, ULONG_MAX) {
307 unsigned long pfn = xa_state.xa_index;
308 struct page *page = pfn_to_online_page(pfn);
309
310 mte_restore_page_tags(page_address(page), tags);
311
312 mte_free_tag_storage(tags);
313 n++;
314 }
315 xa_unlock(&mte_pages);
316
317 pr_info("Restored %d MTE pages\n", n);
318
319 xa_destroy(&mte_pages);
320 }
321
322 #else /* CONFIG_ARM64_MTE */
323
swsusp_mte_save_tags(void)324 static int swsusp_mte_save_tags(void)
325 {
326 return 0;
327 }
328
swsusp_mte_restore_tags(void)329 static void swsusp_mte_restore_tags(void)
330 {
331 }
332
333 #endif /* CONFIG_ARM64_MTE */
334
swsusp_arch_suspend(void)335 int swsusp_arch_suspend(void)
336 {
337 int ret = 0;
338 unsigned long flags;
339 struct sleep_stack_data state;
340
341 if (cpus_are_stuck_in_kernel()) {
342 pr_err("Can't hibernate: no mechanism to offline secondary CPUs.\n");
343 return -EBUSY;
344 }
345
346 flags = local_daif_save();
347
348 if (__cpu_suspend_enter(&state)) {
349 /* make the crash dump kernel image visible/saveable */
350 crash_prepare_suspend();
351
352 ret = swsusp_mte_save_tags();
353 if (ret)
354 return ret;
355
356 sleep_cpu = smp_processor_id();
357 ret = swsusp_save();
358 } else {
359 /* Clean kernel core startup/idle code to PoC*/
360 dcache_clean_inval_poc((unsigned long)__mmuoff_data_start,
361 (unsigned long)__mmuoff_data_end);
362 dcache_clean_inval_poc((unsigned long)__idmap_text_start,
363 (unsigned long)__idmap_text_end);
364
365 /* Clean kvm setup code to PoC? */
366 if (el2_reset_needed()) {
367 dcache_clean_inval_poc(
368 (unsigned long)__hyp_idmap_text_start,
369 (unsigned long)__hyp_idmap_text_end);
370 dcache_clean_inval_poc((unsigned long)__hyp_text_start,
371 (unsigned long)__hyp_text_end);
372 }
373
374 swsusp_mte_restore_tags();
375
376 /* make the crash dump kernel image protected again */
377 crash_post_resume();
378
379 /*
380 * Tell the hibernation core that we've just restored
381 * the memory
382 */
383 in_suspend = 0;
384
385 sleep_cpu = -EINVAL;
386 __cpu_suspend_exit();
387
388 /*
389 * Just in case the boot kernel did turn the SSBD
390 * mitigation off behind our back, let's set the state
391 * to what we expect it to be.
392 */
393 spectre_v4_enable_mitigation(NULL);
394 }
395
396 local_daif_restore(flags);
397
398 return ret;
399 }
400
401 /*
402 * Setup then Resume from the hibernate image using swsusp_arch_suspend_exit().
403 *
404 * Memory allocated by get_safe_page() will be dealt with by the hibernate code,
405 * we don't need to free it here.
406 */
swsusp_arch_resume(void)407 int swsusp_arch_resume(void)
408 {
409 int rc;
410 void *zero_page;
411 size_t exit_size;
412 pgd_t *tmp_pg_dir;
413 phys_addr_t el2_vectors;
414 void __noreturn (*hibernate_exit)(phys_addr_t, phys_addr_t, void *,
415 void *, phys_addr_t, phys_addr_t);
416 struct trans_pgd_info trans_info = {
417 .trans_alloc_page = hibernate_page_alloc,
418 .trans_alloc_arg = (__force void *)GFP_ATOMIC,
419 };
420
421 /*
422 * Restoring the memory image will overwrite the ttbr1 page tables.
423 * Create a second copy of just the linear map, and use this when
424 * restoring.
425 */
426 rc = trans_pgd_create_copy(&trans_info, &tmp_pg_dir, PAGE_OFFSET,
427 PAGE_END);
428 if (rc)
429 return rc;
430
431 /*
432 * We need a zero page that is zero before & after resume in order
433 * to break before make on the ttbr1 page tables.
434 */
435 zero_page = (void *)get_safe_page(GFP_ATOMIC);
436 if (!zero_page) {
437 pr_err("Failed to allocate zero page.\n");
438 return -ENOMEM;
439 }
440
441 if (el2_reset_needed()) {
442 rc = trans_pgd_copy_el2_vectors(&trans_info, &el2_vectors);
443 if (rc) {
444 pr_err("Failed to setup el2 vectors\n");
445 return rc;
446 }
447 }
448
449 exit_size = __hibernate_exit_text_end - __hibernate_exit_text_start;
450 /*
451 * Copy swsusp_arch_suspend_exit() to a safe page. This will generate
452 * a new set of ttbr0 page tables and load them.
453 */
454 rc = create_safe_exec_page(__hibernate_exit_text_start, exit_size,
455 (phys_addr_t *)&hibernate_exit);
456 if (rc) {
457 pr_err("Failed to create safe executable page for hibernate_exit code.\n");
458 return rc;
459 }
460
461 /*
462 * KASLR will cause the el2 vectors to be in a different location in
463 * the resumed kernel. Load hibernate's temporary copy into el2.
464 *
465 * We can skip this step if we booted at EL1, or are running with VHE.
466 */
467 if (el2_reset_needed())
468 __hyp_set_vectors(el2_vectors);
469
470 hibernate_exit(virt_to_phys(tmp_pg_dir), resume_hdr.ttbr1_el1,
471 resume_hdr.reenter_kernel, restore_pblist,
472 resume_hdr.__hyp_stub_vectors, virt_to_phys(zero_page));
473
474 return 0;
475 }
476
hibernate_resume_nonboot_cpu_disable(void)477 int hibernate_resume_nonboot_cpu_disable(void)
478 {
479 if (sleep_cpu < 0) {
480 pr_err("Failing to resume from hibernate on an unknown CPU.\n");
481 return -ENODEV;
482 }
483
484 return freeze_secondary_cpus(sleep_cpu);
485 }
486