[{"id":3675653,"web_url":"http://patchwork.ozlabs.org/comment/3675653/","msgid":"<E41FA391-ABC2-4CA9-850C-BDB3263C1E07@unpredictable.fr>","list_archive_url":null,"date":"2026-04-10T05:06:18","subject":"Re: [PATCH] target/arm/hvf: Fix WFI halting to stop idle vCPU\n spinning","submitter":{"id":91318,"url":"http://patchwork.ozlabs.org/api/people/91318/","name":"Mohamed Mediouni","email":"mohamed@unpredictable.fr"},"content":"> On 10. Apr 2026, at 06:47, Scott J. Goldman <scottjgo@gmail.com> wrote:\n> \n> Commit b5f8f77271 (\"accel/hvf: Implement WFI without using pselect()\")\n> changed hvf_wfi() from blocking the vCPU thread with pselect() to\n> returning EXCP_HLT, intending QEMU's main event loop to handle the\n> idle wait. However, cpu->halted was never set, so cpu_thread_is_idle()\n> always returns false and the vCPU thread spins at 100% CPU per core\n> while the guest is idle.\n> \n> Fix this by:\n> \n> 1. Setting cpu->halted = 1 in hvf_wfi() so the vCPU thread sleeps on\n>   halt_cond in qemu_process_cpu_events().\n> \n> 2. Arming a host-side QEMU_CLOCK_HOST timer to fire when the guest's\n>   virtual timer (CNTV_CVAL_EL0) would expire. This is necessary\n>   because HVF only delivers HV_EXIT_REASON_VTIMER_ACTIVATED during\n>   hv_vcpu_run(), which is not called while the CPU is halted. The\n>   timer callback mirrors the VTIMER_ACTIVATED handler: it raises the\n>   vtimer IRQ through the GIC and marks vtimer_masked, causing the\n>   interrupt delivery chain to wake the vCPU via qemu_cpu_kick().\n> \n> 3. Clearing cpu->halted in hvf_arch_vcpu_exec() when cpu_has_work()\n>   indicates a pending interrupt, and cancelling the WFI timer.\n> \n> Fixes: b5f8f77271 (\"accel/hvf: Implement WFI without using pselect()\")\n> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/959\n> Signed-off-by: Scott J. Goldman <scottjgo@gmail.com>\n\nHi,\n\nReviewed-by: Mohamed Mediouni <mohamed@unpredictable.fr>\n> ---\n> include/system/hvf_int.h |  1 +\n> target/arm/hvf/hvf.c     | 65 +++++++++++++++++++++++++++++++++++++++-\n> 2 files changed, 65 insertions(+), 1 deletion(-)\n> \n> diff --git a/include/system/hvf_int.h b/include/system/hvf_int.h\n> index 2621164cb2..58fb865eba 100644\n> --- a/include/system/hvf_int.h\n> +++ b/include/system/hvf_int.h\n> @@ -48,6 +48,7 @@ struct AccelCPUState {\n>     hv_vcpu_exit_t *exit;\n>     bool vtimer_masked;\n>     bool guest_debug_enabled;\n> +    struct QEMUTimer *wfi_timer;\n> #endif\n> };\n> \n> diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c\n> index 5fc8f6bbbd..0e737d3583 100644\n> --- a/target/arm/hvf/hvf.c\n> +++ b/target/arm/hvf/hvf.c\n> @@ -28,6 +28,7 @@\n> #include \"hw/core/boards.h\"\n> #include \"hw/core/irq.h\"\n> #include \"qemu/main-loop.h\"\n> +#include \"qemu/timer.h\"\n> #include \"system/cpus.h\"\n> #include \"arm-powerctl.h\"\n> #include \"target/arm/cpu.h\"\n> @@ -301,6 +302,8 @@ void hvf_arm_init_debug(void)\n> #define TMR_CTL_IMASK   (1 << 1)\n> #define TMR_CTL_ISTATUS (1 << 2)\n> \n> +static void hvf_wfi_timer_cb(void *opaque);\n> +\n> static uint32_t chosen_ipa_bit_size;\n> \n> typedef struct HVFVTimer {\n> @@ -1214,6 +1217,9 @@ void hvf_arch_vcpu_destroy(CPUState *cpu)\n> {\n>     hv_return_t ret;\n> \n> +    timer_free(cpu->accel->wfi_timer);\n> +    cpu->accel->wfi_timer = NULL;\n> +\n>     ret = hv_vcpu_destroy(cpu->accel->fd);\n>     assert_hvf_ok(ret);\n> }\n> @@ -1352,6 +1358,9 @@ int hvf_arch_init_vcpu(CPUState *cpu)\n>                               arm_cpu->isar.idregs[ID_AA64MMFR0_EL1_IDX]);\n>     assert_hvf_ok(ret);\n> \n> +    cpu->accel->wfi_timer = timer_new_ns(QEMU_CLOCK_HOST,\n> +                                          hvf_wfi_timer_cb, cpu);\n> +\n>     aarch64_add_sme_properties(OBJECT(cpu));\n>     return 0;\n> }\n> @@ -2027,8 +2036,29 @@ static uint64_t hvf_vtimer_val_raw(void)\n>     return mach_absolute_time() - hvf_state->vtimer_offset;\n> }\n> \n> +static void hvf_wfi_timer_cb(void *opaque)\n> +{\n> +    CPUState *cpu = opaque;\n> +    ARMCPU *arm_cpu = ARM_CPU(cpu);\n> +\n> +    /*\n> +     * vtimer expired while the CPU was halted for WFI.\n> +     * \t\n> +     * interrupt and mark as masked so hvf_sync_vtimer() will\n> +     * check and unmask when the guest handles it.\n> +     *\n> +     * The interrupt delivery chain (GIC -> cpu_interrupt ->\n> +     * qemu_cpu_kick) wakes the vCPU thread from halt_cond.\n> +     */\n> +    qemu_set_irq(arm_cpu->gt_timer_outputs[GTIMER_VIRT], 1);\n> +    cpu->accel->vtimer_masked = true;\n> +}\n> +\n> static int hvf_wfi(CPUState *cpu)\n> {\n> +    uint64_t ctl, cval;\n> +    hv_return_t r;\n> +\n>     if (cpu_has_work(cpu)) {\n>         /*\n>          * Don't bother to go into our \"low power state\" if\n> @@ -2037,6 +2067,35 @@ static int hvf_wfi(CPUState *cpu)\n>         return 0;\n>     }\n> \n> +    /*\n> +     * Set up a host-side timer to wake us when the vtimer expires.\n> +     * HVF only delivers HV_EXIT_REASON_VTIMER_ACTIVATED during\n> +     * hv_vcpu_run(), which we won't call while halted.\n> +     */\n> +    r = hv_vcpu_get_sys_reg(cpu->accel->fd, HV_SYS_REG_CNTV_CTL_EL0, &ctl);\n> +    assert_hvf_ok(r);\n> +\n> +    if ((ctl & TMR_CTL_ENABLE) && !(ctl & TMR_CTL_IMASK)) {\n> +        r = hv_vcpu_get_sys_reg(cpu->accel->fd,\n> +                                HV_SYS_REG_CNTV_CVAL_EL0, &cval);\n> +        assert_hvf_ok(r);\n> +\n> +        uint64_t now = hvf_vtimer_val_raw();\n> +        if (cval <= now) {\n> +            /* Timer already expired, don't halt */\n> +            return 0;\n> +        }\n> +\n> +        uint64_t delta_ticks = cval - now;\n\nWe have CNTFRQ_EL0 in cpu->gt_cntfrq_hz but this works too\nas XNU today tries to take care that host and VM run with\nthe same CNTFRQ_EL0\n\n> +        mach_timebase_info_data_t timebase;\n> +        mach_timebase_info(&timebase);\n> +        int64_t delta_ns = delta_ticks * timebase.numer / timebase.denom;\n> +        int64_t deadline = qemu_clock_get_ns(QEMU_CLOCK_HOST) + delta_ns;\n> +\n> +        timer_mod(cpu->accel->wfi_timer, deadline);\n> +    }\n> +\n> +    cpu->halted = 1;\n>     return EXCP_HLT;\n> }\n> \n> @@ -2332,7 +2391,11 @@ int hvf_arch_vcpu_exec(CPUState *cpu)\n>     hv_return_t r;\n> \n>     if (cpu->halted) {\n> -        return EXCP_HLT;\n> +        if (!cpu_has_work(cpu)) {\n> +            return EXCP_HLT;\n> +        }\n> +        cpu->halted = 0;\n> +        timer_del(cpu->accel->wfi_timer);\n>     }\n> \n>     flush_cpu_state(cpu);\n> -- \n> 2.50.1 (Apple Git-155)\n> \n>","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=unpredictable.fr header.i=@unpredictable.fr\n header.a=rsa-sha256 header.s=sig1 header.b=QsXvj74s;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fsPv03bxTz1yGb\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 10 Apr 2026 15:07:38 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wB44n-0002je-O5; Fri, 10 Apr 2026 01:06:41 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <mohamed@unpredictable.fr>)\n id 1wB44l-0002jA-PE\n for qemu-devel@nongnu.org; Fri, 10 Apr 2026 01:06:39 -0400","from p-west2-cluster6-host3-snip4-10.eps.apple.com ([57.103.70.13]\n helo=outbound.mr.icloud.com)\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <mohamed@unpredictable.fr>)\n id 1wB44j-000853-CS\n for qemu-devel@nongnu.org; Fri, 10 Apr 2026 01:06:39 -0400","from outbound.mr.icloud.com (unknown [127.0.0.2])\n by p00-icloudmta-asmtp-us-west-2a-100-percent-5 (Postfix) with ESMTPS id\n E1A08180019A; Fri, 10 Apr 2026 05:06:33 +0000 (UTC)","from smtpclient.apple (unknown [17.57.152.38])\n by p00-icloudmta-asmtp-us-west-2a-100-percent-5 (Postfix) with ESMTPSA id\n D5C2B1800136; Fri, 10 Apr 2026 05:06:31 +0000 (UTC)"],"Dkim-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=unpredictable.fr;\n s=sig1; t=1775797594; x=1778389594;\n bh=fFlzdLPddqiJVPIuJDZJCC+IL3keDdWnwv2Sa7Yn6v0=;\n h=Content-Type:Mime-Version:Subject:From:Date:Message-Id:To:x-icloud-hme;\n b=QsXvj74slMkKp13EKOo+omkKPSDdqrSSnqV+FyQ1ZHmNRPY4FGRaE07FCn8dzpRhRfosO8jHgS6e6XB+s9T6puD7MtSf1X97zHBHf+cny05lWV3CLqHPHZz+4eS+MwfU04HYsgaCc+DjhkCcLRaT4kIjXBzdUWU66Xv0Qg2ALJsPVJ8G5tD/e/3H9e47Q+z6rkV+tN78N7Jd/CZiuLAdbbE7Bmuxwu6Yz1OIne7szE8PNUovohz8v53sDkpBiP+YQ/2zela29gnVTNmAnc8g2bjtCoyomP0mvDkm0WKQlI6GDotLqOZIC7y0vffxi3xg9EjKcizRyuRPGIHV0/l5uQ==","mail-alias-created-date":"1752046281608","Content-Type":"text/plain;\n\tcharset=us-ascii","Mime-Version":"1.0 (Mac OS X Mail 16.0 \\(3864.500.181\\))","Subject":"Re: [PATCH] target/arm/hvf: Fix WFI halting to stop idle vCPU\n spinning","From":"Mohamed Mediouni <mohamed@unpredictable.fr>","In-Reply-To":"<20260410044726.61853-1-scottjgo@gmail.com>","Date":"Fri, 10 Apr 2026 07:06:18 +0200","Cc":"qemu-devel@nongnu.org, qemu-arm@nongnu.org, rbolshakov@ddn.com,\n phil@philjordan.eu, agraf@csgraf.de, peter.maydell@linaro.org","Content-Transfer-Encoding":"quoted-printable","Message-Id":"<E41FA391-ABC2-4CA9-850C-BDB3263C1E07@unpredictable.fr>","References":"<20260410044726.61853-1-scottjgo@gmail.com>","To":"\"Scott J. Goldman\" <scottjgo@gmail.com>","X-Mailer":"Apple Mail (2.3864.500.181)","X-Authority-Info-Out":"v=2.4 cv=Ze0Q98VA c=1 sm=1 tr=0 ts=69d8855a\n cx=c_apl:c_pps:t_out a=9OgfyREA4BUYbbCgc0Y0oA==:117\n a=9OgfyREA4BUYbbCgc0Y0oA==:17 a=kj9zAlcOel0A:10 a=A5OVakUREuEA:10\n a=VkNPw1HP01LnGYTKEx00:22 a=p0WdMEafAAAA:8 a=pGLkceISAAAA:8\n a=wG9qhmRfGSGoVh_Ie2YA:9 a=CjuIK1q_8ugA:10 a=O8hF6Hzn-FEA:10","X-Proofpoint-GUID":"bNDyCNcHHrbGMgKGnHAz24R8jL1U25C9","X-Proofpoint-ORIG-GUID":"bNDyCNcHHrbGMgKGnHAz24R8jL1U25C9","X-Proofpoint-Spam-Details-Enc":"AW1haW4tMjYwNDEwMDA0NCBTYWx0ZWRfX+rqT4V4g3iYu\n +SSX/E5ni58RvtiFXkGf4T6cpEMNLBGLAeS0M2VdDBgL01Hc03RLpPByowr9R4zU/MW/uQiK9q2\n ki5IhMczd1lDTkZIg71K/wQnrChqiYbNh5uBsDXfouFUqXnseWx/eG66YxNAlpk67Cvvyp1N17c\n +M9nIXeImU7EChUOP/K0rY5tp5C9Uq7X7EMPn+1Tdy2XAYvRfjC5RTNot1IGzGcNyhhVf9chjjv\n ukVScojm0bnU/ctCO7AE3wMfnVqD6cQ4dXVcWLEkqQvKK/D3QfQT2saLIFZEwOcPTJ2QOa+J5Pa\n KW1IMBC34F6w07bFdwXnqBcHlMb3H0B6NUEjoEgTMxk5Cwysn+ioB/0u6jquL8=","X-Proofpoint-Virus-Version":"vendor=baseguard\n engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49\n definitions=2026-04-10_01,2026-04-09_02,2025-10-01_01","X-Proofpoint-Spam-Details":"rule=notspam policy=default score=0 malwarescore=0\n bulkscore=0 suspectscore=0 spamscore=0\n lowpriorityscore=0 phishscore=0\n clxscore=1030 adultscore=0 mlxlogscore=999 mlxscore=0 classifier=spam\n authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000\n definitions=main-2604100044","Received-SPF":"pass client-ip=57.103.70.13;\n envelope-from=mohamed@unpredictable.fr; helo=outbound.mr.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,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001,\n RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001,\n 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 <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}},{"id":3675664,"web_url":"http://patchwork.ozlabs.org/comment/3675664/","msgid":"<DHP86IRPEW4G.16N17DFVR56EW@gmail.com>","list_archive_url":null,"date":"2026-04-10T05:28:42","subject":"Re: [PATCH] target/arm/hvf: Fix WFI halting to stop idle vCPU\n spinning","submitter":{"id":93060,"url":"http://patchwork.ozlabs.org/api/people/93060/","name":"Scott J. Goldman","email":"scottjgo@gmail.com"},"content":"On Thu Apr 9, 2026 at 10:06 PM PDT, Mohamed Mediouni wrote:\n>\n>\n>> On 10. Apr 2026, at 06:47, Scott J. Goldman <scottjgo@gmail.com> wrote:\n>> \n>> Commit b5f8f77271 (\"accel/hvf: Implement WFI without using pselect()\")\n>> changed hvf_wfi() from blocking the vCPU thread with pselect() to\n>> returning EXCP_HLT, intending QEMU's main event loop to handle the\n>> idle wait. However, cpu->halted was never set, so cpu_thread_is_idle()\n>> always returns false and the vCPU thread spins at 100% CPU per core\n>> while the guest is idle.\n>> \n>> Fix this by:\n>> \n>> 1. Setting cpu->halted = 1 in hvf_wfi() so the vCPU thread sleeps on\n>>   halt_cond in qemu_process_cpu_events().\n>> \n>> 2. Arming a host-side QEMU_CLOCK_HOST timer to fire when the guest's\n>>   virtual timer (CNTV_CVAL_EL0) would expire. This is necessary\n>>   because HVF only delivers HV_EXIT_REASON_VTIMER_ACTIVATED during\n>>   hv_vcpu_run(), which is not called while the CPU is halted. The\n>>   timer callback mirrors the VTIMER_ACTIVATED handler: it raises the\n>>   vtimer IRQ through the GIC and marks vtimer_masked, causing the\n>>   interrupt delivery chain to wake the vCPU via qemu_cpu_kick().\n>> \n>> 3. Clearing cpu->halted in hvf_arch_vcpu_exec() when cpu_has_work()\n>>   indicates a pending interrupt, and cancelling the WFI timer.\n>> \n>> Fixes: b5f8f77271 (\"accel/hvf: Implement WFI without using pselect()\")\n>> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/959\n>> Signed-off-by: Scott J. Goldman <scottjgo@gmail.com>\n>\n> Hi,\n>\n> Reviewed-by: Mohamed Mediouni <mohamed@unpredictable.fr>\n>> ---\n>> include/system/hvf_int.h |  1 +\n>> target/arm/hvf/hvf.c     | 65 +++++++++++++++++++++++++++++++++++++++-\n>> 2 files changed, 65 insertions(+), 1 deletion(-)\n>> \n>> diff --git a/include/system/hvf_int.h b/include/system/hvf_int.h\n>> index 2621164cb2..58fb865eba 100644\n>> --- a/include/system/hvf_int.h\n>> +++ b/include/system/hvf_int.h\n>> @@ -48,6 +48,7 @@ struct AccelCPUState {\n>>     hv_vcpu_exit_t *exit;\n>>     bool vtimer_masked;\n>>     bool guest_debug_enabled;\n>> +    struct QEMUTimer *wfi_timer;\n>> #endif\n>> };\n>> \n>> diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c\n>> index 5fc8f6bbbd..0e737d3583 100644\n>> --- a/target/arm/hvf/hvf.c\n>> +++ b/target/arm/hvf/hvf.c\n>> @@ -28,6 +28,7 @@\n>> #include \"hw/core/boards.h\"\n>> #include \"hw/core/irq.h\"\n>> #include \"qemu/main-loop.h\"\n>> +#include \"qemu/timer.h\"\n>> #include \"system/cpus.h\"\n>> #include \"arm-powerctl.h\"\n>> #include \"target/arm/cpu.h\"\n>> @@ -301,6 +302,8 @@ void hvf_arm_init_debug(void)\n>> #define TMR_CTL_IMASK   (1 << 1)\n>> #define TMR_CTL_ISTATUS (1 << 2)\n>> \n>> +static void hvf_wfi_timer_cb(void *opaque);\n>> +\n>> static uint32_t chosen_ipa_bit_size;\n>> \n>> typedef struct HVFVTimer {\n>> @@ -1214,6 +1217,9 @@ void hvf_arch_vcpu_destroy(CPUState *cpu)\n>> {\n>>     hv_return_t ret;\n>> \n>> +    timer_free(cpu->accel->wfi_timer);\n>> +    cpu->accel->wfi_timer = NULL;\n>> +\n>>     ret = hv_vcpu_destroy(cpu->accel->fd);\n>>     assert_hvf_ok(ret);\n>> }\n>> @@ -1352,6 +1358,9 @@ int hvf_arch_init_vcpu(CPUState *cpu)\n>>                               arm_cpu->isar.idregs[ID_AA64MMFR0_EL1_IDX]);\n>>     assert_hvf_ok(ret);\n>> \n>> +    cpu->accel->wfi_timer = timer_new_ns(QEMU_CLOCK_HOST,\n>> +                                          hvf_wfi_timer_cb, cpu);\n>> +\n>>     aarch64_add_sme_properties(OBJECT(cpu));\n>>     return 0;\n>> }\n>> @@ -2027,8 +2036,29 @@ static uint64_t hvf_vtimer_val_raw(void)\n>>     return mach_absolute_time() - hvf_state->vtimer_offset;\n>> }\n>> \n>> +static void hvf_wfi_timer_cb(void *opaque)\n>> +{\n>> +    CPUState *cpu = opaque;\n>> +    ARMCPU *arm_cpu = ARM_CPU(cpu);\n>> +\n>> +    /*\n>> +     * vtimer expired while the CPU was halted for WFI.\n>> +     * \t\n>> +     * interrupt and mark as masked so hvf_sync_vtimer() will\n>> +     * check and unmask when the guest handles it.\n>> +     *\n>> +     * The interrupt delivery chain (GIC -> cpu_interrupt ->\n>> +     * qemu_cpu_kick) wakes the vCPU thread from halt_cond.\n>> +     */\n>> +    qemu_set_irq(arm_cpu->gt_timer_outputs[GTIMER_VIRT], 1);\n>> +    cpu->accel->vtimer_masked = true;\n>> +}\n>> +\n>> static int hvf_wfi(CPUState *cpu)\n>> {\n>> +    uint64_t ctl, cval;\n>> +    hv_return_t r;\n>> +\n>>     if (cpu_has_work(cpu)) {\n>>         /*\n>>          * Don't bother to go into our \"low power state\" if\n>> @@ -2037,6 +2067,35 @@ static int hvf_wfi(CPUState *cpu)\n>>         return 0;\n>>     }\n>> \n>> +    /*\n>> +     * Set up a host-side timer to wake us when the vtimer expires.\n>> +     * HVF only delivers HV_EXIT_REASON_VTIMER_ACTIVATED during\n>> +     * hv_vcpu_run(), which we won't call while halted.\n>> +     */\n>> +    r = hv_vcpu_get_sys_reg(cpu->accel->fd, HV_SYS_REG_CNTV_CTL_EL0, &ctl);\n>> +    assert_hvf_ok(r);\n>> +\n>> +    if ((ctl & TMR_CTL_ENABLE) && !(ctl & TMR_CTL_IMASK)) {\n>> +        r = hv_vcpu_get_sys_reg(cpu->accel->fd,\n>> +                                HV_SYS_REG_CNTV_CVAL_EL0, &cval);\n>> +        assert_hvf_ok(r);\n>> +\n>> +        uint64_t now = hvf_vtimer_val_raw();\n>> +        if (cval <= now) {\n>> +            /* Timer already expired, don't halt */\n>> +            return 0;\n>> +        }\n>> +\n>> +        uint64_t delta_ticks = cval - now;\n>\n> We have CNTFRQ_EL0 in cpu->gt_cntfrq_hz but this works too\n> as XNU today tries to take care that host and VM run with\n> the same CNTFRQ_EL0\n>\n\nAh, I can post a follow-up.\n\n>> +        mach_timebase_info_data_t timebase;\n>> +        mach_timebase_info(&timebase);\n>> +        int64_t delta_ns = delta_ticks * timebase.numer / timebase.denom;\n>> +        int64_t deadline = qemu_clock_get_ns(QEMU_CLOCK_HOST) + delta_ns;\n>> +\n>> +        timer_mod(cpu->accel->wfi_timer, deadline);\n>> +    }\n>> +\n>> +    cpu->halted = 1;\n>>     return EXCP_HLT;\n>> }\n>> \n>> @@ -2332,7 +2391,11 @@ int hvf_arch_vcpu_exec(CPUState *cpu)\n>>     hv_return_t r;\n>> \n>>     if (cpu->halted) {\n>> -        return EXCP_HLT;\n>> +        if (!cpu_has_work(cpu)) {\n>> +            return EXCP_HLT;\n>> +        }\n>> +        cpu->halted = 0;\n>> +        timer_del(cpu->accel->wfi_timer);\n>>     }\n>> \n>>     flush_cpu_state(cpu);\n>> -- \n>> 2.50.1 (Apple Git-155)\n>> \n>> \n\nThanks,\n-sjg","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256\n header.s=20251104 header.b=s0gY8Xdd;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fsQMp5hF8z1y2d\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 10 Apr 2026 15:29:10 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wB4QE-0007x5-5s; Fri, 10 Apr 2026 01:28:50 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <scottjgo@gmail.com>)\n id 1wB4QB-0007ws-Vr\n for qemu-devel@nongnu.org; Fri, 10 Apr 2026 01:28:48 -0400","from mail-dy1-x132b.google.com ([2607:f8b0:4864:20::132b])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <scottjgo@gmail.com>)\n id 1wB4QA-0007F4-4H\n for qemu-devel@nongnu.org; Fri, 10 Apr 2026 01:28:47 -0400","by mail-dy1-x132b.google.com with SMTP id\n 5a478bee46e88-2b6b0500e06so2478810eec.1\n for <qemu-devel@nongnu.org>; Thu, 09 Apr 2026 22:28:45 -0700 (PDT)","from localhost ([2601:645:8200:47:ade2:73a1:54c5:e0fe])\n by smtp.gmail.com with ESMTPSA id\n 5a478bee46e88-2d55f5c69casm3396964eec.2.2026.04.09.22.28.43\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Thu, 09 Apr 2026 22:28:43 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=gmail.com; s=20251104; t=1775798924; x=1776403724; darn=nongnu.org;\n h=in-reply-to:references:to:from:subject:cc:message-id:date\n :content-transfer-encoding:mime-version:from:to:cc:subject:date\n :message-id:reply-to;\n bh=//BvhHT67uK2nLwJxFUrnIe05Ect6gqGWpNnBixz1iE=;\n b=s0gY8Xdd1r1i/xzPkrrs0TwUcnq3BdYAIRLz0R1APG1EiyyS5GD2fFvf54VuCc3rL8\n humVLojKkaQm5pd9QkW8GhgtH/xSbHyuQPX/pUFqekT1TbBOEetrovoFLzapbACL7XlP\n eYx8TjSO2jWbVvhkvljr3bCMtbYcbcb257/hUJKe7RmSqpF/Tlfvw0D02h2p80f9BBW4\n yc5eYGLULcRUjG5IBJuQ+oI8FrpucvBbiu2OnjI1vFhL1NVGHFe5xiuXByld/ktEfBaF\n IJNn53/k+Yqyq1vQRO0moPmr6T7J6ggdN5EyIfd1YkpJ68P6DYqkHCuYFoJPu+kf5mCR\n E9Iw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1775798924; x=1776403724;\n h=in-reply-to:references:to:from:subject:cc:message-id:date\n :content-transfer-encoding:mime-version:x-gm-gg:x-gm-message-state\n :from:to:cc:subject:date:message-id:reply-to;\n bh=//BvhHT67uK2nLwJxFUrnIe05Ect6gqGWpNnBixz1iE=;\n b=Uf0DsEsJDzUDE/7HQ1LWHiSPjEAT8Z3AwnZRKP/oCL1lv4i+hhCecMpCyFjTkPalnS\n o5lP1G/mUdoGw9rfYR4vD+2bPbgUI/NreXzC274gNz85VWeJDpy7sVW32/rql+aAsTwX\n T9GcKPpM60bsGMSNLiv21HEOkD3djXAjspIty4VsBjLAfmiCB8n3Mb2+1QfZFtOQdDUL\n RPPl6R89zmEQYVB29qurH0LLsb3XPLS7oenpGQl4yrS18jPTAGEMpeq4AVLr8mzehUD/\n DdKiiNo2bQNdoJI3wPSp4KMRxBSx+S9RQ90v9hlvoRcJsTdOJcd8uZGp4qETRWeoaY0b\n iVWw==","X-Gm-Message-State":"AOJu0Yx1kOWfBMFr6h6+8kWliJzbxW9NRq8eK9BSYy4dohjsrUaMk8mr\n szj+AY4MwY285Z8Ow/sApHheuaiFXNY42mZ8my1OYtddn1UkOzLzJd5I","X-Gm-Gg":"AeBDievjLUQ5Rx6iLkkTCrw26q7tFG3tzaTEMidsv4/YUdFQrZlRbAHjHzY0JyYaQbx\n l25o8w5eX5YdUisvffk7xmp5vCGEnd4R9LquAK1Al3EAxNE8RVixqLMZD6UZsax6XqDarPBsu4A\n CzfEsMJolKyWxzHhVUcK2u8LqUebm/9ddzGGOOpTBXz7ZjpfL3932c7wiAwPf7o+NmX5jmh7Fct\n EXlQ2NhDd92E+4i5hSyPF626TCbpGYnEqBfHBuRtgpCzK/ZVGGlwiGhF282+Vb18uQw549NJiOK\n Ug8w0aCD4GVNwv3NgRONPi8NcNCTDEHQjZth+7sMsJO4RlmGeaQtpqMLu5Vmes8V5LUOx3i80rL\n Zi8gjN5pdIp2RmENaMTtO2G5/WlsJtIio/o/WdXCPBFdBLk28p9dmCTVMpkerp62DnNXrylIHlO\n n78cVbhM6+L0iYMQQUIF3iq5csUAW0Plhz+8BOfikH5oU1XuOIyGjUxdfN7kYD8RMwNc5ObDH7A\n 5M6Dkcsif4=","X-Received":"by 2002:a05:7301:608e:b0:2c1:6cfd:73ee with SMTP id\n 5a478bee46e88-2d589898d4cmr957712eec.24.1775798924080;\n Thu, 09 Apr 2026 22:28:44 -0700 (PDT)","Mime-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","Content-Type":"text/plain; charset=UTF-8","Date":"Thu, 09 Apr 2026 22:28:42 -0700","Message-Id":"<DHP86IRPEW4G.16N17DFVR56EW@gmail.com>","Cc":"<qemu-devel@nongnu.org>, <qemu-arm@nongnu.org>, <rbolshakov@ddn.com>,\n <phil@philjordan.eu>, <agraf@csgraf.de>, <peter.maydell@linaro.org>","Subject":"Re: [PATCH] target/arm/hvf: Fix WFI halting to stop idle vCPU\n spinning","From":"\"Scott J. Goldman\" <scottjgo@gmail.com>","To":"\"Mohamed Mediouni\" <mohamed@unpredictable.fr>, \"Scott J. Goldman\"\n <scottjgo@gmail.com>","X-Mailer":"aerc 0.21.0","References":"<20260410044726.61853-1-scottjgo@gmail.com>\n <E41FA391-ABC2-4CA9-850C-BDB3263C1E07@unpredictable.fr>","In-Reply-To":"<E41FA391-ABC2-4CA9-850C-BDB3263C1E07@unpredictable.fr>","Received-SPF":"pass client-ip=2607:f8b0:4864:20::132b;\n envelope-from=scottjgo@gmail.com; helo=mail-dy1-x132b.google.com","X-Spam_score_int":"-20","X-Spam_score":"-2.1","X-Spam_bar":"--","X-Spam_report":"(-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001,\n RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,\n SPF_PASS=-0.001 autolearn=unavailable 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 <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}}]