x86/retpoline/hyperv: Convert assembler indirect jumps
[pandora-kernel.git] / drivers / hv / hv.c
1 /*
2  * Copyright (c) 2009, Microsoft Corporation.
3  *
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.
7  *
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
11  * more details.
12  *
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.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  *
21  */
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24 #include <linux/kernel.h>
25 #include <linux/mm.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"
32
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,
39 };
40
41 /*
42  * query_hypervisor_presence
43  * - Query the cpuid for presence of windows hypervisor
44  */
45 static int query_hypervisor_presence(void)
46 {
47         unsigned int eax;
48         unsigned int ebx;
49         unsigned int ecx;
50         unsigned int edx;
51         unsigned int op;
52
53         eax = 0;
54         ebx = 0;
55         ecx = 0;
56         edx = 0;
57         op = HVCPUID_VERSION_FEATURES;
58         cpuid(op, &eax, &ebx, &ecx, &edx);
59
60         return ecx & HV_PRESENT_BIT;
61 }
62
63 /*
64  * query_hypervisor_info - Get version info of the windows hypervisor
65  */
66 static int query_hypervisor_info(void)
67 {
68         unsigned int eax;
69         unsigned int ebx;
70         unsigned int ecx;
71         unsigned int edx;
72         unsigned int max_leaf;
73         unsigned int op;
74
75         /*
76         * Its assumed that this is called after confirming that Viridian
77         * is present. Query id and revision.
78         */
79         eax = 0;
80         ebx = 0;
81         ecx = 0;
82         edx = 0;
83         op = HVCPUID_VENDOR_MAXFUNCTION;
84         cpuid(op, &eax, &ebx, &ecx, &edx);
85
86         max_leaf = eax;
87
88         if (max_leaf >= HVCPUID_VERSION) {
89                 eax = 0;
90                 ebx = 0;
91                 ecx = 0;
92                 edx = 0;
93                 op = HVCPUID_VERSION;
94                 cpuid(op, &eax, &ebx, &ecx, &edx);
95                 pr_info("Hyper-V Host OS Build:%d-%d.%d-%d-%d.%d\n",
96                             eax,
97                             ebx >> 16,
98                             ebx & 0xFFFF,
99                             ecx,
100                             edx >> 24,
101                             edx & 0xFFFFFF);
102         }
103         return max_leaf;
104 }
105
106 /*
107  * do_hypercall- Invoke the specified hypercall
108  */
109 static u64 do_hypercall(u64 control, void *input, void *output)
110 {
111 #ifdef CONFIG_X86_64
112         u64 hv_status = 0;
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;
116
117         __asm__ __volatile__("mov %4, %%r8\n"
118                              CALL_NOSPEC
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");
124
125         return hv_status;
126
127 #else
128
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;
140
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)
148                              : "cc", "memory");
149
150         return hv_status_lo | ((u64)hv_status_hi << 32);
151 #endif /* !x86_64 */
152 }
153
154 /*
155  * hv_init - Main initialization routine.
156  *
157  * This routine must be called before any other routines in here are called
158  */
159 int hv_init(void)
160 {
161         int max_leaf;
162         union hv_x64_msr_hypercall_contents hypercall_msr;
163         void *virtaddr = NULL;
164
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);
170
171         if (!query_hypervisor_presence())
172                 goto cleanup;
173
174         max_leaf = query_hypervisor_info();
175
176         rdmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
177
178         if (hv_context.guestid != 0)
179                 goto cleanup;
180
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;
184
185         /* See if the hypercall page is already set */
186         rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
187
188         virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
189
190         if (!virtaddr)
191                 goto cleanup;
192
193         hypercall_msr.enable = 1;
194
195         hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
196         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
197
198         /* Confirm that hypercall page did get setup. */
199         hypercall_msr.as_uint64 = 0;
200         rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
201
202         if (!hypercall_msr.enable)
203                 goto cleanup;
204
205         hv_context.hypercall_page = virtaddr;
206
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),
210                                 GFP_KERNEL);
211         if (!hv_context.signal_event_buffer)
212                 goto cleanup;
213
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;
224
225         return 0;
226
227 cleanup:
228         if (virtaddr) {
229                 if (hypercall_msr.enable) {
230                         hypercall_msr.as_uint64 = 0;
231                         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
232                 }
233
234                 vfree(virtaddr);
235         }
236
237         return -ENOTSUPP;
238 }
239
240 /*
241  * hv_cleanup - Cleanup routine.
242  *
243  * This routine is called normally during driver unloading or exiting.
244  */
245 void hv_cleanup(void)
246 {
247         union hv_x64_msr_hypercall_contents hypercall_msr;
248
249         kfree(hv_context.signal_event_buffer);
250         hv_context.signal_event_buffer = NULL;
251         hv_context.signal_event_param = NULL;
252
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;
258         }
259 }
260
261 /*
262  * hv_post_message - Post a message using the hypervisor message IPC.
263  *
264  * This involves a hypercall.
265  */
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)
269 {
270
271         struct hv_input_post_message *aligned_msg;
272         u16 status;
273
274         if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
275                 return -EMSGSIZE;
276
277         aligned_msg = (struct hv_input_post_message *)
278                         hv_context.post_msg_page[get_cpu()];
279
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);
285
286         status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
287                 & 0xFFFF;
288
289         put_cpu();
290         return status;
291 }
292
293
294 /*
295  * hv_signal_event -
296  * Signal an event on the specified connection using the hypervisor event IPC.
297  *
298  * This involves a hypercall.
299  */
300 u16 hv_signal_event(void)
301 {
302         u16 status;
303
304         status = do_hypercall(HVCALL_SIGNAL_EVENT,
305                                hv_context.signal_event_param,
306                                NULL) & 0xFFFF;
307         return status;
308 }
309
310 /*
311  * hv_synic_init - Initialize the Synthethic Interrupt Controller.
312  *
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.
316  */
317 void hv_synic_init(void *irqarg)
318 {
319         u64 version;
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;
324
325         u32 irq_vector = *((u32 *)(irqarg));
326         int cpu = smp_processor_id();
327
328         if (!hv_context.hypercall_page)
329                 return;
330
331         /* Check the version */
332         rdmsrl(HV_X64_MSR_SVERSION, version);
333
334         hv_context.synic_message_page[cpu] =
335                 (void *)get_zeroed_page(GFP_ATOMIC);
336
337         if (hv_context.synic_message_page[cpu] == NULL) {
338                 pr_err("Unable to allocate SYNIC message page\n");
339                 goto cleanup;
340         }
341
342         hv_context.synic_event_page[cpu] =
343                 (void *)get_zeroed_page(GFP_ATOMIC);
344
345         if (hv_context.synic_event_page[cpu] == NULL) {
346                 pr_err("Unable to allocate SYNIC event page\n");
347                 goto cleanup;
348         }
349
350         hv_context.post_msg_page[cpu] =
351                 (void *)get_zeroed_page(GFP_ATOMIC);
352
353         if (hv_context.post_msg_page[cpu] == NULL) {
354                 pr_err("Unable to allocate post msg page\n");
355                 goto cleanup;
356         }
357
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])
362                 >> PAGE_SHIFT;
363
364         wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
365
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])
370                 >> PAGE_SHIFT;
371
372         wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
373
374         /* Setup the shared SINT. */
375         rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
376
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;
381
382         wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
383
384         /* Enable the global synic bit */
385         rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
386         sctrl.enable = 1;
387
388         wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
389
390         hv_context.synic_initialized = true;
391         return;
392
393 cleanup:
394         if (hv_context.synic_event_page[cpu])
395                 free_page((unsigned long)hv_context.synic_event_page[cpu]);
396
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]);
401         return;
402 }
403
404 /*
405  * hv_synic_cleanup - Cleanup routine for hv_synic_init().
406  */
407 void hv_synic_cleanup(void *arg)
408 {
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();
413
414         if (!hv_context.synic_initialized)
415                 return;
416
417         rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
418
419         shared_sint.masked = 1;
420
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);
424
425         rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
426         simp.simp_enabled = 0;
427         simp.base_simp_gpa = 0;
428
429         wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
430
431         rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
432         siefp.siefp_enabled = 0;
433         siefp.base_siefp_gpa = 0;
434
435         wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
436
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]);
440 }