Skip to contentSkip to conversation

In-kernel XICS interrupt controller emulation

PATCH Applied Paul Mackerras 5 participants 32 messages Feb 14, 2013 – Apr 19, 2013

Subsystems (4)

v1 of 5 Applied Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover In-kernel XICS interrupt controller emulation

This patch series implements in-kernel emulation of the XICS interrupt controller architecture defined in PAPR (Power Architecture Platform Requirements, the document that defines IBM's pSeries platform architecture).

One of the things I have done in this version is to provide a way for this to coexist with other interrupt controller emulations. The XICS emulation exports a vector of function pointers that the generic book3s code uses to call in to it. Other emulations could set this vector to point to their own functions. (I realize that Scott Wood's recently-posted patch series uses an entirely orthogonal approach and may or may not find this useful.)

The interface defined here consists of:

* KVM_CREATE_IRQCHIP_ARGS: like KVM_CREATE_IRQCHIP but takes an argument struct containing a type' field, specifying what overall interrupt controller architecture to emulate, and a param' field (unused by the XICS emulation). This is only called once per VM, before VCPUs are created.

* KVM_IRQCHIP_SET_SOURCES: used to create and configure interrupt sources in bulk. The notion of "interrupt source" is fairly generic; a source has an identifying number (20 bits in the current implementation), a priority, a destination (a vcpu number or potentially a vcpu group identifier), plus flags indicating edge/level sensitivity, a masked bit, and a pending bit.

* KVM_IRQCHIP_GET_SOURCES: used to query the configuration and status of interrupt sources in bulk.

* A 64-bit one_reg identifier, KVM_REG_PPC_ICP_STATE, used to get and set per-vcpu interrupt controller state (per-vcpu priority, interrupt status, and pending IPI and interrupt state).

I believe this corresponds reasonably well to what was discussed at KVM Forum.

This series is against Alex's kvm-ppc-queue branch, although it also applies cleanly on the kvm tree's next branch.

Paul.

#1 lore raw

PATCH 1/9 KVM: PPC: Book3S: Add infrastructure to implement kernel-side RTAS calls

From: Michael Ellerman <[email protected]>

For pseries machine emulation, in order to move the interrupt controller code to the kernel, we need to intercept some RTAS calls in the kernel itself. This adds an infrastructure to allow in-kernel handlers to be registered for RTAS services by name. A new ioctl, KVM_PPC_RTAS_DEFINE_TOKEN, then allows userspace to associate token values with those service names. Then, when the guest requests an RTAS service with one of those token values, it will be handled by the relevant in-kernel handler rather than being passed up to userspace as at present.

---
 Documentation/virtual/kvm/api.txt   |   19 ++++
 arch/powerpc/include/asm/hvcall.h   |    3 +
 arch/powerpc/include/asm/kvm_host.h |    1 +
 arch/powerpc/include/asm/kvm_ppc.h  |    4 +
 arch/powerpc/include/uapi/asm/kvm.h |    6 ++
 arch/powerpc/kvm/Makefile           |    1 +
 arch/powerpc/kvm/book3s_hv.c        |   18 +++-
 arch/powerpc/kvm/book3s_pr_papr.c   |    7 ++
 arch/powerpc/kvm/book3s_rtas.c      |  182 +++++++++++++++++++++++++++++++++++
 arch/powerpc/kvm/powerpc.c          |    9 +-
 include/uapi/linux/kvm.h            |    3 +
 11 files changed, 251 insertions(+), 2 deletions(-)
 create mode 100644 arch/powerpc/kvm/book3s_rtas.c
Documentation/virtual/kvm/api.txt +19 -0
index c2534c3..d3e2d60 100644
@@ -2122,6 +2122,25 @@ header; first `n_valid' valid entries with contents from the data
 written, then `n_invalid' invalid entries, invalidating any previously
 valid entries found.
 
+4.79 KVM_PPC_RTAS_DEFINE_TOKEN
+
+Capability: KVM_CAP_PPC_RTAS
+Architectures: ppc
+Type: vm ioctl
+Parameters: struct kvm_rtas_token_args
+Returns: 0 on success, -1 on error
+
+Defines a token value for a RTAS (Run Time Abstraction Services)
+service in order to allow it to be handled in the kernel.  The
+argument struct gives the name of the service, which must be the name
+of a service that has a kernel-side implementation.  If the token
+value is non-zero, it will be associated with that service, and
+subsequent RTAS calls by the guest specifying that token will be
+handled by the kernel.  If the token value is 0, then any token
+associated with the service will be forgotten, and subsequent RTAS
+calls by the guest for that service will be passed to userspace to be
+handled.
+
 
 5. The kvm_run structure
 ------------------------
arch/powerpc/include/asm/hvcall.h +3 -0
index 7a86706..9ea22b2 100644
@@ -269,6 +269,9 @@
 #define H_GET_MPP_X		0x314
 #define MAX_HCALL_OPCODE	H_GET_MPP_X
 
+/* Platform specific hcalls, used by KVM */
+#define H_RTAS			0xf000
+
 #ifndef __ASSEMBLY__
 
 /**
arch/powerpc/include/asm/kvm_host.h +1 -0
index 8a72d59..8295dc7 100644
@@ -255,6 +255,7 @@ struct kvm_arch {
 #endif /* CONFIG_KVM_BOOK3S_64_HV */
 #ifdef CONFIG_PPC_BOOK3S_64
 	struct list_head spapr_tce_tables;
+	struct list_head rtas_tokens;
 #endif
 };
 
arch/powerpc/include/asm/kvm_ppc.h +4 -0
index 44a657a..dd08cfa 100644
@@ -165,6 +165,10 @@ extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
 
 extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
 
+extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
+extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
+extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
+
 /*
  * Cuts out inst bits with ordering according to spec.
  * That means the leftmost bit is zero. All given bits are included.
arch/powerpc/include/uapi/asm/kvm.h +6 -0
index 16064d0..d90743c 100644
@@ -299,6 +299,12 @@ struct kvm_allocate_rma {
 	__u64 rma_size;
 };
 
+/* for KVM_CAP_PPC_RTAS */
+struct kvm_rtas_token_args {
+	char name[120];
+	__u64 token;	/* Use a token of 0 to undefine a mapping */
+};
+
 struct kvm_book3e_206_tlb_entry {
 	__u32 mas8;
 	__u32 mas1;
arch/powerpc/kvm/Makefile +1 -0
index b772ede..432132c 100644
@@ -86,6 +86,7 @@ kvm-book3s_64-module-objs := \
 	emulate.o \
 	book3s.o \
 	book3s_64_vio.o \
+	book3s_rtas.o \
 	$(kvm-book3s_64-objs-y)
 
 kvm-objs-$(CONFIG_KVM_BOOK3S_64) := $(kvm-book3s_64-module-objs)
arch/powerpc/kvm/book3s_hv.c +17 -1
index 80dcc53..567c264 100644
@@ -479,7 +479,7 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
 	unsigned long req = kvmppc_get_gpr(vcpu, 3);
 	unsigned long target, ret = H_SUCCESS;
 	struct kvm_vcpu *tvcpu;
-	int idx;
+	int idx, rc;
 
 	switch (req) {
 	case H_ENTER:
@@ -515,6 +515,19 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
 					kvmppc_get_gpr(vcpu, 5),
 					kvmppc_get_gpr(vcpu, 6));
 		break;
+	case H_RTAS:
+		if (list_empty(&vcpu->kvm->arch.rtas_tokens))
+			return RESUME_HOST;
+
+		rc = kvmppc_rtas_hcall(vcpu);
+
+		if (rc = -ENOENT)
+			return RESUME_HOST;
+		else if (rc = 0)
+			break;
+
+		/* Send the error out to userspace via KVM_RUN */
+		return rc;
 	default:
 		return RESUME_HOST;
 	}
@@ -1821,6 +1834,7 @@ int kvmppc_core_init_vm(struct kvm *kvm)
 	cpumask_setall(&kvm->arch.need_tlb_flush);
 
 	INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
+	INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
 
 	kvm->arch.rma = NULL;
 
@@ -1866,6 +1880,8 @@ void kvmppc_core_destroy_vm(struct kvm *kvm)
 		kvm->arch.rma = NULL;
 	}
 
+	kvmppc_rtas_tokens_free(kvm);
+
 	kvmppc_free_hpt(kvm);
 	WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
 }
arch/powerpc/kvm/book3s_pr_papr.c +7 -0
index ee02b30..4efa4a4 100644
@@ -246,6 +246,13 @@ int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)
 		clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
 		vcpu->stat.halt_wakeup++;
 		return EMULATE_DONE;
+	case H_RTAS:
+		if (list_empty(&vcpu->kvm->arch.rtas_tokens))
+			return RESUME_HOST;
+		if (kvmppc_rtas_hcall(vcpu))
+			break;
+		kvmppc_set_gpr(vcpu, 3, 0);
+		return EMULATE_DONE;
 	}
 
 	return EMULATE_FAIL;
arch/powerpc/kvm/book3s_rtas.c new file +182 -0

182 lines not on this page · raw message

arch/powerpc/kvm/powerpc.c +8 -1
index 934413c..26d8003 100644
@@ -335,6 +335,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 #ifdef CONFIG_PPC_BOOK3S_64
 	case KVM_CAP_SPAPR_TCE:
 	case KVM_CAP_PPC_ALLOC_HTAB:
+	case KVM_CAP_PPC_RTAS:
 		r = 1;
 		break;
 #endif /* CONFIG_PPC_BOOK3S_64 */
@@ -946,8 +947,8 @@ long kvm_arch_vm_ioctl(struct file *filp,
 
 #ifdef CONFIG_KVM_BOOK3S_64_HV
 	case KVM_ALLOCATE_RMA: {
-		struct kvm *kvm = filp->private_data;
 		struct kvm_allocate_rma rma;
+		struct kvm *kvm = filp->private_data;
 
 		r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
 		if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
@@ -995,6 +996,12 @@ long kvm_arch_vm_ioctl(struct file *filp,
 			r = -EFAULT;
 		break;
 	}
+	case KVM_PPC_RTAS_DEFINE_TOKEN: {
+		struct kvm *kvm = filp->private_data;
+
+		r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
+		break;
+	}
 #endif /* CONFIG_PPC_BOOK3S_64 */
 	default:
 		r = -ENOTTY;
include/uapi/linux/kvm.h +3 -0
index 9a2db57..1e2fda0 100644
@@ -662,6 +662,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_HTAB_FD 84
 #define KVM_CAP_S390_CSS_SUPPORT 85
 #define KVM_CAP_PPC_EPR 86
+#define KVM_CAP_PPC_RTAS 87
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -889,6 +890,8 @@ struct kvm_s390_ucas_mapping {
 #define KVM_ALLOCATE_RMA	  _IOR(KVMIO,  0xa9, struct kvm_allocate_rma)
 /* Available with KVM_CAP_PPC_HTAB_FD */
 #define KVM_PPC_GET_HTAB_FD	  _IOW(KVMIO,  0xaa, struct kvm_get_htab_fd)
+/* Available with KVM_CAP_PPC_RTAS */
+#define KVM_PPC_RTAS_DEFINE_TOKEN _IOW(KVMIO,  0xab, struct kvm_rtas_token_args)
 
 /*
  * ioctls for vcpu fds
Signature · 1 line
1.7.10.rc3.219.g53414
#7 lore raw

PATCH 2/9 KVM: PPC: Remove unused argument to kvmppc_core_dequeue_external

In reply to Paul Mackerras

Currently kvmppc_core_dequeue_external() takes a struct kvm_interrupt * argument and does nothing with it, in any of its implementations. This removes it in order to make things easier for forthcoming in-kernel interrupt controller emulation code.

---
 arch/powerpc/include/asm/kvm_ppc.h |    3 +--
 arch/powerpc/kvm/book3s.c          |    3 +--
 arch/powerpc/kvm/booke.c           |    3 +--
 arch/powerpc/kvm/powerpc.c         |    2 +-
 4 files changed, 4 insertions(+), 7 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +1 -2
index dd08cfa..be611f6 100644
@@ -104,8 +104,7 @@ extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
 extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
 extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
                                        struct kvm_interrupt *irq);
-extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
-                                         struct kvm_interrupt *irq);
+extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
 extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
 
 extern int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
arch/powerpc/kvm/book3s.c +1 -2
index a4b6452..6548445 100644
@@ -160,8 +160,7 @@ void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
 	kvmppc_book3s_queue_irqprio(vcpu, vec);
 }
 
-void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
-                                  struct kvm_interrupt *irq)
+void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
 {
 	kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
 	kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
arch/powerpc/kvm/booke.c +1 -2
index 020923e..f72abd1 100644
@@ -222,8 +222,7 @@ void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
 	kvmppc_booke_queue_irqprio(vcpu, prio);
 }
 
-void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
-                                  struct kvm_interrupt *irq)
+void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
 {
 	clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
 	clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
arch/powerpc/kvm/powerpc.c +1 -1
index 26d8003..1772883 100644
@@ -741,7 +741,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
 {
 	if (irq->irq = KVM_INTERRUPT_UNSET) {
-		kvmppc_core_dequeue_external(vcpu, irq);
+		kvmppc_core_dequeue_external(vcpu);
 		return 0;
 	}
 
Signature · 1 line
1.7.10.rc3.219.g53414
#9 lore raw

PATCH 3/9 KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

In reply to Paul Mackerras

From: Benjamin Herrenschmidt <[email protected]>

This adds in-kernel emulation of the XICS (eXternal Interrupt Controller Specification) interrupt controller specified by PAPR, for both HV and PR KVM guests.

This adds a new KVM_CREATE_IRQCHIP_ARGS ioctl, which is like KVM_CREATE_IRQCHIP in that it indicates that the virtual machine should use in-kernel interrupt controller emulation, but also takes an argument struct that contains the type of interrupt controller architecture and an optional parameter. Currently only one type value is defined, that which indicates the XICS architecture.

The XICS emulation supports up to 1048560 interrupt sources. Interrupt source numbers below 16 are reserved; 0 is used to mean no interrupt and 2 is used for IPIs. Internally these are represented in blocks of 1024, called ICS (interrupt controller source) entities, but that is not visible to userspace.

Two other new ioctls allow userspace to control the interrupt sources. The KVM_IRQCHIP_SET_SOURCES ioctl sets the priority, destination cpu, level/edge sensitivity and pending state of a range of interrupt sources, creating them if they don't already exist. The KVM_IRQCHIP_GET_SOURCES ioctl returns that information for a range of interrupt sources (they are required to already exist).

Each vcpu gets one ICP (interrupt controller presentation) entity. They are created automatically when the vcpu is created provided the KVM_CREATE_IRQCHIP_ARGS ioctl has been performed.

This is based on an initial implementation by Michael Ellerman <[email protected]> reworked by Benjamin Herrenschmidt and Paul Mackerras.

---
 Documentation/virtual/kvm/api.txt     |   51 ++
 arch/powerpc/include/asm/kvm_book3s.h |    1 +
 arch/powerpc/include/asm/kvm_host.h   |    8 +
 arch/powerpc/include/asm/kvm_ppc.h    |   19 +
 arch/powerpc/kvm/Makefile             |    1 +
 arch/powerpc/kvm/book3s.c             |    2 +-
 arch/powerpc/kvm/book3s_hv.c          |   20 +
 arch/powerpc/kvm/book3s_pr.c          |   13 +
 arch/powerpc/kvm/book3s_pr_papr.c     |   16 +
 arch/powerpc/kvm/book3s_rtas.c        |   51 +-
 arch/powerpc/kvm/book3s_xics.c        | 1101 +++++++++++++++++++++++++++++++++
 arch/powerpc/kvm/book3s_xics.h        |  111 ++++
 arch/powerpc/kvm/powerpc.c            |   23 +
 include/uapi/linux/kvm.h              |   29 +
 14 files changed, 1444 insertions(+), 2 deletions(-)
 create mode 100644 arch/powerpc/kvm/book3s_xics.c
 create mode 100644 arch/powerpc/kvm/book3s_xics.h
Documentation/virtual/kvm/api.txt +51 -0
index d3e2d60..0ff9dcf 100644
@@ -2141,6 +2141,57 @@ associated with the service will be forgotten, and subsequent RTAS
 calls by the guest for that service will be passed to userspace to be
 handled.
 
+4.80 KVM_CREATE_IRQCHIP_ARGS
+
+Capability: KVM_CAP_IRQCHIP_ARGS
+Architectures: ppc
+Type: vm ioctl
+Parameters: struct kvm_irqchip_args
+Returns: 0 on success, -1 on error
+
+Creates an interrupt controller model in the kernel.  The type field
+of the argument struct indicates the interrupt controller architecture
+of the virtual machine.  Currently the only value permitted for the
+type field is 1, indicating the XICS (eXternal Interrupt Controller
+Specification) model defined in PAPR.  For XICS, this ioctl indicates
+to the kernel that an interrupt controller presentation (ICP) entity
+should be created for every vcpu, and interrupt controller source
+(ICS) entities should be created to accommodate the sources that are
+configured with the KVM_IRQCHIP_SET_SOURCES ioctl.
+
+4.81 KVM_IRQCHIP_GET_SOURCES
+
+Capability: KVM_CAP_IRQCHIP_ARGS
+Architectures: ppc
+Type: vm ioctl
+Parameters: struct kvm_irq_sources
+Returns: 0 on success, -1 on error
+
+Copies configuration and status information about a range of interrupt
+sources into a user-supplied buffer.  The argument struct gives the
+starting interrupt source number and the number of interrupt sources.
+The user buffer is an array of 64-bit quantities, one per interrupt
+source, with (from the least- significant bit) 32 bits of interrupt
+server number, 8 bits of priority, and 1 bit each for a
+level-sensitive indicator, a masked indicator, and a pending
+indicator.  If some of the sources in the range don't exist, that is,
+have not yet been created with the KVM_IRQCHIP_SET_SOURCES ioctl,
+this returns an ENODEV error.
+
+4.82 KVM_IRQCHIP_SET_SOURCES
+
+Capability: KVM_CAP_IRQCHIP_ARGS
+Architectures: ppc
+Type: vm ioctl
+Parameters: struct kvm_irq_sources
+Returns: 0 on success, -1 on error
+
+Sets the configuration and status for a range of interrupt sources
+from information supplied in a user-supplied buffer, creating the
+sources if they don't already exist.  The argument struct gives the
+starting interrupt source number and the number of interrupt sources.
+The user buffer is formatted as for KVM_IRQCHIP_GET_SOURCES.
+
 
 5. The kvm_run structure
 ------------------------
arch/powerpc/include/asm/kvm_book3s.h +1 -0
index 5a56e1c..17c9a15 100644
@@ -142,6 +142,7 @@ extern int kvmppc_mmu_hv_init(void);
 extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
 extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
 extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
+extern void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
 extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
 extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
 			   bool upper, u32 val);
arch/powerpc/include/asm/kvm_host.h +8 -0
index 8295dc7..b05e7cd 100644
@@ -188,6 +188,10 @@ struct kvmppc_linear_info {
 	int		 type;
 };
 
+/* XICS components, defined in boo3s_xics.c */
+struct kvmppc_xics;
+struct kvmppc_icp;
+
 /*
  * The reverse mapping array has one entry for each HPTE,
  * which stores the guest's view of the second word of the HPTE
@@ -256,6 +260,7 @@ struct kvm_arch {
 #ifdef CONFIG_PPC_BOOK3S_64
 	struct list_head spapr_tce_tables;
 	struct list_head rtas_tokens;
+	struct kvmppc_xics *xics;
 #endif
 };
 
@@ -572,6 +577,9 @@ struct kvm_vcpu_arch {
 	u64 busy_stolen;
 	u64 busy_preempt;
 #endif
+#ifdef CONFIG_PPC_BOOK3S_64
+	struct kvmppc_icp *icp; /* XICS presentation controller */
+#endif
 };
 
 /* Values for vcpu->arch.state */
arch/powerpc/include/asm/kvm_ppc.h +19 -0
index be611f6..f0fd22b 100644
@@ -130,6 +130,13 @@ extern long kvmppc_prepare_vrma(struct kvm *kvm,
 extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
 			struct kvm_memory_slot *memslot, unsigned long porder);
 extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
+extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
+extern int kvmppc_xics_ioctl(struct kvm *kvm, unsigned ioctl, unsigned long arg);
+extern int kvmppc_xics_create(struct kvm *kvm, struct kvm_irqchip_args *args);
+extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
+extern int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu);
+extern void kvmppc_xics_free(struct kvm *kvm);
+
 extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
 				struct kvm_create_spapr_tce *args);
 extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
