Skip to content

Commit 40da8cc

Browse files
committed
KVM: x86/xen: Add event channel interrupt vector upcall
It turns out that we can't handle event channels *entirely* in userspace by delivering them as ExtINT, because KVM is a bit picky about when it accepts ExtINT interrupts from a legacy PIC. The in-kernel local APIC has to have LVT0 configured in APIC_MODE_EXTINT and unmasked, which isn't necessarily the case for Xen guests especially on secondary CPUs. To cope with this, add kvm_xen_get_interrupt() which checks the evtchn_pending_upcall field in the Xen vcpu_info, and delivers the Xen upcall vector (configured by KVM_XEN_ATTR_TYPE_UPCALL_VECTOR) if it's set regardless of LAPIC LVT0 configuration. This gives us the minimum support we need for completely userspace-based implementation of event channels. This does mean that vcpu_enter_guest() needs to check for the evtchn_pending_upcall flag being set, because it can't rely on someone having set KVM_REQ_EVENT unless we were to add some way for userspace to do so manually. Signed-off-by: David Woodhouse <[email protected]>
1 parent f2340cd commit 40da8cc

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ struct msr_bitmap_range {
913913
struct kvm_xen {
914914
bool long_mode;
915915
bool shinfo_set;
916+
u8 upcall_vector;
916917
struct gfn_to_hva_cache shinfo_cache;
917918
};
918919

arch/x86/kvm/irq.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "irq.h"
1515
#include "i8254.h"
1616
#include "x86.h"
17+
#include "xen.h"
1718

1819
/*
1920
* check if there are pending timer events
@@ -56,6 +57,9 @@ int kvm_cpu_has_extint(struct kvm_vcpu *v)
5657
if (!lapic_in_kernel(v))
5758
return v->arch.interrupt.injected;
5859

60+
if (kvm_xen_has_interrupt(v))
61+
return 1;
62+
5963
if (!kvm_apic_accept_pic_intr(v))
6064
return 0;
6165

@@ -110,6 +114,9 @@ static int kvm_cpu_get_extint(struct kvm_vcpu *v)
110114
if (!lapic_in_kernel(v))
111115
return v->arch.interrupt.nr;
112116

117+
if (kvm_xen_has_interrupt(v))
118+
return v->kvm->arch.xen.upcall_vector;
119+
113120
if (irqchip_split(v->kvm)) {
114121
int vector = v->arch.pending_external_vector;
115122

arch/x86/kvm/x86.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8987,7 +8987,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
89878987
static_call(kvm_x86_msr_filter_changed)(vcpu);
89888988
}
89898989

8990-
if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
8990+
if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win ||
8991+
kvm_xen_has_interrupt(vcpu)) {
89918992
++vcpu->stat.req_event;
89928993
kvm_apic_accept_events(vcpu);
89938994
if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {

arch/x86/kvm/xen.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,44 @@ static int kvm_xen_shared_info_init(struct kvm *kvm, gfn_t gfn)
6161
return ret;
6262
}
6363

64+
int __kvm_xen_has_interrupt(struct kvm_vcpu *v)
65+
{
66+
u8 rc = 0;
67+
68+
/*
69+
* If the global upcall vector (HVMIRQ_callback_vector) is set and
70+
* the vCPU's evtchn_upcall_pending flag is set, the IRQ is pending.
71+
*/
72+
struct gfn_to_hva_cache *ghc = &v->arch.xen.vcpu_info_cache;
73+
struct kvm_memslots *slots = kvm_memslots(v->kvm);
74+
unsigned int offset = offsetof(struct vcpu_info, evtchn_upcall_pending);
75+
76+
/* No need for compat handling here */
77+
BUILD_BUG_ON(offsetof(struct vcpu_info, evtchn_upcall_pending) !=
78+
offsetof(struct compat_vcpu_info, evtchn_upcall_pending));
79+
BUILD_BUG_ON(sizeof(rc) !=
80+
sizeof(((struct vcpu_info *)0)->evtchn_upcall_pending));
81+
BUILD_BUG_ON(sizeof(rc) !=
82+
sizeof(((struct compat_vcpu_info *)0)->evtchn_upcall_pending));
83+
84+
/*
85+
* For efficiency, this mirrors the checks for using the valid
86+
* cache in kvm_read_guest_offset_cached(), but just uses
87+
* __get_user() instead. And falls back to the slow path.
88+
*/
89+
if (likely(slots->generation == ghc->generation &&
90+
!kvm_is_error_hva(ghc->hva) && ghc->memslot)) {
91+
/* Fast path */
92+
__get_user(rc, (u8 __user *)ghc->hva + offset);
93+
} else {
94+
/* Slow path */
95+
kvm_read_guest_offset_cached(v->kvm, ghc, &rc, offset,
96+
sizeof(rc));
97+
}
98+
99+
return rc;
100+
}
101+
64102
int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
65103
{
66104
int r = -ENOENT;
@@ -83,6 +121,16 @@ int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
83121
r = kvm_xen_shared_info_init(kvm, data->u.shared_info.gfn);
84122
break;
85123

124+
125+
case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
126+
if (data->u.vector < 0x10)
127+
r = -EINVAL;
128+
else {
129+
kvm->arch.xen.upcall_vector = data->u.vector;
130+
r = 0;
131+
}
132+
break;
133+
86134
default:
87135
break;
88136
}
@@ -110,6 +158,11 @@ int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
110158
}
111159
break;
112160

161+
case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
162+
data->u.vector = kvm->arch.xen.upcall_vector;
163+
r = 0;
164+
break;
165+
113166
default:
114167
break;
115168
}

arch/x86/kvm/xen.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
extern struct static_key_false_deferred kvm_xen_enabled;
1515

16+
int __kvm_xen_has_interrupt(struct kvm_vcpu *vcpu);
1617
int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
1718
int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
1819
int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
@@ -29,6 +30,14 @@ static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
2930
KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL);
3031
}
3132

33+
static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
34+
{
35+
if (static_branch_unlikely(&kvm_xen_enabled.key) &&
36+
vcpu->arch.xen.vcpu_info_set && vcpu->kvm->arch.xen.upcall_vector)
37+
return __kvm_xen_has_interrupt(vcpu);
38+
39+
return 0;
40+
}
3241

3342
/* 32-bit compatibility definitions, also used natively in 32-bit build */
3443
#include <asm/pvclock-abi.h>

include/uapi/linux/kvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,7 @@ struct kvm_xen_hvm_attr {
15961596
__u16 pad[3];
15971597
union {
15981598
__u8 long_mode;
1599+
__u8 vector;
15991600
struct {
16001601
__u64 gfn;
16011602
} shared_info;
@@ -1605,6 +1606,7 @@ struct kvm_xen_hvm_attr {
16051606

16061607
#define KVM_XEN_ATTR_TYPE_LONG_MODE 0x0
16071608
#define KVM_XEN_ATTR_TYPE_SHARED_INFO 0x1
1609+
#define KVM_XEN_ATTR_TYPE_UPCALL_VECTOR 0x2
16081610

16091611
/* Per-vCPU Xen attributes */
16101612
#define KVM_XEN_VCPU_GET_ATTR _IOWR(KVMIO, 0xca, struct kvm_xen_vcpu_attr)

0 commit comments

Comments
 (0)