hibernate.c 14 KB

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