2005-04-17 06:20:36 +08:00
|
|
|
/* Copyright 2004,2005 Pavel Machek <pavel@suse.cz>, Andi Kleen <ak@suse.de>, Rafael J. Wysocki <rjw@sisk.pl>
|
|
|
|
*
|
|
|
|
* Distribute under GPLv2.
|
|
|
|
*
|
2007-10-18 18:04:53 +08:00
|
|
|
* swsusp_arch_resume must not use any stack or any nonlocal variables while
|
|
|
|
* copying pages:
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Its rewriting one kernel image with another. What is stack in "old"
|
|
|
|
* image could very well be data page in "new" image, and overwriting
|
|
|
|
* your own stack under you is bad idea.
|
|
|
|
*/
|
|
|
|
|
|
|
|
.text
|
|
|
|
#include <linux/linkage.h>
|
|
|
|
#include <asm/segment.h>
|
|
|
|
#include <asm/page.h>
|
2005-09-10 03:28:48 +08:00
|
|
|
#include <asm/asm-offsets.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
ENTRY(swsusp_arch_suspend)
|
2007-10-24 04:37:24 +08:00
|
|
|
movq $saved_context, %rax
|
|
|
|
movq %rsp, pt_regs_rsp(%rax)
|
|
|
|
movq %rbp, pt_regs_rbp(%rax)
|
|
|
|
movq %rsi, pt_regs_rsi(%rax)
|
|
|
|
movq %rdi, pt_regs_rdi(%rax)
|
|
|
|
movq %rbx, pt_regs_rbx(%rax)
|
|
|
|
movq %rcx, pt_regs_rcx(%rax)
|
|
|
|
movq %rdx, pt_regs_rdx(%rax)
|
|
|
|
movq %r8, pt_regs_r8(%rax)
|
|
|
|
movq %r9, pt_regs_r9(%rax)
|
|
|
|
movq %r10, pt_regs_r10(%rax)
|
|
|
|
movq %r11, pt_regs_r11(%rax)
|
|
|
|
movq %r12, pt_regs_r12(%rax)
|
|
|
|
movq %r13, pt_regs_r13(%rax)
|
|
|
|
movq %r14, pt_regs_r14(%rax)
|
|
|
|
movq %r15, pt_regs_r15(%rax)
|
|
|
|
pushfq
|
|
|
|
popq pt_regs_eflags(%rax)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-18 18:04:53 +08:00
|
|
|
/* save the address of restore_registers */
|
|
|
|
movq $restore_registers, %rax
|
|
|
|
movq %rax, restore_jump_address(%rip)
|
2007-10-18 18:04:54 +08:00
|
|
|
/* save cr3 */
|
|
|
|
movq %cr3, %rax
|
|
|
|
movq %rax, restore_cr3(%rip)
|
2007-10-18 18:04:53 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
call swsusp_save
|
|
|
|
ret
|
|
|
|
|
[PATCH] x86_64: Set up safe page tables during resume
The following patch makes swsusp avoid the possible temporary corruption
of page translation tables during resume on x86-64. This is achieved by
creating a copy of the relevant page tables that will not be modified by
swsusp and can be safely used by it on resume.
The problem is that during resume on x86-64 swsusp may temporarily
corrupt the page tables used for the direct mapping of RAM. If that
happens, a page fault occurs and cannot be handled properly, which leads
to the solid hang of the affected system. This leads to the loss of the
system's state from before suspend and may result in the loss of data or
the corruption of filesystems, so it is a serious issue. Also, it
appears to happen quite often (for me, as often as 50% of the time).
The problem is related to the fact that (at least) one of the PMD
entries used in the direct memory mapping (starting at PAGE_OFFSET)
points to a page table the physical address of which is much greater
than the physical address of the PMD entry itself. Moreover,
unfortunately, the physical address of the page table before suspend
(i.e. the one stored in the suspend image) happens to be different to
the physical address of the corresponding page table used during resume
(i.e. the one that is valid right before swsusp_arch_resume() in
arch/x86_64/kernel/suspend_asm.S is executed). Thus while the image is
restored, the "offending" PMD entry gets overwritten, so it does not
point to the right physical address any more (i.e. there's no page
table at the address pointed to by it, because it points to the address
the page table has been at during suspend). Consequently, if the PMD
entry is used later on, and it _is_ used in the process of copying the
image pages, a page fault occurs, but it cannot be handled in the normal
way and the system hangs.
In principle we can call create_resume_mapping() from
swsusp_arch_resume() (ie. from suspend_asm.S), but then the memory
allocations in create_resume_mapping(), resume_pud_mapping(), and
resume_pmd_mapping() must be made carefully so that we use _only_
NosaveFree pages in them (the other pages are overwritten by the loop in
swsusp_arch_resume()). Additionally, we are in atomic context at that
time, so we cannot use GFP_KERNEL. Moreover, if one of the allocations
fails, we should free all of the allocated pages, so we need to trace
them somehow.
All of this is done in the appended patch, except that the functions
populating the page tables are located in arch/x86_64/kernel/suspend.c
rather than in init.c. It may be done in a more elegan way in the
future, with the help of some swsusp patches that are in the works now.
[AK: move some externs into headers, renamed a function]
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-10-10 03:19:40 +08:00
|
|
|
ENTRY(restore_image)
|
|
|
|
/* switch to temporary page tables */
|
|
|
|
movq $__PAGE_OFFSET, %rdx
|
|
|
|
movq temp_level4_pgt(%rip), %rax
|
|
|
|
subq %rdx, %rax
|
|
|
|
movq %rax, %cr3
|
|
|
|
/* Flush TLB */
|
2005-04-17 06:20:36 +08:00
|
|
|
movq mmu_cr4_features(%rip), %rax
|
|
|
|
movq %rax, %rdx
|
|
|
|
andq $~(1<<7), %rdx # PGE
|
|
|
|
movq %rdx, %cr4; # turn off PGE
|
|
|
|
movq %cr3, %rcx; # flush TLB
|
|
|
|
movq %rcx, %cr3;
|
|
|
|
movq %rax, %cr4; # turn PGE back on
|
|
|
|
|
2007-10-18 18:04:53 +08:00
|
|
|
/* prepare to jump to the image kernel */
|
|
|
|
movq restore_jump_address(%rip), %rax
|
2007-10-18 18:04:54 +08:00
|
|
|
movq restore_cr3(%rip), %rbx
|
2007-10-18 18:04:53 +08:00
|
|
|
|
|
|
|
/* prepare to copy image data to their original locations */
|
2006-09-26 14:32:52 +08:00
|
|
|
movq restore_pblist(%rip), %rdx
|
2007-10-18 18:04:53 +08:00
|
|
|
movq relocated_restore_code(%rip), %rcx
|
|
|
|
jmpq *%rcx
|
|
|
|
|
|
|
|
/* code below has been relocated to a safe page */
|
|
|
|
ENTRY(core_restore_code)
|
2005-04-17 06:20:36 +08:00
|
|
|
loop:
|
|
|
|
testq %rdx, %rdx
|
|
|
|
jz done
|
|
|
|
|
|
|
|
/* get addresses from the pbe and copy the page */
|
|
|
|
movq pbe_address(%rdx), %rsi
|
|
|
|
movq pbe_orig_address(%rdx), %rdi
|
2007-10-18 18:04:53 +08:00
|
|
|
movq $(PAGE_SIZE >> 3), %rcx
|
2005-04-17 06:20:36 +08:00
|
|
|
rep
|
|
|
|
movsq
|
|
|
|
|
|
|
|
/* progress to the next pbe */
|
|
|
|
movq pbe_next(%rdx), %rdx
|
|
|
|
jmp loop
|
|
|
|
done:
|
2007-10-18 18:04:53 +08:00
|
|
|
/* 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)
|
[PATCH] x86_64: Set up safe page tables during resume
The following patch makes swsusp avoid the possible temporary corruption
of page translation tables during resume on x86-64. This is achieved by
creating a copy of the relevant page tables that will not be modified by
swsusp and can be safely used by it on resume.
The problem is that during resume on x86-64 swsusp may temporarily
corrupt the page tables used for the direct mapping of RAM. If that
happens, a page fault occurs and cannot be handled properly, which leads
to the solid hang of the affected system. This leads to the loss of the
system's state from before suspend and may result in the loss of data or
the corruption of filesystems, so it is a serious issue. Also, it
appears to happen quite often (for me, as often as 50% of the time).
The problem is related to the fact that (at least) one of the PMD
entries used in the direct memory mapping (starting at PAGE_OFFSET)
points to a page table the physical address of which is much greater
than the physical address of the PMD entry itself. Moreover,
unfortunately, the physical address of the page table before suspend
(i.e. the one stored in the suspend image) happens to be different to
the physical address of the corresponding page table used during resume
(i.e. the one that is valid right before swsusp_arch_resume() in
arch/x86_64/kernel/suspend_asm.S is executed). Thus while the image is
restored, the "offending" PMD entry gets overwritten, so it does not
point to the right physical address any more (i.e. there's no page
table at the address pointed to by it, because it points to the address
the page table has been at during suspend). Consequently, if the PMD
entry is used later on, and it _is_ used in the process of copying the
image pages, a page fault occurs, but it cannot be handled in the normal
way and the system hangs.
In principle we can call create_resume_mapping() from
swsusp_arch_resume() (ie. from suspend_asm.S), but then the memory
allocations in create_resume_mapping(), resume_pud_mapping(), and
resume_pmd_mapping() must be made carefully so that we use _only_
NosaveFree pages in them (the other pages are overwritten by the loop in
swsusp_arch_resume()). Additionally, we are in atomic context at that
time, so we cannot use GFP_KERNEL. Moreover, if one of the allocations
fails, we should free all of the allocated pages, so we need to trace
them somehow.
All of this is done in the appended patch, except that the functions
populating the page tables are located in arch/x86_64/kernel/suspend.c
rather than in init.c. It may be done in a more elegan way in the
future, with the help of some swsusp patches that are in the works now.
[AK: move some externs into headers, renamed a function]
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-10-10 03:19:40 +08:00
|
|
|
/* go back to the original page tables */
|
2007-10-18 18:04:54 +08:00
|
|
|
movq %rbx, %cr3
|
2007-05-03 01:27:07 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Flush TLB, including "global" things (vmalloc) */
|
|
|
|
movq mmu_cr4_features(%rip), %rax
|
|
|
|
movq %rax, %rdx
|
|
|
|
andq $~(1<<7), %rdx; # PGE
|
|
|
|
movq %rdx, %cr4; # turn off PGE
|
|
|
|
movq %cr3, %rcx; # flush TLB
|
|
|
|
movq %rcx, %cr3
|
|
|
|
movq %rax, %cr4; # turn PGE back on
|
|
|
|
|
2007-10-24 04:37:24 +08:00
|
|
|
/* 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
|
|
|
|
movq pt_regs_rsi(%rax), %rsi
|
|
|
|
movq pt_regs_rdi(%rax), %rdi
|
|
|
|
movq pt_regs_rbx(%rax), %rbx
|
|
|
|
movq pt_regs_rcx(%rax), %rcx
|
|
|
|
movq pt_regs_rdx(%rax), %rdx
|
|
|
|
movq pt_regs_r8(%rax), %r8
|
|
|
|
movq pt_regs_r9(%rax), %r9
|
|
|
|
movq pt_regs_r10(%rax), %r10
|
|
|
|
movq pt_regs_r11(%rax), %r11
|
|
|
|
movq pt_regs_r12(%rax), %r12
|
|
|
|
movq pt_regs_r13(%rax), %r13
|
|
|
|
movq pt_regs_r14(%rax), %r14
|
|
|
|
movq pt_regs_r15(%rax), %r15
|
|
|
|
pushq pt_regs_eflags(%rax)
|
|
|
|
popfq
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
xorq %rax, %rax
|
|
|
|
|
2007-10-18 18:04:53 +08:00
|
|
|
/* tell the hibernation core that we've just restored the memory */
|
|
|
|
movq %rax, in_suspend(%rip)
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
ret
|