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
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 #include <sys/cpuset.h>
39
40 #include <vm/vm.h>
41 #include <vm/vm_extern.h>
42 #include <vm/vm_kern.h>
43 #include <vm/pmap.h>
44
45 #include <dev/hyperv/include/hyperv.h>
46 #include <dev/hyperv/include/hyperv_busdma.h>
47 #if defined(__aarch64__)
48 #include <dev/hyperv/vmbus/aarch64/hyperv_machdep.h>
49 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h>
50 #else
51 #include <dev/hyperv/vmbus/x86/hyperv_machdep.h>
52 #include <dev/hyperv/vmbus/x86/hyperv_reg.h>
53 #endif
54 #include <dev/hyperv/vmbus/vmbus_var.h>
55 #include <dev/hyperv/vmbus/hyperv_common_reg.h>
56 #include <dev/hyperv/vmbus/hyperv_var.h>
57
58 #define HYPERV_FREEBSD_BUILD 0ULL
59 #define HYPERV_FREEBSD_VERSION ((uint64_t)__FreeBSD_version)
60 #define HYPERV_FREEBSD_OSID 0ULL
61
62 #define MSR_HV_GUESTID_BUILD_FREEBSD \
63 (HYPERV_FREEBSD_BUILD & MSR_HV_GUESTID_BUILD_MASK)
64 #define MSR_HV_GUESTID_VERSION_FREEBSD \
65 ((HYPERV_FREEBSD_VERSION << MSR_HV_GUESTID_VERSION_SHIFT) & \
66 MSR_HV_GUESTID_VERSION_MASK)
67 #define MSR_HV_GUESTID_OSID_FREEBSD \
68 ((HYPERV_FREEBSD_OSID << MSR_HV_GUESTID_OSID_SHIFT) & \
69 MSR_HV_GUESTID_OSID_MASK)
70
71 #define MSR_HV_GUESTID_FREEBSD \
72 (MSR_HV_GUESTID_BUILD_FREEBSD | \
73 MSR_HV_GUESTID_VERSION_FREEBSD | \
74 MSR_HV_GUESTID_OSID_FREEBSD | \
75 MSR_HV_GUESTID_OSTYPE_FREEBSD)
76
77
78 static bool hyperv_identify(void);
79 static void hypercall_memfree(void);
80
81 static struct hypercall_ctx hypercall_context;
82
83 uint64_t
hypercall_post_message(bus_addr_t msg_paddr)84 hypercall_post_message(bus_addr_t msg_paddr)
85 {
86 return hypercall_md(hypercall_context.hc_addr,
87 HYPERCALL_POST_MESSAGE, msg_paddr, 0);
88 }
89
90 uint64_t
hypercall_signal_event(bus_addr_t monprm_paddr)91 hypercall_signal_event(bus_addr_t monprm_paddr)
92 {
93 return hypercall_md(hypercall_context.hc_addr,
94 HYPERCALL_SIGNAL_EVENT, monprm_paddr, 0);
95 }
96
hv_result(uint64_t status)97 static inline int hv_result(uint64_t status)
98 {
99 return status & HV_HYPERCALL_RESULT_MASK;
100 }
101
hv_result_success(uint64_t status)102 static inline bool hv_result_success(uint64_t status)
103 {
104 return hv_result(status) == HV_STATUS_SUCCESS;
105 }
106
hv_repcomp(uint64_t status)107 static inline unsigned int hv_repcomp(uint64_t status)
108 {
109 /* Bits [43:32] of status have 'Reps completed' data. */
110 return ((status & HV_HYPERCALL_REP_COMP_MASK) >>
111 HV_HYPERCALL_REP_COMP_OFFSET);
112 }
113
114 /*
115 * Rep hypercalls. Callers of this functions are supposed to ensure that
116 * rep_count and varhead_size comply with Hyper-V hypercall definition.
117 */
118 uint64_t
hv_do_rep_hypercall(uint16_t code,uint16_t rep_count,uint16_t varhead_size,uint64_t input,uint64_t output)119 hv_do_rep_hypercall(uint16_t code, uint16_t rep_count, uint16_t varhead_size,
120 uint64_t input, uint64_t output)
121 {
122 uint64_t control = code;
123 uint64_t status;
124 uint16_t rep_comp;
125
126 control |= (uint64_t)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
127 control |= (uint64_t)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
128
129 do {
130 status = hypercall_do_md(control, input, output);
131 if (!hv_result_success(status))
132 return status;
133
134 rep_comp = hv_repcomp(status);
135
136 control &= ~HV_HYPERCALL_REP_START_MASK;
137 control |= (uint64_t)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
138
139 } while (rep_comp < rep_count);
140 if (hv_result_success(status))
141 return HV_STATUS_SUCCESS;
142
143 return status;
144 }
145
146 uint64_t
hypercall_do_md(uint64_t input_val,uint64_t input_addr,uint64_t out_addr)147 hypercall_do_md(uint64_t input_val, uint64_t input_addr, uint64_t out_addr)
148 {
149 uint64_t phys_inaddr, phys_outaddr;
150 phys_inaddr = input_addr ? vtophys(input_addr) : 0;
151 phys_outaddr = out_addr ? vtophys(out_addr) : 0;
152 return hypercall_md(hypercall_context.hc_addr,
153 input_val, phys_inaddr, phys_outaddr);
154 }
155
156 int
hyperv_guid2str(const struct hyperv_guid * guid,char * buf,size_t sz)157 hyperv_guid2str(const struct hyperv_guid *guid, char *buf, size_t sz)
158 {
159 const uint8_t *d = guid->hv_guid;
160
161 return snprintf(buf, sz, "%02x%02x%02x%02x-"
162 "%02x%02x-%02x%02x-%02x%02x-"
163 "%02x%02x%02x%02x%02x%02x",
164 d[3], d[2], d[1], d[0],
165 d[5], d[4], d[7], d[6], d[8], d[9],
166 d[10], d[11], d[12], d[13], d[14], d[15]);
167 }
168
169 static bool
hyperv_identify(void)170 hyperv_identify(void)
171 {
172 return(hyperv_identify_features());
173 }
174 static void
hyperv_init(void * dummy __unused)175 hyperv_init(void *dummy __unused)
176 {
177 if (!hyperv_identify()) {
178 /* Not Hyper-V; reset guest id to the generic one. */
179 if (vm_guest == VM_GUEST_HV)
180 vm_guest = VM_GUEST_VM;
181 return;
182 }
183
184 /* Set guest id */
185 WRMSR(MSR_HV_GUEST_OS_ID, MSR_HV_GUESTID_FREEBSD);
186 hyperv_init_tc();
187 }
188 SYSINIT(hyperv_initialize, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, hyperv_init,
189 NULL);
190
191 static void
hypercall_memfree(void)192 hypercall_memfree(void)
193 {
194 kmem_free(hypercall_context.hc_addr, PAGE_SIZE);
195 hypercall_context.hc_addr = NULL;
196 }
197
198 static void
hypercall_create(void * arg __unused)199 hypercall_create(void *arg __unused)
200 {
201
202 int ret;
203 if (vm_guest != VM_GUEST_HV)
204 return;
205
206 /*
207 * NOTE:
208 * - busdma(9), i.e. hyperv_dmamem APIs, can _not_ be used due to
209 * the NX bit.
210 * - Assume kmem_malloc() returns properly aligned memory.
211 */
212 hypercall_context.hc_addr = kmem_malloc(PAGE_SIZE, M_EXEC | M_WAITOK);
213 hypercall_context.hc_paddr = vtophys(hypercall_context.hc_addr);
214 ret = hypercall_page_setup(hypercall_context.hc_paddr);
215 if (ret) {
216 hypercall_memfree();
217 return;
218 }
219 if (bootverbose)
220 printf("hyperv: Hypercall created\n");
221 }
222 SYSINIT(hypercall_ctor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_create, NULL);
223
224 static void
hypercall_destroy(void * arg __unused)225 hypercall_destroy(void *arg __unused)
226 {
227
228 if (hypercall_context.hc_addr == NULL)
229 return;
230 hypercall_disable();
231 hypercall_memfree();
232 if (bootverbose)
233 printf("hyperv: Hypercall destroyed\n");
234 }
235 SYSUNINIT(hypercall_dtor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_destroy,
236 NULL);
237