[PATCH 8/9] use kvm_vcpu_ioctl

Glauber Costa
2009-07-29T17:49:24Z

kvm-all.c | 2 +-
qemu-kvm-x86.c | 66 +++++++++++++++----------------------------------------
qemu-kvm.c | 31 +++++++++++---------------
qemu-kvm.h | 2 +
4 files changed, 34 insertions(+), 67 deletions(-)

diff --git a/kvm-all.c b/kvm-all.c
index 5a4184b..203f1ab 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -848,7 +848,6 @@ int kvm_vm_ioctl(KVMState *s, int type, ...)
return ret;
}

-#ifdef KVM_UPSTREAM
int kvm_vcpu_ioctl(CPUState *env, int type, ...)
{
int ret;
@@ -865,6 +864,7 @@ int kvm_vcpu_ioctl(CPUState *env, int type, ...)

return ret;
}
+#ifdef KVM_UPSTREAM

int kvm_has_sync_mmu(void)
{
diff --git a/qemu-kvm-x86.c b/qemu-kvm-x86.c
index 14f1ee3..63f769a 100644
--- a/qemu-kvm-x86.c
+++ b/qemu-kvm-x86.c
@@ -174,18 +174,11 @@ static int kvm_handle_tpr_access(kvm_vcpu_context_t vcpu)

int kvm_enable_vapic(kvm_vcpu_context_t vcpu, uint64_t vapic)
{
- int r;
struct kvm_vapic_addr va = {
.vapic_addr = vapic,
};

- r = ioctl(vcpu->env->kvm_fd, KVM_SET_VAPIC_ADDR, &va);
- if (r == -1) {
- r = -errno;
- perror("kvm_enable_vapic");
- return r;
- }
- return 0;
+ return kvm_vcpu_ioctl(vcpu->env, KVM_SET_VAPIC_ADDR, &va);
}

#endif
@@ -283,28 +276,16 @@ int kvm_destroy_memory_alias(kvm_context_t kvm, uint64_t phys_start)

int kvm_get_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s)
{
- int r;
if (!kvm_irqchip_in_kernel())
return 0;
- r = ioctl(vcpu->env->kvm_fd, KVM_GET_LAPIC, s);
- if (r == -1) {
- r = -errno;
- perror("kvm_get_lapic");
- }
- return r;
+ return kvm_vcpu_ioctl(vcpu->env, KVM_GET_LAPIC, s);
}

int kvm_set_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s)
{
- int r;
if (!kvm_irqchip_in_kernel())
return 0;
- r = ioctl(vcpu->env->kvm_fd, KVM_SET_LAPIC, s);
- if (r == -1) {
- r = -errno;
- perror("kvm_set_lapic");
- }
- return r;
+ return kvm_vcpu_ioctl(vcpu->env, KVM_SET_LAPIC, s);
}

