cpu.c revision 1.54 1 1.54 jruoho /* $NetBSD: cpu.c,v 1.54 2011/02/24 10:56:03 jruoho Exp $ */
2 1.2 bouyer /* NetBSD: cpu.c,v 1.18 2004/02/20 17:35:01 yamt Exp */
3 1.2 bouyer
4 1.2 bouyer /*-
5 1.2 bouyer * Copyright (c) 2000 The NetBSD Foundation, Inc.
6 1.19 joerg * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
7 1.2 bouyer * All rights reserved.
8 1.2 bouyer *
9 1.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
10 1.2 bouyer * by RedBack Networks Inc.
11 1.2 bouyer *
12 1.2 bouyer * Author: Bill Sommerfeld
13 1.2 bouyer *
14 1.2 bouyer * Redistribution and use in source and binary forms, with or without
15 1.2 bouyer * modification, are permitted provided that the following conditions
16 1.2 bouyer * are met:
17 1.2 bouyer * 1. Redistributions of source code must retain the above copyright
18 1.2 bouyer * notice, this list of conditions and the following disclaimer.
19 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
20 1.2 bouyer * notice, this list of conditions and the following disclaimer in the
21 1.2 bouyer * documentation and/or other materials provided with the distribution.
22 1.2 bouyer *
23 1.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
34 1.2 bouyer */
35 1.2 bouyer
36 1.2 bouyer /*
37 1.2 bouyer * Copyright (c) 1999 Stefan Grefen
38 1.2 bouyer *
39 1.2 bouyer * Redistribution and use in source and binary forms, with or without
40 1.2 bouyer * modification, are permitted provided that the following conditions
41 1.2 bouyer * are met:
42 1.2 bouyer * 1. Redistributions of source code must retain the above copyright
43 1.2 bouyer * notice, this list of conditions and the following disclaimer.
44 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
45 1.2 bouyer * notice, this list of conditions and the following disclaimer in the
46 1.2 bouyer * documentation and/or other materials provided with the distribution.
47 1.2 bouyer * 3. All advertising materials mentioning features or use of this software
48 1.2 bouyer * must display the following acknowledgement:
49 1.2 bouyer * This product includes software developed by the NetBSD
50 1.2 bouyer * Foundation, Inc. and its contributors.
51 1.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
52 1.2 bouyer * contributors may be used to endorse or promote products derived
53 1.2 bouyer * from this software without specific prior written permission.
54 1.2 bouyer *
55 1.2 bouyer * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
56 1.2 bouyer * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 1.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 1.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
59 1.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 1.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 1.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 1.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 1.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 1.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 1.2 bouyer * SUCH DAMAGE.
66 1.2 bouyer */
67 1.2 bouyer
68 1.2 bouyer #include <sys/cdefs.h>
69 1.54 jruoho __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.54 2011/02/24 10:56:03 jruoho Exp $");
70 1.2 bouyer
71 1.2 bouyer #include "opt_ddb.h"
72 1.2 bouyer #include "opt_multiprocessor.h"
73 1.2 bouyer #include "opt_mpbios.h" /* for MPDEBUG */
74 1.2 bouyer #include "opt_mtrr.h"
75 1.2 bouyer #include "opt_xen.h"
76 1.2 bouyer
77 1.2 bouyer #include "lapic.h"
78 1.2 bouyer #include "ioapic.h"
79 1.2 bouyer
80 1.2 bouyer #include <sys/param.h>
81 1.2 bouyer #include <sys/proc.h>
82 1.2 bouyer #include <sys/systm.h>
83 1.2 bouyer #include <sys/device.h>
84 1.31 cegger #include <sys/kmem.h>
85 1.11 cegger #include <sys/cpu.h>
86 1.11 cegger #include <sys/atomic.h>
87 1.32 cegger #include <sys/reboot.h>
88 1.2 bouyer
89 1.51 uebayasi #include <uvm/uvm.h>
90 1.2 bouyer
91 1.2 bouyer #include <machine/cpufunc.h>
92 1.2 bouyer #include <machine/cpuvar.h>
93 1.2 bouyer #include <machine/pmap.h>
94 1.2 bouyer #include <machine/vmparam.h>
95 1.2 bouyer #include <machine/mpbiosvar.h>
96 1.2 bouyer #include <machine/pcb.h>
97 1.2 bouyer #include <machine/specialreg.h>
98 1.2 bouyer #include <machine/segments.h>
99 1.2 bouyer #include <machine/gdt.h>
100 1.2 bouyer #include <machine/mtrr.h>
101 1.2 bouyer #include <machine/pio.h>
102 1.2 bouyer
103 1.2 bouyer #include <xen/vcpuvar.h>
104 1.2 bouyer
105 1.2 bouyer #if NLAPIC > 0
106 1.2 bouyer #include <machine/apicvar.h>
107 1.2 bouyer #include <machine/i82489reg.h>
108 1.2 bouyer #include <machine/i82489var.h>
109 1.2 bouyer #endif
110 1.2 bouyer
111 1.2 bouyer #include <dev/ic/mc146818reg.h>
112 1.2 bouyer #include <dev/isa/isareg.h>
113 1.2 bouyer
114 1.38 cegger #if MAXCPUS > 32
115 1.38 cegger #error cpu_info contains 32bit bitmasks
116 1.38 cegger #endif
117 1.27 ad
118 1.53 jruoho int cpu_match(device_t, cfdata_t, void *);
119 1.53 jruoho void cpu_attach(device_t, device_t, void *);
120 1.53 jruoho int cpu_rescan(device_t, const char *, const int *);
121 1.53 jruoho void cpu_childdetached(device_t, device_t);
122 1.53 jruoho int vcpu_match(device_t, cfdata_t, void *);
123 1.53 jruoho void vcpu_attach(device_t, device_t, void *);
124 1.53 jruoho void cpu_attach_common(device_t, device_t, void *);
125 1.8 dogcow void cpu_offline_md(void);
126 1.2 bouyer
127 1.2 bouyer struct cpu_softc {
128 1.10 cegger device_t sc_dev; /* device tree glue */
129 1.2 bouyer struct cpu_info *sc_info; /* pointer to CPU info */
130 1.32 cegger bool sc_wasonline;
131 1.2 bouyer };
132 1.2 bouyer
133 1.5 joerg int mp_cpu_start(struct cpu_info *, paddr_t);
134 1.2 bouyer void mp_cpu_start_cleanup(struct cpu_info *);
135 1.2 bouyer const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
136 1.2 bouyer mp_cpu_start_cleanup };
137 1.2 bouyer
138 1.53 jruoho CFATTACH_DECL2_NEW(cpu, sizeof(struct cpu_softc),
139 1.53 jruoho cpu_match, cpu_attach, NULL, NULL, cpu_rescan, cpu_childdetached);
140 1.53 jruoho
141 1.10 cegger CFATTACH_DECL_NEW(vcpu, sizeof(struct cpu_softc),
142 1.2 bouyer vcpu_match, vcpu_attach, NULL, NULL);
143 1.2 bouyer
144 1.2 bouyer /*
145 1.2 bouyer * Statically-allocated CPU info for the primary CPU (or the only
146 1.2 bouyer * CPU, on uniprocessors). The CPU info list is initialized to
147 1.2 bouyer * point at it.
148 1.2 bouyer */
149 1.2 bouyer #ifdef TRAPLOG
150 1.2 bouyer #include <machine/tlog.h>
151 1.2 bouyer struct tlog tlog_primary;
152 1.2 bouyer #endif
153 1.38 cegger struct cpu_info cpu_info_primary __aligned(CACHE_LINE_SIZE) = {
154 1.7 bouyer .ci_dev = 0,
155 1.2 bouyer .ci_self = &cpu_info_primary,
156 1.4 bouyer .ci_idepth = -1,
157 1.2 bouyer .ci_curlwp = &lwp0,
158 1.25 ad .ci_curldt = -1,
159 1.2 bouyer #ifdef TRAPLOG
160 1.2 bouyer .ci_tlog = &tlog_primary,
161 1.2 bouyer #endif
162 1.2 bouyer
163 1.2 bouyer };
164 1.38 cegger struct cpu_info phycpu_info_primary __aligned(CACHE_LINE_SIZE) = {
165 1.7 bouyer .ci_dev = 0,
166 1.2 bouyer .ci_self = &phycpu_info_primary,
167 1.2 bouyer };
168 1.2 bouyer
169 1.2 bouyer struct cpu_info *cpu_info_list = &cpu_info_primary;
170 1.38 cegger struct cpu_info *phycpu_info_list = &phycpu_info_primary;
171 1.2 bouyer
172 1.2 bouyer static void cpu_set_tss_gates(struct cpu_info *ci);
173 1.2 bouyer
174 1.11 cegger uint32_t cpus_attached = 0;
175 1.11 cegger uint32_t cpus_running = 0;
176 1.11 cegger
177 1.38 cegger uint32_t phycpus_attached = 0;
178 1.38 cegger uint32_t phycpus_running = 0;
179 1.38 cegger
180 1.43 jym uint32_t cpu_feature[5]; /* X86 CPUID feature bits
181 1.43 jym * [0] basic features %edx
182 1.43 jym * [1] basic features %ecx
183 1.43 jym * [2] extended features %edx
184 1.43 jym * [3] extended features %ecx
185 1.43 jym * [4] VIA padlock features
186 1.43 jym */
187 1.43 jym
188 1.11 cegger bool x86_mp_online;
189 1.11 cegger paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
190 1.2 bouyer
191 1.38 cegger #if defined(MULTIPROCESSOR)
192 1.2 bouyer void cpu_hatch(void *);
193 1.2 bouyer static void cpu_boot_secondary(struct cpu_info *ci);
194 1.2 bouyer static void cpu_start_secondary(struct cpu_info *ci);
195 1.2 bouyer static void cpu_copy_trampoline(void);
196 1.2 bouyer
197 1.2 bouyer /*
198 1.2 bouyer * Runs once per boot once multiprocessor goo has been detected and
199 1.2 bouyer * the local APIC on the boot processor has been mapped.
200 1.2 bouyer *
201 1.2 bouyer * Called from lapic_boot_init() (from mpbios_scan()).
202 1.2 bouyer */
203 1.2 bouyer void
204 1.10 cegger cpu_init_first(void)
205 1.2 bouyer {
206 1.2 bouyer
207 1.38 cegger cpu_info_primary.ci_cpuid = lapic_cpu_number();
208 1.2 bouyer cpu_copy_trampoline();
209 1.2 bouyer }
210 1.38 cegger #endif /* MULTIPROCESSOR */
211 1.2 bouyer
212 1.2 bouyer int
213 1.10 cegger cpu_match(device_t parent, cfdata_t match, void *aux)
214 1.2 bouyer {
215 1.2 bouyer
216 1.2 bouyer return 1;
217 1.2 bouyer }
218 1.2 bouyer
219 1.2 bouyer void
220 1.10 cegger cpu_attach(device_t parent, device_t self, void *aux)
221 1.2 bouyer {
222 1.10 cegger struct cpu_softc *sc = device_private(self);
223 1.2 bouyer struct cpu_attach_args *caa = aux;
224 1.2 bouyer struct cpu_info *ci;
225 1.34 cegger uintptr_t ptr;
226 1.52 bouyer static int nphycpu = 0;
227 1.2 bouyer
228 1.10 cegger sc->sc_dev = self;
229 1.10 cegger
230 1.38 cegger if (phycpus_attached == ~0) {
231 1.34 cegger aprint_error(": increase MAXCPUS\n");
232 1.34 cegger return;
233 1.34 cegger }
234 1.34 cegger
235 1.2 bouyer /*
236 1.2 bouyer * If we're an Application Processor, allocate a cpu_info
237 1.52 bouyer * If we're the first attached CPU use the primary cpu_info,
238 1.52 bouyer * otherwise allocate a new one
239 1.2 bouyer */
240 1.52 bouyer aprint_naive("\n");
241 1.52 bouyer aprint_normal("\n");
242 1.52 bouyer if (nphycpu > 0) {
243 1.52 bouyer struct cpu_info *tmp;
244 1.34 cegger ptr = (uintptr_t)kmem_zalloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
245 1.34 cegger KM_SLEEP);
246 1.42 jym ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
247 1.24 ad ci->ci_curldt = -1;
248 1.52 bouyer
249 1.52 bouyer tmp = phycpu_info_list;
250 1.52 bouyer while (tmp->ci_next)
251 1.52 bouyer tmp = tmp->ci_next;
252 1.52 bouyer
253 1.52 bouyer tmp->ci_next = ci;
254 1.2 bouyer } else {
255 1.2 bouyer ci = &phycpu_info_primary;
256 1.2 bouyer }
257 1.2 bouyer
258 1.2 bouyer ci->ci_self = ci;
259 1.2 bouyer sc->sc_info = ci;
260 1.2 bouyer
261 1.2 bouyer ci->ci_dev = self;
262 1.50 jruoho ci->ci_acpiid = caa->cpu_id;
263 1.23 ad ci->ci_cpuid = caa->cpu_number;
264 1.16 cegger ci->ci_vcpu = NULL;
265 1.52 bouyer ci->ci_index = nphycpu++;
266 1.52 bouyer ci->ci_cpumask = (1 << cpu_index(ci));
267 1.2 bouyer
268 1.52 bouyer atomic_or_32(&phycpus_attached, ci->ci_cpumask);
269 1.38 cegger
270 1.52 bouyer if (!pmf_device_register(self, NULL, NULL))
271 1.52 bouyer aprint_error_dev(self, "couldn't establish power handler\n");
272 1.34 cegger
273 1.2 bouyer return;
274 1.2 bouyer }
275 1.2 bouyer
276 1.2 bouyer int
277 1.53 jruoho cpu_rescan(device_t self, const char *ifattr, const int *locators)
278 1.53 jruoho {
279 1.53 jruoho struct cpu_softc *sc = device_private(self);
280 1.53 jruoho struct cpufeature_attach_args cfaa;
281 1.53 jruoho struct cpu_info *ci = sc->sc_info;
282 1.53 jruoho
283 1.53 jruoho memset(&cfaa, 0, sizeof(cfaa));
284 1.53 jruoho cfaa.ci = ci;
285 1.53 jruoho
286 1.53 jruoho if (ifattr_match(ifattr, "cpufeaturebus")) {
287 1.53 jruoho
288 1.53 jruoho if (ci->ci_frequency == NULL) {
289 1.53 jruoho cfaa.name = "est";
290 1.53 jruoho ci->ci_frequency = config_found_ia(self,
291 1.53 jruoho "cpufeaturebus", &cfaa, NULL);
292 1.53 jruoho }
293 1.54 jruoho
294 1.54 jruoho if (ci->ci_frequency == NULL) {
295 1.54 jruoho cfaa.name = "powernow";
296 1.54 jruoho ci->ci_frequency = config_found_ia(self,
297 1.54 jruoho "cpufeaturebus", &cfaa, NULL);
298 1.54 jruoho }
299 1.53 jruoho }
300 1.53 jruoho
301 1.53 jruoho return 0;
302 1.53 jruoho }
303 1.53 jruoho
304 1.53 jruoho void
305 1.53 jruoho cpu_childdetached(device_t self, device_t child)
306 1.53 jruoho {
307 1.53 jruoho struct cpu_softc *sc = device_private(self);
308 1.53 jruoho struct cpu_info *ci = sc->sc_info;
309 1.53 jruoho
310 1.53 jruoho if (ci->ci_frequency == child)
311 1.53 jruoho ci->ci_frequency = NULL;
312 1.53 jruoho }
313 1.53 jruoho
314 1.53 jruoho int
315 1.10 cegger vcpu_match(device_t parent, cfdata_t match, void *aux)
316 1.2 bouyer {
317 1.2 bouyer struct vcpu_attach_args *vcaa = aux;
318 1.2 bouyer
319 1.2 bouyer if (strcmp(vcaa->vcaa_name, match->cf_name) == 0)
320 1.2 bouyer return 1;
321 1.2 bouyer return 0;
322 1.2 bouyer }
323 1.2 bouyer
324 1.2 bouyer void
325 1.10 cegger vcpu_attach(device_t parent, device_t self, void *aux)
326 1.2 bouyer {
327 1.2 bouyer struct vcpu_attach_args *vcaa = aux;
328 1.2 bouyer
329 1.2 bouyer cpu_attach_common(parent, self, &vcaa->vcaa_caa);
330 1.2 bouyer }
331 1.2 bouyer
332 1.2 bouyer static void
333 1.2 bouyer cpu_vm_init(struct cpu_info *ci)
334 1.2 bouyer {
335 1.2 bouyer int ncolors = 2, i;
336 1.2 bouyer
337 1.2 bouyer for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
338 1.2 bouyer struct x86_cache_info *cai;
339 1.2 bouyer int tcolors;
340 1.2 bouyer
341 1.2 bouyer cai = &ci->ci_cinfo[i];
342 1.2 bouyer
343 1.2 bouyer tcolors = atop(cai->cai_totalsize);
344 1.2 bouyer switch(cai->cai_associativity) {
345 1.2 bouyer case 0xff:
346 1.2 bouyer tcolors = 1; /* fully associative */
347 1.2 bouyer break;
348 1.2 bouyer case 0:
349 1.2 bouyer case 1:
350 1.2 bouyer break;
351 1.2 bouyer default:
352 1.2 bouyer tcolors /= cai->cai_associativity;
353 1.2 bouyer }
354 1.2 bouyer ncolors = max(ncolors, tcolors);
355 1.2 bouyer }
356 1.2 bouyer
357 1.2 bouyer /*
358 1.2 bouyer * Knowing the size of the largest cache on this CPU, re-color
359 1.2 bouyer * our pages.
360 1.2 bouyer */
361 1.2 bouyer if (ncolors <= uvmexp.ncolors)
362 1.2 bouyer return;
363 1.28 bouyer aprint_debug_dev(ci->ci_dev, "%d page colors\n", ncolors);
364 1.2 bouyer uvm_page_recolor(ncolors);
365 1.2 bouyer }
366 1.2 bouyer
367 1.2 bouyer void
368 1.11 cegger cpu_attach_common(device_t parent, device_t self, void *aux)
369 1.2 bouyer {
370 1.10 cegger struct cpu_softc *sc = device_private(self);
371 1.2 bouyer struct cpu_attach_args *caa = aux;
372 1.2 bouyer struct cpu_info *ci;
373 1.12 cegger uintptr_t ptr;
374 1.2 bouyer int cpunum = caa->cpu_number;
375 1.38 cegger static bool again = false;
376 1.2 bouyer
377 1.10 cegger sc->sc_dev = self;
378 1.10 cegger
379 1.2 bouyer /*
380 1.2 bouyer * If we're an Application Processor, allocate a cpu_info
381 1.2 bouyer * structure, otherwise use the primary's.
382 1.2 bouyer */
383 1.2 bouyer if (caa->cpu_role == CPU_ROLE_AP) {
384 1.12 cegger aprint_naive(": Application Processor\n");
385 1.31 cegger ptr = (uintptr_t)kmem_alloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
386 1.31 cegger KM_SLEEP);
387 1.42 jym ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
388 1.12 cegger memset(ci, 0, sizeof(*ci));
389 1.2 bouyer #ifdef TRAPLOG
390 1.31 cegger ci->ci_tlog_base = kmem_zalloc(sizeof(struct tlog), KM_SLEEP);
391 1.2 bouyer #endif
392 1.2 bouyer } else {
393 1.12 cegger aprint_naive(": %s Processor\n",
394 1.12 cegger caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
395 1.2 bouyer ci = &cpu_info_primary;
396 1.38 cegger #if NLAPIC > 0
397 1.38 cegger if (cpunum != lapic_cpu_number()) {
398 1.38 cegger /* XXX should be done earlier */
399 1.38 cegger uint32_t reg;
400 1.38 cegger aprint_verbose("\n");
401 1.38 cegger aprint_verbose_dev(self, "running CPU at apic %d"
402 1.38 cegger " instead of at expected %d", lapic_cpu_number(),
403 1.38 cegger cpunum);
404 1.38 cegger reg = i82489_readreg(LAPIC_ID);
405 1.38 cegger i82489_writereg(LAPIC_ID, (reg & ~LAPIC_ID_MASK) |
406 1.38 cegger (cpunum << LAPIC_ID_SHIFT));
407 1.38 cegger }
408 1.2 bouyer if (cpunum != lapic_cpu_number()) {
409 1.38 cegger aprint_error_dev(self, "unable to reset apic id\n");
410 1.2 bouyer }
411 1.2 bouyer #endif
412 1.2 bouyer }
413 1.2 bouyer
414 1.2 bouyer ci->ci_self = ci;
415 1.2 bouyer sc->sc_info = ci;
416 1.2 bouyer ci->ci_dev = self;
417 1.23 ad ci->ci_cpuid = cpunum;
418 1.16 cegger
419 1.16 cegger KASSERT(HYPERVISOR_shared_info != NULL);
420 1.16 cegger ci->ci_vcpu = &HYPERVISOR_shared_info->vcpu_info[cpunum];
421 1.16 cegger
422 1.2 bouyer ci->ci_func = caa->cpu_func;
423 1.2 bouyer
424 1.38 cegger /* Must be called before mi_cpu_attach(). */
425 1.38 cegger cpu_vm_init(ci);
426 1.38 cegger
427 1.2 bouyer if (caa->cpu_role == CPU_ROLE_AP) {
428 1.2 bouyer int error;
429 1.2 bouyer
430 1.2 bouyer error = mi_cpu_attach(ci);
431 1.2 bouyer if (error != 0) {
432 1.2 bouyer aprint_normal("\n");
433 1.38 cegger aprint_error_dev(self,
434 1.38 cegger "mi_cpu_attach failed with %d\n", error);
435 1.2 bouyer return;
436 1.2 bouyer }
437 1.2 bouyer } else {
438 1.2 bouyer KASSERT(ci->ci_data.cpu_idlelwp != NULL);
439 1.2 bouyer }
440 1.2 bouyer
441 1.23 ad ci->ci_cpumask = (1 << cpu_index(ci));
442 1.2 bouyer pmap_reference(pmap_kernel());
443 1.2 bouyer ci->ci_pmap = pmap_kernel();
444 1.2 bouyer ci->ci_tlbstate = TLBSTATE_STALE;
445 1.2 bouyer
446 1.38 cegger /*
447 1.38 cegger * Boot processor may not be attached first, but the below
448 1.38 cegger * must be done to allow booting other processors.
449 1.38 cegger */
450 1.38 cegger if (!again) {
451 1.38 cegger atomic_or_32(&ci->ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
452 1.38 cegger /* Basic init. */
453 1.38 cegger cpu_intr_init(ci);
454 1.38 cegger cpu_get_tsc_freq(ci);
455 1.38 cegger cpu_init(ci);
456 1.38 cegger cpu_set_tss_gates(ci);
457 1.38 cegger pmap_cpu_init_late(ci);
458 1.38 cegger #if NLAPIC > 0
459 1.38 cegger if (caa->cpu_role != CPU_ROLE_SP) {
460 1.38 cegger /* Enable lapic. */
461 1.38 cegger lapic_enable();
462 1.38 cegger lapic_set_lvt();
463 1.38 cegger lapic_calibrate_timer();
464 1.38 cegger }
465 1.38 cegger #endif
466 1.38 cegger /* Make sure DELAY() is initialized. */
467 1.38 cegger DELAY(1);
468 1.38 cegger again = true;
469 1.38 cegger }
470 1.38 cegger
471 1.2 bouyer /* further PCB init done later. */
472 1.2 bouyer
473 1.2 bouyer switch (caa->cpu_role) {
474 1.2 bouyer case CPU_ROLE_SP:
475 1.38 cegger atomic_or_32(&ci->ci_flags, CPUF_SP);
476 1.21 ad cpu_identify(ci);
477 1.12 cegger #if 0
478 1.12 cegger x86_errata();
479 1.12 cegger #endif
480 1.38 cegger x86_cpu_idle_init();
481 1.2 bouyer break;
482 1.2 bouyer
483 1.2 bouyer case CPU_ROLE_BP:
484 1.38 cegger atomic_or_32(&ci->ci_flags, CPUF_BSP);
485 1.21 ad cpu_identify(ci);
486 1.2 bouyer cpu_init(ci);
487 1.14 bouyer #if 0
488 1.12 cegger x86_errata();
489 1.12 cegger #endif
490 1.38 cegger x86_cpu_idle_init();
491 1.2 bouyer break;
492 1.2 bouyer
493 1.2 bouyer case CPU_ROLE_AP:
494 1.2 bouyer /*
495 1.2 bouyer * report on an AP
496 1.2 bouyer */
497 1.2 bouyer
498 1.2 bouyer #if defined(MULTIPROCESSOR)
499 1.2 bouyer cpu_intr_init(ci);
500 1.2 bouyer gdt_alloc_cpu(ci);
501 1.2 bouyer cpu_set_tss_gates(ci);
502 1.12 cegger pmap_cpu_init_early(ci);
503 1.12 cegger pmap_cpu_init_late(ci);
504 1.2 bouyer cpu_start_secondary(ci);
505 1.2 bouyer if (ci->ci_flags & CPUF_PRESENT) {
506 1.30 cegger struct cpu_info *tmp;
507 1.30 cegger
508 1.2 bouyer identifycpu(ci);
509 1.30 cegger tmp = cpu_info_list;
510 1.30 cegger while (tmp->ci_next)
511 1.30 cegger tmp = tmp->ci_next;
512 1.30 cegger
513 1.30 cegger tmp->ci_next = ci;
514 1.2 bouyer }
515 1.2 bouyer #else
516 1.38 cegger aprint_error_dev(self, "not started\n");
517 1.2 bouyer #endif
518 1.2 bouyer break;
519 1.2 bouyer
520 1.2 bouyer default:
521 1.12 cegger aprint_normal("\n");
522 1.2 bouyer panic("unknown processor type??\n");
523 1.2 bouyer }
524 1.2 bouyer
525 1.46 cegger pat_init(ci);
526 1.34 cegger atomic_or_32(&cpus_attached, ci->ci_cpumask);
527 1.2 bouyer
528 1.12 cegger #if 0
529 1.12 cegger if (!pmf_device_register(self, cpu_suspend, cpu_resume))
530 1.12 cegger aprint_error_dev(self, "couldn't establish power handler\n");
531 1.12 cegger #endif
532 1.12 cegger
533 1.2 bouyer #if defined(MULTIPROCESSOR)
534 1.2 bouyer if (mp_verbose) {
535 1.2 bouyer struct lwp *l = ci->ci_data.cpu_idlelwp;
536 1.37 rmind struct pcb *pcb = lwp_getpcb(l);
537 1.2 bouyer
538 1.38 cegger aprint_verbose_dev(self,
539 1.38 cegger "idle lwp at %p, idle sp at 0x%p\n",
540 1.12 cegger l,
541 1.12 cegger #ifdef i386
542 1.37 rmind (void *)pcb->pcb_esp
543 1.12 cegger #else
544 1.37 rmind (void *)pcb->pcb_rsp
545 1.12 cegger #endif
546 1.12 cegger );
547 1.12 cegger
548 1.2 bouyer }
549 1.2 bouyer #endif
550 1.2 bouyer }
551 1.2 bouyer
552 1.2 bouyer /*
553 1.2 bouyer * Initialize the processor appropriately.
554 1.2 bouyer */
555 1.2 bouyer
556 1.2 bouyer void
557 1.10 cegger cpu_init(struct cpu_info *ci)
558 1.2 bouyer {
559 1.2 bouyer
560 1.2 bouyer /*
561 1.2 bouyer * On a P6 or above, enable global TLB caching if the
562 1.2 bouyer * hardware supports it.
563 1.2 bouyer */
564 1.43 jym if (cpu_feature[0] & CPUID_PGE)
565 1.2 bouyer lcr4(rcr4() | CR4_PGE); /* enable global TLB caching */
566 1.2 bouyer
567 1.2 bouyer #ifdef XXXMTRR
568 1.2 bouyer /*
569 1.2 bouyer * On a P6 or above, initialize MTRR's if the hardware supports them.
570 1.2 bouyer */
571 1.43 jym if (cpu_feature[0] & CPUID_MTRR) {
572 1.2 bouyer if ((ci->ci_flags & CPUF_AP) == 0)
573 1.2 bouyer i686_mtrr_init_first();
574 1.2 bouyer mtrr_init_cpu(ci);
575 1.2 bouyer }
576 1.2 bouyer #endif
577 1.2 bouyer /*
578 1.2 bouyer * If we have FXSAVE/FXRESTOR, use them.
579 1.2 bouyer */
580 1.43 jym if (cpu_feature[0] & CPUID_FXSR) {
581 1.2 bouyer lcr4(rcr4() | CR4_OSFXSR);
582 1.2 bouyer
583 1.2 bouyer /*
584 1.2 bouyer * If we have SSE/SSE2, enable XMM exceptions.
585 1.2 bouyer */
586 1.43 jym if (cpu_feature[0] & (CPUID_SSE|CPUID_SSE2))
587 1.2 bouyer lcr4(rcr4() | CR4_OSXMMEXCPT);
588 1.2 bouyer }
589 1.2 bouyer
590 1.47 jym #ifdef __x86_64__
591 1.47 jym /* No user PGD mapped for this CPU yet */
592 1.47 jym ci->ci_xen_current_user_pgd = 0;
593 1.47 jym #endif
594 1.47 jym
595 1.34 cegger atomic_or_32(&cpus_running, ci->ci_cpumask);
596 1.11 cegger atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
597 1.2 bouyer }
598 1.2 bouyer
599 1.2 bouyer
600 1.2 bouyer #ifdef MULTIPROCESSOR
601 1.2 bouyer void
602 1.10 cegger cpu_boot_secondary_processors(void)
603 1.2 bouyer {
604 1.2 bouyer struct cpu_info *ci;
605 1.2 bouyer u_long i;
606 1.2 bouyer
607 1.38 cegger for (i = 0; i < maxcpus; i++) {
608 1.38 cegger ci = cpu_lookup(i);
609 1.2 bouyer if (ci == NULL)
610 1.2 bouyer continue;
611 1.2 bouyer if (ci->ci_data.cpu_idlelwp == NULL)
612 1.2 bouyer continue;
613 1.2 bouyer if ((ci->ci_flags & CPUF_PRESENT) == 0)
614 1.2 bouyer continue;
615 1.2 bouyer if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
616 1.2 bouyer continue;
617 1.2 bouyer cpu_boot_secondary(ci);
618 1.2 bouyer }
619 1.11 cegger
620 1.11 cegger x86_mp_online = true;
621 1.2 bouyer }
622 1.2 bouyer
623 1.2 bouyer static void
624 1.2 bouyer cpu_init_idle_lwp(struct cpu_info *ci)
625 1.2 bouyer {
626 1.2 bouyer struct lwp *l = ci->ci_data.cpu_idlelwp;
627 1.37 rmind struct pcb *pcb = lwp_getpcb(l);
628 1.2 bouyer
629 1.2 bouyer pcb->pcb_cr0 = rcr0();
630 1.2 bouyer }
631 1.2 bouyer
632 1.2 bouyer void
633 1.10 cegger cpu_init_idle_lwps(void)
634 1.2 bouyer {
635 1.2 bouyer struct cpu_info *ci;
636 1.2 bouyer u_long i;
637 1.2 bouyer
638 1.38 cegger for (i = 0; i < maxcpus; i++) {
639 1.38 cegger ci = cpu_lookup(i);
640 1.2 bouyer if (ci == NULL)
641 1.2 bouyer continue;
642 1.2 bouyer if (ci->ci_data.cpu_idlelwp == NULL)
643 1.2 bouyer continue;
644 1.2 bouyer if ((ci->ci_flags & CPUF_PRESENT) == 0)
645 1.2 bouyer continue;
646 1.2 bouyer cpu_init_idle_lwp(ci);
647 1.2 bouyer }
648 1.2 bouyer }
649 1.2 bouyer
650 1.2 bouyer void
651 1.10 cegger cpu_start_secondary(struct cpu_info *ci)
652 1.2 bouyer {
653 1.2 bouyer int i;
654 1.2 bouyer struct pmap *kpm = pmap_kernel();
655 1.11 cegger extern uint32_t mp_pdirpa;
656 1.2 bouyer
657 1.2 bouyer mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */
658 1.2 bouyer
659 1.11 cegger atomic_or_32(&ci->ci_flags, CPUF_AP);
660 1.2 bouyer
661 1.11 cegger aprint_debug_dev(ci->ci_dev, "starting\n");
662 1.2 bouyer
663 1.2 bouyer ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
664 1.11 cegger if (CPU_STARTUP(ci, mp_trampoline_paddr) != 0)
665 1.11 cegger return;
666 1.2 bouyer
667 1.2 bouyer /*
668 1.2 bouyer * wait for it to become ready
669 1.2 bouyer */
670 1.11 cegger for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i > 0; i--) {
671 1.11 cegger #ifdef MPDEBUG
672 1.11 cegger extern int cpu_trace[3];
673 1.11 cegger static int otrace[3];
674 1.11 cegger if (memcmp(otrace, cpu_trace, sizeof(otrace)) != 0) {
675 1.11 cegger aprint_debug_dev(ci->ci_dev, "trace %02x %02x %02x\n",
676 1.11 cegger cpu_trace[0], cpu_trace[1], cpu_trace[2]);
677 1.11 cegger memcpy(otrace, cpu_trace, sizeof(otrace));
678 1.11 cegger }
679 1.11 cegger #endif
680 1.2 bouyer delay(10);
681 1.2 bouyer }
682 1.11 cegger if ((ci->ci_flags & CPUF_PRESENT) == 0) {
683 1.9 cegger aprint_error_dev(ci->ci_dev, "failed to become ready\n");
684 1.2 bouyer #if defined(MPDEBUG) && defined(DDB)
685 1.2 bouyer printf("dropping into debugger; continue from here to resume boot\n");
686 1.2 bouyer Debugger();
687 1.2 bouyer #endif
688 1.2 bouyer }
689 1.2 bouyer
690 1.2 bouyer CPU_START_CLEANUP(ci);
691 1.2 bouyer }
692 1.2 bouyer
693 1.2 bouyer void
694 1.10 cegger cpu_boot_secondary(struct cpu_info *ci)
695 1.2 bouyer {
696 1.2 bouyer int i;
697 1.2 bouyer
698 1.11 cegger atomic_or_32(&ci->ci_flags, CPUF_GO);
699 1.11 cegger for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i > 0; i--) {
700 1.2 bouyer delay(10);
701 1.2 bouyer }
702 1.11 cegger if ((ci->ci_flags & CPUF_RUNNING) == 0) {
703 1.11 cegger aprint_error_dev(ci->ci_dev, "CPU failed to start\n");
704 1.2 bouyer #if defined(MPDEBUG) && defined(DDB)
705 1.2 bouyer printf("dropping into debugger; continue from here to resume boot\n");
706 1.2 bouyer Debugger();
707 1.2 bouyer #endif
708 1.2 bouyer }
709 1.2 bouyer }
710 1.2 bouyer
711 1.2 bouyer /*
712 1.2 bouyer * The CPU ends up here when its ready to run
713 1.2 bouyer * This is called from code in mptramp.s; at this point, we are running
714 1.2 bouyer * in the idle pcb/idle stack of the new CPU. When this function returns,
715 1.2 bouyer * this processor will enter the idle loop and start looking for work.
716 1.2 bouyer *
717 1.2 bouyer * XXX should share some of this with init386 in machdep.c
718 1.2 bouyer */
719 1.2 bouyer void
720 1.2 bouyer cpu_hatch(void *v)
721 1.2 bouyer {
722 1.2 bouyer struct cpu_info *ci = (struct cpu_info *)v;
723 1.37 rmind struct pcb *pcb;
724 1.11 cegger int s, i;
725 1.11 cegger
726 1.21 ad cpu_probe(ci);
727 1.11 cegger
728 1.43 jym cpu_feature[0] &= ~CPUID_FEAT_BLACKLIST;
729 1.43 jym cpu_feature[2] &= ~CPUID_FEAT_EXT_BLACKLIST;
730 1.2 bouyer
731 1.43 jym cpu_init_msrs(ci, true);
732 1.2 bouyer
733 1.11 cegger KDASSERT((ci->ci_flags & CPUF_PRESENT) == 0);
734 1.11 cegger atomic_or_32(&ci->ci_flags, CPUF_PRESENT);
735 1.11 cegger while ((ci->ci_flags & CPUF_GO) == 0) {
736 1.11 cegger /* Don't use delay, boot CPU may be patching the text. */
737 1.11 cegger for (i = 10000; i != 0; i--)
738 1.11 cegger x86_pause();
739 1.11 cegger }
740 1.2 bouyer
741 1.11 cegger /* Because the text may have been patched in x86_patch(). */
742 1.11 cegger wbinvd();
743 1.11 cegger x86_flush();
744 1.2 bouyer
745 1.11 cegger KASSERT((ci->ci_flags & CPUF_RUNNING) == 0);
746 1.2 bouyer
747 1.37 rmind pcb = lwp_getpcb(curlwp);
748 1.12 cegger lcr3(pmap_kernel()->pm_pdirpa);
749 1.37 rmind pcb->pcb_cr3 = pmap_kernel()->pm_pdirpa;
750 1.37 rmind pcb = lwp_getpcb(ci->ci_data.cpu_idlelwp);
751 1.37 rmind lcr0(pcb->pcb_cr0);
752 1.37 rmind
753 1.2 bouyer cpu_init_idt();
754 1.11 cegger gdt_init_cpu(ci);
755 1.11 cegger lapic_enable();
756 1.2 bouyer lapic_set_lvt();
757 1.11 cegger lapic_initclocks();
758 1.11 cegger
759 1.12 cegger #ifdef i386
760 1.2 bouyer npxinit(ci);
761 1.12 cegger #else
762 1.12 cegger fpuinit(ci);
763 1.12 cegger #endif
764 1.2 bouyer
765 1.2 bouyer lldt(GSEL(GLDT_SEL, SEL_KPL));
766 1.12 cegger ltr(ci->ci_tss_sel);
767 1.2 bouyer
768 1.2 bouyer cpu_init(ci);
769 1.11 cegger cpu_get_tsc_freq(ci);
770 1.2 bouyer
771 1.2 bouyer s = splhigh();
772 1.11 cegger #ifdef i386
773 1.2 bouyer lapic_tpr = 0;
774 1.11 cegger #else
775 1.11 cegger lcr8(0);
776 1.11 cegger #endif
777 1.11 cegger x86_enable_intr();
778 1.11 cegger splx(s);
779 1.12 cegger #if 0
780 1.11 cegger x86_errata();
781 1.11 cegger #endif
782 1.2 bouyer
783 1.11 cegger aprint_debug_dev(ci->ci_dev, "CPU %ld running\n",
784 1.11 cegger (long)ci->ci_cpuid);
785 1.2 bouyer }
786 1.2 bouyer
787 1.2 bouyer #if defined(DDB)
788 1.2 bouyer
789 1.2 bouyer #include <ddb/db_output.h>
790 1.2 bouyer #include <machine/db_machdep.h>
791 1.2 bouyer
792 1.2 bouyer /*
793 1.2 bouyer * Dump CPU information from ddb.
794 1.2 bouyer */
795 1.2 bouyer void
796 1.2 bouyer cpu_debug_dump(void)
797 1.2 bouyer {
798 1.2 bouyer struct cpu_info *ci;
799 1.2 bouyer CPU_INFO_ITERATOR cii;
800 1.2 bouyer
801 1.13 yamt db_printf("addr dev id flags ipis curlwp fpcurlwp\n");
802 1.2 bouyer for (CPU_INFO_FOREACH(cii, ci)) {
803 1.2 bouyer db_printf("%p %s %ld %x %x %10p %10p\n",
804 1.2 bouyer ci,
805 1.9 cegger ci->ci_dev == NULL ? "BOOT" : device_xname(ci->ci_dev),
806 1.12 cegger (long)ci->ci_cpuid,
807 1.2 bouyer ci->ci_flags, ci->ci_ipis,
808 1.2 bouyer ci->ci_curlwp,
809 1.2 bouyer ci->ci_fpcurlwp);
810 1.2 bouyer }
811 1.2 bouyer }
812 1.38 cegger #endif /* DDB */
813 1.2 bouyer
814 1.2 bouyer static void
815 1.10 cegger cpu_copy_trampoline(void)
816 1.2 bouyer {
817 1.2 bouyer /*
818 1.2 bouyer * Copy boot code.
819 1.2 bouyer */
820 1.2 bouyer extern u_char cpu_spinup_trampoline[];
821 1.2 bouyer extern u_char cpu_spinup_trampoline_end[];
822 1.11 cegger
823 1.11 cegger vaddr_t mp_trampoline_vaddr;
824 1.11 cegger
825 1.11 cegger mp_trampoline_vaddr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
826 1.11 cegger UVM_KMF_VAONLY);
827 1.11 cegger
828 1.11 cegger pmap_kenter_pa(mp_trampoline_vaddr, mp_trampoline_paddr,
829 1.36 cegger VM_PROT_READ | VM_PROT_WRITE, 0);
830 1.11 cegger pmap_update(pmap_kernel());
831 1.11 cegger memcpy((void *)mp_trampoline_vaddr,
832 1.11 cegger cpu_spinup_trampoline,
833 1.11 cegger cpu_spinup_trampoline_end - cpu_spinup_trampoline);
834 1.11 cegger
835 1.11 cegger pmap_kremove(mp_trampoline_vaddr, PAGE_SIZE);
836 1.11 cegger pmap_update(pmap_kernel());
837 1.11 cegger uvm_km_free(kernel_map, mp_trampoline_vaddr, PAGE_SIZE, UVM_KMF_VAONLY);
838 1.2 bouyer }
839 1.2 bouyer
840 1.38 cegger #endif /* MULTIPROCESSOR */
841 1.2 bouyer
842 1.11 cegger #ifdef i386
843 1.11 cegger #if 0
844 1.11 cegger static void
845 1.11 cegger tss_init(struct i386tss *tss, void *stack, void *func)
846 1.11 cegger {
847 1.11 cegger memset(tss, 0, sizeof *tss);
848 1.11 cegger tss->tss_esp0 = tss->tss_esp = (int)((char *)stack + USPACE - 16);
849 1.11 cegger tss->tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
850 1.11 cegger tss->__tss_cs = GSEL(GCODE_SEL, SEL_KPL);
851 1.11 cegger tss->tss_fs = GSEL(GCPU_SEL, SEL_KPL);
852 1.11 cegger tss->tss_gs = tss->__tss_es = tss->__tss_ds =
853 1.11 cegger tss->__tss_ss = GSEL(GDATA_SEL, SEL_KPL);
854 1.11 cegger tss->tss_cr3 = pmap_kernel()->pm_pdirpa;
855 1.11 cegger tss->tss_esp = (int)((char *)stack + USPACE - 16);
856 1.11 cegger tss->tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
857 1.11 cegger tss->__tss_eflags = PSL_MBO | PSL_NT; /* XXX not needed? */
858 1.11 cegger tss->__tss_eip = (int)func;
859 1.11 cegger }
860 1.11 cegger #endif
861 1.2 bouyer
862 1.2 bouyer /* XXX */
863 1.2 bouyer #define IDTVEC(name) __CONCAT(X, name)
864 1.2 bouyer typedef void (vector)(void);
865 1.2 bouyer extern vector IDTVEC(tss_trap08);
866 1.2 bouyer #ifdef DDB
867 1.2 bouyer extern vector Xintrddbipi;
868 1.2 bouyer extern int ddb_vec;
869 1.2 bouyer #endif
870 1.2 bouyer
871 1.2 bouyer static void
872 1.2 bouyer cpu_set_tss_gates(struct cpu_info *ci)
873 1.2 bouyer {
874 1.11 cegger #if 0
875 1.11 cegger struct segment_descriptor sd;
876 1.11 cegger
877 1.11 cegger ci->ci_doubleflt_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
878 1.11 cegger UVM_KMF_WIRED);
879 1.11 cegger tss_init(&ci->ci_doubleflt_tss, ci->ci_doubleflt_stack,
880 1.11 cegger IDTVEC(tss_trap08));
881 1.11 cegger setsegment(&sd, &ci->ci_doubleflt_tss, sizeof(struct i386tss) - 1,
882 1.11 cegger SDT_SYS386TSS, SEL_KPL, 0, 0);
883 1.11 cegger ci->ci_gdt[GTRAPTSS_SEL].sd = sd;
884 1.11 cegger setgate(&idt[8], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
885 1.11 cegger GSEL(GTRAPTSS_SEL, SEL_KPL));
886 1.11 cegger #endif
887 1.11 cegger
888 1.2 bouyer #if defined(DDB) && defined(MULTIPROCESSOR)
889 1.2 bouyer /*
890 1.2 bouyer * Set up separate handler for the DDB IPI, so that it doesn't
891 1.2 bouyer * stomp on a possibly corrupted stack.
892 1.2 bouyer *
893 1.2 bouyer * XXX overwriting the gate set in db_machine_init.
894 1.2 bouyer * Should rearrange the code so that it's set only once.
895 1.2 bouyer */
896 1.2 bouyer ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
897 1.2 bouyer UVM_KMF_WIRED);
898 1.6 yamt tss_init(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
899 1.2 bouyer Xintrddbipi);
900 1.2 bouyer
901 1.2 bouyer setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
902 1.2 bouyer SDT_SYS386TSS, SEL_KPL, 0, 0);
903 1.2 bouyer ci->ci_gdt[GIPITSS_SEL].sd = sd;
904 1.2 bouyer
905 1.2 bouyer setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
906 1.2 bouyer GSEL(GIPITSS_SEL, SEL_KPL));
907 1.2 bouyer #endif
908 1.2 bouyer }
909 1.11 cegger #else
910 1.11 cegger static void
911 1.11 cegger cpu_set_tss_gates(struct cpu_info *ci)
912 1.11 cegger {
913 1.11 cegger
914 1.11 cegger }
915 1.11 cegger #endif /* i386 */
916 1.2 bouyer
917 1.2 bouyer int
918 1.5 joerg mp_cpu_start(struct cpu_info *ci, paddr_t target)
919 1.2 bouyer {
920 1.2 bouyer #if 0
921 1.2 bouyer #if NLAPIC > 0
922 1.2 bouyer int error;
923 1.2 bouyer #endif
924 1.2 bouyer unsigned short dwordptr[2];
925 1.2 bouyer
926 1.2 bouyer /*
927 1.11 cegger * Bootstrap code must be addressable in real mode
928 1.11 cegger * and it must be page aligned.
929 1.11 cegger */
930 1.11 cegger KASSERT(target < 0x10000 && target % PAGE_SIZE == 0);
931 1.11 cegger
932 1.11 cegger /*
933 1.2 bouyer * "The BSP must initialize CMOS shutdown code to 0Ah ..."
934 1.2 bouyer */
935 1.2 bouyer
936 1.2 bouyer outb(IO_RTC, NVRAM_RESET);
937 1.2 bouyer outb(IO_RTC+1, NVRAM_RESET_JUMP);
938 1.2 bouyer
939 1.2 bouyer /*
940 1.2 bouyer * "and the warm reset vector (DWORD based at 40:67) to point
941 1.2 bouyer * to the AP startup code ..."
942 1.2 bouyer */
943 1.2 bouyer
944 1.2 bouyer dwordptr[0] = 0;
945 1.5 joerg dwordptr[1] = target >> 4;
946 1.2 bouyer
947 1.36 cegger pmap_kenter_pa (0, 0, VM_PROT_READ|VM_PROT_WRITE, 0);
948 1.45 rmind pmap_update(pmap_kernel());
949 1.45 rmind
950 1.11 cegger memcpy ((uint8_t *) 0x467, dwordptr, 4);
951 1.45 rmind
952 1.2 bouyer pmap_kremove (0, PAGE_SIZE);
953 1.45 rmind pmap_update(pmap_kernel());
954 1.2 bouyer
955 1.2 bouyer #if NLAPIC > 0
956 1.2 bouyer /*
957 1.2 bouyer * ... prior to executing the following sequence:"
958 1.2 bouyer */
959 1.2 bouyer
960 1.2 bouyer if (ci->ci_flags & CPUF_AP) {
961 1.23 ad if ((error = x86_ipi_init(ci->ci_cpuid)) != 0)
962 1.2 bouyer return error;
963 1.2 bouyer
964 1.2 bouyer delay(10000);
965 1.2 bouyer
966 1.2 bouyer if (cpu_feature & CPUID_APIC) {
967 1.23 ad error = x86_ipi_init(ci->ci_cpuid);
968 1.11 cegger if (error != 0) {
969 1.11 cegger aprint_error_dev(ci->ci_dev, "%s: IPI not taken (1)\n",
970 1.11 cegger __func__);
971 1.11 cegger return error;
972 1.11 cegger }
973 1.11 cegger
974 1.11 cegger delay(10000);
975 1.2 bouyer
976 1.23 ad error = x86_ipi(target / PAGE_SIZE, ci->ci_cpuid,
977 1.11 cegger LAPIC_DLMODE_STARTUP);
978 1.11 cegger if (error != 0) {
979 1.11 cegger aprint_error_dev(ci->ci_dev, "%s: IPI not taken (2)\n",
980 1.11 cegger __func__);
981 1.2 bouyer return error;
982 1.11 cegger }
983 1.2 bouyer delay(200);
984 1.2 bouyer
985 1.23 ad error = x86_ipi(target / PAGE_SIZE, ci->ci_cpuid,
986 1.11 cegger LAPIC_DLMODE_STARTUP);
987 1.11 cegger if (error != 0) {
988 1.11 cegger aprint_error_dev(ci->ci_dev, "%s: IPI not taken ((3)\n",
989 1.11 cegger __func__);
990 1.2 bouyer return error;
991 1.11 cegger }
992 1.2 bouyer delay(200);
993 1.2 bouyer }
994 1.2 bouyer }
995 1.2 bouyer #endif
996 1.2 bouyer #endif /* 0 */
997 1.2 bouyer return 0;
998 1.2 bouyer }
999 1.2 bouyer
1000 1.2 bouyer void
1001 1.2 bouyer mp_cpu_start_cleanup(struct cpu_info *ci)
1002 1.2 bouyer {
1003 1.2 bouyer #if 0
1004 1.2 bouyer /*
1005 1.2 bouyer * Ensure the NVRAM reset byte contains something vaguely sane.
1006 1.2 bouyer */
1007 1.2 bouyer
1008 1.2 bouyer outb(IO_RTC, NVRAM_RESET);
1009 1.2 bouyer outb(IO_RTC+1, NVRAM_RESET_RST);
1010 1.2 bouyer #endif
1011 1.2 bouyer }
1012 1.2 bouyer
1013 1.2 bouyer void
1014 1.3 bouyer cpu_init_msrs(struct cpu_info *ci, bool full)
1015 1.2 bouyer {
1016 1.43 jym #ifdef __x86_64__
1017 1.3 bouyer if (full) {
1018 1.3 bouyer HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
1019 1.11 cegger HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (uint64_t) ci);
1020 1.3 bouyer HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
1021 1.3 bouyer }
1022 1.43 jym #endif /* __x86_64__ */
1023 1.44 jym
1024 1.44 jym if (cpu_feature[2] & CPUID_NOX)
1025 1.44 jym wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE);
1026 1.2 bouyer }
1027 1.2 bouyer
1028 1.11 cegger void
1029 1.11 cegger cpu_offline_md(void)
1030 1.11 cegger {
1031 1.11 cegger int s;
1032 1.11 cegger
1033 1.11 cegger s = splhigh();
1034 1.11 cegger #ifdef __i386__
1035 1.11 cegger npxsave_cpu(true);
1036 1.11 cegger #else
1037 1.11 cegger fpusave_cpu(true);
1038 1.11 cegger #endif
1039 1.11 cegger splx(s);
1040 1.11 cegger }
1041 1.11 cegger
1042 1.11 cegger #if 0
1043 1.11 cegger /* XXX joerg restructure and restart CPUs individually */
1044 1.11 cegger static bool
1045 1.41 dyoung cpu_suspend(device_t dv, const pmf_qual_t *qual)
1046 1.11 cegger {
1047 1.11 cegger struct cpu_softc *sc = device_private(dv);
1048 1.11 cegger struct cpu_info *ci = sc->sc_info;
1049 1.11 cegger int err;
1050 1.11 cegger
1051 1.11 cegger if (ci->ci_flags & CPUF_PRIMARY)
1052 1.11 cegger return true;
1053 1.11 cegger if (ci->ci_data.cpu_idlelwp == NULL)
1054 1.11 cegger return true;
1055 1.11 cegger if ((ci->ci_flags & CPUF_PRESENT) == 0)
1056 1.11 cegger return true;
1057 1.11 cegger
1058 1.11 cegger sc->sc_wasonline = !(ci->ci_schedstate.spc_flags & SPCF_OFFLINE);
1059 1.11 cegger
1060 1.11 cegger if (sc->sc_wasonline) {
1061 1.11 cegger mutex_enter(&cpu_lock);
1062 1.29 rmind err = cpu_setstate(ci, false);
1063 1.11 cegger mutex_exit(&cpu_lock);
1064 1.11 cegger
1065 1.11 cegger if (err)
1066 1.11 cegger return false;
1067 1.11 cegger }
1068 1.11 cegger
1069 1.11 cegger return true;
1070 1.11 cegger }
1071 1.11 cegger
1072 1.11 cegger static bool
1073 1.41 dyoung cpu_resume(device_t dv, const pmf_qual_t *qual)
1074 1.11 cegger {
1075 1.11 cegger struct cpu_softc *sc = device_private(dv);
1076 1.11 cegger struct cpu_info *ci = sc->sc_info;
1077 1.11 cegger int err = 0;
1078 1.11 cegger
1079 1.11 cegger if (ci->ci_flags & CPUF_PRIMARY)
1080 1.11 cegger return true;
1081 1.11 cegger if (ci->ci_data.cpu_idlelwp == NULL)
1082 1.11 cegger return true;
1083 1.11 cegger if ((ci->ci_flags & CPUF_PRESENT) == 0)
1084 1.11 cegger return true;
1085 1.11 cegger
1086 1.11 cegger if (sc->sc_wasonline) {
1087 1.11 cegger mutex_enter(&cpu_lock);
1088 1.29 rmind err = cpu_setstate(ci, true);
1089 1.11 cegger mutex_exit(&cpu_lock);
1090 1.11 cegger }
1091 1.11 cegger
1092 1.11 cegger return err == 0;
1093 1.11 cegger }
1094 1.11 cegger #endif
1095 1.11 cegger
1096 1.2 bouyer void
1097 1.2 bouyer cpu_get_tsc_freq(struct cpu_info *ci)
1098 1.2 bouyer {
1099 1.16 cegger const volatile vcpu_time_info_t *tinfo = &ci->ci_vcpu->time;
1100 1.2 bouyer delay(1000000);
1101 1.2 bouyer uint64_t freq = 1000000000ULL << 32;
1102 1.2 bouyer freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
1103 1.2 bouyer if ( tinfo->tsc_shift < 0 )
1104 1.2 bouyer freq = freq << -tinfo->tsc_shift;
1105 1.2 bouyer else
1106 1.2 bouyer freq = freq >> tinfo->tsc_shift;
1107 1.20 ad ci->ci_data.cpu_cc_freq = freq;
1108 1.2 bouyer }
1109 1.19 joerg
1110 1.19 joerg void
1111 1.19 joerg x86_cpu_idle_xen(void)
1112 1.19 joerg {
1113 1.19 joerg struct cpu_info *ci = curcpu();
1114 1.19 joerg
1115 1.19 joerg KASSERT(ci->ci_ilevel == IPL_NONE);
1116 1.19 joerg
1117 1.19 joerg x86_disable_intr();
1118 1.19 joerg if (!__predict_false(ci->ci_want_resched)) {
1119 1.19 joerg idle_block();
1120 1.19 joerg } else {
1121 1.19 joerg x86_enable_intr();
1122 1.19 joerg }
1123 1.19 joerg }
1124 1.47 jym
1125 1.47 jym /*
1126 1.47 jym * Loads pmap for the current CPU.
1127 1.47 jym */
1128 1.47 jym void
1129 1.47 jym cpu_load_pmap(struct pmap *pmap)
1130 1.47 jym {
1131 1.47 jym #ifdef i386
1132 1.47 jym #ifdef PAE
1133 1.47 jym int i, s;
1134 1.47 jym struct cpu_info *ci;
1135 1.47 jym
1136 1.47 jym s = splvm(); /* just to be safe */
1137 1.47 jym ci = curcpu();
1138 1.47 jym paddr_t l3_pd = xpmap_ptom_masked(ci->ci_pae_l3_pdirpa);
1139 1.47 jym /* don't update the kernel L3 slot */
1140 1.47 jym for (i = 0 ; i < PDP_SIZE - 1; i++) {
1141 1.47 jym xpq_queue_pte_update(l3_pd + i * sizeof(pd_entry_t),
1142 1.47 jym xpmap_ptom(pmap->pm_pdirpa[i]) | PG_V);
1143 1.47 jym }
1144 1.47 jym splx(s);
1145 1.47 jym tlbflush();
1146 1.47 jym #else /* PAE */
1147 1.47 jym lcr3(pmap_pdirpa(pmap, 0));
1148 1.47 jym #endif /* PAE */
1149 1.47 jym #endif /* i386 */
1150 1.47 jym
1151 1.47 jym #ifdef __x86_64__
1152 1.47 jym int i, s;
1153 1.47 jym pd_entry_t *old_pgd, *new_pgd;
1154 1.47 jym paddr_t addr;
1155 1.47 jym struct cpu_info *ci;
1156 1.47 jym
1157 1.47 jym /* kernel pmap always in cr3 and should never go in user cr3 */
1158 1.47 jym if (pmap_pdirpa(pmap, 0) != pmap_pdirpa(pmap_kernel(), 0)) {
1159 1.47 jym ci = curcpu();
1160 1.47 jym /*
1161 1.47 jym * Map user space address in kernel space and load
1162 1.47 jym * user cr3
1163 1.47 jym */
1164 1.47 jym s = splvm();
1165 1.47 jym new_pgd = pmap->pm_pdir;
1166 1.47 jym old_pgd = pmap_kernel()->pm_pdir;
1167 1.47 jym addr = xpmap_ptom(pmap_pdirpa(pmap_kernel(), 0));
1168 1.47 jym for (i = 0; i < PDIR_SLOT_PTE;
1169 1.47 jym i++, addr += sizeof(pd_entry_t)) {
1170 1.47 jym if ((new_pgd[i] & PG_V) || (old_pgd[i] & PG_V))
1171 1.47 jym xpq_queue_pte_update(addr, new_pgd[i]);
1172 1.47 jym }
1173 1.47 jym tlbflush();
1174 1.47 jym xen_set_user_pgd(pmap_pdirpa(pmap, 0));
1175 1.47 jym ci->ci_xen_current_user_pgd = pmap_pdirpa(pmap, 0);
1176 1.47 jym splx(s);
1177 1.47 jym }
1178 1.47 jym #endif /* __x86_64__ */
1179 1.47 jym }
1180