From nobody Tue Feb 10 15:45:29 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646677452767563.194759223614; Mon, 7 Mar 2022 10:24:12 -0800 (PST) Received: from localhost ([::1]:34904 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nRI1j-0008Sa-Pl for importer@patchew.org; Mon, 07 Mar 2022 13:24:11 -0500 Received: from eggs.gnu.org ([209.51.188.92]:55346) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRHuh-00013S-N2 for qemu-devel@nongnu.org; Mon, 07 Mar 2022 13:16:55 -0500 Received: from [2a00:1450:4864:20::529] (port=43874 helo=mail-ed1-x529.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nRHud-0005Mg-4v for qemu-devel@nongnu.org; Mon, 07 Mar 2022 13:16:55 -0500 Received: by mail-ed1-x529.google.com with SMTP id f8so21137164edf.10 for ; Mon, 07 Mar 2022 10:16:38 -0800 (PST) Received: from avogadro.lan ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id d8-20020a170906304800b006cdf8a1e146sm4983382ejd.217.2022.03.07.10.16.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Mar 2022 10:16:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=M5dbaBdYAMjhrYbiwidflPhtFdmpOVM1ditmWCxx0uo=; b=SiSxul6XasWX2ziARjKwzxPd0DsfCqMhPX935iuIMhFWnu9WUkkpxySr41w4nkzZIp 0Os9UISV/nGe6Jqx5zPujm8Xo9qkrfmsAjVUd//kQWCUT2RYvDr44L7LnjKffXp8L5fy FWCi6cfO3NnGY0qzdWC35xSIaSYTED2xIMMQiQ0OnPBEnqeEzqFVSgnP0YMRgfj7hQkK SAxkKawVF7bSd2f5Hfamhqn+HkldilDTuv29/eStPO0ylgsfizcp9OjgaLvneWbhTjsb Woxxsgf7nN/5rXTy8m6DHRpAI+QgWuDeOxrcMZkhbW85JmpzzdTGSEELAfZlfVRQsCHO cJWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=M5dbaBdYAMjhrYbiwidflPhtFdmpOVM1ditmWCxx0uo=; b=Ca/Anrsfr6H/s0YKtqg1vLqcmGtFxtD0dNcWvh1IL39ePE1eSIh99ra+cWIgQGwNss l/0IpN59Mg5GBaDm/hdigIfBK4Sy7nqZBYYRzlJ/MJCkK2ZEsQfMeEFRMxFeUOkgr6nc ROgVu6zXnIf/YKfSKIGQoykKQ7s8+VRlLmqVi5ty8agqhQAbW5V06A5AM1LnOGbD+NYs ekhi+2qw2wnB1zRxIgR8/y0Co63zIKetPZBN9RkbPdeIpZO8lcvinFGzroy++3qefBFm vWe2MC1fNk6Is5iNDalUcuK5RVQuzFZwFglFVyeKWL7U+Ywd0hmlPeZ7rBlbmTLCxrum Qwuw== X-Gm-Message-State: AOAM530wwbAG2lYxUHK5GpY+StrEQthxOMkYVMR4Q08GEQjUObtrQeoH BefK4HzHli4HOgVN0FNBSeDb53KcsPM= X-Google-Smtp-Source: ABdhPJzYo2PvMFqn9Il0OdymtvoBnmvN4ErK12UiQope5a7PArsLCEtWr5gg0iLp+k0S2b1t0hovfA== X-Received: by 2002:a05:6402:1941:b0:413:2b5f:9074 with SMTP id f1-20020a056402194100b004132b5f9074mr12253622edz.414.1646676997137; Mon, 07 Mar 2022 10:16:37 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 02/23] whpx: Fixed incorrect CR8/TPR synchronization Date: Mon, 7 Mar 2022 19:16:12 +0100 Message-Id: <20220307181633.596898-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220307181633.596898-1-pbonzini@redhat.com> References: <20220307181633.596898-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::529 (failed) 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: pass client-ip=2a00:1450:4864:20::529; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x529.google.com X-Spam_score_int: 0 X-Spam_score: -0.1 X-Spam_bar: / X-Spam_report: (-0.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: , Cc: Ivan Shcherbakov Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646677454179100001 Content-Type: text/plain; charset="utf-8" From: Ivan Shcherbakov This fixes the following error triggered when stopping and resuming a 64-bit Linux kernel via gdb: qemu-system-x86_64.exe: WHPX: Failed to set virtual processor context, hr= =3Dc0350005 The previous logic for synchronizing the values did not take into account that the lower 4 bits of the CR8 register, containing the priority level, mapped to bits 7:4 of the APIC.TPR register (see section 10.8.6.1 of Volume 3 of Intel 64 and IA-32 Architectures Software Developer's Manual). The caused WHvSetVirtualProcessorRegisters() to fail with an error, effectively preventing GDB from changing the guest context. Signed-off-by: Ivan Shcherbakov Message-Id: <010b01d82874$bb4ef160$31ecd420$@sysprogs.com> Signed-off-by: Paolo Bonzini --- target/i386/whpx/whpx-all.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/target/i386/whpx/whpx-all.c b/target/i386/whpx/whpx-all.c index edd4fafbdf..63203730bc 100644 --- a/target/i386/whpx/whpx-all.c +++ b/target/i386/whpx/whpx-all.c @@ -256,6 +256,21 @@ static int whpx_set_tsc(CPUState *cpu) return 0; } =20 +/* + * The CR8 register in the CPU is mapped to the TPR register of the APIC, + * however, they use a slightly different encoding. Specifically: + * + * APIC.TPR[bits 7:4] =3D CR8[bits 3:0] + * + * This mechanism is described in section 10.8.6.1 of Volume 3 of Intel 64 + * and IA-32 Architectures Software Developer's Manual. + */ + +static uint64_t whpx_apic_tpr_to_cr8(uint64_t tpr) +{ + return tpr >> 4; +} + static void whpx_set_registers(CPUState *cpu, int level) { struct whpx_state *whpx =3D &whpx_global; @@ -284,7 +299,7 @@ static void whpx_set_registers(CPUState *cpu, int level) v86 =3D (env->eflags & VM_MASK); r86 =3D !(env->cr[0] & CR0_PE_MASK); =20 - vcpu->tpr =3D cpu_get_apic_tpr(x86_cpu->apic_state); + vcpu->tpr =3D whpx_apic_tpr_to_cr8(cpu_get_apic_tpr(x86_cpu->apic_stat= e)); vcpu->apic_base =3D cpu_get_apic_base(x86_cpu->apic_state); =20 idx =3D 0; @@ -475,6 +490,17 @@ static void whpx_get_registers(CPUState *cpu) hr); } =20 + if (whpx_apic_in_platform()) { + /* + * Fetch the TPR value from the emulated APIC. It may get overwrit= ten + * below with the value from CR8 returned by + * WHvGetVirtualProcessorRegisters(). + */ + whpx_apic_get(x86_cpu->apic_state); + vcpu->tpr =3D whpx_apic_tpr_to_cr8( + cpu_get_apic_tpr(x86_cpu->apic_state)); + } + idx =3D 0; =20 /* Indexes for first 16 registers match between HV and QEMU definition= s */ --=20 2.34.1