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