From nobody Tue Feb 10 12:13:59 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 16739063890021013.0621152064534; Mon, 16 Jan 2023 13:59:49 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pHXV9-0006yD-3I; Mon, 16 Jan 2023 16:58:47 -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 1pHXUr-0006jM-R7 for qemu-devel@nongnu.org; Mon, 16 Jan 2023 16:58:29 -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 1pHXUm-0003ZP-R8 for qemu-devel@nongnu.org; Mon, 16 Jan 2023 16:58:29 -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 1pHXUk-0097VJ-8e; Mon, 16 Jan 2023 21:58:26 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1pHXUW-004iP2-1j; Mon, 16 Jan 2023 21:58:08 +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: Content-Type:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description; bh=z36itPGgE45FVcWHzz8SiWENC1Vb3J3Hqet/BUpUSyM=; b=YV8hCjpgcTD2C3Jda+MBCKGpIk YpgNpEKqQcGGDyqMGXjdSqN1Z6Ey1q8jwM7oXs3g74FWglnnVZBGwrGfEXgDxaBDYx6MEYKsOUxgq p1qiiJ0zwjoApVt9/T2iit6xnC3seC6fk4NTTNjqha0+z84NEj1ZvA7yonn15Tv0SZRk3Z8BtwVg6 X7piiPQ1CrPAfsY3sm/7vb7geSWswcQUXE6hwNG+pws7dA1W3+I0mJIT8Ny93IqHXapKyHXyJ+ZLl HBDEwy/BSdVhTBpIqFkOPp8Aplu2dq6zWl3f7l+jrtyDXMeyaCUYdXz+uldVudT8DHTD+HZv57A7L o2v/tasQ==; 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 , Julien Grall , "Michael S. Tsirkin" , Marcel Apfelbaum , armbru@redhat.com Subject: [PATCH v7 21/51] i386/xen: handle VCPUOP_register_vcpu_time_info Date: Mon, 16 Jan 2023 21:57:35 +0000 Message-Id: <20230116215805.1123514-22-dwmw2@infradead.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230116215805.1123514-1-dwmw2@infradead.org> References: <20230116215805.1123514-1-dwmw2@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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+fb0b8ce1ba8490165fd5+7085+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: 1673906390170100003 From: Joao Martins In order to support Linux vdso in Xen. Signed-off-by: Joao Martins Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant --- target/i386/cpu.h | 1 + target/i386/kvm/xen-emu.c | 100 +++++++++++++++++++++++++++++++++----- target/i386/machine.c | 1 + 3 files changed, 90 insertions(+), 12 deletions(-) diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 109b2e5669..96c2d0d5cb 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -1790,6 +1790,7 @@ typedef struct CPUArchState { struct kvm_nested_state *nested_state; uint64_t xen_vcpu_info_gpa; uint64_t xen_vcpu_info_default_gpa; + uint64_t xen_vcpu_time_info_gpa; #endif #if defined(CONFIG_HVF) HVFX86LazyFlags hvf_lflags; diff --git a/target/i386/kvm/xen-emu.c b/target/i386/kvm/xen-emu.c index 817196c988..4f905eda74 100644 --- a/target/i386/kvm/xen-emu.c +++ b/target/i386/kvm/xen-emu.c @@ -36,28 +36,41 @@ #define hypercall_compat32(longmode) (false) #endif =20 -static int kvm_gva_rw(CPUState *cs, uint64_t gva, void *_buf, size_t sz, - bool is_write) +static bool kvm_gva_to_gpa(CPUState *cs, uint64_t gva, uint64_t *gpa, + size_t *len, bool is_write) { - uint8_t *buf =3D (uint8_t *)_buf; - int ret; - - while (sz) { struct kvm_translation tr =3D { .linear_address =3D gva, }; =20 - size_t len =3D TARGET_PAGE_SIZE - (tr.linear_address & ~TARGET_PAG= E_MASK); - if (len > sz) { - len =3D sz; + if (len) { + *len =3D TARGET_PAGE_SIZE - (gva & ~TARGET_PAGE_MASK); + } + + if (kvm_vcpu_ioctl(cs, KVM_TRANSLATE, &tr) || !tr.valid || + (is_write && !tr.writeable)) { + return false; } + *gpa =3D tr.physical_address; + return true; +} + +static int kvm_gva_rw(CPUState *cs, uint64_t gva, void *_buf, size_t sz, + bool is_write) +{ + uint8_t *buf =3D (uint8_t *)_buf; + uint64_t gpa; + size_t len; =20 - ret =3D kvm_vcpu_ioctl(cs, KVM_TRANSLATE, &tr); - if (ret || !tr.valid || (is_write && !tr.writeable)) { + while (sz) { + if (!kvm_gva_to_gpa(cs, gva, &gpa, &len, is_write)) { return -EFAULT; } + if (len > sz) { + len =3D sz; + } =20 - cpu_physical_memory_rw(tr.physical_address, buf, len, is_write); + cpu_physical_memory_rw(gpa, buf, len, is_write); =20 buf +=3D len; sz -=3D len; @@ -145,6 +158,7 @@ int kvm_xen_init_vcpu(CPUState *cs) =20 env->xen_vcpu_info_gpa =3D INVALID_GPA; env->xen_vcpu_info_default_gpa =3D INVALID_GPA; + env->xen_vcpu_time_info_gpa =3D INVALID_GPA; =20 return 0; } @@ -228,6 +242,17 @@ static void do_set_vcpu_info_gpa(CPUState *cs, run_on_= cpu_data data) env->xen_vcpu_info_gpa); } =20 +static void do_set_vcpu_time_info_gpa(CPUState *cs, run_on_cpu_data data) +{ + X86CPU *cpu =3D X86_CPU(cs); + CPUX86State *env =3D &cpu->env; + + env->xen_vcpu_time_info_gpa =3D data.host_ulong; + + kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO, + env->xen_vcpu_time_info_gpa); +} + static void do_vcpu_soft_reset(CPUState *cs, run_on_cpu_data data) { X86CPU *cpu =3D X86_CPU(cs); @@ -235,8 +260,11 @@ static void do_vcpu_soft_reset(CPUState *cs, run_on_cp= u_data data) =20 env->xen_vcpu_info_gpa =3D INVALID_GPA; env->xen_vcpu_info_default_gpa =3D INVALID_GPA; + env->xen_vcpu_time_info_gpa =3D INVALID_GPA; =20 kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO, INVALID_GP= A); + kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO, + INVALID_GPA); } =20 static int xen_set_shared_info(uint64_t gfn) @@ -450,6 +478,42 @@ static int vcpuop_register_vcpu_info(CPUState *cs, CPU= State *target, return 0; } =20 +static int vcpuop_register_vcpu_time_info(CPUState *cs, CPUState *target, + uint64_t arg) +{ + struct vcpu_register_time_memory_area tma; + uint64_t gpa; + size_t len; + + /* No need for 32/64 compat handling */ + qemu_build_assert(sizeof(tma) =3D=3D 8); + qemu_build_assert(sizeof(struct vcpu_time_info) =3D=3D 32); + + if (!target) { + return -ENOENT; + } + + if (kvm_copy_from_gva(cs, arg, &tma, sizeof(tma))) { + return -EFAULT; + } + + /* + * Xen actually uses the GVA and does the translation through the guest + * page tables each time. But Linux/KVM uses the GPA, on the assumption + * that guests only ever use *global* addresses (kernel virtual addres= ses) + * for it. If Linux is changed to redo the GVA=E2=86=92GPA translation= each time, + * it will offer a new vCPU attribute for that, and we'll use it inste= ad. + */ + if (!kvm_gva_to_gpa(cs, tma.addr.p, &gpa, &len, false) || + len < sizeof(struct vcpu_time_info)) { + return -EFAULT; + } + + async_run_on_cpu(target, do_set_vcpu_time_info_gpa, + RUN_ON_CPU_HOST_ULONG(gpa)); + return 0; +} + static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu, int cmd, int vcpu_id, uint64_t arg) { @@ -458,6 +522,9 @@ static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *= exit, X86CPU *cpu, int err; =20 switch (cmd) { + case VCPUOP_register_vcpu_time_memory_area: + err =3D vcpuop_register_vcpu_time_info(cs, dest, arg); + break; case VCPUOP_register_vcpu_info: err =3D vcpuop_register_vcpu_info(cs, dest, arg); break; @@ -645,6 +712,15 @@ int kvm_put_xen_state(CPUState *cs) } } =20 + gpa =3D env->xen_vcpu_time_info_gpa; + if (gpa !=3D INVALID_GPA) { + ret =3D kvm_xen_set_vcpu_attr(cs, KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME= _INFO, + gpa); + if (ret < 0) { + return ret; + } + } + return 0; } =20 diff --git a/target/i386/machine.c b/target/i386/machine.c index 1215e616c8..eb657907ca 100644 --- a/target/i386/machine.c +++ b/target/i386/machine.c @@ -1272,6 +1272,7 @@ static const VMStateDescription vmstate_xen_vcpu =3D { .fields =3D (VMStateField[]) { VMSTATE_UINT64(env.xen_vcpu_info_gpa, X86CPU), VMSTATE_UINT64(env.xen_vcpu_info_default_gpa, X86CPU), + VMSTATE_UINT64(env.xen_vcpu_time_info_gpa, X86CPU), VMSTATE_END_OF_LIST() } }; --=20 2.39.0