spinup_ap.c revision 245652
1/*- 2 * Copyright (c) 2012 NetApp, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/usr.sbin/bhyve/spinup_ap.c 245020 2013-01-04 02:04:41Z neel $ 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/usr.sbin/bhyve/spinup_ap.c 245020 2013-01-04 02:04:41Z neel $"); 31 32#include <sys/param.h> 33#include <sys/types.h> 34 35#include <machine/vmm.h> 36#include <vmmapi.h> 37 38#include <stdio.h> 39#include <stdlib.h> 40#include <assert.h> 41 42#include "bhyverun.h" 43#include "spinup_ap.h" 44 45static void 46spinup_ap_realmode(struct vmctx *ctx, int newcpu, uint64_t *rip) 47{ 48 int vector, error; 49 uint16_t cs; 50 uint64_t desc_base; 51 uint32_t desc_limit, desc_access; 52 53 vector = *rip >> PAGE_SHIFT; 54 *rip = 0; 55 56 /* 57 * Update the %cs and %rip of the guest so that it starts 58 * executing real mode code at at 'vector << 12'. 59 */ 60 error = vm_set_register(ctx, newcpu, VM_REG_GUEST_RIP, *rip); 61 assert(error == 0); 62 63 error = vm_get_desc(ctx, newcpu, VM_REG_GUEST_CS, &desc_base, 64 &desc_limit, &desc_access); 65 assert(error == 0); 66 67 desc_base = vector << PAGE_SHIFT; 68 error = vm_set_desc(ctx, newcpu, VM_REG_GUEST_CS, 69 desc_base, desc_limit, desc_access); 70 assert(error == 0); 71 72 cs = (vector << PAGE_SHIFT) >> 4; 73 error = vm_set_register(ctx, newcpu, VM_REG_GUEST_CS, cs); 74 assert(error == 0); 75} 76 77int 78spinup_ap(struct vmctx *ctx, int vcpu, int newcpu, uint64_t rip) 79{ 80 int error; 81 82 assert(newcpu != 0); 83 assert(newcpu < guest_ncpus); 84 85 error = vcpu_reset(ctx, newcpu); 86 assert(error == 0); 87 88 /* Set up capabilities */ 89 if (fbsdrun_vmexit_on_hlt()) { 90 error = vm_set_capability(ctx, newcpu, VM_CAP_HALT_EXIT, 1); 91 assert(error == 0); 92 } 93 94 if (fbsdrun_vmexit_on_pause()) { 95 error = vm_set_capability(ctx, newcpu, VM_CAP_PAUSE_EXIT, 1); 96 assert(error == 0); 97 } 98 99 if (fbsdrun_disable_x2apic()) 100 error = vm_set_x2apic_state(ctx, newcpu, X2APIC_DISABLED); 101 else 102 error = vm_set_x2apic_state(ctx, newcpu, X2APIC_ENABLED); 103 assert(error == 0); 104 105 /* 106 * Enable the 'unrestricted guest' mode for 'newcpu'. 107 * 108 * Set up the processor state in power-on 16-bit mode, with the CS:IP 109 * init'd to the specified low-mem 4K page. 110 */ 111 error = vm_set_capability(ctx, newcpu, VM_CAP_UNRESTRICTED_GUEST, 1); 112 assert(error == 0); 113 114 spinup_ap_realmode(ctx, newcpu, &rip); 115 116 fbsdrun_addcpu(ctx, newcpu, rip); 117 118 return (newcpu); 119} 120