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