hibernate.c 11 KB

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