Skip to content

Commit 90c7379

Browse files
legoaterpaulusmack
authored andcommitted
KVM: PPC: Book3S HV: Add a new KVM device for the XIVE native exploitation mode
This is the basic framework for the new KVM device supporting the XIVE native exploitation mode. The user interface exposes a new KVM device to be created by QEMU, only available when running on a L0 hypervisor. Support for nested guests is not available yet. The XIVE device reuses the device structure of the XICS-on-XIVE device as they have a lot in common. That could possibly change in the future if the need arise. Signed-off-by: Cédric Le Goater <[email protected]> Reviewed-by: David Gibson <[email protected]> Signed-off-by: Paul Mackerras <[email protected]>
1 parent a878957 commit 90c7379

File tree

8 files changed

+219
-2
lines changed

8 files changed

+219
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
POWER9 eXternal Interrupt Virtualization Engine (XIVE Gen1)
2+
==========================================================
3+
4+
Device types supported:
5+
KVM_DEV_TYPE_XIVE POWER9 XIVE Interrupt Controller generation 1
6+
7+
This device acts as a VM interrupt controller. It provides the KVM
8+
interface to configure the interrupt sources of a VM in the underlying
9+
POWER9 XIVE interrupt controller.
10+
11+
Only one XIVE instance may be instantiated. A guest XIVE device
12+
requires a POWER9 host and the guest OS should have support for the
13+
XIVE native exploitation interrupt mode. If not, it should run using
14+
the legacy interrupt mode, referred as XICS (POWER7/8).
15+
16+
* Groups:
17+
18+
1. KVM_DEV_XIVE_GRP_CTRL
19+
Provides global controls on the device

arch/powerpc/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ extern struct kvm_device_ops kvm_xics_ops;
225225
struct kvmppc_xive;
226226
struct kvmppc_xive_vcpu;
227227
extern struct kvm_device_ops kvm_xive_ops;
228+
extern struct kvm_device_ops kvm_xive_native_ops;
228229

229230
struct kvmppc_passthru_irqmap;
230231

arch/powerpc/include/asm/kvm_ppc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ extern int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
593593
extern int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
594594
int level, bool line_status);
595595
extern void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu);
596+
597+
extern void kvmppc_xive_native_init_module(void);
598+
extern void kvmppc_xive_native_exit_module(void);
599+
596600
#else
597601
static inline int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
598602
u32 priority) { return -1; }
@@ -616,6 +620,10 @@ static inline int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval) { retur
616620
static inline int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
617621
int level, bool line_status) { return -ENODEV; }
618622
static inline void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu) { }
623+
624+
static inline void kvmppc_xive_native_init_module(void) { }
625+
static inline void kvmppc_xive_native_exit_module(void) { }
626+
619627
#endif /* CONFIG_KVM_XIVE */
620628

621629
#if defined(CONFIG_PPC_POWERNV) && defined(CONFIG_KVM_BOOK3S_64_HANDLER)

