Skip to content

Commit 56b5354

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86: hyper-v: Add helper to read hypercall data for array
Move the guts of kvm_get_sparse_vp_set() to a helper so that the code for reading a guest-provided array can be reused in the future, e.g. for getting a list of virtual addresses whose TLB entries need to be flushed. Opportunisticaly swap the order of the data and XMM adjustment so that the XMM/gpa offsets are bundled together. No functional change intended. Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Vitaly Kuznetsov <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 0823570 commit 56b5354

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

arch/x86/kvm/hyperv.c

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,38 +1753,51 @@ struct kvm_hv_hcall {
17531753
sse128_t xmm[HV_HYPERCALL_MAX_XMM_REGISTERS];
17541754
};
17551755

1756-
static u64 kvm_get_sparse_vp_set(struct kvm *kvm, struct kvm_hv_hcall *hc,
1757-
int consumed_xmm_halves,
1758-
u64 *sparse_banks, gpa_t offset)
1759-
{
1760-
u16 var_cnt;
1761-
int i;
17621756

1763-
if (hc->var_cnt > 64)
1764-
return -EINVAL;
1765-
1766-
/* Ignore banks that cannot possibly contain a legal VP index. */
1767-
var_cnt = min_t(u16, hc->var_cnt, KVM_HV_MAX_SPARSE_VCPU_SET_BITS);
1757+
static int kvm_hv_get_hc_data(struct kvm *kvm, struct kvm_hv_hcall *hc,
1758+
u16 orig_cnt, u16 cnt_cap, u64 *data,
1759+
int consumed_xmm_halves, gpa_t offset)
1760+
{
1761+
/*
1762+
* Preserve the original count when ignoring entries via a "cap", KVM
1763+
* still needs to validate the guest input (though the non-XMM path
1764+
* punts on the checks).
1765+
*/
1766+
u16 cnt = min(orig_cnt, cnt_cap);
1767+
int i, j;
17681768

17691769
if (hc->fast) {
17701770
/*
17711771
* Each XMM holds two sparse banks, but do not count halves that
17721772
* have already been consumed for hypercall parameters.
17731773
*/
1774-
if (hc->var_cnt > 2 * HV_HYPERCALL_MAX_XMM_REGISTERS - consumed_xmm_halves)
1774+
if (orig_cnt > 2 * HV_HYPERCALL_MAX_XMM_REGISTERS - consumed_xmm_halves)
17751775
return HV_STATUS_INVALID_HYPERCALL_INPUT;
1776-
for (i = 0; i < var_cnt; i++) {
1777-
int j = i + consumed_xmm_halves;
1776+
1777+
for (i = 0; i < cnt; i++) {
1778+
j = i + consumed_xmm_halves;
17781779
if (j % 2)
1779-
sparse_banks[i] = sse128_hi(hc->xmm[j / 2]);
1780+
data[i] = sse128_hi(hc->xmm[j / 2]);
17801781
else
1781-
sparse_banks[i] = sse128_lo(hc->xmm[j / 2]);
1782+
data[i] = sse128_lo(hc->xmm[j / 2]);
17821783
}
17831784
return 0;
17841785
}
17851786

1786-
return kvm_read_guest(kvm, hc->ingpa + offset, sparse_banks,
1787-
var_cnt * sizeof(*sparse_banks));
1787+
return kvm_read_guest(kvm, hc->ingpa + offset, data,
1788+
cnt * sizeof(*data));
1789+
}
1790+
1791+
static u64 kvm_get_sparse_vp_set(struct kvm *kvm, struct kvm_hv_hcall *hc,
1792+
u64 *sparse_banks, int consumed_xmm_halves,
1793+
gpa_t offset)
1794+
{
1795+
if (hc->var_cnt > 64)
1796+
return -EINVAL;
1797+
1798+
/* Cap var_cnt to ignore banks that cannot contain a legal VP index. */
1799+
return kvm_hv_get_hc_data(kvm, hc, hc->var_cnt, KVM_HV_MAX_SPARSE_VCPU_SET_BITS,
1800+
sparse_banks, consumed_xmm_halves, offset);
17881801
}
17891802

17901803
static void hv_tlb_flush_enqueue(struct kvm_vcpu *vcpu)
@@ -1895,7 +1908,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
18951908
if (!hc->var_cnt)
18961909
goto ret_success;
18971910

1898-
if (kvm_get_sparse_vp_set(kvm, hc, 2, sparse_banks,
1911+
if (kvm_get_sparse_vp_set(kvm, hc, sparse_banks, 2,
18991912
offsetof(struct hv_tlb_flush_ex,
19001913
hv_vp_set.bank_contents)))
19011914
return HV_STATUS_INVALID_HYPERCALL_INPUT;
@@ -2006,7 +2019,7 @@ static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
20062019
if (!hc->var_cnt)
20072020
goto ret_success;
20082021

2009-
if (kvm_get_sparse_vp_set(kvm, hc, 1, sparse_banks,
2022+
if (kvm_get_sparse_vp_set(kvm, hc, sparse_banks, 1,
20102023
offsetof(struct hv_send_ipi_ex,
20112024
vp_set.bank_contents)))
20122025
return HV_STATUS_INVALID_HYPERCALL_INPUT;

0 commit comments

Comments
 (0)