From: David Woodhouse <[email protected]>

A guest using VSM names the target VTL of HvCallSendSyntheticClusterIpi
and HvCallSendSyntheticClusterIpiEx in the input's in_vtl field (TLFS
hv_input_vtl, the byte after the vector). KVM treats that byte as
reserved: the fast form of HvCallSendSyntheticClusterIpi fails any
input with bits above 31 set with HV_STATUS_INVALID_HYPERCALL_INPUT,
and the other forms ignore it.

Decode in_vtl in both the memory and the fast forms and deliver the IPI
into that plane's twin of each targeted VP, or into the sender's own
plane when use_target_vtl is clear. A VP with no twin in the target
plane is skipped. Reserved bits 7:5 of in_vtl are rejected with
HV_STATUS_INVALID_HYPERCALL_INPUT before anything is delivered.

A target VTL above the caller's is refused with HV_STATUS_ACCESS_DENIED,
also for an empty EX set.
Under the VTL privilege model a VTL may interrupt its own level and
lower ones, but never a more privileged one (TLFS ยง15.9). The target
plane is therefore always at or below the caller's, and so below
KVM_MAX_PLANES.

struct hv_send_ipi and struct hv_send_ipi_ex are shared with the Linux
guest code, which now clears in_vtl and the remaining reserved bytes
separately.

The target twin is looked up without the locks that serialise twin
creation, so read the slot with READ_ONCE() to pair with its
publication.

Signed-off-by: David Woodhouse <[email protected]>
---
 arch/x86/hyperv/hv_apic.c   |  3 +-
 arch/x86/kvm/hyperv.c       | 69 ++++++++++++++++++++++++++++++++++---
 include/hyperv/hvgdk_mini.h |  3 +-
 include/hyperv/hvhdk_mini.h |  3 +-
 4 files changed, 70 insertions(+), 8 deletions(-)

diff --git a/arch/x86/hyperv/hv_apic.c b/arch/x86/hyperv/hv_apic.c
index 95f1782d1e17..3655b744395d 100644
--- a/arch/x86/hyperv/hv_apic.c
+++ b/arch/x86/hyperv/hv_apic.c
@@ -125,7 +125,8 @@ static bool __send_ipi_mask_ex(const struct cpumask *mask, 
int vector,
                goto ipi_mask_ex_done;
 
        ipi_arg->vector = vector;
-       ipi_arg->reserved = 0;
+       ipi_arg->in_vtl.as_uint8 = 0;
+       memset(ipi_arg->reserved, 0, sizeof(ipi_arg->reserved));
        ipi_arg->vp_set.valid_bank_mask = 0;
 
        /*
diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
index 3494f3b29809..8a33127169fa 100644
--- a/arch/x86/kvm/hyperv.c
+++ b/arch/x86/kvm/hyperv.c
@@ -2683,7 +2683,8 @@ static s64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct 
kvm_hv_hcall *hc)
 }
 
 static void kvm_hv_send_ipi_to_many(struct kvm *kvm, u32 vector,
-                                   u64 *sparse_banks, u64 valid_bank_mask)
+                                   u64 *sparse_banks, u64 valid_bank_mask,
+                                   int target_plane)
 {
        struct kvm_lapic_irq irq = {
                .delivery_mode = APIC_DM_FIXED,
@@ -2693,16 +2694,44 @@ static void kvm_hv_send_ipi_to_many(struct kvm *kvm, 
u32 vector,
        unsigned long i;
 
        kvm_for_each_vcpu(i, vcpu, kvm) {
+               struct kvm_vcpu *target = vcpu;
+
                if (sparse_banks &&
                    !hv_is_vp_in_sparse_set(kvm_hv_get_vpindex(vcpu),
                                            valid_bank_mask, sparse_banks))
                        continue;
 
+               /*
+                * TLFS: the IPI targets the VPs' in_vtl (or the sender's
+                * VTL): deliver into that plane's APIC.
+                */
+               if (target_plane) {
+                       target = READ_ONCE(vcpu->common->vcpus[target_plane]);
+                       if (!target)
+                               continue;
+               }
+
                /* We fail only when APIC is disabled */
-               kvm_apic_set_irq(vcpu, &irq, NULL);
+               kvm_apic_set_irq(target, &irq, NULL);
        }
 }
 
+/*
+ * TLFS: in_vtl bit 4 selects an explicit target VTL.  A VTL may interrupt
+ * its own level and lower ones, never a higher one (TLFS 15.9, "VTL
+ * Interrupt Management": each VTL sends and receives interrupts without
+ * interference from a less secure VTL).  Returns the target plane, or -1
+ * for a target above the caller's.
+ */
+static int hv_send_ipi_target_plane(struct kvm_vcpu *vcpu, u8 in_vtl)
+{
+       if (!(in_vtl & 0x10))
+               return vcpu->plane_level;
+       if ((in_vtl & 0xf) > vcpu->plane_level)
+               return -1;
+       return in_vtl & 0xf;
+}
+
 static int kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
 {
        struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
@@ -2712,6 +2741,8 @@ static int kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct 
kvm_hv_hcall *hc)
        struct hv_send_ipi send_ipi;
        u64 valid_bank_mask;
        u32 vector;
