1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 NetApp, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 37 #include <machine/vmm.h> 38 #include <vmmapi.h> 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <assert.h> 43 44 #include "bhyverun.h" 45 #include "spinup_ap.h" 46 47 static void 48 spinup_ap_realmode(struct vmctx *ctx, int newcpu, uint64_t *rip) 49 { 50 int vector, error; 51 uint16_t cs; 52 uint64_t desc_base; 53 uint32_t desc_limit, desc_access; 54 55 vector = *rip >> PAGE_SHIFT; 56 *rip = 0; 57 58 /* 59 * Update the %cs and %rip of the guest so that it starts 60 * executing real mode code at at 'vector << 12'. 61 */ 62 error = vm_set_register(ctx, newcpu, VM_REG_GUEST_RIP, *rip); 63 assert(error == 0); 64 65 error = vm_get_desc(ctx, newcpu, VM_REG_GUEST_CS, &desc_base, 66 &desc_limit, &desc_access); 67 assert(error == 0); 68 69 desc_base = vector << PAGE_SHIFT; 70 error = vm_set_desc(ctx, newcpu, VM_REG_GUEST_CS, 71 desc_base, desc_limit, desc_access); 72 assert(error == 0); 73 74 cs = (vector << PAGE_SHIFT) >> 4; 75 error = vm_set_register(ctx, newcpu, VM_REG_GUEST_CS, cs); 76 assert(error == 0); 77 } 78 79 int 80 spinup_ap(struct vmctx *ctx, int vcpu, int newcpu, uint64_t rip) 81 { 82 int error; 83 84 assert(newcpu != 0); 85 assert(newcpu < guest_ncpus); 86 87 error = vcpu_reset(ctx, newcpu); 88 assert(error == 0); 89 90 fbsdrun_set_capabilities(ctx, newcpu); 91 92 /* 93 * Enable the 'unrestricted guest' mode for 'newcpu'. 94 * 95 * Set up the processor state in power-on 16-bit mode, with the CS:IP 96 * init'd to the specified low-mem 4K page. 97 */ 98 error = vm_set_capability(ctx, newcpu, VM_CAP_UNRESTRICTED_GUEST, 1); 99 assert(error == 0); 100 101 spinup_ap_realmode(ctx, newcpu, &rip); 102 103 #ifdef __FreeBSD__ 104 fbsdrun_addcpu(ctx, vcpu, newcpu, rip); 105 #else 106 fbsdrun_addcpu(ctx, vcpu, newcpu, rip, false); 107 #endif 108 109 return (newcpu); 110 } 111