hibernate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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/cpu.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/mm.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/cputype.h>
  29. #include <asm/daifflags.h>
  30. #include <asm/irqflags.h>
  31. #include <asm/kexec.h>
  32. #include <asm/memory.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/pgalloc.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/pgtable-hwdef.h>
  37. #include <asm/sections.h>
  38. #include <asm/smp.h>
  39. #include <asm/smp_plat.h>
  40. #include <asm/suspend.h>
  41. #include <asm/sysreg.h>
  42. #include <asm/virt.h>
  43. /*
  44. * Hibernate core relies on this value being 0 on resume, and marks it
  45. * __nosavedata assuming it will keep the resume kernel's '0' value. This
  46. * doesn't happen with either KASLR.
  47. *
  48. * defined as "__visible int in_suspend __nosavedata" in
  49. * kernel/power/hibernate.c
  50. */
  51. extern int in_suspend;
  52. /* Do we need to reset el2? */
  53. #define el2_reset_needed() (is_hyp_mode_available() && !is_kernel_in_hyp_mode())
  54. /* temporary el2 vectors in the __hibernate_exit_text section. */
  55. extern char hibernate_el2_vectors[];
  56. /* hyp-stub vectors, used to restore el2 during resume from hibernate. */
  57. extern char __hyp_stub_vectors[];
  58. /*
  59. * The logical cpu number we should resume on, initialised to a non-cpu
  60. * number.
  61. */
  62. static int sleep_cpu = -EINVAL;
  63. /*
  64. * Values that may not change over hibernate/resume. We put the build number
  65. * and date in here so that we guarantee not to resume with a different
  66. * kernel.
  67. */
  68. struct arch_hibernate_hdr_invariants {
  69. char uts_version[__NEW_UTS_LEN + 1];
  70. };
  71. /* These values need to be know across a hibernate/restore. */
  72. static struct arch_hibernate_hdr {
  73. struct arch_hibernate_hdr_invariants invariants;
  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. * We need to know where the __hyp_stub_vectors are after restore to
  79. * re-configure el2.
  80. */
  81. phys_addr_t __hyp_stub_vectors;
  82. u64 sleep_cpu_mpidr;
  83. } resume_hdr;
  84. static inline void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i)
  85. {
  86. memset(i, 0, sizeof(*i));
  87. memcpy(i->uts_version, init_utsname()->version, sizeof(i->uts_version));
  88. }
  89. int pfn_is_nosave(unsigned long pfn)
  90. {
  91. unsigned long nosave_begin_pfn = sym_to_pfn(&__nosave_begin);
  92. unsigned long nosave_end_pfn = sym_to_pfn(&__nosave_end - 1);
  93. return ((pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn)) ||
  94. crash_is_nosave(pfn);
  95. }
  96. void notrace save_processor_state(void)
  97. {
  98. WARN_ON(num_online_cpus() != 1);
  99. }
  100. void notrace restore_processor_state(void)
  101. {
  102. }
  103. int arch_hibernation_header_save(void *addr, unsigned int max_size)
  104. {
  105. struct arch_hibernate_hdr *hdr = addr;
  106. if (max_size < sizeof(*hdr))
  107. return -EOVERFLOW;
  108. arch_hdr_invariants(&hdr->invariants);
  109. hdr->ttbr1_el1 = __pa_symbol(swapper_pg_dir);
  110. hdr->reenter_kernel = _cpu_resume;
  111. /* We can't use __hyp_get_vectors() because kvm may still be loaded */
  112. if (el2_reset_needed())
  113. hdr->__hyp_stub_vectors = __pa_symbol(__hyp_stub_vectors);
  114. else
  115. hdr->__hyp_stub_vectors = 0;
  116. /* Save the mpidr of the cpu we called cpu_suspend() on... */
  117. if (sleep_cpu < 0) {
  118. pr_err("Failing to hibernate on an unknown CPU.\n");
  119. return -ENODEV;
  120. }
  121. hdr->sleep_cpu_mpidr = cpu_logical_map(sleep_cpu);
  122. pr_info("Hibernating on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
  123. hdr->sleep_cpu_mpidr);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(arch_hibernation_header_save);
  127. int arch_hibernation_header_restore(void *addr)
  128. {
  129. int ret;
  130. struct arch_hibernate_hdr_invariants invariants;
  131. struct arch_hibernate_hdr *hdr = addr;
  132. arch_hdr_invariants(&invariants);
  133. if (memcmp(&hdr->invariants, &invariants, sizeof(invariants))) {
  134. pr_crit("Hibernate image not generated by this kernel!\n");
  135. return -EINVAL;
  136. }
  137. sleep_cpu = get_logical_index(hdr->sleep_cpu_mpidr);
  138. pr_info("Hibernated on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
  139. hdr->sleep_cpu_mpidr);
  140. if (sleep_cpu < 0) {
  141. pr_crit("Hibernated on a CPU not known to this kernel!\n");
  142. sleep_cpu = -EINVAL;
  143. return -EINVAL;
  144. }
  145. if (!cpu_online(sleep_cpu)) {
  146. pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n");
  147. ret = cpu_up(sleep_cpu);
  148. if (ret) {
  149. pr_err("Failed to bring hibernate-CPU up!\n");
  150. sleep_cpu = -EINVAL;
  151. return ret;
  152. }
  153. }
  154. resume_hdr = *hdr;
  155. return 0;
  156. }
  157. EXPORT_SYMBOL(arch_hibernation_header_restore);
  158. /*
  159. * Copies length bytes, starting at src_start into an new page,
  160. * perform cache maintentance, then maps it at the specified address low
  161. * address as executable.
  162. *
  163. * This is used by hibernate to copy the code it needs to execute when
  164. * overwriting the kernel text. This function generates a new set of page
  165. * tables, which it loads into ttbr0.
  166. *
  167. * Length is provided as we probably only want 4K of data, even on a 64K
  168. * page system.
  169. */
  170. static int create_safe_exec_page(void *src_start, size_t length,
  171. unsigned long dst_addr,
  172. phys_addr_t *phys_dst_addr,
  173. void *(*allocator)(gfp_t mask),
  174. gfp_t mask)
  175. {
  176. int rc = 0;
  177. pgd_t *trans_pgd;
  178. pgd_t *pgdp;
  179. pud_t *pudp;
  180. pmd_t *pmdp;
  181. pte_t *ptep;
  182. unsigned long dst = (unsigned long)allocator(mask);
  183. if (!dst) {
  184. rc = -ENOMEM;
  185. goto out;
  186. }
  187. memcpy((void *)dst, src_start, length);
  188. __flush_icache_range(dst, dst + length);
  189. trans_pgd = allocator(mask);
  190. if (!trans_pgd) {
  191. rc = -ENOMEM;
  192. goto out;
  193. }
  194. pgdp = pgd_offset_raw(trans_pgd, dst_addr);
  195. if (pgd_none(READ_ONCE(*pgdp))) {
  196. pudp = allocator(mask);
  197. if (!pudp) {
  198. rc = -ENOMEM;
  199. goto out;
  200. }
  201. pgd_populate(&init_mm, pgdp, pudp);
  202. }
  203. pudp = pud_offset(pgdp, dst_addr);
  204. if (pud_none(READ_ONCE(*pudp))) {
  205. pmdp = allocator(mask);
  206. if (!pmdp) {
  207. rc = -ENOMEM;
  208. goto out;
  209. }
  210. pud_populate(&init_mm, pudp, pmdp);
  211. }
  212. pmdp = pmd_offset(pudp, dst_addr);
  213. if (pmd_none(READ_ONCE(*pmdp))) {
  214. ptep = allocator(mask);
  215. if (!ptep) {
  216. rc = -ENOMEM;
  217. goto out;
  218. }
  219. pmd_populate_kernel(&init_mm, pmdp, ptep);
  220. }
  221. ptep = pte_offset_kernel(pmdp, dst_addr);
  222. set_pte(ptep, pfn_pte(virt_to_pfn(dst), PAGE_KERNEL_EXEC));
  223. /*
  224. * Load our new page tables. A strict BBM approach requires that we
  225. * ensure that TLBs are free of any entries that may overlap with the
  226. * global mappings we are about to install.
  227. *
  228. * For a real hibernate/resume cycle TTBR0 currently points to a zero
  229. * page, but TLBs may contain stale ASID-tagged entries (e.g. for EFI
  230. * runtime services), while for a userspace-driven test_resume cycle it
  231. * points to userspace page tables (and we must point it at a zero page
  232. * ourselves). Elsewhere we only (un)install the idmap with preemption
  233. * disabled, so T0SZ should be as required regardless.
  234. */
  235. cpu_set_reserved_ttbr0();
  236. local_flush_tlb_all();
  237. write_sysreg(phys_to_ttbr(virt_to_phys(pgdp)), ttbr0_el1);
  238. isb();
  239. *phys_dst_addr = virt_to_phys((void *)dst);
  240. out:
  241. return rc;
  242. }
  243. #define dcache_clean_range(start, end) __flush_dcache_area(start, (end - start))
  244. int swsusp_arch_suspend(void)
  245. {
  246. int ret = 0;
  247. unsigned long flags;
  248. struct sleep_stack_data state;
  249. if (cpus_are_stuck_in_kernel()) {
  250. pr_err("Can't hibernate: no mechanism to offline secondary CPUs.\n");
  251. return -EBUSY;
  252. }
  253. flags = local_daif_save();
  254. if (__cpu_suspend_enter(&state)) {
  255. /* make the crash dump kernel image visible/saveable */
  256. crash_prepare_suspend();
  257. sleep_cpu = smp_processor_id();
  258. ret = swsusp_save();
  259. } else {
  260. /* Clean kernel core startup/idle code to PoC*/
  261. dcache_clean_range(__mmuoff_data_start, __mmuoff_data_end);
  262. dcache_clean_range(__idmap_text_start, __idmap_text_end);
  263. /* Clean kvm setup code to PoC? */
  264. if (el2_reset_needed()) {
  265. dcache_clean_range(__hyp_idmap_text_start, __hyp_idmap_text_end);
  266. dcache_clean_range(__hyp_text_start, __hyp_text_end);
  267. }
  268. /* make the crash dump kernel image protected again */
  269. crash_post_resume();
  270. /*
  271. * Tell the hibernation core that we've just restored
  272. * the memory
  273. */
  274. in_suspend = 0;
  275. sleep_cpu = -EINVAL;
  276. __cpu_suspend_exit();
  277. /*
  278. * Just in case the boot kernel did turn the SSBD
  279. * mitigation off behind our back, let's set the state
  280. * to what we expect it to be.
  281. */
  282. switch (arm64_get_ssbd_state()) {
  283. case ARM64_SSBD_FORCE_ENABLE:
  284. case ARM64_SSBD_KERNEL:
  285. arm64_set_ssbd_mitigation(true);
  286. }
  287. }
  288. local_daif_restore(flags);
  289. return ret;
  290. }
  291. static void _copy_pte(pte_t *dst_ptep, pte_t *src_ptep, unsigned long addr)
  292. {
  293. pte_t pte = READ_ONCE(*src_ptep);
  294. if (pte_valid(pte)) {
  295. /*
  296. * Resume will overwrite areas that may be marked
  297. * read only (code, rodata). Clear the RDONLY bit from
  298. * the temporary mappings we use during restore.
  299. */
  300. set_pte(dst_ptep, pte_mkwrite(pte));
  301. } else if (debug_pagealloc_enabled() && !pte_none(pte)) {
  302. /*
  303. * debug_pagealloc will removed the PTE_VALID bit if
  304. * the page isn't in use by the resume kernel. It may have
  305. * been in use by the original kernel, in which case we need
  306. * to put it back in our copy to do the restore.
  307. *
  308. * Before marking this entry valid, check the pfn should
  309. * be mapped.
  310. */
  311. BUG_ON(!pfn_valid(pte_pfn(pte)));
  312. set_pte(dst_ptep, pte_mkpresent(pte_mkwrite(pte)));
  313. }
  314. }
  315. static int copy_pte(pmd_t *dst_pmdp, pmd_t *src_pmdp, unsigned long start,
  316. unsigned long end)
  317. {
  318. pte_t *src_ptep;
  319. pte_t *dst_ptep;
  320. unsigned long addr = start;
  321. dst_ptep = (pte_t *)get_safe_page(GFP_ATOMIC);
  322. if (!dst_ptep)
  323. return -ENOMEM;
  324. pmd_populate_kernel(&init_mm, dst_pmdp, dst_ptep);
  325. dst_ptep = pte_offset_kernel(dst_pmdp, start);
  326. src_ptep = pte_offset_kernel(src_pmdp, start);
  327. do {
  328. _copy_pte(dst_ptep, src_ptep, addr);
  329. } while (dst_ptep++, src_ptep++, addr += PAGE_SIZE, addr != end);
  330. return 0;
  331. }
  332. static int copy_pmd(pud_t *dst_pudp, pud_t *src_pudp, unsigned long start,
  333. unsigned long end)
  334. {
  335. pmd_t *src_pmdp;
  336. pmd_t *dst_pmdp;
  337. unsigned long next;
  338. unsigned long addr = start;
  339. if (pud_none(READ_ONCE(*dst_pudp))) {
  340. dst_pmdp = (pmd_t *)get_safe_page(GFP_ATOMIC);
  341. if (!dst_pmdp)
  342. return -ENOMEM;
  343. pud_populate(&init_mm, dst_pudp, dst_pmdp);
  344. }
  345. dst_pmdp = pmd_offset(dst_pudp, start);
  346. src_pmdp = pmd_offset(src_pudp, start);
  347. do {
  348. pmd_t pmd = READ_ONCE(*src_pmdp);
  349. next = pmd_addr_end(addr, end);
  350. if (pmd_none(pmd))
  351. continue;
  352. if (pmd_table(pmd)) {
  353. if (copy_pte(dst_pmdp, src_pmdp, addr, next))
  354. return -ENOMEM;
  355. } else {
  356. set_pmd(dst_pmdp,
  357. __pmd(pmd_val(pmd) & ~PMD_SECT_RDONLY));
  358. }
  359. } while (dst_pmdp++, src_pmdp++, addr = next, addr != end);
  360. return 0;
  361. }
  362. static int copy_pud(pgd_t *dst_pgdp, pgd_t *src_pgdp, unsigned long start,
  363. unsigned long end)
  364. {
  365. pud_t *dst_pudp;
  366. pud_t *src_pudp;
  367. unsigned long next;
  368. unsigned long addr = start;
  369. if (pgd_none(READ_ONCE(*dst_pgdp))) {
  370. dst_pudp = (pud_t *)get_safe_page(GFP_ATOMIC);
  371. if (!dst_pudp)
  372. return -ENOMEM;
  373. pgd_populate(&init_mm, dst_pgdp, dst_pudp);
  374. }
  375. dst_pudp = pud_offset(dst_pgdp, start);
  376. src_pudp = pud_offset(src_pgdp, start);
  377. do {
  378. pud_t pud = READ_ONCE(*src_pudp);
  379. next = pud_addr_end(addr, end);
  380. if (pud_none(pud))
  381. continue;
  382. if (pud_table(pud)) {
  383. if (copy_pmd(dst_pudp, src_pudp, addr, next))
  384. return -ENOMEM;
  385. } else {
  386. set_pud(dst_pudp,
  387. __pud(pud_val(pud) & ~PMD_SECT_RDONLY));
  388. }
  389. } while (dst_pudp++, src_pudp++, addr = next, addr != end);
  390. return 0;
  391. }
  392. static int copy_page_tables(pgd_t *dst_pgdp, unsigned long start,
  393. unsigned long end)
  394. {
  395. unsigned long next;
  396. unsigned long addr = start;
  397. pgd_t *src_pgdp = pgd_offset_k(start);
  398. dst_pgdp = pgd_offset_raw(dst_pgdp, start);
  399. do {
  400. next = pgd_addr_end(addr, end);
  401. if (pgd_none(READ_ONCE(*src_pgdp)))
  402. continue;
  403. if (copy_pud(dst_pgdp, src_pgdp, addr, next))
  404. return -ENOMEM;
  405. } while (dst_pgdp++, src_pgdp++, addr = next, addr != end);
  406. return 0;
  407. }
  408. /*
  409. * Setup then Resume from the hibernate image using swsusp_arch_suspend_exit().
  410. *
  411. * Memory allocated by get_safe_page() will be dealt with by the hibernate code,
  412. * we don't need to free it here.
  413. */
  414. int swsusp_arch_resume(void)
  415. {
  416. int rc = 0;
  417. void *zero_page;
  418. size_t exit_size;
  419. pgd_t *tmp_pg_dir;
  420. phys_addr_t phys_hibernate_exit;
  421. void __noreturn (*hibernate_exit)(phys_addr_t, phys_addr_t, void *,
  422. void *, phys_addr_t, phys_addr_t);
  423. /*
  424. * Restoring the memory image will overwrite the ttbr1 page tables.
  425. * Create a second copy of just the linear map, and use this when
  426. * restoring.
  427. */
  428. tmp_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
  429. if (!tmp_pg_dir) {
  430. pr_err("Failed to allocate memory for temporary page tables.\n");
  431. rc = -ENOMEM;
  432. goto out;
  433. }
  434. rc = copy_page_tables(tmp_pg_dir, PAGE_OFFSET, 0);
  435. if (rc)
  436. goto out;
  437. /*
  438. * We need a zero page that is zero before & after resume in order to
  439. * to break before make on the ttbr1 page tables.
  440. */
  441. zero_page = (void *)get_safe_page(GFP_ATOMIC);
  442. if (!zero_page) {
  443. pr_err("Failed to allocate zero page.\n");
  444. rc = -ENOMEM;
  445. goto out;
  446. }
  447. /*
  448. * Locate the exit code in the bottom-but-one page, so that *NULL
  449. * still has disastrous affects.
  450. */
  451. hibernate_exit = (void *)PAGE_SIZE;
  452. exit_size = __hibernate_exit_text_end - __hibernate_exit_text_start;
  453. /*
  454. * Copy swsusp_arch_suspend_exit() to a safe page. This will generate
  455. * a new set of ttbr0 page tables and load them.
  456. */
  457. rc = create_safe_exec_page(__hibernate_exit_text_start, exit_size,
  458. (unsigned long)hibernate_exit,
  459. &phys_hibernate_exit,
  460. (void *)get_safe_page, GFP_ATOMIC);
  461. if (rc) {
  462. pr_err("Failed to create safe executable page for hibernate_exit code.\n");
  463. goto out;
  464. }
  465. /*
  466. * The hibernate exit text contains a set of el2 vectors, that will
  467. * be executed at el2 with the mmu off in order to reload hyp-stub.
  468. */
  469. __flush_dcache_area(hibernate_exit, exit_size);
  470. /*
  471. * KASLR will cause the el2 vectors to be in a different location in
  472. * the resumed kernel. Load hibernate's temporary copy into el2.
  473. *
  474. * We can skip this step if we booted at EL1, or are running with VHE.
  475. */
  476. if (el2_reset_needed()) {
  477. phys_addr_t el2_vectors = phys_hibernate_exit; /* base */
  478. el2_vectors += hibernate_el2_vectors -
  479. __hibernate_exit_text_start; /* offset */
  480. __hyp_set_vectors(el2_vectors);
  481. }
  482. hibernate_exit(virt_to_phys(tmp_pg_dir), resume_hdr.ttbr1_el1,
  483. resume_hdr.reenter_kernel, restore_pblist,
  484. resume_hdr.__hyp_stub_vectors, virt_to_phys(zero_page));
  485. out:
  486. return rc;
  487. }
  488. int hibernate_resume_nonboot_cpu_disable(void)
  489. {
  490. if (sleep_cpu < 0) {
  491. pr_err("Failing to resume from hibernate on an unknown CPU.\n");
  492. return -ENODEV;
  493. }
  494. return freeze_secondary_cpus(sleep_cpu);
  495. }