x86_64: Save registers in saved_context during suspend and hibernation
PATCH 1/1 x86_64: Save registers in saved_context during suspend and hibernation
From: Rafael J. Wysocki <[email protected]>
During hibernation and suspend on x86_64 save CPU registers in the saved_context structure rather than in a handful of separate variables.
--- arch/x86_64/kernel/acpi/wakeup.S | 101 ++++++++++++++++++++------------------- arch/x86_64/kernel/asm-offsets.c | 22 ++++++++ arch/x86_64/kernel/suspend.c | 6 -- arch/x86_64/kernel/suspend_asm.S | 72 ++++++++++++++------------- include/asm-x86_64/suspend.h | 39 ++++++++------- 5 files changed, 135 insertions(+), 105 deletions(-)
arch/x86_64/kernel/asm-offsets.c +22 -0
@@ -76,6 +76,28 @@ int main(void)DEFINE (pbe_orig_address, offsetof(struct pbe , orig_address));DEFINE (pbe_next, offsetof(struct pbe , next));BLANK (); +DEFINE (saved_context_rbx, offsetof(struct saved_context , rbx)); +DEFINE (saved_context_rcx, offsetof(struct saved_context , rcx)); +DEFINE (saved_context_rdx, offsetof(struct saved_context , rdx)); +DEFINE (saved_context_rsp, offsetof(struct saved_context , rsp)); +DEFINE (saved_context_rbp, offsetof(struct saved_context , rbp)); +DEFINE (saved_context_rsi, offsetof(struct saved_context , rsi)); +DEFINE (saved_context_rdi, offsetof(struct saved_context , rdi)); +DEFINE (saved_context_r8, offsetof(struct saved_context , r8)); +DEFINE (saved_context_r9, offsetof(struct saved_context , r9)); +DEFINE (saved_context_r10, offsetof(struct saved_context , r10)); +DEFINE (saved_context_r11, offsetof(struct saved_context , r11)); +DEFINE (saved_context_r12, offsetof(struct saved_context , r12)); +DEFINE (saved_context_r13, offsetof(struct saved_context , r13)); +DEFINE (saved_context_r14, offsetof(struct saved_context , r14)); +DEFINE (saved_context_r15, offsetof(struct saved_context , r15)); +DEFINE (saved_context_rflags, offsetof(struct saved_context , rflags)); +DEFINE (saved_context_cr0, offsetof(struct saved_context , cr0)); +DEFINE (saved_context_cr2, offsetof(struct saved_context , cr2)); +DEFINE (saved_context_cr3, offsetof(struct saved_context , cr3)); +DEFINE (saved_context_cr4, offsetof(struct saved_context , cr4)); +DEFINE (saved_context_cr8, offsetof(struct saved_context , cr8)); +BLANK ();DEFINE (TSS_ist, offsetof(struct tss_struct , ist));BLANK ();DEFINE (crypto_tfm_ctx_offset, offsetof(struct crypto_tfm , __crt_ctx));
include/asm-x86_64/suspend.h +23 -16
@@ -3,6 +3,9 @@* Based on code * Copyright 2001 Patrick Mochel <[email protected]> */ +#ifndef __ASM_X86_64_SUSPEND_H +#define __ASM_X86_64_SUSPEND_H +#include <asm/desc.h> #include <asm/i387.h>
@@ -12,8 +15,24 @@ arch_prepare_suspend(void)return 0 ; } -/* Image of the saved processor state. If you touch this, fix acpi_wakeup.S. */ +/* Image of the saved processor state. If you touch this, fix acpi/wakeup.S. */ struct saved_context { +unsigned long rax; +unsigned long rbx; +unsigned long rcx; +unsigned long rdx; +unsigned long rsp; +unsigned long rbp; +unsigned long rsi; +unsigned long rdi; +unsigned long r8; +unsigned long r9; +unsigned long r10; +unsigned long r11; +unsigned long r12; +unsigned long r13; +unsigned long r14; +unsigned long r15;u16 ds, es, fs, gs, ss;unsigned long gs_base, gs_kernel_base, fs_base;unsigned long cr0, cr2, cr3, cr4, cr8;
@@ -29,27 +48,15 @@ struct saved_context {unsigned long tr;unsigned long safety;unsigned long return_address; -unsigned long eflags; +unsigned long rflags; } __attribute__((packed)); -/* We'll access these from assembly, so we'd better have them outside struct */ -extern unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx; -extern unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi; -extern unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11; -extern unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15; -extern unsigned long saved_context_eflags; -#define loaddebug (thread,register) \ set_debugreg((thread)->debugreg##register, register)extern void fix_processor_context (void ); -extern unsigned long saved_rip; -extern unsigned long saved_rsp; -extern unsigned long saved_rbp; -extern unsigned long saved_rbx; -extern unsigned long saved_rsi; -extern unsigned long saved_rdi; -/* routines for saving/restoring kernel state */ extern int acpi_save_state_mem (void ); + +#endif /* __ASM_X86_64_SUSPEND_H */
arch/x86_64/kernel/suspend.c +0 -6
@@ -19,12 +19,6 @@ extern const void __nosave_begin, __nosastruct saved_context saved_context; -unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx; -unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi; -unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11; -unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15; -unsigned long saved_context_eflags; -void __save_processor_state (struct saved_context *ctxt) {kernel_fpu_begin ();
arch/x86_64/kernel/suspend_asm.S +37 -35
84 lines not on this page · raw message
arch/x86_64/kernel/acpi/wakeup.S +53 -48
122 lines not on this page · raw message
Quoted 4 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
During hibernation and suspend on
x86_64save CPU registers in thesaved_contextstructure rather than in a handful of separate variables.
Nice. These variables always annoyed me too when looking at that code.
arch/x86_64/kernel/asm-offsets.c int main(void) · line 79
Quoted 3 lines
arch/x86_64/kernel/asm-offsets.c +1
-0DEFINE (pbe_next, offsetof(struct pbe , next));BLANK (); +DEFINE (saved_context_rbx, offsetof(struct saved_context , rbx));
But is there a reason you can't just use a pt_regs and then an array for the crNs ?
-Andi
Quoted 9 lines · Andi Kleen
Quoted 4 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
During hibernation and suspend on
x86_64save CPU registers in thesaved_contextstructure rather than in a handful of separate variables.Nice. These variables always annoyed me too when looking at that code.
Quoted 6 lines
arch/x86_64/kernel/asm-offsets.c int main(void) · line 79
Quoted 3 lines
arch/x86_64/kernel/asm-offsets.c +1
-0DEFINE (pbe_next, offsetof(struct pbe , next));BLANK (); +DEFINE (saved_context_rbx, offsetof(struct saved_context , rbx));But is there a reason you can't just use a
pt_regsand then an array for the crNs ?
Hm, I think I can use pt_regs. I'll try to redo the patch to use it.
OTOH, crNs already are members of saved_context and are accessed from C. I wouldn't like to change that code.
Greetings,
Rafael
Signature · 1 line
"Premature optimization is the root of all evil." - Donald Knuth
Quoted 10 lines · Rafael J. Wysocki
Quoted 9 lines · Andi Kleen
Quoted 4 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
During hibernation and suspend on
x86_64save CPU registers in thesaved_contextstructure rather than in a handful of separate variables.Nice. These variables always annoyed me too when looking at that code.
Quoted 8 lines
Quoted 6 lines
Review
Quoted 3 lines
(quoted diff) +1
-0DEFINE(pbe_next, offsetof(struct pbe, next)); BLANK(); + DEFINE(saved_context_rbx, offsetof(struct saved_context, rbx));But is there a reason you can't just use a
pt_regsand then an array for the crNs ?Hm, I think I can use
pt_regs. I'll try to redo the patch to use it.
The updated patch (using pt_regs) is appended.
Greetings,
Rafael
---
From: Rafael J. Wysocki <[email protected]>
During hibernation and suspend on x86_64 save CPU registers in the saved_context structure rather than in a handful of separate variables.
--- arch/x86_64/kernel/acpi/wakeup.S | 101 ++++++++++++++++++++------------------- arch/x86_64/kernel/asm-offsets.c | 28 ++++++++++ arch/x86_64/kernel/suspend.c | 6 -- arch/x86_64/kernel/suspend_asm.S | 72 ++++++++++++++------------- include/asm-x86_64/suspend.h | 23 ++------ 5 files changed, 125 insertions(+), 105 deletions(-)
arch/x86_64/kernel/asm-offsets.c +28 -0
@@ -76,6 +76,34 @@ int main(void)DEFINE (pbe_orig_address, offsetof(struct pbe , orig_address));DEFINE (pbe_next, offsetof(struct pbe , next));BLANK (); +#define ENTRY (entry) DEFINE(pt_regs_ ## entry, offsetof(struct pt_regs, entry)) +ENTRY (rbx); +ENTRY (rbx); +ENTRY (rcx); +ENTRY (rdx); +ENTRY (rsp); +ENTRY (rbp); +ENTRY (rsi); +ENTRY (rdi); +ENTRY (r8); +ENTRY (r9); +ENTRY (r10); +ENTRY (r11); +ENTRY (r12); +ENTRY (r13); +ENTRY (r14); +ENTRY (r15); +ENTRY (eflags); +BLANK (); +#undef ENTRY +#define ENTRY (entry) DEFINE(saved_context_ ## entry, offsetof(struct saved_context, entry)) +ENTRY (cr0); +ENTRY (cr2); +ENTRY (cr3); +ENTRY (cr4); +ENTRY (cr8); +BLANK (); +#undef ENTRYDEFINE (TSS_ist, offsetof(struct tss_struct , ist));BLANK ();DEFINE (crypto_tfm_ctx_offset, offsetof(struct crypto_tfm , __crt_ctx));
include/asm-x86_64/suspend.h +7 -16
@@ -3,6 +3,9 @@* Based on code * Copyright 2001 Patrick Mochel <[email protected]> */ +#ifndef __ASM_X86_64_SUSPEND_H +#define __ASM_X86_64_SUSPEND_H +#include <asm/desc.h> #include <asm/i387.h>
@@ -12,8 +15,9 @@ arch_prepare_suspend(void)return 0 ; } -/* Image of the saved processor state. If you touch this, fix acpi_wakeup.S. */ +/* Image of the saved processor state. If you touch this, fix acpi/wakeup.S. */ struct saved_context { +struct pt_regs regs;u16 ds, es, fs, gs, ss;unsigned long gs_base, gs_kernel_base, fs_base;unsigned long cr0, cr2, cr3, cr4, cr8;
@@ -29,27 +33,14 @@ struct saved_context {unsigned long tr;unsigned long safety;unsigned long return_address; -unsigned long eflags; } __attribute__((packed)); -/* We'll access these from assembly, so we'd better have them outside struct */ -extern unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx; -extern unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi; -extern unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11; -extern unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15; -extern unsigned long saved_context_eflags; -#define loaddebug (thread,register) \ set_debugreg((thread)->debugreg##register, register)extern void fix_processor_context (void ); -extern unsigned long saved_rip; -extern unsigned long saved_rsp; -extern unsigned long saved_rbp; -extern unsigned long saved_rbx; -extern unsigned long saved_rsi; -extern unsigned long saved_rdi; -/* routines for saving/restoring kernel state */ extern int acpi_save_state_mem (void ); + +#endif /* __ASM_X86_64_SUSPEND_H */
arch/x86_64/kernel/suspend.c +0 -6
@@ -19,12 +19,6 @@ extern const void __nosave_begin, __nosastruct saved_context saved_context; -unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx; -unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi; -unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11; -unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15; -unsigned long saved_context_eflags; -void __save_processor_state (struct saved_context *ctxt) {kernel_fpu_begin ();
arch/x86_64/kernel/suspend_asm.S +37 -35
84 lines not on this page · raw message
arch/x86_64/kernel/acpi/wakeup.S +53 -48
122 lines not on this page · raw message
Hi!
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Nice. These variables always annoyed me too when looking at that code.
arch/x86_64/kernel/asm-offsets.c int main(void)
Quoted 3 lines
arch/x86_64/kernel/asm-offsets.c +1
-0DEFINE (pbe_next, offsetof(struct pbe , next));BLANK (); +DEFINE (saved_context_rbx, offsetof(struct saved_context , rbx));But is there a reason you can't just use a
pt_regsand then an array for the crNs ?
Quoted 3 lines
Quoted 1 line
Hm, I think I can use
pt_regs. I'll try to redo the patch to use it.The updated patch (using
pt_regs) is appended.
Looks ok to me.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html