]> err.no Git - linux-2.6/blob - arch/x86/kernel/suspend_asm_64.S
Hibernation: Arbitrary boot kernel support on x86_64
[linux-2.6] / arch / x86 / kernel / suspend_asm_64.S
1 /* Copyright 2004,2005 Pavel Machek <pavel@suse.cz>, Andi Kleen <ak@suse.de>, Rafael J. Wysocki <rjw@sisk.pl>
2  *
3  * Distribute under GPLv2.
4  *
5  * swsusp_arch_resume must not use any stack or any nonlocal variables while
6  * copying pages:
7  *
8  * Its rewriting one kernel image with another. What is stack in "old"
9  * image could very well be data page in "new" image, and overwriting
10  * your own stack under you is bad idea.
11  */
12         
13         .text
14 #include <linux/linkage.h>
15 #include <asm/segment.h>
16 #include <asm/page.h>
17 #include <asm/asm-offsets.h>
18
19 ENTRY(swsusp_arch_suspend)
20
21         movq %rsp, saved_context_esp(%rip)
22         movq %rax, saved_context_eax(%rip)
23         movq %rbx, saved_context_ebx(%rip)
24         movq %rcx, saved_context_ecx(%rip)
25         movq %rdx, saved_context_edx(%rip)
26         movq %rbp, saved_context_ebp(%rip)
27         movq %rsi, saved_context_esi(%rip)
28         movq %rdi, saved_context_edi(%rip)
29         movq %r8,  saved_context_r08(%rip)
30         movq %r9,  saved_context_r09(%rip)
31         movq %r10, saved_context_r10(%rip)
32         movq %r11, saved_context_r11(%rip)
33         movq %r12, saved_context_r12(%rip)
34         movq %r13, saved_context_r13(%rip)
35         movq %r14, saved_context_r14(%rip)
36         movq %r15, saved_context_r15(%rip)
37         pushfq ; popq saved_context_eflags(%rip)
38
39         /* save the address of restore_registers */
40         movq    $restore_registers, %rax
41         movq    %rax, restore_jump_address(%rip)
42
43         call swsusp_save
44         ret
45
46 ENTRY(restore_image)
47         /* switch to temporary page tables */
48         movq    $__PAGE_OFFSET, %rdx
49         movq    temp_level4_pgt(%rip), %rax
50         subq    %rdx, %rax
51         movq    %rax, %cr3
52         /* Flush TLB */
53         movq    mmu_cr4_features(%rip), %rax
54         movq    %rax, %rdx
55         andq    $~(1<<7), %rdx  # PGE
56         movq    %rdx, %cr4;  # turn off PGE
57         movq    %cr3, %rcx;  # flush TLB
58         movq    %rcx, %cr3;
59         movq    %rax, %cr4;  # turn PGE back on
60
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 */
65         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)
71 loop:
72         testq   %rdx, %rdx
73         jz      done
74
75         /* get addresses from the pbe and copy the page */
76         movq    pbe_address(%rdx), %rsi
77         movq    pbe_orig_address(%rdx), %rdi
78         movq    $(PAGE_SIZE >> 3), %rcx
79         rep
80         movsq
81
82         /* progress to the next pbe */
83         movq    pbe_next(%rdx), %rdx
84         jmp     loop
85 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)
100         /* go back to the original page tables */
101         movq    $(init_level4_pgt - __START_KERNEL_map), %rax
102         addq    phys_base(%rip), %rax
103         movq    %rax, %cr3
104
105         /* Flush TLB, including "global" things (vmalloc) */
106         movq    mmu_cr4_features(%rip), %rax
107         movq    %rax, %rdx
108         andq    $~(1<<7), %rdx;  # PGE
109         movq    %rdx, %cr4;  # turn off PGE
110         movq    %cr3, %rcx;  # flush TLB
111         movq    %rcx, %cr3
112         movq    %rax, %cr4;  # turn PGE back on
113
114         movq saved_context_esp(%rip), %rsp
115         movq saved_context_ebp(%rip), %rbp
116         /* restore GPRs (we don't restore %rax, it must be 0 anyway) */
117         movq saved_context_ebx(%rip), %rbx
118         movq saved_context_ecx(%rip), %rcx
119         movq saved_context_edx(%rip), %rdx
120         movq saved_context_esi(%rip), %rsi
121         movq saved_context_edi(%rip), %rdi
122         movq saved_context_r08(%rip), %r8
123         movq saved_context_r09(%rip), %r9
124         movq saved_context_r10(%rip), %r10
125         movq saved_context_r11(%rip), %r11
126         movq saved_context_r12(%rip), %r12
127         movq saved_context_r13(%rip), %r13
128         movq saved_context_r14(%rip), %r14
129         movq saved_context_r15(%rip), %r15
130         pushq saved_context_eflags(%rip) ; popfq
131
132         xorq    %rax, %rax
133
134         /* tell the hibernation core that we've just restored the memory */
135         movq    %rax, in_suspend(%rip)
136
137         ret