Skip to content

Commit

Permalink
hyperv: factor out arch-independent API into hw/hyperv
Browse files Browse the repository at this point in the history
A significant part of hyperv.c is not actually tied to x86, and can
be moved to hw/.

This will allow to maintain most of Hyper-V and VMBus
target-independent, and to avoid conflicts with inclusion of
arch-specific headers down the road in VMBus implementation.

Also this stuff can now be opt-out with CONFIG_HYPERV.

Signed-off-by: Roman Kagan <rkagan@virtuozzo.com>
Message-Id: <20180921082041.29380-4-rkagan@virtuozzo.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
rvka authored and bonzini committed Oct 19, 2018
1 parent 51f0ac6 commit 701189e
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 146 deletions.
1 change: 1 addition & 0 deletions hw/Makefile.objs
Expand Up @@ -9,6 +9,7 @@ devices-dirs-$(CONFIG_SOFTMMU) += cpu/
devices-dirs-$(CONFIG_SOFTMMU) += display/
devices-dirs-$(CONFIG_SOFTMMU) += dma/
devices-dirs-$(CONFIG_SOFTMMU) += gpio/
devices-dirs-$(CONFIG_HYPERV) += hyperv/
devices-dirs-$(CONFIG_SOFTMMU) += i2c/
devices-dirs-$(CONFIG_SOFTMMU) += ide/
devices-dirs-$(CONFIG_SOFTMMU) += input/
Expand Down
2 changes: 2 additions & 0 deletions hw/hyperv/Makefile.objs
@@ -0,0 +1,2 @@
obj-y += hyperv.o
obj-$(CONFIG_HYPERV_TESTDEV) += hyperv_testdev.o
138 changes: 138 additions & 0 deletions hw/hyperv/hyperv.c
@@ -0,0 +1,138 @@
/*
* Hyper-V guest/hypervisor interaction
*
* Copyright (c) 2015-2018 Virtuozzo International GmbH.
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/

#include "qemu/osdep.h"
#include "qemu/main-loop.h"
#include "sysemu/kvm.h"
#include "hw/hyperv/hyperv.h"

struct HvSintRoute {
uint32_t sint;
CPUState *cs;
int gsi;
EventNotifier sint_set_notifier;
EventNotifier sint_ack_notifier;
HvSintAckClb sint_ack_clb;
void *sint_ack_clb_data;
unsigned refcount;
};

static CPUState *hyperv_find_vcpu(uint32_t vp_index)
{
CPUState *cs = qemu_get_cpu(vp_index);
assert(hyperv_vp_index(cs) == vp_index);
return cs;
}

static void kvm_hv_sint_ack_handler(EventNotifier *notifier)
{
HvSintRoute *sint_route = container_of(notifier, HvSintRoute,
sint_ack_notifier);
event_notifier_test_and_clear(notifier);
sint_route->sint_ack_clb(sint_route->sint_ack_clb_data);
}

HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
HvSintAckClb sint_ack_clb,
void *sint_ack_clb_data)
{
HvSintRoute *sint_route;
EventNotifier *ack_notifier;
int r, gsi;
CPUState *cs;

cs = hyperv_find_vcpu(vp_index);
if (!cs) {
return NULL;
}

sint_route = g_new0(HvSintRoute, 1);
r = event_notifier_init(&sint_route->sint_set_notifier, false);
if (r) {
goto err;
}

ack_notifier = sint_ack_clb ? &sint_route->sint_ack_notifier : NULL;
if (ack_notifier) {
r = event_notifier_init(ack_notifier, false);
if (r) {
goto err_sint_set_notifier;
}

event_notifier_set_handler(ack_notifier, kvm_hv_sint_ack_handler);
}

gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
if (gsi < 0) {
goto err_gsi;
}

r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
&sint_route->sint_set_notifier,
ack_notifier, gsi);
if (r) {
goto err_irqfd;
}
sint_route->gsi = gsi;
sint_route->sint_ack_clb = sint_ack_clb;
sint_route->sint_ack_clb_data = sint_ack_clb_data;
sint_route->cs = cs;
sint_route->sint = sint;
sint_route->refcount = 1;

return sint_route;

err_irqfd:
kvm_irqchip_release_virq(kvm_state, gsi);
err_gsi:
if (ack_notifier) {
event_notifier_set_handler(ack_notifier, NULL);
event_notifier_cleanup(ack_notifier);
}
err_sint_set_notifier:
event_notifier_cleanup(&sint_route->sint_set_notifier);
err:
g_free(sint_route);

return NULL;
}

void hyperv_sint_route_ref(HvSintRoute *sint_route)
{
sint_route->refcount++;
}

void hyperv_sint_route_unref(HvSintRoute *sint_route)
{
if (!sint_route) {
return;
}

assert(sint_route->refcount > 0);

if (--sint_route->refcount) {
return;
}

kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
&sint_route->sint_set_notifier,
sint_route->gsi);
kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
if (sint_route->sint_ack_clb) {
event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
event_notifier_cleanup(&sint_route->sint_ack_notifier);
}
event_notifier_cleanup(&sint_route->sint_set_notifier);
g_free(sint_route);
}

int hyperv_sint_route_set_sint(HvSintRoute *sint_route)
{
return event_notifier_set(&sint_route->sint_set_notifier);
}
2 changes: 1 addition & 1 deletion hw/misc/hyperv_testdev.c → hw/hyperv/hyperv_testdev.c
Expand Up @@ -15,7 +15,7 @@
#include "qemu/queue.h"
#include "hw/qdev.h"
#include "hw/isa/isa.h"
#include "target/i386/hyperv.h"
#include "hw/hyperv/hyperv.h"

typedef struct TestSintRoute {
QLIST_ENTRY(TestSintRoute) le;
Expand Down
1 change: 0 additions & 1 deletion hw/misc/Makefile.objs
Expand Up @@ -71,7 +71,6 @@ obj-$(CONFIG_IOTKIT_SYSCTL) += iotkit-sysctl.o
obj-$(CONFIG_IOTKIT_SYSINFO) += iotkit-sysinfo.o

obj-$(CONFIG_PVPANIC) += pvpanic.o
obj-$(CONFIG_HYPERV_TESTDEV) += hyperv_testdev.o
obj-$(CONFIG_AUX) += auxbus.o
obj-$(CONFIG_ASPEED_SOC) += aspeed_scu.o aspeed_sdmc.o
obj-$(CONFIG_MSF2) += msf2-sysreg.o
31 changes: 31 additions & 0 deletions include/hw/hyperv/hyperv.h
@@ -0,0 +1,31 @@
/*
* Hyper-V guest/hypervisor interaction
*
* Copyright (c) 2015-2018 Virtuozzo International GmbH.
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/

#ifndef HW_HYPERV_HYPERV_H
#define HW_HYPERV_HYPERV_H

#include "cpu-qom.h"

typedef struct HvSintRoute HvSintRoute;
typedef void (*HvSintAckClb)(void *data);

HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
HvSintAckClb sint_ack_clb,
void *sint_ack_clb_data);
void hyperv_sint_route_ref(HvSintRoute *sint_route);
void hyperv_sint_route_unref(HvSintRoute *sint_route);

int hyperv_sint_route_set_sint(HvSintRoute *sint_route);

static inline uint32_t hyperv_vp_index(CPUState *cs)
{
return cs->cpu_index;
}

#endif
127 changes: 1 addition & 126 deletions target/i386/hyperv.c
Expand Up @@ -12,28 +12,10 @@
*/

