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