hibernate.c 13 KB

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