2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4 * Copyright (c) 2008-2009 Jung-uk Kim <jkim@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <machine/asmacros.h>
32 #include <machine/specialreg.h>
34 #include "acpi_wakedata.h"
37 #define WAKEUP_DECL(member) \
38 .set WAKEUP_ ## member, wakeup_ ## member - wakeup_ctx
50 #define WAKEUP_CTX(member) WAKEUP_ ## member (%rdi)
51 #define WAKEUP_PCB(member) PCB_ ## member(%r11)
52 #define WAKEUP_XPCB(member) XPCB_ ## member(%r11)
54 ENTRY(acpi_restorecpu)
55 /* Switch to KPML4phys. */
65 movq WAKEUP_CTX(xpcb), %r11
67 /* Force kernel segment registers. */
77 movl $MSR_FSBASE, %ecx
78 movl WAKEUP_PCB(FSBASE), %eax
79 movl 4 + WAKEUP_PCB(FSBASE), %edx
81 movl $MSR_GSBASE, %ecx
82 movl WAKEUP_PCB(GSBASE), %eax
83 movl 4 + WAKEUP_PCB(GSBASE), %edx
85 movl $MSR_KGSBASE, %ecx
86 movl WAKEUP_XPCB(KGSBASE), %eax
87 movl 4 + WAKEUP_XPCB(KGSBASE), %edx
92 movl WAKEUP_CTX(efer), %eax
97 movl WAKEUP_CTX(pat), %eax
98 movl 4 + WAKEUP_CTX(pat), %edx
101 /* Restore fast syscall stuff. */
103 movl WAKEUP_CTX(star), %eax
104 movl 4 + WAKEUP_CTX(star), %edx
106 movl $MSR_LSTAR, %ecx
107 movl WAKEUP_CTX(lstar), %eax
108 movl 4 + WAKEUP_CTX(lstar), %edx
110 movl $MSR_CSTAR, %ecx
111 movl WAKEUP_CTX(cstar), %eax
112 movl 4 + WAKEUP_CTX(cstar), %edx
114 movl $MSR_SF_MASK, %ecx
115 movl WAKEUP_CTX(sfmask), %eax
118 /* Restore CR0, CR2 and CR4. */
119 movq WAKEUP_XPCB(CR0), %rax
121 movq WAKEUP_XPCB(CR2), %rax
123 movq WAKEUP_XPCB(CR4), %rax
126 /* Restore descriptor tables. */
127 lidt WAKEUP_XPCB(IDT)
128 lldt WAKEUP_XPCB(LDT)
131 #define SDT_SYSBSY 11
133 /* Clear "task busy" bit and reload TR. */
135 andb $(~SDT_SYSBSY | SDT_SYSTSS), 5(%rax)
136 movw WAKEUP_XPCB(TR), %ax
142 /* Restore other callee saved registers. */
143 movq WAKEUP_PCB(R15), %r15
144 movq WAKEUP_PCB(R14), %r14
145 movq WAKEUP_PCB(R13), %r13
146 movq WAKEUP_PCB(R12), %r12
147 movq WAKEUP_PCB(RBP), %rbp
148 movq WAKEUP_PCB(RSP), %rsp
149 movq WAKEUP_PCB(RBX), %rbx
151 /* Restore debug registers. */
152 movq WAKEUP_PCB(DR0), %rax
154 movq WAKEUP_PCB(DR1), %rax
156 movq WAKEUP_PCB(DR2), %rax
158 movq WAKEUP_PCB(DR3), %rax
160 movq WAKEUP_PCB(DR6), %rax
162 movq WAKEUP_PCB(DR7), %rax
165 /* Restore return address. */
166 movq WAKEUP_PCB(RIP), %rax
169 /* Indicate the CPU is resumed. */
171 movl %eax, WAKEUP_CTX(cpu)
177 /* Fetch XPCB and save CPU context. */
182 /* Patch caller's return address and stack pointer. */
184 movq %rax, WAKEUP_PCB(RIP)
186 movq %rax, WAKEUP_PCB(RSP)