]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/hyperv/vmbus/hv_hv.c
Import sqlite3 3.12.1
[FreeBSD/FreeBSD.git] / sys / dev / hyperv / vmbus / hv_hv.c
1 /*-
2  * Copyright (c) 2009-2012 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 Hypver-V/Azure
31  */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/pcpu.h>
39 #include <sys/timetc.h>
40 #include <machine/bus.h>
41 #include <machine/md_var.h>
42 #include <vm/vm.h>
43 #include <vm/vm_param.h>
44 #include <vm/pmap.h>
45
46
47 #include "hv_vmbus_priv.h"
48
49 #define HV_NANOSECONDS_PER_SEC          1000000000L
50
51 #define HYPERV_INTERFACE                0x31237648      /* HV#1 */
52
53 static u_int hv_get_timecount(struct timecounter *tc);
54
55 u_int   hyperv_features;
56 u_int   hyperv_recommends;
57
58 static u_int    hyperv_pm_features;
59 static u_int    hyperv_features3;
60
61 /**
62  * Globals
63  */
64 hv_vmbus_context hv_vmbus_g_context = {
65         .syn_ic_initialized = FALSE,
66         .hypercall_page = NULL,
67 };
68
69 static struct timecounter hv_timecounter = {
70         hv_get_timecount, 0, ~0u, HV_NANOSECONDS_PER_SEC/100, "Hyper-V", HV_NANOSECONDS_PER_SEC/100
71 };
72
73 static u_int
74 hv_get_timecount(struct timecounter *tc)
75 {
76         u_int now = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
77         return (now);
78 }
79
80 /**
81  * @brief Invoke the specified hypercall
82  */
83 static uint64_t
84 hv_vmbus_do_hypercall(uint64_t control, void* input, void* output)
85 {
86 #ifdef __x86_64__
87         uint64_t hv_status = 0;
88         uint64_t input_address = (input) ? hv_get_phys_addr(input) : 0;
89         uint64_t output_address = (output) ? hv_get_phys_addr(output) : 0;
90         volatile void* hypercall_page = hv_vmbus_g_context.hypercall_page;
91
92         __asm__ __volatile__ ("mov %0, %%r8" : : "r" (output_address): "r8");
93         __asm__ __volatile__ ("call *%3" : "=a"(hv_status):
94                                 "c" (control), "d" (input_address),
95                                 "m" (hypercall_page));
96         return (hv_status);
97 #else
98         uint32_t control_high = control >> 32;
99         uint32_t control_low = control & 0xFFFFFFFF;
100         uint32_t hv_status_high = 1;
101         uint32_t hv_status_low = 1;
102         uint64_t input_address = (input) ? hv_get_phys_addr(input) : 0;
103         uint32_t input_address_high = input_address >> 32;
104         uint32_t input_address_low = input_address & 0xFFFFFFFF;
105         uint64_t output_address = (output) ? hv_get_phys_addr(output) : 0;
106         uint32_t output_address_high = output_address >> 32;
107         uint32_t output_address_low = output_address & 0xFFFFFFFF;
108         volatile void* hypercall_page = hv_vmbus_g_context.hypercall_page;
109
110         __asm__ __volatile__ ("call *%8" : "=d"(hv_status_high),
111                                 "=a"(hv_status_low) : "d" (control_high),
112                                 "a" (control_low), "b" (input_address_high),
113                                 "c" (input_address_low),
114                                 "D"(output_address_high),
115                                 "S"(output_address_low), "m" (hypercall_page));
116         return (hv_status_low | ((uint64_t)hv_status_high << 32));
117 #endif /* __x86_64__ */
118 }
119
120 /**
121  *  @brief Main initialization routine.
122  *
123  *  This routine must be called
124  *  before any other routines in here are called
125  */
126 int
127 hv_vmbus_init(void) 
128 {
129         hv_vmbus_x64_msr_hypercall_contents     hypercall_msr;
130         void*                                   virt_addr = NULL;
131
132         memset(
133             hv_vmbus_g_context.syn_ic_event_page,
134             0,
135             sizeof(hv_vmbus_handle) * MAXCPU);
136
137         memset(
138             hv_vmbus_g_context.syn_ic_msg_page,
139             0,
140             sizeof(hv_vmbus_handle) * MAXCPU);
141
142         if (vm_guest != VM_GUEST_HV)
143             goto cleanup;
144
145         /*
146          * Write our OS info
147          */
148         uint64_t os_guest_info = HV_FREEBSD_GUEST_ID;
149         wrmsr(HV_X64_MSR_GUEST_OS_ID, os_guest_info);
150         hv_vmbus_g_context.guest_id = os_guest_info;
151
152         /*
153          * See if the hypercall page is already set
154          */
155         hypercall_msr.as_uint64_t = rdmsr(HV_X64_MSR_HYPERCALL);
156         virt_addr = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
157
158         hypercall_msr.u.enable = 1;
159         hypercall_msr.u.guest_physical_address =
160             (hv_get_phys_addr(virt_addr) >> PAGE_SHIFT);
161         wrmsr(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64_t);
162
163         /*
164          * Confirm that hypercall page did get set up
165          */
166         hypercall_msr.as_uint64_t = 0;
167         hypercall_msr.as_uint64_t = rdmsr(HV_X64_MSR_HYPERCALL);
168
169         if (!hypercall_msr.u.enable)
170             goto cleanup;
171
172         hv_vmbus_g_context.hypercall_page = virt_addr;
173
174         hv_et_init();
175         
176         return (0);
177
178         cleanup:
179         if (virt_addr != NULL) {
180             if (hypercall_msr.u.enable) {
181                 hypercall_msr.as_uint64_t = 0;
182                 wrmsr(HV_X64_MSR_HYPERCALL,
183                                         hypercall_msr.as_uint64_t);
184             }
185
186             free(virt_addr, M_DEVBUF);
187         }
188         return (ENOTSUP);
189 }
190
191 /**
192  * @brief Cleanup routine, called normally during driver unloading or exiting
193  */
194 void
195 hv_vmbus_cleanup(void) 
196 {
197         hv_vmbus_x64_msr_hypercall_contents hypercall_msr;
198
199         if (hv_vmbus_g_context.guest_id == HV_FREEBSD_GUEST_ID) {
200             if (hv_vmbus_g_context.hypercall_page != NULL) {
201                 hypercall_msr.as_uint64_t = 0;
202                 wrmsr(HV_X64_MSR_HYPERCALL,
203                                         hypercall_msr.as_uint64_t);
204                 free(hv_vmbus_g_context.hypercall_page, M_DEVBUF);
205                 hv_vmbus_g_context.hypercall_page = NULL;
206             }
207         }
208 }
209
210 /**
211  * @brief Post a message using the hypervisor message IPC.
212  * (This involves a hypercall.)
213  */
214 hv_vmbus_status
215 hv_vmbus_post_msg_via_msg_ipc(
216         hv_vmbus_connection_id  connection_id,
217         hv_vmbus_msg_type       message_type,
218         void*                   payload,
219         size_t                  payload_size)
220 {
221         struct alignedinput {
222             uint64_t alignment8;
223             hv_vmbus_input_post_message msg;
224         };
225
226         hv_vmbus_input_post_message*    aligned_msg;
227         hv_vmbus_status                 status;
228         size_t                          addr;
229
230         if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
231             return (EMSGSIZE);
232
233         addr = (size_t) malloc(sizeof(struct alignedinput), M_DEVBUF,
234                             M_ZERO | M_NOWAIT);
235         KASSERT(addr != 0,
236             ("Error VMBUS: malloc failed to allocate message buffer!"));
237         if (addr == 0)
238             return (ENOMEM);
239
240         aligned_msg = (hv_vmbus_input_post_message*)
241             (HV_ALIGN_UP(addr, HV_HYPERCALL_PARAM_ALIGN));
242
243         aligned_msg->connection_id = connection_id;
244         aligned_msg->message_type = message_type;
245         aligned_msg->payload_size = payload_size;
246         memcpy((void*) aligned_msg->payload, payload, payload_size);
247
248         status = hv_vmbus_do_hypercall(
249                     HV_CALL_POST_MESSAGE, aligned_msg, 0) & 0xFFFF;
250
251         free((void *) addr, M_DEVBUF);
252         return (status);
253 }
254
255 /**
256  * @brief Signal an event on the specified connection using the hypervisor
257  * event IPC. (This involves a hypercall.)
258  */
259 hv_vmbus_status
260 hv_vmbus_signal_event(void *con_id)
261 {
262         hv_vmbus_status status;
263
264         status = hv_vmbus_do_hypercall(
265                     HV_CALL_SIGNAL_EVENT,
266                     con_id,
267                     0) & 0xFFFF;
268
269         return (status);
270 }
271
272 /**
273  * @brief hv_vmbus_synic_init
274  */
275 void
276 hv_vmbus_synic_init(void *arg)
277
278 {
279         int                     cpu;
280         uint64_t                hv_vcpu_index;
281         hv_vmbus_synic_simp     simp;
282         hv_vmbus_synic_siefp    siefp;
283         hv_vmbus_synic_scontrol sctrl;
284         hv_vmbus_synic_sint     shared_sint;
285         uint64_t                version;
286         hv_setup_args*          setup_args = (hv_setup_args *)arg;
287
288         cpu = PCPU_GET(cpuid);
289
290         if (hv_vmbus_g_context.hypercall_page == NULL)
291             return;
292
293         /*
294          * TODO: Check the version
295          */
296         version = rdmsr(HV_X64_MSR_SVERSION);
297         
298         hv_vmbus_g_context.syn_ic_msg_page[cpu] =
299             setup_args->page_buffers[2 * cpu];
300         hv_vmbus_g_context.syn_ic_event_page[cpu] =
301             setup_args->page_buffers[2 * cpu + 1];
302
303         /*
304          * Setup the Synic's message page
305          */
306
307         simp.as_uint64_t = rdmsr(HV_X64_MSR_SIMP);
308         simp.u.simp_enabled = 1;
309         simp.u.base_simp_gpa = ((hv_get_phys_addr(
310             hv_vmbus_g_context.syn_ic_msg_page[cpu])) >> PAGE_SHIFT);
311
312         wrmsr(HV_X64_MSR_SIMP, simp.as_uint64_t);
313
314         /*
315          * Setup the Synic's event page
316          */
317         siefp.as_uint64_t = rdmsr(HV_X64_MSR_SIEFP);
318         siefp.u.siefp_enabled = 1;
319         siefp.u.base_siefp_gpa = ((hv_get_phys_addr(
320             hv_vmbus_g_context.syn_ic_event_page[cpu])) >> PAGE_SHIFT);
321
322         wrmsr(HV_X64_MSR_SIEFP, siefp.as_uint64_t);
323
324         /*HV_SHARED_SINT_IDT_VECTOR + 0x20; */
325         shared_sint.as_uint64_t = 0;
326         shared_sint.u.vector = setup_args->vector;
327         shared_sint.u.masked = FALSE;
328         shared_sint.u.auto_eoi = TRUE;
329
330         wrmsr(HV_X64_MSR_SINT0 + HV_VMBUS_MESSAGE_SINT,
331             shared_sint.as_uint64_t);
332
333         wrmsr(HV_X64_MSR_SINT0 + HV_VMBUS_TIMER_SINT,
334             shared_sint.as_uint64_t);
335
336         /* Enable the global synic bit */
337         sctrl.as_uint64_t = rdmsr(HV_X64_MSR_SCONTROL);
338         sctrl.u.enable = 1;
339
340         wrmsr(HV_X64_MSR_SCONTROL, sctrl.as_uint64_t);
341
342         hv_vmbus_g_context.syn_ic_initialized = TRUE;
343
344         /*
345          * Set up the cpuid mapping from Hyper-V to FreeBSD.
346          * The array is indexed using FreeBSD cpuid.
347          */
348         hv_vcpu_index = rdmsr(HV_X64_MSR_VP_INDEX);
349         hv_vmbus_g_context.hv_vcpu_index[cpu] = (uint32_t)hv_vcpu_index;
350
351         return;
352 }
353
354 /**
355  * @brief Cleanup routine for hv_vmbus_synic_init()
356  */
357 void hv_vmbus_synic_cleanup(void *arg)
358 {
359         hv_vmbus_synic_sint     shared_sint;
360         hv_vmbus_synic_simp     simp;
361         hv_vmbus_synic_siefp    siefp;
362
363         if (!hv_vmbus_g_context.syn_ic_initialized)
364             return;
365
366         shared_sint.as_uint64_t = rdmsr(
367             HV_X64_MSR_SINT0 + HV_VMBUS_MESSAGE_SINT);
368
369         shared_sint.u.masked = 1;
370
371         /*
372          * Disable the interrupt 0
373          */
374         wrmsr(
375             HV_X64_MSR_SINT0 + HV_VMBUS_MESSAGE_SINT,
376             shared_sint.as_uint64_t);
377
378         shared_sint.as_uint64_t = rdmsr(
379             HV_X64_MSR_SINT0 + HV_VMBUS_TIMER_SINT);
380
381         shared_sint.u.masked = 1;
382
383         /*
384          * Disable the interrupt 1
385          */
386         wrmsr(
387             HV_X64_MSR_SINT0 + HV_VMBUS_TIMER_SINT,
388             shared_sint.as_uint64_t);
389         simp.as_uint64_t = rdmsr(HV_X64_MSR_SIMP);
390         simp.u.simp_enabled = 0;
391         simp.u.base_simp_gpa = 0;
392
393         wrmsr(HV_X64_MSR_SIMP, simp.as_uint64_t);
394
395         siefp.as_uint64_t = rdmsr(HV_X64_MSR_SIEFP);
396         siefp.u.siefp_enabled = 0;
397         siefp.u.base_siefp_gpa = 0;
398
399         wrmsr(HV_X64_MSR_SIEFP, siefp.as_uint64_t);
400 }
401
402 static bool
403 hyperv_identify(void)
404 {
405         u_int regs[4];
406         unsigned int maxLeaf;
407         unsigned int op;
408
409         if (vm_guest != VM_GUEST_HV)
410                 return (false);
411
412         op = HV_CPU_ID_FUNCTION_HV_VENDOR_AND_MAX_FUNCTION;
413         do_cpuid(op, regs);
414         maxLeaf = regs[0];
415         if (maxLeaf < HV_CPU_ID_FUNCTION_MS_HV_IMPLEMENTATION_LIMITS)
416                 return (false);
417
418         op = HV_CPU_ID_FUNCTION_HV_INTERFACE;
419         do_cpuid(op, regs);
420         if (regs[0] != HYPERV_INTERFACE)
421                 return (false);
422
423         op = HV_CPU_ID_FUNCTION_MS_HV_FEATURES;
424         do_cpuid(op, regs);
425         if ((regs[0] & HV_FEATURE_MSR_HYPERCALL) == 0) {
426                 /*
427                  * Hyper-V w/o Hypercall is impossible; someone
428                  * is faking Hyper-V.
429                  */
430                 return (false);
431         }
432         hyperv_features = regs[0];
433         hyperv_pm_features = regs[2];
434         hyperv_features3 = regs[3];
435
436         op = HV_CPU_ID_FUNCTION_MS_HV_VERSION;
437         do_cpuid(op, regs);
438         printf("Hyper-V Version: %d.%d.%d [SP%d]\n",
439             regs[1] >> 16, regs[1] & 0xffff, regs[0], regs[2]);
440
441         printf("  Features=0x%b\n", hyperv_features,
442             "\020"
443             "\001VPRUNTIME"     /* MSR_VP_RUNTIME */
444             "\002TMREFCNT"      /* MSR_TIME_REF_COUNT */
445             "\003SYNIC"         /* MSRs for SynIC */
446             "\004SYNTM"         /* MSRs for SynTimer */
447             "\005APIC"          /* MSR_{EOI,ICR,TPR} */
448             "\006HYPERCALL"     /* MSR_{GUEST_OS_ID,HYPERCALL} */
449             "\007VPINDEX"       /* MSR_VP_INDEX */
450             "\010RESET"         /* MSR_RESET */
451             "\011STATS"         /* MSR_STATS_ */
452             "\012REFTSC"        /* MSR_REFERENCE_TSC */
453             "\013IDLE"          /* MSR_GUEST_IDLE */
454             "\014TMFREQ"        /* MSR_{TSC,APIC}_FREQUENCY */
455             "\015DEBUG");       /* MSR_SYNTH_DEBUG_ */
456         printf("  PM Features=max C%u, 0x%b\n",
457             HV_PM_FEATURE_CSTATE(hyperv_pm_features),
458             (hyperv_pm_features & ~HV_PM_FEATURE_CSTATE_MASK),
459             "\020"
460             "\005C3HPET");      /* HPET is required for C3 state */
461         printf("  Features3=0x%b\n", hyperv_features3,
462             "\020"
463             "\001MWAIT"         /* MWAIT */
464             "\002DEBUG"         /* guest debug support */
465             "\003PERFMON"       /* performance monitor */
466             "\004PCPUDPE"       /* physical CPU dynamic partition event */
467             "\005XMMHC"         /* hypercall input through XMM regs */
468             "\006IDLE"          /* guest idle support */
469             "\007SLEEP"         /* hypervisor sleep support */
470             "\010NUMA"          /* NUMA distance query support */
471             "\011TMFREQ"        /* timer frequency query (TSC, LAPIC) */
472             "\012SYNCMC"        /* inject synthetic machine checks */
473             "\013CRASH"         /* MSRs for guest crash */
474             "\014DEBUGMSR"      /* MSRs for guest debug */
475             "\015NPIEP"         /* NPIEP */
476             "\016HVDIS");       /* disabling hypervisor */
477
478         op = HV_CPU_ID_FUNCTION_MS_HV_ENLIGHTENMENT_INFORMATION;
479         do_cpuid(op, regs);
480         hyperv_recommends = regs[0];
481         if (bootverbose)
482                 printf("  Recommends: %08x %08x\n", regs[0], regs[1]);
483
484         op = HV_CPU_ID_FUNCTION_MS_HV_IMPLEMENTATION_LIMITS;
485         do_cpuid(op, regs);
486         if (bootverbose) {
487                 printf("  Limits: Vcpu:%d Lcpu:%d Int:%d\n",
488                     regs[0], regs[1], regs[2]);
489         }
490
491         if (maxLeaf >= HV_CPU_ID_FUNCTION_MS_HV_HARDWARE_FEATURE) {
492                 op = HV_CPU_ID_FUNCTION_MS_HV_HARDWARE_FEATURE;
493                 do_cpuid(op, regs);
494                 if (bootverbose) {
495                         printf("  HW Features: %08x AMD: %08x\n",
496                             regs[0], regs[3]);
497                 }
498         }
499
500         return (true);
501 }
502
503 static void
504 hyperv_init(void *dummy __unused)
505 {
506         if (!hyperv_identify())
507                 return;
508
509         if (hyperv_features & HV_FEATURE_MSR_TIME_REFCNT) {
510                 /* Register virtual timecount */
511                 tc_init(&hv_timecounter);
512         }
513 }
514 SYSINIT(hyperv_initialize, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, hyperv_init,
515     NULL);