2 * Suspend support specific for i386.
4 * Distribute under GPLv2
6 * Copyright (c) 2002 Pavel Machek <pavel@suse.cz>
7 * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
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>
22 #include <linux/device.h>
23 #include <linux/suspend.h>
24 #include <asm/uaccess.h>
26 #include <asm/tlbflush.h>
28 #include <asm/proto.h>
30 struct saved_context saved_context;
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;
38 void __save_processor_state(struct saved_context *ctxt)
45 asm volatile ("sgdt %0" : "=m" (ctxt->gdt_limit));
46 asm volatile ("sidt %0" : "=m" (ctxt->idt_limit));
47 asm volatile ("str %0" : "=m" (ctxt->tr));
49 /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
50 /* EFER should be constant for kernel version, no need to handle it. */
54 asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
55 asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
56 asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
57 asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
58 asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
60 rdmsrl(MSR_FS_BASE, ctxt->fs_base);
61 rdmsrl(MSR_GS_BASE, ctxt->gs_base);
62 rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
67 asm volatile ("movq %%cr0, %0" : "=r" (ctxt->cr0));
68 asm volatile ("movq %%cr2, %0" : "=r" (ctxt->cr2));
69 asm volatile ("movq %%cr3, %0" : "=r" (ctxt->cr3));
70 asm volatile ("movq %%cr4, %0" : "=r" (ctxt->cr4));
71 asm volatile ("movq %%cr8, %0" : "=r" (ctxt->cr8));
74 void save_processor_state(void)
76 __save_processor_state(&saved_context);
82 /* restore FPU regs if necessary */
83 /* Do it out of line so that gcc does not move cr0 load to some stupid place */
85 mxcsr_feature_mask_init();
88 void __restore_processor_state(struct saved_context *ctxt)
93 asm volatile ("movq %0, %%cr8" :: "r" (ctxt->cr8));
94 asm volatile ("movq %0, %%cr4" :: "r" (ctxt->cr4));
95 asm volatile ("movq %0, %%cr3" :: "r" (ctxt->cr3));
96 asm volatile ("movq %0, %%cr2" :: "r" (ctxt->cr2));
97 asm volatile ("movq %0, %%cr0" :: "r" (ctxt->cr0));
100 * now restore the descriptor tables to their proper values
101 * ltr is done i fix_processor_context().
103 asm volatile ("lgdt %0" :: "m" (ctxt->gdt_limit));
104 asm volatile ("lidt %0" :: "m" (ctxt->idt_limit));
109 asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
110 asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
111 asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
112 load_gs_index(ctxt->gs);
113 asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
115 wrmsrl(MSR_FS_BASE, ctxt->fs_base);
116 wrmsrl(MSR_GS_BASE, ctxt->gs_base);
117 wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
119 fix_processor_context();
125 void restore_processor_state(void)
127 __restore_processor_state(&saved_context);
130 void fix_processor_context(void)
132 int cpu = smp_processor_id();
133 struct tss_struct *t = &per_cpu(init_tss, cpu);
135 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. */
137 cpu_gdt_table[cpu][GDT_ENTRY_TSS].type = 9;
139 syscall_init(); /* This sets MSR_*STAR and related */
140 load_TR_desc(); /* This does ltr */
141 load_LDT(¤t->active_mm->context); /* This does lldt */
144 * Now maybe reload the debug registers
146 if (current->thread.debugreg7){
147 loaddebug(¤t->thread, 0);
148 loaddebug(¤t->thread, 1);
149 loaddebug(¤t->thread, 2);
150 loaddebug(¤t->thread, 3);
152 loaddebug(¤t->thread, 6);
153 loaddebug(¤t->thread, 7);