From nobody Sun Apr 5 19:50:07 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=reject dis=none) header.from=unpredictable.fr ARC-Seal: i=1; a=rsa-sha256; t=1774294714; cv=none; d=zohomail.com; s=zohoarc; b=I3Jmkw6w4auX5+S85zr61bEbACX+JQDIdhPpZQMRVTRDNJUjp7ttlsOF6QFAAnXi4m4VkyKqHH4tJ1TP+/2a/NCo5FkEZ+F38Q6QvlFvAB/4EcUSBdafVmCufn84ewh29wR93YgA2OlAfQB6LPUTmPn3xLUPUf9bnCN/W2k02Jc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1774294714; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=O7bv60w0UvYYo2pAak90SAs20MfD3p1ZThvHIrFn3bk=; b=j6h9DYWvfCvl+sKz92qMjj6bXIJKLvs2aPlpiJeFaHkh6n/WTXnr6qc5yOSJNodcWKIZQ4HHnkgOB4anGZGV3MnqKgiIwI4NUZ2dTx0pCX854V5QAK7n/cRTCj2czHUl72/DqTpEHZ56tU2UECVzUelt5mz8AjvvYN48WVGWG7o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17742947146258.71072981602083; Mon, 23 Mar 2026 12:38:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w4l5Z-0005w9-7w; Mon, 23 Mar 2026 15:37:25 -0400 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 1w4l5R-0005qm-Jv for qemu-devel@nongnu.org; Mon, 23 Mar 2026 15:37:17 -0400 Received: from p-west3-cluster3-host12-snip4-10.eps.apple.com ([57.103.72.231] helo=outbound.ms.icloud.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w4l5P-0004Zg-U2 for qemu-devel@nongnu.org; Mon, 23 Mar 2026 15:37:17 -0400 Received: from outbound.ms.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-west-3a-60-percent-7 (Postfix) with ESMTPS id AD7041800100; Mon, 23 Mar 2026 19:37:11 +0000 (UTC) Received: from mac.home (unknown [17.57.154.37]) by p00-icloudmta-asmtp-us-west-3a-60-percent-7 (Postfix) with ESMTPSA id D79351800101; Mon, 23 Mar 2026 19:37:09 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unpredictable.fr; s=sig1; t=1774294633; x=1776886633; bh=O7bv60w0UvYYo2pAak90SAs20MfD3p1ZThvHIrFn3bk=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=JEs5RMkIWT/0hL2TJZUeqKH1SD9sDTz86QbjTjEmJVO6XWUDAFk5QclFvN/gE55CIE/h58iLkv+T27AJj0nsK3Vmou2/EA0Wk7sF55VXGQCHoKqiV+UpvX55BCn8SL3n6jKSf1mqx2Hbrc0/dOW6mIkm9z125YjWz5ySoZMNbUeNpsnqwHof02n0NThLbq5gb6Ue29rrP2reALw97iLdMR1oHxDZwCF9HrgRMKsPvUhponyQUeYYxag/k9lfN1/3+tAErnWmfX7kzuPmmK9VC2rLO/KRTN67un/c2x53vUkGpJ/XsISRWk15JZcurpWsCeRPLIkorvQF5ylsRNtD3w== mail-alias-created-date: 1752046281608 From: Mohamed Mediouni To: qemu-devel@nongnu.org Cc: Mohamed Mediouni , Wei Liu , Phil Dennis-Jordan , Pedro Barbuda , Roman Bolshakov Subject: [PATCH 12/12] whpx: i386: fast runtime state reads Date: Mon, 23 Mar 2026 20:36:45 +0100 Message-ID: <20260323193645.82602-13-mohamed@unpredictable.fr> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260323193645.82602-1-mohamed@unpredictable.fr> References: <20260323193645.82602-1-mohamed@unpredictable.fr> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Authority-Info-Out: v=2.4 cv=MsRfKmae c=1 sm=1 tr=0 ts=69c19668 cx=c_apl:c_pps:t_out a=qkKslKyYc0ctBTeLUVfTFg==:117 a=Yq5XynenixoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=WK8LibQvd4XDxIxmQ_8A:9 X-Proofpoint-GUID: YDjA3Nqse8yiy_CaBfLpNH7x6_cOtsIH X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMzIzMDE0NSBTYWx0ZWRfX0Tf93dJjoz06 ZZZ6hVEPMtMZ13+0ohVGePv5q90DiEwj0+tErXQb2chBvX4KCbrv083DxDZEUvGP7WsHuc5uMwi 2GzWBNs6NOaw2kxAsE1iULuJMJ1bM/TuGJCNE05iTLCq5dE1X/2oaf9emjVeg+R/HrFlB4TMmSN JR3mNxVQ5z1hvVItQAxEXgIiiNyQokVpvo5I3fHwjEGeX4k2vUIk0s6iBepTYOYX8nRFd/yeZP5 aPyvHtF0z17bqWyfY1iubNb2skBaB76MTs3NtZ3Npttw2juVPIlNW0qZdkUOqZap1RUHfTUe/e5 j7psW42AJAQrjQIYTNyktFKHjI5PwvQH5amUrfK9vOnDVRhM3UJEs2t58O5DP8= X-Proofpoint-ORIG-GUID: YDjA3Nqse8yiy_CaBfLpNH7x6_cOtsIH X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-03-23_04,2026-03-23_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 adultscore=0 mlxlogscore=761 clxscore=1030 bulkscore=0 phishscore=0 suspectscore=0 mlxscore=0 lowpriorityscore=0 spamscore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2603230145 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=57.103.72.231; envelope-from=mohamed@unpredictable.fr; helo=outbound.ms.icloud.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-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: qemu development 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: pass (identity @unpredictable.fr) X-ZM-MESSAGEID: 1774294716229154100 Content-Type: text/plain; charset="utf-8" Now that there's an on-demand interface for fetching CRs and segments, only query GPRs and query everything else on-demand for vmexits. Signed-off-by: Mohamed Mediouni --- target/i386/whpx/whpx-all.c | 77 +++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/target/i386/whpx/whpx-all.c b/target/i386/whpx/whpx-all.c index 623bfb0105..021398e5bd 100644 --- a/target/i386/whpx/whpx-all.c +++ b/target/i386/whpx/whpx-all.c @@ -156,6 +156,26 @@ static const WHV_REGISTER_NAME whpx_register_names[] = =3D { */ }; =20 +static const WHV_REGISTER_NAME whpx_register_names_for_vmexit[] =3D { + /* X64 General purpose registers */ + WHvX64RegisterRax, + WHvX64RegisterRcx, + WHvX64RegisterRdx, + WHvX64RegisterRbx, + WHvX64RegisterRsp, + WHvX64RegisterRbp, + WHvX64RegisterRsi, + WHvX64RegisterRdi, + WHvX64RegisterR8, + WHvX64RegisterR9, + WHvX64RegisterR10, + WHvX64RegisterR11, + WHvX64RegisterR12, + WHvX64RegisterR13, + WHvX64RegisterR14, + WHvX64RegisterR15, +}; + struct whpx_register_set { WHV_REGISTER_VALUE values[RTL_NUMBER_OF(whpx_register_names)]; }; @@ -593,6 +613,47 @@ static void whpx_get_xcrs(CPUState *cpu) cpu_env(cpu)->xcr0 =3D xcr0.Reg64; } =20 +static void whpx_get_registers_for_vmexit(CPUState *cpu, WHPXStateLevel le= vel) +{ + struct whpx_state *whpx =3D &whpx_global; + AccelCPUState *vcpu =3D cpu->accel; + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; + struct whpx_register_set vcxt; + HRESULT hr; + int idx; + int idx_next; + + assert(cpu_is_stopped(cpu) || qemu_cpu_is_self(cpu)); + + hr =3D whp_dispatch.WHvGetVirtualProcessorRegisters( + whpx->partition, cpu->cpu_index, + whpx_register_names_for_vmexit, + RTL_NUMBER_OF(whpx_register_names_for_vmexit), + &vcxt.values[0]); + if (FAILED(hr)) { + error_report("WHPX: Failed to get virtual processor context, hr=3D= %08lx", + hr); + } + + idx =3D 0; + + /* Indexes for first 16 registers match between HV and QEMU definition= s */ + idx_next =3D 16; + for (idx =3D 0; idx < CPU_NB_REGS; idx +=3D 1) { + env->regs[idx] =3D vcxt.values[idx].Reg64; + } + idx =3D idx_next; + + env->eip =3D vcpu->exit_ctx.VpContext.Rip; + env->eflags =3D vcpu->exit_ctx.VpContext.Rflags; + rflags_to_lflags(env); + + assert(idx =3D=3D RTL_NUMBER_OF(whpx_register_names_for_vmexit)); + + x86_update_hflags(env); +} + void whpx_get_registers(CPUState *cpu, WHPXStateLevel level) { struct whpx_state *whpx =3D &whpx_global; @@ -608,7 +669,11 @@ void whpx_get_registers(CPUState *cpu, WHPXStateLevel = level) =20 assert(cpu_is_stopped(cpu) || qemu_cpu_is_self(cpu)); =20 - if (level > WHPX_LEVEL_FAST_RUNTIME_STATE && !env->tsc_valid) { + if (level =3D=3D WHPX_LEVEL_FAST_RUNTIME_STATE) { + return whpx_get_registers_for_vmexit(cpu, level); + } + + if (!env->tsc_valid) { whpx_get_tsc(cpu); env->tsc_valid =3D !runstate_is_running(); } @@ -623,7 +688,7 @@ void whpx_get_registers(CPUState *cpu, WHPXStateLevel l= evel) hr); } =20 - if (level > WHPX_LEVEL_FAST_RUNTIME_STATE && whpx_irqchip_in_kernel())= { + if (whpx_irqchip_in_kernel()) { /* * Fetch the TPR value from the emulated APIC. It may get overwrit= ten * below with the value from CR8 returned by @@ -680,7 +745,7 @@ void whpx_get_registers(CPUState *cpu, WHPXStateLevel l= evel) env->cr[4] =3D vcxt.values[idx++].Reg64; assert(whpx_register_names[idx] =3D=3D WHvX64RegisterCr8); tpr =3D vcxt.values[idx++].Reg64; - if (level > WHPX_LEVEL_FAST_RUNTIME_STATE && tpr !=3D vcpu->tpr) { + if (tpr !=3D vcpu->tpr) { vcpu->tpr =3D tpr; cpu_set_apic_tpr(x86_cpu->apic_state, whpx_cr8_to_apic_tpr(tpr)); } @@ -691,9 +756,7 @@ void whpx_get_registers(CPUState *cpu, WHPXStateLevel l= evel) * Extended control registers needs to be handled separately depending * on whether xsave is supported/enabled or not. */ - if (level > WHPX_LEVEL_FAST_RUNTIME_STATE) { - whpx_get_xcrs(cpu); - } + whpx_get_xcrs(cpu); =20 /* 16 XMM registers */ assert(whpx_register_names[idx] =3D=3D WHvX64RegisterXmm0); @@ -768,7 +831,7 @@ void whpx_get_registers(CPUState *cpu, WHPXStateLevel l= evel) =20 assert(idx =3D=3D RTL_NUMBER_OF(whpx_register_names)); =20 - if (level > WHPX_LEVEL_FAST_RUNTIME_STATE && whpx_irqchip_in_kernel())= { + if (whpx_irqchip_in_kernel()) { whpx_apic_get(x86_cpu->apic_state); } =20 --=20 2.50.1 (Apple Git-155)