blob: 79b2370c7facda90db18255983dc85b58523dab0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14static struct saved_context saved_context;
15
16unsigned long saved_context_ebx;
17unsigned long saved_context_esp, saved_context_ebp;
18unsigned long saved_context_esi, saved_context_edi;
19unsigned long saved_context_eflags;
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021void __save_processor_state(struct saved_context *ctxt)
22{
23 kernel_fpu_begin();
24
25 /*
26 * descriptor tables
27 */
Zachary Amsden4d37e7e2005-09-03 15:56:38 -070028 store_gdt(&ctxt->gdt_limit);
29 store_idt(&ctxt->idt_limit);
30 store_tr(ctxt->tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32 /*
33 * segment registers
34 */
Zachary Amsden4d37e7e2005-09-03 15:56:38 -070035 savesegment(es, ctxt->es);
36 savesegment(fs, ctxt->fs);
37 savesegment(gs, ctxt->gs);
38 savesegment(ss, ctxt->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 /*
41 * control registers
42 */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070043 ctxt->cr0 = read_cr0();
44 ctxt->cr2 = read_cr2();
45 ctxt->cr3 = read_cr3();
46 ctxt->cr4 = read_cr4();
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49void save_processor_state(void)
50{
51 __save_processor_state(&saved_context);
52}
53
Shaohua Li08967f92005-10-30 14:59:28 -080054static void do_fpu_end(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Shaohua Li08967f92005-10-30 14:59:28 -080056 /*
57 * Restore FPU regs if necessary.
58 */
59 kernel_fpu_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void fix_processor_context(void)
63{
64 int cpu = smp_processor_id();
65 struct tss_struct * t = &per_cpu(init_tss, cpu);
66
67 set_tss_desc(cpu,t); /* This just modifies memory; should not be necessary. But... This is necessary, because 386 hardware has concept of busy TSS or some similar stupidity. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 load_TR_desc(); /* This does ltr */
70 load_LDT(&current->active_mm->context); /* This does lldt */
71
72 /*
73 * Now maybe reload the debug registers
74 */
75 if (current->thread.debugreg[7]){
Vincent Hanquez1cc6f122005-06-23 00:08:43 -070076 set_debugreg(current->thread.debugreg[0], 0);
77 set_debugreg(current->thread.debugreg[1], 1);
78 set_debugreg(current->thread.debugreg[2], 2);
79 set_debugreg(current->thread.debugreg[3], 3);
80 /* no 4 and 5 */
81 set_debugreg(current->thread.debugreg[6], 6);
82 set_debugreg(current->thread.debugreg[7], 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
85}
86
87void __restore_processor_state(struct saved_context *ctxt)
88{
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /*
90 * control registers
91 */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070092 write_cr4(ctxt->cr4);
93 write_cr3(ctxt->cr3);
94 write_cr2(ctxt->cr2);
Pavel Machek30d6b2f2006-05-22 22:35:29 -070095 write_cr0(ctxt->cr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 /*
Pavel Machek8d783b32005-06-25 14:55:14 -070098 * now restore the descriptor tables to their proper values
99 * ltr is done i fix_processor_context().
100 */
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700101 load_gdt(&ctxt->gdt_limit);
102 load_idt(&ctxt->idt_limit);
Pavel Machek8d783b32005-06-25 14:55:14 -0700103
104 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * segment registers
106 */
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700107 loadsegment(es, ctxt->es);
108 loadsegment(fs, ctxt->fs);
109 loadsegment(gs, ctxt->gs);
110 loadsegment(ss, ctxt->ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * sysenter MSRs
114 */
115 if (boot_cpu_has(X86_FEATURE_SEP))
Li Shaohua6fe940d2005-06-25 14:54:53 -0700116 enable_sep_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 fix_processor_context();
119 do_fpu_end();
Shaohua Li3b520b22005-07-07 17:56:38 -0700120 mtrr_ap_init();
Shaohua Li31ab2692005-11-07 00:58:42 -0800121 mcheck_init(&boot_cpu_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124void restore_processor_state(void)
125{
126 __restore_processor_state(&saved_context);
127}
128
129/* Needed by apm.c */
130EXPORT_SYMBOL(save_processor_state);
131EXPORT_SYMBOL(restore_processor_state);