cpu.c revision 1.3.2.4 1 1.3.2.1 bouyer /* $NetBSD: cpu.c,v 1.3.2.4 2008/01/08 22:10:41 bouyer 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.3.2.1 bouyer __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.3.2.4 2008/01/08 22:10:41 bouyer 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.2 bouyer
93 1.2 bouyer #include <uvm/uvm_extern.h>
94 1.2 bouyer
95 1.2 bouyer #include <machine/cpu.h>
96 1.2 bouyer #include <machine/cpufunc.h>
97 1.2 bouyer #include <machine/cpuvar.h>
98 1.2 bouyer #include <machine/pmap.h>
99 1.2 bouyer #include <machine/vmparam.h>
100 1.2 bouyer #include <machine/mpbiosvar.h>
101 1.2 bouyer #include <machine/pcb.h>
102 1.2 bouyer #include <machine/specialreg.h>
103 1.2 bouyer #include <machine/segments.h>
104 1.2 bouyer #include <machine/gdt.h>
105 1.2 bouyer #include <machine/mtrr.h>
106 1.2 bouyer #include <machine/pio.h>
107 1.2 bouyer
108 1.2 bouyer #ifdef XEN3
109 1.2 bouyer #include <xen/vcpuvar.h>
110 1.2 bouyer #endif
111 1.2 bouyer
112 1.2 bouyer #if NLAPIC > 0
113 1.2 bouyer #include <machine/apicvar.h>
114 1.2 bouyer #include <machine/i82489reg.h>
115 1.2 bouyer #include <machine/i82489var.h>
116 1.2 bouyer #endif
117 1.2 bouyer
118 1.2 bouyer #if NIOAPIC > 0
119 1.2 bouyer #include <machine/i82093var.h>
120 1.2 bouyer #endif
121 1.2 bouyer
122 1.2 bouyer #include <dev/ic/mc146818reg.h>
123 1.2 bouyer #include <dev/isa/isareg.h>
124 1.2 bouyer
125 1.2 bouyer int cpu_match(struct device *, struct cfdata *, void *);
126 1.2 bouyer void cpu_attach(struct device *, struct device *, void *);
127 1.2 bouyer #ifdef XEN3
128 1.2 bouyer int vcpu_match(struct device *, struct cfdata *, void *);
129 1.2 bouyer void vcpu_attach(struct device *, struct device *, void *);
130 1.2 bouyer #endif
131 1.2 bouyer void cpu_attach_common(struct device *, struct device *, void *);
132 1.2 bouyer
133 1.2 bouyer struct cpu_softc {
134 1.2 bouyer struct device sc_dev; /* device tree glue */
135 1.2 bouyer struct cpu_info *sc_info; /* pointer to CPU info */
136 1.2 bouyer };
137 1.2 bouyer
138 1.3.2.2 bouyer int mp_cpu_start(struct cpu_info *, paddr_t);
139 1.2 bouyer void mp_cpu_start_cleanup(struct cpu_info *);
140 1.2 bouyer const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
141 1.2 bouyer mp_cpu_start_cleanup };
142 1.2 bouyer
143 1.2 bouyer CFATTACH_DECL(cpu, sizeof(struct cpu_softc),
144 1.2 bouyer cpu_match, cpu_attach, NULL, NULL);
145 1.2 bouyer #ifdef XEN3
146 1.2 bouyer CFATTACH_DECL(vcpu, sizeof(struct cpu_softc),
147 1.2 bouyer vcpu_match, vcpu_attach, NULL, NULL);
148 1.2 bouyer #endif
149 1.2 bouyer
150 1.2 bouyer /*
151 1.2 bouyer * Statically-allocated CPU info for the primary CPU (or the only
152 1.2 bouyer * CPU, on uniprocessors). The CPU info list is initialized to
153 1.2 bouyer * point at it.
154 1.2 bouyer */
155 1.2 bouyer #ifdef TRAPLOG
156 1.2 bouyer #include <machine/tlog.h>
157 1.2 bouyer struct tlog tlog_primary;
158 1.2 bouyer #endif
159 1.2 bouyer struct cpu_info cpu_info_primary = {
160 1.3.2.3 bouyer .ci_dev = 0,
161 1.2 bouyer .ci_self = &cpu_info_primary,
162 1.3.2.1 bouyer .ci_idepth = -1,
163 1.2 bouyer .ci_curlwp = &lwp0,
164 1.2 bouyer #ifdef TRAPLOG
165 1.2 bouyer .ci_tlog = &tlog_primary,
166 1.2 bouyer #endif
167 1.2 bouyer
168 1.2 bouyer };
169 1.2 bouyer struct cpu_info phycpu_info_primary = {
170 1.3.2.3 bouyer .ci_dev = 0,
171 1.2 bouyer .ci_self = &phycpu_info_primary,
172 1.2 bouyer };
173 1.2 bouyer
174 1.2 bouyer struct cpu_info *cpu_info_list = &cpu_info_primary;
175 1.2 bouyer
176 1.2 bouyer static void cpu_set_tss_gates(struct cpu_info *ci);
177 1.2 bouyer
178 1.2 bouyer u_int32_t cpus_attached = 0;
179 1.2 bouyer
180 1.2 bouyer struct cpu_info *phycpu_info[X86_MAXPROCS] = { &cpu_info_primary };
181 1.2 bouyer
182 1.2 bouyer #ifdef MULTIPROCESSOR
183 1.2 bouyer /*
184 1.2 bouyer * Array of CPU info structures. Must be statically-allocated because
185 1.2 bouyer * curproc, etc. are used early.
186 1.2 bouyer */
187 1.2 bouyer struct cpu_info *cpu_info[X86_MAXPROCS] = { &cpu_info_primary };
188 1.2 bouyer
189 1.2 bouyer u_int32_t cpus_running = 0;
190 1.2 bouyer
191 1.2 bouyer void cpu_hatch(void *);
192 1.2 bouyer static void cpu_boot_secondary(struct cpu_info *ci);
193 1.2 bouyer static void cpu_start_secondary(struct cpu_info *ci);
194 1.2 bouyer static void cpu_copy_trampoline(void);
195 1.2 bouyer
196 1.2 bouyer /*
197 1.2 bouyer * Runs once per boot once multiprocessor goo has been detected and
198 1.2 bouyer * the local APIC on the boot processor has been mapped.
199 1.2 bouyer *
200 1.2 bouyer * Called from lapic_boot_init() (from mpbios_scan()).
201 1.2 bouyer */
202 1.2 bouyer void
203 1.2 bouyer cpu_init_first()
204 1.2 bouyer {
205 1.2 bouyer int cpunum = lapic_cpu_number();
206 1.2 bouyer
207 1.2 bouyer if (cpunum != 0) {
208 1.2 bouyer cpu_info[0] = NULL;
209 1.2 bouyer cpu_info[cpunum] = &cpu_info_primary;
210 1.2 bouyer }
211 1.2 bouyer
212 1.2 bouyer cpu_copy_trampoline();
213 1.2 bouyer }
214 1.2 bouyer #endif
215 1.2 bouyer
216 1.2 bouyer int
217 1.2 bouyer cpu_match(parent, match, aux)
218 1.2 bouyer struct device *parent;
219 1.2 bouyer struct cfdata *match;
220 1.2 bouyer void *aux;
221 1.2 bouyer {
222 1.2 bouyer
223 1.2 bouyer return 1;
224 1.2 bouyer }
225 1.2 bouyer
226 1.2 bouyer void
227 1.2 bouyer cpu_attach(parent, self, aux)
228 1.2 bouyer struct device *parent, *self;
229 1.2 bouyer void *aux;
230 1.2 bouyer {
231 1.2 bouyer #ifdef XEN3
232 1.2 bouyer struct cpu_softc *sc = (void *) self;
233 1.2 bouyer struct cpu_attach_args *caa = aux;
234 1.2 bouyer struct cpu_info *ci;
235 1.2 bouyer int cpunum = caa->cpu_number;
236 1.2 bouyer
237 1.2 bouyer /*
238 1.2 bouyer * If we're an Application Processor, allocate a cpu_info
239 1.2 bouyer * structure, otherwise use the primary's.
240 1.2 bouyer */
241 1.2 bouyer if (caa->cpu_role == CPU_ROLE_AP) {
242 1.2 bouyer ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
243 1.2 bouyer if (phycpu_info[cpunum] != NULL)
244 1.2 bouyer panic("cpu at apic id %d already attached?", cpunum);
245 1.2 bouyer phycpu_info[cpunum] = ci;
246 1.2 bouyer } else {
247 1.2 bouyer ci = &phycpu_info_primary;
248 1.2 bouyer if (cpunum != 0) {
249 1.2 bouyer phycpu_info[0] = NULL;
250 1.2 bouyer phycpu_info[cpunum] = ci;
251 1.2 bouyer }
252 1.2 bouyer }
253 1.2 bouyer
254 1.2 bouyer ci->ci_self = ci;
255 1.2 bouyer sc->sc_info = ci;
256 1.2 bouyer
257 1.2 bouyer ci->ci_dev = self;
258 1.2 bouyer ci->ci_apicid = caa->cpu_number;
259 1.2 bouyer ci->ci_cpuid = ci->ci_apicid;
260 1.2 bouyer
261 1.2 bouyer printf(": ");
262 1.2 bouyer switch (caa->cpu_role) {
263 1.2 bouyer case CPU_ROLE_SP:
264 1.2 bouyer printf("(uniprocessor)\n");
265 1.2 bouyer ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
266 1.2 bouyer break;
267 1.2 bouyer
268 1.2 bouyer case CPU_ROLE_BP:
269 1.2 bouyer printf("(boot processor)\n");
270 1.2 bouyer ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
271 1.2 bouyer #if NIOAPIC > 0
272 1.2 bouyer ioapic_bsp_id = caa->cpu_number;
273 1.2 bouyer #endif
274 1.2 bouyer break;
275 1.2 bouyer
276 1.2 bouyer case CPU_ROLE_AP:
277 1.2 bouyer /*
278 1.2 bouyer * report on an AP
279 1.2 bouyer */
280 1.2 bouyer printf("(application processor)\n");
281 1.2 bouyer break;
282 1.2 bouyer
283 1.2 bouyer default:
284 1.2 bouyer panic("unknown processor type??\n");
285 1.2 bouyer }
286 1.2 bouyer return;
287 1.2 bouyer #else
288 1.2 bouyer cpu_attach_common(parent, self, aux);
289 1.2 bouyer #endif
290 1.2 bouyer }
291 1.2 bouyer
292 1.2 bouyer #ifdef XEN3
293 1.2 bouyer int
294 1.2 bouyer vcpu_match(parent, match, aux)
295 1.2 bouyer struct device *parent;
296 1.2 bouyer struct cfdata *match;
297 1.2 bouyer void *aux;
298 1.2 bouyer {
299 1.2 bouyer struct vcpu_attach_args *vcaa = aux;
300 1.2 bouyer
301 1.2 bouyer if (strcmp(vcaa->vcaa_name, match->cf_name) == 0)
302 1.2 bouyer return 1;
303 1.2 bouyer return 0;
304 1.2 bouyer }
305 1.2 bouyer
306 1.2 bouyer void
307 1.2 bouyer vcpu_attach(parent, self, aux)
308 1.2 bouyer struct device *parent, *self;
309 1.2 bouyer void *aux;
310 1.2 bouyer {
311 1.2 bouyer struct vcpu_attach_args *vcaa = aux;
312 1.2 bouyer
313 1.2 bouyer cpu_attach_common(parent, self, &vcaa->vcaa_caa);
314 1.2 bouyer }
315 1.2 bouyer #endif
316 1.2 bouyer
317 1.2 bouyer static void
318 1.2 bouyer cpu_vm_init(struct cpu_info *ci)
319 1.2 bouyer {
320 1.2 bouyer int ncolors = 2, i;
321 1.2 bouyer
322 1.2 bouyer for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
323 1.2 bouyer struct x86_cache_info *cai;
324 1.2 bouyer int tcolors;
325 1.2 bouyer
326 1.2 bouyer cai = &ci->ci_cinfo[i];
327 1.2 bouyer
328 1.2 bouyer tcolors = atop(cai->cai_totalsize);
329 1.2 bouyer switch(cai->cai_associativity) {
330 1.2 bouyer case 0xff:
331 1.2 bouyer tcolors = 1; /* fully associative */
332 1.2 bouyer break;
333 1.2 bouyer case 0:
334 1.2 bouyer case 1:
335 1.2 bouyer break;
336 1.2 bouyer default:
337 1.2 bouyer tcolors /= cai->cai_associativity;
338 1.2 bouyer }
339 1.2 bouyer ncolors = max(ncolors, tcolors);
340 1.2 bouyer }
341 1.2 bouyer
342 1.2 bouyer /*
343 1.2 bouyer * Knowing the size of the largest cache on this CPU, re-color
344 1.2 bouyer * our pages.
345 1.2 bouyer */
346 1.2 bouyer if (ncolors <= uvmexp.ncolors)
347 1.2 bouyer return;
348 1.2 bouyer printf("%s: %d page colors\n", ci->ci_dev->dv_xname, ncolors);
349 1.2 bouyer uvm_page_recolor(ncolors);
350 1.2 bouyer }
351 1.2 bouyer
352 1.2 bouyer void
353 1.2 bouyer cpu_attach_common(parent, self, aux)
354 1.2 bouyer struct device *parent, *self;
355 1.2 bouyer void *aux;
356 1.2 bouyer {
357 1.2 bouyer struct cpu_softc *sc = (void *) self;
358 1.2 bouyer struct cpu_attach_args *caa = aux;
359 1.2 bouyer struct cpu_info *ci;
360 1.2 bouyer #if defined(MULTIPROCESSOR)
361 1.2 bouyer int cpunum = caa->cpu_number;
362 1.2 bouyer #endif
363 1.2 bouyer
364 1.2 bouyer /*
365 1.2 bouyer * If we're an Application Processor, allocate a cpu_info
366 1.2 bouyer * structure, otherwise use the primary's.
367 1.2 bouyer */
368 1.2 bouyer if (caa->cpu_role == CPU_ROLE_AP) {
369 1.2 bouyer ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
370 1.2 bouyer #if defined(MULTIPROCESSOR)
371 1.2 bouyer if (cpu_info[cpunum] != NULL)
372 1.2 bouyer panic("cpu at apic id %d already attached?", cpunum);
373 1.2 bouyer cpu_info[cpunum] = ci;
374 1.2 bouyer #endif
375 1.2 bouyer #ifdef TRAPLOG
376 1.2 bouyer ci->ci_tlog_base = malloc(sizeof(struct tlog),
377 1.2 bouyer M_DEVBUF, M_WAITOK);
378 1.2 bouyer #endif
379 1.2 bouyer } else {
380 1.2 bouyer ci = &cpu_info_primary;
381 1.2 bouyer #if defined(MULTIPROCESSOR)
382 1.2 bouyer if (cpunum != lapic_cpu_number()) {
383 1.2 bouyer panic("%s: running CPU is at apic %d"
384 1.2 bouyer " instead of at expected %d",
385 1.2 bouyer sc->sc_dev.dv_xname, lapic_cpu_number(), cpunum);
386 1.2 bouyer }
387 1.2 bouyer #endif
388 1.2 bouyer }
389 1.2 bouyer
390 1.2 bouyer ci->ci_self = ci;
391 1.2 bouyer sc->sc_info = ci;
392 1.2 bouyer
393 1.2 bouyer ci->ci_dev = self;
394 1.2 bouyer ci->ci_apicid = caa->cpu_number;
395 1.2 bouyer #ifdef MULTIPROCESSOR
396 1.2 bouyer ci->ci_cpuid = ci->ci_apicid;
397 1.2 bouyer #else
398 1.2 bouyer ci->ci_cpuid = 0; /* False for APs, but they're not used anyway */
399 1.2 bouyer #endif
400 1.2 bouyer ci->ci_cpumask = (1 << ci->ci_cpuid);
401 1.2 bouyer ci->ci_func = caa->cpu_func;
402 1.2 bouyer
403 1.2 bouyer if (caa->cpu_role == CPU_ROLE_AP) {
404 1.2 bouyer #if defined(MULTIPROCESSOR)
405 1.2 bouyer int error;
406 1.2 bouyer
407 1.2 bouyer error = mi_cpu_attach(ci);
408 1.2 bouyer if (error != 0) {
409 1.2 bouyer aprint_normal("\n");
410 1.2 bouyer aprint_error("%s: mi_cpu_attach failed with %d\n",
411 1.2 bouyer sc->sc_dev.dv_xname, error);
412 1.2 bouyer return;
413 1.2 bouyer }
414 1.2 bouyer #endif
415 1.2 bouyer } else {
416 1.2 bouyer KASSERT(ci->ci_data.cpu_idlelwp != NULL);
417 1.2 bouyer }
418 1.2 bouyer
419 1.2 bouyer pmap_reference(pmap_kernel());
420 1.2 bouyer ci->ci_pmap = pmap_kernel();
421 1.2 bouyer ci->ci_tlbstate = TLBSTATE_STALE;
422 1.2 bouyer
423 1.2 bouyer /* further PCB init done later. */
424 1.2 bouyer
425 1.2 bouyer printf(": ");
426 1.2 bouyer
427 1.2 bouyer switch (caa->cpu_role) {
428 1.2 bouyer case CPU_ROLE_SP:
429 1.2 bouyer printf("(uniprocessor)\n");
430 1.2 bouyer ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
431 1.2 bouyer cpu_intr_init(ci);
432 1.2 bouyer identifycpu(ci);
433 1.2 bouyer cpu_init(ci);
434 1.2 bouyer cpu_set_tss_gates(ci);
435 1.2 bouyer break;
436 1.2 bouyer
437 1.2 bouyer case CPU_ROLE_BP:
438 1.2 bouyer printf("apid %d (boot processor)\n", caa->cpu_number);
439 1.2 bouyer ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
440 1.2 bouyer cpu_intr_init(ci);
441 1.2 bouyer identifycpu(ci);
442 1.2 bouyer cpu_init(ci);
443 1.2 bouyer cpu_set_tss_gates(ci);
444 1.2 bouyer break;
445 1.2 bouyer
446 1.2 bouyer case CPU_ROLE_AP:
447 1.2 bouyer /*
448 1.2 bouyer * report on an AP
449 1.2 bouyer */
450 1.2 bouyer printf("apid %d (application processor)\n", caa->cpu_number);
451 1.2 bouyer
452 1.2 bouyer #if defined(MULTIPROCESSOR)
453 1.2 bouyer cpu_intr_init(ci);
454 1.2 bouyer gdt_alloc_cpu(ci);
455 1.2 bouyer cpu_set_tss_gates(ci);
456 1.2 bouyer cpu_start_secondary(ci);
457 1.2 bouyer if (ci->ci_flags & CPUF_PRESENT) {
458 1.2 bouyer identifycpu(ci);
459 1.2 bouyer ci->ci_next = cpu_info_list->ci_next;
460 1.2 bouyer cpu_info_list->ci_next = ci;
461 1.2 bouyer }
462 1.2 bouyer #else
463 1.2 bouyer printf("%s: not started\n", sc->sc_dev.dv_xname);
464 1.2 bouyer #endif
465 1.2 bouyer break;
466 1.2 bouyer
467 1.2 bouyer default:
468 1.2 bouyer panic("unknown processor type??\n");
469 1.2 bouyer }
470 1.2 bouyer cpu_vm_init(ci);
471 1.2 bouyer
472 1.2 bouyer cpus_attached |= (1 << ci->ci_cpuid);
473 1.2 bouyer
474 1.2 bouyer #if defined(MULTIPROCESSOR)
475 1.2 bouyer if (mp_verbose) {
476 1.2 bouyer struct lwp *l = ci->ci_data.cpu_idlelwp;
477 1.2 bouyer
478 1.2 bouyer aprint_verbose("%s: idle lwp at %p, idle sp at 0x%x\n",
479 1.2 bouyer sc->sc_dev.dv_xname, l, l->l_addr->u_pcb.pcb_esp);
480 1.2 bouyer }
481 1.2 bouyer #endif
482 1.2 bouyer }
483 1.2 bouyer
484 1.2 bouyer /*
485 1.2 bouyer * Initialize the processor appropriately.
486 1.2 bouyer */
487 1.2 bouyer
488 1.2 bouyer void
489 1.2 bouyer cpu_init(ci)
490 1.2 bouyer struct cpu_info *ci;
491 1.2 bouyer {
492 1.2 bouyer /* configure the CPU if needed */
493 1.2 bouyer if (ci->cpu_setup != NULL)
494 1.2 bouyer (*ci->cpu_setup)(ci);
495 1.2 bouyer
496 1.2 bouyer /*
497 1.2 bouyer * On a P6 or above, enable global TLB caching if the
498 1.2 bouyer * hardware supports it.
499 1.2 bouyer */
500 1.2 bouyer if (cpu_feature & CPUID_PGE)
501 1.2 bouyer lcr4(rcr4() | CR4_PGE); /* enable global TLB caching */
502 1.2 bouyer
503 1.2 bouyer #ifdef XXXMTRR
504 1.2 bouyer /*
505 1.2 bouyer * On a P6 or above, initialize MTRR's if the hardware supports them.
506 1.2 bouyer */
507 1.2 bouyer if (cpu_feature & CPUID_MTRR) {
508 1.2 bouyer if ((ci->ci_flags & CPUF_AP) == 0)
509 1.2 bouyer i686_mtrr_init_first();
510 1.2 bouyer mtrr_init_cpu(ci);
511 1.2 bouyer }
512 1.2 bouyer #endif
513 1.2 bouyer /*
514 1.2 bouyer * If we have FXSAVE/FXRESTOR, use them.
515 1.2 bouyer */
516 1.2 bouyer if (cpu_feature & CPUID_FXSR) {
517 1.2 bouyer lcr4(rcr4() | CR4_OSFXSR);
518 1.2 bouyer
519 1.2 bouyer /*
520 1.2 bouyer * If we have SSE/SSE2, enable XMM exceptions.
521 1.2 bouyer */
522 1.2 bouyer if (cpu_feature & (CPUID_SSE|CPUID_SSE2))
523 1.2 bouyer lcr4(rcr4() | CR4_OSXMMEXCPT);
524 1.2 bouyer }
525 1.2 bouyer
526 1.2 bouyer #ifdef MULTIPROCESSOR
527 1.2 bouyer ci->ci_flags |= CPUF_RUNNING;
528 1.2 bouyer cpus_running |= 1 << ci->ci_cpuid;
529 1.2 bouyer #endif
530 1.2 bouyer }
531 1.2 bouyer
532 1.2 bouyer
533 1.2 bouyer #ifdef MULTIPROCESSOR
534 1.2 bouyer void
535 1.2 bouyer cpu_boot_secondary_processors()
536 1.2 bouyer {
537 1.2 bouyer struct cpu_info *ci;
538 1.2 bouyer u_long i;
539 1.2 bouyer
540 1.2 bouyer for (i=0; i < X86_MAXPROCS; i++) {
541 1.2 bouyer ci = cpu_info[i];
542 1.2 bouyer if (ci == NULL)
543 1.2 bouyer continue;
544 1.2 bouyer if (ci->ci_data.cpu_idlelwp == NULL)
545 1.2 bouyer continue;
546 1.2 bouyer if ((ci->ci_flags & CPUF_PRESENT) == 0)
547 1.2 bouyer continue;
548 1.2 bouyer if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
549 1.2 bouyer continue;
550 1.2 bouyer cpu_boot_secondary(ci);
551 1.2 bouyer }
552 1.2 bouyer }
553 1.2 bouyer
554 1.2 bouyer static void
555 1.2 bouyer cpu_init_idle_lwp(struct cpu_info *ci)
556 1.2 bouyer {
557 1.2 bouyer struct lwp *l = ci->ci_data.cpu_idlelwp;
558 1.2 bouyer struct pcb *pcb = &l->l_addr->u_pcb;
559 1.2 bouyer
560 1.2 bouyer pcb->pcb_cr0 = rcr0();
561 1.2 bouyer }
562 1.2 bouyer
563 1.2 bouyer void
564 1.2 bouyer cpu_init_idle_lwps()
565 1.2 bouyer {
566 1.2 bouyer struct cpu_info *ci;
567 1.2 bouyer u_long i;
568 1.2 bouyer
569 1.2 bouyer for (i = 0; i < X86_MAXPROCS; i++) {
570 1.2 bouyer ci = cpu_info[i];
571 1.2 bouyer if (ci == NULL)
572 1.2 bouyer continue;
573 1.2 bouyer if (ci->ci_data.cpu_idlelwp == NULL)
574 1.2 bouyer continue;
575 1.2 bouyer if ((ci->ci_flags & CPUF_PRESENT) == 0)
576 1.2 bouyer continue;
577 1.2 bouyer cpu_init_idle_lwp(ci);
578 1.2 bouyer }
579 1.2 bouyer }
580 1.2 bouyer
581 1.2 bouyer void
582 1.2 bouyer cpu_start_secondary (ci)
583 1.2 bouyer struct cpu_info *ci;
584 1.2 bouyer {
585 1.2 bouyer int i;
586 1.2 bouyer struct pmap *kpm = pmap_kernel();
587 1.2 bouyer extern u_int32_t mp_pdirpa;
588 1.2 bouyer
589 1.2 bouyer mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */
590 1.2 bouyer
591 1.2 bouyer ci->ci_flags |= CPUF_AP;
592 1.2 bouyer
593 1.2 bouyer printf("%s: starting\n", ci->ci_dev->dv_xname);
594 1.2 bouyer
595 1.2 bouyer ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
596 1.2 bouyer CPU_STARTUP(ci);
597 1.2 bouyer
598 1.2 bouyer /*
599 1.2 bouyer * wait for it to become ready
600 1.2 bouyer */
601 1.2 bouyer for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i>0;i--) {
602 1.2 bouyer delay(10);
603 1.2 bouyer }
604 1.2 bouyer if (! (ci->ci_flags & CPUF_PRESENT)) {
605 1.2 bouyer printf("%s: failed to become ready\n", ci->ci_dev->dv_xname);
606 1.2 bouyer #if defined(MPDEBUG) && defined(DDB)
607 1.2 bouyer printf("dropping into debugger; continue from here to resume boot\n");
608 1.2 bouyer Debugger();
609 1.2 bouyer #endif
610 1.2 bouyer }
611 1.2 bouyer
612 1.2 bouyer CPU_START_CLEANUP(ci);
613 1.2 bouyer }
614 1.2 bouyer
615 1.2 bouyer void
616 1.2 bouyer cpu_boot_secondary(ci)
617 1.2 bouyer struct cpu_info *ci;
618 1.2 bouyer {
619 1.2 bouyer int i;
620 1.2 bouyer
621 1.2 bouyer ci->ci_flags |= CPUF_GO; /* XXX atomic */
622 1.2 bouyer
623 1.2 bouyer for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i>0;i--) {
624 1.2 bouyer delay(10);
625 1.2 bouyer }
626 1.2 bouyer if (! (ci->ci_flags & CPUF_RUNNING)) {
627 1.2 bouyer printf("CPU failed to start\n");
628 1.2 bouyer #if defined(MPDEBUG) && defined(DDB)
629 1.2 bouyer printf("dropping into debugger; continue from here to resume boot\n");
630 1.2 bouyer Debugger();
631 1.2 bouyer #endif
632 1.2 bouyer }
633 1.2 bouyer }
634 1.2 bouyer
635 1.2 bouyer /*
636 1.2 bouyer * The CPU ends up here when its ready to run
637 1.2 bouyer * This is called from code in mptramp.s; at this point, we are running
638 1.2 bouyer * in the idle pcb/idle stack of the new CPU. When this function returns,
639 1.2 bouyer * this processor will enter the idle loop and start looking for work.
640 1.2 bouyer *
641 1.2 bouyer * XXX should share some of this with init386 in machdep.c
642 1.2 bouyer */
643 1.2 bouyer void
644 1.2 bouyer cpu_hatch(void *v)
645 1.2 bouyer {
646 1.2 bouyer struct cpu_info *ci = (struct cpu_info *)v;
647 1.2 bouyer int s;
648 1.2 bouyer #ifdef __x86_64__
649 1.2 bouyer cpu_init_msrs(ci);
650 1.2 bouyer #endif
651 1.2 bouyer
652 1.2 bouyer cpu_probe_features(ci);
653 1.2 bouyer cpu_feature &= ci->ci_feature_flags;
654 1.2 bouyer /* not on Xen... */
655 1.2 bouyer cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX);
656 1.2 bouyer
657 1.2 bouyer #ifdef DEBUG
658 1.2 bouyer if (ci->ci_flags & CPUF_PRESENT)
659 1.2 bouyer panic("%s: already running!?", ci->ci_dev->dv_xname);
660 1.2 bouyer #endif
661 1.2 bouyer
662 1.2 bouyer ci->ci_flags |= CPUF_PRESENT;
663 1.2 bouyer
664 1.2 bouyer lapic_enable();
665 1.2 bouyer lapic_initclocks();
666 1.2 bouyer
667 1.2 bouyer while ((ci->ci_flags & CPUF_GO) == 0)
668 1.2 bouyer delay(10);
669 1.2 bouyer #ifdef DEBUG
670 1.2 bouyer if (ci->ci_flags & CPUF_RUNNING)
671 1.2 bouyer panic("%s: already running!?", ci->ci_dev->dv_xname);
672 1.2 bouyer #endif
673 1.2 bouyer
674 1.2 bouyer lcr0(ci->ci_data.cpu_idlelwp->l_addr->u_pcb.pcb_cr0);
675 1.2 bouyer cpu_init_idt();
676 1.2 bouyer lapic_set_lvt();
677 1.2 bouyer gdt_init_cpu(ci);
678 1.2 bouyer npxinit(ci);
679 1.2 bouyer
680 1.2 bouyer lldt(GSEL(GLDT_SEL, SEL_KPL));
681 1.2 bouyer
682 1.2 bouyer cpu_init(ci);
683 1.2 bouyer
684 1.2 bouyer s = splhigh();
685 1.2 bouyer lapic_tpr = 0;
686 1.2 bouyer enable_intr();
687 1.2 bouyer
688 1.2 bouyer printf("%s: CPU %ld running\n",ci->ci_dev->dv_xname, ci->ci_cpuid);
689 1.2 bouyer if (ci->ci_feature_flags & CPUID_TSC)
690 1.2 bouyer cc_microset(ci);
691 1.2 bouyer splx(s);
692 1.2 bouyer }
693 1.2 bouyer
694 1.2 bouyer #if defined(DDB)
695 1.2 bouyer
696 1.2 bouyer #include <ddb/db_output.h>
697 1.2 bouyer #include <machine/db_machdep.h>
698 1.2 bouyer
699 1.2 bouyer /*
700 1.2 bouyer * Dump CPU information from ddb.
701 1.2 bouyer */
702 1.2 bouyer void
703 1.2 bouyer cpu_debug_dump(void)
704 1.2 bouyer {
705 1.2 bouyer struct cpu_info *ci;
706 1.2 bouyer CPU_INFO_ITERATOR cii;
707 1.2 bouyer
708 1.2 bouyer db_printf("addr dev id flags ipis curproc fpcurproc\n");
709 1.2 bouyer for (CPU_INFO_FOREACH(cii, ci)) {
710 1.2 bouyer db_printf("%p %s %ld %x %x %10p %10p\n",
711 1.2 bouyer ci,
712 1.2 bouyer ci->ci_dev == NULL ? "BOOT" : ci->ci_dev->dv_xname,
713 1.2 bouyer ci->ci_cpuid,
714 1.2 bouyer ci->ci_flags, ci->ci_ipis,
715 1.2 bouyer ci->ci_curlwp,
716 1.2 bouyer ci->ci_fpcurlwp);
717 1.2 bouyer }
718 1.2 bouyer }
719 1.2 bouyer #endif
720 1.2 bouyer
721 1.2 bouyer static void
722 1.2 bouyer cpu_copy_trampoline()
723 1.2 bouyer {
724 1.2 bouyer /*
725 1.2 bouyer * Copy boot code.
726 1.2 bouyer */
727 1.2 bouyer extern u_char cpu_spinup_trampoline[];
728 1.2 bouyer extern u_char cpu_spinup_trampoline_end[];
729 1.2 bouyer pmap_kenter_pa((vaddr_t)MP_TRAMPOLINE, /* virtual */
730 1.2 bouyer (paddr_t)MP_TRAMPOLINE, /* physical */
731 1.2 bouyer VM_PROT_ALL); /* protection */
732 1.2 bouyer memcpy((void *)MP_TRAMPOLINE,
733 1.2 bouyer cpu_spinup_trampoline,
734 1.2 bouyer cpu_spinup_trampoline_end-cpu_spinup_trampoline);
735 1.2 bouyer }
736 1.2 bouyer
737 1.2 bouyer #endif
738 1.2 bouyer
739 1.2 bouyer
740 1.2 bouyer /* XXX */
741 1.2 bouyer #define IDTVEC(name) __CONCAT(X, name)
742 1.2 bouyer typedef void (vector)(void);
743 1.2 bouyer extern vector IDTVEC(tss_trap08);
744 1.2 bouyer #ifdef DDB
745 1.2 bouyer extern vector Xintrddbipi;
746 1.2 bouyer extern int ddb_vec;
747 1.2 bouyer #endif
748 1.2 bouyer
749 1.2 bouyer static void
750 1.2 bouyer cpu_set_tss_gates(struct cpu_info *ci)
751 1.2 bouyer {
752 1.2 bouyer #if defined(DDB) && defined(MULTIPROCESSOR)
753 1.2 bouyer /*
754 1.2 bouyer * Set up separate handler for the DDB IPI, so that it doesn't
755 1.2 bouyer * stomp on a possibly corrupted stack.
756 1.2 bouyer *
757 1.2 bouyer * XXX overwriting the gate set in db_machine_init.
758 1.2 bouyer * Should rearrange the code so that it's set only once.
759 1.2 bouyer */
760 1.2 bouyer ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
761 1.2 bouyer UVM_KMF_WIRED);
762 1.3.2.4 bouyer tss_init(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
763 1.2 bouyer Xintrddbipi);
764 1.2 bouyer
765 1.2 bouyer setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
766 1.2 bouyer SDT_SYS386TSS, SEL_KPL, 0, 0);
767 1.2 bouyer ci->ci_gdt[GIPITSS_SEL].sd = sd;
768 1.2 bouyer
769 1.2 bouyer setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
770 1.2 bouyer GSEL(GIPITSS_SEL, SEL_KPL));
771 1.2 bouyer #endif
772 1.2 bouyer }
773 1.2 bouyer
774 1.2 bouyer int
775 1.3.2.2 bouyer mp_cpu_start(struct cpu_info *ci, paddr_t target)
776 1.2 bouyer {
777 1.2 bouyer #if 0
778 1.2 bouyer #if NLAPIC > 0
779 1.2 bouyer int error;
780 1.2 bouyer #endif
781 1.2 bouyer unsigned short dwordptr[2];
782 1.2 bouyer
783 1.2 bouyer /*
784 1.2 bouyer * "The BSP must initialize CMOS shutdown code to 0Ah ..."
785 1.2 bouyer */
786 1.2 bouyer
787 1.2 bouyer outb(IO_RTC, NVRAM_RESET);
788 1.2 bouyer outb(IO_RTC+1, NVRAM_RESET_JUMP);
789 1.2 bouyer
790 1.2 bouyer /*
791 1.2 bouyer * "and the warm reset vector (DWORD based at 40:67) to point
792 1.2 bouyer * to the AP startup code ..."
793 1.2 bouyer */
794 1.2 bouyer
795 1.2 bouyer dwordptr[0] = 0;
796 1.3.2.2 bouyer dwordptr[1] = target >> 4;
797 1.2 bouyer
798 1.2 bouyer pmap_kenter_pa (0, 0, VM_PROT_READ|VM_PROT_WRITE);
799 1.2 bouyer memcpy ((u_int8_t *) 0x467, dwordptr, 4);
800 1.2 bouyer pmap_kremove (0, PAGE_SIZE);
801 1.2 bouyer
802 1.2 bouyer #if NLAPIC > 0
803 1.2 bouyer /*
804 1.2 bouyer * ... prior to executing the following sequence:"
805 1.2 bouyer */
806 1.2 bouyer
807 1.2 bouyer if (ci->ci_flags & CPUF_AP) {
808 1.2 bouyer if ((error = x86_ipi_init(ci->ci_apicid)) != 0)
809 1.2 bouyer return error;
810 1.2 bouyer
811 1.2 bouyer delay(10000);
812 1.2 bouyer
813 1.2 bouyer if (cpu_feature & CPUID_APIC) {
814 1.2 bouyer
815 1.3.2.2 bouyer if ((error = x86_ipi(target/PAGE_SIZE,
816 1.2 bouyer ci->ci_apicid,
817 1.2 bouyer LAPIC_DLMODE_STARTUP)) != 0)
818 1.2 bouyer return error;
819 1.2 bouyer delay(200);
820 1.2 bouyer
821 1.3.2.2 bouyer if ((error = x86_ipi(target/PAGE_SIZE,
822 1.2 bouyer ci->ci_apicid,
823 1.2 bouyer LAPIC_DLMODE_STARTUP)) != 0)
824 1.2 bouyer return error;
825 1.2 bouyer delay(200);
826 1.2 bouyer }
827 1.2 bouyer }
828 1.2 bouyer #endif
829 1.2 bouyer #endif /* 0 */
830 1.2 bouyer return 0;
831 1.2 bouyer }
832 1.2 bouyer
833 1.2 bouyer void
834 1.2 bouyer mp_cpu_start_cleanup(struct cpu_info *ci)
835 1.2 bouyer {
836 1.2 bouyer #if 0
837 1.2 bouyer /*
838 1.2 bouyer * Ensure the NVRAM reset byte contains something vaguely sane.
839 1.2 bouyer */
840 1.2 bouyer
841 1.2 bouyer outb(IO_RTC, NVRAM_RESET);
842 1.2 bouyer outb(IO_RTC+1, NVRAM_RESET_RST);
843 1.2 bouyer #endif
844 1.2 bouyer }
845 1.2 bouyer
846 1.2 bouyer #ifdef __x86_64__
847 1.2 bouyer
848 1.2 bouyer void
849 1.3 bouyer cpu_init_msrs(struct cpu_info *ci, bool full)
850 1.2 bouyer {
851 1.3 bouyer if (full) {
852 1.3 bouyer HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
853 1.3 bouyer HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (u_int64_t) ci);
854 1.3 bouyer HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
855 1.3 bouyer }
856 1.2 bouyer }
857 1.2 bouyer #endif /* __x86_64__ */
858 1.2 bouyer
859 1.2 bouyer void
860 1.2 bouyer cpu_get_tsc_freq(struct cpu_info *ci)
861 1.2 bouyer {
862 1.2 bouyer #ifdef XEN3
863 1.2 bouyer const volatile vcpu_time_info_t *tinfo =
864 1.2 bouyer &HYPERVISOR_shared_info->vcpu_info[0].time;
865 1.2 bouyer delay(1000000);
866 1.2 bouyer uint64_t freq = 1000000000ULL << 32;
867 1.2 bouyer freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
868 1.2 bouyer if ( tinfo->tsc_shift < 0 )
869 1.2 bouyer freq = freq << -tinfo->tsc_shift;
870 1.2 bouyer else
871 1.2 bouyer freq = freq >> tinfo->tsc_shift;
872 1.2 bouyer ci->ci_tsc_freq = freq;
873 1.2 bouyer #else
874 1.2 bouyer /* XXX this needs to read the shared_info of the CPU being probed.. */
875 1.2 bouyer ci->ci_tsc_freq = HYPERVISOR_shared_info->cpu_freq;
876 1.2 bouyer #endif /* XEN3 */
877 1.2 bouyer }
878