kvm tools: Clean up LINT assignment code
PATCH 1/1 kvm tools: Clean up LINT assignment code
Just set delivery mode directly without going through ugly casting.
This cleans up and simplifies the code.
--- tools/kvm/x86/kvm-cpu.c | 10 ++-------- 1 files changed, 2 insertions(+), 8 deletions(-)
tools/kvm/x86/kvm-cpu.c +2 -8
@@ -81,18 +81,12 @@ static int kvm_cpu__set_lint(struct kvm_cpu *vcpu) {struct kvm_lapic_state klapic;struct local_apic *lapic = (void *)&klapic; -u32 lvt;if (ioctl (vcpu->vcpu_fd,KVM_GET_LAPIC , &klapic))return -1 ; - lvt = *(u32 *)&lapic->lvt_lint0; - lvt =SET_APIC_DELIVERY_MODE (lvt,APIC_MODE_EXTINT ); - *(u32 *)&lapic->lvt_lint0 = lvt; - - lvt = *(u32 *)&lapic->lvt_lint1; - lvt =SET_APIC_DELIVERY_MODE (lvt,APIC_MODE_NMI ); - *(u32 *)&lapic->lvt_lint1 = lvt; + lapic->lvt_lint0.delivery_mode =APIC_MODE_EXTINT ; + lapic->lvt_lint1.delivery_mode =APIC_MODE_NMI ;return ioctl (vcpu->vcpu_fd,KVM_SET_LAPIC , &klapic); }
Signature · 1 line
1.7.8
Hi Sasha,
Quoted 24 lines · Sasha Levin
Just set delivery mode directly without going through ugly casting.
This cleans up and simplifies the code.
Signed-off-by Sasha Levin <[email protected]>--- tools/kvm/x86/kvm-cpu.c | 10 ++-------- 1 files changed, 2 insertions(+), 8 deletions(-)tools/kvm/x86/kvm-cpu.c +0
-3@@ -81,18 +81,12 @@ static int kvm_cpu__set_lint(struct kvm_cpu *vcpu) {struct kvm_lapic_state klapic;struct local_apic *lapic = (void *)&klapic; -u32 lvt;if (ioctl (vcpu->vcpu_fd,KVM_GET_LAPIC , &klapic))return -1 ; - lvt = *(u32 *)&lapic->lvt_lint0; - lvt =SET_APIC_DELIVERY_MODE (lvt,APIC_MODE_EXTINT );
tools/kvm/x86/kvm-cpu.c static int kvm_cpu__set_lint(struct kvm_cpu *vcpu) · line 92
Quoted 10 lines
tools/kvm/x86/kvm-cpu.c +2
-5- *(u32 *)&lapic->lvt_lint0 = lvt; - - lvt = *(u32 *)&lapic->lvt_lint1; - lvt =SET_APIC_DELIVERY_MODE (lvt,APIC_MODE_NMI ); - *(u32 *)&lapic->lvt_lint1 = lvt; + lapic->lvt_lint0.delivery_mode =APIC_MODE_EXTINT ; + lapic->lvt_lint1.delivery_mode =APIC_MODE_NMI ;return ioctl (vcpu->vcpu_fd,KVM_SET_LAPIC , &klapic); }
I'm getting this on x86-32, gcc 4.4.3:
CC x86/kvm-cpu.o
cc1: warnings being treated as errorsx86/kvm-cpu.c: In function ‘kvm_cpu__set_lint’:x86/kvm-cpu.c:89: error: dereferencing pointer ‘lapic’ does break strict-aliasing rules x86/kvm-cpu.c:88: error: dereferencing pointer ‘lapic’ does break strict-aliasing rules x86/kvm-cpu.c:83: note: initialized from here
make: *** [x86/kvm-cpu.o] Error 1
Removing the nasty aliasing (patch below) seems to be a good way to go. What do you think?
Cheers,
Matt
---
tools/kvm/x86/kvm-cpu.c +5 -6
@@ -79,16 +79,15 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu)static int kvm_cpu__set_lint (struct kvm_cpu *vcpu) { -struct kvm_lapic_state klapic; -struct local_apic *lapic = (void *)&klapic; +struct local_apic lapic; -if (ioctl (vcpu->vcpu_fd,KVM_GET_LAPIC , &klapic)) +if (ioctl (vcpu->vcpu_fd,KVM_GET_LAPIC , &lapic))return -1 ; - lapic->lvt_lint0.delivery_mode =APIC_MODE_EXTINT ; - lapic->lvt_lint1.delivery_mode =APIC_MODE_NMI ; + lapic.lvt_lint0.delivery_mode =APIC_MODE_EXTINT ; + lapic.lvt_lint1.delivery_mode =APIC_MODE_NMI ; -return ioctl (vcpu->vcpu_fd,KVM_SET_LAPIC , &klapic); +return ioctl (vcpu->vcpu_fd,KVM_SET_LAPIC , &lapic); }struct kvm_cpu *kvm_cpu__init (struct kvm *kvm,unsigned long cpu_id)
Sasha Levin
Acked-byQuoted 64 lines · Matt Evans
Hi Sasha,
Quoted 24 lines · Sasha Levin
Just set delivery mode directly without going through ugly casting.
This cleans up and simplifies the code.
Signed-off-by Sasha Levin <[email protected]>--- tools/kvm/x86/kvm-cpu.c | 10 ++-------- 1 files changed, 2 insertions(+), 8 deletions(-)tools/kvm/x86/kvm-cpu.c +0
-3@@ -81,18 +81,12 @@ static int kvm_cpu__set_lint(struct kvm_cpu *vcpu) {struct kvm_lapic_state klapic;struct local_apic *lapic = (void *)&klapic; -u32 lvt;if (ioctl (vcpu->vcpu_fd,KVM_GET_LAPIC , &klapic))return -1 ; - lvt = *(u32 *)&lapic->lvt_lint0; - lvt =SET_APIC_DELIVERY_MODE (lvt,APIC_MODE_EXTINT );tools/kvm/x86/kvm-cpu.c static int kvm_cpu__set_lint(struct kvm_cpu *vcpu) · line 92
Quoted 10 lines
tools/kvm/x86/kvm-cpu.c +2
-5- *(u32 *)&lapic->lvt_lint0 = lvt; - - lvt = *(u32 *)&lapic->lvt_lint1; - lvt =SET_APIC_DELIVERY_MODE (lvt,APIC_MODE_NMI ); - *(u32 *)&lapic->lvt_lint1 = lvt; + lapic->lvt_lint0.delivery_mode =APIC_MODE_EXTINT ; + lapic->lvt_lint1.delivery_mode =APIC_MODE_NMI ;return ioctl (vcpu->vcpu_fd,KVM_SET_LAPIC , &klapic); }I'm getting this on x86-32, gcc 4.4.3:
CC
x86/kvm-cpu.o
cc1: warnings being treated as errorsx86/kvm-cpu.c: In function ‘kvm_cpu__set_lint’:x86/kvm-cpu.c:89: error: dereferencing pointer ‘lapic’ does break strict-aliasing rulesx86/kvm-cpu.c:88: error: dereferencing pointer ‘lapic’ does break strict-aliasing rulesx86/kvm-cpu.c:83: note: initialized from here
make: *** [x86/kvm-cpu.o] Error 1Removing the nasty aliasing (patch below) seems to be a good way to go. What do you think?
Cheers,
Matt
---
tools/kvm/x86/kvm-cpu.c +0
-0
tools/kvm/x86/kvm-cpu.c void kvm_cpu__delete(struct kvm_cpu *vcpu) · line 85
Quoted 10 lines
tools/kvm/x86/kvm-cpu.c +2
-3@@ -79,16 +79,15 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu)static int kvm_cpu__set_lint (struct kvm_cpu *vcpu) { -struct kvm_lapic_state klapic; -struct local_apic *lapic = (void *)&klapic; +struct local_apic lapic; -if (ioctl (vcpu->vcpu_fd,KVM_GET_LAPIC , &klapic)) +if (ioctl (vcpu->vcpu_fd,KVM_GET_LAPIC , &lapic))return -1 ;
It felt a bit wrong to me getting the ioctl to assign it directly to local_apic since api.txt says it should be returning a kvm_lapic_state. But on the other hand, api.txt also says "The data format and layout are the same as documented in the architecture manual." so I guess we can go with that.
Signature · 1 line
Sasha.
Quoted 82 lines · Sasha Levin
Quoted 64 lines · Matt Evans
Hi Sasha,
Quoted 24 lines · Sasha Levin
Just set delivery mode directly without going through ugly casting.
This cleans up and simplifies the code.
Signed-off-by Sasha Levin <[email protected]>---
tools/kvm/x86/kvm-cpu.c| 10 ++--------1 files changed, 2 insertions(+), 8 deletions(-)tools/kvm/x86/kvm-cpu.c +0
-3@@ -81,18 +81,12 @@ static int kvm_cpu__set_lint(struct kvm_cpu *vcpu) {struct kvm_lapic_state klapic;struct local_apic *lapic = (void *)&klapic; -u32 lvt;if (ioctl (vcpu->vcpu_fd,KVM_GET_LAPIC , &klapic))return -1 ; - lvt = *(u32 *)&lapic->lvt_lint0; - lvt =SET_APIC_DELIVERY_MODE (lvt,APIC_MODE_EXTINT );tools/kvm/x86/kvm-cpu.c static int kvm_cpu__set_lint(struct kvm_cpu *vcpu) · line 92
Quoted 10 lines
tools/kvm/x86/kvm-cpu.c +2
-5- *(u32 *)&lapic->lvt_lint0 = lvt; - - lvt = *(u32 *)&lapic->lvt_lint1; - lvt =SET_APIC_DELIVERY_MODE (lvt,APIC_MODE_NMI ); - *(u32 *)&lapic->lvt_lint1 = lvt; + lapic->lvt_lint0.delivery_mode =APIC_MODE_EXTINT ; + lapic->lvt_lint1.delivery_mode =APIC_MODE_NMI ;return ioctl (vcpu->vcpu_fd,KVM_SET_LAPIC , &klapic); }I'm getting this on x86-32, gcc 4.4.3:
CC
x86/kvm-cpu.o
cc1: warnings being treated as errorsx86/kvm-cpu.c: In function ‘kvm_cpu__set_lint’:x86/kvm-cpu.c:89: error: dereferencing pointer ‘lapic’ does break strict-aliasing rulesx86/kvm-cpu.c:88: error: dereferencing pointer ‘lapic’ does break strict-aliasing rulesx86/kvm-cpu.c:83: note: initialized from here
make: *** [x86/kvm-cpu.o] Error 1Removing the nasty aliasing (patch below) seems to be a good way to go. What do you think?
Cheers,
Matt
---
tools/kvm/x86/kvm-cpu.c +0
-0tools/kvm/x86/kvm-cpu.c void kvm_cpu__delete(struct kvm_cpu *vcpu) · line 85
Quoted 10 lines
tools/kvm/x86/kvm-cpu.c +2
-3@@ -79,16 +79,15 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu)static int kvm_cpu__set_lint (struct kvm_cpu *vcpu) { -struct kvm_lapic_state klapic; -struct local_apic *lapic = (void *)&klapic; +struct local_apic lapic; -if (ioctl (vcpu->vcpu_fd,KVM_GET_LAPIC , &klapic)) +if (ioctl (vcpu->vcpu_fd,KVM_GET_LAPIC , &lapic))return -1 ;It felt a bit wrong to me getting the ioctl to assign it directly to
local_apicsince api.txt says it should be returning akvm_lapic_state. But on the other hand, api.txt also says "The data format and layout are the same as documented in the architecture manual." so I guess we can go with that.
Quoted 1 line
Acked-by Sasha Levin <[email protected]>
Pekka, as I forgot the SOB:
(Or would you prefer a repost so it's all in one?)
Matt