Hibernation: Arbitrary boot kernel support on x86_64
Subsystems (2)
cover Hibernation: Arbitrary boot kernel support on x86_64
Hi,
The following two patches make it possible to restore the memory state from a hibernation image with the help of a kernel different from the image one.
The first patch adds the generic, platform independent code needed for that. The second patch implements the idea on x86_64.
Greetings,
Rafael
PATCH 1/2 Hibernation: Arbitrary boot kernel support - generic code
From: Rafael J. Wysocki <[email protected]>
Add the bits needed for supporting arbitrary boot kernels to the common hibernation code.
To support arbitrary boot kernels, make it possible to replace the 'struct new_utsname' and the kernel version in the hibernation image header by some architecture specific data that will be used to verify if the image is valid and to restore the image.
--- kernel/power/power.h | 6 +++- kernel/power/snapshot.c | 60 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 17 deletions(-)
kernel/power/power.h +4 -2
@@ -11,14 +11,16 @@ struct swsusp_info {unsigned long size; } __attribute__((aligned(PAGE_SIZE))); - -#ifdef CONFIG_HIBERNATION +/* Maximum size of architecture specific data in a hibernation header */ +#define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4) +/* * Keep some memory free so that I/O operations can succeed without paging * [Might this be more than 4 MB?] */ #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) +/* * Keep 1 MB of memory free so that device drivers can allocate some pages in * their .suspend() routines without breaking the suspend to disk.
kernel/power/snapshot.c +45 -15
91 lines not on this page · raw message
PATCH 2/2 Hibernation: Arbitrary boot kernel support on x86_64
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
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. It restores all of the image kernel's memory that has not been restored yet except for the one page containing the very code that is being executed at that time. 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 restores the last remaining page of the image kernel's memory containing the first, already executed, part of the core restoration code (temporary page tables created by the boot kernel are used at this stage). It also 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 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.
--- arch/x86_64/kernel/suspend.c | 43 ++++++++++++++++++++++++++ arch/x86_64/kernel/suspend_asm.S | 63 +++++++++++++++++++++++++++++++++------ include/asm-x86_64/suspend.h | 6 +++ 3 files changed, 103 insertions(+), 9 deletions(-)
arch/x86_64/kernel/suspend_asm.S +54 -9
@@ -2,8 +2,8 @@ * * Distribute under GPLv2. * - * swsusp_arch_resume may not use any stack, nor any variable that is - * not "NoSave" during copying pages: + * swsusp_arch_resume must not use any stack or any nonlocal variables while + * copying pages: * * Its rewriting one kernel image with another. What is stack in "old" * image could very well be data page in "new" image, and overwriting
@@ -36,10 +36,20 @@ ENTRY(swsusp_arch_suspend) pushfq popq pt_regs_eflags(%rax) + /* save the address of restore_registers */ + movq $restore_registers, %rax + movq %rax, restore_jump_address(%rip) + call swsusp_save ret ENTRY(restore_image) + /* compute the address of the page we are at and store it in R9 */ + movq $(restore_image - __START_KERNEL_map), %rax + movq $__PAGE_OFFSET, %r9 + addq %rax, %r9 + andq $PAGE_MASK, %r9 + /* switch to temporary page tables */ movq $__PAGE_OFFSET, %rdx movq temp_level4_pgt(%rip), %rax
@@ -54,6 +64,11 @@ ENTRY(restore_image) movq %rcx, %cr3; movq %rax, %cr4; # turn PGE back on + /* prepare to jump to the image kernel */ + movq restore_jump_address(%rip), %rax + + /* copy image data to their original locations */ + xorq %r10, %r10 movq restore_pblist(%rip), %rdx loop: testq %rdx, %rdx
@@ -62,16 +77,46 @@ loop: /* get addresses from the pbe and copy the page */ movq pbe_address(%rdx), %rsi movq pbe_orig_address(%rdx), %rdi - movq $512, %rcx + /* skip the page we are at (address stored in R9) */ + cmpq %rdi, %r9 + jne 1f + /* save the address of the data to be copied to the skipped page */ + movq %rsi, %r10 + jmp 2f +1: movq $(PAGE_SIZE >> 3), %rcx rep movsq /* progress to the next pbe */ - movq pbe_next(%rdx), %rdx +2: movq pbe_next(%rdx), %rdx jmp loop done: + /* jump to the restore_registers address from the image header */ + jmpq *%rax + /* + * NOTE: This assumes that the boot kernel's text mapping covers the + * image kernel's page containing restore_registers and the address of + * this page is the same as in the image kernel's text mapping (it + * should always be true, because the text mapping is linear, starting + * from 0, and is supposed to cover the entire kernel text for every + * kernel). + */ + +.balign PAGE_SIZE + /* code below belongs to the image kernel */ +ENTRY(restore_registers) + /* check if we have one more image page to copy */ + testq %r10, %r10 + jz 1f + /* copy the skipped page */ + movq %r10, %rsi + movq %r9, %rdi + movq $(PAGE_SIZE >> 3), %rcx + rep + movsq + /* go back to the original page tables */ - movq $(init_level4_pgt - __START_KERNEL_map), %rax +1: movq $(init_level4_pgt - __START_KERNEL_map), %rax addq phys_base(%rip), %rax movq %rax, %cr3
@@ -84,10 +129,7 @@ done: movq %rcx, %cr3 movq %rax, %cr4; # turn PGE back on - movl $24, %eax - movl %eax, %ds - - /* We don't restore %rax, it must be 0 anyway */ + /* restore GPRs (we don't restore %rax, it must be 0 anyway) */ movq $saved_context, %rax movq pt_regs_rsp(%rax), %rsp movq pt_regs_rbp(%rax), %rbp
@@ -109,4 +151,7 @@ done: xorq %rax, %rax + /* tell the hibernation core that we've just restored the memory */ + movq %rax, in_suspend(%rip) + ret
arch/x86_64/kernel/suspend.c +43 -0
53 lines not on this page · raw message
include/asm-x86_64/suspend.h +6 -0
@@ -43,4 +43,10 @@ extern void fix_processor_context(void);/* routines for saving/restoring kernel state */ extern int acpi_save_state_mem (void ); +#define ARCH_HAS_HIBERNATION_HEADER + +/* arch/x86_64/kernel/suspend.c */ +extern int arch_hibernation_header_save (void *addr,unsigned int max_size); +extern int arch_hibernation_header_restore (void *addr); +#endif /* __ASM_X86_64_SUSPEND_H */
Quoted 1 line · Rafael J. Wysocki
The idea is [...]
Shouldn't that actually be added into some generic (non-x86-64) doc file?
johannes
Quoted 6 lines · Johannes Berg
Quoted 1 line · Rafael J. Wysocki
The idea is [...]
Shouldn't that actually be added into some generic (non-x86-64) doc file?
Yes, I will update the documentation in the future.
Greetings,
Rafael
Quoted 6 lines · Johannes Berg
Quoted 1 line · Rafael J. Wysocki
The idea is [...]
Shouldn't that actually be added into some generic (non-x86-64) doc file?
Yes, I will update the documentation in the future.
Greetings,
Rafael
Quoted 1 line · Rafael J. Wysocki
The idea is [...]
Shouldn't that actually be added into some generic (non-x86-64) doc file?
johannes
Hi!
Quoted 7 lines
From: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on
x86_64with 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
What happens in case where both parts want to be
at the same place? (Like kernel being restored is 4KB smaller, so that routines now collide?)
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
include/asm-x86_64/suspend.h extern void fix_processor_context(void); · line 46
Quoted 9 lines · On Fri, 24 Aug 2007 12:11:54 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Index:
linux-2.6.23-rc3/include/asm-x86_64/suspend.h
===================================================================include/asm-x86_64/suspend.h +1
-0@@ -43,4 +43,10 @@ extern void fix_processor_context(void);/* routines for saving/restoring kernel state */ extern int acpi_save_state_mem (void ); +#define ARCH_HAS_HIBERNATION_HEADER
The preferred way of doing this is via Kconfig, please. ie: add a CONFIG_HIBERNATION_HEADER to arch/x86_64/Kconfig.
include/asm-x86_64/suspend.h extern void fix_processor_context(void); · line 50
Quoted 4 lines
include/asm-x86_64/suspend.h +4
-0+ +/* arch/x86_64/kernel/suspend.c */ +extern int arch_hibernation_header_save (void *addr,unsigned int max_size); +extern int arch_hibernation_header_restore (void *addr);
Given that these are called from non-arch-specific code, they must have the same signature across all architectures. So there's no point in putting the prototypes into an arch-specific header file.
It would be better to do something like this in (say) suspend.h:
#ifdef CONFIG_HIBERNATION_HEADER
extern int arch_hibernation_header_save(void *addr, unsigned int max_size); extern int arch_hibernation_header_restore(void *addr);
#else
static inline int arch_hibernation_header_save(void *addr,
unsigned int max_size)
{
return 0;
}static inline int arch_hibernation_header_restore(void *addr)
{
return 0;
}#endif
then go nuke some ifdefs from the .c files.
Quoted 10 lines · Andrew Morton
The preferred way of doing this is via
Kconfig, please. ie: add aCONFIG_HIBERNATION_HEADERtoarch/x86_64/Kconfig.It would be better to do something like this in (say)
suspend.h:#ifdef
CONFIG_HIBERNATION_HEADER
extern intarch_hibernation_header_save(void *addr, unsigned int max_size); extern intarch_hibernation_header_restore(void *addr);
#else
static inline int arch_hibernation_header_save(void *addr,
In fact, I guess we don't need to bother with this at all. The generic code for doing this (via the utsname based header) is tiny, so as far as I can tell it could just be made weak symbols (by this I mean init_header_complete() and check_image_kernel()), and then all the #ifdefs can just go.
johannes
Quoted 13 lines · Johannes Berg
Quoted 10 lines · Andrew Morton
The preferred way of doing this is via
Kconfig, please. ie: add aCONFIG_HIBERNATION_HEADERtoarch/x86_64/Kconfig.It would be better to do something like this in (say)
suspend.h:#ifdef
CONFIG_HIBERNATION_HEADER
extern intarch_hibernation_header_save(void *addr, unsigned int max_size); extern intarch_hibernation_header_restore(void *addr);
#else
static inline int arch_hibernation_header_save(void *addr,
Quoted 5 lines
In fact, I guess we don't need to bother with this at all. The generic code for doing this (via the utsname based header) is tiny, so as far as I can tell it could just be made weak symbols (by this I mean
init_header_complete()andcheck_image_kernel()), and then all the #ifdefs can just go.
Well, I don't like the "weak symbols" stuff, but I have managed to limit the number of additional #ifdefs in snapshot.c to just one.
The "generic" patch is now the following:
---
From: Rafael J. Wysocki <[email protected]>
Add the bits needed for supporting arbitrary boot kernels to the common hibernation code.
To support arbitrary boot kernels, make it possible to replace the 'struct new_utsname' and the kernel version in the hibernation image header by some architecture specific data that will be used to verify if the image is valid and to restore the image.
--- kernel/power/power.h | 20 +++++++++++++++++- kernel/power/snapshot.c | 53 +++++++++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 19 deletions(-)
kernel/power/power.h +19 -1
@@ -11,14 +11,32 @@ struct swsusp_info {unsigned long size; } __attribute__((aligned(PAGE_SIZE))); +#ifdef CONFIG_HIBERNATION +#ifdef CONFIG_ARCH_HIBERNATION_HEADER +/* Maximum size of architecture specific data in a hibernation header */ +#define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4) +extern int arch_hibernation_header_save (void *addr,unsigned int max_size); +extern int arch_hibernation_header_restore (void *addr); + +static inline int init_header_complete (struct swsusp_info *info) +{ +return arch_hibernation_header_save (info,MAX_ARCH_HEADER_SIZE ); +} + +static inline char *check_image_kernel (struct swsusp_info *info) +{ +return arch_hibernation_header_restore (info) ? +"architecture specific data" :NULL ; +} +#endif /* CONFIG_ARCH_HIBERNATION_HEADER */ -#ifdef CONFIG_HIBERNATION /* * Keep some memory free so that I/O operations can succeed without paging * [Might this be more than 4 MB?] */ #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) +/* * Keep 1 MB of memory free so that device drivers can allocate some pages in * their .suspend() routines without breaking the suspend to disk.
kernel/power/snapshot.c +35 -18
81 lines not on this page · raw message
Quoted 4 lines · Rafael J. Wysocki
Well, I don't like the "weak symbols" stuff, but I have managed to limit the number of additional #ifdefs in
snapshot.cto just one.The "generic" patch is now the following:
Fine with me, I was just throwing out ideas anyway :)
johannes
[Sorry for the duplicate, but my mailer decided to mess up the CC list for an unknown reason.]
Quoted 13 lines · Johannes Berg
Quoted 10 lines · Andrew Morton
The preferred way of doing this is via
Kconfig, please. ie: add aCONFIG_HIBERNATION_HEADERtoarch/x86_64/Kconfig.It would be better to do something like this in (say)
suspend.h:#ifdef
CONFIG_HIBERNATION_HEADER
extern intarch_hibernation_header_save(void *addr, unsigned int max_size); extern intarch_hibernation_header_restore(void *addr);
#else
static inline int arch_hibernation_header_save(void *addr,
Quoted 5 lines
In fact, I guess we don't need to bother with this at all. The generic code for doing this (via the utsname based header) is tiny, so as far as I can tell it could just be made weak symbols (by this I mean
init_header_complete()andcheck_image_kernel()), and then all the #ifdefs can just go.
Well, I don't like the "weak symbols" stuff, but I have managed to limit the number of additional #ifdefs in snapshot.c to just one.
The "generic" patch is now the following:
---
From: Rafael J. Wysocki <[email protected]>
Add the bits needed for supporting arbitrary boot kernels to the common hibernation code.
To support arbitrary boot kernels, make it possible to replace the 'struct new_utsname' and the kernel version in the hibernation image header by some architecture specific data that will be used to verify if the image is valid and to restore the image.
--- kernel/power/power.h | 20 +++++++++++++++++- kernel/power/snapshot.c | 53 +++++++++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 19 deletions(-)
kernel/power/power.h +19 -1
@@ -11,14 +11,32 @@ struct swsusp_info {unsigned long size; } __attribute__((aligned(PAGE_SIZE))); +#ifdef CONFIG_HIBERNATION +#ifdef CONFIG_ARCH_HIBERNATION_HEADER +/* Maximum size of architecture specific data in a hibernation header */ +#define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4) +extern int arch_hibernation_header_save (void *addr,unsigned int max_size); +extern int arch_hibernation_header_restore (void *addr); + +static inline int init_header_complete (struct swsusp_info *info) +{ +return arch_hibernation_header_save (info,MAX_ARCH_HEADER_SIZE ); +} + +static inline char *check_image_kernel (struct swsusp_info *info) +{ +return arch_hibernation_header_restore (info) ? +"architecture specific data" :NULL ; +} +#endif /* CONFIG_ARCH_HIBERNATION_HEADER */ -#ifdef CONFIG_HIBERNATION /* * Keep some memory free so that I/O operations can succeed without paging * [Might this be more than 4 MB?] */ #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) +/* * Keep 1 MB of memory free so that device drivers can allocate some pages in * their .suspend() routines without breaking the suspend to disk.
kernel/power/snapshot.c +35 -18
81 lines not on this page · raw message
Quoted 13 lines · Johannes Berg
Quoted 10 lines · Andrew Morton
The preferred way of doing this is via
Kconfig, please. ie: add aCONFIG_HIBERNATION_HEADERtoarch/x86_64/Kconfig.It would be better to do something like this in (say)
suspend.h:#ifdef
CONFIG_HIBERNATION_HEADER
extern intarch_hibernation_header_save(void *addr, unsigned int max_size); extern intarch_hibernation_header_restore(void *addr);
#else
static inline int arch_hibernation_header_save(void *addr,
Quoted 5 lines
In fact, I guess we don't need to bother with this at all. The generic code for doing this (via the utsname based header) is tiny, so as far as I can tell it could just be made weak symbols (by this I mean
init_header_complete()andcheck_image_kernel()), and then all the #ifdefs can just go.
Well, I don't like the "weak symbols" stuff, but I have managed to limit the number of additional #ifdefs in snapshot.c to just one.
The "generic" patch is now the following:
---
From: Rafael J. Wysocki <[email protected]>
Add the bits needed for supporting arbitrary boot kernels to the common hibernation code.
To support arbitrary boot kernels, make it possible to replace the 'struct new_utsname' and the kernel version in the hibernation image header by some architecture specific data that will be used to verify if the image is valid and to restore the image.
--- kernel/power/power.h | 20 +++++++++++++++++- kernel/power/snapshot.c | 53 +++++++++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 19 deletions(-)
kernel/power/power.h +19 -1
@@ -11,14 +11,32 @@ struct swsusp_info {unsigned long size; } __attribute__((aligned(PAGE_SIZE))); +#ifdef CONFIG_HIBERNATION +#ifdef CONFIG_ARCH_HIBERNATION_HEADER +/* Maximum size of architecture specific data in a hibernation header */ +#define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4) +extern int arch_hibernation_header_save (void *addr,unsigned int max_size); +extern int arch_hibernation_header_restore (void *addr); + +static inline int init_header_complete (struct swsusp_info *info) +{ +return arch_hibernation_header_save (info,MAX_ARCH_HEADER_SIZE ); +} + +static inline char *check_image_kernel (struct swsusp_info *info) +{ +return arch_hibernation_header_restore (info) ? +"architecture specific data" :NULL ; +} +#endif /* CONFIG_ARCH_HIBERNATION_HEADER */ -#ifdef CONFIG_HIBERNATION /* * Keep some memory free so that I/O operations can succeed without paging * [Might this be more than 4 MB?] */ #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) +/* * Keep 1 MB of memory free so that device drivers can allocate some pages in * their .suspend() routines without breaking the suspend to disk.
kernel/power/snapshot.c +35 -18
81 lines not on this page · raw message
Quoted 11 lines · Andrew Morton
Quoted 8 lines · On Fri, 24 Aug 2007 12:11:54 +0200 "Rafael J. Wysocki" <[email protected]> wrote:
Index:
linux-2.6.23-rc3/include/asm-x86_64/suspend.h
===================================================================include/asm-x86_64/suspend.h +0
-0@@ -43,4 +43,10 @@ extern void fix_processor_context(void);/* routines for saving/restoring kernel state */ extern int acpi_save_state_mem (void );
Quoted 4 lines
include/asm-x86_64/suspend.h extern void fix_processor_context(void); · line 46
Quoted 1 line
include/asm-x86_64/suspend.h +1
-0+#define ARCH_HAS_HIBERNATION_HEADER The preferred way of doing this is via
Kconfig, please. ie: add aCONFIG_HIBERNATION_HEADERtoarch/x86_64/Kconfig.
OK
Quoted 21 lines
include/asm-x86_64/suspend.h extern void fix_processor_context(void); · line 50
Quoted 4 lines
include/asm-x86_64/suspend.h +4
-0+ +/* arch/x86_64/kernel/suspend.c */ +extern int arch_hibernation_header_save (void *addr,unsigned int max_size); +extern int arch_hibernation_header_restore (void *addr);Given that these are called from non-arch-specific code, they must have the same signature across all architectures. So there's no point in putting the prototypes into an arch-specific header file.
It would be better to do something like this in (say)
suspend.h:#ifdef
CONFIG_HIBERNATION_HEADER
extern intarch_hibernation_header_save(void *addr, unsigned int max_size); extern intarch_hibernation_header_restore(void *addr);
#else
static inline int arch_hibernation_header_save(void *addr,
unsigned intmax_size){ return 0; }
Quoted 7 lines
static inline int
arch_hibernation_header_restore(void *addr){ return 0; }#endif
then go nuke some ifdefs from the .c files.
The ifdefs in snapshot.c are necessary anyway, because they are around some code that is only compiled when the CONFIG_HIBERNATION_HEADER is undefined.
I'll post the reworked patches in a new thread once again after the other issue raised by Pavel gets settled.
Hi!
Quoted 7 lines
From: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on
x86_64with 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
What happens in case where both parts want to be
at the same place? (Like kernel being restored is 4KB smaller, so that routines now collide?)
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Quoted 7 lines · Pavel Machek
Hi!
Quoted 5 lines
From: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on
x86_64with the help of a kernel different from the one in the image.
Quoted 6 lines
Quoted 2 lines
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
What happens in case where both parts want to be
at the same place? (Like kernel being restored is 4KB smaller, so that routines now collide?)
Bad things, but I can't see how to avoid that reliably.
Greetings,
Rafael
Quoted 8 lines · Rafael J. Wysocki
Quoted 7 lines · Pavel Machek
Hi!
Quoted 5 lines
From: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on
x86_64with the help of a kernel different from the one in the image.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
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
What happens in case where both parts want to be
at the same place? (Like kernel being restored is 4KB smaller, so that routines now collide?)Bad things, but I can't see how to avoid that reliably.
can you at least detect it reliably? (feed a program both kernel images and have it tell you 'yes/no')
David Lang
Quoted 13 lines · On Saturday, 25 August 2007 20:32, [email protected] wrote:
Quoted 11 lines · Rafael J. Wysocki
Quoted 10 lines · Pavel Machek
Hi!
Quoted 7 lines
From: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on
x86_64with 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
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
What happens in case where both parts want to be
at the same place? (Like kernel being restored is 4KB smaller, so that routines now collide?)Bad things, but I can't see how to avoid that reliably.
can you at least detect it reliably? (feed a program both kernel images and have it tell you 'yes/no')
Well, I have an idea how to handle that, but I need to test it. Stay tuned. :-)
Greetings,
Rafael
Quoted 13 lines · On Saturday, 25 August 2007 20:32, [email protected] wrote:
Quoted 11 lines · Rafael J. Wysocki
Quoted 10 lines · Pavel Machek
Hi!
Quoted 7 lines
From: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on
x86_64with 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
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
What happens in case where both parts want to be
at the same place? (Like kernel being restored is 4KB smaller, so that routines now collide?)Bad things, but I can't see how to avoid that reliably.
can you at least detect it reliably? (feed a program both kernel images and have it tell you 'yes/no')
Well, I have an idea how to handle that, but I need to test it. Stay tuned. :-)
Greetings,
Rafael
PATCH 2/2 Hibernation: Arbitrary boot kernel support on x86_64 (updated)
In reply to Rafael J. WysockiQuoted 8 lines · Rafael J. Wysocki
Quoted 7 lines · Pavel Machek
Hi!
Quoted 5 lines
From: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on
x86_64with the help of a kernel different from the one in the image.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
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
What happens in case where both parts want to be
at the same place? (Like kernel being restored is 4KB smaller, so that routines now collide?)Bad things, but I can't see how to avoid that reliably.
Below is an analogous patch without this problem. The slightly ugly thing about it is that all pages in the temporary mapping have the NX bit cleard now, so that we can run some code out of one of them. Still, IMO, that isn't really important, because the temporary page tables are dropped as soon as we jump to restore_registers.
Greetings,
Rafael
---
From: Rafael J. Wysocki <[email protected]>
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.
--- arch/x86_64/Kconfig | 5 +++ arch/x86_64/kernel/suspend.c | 54 ++++++++++++++++++++++++++++++++++++++- arch/x86_64/kernel/suspend_asm.S | 41 ++++++++++++++++++++++++----- include/asm-x86_64/suspend.h | 3 ++ 4 files changed, 95 insertions(+), 8 deletions(-)
arch/x86_64/kernel/suspend_asm.S +34 -7
@@ -2,8 +2,8 @@ * * Distribute under GPLv2. * - * swsusp_arch_resume may not use any stack, nor any variable that is - * not "NoSave" during copying pages: + * swsusp_arch_resume must not use any stack or any nonlocal variables while + * copying pages: * * Its rewriting one kernel image with another. What is stack in "old" * image could very well be data page in "new" image, and overwriting
@@ -36,6 +36,10 @@ ENTRY(swsusp_arch_suspend) pushfq popq pt_regs_eflags(%rax) + /* save the address of restore_registers */ + movq $restore_registers, %rax + movq %rax, restore_jump_address(%rip) + call swsusp_save ret
@@ -54,7 +58,16 @@ ENTRY(restore_image) movq %rcx, %cr3; movq %rax, %cr4; # turn PGE back on + /* prepare to jump to the image kernel */ + movq restore_jump_address(%rip), %rax + + /* prepare to copy image data to their original locations */ movq restore_pblist(%rip), %rdx + movq relocated_restore_code(%rip), %rcx + jmpq *%rcx + + /* code below has been relocated to a safe page */ +ENTRY(core_restore_code) loop: testq %rdx, %rdx jz done
@@ -62,7 +75,7 @@ loop: /* get addresses from the pbe and copy the page */ movq pbe_address(%rdx), %rsi movq pbe_orig_address(%rdx), %rdi - movq $512, %rcx + movq $(PAGE_SIZE >> 3), %rcx rep movsq
@@ -70,6 +83,20 @@ loop: movq pbe_next(%rdx), %rdx jmp loop done: + /* jump to the restore_registers address from the image header */ + jmpq *%rax + /* + * NOTE: This assumes that the boot kernel's text mapping covers the + * image kernel's page containing restore_registers and the address of + * this page is the same as in the image kernel's text mapping (it + * should always be true, because the text mapping is linear, starting + * from 0, and is supposed to cover the entire kernel text for every + * kernel). + * + * code below belongs to the image kernel + */ + +ENTRY(restore_registers) /* go back to the original page tables */ movq $(init_level4_pgt - __START_KERNEL_map), %rax addq phys_base(%rip), %rax
@@ -84,10 +111,7 @@ done: movq %rcx, %cr3 movq %rax, %cr4; # turn PGE back on - movl $24, %eax - movl %eax, %ds - - /* We don't restore %rax, it must be 0 anyway */ + /* restore GPRs (we don't restore %rax, it must be 0 anyway) */ movq $saved_context, %rax movq pt_regs_rsp(%rax), %rsp movq pt_regs_rbp(%rax), %rbp
@@ -109,4 +133,7 @@ done: xorq %rax, %rax + /* tell the hibernation core that we've just restored the memory */ + movq %rax, in_suspend(%rip) + ret
arch/x86_64/kernel/suspend.c +53 -1
78 lines not on this page · raw message
arch/x86_64/Kconfig +5 -0
@@ -710,6 +710,11 @@ menu "Power management options" source kernel/power/Kconfig +config ARCH_HIBERNATION_HEADER + bool + depends on HIBERNATION + default y + source "drivers/acpi/Kconfig" source "arch/x86_64/kernel/cpufreq/Kconfig"
include/asm-x86_64/suspend.h +3 -0
@@ -43,4 +43,7 @@ extern void fix_processor_context(void);/* routines for saving/restoring kernel state */ extern int acpi_save_state_mem (void ); +extern char core_restore_code; +extern char restore_registers; +#endif /* __ASM_X86_64_SUSPEND_H */
Re: [PATCH -mm 2/2] Hibernation: Arbitrary boot kernel support on x86_64 (updated)
Quoted 56 lines · Rafael J. Wysocki
Quoted 8 lines · Rafael J. Wysocki
Quoted 7 lines · Pavel Machek
Hi!
Quoted 5 lines
From: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on
x86_64with the help of a kernel different from the one in the image.Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
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
What happens in case where both parts want to be
at the same place? (Like kernel being restored is 4KB smaller, so that routines now collide?)Bad things, but I can't see how to avoid that reliably.
Below is an analogous patch without this problem. The slightly ugly thing about it is that all pages in the temporary mapping have the NX bit cleard now, so that we can run some code out of one of them. Still, IMO, that isn't really important, because the temporary page tables are dropped as soon as we jump to
restore_registers.Greetings,
Rafael---
From: Rafael J. Wysocki <[email protected]>Make it possible to restore a hibernation image on
x86_64with 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.
Quoted 1 line
Signed-off-by Rafael J. Wysocki <[email protected]>
ACK.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html