#endif
@@ -356,7 +337,7 @@ int kvm_has_pit_state2(kvm_context_t kvm)
void kvm_show_code(kvm_vcpu_context_t vcpu)
{
#define SHOW_CODE_LEN 50
- int fd = vcpu->env->kvm_fd;
+// int fd = vcpu->env->kvm_fd;
struct kvm_regs regs;
struct kvm_sregs sregs;
int r, n;
@@ -365,13 +346,13 @@ void kvm_show_code(kvm_vcpu_context_t vcpu)
char code_str[SHOW_CODE_LEN * 3 + 1];
unsigned long rip;

- r = ioctl(fd, KVM_GET_SREGS, &sregs);
- if (r == -1) {
+ r = kvm_vcpu_ioctl(vcpu->env, KVM_GET_SREGS, &sregs);
+ if (r < 0) {
perror("KVM_GET_SREGS");
return;
}
- r = ioctl(fd, KVM_GET_REGS, &regs);
- if (r == -1) {
+ r = kvm_vcpu_ioctl(vcpu->env, KVM_GET_REGS, &regs);
+ if (r < 0) {
perror("KVM_GET_REGS");
return;
}
@@ -420,29 +401,25 @@ struct kvm_msr_list *kvm_get_msr_list(kvm_context_t kvm)
int kvm_get_msrs(kvm_vcpu_context_t vcpu, struct kvm_msr_entry *msrs, int n)
{
struct kvm_msrs *kmsrs = qemu_malloc(sizeof *kmsrs + n * sizeof *msrs);
- int r, e;
+ int r;

kmsrs->nmsrs = n;
memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
- r = ioctl(vcpu->env->kvm_fd, KVM_GET_MSRS, kmsrs);
- e = errno;
+ r = kvm_vcpu_ioctl(vcpu->env, KVM_GET_MSRS, kmsrs);
memcpy(msrs, kmsrs->entries, n * sizeof *msrs);
free(kmsrs);
- errno = e;
return r;
}

int kvm_set_msrs(kvm_vcpu_context_t vcpu, struct kvm_msr_entry *msrs, int n)
{
struct kvm_msrs *kmsrs = qemu_malloc(sizeof *kmsrs + n * sizeof *msrs);
- int r, e;
+ int r;

kmsrs->nmsrs = n;
memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
- r = ioctl(vcpu->env->kvm_fd, KVM_SET_MSRS, kmsrs);
- e = errno;
+ r = kvm_vcpu_ioctl(vcpu->env, KVM_SET_MSRS, kmsrs);
free(kmsrs);
- errno = e;
return r;
}

@@ -464,7 +441,7 @@ int kvm_get_mce_cap_supported(kvm_context_t kvm, uint64_t *mce_cap,
int kvm_setup_mce(kvm_vcpu_context_t vcpu, uint64_t *mcg_cap)
{
#ifdef KVM_CAP_MCE
- return ioctl(vcpu->env->kvm_fd, KVM_X86_SETUP_MCE, mcg_cap);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_X86_SETUP_MCE, mcg_cap);
#else
return -ENOSYS;
#endif
@@ -473,7 +450,7 @@ int kvm_setup_mce(kvm_vcpu_context_t vcpu, uint64_t *mcg_cap)
int kvm_set_mce(kvm_vcpu_context_t vcpu, struct kvm_x86_mce *m)
{
#ifdef KVM_CAP_MCE
- return ioctl(vcpu->env->kvm_fd, KVM_X86_SET_MCE, m);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_X86_SET_MCE, m);
#else
return -ENOSYS;
#endif
@@ -563,7 +540,7 @@ int kvm_setup_cpuid(kvm_vcpu_context_t vcpu, int nent,

cpuid->nent = nent;
memcpy(cpuid->entries, entries, nent * sizeof(*entries));
- r = ioctl(vcpu->env->kvm_fd, KVM_SET_CPUID, cpuid);
+ r = kvm_vcpu_ioctl(vcpu->env, KVM_SET_CPUID, cpuid);

free(cpuid);
return r;
@@ -579,10 +556,9 @@ int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu, int nent,

cpuid->nent = nent;
memcpy(cpuid->entries, entries, nent * sizeof(*entries));
- r = ioctl(vcpu->env->kvm_fd, KVM_SET_CPUID2, cpuid);
- if (r == -1) {
+ r = kvm_vcpu_ioctl(vcpu->env, KVM_SET_CPUID2, cpuid);
+ if (r < 0) {
fprintf(stderr, "kvm_setup_cpuid2: %m\n");
- r = -errno;
}
free(cpuid);
return r;
@@ -634,13 +610,7 @@ static int tpr_access_reporting(kvm_vcpu_context_t vcpu, int enabled)
r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_VAPIC);
if (r <= 0)
return -ENOSYS;
- r = ioctl(vcpu->env->kvm_fd, KVM_TPR_ACCESS_REPORTING, &tac);
- if (r == -1) {
- r = -errno;
- perror("KVM_TPR_ACCESS_REPORTING");
- return r;
- }
- return 0;
+ return kvm_vcpu_ioctl(vcpu->env, KVM_TPR_ACCESS_REPORTING, &tac);
}

int kvm_enable_tpr_access_reporting(kvm_vcpu_context_t vcpu)
diff --git a/qemu-kvm.c b/qemu-kvm.c
index 6b9b29f..bc06f9f 100644
--- a/qemu-kvm.c
+++ b/qemu-kvm.c
@@ -812,32 +812,32 @@ int handle_debug(kvm_vcpu_context_t vcpu, void *env)

int kvm_get_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs)
{
- return ioctl(vcpu->env->kvm_fd, KVM_GET_REGS, regs);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_GET_REGS, regs);
}

int kvm_set_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs)
{
- return ioctl(vcpu->env->kvm_fd, KVM_SET_REGS, regs);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_SET_REGS, regs);
}

