These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / target-i386 / hyperv.c
1 /*
2  * QEMU KVM Hyper-V support
3  *
4  * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
5  *
6  * Authors:
7  *  Andrey Smetanin <asmetanin@virtuozzo.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  *
12  */
13
14 #include "qemu/osdep.h"
15 #include "hyperv.h"
16 #include "standard-headers/asm-x86/hyperv.h"
17
18 int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
19 {
20     CPUX86State *env = &cpu->env;
21
22     switch (exit->type) {
23     case KVM_EXIT_HYPERV_SYNIC:
24         if (!cpu->hyperv_synic) {
25             return -1;
26         }
27
28         /*
29          * For now just track changes in SynIC control and msg/evt pages msr's.
30          * When SynIC messaging/events processing will be added in future
31          * here we will do messages queues flushing and pages remapping.
32          */
33         switch (exit->u.synic.msr) {
34         case HV_X64_MSR_SCONTROL:
35             env->msr_hv_synic_control = exit->u.synic.control;
36             break;
37         case HV_X64_MSR_SIMP:
38             env->msr_hv_synic_msg_page = exit->u.synic.msg_page;
39             break;
40         case HV_X64_MSR_SIEFP:
41             env->msr_hv_synic_evt_page = exit->u.synic.evt_page;
42             break;
43         default:
44             return -1;
45         }
46         return 0;
47     case KVM_EXIT_HYPERV_HCALL: {
48         uint16_t code;
49
50         code  = exit->u.hcall.input & 0xffff;
51         switch (code) {
52         case HVCALL_POST_MESSAGE:
53         case HVCALL_SIGNAL_EVENT:
54         default:
55             exit->u.hcall.result = HV_STATUS_INVALID_HYPERCALL_CODE;
56             return 0;
57         }
58     }
59     default:
60         return -1;
61     }
62 }
63
64 static void kvm_hv_sint_ack_handler(EventNotifier *notifier)
65 {
66     HvSintRoute *sint_route = container_of(notifier, HvSintRoute,
67                                            sint_ack_notifier);
68     event_notifier_test_and_clear(notifier);
69     if (sint_route->sint_ack_clb) {
70         sint_route->sint_ack_clb(sint_route);
71     }
72 }
73
74 HvSintRoute *kvm_hv_sint_route_create(uint32_t vcpu_id, uint32_t sint,
75                                       HvSintAckClb sint_ack_clb)
76 {
77     HvSintRoute *sint_route;
78     int r, gsi;
79
80     sint_route = g_malloc0(sizeof(*sint_route));
81     r = event_notifier_init(&sint_route->sint_set_notifier, false);
82     if (r) {
83         goto err;
84     }
85
86     r = event_notifier_init(&sint_route->sint_ack_notifier, false);
87     if (r) {
88         goto err_sint_set_notifier;
89     }
90
91     event_notifier_set_handler(&sint_route->sint_ack_notifier, false,
92                                kvm_hv_sint_ack_handler);
93
94     gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vcpu_id, sint);
95     if (gsi < 0) {
96         goto err_gsi;
97     }
98
99     r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
100                                            &sint_route->sint_set_notifier,
101                                            &sint_route->sint_ack_notifier, gsi);
102     if (r) {
103         goto err_irqfd;
104     }
105     sint_route->gsi = gsi;
106     sint_route->sint_ack_clb = sint_ack_clb;
107     sint_route->vcpu_id = vcpu_id;
108     sint_route->sint = sint;
109
110     return sint_route;
111
112 err_irqfd:
113     kvm_irqchip_release_virq(kvm_state, gsi);
114 err_gsi:
115     event_notifier_set_handler(&sint_route->sint_ack_notifier, false, NULL);
116     event_notifier_cleanup(&sint_route->sint_ack_notifier);
117 err_sint_set_notifier:
118     event_notifier_cleanup(&sint_route->sint_set_notifier);
119 err:
120     g_free(sint_route);
121
122     return NULL;
123 }
124
125 void kvm_hv_sint_route_destroy(HvSintRoute *sint_route)
126 {
127     kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
128                                           &sint_route->sint_set_notifier,
129                                           sint_route->gsi);
130     kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
131     event_notifier_set_handler(&sint_route->sint_ack_notifier, false, NULL);
132     event_notifier_cleanup(&sint_route->sint_ack_notifier);
133     event_notifier_cleanup(&sint_route->sint_set_notifier);
134     g_free(sint_route);
135 }
136
137 int kvm_hv_sint_route_set_sint(HvSintRoute *sint_route)
138 {
139     return event_notifier_set(&sint_route->sint_set_notifier);
140 }