hibernate.c 14 KB

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