hibernate_64.c 8.7 KB

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