arch/powerpc/include/uapi/asm/kvm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,4 +677,7 @@ struct kvm_ppc_cpu_char {
677677
#define KVM_XICS_PRESENTED (1ULL << 43)
678678
#define KVM_XICS_QUEUED (1ULL << 44)
679679

680+
/* POWER9 XIVE Native Interrupt Controller */
681+
#define KVM_DEV_XIVE_GRP_CTRL 1
682+
680683
#endif /* __LINUX_KVM_POWERPC_H */

arch/powerpc/kvm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ endif
9494
kvm-book3s_64-objs-$(CONFIG_KVM_XICS) += \
9595
book3s_xics.o
9696

97-
kvm-book3s_64-objs-$(CONFIG_KVM_XIVE) += book3s_xive.o
97+
kvm-book3s_64-objs-$(CONFIG_KVM_XIVE) += book3s_xive.o book3s_xive_native.o
9898
kvm-book3s_64-objs-$(CONFIG_SPAPR_TCE_IOMMU) += book3s_64_vio.o
9999

100100
kvm-book3s_64-module-objs := \

arch/powerpc/kvm/book3s.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,9 @@ static int kvmppc_book3s_init(void)
10501050
if (xics_on_xive()) {
10511051
kvmppc_xive_init_module();
10521052
kvm_register_device_ops(&kvm_xive_ops, KVM_DEV_TYPE_XICS);
1053+
kvmppc_xive_native_init_module();
1054+
kvm_register_device_ops(&kvm_xive_native_ops,
1055+
KVM_DEV_TYPE_XIVE);
10531056
} else
10541057
#endif
10551058
kvm_register_device_ops(&kvm_xics_ops, KVM_DEV_TYPE_XICS);
@@ -1060,8 +1063,10 @@ static int kvmppc_book3s_init(void)
10601063
static void kvmppc_book3s_exit(void)
10611064
{
10621065
#ifdef CONFIG_KVM_XICS
1063-
if (xics_on_xive())
1066+
if (xics_on_xive()) {
10641067
kvmppc_xive_exit_module();
1068+
kvmppc_xive_native_exit_module();
1069+
}
10651070
#endif
10661071
#ifdef CONFIG_KVM_BOOK3S_32_HANDLER
10671072
kvmppc_book3s_exit_pr();

arch/powerpc/kvm/book3s_xive_native.c

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (c) 2017-2019, IBM Corporation.
4+
*/
5+
6+
#define pr_fmt(fmt) "xive-kvm: " fmt
7+
8+
#include <linux/kernel.h>
9+
#include <linux/kvm_host.h>
10+
#include <linux/err.h>
11+
#include <linux/gfp.h>
12+
#include <linux/spinlock.h>
13+
#include <linux/delay.h>
14+
#include <asm/uaccess.h>
15+
#include <asm/kvm_book3s.h>
16+
#include <asm/kvm_ppc.h>
17+
#include <asm/hvcall.h>
18+
#include <asm/xive.h>
19+
#include <asm/xive-regs.h>
20+
#include <asm/debug.h>
21+
#include <asm/debugfs.h>
22+
#include <asm/opal.h>
23+
24+
#include <linux/debugfs.h>
25+
#include <linux/seq_file.h>
26+
27+
#include "book3s_xive.h"
28+
29+
static int kvmppc_xive_native_set_attr(struct kvm_device *dev,
30+
struct kvm_device_attr *attr)
31+
{
32+
switch (attr->group) {
33+
case KVM_DEV_XIVE_GRP_CTRL:
34+
break;
35+
}
36+
return -ENXIO;
37+
}
38+
39+
static int kvmppc_xive_native_get_attr(struct kvm_device *dev,
40+
struct kvm_device_attr *attr)
41+
{
42+
return -ENXIO;
43+
}
44+
45+
static int kvmppc_xive_native_has_attr(struct kvm_device *dev,
46+
struct kvm_device_attr *attr)
47+
{
48+
switch (attr->group) {
49+
case KVM_DEV_XIVE_GRP_CTRL:
50+
break;
51+
}
52+
return -ENXIO;
53+
}
54+
55+
static void kvmppc_xive_native_free(struct kvm_device *dev)
56+
{
57+
struct kvmppc_xive *xive = dev->private;
58+
struct kvm *kvm = xive->kvm;
59+
60+
debugfs_remove(xive->dentry);
61+
62+
pr_devel("Destroying xive native device\n");
63+
64+
if (kvm)
65+
kvm->arch.xive = NULL;
66+
67+
if (xive->vp_base != XIVE_INVALID_VP)
68+
xive_native_free_vp_block(xive->vp_base);
69+
70+
kfree(xive);
71+
kfree(dev);
72+
}
73+
74+
static int kvmppc_xive_native_create(struct kvm_device *dev, u32 type)
75+
{
76+
struct kvmppc_xive *xive;
77+
struct kvm *kvm = dev->kvm;
78+
int ret = 0;
79+
80+
pr_devel("Creating xive native device\n");
81+
82+
if (kvm->arch.xive)
83+
return -EEXIST;
84+
85+
xive = kzalloc(sizeof(*xive), GFP_KERNEL);
86+
if (!xive)
87+
return -ENOMEM;
88+
89+
dev->private = xive;
90+
xive->dev = dev;
91+
xive->kvm = kvm;
92+
kvm->arch.xive = xive;
93+
94+
/*
95+
* Allocate a bunch of VPs. KVM_MAX_VCPUS is a large value for
96+
* a default. Getting the max number of CPUs the VM was
97+
* configured with would improve our usage of the XIVE VP space.
98+
*/
99+
xive->vp_base = xive_native_alloc_vp_block(KVM_MAX_VCPUS);
100+
pr_devel("VP_Base=%x\n", xive->vp_base);
101+
102+
if (xive->vp_base == XIVE_INVALID_VP)
103+
ret = -ENXIO;
104+
105+
xive->single_escalation = xive_native_has_single_escalation();
106+
107+
if (ret)
108+
kfree(xive);
109+
110+
return ret;
111+
}
112+
113+
static int xive_native_debug_show(struct seq_file *m, void *private)
114+
{
115+
struct kvmppc_xive *xive = m->private;
116+
struct kvm *kvm = xive->kvm;
117+
118+
if (!kvm)
119+
return 0;
120+
121+
return 0;
122+
}
123+
124+
static int xive_native_debug_open(struct inode *inode, struct file *file)
125+
{
126+
return single_open(file, xive_native_debug_show, inode->i_private);
127+
}
128+
129+
static const struct file_operations xive_native_debug_fops = {
130+
.open = xive_native_debug_open,
131+
.read = seq_read,
132+
.llseek = seq_lseek,
133+
.release = single_release,
134+
};
135+
136+
static void xive_native_debugfs_init(struct kvmppc_xive *xive)
137+
{
138+
char *name;
139+
140+
name = kasprintf(GFP_KERNEL, "kvm-xive-%p", xive);
141+
if (!name) {
142+
pr_err("%s: no memory for name\n", __func__);
143+
return;
144+
}
145+
146+
xive->dentry = debugfs_create_file(name, 0444, powerpc_debugfs_root,
147+
xive, &xive_native_debug_fops);
148+
149+
pr_debug("%s: created %s\n", __func__, name);
150+
kfree(name);
151+
}
152+
153+
static void kvmppc_xive_native_init(struct kvm_device *dev)
154+
{
155+
struct kvmppc_xive *xive = (struct kvmppc_xive *)dev->private;
156+
157+
/* Register some debug interfaces */
158+
xive_native_debugfs_init(xive);
159+
}
160+
161+
struct kvm_device_ops kvm_xive_native_ops = {
162+
.name = "kvm-xive-native",
163+
.create = kvmppc_xive_native_create,
164+
.init = kvmppc_xive_native_init,
165+
.destroy = kvmppc_xive_native_free,
166+
.set_attr = kvmppc_xive_native_set_attr,
167+
.get_attr = kvmppc_xive_native_get_attr,
168+
.has_attr = kvmppc_xive_native_has_attr,
169+
};
170+
171+
void kvmppc_xive_native_init_module(void)
172+
{
173+
;
174+
}
175+
176+
void kvmppc_xive_native_exit_module(void)
177+
{
178+
;
179+
}

include/uapi/linux/kvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,8 @@ enum kvm_device_type {
12111211
#define KVM_DEV_TYPE_ARM_VGIC_V3 KVM_DEV_TYPE_ARM_VGIC_V3
12121212
KVM_DEV_TYPE_ARM_VGIC_ITS,
12131213
#define KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_ARM_VGIC_ITS
1214+
KVM_DEV_TYPE_XIVE,
1215+
#define KVM_DEV_TYPE_XIVE KVM_DEV_TYPE_XIVE
12141216
KVM_DEV_TYPE_MAX,
12151217
};
12161218

0 commit comments

Comments
 (0)