1 /*-
2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD$
34 */
35
36 #include <machine/asmacros.h>
37 #include <machine/specialreg.h>
38
39 #include "assym.s"
40 #include "opt_sched.h"
41
42 /*****************************************************************************/
43 /* Scheduling */
44 /*****************************************************************************/
45
46 .text
47
48 #ifdef SMP
49 #define LK lock ;
50 #else
51 #define LK
52 #endif
53
54 #if defined(SCHED_ULE) && defined(SMP)
55 #define SETLK xchgq
56 #else
57 #define SETLK movq
58 #endif
59
60 /*
61 * cpu_throw()
62 *
63 * This is the second half of cpu_switch(). It is used when the current
64 * thread is either a dummy or slated to die, and we no longer care
65 * about its state. This is only a slight optimization and is probably
66 * not worth it anymore. Note that we need to clear the pm_active bits so
67 * we do need the old proc if it still exists.
68 * %rdi = oldtd
69 * %rsi = newtd
70 */
71 ENTRY(cpu_throw)
72 movl PCPU(CPUID), %eax
73 testq %rdi,%rdi /* no thread? */
74 jz 1f
75 /* release bit from old pm_active */
76 movq TD_PROC(%rdi), %rdx /* oldtd->td_proc */
77 movq P_VMSPACE(%rdx), %rdx /* proc->p_vmspace */
78 LK btrl %eax, VM_PMAP+PM_ACTIVE(%rdx) /* clear old */
79 1:
80 movq TD_PCB(%rsi),%rdx /* newtd->td_proc */
81 movq PCB_CR3(%rdx),%rdx
82 movq %rdx,%cr3 /* new address space */
83 jmp swact
84 END(cpu_throw)
85
86 /*
87 * cpu_switch(old, new, mtx)
88 *
89 * Save the current thread state, then select the next thread to run
90 * and load its state.
91 * %rdi = oldtd
92 * %rsi = newtd
93 * %rdx = mtx
94 */
95 ENTRY(cpu_switch)
96 /* Switch to new thread. First, save context. */
97 movq TD_PCB(%rdi),%r8
98
99 movq (%rsp),%rax /* Hardware registers */
100 movq %rax,PCB_RIP(%r8)
101 movq %rbx,PCB_RBX(%r8)
102 movq %rsp,PCB_RSP(%r8)
103 movq %rbp,PCB_RBP(%r8)
104 movq %r12,PCB_R12(%r8)
105 movq %r13,PCB_R13(%r8)
106 movq %r14,PCB_R14(%r8)
107 movq %r15,PCB_R15(%r8)
108
109 testl $PCB_32BIT,PCB_FLAGS(%r8)
110 jz 1f /* no, skip over */
111
112 /* Save userland %gs */
113 movl %gs,PCB_GS(%r8)
114 movq PCB_GS32P(%r8),%rax
115 movq (%rax),%rax
116 movq %rax,PCB_GS32SD(%r8)
117
118 1:
119 /* Test if debug registers should be saved. */
120 testl $PCB_DBREGS,PCB_FLAGS(%r8)
121 jz 1f /* no, skip over */
122 movq %dr7,%rax /* yes, do the save */
123 movq %rax,PCB_DR7(%r8)
124 andq $0x0000fc00, %rax /* disable all watchpoints */
125 movq %rax,%dr7
126 movq %dr6,%rax
127 movq %rax,PCB_DR6(%r8)
128 movq %dr3,%rax
129 movq %rax,PCB_DR3(%r8)
130 movq %dr2,%rax
131 movq %rax,PCB_DR2(%r8)
132 movq %dr1,%rax
133 movq %rax,PCB_DR1(%r8)
134 movq %dr0,%rax
135 movq %rax,PCB_DR0(%r8)
136 1:
137
138 /* have we used fp, and need a save? */
139 cmpq %rdi,PCPU(FPCURTHREAD)
140 jne 1f
141 addq $PCB_SAVEFPU,%r8
142 clts
143 fxsave (%r8)
144 smsw %ax
145 orb $CR0_TS,%al
146 lmsw %ax
147 xorl %eax,%eax
148 movq %rax,PCPU(FPCURTHREAD)
149 1:
150
151 /* Save is done. Now fire up new thread. Leave old vmspace. */
152 movq TD_PCB(%rsi),%r8
153
154 /* switch address space */
155 movq PCB_CR3(%r8),%rcx
156 movq %cr3,%rax
157 cmpq %rcx,%rax /* Same address space? */
158 jne swinact
159 SETLK %rdx, TD_LOCK(%rdi) /* Release the old thread */
160 jmp sw1
161 swinact:
162 movq %rcx,%cr3 /* new address space */
163 movl PCPU(CPUID), %eax
164 /* Release bit from old pmap->pm_active */
165 movq TD_PROC(%rdi), %rcx /* oldproc */
166 movq P_VMSPACE(%rcx), %rcx
167 LK btrl %eax, VM_PMAP+PM_ACTIVE(%rcx) /* clear old */
168 SETLK %rdx, TD_LOCK(%rdi) /* Release the old thread */
169 swact:
170 /* Set bit in new pmap->pm_active */
171 movq TD_PROC(%rsi),%rdx /* newproc */
172 movq P_VMSPACE(%rdx), %rdx
173 LK btsl %eax, VM_PMAP+PM_ACTIVE(%rdx) /* set new */
174
175 sw1:
176 #if defined(SCHED_ULE) && defined(SMP)
177 /* Wait for the new thread to become unblocked */
178 movq $blocked_lock, %rdx
179 1:
180 movq TD_LOCK(%rsi),%rcx
181 cmpq %rcx, %rdx
182 pause
183 je 1b
184 lfence
185 #endif
186 /*
187 * At this point, we've switched address spaces and are ready
188 * to load up the rest of the next context.
189 */
190 movq TD_PCB(%rsi),%r8
191
192 /* Restore userland %fs */
193 movl $MSR_FSBASE,%ecx
194 movl PCB_FSBASE(%r8),%eax
195 movl PCB_FSBASE+4(%r8),%edx
196 wrmsr
197
198 /* Restore userland %gs */
199 movl $MSR_KGSBASE,%ecx
200 movl PCB_GSBASE(%r8),%eax
201 movl PCB_GSBASE+4(%r8),%edx
202 wrmsr
203
204 /* Update the TSS_RSP0 pointer for the next interrupt */
205 movq PCPU(TSSP), %rax
206 addq $COMMON_TSS_RSP0, %rax
207 leaq -16(%r8), %rbx
208 movq %rbx, (%rax)
209 movq %rbx, PCPU(RSP0)
210
211 movq %r8, PCPU(CURPCB)
212 movq %rsi, PCPU(CURTHREAD) /* into next thread */
213
214 testl $PCB_32BIT,PCB_FLAGS(%r8)
215 jz 1f /* no, skip over */
216
217 /* Restore userland %gs while preserving kernel gsbase */
218 movq PCB_GS32P(%r8),%rax
219 movq PCB_GS32SD(%r8),%rbx
220 movq %rbx,(%rax)
221 movl $MSR_GSBASE,%ecx
222 rdmsr
223 movl PCB_GS(%r8),%gs
224 wrmsr
225
226 1:
227 /* Restore context. */
228 movq PCB_RBX(%r8),%rbx
229 movq PCB_RSP(%r8),%rsp
230 movq PCB_RBP(%r8),%rbp
231 movq PCB_R12(%r8),%r12
232 movq PCB_R13(%r8),%r13
233 movq PCB_R14(%r8),%r14
234 movq PCB_R15(%r8),%r15
235 movq PCB_RIP(%r8),%rax
236 movq %rax,(%rsp)
237
238 /* Test if debug registers should be restored. */
239 testl $PCB_DBREGS,PCB_FLAGS(%r8)
240 jz 1f
241 movq PCB_DR6(%r8),%rax
242 movq %rax,%dr6
243 movq PCB_DR3(%r8),%rax
244 movq %rax,%dr3
245 movq PCB_DR2(%r8),%rax
246 movq %rax,%dr2
247 movq PCB_DR1(%r8),%rax
248 movq %rax,%dr1
249 movq PCB_DR0(%r8),%rax
250 movq %rax,%dr0
251 /* But preserve reserved bits in %dr7 */
252 movq %dr7,%rax
253 andq $0x0000fc00,%rax
254 movq PCB_DR7(%r8),%rcx
255 andq $~0x0000fc00,%rcx
256 orq %rcx,%rax
257 movq %rax,%dr7
258 1:
259 ret
260 END(cpu_switch)
261
262 /*
263 * savectx(pcb)
264 * Update pcb, saving current processor state.
265 */
266 ENTRY(savectx)
267 /* Fetch PCB. */
268 movq %rdi,%rcx
269
270 /* Save caller's return address. */
271 movq (%rsp),%rax
272 movq %rax,PCB_RIP(%rcx)
273
274 movq %cr3,%rax
275 movq %rax,PCB_CR3(%rcx)
276
277 movq %rbx,PCB_RBX(%rcx)
278 movq %rsp,PCB_RSP(%rcx)
279 movq %rbp,PCB_RBP(%rcx)
280 movq %r12,PCB_R12(%rcx)
281 movq %r13,PCB_R13(%rcx)
282 movq %r14,PCB_R14(%rcx)
283 movq %r15,PCB_R15(%rcx)
284
285 /*
286 * If fpcurthread == NULL, then the fpu h/w state is irrelevant and the
287 * state had better already be in the pcb. This is true for forks
288 * but not for dumps (the old book-keeping with FP flags in the pcb
289 * always lost for dumps because the dump pcb has 0 flags).
290 *
291 * If fpcurthread != NULL, then we have to save the fpu h/w state to
292 * fpcurthread's pcb and copy it to the requested pcb, or save to the
293 * requested pcb and reload. Copying is easier because we would
294 * have to handle h/w bugs for reloading. We used to lose the
295 * parent's fpu state for forks by forgetting to reload.
296 */
297 pushfq
298 cli
299 movq PCPU(FPCURTHREAD),%rax
300 testq %rax,%rax
301 je 1f
302
303 movq TD_PCB(%rax),%rdi
304 leaq PCB_SAVEFPU(%rdi),%rdi
305 clts
306 fxsave (%rdi)
307 smsw %ax
308 orb $CR0_TS,%al
309 lmsw %ax
310
311 movq $PCB_SAVEFPU_SIZE,%rdx /* arg 3 */
312 leaq PCB_SAVEFPU(%rcx),%rsi /* arg 2 */
313 /* arg 1 (%rdi) already loaded */
314 call bcopy
315 1:
316 popfq
317
318 ret
319 END(savectx)
Cache object: f30fec0abf14a41131baaf734c80b928
|