hibernate.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hibernation support for x86
  4. *
  5. * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
  6. * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
  7. * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
  8. */
  9. #include <linux/gfp.h>
  10. #include <linux/smp.h>
  11. #include <linux/suspend.h>
  12. #include <linux/scatterlist.h>
  13. #include <linux/kdebug.h>
  14. #include <crypto/hash.h>
  15. #include <asm/e820/api.h>
  16. #include <asm/init.h>
  17. #include <asm/proto.h>
  18. #include <asm/page.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/mtrr.h>
  21. #include <asm/sections.h>
  22. #include <asm/suspend.h>
  23. #include <asm/tlbflush.h>
  24. /*
  25. * Address to jump to in the last phase of restore in order to get to the image
  26. * kernel's text (this value is passed in the image header).
  27. */
  28. unsigned long restore_jump_address __visible;
  29. unsigned long jump_address_phys;
  30. /*
  31. * Value of the cr3 register from before the hibernation (this value is passed
  32. * in the image header).
  33. */
  34. unsigned long restore_cr3 __visible;
  35. unsigned long temp_pgt __visible;
  36. unsigned long relocated_restore_code __visible;
  37. /**
  38. * pfn_is_nosave - check if given pfn is in the 'nosave' section
  39. */
  40. int pfn_is_nosave(unsigned long pfn)
  41. {
  42. unsigned long nosave_begin_pfn;
  43. unsigned long nosave_end_pfn;
  44. nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
  45. nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
  46. return pfn >= nosave_begin_pfn && pfn < nosave_end_pfn;
  47. }
  48. #define MD5_DIGEST_SIZE 16
  49. struct restore_data_record {
  50. unsigned long jump_address;
  51. unsigned long jump_address_phys;
  52. unsigned long cr3;
  53. unsigned long magic;
  54. u8 e820_digest[MD5_DIGEST_SIZE];
  55. };
  56. #if IS_BUILTIN(CONFIG_CRYPTO_MD5)
  57. /**
  58. * get_e820_md5 - calculate md5 according to given e820 table
  59. *
  60. * @table: the e820 table to be calculated
  61. * @buf: the md5 result to be stored to
  62. */
  63. static int get_e820_md5(struct e820_table *table, void *buf)
  64. {
  65. struct crypto_shash *tfm;
  66. struct shash_desc *desc;
  67. int size;
  68. int ret = 0;
  69. tfm = crypto_alloc_shash("md5", 0, 0);
  70. if (IS_ERR(tfm))
  71. return -ENOMEM;
  72. desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
  73. GFP_KERNEL);
  74. if (!desc) {
  75. ret = -ENOMEM;
  76. goto free_tfm;
  77. }
  78. desc->tfm = tfm;
  79. desc->flags = 0;
  80. size = offsetof(struct e820_table, entries) +
  81. sizeof(struct e820_entry) * table->nr_entries;
  82. if (crypto_shash_digest(desc, (u8 *)table, size, buf))
  83. ret = -EINVAL;
  84. kzfree(desc);
  85. free_tfm:
  86. crypto_free_shash(tfm);
  87. return ret;
  88. }
  89. static int hibernation_e820_save(void *buf)
  90. {
  91. return get_e820_md5(e820_table_firmware, buf);
  92. }
  93. static bool hibernation_e820_mismatch(void *buf)
  94. {
  95. int ret;
  96. u8 result[MD5_DIGEST_SIZE];
  97. memset(result, 0, MD5_DIGEST_SIZE);
  98. /* If there is no digest in suspend kernel, let it go. */
  99. if (!memcmp(result, buf, MD5_DIGEST_SIZE))
  100. return false;
  101. ret = get_e820_md5(e820_table_firmware, result);
  102. if (ret)
  103. return true;
  104. return memcmp(result, buf, MD5_DIGEST_SIZE) ? true : false;
  105. }
  106. #else
  107. static int hibernation_e820_save(void *buf)
  108. {
  109. return 0;
  110. }
  111. static bool hibernation_e820_mismatch(void *buf)
  112. {
  113. /* If md5 is not builtin for restore kernel, let it go. */
  114. return false;
  115. }
  116. #endif
  117. #ifdef CONFIG_X86_64
  118. #define RESTORE_MAGIC 0x23456789ABCDEF01UL
  119. #else
  120. #define RESTORE_MAGIC 0x12345678UL
  121. #endif
  122. /**
  123. * arch_hibernation_header_save - populate the architecture specific part
  124. * of a hibernation image header
  125. * @addr: address to save the data at
  126. */
  127. int arch_hibernation_header_save(void *addr, unsigned int max_size)
  128. {
  129. struct restore_data_record *rdr = addr;
  130. if (max_size < sizeof(struct restore_data_record))
  131. return -EOVERFLOW;
  132. rdr->magic = RESTORE_MAGIC;
  133. rdr->jump_address = (unsigned long)restore_registers;
  134. rdr->jump_address_phys = __pa_symbol(restore_registers);
  135. /*
  136. * The restore code fixes up CR3 and CR4 in the following sequence:
  137. *
  138. * [in hibernation asm]
  139. * 1. CR3 <= temporary page tables
  140. * 2. CR4 <= mmu_cr4_features (from the kernel that restores us)
  141. * 3. CR3 <= rdr->cr3
  142. * 4. CR4 <= mmu_cr4_features (from us, i.e. the image kernel)
  143. * [in restore_processor_state()]
  144. * 5. CR4 <= saved CR4
  145. * 6. CR3 <= saved CR3
  146. *
  147. * Our mmu_cr4_features has CR4.PCIDE=0, and toggling
  148. * CR4.PCIDE while CR3's PCID bits are nonzero is illegal, so
  149. * rdr->cr3 needs to point to valid page tables but must not
  150. * have any of the PCID bits set.
  151. */
  152. rdr->cr3 = restore_cr3 & ~CR3_PCID_MASK;
  153. return hibernation_e820_save(rdr->e820_digest);
  154. }
  155. /**
  156. * arch_hibernation_header_restore - read the architecture specific data
  157. * from the hibernation image header
  158. * @addr: address to read the data from
  159. */
  160. int arch_hibernation_header_restore(void *addr)
  161. {
  162. struct restore_data_record *rdr = addr;
  163. if (rdr->magic != RESTORE_MAGIC) {
  164. pr_crit("Unrecognized hibernate image header format!\n");
  165. return -EINVAL;
  166. }
  167. restore_jump_address = rdr->jump_address;
  168. jump_address_phys = rdr->jump_address_phys;
  169. restore_cr3 = rdr->cr3;
  170. if (hibernation_e820_mismatch(rdr->e820_digest)) {
  171. pr_crit("Hibernate inconsistent memory map detected!\n");
  172. return -ENODEV;
  173. }
  174. return 0;
  175. }
  176. int relocate_restore_code(void)
  177. {
  178. pgd_t *pgd;
  179. p4d_t *p4d;
  180. pud_t *pud;
  181. pmd_t *pmd;
  182. pte_t *pte;
  183. relocated_restore_code = get_safe_page(GFP_ATOMIC);
  184. if (!relocated_restore_code)
  185. return -ENOMEM;
  186. memcpy((void *)relocated_restore_code, core_restore_code, PAGE_SIZE);
  187. /* Make the page containing the relocated code executable */
  188. pgd = (pgd_t *)__va(read_cr3_pa()) +
  189. pgd_index(relocated_restore_code);
  190. p4d = p4d_offset(pgd, relocated_restore_code);
  191. if (p4d_large(*p4d)) {
  192. set_p4d(p4d, __p4d(p4d_val(*p4d) & ~_PAGE_NX));
  193. goto out;
  194. }
  195. pud = pud_offset(p4d, relocated_restore_code);
  196. if (pud_large(*pud)) {
  197. set_pud(pud, __pud(pud_val(*pud) & ~_PAGE_NX));
  198. goto out;
  199. }
  200. pmd = pmd_offset(pud, relocated_restore_code);
  201. if (pmd_large(*pmd)) {
  202. set_pmd(pmd, __pmd(pmd_val(*pmd) & ~_PAGE_NX));
  203. goto out;
  204. }
  205. pte = pte_offset_kernel(pmd, relocated_restore_code);
  206. set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_NX));
  207. out:
  208. __flush_tlb_all();
  209. return 0;
  210. }