linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: cdall@linaro.org (Christoffer Dall)
To: linux-arm-kernel@lists.infradead.org
Subject: [RFC 27/55] KVM: arm/arm64: Emulate GICH interface on GICv2
Date: Wed, 22 Feb 2017 14:06:16 +0100	[thread overview]
Message-ID: <20170222130616.GO26976@cbox> (raw)
In-Reply-To: <1483943091-1364-28-git-send-email-jintack@cs.columbia.edu>

On Mon, Jan 09, 2017 at 01:24:23AM -0500, Jintack Lim wrote:
> Emulate GICH interface accesses from the guest hypervisor.
> 
> Signed-off-by: Jintack Lim <jintack@cs.columbia.edu>
> Signed-off-by: Shih-Wei Li <shihwei@cs.columbia.edu>
> Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
> ---
>  arch/arm64/kvm/Makefile            |   1 +
>  virt/kvm/arm/vgic/vgic-v2-nested.c | 207 +++++++++++++++++++++++++++++++++++++
>  2 files changed, 208 insertions(+)
>  create mode 100644 virt/kvm/arm/vgic/vgic-v2-nested.c
> 
> diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile
> index 9c35e9a..8573faf 100644
> --- a/arch/arm64/kvm/Makefile
> +++ b/arch/arm64/kvm/Makefile
> @@ -37,3 +37,4 @@ kvm-$(CONFIG_KVM_ARM_PMU) += $(KVM)/arm/pmu.o
>  
>  kvm-$(CONFIG_KVM_ARM_NESTED_HYP) += handle_exit_nested.o
>  kvm-$(CONFIG_KVM_ARM_NESTED_HYP) += emulate-nested.o
> +kvm-$(CONFIG_KVM_ARM_NESTED_HYP) += $(KVM)/arm/vgic/vgic-v2-nested.o
> diff --git a/virt/kvm/arm/vgic/vgic-v2-nested.c b/virt/kvm/arm/vgic/vgic-v2-nested.c
> new file mode 100644
> index 0000000..b13128e
> --- /dev/null
> +++ b/virt/kvm/arm/vgic/vgic-v2-nested.c
> @@ -0,0 +1,207 @@
> +#include <linux/cpu.h>
> +#include <linux/kvm.h>
> +#include <linux/kvm_host.h>
> +#include <linux/interrupt.h>
> +#include <linux/io.h>
> +#include <linux/uaccess.h>
> +
> +#include <linux/irqchip/arm-gic.h>
> +
> +#include <asm/kvm_emulate.h>
> +#include <asm/kvm_arm.h>
> +#include <asm/kvm_mmu.h>
> +#include <kvm/arm_vgic.h>
> +
> +#include "vgic.h"
> +#include "vgic-mmio.h"
> +
> +static inline struct vgic_v2_cpu_if *vcpu_nested_if(struct kvm_vcpu *vcpu)
> +{
> +	return &vcpu->arch.vgic_cpu.nested_vgic_v2;
> +}
> +
> +static inline struct vgic_v2_cpu_if *vcpu_shadow_if(struct kvm_vcpu *vcpu)
> +{
> +	return &vcpu->arch.vgic_cpu.shadow_vgic_v2;
> +}
> +
> +static unsigned long vgic_mmio_read_v2_vtr(struct kvm_vcpu *vcpu,
> +					   gpa_t addr, unsigned int len)
> +{
> +	u32 reg;
> +
> +	reg = kvm_vgic_global_state.nr_lr - 1;
> +	reg |= 0b100 << 26;
> +	reg |= 0b100 << 29;

Pure magic?  Can we have some defines?  Have you checked the existing
header file if we have some defines for this?

> +
> +	return reg;
> +}
> +
> +static inline bool lr_triggers_eoi(u32 lr)
> +{
> +	return !(lr & (GICH_LR_STATE | GICH_LR_HW)) && (lr & GICH_LR_EOI);
> +}
> +
> +static unsigned long get_eisr(struct kvm_vcpu *vcpu, bool upper_reg)
> +{
> +	struct vgic_v2_cpu_if *cpu_if = vcpu_nested_if(vcpu);
> +	int max_lr = upper_reg ? 64 : 32;
> +	int min_lr = upper_reg ? 32 : 0;
> +	int nr_lr = min(kvm_vgic_global_state.nr_lr, max_lr);
> +	int i;
> +	u32 reg = 0;

So the assumption here is that we can only emualte a virtual GICH
interface with the same number of LRs that the hardware has, yes ?  Can
you document this assumption in the commit message and explain how we
deal with nr_lr for all this logic based on that.

Seems like this could go in the commit message.

> +
> +	for (i = min_lr; i < nr_lr; i++) {
> +		if (lr_triggers_eoi(cpu_if->vgic_lr[i]))
> +			reg |= BIT(i - min_lr);
> +	}
> +
> +	return reg;
> +}
> +
> +static unsigned long vgic_mmio_read_v2_eisr0(struct kvm_vcpu *vcpu,
> +					     gpa_t addr, unsigned int len)
> +{
> +	return get_eisr(vcpu, false);
> +}
> +
> +static unsigned long vgic_mmio_read_v2_eisr1(struct kvm_vcpu *vcpu,
> +					     gpa_t addr, unsigned int len)
> +{
> +	return get_eisr(vcpu, true);
> +}
> +
> +static u32 get_elrsr(struct kvm_vcpu *vcpu, bool upper_reg)
> +{
> +	struct vgic_v2_cpu_if *cpu_if = vcpu_nested_if(vcpu);
> +	int max_lr = upper_reg ? 64 : 32;
> +	int min_lr = upper_reg ? 32 : 0;
> +	int nr_lr = min(kvm_vgic_global_state.nr_lr, max_lr);
> +	u32 reg = 0;
> +	int i;
> +
> +	for (i = min_lr; i < nr_lr; i++) {
> +		if (!(cpu_if->vgic_lr[i] & GICH_LR_STATE))
> +			reg |= BIT(i - min_lr);
> +	}
> +
> +	return reg;
> +}
> +
> +static unsigned long vgic_mmio_read_v2_elrsr0(struct kvm_vcpu *vcpu,
> +					      gpa_t addr, unsigned int len)
> +{
> +	return get_elrsr(vcpu, false);
> +}
> +
> +static unsigned long vgic_mmio_read_v2_elrsr1(struct kvm_vcpu *vcpu,
> +					      gpa_t addr, unsigned int len)
> +{
> +	return get_elrsr(vcpu, true);
> +}
> +
> +static unsigned long vgic_mmio_read_v2_misr(struct kvm_vcpu *vcpu,
> +					    gpa_t addr, unsigned int len)
> +{
> +	struct vgic_v2_cpu_if *cpu_if = vcpu_nested_if(vcpu);
> +	int nr_lr = kvm_vgic_global_state.nr_lr;
> +	u32 reg = 0;
> +
> +	if (vgic_mmio_read_v2_eisr0(vcpu, addr, len) ||
> +			vgic_mmio_read_v2_eisr1(vcpu, addr, len))
> +		reg |= GICH_MISR_EOI;
> +
> +	if (cpu_if->vgic_hcr & GICH_HCR_UIE) {
> +		u32 elrsr0 = vgic_mmio_read_v2_elrsr0(vcpu, addr, len);
> +		u32 elrsr1 = vgic_mmio_read_v2_elrsr1(vcpu, addr, len);
> +		int used_lrs;
> +
> +		used_lrs = nr_lr - (hweight32(elrsr0) + hweight32(elrsr1));
> +		if (used_lrs <= 1)
> +			reg |= GICH_MISR_U;
> +	}
> +
> +	/* TODO: Support remaining bits in this register */

Is this going to happen in this series?  Why don't we just do it here?

> +	return reg;
> +}
> +
> +static unsigned long vgic_mmio_read_v2_gich(struct kvm_vcpu *vcpu,
> +					    gpa_t addr, unsigned int len)
> +{
> +	struct vgic_v2_cpu_if *cpu_if = vcpu_nested_if(vcpu);
> +	u32 value;
> +
> +	switch (addr & 0xfff) {
> +	case GICH_HCR:
> +		value = cpu_if->vgic_hcr;
> +		break;
> +	case GICH_VMCR:
> +		value = cpu_if->vgic_vmcr;
> +		break;
> +	case GICH_APR:
> +		value = cpu_if->vgic_apr;
> +		break;
> +	case GICH_LR0 ... (GICH_LR0 + 4 * (VGIC_V2_MAX_LRS - 1)):
> +		value = cpu_if->vgic_lr[(addr & 0xff) >> 2];
> +		break;
> +	default:
> +		return 0;
> +	}
> +
> +	return value;
> +}
> +
> +static void vgic_mmio_write_v2_gich(struct kvm_vcpu *vcpu,
> +				    gpa_t addr, unsigned int len,
> +				    unsigned long val)
> +{
> +	struct vgic_v2_cpu_if *cpu_if = vcpu_nested_if(vcpu);
> +
> +	switch (addr & 0xfff) {
> +	case GICH_HCR:
> +		cpu_if->vgic_hcr = val;
> +		break;
> +	case GICH_VMCR:
> +		cpu_if->vgic_vmcr = val;
> +		break;
> +	case GICH_APR:
> +		cpu_if->vgic_apr = val;
> +		break;
> +	case GICH_LR0 ... (GICH_LR0 + 4 * (VGIC_V2_MAX_LRS - 1)):
> +		cpu_if->vgic_lr[(addr & 0xff) >> 2] = val;

Don't you need to check if we actually support this particular LR ?

> +		break;
> +	}
> +}
> +
> +static const struct vgic_register_region vgic_v2_gich_registers[] = {
> +	REGISTER_DESC_WITH_LENGTH(GICH_HCR,
> +		vgic_mmio_read_v2_gich, vgic_mmio_write_v2_gich, 4,
> +		VGIC_ACCESS_32bit),
> +	REGISTER_DESC_WITH_LENGTH(GICH_VTR,
> +		vgic_mmio_read_v2_vtr, vgic_mmio_write_wi, 4,
> +		VGIC_ACCESS_32bit),
> +	REGISTER_DESC_WITH_LENGTH(GICH_VMCR,
> +		vgic_mmio_read_v2_gich, vgic_mmio_write_v2_gich, 4,
> +		VGIC_ACCESS_32bit),
> +	REGISTER_DESC_WITH_LENGTH(GICH_MISR,
> +		vgic_mmio_read_v2_misr, vgic_mmio_write_wi, 4,
> +		VGIC_ACCESS_32bit),
> +	REGISTER_DESC_WITH_LENGTH(GICH_EISR0,
> +		vgic_mmio_read_v2_eisr0, vgic_mmio_write_wi, 4,
> +		VGIC_ACCESS_32bit),
> +	REGISTER_DESC_WITH_LENGTH(GICH_EISR1,
> +		vgic_mmio_read_v2_eisr1, vgic_mmio_write_wi, 4,
> +		VGIC_ACCESS_32bit),
> +	REGISTER_DESC_WITH_LENGTH(GICH_ELRSR0,
> +		vgic_mmio_read_v2_elrsr0, vgic_mmio_write_wi, 4,
> +		VGIC_ACCESS_32bit),
> +	REGISTER_DESC_WITH_LENGTH(GICH_ELRSR1,
> +		vgic_mmio_read_v2_elrsr1, vgic_mmio_write_wi, 4,
> +		VGIC_ACCESS_32bit),
> +	REGISTER_DESC_WITH_LENGTH(GICH_APR,
> +		vgic_mmio_read_v2_gich, vgic_mmio_write_v2_gich, 4,
> +		VGIC_ACCESS_32bit),
> +	REGISTER_DESC_WITH_LENGTH(GICH_LR0,
> +		vgic_mmio_read_v2_gich, vgic_mmio_write_v2_gich,
> +		4 * VGIC_V2_MAX_LRS, VGIC_ACCESS_32bit),
> +};
> -- 
> 1.9.1
> 
> 

  reply	other threads:[~2017-02-22 13:06 UTC|newest]