int kvm_get_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu)
{
- return ioctl(vcpu->env->kvm_fd, KVM_GET_FPU, fpu);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_GET_FPU, fpu);
}

int kvm_set_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu)
{
- return ioctl(vcpu->env->kvm_fd, KVM_SET_FPU, fpu);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_SET_FPU, fpu);
}

int kvm_get_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *sregs)
{
- return ioctl(vcpu->env->kvm_fd, KVM_GET_SREGS, sregs);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_GET_SREGS, sregs);
}

int kvm_set_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *sregs)
{
- return ioctl(vcpu->env->kvm_fd, KVM_SET_SREGS, sregs);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_SET_SREGS, sregs);
}

#ifdef KVM_CAP_MP_STATE
@@ -847,7 +847,7 @@ int kvm_get_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state)

r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_MP_STATE);
if (r > 0)
- return ioctl(vcpu->env->kvm_fd, KVM_GET_MP_STATE, mp_state);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_GET_MP_STATE, mp_state);
return -ENOSYS;
}

@@ -857,7 +857,7 @@ int kvm_set_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state)

r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_MP_STATE);
if (r > 0)
- return ioctl(vcpu->env->kvm_fd, KVM_SET_MP_STATE, mp_state);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_SET_MP_STATE, mp_state);
return -ENOSYS;
}
#endif
@@ -1034,13 +1034,13 @@ int kvm_inject_irq(kvm_vcpu_context_t vcpu, unsigned irq)
struct kvm_interrupt intr;

intr.irq = irq;
- return ioctl(vcpu->env->kvm_fd, KVM_INTERRUPT, &intr);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_INTERRUPT, &intr);
}

#ifdef KVM_CAP_SET_GUEST_DEBUG
int kvm_set_guest_debug(kvm_vcpu_context_t vcpu, struct kvm_guest_debug *dbg)
{
- return ioctl(vcpu->env->kvm_fd, KVM_SET_GUEST_DEBUG, dbg);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_SET_GUEST_DEBUG, dbg);
}
#endif

@@ -1050,18 +1050,13 @@ int kvm_set_signal_mask(kvm_vcpu_context_t vcpu, const sigset_t *sigset)
int r;

if (!sigset) {
- r = ioctl(vcpu->env->kvm_fd, KVM_SET_SIGNAL_MASK, NULL);
- if (r == -1)
- r = -errno;
- return r;
+ return kvm_vcpu_ioctl(vcpu->env, KVM_SET_SIGNAL_MASK, NULL);
}
sigmask = qemu_malloc(sizeof(*sigmask) + sizeof(*sigset));

sigmask->len = 8;
memcpy(sigmask->sigset, sigset, sizeof(*sigset));
- r = ioctl(vcpu->env->kvm_fd, KVM_SET_SIGNAL_MASK, sigmask);
- if (r == -1)
- r = -errno;
+ r = kvm_vcpu_ioctl(vcpu->env, KVM_SET_SIGNAL_MASK, sigmask);
free(sigmask);
return r;
}
@@ -1078,7 +1073,7 @@ int kvm_has_sync_mmu(void)
int kvm_inject_nmi(kvm_vcpu_context_t vcpu)
{
#ifdef KVM_CAP_USER_NMI
- return ioctl(vcpu->env->kvm_fd, KVM_NMI);
+ return kvm_vcpu_ioctl(vcpu->env, KVM_NMI);
#else
return -ENOSYS;
#endif
diff --git a/qemu-kvm.h b/qemu-kvm.h
index 1dc9873..1fc1e8f 100644
--- a/qemu-kvm.h
+++ b/qemu-kvm.h
@@ -1202,6 +1202,8 @@ extern KVMState *kvm_state;

int kvm_ioctl(KVMState *s, int type, ...);
int kvm_vm_ioctl(KVMState *s, int type, ...);
+int kvm_vcpu_ioctl(CPUState *env, int type, ...);
+
int kvm_check_extension(KVMState *s, unsigned int ext);

#endif
--
1.6.2.2