Skip to content

Commit d158cbd

Browse files
rjwysockiLinus Torvalds
authored andcommitted
Hibernation: Arbitrary boot kernel support on x86_64
Make it possible to restore a hibernation image on x86_64 with the help of a kernel different from the one in the image. The idea is to split the core restoration code into two separate parts and to place each of them in a different page.  The first part belongs to the boot kernel and is executed as the last step of the image kernel's memory restoration procedure.  Before being executed, it is relocated to a safe page that won't be overwritten while copying the image kernel pages. The final operation performed by it is a jump to the second part of the core restoration code that belongs to the image kernel and has just been restored. This code makes the CPU switch to the image kernel's page tables and restores the state of general purpose registers (including the stack pointer) from before the hibernation. The main issue with this idea is that in order to jump to the second part of the core restoration code the boot kernel needs to know its address.  However, this address may be passed to it in the image header.  Namely, the part of the image header previously used for checking if the version of the image kernel is correct can be replaced with some architecture specific data that will allow the boot kernel to jump to the right address within the image kernel.  These data should also be used for checking if the image kernel is compatible with the boot kernel (as far as the memory restroration procedure is concerned). It can be done, for example, with the help of a "magic" value that has to be equal in both kernels, so that they can be regarded as compatible. Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl> Acked-by: Pavel Machek <pavel@ucw.cz> Cc: Andi Kleen <ak@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d307c4a commit d158cbd

4 files changed

Lines changed: 94 additions & 8 deletions

File tree

‎arch/x86/kernel/suspend_64.c‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,16 @@ void fix_processor_context(void)
150150
/* Defined in arch/x86_64/kernel/suspend_asm.S */
151151
extern int restore_image(void);
152152

153+
/*
154+
* Address to jump to in the last phase of restore in order to get to the image
155+
* kernel's text (this value is passed in the image header).
156+
*/
157+
unsigned long restore_jump_address;
158+
153159
pgd_t *temp_level4_pgt;
154160

161+
void *relocated_restore_code;
162+
155163
static int res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
156164
{
157165
long i, j;
@@ -175,7 +183,7 @@ static int res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long en
175183

176184
if (paddr >= end)
177185
break;
178-
pe = _PAGE_NX | _PAGE_PSE | _KERNPG_TABLE | paddr;
186+
pe = __PAGE_KERNEL_LARGE_EXEC | paddr;
179187
pe &= __supported_pte_mask;
180188
set_pmd(pmd, __pmd(pe));
181189
}
@@ -222,6 +230,13 @@ int swsusp_arch_resume(void)
222230
/* We have got enough memory and from now on we cannot recover */
223231
if ((error = set_up_temporary_mappings()))
224232
return error;
233+
234+
relocated_restore_code = (void *)get_safe_page(GFP_ATOMIC);
235+
if (!relocated_restore_code)
236+
return -ENOMEM;
237+
memcpy(relocated_restore_code, &core_restore_code,
238+
&restore_registers - &core_restore_code);
239+
225240
restore_image();
226241
return 0;
227242
}
@@ -236,4 +251,41 @@ int pfn_is_nosave(unsigned long pfn)
236251
unsigned long nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT;
237252
return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
238253
}
254+
255+
struct restore_data_record {
256+
unsigned long jump_address;
257+
unsigned long control;
258+
};
259+
260+
#define RESTORE_MAGIC 0x0123456789ABCDEFUL
261+
262+
/**
263+
* arch_hibernation_header_save - populate the architecture specific part
264+
* of a hibernation image header
265+
* @addr: address to save the data at
266+
*/
267+
int arch_hibernation_header_save(void *addr, unsigned int max_size)
268+
{
269+
struct restore_data_record *rdr = addr;
270+
271+
if (max_size < sizeof(struct restore_data_record))
272+
return -EOVERFLOW;
273+
rdr->jump_address = restore_jump_address;
274+
rdr->control = (restore_jump_address ^ RESTORE_MAGIC);
275+
return 0;
276+
}
277+
278+
/**
279+
* arch_hibernation_header_restore - read the architecture specific data
280+
* from the hibernation image header
281+
* @addr: address to read the data from
282+
*/
283+
int arch_hibernation_header_restore(void *addr)
284+
{
285+
struct restore_data_record *rdr = addr;
286+
287+
restore_jump_address = rdr->jump_address;
288+
return (rdr->control == (restore_jump_address ^ RESTORE_MAGIC)) ?
289+
0 : -EINVAL;
290+
}
239291
#endif /* CONFIG_HIBERNATION */

