hibernate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*:
  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
  18. #include <linux/kvm_host.h>
  19. #include <linux/mm.h>
  20. #include <linux/notifier.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. #include <asm/barrier.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/irqflags.h>
  29. #include <asm/memory.h>
  30. #include <asm/mmu_context.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/pgtable-hwdef.h>
  34. #include <asm/sections.h>
  35. #include <asm/smp.h>
  36. #include <asm/suspend.h>
  37. #include <asm/sysreg.h>
  38. #include <asm/virt.h>
  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. /* Find a symbols alias in the linear map */
  49. #define LMADDR(x) phys_to_virt(virt_to_phys(x))
  50. /* Do we need to reset el2? */
  51. #define el2_reset_needed() (is_hyp_mode_available() && !is_kernel_in_hyp_mode())
  52. /* temporary el2 vectors in the __hibernate_exit_text section. */
  53. extern char hibernate_el2_vectors[];
  54. /* hyp-stub vectors, used to restore el2 during resume from hibernate. */
  55. extern char __hyp_stub_vectors[];
  56. /*
  57. * Values that may not change over hibernate/resume. We put the build number
  58. * and date in here so that we guarantee not to resume with a different
  59. * kernel.
  60. */
  61. struct arch_hibernate_hdr_invariants {
  62. char uts_version[__NEW_UTS_LEN + 1];
  63. };
  64. /* These values need to be know across a hibernate/restore. */
  65. static struct arch_hibernate_hdr {
  66. struct arch_hibernate_hdr_invariants invariants;
  67. /* These are needed to find the relocated kernel if built with kaslr */
  68. phys_addr_t ttbr1_el1;
  69. void (*reenter_kernel)(void);
  70. /*
  71. * We need to know where the __hyp_stub_vectors are after restore to
  72. * re-configure el2.
  73. */
  74. phys_addr_t __hyp_stub_vectors;
  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 = virt_to_pfn(&__nosave_begin);
  84. unsigned long nosave_end_pfn = virt_to_pfn(&__nosave_end - 1);
  85. return (pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn);
  86. }
  87. void notrace save_processor_state(void)
  88. {
  89. WARN_ON(num_online_cpus() != 1);
  90. }
  91. void notrace restore_processor_state(void)
  92. {
  93. }
  94. int arch_hibernation_header_save(void *addr, unsigned int max_size)
  95. {
  96. struct arch_hibernate_hdr *hdr = addr;
  97. if (max_size < sizeof(*hdr))
  98. return -EOVERFLOW;
  99. arch_hdr_invariants(&hdr->invariants);
  100. hdr->ttbr1_el1 = virt_to_phys(swapper_pg_dir);
  101. hdr->reenter_kernel = _cpu_resume;
  102. /* We can't use __hyp_get_vectors() because kvm may still be loaded */
  103. if (el2_reset_needed())
  104. hdr->__hyp_stub_vectors = virt_to_phys(__hyp_stub_vectors);
  105. else
  106. hdr->__hyp_stub_vectors = 0;
  107. return 0;
  108. }
  109. EXPORT_SYMBOL(arch_hibernation_header_save);
  110. int arch_hibernation_header_restore(void *addr)
  111. {
  112. struct arch_hibernate_hdr_invariants invariants;
  113. struct arch_hibernate_hdr *hdr = addr;
  114. arch_hdr_invariants(&invariants);
  115. if (memcmp(&hdr->invariants, &invariants, sizeof(invariants))) {
  116. pr_crit("Hibernate image not generated by this kernel!\n");
  117. return -EINVAL;
  118. }
  119. resume_hdr = *hdr;
  120. return 0;
  121. }
  122. EXPORT_SYMBOL(arch_hibernation_header_restore);
  123. /*
  124. * Copies length bytes, starting at src_start into an new page,
  125. * perform cache maintentance, then maps it at the specified address low
  126. * address as executable.
  127. *
  128. * This is used by hibernate to copy the code it needs to execute when
  129. * overwriting the kernel text. This function generates a new set of page
  130. * tables, which it loads into ttbr0.
  131. *
  132. * Length is provided as we probably only want 4K of data, even on a 64K
  133. * page system.
  134. */
  135. static int create_safe_exec_page(void *src_start, size_t length,
  136. unsigned long dst_addr,
  137. phys_addr_t *phys_dst_addr,
  138. void *(*allocator)(gfp_t mask),
  139. gfp_t mask)
  140. {
  141. int rc = 0;
  142. pgd_t *pgd;
  143. pud_t *pud;
  144. pmd_t *pmd;
  145. pte_t *pte;
  146. unsigned long dst = (unsigned long)allocator(mask);
  147. if (!dst) {
  148. rc = -ENOMEM;
  149. goto out;
  150. }
  151. memcpy((void *)dst, src_start, length);
  152. flush_icache_range(dst, dst + length);
  153. pgd = pgd_offset_raw(allocator(mask), dst_addr);
  154. if (pgd_none(*pgd)) {
  155. pud = allocator(mask);
  156. if (!pud) {
  157. rc = -ENOMEM;
  158. goto out;
  159. }
  160. pgd_populate(&init_mm, pgd, pud);
  161. }
  162. pud = pud_offset(pgd, dst_addr);
  163. if (pud_none(*pud)) {
  164. pmd = allocator(mask);
  165. if (!pmd) {
  166. rc = -ENOMEM;
  167. goto out;
  168. }
  169. pud_populate(&init_mm, pud, pmd);
  170. }
  171. pmd = pmd_offset(pud, dst_addr);
  172. if (pmd_none(*pmd)) {
  173. pte = allocator(mask);
  174. if (!pte) {
  175. rc = -ENOMEM;
  176. goto out;
  177. }
  178. pmd_populate_kernel(&init_mm, pmd, pte);
  179. }
  180. pte = pte_offset_kernel(pmd, dst_addr);
  181. set_pte(pte, __pte(virt_to_phys((void *)dst) |
  182. pgprot_val(PAGE_KERNEL_EXEC)));
  183. /*
  184. * Load our new page tables. A strict BBM approach requires that we
  185. * ensure that TLBs are free of any entries that may overlap with the
  186. * global mappings we are about to install.
  187. *
  188. * For a real hibernate/resume cycle TTBR0 currently points to a zero
  189. * page, but TLBs may contain stale ASID-tagged entries (e.g. for EFI
  190. * runtime services), while for a userspace-driven test_resume cycle it
  191. * points to userspace page tables (and we must point it at a zero page
  192. * ourselves). Elsewhere we only (un)install the idmap with preemption
  193. * disabled, so T0SZ should be as required regardless.
  194. */
  195. cpu_set_reserved_ttbr0();
  196. local_flush_tlb_all();
  197. write_sysreg(virt_to_phys(pgd), ttbr0_el1);
  198. isb();
  199. *phys_dst_addr = virt_to_phys((void *)dst);
  200. out:
  201. return rc;
  202. }
  203. int swsusp_arch_suspend(void)
  204. {
  205. int ret = 0;
  206. unsigned long flags;
  207. struct sleep_stack_data state;
  208. if (cpus_are_stuck_in_kernel()) {
  209. pr_err("Can't hibernate: no mechanism to offline secondary CPUs.\n");
  210. return -EBUSY;
  211. }
  212. local_dbg_save(flags);
  213. if (__cpu_suspend_enter(&state)) {
  214. ret = swsusp_save();
  215. } else {
  216. /* Clean kernel to PoC for secondary core startup */
  217. __flush_dcache_area(LMADDR(KERNEL_START), KERNEL_END - KERNEL_START);
  218. /*
  219. * Tell the hibernation core that we've just restored
  220. * the memory
  221. */
  222. in_suspend = 0;
  223. __cpu_suspend_exit();
  224. }
  225. local_dbg_restore(flags);
  226. return ret;
  227. }
  228. static int copy_pte(pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long start,
  229. unsigned long end)
  230. {
  231. pte_t *src_pte;
  232. pte_t *dst_pte;
  233. unsigned long addr = start;
  234. dst_pte = (pte_t *)get_safe_page(GFP_ATOMIC);
  235. if (!dst_pte)
  236. return -ENOMEM;
  237. pmd_populate_kernel(&init_mm, dst_pmd, dst_pte);
  238. dst_pte = pte_offset_kernel(dst_pmd, start);
  239. src_pte = pte_offset_kernel(src_pmd, start);
  240. do {
  241. if (!pte_none(*src_pte))
  242. /*
  243. * Resume will overwrite areas that may be marked
  244. * read only (code, rodata). Clear the RDONLY bit from
  245. * the temporary mappings we use during restore.
  246. */
  247. set_pte(dst_pte, __pte(pte_val(*src_pte) & ~PTE_RDONLY));
  248. } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
  249. return 0;
  250. }
  251. static int copy_pmd(pud_t *dst_pud, pud_t *src_pud, unsigned long start,
  252. unsigned long end)
  253. {
  254. pmd_t *src_pmd;
  255. pmd_t *dst_pmd;
  256. unsigned long next;
  257. unsigned long addr = start;
  258. if (pud_none(*dst_pud)) {
  259. dst_pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
  260. if (!dst_pmd)
  261. return -ENOMEM;
  262. pud_populate(&init_mm, dst_pud, dst_pmd);
  263. }
  264. dst_pmd = pmd_offset(dst_pud, start);
  265. src_pmd = pmd_offset(src_pud, start);
  266. do {
  267. next = pmd_addr_end(addr, end);
  268. if (pmd_none(*src_pmd))
  269. continue;
  270. if (pmd_table(*src_pmd)) {
  271. if (copy_pte(dst_pmd, src_pmd, addr, next))
  272. return -ENOMEM;
  273. } else {
  274. set_pmd(dst_pmd,
  275. __pmd(pmd_val(*src_pmd) & ~PMD_SECT_RDONLY));
  276. }
  277. } while (dst_pmd++, src_pmd++, addr = next, addr != end);
  278. return 0;
  279. }
  280. static int copy_pud(pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long start,
  281. unsigned long end)
  282. {
  283. pud_t *dst_pud;
  284. pud_t *src_pud;
  285. unsigned long next;
  286. unsigned long addr = start;
  287. if (pgd_none(*dst_pgd)) {
  288. dst_pud = (pud_t *)get_safe_page(GFP_ATOMIC);
  289. if (!dst_pud)
  290. return -ENOMEM;
  291. pgd_populate(&init_mm, dst_pgd, dst_pud);
  292. }
  293. dst_pud = pud_offset(dst_pgd, start);
  294. src_pud = pud_offset(src_pgd, start);
  295. do {
  296. next = pud_addr_end(addr, end);
  297. if (pud_none(*src_pud))
  298. continue;
  299. if (pud_table(*(src_pud))) {
  300. if (copy_pmd(dst_pud, src_pud, addr, next))
  301. return -ENOMEM;
  302. } else {
  303. set_pud(dst_pud,
  304. __pud(pud_val(*src_pud) & ~PMD_SECT_RDONLY));
  305. }
  306. } while (dst_pud++, src_pud++, addr = next, addr != end);
  307. return 0;
  308. }
  309. static int copy_page_tables(pgd_t *dst_pgd, unsigned long start,
  310. unsigned long end)
  311. {
  312. unsigned long next;
  313. unsigned long addr = start;
  314. pgd_t *src_pgd = pgd_offset_k(start);
  315. dst_pgd = pgd_offset_raw(dst_pgd, start);
  316. do {
  317. next = pgd_addr_end(addr, end);
  318. if (pgd_none(*src_pgd))
  319. continue;
  320. if (copy_pud(dst_pgd, src_pgd, addr, next))
  321. return -ENOMEM;
  322. } while (dst_pgd++, src_pgd++, addr = next, addr != end);
  323. return 0;
  324. }
  325. /*
  326. * Setup then Resume from the hibernate image using swsusp_arch_suspend_exit().
  327. *
  328. * Memory allocated by get_safe_page() will be dealt with by the hibernate code,
  329. * we don't need to free it here.
  330. */
  331. int swsusp_arch_resume(void)
  332. {
  333. int rc = 0;
  334. void *zero_page;
  335. size_t exit_size;
  336. pgd_t *tmp_pg_dir;
  337. void *lm_restore_pblist;
  338. phys_addr_t phys_hibernate_exit;
  339. void __noreturn (*hibernate_exit)(phys_addr_t, phys_addr_t, void *,
  340. void *, phys_addr_t, phys_addr_t);
  341. /*
  342. * Restoring the memory image will overwrite the ttbr1 page tables.
  343. * Create a second copy of just the linear map, and use this when
  344. * restoring.
  345. */
  346. tmp_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
  347. if (!tmp_pg_dir) {
  348. pr_err("Failed to allocate memory for temporary page tables.");
  349. rc = -ENOMEM;
  350. goto out;
  351. }
  352. rc = copy_page_tables(tmp_pg_dir, PAGE_OFFSET, 0);
  353. if (rc)
  354. goto out;
  355. /*
  356. * Since we only copied the linear map, we need to find restore_pblist's
  357. * linear map address.
  358. */
  359. lm_restore_pblist = LMADDR(restore_pblist);
  360. /*
  361. * We need a zero page that is zero before & after resume in order to
  362. * to break before make on the ttbr1 page tables.
  363. */
  364. zero_page = (void *)get_safe_page(GFP_ATOMIC);
  365. if (!zero_page) {
  366. pr_err("Failed to allocate zero page.");
  367. rc = -ENOMEM;
  368. goto out;
  369. }
  370. /*
  371. * Locate the exit code in the bottom-but-one page, so that *NULL
  372. * still has disastrous affects.
  373. */
  374. hibernate_exit = (void *)PAGE_SIZE;
  375. exit_size = __hibernate_exit_text_end - __hibernate_exit_text_start;
  376. /*
  377. * Copy swsusp_arch_suspend_exit() to a safe page. This will generate
  378. * a new set of ttbr0 page tables and load them.
  379. */
  380. rc = create_safe_exec_page(__hibernate_exit_text_start, exit_size,
  381. (unsigned long)hibernate_exit,
  382. &phys_hibernate_exit,
  383. (void *)get_safe_page, GFP_ATOMIC);
  384. if (rc) {
  385. pr_err("Failed to create safe executable page for hibernate_exit code.");
  386. goto out;
  387. }
  388. /*
  389. * The hibernate exit text contains a set of el2 vectors, that will
  390. * be executed at el2 with the mmu off in order to reload hyp-stub.
  391. */
  392. __flush_dcache_area(hibernate_exit, exit_size);
  393. /*
  394. * KASLR will cause the el2 vectors to be in a different location in
  395. * the resumed kernel. Load hibernate's temporary copy into el2.
  396. *
  397. * We can skip this step if we booted at EL1, or are running with VHE.
  398. */
  399. if (el2_reset_needed()) {
  400. phys_addr_t el2_vectors = phys_hibernate_exit; /* base */
  401. el2_vectors += hibernate_el2_vectors -
  402. __hibernate_exit_text_start; /* offset */
  403. __hyp_set_vectors(el2_vectors);
  404. }
  405. hibernate_exit(virt_to_phys(tmp_pg_dir), resume_hdr.ttbr1_el1,
  406. resume_hdr.reenter_kernel, lm_restore_pblist,
  407. resume_hdr.__hyp_stub_vectors, virt_to_phys(zero_page));
  408. out:
  409. return rc;
  410. }
  411. static int check_boot_cpu_online_pm_callback(struct notifier_block *nb,
  412. unsigned long action, void *ptr)
  413. {
  414. if (action == PM_HIBERNATION_PREPARE &&
  415. cpumask_first(cpu_online_mask) != 0) {
  416. pr_warn("CPU0 is offline.\n");
  417. return notifier_from_errno(-ENODEV);
  418. }
  419. return NOTIFY_OK;
  420. }
  421. static int __init check_boot_cpu_online_init(void)
  422. {
  423. /*
  424. * Set this pm_notifier callback with a lower priority than
  425. * cpu_hotplug_pm_callback, so that cpu_hotplug_pm_callback will be
  426. * called earlier to disable cpu hotplug before the cpu online check.
  427. */
  428. pm_notifier(check_boot_cpu_online_pm_callback, -INT_MAX);
  429. return 0;
  430. }
  431. core_initcall(check_boot_cpu_online_init);