Thread overview: 111+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-09  6:23 [RFC 00/55] Nested Virtualization on KVM/ARM Jintack Lim
2017-01-09  6:23 ` [RFC 01/55] arm64: Add missing TCR hw defines Jintack Lim
2017-01-09  6:23 ` [RFC 02/55] KVM: arm64: Add nesting config option Jintack Lim
2017-01-09  6:23 ` [RFC 03/55] KVM: arm64: Add KVM nesting feature Jintack Lim
2017-01-09  6:24 ` [RFC 04/55] KVM: arm64: Allow userspace to set PSR_MODE_EL2x Jintack Lim
2017-01-09  6:24 ` [RFC 05/55] KVM: arm64: Add vcpu_mode_el2 primitive to support nesting Jintack Lim
2017-01-09  6:24 ` [RFC 06/55] KVM: arm64: Add EL2 execution context for nesting Jintack Lim
2017-02-22 11:10   ` Christoffer Dall
2017-06-26 14:33     ` Jintack Lim
2017-07-03  9:03       ` Christoffer Dall
2017-07-03  9:32         ` Marc Zyngier
2017-07-03  9:54           ` Christoffer Dall
2017-07-03 14:44             ` Jintack Lim
2017-07-03 15:30               ` Christoffer Dall
2017-01-09  6:24 ` [RFC 07/55] KVM: arm/arm64: Add virtual EL2 state emulation framework Jintack Lim
2017-02-22 11:12   ` Christoffer Dall
2017-06-01 20:05   ` Bandan Das
2017-06-02 11:51     ` Christoffer Dall
2017-06-02 17:36       ` Bandan Das
2017-06-02 19:06         ` Christoffer Dall
2017-06-02 19:25           ` Bandan Das
2017-01-09  6:24 ` [RFC 08/55] KVM: arm64: Set virtual EL2 context depending on the guest exception level Jintack Lim
2017-02-22 11:14   ` Christoffer Dall
2017-06-01 20:22   ` Bandan Das
2017-06-02  8:48     ` Marc Zyngier
2017-01-09  6:24 ` [RFC 09/55] KVM: arm64: Set shadow EL1 registers for virtual EL2 execution Jintack Lim
2017-02-22 11:19   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 10/55] KVM: arm64: Synchronize EL1 system registers on virtual EL2 entry and exit Jintack Lim
2017-06-06 20:16   ` Bandan Das
2017-06-07  4:26     ` Jintack Lim
2017-01-09  6:24 ` [RFC 11/55] KVM: arm64: Emulate taking an exception to the guest hypervisor Jintack Lim
2017-02-22 11:28   ` Christoffer Dall
2017-06-06 20:21   ` Bandan Das
2017-06-06 20:38     ` Jintack Lim
2017-06-06 22:07       ` Bandan Das
2017-06-06 23:16         ` Jintack Lim
2017-06-07 17:21           ` Bandan Das
2017-01-09  6:24 ` [RFC 12/55] KVM: arm64: Handle EL2 register access traps Jintack Lim
2017-02-22 11:30   ` Christoffer Dall
2017-02-22 11:31   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 13/55] KVM: arm64: Handle eret instruction traps Jintack Lim
2017-01-09  6:24 ` [RFC 14/55] KVM: arm64: Take account of system " Jintack Lim
2017-02-22 11:34   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 15/55] KVM: arm64: Trap EL1 VM register accesses in virtual EL2 Jintack Lim
2017-01-09  6:24 ` [RFC 16/55] KVM: arm64: Forward VM reg traps to the guest hypervisor Jintack Lim
2017-02-22 11:39   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 17/55] KVM: arm64: Trap SPSR_EL1, ELR_EL1 and VBAR_EL1 in virtual EL2 Jintack Lim
2017-02-22 11:40   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 18/55] KVM: arm64: Forward traps due to HCR_EL2.NV1 bit to the guest hypervisor Jintack Lim
2017-02-22 11:41   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 19/55] KVM: arm64: Trap CPACR_EL1 access in virtual EL2 Jintack Lim
2017-01-09  6:24 ` [RFC 20/55] KVM: arm64: Forward CPACR_EL1 traps to the guest hypervisor Jintack Lim
2017-01-09  6:24 ` [RFC 21/55] KVM: arm64: Forward HVC instruction " Jintack Lim
2017-02-22 11:47   ` Christoffer Dall
2017-06-26 15:21     ` Jintack Lim
2017-07-03  9:08       ` Christoffer Dall
2017-07-03  9:31         ` Andrew Jones
2017-07-03  9:51           ` Christoffer Dall
2017-07-03 12:03             ` Will Deacon
2017-07-03 12:35               ` Marc Zyngier
2017-07-03 13:29         ` Jintack Lim
2017-01-09  6:24 ` [RFC 22/55] KVM: arm64: Handle PSCI call from the guest Jintack Lim
2017-01-09  6:24 ` [RFC 23/55] KVM: arm64: Forward WFX to the guest hypervisor Jintack Lim
2017-01-09  6:24 ` [RFC 24/55] KVM: arm64: Forward FP exceptions " Jintack Lim
2017-01-09  6:24 ` [RFC 25/55] KVM: arm/arm64: Let vcpu thread modify its own active state Jintack Lim
2017-02-22 12:27   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 26/55] KVM: arm/arm64: Add VGIC data structures for the nesting Jintack Lim
2017-01-09  6:24 ` [RFC 27/55] KVM: arm/arm64: Emulate GICH interface on GICv2 Jintack Lim
2017-02-22 13:06   ` Christoffer Dall [this message]
2017-01-09  6:24 ` [RFC 28/55] KVM: arm/arm64: Prepare vgic state for the nested VM Jintack Lim
2017-02-22 13:12   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 29/55] KVM: arm/arm64: Set up the prepared vgic state Jintack Lim
2017-01-09  6:24 ` [RFC 30/55] KVM: arm/arm64: Inject irqs to the guest hypervisor Jintack Lim
2017-02-22 13:16   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 31/55] KVM: arm/arm64: Inject maintenance interrupts " Jintack Lim
2017-02-22 13:19   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 32/55] KVM: arm/arm64: register GICH iodev for " Jintack Lim
2017-02-22 13:21   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 33/55] KVM: arm/arm64: Remove unused params in mmu functions Jintack Lim
2017-01-09  6:24 ` [RFC 34/55] KVM: arm/arm64: Abstract stage-2 MMU state into a separate structure Jintack Lim
2017-01-09  6:24 ` [RFC 35/55] KVM: arm/arm64: Support mmu for the virtual EL2 execution Jintack Lim
2017-02-22 13:38   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 36/55] KVM: arm64: Invalidate virtual EL2 TLB entries when needed Jintack Lim
2017-01-09  6:24 ` [RFC 37/55] KVM: arm64: Setup vttbr_el2 on each VM entry Jintack Lim
2017-01-09  6:24 ` [RFC 38/55] KVM: arm/arm64: Make mmu functions non-static Jintack Lim
2017-01-09  6:24 ` [RFC 39/55] KVM: arm/arm64: Add mmu context for the nesting Jintack Lim
2017-02-22 13:34   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 40/55] KVM: arm/arm64: Handle vttbr_el2 write operation from the guest hypervisor Jintack Lim
2017-02-22 17:59   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 41/55] KVM: arm/arm64: Unmap/flush shadow stage 2 page tables Jintack Lim
2017-02-22 18:09   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 42/55] KVM: arm64: Implement nested Stage-2 page table walk logic Jintack Lim
2017-01-09  6:24 ` [RFC 43/55] KVM: arm/arm64: Handle shadow stage 2 page faults Jintack Lim
2017-01-09  6:24 ` [RFC 44/55] KVM: arm/arm64: Move kvm_is_write_fault to header file Jintack Lim
2017-01-09  6:24 ` [RFC 45/55] KVM: arm64: KVM: Inject stage-2 page faults Jintack Lim
2017-01-09  6:24 ` [RFC 46/55] KVM: arm64: Add more info to the S2 translation result Jintack Lim
2017-01-09  6:24 ` [RFC 47/55] KVM: arm/arm64: Forward the guest hypervisor's stage 2 permission faults Jintack Lim
2017-02-22 18:15   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 48/55] KVM: arm64: Emulate TLBI instruction Jintack Lim
2017-01-09  6:24 ` [RFC 49/55] KVM: arm64: Fixes to toggle_cache for nesting Jintack Lim
2017-01-09  6:24 ` [RFC 50/55] KVM: arm/arm64: Abstract kvm_phys_addr_ioremap() function Jintack Lim
2017-01-09  6:24 ` [RFC 51/55] KVM: arm64: Expose physical address of vcpu interface Jintack Lim
2017-01-09  6:24 ` [RFC 52/55] KVM: arm/arm64: Create a vcpu mapping for the nested VM Jintack Lim
2017-01-09  6:24 ` [RFC 53/55] KVM: arm64: Reflect shadow VMPIDR_EL2 value to MPIDR_EL1 Jintack Lim
2017-01-09  6:24 ` [RFC 54/55] KVM: arm/arm64: Adjust virtual offset considering nesting Jintack Lim
2017-02-22 19:28   ` Christoffer Dall
2017-01-09  6:24 ` [RFC 55/55] KVM: arm64: Enable nested virtualization Jintack Lim
2017-01-09 15:05 ` [RFC 00/55] Nested Virtualization on KVM/ARM David Hildenbrand
2017-01-10 16:18   ` Jintack Lim
2017-02-22 18:23 ` Christoffer Dall
2017-02-24 10:28   ` Jintack Lim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170222130616.GO26976@cbox \
    --to=cdall@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).