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