Merge ../torvalds-2.6/
[sfrench/cifs-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/smp.h>
12 #include <linux/suspend.h>
13 #include <asm/proto.h>
14
15 struct saved_context saved_context;
16
17 unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx;
18 unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi;
19 unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11;
20 unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15;
21 unsigned long saved_context_eflags;
22
23 void __save_processor_state(struct saved_context *ctxt)
24 {
25         kernel_fpu_begin();
26
27         /*
28          * descriptor tables
29          */
30         asm volatile ("sgdt %0" : "=m" (ctxt->gdt_limit));
31         asm volatile ("sidt %0" : "=m" (ctxt->idt_limit));
32         asm volatile ("str %0"  : "=m" (ctxt->tr));
33
34         /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
35         /* EFER should be constant for kernel version, no need to handle it. */
36         /*
37          * segment registers
38          */
39         asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
40         asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
41         asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
42         asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
43         asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
44
45         rdmsrl(MSR_FS_BASE, ctxt->fs_base);
46         rdmsrl(MSR_GS_BASE, ctxt->gs_base);
47         rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
48
49         /*
50          * control registers 
51          */
52         asm volatile ("movq %%cr0, %0" : "=r" (ctxt->cr0));
53         asm volatile ("movq %%cr2, %0" : "=r" (ctxt->cr2));
54         asm volatile ("movq %%cr3, %0" : "=r" (ctxt->cr3));
55         asm volatile ("movq %%cr4, %0" : "=r" (ctxt->cr4));
56         asm volatile ("movq %%cr8, %0" : "=r" (ctxt->cr8));
57 }
58
59 void save_processor_state(void)
60 {
61         __save_processor_state(&saved_context);
62 }
63
64 static void
65 do_fpu_end(void)
66 {
67         /* restore FPU regs if necessary */
68         /* Do it out of line so that gcc does not move cr0 load to some stupid place */
69         kernel_fpu_end();
70         mxcsr_feature_mask_init();
71 }
72
73 void __restore_processor_state(struct saved_context *ctxt)
74 {
75         /*
76          * control registers
77          */
78         asm volatile ("movq %0, %%cr8" :: "r" (ctxt->cr8));
79         asm volatile ("movq %0, %%cr4" :: "r" (ctxt->cr4));
80         asm volatile ("movq %0, %%cr3" :: "r" (ctxt->cr3));
81         asm volatile ("movq %0, %%cr2" :: "r" (ctxt->cr2));
82         asm volatile ("movq %0, %%cr0" :: "r" (ctxt->cr0));
83
84         /*
85          * now restore the descriptor tables to their proper values
86          * ltr is done i fix_processor_context().
87          */
88         asm volatile ("lgdt %0" :: "m" (ctxt->gdt_limit));
89         asm volatile ("lidt %0" :: "m" (ctxt->idt_limit));
90
91         /*
92          * segment registers
93          */
94         asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
95         asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
96         asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
97         load_gs_index(ctxt->gs);
98         asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
99
100         wrmsrl(MSR_FS_BASE, ctxt->fs_base);
101         wrmsrl(MSR_GS_BASE, ctxt->gs_base);
102         wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
103
104         fix_processor_context();
105
106         do_fpu_end();
107         mtrr_ap_init();
108 }
109
110 void restore_processor_state(void)
111 {
112         __restore_processor_state(&saved_context);
113 }
114
115 void fix_processor_context(void)
116 {
117         int cpu = smp_processor_id();
118         struct tss_struct *t = &per_cpu(init_tss, cpu);
119
120         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. */
121
122         cpu_gdt_table[cpu][GDT_ENTRY_TSS].type = 9;
123
124         syscall_init();                         /* This sets MSR_*STAR and related */
125         load_TR_desc();                         /* This does ltr */
126         load_LDT(&current->active_mm->context); /* This does lldt */
127
128         /*
129          * Now maybe reload the debug registers
130          */
131         if (current->thread.debugreg7){
132                 loaddebug(&current->thread, 0);
133                 loaddebug(&current->thread, 1);
134                 loaddebug(&current->thread, 2);
135                 loaddebug(&current->thread, 3);
136                 /* no 4 and 5 */
137                 loaddebug(&current->thread, 6);
138                 loaddebug(&current->thread, 7);
139         }
140
141 }
142
143