xref: /netbsd-src/sys/arch/xen/x86/cpu.c (revision 1580a27b92f58fcdcb23fdfbc04a7c2b54a0b7c8)
1 /*	$NetBSD: cpu.c,v 1.116 2017/11/11 11:00:47 maxv Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by RedBack Networks Inc.
10  *
11  * Author: Bill Sommerfeld
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1999 Stefan Grefen
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *      This product includes software developed by the NetBSD
49  *      Foundation, Inc. and its contributors.
50  * 4. Neither the name of The NetBSD Foundation nor the names of its
51  *    contributors may be used to endorse or promote products derived
52  *    from this software without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
55  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  */
66 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.116 2017/11/11 11:00:47 maxv Exp $");
69 
70 #include "opt_ddb.h"
71 #include "opt_multiprocessor.h"
72 #include "opt_mpbios.h"		/* for MPDEBUG */
73 #include "opt_mtrr.h"
74 #include "opt_xen.h"
75 
76 #include "lapic.h"
77 #include "ioapic.h"
78 
79 #include <sys/param.h>
80 #include <sys/proc.h>
81 #include <sys/systm.h>
82 #include <sys/device.h>
83 #include <sys/kmem.h>
84 #include <sys/cpu.h>
85 #include <sys/cpufreq.h>
86 #include <sys/atomic.h>
87 #include <sys/reboot.h>
88 #include <sys/idle.h>
89 
90 #include <uvm/uvm.h>
91 
92 #include <machine/cpu.h>
93 #include <machine/cpufunc.h>
94 #include <machine/cpuvar.h>
95 #include <machine/pmap.h>
96 #include <machine/vmparam.h>
97 #include <machine/mpbiosvar.h>
98 #include <machine/pcb.h>
99 #include <machine/specialreg.h>
100 #include <machine/segments.h>
101 #include <machine/gdt.h>
102 #include <machine/mtrr.h>
103 #include <machine/pio.h>
104 
105 #include <x86/fpu.h>
106 
107 #include <xen/xen.h>
108 #include <xen/xen-public/vcpu.h>
109 #include <xen/vcpuvar.h>
110 
111 #if NLAPIC > 0
112 #include <machine/apicvar.h>
113 #include <machine/i82489reg.h>
114 #include <machine/i82489var.h>
115 #endif
116 
117 #include <dev/ic/mc146818reg.h>
118 #include <dev/isa/isareg.h>
119 
120 static int	cpu_match(device_t, cfdata_t, void *);
121 static void	cpu_attach(device_t, device_t, void *);
122 static void	cpu_defer(device_t);
123 static int	cpu_rescan(device_t, const char *, const int *);
124 static void	cpu_childdetached(device_t, device_t);
125 static int	vcpu_match(device_t, cfdata_t, void *);
126 static void	vcpu_attach(device_t, device_t, void *);
127 static void	cpu_attach_common(device_t, device_t, void *);
128 void		cpu_offline_md(void);
129 
130 struct cpu_softc {
131 	device_t sc_dev;		/* device tree glue */
132 	struct cpu_info *sc_info;	/* pointer to CPU info */
133 	bool sc_wasonline;
134 };
135 
136 int mp_cpu_start(struct cpu_info *, vaddr_t);
137 void mp_cpu_start_cleanup(struct cpu_info *);
138 const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
139 				      mp_cpu_start_cleanup };
140 
141 CFATTACH_DECL2_NEW(cpu, sizeof(struct cpu_softc),
142     cpu_match, cpu_attach, NULL, NULL, cpu_rescan, cpu_childdetached);
143 
144 CFATTACH_DECL_NEW(vcpu, sizeof(struct cpu_softc),
145     vcpu_match, vcpu_attach, NULL, NULL);
146 
147 /*
148  * Statically-allocated CPU info for the primary CPU (or the only
149  * CPU, on uniprocessors).  The CPU info list is initialized to
150  * point at it.
151  */
152 struct cpu_info cpu_info_primary __aligned(CACHE_LINE_SIZE) = {
153 	.ci_dev = 0,
154 	.ci_self = &cpu_info_primary,
155 	.ci_idepth = -1,
156 	.ci_curlwp = &lwp0,
157 	.ci_curldt = -1,
158 };
159 struct cpu_info phycpu_info_primary __aligned(CACHE_LINE_SIZE) = {
160 	.ci_dev = 0,
161 	.ci_self = &phycpu_info_primary,
162 };
163 
164 struct cpu_info *cpu_info_list = &cpu_info_primary;
165 struct cpu_info *phycpu_info_list = &phycpu_info_primary;
166 
167 uint32_t cpu_feature[7] __read_mostly; /* X86 CPUID feature bits
168 			  *	[0] basic features %edx
169 			  *	[1] basic features %ecx
170 			  *	[2] extended features %edx
171 			  *	[3] extended features %ecx
172 			  *	[4] VIA padlock features
173 			  *	[5] structured extended features cpuid.7:%ebx
174 			  *	[6] structured extended features cpuid.7:%ecx
175 			  */
176 
177 bool x86_mp_online;
178 paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
179 
180 #if defined(MULTIPROCESSOR)
181 void    	cpu_hatch(void *);
182 static void    	cpu_boot_secondary(struct cpu_info *ci);
183 static void    	cpu_start_secondary(struct cpu_info *ci);
184 #endif	/* MULTIPROCESSOR */
185 
186 static int
187 cpu_match(device_t parent, cfdata_t match, void *aux)
188 {
189 
190 	return 1;
191 }
192 
193 static void
194 cpu_attach(device_t parent, device_t self, void *aux)
195 {
196 	struct cpu_softc *sc = device_private(self);
197 	struct cpu_attach_args *caa = aux;
198 	struct cpu_info *ci;
199 	uintptr_t ptr;
200 	static int nphycpu = 0;
201 
202 	sc->sc_dev = self;
203 
204 	/*
205 	 * If we're an Application Processor, allocate a cpu_info
206 	 * If we're the first attached CPU use the primary cpu_info,
207 	 * otherwise allocate a new one
208 	 */
209 	aprint_naive("\n");
210 	aprint_normal("\n");
211 	if (nphycpu > 0) {
212 		struct cpu_info *tmp;
213 		ptr = (uintptr_t)kmem_zalloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
214 		    KM_SLEEP);
215 		ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
216 		ci->ci_curldt = -1;
217 
218 		tmp = phycpu_info_list;
219 		while (tmp->ci_next)
220 			tmp = tmp->ci_next;
221 
222 		tmp->ci_next = ci;
223 	} else {
224 		ci = &phycpu_info_primary;
225 	}
226 
227 	ci->ci_self = ci;
228 	sc->sc_info = ci;
229 
230 	ci->ci_dev = self;
231 	ci->ci_acpiid = caa->cpu_id;
232 	ci->ci_cpuid = caa->cpu_number;
233 	ci->ci_vcpu = NULL;
234 	ci->ci_index = nphycpu++;
235 
236 	if (!pmf_device_register(self, NULL, NULL))
237 		aprint_error_dev(self, "couldn't establish power handler\n");
238 
239 	(void)config_defer(self, cpu_defer);
240 }
241 
242 static void
243 cpu_defer(device_t self)
244 {
245 	cpu_rescan(self, NULL, NULL);
246 }
247 
248 static int
249 cpu_rescan(device_t self, const char *ifattr, const int *locators)
250 {
251 	struct cpu_softc *sc = device_private(self);
252 	struct cpufeature_attach_args cfaa;
253 	struct cpu_info *ci = sc->sc_info;
254 
255 	memset(&cfaa, 0, sizeof(cfaa));
256 	cfaa.ci = ci;
257 
258 	if (ifattr_match(ifattr, "cpufeaturebus")) {
259 
260 		if (ci->ci_frequency == NULL) {
261 			cfaa.name = "frequency";
262 			ci->ci_frequency = config_found_ia(self,
263 			    "cpufeaturebus", &cfaa, NULL);
264 		}
265 	}
266 
267 	return 0;
268 }
269 
270 static void
271 cpu_childdetached(device_t self, device_t child)
272 {
273 	struct cpu_softc *sc = device_private(self);
274 	struct cpu_info *ci = sc->sc_info;
275 
276 	if (ci->ci_frequency == child)
277 		ci->ci_frequency = NULL;
278 }
279 
280 static int
281 vcpu_match(device_t parent, cfdata_t match, void *aux)
282 {
283 	struct vcpu_attach_args *vcaa = aux;
284 	struct vcpu_runstate_info vcr;
285 	int error;
286 
287 	if (strcmp(vcaa->vcaa_name, match->cf_name) == 0) {
288 		error = HYPERVISOR_vcpu_op(VCPUOP_get_runstate_info,
289 		    vcaa->vcaa_caa.cpu_number, &vcr);
290 		switch (error) {
291 		case 0:
292 			return 1;
293 		case -ENOENT:
294 			return 0;
295 		default:
296 			panic("Unknown hypervisor error %d returned on vcpu runstate probe\n", error);
297 		}
298 	}
299 
300 	return 0;
301 }
302 
303 static void
304 vcpu_attach(device_t parent, device_t self, void *aux)
305 {
306 	struct vcpu_attach_args *vcaa = aux;
307 
308 	KASSERT(vcaa->vcaa_caa.cpu_func == NULL);
309 	vcaa->vcaa_caa.cpu_func = &mp_cpu_funcs;
310 	cpu_attach_common(parent, self, &vcaa->vcaa_caa);
311 
312 	if (!pmf_device_register(self, NULL, NULL))
313 		aprint_error_dev(self, "couldn't establish power handler\n");
314 }
315 
316 static int
317 vcpu_is_up(struct cpu_info *ci)
318 {
319 	KASSERT(ci != NULL);
320 	return HYPERVISOR_vcpu_op(VCPUOP_is_up, ci->ci_cpuid, NULL);
321 }
322 
323 static void
324 cpu_vm_init(struct cpu_info *ci)
325 {
326 	int ncolors = 2, i;
327 
328 	for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
329 		struct x86_cache_info *cai;
330 		int tcolors;
331 
332 		cai = &ci->ci_cinfo[i];
333 
334 		tcolors = atop(cai->cai_totalsize);
335 		switch (cai->cai_associativity) {
336 		case 0xff:
337 			tcolors = 1; /* fully associative */
338 			break;
339 		case 0:
340 		case 1:
341 			break;
342 		default:
343 			tcolors /= cai->cai_associativity;
344 		}
345 		ncolors = max(ncolors, tcolors);
346 	}
347 
348 	/*
349 	 * Knowing the size of the largest cache on this CPU, potentially
350 	 * re-color our pages.
351 	 */
352 	aprint_debug_dev(ci->ci_dev, "%d page colors\n", ncolors);
353 	uvm_page_recolor(ncolors);
354 	pmap_tlb_cpu_init(ci);
355 #ifndef __HAVE_DIRECT_MAP
356 	pmap_vpage_cpu_init(ci);
357 #endif
358 }
359 
360 static void
361 cpu_attach_common(device_t parent, device_t self, void *aux)
362 {
363 	struct cpu_softc *sc = device_private(self);
364 	struct cpu_attach_args *caa = aux;
365 	struct cpu_info *ci;
366 	uintptr_t ptr;
367 	int cpunum = caa->cpu_number;
368 	static bool again = false;
369 
370 	sc->sc_dev = self;
371 
372 	/*
373 	 * If we're an Application Processor, allocate a cpu_info
374 	 * structure, otherwise use the primary's.
375 	 */
376 	if (caa->cpu_role == CPU_ROLE_AP) {
377 		aprint_naive(": Application Processor\n");
378 		ptr = (uintptr_t)kmem_alloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
379 		    KM_SLEEP);
380 		ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
381 		memset(ci, 0, sizeof(*ci));
382 	} else {
383 		aprint_naive(": %s Processor\n",
384 		    caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
385 		ci = &cpu_info_primary;
386 	}
387 
388 	ci->ci_self = ci;
389 	sc->sc_info = ci;
390 	ci->ci_dev = self;
391 	ci->ci_cpuid = cpunum;
392 
393 	KASSERT(HYPERVISOR_shared_info != NULL);
394 	KASSERT(cpunum < XEN_LEGACY_MAX_VCPUS);
395 	ci->ci_vcpu = &HYPERVISOR_shared_info->vcpu_info[cpunum];
396 
397 	KASSERT(ci->ci_func == 0);
398 	ci->ci_func = caa->cpu_func;
399 	aprint_normal("\n");
400 
401 	/* Must be called before mi_cpu_attach(). */
402 	cpu_vm_init(ci);
403 
404 	if (caa->cpu_role == CPU_ROLE_AP) {
405 		int error;
406 
407 		error = mi_cpu_attach(ci);
408 
409 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
410 		if (error != 0) {
411 			aprint_error_dev(self,
412 			    "mi_cpu_attach failed with %d\n", error);
413 			return;
414 		}
415 
416 	} else {
417 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
418 	}
419 
420 	KASSERT(ci->ci_cpuid == ci->ci_index);
421 #ifdef __x86_64__
422 	/* No user PGD mapped for this CPU yet */
423 	ci->ci_xen_current_user_pgd = 0;
424 #endif
425 #if defined(__x86_64__) || defined(PAE)
426 	mutex_init(&ci->ci_kpm_mtx, MUTEX_DEFAULT, IPL_VM);
427 #endif
428 	pmap_reference(pmap_kernel());
429 	ci->ci_pmap = pmap_kernel();
430 	ci->ci_tlbstate = TLBSTATE_STALE;
431 
432 	/*
433 	 * Boot processor may not be attached first, but the below
434 	 * must be done to allow booting other processors.
435 	 */
436 	if (!again) {
437 		atomic_or_32(&ci->ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
438 		/* Basic init. */
439 		cpu_intr_init(ci);
440 		cpu_get_tsc_freq(ci);
441 		cpu_init(ci);
442 		pmap_cpu_init_late(ci);
443 
444 		/* Every processor needs to init its own ipi h/w (similar to lapic) */
445 		xen_ipi_init();
446 
447 		/* Make sure DELAY() is initialized. */
448 		DELAY(1);
449 		again = true;
450 	}
451 
452 	/* further PCB init done later. */
453 
454 	switch (caa->cpu_role) {
455 	case CPU_ROLE_SP:
456 		atomic_or_32(&ci->ci_flags, CPUF_SP);
457 		cpu_identify(ci);
458 		x86_cpu_idle_init();
459 		break;
460 
461 	case CPU_ROLE_BP:
462 		atomic_or_32(&ci->ci_flags, CPUF_BSP);
463 		cpu_identify(ci);
464 		x86_cpu_idle_init();
465 		break;
466 
467 	case CPU_ROLE_AP:
468 		atomic_or_32(&ci->ci_flags, CPUF_AP);
469 
470 		/*
471 		 * report on an AP
472 		 */
473 
474 #if defined(MULTIPROCESSOR)
475 		/* interrupt handler stack */
476 		cpu_intr_init(ci);
477 
478 		/* Setup per-cpu memory for gdt */
479 		gdt_alloc_cpu(ci);
480 
481 		pmap_cpu_init_late(ci);
482 		cpu_start_secondary(ci);
483 
484 		if (ci->ci_flags & CPUF_PRESENT) {
485 			struct cpu_info *tmp;
486 
487 			cpu_identify(ci);
488 			tmp = cpu_info_list;
489 			while (tmp->ci_next)
490 				tmp = tmp->ci_next;
491 
492 			tmp->ci_next = ci;
493 		}
494 #else
495 		aprint_error_dev(ci->ci_dev, "not started\n");
496 #endif
497 		break;
498 
499 	default:
500 		panic("unknown processor type??\n");
501 	}
502 
503 #ifdef MPVERBOSE
504 	if (mp_verbose) {
505 		struct lwp *l = ci->ci_data.cpu_idlelwp;
506 		struct pcb *pcb = lwp_getpcb(l);
507 
508 		aprint_verbose_dev(self,
509 		    "idle lwp at %p, idle sp at 0x%p\n",
510 		    l,
511 #ifdef i386
512 		    (void *)pcb->pcb_esp
513 #else
514 		    (void *)pcb->pcb_rsp
515 #endif
516 		);
517 
518 	}
519 #endif /* MPVERBOSE */
520 }
521 
522 /*
523  * Initialize the processor appropriately.
524  */
525 
526 void
527 cpu_init(struct cpu_info *ci)
528 {
529 
530 	/*
531 	 * If we have FXSAVE/FXRESTOR, use them.
532 	 */
533 	if (cpu_feature[0] & CPUID_FXSR) {
534 		lcr4(rcr4() | CR4_OSFXSR);
535 
536 		/*
537 		 * If we have SSE/SSE2, enable XMM exceptions.
538 		 */
539 		if (cpu_feature[0] & (CPUID_SSE|CPUID_SSE2))
540 			lcr4(rcr4() | CR4_OSXMMEXCPT);
541 	}
542 
543 	if (x86_fpu_save >= FPU_SAVE_FXSAVE) {
544 		fpuinit_mxcsr_mask();
545 	}
546 
547 	atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
548 }
549 
550 
551 #ifdef MULTIPROCESSOR
552 
553 void
554 cpu_boot_secondary_processors(void)
555 {
556 	struct cpu_info *ci;
557 	u_long i;
558 	for (i = 0; i < maxcpus; i++) {
559 		ci = cpu_lookup(i);
560 		if (ci == NULL)
561 			continue;
562 		if (ci->ci_data.cpu_idlelwp == NULL)
563 			continue;
564 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
565 			continue;
566 		if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
567 			continue;
568 		cpu_boot_secondary(ci);
569 	}
570 
571 	x86_mp_online = true;
572 }
573 
574 static void
575 cpu_init_idle_lwp(struct cpu_info *ci)
576 {
577 	struct lwp *l = ci->ci_data.cpu_idlelwp;
578 	struct pcb *pcb = lwp_getpcb(l);
579 
580 	pcb->pcb_cr0 = rcr0();
581 }
582 
583 void
584 cpu_init_idle_lwps(void)
585 {
586 	struct cpu_info *ci;
587 	u_long i;
588 
589 	for (i = 0; i < maxcpus; i++) {
590 		ci = cpu_lookup(i);
591 		if (ci == NULL)
592 			continue;
593 		if (ci->ci_data.cpu_idlelwp == NULL)
594 			continue;
595 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
596 			continue;
597 		cpu_init_idle_lwp(ci);
598 	}
599 }
600 
601 static void
602 cpu_start_secondary(struct cpu_info *ci)
603 {
604 	int i;
605 
606 	aprint_debug_dev(ci->ci_dev, "starting\n");
607 
608 	ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
609 
610 	if (CPU_STARTUP(ci, (vaddr_t) cpu_hatch) != 0) {
611 		return;
612 	}
613 
614 	/*
615 	 * wait for it to become ready
616 	 */
617 	for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i > 0; i--) {
618 		delay(10);
619 	}
620 	if ((ci->ci_flags & CPUF_PRESENT) == 0) {
621 		aprint_error_dev(ci->ci_dev, "failed to become ready\n");
622 #if defined(MPDEBUG) && defined(DDB)
623 		printf("dropping into debugger; continue from here to resume boot\n");
624 		Debugger();
625 #endif
626 	}
627 
628 	CPU_START_CLEANUP(ci);
629 }
630 
631 void
632 cpu_boot_secondary(struct cpu_info *ci)
633 {
634 	int i;
635 	atomic_or_32(&ci->ci_flags, CPUF_GO);
636 	for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i > 0; i--) {
637 		delay(10);
638 	}
639 	if ((ci->ci_flags & CPUF_RUNNING) == 0) {
640 		aprint_error_dev(ci->ci_dev, "CPU failed to start\n");
641 #if defined(MPDEBUG) && defined(DDB)
642 		printf("dropping into debugger; continue from here to resume boot\n");
643 		Debugger();
644 #endif
645 	}
646 }
647 
648 /*
649  * APs end up here immediately after initialisation and VCPUOP_up in
650  * mp_cpu_start().
651  * At this point, we are running in the idle pcb/idle stack of the new
652  * CPU.  This function jumps to the idle loop and starts looking for
653  * work.
654  */
655 extern void x86_64_tls_switch(struct lwp *);
656 void
657 cpu_hatch(void *v)
658 {
659 	struct cpu_info *ci = (struct cpu_info *)v;
660 	struct pcb *pcb;
661 	int s, i;
662 
663 	/* Setup TLS and kernel GS/FS */
664 	cpu_init_msrs(ci, true);
665 	cpu_init_idt();
666 	gdt_init_cpu(ci);
667 
668 	cpu_probe(ci);
669 
670 	atomic_or_32(&ci->ci_flags, CPUF_PRESENT);
671 
672 	while ((ci->ci_flags & CPUF_GO) == 0) {
673 		/* Don't use delay, boot CPU may be patching the text. */
674 		for (i = 10000; i != 0; i--)
675 			x86_pause();
676 	}
677 
678 	/* Because the text may have been patched in x86_patch(). */
679 	x86_flush();
680 	tlbflushg();
681 
682 	KASSERT((ci->ci_flags & CPUF_RUNNING) == 0);
683 
684 	pcb = lwp_getpcb(curlwp);
685 	pcb->pcb_cr3 = pmap_pdirpa(pmap_kernel(), 0);
686 	pcb = lwp_getpcb(ci->ci_data.cpu_idlelwp);
687 
688 	xen_ipi_init();
689 
690 	xen_initclocks();
691 
692 #ifdef __x86_64__
693 	fpuinit(ci);
694 #endif
695 
696 	lldt(GSEL(GLDT_SEL, SEL_KPL));
697 
698 	cpu_init(ci);
699 	cpu_get_tsc_freq(ci);
700 
701 	s = splhigh();
702 	x86_enable_intr();
703 	splx(s);
704 
705 	aprint_debug_dev(ci->ci_dev, "running\n");
706 
707 	cpu_switchto(NULL, ci->ci_data.cpu_idlelwp, true);
708 
709 	idle_loop(NULL);
710 	KASSERT(false);
711 }
712 
713 #if defined(DDB)
714 
715 #include <ddb/db_output.h>
716 #include <machine/db_machdep.h>
717 
718 /*
719  * Dump CPU information from ddb.
720  */
721 void
722 cpu_debug_dump(void)
723 {
724 	struct cpu_info *ci;
725 	CPU_INFO_ITERATOR cii;
726 
727 	db_printf("addr		dev	id	flags	ipis	curlwp 		fpcurlwp\n");
728 	for (CPU_INFO_FOREACH(cii, ci)) {
729 		db_printf("%p	%s	%ld	%x	%x	%10p	%10p\n",
730 		    ci,
731 		    ci->ci_dev == NULL ? "BOOT" : device_xname(ci->ci_dev),
732 		    (long)ci->ci_cpuid,
733 		    ci->ci_flags, ci->ci_ipis,
734 		    ci->ci_curlwp,
735 		    ci->ci_fpcurlwp);
736 	}
737 }
738 #endif /* DDB */
739 
740 #endif /* MULTIPROCESSOR */
741 
742 extern void hypervisor_callback(void);
743 extern void failsafe_callback(void);
744 #ifdef __x86_64__
745 typedef void (vector)(void);
746 extern vector Xsyscall, Xsyscall32;
747 #endif
748 
749 /*
750  * Setup the "trampoline". On Xen, we setup nearly all cpu context
751  * outside a trampoline, so we prototype and call targetip like so:
752  * void targetip(struct cpu_info *);
753  */
754 
755 static void
756 gdt_prepframes(paddr_t *frames, vaddr_t base, uint32_t entries)
757 {
758 	int i;
759 	for (i = 0; i < entries; i++) {
760 		frames[i] = ((paddr_t)xpmap_ptetomach(
761 		    (pt_entry_t *)(base + (i << PAGE_SHIFT)))) >> PAGE_SHIFT;
762 
763 		/* Mark Read-only */
764 		pmap_pte_clearbits(kvtopte(base + (i << PAGE_SHIFT)),
765 		    PG_RW);
766 	}
767 }
768 
769 #ifdef __x86_64__
770 extern char *ldtstore;
771 
772 static void
773 xen_init_amd64_vcpuctxt(struct cpu_info *ci, struct vcpu_guest_context *initctx,
774     void targetrip(struct cpu_info *))
775 {
776 	/* page frames to point at GDT */
777 	extern int gdt_size;
778 	paddr_t frames[16];
779 	psize_t gdt_ents;
780 
781 	struct lwp *l;
782 	struct pcb *pcb;
783 
784 	volatile struct vcpu_info *vci;
785 
786 	KASSERT(ci != NULL);
787 	KASSERT(ci != &cpu_info_primary);
788 	KASSERT(initctx != NULL);
789 	KASSERT(targetrip != NULL);
790 
791 	memset(initctx, 0, sizeof(*initctx));
792 
793 	gdt_ents = roundup(gdt_size, PAGE_SIZE) >> PAGE_SHIFT;
794 	KASSERT(gdt_ents <= 16);
795 
796 	gdt_prepframes(frames, (vaddr_t)ci->ci_gdt, gdt_ents);
797 
798 	/* Initialise the vcpu context: We use idle_loop()'s pcb context. */
799 
800 	l = ci->ci_data.cpu_idlelwp;
801 
802 	KASSERT(l != NULL);
803 	pcb = lwp_getpcb(l);
804 	KASSERT(pcb != NULL);
805 
806 	/* resume with interrupts off */
807 	vci = ci->ci_vcpu;
808 	vci->evtchn_upcall_mask = 1;
809 	xen_mb();
810 
811 	/* resume in kernel-mode */
812 	initctx->flags = VGCF_in_kernel | VGCF_online;
813 
814 	/* Stack and entry points:
815 	 * We arrange for the stack frame for cpu_hatch() to
816 	 * appear as a callee frame of lwp_trampoline(). Being a
817 	 * leaf frame prevents trampling on any of the MD stack setup
818 	 * that x86/vm_machdep.c:cpu_lwp_fork() does for idle_loop()
819 	 */
820 
821 	initctx->user_regs.rdi = (uint64_t) ci; /* targetrip(ci); */
822 	initctx->user_regs.rip = (vaddr_t) targetrip;
823 
824 	initctx->user_regs.cs = GSEL(GCODE_SEL, SEL_KPL);
825 
826 	initctx->user_regs.rflags = pcb->pcb_flags;
827 	initctx->user_regs.rsp = pcb->pcb_rsp;
828 
829 	/* Data segments */
830 	initctx->user_regs.ss = GSEL(GDATA_SEL, SEL_KPL);
831 	initctx->user_regs.es = GSEL(GDATA_SEL, SEL_KPL);
832 	initctx->user_regs.ds = GSEL(GDATA_SEL, SEL_KPL);
833 
834 	/* GDT */
835 	memcpy(initctx->gdt_frames, frames, sizeof(frames));
836 	initctx->gdt_ents = gdt_ents;
837 
838 	/* LDT */
839 	initctx->ldt_base = (unsigned long)ldtstore;
840 	initctx->ldt_ents = LDT_SIZE >> 3;
841 
842 	/* Kernel context state */
843 	initctx->kernel_ss = GSEL(GDATA_SEL, SEL_KPL);
844 	initctx->kernel_sp = pcb->pcb_rsp0;
845 	initctx->ctrlreg[0] = pcb->pcb_cr0;
846 	initctx->ctrlreg[1] = 0; /* "resuming" from kernel - no User cr3. */
847 	initctx->ctrlreg[2] = (vaddr_t)targetrip;
848 	/*
849 	 * Use pmap_kernel() L4 PD directly, until we setup the
850 	 * per-cpu L4 PD in pmap_cpu_init_late()
851 	 */
852 	initctx->ctrlreg[3] = xen_pfn_to_cr3(x86_btop(xpmap_ptom(ci->ci_kpm_pdirpa)));
853 	initctx->ctrlreg[4] = CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT;
854 
855 	/* Xen callbacks */
856 	initctx->event_callback_eip = (unsigned long)hypervisor_callback;
857 	initctx->failsafe_callback_eip = (unsigned long)failsafe_callback;
858 	initctx->syscall_callback_eip = (unsigned long)Xsyscall;
859 
860 	return;
861 }
862 #else /* i386 */
863 extern union descriptor *ldtstore;
864 extern void Xsyscall(void);
865 
866 static void
867 xen_init_i386_vcpuctxt(struct cpu_info *ci, struct vcpu_guest_context *initctx,
868     void targeteip(struct cpu_info *))
869 {
870 	/* page frames to point at GDT */
871 	extern int gdt_size;
872 	paddr_t frames[16];
873 	psize_t gdt_ents;
874 
875 	struct lwp *l;
876 	struct pcb *pcb;
877 
878 	volatile struct vcpu_info *vci;
879 
880 	KASSERT(ci != NULL);
881 	KASSERT(ci != &cpu_info_primary);
882 	KASSERT(initctx != NULL);
883 	KASSERT(targeteip != NULL);
884 
885 	memset(initctx, 0, sizeof(*initctx));
886 
887 	gdt_ents = roundup(gdt_size, PAGE_SIZE) >> PAGE_SHIFT;
888 	KASSERT(gdt_ents <= 16);
889 
890 	gdt_prepframes(frames, (vaddr_t)ci->ci_gdt, gdt_ents);
891 
892 	/*
893 	 * Initialise the vcpu context:
894 	 * We use this cpu's idle_loop() pcb context.
895 	 */
896 
897 	l = ci->ci_data.cpu_idlelwp;
898 
899 	KASSERT(l != NULL);
900 	pcb = lwp_getpcb(l);
901 	KASSERT(pcb != NULL);
902 
903 	/* resume with interrupts off */
904 	vci = ci->ci_vcpu;
905 	vci->evtchn_upcall_mask = 1;
906 	xen_mb();
907 
908 	/* resume in kernel-mode */
909 	initctx->flags = VGCF_in_kernel | VGCF_online;
910 
911 	/* Stack frame setup for cpu_hatch():
912 	 * We arrange for the stack frame for cpu_hatch() to
913 	 * appear as a callee frame of lwp_trampoline(). Being a
914 	 * leaf frame prevents trampling on any of the MD stack setup
915 	 * that x86/vm_machdep.c:cpu_lwp_fork() does for idle_loop()
916 	 */
917 
918 	initctx->user_regs.esp = pcb->pcb_esp - 4; /* Leave word for
919 						      arg1 */
920 	{
921 		/* targeteip(ci); */
922 		uint32_t *arg = (uint32_t *)initctx->user_regs.esp;
923 		arg[1] = (uint32_t)ci; /* arg1 */
924 	}
925 
926 	initctx->user_regs.eip = (vaddr_t)targeteip;
927 	initctx->user_regs.cs = GSEL(GCODE_SEL, SEL_KPL);
928 	initctx->user_regs.eflags |= pcb->pcb_iopl;
929 
930 	/* Data segments */
931 	initctx->user_regs.ss = GSEL(GDATA_SEL, SEL_KPL);
932 	initctx->user_regs.es = GSEL(GDATA_SEL, SEL_KPL);
933 	initctx->user_regs.ds = GSEL(GDATA_SEL, SEL_KPL);
934 	initctx->user_regs.fs = GSEL(GDATA_SEL, SEL_KPL);
935 
936 	/* GDT */
937 	memcpy(initctx->gdt_frames, frames, sizeof(frames));
938 	initctx->gdt_ents = gdt_ents;
939 
940 	/* LDT */
941 	initctx->ldt_base = (unsigned long)ldtstore;
942 	initctx->ldt_ents = NLDT;
943 
944 	/* Kernel context state */
945 	initctx->kernel_ss = GSEL(GDATA_SEL, SEL_KPL);
946 	initctx->kernel_sp = pcb->pcb_esp0;
947 	initctx->ctrlreg[0] = pcb->pcb_cr0;
948 	initctx->ctrlreg[1] = 0; /* "resuming" from kernel - no User cr3. */
949 	initctx->ctrlreg[2] = (vaddr_t)targeteip;
950 #ifdef PAE
951 	initctx->ctrlreg[3] = xen_pfn_to_cr3(x86_btop(xpmap_ptom(ci->ci_pae_l3_pdirpa)));
952 #else
953 	initctx->ctrlreg[3] = xen_pfn_to_cr3(x86_btop(xpmap_ptom(pcb->pcb_cr3)));
954 #endif
955 	initctx->ctrlreg[4] = /* CR4_PAE | */CR4_OSFXSR | CR4_OSXMMEXCPT;
956 
957 	/* Xen callbacks */
958 	initctx->event_callback_eip = (unsigned long)hypervisor_callback;
959 	initctx->event_callback_cs = GSEL(GCODE_SEL, SEL_KPL);
960 	initctx->failsafe_callback_eip = (unsigned long)failsafe_callback;
961 	initctx->failsafe_callback_cs = GSEL(GCODE_SEL, SEL_KPL);
962 
963 	return;
964 }
965 #endif /* __x86_64__ */
966 
967 int
968 mp_cpu_start(struct cpu_info *ci, vaddr_t target)
969 {
970 	int hyperror;
971 	struct vcpu_guest_context vcpuctx;
972 
973 	KASSERT(ci != NULL);
974 	KASSERT(ci != &cpu_info_primary);
975 	KASSERT(ci->ci_flags & CPUF_AP);
976 
977 #ifdef __x86_64__
978 	xen_init_amd64_vcpuctxt(ci, &vcpuctx, (void (*)(struct cpu_info *))target);
979 #else
980 	xen_init_i386_vcpuctxt(ci, &vcpuctx, (void (*)(struct cpu_info *))target);
981 #endif
982 
983 	/* Initialise the given vcpu to execute cpu_hatch(ci); */
984 	if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_initialise, ci->ci_cpuid, &vcpuctx))) {
985 		aprint_error(": context initialisation failed. errno = %d\n", hyperror);
986 		return hyperror;
987 	}
988 
989 	/* Start it up */
990 
991 	/* First bring it down */
992 	if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL))) {
993 		aprint_error(": VCPUOP_down hypervisor command failed. errno = %d\n", hyperror);
994 		return hyperror;
995 	}
996 
997 	if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_up, ci->ci_cpuid, NULL))) {
998 		aprint_error(": VCPUOP_up hypervisor command failed. errno = %d\n", hyperror);
999 		return hyperror;
1000 	}
1001 
1002 	if (!vcpu_is_up(ci)) {
1003 		aprint_error(": did not come up\n");
1004 		return -1;
1005 	}
1006 
1007 	return 0;
1008 }
1009 
1010 void
1011 mp_cpu_start_cleanup(struct cpu_info *ci)
1012 {
1013 	if (vcpu_is_up(ci)) {
1014 		aprint_debug_dev(ci->ci_dev, "is started.\n");
1015 	} else {
1016 		aprint_error_dev(ci->ci_dev, "did not start up.\n");
1017 	}
1018 }
1019 
1020 void
1021 cpu_init_msrs(struct cpu_info *ci, bool full)
1022 {
1023 #ifdef __x86_64__
1024 	if (full) {
1025 		HYPERVISOR_set_segment_base(SEGBASE_FS, 0);
1026 		HYPERVISOR_set_segment_base(SEGBASE_GS_KERNEL, (uint64_t)ci);
1027 		HYPERVISOR_set_segment_base(SEGBASE_GS_USER, 0);
1028 	}
1029 #endif
1030 
1031 	if (cpu_feature[2] & CPUID_NOX)
1032 		wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE);
1033 }
1034 
1035 void
1036 cpu_offline_md(void)
1037 {
1038 	int s;
1039 
1040 	s = splhigh();
1041 	fpusave_cpu(true);
1042 	splx(s);
1043 }
1044 
1045 void
1046 cpu_get_tsc_freq(struct cpu_info *ci)
1047 {
1048 	uint32_t vcpu_tversion;
1049 	const volatile vcpu_time_info_t *tinfo = &ci->ci_vcpu->time;
1050 
1051 	vcpu_tversion = tinfo->version;
1052 	while (tinfo->version == vcpu_tversion); /* Wait for a time update. XXX: timeout ? */
1053 
1054 	uint64_t freq = 1000000000ULL << 32;
1055 	freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
1056 	if (tinfo->tsc_shift < 0)
1057 		freq = freq << -tinfo->tsc_shift;
1058 	else
1059 		freq = freq >> tinfo->tsc_shift;
1060 	ci->ci_data.cpu_cc_freq = freq;
1061 }
1062 
1063 void
1064 x86_cpu_idle_xen(void)
1065 {
1066 	struct cpu_info *ci = curcpu();
1067 
1068 	KASSERT(ci->ci_ilevel == IPL_NONE);
1069 
1070 	x86_disable_intr();
1071 	if (!__predict_false(ci->ci_want_resched)) {
1072 		idle_block();
1073 	} else {
1074 		x86_enable_intr();
1075 	}
1076 }
1077 
1078 /*
1079  * Loads pmap for the current CPU.
1080  */
1081 void
1082 cpu_load_pmap(struct pmap *pmap, struct pmap *oldpmap)
1083 {
1084 	KASSERT(pmap != pmap_kernel());
1085 
1086 #if defined(__x86_64__) || defined(PAE)
1087 	struct cpu_info *ci = curcpu();
1088 	cpuid_t cid = cpu_index(ci);
1089 
1090 	mutex_enter(&ci->ci_kpm_mtx);
1091 	/* make new pmap visible to xen_kpm_sync() */
1092 	kcpuset_atomic_set(pmap->pm_xen_ptp_cpus, cid);
1093 #endif
1094 
1095 #ifdef i386
1096 #ifdef PAE
1097 	{
1098 		int i;
1099 		paddr_t l3_pd = xpmap_ptom_masked(ci->ci_pae_l3_pdirpa);
1100 		/* don't update the kernel L3 slot */
1101 		for (i = 0 ; i < PDP_SIZE - 1; i++) {
1102 			xpq_queue_pte_update(l3_pd + i * sizeof(pd_entry_t),
1103 			    xpmap_ptom(pmap->pm_pdirpa[i]) | PG_V);
1104 		}
1105 		tlbflush();
1106 	}
1107 #else /* PAE */
1108 	lcr3(pmap_pdirpa(pmap, 0));
1109 #endif /* PAE */
1110 #endif /* i386 */
1111 
1112 #ifdef __x86_64__
1113 	{
1114 		int i;
1115 		pd_entry_t *new_pgd;
1116 		paddr_t l4_pd_ma;
1117 
1118 		l4_pd_ma = xpmap_ptom_masked(ci->ci_kpm_pdirpa);
1119 
1120 		/*
1121 		 * Map user space address in kernel space and load
1122 		 * user cr3
1123 		 */
1124 		new_pgd = pmap->pm_pdir;
1125 		KASSERT(pmap == ci->ci_pmap);
1126 
1127 		/* Copy user pmap L4 PDEs (in user addr. range) to per-cpu L4 */
1128 		for (i = 0; i < PDIR_SLOT_PTE; i++) {
1129 			KASSERT(pmap != pmap_kernel() || new_pgd[i] == 0);
1130 			if (ci->ci_kpm_pdir[i] != new_pgd[i]) {
1131 				xpq_queue_pte_update(
1132 				    l4_pd_ma + i * sizeof(pd_entry_t),
1133 				    new_pgd[i]);
1134 			}
1135 		}
1136 
1137 		xen_set_user_pgd(pmap_pdirpa(pmap, 0));
1138 		ci->ci_xen_current_user_pgd = pmap_pdirpa(pmap, 0);
1139 
1140 		tlbflush();
1141 	}
1142 #endif /* __x86_64__ */
1143 
1144 #if defined(__x86_64__) || defined(PAE)
1145 	/* old pmap no longer visible to xen_kpm_sync() */
1146 	if (oldpmap != pmap_kernel()) {
1147 		kcpuset_atomic_clear(oldpmap->pm_xen_ptp_cpus, cid);
1148 	}
1149 	mutex_exit(&ci->ci_kpm_mtx);
1150 #endif
1151 }
1152 
1153 /*
1154  * pmap_cpu_init_late: perform late per-CPU initialization.
1155  *
1156  * Short note about percpu PDIR pages. Both the PAE and __x86_64__ architectures
1157  * have per-cpu PDIR tables, for two different reasons:
1158  *  - on PAE, this is to get around Xen's pagetable setup constraints (multiple
1159  *    L3[3]s cannot point to the same L2 - Xen will refuse to pin a table set up
1160  *    this way).
1161  *  - on __x86_64__, this is for multiple CPUs to map in different user pmaps
1162  *    (see cpu_load_pmap()).
1163  *
1164  * What this means for us is that the PDIR of the pmap_kernel() is considered
1165  * to be a canonical "SHADOW" PDIR with the following properties:
1166  *  - its recursive mapping points to itself
1167  *  - per-cpu recursive mappings point to themselves on __x86_64__
1168  *  - per-cpu L4 pages' kernel entries are expected to be in sync with
1169  *    the shadow
1170  */
1171 
1172 void
1173 pmap_cpu_init_late(struct cpu_info *ci)
1174 {
1175 #if defined(PAE) || defined(__x86_64__)
1176 	/*
1177 	 * The BP has already its own PD page allocated during early
1178 	 * MD startup.
1179 	 */
1180 
1181 #if defined(__x86_64__)
1182 	/* Setup per-cpu normal_pdes */
1183 	int i;
1184 	extern pd_entry_t * const normal_pdes[];
1185 	for (i = 0;i < PTP_LEVELS - 1;i++) {
1186 		ci->ci_normal_pdes[i] = normal_pdes[i];
1187 	}
1188 #endif /* __x86_64__ */
1189 
1190 	if (ci == &cpu_info_primary)
1191 		return;
1192 
1193 	KASSERT(ci != NULL);
1194 
1195 #if defined(PAE)
1196 	cpu_alloc_l3_page(ci);
1197 	KASSERT(ci->ci_pae_l3_pdirpa != 0);
1198 
1199 	/* Initialise L2 entries 0 - 2: Point them to pmap_kernel() */
1200 	int i;
1201 	for (i = 0 ; i < PDP_SIZE - 1; i++) {
1202 		ci->ci_pae_l3_pdir[i] =
1203 		    xpmap_ptom_masked(pmap_kernel()->pm_pdirpa[i]) | PG_V;
1204 	}
1205 #endif /* PAE */
1206 
1207 	ci->ci_kpm_pdir = (pd_entry_t *)uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
1208 	    UVM_KMF_WIRED | UVM_KMF_ZERO | UVM_KMF_NOWAIT);
1209 
1210 	if (ci->ci_kpm_pdir == NULL) {
1211 		panic("%s: failed to allocate L4 per-cpu PD for CPU %d\n",
1212 		    __func__, cpu_index(ci));
1213 	}
1214 	ci->ci_kpm_pdirpa = vtophys((vaddr_t)ci->ci_kpm_pdir);
1215 	KASSERT(ci->ci_kpm_pdirpa != 0);
1216 
1217 #if defined(__x86_64__)
1218 	extern pt_entry_t xpmap_pg_nx;
1219 
1220 	/* Copy over the pmap_kernel() shadow L4 entries */
1221 	memcpy(ci->ci_kpm_pdir, pmap_kernel()->pm_pdir, PAGE_SIZE);
1222 
1223 	/* Recursive kernel mapping */
1224 	ci->ci_kpm_pdir[PDIR_SLOT_PTE] = xpmap_ptom_masked(ci->ci_kpm_pdirpa)
1225 	    | PG_V | xpmap_pg_nx;
1226 #elif defined(PAE)
1227 	/* Copy over the pmap_kernel() shadow L2 entries */
1228 	memcpy(ci->ci_kpm_pdir, pmap_kernel()->pm_pdir + PDIR_SLOT_KERN,
1229 	    nkptp[PTP_LEVELS - 1] * sizeof(pd_entry_t));
1230 #endif
1231 
1232 	/* Xen wants a RO pdir. */
1233 	pmap_protect(pmap_kernel(), (vaddr_t)ci->ci_kpm_pdir,
1234 	    (vaddr_t)ci->ci_kpm_pdir + PAGE_SIZE, VM_PROT_READ);
1235 	pmap_update(pmap_kernel());
1236 #if defined(PAE)
1237 	/*
1238 	 * Initialize L3 entry 3. This mapping is shared across all pmaps and is
1239 	 * static, ie: loading a new pmap will not update this entry.
1240 	 */
1241 	ci->ci_pae_l3_pdir[3] = xpmap_ptom_masked(ci->ci_kpm_pdirpa) | PG_V;
1242 
1243 	/* Xen wants a RO L3. */
1244 	pmap_protect(pmap_kernel(), (vaddr_t)ci->ci_pae_l3_pdir,
1245 	    (vaddr_t)ci->ci_pae_l3_pdir + PAGE_SIZE, VM_PROT_READ);
1246 	pmap_update(pmap_kernel());
1247 
1248 	xpq_queue_pin_l3_table(xpmap_ptom_masked(ci->ci_pae_l3_pdirpa));
1249 
1250 #elif defined(__x86_64__)
1251 	xpq_queue_pin_l4_table(xpmap_ptom_masked(ci->ci_kpm_pdirpa));
1252 #endif /* PAE , __x86_64__ */
1253 #endif /* defined(PAE) || defined(__x86_64__) */
1254 }
1255 
1256 /*
1257  * Notify all other cpus to halt.
1258  */
1259 
1260 void
1261 cpu_broadcast_halt(void)
1262 {
1263 	xen_broadcast_ipi(XEN_IPI_HALT);
1264 }
1265 
1266 /*
1267  * Send a dummy ipi to a cpu.
1268  */
1269 
1270 void
1271 cpu_kick(struct cpu_info *ci)
1272 {
1273 	(void)xen_send_ipi(ci, XEN_IPI_KICK);
1274 }
1275