@@ -167,6 +174,8 @@ extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
 extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
 extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
 extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
+extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, u32 priority);
+extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, u32 *priority);
 
 /*
  * Cuts out inst bits with ordering according to spec.
@@ -263,6 +272,16 @@ static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
 
 static inline void kvm_linear_init(void)
 {}
+
+#endif
+
+#ifdef CONFIG_PPC_BOOK3S_64
+static inline int kvmppc_xics_enabled(struct kvm *kvm)
+{
+	return kvm->arch.xics != NULL;
+}
+#else
+static inline int kvmppc_xics_enabled(struct kvm *kvm) { return 0; }
 #endif
 
 static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
arch/powerpc/kvm/Makefile +1 -0
index 432132c..e2eb04c 100644
@@ -87,6 +87,7 @@ kvm-book3s_64-module-objs := \
 	book3s.o \
 	book3s_64_vio.o \
 	book3s_rtas.o \
+	book3s_xics.o \
 	$(kvm-book3s_64-objs-y)
 
 kvm-objs-$(CONFIG_KVM_BOOK3S_64) := $(kvm-book3s_64-module-objs)
arch/powerpc/kvm/book3s.c +1 -1
index 6548445..c5a4478 100644
@@ -104,7 +104,7 @@ static int kvmppc_book3s_vec2irqprio(unsigned int vec)
 	return prio;
 }
 
-static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
+void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
 					  unsigned int vec)
 {
 	unsigned long old_pending = vcpu->arch.pending_exceptions;
arch/powerpc/kvm/book3s_hv.c +20 -0
index 567c264..aa3a0db 100644
@@ -528,6 +528,14 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
 
 		/* Send the error out to userspace via KVM_RUN */
 		return rc;
+	case H_XIRR:
+	case H_CPPR:
+	case H_EOI:
+	case H_IPI:
+		if (kvmppc_xics_enabled(vcpu->kvm)) {
+			ret = kvmppc_xics_hcall(vcpu, req);
+			break;
+		} /* fallthrough */
 	default:
 		return RESUME_HOST;
 	}
@@ -886,6 +894,13 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
 	spin_lock_init(&vcpu->arch.tbacct_lock);
 	vcpu->arch.busy_preempt = TB_NIL;
 
+	/* Create the XICS */
+	if (kvmppc_xics_enabled(kvm)) {
+		err = kvmppc_xics_create_icp(vcpu);
+		if (err < 0)
+			goto free_vcpu;
+	}
+
 	kvmppc_mmu_book3s_hv_init(vcpu);
 
 	vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
@@ -937,6 +952,8 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
 		kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
 	spin_unlock(&vcpu->arch.vpa_update_lock);
 	kvm_vcpu_uninit(vcpu);
+	if (kvmppc_xics_enabled(vcpu->kvm))
+		kvmppc_xics_free_icp(vcpu);
 	kmem_cache_free(kvm_vcpu_cache, vcpu);
 }
 
@@ -1882,6 +1899,9 @@ void kvmppc_core_destroy_vm(struct kvm *kvm)
 
 	kvmppc_rtas_tokens_free(kvm);
 
+	if (kvmppc_xics_enabled(kvm))
+		kvmppc_xics_free(kvm);
+
 	kvmppc_free_hpt(kvm);
 	WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
 }
arch/powerpc/kvm/book3s_pr.c +13 -0
index 73ed11c..9b2237f 100644
@@ -1069,6 +1069,13 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
 	if (err < 0)
 		goto uninit_vcpu;
 
+	/* Create the XICS */
+	if (kvmppc_xics_enabled(kvm)) {
+		err = kvmppc_xics_create_icp(vcpu);
+		if (err < 0)
+			goto free_vcpu;
+	}
+
 	return vcpu;
 
 uninit_vcpu:
@@ -1085,6 +1092,8 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
 {
 	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
 
+	if (kvmppc_xics_enabled(vcpu->kvm))
+		kvmppc_xics_free_icp(vcpu);
 	free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
 	kvm_vcpu_uninit(vcpu);
 	kfree(vcpu_book3s->shadow_vcpu);
@@ -1293,6 +1302,7 @@ int kvmppc_core_init_vm(struct kvm *kvm)
 {
 #ifdef CONFIG_PPC64
 	INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
+	INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
 #endif
 
 	return 0;
@@ -1303,6 +1313,9 @@ void kvmppc_core_destroy_vm(struct kvm *kvm)
 #ifdef CONFIG_PPC64
 	WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
 #endif
+	if (kvmppc_xics_enabled(kvm))
+		kvmppc_xics_free(kvm);
+
 }
 
 static int kvmppc_book3s_init(void)
arch/powerpc/kvm/book3s_pr_papr.c +16 -0
index 4efa4a4..94cec5b 100644
@@ -227,6 +227,15 @@ static int kvmppc_h_pr_put_tce(struct kvm_vcpu *vcpu)
 	return EMULATE_DONE;
 }
 
+static int kvmppc_h_pr_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
+{
+	long rc = kvmppc_xics_hcall(vcpu, cmd);
+	if (rc == H_TOO_HARD)
+		return EMULATE_FAIL;
+	kvmppc_set_gpr(vcpu, 3, rc);
+	return EMULATE_DONE;
+}
+
 int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)
 {
 	switch (cmd) {
@@ -246,6 +255,13 @@ int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)
 		clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
 		vcpu->stat.halt_wakeup++;
 		return EMULATE_DONE;
+	case H_XIRR:
+	case H_CPPR:
+	case H_EOI:
+	case H_IPI:
+		if (kvmppc_xics_enabled(vcpu->kvm))
+			return kvmppc_h_pr_xics_hcall(vcpu, cmd);
+		break;
 	case H_RTAS:
 		if (list_empty(&vcpu->kvm->arch.rtas_tokens))
 			return RESUME_HOST;
arch/powerpc/kvm/book3s_rtas.c +50 -1

62 lines not on this page · raw message

arch/powerpc/kvm/book3s_xics.c new file +1101 -0

1,101 lines not on this page · raw message

arch/powerpc/kvm/book3s_xics.h new file +111 -0

111 lines not on this page · raw message

arch/powerpc/kvm/powerpc.c +23 -0
index 1772883..3bcc030 100644
@@ -383,6 +383,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 		break;
 #ifdef CONFIG_PPC_BOOK3S_64
 	case KVM_CAP_PPC_GET_SMMU_INFO:
+	case KVM_CAP_IRQCHIP_ARGS:
 		r = 1;
 		break;
 #endif
@@ -1002,6 +1003,28 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
 		break;
 	}
+	case KVM_IRQ_LINE:
+	case KVM_IRQCHIP_GET_SOURCES:
+	case KVM_IRQCHIP_SET_SOURCES: {
+		struct kvm *kvm = filp->private_data;
+
+		r = -ENOTTY;
+		if (kvmppc_xics_enabled(kvm))
+			r = kvmppc_xics_ioctl(kvm, ioctl, arg);
+		break;
+	}
+	case KVM_CREATE_IRQCHIP_ARGS: {
+		struct kvm *kvm = filp->private_data;
+		struct kvm_irqchip_args args;
+
+		r = -EFAULT;
+		if (copy_from_user(&args, argp, sizeof(args)))
+			break;
+		r = -EINVAL;
+		if (args.type == KVM_IRQCHIP_TYPE_XICS)
+			r = kvmppc_xics_create(kvm, &args);
+		break;
+	}
 #endif /* CONFIG_PPC_BOOK3S_64 */
 	default:
 		r = -ENOTTY;
