108687aecSSergio Luis /* 208687aecSSergio Luis * Suspend support specific for i386/x86-64. 308687aecSSergio Luis * 408687aecSSergio Luis * Distribute under GPLv2 508687aecSSergio Luis * 608687aecSSergio Luis * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl> 708687aecSSergio Luis * Copyright (c) 2002 Pavel Machek <pavel@suse.cz> 808687aecSSergio Luis * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org> 908687aecSSergio Luis */ 1008687aecSSergio Luis 1108687aecSSergio Luis #include <linux/suspend.h> 1208687aecSSergio Luis #include <linux/smp.h> 1308687aecSSergio Luis 1408687aecSSergio Luis #include <asm/pgtable.h> 1508687aecSSergio Luis #include <asm/proto.h> 1608687aecSSergio Luis #include <asm/mtrr.h> 1708687aecSSergio Luis #include <asm/page.h> 1808687aecSSergio Luis #include <asm/mce.h> 1908687aecSSergio Luis #include <asm/xcr.h> 2008687aecSSergio Luis #include <asm/suspend.h> 2108687aecSSergio Luis 2208687aecSSergio Luis #ifdef CONFIG_X86_32 2308687aecSSergio Luis static struct saved_context saved_context; 2408687aecSSergio Luis 2508687aecSSergio Luis unsigned long saved_context_ebx; 2608687aecSSergio Luis unsigned long saved_context_esp, saved_context_ebp; 2708687aecSSergio Luis unsigned long saved_context_esi, saved_context_edi; 2808687aecSSergio Luis unsigned long saved_context_eflags; 2908687aecSSergio Luis #else 3008687aecSSergio Luis /* CONFIG_X86_64 */ 3108687aecSSergio Luis struct saved_context saved_context; 3208687aecSSergio Luis #endif 3308687aecSSergio Luis 3408687aecSSergio Luis /** 3508687aecSSergio Luis * __save_processor_state - save CPU registers before creating a 3608687aecSSergio Luis * hibernation image and before restoring the memory state from it 3708687aecSSergio Luis * @ctxt - structure to store the registers contents in 3808687aecSSergio Luis * 3908687aecSSergio Luis * NOTE: If there is a CPU register the modification of which by the 4008687aecSSergio Luis * boot kernel (ie. the kernel used for loading the hibernation image) 4108687aecSSergio Luis * might affect the operations of the restored target kernel (ie. the one 4208687aecSSergio Luis * saved in the hibernation image), then its contents must be saved by this 4308687aecSSergio Luis * function. In other words, if kernel A is hibernated and different 4408687aecSSergio Luis * kernel B is used for loading the hibernation image into memory, the 4508687aecSSergio Luis * kernel A's __save_processor_state() function must save all registers 4608687aecSSergio Luis * needed by kernel A, so that it can operate correctly after the resume 4708687aecSSergio Luis * regardless of what kernel B does in the meantime. 4808687aecSSergio Luis */ 4908687aecSSergio Luis static void __save_processor_state(struct saved_context *ctxt) 5008687aecSSergio Luis { 5108687aecSSergio Luis #ifdef CONFIG_X86_32 5208687aecSSergio Luis mtrr_save_fixed_ranges(NULL); 5308687aecSSergio Luis #endif 5408687aecSSergio Luis kernel_fpu_begin(); 5508687aecSSergio Luis 5608687aecSSergio Luis /* 5708687aecSSergio Luis * descriptor tables 5808687aecSSergio Luis */ 5908687aecSSergio Luis #ifdef CONFIG_X86_32 6008687aecSSergio Luis store_gdt(&ctxt->gdt); 6108687aecSSergio Luis store_idt(&ctxt->idt); 6208687aecSSergio Luis #else 6308687aecSSergio Luis /* CONFIG_X86_64 */ 6408687aecSSergio Luis store_gdt((struct desc_ptr *)&ctxt->gdt_limit); 6508687aecSSergio Luis store_idt((struct desc_ptr *)&ctxt->idt_limit); 6608687aecSSergio Luis #endif 6708687aecSSergio Luis store_tr(ctxt->tr); 6808687aecSSergio Luis 6908687aecSSergio Luis /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */ 7008687aecSSergio Luis /* 7108687aecSSergio Luis * segment registers 7208687aecSSergio Luis */ 7308687aecSSergio Luis #ifdef CONFIG_X86_32 7408687aecSSergio Luis savesegment(es, ctxt->es); 7508687aecSSergio Luis savesegment(fs, ctxt->fs); 7608687aecSSergio Luis savesegment(gs, ctxt->gs); 7708687aecSSergio Luis savesegment(ss, ctxt->ss); 7808687aecSSergio Luis #else 7908687aecSSergio Luis /* CONFIG_X86_64 */ 8008687aecSSergio Luis asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds)); 8108687aecSSergio Luis asm volatile ("movw %%es, %0" : "=m" (ctxt->es)); 8208687aecSSergio Luis asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs)); 8308687aecSSergio Luis asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs)); 8408687aecSSergio Luis asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss)); 8508687aecSSergio Luis 8608687aecSSergio Luis rdmsrl(MSR_FS_BASE, ctxt->fs_base); 8708687aecSSergio Luis rdmsrl(MSR_GS_BASE, ctxt->gs_base); 8808687aecSSergio Luis rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base); 8908687aecSSergio Luis mtrr_save_fixed_ranges(NULL); 9008687aecSSergio Luis 9108687aecSSergio Luis rdmsrl(MSR_EFER, ctxt->efer); 9208687aecSSergio Luis #endif 9308687aecSSergio Luis 9408687aecSSergio Luis /* 9508687aecSSergio Luis * control registers 9608687aecSSergio Luis */ 9708687aecSSergio Luis ctxt->cr0 = read_cr0(); 9808687aecSSergio Luis ctxt->cr2 = read_cr2(); 9908687aecSSergio Luis ctxt->cr3 = read_cr3(); 10008687aecSSergio Luis #ifdef CONFIG_X86_32 10108687aecSSergio Luis ctxt->cr4 = read_cr4_safe(); 10208687aecSSergio Luis #else 10308687aecSSergio Luis /* CONFIG_X86_64 */ 10408687aecSSergio Luis ctxt->cr4 = read_cr4(); 10508687aecSSergio Luis ctxt->cr8 = read_cr8(); 10608687aecSSergio Luis #endif 10708687aecSSergio Luis } 10808687aecSSergio Luis 10908687aecSSergio Luis /* Needed by apm.c */ 11008687aecSSergio Luis void save_processor_state(void) 11108687aecSSergio Luis { 11208687aecSSergio Luis __save_processor_state(&saved_context); 11308687aecSSergio Luis } 11408687aecSSergio Luis #ifdef CONFIG_X86_32 11508687aecSSergio Luis EXPORT_SYMBOL(save_processor_state); 11608687aecSSergio Luis #endif 11708687aecSSergio Luis 11808687aecSSergio Luis static void do_fpu_end(void) 11908687aecSSergio Luis { 12008687aecSSergio Luis /* 12108687aecSSergio Luis * Restore FPU regs if necessary. 12208687aecSSergio Luis */ 12308687aecSSergio Luis kernel_fpu_end(); 12408687aecSSergio Luis } 12508687aecSSergio Luis 12608687aecSSergio Luis static void fix_processor_context(void) 12708687aecSSergio Luis { 12808687aecSSergio Luis int cpu = smp_processor_id(); 12908687aecSSergio Luis struct tss_struct *t = &per_cpu(init_tss, cpu); 13008687aecSSergio Luis 13108687aecSSergio Luis set_tss_desc(cpu, t); /* 13208687aecSSergio Luis * This just modifies memory; should not be 13308687aecSSergio Luis * necessary. But... This is necessary, because 13408687aecSSergio Luis * 386 hardware has concept of busy TSS or some 13508687aecSSergio Luis * similar stupidity. 13608687aecSSergio Luis */ 13708687aecSSergio Luis 13808687aecSSergio Luis #ifdef CONFIG_X86_64 13908687aecSSergio Luis get_cpu_gdt_table(cpu)[GDT_ENTRY_TSS].type = 9; 14008687aecSSergio Luis 14108687aecSSergio Luis syscall_init(); /* This sets MSR_*STAR and related */ 14208687aecSSergio Luis #endif 14308687aecSSergio Luis load_TR_desc(); /* This does ltr */ 14408687aecSSergio Luis load_LDT(¤t->active_mm->context); /* This does lldt */ 14508687aecSSergio Luis 14608687aecSSergio Luis /* 14708687aecSSergio Luis * Now maybe reload the debug registers 14808687aecSSergio Luis */ 14908687aecSSergio Luis if (current->thread.debugreg7) { 15008687aecSSergio Luis #ifdef CONFIG_X86_32 15108687aecSSergio Luis set_debugreg(current->thread.debugreg0, 0); 15208687aecSSergio Luis set_debugreg(current->thread.debugreg1, 1); 15308687aecSSergio Luis set_debugreg(current->thread.debugreg2, 2); 15408687aecSSergio Luis set_debugreg(current->thread.debugreg3, 3); 15508687aecSSergio Luis /* no 4 and 5 */ 15608687aecSSergio Luis set_debugreg(current->thread.debugreg6, 6); 15708687aecSSergio Luis set_debugreg(current->thread.debugreg7, 7); 15808687aecSSergio Luis #else 15908687aecSSergio Luis /* CONFIG_X86_64 */ 16008687aecSSergio Luis loaddebug(¤t->thread, 0); 16108687aecSSergio Luis loaddebug(¤t->thread, 1); 16208687aecSSergio Luis loaddebug(¤t->thread, 2); 16308687aecSSergio Luis loaddebug(¤t->thread, 3); 16408687aecSSergio Luis /* no 4 and 5 */ 16508687aecSSergio Luis loaddebug(¤t->thread, 6); 16608687aecSSergio Luis loaddebug(¤t->thread, 7); 16708687aecSSergio Luis #endif 16808687aecSSergio Luis } 16908687aecSSergio Luis 17008687aecSSergio Luis } 17108687aecSSergio Luis 17208687aecSSergio Luis /** 17308687aecSSergio Luis * __restore_processor_state - restore the contents of CPU registers saved 17408687aecSSergio Luis * by __save_processor_state() 17508687aecSSergio Luis * @ctxt - structure to load the registers contents from 17608687aecSSergio Luis */ 17708687aecSSergio Luis static void __restore_processor_state(struct saved_context *ctxt) 17808687aecSSergio Luis { 17908687aecSSergio Luis /* 18008687aecSSergio Luis * control registers 18108687aecSSergio Luis */ 18208687aecSSergio Luis /* cr4 was introduced in the Pentium CPU */ 18308687aecSSergio Luis #ifdef CONFIG_X86_32 18408687aecSSergio Luis if (ctxt->cr4) 18508687aecSSergio Luis write_cr4(ctxt->cr4); 18608687aecSSergio Luis #else 18708687aecSSergio Luis /* CONFIG X86_64 */ 18808687aecSSergio Luis wrmsrl(MSR_EFER, ctxt->efer); 18908687aecSSergio Luis write_cr8(ctxt->cr8); 19008687aecSSergio Luis write_cr4(ctxt->cr4); 19108687aecSSergio Luis #endif 19208687aecSSergio Luis write_cr3(ctxt->cr3); 19308687aecSSergio Luis write_cr2(ctxt->cr2); 19408687aecSSergio Luis write_cr0(ctxt->cr0); 19508687aecSSergio Luis 19608687aecSSergio Luis /* 19708687aecSSergio Luis * now restore the descriptor tables to their proper values 19808687aecSSergio Luis * ltr is done i fix_processor_context(). 19908687aecSSergio Luis */ 20008687aecSSergio Luis #ifdef CONFIG_X86_32 20108687aecSSergio Luis load_gdt(&ctxt->gdt); 20208687aecSSergio Luis load_idt(&ctxt->idt); 20308687aecSSergio Luis #else 20408687aecSSergio Luis /* CONFIG_X86_64 */ 20508687aecSSergio Luis load_gdt((const struct desc_ptr *)&ctxt->gdt_limit); 20608687aecSSergio Luis load_idt((const struct desc_ptr *)&ctxt->idt_limit); 20708687aecSSergio Luis #endif 20808687aecSSergio Luis 20908687aecSSergio Luis /* 21008687aecSSergio Luis * segment registers 21108687aecSSergio Luis */ 21208687aecSSergio Luis #ifdef CONFIG_X86_32 21308687aecSSergio Luis loadsegment(es, ctxt->es); 21408687aecSSergio Luis loadsegment(fs, ctxt->fs); 21508687aecSSergio Luis loadsegment(gs, ctxt->gs); 21608687aecSSergio Luis loadsegment(ss, ctxt->ss); 21708687aecSSergio Luis 21808687aecSSergio Luis /* 21908687aecSSergio Luis * sysenter MSRs 22008687aecSSergio Luis */ 22108687aecSSergio Luis if (boot_cpu_has(X86_FEATURE_SEP)) 22208687aecSSergio Luis enable_sep_cpu(); 22308687aecSSergio Luis #else 22408687aecSSergio Luis /* CONFIG_X86_64 */ 22508687aecSSergio Luis asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds)); 22608687aecSSergio Luis asm volatile ("movw %0, %%es" :: "r" (ctxt->es)); 22708687aecSSergio Luis asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs)); 22808687aecSSergio Luis load_gs_index(ctxt->gs); 22908687aecSSergio Luis asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss)); 23008687aecSSergio Luis 23108687aecSSergio Luis wrmsrl(MSR_FS_BASE, ctxt->fs_base); 23208687aecSSergio Luis wrmsrl(MSR_GS_BASE, ctxt->gs_base); 23308687aecSSergio Luis wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base); 23408687aecSSergio Luis #endif 23508687aecSSergio Luis 23608687aecSSergio Luis /* 23708687aecSSergio Luis * restore XCR0 for xsave capable cpu's. 23808687aecSSergio Luis */ 23908687aecSSergio Luis if (cpu_has_xsave) 24008687aecSSergio Luis xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask); 24108687aecSSergio Luis 24208687aecSSergio Luis fix_processor_context(); 24308687aecSSergio Luis 24408687aecSSergio Luis do_fpu_end(); 245*d0af9eedSSuresh Siddha mtrr_bp_restore(); 24608687aecSSergio Luis 2477262b6e4SHidetoshi Seto #ifdef CONFIG_X86_OLD_MCE 24808687aecSSergio Luis mcheck_init(&boot_cpu_data); 24908687aecSSergio Luis #endif 25008687aecSSergio Luis } 25108687aecSSergio Luis 25208687aecSSergio Luis /* Needed by apm.c */ 25308687aecSSergio Luis void restore_processor_state(void) 25408687aecSSergio Luis { 25508687aecSSergio Luis __restore_processor_state(&saved_context); 25608687aecSSergio Luis } 25708687aecSSergio Luis #ifdef CONFIG_X86_32 25808687aecSSergio Luis EXPORT_SYMBOL(restore_processor_state); 25908687aecSSergio Luis #endif 260