Skip to content

Commit 38e20b0

Browse files
Sheng YangJeremy Fitzhardinge
authored andcommitted
x86/xen: event channels delivery on HVM.
Set the callback to receive evtchns from Xen, using the callback vector delivery mechanism. The traditional way for receiving event channel notifications from Xen is via the interrupts from the platform PCI device. The callback vector is a newer alternative that allow us to receive notifications on any vcpu and doesn't need any PCI support: we allocate a vector exclusively to receive events, in the vector handler we don't need to interact with the vlapic, therefore we avoid a VMEXIT. Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> Signed-off-by: Sheng Yang <sheng@linux.intel.com> Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com>
1 parent bee6ab5 commit 38e20b0

9 files changed

Lines changed: 118 additions & 7 deletions

File tree

‎arch/x86/include/asm/irq_vectors.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@
125125
*/
126126
#define MCE_SELF_VECTOR 0xeb
127127

128+
/* Xen vector callback to receive events in a HVM domain */
129+
#define XEN_HVM_EVTCHN_CALLBACK 0xe9
130+
128131
#define NR_VECTORS 256
129132

130133
#define FPU_IRQ 13

‎arch/x86/kernel/entry_32.S‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,9 @@ ENTRY(xen_failsafe_callback)
11661166
.previous
11671167
ENDPROC(xen_failsafe_callback)
11681168

1169+
BUILD_INTERRUPT3(xen_hvm_callback_vector, XEN_HVM_EVTCHN_CALLBACK,
1170+
xen_evtchn_do_upcall)
1171+
11691172
#endif /* CONFIG_XEN */
11701173

11711174
#ifdef CONFIG_FUNCTION_TRACER

‎arch/x86/kernel/entry_64.S‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,9 @@ ENTRY(xen_failsafe_callback)
13291329
CFI_ENDPROC
13301330
END(xen_failsafe_callback)
13311331

1332+
apicinterrupt XEN_HVM_EVTCHN_CALLBACK \
1333+
xen_hvm_callback_vector xen_evtchn_do_upcall
1334+
13321335
#endif /* CONFIG_XEN */
13331336

13341337
/*

‎arch/x86/xen/enlighten.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
1212
*/
1313

14+
#include <linux/cpu.h>
1415
#include <linux/kernel.h>
1516
#include <linux/init.h>
1617
#include <linux/smp.h>
@@ -38,6 +39,7 @@
3839
#include <xen/interface/memory.h>
3940
#include <xen/features.h>
4041
#include <xen/page.h>
42+
#include <xen/hvm.h>
4143
#include <xen/hvc-console.h>
4244

4345
#include <asm/paravirt.h>
@@ -80,6 +82,8 @@ struct shared_info xen_dummy_shared_info;
8082
void *xen_initial_gdt;
8183

8284
RESERVE_BRK(shared_info_page_brk, PAGE_SIZE);
85+
__read_mostly int xen_have_vector_callback;
86+
EXPORT_SYMBOL_GPL(xen_have_vector_callback);
8387

8488
/*
8589
* Point at some empty memory to start with. We map the real shared_info
@@ -1277,6 +1281,24 @@ static void __init init_shared_info(void)
12771281
per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
12781282
}
12791283

1284+
static int __cpuinit xen_hvm_cpu_notify(struct notifier_block *self,
1285+
unsigned long action, void *hcpu)
1286+
{
1287+
int cpu = (long)hcpu;
1288+
switch (action) {
1289+
case CPU_UP_PREPARE:
1290+
per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
1291+
break;
1292+
default:
1293+
break;
1294+
}
1295+
return NOTIFY_OK;
1296+
}
1297+
1298+
static struct notifier_block __cpuinitdata xen_hvm_cpu_notifier = {
1299+
.notifier_call = xen_hvm_cpu_notify,
1300+
};
1301+
12801302
static void __init xen_hvm_guest_init(void)
12811303
{
12821304
int r;
@@ -1287,6 +1309,12 @@ static void __init xen_hvm_guest_init(void)
12871309
return;
12881310

12891311
init_shared_info();
1312+
1313+
if (xen_feature(XENFEAT_hvm_callback_vector))
1314+
xen_have_vector_callback = 1;
1315+
register_cpu_notifier(&xen_hvm_cpu_notifier);
1316+
have_vcpu_info_placement = 0;
1317+
x86_init.irqs.intr_init = xen_init_IRQ;
12901318
}
12911319

12921320
static bool __init xen_hvm_platform(void)

‎arch/x86/xen/xen-ops.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ void xen_enable_sysenter(void);
3838
void xen_enable_syscall(void);
3939
void xen_vcpu_restore(void);
4040

41+
void xen_callback_vector(void);
42+
4143
void __init xen_build_dynamic_phys_to_machine(void);
4244

4345
void xen_init_irq_ops(void);

‎drivers/xen/events.c‎

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,22 @@
2929
#include <linux/bootmem.h>
3030
#include <linux/slab.h>
3131

32+
#include <asm/desc.h>
3233
#include <asm/ptrace.h>
3334
#include <asm/irq.h>
3435
#include <asm/idle.h>
3536
#include <asm/sync_bitops.h>
3637
#include <asm/xen/hypercall.h>
3738
#include <asm/xen/hypervisor.h>
3839

40+
#include <xen/xen.h>
41+
#include <xen/hvm.h>
3942
#include <xen/xen-ops.h>
4043
#include <xen/events.h>
4144
#include <xen/interface/xen.h>
4245
#include <xen/interface/event_channel.h>
46+
#include <xen/interface/hvm/hvm_op.h>
47+
#include <xen/interface/hvm/params.h>
4348

4449
/*
4550
* This lock protects updates to the following mapping and reference-count
@@ -617,17 +622,13 @@ static DEFINE_PER_CPU(unsigned, xed_nesting_count);
617622
* a bitset of words which contain pending event bits. The second
618623
* level is a bitset of pending events themselves.
619624
*/
620-
void xen_evtchn_do_upcall(struct pt_regs *regs)
625+
static void __xen_evtchn_do_upcall(void)
621626
{
622627
int cpu = get_cpu();
623-
struct pt_regs *old_regs = set_irq_regs(regs);
624628
struct shared_info *s = HYPERVISOR_shared_info;
625629
struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
626630
unsigned count;
627631

628-
exit_idle();
629-
irq_enter();
630-
631632
do {
632633
unsigned long pending_words;
633634

@@ -667,10 +668,26 @@ void xen_evtchn_do_upcall(struct pt_regs *regs)
667668
} while(count != 1);
668669

669670
out:
671+
672+
put_cpu();
673+
}
674+
675+
void xen_evtchn_do_upcall(struct pt_regs *regs)
676+
{
677+
struct pt_regs *old_regs = set_irq_regs(regs);
678+
679+
exit_idle();
680+
irq_enter();
681+
682+
__xen_evtchn_do_upcall();
683+
670684
irq_exit();
671685
set_irq_regs(old_regs);
686+
}
672687

