hibernate.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <linux/cpu.h>
  15. #include <linux/pgtable.h>
  16. #include <linux/types.h>
  17. #include <linux/crc32.h>
  18. #include <asm/e820/api.h>
  19. #include <asm/init.h>
  20. #include <asm/proto.h>
  21. #include <asm/page.h>
  22. #include <asm/mtrr.h>
  23. #include <asm/sections.h>
  24. #include <asm/suspend.h>
  25. #include <asm/tlbflush.h>
  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_pgt __visible;
  38. unsigned long relocated_restore_code __visible;
  39. /**
  40. * pfn_is_nosave - check if given pfn is in the 'nosave' section
  41. */
  42. int pfn_is_nosave(unsigned long pfn)
  43. {
  44. unsigned long nosave_begin_pfn;
  45. unsigned long nosave_end_pfn;
  46. nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT;
  47. nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
  48. return pfn >= nosave_begin_pfn && pfn < nosave_end_pfn;
  49. }
  50. struct restore_data_record {
  51. unsigned long jump_address;
  52. unsigned long jump_address_phys;
  53. unsigned long cr3;
  54. unsigned long magic;
  55. unsigned long e820_checksum;
  56. };
  57. /**
  58. * compute_e820_crc32 - calculate crc32 of a given e820 table
  59. *
  60. * @table: the e820 table to be calculated
  61. *
  62. * Return: the resulting checksum
  63. */
  64. static inline u32 compute_e820_crc32(struct e820_table *table)
  65. {
  66. int size = offsetof(struct e820_table, entries) +
  67. sizeof(struct e820_entry) * table->nr_entries;
  68. return ~crc32_le(~0, (unsigned char const *)table, size);
  69. }
  70. #ifdef CONFIG_X86_64
  71. #define RESTORE_MAGIC 0x23456789ABCDEF02UL
  72. #else
  73. #define RESTORE_MAGIC 0x12345679UL
  74. #endif
  75. /**
  76. * arch_hibernation_header_save - populate the architecture specific part
  77. * of a hibernation image header
  78. * @addr: address to save the data at
  79. */
  80. int arch_hibernation_header_save(void *addr, unsigned int max_size)
  81. {
  82. struct restore_data_record *rdr = addr;
  83. if (max_size < sizeof(struct restore_data_record))
  84. return -EOVERFLOW;
  85. rdr->magic = RESTORE_MAGIC;
  86. rdr->jump_address = (unsigned long)restore_registers;
  87. rdr->jump_address_phys = __pa_symbol(restore_registers);
  88. /*
  89. * The restore code fixes up CR3 and CR4 in the following sequence:
  90. *
  91. * [in hibernation asm]
  92. * 1. CR3 <= temporary page tables
  93. * 2. CR4 <= mmu_cr4_features (from the kernel that restores us)
  94. * 3. CR3 <= rdr->cr3
  95. * 4. CR4 <= mmu_cr4_features (from us, i.e. the image kernel)
  96. * [in restore_processor_state()]
  97. * 5. CR4 <= saved CR4
  98. * 6. CR3 <= saved CR3
  99. *
  100. * Our mmu_cr4_features has CR4.PCIDE=0, and toggling
  101. * CR4.PCIDE while CR3's PCID bits are nonzero is illegal, so
  102. * rdr->cr3 needs to point to valid page tables but must not
  103. * have any of the PCID bits set.
  104. */
  105. rdr->cr3 = restore_cr3 & ~CR3_PCID_MASK;
  106. rdr->e820_checksum = compute_e820_crc32(e820_table_firmware);
  107. return 0;
  108. }
  109. /**
  110. * arch_hibernation_header_restore - read the architecture specific data
  111. * from the hibernation image header
  112. * @addr: address to read the data from
  113. */
  114. int arch_hibernation_header_restore(void *addr)
  115. {
  116. struct restore_data_record *rdr = addr;
  117. if (rdr->magic != RESTORE_MAGIC) {
  118. pr_crit("Unrecognized hibernate image header format!\n");
  119. return -EINVAL;
  120. }
  121. restore_jump_address = rdr->jump_address;
  122. jump_address_phys = rdr->jump_address_phys;
  123. restore_cr3 = rdr->cr3;
  124. if (rdr->e820_checksum != compute_e820_crc32(e820_table_firmware)) {
  125. pr_crit("Hibernate inconsistent memory map detected!\n");
  126. return -ENODEV;
  127. }
  128. return 0;
  129. }
  130. int relocate_restore_code(void)
  131. {
  132. pgd_t *pgd;
  133. p4d_t *p4d;
  134. pud_t *pud;
  135. pmd_t *pmd;
  136. pte_t *pte;
  137. relocated_restore_code = get_safe_page(GFP_ATOMIC);
  138. if (!relocated_restore_code)
  139. return -ENOMEM;
  140. memcpy((void *)relocated_restore_code, core_restore_code, PAGE_SIZE);
  141. /* Make the page containing the relocated code executable */
  142. pgd = (pgd_t *)__va(read_cr3_pa()) +
  143. pgd_index(relocated_restore_code);
  144. p4d = p4d_offset(pgd, relocated_restore_code);
  145. if (p4d_large(*p4d)) {
  146. set_p4d(p4d, __p4d(p4d_val(*p4d) & ~_PAGE_NX));
  147. goto out;
  148. }
  149. pud = pud_offset(p4d, relocated_restore_code);
  150. if (pud_large(*pud)) {
  151. set_pud(pud, __pud(pud_val(*pud) & ~_PAGE_NX));
  152. goto out;
  153. }
  154. pmd = pmd_offset(pud, relocated_restore_code);
  155. if (pmd_large(*pmd)) {
  156. set_pmd(pmd, __pmd(pmd_val(*pmd) & ~_PAGE_NX));
  157. goto out;
  158. }
  159. pte = pte_offset_kernel(pmd, relocated_restore_code);
  160. set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_NX));
  161. out:
  162. __flush_tlb_all();
  163. return 0;
  164. }
  165. int arch_resume_nosmt(void)
  166. {
  167. int ret = 0;
  168. /*
  169. * We reached this while coming out of hibernation. This means
  170. * that SMT siblings are sleeping in hlt, as mwait is not safe
  171. * against control transition during resume (see comment in
  172. * hibernate_resume_nonboot_cpu_disable()).
  173. *
  174. * If the resumed kernel has SMT disabled, we have to take all the
  175. * SMT siblings out of hlt, and offline them again so that they
  176. * end up in mwait proper.
  177. *
  178. * Called with hotplug disabled.
  179. */
  180. cpu_hotplug_enable();
  181. if (cpu_smt_control == CPU_SMT_DISABLED ||
  182. cpu_smt_control == CPU_SMT_FORCE_DISABLED) {
  183. enum cpuhp_smt_control old = cpu_smt_control;
  184. ret = cpuhp_smt_enable();
  185. if (ret)
  186. goto out;
  187. ret = cpuhp_smt_disable(old);
  188. if (ret)
  189. goto out;
  190. }
  191. out:
  192. cpu_hotplug_disable();
  193. return ret;
  194. }