ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / x86_64 / kernel / suspend.c
1 /*
2  * Suspend support specific for i386.
3  *
4  * Distribute under GPLv2
5  *
6  * Copyright (c) 2002 Pavel Machek <pavel@suse.cz>
7  * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
8  */
9
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/spinlock.h>
16 #include <linux/poll.h>
17 #include <linux/delay.h>
18 #include <linux/sysrq.h>
19 #include <linux/proc_fs.h>
20 #include <linux/irq.h>
21 #include <linux/pm.h>
22 #include <linux/device.h>
23 #include <linux/suspend.h>
24 #include <asm/uaccess.h>
25 #include <asm/acpi.h>
26 #include <asm/tlbflush.h>
27 #include <asm/io.h>
28 #include <asm/proto.h>
29
30 struct saved_context saved_context;
31
32 unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx;
33 unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi;
34 unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11;
35 unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15;
36 unsigned long saved_context_eflags;
37
38 void save_processor_state (void)
39 {
40         kernel_fpu_begin();
41
42         /*
43          * descriptor tables
44          */
45         asm volatile ("sgdt %0" : "=m" (saved_context.gdt_limit));
46         asm volatile ("sidt %0" : "=m" (saved_context.idt_limit));
47         asm volatile ("sldt %0" : "=m" (saved_context.ldt));
48         asm volatile ("str %0"  : "=m" (saved_context.tr));
49
50         /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
51         /* EFER should be constant for kernel version, no need to handle it. */
52         /*
53          * segment registers
54          */
55         asm volatile ("movw %%ds, %0" : "=m" (saved_context.ds));
56         asm volatile ("movw %%es, %0" : "=m" (saved_context.es));
57         asm volatile ("movw %%fs, %0" : "=m" (saved_context.fs));
58         asm volatile ("movw %%gs, %0" : "=m" (saved_context.gs));
59         asm volatile ("movw %%ss, %0" : "=m" (saved_context.ss));
60
61         rdmsrl(MSR_FS_BASE, saved_context.fs_base);
62         rdmsrl(MSR_GS_BASE, saved_context.gs_base);
63         rdmsrl(MSR_KERNEL_GS_BASE, saved_context.gs_kernel_base);
64
65         /*
66          * control registers 
67          */
68         asm volatile ("movq %%cr0, %0" : "=r" (saved_context.cr0));
69         asm volatile ("movq %%cr2, %0" : "=r" (saved_context.cr2));
70         asm volatile ("movq %%cr3, %0" : "=r" (saved_context.cr3));
71         asm volatile ("movq %%cr4, %0" : "=r" (saved_context.cr4));
72 }
73
74 static void
75 do_fpu_end(void)
76 {
77         /* restore FPU regs if necessary */
78         /* Do it out of line so that gcc does not move cr0 load to some stupid place */
79         kernel_fpu_end();
80         mxcsr_feature_mask_init();
81 }
82
83 void restore_processor_state(void)
84 {
85         /*
86          * control registers
87          */
88         asm volatile ("movq %0, %%cr4" :: "r" (saved_context.cr4));
89         asm volatile ("movq %0, %%cr3" :: "r" (saved_context.cr3));
90         asm volatile ("movq %0, %%cr2" :: "r" (saved_context.cr2));
91         asm volatile ("movq %0, %%cr0" :: "r" (saved_context.cr0));
92
93         /*
94          * segment registers
95          */
96         asm volatile ("movw %0, %%ds" :: "r" (saved_context.ds));
97         asm volatile ("movw %0, %%es" :: "r" (saved_context.es));
98         asm volatile ("movw %0, %%fs" :: "r" (saved_context.fs));
99         load_gs_index(saved_context.gs);
100         asm volatile ("movw %0, %%ss" :: "r" (saved_context.ss));
101
102         wrmsrl(MSR_FS_BASE, saved_context.fs_base);
103         wrmsrl(MSR_GS_BASE, saved_context.gs_base);
104         wrmsrl(MSR_KERNEL_GS_BASE, saved_context.gs_kernel_base);
105
106         /*
107          * now restore the descriptor tables to their proper values
108          * ltr is done i fix_processor_context().
109          */
110         asm volatile ("lgdt %0" :: "m" (saved_context.gdt_limit));
111         asm volatile ("lidt %0" :: "m" (saved_context.idt_limit));
112         asm volatile ("lldt %0" :: "m" (saved_context.ldt));
113
114         fix_processor_context();
115
116         do_fpu_end();
117 }
118
119 void fix_processor_context(void)
120 {
121         int cpu = smp_processor_id();
122         struct tss_struct * t = init_tss + cpu;
123
124         set_tss_desc(cpu,t);    /* This just modifies memory; should not be neccessary. But... This is neccessary, because 386 hardware has concept of busy TSS or some similar stupidity. */
125
126         cpu_gdt_table[cpu][GDT_ENTRY_TSS].type = 9;
127
128         syscall_init();                         /* This sets MSR_*STAR and related */
129         load_TR_desc();                         /* This does ltr */
130         load_LDT(&current->active_mm->context); /* This does lldt */
131
132         /*
133          * Now maybe reload the debug registers
134          */
135         if (current->thread.debugreg7){
136                 loaddebug(&current->thread, 0);
137                 loaddebug(&current->thread, 1);
138                 loaddebug(&current->thread, 2);
139                 loaddebug(&current->thread, 3);
140                 /* no 4 and 5 */
141                 loaddebug(&current->thread, 6);
142                 loaddebug(&current->thread, 7);
143         }
144
145 }
146
147