hibernate.c 11 KB

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