#include "qemu/osdep.h"
#include "qemu/main-loop.h"
#include "hyperv.h"
#include "hw/hyperv/hyperv.h"
#include "hyperv-proto.h"

struct HvSintRoute {
uint32_t sint;
X86CPU *cpu;
int gsi;
EventNotifier sint_set_notifier;
EventNotifier sint_ack_notifier;
HvSintAckClb sint_ack_clb;
void *sint_ack_clb_data;
unsigned refcount;
};

static X86CPU *hyperv_find_vcpu(uint32_t vp_index)
{
X86CPU *cpu = X86_CPU(qemu_get_cpu(vp_index));
assert(hyperv_vp_index(cpu) == vp_index);
return cpu;
}

int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
{
CPUX86State *env = &cpu->env;
Expand Down Expand Up @@ -79,110 +61,3 @@ int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)
return -1;
}
}

static void kvm_hv_sint_ack_handler(EventNotifier *notifier)
{
HvSintRoute *sint_route = container_of(notifier, HvSintRoute,
sint_ack_notifier);
event_notifier_test_and_clear(notifier);
sint_route->sint_ack_clb(sint_route->sint_ack_clb_data);
}

HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
HvSintAckClb sint_ack_clb,
void *sint_ack_clb_data)
{
HvSintRoute *sint_route;
EventNotifier *ack_notifier;
int r, gsi;
X86CPU *cpu;

cpu = hyperv_find_vcpu(vp_index);
if (!cpu) {
return NULL;
}

sint_route = g_new0(HvSintRoute, 1);
r = event_notifier_init(&sint_route->sint_set_notifier, false);
if (r) {
goto err;
}

ack_notifier = sint_ack_clb ? &sint_route->sint_ack_notifier : NULL;
if (ack_notifier) {
r = event_notifier_init(ack_notifier, false);
if (r) {
goto err_sint_set_notifier;
}

event_notifier_set_handler(ack_notifier, kvm_hv_sint_ack_handler);
}

gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
if (gsi < 0) {
goto err_gsi;
}

r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
&sint_route->sint_set_notifier,
ack_notifier, gsi);
if (r) {
goto err_irqfd;
}
sint_route->gsi = gsi;
sint_route->sint_ack_clb = sint_ack_clb;
sint_route->sint_ack_clb_data = sint_ack_clb_data;
sint_route->cpu = cpu;
sint_route->sint = sint;
sint_route->refcount = 1;

