1 /*- 2 * Copyright (c) 2009-2012,2016-2017 Microsoft Corp. 3 * Copyright (c) 2012 NetApp Inc. 4 * Copyright (c) 2012 Citrix 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 unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * Implements low-level interactions with Hyper-V/Azure 31 */ 32 #include <sys/cdefs.h> 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/malloc.h> 36 #include <sys/systm.h> 37 #include <sys/timetc.h> 38 39 #include <vm/vm.h> 40 #include <vm/vm_extern.h> 41 #include <vm/vm_kern.h> 42 #include <vm/pmap.h> 43 44 #include <dev/hyperv/include/hyperv.h> 45 #include <dev/hyperv/include/hyperv_busdma.h> 46 #if defined(__aarch64__) 47 #include <dev/hyperv/vmbus/aarch64/hyperv_machdep.h> 48 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h> 49 #else 50 #include <dev/hyperv/vmbus/x86/hyperv_machdep.h> 51 #include <dev/hyperv/vmbus/x86/hyperv_reg.h> 52 #endif 53 #include <dev/hyperv/vmbus/hyperv_common_reg.h> 54 #include <dev/hyperv/vmbus/hyperv_var.h> 55 56 #define HYPERV_FREEBSD_BUILD 0ULL 57 #define HYPERV_FREEBSD_VERSION ((uint64_t)__FreeBSD_version) 58 #define HYPERV_FREEBSD_OSID 0ULL 59 60 #define MSR_HV_GUESTID_BUILD_FREEBSD \ 61 (HYPERV_FREEBSD_BUILD & MSR_HV_GUESTID_BUILD_MASK) 62 #define MSR_HV_GUESTID_VERSION_FREEBSD \ 63 ((HYPERV_FREEBSD_VERSION << MSR_HV_GUESTID_VERSION_SHIFT) & \ 64 MSR_HV_GUESTID_VERSION_MASK) 65 #define MSR_HV_GUESTID_OSID_FREEBSD \ 66 ((HYPERV_FREEBSD_OSID << MSR_HV_GUESTID_OSID_SHIFT) & \ 67 MSR_HV_GUESTID_OSID_MASK) 68 69 #define MSR_HV_GUESTID_FREEBSD \ 70 (MSR_HV_GUESTID_BUILD_FREEBSD | \ 71 MSR_HV_GUESTID_VERSION_FREEBSD | \ 72 MSR_HV_GUESTID_OSID_FREEBSD | \ 73 MSR_HV_GUESTID_OSTYPE_FREEBSD) 74 75 static bool hyperv_identify(void); 76 static void hypercall_memfree(void); 77 78 static struct hypercall_ctx hypercall_context; 79 uint64_t 80 hypercall_post_message(bus_addr_t msg_paddr) 81 { 82 return hypercall_md(hypercall_context.hc_addr, 83 HYPERCALL_POST_MESSAGE, msg_paddr, 0); 84 } 85 86 uint64_t 87 hypercall_signal_event(bus_addr_t monprm_paddr) 88 { 89 return hypercall_md(hypercall_context.hc_addr, 90 HYPERCALL_SIGNAL_EVENT, monprm_paddr, 0); 91 } 92 93 int 94 hyperv_guid2str(const struct hyperv_guid *guid, char *buf, size_t sz) 95 { 96 const uint8_t *d = guid->hv_guid; 97 98 return snprintf(buf, sz, "%02x%02x%02x%02x-" 99 "%02x%02x-%02x%02x-%02x%02x-" 100 "%02x%02x%02x%02x%02x%02x", 101 d[3], d[2], d[1], d[0], 102 d[5], d[4], d[7], d[6], d[8], d[9], 103 d[10], d[11], d[12], d[13], d[14], d[15]); 104 } 105 106 static bool 107 hyperv_identify(void) 108 { 109 return(hyperv_identify_features()); 110 } 111 static void 112 hyperv_init(void *dummy __unused) 113 { 114 if (!hyperv_identify()) { 115 /* Not Hyper-V; reset guest id to the generic one. */ 116 if (vm_guest == VM_GUEST_HV) 117 vm_guest = VM_GUEST_VM; 118 return; 119 } 120 121 /* Set guest id */ 122 WRMSR(MSR_HV_GUEST_OS_ID, MSR_HV_GUESTID_FREEBSD); 123 hyperv_init_tc(); 124 } 125 SYSINIT(hyperv_initialize, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, hyperv_init, 126 NULL); 127 128 static void 129 hypercall_memfree(void) 130 { 131 kmem_free(hypercall_context.hc_addr, PAGE_SIZE); 132 hypercall_context.hc_addr = NULL; 133 } 134 135 static void 136 hypercall_create(void *arg __unused) 137 { 138 139 int ret; 140 if (vm_guest != VM_GUEST_HV) 141 return; 142 143 /* 144 * NOTE: 145 * - busdma(9), i.e. hyperv_dmamem APIs, can _not_ be used due to 146 * the NX bit. 147 * - Assume kmem_malloc() returns properly aligned memory. 148 */ 149 hypercall_context.hc_addr = kmem_malloc(PAGE_SIZE, M_EXEC | M_WAITOK); 150 hypercall_context.hc_paddr = vtophys(hypercall_context.hc_addr); 151 ret = hypercall_page_setup(hypercall_context.hc_paddr); 152 if (ret) { 153 hypercall_memfree(); 154 return; 155 } 156 if (bootverbose) 157 printf("hyperv: Hypercall created\n"); 158 } 159 SYSINIT(hypercall_ctor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_create, NULL); 160 161 static void 162 hypercall_destroy(void *arg __unused) 163 { 164 165 if (hypercall_context.hc_addr == NULL) 166 return; 167 hypercall_disable(); 168 hypercall_memfree(); 169 if (bootverbose) 170 printf("hyperv: Hypercall destroyed\n"); 171 } 172 SYSUNINIT(hypercall_dtor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_destroy, 173 NULL); 174