include/uapi/linux/kvm.h +29 -0
index 1e2fda0..25d73c0 100644
@@ -115,6 +115,7 @@ struct kvm_irq_level {
 	 * ACPI gsi notion of irq.
 	 * For IA-64 (APIC model) IOAPIC0: irq 0-23; IOAPIC1: irq 24-47..
 	 * For X86 (standard AT mode) PIC0/1: irq 0-15. IOAPIC0: 0-23..
+	 * On powerpc SPAPR, the ICS source number, level is ignored.
 	 */
 	union {
 		__u32 irq;
@@ -146,6 +147,15 @@ struct kvm_pit_config {
 
 #define KVM_PIT_SPEAKER_DUMMY     1
 
+/* for KVM_CREATE_IRQCHIP_ARGS */
+struct kvm_irqchip_args {
+	__u64 type;
+	__u64 param;
+};
+
+/* values for type */
+#define KVM_IRQCHIP_TYPE_XICS	1	/* Power server external intr ctrler */
+
 #define KVM_EXIT_UNKNOWN          0
 #define KVM_EXIT_EXCEPTION        1
 #define KVM_EXIT_IO               2
@@ -663,6 +673,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_S390_CSS_SUPPORT 85
 #define KVM_CAP_PPC_EPR 86
 #define KVM_CAP_PPC_RTAS 87
+#define KVM_CAP_IRQCHIP_ARGS 88
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -805,6 +816,21 @@ struct kvm_msi {
 	__u8  pad[16];
 };
 
+struct kvm_irq_sources {
+	__u32 irq;
+	__u32 nr_irqs;
+	__u64 __user *irqbuf;
+};
+
+/* irqbuf entries are laid out like this: */
+#define KVM_IRQ_SERVER_SHIFT	0
+#define KVM_IRQ_SERVER_MASK	0xffffffffULL
+#define KVM_IRQ_PRIORITY_SHIFT	32
+#define KVM_IRQ_PRIORITY_MASK	0xff
+#define KVM_IRQ_LEVEL_SENSITIVE	(1ULL << 40)
+#define KVM_IRQ_MASKED		(1ULL << 41)
+#define KVM_IRQ_PENDING		(1ULL << 42)
+
 /*
  * ioctls for VM fds
  */
@@ -892,6 +918,9 @@ struct kvm_s390_ucas_mapping {
 #define KVM_PPC_GET_HTAB_FD	  _IOW(KVMIO,  0xaa, struct kvm_get_htab_fd)
 /* Available with KVM_CAP_PPC_RTAS */
 #define KVM_PPC_RTAS_DEFINE_TOKEN _IOW(KVMIO,  0xab, struct kvm_rtas_token_args)
+#define KVM_CREATE_IRQCHIP_ARGS   _IOW(KVMIO,  0xac, struct kvm_irqchip_args)
+#define KVM_IRQCHIP_GET_SOURCES	  _IOW(KVMIO,  0xad, struct kvm_irq_sources)
+#define KVM_IRQCHIP_SET_SOURCES	  _IOW(KVMIO,  0xae, struct kvm_irq_sources)
 
 /*
  * ioctls for vcpu fds
Signature · 1 line
1.7.10.rc3.219.g53414
#26 lore raw

PATCH 4/9 KVM: PPC: Book3S: Generalize interfaces to interrupt controller emulation

In reply to Paul Mackerras

This makes the XICS interrupt controller emulation code export a struct containing function pointers for the various calls into the XICS code. The generic book3s code then uses these function pointers instead of calling directly into the XICS code (except for the XICS instantiation function).

This should make it possible for other interrupt controller emulations to coexist with the XICS emulation.

---
 arch/powerpc/include/asm/kvm_host.h |   11 ++++-
 arch/powerpc/include/asm/kvm_ppc.h  |   14 ------
 arch/powerpc/kvm/book3s_hv.c        |   18 +++----
 arch/powerpc/kvm/book3s_pr.c        |   14 +++---
 arch/powerpc/kvm/book3s_pr_papr.c   |   10 ++--
 arch/powerpc/kvm/book3s_xics.c      |   93 +++++++++++++++++++----------------
 arch/powerpc/kvm/powerpc.c          |    4 +-
 7 files changed, 85 insertions(+), 79 deletions(-)
arch/powerpc/include/asm/kvm_host.h +10 -1
index b05e7cd..8af4c0b 100644
@@ -230,8 +230,18 @@ struct kvm_arch_memory_slot {
 #endif /* CONFIG_KVM_BOOK3S_64_HV */
 };
 
+struct kvm_irq_ctrler {
+	int (*setup_vcpu)(struct kvm_vcpu *vcpu);
+	void (*teardown_vcpu)(struct kvm_vcpu *vcpu);
+	void (*free_ctrler)(struct kvm *kvm);
+	int (*hcall)(struct kvm_vcpu *vcpu, unsigned long req);
+	int (*ioctl)(struct kvm *kvm, unsigned int ioctl, unsigned long arg);
+};
+
 struct kvm_arch {
 	unsigned int lpid;
+	struct kvm_irq_ctrler *irq_ctrler;
+	void *irq_ctrler_private;
 #ifdef CONFIG_KVM_BOOK3S_64_HV
 	unsigned long hpt_virt;
 	struct revmap_entry *revmap;
@@ -260,7 +270,6 @@ struct kvm_arch {
 #ifdef CONFIG_PPC_BOOK3S_64
 	struct list_head spapr_tce_tables;
 	struct list_head rtas_tokens;
-	struct kvmppc_xics *xics;
 #endif
 };
 
arch/powerpc/include/asm/kvm_ppc.h +0 -14
index f0fd22b..2308fff 100644
@@ -130,12 +130,7 @@ extern long kvmppc_prepare_vrma(struct kvm *kvm,
 extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
 			struct kvm_memory_slot *memslot, unsigned long porder);
 extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
-extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
-extern int kvmppc_xics_ioctl(struct kvm *kvm, unsigned ioctl, unsigned long arg);
 extern int kvmppc_xics_create(struct kvm *kvm, struct kvm_irqchip_args *args);
-extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
-extern int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu);
-extern void kvmppc_xics_free(struct kvm *kvm);
 
 extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
 				struct kvm_create_spapr_tce *args);
@@ -275,15 +270,6 @@ static inline void kvm_linear_init(void)
 
 #endif
 
-#ifdef CONFIG_PPC_BOOK3S_64
-static inline int kvmppc_xics_enabled(struct kvm *kvm)
-{
-	return kvm->arch.xics != NULL;
-}
-#else
-static inline int kvmppc_xics_enabled(struct kvm *kvm) { return 0; }
-#endif
-
 static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
 {
 #ifdef CONFIG_KVM_BOOKE_HV
arch/powerpc/kvm/book3s_hv.c +9 -9
index aa3a0db..8f09c36 100644
@@ -532,8 +532,8 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
 	case H_CPPR:
 	case H_EOI:
 	case H_IPI:
-		if (kvmppc_xics_enabled(vcpu->kvm)) {
-			ret = kvmppc_xics_hcall(vcpu, req);
+		if (vcpu->kvm->arch.irq_ctrler) {
+			ret = vcpu->kvm->arch.irq_ctrler->hcall(vcpu, req);
 			break;
 		} /* fallthrough */
 	default:
@@ -894,9 +894,9 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
 	spin_lock_init(&vcpu->arch.tbacct_lock);
 	vcpu->arch.busy_preempt = TB_NIL;
 
-	/* Create the XICS */
-	if (kvmppc_xics_enabled(kvm)) {
-		err = kvmppc_xics_create_icp(vcpu);
+	/* Create the interrupt-controller-specific state */
+	if (kvm->arch.irq_ctrler) {
+		err = kvm->arch.irq_ctrler->setup_vcpu(vcpu);
 		if (err < 0)
 			goto free_vcpu;
 	}
@@ -952,8 +952,8 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
 		kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
 	spin_unlock(&vcpu->arch.vpa_update_lock);
 	kvm_vcpu_uninit(vcpu);
-	if (kvmppc_xics_enabled(vcpu->kvm))
-		kvmppc_xics_free_icp(vcpu);
+	if (vcpu->kvm->arch.irq_ctrler)
+		vcpu->kvm->arch.irq_ctrler->teardown_vcpu(vcpu);
 	kmem_cache_free(kvm_vcpu_cache, vcpu);
 }
 
@@ -1899,8 +1899,8 @@ void kvmppc_core_destroy_vm(struct kvm *kvm)
 
 	kvmppc_rtas_tokens_free(kvm);
 
-	if (kvmppc_xics_enabled(kvm))
-		kvmppc_xics_free(kvm);
+	if (kvm->arch.irq_ctrler)
+		kvm->arch.irq_ctrler->free_ctrler(kvm);
 
 	kvmppc_free_hpt(kvm);
 	WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
arch/powerpc/kvm/book3s_pr.c +7 -7
index 9b2237f..0c84242 100644
@@ -1069,9 +1069,9 @@ struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
 	if (err < 0)
 		goto uninit_vcpu;
 
-	/* Create the XICS */
-	if (kvmppc_xics_enabled(kvm)) {
-		err = kvmppc_xics_create_icp(vcpu);
+	/* Create the interrupt-controller-specific vcpu state */
+	if (kvm->arch.irq_ctrler) {
+		err = kvm->arch.irq_ctrler->setup_vcpu(vcpu);
 		if (err < 0)
 			goto free_vcpu;
 	}
@@ -1092,8 +1092,8 @@ void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
 {
 	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
 
-	if (kvmppc_xics_enabled(vcpu->kvm))
-		kvmppc_xics_free_icp(vcpu);
+	if (vcpu->kvm->arch.irq_ctrler)
+		vcpu->kvm->arch.irq_ctrler->teardown_vcpu(vcpu);
 	free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
 	kvm_vcpu_uninit(vcpu);
 	kfree(vcpu_book3s->shadow_vcpu);
@@ -1313,8 +1313,8 @@ void kvmppc_core_destroy_vm(struct kvm *kvm)
 #ifdef CONFIG_PPC64
 	WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
 #endif
-	if (kvmppc_xics_enabled(kvm))
-		kvmppc_xics_free(kvm);
+	if (kvm->arch.irq_ctrler)
+		kvm->arch.irq_ctrler->free_ctrler(kvm);
 
 }
 
arch/powerpc/kvm/book3s_pr_papr.c +6 -4
index 94cec5b..c423b51 100644
@@ -229,7 +229,11 @@ static int kvmppc_h_pr_put_tce(struct kvm_vcpu *vcpu)
 
 static int kvmppc_h_pr_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
 {
-	long rc = kvmppc_xics_hcall(vcpu, cmd);
+	long rc;
+
+	if (!vcpu->kvm->arch.irq_ctrler)
+		return EMULATE_FAIL;
+	rc = vcpu->kvm->arch.irq_ctrler->hcall(vcpu, cmd);
 	if (rc == H_TOO_HARD)
 		return EMULATE_FAIL;
 	kvmppc_set_gpr(vcpu, 3, rc);
@@ -259,9 +263,7 @@ int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)
 	case H_CPPR:
 	case H_EOI:
 	case H_IPI:
-		if (kvmppc_xics_enabled(vcpu->kvm))
-			return kvmppc_h_pr_xics_hcall(vcpu, cmd);
-		break;
+		return kvmppc_h_pr_xics_hcall(vcpu, cmd);
 	case H_RTAS:
 		if (list_empty(&vcpu->kvm->arch.rtas_tokens))
 			return RESUME_HOST;
arch/powerpc/kvm/book3s_xics.c +51 -42

192 lines not on this page · raw message

arch/powerpc/kvm/powerpc.c +2 -2
index 3bcc030..e429839 100644
@@ -1009,8 +1009,8 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		struct kvm *kvm = filp->private_data;
 
 		r = -ENOTTY;
-		if (kvmppc_xics_enabled(kvm))
-			r = kvmppc_xics_ioctl(kvm, ioctl, arg);
+		if (kvm->arch.irq_ctrler)
+			r = kvm->arch.irq_ctrler->ioctl(kvm, ioctl, arg);
 		break;
 	}
 	case KVM_CREATE_IRQCHIP_ARGS: {
Signature · 1 line
1.7.10.rc3.219.g53414
#27 lore raw

PATCH 5/9 KVM: PPC: Book3S: Facilities to save/restore XICS presentation ctrler state

In reply to Paul Mackerras

This adds the ability for userspace to save and restore the state of the XICS interrupt presentation controllers (ICPs) via the KVM_GET/SET_ONE_REG interface. Since there is one ICP per vcpu, we simply define a new 64-bit register in the ONE_REG space for the ICP state. The state includes the CPU priority setting, the pending IPI priority, and the priority and source number of any pending external interrupt.

---
 Documentation/virtual/kvm/api.txt   |    1 +
 arch/powerpc/include/asm/kvm_host.h |    2 +
 arch/powerpc/include/uapi/asm/kvm.h |   13 +++++
 arch/powerpc/kvm/book3s.c           |   19 ++++++++
 arch/powerpc/kvm/book3s_xics.c      |   92 +++++++++++++++++++++++++++++++++++
 5 files changed, 127 insertions(+)
Documentation/virtual/kvm/api.txt +1 -0
index 0ff9dcf..466636b 100644
@@ -1762,6 +1762,7 @@ registers, find a list below:
   PPC   | KVM_REG_PPC_VPA_DTL   | 128
   PPC   | KVM_REG_PPC_EPCR	| 32
   PPC   | KVM_REG_PPC_EPR	| 32
+  PPC   | KVM_REG_PPC_ICP_STATE | 64
 
 4.69 KVM_GET_ONE_REG
 
arch/powerpc/include/asm/kvm_host.h +2 -0
index 8af4c0b..2eb4c27 100644
@@ -236,6 +236,8 @@ struct kvm_irq_ctrler {
 	void (*free_ctrler)(struct kvm *kvm);
 	int (*hcall)(struct kvm_vcpu *vcpu, unsigned long req);
 	int (*ioctl)(struct kvm *kvm, unsigned int ioctl, unsigned long arg);
+	u64 (*get_one_reg)(struct kvm_vcpu *vcpu, u64 reg);
+	int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 reg, u64 val);
 };
 
 struct kvm_arch {
arch/powerpc/include/uapi/asm/kvm.h +13 -0
index d90743c..8e34553 100644
@@ -423,4 +423,17 @@ struct kvm_get_htab_header {
 #define KVM_REG_PPC_EPCR	(KVM_REG_PPC | KVM_REG_SIZE_U32 | 0x85)
 #define KVM_REG_PPC_EPR		(KVM_REG_PPC | KVM_REG_SIZE_U32 | 0x86)
 
+/* Per-vcpu interrupt controller state */
+#define KVM_REG_PPC_ICP_STATE	(KVM_REG_PPC | KVM_REG_SIZE_U64 | 0x87)
+
+/* Layout of above for XICS */
+#define  KVM_REG_PPC_ICP_CPPR_SHIFT	56	/* current proc priority */
+#define  KVM_REG_PPC_ICP_CPPR_MASK	0xff
+#define  KVM_REG_PPC_ICP_XISR_SHIFT	32	/* interrupt status field */
+#define  KVM_REG_PPC_ICP_XISR_MASK	0xffffff
+#define  KVM_REG_PPC_ICP_MFRR_SHIFT	24	/* pending IPI priority */
+#define  KVM_REG_PPC_ICP_MFRR_MASK	0xff
+#define  KVM_REG_PPC_ICP_PPRI_SHIFT	16	/* pending irq priority */
+#define  KVM_REG_PPC_ICP_PPRI_MASK	0xff
+
 #endif /* __LINUX_KVM_POWERPC_H */
arch/powerpc/kvm/book3s.c +19 -0
index c5a4478..8d5ee31 100644
@@ -529,6 +529,15 @@ int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
 			val = get_reg_val(reg->id, vcpu->arch.vscr.u[3]);
 			break;
 #endif /* CONFIG_ALTIVEC */
+		case KVM_REG_PPC_ICP_STATE: {
+			struct kvm_irq_ctrler *ic = vcpu->kvm->arch.irq_ctrler;
+			if (!ic) {
+				r = -ENXIO;
+				break;
+			}
+			val = get_reg_val(reg->id, ic->get_one_reg(vcpu, reg->id));
+			break;
+		}
 		default:
 			r = -EINVAL;
 			break;
@@ -591,6 +600,16 @@ int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
 			vcpu->arch.vscr.u[3] = set_reg_val(reg->id, val);
 			break;
 #endif /* CONFIG_ALTIVEC */
+		case KVM_REG_PPC_ICP_STATE: {
+			struct kvm_irq_ctrler *ic = vcpu->kvm->arch.irq_ctrler;
+
+			if (!ic) {
+				r = -ENXIO;
+				break;
+			}
+			r = ic->set_one_reg(vcpu, reg->id, set_reg_val(reg->id, val));
+			break;
+		}
 		default:
 			r = -EINVAL;
 			break;
arch/powerpc/kvm/book3s_xics.c +92 -0

104 lines not on this page · raw message

Signature · 1 line
1.7.10.rc3.219.g53414
#28 lore raw

PATCH 6/9 KVM: PPC: Book3S HV: Speed up wakeups of CPUs on HV KVM

In reply to Paul Mackerras

From: Benjamin Herrenschmidt <[email protected]>

Currently, we wake up a CPU by sending a host IPI with smp_send_reschedule() to thread 0 of that core, which will take all threads out of the guest, and cause them to re-evaluate their interrupt status on the way back in.

This adds a mechanism to differentiate real host IPIs from IPIs sent by KVM for guest threads to poke each other, in order to target the guest threads precisely when possible and avoid that global switch of the core to host state.

We then use this new facility in the in-kernel XICS code.

---
 arch/powerpc/include/asm/kvm_book3s_asm.h |    8 ++-
 arch/powerpc/include/asm/kvm_ppc.h        |   29 ++++++++
 arch/powerpc/kernel/asm-offsets.c         |    2 +
 arch/powerpc/kvm/book3s_hv.c              |   26 +++++++-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S   |  102 ++++++++++++++++++++++++-----
 arch/powerpc/kvm/book3s_xics.c            |    2 +-
 arch/powerpc/sysdev/xics/icp-native.c     |    8 +++
 7 files changed, 158 insertions(+), 19 deletions(-)
arch/powerpc/include/asm/kvm_book3s_asm.h +7 -1
index 88609b2..923522d 100644
@@ -20,6 +20,11 @@
 #ifndef __ASM_KVM_BOOK3S_ASM_H__
 #define __ASM_KVM_BOOK3S_ASM_H__
 
+/* XICS ICP register offsets */
+#define XICS_XIRR		4
+#define XICS_MFRR		0xc
+#define XICS_IPI		2	/* interrupt source # for IPIs */
+
 #ifdef __ASSEMBLY__
 
 #ifdef CONFIG_KVM_BOOK3S_HANDLER
@@ -81,10 +86,11 @@ struct kvmppc_host_state {
 #ifdef CONFIG_KVM_BOOK3S_64_HV
 	u8 hwthread_req;
 	u8 hwthread_state;
-
+	u8 host_ipi;
 	struct kvm_vcpu *kvm_vcpu;
 	struct kvmppc_vcore *kvm_vcore;
 	unsigned long xics_phys;
+	u32 saved_xirr;
 	u64 dabr;
 	u64 host_mmcr[3];
 	u32 host_pmc[8];
arch/powerpc/include/asm/kvm_ppc.h +29 -0
index 2308fff..f371af8a 100644
@@ -259,6 +259,21 @@ static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
 	paca[cpu].kvm_hstate.xics_phys = addr;
 }
 
+static inline u32 kvmppc_get_xics_latch(void)
+{
+	u32 xirr = get_paca()->kvm_hstate.saved_xirr;
+
+	get_paca()->kvm_hstate.saved_xirr = 0;
+
+	return xirr;
+}
+
+static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
+{
+	paca[cpu].kvm_hstate.host_ipi = host_ipi;
+}
+
+extern void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu);
 extern void kvm_linear_init(void);
 
 #else
@@ -268,6 +283,18 @@ static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
 static inline void kvm_linear_init(void)
 {}
 
+static inline u32 kvmppc_get_xics_latch(void)
+{
+	return 0;
+}
+
+static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
+{}
+
+static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
+{
+	kvm_vcpu_kick(vcpu);
+}
 #endif
 
 static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
@@ -332,4 +359,6 @@ static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
 	return ea;
 }
 
+extern void xics_wake_cpu(int cpu);
+
 #endif /* __POWERPC_KVM_PPC_H__ */
arch/powerpc/kernel/asm-offsets.c +2 -0
index 46f6afd..c564ac3 100644
@@ -543,6 +543,8 @@ int main(void)
 	HSTATE_FIELD(HSTATE_KVM_VCPU, kvm_vcpu);
 	HSTATE_FIELD(HSTATE_KVM_VCORE, kvm_vcore);
 	HSTATE_FIELD(HSTATE_XICS_PHYS, xics_phys);
+	HSTATE_FIELD(HSTATE_SAVED_XIRR, saved_xirr);
+	HSTATE_FIELD(HSTATE_HOST_IPI, host_ipi);
 	HSTATE_FIELD(HSTATE_MMCR, host_mmcr);
 	HSTATE_FIELD(HSTATE_PMC, host_pmc);
 	HSTATE_FIELD(HSTATE_PURR, host_purr);
arch/powerpc/kvm/book3s_hv.c +25 -1
index 8f09c36..1365440 100644
@@ -66,6 +66,31 @@
 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
 
+void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
+{
+	int me;
+	int cpu = vcpu->cpu;
+	wait_queue_head_t *wqp;
+
+	wqp = kvm_arch_vcpu_wq(vcpu);
+	if (waitqueue_active(wqp)) {
+		wake_up_interruptible(wqp);
+		++vcpu->stat.halt_wakeup;
+	}
+
+	me = get_cpu();
+
+	/* CPU points to the first thread of the core */
+	if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
+		int real_cpu = cpu + vcpu->arch.ptid;
+		if (paca[real_cpu].kvm_hstate.xics_phys)
+			xics_wake_cpu(real_cpu);
+		else if (cpu_online(cpu))
+			smp_send_reschedule(cpu);			
+	}
+	put_cpu();
+}
+
 /*
  * We use the vcpu_load/put functions to measure stolen time.
  * Stolen time is counted as time when either the vcpu is able to
@@ -985,7 +1010,6 @@ static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
 }
 
 extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
-extern void xics_wake_cpu(int cpu);
 
 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
 				   struct kvm_vcpu *vcpu)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +86 -16

134 lines not on this page · raw message

arch/powerpc/kvm/book3s_xics.c +1 -1
index f54b934..2027427 100644
@@ -220,7 +220,7 @@ static inline bool icp_try_update(struct kvmppc_icp *icp,
 		kvmppc_book3s_queue_irqprio(icp->vcpu,
 					    BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
 		if (!change_self)
-			kvm_vcpu_kick(icp->vcpu);
+			kvmppc_fast_vcpu_kick(icp->vcpu);
 	}
  bail:
 	return success;
arch/powerpc/sysdev/xics/icp-native.c +8 -0
index 48861d3..20b328b 100644
@@ -51,6 +51,12 @@ static struct icp_ipl __iomem *icp_native_regs[NR_CPUS];
 static inline unsigned int icp_native_get_xirr(void)
 {
 	int cpu = smp_processor_id();
+	unsigned int xirr;
+
+	/* Handled an interrupt latched by KVM */
+	xirr = kvmppc_get_xics_latch();
+	if (xirr)
+		return xirr;
 
 	return in_be32(&icp_native_regs[cpu]->xirr.word);
 }
@@ -138,6 +144,7 @@ static unsigned int icp_native_get_irq(void)
 
 static void icp_native_cause_ipi(int cpu, unsigned long data)
 {
+	kvmppc_set_host_ipi(cpu, 1);
 	icp_native_set_qirr(cpu, IPI_PRIORITY);
 }
 
@@ -151,6 +158,7 @@ static irqreturn_t icp_native_ipi_action(int irq, void *dev_id)
 {
 	int cpu = smp_processor_id();
 
+	kvmppc_set_host_ipi(cpu, 0);
 	icp_native_set_qirr(cpu, 0xff);
 
 	return smp_ipi_demux();
Signature · 1 line
1.7.10.rc3.219.g53414
#29 lore raw

PATCH 7/9 KVM: PPC: Book3S HV: Add support for real mode ICP in XICS emulation

In reply to Paul Mackerras

From: Benjamin Herrenschmidt <[email protected]>

This adds an implementation of the XICS hypercalls in real mode for HV KVM, which allows us to avoid exiting the guest MMU context on all threads for a variety of operations such as fetching a pending interrupt, EOI of messages, IPIs, etc.

---
 arch/powerpc/kvm/Makefile               |    1 +
 arch/powerpc/kvm/book3s_hv_rm_xics.c    |  402 +++++++++++++++++++++++++++++++
 arch/powerpc/kvm/book3s_hv_rmhandlers.S |   10 +-
 arch/powerpc/kvm/book3s_xics.c          |   64 ++++-
 arch/powerpc/kvm/book3s_xics.h          |   16 ++
 5 files changed, 475 insertions(+), 18 deletions(-)
 create mode 100644 arch/powerpc/kvm/book3s_hv_rm_xics.c
arch/powerpc/kvm/Makefile +1 -0
index e2eb04c..895e880 100644
@@ -77,6 +77,7 @@ kvm-book3s_64-builtin-objs-$(CONFIG_KVM_BOOK3S_64_HV) := \
 	book3s_hv_rm_mmu.o \
 	book3s_64_vio_hv.o \
 	book3s_hv_ras.o \
+	book3s_hv_rm_xics.o \
 	book3s_hv_builtin.o
 
 kvm-book3s_64-module-objs := \
arch/powerpc/kvm/book3s_hv_rm_xics.c new file +402 -0

402 lines not on this page · raw message

arch/powerpc/kvm/book3s_hv_rmhandlers.S +5 -5
index c84c39d..fe908f5 100644
@@ -1411,11 +1411,11 @@ hcall_real_table:
 	.long	0		/* 0x58 */
 	.long	0		/* 0x5c */
 	.long	0		/* 0x60 */
-	.long	0		/* 0x64 */
-	.long	0		/* 0x68 */
-	.long	0		/* 0x6c */
-	.long	0		/* 0x70 */
-	.long	0		/* 0x74 */
+	.long	.kvmppc_rm_h_eoi - hcall_real_table
+	.long	.kvmppc_rm_h_cppr - hcall_real_table
+	.long	.kvmppc_rm_h_ipi - hcall_real_table
+	.long	0		/* 0x70 - H_IPOLL */
+	.long	.kvmppc_rm_h_xirr - hcall_real_table
 	.long	0		/* 0x78 */
 	.long	0		/* 0x7c */
 	.long	0		/* 0x80 */
arch/powerpc/kvm/book3s_xics.c +51 -13

129 lines not on this page · raw message

arch/powerpc/kvm/book3s_xics.h +16 -0
index 0e20a51..2da27d7 100644
@@ -62,6 +62,20 @@ struct kvmppc_icp {
 	struct kvm_vcpu *vcpu;
 	union kvmppc_icp_state state;
 	unsigned long resend_map[ICP_RESEND_MAP_SIZE];
+
+	/* Real mode might find something too hard, here's the action
+	 * it might request from virtual mode
+	 */
+#define XICS_RM_KICK_VCPU	0x1
+#define XICS_RM_CHECK_RESEND	0x2
+#define XICS_RM_REJECT		0x4
+	u32 rm_action;
+	struct kvm_vcpu *rm_kick_target;
+	u32  rm_reject;
+
+	/* Debug stuff for real mode */
+	union kvmppc_icp_state rm_dbgstate;
+	struct kvm_vcpu *rm_dbgtgt;
 };
 
 struct kvmppc_ics {
@@ -74,6 +88,8 @@ struct kvmppc_xics {
 	struct kvm *kvm;
 	struct dentry *dentry;
 	u32 max_icsid;
+	bool real_mode;
+	bool real_mode_dbg;
 	struct kvmppc_ics *ics[KVMPPC_XICS_MAX_ICS_ID + 1];
 };
 
Signature · 1 line
1.7.10.rc3.219.g53414
#30 lore raw

PATCH 8/9 KVM: PPC: Book3S HV: Improve real-mode handling of external interrupts

In reply to Paul Mackerras

This streamlines our handling of external interrupts that come in while we're in the guest. First, when waking up a hardware thread that was napping, we split off the "napping due to H_CEDE" case earlier, and use the code that handles an external interrupt (0x500) in the guest to handle that too. Secondly, the code that handles those external interrupts now checks if any other thread is exiting to the host before bouncing an external interrupt to the guest, and also checks that there is actually an external interrupt pending for the guest before setting the LPCR MER bit (mediated external request).

This also makes sure that we clear the "ceded" flag when we handle a wakeup from cede in real mode, and fixes a potential infinite loop in kvmppc_run_vcpu() which can occur if we ever end up with the ceded flag set but MSR[EE] off.

---
 arch/powerpc/include/asm/reg.h          |    1 +
 arch/powerpc/kvm/book3s_hv.c            |    5 +-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S |  140 +++++++++++++++++--------------
 3 files changed, 81 insertions(+), 65 deletions(-)
arch/powerpc/include/asm/reg.h +1 -0
index 11ae3d8..abe34e0 100644
@@ -254,6 +254,7 @@
 #define     LPCR_PECE1	0x00002000	/* decrementer can cause exit */
 #define     LPCR_PECE2	0x00001000	/* machine check etc can cause exit */
 #define   LPCR_MER	0x00000800	/* Mediated External Exception */
+#define   LPCR_MER_SH	11
 #define   LPCR_LPES    0x0000000c
 #define   LPCR_LPES0   0x00000008      /* LPAR Env selector 0 */
 #define   LPCR_LPES1   0x00000004      /* LPAR Env selector 1 */
arch/powerpc/kvm/book3s_hv.c +4 -1
index 1365440..bd751a3 100644
@@ -1384,9 +1384,12 @@ static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
 			break;
 		vc->runner = vcpu;
 		n_ceded = 0;
-		list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
+		list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
 			if (!v->arch.pending_exceptions)
 				n_ceded += v->arch.ceded;
+			else
+				v->arch.ceded = 0;
+		}
 		if (n_ceded == vc->n_runnable)
 			kvmppc_vcore_blocked(vc);
 		else
arch/powerpc/kvm/book3s_hv_rmhandlers.S +76 -64

234 lines not on this page · raw message

Signature · 1 line
1.7.10.rc3.219.g53414
#31 lore raw

PATCH 9/9 KVM: PPC: Book3S: Add support for ibm,int-on/off RTAS calls

In reply to Paul Mackerras

This adds support for the ibm,int-on and ibm,int-off RTAS calls to the in-kernel XICS emulation and corrects the handling of the saved priority by the ibm,set-xive RTAS call. With this, ibm,int-off sets the specified interrupt's priority in its saved_priority field and sets the priority to 0xff (the least favoured value). ibm,int-on restores the saved_priority to the priority field, and ibm,set-xive sets both the priority and the saved_priority to the specified priority value.

---
 arch/powerpc/include/asm/kvm_ppc.h |    2 +
 arch/powerpc/kvm/book3s_rtas.c     |   40 ++++++++++++++
 arch/powerpc/kvm/book3s_xics.c     |  101 +++++++++++++++++++++++++++++-------
 arch/powerpc/kvm/book3s_xics.h     |    2 +-
 4 files changed, 125 insertions(+), 20 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +2 -0
index f371af8a..dd7c1fc 100644
@@ -171,6 +171,8 @@ extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
 extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
 extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, u32 priority);
 extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, u32 *priority);
+extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
+extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
 
 /*
  * Cuts out inst bits with ordering according to spec.
arch/powerpc/kvm/book3s_rtas.c +40 -0
index 6a6c1fe..fc1a749 100644
@@ -64,6 +64,44 @@ out:
 	args->rets[0] = rc;
 }
 
+static void kvm_rtas_int_off(struct kvm_vcpu *vcpu, struct rtas_args *args)
+{
+	u32 irq;
+	int rc;
+
+	if (args->nargs != 1 || args->nret != 1) {
+		rc = -3;
+		goto out;
+	}
+
+	irq = args->args[0];
+
+	rc = kvmppc_xics_int_off(vcpu->kvm, irq);
+	if (rc)
+		rc = -3;
+out:
+	args->rets[0] = rc;
+}
+
+static void kvm_rtas_int_on(struct kvm_vcpu *vcpu, struct rtas_args *args)
+{
+	u32 irq;
+	int rc;
+
+	if (args->nargs != 1 || args->nret != 1) {
+		rc = -3;
+		goto out;
+	}
+
+	irq = args->args[0];
+
+	rc = kvmppc_xics_int_on(vcpu->kvm, irq);
+	if (rc)
+		rc = -3;
+out:
+	args->rets[0] = rc;
+}
+
 struct rtas_handler {
 	void (*handler)(struct kvm_vcpu *vcpu, struct rtas_args *args);
 	char *name;
@@ -72,6 +110,8 @@ struct rtas_handler {
 static struct rtas_handler rtas_handlers[] = {
 	{ .name = "ibm,set-xive", .handler = kvm_rtas_set_xive },
 	{ .name = "ibm,get-xive", .handler = kvm_rtas_get_xive },
+	{ .name = "ibm,int-off",  .handler = kvm_rtas_int_off },
+	{ .name = "ibm,int-on",   .handler = kvm_rtas_int_on },
 };
 
 struct rtas_token_definition {
arch/powerpc/kvm/book3s_xics.c +82 -19

154 lines not on this page · raw message

arch/powerpc/kvm/book3s_xics.h +1 -1
index 2da27d7..9c1f1bb 100644
@@ -35,7 +35,7 @@ struct ics_irq_state {
 	u32 number;
 	u32 server;
 	u8  priority;
-	u8  saved_priority; /* currently unused */
+	u8  saved_priority;
 	u8  resend;
 	u8  masked_pending;
 	u8  asserted; /* Only for LSI */
Signature · 1 line
1.7.10.rc3.219.g53414
#10 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

In reply to Paul Mackerras
Quoted 6 lines · Paul Mackerras

From: Benjamin Herrenschmidt <[email protected]>

This adds in-kernel emulation of the XICS (eXternal Interrupt Controller Specification) interrupt controller specified by PAPR, for both HV and PR KVM guests.

Quoted 6 lines

This adds a new KVM_CREATE_IRQCHIP_ARGS ioctl, which is like KVM_CREATE_IRQCHIP in that it indicates that the virtual machine should use in-kernel interrupt controller emulation, but also takes an argument struct that contains the type of interrupt controller architecture and an optional parameter. Currently only one type value is defined, that which indicates the XICS architecture.

Would the device config API I posted a couple days ago work for you?

Quoted 6 lines

The XICS emulation supports up to 1048560 interrupt sources. Interrupt source numbers below 16 are reserved; 0 is used to mean no interrupt and 2 is used for IPIs. Internally these are represented in blocks of 1024, called ICS (interrupt controller source) entities, but that is not visible to userspace.

Quoted 6 lines

Two other new ioctls allow userspace to control the interrupt sources. The KVM_IRQCHIP_SET_SOURCES ioctl sets the priority, destination cpu, level/edge sensitivity and pending state of a range of interrupt sources, creating them if they don't already exist. The KVM_IRQCHIP_GET_SOURCES ioctl returns that information for a range of interrupt sources (they are required to already exist).

Why is it userspace's job to control these? If you use KVM_IRQ_PENDING for interrupt injection, what if there's a race with the user changing other flags via MMIO? Maybe this isn't an issue with XICS, but this is being presented as a generic API.

Documentation/virtual/kvm/api.txt associated with the service will be forgotten, and subsequent RTAS · line 2147

Quoted 4 lines

+4.80 KVM_CREATE_IRQCHIP_ARGS

Documentation/virtual/kvm/api.txt +3 -0
+
+Capability: KVM_CAP_IRQCHIP_ARGS
+Architectures: ppc

Why just ppc?

include/uapi/linux/kvm.h struct kvm_msi { · line 823

Quoted 5 lines
include/uapi/linux/kvm.h +5 -0
+struct kvm_irq_sources {
+	__u32 irq;
+	__u32 nr_irqs;
+	__u64 __user *irqbuf;
+};

Please no pointers in UAPI -- this would require a compat wrapper with 32-bit user and 64-bit kernel.

include/uapi/linux/kvm.h struct kvm_msi { · line 832

Quoted 8 lines
include/uapi/linux/kvm.h +8 -0
+/* irqbuf entries are laid out like this: */
+#define KVM_IRQ_SERVER_SHIFT	0
+#define KVM_IRQ_SERVER_MASK	0xffffffffULL
+#define KVM_IRQ_PRIORITY_SHIFT	32
+#define KVM_IRQ_PRIORITY_MASK	0xff
+#define KVM_IRQ_LEVEL_SENSITIVE	(1ULL << 40)
+#define KVM_IRQ_MASKED		(1ULL << 41)
+#define KVM_IRQ_PENDING		(1ULL << 42)

What does "server" mean? Do you mean "laid out like this for XICS"? Let's please have a clean separation between what is generic and what is implementation-specific.

-Scott

#11 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 7 lines · Scott Wood
Quoted 6 lines · Paul Mackerras

From: Benjamin Herrenschmidt <[email protected]>

This adds in-kernel emulation of the XICS (eXternal Interrupt Controller Specification) interrupt controller specified by PAPR, for both HV and PR KVM guests.

Quoted 8 lines
Quoted 6 lines

This adds a new KVM_CREATE_IRQCHIP_ARGS ioctl, which is like KVM_CREATE_IRQCHIP in that it indicates that the virtual machine should use in-kernel interrupt controller emulation, but also takes an argument struct that contains the type of interrupt controller architecture and an optional parameter. Currently only one type value is defined, that which indicates the XICS architecture.

Would the device config API I posted a couple days ago work for you?

I suppose it could be made to work. It doesn't feel like a natural fit though, because your API seems to assume (AFAICT) that a device is manipulated via registers at specific physical addresses, so I would have to invent an artificial set of registers with addresses and bit layouts, that aren't otherwise required. The XICS is operated from the guest side via hcalls, not via emulated MMIO.

Part of the reason I went with the API that I did is that that was what was agreed on at KVM Forum (as far as I can tell, not having been at the meeting). Your device API seems to be quite different to that.

Quoted 6 lines
Quoted 6 lines

The XICS emulation supports up to 1048560 interrupt sources. Interrupt source numbers below 16 are reserved; 0 is used to mean no interrupt and 2 is used for IPIs. Internally these are represented in blocks of 1024, called ICS (interrupt controller source) entities, but that is not visible to userspace.

Quoted 8 lines
Quoted 6 lines

Two other new ioctls allow userspace to control the interrupt sources. The KVM_IRQCHIP_SET_SOURCES ioctl sets the priority, destination cpu, level/edge sensitivity and pending state of a range of interrupt sources, creating them if they don't already exist. The KVM_IRQCHIP_GET_SOURCES ioctl returns that information for a range of interrupt sources (they are required to already exist).

Why is it userspace's job to control these? If you use KVM_IRQ_PENDING

These are primarily there to support live migration. For live migration, userspace needs to be able to extract the entire state of the virtual machine from the old guest, and then set the new guest to that exact same state. We have live migration working in qemu for pSeries guests with in-kernel XICS emulation using this interface. If you're not doing live migration, the only time userspace needs to use these is at initialization, when it does a SET_SOURCES to create the interrupt sources it wants the VM to have.

Quoted 3 lines

for interrupt injection, what if there's a race with the user changing other flags via MMIO? Maybe this isn't an issue with XICS, but this is being presented as a generic API.

They're not used while the guest is running, as I said, but even if they were, there is appropriate locking in there to handle any races.

Quoted 6 lines

Documentation/virtual/kvm/api.txt associated with the service will be forgotten, and subsequent RTAS · line 2147

Quoted 4 lines

+4.80 KVM_CREATE_IRQCHIP_ARGS

Documentation/virtual/kvm/api.txt +3 -0
+
+Capability: KVM_CAP_IRQCHIP_ARGS
+Architectures: ppc

Why just ppc?

Just because only PPC has code to handle it at this point. I would hope that ARM and others could pick it up. Maybe I should make it:

+Architectures: all (but so far only implemented on ppc)

or something.

Quoted 8 lines

include/uapi/linux/kvm.h struct kvm_msi { · line 823

Quoted 5 lines
include/uapi/linux/kvm.h +5 -0
+struct kvm_irq_sources {
+	__u32 irq;
+	__u32 nr_irqs;
+	__u64 __user *irqbuf;
+};

Please no pointers in UAPI -- this would require a compat wrapper with 32-bit user and 64-bit kernel.

Hmmm, you're right. I suppose it will have to be a fixed-size buffer, which is generally less efficient, but since it's mainly for migration that probably doesn't matter. In fact, I could probably use the existing KVM_GET_IRQCHIP and KVM_SET_IRQCHIP ioctls, if I use the chip_id' field for irq' and the pad' field for nr_irqs'.

Quoted 10 lines

include/uapi/linux/kvm.h struct kvm_msi { · line 832

Quoted 8 lines
include/uapi/linux/kvm.h +8 -0
+/* irqbuf entries are laid out like this: */
+#define KVM_IRQ_SERVER_SHIFT	0
+#define KVM_IRQ_SERVER_MASK	0xffffffffULL
+#define KVM_IRQ_PRIORITY_SHIFT	32
+#define KVM_IRQ_PRIORITY_MASK	0xff
+#define KVM_IRQ_LEVEL_SENSITIVE	(1ULL << 40)
+#define KVM_IRQ_MASKED		(1ULL << 41)
+#define KVM_IRQ_PENDING		(1ULL << 42)

What does "server" mean? Do you mean "laid out like this for XICS"?

Sorry, I should have made that "destination" rather than "server". You're right, "server" is confusing, but it just means "where the interrupt is sent to be handled". It has nothing particularly to do with "server" computers.

Quoted 2 lines

Let's please have a clean separation between what is generic and what is implementation-specific.

I believe that the interface is pretty cleanly generic - the model is a set of interrupt sources and some per-vcpu state, with priorities to decide which interrupts get delivered when. That describes the basics of just about any SMP-capable interrupt controller, including MPIC.

MPIC would still need an extra interface for userspace to save and restore things like the timer registers at live migration time, and for userspace to configure the base address, MPIC version, etc., of course.

Paul.

#12 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 18 lines · Paul Mackerras
Quoted 5 lines · Scott Wood
Quoted 4 lines · Paul Mackerras

From: Benjamin Herrenschmidt <[email protected]>

This adds in-kernel emulation of the XICS (eXternal Interrupt Controller Specification) interrupt controller specified by PAPR,

for

Quoted 5 lines
Quoted 5 lines

both HV and PR KVM guests.

This adds a new KVM_CREATE_IRQCHIP_ARGS ioctl, which is like KVM_CREATE_IRQCHIP in that it indicates that the virtual machine should use in-kernel interrupt controller emulation, but also

takes an

Quoted 2 lines
Quoted 2 lines

argument struct that contains the type of interrupt controller architecture and an optional parameter. Currently only one type

value

Quoted 2 lines
Quoted 1 line

is defined, that which indicates the XICS architecture.

Quoted 8 lines
Quoted 1 line

Would the device config API I posted a couple days ago work for you?

I suppose it could be made to work. It doesn't feel like a natural fit though, because your API seems to assume (AFAICT) that a device is manipulated via registers at specific physical addresses, so I would have to invent an artificial set of registers with addresses and bit layouts, that aren't otherwise required. The XICS is operated from the guest side via hcalls, not via emulated MMIO.

I don't think it makes such an assumption. The MPIC device has physical registers, so it exposes them, but it also exposes things that are not physical registers (e.g. the per-IRQ input state). The generic device control layer leaves interpretation of attributes up to the device.

I think it would be easier to fit XICS into the device control api model than to fit MPIC into this model, not to mention what would happen if we later want to emulate some other type of device -- x86 already has at least one non-irqchip emulated device (i8254).

Quoted 3 lines

Part of the reason I went with the API that I did is that that was what was agreed on at KVM Forum (as far as I can tell, not having been at the meeting). Your device API seems to be quite different to that.

I wasn't there either. It's fine to have discussions at such events, but it should not preclude others from having input, or better ideas from being considered afterward.

Quoted 19 lines
Quoted 2 lines
Quoted 2 lines

The XICS emulation supports up to 1048560 interrupt sources. Interrupt source numbers below 16 are reserved; 0 is used to mean

no

Quoted 1 line
Quoted 1 line

interrupt and 2 is used for IPIs. Internally these are

represented in

Quoted 1 line
Quoted 1 line

blocks of 1024, called ICS (interrupt controller source) entities,

but

Quoted 5 lines
Quoted 5 lines

that is not visible to userspace.

Two other new ioctls allow userspace to control the interrupt sources. The KVM_IRQCHIP_SET_SOURCES ioctl sets the priority, destination cpu, level/edge sensitivity and pending state of a

range

Quoted 1 line
Quoted 1 line

of interrupt sources, creating them if they don't already exist.

The

Quoted 1 line
Quoted 1 line

KVM_IRQCHIP_GET_SOURCES ioctl returns that information for a range

of

Quoted 2 lines
Quoted 1 line

interrupt sources (they are required to already exist).

Quoted 7 lines
Quoted 1 line

Why is it userspace's job to control these? If you use

KVM_IRQ_PENDING

These are primarily there to support live migration. For live migration, userspace needs to be able to extract the entire state of the virtual machine from the old guest, and then set the new guest to that exact same state.

In that case, the state it describes is insufficient for MPIC.

Quoted 3 lines

We have live migration working in qemu for
pSeries guests with in-kernel XICS emulation using this interface. If you're not doing live migration,

We don't yet, but would prefer not to assume that it'll never happen.

Quoted 8 lines
Quoted 1 line

for interrupt injection, what if there's a race with the user

changing

Quoted 1 line

other flags via MMIO? Maybe this isn't an issue with XICS, but

this is

Quoted 1 line

being presented as a generic API.

They're not used while the guest is running, as I said, but even if they were, there is appropriate locking in there to handle any races.

OK, KVM_IRQ_LINE is still used for interrupt injection. I was hoping to avoid going through a standardized interface that forces a global interrupt numberspace.

How do MSIs get injected?

BTW, do you have any plans regarding irqfd?

Quoted 11 lines
Quoted 7 lines

include/uapi/linux/kvm.h struct kvm_msi { · line 823

Quoted 5 lines
include/uapi/linux/kvm.h +5 -0
+struct kvm_irq_sources {
+	__u32 irq;
+	__u32 nr_irqs;
+	__u64 __user *irqbuf;
+};

Please no pointers in UAPI -- this would require a compat wrapper

with

Quoted 1 line

32-bit user and 64-bit kernel.

Hmmm, you're right. I suppose it will have to be a fixed-size buffer,

It doesn't need to be a fixed size buffer. You can still have pointers, but they need to be represented as a plain "__u64" with users casting the pointer.

Quoted 11 lines
Quoted 10 lines

include/uapi/linux/kvm.h struct kvm_msi { · line 832

Quoted 8 lines
include/uapi/linux/kvm.h +8 -0
+/* irqbuf entries are laid out like this: */
+#define KVM_IRQ_SERVER_SHIFT	0
+#define KVM_IRQ_SERVER_MASK	0xffffffffULL
+#define KVM_IRQ_PRIORITY_SHIFT	32
+#define KVM_IRQ_PRIORITY_MASK	0xff
+#define KVM_IRQ_LEVEL_SENSITIVE	(1ULL << 40)
+#define KVM_IRQ_MASKED		(1ULL << 41)
+#define KVM_IRQ_PENDING		(1ULL << 42)

What does "server" mean? Do you mean "laid out like this for XICS"?

Quoted 4 lines

Sorry, I should have made that "destination" rather than "server". You're right, "server" is confusing, but it just means "where the interrupt is sent to be handled". It has nothing particularly to do with "server" computers.

Right, I was aware of the IBM terminology here -- just thought it wasn't appropriate in a generic interface.

What about interrupt controllers that allow multiple destinations? More than 256 priorities? Different "levels" of output (normal, critical, machine check)? Programmable vector numbers? Active high/low control?

I just don't think irqchip state can be sanely made generic.

Quoted 8 lines
Quoted 1 line

Let's please have a clean separation between what is generic and

what is

Quoted 1 line

implementation-specific.

I believe that the interface is pretty cleanly generic - the model is a set of interrupt sources and some per-vcpu state, with priorities to decide which interrupts get delivered when. That describes the basics of just about any SMP-capable interrupt controller, including MPIC.

The per-vcpu state isn't even part of this AFAICT. It's an XICS-specific ONE_REG -- which is fine, but all that's left of the "generic" API is the get/set sources which is an imperfect match to our per-IRQ state and it's not clear how an implementation should extend it.

It would be straightforward to map it to the device control api by having XICS declare an attribute group that corresponds to IRQ sources (like KVM_DEV_MPIC_GRP_IRQ_ACTIVE) and the attribute data be what you've defined for kvm_irq_sources. Or if you want to preserve the "set multiple IRQs at once" approach, you could have an attribute that takes exactly a kvm_irq_sources struct, though it might be better to have a generic batched attribute set/get ioctl (as has been proposed for ONE_REG if the need arises).

Quoted 4 lines

MPIC would still need an extra interface for userspace to save and restore things like the timer registers at live migration time, and for userspace to configure the base address, MPIC version, etc., of course.

Yes, which pretty much means we need the device control api anyway -- or MPIC-specific ioctls, which I wanted to avoid.

-Scott

#13 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 34 lines · Scott Wood
Quoted 18 lines · Paul Mackerras
Quoted 5 lines · Scott Wood
Quoted 4 lines · Paul Mackerras

From: Benjamin Herrenschmidt <[email protected]>

This adds in-kernel emulation of the XICS (eXternal Interrupt Controller Specification) interrupt controller specified by

PAPR, for

Quoted 5 lines
Quoted 5 lines

both HV and PR KVM guests.

This adds a new KVM_CREATE_IRQCHIP_ARGS ioctl, which is like KVM_CREATE_IRQCHIP in that it indicates that the virtual machine should use in-kernel interrupt controller emulation, but also

takes an

Quoted 2 lines
Quoted 2 lines

argument struct that contains the type of interrupt controller architecture and an optional parameter. Currently only one

type value

Quoted 2 lines
Quoted 1 line

is defined, that which indicates the XICS architecture.

Quoted 8 lines
Quoted 1 line

Would the device config API I posted a couple days ago work for you?

I suppose it could be made to work. It doesn't feel like a natural fit though, because your API seems to assume (AFAICT) that a device is manipulated via registers at specific physical addresses, so I would have to invent an artificial set of registers with addresses and bit layouts, that aren't otherwise required. The XICS is operated from the guest side via hcalls, not via emulated MMIO.

I don't think it makes such an assumption. The MPIC device has physical registers, so it exposes them, but it also exposes things that are not physical registers (e.g. the per-IRQ input state). The generic device control layer leaves interpretation of attributes up to the device.

Quoted 4 lines

I think it would be easier to fit XICS into the device control api model than to fit MPIC into this model, not to mention what would happen if we later want to emulate some other type of device -- x86 already has at least one non-irqchip emulated device (i8254).

I have no particular objection to the device control API per se, but I have two objections to using it as the primary interface to the XICS emulation.

First, I dislike the magical side-effect where creating a device of a particular type (e.g. MPIC or XICS) automatically attaches it to the interrupt lines of the vcpus. I prefer an explicit request to do in-kernel interrupt control. Further, the magic means that you can only have one instance of the device, whereas you might want to model the interrupt controller architecture as several devices. You could do that using several device types, but then the interconnections between them would also be magic.

Secondly, it means that we are completely abandoning any attempt to define an abstract or generic interface to in-kernel interrupt controller emulations. Each device will have its own unique set of attribute groups and its own unique userspace code to drive it, with no commonality between them.

Quoted 7 lines
Quoted 3 lines

Part of the reason I went with the API that I did is that that was what was agreed on at KVM Forum (as far as I can tell, not having been at the meeting). Your device API seems to be quite different to that.

I wasn't there either. It's fine to have discussions at such events, but it should not preclude others from having input, or better ideas from being considered afterward.

Sure - I was just trying to fit in with the expressed wish of the maintainer.

Quoted 20 lines
Quoted 20 lines
Quoted 2 lines
Quoted 2 lines

The XICS emulation supports up to 1048560 interrupt sources. Interrupt source numbers below 16 are reserved; 0 is used to

mean no

Quoted 1 line
Quoted 1 line

interrupt and 2 is used for IPIs. Internally these are

represented in

Quoted 1 line
Quoted 1 line

blocks of 1024, called ICS (interrupt controller source)

entities, but

Quoted 5 lines
Quoted 5 lines

that is not visible to userspace.

Two other new ioctls allow userspace to control the interrupt sources. The KVM_IRQCHIP_SET_SOURCES ioctl sets the priority, destination cpu, level/edge sensitivity and pending state of a

range

Quoted 1 line
Quoted 1 line

of interrupt sources, creating them if they don't already

exist. The

Quoted 1 line
Quoted 1 line

KVM_IRQCHIP_GET_SOURCES ioctl returns that information for a

range of

Quoted 3 lines
Quoted 1 line

interrupt sources (they are required to already exist).

Why is it userspace's job to control these? If you use

Quoted 8 lines
Quoted 6 lines

KVM_IRQ_PENDING

These are primarily there to support live migration. For live migration, userspace needs to be able to extract the entire state of the virtual machine from the old guest, and then set the new guest to that exact same state.

In that case, the state it describes is insufficient for MPIC.

Yes, MPIC has other random stuff in it besides interrupt control, so that's not surprising.

Quoted 10 lines
Quoted 3 lines

We have live migration working in qemu for
pSeries guests with in-kernel XICS emulation using this interface. If you're not doing live migration,

We don't yet, but would prefer not to assume that it'll never happen.

Quoted 4 lines
Quoted 1 line

for interrupt injection, what if there's a race with the user

changing

Quoted 1 line

other flags via MMIO? Maybe this isn't an issue with XICS, but

this is

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

being presented as a generic API.

They're not used while the guest is running, as I said, but even if they were, there is appropriate locking in there to handle any races.

OK, KVM_IRQ_LINE is still used for interrupt injection. I was hoping to avoid going through a standardized interface that forces a global interrupt numberspace.

Why?

Quoted 1 line

How do MSIs get injected?

Just like other interrupts - from the point of view of the interrupt controller they're edge-triggered interrupt sources.

Quoted 1 line

BTW, do you have any plans regarding irqfd?

I'm going to look at that next.

Quoted 28 lines
Quoted 8 lines
Quoted 8 lines

include/uapi/linux/kvm.h struct kvm_msi { · line 823

Quoted 6 lines
include/uapi/linux/kvm.h +5 -0
+struct kvm_irq_sources {
+	__u32 irq;
+	__u32 nr_irqs;
+	__u64 __user *irqbuf;
+};

Please no pointers in UAPI -- this would require a compat

Quoted 4 lines

wrapper with

Quoted 1 line

32-bit user and 64-bit kernel.

Hmmm, you're right. I suppose it will have to be a fixed-size buffer,

It doesn't need to be a fixed size buffer. You can still have pointers, but they need to be represented as a plain "__u64" with users casting the pointer.

Quoted 11 lines
Quoted 10 lines

include/uapi/linux/kvm.h struct kvm_msi { · line 832

Quoted 8 lines
include/uapi/linux/kvm.h +8 -0
+/* irqbuf entries are laid out like this: */
+#define KVM_IRQ_SERVER_SHIFT	0
+#define KVM_IRQ_SERVER_MASK	0xffffffffULL
+#define KVM_IRQ_PRIORITY_SHIFT	32
+#define KVM_IRQ_PRIORITY_MASK	0xff
+#define KVM_IRQ_LEVEL_SENSITIVE	(1ULL << 40)
+#define KVM_IRQ_MASKED		(1ULL << 41)
+#define KVM_IRQ_PENDING		(1ULL << 42)

What does "server" mean? Do you mean "laid out like this for XICS"?

Quoted 7 lines
Quoted 4 lines

Sorry, I should have made that "destination" rather than "server". You're right, "server" is confusing, but it just means "where the interrupt is sent to be handled". It has nothing particularly to do with "server" computers.

Right, I was aware of the IBM terminology here -- just thought it wasn't appropriate in a generic interface.

Sure.

Quoted 1 line

What about interrupt controllers that allow multiple destinations?

The destination can be an identifier for a group of vcpus, or even a bitmap -- that's why I made it 32 bits.

Quoted 3 lines

More than 256 priorities? Different "levels" of output (normal, critical, machine check)? Programmable vector numbers? Active high/low control?

There are plenty of bits free in the 64 bits per source that I have allowed. We can accommodate those things. (BTW, I think having more than 256 priorities would be insane - do you know of any actual example that does?)

Quoted 11 lines

I just don't think irqchip state can be sanely made generic.

Quoted 8 lines
Quoted 1 line

Let's please have a clean separation between what is generic and

what is

Quoted 1 line

implementation-specific.

I believe that the interface is pretty cleanly generic - the model is a set of interrupt sources and some per-vcpu state, with priorities to decide which interrupts get delivered when. That describes the basics of just about any SMP-capable interrupt controller, including MPIC.

Quoted 5 lines

The per-vcpu state isn't even part of this AFAICT. It's an XICS-specific ONE_REG -- which is fine, but all that's left of the "generic" API is the get/set sources which is an imperfect match to our per-IRQ state and it's not clear how an implementation should extend it.

Yes, the names of the bitfields in the ICP state word are XICS-specific, but the concepts are pretty generic - current processor priority, identifier for interrupt awaiting service, pending IPI request priority, pending interrupt request priority.

Quoted 8 lines

It would be straightforward to map it to the device control api by having XICS declare an attribute group that corresponds to IRQ sources (like KVM_DEV_MPIC_GRP_IRQ_ACTIVE) and the attribute data be what you've defined for kvm_irq_sources. Or if you want to preserve the "set multiple IRQs at once" approach, you could have an attribute that takes exactly a kvm_irq_sources struct, though it might be better to have a generic batched attribute set/get ioctl (as has been proposed for ONE_REG if the need arises).

Sure, the device control API can probably do just about anything. We could even use it to create and control vcpus and memory slots, but that doesn't mean we should.

Quoted 7 lines
Quoted 4 lines

MPIC would still need an extra interface for userspace to save and restore things like the timer registers at live migration time, and for userspace to configure the base address, MPIC version, etc., of course.

Yes, which pretty much means we need the device control api anyway -- or MPIC-specific ioctls, which I wanted to avoid.

I agree that MPIC has enough extra stuff in it, besides interrupt control, that it probably needs your device control API, at least for that extra stuff.

Paul.

#14 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 8 lines · Paul Mackerras

I have no particular objection to the device control API per se, but I have two objections to using it as the primary interface to the XICS emulation.

First, I dislike the magical side-effect where creating a device of a particular type (e.g. MPIC or XICS) automatically attaches it to the interrupt lines of the vcpus. I prefer an explicit request to do in-kernel interrupt control.

OK. This is device-specific behavior, so you could define it differently for XICS than MPIC. I suppose we could change it for MPIC as well, to leave an opening for the unlikely case where we'd want to model an MPIC that isn't directly connected to the CPUs.

How is the explicit request made in this patchset?

Quoted 5 lines

Secondly, it means that we are completely abandoning any attempt to define an abstract or generic interface to in-kernel interrupt controller emulations. Each device will have its own unique set of attribute groups and its own unique userspace code to drive it, with no commonality between them.

Yes. I am unconvinced that such an abstraction is well-advised (especially after seeing existing "generic" interfaces that are clearly APIC-oriented). This isn't like normal driver interfaces where we're abstracting away hardware differences to let generic code use a device. Userspace knows what kind of device it wants, and how it wants it to integrate with the rest of the emulated system. We'd have to go out of our way to apply the abstraction on *both* ends. What do we get from that other than a chance that the abstraction leaks? What significant code actually becomes common? kvm_set_irq() is just a thin wrapper around the ioctl.

Quoted 15 lines
Quoted 5 lines
Quoted 3 lines

We have live migration working in qemu for
pSeries guests with in-kernel XICS emulation using this interface. If you're not doing live migration,

We don't yet, but would prefer not to assume that it'll never

happen.

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

for interrupt injection, what if there's a race with the user

changing

Quoted 1 line

other flags via MMIO? Maybe this isn't an issue with XICS, but

this is

Quoted 1 line

being presented as a generic API.

They're not used while the guest is running, as I said, but even if they were, there is appropriate locking in there to handle any

Quoted 7 lines

races.

Quoted 4 lines

OK, KVM_IRQ_LINE is still used for interrupt injection. I was hoping to avoid going through a standardized interface that forces a global interrupt numberspace.

Why?

The standardized interface doesn't make things any easier (as noted above, the caller is already mpic-specific code), and we'd have to come up with a scheme for flattening our interrupt numberspace (rather than introduce new attribute groups for things like IPI and timer interrupts). It may still be necessary when it comes to irqfd, though...

Quoted 4 lines
Quoted 1 line

How do MSIs get injected?

Just like other interrupts - from the point of view of the interrupt controller they're edge-triggered interrupt sources.

Ah right, I guess this is all set up via hcalls for XICS.

With MPIC exposing its registers via the device control api, everything just works -- the PCI device generates a write to the MPIC's memory region, the QEMU MPIC stub sends the write to the kernel as for any other MMIO access (this passthrough is also useful for debugging), the in-kernel MPIC sees the write to the "generate an MSI" register and does its thing. Compare that to all special the MSI code for APIC... :-)

Quoted 3 lines
Quoted 1 line

BTW, do you have any plans regarding irqfd?

I'm going to look at that next.

Likewise... We should probably coordinate our efforts so that at least the de-APICization of the code only has to get done once.

Quoted 4 lines
Quoted 1 line

What about interrupt controllers that allow multiple destinations?

The destination can be an identifier for a group of vcpus, or even a bitmap -- that's why I made it 32 bits.

So you can have single delivery, or be limited to 32 vcpus, or have to implement some destination ID allocation scheme (which is more state that needs to be accessible somehow).

Quoted 6 lines
Quoted 3 lines

More than 256 priorities? Different "levels" of output (normal, critical, machine check)? Programmable vector numbers? Active high/low control?

There are plenty of bits free in the 64 bits per source that I have allowed. We can accommodate those things.

MPIC vector numbers take up 16 of the bits. The architected interrupt level field is 8 bits, though only a handful of values are actually needed. Add a couple binary flags, and it gets pretty tight if a third type of interrupt controller starts wanting something new.

Quoted 3 lines

(BTW, I think having more
than 256 priorities would be insane - do you know of any actual example that does?)

No, but hardware designers have been known to do insane things.

Quoted 10 lines
Quoted 5 lines

The per-vcpu state isn't even part of this AFAICT. It's an XICS-specific ONE_REG -- which is fine, but all that's left of the "generic" API is the get/set sources which is an imperfect match to our per-IRQ state and it's not clear how an implementation should extend it.

Yes, the names of the bitfields in the ICP state word are XICS-specific, but the concepts are pretty generic - current processor priority, identifier for interrupt awaiting service, pending IPI request priority, pending interrupt request priority.

We don't have separate concepts of "pending IPI request priority" and "pending interrupt request priority". There can be multiple interrupts awaiting service (or even in service, if different priorities). We have both "current task priority" (which is a user-set mask-by-priority register) and the priority of the highest-prio in-service interrupt -- which would "current processor priorty" be? Etc.

-Scott

#15 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 10 lines · Scott Wood
Quoted 8 lines · Paul Mackerras

I have no particular objection to the device control API per se, but I have two objections to using it as the primary interface to the XICS emulation.

First, I dislike the magical side-effect where creating a device of a particular type (e.g. MPIC or XICS) automatically attaches it to the interrupt lines of the vcpus. I prefer an explicit request to do in-kernel interrupt control.

Quoted 4 lines

OK. This is device-specific behavior, so you could define it differently for XICS than MPIC. I suppose we could change it for MPIC as well, to leave an opening for the unlikely case where we'd want to model an MPIC that isn't directly connected to the CPUs.

You can have cascaded MPICs; I once had a powerbook that had one MPIC cascaded into another.

Quoted 1 line

How is the explicit request made in this patchset?

The KVM_CREATE_IRQCHIP_ARGS ioctl says that you want emulation of a specific interrupt controller architecture connected to the vcpus' external interrupt inputs. In that sense it's explicit, compared to a generic "create device" ioctl that could be for any device.

Quoted 6 lines
Quoted 5 lines

Secondly, it means that we are completely abandoning any attempt to define an abstract or generic interface to in-kernel interrupt controller emulations. Each device will have its own unique set of attribute groups and its own unique userspace code to drive it, with no commonality between them.

Quoted 10 lines

Yes. I am unconvinced that such an abstraction is well-advised (especially after seeing existing "generic" interfaces that are clearly APIC-oriented). This isn't like normal driver interfaces where we're abstracting away hardware differences to let generic code use a device. Userspace knows what kind of device it wants, and how it wants it to integrate with the rest of the emulated system. We'd have to go out of our way to apply the abstraction on *both* ends. What do we get from that other than a chance that the abstraction leaks? What significant code actually becomes common? kvm_set_irq() is just a thin wrapper around the ioctl.

I'd think there could be some code reduction in the live migration code, but I'd need a qemu hacker to chime in here.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

OK, KVM_IRQ_LINE is still used for interrupt injection. I was hoping to avoid going through a standardized interface that forces a global interrupt numberspace.

Quoted 8 lines
Quoted 1 line

Why?

The standardized interface doesn't make things any easier (as noted above, the caller is already mpic-specific code), and we'd have to come up with a scheme for flattening our interrupt numberspace (rather than introduce new attribute groups for things like IPI and timer interrupts). It may still be necessary when it comes to irqfd, though...

With 24 bits of interrupt source number, you don't have to flatten it very far. IPIs are handled separately and don't appear in the interrupt source space.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

How do MSIs get injected?

Just like other interrupts - from the point of view of the interrupt controller they're edge-triggered interrupt sources.

Ah right, I guess this is all set up via hcalls for XICS.

Quoted 7 lines

With MPIC exposing its registers via the device control api, everything just works -- the PCI device generates a write to the MPIC's memory region, the QEMU MPIC stub sends the write to the kernel as for any other MMIO access (this passthrough is also useful for debugging), the in-kernel MPIC sees the write to the "generate an MSI" register and does its thing. Compare that to all special the MSI code for APIC... :-)

You're doing a round trip to userspace for every MPIC register access by the guest? Seriously? If that's so, then why bother with in-kernel emulation at all? Once you're back in userspace, it's just as fast to do the emulation there as in the kernel.

Quoted 8 lines
Quoted 2 lines

There are plenty of bits free in the 64 bits per source that I have allowed. We can accommodate those things.

MPIC vector numbers take up 16 of the bits. The architected interrupt level field is 8 bits, though only a handful of values are actually needed. Add a couple binary flags, and it gets pretty tight if a third type of interrupt controller starts wanting something new.

There's enough space for MPIC to have 16 bits of vector and some flags. We don't need to overdesign this.

Quoted 7 lines
Quoted 4 lines

Yes, the names of the bitfields in the ICP state word are XICS-specific, but the concepts are pretty generic - current processor priority, identifier for interrupt awaiting service, pending IPI request priority, pending interrupt request priority.

We don't have separate concepts of "pending IPI request priority" and "pending interrupt request priority". There can be multiple

Sorry, I meant "pending interrupt request". You do have that, it's what you read from the interrupt acknowledge register.

Quoted 5 lines

interrupts awaiting service (or even in service, if different priorities). We have both "current task priority" (which is a user-set mask-by-priority register) and the priority of the highest-prio in-service interrupt -- which would "current processor priorty" be? Etc.

It would be the current task priority. I assume MPIC maintains a 16-bit map of the interrupt priorities in service, so that would need to be added. (XICS doesn't maintain the stack in hardware, which would require 256 bits of state, but instead gets software to do that.)

Paul.

#16 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 15 lines · Paul Mackerras
Quoted 2 lines · Scott Wood
Quoted 1 line · Paul Mackerras

I have no particular objection to the device control API per se,

but

Quoted 1 line
Quoted 1 line

I have two objections to using it as the primary interface to the

XICS

Quoted 3 lines
Quoted 3 lines

emulation.

First, I dislike the magical side-effect where creating a device

of a

Quoted 1 line
Quoted 1 line

particular type (e.g. MPIC or XICS) automatically attaches it to

the

Quoted 3 lines
Quoted 2 lines

interrupt lines of the vcpus. I prefer an explicit request to do in-kernel interrupt control.

Quoted 7 lines
Quoted 4 lines

OK. This is device-specific behavior, so you could define it differently for XICS than MPIC. I suppose we could change it for MPIC as well, to leave an opening for the unlikely case where we'd want to model an MPIC that isn't directly connected to the CPUs.

You can have cascaded MPICs; I once had a powerbook that had one MPIC cascaded into another.

OK.

Quoted 6 lines
Quoted 1 line

How is the explicit request made in this patchset?

The KVM_CREATE_IRQCHIP_ARGS ioctl says that you want emulation of a specific interrupt controller architecture connected to the vcpus' external interrupt inputs. In that sense it's explicit, compared to a generic "create device" ioctl that could be for any device.

Hooking up to the CPU's interrupt lines is implicit in creating an MPIC (and I'm fine with changing that), not in creating any device. I don't see how it's worse than being implicit in calling KVM_CREATE_IRQCHIP_ARGS (which doesn't allow for cascaded irqchips).

Quoted 10 lines
Quoted 6 lines

The standardized interface doesn't make things any easier (as noted above, the caller is already mpic-specific code), and we'd have to come up with a scheme for flattening our interrupt numberspace (rather than introduce new attribute groups for things like IPI and timer interrupts). It may still be necessary when it comes to irqfd, though...

With 24 bits of interrupt source number, you don't have to flatten it very far. IPIs are handled separately and don't appear in the interrupt source space.

They do need to appear in the interrupt source space if we want to inject or irqfd them. Most users won't want to do that, but we have had a customer directly assign IPIs (to communicate with an OS running on the other CPU in an AMP setup -- host Linux was non-SMP so wasn't using them) and MPIC timers to a guest.

Quoted 16 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

How do MSIs get injected?

Just like other interrupts - from the point of view of the

interrupt

Quoted 11 lines
Quoted 1 line

controller they're edge-triggered interrupt sources.

Ah right, I guess this is all set up via hcalls for XICS.

With MPIC exposing its registers via the device control api, everything just works -- the PCI device generates a write to the MPIC's memory region, the QEMU MPIC stub sends the write to the kernel as for any other MMIO access (this passthrough is also useful for debugging), the in-kernel MPIC sees the write to the "generate an MSI" register and does its thing. Compare that to all special the MSI code for APIC... :-)

Quoted 2 lines

You're doing a round trip to userspace for every MPIC register access by the guest? Seriously?

No. Accesses by the guest get handled in the kernel. Accesses in QEMU, including MSIs generated by virtio, get forwarded to the kernel.

Quoted 11 lines
Quoted 8 lines
Quoted 2 lines

There are plenty of bits free in the 64 bits per source that I have allowed. We can accommodate those things.

MPIC vector numbers take up 16 of the bits. The architected interrupt level field is 8 bits, though only a handful of values are actually needed. Add a couple binary flags, and it gets pretty tight if a third type of interrupt controller starts wanting something new.

There's enough space for MPIC to have 16 bits of vector and some flags. We don't need to overdesign this.

I view anything other than passing the actual MPIC register values around as overdesign here, given that it is communication between hw/kvm/mpic.c on the QEMU side and arch/powerpc/kvm/mpic.c on the kernel side.

Quoted 9 lines
Quoted 5 lines

interrupts awaiting service (or even in service, if different priorities). We have both "current task priority" (which is a user-set mask-by-priority register) and the priority of the highest-prio in-service interrupt -- which would "current processor priorty" be? Etc.

It would be the current task priority. I assume MPIC maintains a 16-bit map of the interrupt priorities in service, so that would need to be added.

We don't maintain such a map in the emulation code. We have a per-CPU bitmap of the actual interrupt sources pending/active, which is another attribute that would need to be added in order to support migration on MPIC.

-Scott

#17 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 10 lines · Scott Wood
Quoted 4 lines · Paul Mackerras

The KVM_CREATE_IRQCHIP_ARGS ioctl says that you want emulation of a specific interrupt controller architecture connected to the vcpus' external interrupt inputs. In that sense it's explicit, compared to a generic "create device" ioctl that could be for any device.

Hooking up to the CPU's interrupt lines is implicit in creating an MPIC (and I'm fine with changing that), not in creating any device. I don't see how it's worse than being implicit in calling KVM_CREATE_IRQCHIP_ARGS (which doesn't allow for cascaded irqchips).

First, KVM_CREATE_IRQCHIP_ARGS specifies the overall architecture of the interrupt control subsystem, so yes it does allow for cascaded controllers.

Secondly, the difference is that if you see a KVM_CREATE_IRQCHIP_ARGS call, you know that the vcpus' interrupt inputs will be driven by kernel code. If you see a KVM_CREATE_DEVICE call, you don't know that; they might be, or they might not be.

Quoted 6 lines
Quoted 2 lines

You're doing a round trip to userspace for every MPIC register access by the guest? Seriously?

No. Accesses by the guest get handled in the kernel. Accesses in QEMU, including MSIs generated by virtio, get forwarded to the kernel.

OK, I missed the path where that gets done, then.

Quoted 5 lines
Quoted 3 lines

It would be the current task priority. I assume MPIC maintains a 16-bit map of the interrupt priorities in service, so that would need to be added.

We don't maintain such a map in the emulation code. We have a

Oh, so how do you handle EOI of nested interrupts? How do you know what to reset the CPU priority to in that case?

Quoted 3 lines

per-CPU bitmap of the actual interrupt sources pending/active, which is another attribute that would need to be added in order to support migration on MPIC.

Not really, that can be recomputed from the sources easily enough.

Paul.

#18 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 8 lines · Paul Mackerras
Quoted 4 lines · Scott Wood
Quoted 3 lines · Paul Mackerras

The KVM_CREATE_IRQCHIP_ARGS ioctl says that you want emulation of a specific interrupt controller architecture connected to the vcpus' external interrupt inputs. In that sense it's explicit, compared

to a

Quoted 2 lines
Quoted 1 line

generic "create device" ioctl that could be for any device.

Quoted 8 lines
Quoted 4 lines

Hooking up to the CPU's interrupt lines is implicit in creating an MPIC (and I'm fine with changing that), not in creating any device. I don't see how it's worse than being implicit in calling KVM_CREATE_IRQCHIP_ARGS (which doesn't allow for cascaded irqchips).

First, KVM_CREATE_IRQCHIP_ARGS specifies the overall architecture of the interrupt control subsystem, so yes it does allow for cascaded controllers.

Fine, but in that case you're dealing with a new irqchip (or irqarch if you prefer) type number (or param). If you wanted to take that approach with cascaded MPICs (I wouldn't), you would create a new device type number. I don't see the difference here.

Quoted 4 lines

Secondly, the difference is that if you see a KVM_CREATE_IRQCHIP_ARGS call, you know that the vcpus' interrupt inputs will be driven by kernel code. If you see a KVM_CREATE_DEVICE call, you don't know that; they might be, or they might not be.

I just don't understand what you mean here. Nobody's suggesting that we make this assumption as soon as you see a "KVM_CREATE_DEVICE" call for any random device. It's specifically in the creation of a KVM_DEV_TYPE_FSL_MPIC_20 or KVM_DEV_TYPE_FSL_MPIC_42 that this assumption is currently made. I don't see how creation of one of those specific devices is any different from calling KVM_CREATE_IRQCHIP_ARGS in terms of the intent that can reasonably be inferred.

Quoted 9 lines
Quoted 1 line
Quoted 1 line

You're doing a round trip to userspace for every MPIC register

access

Quoted 5 lines
Quoted 1 line

by the guest? Seriously?

No. Accesses by the guest get handled in the kernel. Accesses in QEMU, including MSIs generated by virtio, get forwarded to the kernel.

OK, I missed the path where that gets done, then.

ppce500_pci has a memory region for e500-pci-bar0 which is an alias for the ccsr memory region. The mpic registers are a child of the ccsr memory region. When hw/kvm/mpic.c in QEMU sees an access to that mpic memory region, it forwards the access to the kernel via KVM_DEV_MPIC_GRP_REGISTER.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

It would be the current task priority. I assume MPIC maintains a 16-bit map of the interrupt priorities in service, so that would

need

Quoted 3 lines
Quoted 1 line

to be added.

We don't maintain such a map in the emulation code. We have a

Oh, so how do you handle EOI of nested interrupts?

We scan the in-service bitmap for pending interrupts, and choose the one that has the highest priority as the one that the EOI must be referring to.

Just having a bitmap of priorities would only tell us the priority of the highest priority in-service interrupt, not which actual interrupt it is.

Quoted 1 line

How do you know what to reset the CPU priority to in that case?

Once we identify the interrupt that is being EOId as above, we clear that bit and check again to see if there's a remaining interrupt whose priority is high enough to prevent a pending interrupt from being delivered.

Quoted 5 lines
Quoted 3 lines

per-CPU bitmap of the actual interrupt sources pending/active, which is another attribute that would need to be added in order to support migration on MPIC.

Not really, that can be recomputed from the sources easily enough.

I'm skeptical. IPIs at least would be a problem, as would other multicast interrupts if we allowed that. How would we distinguish an interrupt that is pending from one that is in-service, just from the sources?

In any case, it's a bit premature to discuss what we'd need for migration until QEMU itself can save/restore a normal QEMU openpic. When that time comes, attributes can be added for whatever extra state we need (if any) to extend that capability to in-kernel MPICs.

-Scott

#19 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

In reply to Paul Mackerras
Quoted 40 lines · Paul Mackerras
Quoted 34 lines · Scott Wood
Quoted 18 lines · Paul Mackerras
Quoted 5 lines · Scott Wood
Quoted 4 lines · Paul Mackerras

From: Benjamin Herrenschmidt <[email protected]>

This adds in-kernel emulation of the XICS (eXternal Interrupt Controller Specification) interrupt controller specified by

PAPR, for

Quoted 5 lines
Quoted 5 lines

both HV and PR KVM guests.

This adds a new KVM_CREATE_IRQCHIP_ARGS ioctl, which is like KVM_CREATE_IRQCHIP in that it indicates that the virtual machine should use in-kernel interrupt controller emulation, but also

takes an

Quoted 2 lines
Quoted 2 lines

argument struct that contains the type of interrupt controller architecture and an optional parameter. Currently only one

type value

Quoted 2 lines
Quoted 1 line

is defined, that which indicates the XICS architecture.

Quoted 8 lines
Quoted 1 line

Would the device config API I posted a couple days ago work for you?

I suppose it could be made to work. It doesn't feel like a natural fit though, because your API seems to assume (AFAICT) that a device is manipulated via registers at specific physical addresses, so I would have to invent an artificial set of registers with addresses and bit layouts, that aren't otherwise required. The XICS is operated from the guest side via hcalls, not via emulated MMIO.

I don't think it makes such an assumption. The MPIC device has physical registers, so it exposes them, but it also exposes things that are not physical registers (e.g. the per-IRQ input state). The generic device control layer leaves interpretation of attributes up to the device.

Quoted 4 lines

I think it would be easier to fit XICS into the device control api model than to fit MPIC into this model, not to mention what would happen if we later want to emulate some other type of device -- x86 already has at least one non-irqchip emulated device (i8254).

Quoted 8 lines

I have no particular objection to the device control API per se, but I have two objections to using it as the primary interface to the XICS emulation.

First, I dislike the magical side-effect where creating a device of a particular type (e.g. MPIC or XICS) automatically attaches it to the interrupt lines of the vcpus. I prefer an explicit request to do in-kernel interrupt control.

This is probably a stupid question, but why the KVM_SET_IRQCHIP/KVM_SET_GSI_ROUTING interface is not appropriate for your purposes?

x86 sets up a default GSI->IRQCHIP PIN mapping on creation (during KVM_SET_IRQCHIP), but it can be modified with KVM_SET_GSI_ROUTING.

Quoted 11 lines

Further, the magic means that you can
only have one instance of the device, whereas you might want to model the interrupt controller architecture as several devices. You could do that using several device types, but then the interconnections between them would also be magic.

Secondly, it means that we are completely abandoning any attempt to define an abstract or generic interface to in-kernel interrupt controller emulations. Each device will have its own unique set of attribute groups and its own unique userspace code to drive it, with no commonality between them.

<snip>

#20 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 6 lines · Marcelo Tosatti

This is probably a stupid question, but why the KVM_SET_IRQCHIP/KVM_SET_GSI_ROUTING interface is not appropriate for your purposes?

x86 sets up a default GSI->IRQCHIP PIN mapping on creation (during KVM_SET_IRQCHIP), but it can be modified with KVM_SET_GSI_ROUTING.

To start, the whole IRQ routing stuff is poorly documented.

Am I supposed to make up GSI numbers and use the routing thing to associate them with real interrupts? Are there constraints on what sort of GSI numbers I can choose (I now see in the code that KVM_MAX_IRQ_ROUTES is returned from the capability check, but where is that documented? It looks like the APIC implementation has default routes, where is that documented?)? Where does the code live to manage this table, and how APICy is it (looks like the answer is "irq_comm.c, and very")? I suppose I could write another implementation of the table management code for MPIC, though the placement of "irqchip" inside the route entry, rather than as an argument to KVM_IRQ_LINE, suggests the table is supposed to be global, not in the individual interrupt controller.

It looks like I'm going to have to do this anyway for irqfd, though that doesn't make the other uses of the device control api go away. Even KVM_DEV_MPIC_GRP_IRQ_ACTIVE would still be useful for reading the status for debugging (reading device registers doesn't quite do it, since the "active" bit won't show up if the interrupt is masked). At that point, is it more offensive to make it read-only even though it would be trivial to make it read/write (which would allow users who don't need it to bypass the routing API), or to make it read/write and live with there being more than one way to do something?

KVM_SET_IRQCHIP is not suitable because we have more than 512 bytes of state, and because it doesn't allow debugging access to device registers (e.g. inspecting from the QEMU command line), and because it's hard to add new pieces of state if we realize we left something out. It reminds be of GET/SET_SREGS. With that, I did what you seem to want here, which is to adapt the existing interfaces, using feature flags to control optional state. It ended up being a mess, and ONE_REG was introduced as a replacement. The device control API is the equivalent of ONE_REG for things other than vcpus.

-Scott

#21 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 5 lines · Scott Wood
Quoted 4 lines · Marcelo Tosatti

This is probably a stupid question, but why the KVM_SET_IRQCHIP/KVM_SET_GSI_ROUTING interface is not appropriate for your purposes?

Quoted 7 lines
Quoted 2 lines

x86 sets up a default GSI->IRQCHIP PIN mapping on creation (during KVM_SET_IRQCHIP), but it can be modified with KVM_SET_GSI_ROUTING.

To start, the whole IRQ routing stuff is poorly documented.

Am I supposed to make up GSI numbers and use the routing thing to associate them with real interrupts?

I have no idea. Is mapping from one integer linear space (GSIs) to real interrupts suitable for you?

Quoted 4 lines

Are there constraints on what
sort of GSI numbers I can choose (I now see in the code that KVM_MAX_IRQ_ROUTES is returned from the capability check, but where is that documented?

Don't think it is.

Quoted 2 lines

It looks like the APIC implementation has
default routes, where is that documented?)?

In the code.

Quoted 2 lines

Where does the code live to manage this table, and how APICy is it (looks like the answer is "irq_comm.c, and very")?

Thinking faster than typing? Not sure what you mean.

Quoted 5 lines

I suppose I could write another
implementation of the table management code for MPIC, though the placement of "irqchip" inside the route entry, rather than as an argument to KVM_IRQ_LINE, suggests the table is supposed to be global, not in the individual interrupt controller.

Yes the table is global. It maps GSI ("Global System Interrupt" IIRC) (integer) to (irqchip,pin) pair.

Quoted 7 lines

It looks like I'm going to have to do this anyway for irqfd, though that doesn't make the other uses of the device control api go away. Even KVM_DEV_MPIC_GRP_IRQ_ACTIVE would still be useful for reading the status for debugging (reading device registers doesn't quite do it, since the "active" bit won't show up if the interrupt is masked).

Quoted 5 lines

At that point, is it more offensive to make it read-only even though it would be trivial to make it read/write (which would allow users who don't need it to bypass the routing API), or to make it read/write and live with there being more than one way to do something?

Can't follow this sentence.

Quoted 11 lines

KVM_SET_IRQCHIP is not suitable because we have more than 512 bytes of state, and because it doesn't allow debugging access to device registers (e.g. inspecting from the QEMU command line), and because it's hard to add new pieces of state if we realize we left something out. It reminds be of GET/SET_SREGS. With that, I did what you seem to want here, which is to adapt the existing interfaces, using feature flags to control optional state. It ended up being a mess, and ONE_REG was introduced as a replacement. The device control API is the equivalent of ONE_REG for things other than vcpus.

-Scott

- ACK on 512 bytes not sufficient. Add another ioctl, SET_IRQCHIP2?
- Agree on poor extensibility of interface. Adding a reserved amount of space as padding and versioning such as has been done so far is not acceptable?
- Debugging: why is reading entire register state not acceptable? Yes, its slow.

#22 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

Quoted 10 lines · Marcelo Tosatti
Quoted 3 lines · Scott Wood
Quoted 2 lines · Marcelo Tosatti

This is probably a stupid question, but why the KVM_SET_IRQCHIP/KVM_SET_GSI_ROUTING interface is not appropriate

for

Quoted 5 lines
Quoted 4 lines

your purposes?

x86 sets up a default GSI->IRQCHIP PIN mapping on creation (during KVM_SET_IRQCHIP), but it can be modified with KVM_SET_GSI_ROUTING.

Quoted 7 lines
Quoted 4 lines

To start, the whole IRQ routing stuff is poorly documented.

Am I supposed to make up GSI numbers and use the routing thing to associate them with real interrupts?

I have no idea. Is mapping from one integer linear space (GSIs) to real interrupts suitable for you?

I can live with it.

Quoted 5 lines
Quoted 1 line

Where does the code live to manage this table, and how APICy is it

(looks like the

Quoted 1 line

answer is "irq_comm.c, and very")?

Thinking faster than typing? Not sure what you mean.

Sorry... The code to manage the table lives in virt/kvm/irq_comm.c. That code is very APIC-specific and not currently in a state that invites sharing.

Quoted 7 lines
Quoted 6 lines

It looks like I'm going to have to do this anyway for irqfd, though that doesn't make the other uses of the device control api go away. Even KVM_DEV_MPIC_GRP_IRQ_ACTIVE would still be useful for reading the status for debugging (reading device registers doesn't quite do it, since the "active" bit won't show up if the interrupt is masked).

Quoted 7 lines
Quoted 5 lines

At that point, is it more offensive to make it read-only even though it would be trivial to make it read/write (which would allow users who don't need it to bypass the routing API), or to make it read/write and live with there being more than one way to do something?

Can't follow this sentence.

Suppose, for the sake of irqfd (and maillist tranquility) MPIC uses existing KVM_IRQ_LINE and such. Will there be objection to being able to use KVM_GET_DEVICE_ATTR to *get* the irq line status for debugging purposes (maybe also useful for migration)? If there's no objection to that, would there be any actual reason, beyond saving a few lines of glue code, to make it a read-only attribute?

Quoted 10 lines
Quoted 10 lines

KVM_SET_IRQCHIP is not suitable because we have more than 512 bytes of state, and because it doesn't allow debugging access to device registers (e.g. inspecting from the QEMU command line), and because it's hard to add new pieces of state if we realize we left something out. It reminds be of GET/SET_SREGS. With that, I did what you seem to want here, which is to adapt the existing interfaces, using feature flags to control optional state. It ended up being a mess, and ONE_REG was introduced as a replacement. The device control API is the equivalent of ONE_REG for things other than vcpus.

Quoted 3 lines
Quoted 1 line

-Scott

- ACK on 512 bytes not sufficient. Add another ioctl, SET_IRQCHIP2?

Well, that's what KVM_SET_DEVICE_ATTR is.

Quoted 3 lines

- Agree on poor extensibility of interface. Adding a reserved amount of space as padding and versioning such as has been done so far is not acceptable?

That's exactly what we did with SREGS, and it got declared a mess and replaced with ONE_REG. I'm trying to learn from my mistakes. :-)

Quoted 2 lines

- Debugging: why is reading entire register state not acceptable? Yes, its slow.

For one, it's more work. The current way works by simulating a guest MMIO access. No blob format to design, implement, and keep compatible.

-Scott

#23 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

In reply to Scott Wood
Quoted 5 lines · Scott Wood
Quoted 4 lines · Marcelo Tosatti

This is probably a stupid question, but why the KVM_SET_IRQCHIP/KVM_SET_GSI_ROUTING interface is not appropriate for your purposes?

Quoted 7 lines
Quoted 2 lines

x86 sets up a default GSI->IRQCHIP PIN mapping on creation (during KVM_SET_IRQCHIP), but it can be modified with KVM_SET_GSI_ROUTING.

To start, the whole IRQ routing stuff is poorly documented.

Am I supposed to make up GSI numbers and use the routing thing to associate them with real interrupts?

You can consider GSI to be a cookie that you use to refer to whatever data you've put into routing table by KVM_IRQ_LINE/irqfd interface. Even on x86, when irq routing is used to inject MSI interrupt, this is exactly how GSI is used. In MSI case it does not have a meaning besides "look at that interrupt entry to see what MSI should be injected".

Quoted 4 lines

Are there constraints on what
sort of GSI numbers I can choose (I now see in the code that KVM_MAX_IRQ_ROUTES is returned from the capability check, but where is that documented?

The only constrain is that the number should be smalled than KVM_MAX_IRQ_ROUTES, but this is implementation detail. Current implementation uses array to map from GSI to a data, if a lot more entries then currently allowed is needed implementation may be changed to different data structure.

Quoted 2 lines

It looks like the APIC implementation has
default routes, where is that documented?)?

It is very PC centric, should not be even compiled for other arches.

Quoted 3 lines

Where does the code
live to manage this table, and how APICy is it (looks like the answer is "irq_comm.c, and very")?

It is a mistake to refer to the irq routing table as APICy :). It is certainly PC centric currently, but there is at least one HW layer between it and the APIC. PC has global GSI space, each GSI can be delivered via different irq chip. Some GSIs can be delivered through multiple irq chips. Irq routing table provides mapping between GSI and irq chips it should be delivered through. Some irq chips deliver interrupt via APIC some not, but this is different story.

The work is needed to make the code not PC centric, but it should not be a lot of work.

Quoted 6 lines

I suppose I could write another
implementation of the table management code for MPIC, though the placement of "irqchip" inside the route entry, rather than as an argument to KVM_IRQ_LINE, suggests the table is supposed to be global, not in the individual interrupt controller.

Yes, it is global. It sits between emulated devices and irq chips.

--
Gleb.

#24 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

In reply to Marcelo Tosatti
Quoted 6 lines · Marcelo Tosatti

This is probably a stupid question, but why the KVM_SET_IRQCHIP/KVM_SET_GSI_ROUTING interface is not appropriate for your purposes?

x86 sets up a default GSI->IRQCHIP PIN mapping on creation (during KVM_SET_IRQCHIP), but it can be modified with KVM_SET_GSI_ROUTING.

So, I see Scott already answered from the point of view of his MPIC emulation stuff, but I'll answer too from the point of view of my XICS emulation code.

My understanding, possibly imperfect, is that in a real system the routing of GSIs to IOAPICs would either be hardwired or set up by the BIOS, described in ACPI tables, and not modified by the operating system. Is that correct? So my belief is that the GSI routing is fundamentally distinct from and handled differently from the routing of interrupts to CPUs, which is fully under the control of the OS.

In the XICS model we have a set of interrupt sources, each identified by a 24-bit number. Control operations on an interrupt source just identify the source by its number. Thus the interrupt source number is like a GSI, but we don't need to map that to a different space (e.g. IOAPIC identifier and input number) in order to operate on it, we can just operate on it directly.

Paul.

#2 lore raw

Re: [PATCH 1/9] KVM: PPC: Book3S: Add infrastructure to implement kernel-side RTAS calls

In reply to Paul Mackerras
Quoted 65 lines · Paul Mackerras

From: Michael Ellerman <[email protected]>

For pseries machine emulation, in order to move the interrupt controller code to the kernel, we need to intercept some RTAS calls in the kernel itself. This adds an infrastructure to allow in-kernel handlers to be registered for RTAS services by name. A new ioctl, KVM_PPC_RTAS_DEFINE_TOKEN, then allows userspace to associate token values with those service names. Then, when the guest requests an RTAS service with one of those token values, it will be handled by the relevant in-kernel handler rather than being passed up to userspace as at present.

---

Documentation/virtual/kvm/api.txt   |   19 ++++
arch/powerpc/include/asm/hvcall.h   |    3 +
arch/powerpc/include/asm/kvm_host.h |    1 +
arch/powerpc/include/asm/kvm_ppc.h  |    4 +
arch/powerpc/include/uapi/asm/kvm.h |    6 ++
arch/powerpc/kvm/Makefile           |    1 +
arch/powerpc/kvm/book3s_hv.c        |   18 +++-
arch/powerpc/kvm/book3s_pr_papr.c   |    7 ++
arch/powerpc/kvm/book3s_rtas.c      |  182 +++++++++++++++++++++++++++++++++++
arch/powerpc/kvm/powerpc.c          |    9 +-
include/uapi/linux/kvm.h            |    3 +
11 files changed, 251 insertions(+), 2 deletions(-)

create mode 100644 arch/powerpc/kvm/book3s_rtas.c

Documentation/virtual/kvm/api.txt +0 -0
index c2534c3..d3e2d60 100644
@@ -2122,6 +2122,25 @@ header; first `n_valid' valid entries with contents from the data

written, then `n_invalid' invalid entries, invalidating any previously valid entries found.

+4.79 KVM_PPC_RTAS_DEFINE_TOKEN

Documentation/virtual/kvm/api.txt +18 -0
+
+Capability: KVM_CAP_PPC_RTAS
+Architectures: ppc
+Type: vm ioctl
+Parameters: struct kvm_rtas_token_args
+Returns: 0 on success, -1 on error
+
+Defines a token value for a RTAS (Run Time Abstraction Services)
+service in order to allow it to be handled in the kernel.  The
+argument struct gives the name of the service, which must be the name
+of a service that has a kernel-side implementation.  If the token
+value is non-zero, it will be associated with that service, and
+subsequent RTAS calls by the guest specifying that token will be
+handled by the kernel.  If the token value is 0, then any token
+associated with the service will be forgotten, and subsequent RTAS
+calls by the guest for that service will be passed to userspace to be
+handled.
+

5. The kvm_run structure
------------------------

arch/powerpc/include/asm/hvcall.h +0 -0
index 7a86706..9ea22b2 100644

arch/powerpc/include/asm/hvcall.h line 273

Quoted 5 lines
arch/powerpc/include/asm/hvcall.h +2 -0
@@ -269,6 +269,9 @@
 #define H_GET_MPP_X		0x314
 #define MAX_HCALL_OPCODE	H_GET_MPP_X
 
+/* Platform specific hcalls, used by KVM */
+#define H_RTAS			0xf000

How about you define a different hcall ID for this? Then QEMU would create its "rtas entry blob" such that KVM-routed RTAS handling goes to KVM directly.

Alex

#8 lore raw

Re: [PATCH 2/9] KVM: PPC: Remove unused argument to kvmppc_core_dequeue_external

In reply to Paul Mackerras
Quoted 6 lines · Paul Mackerras

Currently kvmppc_core_dequeue_external() takes a struct kvm_interrupt * argument and does nothing with it, in any of its implementations. This removes it in order to make things easier for forthcoming in-kernel interrupt controller emulation code.

Thanks, applied to kvm-ppc-queue.

Alex

#25 lore raw

Re: [PATCH 3/9] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller

In reply to Paul Mackerras
Quoted 13 lines · Paul Mackerras
Quoted 6 lines · Marcelo Tosatti

This is probably a stupid question, but why the KVM_SET_IRQCHIP/KVM_SET_GSI_ROUTING interface is not appropriate for your purposes?

x86 sets up a default GSI->IRQCHIP PIN mapping on creation (during KVM_SET_IRQCHIP), but it can be modified with KVM_SET_GSI_ROUTING.

So, I see Scott already answered from the point of view of his MPIC emulation stuff, but I'll answer too from the point of view of my XICS emulation code.

Quoted 6 lines

My understanding, possibly imperfect, is that in a real system the routing of GSIs to IOAPICs would either be hardwired or set up by the BIOS, described in ACPI tables, and not modified by the operating system. Is that correct? So my belief is that the GSI routing is fundamentally distinct from and handled differently from the routing of interrupts to CPUs, which is fully under the control of the OS.

It's a different layer. I guess there's really some confusion on names here :). I'm always confused when I read "sources" and you apparently get confused when you read about GSIs.

GSIs are an ACPI concept. It's not x86 specific, it's also not APIC specific. It's just a global name space for IRQs.

Imagine you have 2 MPICs in your system. But you only want to use a single token / numer to access any IRQ on any chip. That's where GSIs come into play. They map different irqchip IRQs onto a flat number space. To speak with x86 names:

Virtualization perspective:

QEMU -> GSI -> IOAPIC -> LAPIC -> CPU

Device perspective:

Device irq line -> IOAPIC -> LAPIC -> CPU

The "IOAPIC" is the piece of hardware that interrupt lines get attached to. You connect a pin on it to an irq pin of your device. That talks to the LAPIC to actually schedule interrupts on target CPUs. The LAPIC then fetches interrupts and pulls the CPU's interrupt line.

Of course, things are slightly more complicated in the x86 world, as everything behind the IOAPIC also carries a payload defining which pin actually got triggered, but you get the idea.

So really just consider GSIs as a "global flat number space" for irqchip pins.

Alex

Quoted 12 lines

In the XICS model we have a set of interrupt sources, each identified by a 24-bit number. Control operations on an interrupt source just identify the source by its number. Thus the interrupt source number is like a GSI, but we don't need to map that to a different space (e.g. IOAPIC identifier and input number) in order to operate on it, we can just operate on it directly.

Paul.
--
To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

Re: [PATCH 1/9] KVM: PPC: Book3S: Add infrastructure to implement kernel-side RTAS calls

In reply to Alexander Graf
Quoted 6 lines · Alexander Graf

arch/powerpc/include/asm/hvcall.h line 273

Quoted 2 lines
arch/powerpc/include/asm/hvcall.h +2 -0
+/* Platform specific hcalls, used by KVM */
+#define H_RTAS			0xf000

How about you define a different hcall ID for this? Then QEMU would create its "rtas entry blob" such that KVM-routed RTAS handling goes to KVM directly.

QEMU can still do that, and I don't see that it would change the kernel side if it did. We would still have to have agreement between the kernel and userspace as to what the hcall number for invoking the in-kernel RTAS calls was, and the kernel would still have to keep a list of token numbers and how they correspond to the functions it provides. The only thing different would be that the in-kernel RTAS hcall could return to the guest if it didn't recognize the token number, rather than pushing the problem up to userspace. However, that wouldn't make the code any simpler, and it isn't a situation where performance is an issue.

Do you see some kernel-side improvements or simplifications from your suggestion that I'm missing? Remember, the guest gets the token numbers from the device tree (properties under the /rtas node), so they are under the control of userspace/QEMU.

Paul.

#4 lore raw

Re: [PATCH 1/9] KVM: PPC: Book3S: Add infrastructure to implement kernel-side RTAS calls

Quoted 19 lines · Paul Mackerras
Quoted 6 lines · Alexander Graf

arch/powerpc/include/asm/hvcall.h line 273

Quoted 2 lines
arch/powerpc/include/asm/hvcall.h +2 -0
+/* Platform specific hcalls, used by KVM */
+#define H_RTAS			0xf000

How about you define a different hcall ID for this? Then QEMU would create its "rtas entry blob" such that KVM-routed RTAS handling goes to KVM directly.

QEMU can still do that, and I don't see that it would change the kernel side if it did. We would still have to have agreement between the kernel and userspace as to what the hcall number for invoking the in-kernel RTAS calls was, and the kernel would still have to keep a list of token numbers and how they correspond to the functions it provides. The only thing different would be that the in-kernel RTAS hcall could return to the guest if it didn't recognize the token number, rather than pushing the problem up to userspace. However, that wouldn't make the code any simpler, and it isn't a situation where performance is an issue.

Quoted 4 lines

Do you see some kernel-side improvements or simplifications from your suggestion that I'm missing? Remember, the guest gets the token numbers from the device tree (properties under the /rtas node), so they are under the control of userspace/QEMU.

The code flow with this patch:

<setup time>

  foreach (override in overrides)
    ioctl(OVERRIDE_RTAS, ...);

<runtime>

  switch (hcall_id) {
  case QEMU_RTAS_ID:
    foreach (override in kvm_overrides) {
      int rtas_id = ...;
      if (override.rtas_id == rtas_id) {
        handle_rtas();
        handled = true;
      }
    }
    if (!handled)
      pass_to_qemu();
    break;
  default:
    pass_to_qemu();
    break
  }

What I'm suggesting:

<setup time>

nothing from KVM's point of view

<runtime>

  switch (hcall_id) {
  case KVM_RTAS_ID:
    handle_rtas();
    break;
  default:
    pass_to_qemu();
    break;
  }

Which one looks easier and less error prone to you? :)

Speaking of which, how does user space know that the kernel actually supports a specific RTAS token?

Alex

#5 lore raw

Re: [PATCH 1/9] KVM: PPC: Book3S: Add infrastructure to implement kernel-side RTAS calls

Quoted 34 lines · Alexander Graf
Quoted 19 lines · Paul Mackerras
Quoted 6 lines · Alexander Graf

arch/powerpc/include/asm/hvcall.h line 273

Quoted 2 lines
arch/powerpc/include/asm/hvcall.h +2 -0
+/* Platform specific hcalls, used by KVM */
+#define H_RTAS			0xf000

How about you define a different hcall ID for this? Then QEMU would create its "rtas entry blob" such that KVM-routed RTAS handling goes to KVM directly.

QEMU can still do that, and I don't see that it would change the kernel side if it did. We would still have to have agreement between the kernel and userspace as to what the hcall number for invoking the in-kernel RTAS calls was, and the kernel would still have to keep a list of token numbers and how they correspond to the functions it provides. The only thing different would be that the in-kernel RTAS hcall could return to the guest if it didn't recognize the token number, rather than pushing the problem up to userspace. However, that wouldn't make the code any simpler, and it isn't a situation where performance is an issue.

Quoted 4 lines

Do you see some kernel-side improvements or simplifications from your suggestion that I'm missing? Remember, the guest gets the token numbers from the device tree (properties under the /rtas node), so they are under the control of userspace/QEMU.

The code flow with this patch:

<setup time>

  foreach (override in overrides)
    ioctl(OVERRIDE_RTAS, ...);
Quoted 8 lines

<runtime>

  switch (hcall_id) {
  case QEMU_RTAS_ID:
    foreach (override in kvm_overrides) {
      int rtas_id = ...;
      if (override.rtas_id == rtas_id) {
        handle_rtas();

Actually this is more like: override.handler();

Quoted 9 lines
        handled = true;
      }
    }
    if (!handled)
      pass_to_qemu();
    break;
  default:
    pass_to_qemu();
    break
Quoted 7 lines

}

What I'm suggesting:

<setup time>

nothing from KVM's point of view

Actually, this can't be "nothing".

The way the RTAS calls work is that there is a name and a "token" (32-bit integer value) for each RTAS call. The tokens have to be unique for each different name. Userspace puts the names and tokens in the device tree under the /rtas node (a set of properties where the property name is the RTAS function name and the property value is the token). The guest looks up the token for each RTAS function it wants to use, and passes the token in the argument buffer for the RTAS call.

This means that userspace has to know the names and tokens for all supported RTAS functions, both the ones implemented in the kernel and the ones implemented in userspace.

Also, the token numbers are pretty arbitrary, and the token numbers for the kernel-implemented RTAS functions could be chosen by userspace or by the kernel. If they're chosen by the kernel, then userspace needs a way to discover them (so it can put them in the device tree), and also has to avoid choosing any token numbers for its functions that collide with a kernel-chosen token. If userspace chooses the token numbers, it has to tell the kernel what token numbers it has chosen for the kernel-implemented RTAS functions. We chose the latter since it gives userspace more control.

So this <setup time> code has to be either (your suggestion):

foreach RTAS function possibly implemented in kernel {
query kernel token for function, by name
if that gives an error, mark function as needing to be
implemented in userspace
}
(userspace) allocate tokens for remaining functions,
avoiding collisions with kernel-chosen tokens

or else it is (my suggestion):

(userspace) allocate tokens for all RTAS functions foreach RTAS function possibly implemented in kernel {
tell kernel the (name, token) correspondence
}

Quoted 5 lines

<runtime>

  switch (hcall_id) {
  case KVM_RTAS_ID:
    handle_rtas();

Here, you've compressed details that you expanded in your pseudo-code above, making this a less than fair comparison. This handle_rtas() function has to fetch the token and branch out to the appropriate handler routine. Whether that's a switch statement or a loop over registered handlers doesn't make all that much difference.

Quoted 11 lines
    break;
  default:
    pass_to_qemu();
    break;

}

Which one looks easier and less error prone to you? :)

Speaking of which, how does user space know that the kernel actually supports a specific RTAS token?

It's really the names that are more important, the tokens are pretty arbitrary. In my scheme, userspace does a KVM_PPC_RTAS_DEFINE_TOKEN ioctl giving the name and the (userspace-chosen) token, which gets an error if the kernel doesn't recognize the name. In your scheme, there would have to be an equivalent ioctl to query the (kernel-chosen) token for a given name, which once again would return an error if the kernel doesn't recognize the name. Either way the kernel has to have a list of names that it knows about.

Paul.

#6 lore raw

Re: [PATCH 1/9] KVM: PPC: Book3S: Add infrastructure to implement kernel-side RTAS calls

Quoted 128 lines · Paul Mackerras
Quoted 34 lines · Alexander Graf
Quoted 19 lines · Paul Mackerras
Quoted 6 lines · Alexander Graf

arch/powerpc/include/asm/hvcall.h line 273

Quoted 2 lines
arch/powerpc/include/asm/hvcall.h +2 -0
+/* Platform specific hcalls, used by KVM */
+#define H_RTAS			0xf000

How about you define a different hcall ID for this? Then QEMU would create its "rtas entry blob" such that KVM-routed RTAS handling goes to KVM directly.

QEMU can still do that, and I don't see that it would change the kernel side if it did. We would still have to have agreement between the kernel and userspace as to what the hcall number for invoking the in-kernel RTAS calls was, and the kernel would still have to keep a list of token numbers and how they correspond to the functions it provides. The only thing different would be that the in-kernel RTAS hcall could return to the guest if it didn't recognize the token number, rather than pushing the problem up to userspace. However, that wouldn't make the code any simpler, and it isn't a situation where performance is an issue.

Quoted 4 lines

Do you see some kernel-side improvements or simplifications from your suggestion that I'm missing? Remember, the guest gets the token numbers from the device tree (properties under the /rtas node), so they are under the control of userspace/QEMU.

The code flow with this patch:

<setup time>

foreach (override in overrides)
ioctl(OVERRIDE_RTAS, ...);

Quoted 8 lines

<runtime>

switch (hcall_id) {
case QEMU_RTAS_ID:

   foreach (override in kvm_overrides) {
     int rtas_id = ...;
     if (override.rtas_id == rtas_id) {
       handle_rtas();

Actually this is more like: override.handler();

Quoted 9 lines
       handled = true;
     }
   }
   if (!handled)
     pass_to_qemu();
   break;
 default:
   pass_to_qemu();
   break
Quoted 7 lines

}

What I'm suggesting:

<setup time>

nothing from KVM's point of view

Actually, this can't be "nothing".

The way the RTAS calls work is that there is a name and a "token" (32-bit integer value) for each RTAS call. The tokens have to be unique for each different name. Userspace puts the names and tokens in the device tree under the /rtas node (a set of properties where the property name is the RTAS function name and the property value is the token). The guest looks up the token for each RTAS function it wants to use, and passes the token in the argument buffer for the RTAS call.

This means that userspace has to know the names and tokens for all supported RTAS functions, both the ones implemented in the kernel and the ones implemented in userspace.

Also, the token numbers are pretty arbitrary, and the token numbers for the kernel-implemented RTAS functions could be chosen by userspace or by the kernel. If they're chosen by the kernel, then userspace needs a way to discover them (so it can put them in the device tree), and also has to avoid choosing any token numbers for its functions that collide with a kernel-chosen token. If userspace chooses the token numbers, it has to tell the kernel what token numbers it has chosen for the kernel-implemented RTAS functions. We chose the latter since it gives userspace more control.

So this <setup time> code has to be either (your suggestion):

foreach RTAS function possibly implemented in kernel {
query kernel token for function, by name
if that gives an error, mark function as needing to be
implemented in userspace
}
(userspace) allocate tokens for remaining functions,
avoiding collisions with kernel-chosen tokens

or else it is (my suggestion):

(userspace) allocate tokens for all RTAS functions foreach RTAS function possibly implemented in kernel {
tell kernel the (name, token) correspondence
}

Quoted 5 lines

<runtime>

switch (hcall_id) {
case KVM_RTAS_ID:

   handle_rtas();

Here, you've compressed details that you expanded in your pseudo-code above, making this a less than fair comparison. This handle_rtas() function has to fetch the token and branch out to the appropriate handler routine. Whether that's a switch statement or a loop over registered handlers doesn't make all that much difference.

Quoted 11 lines
   break;
 default:
   pass_to_qemu();
   break;

}

Which one looks easier and less error prone to you? :)

Speaking of which, how does user space know that the kernel actually supports a specific RTAS token?

Quoted 8 lines

It's really the names that are more important, the tokens are pretty arbitrary. In my scheme, userspace does a KVM_PPC_RTAS_DEFINE_TOKEN ioctl giving the name and the (userspace-chosen) token, which gets an error if the kernel doesn't recognize the name. In your scheme, there would have to be an equivalent ioctl to query the (kernel-chosen) token for a given name, which once again would return an error if the kernel doesn't recognize the name. Either way the kernel has to have a list of names that it knows about.

Hrm. I think I'm slowly grasping what the real issue is.

Fair enough, your approach works for me then.

Alex