+       u8 in_vtl = 0;
+       int target_plane;
        bool all_cpus;
        int r;
 
@@ -2729,13 +2760,28 @@ static int kvm_hv_send_ipi(struct kvm_vcpu *vcpu, 
struct kvm_hv_hcall *hc)
                                return HV_STATUS_INVALID_HYPERCALL_INPUT;
                        sparse_banks[0] = send_ipi.cpu_mask;
                        vector = send_ipi.vector;
+                       in_vtl = send_ipi.in_vtl.as_uint8;
                } else {
                        /* 'reserved' part of hv_send_ipi should be 0 */
-                       if (unlikely(hc->ingpa >> 32 != 0))
+                       if (unlikely(hc->ingpa >> 40 != 0))
                                return HV_STATUS_INVALID_HYPERCALL_INPUT;
                        sparse_banks[0] = hc->outgpa;
                        vector = (u32)hc->ingpa;
+                       in_vtl = (hc->ingpa >> 32) & 0xff;
                }
+
+               /*
+                * Only hv_input_vtl.target_vtl (bits 3:0) and
+                * use_target_vtl (bit 4) are defined; reserved_z (7:5)
+                * must be zero in every form of the hypercall, before
+                * anything is delivered.
+                */
+               if (unlikely(in_vtl & 0xe0))
+                       return HV_STATUS_INVALID_HYPERCALL_INPUT;
+               target_plane = hv_send_ipi_target_plane(vcpu, in_vtl);
+               if (target_plane < 0)
+                       return HV_STATUS_ACCESS_DENIED;
+
                all_cpus = false;
                valid_bank_mask = BIT_ULL(0);
 
@@ -2745,12 +2791,24 @@ static int kvm_hv_send_ipi(struct kvm_vcpu *vcpu, 
struct kvm_hv_hcall *hc)
                        if (unlikely(kvm_vcpu_read_guest(vcpu, hc->ingpa, 
&send_ipi_ex,
                                                         sizeof(send_ipi_ex))))
                                return HV_STATUS_INVALID_HYPERCALL_INPUT;
+                       in_vtl = send_ipi_ex.in_vtl.as_uint8;
                } else {
                        send_ipi_ex.vector = (u32)hc->ingpa;
                        send_ipi_ex.vp_set.format = hc->outgpa;
                        send_ipi_ex.vp_set.valid_bank_mask = 
sse128_lo(hc->xmm[0]);
+                       in_vtl = (hc->ingpa >> 32) & 0xff;
                }
 
+               /*
+                * As above: hv_input_vtl bits 7:5 are reserved, and a target
+                * VTL above the caller's is refused even for an empty set.
+                */
+               if (unlikely(in_vtl & 0xe0))
+                       return HV_STATUS_INVALID_HYPERCALL_INPUT;
+               target_plane = hv_send_ipi_target_plane(vcpu, in_vtl);
+               if (target_plane < 0)
+                       return HV_STATUS_ACCESS_DENIED;
+
                trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector,
                                         send_ipi_ex.vp_set.format,
                                         send_ipi_ex.vp_set.valid_bank_mask);
@@ -2783,9 +2841,10 @@ static int kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct 
kvm_hv_hcall *hc)
                return HV_STATUS_INVALID_HYPERCALL_INPUT;
 
        if (all_cpus)
-               kvm_hv_send_ipi_to_many(kvm, vector, NULL, 0);
+               kvm_hv_send_ipi_to_many(kvm, vector, NULL, 0, target_plane);
        else
-               kvm_hv_send_ipi_to_many(kvm, vector, sparse_banks, 
valid_bank_mask);
+               kvm_hv_send_ipi_to_many(kvm, vector, sparse_banks,
+                                       valid_bank_mask, target_plane);
 
 ret_success:
        return HV_STATUS_SUCCESS;
diff --git a/include/hyperv/hvgdk_mini.h b/include/hyperv/hvgdk_mini.h
index 8346b2212c8c..21fcdd085a9b 100644
--- a/include/hyperv/hvgdk_mini.h
+++ b/include/hyperv/hvgdk_mini.h
@@ -1356,7 +1356,8 @@ struct hv_input_set_vp_registers {
 /* HvCallSendSyntheticClusterIpi hypercall */
 struct hv_send_ipi {    /* HV_INPUT_SEND_SYNTHETIC_CLUSTER_IPI */
        u32 vector;
-       u32 reserved;
+       union hv_input_vtl in_vtl;
+       u8 reserved[3];
        u64 cpu_mask;
 } __packed;
 
diff --git a/include/hyperv/hvhdk_mini.h b/include/hyperv/hvhdk_mini.h
index 57b70efe60d4..6ded425453df 100644
--- a/include/hyperv/hvhdk_mini.h
+++ b/include/hyperv/hvhdk_mini.h
@@ -465,7 +465,8 @@ struct hv_input_unmap_device_interrupt {
 
 struct hv_send_ipi_ex { /* HV_INPUT_SEND_SYNTHETIC_CLUSTER_IPI_EX */
        u32 vector;
-       u32 reserved;
+       union hv_input_vtl in_vtl;
+       u8 reserved[3];
        struct hv_vpset vp_set;
 } __packed;
 
-- 
2.50.1


Reply via email to