hibernate_64.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Hibernation support for x86-64
  3. *
  4. * Distribute under GPLv2
  5. *
  6. * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
  7. * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
  8. * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
  9. */
  10. #include <linux/gfp.h>
  11. #include <linux/smp.h>
  12. #include <linux/suspend.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/kdebug.h>
  15. #include <linux/cpu.h>
  16. #include <crypto/hash.h>
  17. #include <asm/e820/api.h>
  18. #include <asm/init.h>
  19. #include <asm/proto.h>
  20. #include <asm/page.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/mtrr.h>
  23. #include <asm/sections.h>
  24. #include <asm/suspend.h>
  25. #include <asm/tlbflush.h>
  26. /* Defined in hibernate_asm_64.S */
  27. extern asmlinkage __visible int restore_image(void);
  28. /*
  29. * Address to jump to in the last phase of restore in order to get to the image
  30. * kernel's text (this value is passed in the image header).
  31. */
  32. unsigned long restore_jump_address __visible;
  33. unsigned long jump_address_phys;
  34. /*
  35. * Value of the cr3 register from before the hibernation (this value is passed
  36. * in the image header).
  37. */
  38. unsigned long restore_cr3 __visible;
  39. unsigned long temp_level4_pgt __visible;
  40. unsigned long relocated_restore_code __visible;
  41. static int set_up_temporary_text_mapping(pgd_t *pgd)
  42. {
  43. pmd_t *pmd;
  44. pud_t *pud;
  45. p4d_t *p4d = NULL;
  46. pgprot_t pgtable_prot = __pgprot(_KERNPG_TABLE);
  47. pgprot_t pmd_text_prot = __pgprot(__PAGE_KERNEL_LARGE_EXEC);
  48. /* Filter out unsupported __PAGE_KERNEL* bits: */
  49. pgprot_val(pmd_text_prot) &= __default_kernel_pte_mask;
  50. pgprot_val(pgtable_prot) &= __default_kernel_pte_mask;
  51. /*
  52. * The new mapping only has to cover the page containing the image
  53. * kernel's entry point (jump_address_phys), because the switch over to
  54. * it is carried out by relocated code running from a page allocated
  55. * specifically for this purpose and covered by the identity mapping, so
  56. * the temporary kernel text mapping is only needed for the final jump.
  57. * Moreover, in that mapping the virtual address of the image kernel's
  58. * entry point must be the same as its virtual address in the image
  59. * kernel (restore_jump_address), so the image kernel's
  60. * restore_registers() code doesn't find itself in a different area of
  61. * the virtual address space after switching over to the original page
  62. * tables used by the image kernel.
  63. */
  64. if (pgtable_l5_enabled()) {
  65. p4d = (p4d_t *)get_safe_page(GFP_ATOMIC);
  66. if (!p4d)
  67. return -ENOMEM;
  68. }
  69. pud = (pud_t *)get_safe_page(GFP_ATOMIC);
  70. if (!pud)
  71. return -ENOMEM;
  72. pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
  73. if (!pmd)
  74. return -ENOMEM;
  75. set_pmd(pmd + pmd_index(restore_jump_address),
  76. __pmd((jump_address_phys & PMD_MASK) | pgprot_val(pmd_text_prot)));
  77. set_pud(pud + pud_index(restore_jump_address),
  78. __pud(__pa(pmd) | pgprot_val(pgtable_prot)));
  79. if (p4d) {
  80. p4d_t new_p4d = __p4d(__pa(pud) | pgprot_val(pgtable_prot));
  81. pgd_t new_pgd = __pgd(__pa(p4d) | pgprot_val(pgtable_prot));
  82. set_p4d(p4d + p4d_index(restore_jump_address), new_p4d);
  83. set_pgd(pgd + pgd_index(restore_jump_address), new_pgd);
  84. } else {
  85. /* No p4d for 4-level paging: point the pgd to the pud page table */
  86. pgd_t new_pgd = __pgd(__pa(pud) | pgprot_val(pgtable_prot));
  87. set_pgd(pgd + pgd_index(restore_jump_address), new_pgd);
  88. }
  89. return 0;
  90. }
  91. static void *alloc_pgt_page(void *context)
  92. {
  93. return (void *)get_safe_page(GFP_ATOMIC);
  94. }
  95. static int set_up_temporary_mappings(void)
  96. {
  97. struct x86_mapping_info info = {
  98. .alloc_pgt_page = alloc_pgt_page,
  99. .page_flag = __PAGE_KERNEL_LARGE_EXEC,
  100. .offset = __PAGE_OFFSET,
  101. };
  102. unsigned long mstart, mend;
  103. pgd_t *pgd;
  104. int result;
  105. int i;
  106. pgd = (pgd_t *)get_safe_page(GFP_ATOMIC);
  107. if (!pgd)
  108. return -ENOMEM;
  109. /* Prepare a temporary mapping for the kernel text */
  110. result = set_up_temporary_text_mapping(pgd);
  111. if (result)
  112. return result;
  113. /* Set up the direct mapping from scratch */
  114. for (i = 0; i < nr_pfn_mapped; i++) {
  115. mstart = pfn_mapped[i].start << PAGE_SHIFT;
  116. mend = pfn_mapped[i].end << PAGE_SHIFT;
  117. result = kernel_ident_mapping_init(&info, pgd, mstart, mend);
  118. if (result)
  119. return result;
  120. }
  121. temp_level4_pgt = __pa(pgd);
  122. return 0;
  123. }
  124. static int relocate_restore_code(void)
  125. {
  126. pgd_t *pgd;
  127. p4d_t *p4d;
  128. pud_t *pud;
  129. pmd_t *pmd;
  130. pte_t *pte;
  131. relocated_restore_code = get_safe_page(GFP_ATOMIC);
  132. if (!relocated_restore_code)
  133. return -ENOMEM;
  134. memcpy((void *)relocated_restore_code, core_restore_code, PAGE_SIZE);
  135. /* Make the page containing the relocated code executable */
  136. pgd = (pgd_t *)__va(read_cr3_pa()) +
  137. pgd_index(relocated_restore_code);
  138. p4d = p4d_offset(pgd, relocated_restore_code);
  139. if (p4d_large(*p4d)) {
  140. set_p4d(p4d, __p4d(p4d_val(*p4d) & ~_PAGE_NX));
  141. goto out;
  142. }
  143. pud = pud_offset(p4d, relocated_restore_code);
  144. if (pud_large(*pud)) {
  145. set_pud(pud, __pud(pud_val(*pud) & ~_PAGE_NX));
  146. goto out;
  147. }
  148. pmd = pmd_offset(pud, relocated_restore_code);
  149. if (pmd_large(*pmd)) {
  150. set_pmd(pmd, __pmd(pmd_val(*pmd) & ~_PAGE_NX));
  151. goto out;
  152. }
  153. pte = pte_offset_kernel(pmd, relocated_restore_code);
  154. set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_NX));
  155. out:
  156. __flush_tlb_all();
  157. return 0;
  158. }
  159. asmlinkage int swsusp_arch_resume(void)
  160. {
  161. int error;
  162. /* We have got enough memory and from now on we cannot recover */
  163. error = set_up_temporary_mappings();
  164. if (error)
  165. return error;
  166. error = relocate_restore_code();
  167. if (error)
  168. return error;
  169. restore_image();
  170. return 0;
  171. }
  172. /*
  173. * pfn_is_nosave - check if given pfn is in the 'nosave' section
  174. */
  175. int pfn_is_nosave(unsigned long pfn)
  176. {
  177. unsigned long nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
  178. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
  179. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  180. }
  181. #define MD5_DIGEST_SIZE 16
  182. struct restore_data_record {
  183. unsigned long jump_address;
  184. unsigned long jump_address_phys;
  185. unsigned long cr3;
  186. unsigned long magic;
  187. u8 e820_digest[MD5_DIGEST_SIZE];
  188. };
  189. #define RESTORE_MAGIC 0x23456789ABCDEF01UL
  190. #if IS_BUILTIN(CONFIG_CRYPTO_MD5)
  191. /**
  192. * get_e820_md5 - calculate md5 according to given e820 table
  193. *
  194. * @table: the e820 table to be calculated
  195. * @buf: the md5 result to be stored to
  196. */
  197. static int get_e820_md5(struct e820_table *table, void *buf)
  198. {
  199. struct crypto_shash *tfm;
  200. struct shash_desc *desc;
  201. int size;
  202. int ret = 0;
  203. tfm = crypto_alloc_shash("md5", 0, 0);
  204. if (IS_ERR(tfm))
  205. return -ENOMEM;
  206. desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
  207. GFP_KERNEL);
  208. if (!desc) {
  209. ret = -ENOMEM;
  210. goto free_tfm;
  211. }
  212. desc->tfm = tfm;
  213. desc->flags = 0;
  214. size = offsetof(struct e820_table, entries) +
  215. sizeof(struct e820_entry) * table->nr_entries;
  216. if (crypto_shash_digest(desc, (u8 *)table, size, buf))
  217. ret = -EINVAL;
  218. kzfree(desc);
  219. free_tfm:
  220. crypto_free_shash(tfm);
  221. return ret;
  222. }
  223. static int hibernation_e820_save(void *buf)
  224. {
  225. return get_e820_md5(e820_table_firmware, buf);
  226. }
  227. static bool hibernation_e820_mismatch(void *buf)
  228. {
  229. int ret;
  230. u8 result[MD5_DIGEST_SIZE];
  231. memset(result, 0, MD5_DIGEST_SIZE);
  232. /* If there is no digest in suspend kernel, let it go. */
  233. if (!memcmp(result, buf, MD5_DIGEST_SIZE))
  234. return false;
  235. ret = get_e820_md5(e820_table_firmware, result);
  236. if (ret)
  237. return true;
  238. return memcmp(result, buf, MD5_DIGEST_SIZE) ? true : false;
  239. }
  240. #else
  241. static int hibernation_e820_save(void *buf)
  242. {
  243. return 0;
  244. }
  245. static bool hibernation_e820_mismatch(void *buf)
  246. {
  247. /* If md5 is not builtin for restore kernel, let it go. */
  248. return false;
  249. }
  250. #endif
  251. /**
  252. * arch_hibernation_header_save - populate the architecture specific part
  253. * of a hibernation image header
  254. * @addr: address to save the data at
  255. */
  256. int arch_hibernation_header_save(void *addr, unsigned int max_size)
  257. {
  258. struct restore_data_record *rdr = addr;
  259. if (max_size < sizeof(struct restore_data_record))
  260. return -EOVERFLOW;
  261. rdr->jump_address = (unsigned long)restore_registers;
  262. rdr->jump_address_phys = __pa_symbol(restore_registers);
  263. /*
  264. * The restore code fixes up CR3 and CR4 in the following sequence:
  265. *
  266. * [in hibernation asm]
  267. * 1. CR3 <= temporary page tables
  268. * 2. CR4 <= mmu_cr4_features (from the kernel that restores us)
  269. * 3. CR3 <= rdr->cr3
  270. * 4. CR4 <= mmu_cr4_features (from us, i.e. the image kernel)
  271. * [in restore_processor_state()]
  272. * 5. CR4 <= saved CR4
  273. * 6. CR3 <= saved CR3
  274. *
  275. * Our mmu_cr4_features has CR4.PCIDE=0, and toggling
  276. * CR4.PCIDE while CR3's PCID bits are nonzero is illegal, so
  277. * rdr->cr3 needs to point to valid page tables but must not
  278. * have any of the PCID bits set.
  279. */
  280. rdr->cr3 = restore_cr3 & ~CR3_PCID_MASK;
  281. rdr->magic = RESTORE_MAGIC;
  282. return hibernation_e820_save(rdr->e820_digest);
  283. }
  284. /**
  285. * arch_hibernation_header_restore - read the architecture specific data
  286. * from the hibernation image header
  287. * @addr: address to read the data from
  288. */
  289. int arch_hibernation_header_restore(void *addr)
  290. {
  291. struct restore_data_record *rdr = addr;
  292. restore_jump_address = rdr->jump_address;
  293. jump_address_phys = rdr->jump_address_phys;
  294. restore_cr3 = rdr->cr3;
  295. if (rdr->magic != RESTORE_MAGIC) {
  296. pr_crit("Unrecognized hibernate image header format!\n");
  297. return -EINVAL;
  298. }
  299. if (hibernation_e820_mismatch(rdr->e820_digest)) {
  300. pr_crit("Hibernate inconsistent memory map detected!\n");
  301. return -ENODEV;
  302. }
  303. return 0;
  304. }
  305. int arch_resume_nosmt(void)
  306. {
  307. int ret = 0;
  308. /*
  309. * We reached this while coming out of hibernation. This means
  310. * that SMT siblings are sleeping in hlt, as mwait is not safe
  311. * against control transition during resume (see comment in
  312. * hibernate_resume_nonboot_cpu_disable()).
  313. *
  314. * If the resumed kernel has SMT disabled, we have to take all the
  315. * SMT siblings out of hlt, and offline them again so that they
  316. * end up in mwait proper.
  317. *
  318. * Called with hotplug disabled.
  319. */
  320. cpu_hotplug_enable();
  321. if (cpu_smt_control == CPU_SMT_DISABLED ||
  322. cpu_smt_control == CPU_SMT_FORCE_DISABLED) {
  323. enum cpuhp_smt_control old = cpu_smt_control;
  324. ret = cpuhp_smt_enable();
  325. if (ret)
  326. goto out;
  327. ret = cpuhp_smt_disable(old);
  328. if (ret)
  329. goto out;
  330. }
  331. out:
  332. cpu_hotplug_disable();
  333. return ret;
  334. }