From nobody Tue Feb 10 07:43:16 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1670580518600205.84649644680076; Fri, 9 Dec 2022 02:08:38 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p3a7j-0002Zp-2Y; Fri, 09 Dec 2022 04:56:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p3a7K-0002Rt-Kt for qemu-devel@nongnu.org; Fri, 09 Dec 2022 04:56:30 -0500 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p3a7G-0006pD-Qr for qemu-devel@nongnu.org; Fri, 09 Dec 2022 04:56:30 -0500 Received: from i7.infradead.org ([2001:8b0:10b:1:21e:67ff:fecb:7a92]) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1p3a7G-007tiX-Ka; Fri, 09 Dec 2022 09:56:26 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1p3a78-002tZZ-83; Fri, 09 Dec 2022 09:56:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=V5ldt7mGXKiS4do5zSs5OUY0uAs4Hvft2QByQ6yhWIg=; b=PF5U7cyKRUsXLqYruzKJVNn6iF wy1jDLsxyFyUaMLKg3ZuHSn7WQ04SOwGOubb84BNe4qFlevPrfNo/DKtwNfcUTRPgE4IEM4A3pSPy cV4UiV62tuZXaKFoEhcqRKZ78B1ZrCEceAKYhsuL9q1FN4TXpeDZuexs2mRwe7dItVhgVcziv54t5 YedibP5sr/gU3QMtH/ymX+SOvtuxi+c5/RJLk33MyHapShurppEvn0qwVSa00VJm8CBnXTGpfTr7I en9BFQm82Fvho2ItfSHqj8HLwI1CnFA8Plvl2A0Up2GTQAHfsMKQMr8rAsJANVcaf8L0D4EeEUAph FHsJpuEA==; From: David Woodhouse To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Paul Durrant , Joao Martins , Ankur Arora , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Juan Quintela , "Dr . David Alan Gilbert" , Claudio Fontana Subject: [RFC PATCH v2 19/22] i386/xen: implement HVMOP_set_evtchn_upcall_vector Date: Fri, 9 Dec 2022 09:56:09 +0000 Message-Id: <20221209095612.689243-20-dwmw2@infradead.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20221209095612.689243-1-dwmw2@infradead.org> References: <20221209095612.689243-1-dwmw2@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2001:8b0:10b:1236::1; envelope-from=BATV+00235e7d7be18c3eb674+7047+infradead.org+dwmw2@casper.srs.infradead.org; helo=casper.infradead.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1670580520385100003 Content-Type: text/plain; charset="utf-8" From: Ankur Arora The HVMOP_set_evtchn_upcall_vector hypercall sets the per-vCPU upcall vector, to be delivered to the local APIC just like an MSI (with an EOI). This takes precedence over the system-wide delivery method set by the HVMOP_set_param hypercall with HVM_PARAM_CALLBACK_IRQ. It's used by Windows and Xen (PV shim) guests but normally not by Linux. Signed-off-by: Ankur Arora Signed-off-by: Joao Martins [dwmw2: Rework for upstream kernel changes and split from HVMOP_set_param] Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant --- target/i386/cpu.h | 1 + target/i386/kvm/kvm.c | 7 ++++ target/i386/machine.c | 4 ++- target/i386/trace-events | 1 + target/i386/xen.c | 69 +++++++++++++++++++++++++++++++++++++--- target/i386/xen.h | 1 + 6 files changed, 77 insertions(+), 6 deletions(-) diff --git a/target/i386/cpu.h b/target/i386/cpu.h index bf44a87ddb..938a1b9c8b 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -1792,6 +1792,7 @@ typedef struct CPUArchState { uint64_t xen_vcpu_info_default_gpa; uint64_t xen_vcpu_time_info_gpa; uint64_t xen_vcpu_runstate_gpa; + uint8_t xen_vcpu_callback_vector; #endif #if defined(CONFIG_HVF) HVFX86LazyFlags hvf_lflags; diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index a5e67a3119..dc1b3fc502 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -4762,6 +4762,13 @@ int kvm_arch_put_registers(CPUState *cpu, int level) return ret; } } + + if (x86_cpu->env.xen_vcpu_callback_vector) { + ret =3D kvm_xen_set_vcpu_callback_vector(cpu); + if (ret < 0) { + return ret; + } + } } #endif =20 diff --git a/target/i386/machine.c b/target/i386/machine.c index 6a510e5cbd..09e13cf716 100644 --- a/target/i386/machine.c +++ b/target/i386/machine.c @@ -1265,7 +1265,8 @@ static bool xen_vcpu_needed(void *opaque) return (env->xen_vcpu_info_gpa !=3D UINT64_MAX || env->xen_vcpu_info_default_gpa !=3D UINT64_MAX || env->xen_vcpu_time_info_gpa !=3D UINT64_MAX || - env->xen_vcpu_runstate_gpa !=3D UINT64_MAX); + env->xen_vcpu_runstate_gpa !=3D UINT64_MAX || + env->xen_vcpu_callback_vector !=3D 0); } =20 static const VMStateDescription vmstate_xen_vcpu =3D { @@ -1278,6 +1279,7 @@ static const VMStateDescription vmstate_xen_vcpu =3D { VMSTATE_UINT64(env.xen_vcpu_info_default_gpa, X86CPU), VMSTATE_UINT64(env.xen_vcpu_time_info_gpa, X86CPU), VMSTATE_UINT64(env.xen_vcpu_runstate_gpa, X86CPU), + VMSTATE_UINT8(env.xen_vcpu_callback_vector, X86CPU), VMSTATE_END_OF_LIST() } }; diff --git a/target/i386/trace-events b/target/i386/trace-events index 7118640697..58e28f1a19 100644 --- a/target/i386/trace-events +++ b/target/i386/trace-events @@ -16,3 +16,4 @@ kvm_sev_attestation_report(const char *mnonce, const char= *data) "mnonce %s data kvm_xen_hypercall(int cpu, uint8_t cpl, uint64_t input, uint64_t a0, uint6= 4_t a1, uint64_t a2, uint64_t ret) "xen_hypercall: cpu %d cpl %d input %" P= RIu64 " a0 0x%" PRIx64 " a1 0x%" PRIx64 " a2 0x%" PRIx64" ret 0x%" PRIx64 kvm_xen_set_shared_info(uint64_t gfn) "shared info at gfn 0x%" PRIx64 kvm_xen_set_vcpu_attr(int cpu, int type, uint64_t gpa) "vcpu attr cpu %d t= ype %d gpa 0x%" PRIx64 +kvm_xen_set_vcpu_callback(int cpu, int vector) "callback vcpu %d vector %d" diff --git a/target/i386/xen.c b/target/i386/xen.c index 97032049e6..2583c00a6b 100644 --- a/target/i386/xen.c +++ b/target/i386/xen.c @@ -19,6 +19,7 @@ #include "standard-headers/xen/version.h" #include "standard-headers/xen/memory.h" #include "standard-headers/xen/hvm/hvm_op.h" +#include "standard-headers/xen/hvm/params.h" #include "standard-headers/xen/vcpu.h" =20 static bool kvm_gva_to_gpa(CPUState *cs, uint64_t gva, uint64_t *gpa, @@ -127,7 +128,8 @@ static bool kvm_xen_hcall_xen_version(struct kvm_xen_ex= it *exit, X86CPU *cpu, fi.submap |=3D 1 << XENFEAT_writable_page_tables | 1 << XENFEAT_writable_descriptor_tables | 1 << XENFEAT_auto_translated_physmap | - 1 << XENFEAT_supervisor_mode_kernel; + 1 << XENFEAT_supervisor_mode_kernel | + 1 << XENFEAT_hvm_callback_vector; } =20 err =3D kvm_copy_to_gva(CPU(cpu), arg, &fi, sizeof(fi)); @@ -154,6 +156,29 @@ int kvm_xen_set_vcpu_attr(CPUState *cs, uint16_t type,= uint64_t gpa) return kvm_vcpu_ioctl(cs, KVM_XEN_VCPU_SET_ATTR, &xhsi); } =20 +int kvm_xen_set_vcpu_callback_vector(CPUState *cs) +{ + uint8_t vector =3D X86_CPU(cs)->env.xen_vcpu_callback_vector; + struct kvm_xen_vcpu_attr xva; + + xva.type =3D KVM_XEN_VCPU_ATTR_TYPE_UPCALL_VECTOR; + xva.u.vector =3D vector; + + trace_kvm_xen_set_vcpu_callback(cs->cpu_index, vector); + + return kvm_vcpu_ioctl(cs, KVM_XEN_HVM_SET_ATTR, &xva); +} + +static void do_set_vcpu_callback_vector(CPUState *cs, run_on_cpu_data data) +{ + X86CPU *cpu =3D X86_CPU(cs); + CPUX86State *env =3D &cpu->env; + + env->xen_vcpu_callback_vector =3D data.host_int; + + kvm_xen_set_vcpu_callback_vector(cs); +} + static void do_set_vcpu_info_default_gpa(CPUState *cs, run_on_cpu_data dat= a) { X86CPU *cpu =3D X86_CPU(cs); @@ -262,17 +287,51 @@ static bool kvm_xen_hcall_memory_op(struct kvm_xen_ex= it *exit, return true; } =20 -static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, +static int kvm_xen_hcall_evtchn_upcall_vector(struct kvm_xen_exit *exit, + X86CPU *cpu, uint64_t arg) +{ + struct xen_hvm_evtchn_upcall_vector *up; + CPUState *target_cs; + int vector; + + up =3D gva_to_hva(CPU(cpu), arg); + if (!up) { + return -EFAULT; + } + + vector =3D up->vector; + if (vector < 0x10) { + return -EINVAL; + } + + target_cs =3D qemu_get_cpu(up->vcpu); + if (!target_cs) { + return -EINVAL; + } + + async_run_on_cpu(target_cs, do_set_vcpu_callback_vector, RUN_ON_CPU_HO= ST_INT(vector)); + return 0; +} + +static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, X86CPU *cpu, int cmd, uint64_t arg) { + int ret =3D -ENOSYS; switch (cmd) { + case HVMOP_set_evtchn_upcall_vector: + ret =3D kvm_xen_hcall_evtchn_upcall_vector(exit, cpu, + exit->u.hcall.params[= 0]); + break; case HVMOP_pagetable_dying: - exit->u.hcall.result =3D -ENOSYS; - return true; + ret =3D -ENOSYS; + break; =20 default: return false; } + + exit->u.hcall.result =3D ret; + return true; } =20 static int vcpuop_register_vcpu_info(CPUState *cs, CPUState *target, @@ -377,7 +436,7 @@ static bool __kvm_xen_handle_exit(X86CPU *cpu, struct k= vm_xen_exit *exit) exit->u.hcall.params[1], exit->u.hcall.params[2]); case __HYPERVISOR_hvm_op: - return kvm_xen_hcall_hvm_op(exit, exit->u.hcall.params[0], + return kvm_xen_hcall_hvm_op(exit, cpu, exit->u.hcall.params[0], exit->u.hcall.params[1]); case __HYPERVISOR_memory_op: return kvm_xen_hcall_memory_op(exit, exit->u.hcall.params[0], diff --git a/target/i386/xen.h b/target/i386/xen.h index 53573e07f8..07b133ae29 100644 --- a/target/i386/xen.h +++ b/target/i386/xen.h @@ -25,5 +25,6 @@ int kvm_xen_init(KVMState *s, uint32_t xen_version); int kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit); int kvm_xen_set_vcpu_attr(CPUState *cs, uint16_t type, uint64_t gpa); +int kvm_xen_set_vcpu_callback_vector(CPUState *cs); =20 #endif /* QEMU_I386_XEN_H */ --=20 2.35.3