qemu-kvm cleanups: ioctl merge
v2 cover qemu-kvm cleanups: ioctl merge
Marcelo,
This is a resend of the ioctl series. I'm now changing all call sites to reflect upstream behaviour.
Thanks!
PATCH v2 1/5 remove kvm types from handle unhandled
I'm in an ongoing process of not using kvm-specific types in function declarations. handle_unhandled() is the first victim. Since we don't really use this data, but just the reason, remove them entirely.
--- qemu-kvm.c | 9 +++------ 1 files changed, 3 insertions(+), 6 deletions(-)
qemu-kvm.c +3 -6
@@ -176,8 +176,7 @@ int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t *data, int len)return 0 ; } -static int handle_unhandled (kvm_context_t kvm,kvm_vcpu_context_t vcpu, -uint64_t reason) +static int handle_unhandled (uint64_t reason) {fprintf (stderr,"kvm: unhandled exit %" PRIx64"\n" , reason);return -EINVAL ;
@@ -1085,12 +1084,10 @@ again:if (1 ) {switch (run->exit_reason) {case KVM_EXIT_UNKNOWN : - r =handle_unhandled (kvm, vcpu, - run->hw.hardware_exit_reason); + r =handle_unhandled (run->hw.hardware_exit_reason);break ;case KVM_EXIT_FAIL_ENTRY : - r =handle_unhandled (kvm, vcpu, - run->fail_entry.hardware_entry_failure_reason); + r =handle_unhandled (run->fail_entry.hardware_entry_failure_reason);break ;case KVM_EXIT_EXCEPTION : fprintf(stderr, "exception %d (%x)\n",
Signature · 1 line
1.6.2.2
PATCH v2 2/5 reuse kvm_vm_ioctl
Start using kvm_vm_ioctl's code.
For type safety, delete vm_fd from kvm_context entirely, so the compiler can play along with us helping to detect errors I might have made.
Also, we were slightly different from qemu upstream in handling error code from ioctl, since we were always testing for -1, while kvm_vm_ioctl returns -errno. We already did this in most of the call sites, so this patch has the big advantage of simplifying call sites.
Diffstat says:
4 files changed, 58 insertions(+), 134 deletions(-)
--- kvm-all.c | 2 + qemu-kvm-x86.c | 51 ++++++---------------- qemu-kvm.c | 133 +++++++++++++++++--------------------------------------- qemu-kvm.h | 6 +- 4 files changed, 58 insertions(+), 134 deletions(-)
kvm-all.c +2 -0
@@ -809,6 +809,7 @@ int kvm_ioctl(KVMState *s, int type, ...) return ret; } +#endif int kvm_vm_ioctl (KVMState *s,int type, ...) {
@@ -827,6 +828,7 @@ int kvm_vm_ioctl(KVMState *s, int type, ...)return ret; } +#ifdef KVM_UPSTREAM int kvm_vcpu_ioctl (CPUState *env,int type, ...) {int ret;
qemu-kvm-x86.c +13 -38
116 lines not on this page · raw message
qemu-kvm.c +40 -93
291 lines not on this page · raw message
qemu-kvm.h +3 -3
@@ -54,9 +54,6 @@ extern int kvm_abi;struct kvm_context {/// Filedescriptor to /dev/kvm int fd; -int vm_fd; -/// Callbacks that KVM uses to emulate various unvirtualizable functionality -struct kvm_callbacks *callbacks;void *opaque;/// is dirty pages logging enabled for all regions or not int dirty_pages_log_all;
@@ -1199,5 +1196,8 @@ typedef struct KVMState struct kvm_context kvm_context; } KVMState; +extern KVMState *kvm_state; + +int kvm_vm_ioctl (KVMState *s,int type, ...);#endif
Signature · 1 line
1.6.2.2
PATCH v2 3/5 reuse kvm_ioctl
Start using kvm_ioctl's code.
For type safety, delete fd from kvm_context entirely, so the compiler can play along with us helping to detect errors I might have made.
Also, we were slightly different from qemu upstream in handling error code from ioctl, since we were always testing for -1, while kvm_vm_ioctl returns -errno. We already did this in most of the call sites, so this patch has the big advantage of simplifying call sites.
--- kvm-all.c | 2 +- qemu-kvm-x86.c | 37 +++++++++++++++++-------------------- qemu-kvm.c | 41 ++++++++++++++++++++--------------------- qemu-kvm.h | 3 +-- 4 files changed, 39 insertions(+), 44 deletions(-)
kvm-all.c +1 -1
@@ -793,6 +793,7 @@ void kvm_set_phys_mem(target_phys_addr_t start_addr, } } +#endif int kvm_ioctl (KVMState *s,int type, ...) {int ret;
@@ -809,7 +810,6 @@ int kvm_ioctl(KVMState *s, int type, ...)return ret; } -#endif int kvm_vm_ioctl (KVMState *s,int type, ...) {
qemu-kvm-x86.c +17 -20
@@ -38,7 +38,7 @@ int kvm_set_tss_addr(kvm_context_t kvm, unsigned long addr)#ifdef KVM_CAP_SET_TSS_ADDR int r; - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_SET_TSS_ADDR ); + r =kvm_ioctl (kvm_state,KVM_CHECK_EXTENSION ,KVM_CAP_SET_TSS_ADDR );if (r >0 ) { r =kvm_vm_ioctl (kvm_state,KVM_SET_TSS_ADDR , addr);if (r <0 ) {
@@ -56,7 +56,7 @@ static int kvm_init_tss(kvm_context_t kvm)#ifdef KVM_CAP_SET_TSS_ADDR int r; - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_SET_TSS_ADDR ); + r =kvm_ioctl (kvm_state,KVM_CHECK_EXTENSION ,KVM_CAP_SET_TSS_ADDR );if (r >0 ) {/* * this address is 3 pages before the bios, and the bios should present
@@ -80,7 +80,7 @@ static int kvm_create_pit(kvm_context_t kvm) kvm->pit_in_kernel =0 ;if (!kvm->no_pit_creation) { - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_PIT ); + r =kvm_ioctl (kvm_state,KVM_CHECK_EXTENSION ,KVM_CAP_PIT );if (r >0 ) { r =kvm_vm_ioctl (kvm_state,KVM_CREATE_PIT );if (r >=0 )
@@ -356,11 +356,11 @@ void kvm_show_code(kvm_vcpu_context_t vcpu)struct kvm_msr_list *kvm_get_msr_list (kvm_context_t kvm) {struct kvm_msr_list sizer, *msrs; -int r, e; +int r; sizer.nmsrs =0 ; - r =ioctl (kvm->fd,KVM_GET_MSR_INDEX_LIST , &sizer); -if (r ==-1 && errno !=E2BIG ) + r =kvm_ioctl (kvm_state,KVM_GET_MSR_INDEX_LIST , &sizer); +if (r <0 && r != -E2BIG )return NULL ;/* Old kernel modules had a bug and could write beyond the provided memory. Allocate at least a safe amount of 1K. */
@@ -368,11 +368,10 @@ struct kvm_msr_list *kvm_get_msr_list(kvm_context_t kvm) sizer.nmsrs * sizeof(*msrs->indices))); msrs->nmsrs = sizer.nmsrs; - r =ioctl (kvm->fd,KVM_GET_MSR_INDEX_LIST , msrs); -if (r ==-1 ) { - e = errno; + r =kvm_ioctl (kvm_state,KVM_GET_MSR_INDEX_LIST , msrs); +if (r <0 ) {free (msrs); - errno = e; + errno = r;return NULL ; }return msrs;
@@ -413,10 +412,10 @@ int kvm_get_mce_cap_supported(kvm_context_t kvm, uint64_t *mce_cap,#ifdef KVM_CAP_MCE int r; - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_MCE ); + r =kvm_ioctl (kvm_state,KVM_CHECK_EXTENSION ,KVM_CAP_MCE );if (r >0 ) { *max_banks = r; -return ioctl (kvm->fd,KVM_X86_GET_MCE_CAP_SUPPORTED , mce_cap); +return kvm_ioctl (kvm_state,KVM_X86_GET_MCE_CAP_SUPPORTED , mce_cap); }#endif return -ENOSYS ;
@@ -554,7 +553,7 @@ int kvm_set_shadow_pages(kvm_context_t kvm, unsigned int nrshadow_pages)#ifdef KVM_CAP_MMU_SHADOW_CACHE_CONTROL int r; - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION , + r =kvm_ioctl (kvm_state,KVM_CHECK_EXTENSION ,KVM_CAP_MMU_SHADOW_CACHE_CONTROL );if (r >0 ) { r =kvm_vm_ioctl (kvm_state,KVM_SET_NR_MMU_PAGES , nrshadow_pages);
@@ -573,7 +572,7 @@ int kvm_get_shadow_pages(kvm_context_t kvm, unsigned int *nrshadow_pages)#ifdef KVM_CAP_MMU_SHADOW_CACHE_CONTROL int r; - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION , + r =kvm_ioctl (kvm_state,KVM_CHECK_EXTENSION ,KVM_CAP_MMU_SHADOW_CACHE_CONTROL );if (r >0 ) { *nrshadow_pages =kvm_vm_ioctl (kvm_state,KVM_GET_NR_MMU_PAGES );
@@ -592,8 +591,8 @@ static int tpr_access_reporting(kvm_vcpu_context_t vcpu, int enabled) .enabled = enabled, }; - r =ioctl (vcpu->kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_VAPIC ); -if (r ==-1 || r ==0 ) + r =kvm_ioctl (kvm_state,KVM_CHECK_EXTENSION ,KVM_CAP_VAPIC ); +if (r <=0 )return -ENOSYS ; r =ioctl (vcpu->fd,KVM_TPR_ACCESS_REPORTING , &tac);if (r ==-1 ) {
@@ -626,10 +625,8 @@ static struct kvm_cpuid2 *try_get_cpuid(kvm_context_t kvm, int max) size =sizeof (*cpuid) + max *sizeof (*cpuid->entries); cpuid =qemu_malloc (size); cpuid->nent = max; - r =ioctl (kvm->fd,KVM_GET_SUPPORTED_CPUID , cpuid); -if (r ==-1 ) - r = -errno; -else if (r ==0 && cpuid->nent >= max) + r =kvm_ioctl (kvm_state,KVM_GET_SUPPORTED_CPUID , cpuid); +if (r ==0 && cpuid->nent >= max) r = -E2BIG ;if (r <0 ) {if (r == -E2BIG ) {
qemu-kvm.c +20 -21
147 lines not on this page · raw message
qemu-kvm.h +1 -2
@@ -52,8 +52,6 @@ extern int kvm_abi;*/ struct kvm_context { -/// Filedescriptor to /dev/kvm -int fd;void *opaque;/// is dirty pages logging enabled for all regions or not int dirty_pages_log_all;
@@ -1198,6 +1196,7 @@ typedef struct KVMStateextern KVMState *kvm_state; +int kvm_ioctl (KVMState *s,int type, ...);int kvm_vm_ioctl (KVMState *s,int type, ...);#endif
Signature · 1 line
1.6.2.2
PATCH v2 4/5 check extension
use upstream check_extension code
--- hw/device-assignment.c | 2 +- kvm-all.c | 2 ++ qemu-kvm-x86.c | 6 +++--- qemu-kvm.c | 18 ++++-------------- qemu-kvm.h | 2 +- 5 files changed, 11 insertions(+), 19 deletions(-)
hw/device-assignment.c +1 -1
@@ -639,7 +639,7 @@ static int assign_device(AssignedDevInfo *adev)/* We always enable the IOMMU if present * (or when not disabled on the command line) */ - r =kvm_check_extension (kvm_context,KVM_CAP_IOMMU ); + r =kvm_check_extension (kvm_state,KVM_CAP_IOMMU );if (r && !adev->disable_iommu) assigned_dev_data.flags |=KVM_DEV_ASSIGN_ENABLE_IOMMU ;#endif
kvm-all.c +2 -0
@@ -383,6 +383,7 @@ int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size) return ret; } +#endif int kvm_check_extension (KVMState *s,unsigned int extension) {int ret;
@@ -394,6 +395,7 @@ int kvm_check_extension(KVMState *s, unsigned int extension)return ret; } +#ifdef KVM_UPSTREAM int kvm_init (int smp_cpus) {
qemu-kvm-x86.c +3 -3
@@ -303,7 +303,7 @@ int kvm_has_pit_state2(kvm_context_t kvm)int r =0 ;#ifdef KVM_CAP_PIT_STATE2 - r =kvm_check_extension (kvm,KVM_CAP_PIT_STATE2 ); + r =kvm_check_extension (kvm_state,KVM_CAP_PIT_STATE2 );#endif return r; }
@@ -657,7 +657,7 @@ uint32_t kvm_get_supported_cpuid(kvm_context_t kvm, uint32_t function, int reg)uint32_t ret =0 ;uint32_t cpuid_1_edx; -if (!kvm_check_extension (kvm,KVM_CAP_EXT_CPUID )) { +if (!kvm_check_extension (kvm_state,KVM_CAP_EXT_CPUID )) {return -1U ; }
@@ -1189,7 +1189,7 @@ static int get_para_features(kvm_context_t kvm_context)int i, features =0 ;for (i =0 ; i <ARRAY_SIZE (para_features)-1 ; i++) { -if (kvm_check_extension (kvm_context, para_features[i].cap)) +if (kvm_check_extension (kvm_state, para_features[i].cap)) features |= (1 << para_features[i].feature); }
qemu-kvm.c +4 -14
@@ -589,16 +589,6 @@ static int kvm_create_default_phys_mem(kvm_context_t kvm,return -1 ; } -int kvm_check_extension (kvm_context_t kvm,int ext) -{ -int ret; - - ret =kvm_ioctl (kvm_state,KVM_CHECK_EXTENSION , ext); -if (ret >0 ) -return ret; -return 0 ; -} -void kvm_create_irqchip (kvm_context_t kvm) {int r;
@@ -1345,7 +1335,7 @@ int kvm_has_gsi_routing(kvm_context_t kvm)int r =0 ;#ifdef KVM_CAP_IRQ_ROUTING - r =kvm_check_extension (kvm,KVM_CAP_IRQ_ROUTING ); + r =kvm_check_extension (kvm_state,KVM_CAP_IRQ_ROUTING );#endif return r; }
@@ -1353,7 +1343,7 @@ int kvm_has_gsi_routing(kvm_context_t kvm)int kvm_get_gsi_count (kvm_context_t kvm) {#ifdef KVM_CAP_IRQ_ROUTING -return kvm_check_extension (kvm,KVM_CAP_IRQ_ROUTING ); +return kvm_check_extension (kvm_state,KVM_CAP_IRQ_ROUTING );#else return -EINVAL ;#endif
@@ -1606,7 +1596,7 @@ int kvm_irqfd(kvm_context_t kvm, int gsi, int flags)int r;int fd; -if (!kvm_check_extension (kvm,KVM_CAP_IRQFD )) +if (!kvm_check_extension (kvm_state,KVM_CAP_IRQFD ))return -ENOENT ; fd =eventfd (0 ,0 );
@@ -2381,7 +2371,7 @@ int kvm_setup_guest_memory(void *area, unsigned long size)int kvm_qemu_check_extension (int ext) { -return kvm_check_extension (kvm_context, ext); +return kvm_check_extension (kvm_state, ext); }int kvm_qemu_init_env (CPUState *cenv)
qemu-kvm.h +1 -1
@@ -167,7 +167,6 @@ int kvm_create(kvm_context_t kvm,unsigned long phys_mem_bytes, void **phys_mem);int kvm_create_vm (kvm_context_t kvm); -int kvm_check_extension (kvm_context_t kvm,int ext);void kvm_create_irqchip (kvm_context_t kvm);/*!
@@ -1198,5 +1197,6 @@ extern KVMState *kvm_state;int kvm_ioctl (KVMState *s,int type, ...);int kvm_vm_ioctl (KVMState *s,int type, ...); +int kvm_check_extension (KVMState *s,unsigned int ext);#endif
Signature · 1 line
1.6.2.2
PATCH v2 5/5 use upstream cpuid code
use cpuid code from upstream. By doing that, we lose the following snippet in kvm_get_supported_cpuid():
ret |= 1 << 12; /* MTRR */
ret |= 1 << 16; /* PAT */
ret |= 1 << 7; /* MCE */
ret |= 1 << 14; /* MCA */A quick search in mailing lists says this code is not really necessary, and we're keeping it just for backwards compatibility. This is not that important, because we'd lose it anyway in the golden day in which we totally merge with qemu. Anyway, if it do _is_ important, we can send a patch to qemu with it.
--- qemu-kvm-x86.c | 119 ----------------------------------------------------- target-i386/kvm.c | 2 + 2 files changed, 2 insertions(+), 119 deletions(-)
qemu-kvm-x86.c +0 -119
@@ -615,106 +615,6 @@ int kvm_disable_tpr_access_reporting(kvm_vcpu_context_t vcpu)#endif -#ifdef KVM_CAP_EXT_CPUID - -static struct kvm_cpuid2 *try_get_cpuid (kvm_context_t kvm,int max) -{ -struct kvm_cpuid2 *cpuid; -int r, size; - - size =sizeof (*cpuid) + max *sizeof (*cpuid->entries); - cpuid =qemu_malloc (size); - cpuid->nent = max; - r =kvm_ioctl (kvm_state,KVM_GET_SUPPORTED_CPUID , cpuid); -if (r ==0 && cpuid->nent >= max) - r = -E2BIG ; -if (r <0 ) { -if (r == -E2BIG ) { -free (cpuid); -return NULL ; - }else { -fprintf (stderr,"KVM_GET_SUPPORTED_CPUID failed: %s\n" , -strerror (-r)); -exit (1 ); - } - } -return cpuid; -} - -#define R_EAX 0 -#define R_ECX 1 -#define R_EDX 2 -#define R_EBX 3 -#define R_ESP 4 -#define R_EBP 5 -#define R_ESI 6 -#define R_EDI 7 - -uint32_t kvm_get_supported_cpuid (kvm_context_t kvm,uint32_t function,int reg) -{ -struct kvm_cpuid2 *cpuid; -int i, max; -uint32_t ret =0 ; -uint32_t cpuid_1_edx; - -if (!kvm_check_extension (kvm_state,KVM_CAP_EXT_CPUID )) { -return -1U ; - } - - max =1 ; -while ((cpuid =try_get_cpuid (kvm, max)) ==NULL ) { - max *=2 ; - } - -for (i =0 ; i < cpuid->nent; ++i) { -if (cpuid->entries[i].function == function) { -switch (reg) { -case R_EAX : - ret = cpuid->entries[i].eax; -break ; -case R_EBX : - ret = cpuid->entries[i].ebx; -break ; -case R_ECX : - ret = cpuid->entries[i].ecx; -break ; -case R_EDX : - ret = cpuid->entries[i].edx; -if (function ==1 ) { -/* kvm misreports the following features -*/ - ret |=1 <<12 ;/* MTRR */ - ret |=1 <<16 ;/* PAT */ - ret |=1 <<7 ;/* MCE */ - ret |=1 <<14 ;/* MCA */ - } - -/* On Intel, kvm returns cpuid according to -* the Intel spec, so add missing bits -* according to the AMD spec: -*/ -if (function ==0x80000001 ) { - cpuid_1_edx =kvm_get_supported_cpuid (kvm,1 ,R_EDX ); - ret |= cpuid_1_edx &0xdfeff7ff ; - } -break ; - } - } - } - -free (cpuid); - -return ret; -} - -#else - -uint32_t kvm_get_supported_cpuid (kvm_context_t kvm,uint32_t function,int reg) -{ -return -1U ; -} - -#endif int kvm_qemu_create_memory_alias (uint64_t phys_start,uint64_t len,uint64_t target_phys)
@@ -1196,19 +1096,6 @@ static int get_para_features(kvm_context_t kvm_context)return features; } -static void kvm_trim_features (uint32_t *features,uint32_t supported) -{ -int i; -uint32_t mask; - -for (i =0 ; i <32 ; ++i) { - mask =1U << i; -if ((*features & mask) && !(supported & mask)) { - *features &= ~mask; - } - } -} -int kvm_arch_qemu_init_env (CPUState *cenv) {struct kvm_cpuid_entry2 cpuid_ent[100 ];
@@ -1626,12 +1513,6 @@ int kvm_arch_init_irq_routing(void)return 0 ; } -uint32_t kvm_arch_get_supported_cpuid (CPUState *env,uint32_t function, -int reg) -{ -return kvm_get_supported_cpuid (kvm_context, function, reg); -} -void kvm_arch_process_irqchip_events (CPUState *env) {kvm_arch_save_regs (env);
target-i386/kvm.c +2 -0
@@ -35,6 +35,7 @@ do { } while (0)#endif +#endif #ifdef KVM_CAP_EXT_CPUID static struct kvm_cpuid2 *try_get_cpuid (KVMState *s,int max)
@@ -130,6 +131,7 @@ static void kvm_trim_features(uint32_t *features, uint32_t supported) } } } +#ifdef KVM_UPSTREAM int kvm_arch_init_vcpu (CPUState *env) {
Signature · 1 line
1.6.2.2