2 * Copyright (c) 2012 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
35 #include <machine/vmm.h>
43 #include "spinup_ap.h"
46 * Trampoline for hypervisor direct 64-bit jump.
48 * 0 - signature for guest->host verification
49 * 8 - kernel virtual address of trampoline
50 * 16 - instruction virtual address
51 * 24 - stack pointer virtual address
52 * 32 - CR3, physical address of kernel page table
53 * 40 - 24-byte area for null/code/data GDT entries
55 #define MP_V64T_SIG 0xcafebabecafebabeULL
66 spinup_ap_realmode(struct vmctx *ctx, int newcpu, uint64_t *rip)
71 uint32_t desc_limit, desc_access;
73 vector = *rip >> PAGE_SHIFT;
77 * Update the %cs and %rip of the guest so that it starts
78 * executing real mode code at at 'vector << 12'.
80 error = vm_set_register(ctx, newcpu, VM_REG_GUEST_RIP, *rip);
83 error = vm_get_desc(ctx, newcpu, VM_REG_GUEST_CS, &desc_base,
84 &desc_limit, &desc_access);
87 desc_base = vector << PAGE_SHIFT;
88 error = vm_set_desc(ctx, newcpu, VM_REG_GUEST_CS,
89 desc_base, desc_limit, desc_access);
92 cs = (vector << PAGE_SHIFT) >> 4;
93 error = vm_set_register(ctx, newcpu, VM_REG_GUEST_CS, cs);
98 spinup_ap_direct64(struct vmctx *ctx, int newcpu, uint64_t *rip)
100 struct mp_v64tramp *mvt;
105 mvt = paddr_guest2host(*rip);
107 assert(mvt->mt_sig == MP_V64T_SIG);
110 * Set up the 3-entry GDT using memory supplied in the
111 * guest's trampoline structure.
113 vm_setup_freebsd_gdt(mvt->mt_gdtr);
115 #define CHECK_ERROR(msg) \
124 /* Get the guest virtual address of the GDT */
125 gdtbase = mvt->mt_virt + __offsetof(struct mp_v64tramp, mt_gdtr);
127 error = vm_setup_freebsd_registers(ctx, newcpu, mvt->mt_eip,
128 mvt->mt_cr3, gdtbase, mvt->mt_rsp);
129 CHECK_ERROR("vm_setup_freebsd_registers");
133 printf("spinup_ap_direct64: machine state error: %s", errstr);
138 spinup_ap(struct vmctx *ctx, int vcpu, int newcpu, uint64_t rip)
143 assert(newcpu < guest_ncpus);
145 error = vcpu_reset(ctx, newcpu);
148 /* Set up capabilities */
149 if (fbsdrun_vmexit_on_hlt()) {
150 error = vm_set_capability(ctx, newcpu, VM_CAP_HALT_EXIT, 1);
154 if (fbsdrun_vmexit_on_pause()) {
155 error = vm_set_capability(ctx, newcpu, VM_CAP_PAUSE_EXIT, 1);
159 if (fbsdrun_disable_x2apic())
160 error = vm_set_x2apic_state(ctx, newcpu, X2APIC_DISABLED);
162 error = vm_set_x2apic_state(ctx, newcpu, X2APIC_ENABLED);
166 * There are 2 startup modes possible here:
167 * - if the CPU supports 'unrestricted guest' mode, the spinup can
168 * set up the processor state in power-on 16-bit mode, with the CS:IP
169 * init'd to the specified low-mem 4K page.
170 * - if the guest has requested a 64-bit trampoline in the low-mem 4K
171 * page by placing in the specified signature, set up the register
172 * state using register state in the signature. Note that this
173 * requires accessing guest physical memory to read the signature
174 * while 'unrestricted mode' does not.
176 error = vm_set_capability(ctx, newcpu, VM_CAP_UNRESTRICTED_GUEST, 1);
178 spinup_ap_direct64(ctx, newcpu, &rip);
180 spinup_ap_realmode(ctx, newcpu, &rip);
183 fbsdrun_addcpu(ctx, newcpu, rip);