1 /*-
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>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: releng/8.0/sys/amd64/acpica/acpi_switch.S 190636 2009-04-02 01:46:57Z jkim $
29 */
30
31 #include <machine/asmacros.h>
32 #include <machine/specialreg.h>
33
34 #include "acpi_wakedata.h"
35 #include "assym.s"
36
37 #define WAKEUP_DECL(member) \
38 .set WAKEUP_ ## member, wakeup_ ## member - wakeup_ctx
39
40 WAKEUP_DECL(xpcb)
41 WAKEUP_DECL(gdt)
42 WAKEUP_DECL(efer)
43 WAKEUP_DECL(pat)
44 WAKEUP_DECL(star)
45 WAKEUP_DECL(lstar)
46 WAKEUP_DECL(cstar)
47 WAKEUP_DECL(sfmask)
48 WAKEUP_DECL(cpu)
49
50 #define WAKEUP_CTX(member) WAKEUP_ ## member (%rdi)
51 #define WAKEUP_PCB(member) PCB_ ## member(%r11)
52 #define WAKEUP_XPCB(member) XPCB_ ## member(%r11)
53
54 ENTRY(acpi_restorecpu)
55 /* Switch to KPML4phys. */
56 movq %rsi, %rax
57 movq %rax, %cr3
58
59 /* Restore GDT. */
60 lgdt WAKEUP_CTX(gdt)
61 jmp 1f
62 1:
63
64 /* Fetch PCB. */
65 movq WAKEUP_CTX(xpcb), %r11
66
67 /* Force kernel segment registers. */
68 movl $KDSEL, %eax
69 movw %ax, %ds
70 movw %ax, %es
71 movw %ax, %ss
72 movl $KUF32SEL, %eax
73 movw %ax, %fs
74 movl $KUG32SEL, %eax
75 movw %ax, %gs
76
77 movl $MSR_FSBASE, %ecx
78 movl WAKEUP_PCB(FSBASE), %eax
79 movl 4 + WAKEUP_PCB(FSBASE), %edx
80 wrmsr
81 movl $MSR_GSBASE, %ecx
82 movl WAKEUP_PCB(GSBASE), %eax
83 movl 4 + WAKEUP_PCB(GSBASE), %edx
84 wrmsr
85 movl $MSR_KGSBASE, %ecx
86 movl WAKEUP_XPCB(KGSBASE), %eax
87 movl 4 + WAKEUP_XPCB(KGSBASE), %edx
88 wrmsr
89
90 /* Restore EFER. */
91 movl $MSR_EFER, %ecx
92 movl WAKEUP_CTX(efer), %eax
93 wrmsr
94
95 /* Restore PAT. */
96 movl $MSR_PAT, %ecx
97 movl WAKEUP_CTX(pat), %eax
98 movl 4 + WAKEUP_CTX(pat), %edx
99 wrmsr
100
101 /* Restore fast syscall stuff. */
102 movl $MSR_STAR, %ecx
103 movl WAKEUP_CTX(star), %eax
104 movl 4 + WAKEUP_CTX(star), %edx
105 wrmsr
106 movl $MSR_LSTAR, %ecx
107 movl WAKEUP_CTX(lstar), %eax
108 movl 4 + WAKEUP_CTX(lstar), %edx
109 wrmsr
110 movl $MSR_CSTAR, %ecx
111 movl WAKEUP_CTX(cstar), %eax
112 movl 4 + WAKEUP_CTX(cstar), %edx
113 wrmsr
114 movl $MSR_SF_MASK, %ecx
115 movl WAKEUP_CTX(sfmask), %eax
116 wrmsr
117
118 /* Restore CR0, CR2 and CR4. */
119 movq WAKEUP_XPCB(CR0), %rax
120 movq %rax, %cr0
121 movq WAKEUP_XPCB(CR2), %rax
122 movq %rax, %cr2
123 movq WAKEUP_XPCB(CR4), %rax
124 movq %rax, %cr4
125
126 /* Restore descriptor tables. */
127 lidt WAKEUP_XPCB(IDT)
128 lldt WAKEUP_XPCB(LDT)
129
130 #define SDT_SYSTSS 9
131 #define SDT_SYSBSY 11
132
133 /* Clear "task busy" bit and reload TR. */
134 movq PCPU(TSS), %rax
135 andb $(~SDT_SYSBSY | SDT_SYSTSS), 5(%rax)
136 movw WAKEUP_XPCB(TR), %ax
137 ltr %ax
138
139 #undef SDT_SYSTSS
140 #undef SDT_SYSBSY
141
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
150
151 /* Restore debug registers. */
152 movq WAKEUP_PCB(DR0), %rax
153 movq %rax, %dr0
154 movq WAKEUP_PCB(DR1), %rax
155 movq %rax, %dr1
156 movq WAKEUP_PCB(DR2), %rax
157 movq %rax, %dr2
158 movq WAKEUP_PCB(DR3), %rax
159 movq %rax, %dr3
160 movq WAKEUP_PCB(DR6), %rax
161 movq %rax, %dr6
162 movq WAKEUP_PCB(DR7), %rax
163 movq %rax, %dr7
164
165 /* Restore return address. */
166 movq WAKEUP_PCB(RIP), %rax
167 movq %rax, (%rsp)
168
169 /* Indicate the CPU is resumed. */
170 xorl %eax, %eax
171 movl %eax, WAKEUP_CTX(cpu)
172
173 ret
174 END(acpi_restorecpu)
175
176 ENTRY(acpi_savecpu)
177 /* Fetch XPCB and save CPU context. */
178 movq %rdi, %r10
179 call savectx2
180 movq %r10, %r11
181
182 /* Patch caller's return address and stack pointer. */
183 movq (%rsp), %rax
184 movq %rax, WAKEUP_PCB(RIP)
185 movq %rsp, %rax
186 movq %rax, WAKEUP_PCB(RSP)
187
188 movl $1, %eax
189 ret
190 END(acpi_savecpu)
Cache object: b3b6c17a9081a412e1f166a220dc9e3f
|