hibernate_64.c 9.5 KB

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