673-
put_cpu();
688+
void xen_hvm_evtchn_do_upcall(void)
689+
{
690+
__xen_evtchn_do_upcall();
674691
}
675692

676693
/* Rebind a new event channel to an existing irq. */
@@ -933,6 +950,40 @@ static struct irq_chip xen_dynamic_chip __read_mostly = {
933950
.retrigger = retrigger_dynirq,
934951
};
935952

953+
int xen_set_callback_via(uint64_t via)
954+
{
955+
struct xen_hvm_param a;
956+
a.domid = DOMID_SELF;
957+
a.index = HVM_PARAM_CALLBACK_IRQ;
958+
a.value = via;
959+
return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
960+
}
961+
EXPORT_SYMBOL_GPL(xen_set_callback_via);
962+
963+
/* Vector callbacks are better than PCI interrupts to receive event
964+
* channel notifications because we can receive vector callbacks on any
965+
* vcpu and we don't need PCI support or APIC interactions. */
966+
void xen_callback_vector(void)
967+
{
968+
int rc;
969+
uint64_t callback_via;
970+
if (xen_have_vector_callback) {
971+
callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
972+
rc = xen_set_callback_via(callback_via);
973+
if (rc) {
974+
printk(KERN_ERR "Request for Xen HVM callback vector"
975+
" failed.\n");
976+
xen_have_vector_callback = 0;
977+
return;
978+
}
979+
printk(KERN_INFO "Xen HVM callback vector for event delivery is "
980+
"enabled\n");
981+
/* in the restore case the vector has already been allocated */
982+
if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
983+
alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
984+
}
985+
}
986+
936987
void __init xen_init_IRQ(void)
937988
{
938989
int i;
@@ -947,5 +998,10 @@ void __init xen_init_IRQ(void)
947998
for (i = 0; i < NR_EVENT_CHANNELS; i++)
948999
mask_evtchn(i);
9491000

950-
irq_ctx_init(smp_processor_id());
1001+
if (xen_hvm_domain()) {
1002+
xen_callback_vector();
1003+
native_init_IRQ();
1004+
} else {
1005+
irq_ctx_init(smp_processor_id());
1006+
}
9511007
}

‎include/xen/events.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,11 @@ void xen_poll_irq(int irq);
5656
/* Determine the IRQ which is bound to an event channel */
5757
unsigned irq_from_evtchn(unsigned int evtchn);
5858

59+
/* Xen HVM evtchn vector callback */
60+
extern void xen_hvm_callback_vector(void);
61+
extern int xen_have_vector_callback;
62+
int xen_set_callback_via(uint64_t via);
63+
void xen_evtchn_do_upcall(struct pt_regs *regs);
64+
void xen_hvm_evtchn_do_upcall(void);
65+
5966
#endif /* _XEN_EVENTS_H */

‎include/xen/hvm.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define XEN_HVM_H__
44

55
#include <xen/interface/hvm/params.h>
6+
#include <asm/xen/hypercall.h>
67

78
static inline int hvm_get_parameter(int idx, uint64_t *value)
89
{
@@ -21,4 +22,9 @@ static inline int hvm_get_parameter(int idx, uint64_t *value)
2122
return r;
2223
}
2324

25+
#define HVM_CALLBACK_VIA_TYPE_VECTOR 0x2
26+
#define HVM_CALLBACK_VIA_TYPE_SHIFT 56
27+
#define HVM_CALLBACK_VECTOR(x) (((uint64_t)HVM_CALLBACK_VIA_TYPE_VECTOR)<<\
28+
HVM_CALLBACK_VIA_TYPE_SHIFT | (x))
29+
2430
#endif /* XEN_HVM_H__ */

‎include/xen/interface/features.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
/* x86: Does this Xen host support the MMU_PT_UPDATE_PRESERVE_AD hypercall? */
4242
#define XENFEAT_mmu_pt_update_preserve_ad 5
4343

44+
/* x86: Does this Xen host support the HVM callback vector type? */
45+
#define XENFEAT_hvm_callback_vector 8
46+
4447
#define XENFEAT_NR_SUBMAPS 1
4548

4649
#endif /* __XEN_PUBLIC_FEATURES_H__ */

0 commit comments

Comments
 (0)