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