‎arch/x86/kernel/suspend_asm_64.S‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
*
33
* Distribute under GPLv2.
44
*
5-
* swsusp_arch_resume may not use any stack, nor any variable that is
6-
* not "NoSave" during copying pages:
5+
* swsusp_arch_resume must not use any stack or any nonlocal variables while
6+
* copying pages:
77
*
88
* Its rewriting one kernel image with another. What is stack in "old"
99
* image could very well be data page in "new" image, and overwriting
@@ -36,6 +36,10 @@ ENTRY(swsusp_arch_suspend)
3636
movq %r15, saved_context_r15(%rip)
3737
pushfq ; popq saved_context_eflags(%rip)
3838

39+
/* save the address of restore_registers */
40+
movq $restore_registers, %rax
41+
movq %rax, restore_jump_address(%rip)
42+
3943
call swsusp_save
4044
ret
4145

@@ -54,22 +58,45 @@ ENTRY(restore_image)
5458
movq %rcx, %cr3;
5559
movq %rax, %cr4; # turn PGE back on
5660

61+
/* prepare to jump to the image kernel */
62+
movq restore_jump_address(%rip), %rax
63+
64+
/* prepare to copy image data to their original locations */
5765
movq restore_pblist(%rip), %rdx
66+
movq relocated_restore_code(%rip), %rcx
67+
jmpq *%rcx
68+
69+
/* code below has been relocated to a safe page */
70+
ENTRY(core_restore_code)
5871
loop:
5972
testq %rdx, %rdx
6073
jz done
6174

6275
/* get addresses from the pbe and copy the page */
6376
movq pbe_address(%rdx), %rsi
6477
movq pbe_orig_address(%rdx), %rdi
65-
movq $512, %rcx
78+
movq $(PAGE_SIZE >> 3), %rcx
6679
rep
6780
movsq
6881

6982
/* progress to the next pbe */
7083
movq pbe_next(%rdx), %rdx
7184
jmp loop
7285
done:
86+
/* jump to the restore_registers address from the image header */
87+
jmpq *%rax
88+
/*
89+
* NOTE: This assumes that the boot kernel's text mapping covers the
90+
* image kernel's page containing restore_registers and the address of
91+
* this page is the same as in the image kernel's text mapping (it
92+
* should always be true, because the text mapping is linear, starting
93+
* from 0, and is supposed to cover the entire kernel text for every
94+
* kernel).
95+
*
96+
* code below belongs to the image kernel
97+
*/
98+
99+
ENTRY(restore_registers)
73100
/* go back to the original page tables */
74101
movq $(init_level4_pgt - __START_KERNEL_map), %rax
75102
addq phys_base(%rip), %rax
@@ -84,12 +111,9 @@ done:
84111
movq %rcx, %cr3
85112
movq %rax, %cr4; # turn PGE back on
86113

87-
movl $24, %eax
88-
movl %eax, %ds
89-
90114
movq saved_context_esp(%rip), %rsp
91115
movq saved_context_ebp(%rip), %rbp
92-
/* Don't restore %rax, it must be 0 anyway */
116+
/* restore GPRs (we don't restore %rax, it must be 0 anyway) */
93117
movq saved_context_ebx(%rip), %rbx
94118
movq saved_context_ecx(%rip), %rcx
95119
movq saved_context_edx(%rip), %rdx
@@ -107,4 +131,7 @@ done:
107131

108132
xorq %rax, %rax
109133

134+
/* tell the hibernation core that we've just restored the memory */
135+
movq %rax, in_suspend(%rip)
136+
110137
ret

‎arch/x86_64/Kconfig‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,11 @@ menu "Power management options"
716716

717717
source kernel/power/Kconfig
718718

719+
config ARCH_HIBERNATION_HEADER
720+
bool
721+
depends on HIBERNATION
722+
default y
723+
719724
source "drivers/acpi/Kconfig"
720725

721726
source "arch/x86/kernel/cpufreq/Kconfig"

‎include/asm-x86/suspend_64.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ extern unsigned long saved_rdi;
5353

5454
/* routines for saving/restoring kernel state */
5555
extern int acpi_save_state_mem(void);
56+
extern char core_restore_code;
57+
extern char restore_registers;

0 commit comments

Comments
 (0)