2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/hyperv.h>
29 #include <asm/hyperv.h>
30 #include <asm/nospec-branch.h>
31 #include "hyperv_vmbus.h"
33 /* The one and only */
34 struct hv_context hv_context = {
35 .synic_initialized = false,
36 .hypercall_page = NULL,
37 .signal_event_param = NULL,
38 .signal_event_buffer = NULL,
42 * query_hypervisor_presence
43 * - Query the cpuid for presence of windows hypervisor
45 static int query_hypervisor_presence(void)
57 op = HVCPUID_VERSION_FEATURES;
58 cpuid(op, &eax, &ebx, &ecx, &edx);
60 return ecx & HV_PRESENT_BIT;
64 * query_hypervisor_info - Get version info of the windows hypervisor
66 static int query_hypervisor_info(void)
72 unsigned int max_leaf;
76 * Its assumed that this is called after confirming that Viridian
77 * is present. Query id and revision.
83 op = HVCPUID_VENDOR_MAXFUNCTION;
84 cpuid(op, &eax, &ebx, &ecx, &edx);
88 if (max_leaf >= HVCPUID_VERSION) {
94 cpuid(op, &eax, &ebx, &ecx, &edx);
95 pr_info("Hyper-V Host OS Build:%d-%d.%d-%d-%d.%d\n",
107 * do_hypercall- Invoke the specified hypercall
109 static u64 do_hypercall(u64 control, void *input, void *output)
113 u64 input_address = (input) ? virt_to_phys(input) : 0;
114 u64 output_address = (output) ? virt_to_phys(output) : 0;
115 void *hypercall_page = hv_context.hypercall_page;
117 __asm__ __volatile__("mov %4, %%r8\n"
119 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
120 "+c" (control), "+d" (input_address)
121 : "r" (output_address),
122 THUNK_TARGET(hypercall_page)
123 : "cc", "memory", "r8", "r9", "r10", "r11");
129 u32 control_hi = control >> 32;
130 u32 control_lo = control & 0xFFFFFFFF;
131 u32 hv_status_hi = 1;
132 u32 hv_status_lo = 1;
133 u64 input_address = (input) ? virt_to_phys(input) : 0;
134 u32 input_address_hi = input_address >> 32;
135 u32 input_address_lo = input_address & 0xFFFFFFFF;
136 u64 output_address = (output) ? virt_to_phys(output) : 0;
137 u32 output_address_hi = output_address >> 32;
138 u32 output_address_lo = output_address & 0xFFFFFFFF;
139 void *hypercall_page = hv_context.hypercall_page;
141 __asm__ __volatile__(CALL_NOSPEC
142 : "=d" (hv_status_hi), "=a" (hv_status_lo),
143 "+c" (input_address_lo), ASM_CALL_CONSTRAINT
144 : "d" (control_hi), "a" (control_lo),
145 "b" (input_address_hi),
146 "D"(output_address_hi), "S"(output_address_lo),
147 THUNK_TARGET(hypercall_page)
150 return hv_status_lo | ((u64)hv_status_hi << 32);
155 * hv_init - Main initialization routine.
157 * This routine must be called before any other routines in here are called
162 union hv_x64_msr_hypercall_contents hypercall_msr;
163 void *virtaddr = NULL;
165 memset(hv_context.synic_event_page, 0, sizeof(void *) * MAX_NUM_CPUS);
166 memset(hv_context.synic_message_page, 0,
167 sizeof(void *) * MAX_NUM_CPUS);
168 memset(hv_context.post_msg_page, 0,
169 sizeof(void *) * MAX_NUM_CPUS);
171 if (!query_hypervisor_presence())
174 max_leaf = query_hypervisor_info();
176 rdmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
178 if (hv_context.guestid != 0)
181 /* Write our OS info */
182 wrmsrl(HV_X64_MSR_GUEST_OS_ID, HV_LINUX_GUEST_ID);
183 hv_context.guestid = HV_LINUX_GUEST_ID;
185 /* See if the hypercall page is already set */
186 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
188 virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
193 hypercall_msr.enable = 1;
195 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
196 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
198 /* Confirm that hypercall page did get setup. */
199 hypercall_msr.as_uint64 = 0;
200 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
202 if (!hypercall_msr.enable)
205 hv_context.hypercall_page = virtaddr;
207 /* Setup the global signal event param for the signal event hypercall */
208 hv_context.signal_event_buffer =
209 kmalloc(sizeof(struct hv_input_signal_event_buffer),
211 if (!hv_context.signal_event_buffer)
214 hv_context.signal_event_param =
215 (struct hv_input_signal_event *)
216 (ALIGN((unsigned long)
217 hv_context.signal_event_buffer,
218 HV_HYPERCALL_PARAM_ALIGN));
219 hv_context.signal_event_param->connectionid.asu32 = 0;
220 hv_context.signal_event_param->connectionid.u.id =
221 VMBUS_EVENT_CONNECTION_ID;
222 hv_context.signal_event_param->flag_number = 0;
223 hv_context.signal_event_param->rsvdz = 0;
229 if (hypercall_msr.enable) {
230 hypercall_msr.as_uint64 = 0;
231 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
241 * hv_cleanup - Cleanup routine.
243 * This routine is called normally during driver unloading or exiting.
245 void hv_cleanup(void)
247 union hv_x64_msr_hypercall_contents hypercall_msr;
249 kfree(hv_context.signal_event_buffer);
250 hv_context.signal_event_buffer = NULL;
251 hv_context.signal_event_param = NULL;
253 if (hv_context.hypercall_page) {
254 hypercall_msr.as_uint64 = 0;
255 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
256 vfree(hv_context.hypercall_page);
257 hv_context.hypercall_page = NULL;
262 * hv_post_message - Post a message using the hypervisor message IPC.
264 * This involves a hypercall.
266 u16 hv_post_message(union hv_connection_id connection_id,
267 enum hv_message_type message_type,
268 void *payload, size_t payload_size)
271 struct hv_input_post_message *aligned_msg;
274 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
277 aligned_msg = (struct hv_input_post_message *)
278 hv_context.post_msg_page[get_cpu()];
280 aligned_msg->connectionid = connection_id;
281 aligned_msg->reserved = 0;
282 aligned_msg->message_type = message_type;
283 aligned_msg->payload_size = payload_size;
284 memcpy((void *)aligned_msg->payload, payload, payload_size);
286 status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
296 * Signal an event on the specified connection using the hypervisor event IPC.
298 * This involves a hypercall.
300 u16 hv_signal_event(void)
304 status = do_hypercall(HVCALL_SIGNAL_EVENT,
305 hv_context.signal_event_param,
311 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
313 * If it is already initialized by another entity (ie x2v shim), we need to
314 * retrieve the initialized message and event pages. Otherwise, we create and
315 * initialize the message and event pages.
317 void hv_synic_init(void *irqarg)
320 union hv_synic_simp simp;
321 union hv_synic_siefp siefp;
322 union hv_synic_sint shared_sint;
323 union hv_synic_scontrol sctrl;
325 u32 irq_vector = *((u32 *)(irqarg));
326 int cpu = smp_processor_id();
328 if (!hv_context.hypercall_page)
331 /* Check the version */
332 rdmsrl(HV_X64_MSR_SVERSION, version);
334 hv_context.synic_message_page[cpu] =
335 (void *)get_zeroed_page(GFP_ATOMIC);
337 if (hv_context.synic_message_page[cpu] == NULL) {
338 pr_err("Unable to allocate SYNIC message page\n");
342 hv_context.synic_event_page[cpu] =
343 (void *)get_zeroed_page(GFP_ATOMIC);
345 if (hv_context.synic_event_page[cpu] == NULL) {
346 pr_err("Unable to allocate SYNIC event page\n");
350 hv_context.post_msg_page[cpu] =
351 (void *)get_zeroed_page(GFP_ATOMIC);
353 if (hv_context.post_msg_page[cpu] == NULL) {
354 pr_err("Unable to allocate post msg page\n");
358 /* Setup the Synic's message page */
359 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
360 simp.simp_enabled = 1;
361 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
364 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
366 /* Setup the Synic's event page */
367 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
368 siefp.siefp_enabled = 1;
369 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
372 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
374 /* Setup the shared SINT. */
375 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
377 shared_sint.as_uint64 = 0;
378 shared_sint.vector = irq_vector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
379 shared_sint.masked = false;
380 shared_sint.auto_eoi = false;
382 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
384 /* Enable the global synic bit */
385 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
388 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
390 hv_context.synic_initialized = true;
394 if (hv_context.synic_event_page[cpu])
395 free_page((unsigned long)hv_context.synic_event_page[cpu]);
397 if (hv_context.synic_message_page[cpu])
398 free_page((unsigned long)hv_context.synic_message_page[cpu]);
399 if (hv_context.post_msg_page[cpu])
400 free_page((unsigned long)hv_context.post_msg_page[cpu]);
405 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
407 void hv_synic_cleanup(void *arg)
409 union hv_synic_sint shared_sint;
410 union hv_synic_simp simp;
411 union hv_synic_siefp siefp;
412 int cpu = smp_processor_id();
414 if (!hv_context.synic_initialized)
417 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
419 shared_sint.masked = 1;
421 /* Need to correctly cleanup in the case of SMP!!! */
422 /* Disable the interrupt */
423 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
425 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
426 simp.simp_enabled = 0;
427 simp.base_simp_gpa = 0;
429 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
431 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
432 siefp.siefp_enabled = 0;
433 siefp.base_siefp_gpa = 0;
435 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
437 free_page((unsigned long)hv_context.synic_message_page[cpu]);
438 free_page((unsigned long)hv_context.synic_event_page[cpu]);
439 free_page((unsigned long)hv_context.post_msg_page[cpu]);