hibernate_64.c 7.4 KB

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