return sint_route;

err_irqfd:
kvm_irqchip_release_virq(kvm_state, gsi);
err_gsi:
if (ack_notifier) {
event_notifier_set_handler(ack_notifier, NULL);
event_notifier_cleanup(ack_notifier);
}
err_sint_set_notifier:
event_notifier_cleanup(&sint_route->sint_set_notifier);
err:
g_free(sint_route);

return NULL;
}

void hyperv_sint_route_ref(HvSintRoute *sint_route)
{
sint_route->refcount++;
}

void hyperv_sint_route_unref(HvSintRoute *sint_route)
{
if (!sint_route) {
return;
}

assert(sint_route->refcount > 0);

if (--sint_route->refcount) {
return;
}

kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
&sint_route->sint_set_notifier,
sint_route->gsi);
kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
if (sint_route->sint_ack_clb) {
event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
event_notifier_cleanup(&sint_route->sint_ack_notifier);
}
event_notifier_cleanup(&sint_route->sint_set_notifier);
g_free(sint_route);
}

int hyperv_sint_route_set_sint(HvSintRoute *sint_route)
{
return event_notifier_set(&sint_route->sint_set_notifier);
}
17 changes: 1 addition & 16 deletions target/i386/hyperv.h
Expand Up @@ -16,23 +16,8 @@

#include "cpu.h"
#include "sysemu/kvm.h"

typedef struct HvSintRoute HvSintRoute;
typedef void (*HvSintAckClb)(void *data);
#include "hw/hyperv/hyperv.h"

int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit);

HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
HvSintAckClb sint_ack_clb,
void *sint_ack_clb_data);
void hyperv_sint_route_ref(HvSintRoute *sint_route);
void hyperv_sint_route_unref(HvSintRoute *sint_route);

int hyperv_sint_route_set_sint(HvSintRoute *sint_route);

static inline uint32_t hyperv_vp_index(X86CPU *cpu)
{
return CPU(cpu)->cpu_index;
}

#endif
5 changes: 3 additions & 2 deletions target/i386/kvm.c
Expand Up @@ -774,7 +774,7 @@ static int hyperv_init_vcpu(X86CPU *cpu)
}
assert(ret == 1);

if (msr_data.entries[0].data != hyperv_vp_index(cpu)) {
if (msr_data.entries[0].data != hyperv_vp_index(CPU(cpu))) {
error_report("kernel's vp_index != QEMU's vp_index");
return -ENXIO;
}
Expand Down Expand Up @@ -1949,7 +1949,8 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
kvm_msr_entry_add(cpu, HV_X64_MSR_VP_RUNTIME, env->msr_hv_runtime);
}
if (cpu->hyperv_vpindex && hv_vpindex_settable) {
kvm_msr_entry_add(cpu, HV_X64_MSR_VP_INDEX, hyperv_vp_index(cpu));
kvm_msr_entry_add(cpu, HV_X64_MSR_VP_INDEX,
hyperv_vp_index(CPU(cpu)));
}
if (cpu->hyperv_synic) {
int j;
Expand Down

0 comments on commit 701189e

Please sign in to comment.