From nobody Mon Oct 27 01:13:19 2025 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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1759462309; cv=none; d=zohomail.com; s=zohoarc; b=NNVvQOvLPks8Yg0c7n9xGvRPgpkbBe6rjFheypIZRYp7enrY3+oK27z1UXA1DrbH5MC+R+apt73MOxU1dhsUuqNeYXfWE4O3YHC4TfZgE/3VfK23Stx8EXLmgygwraWEeTlJwSRYsxck5a9tggdpvMsrOqmqmIK8Cz+Wl+pFhh0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1759462309; 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=y9e72oUIBV4vzVLXmEifMrDsgj+nHpDSwe09C+KGtro=; b=bp99V0neam9vxfkrdZ2K4idcEaN7xSEceyjgSiM+BUzgHcX8KilQ8bgg4RuJYTqABIVtkf4SK/TGKSomk9dZIUTOsk5ErD/XKy94mdlVxpguaCqbxBaX/ZYZrO4NtPTbdkaETD+5W92Dci+JkEhCoedGR6qoBM1viVSuy/DMZ9w= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1759462309714975.9113126357496; Thu, 2 Oct 2025 20:31:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v4WTC-00077S-1X; Thu, 02 Oct 2025 23:28:34 -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 1v4WT4-000733-El for qemu-devel@nongnu.org; Thu, 02 Oct 2025 23:28:27 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1v4WSt-0001Da-UP for qemu-devel@nongnu.org; Thu, 02 Oct 2025 23:28:25 -0400 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-77f5d497692so2484633b3a.1 for ; Thu, 02 Oct 2025 20:28:12 -0700 (PDT) Received: from toolbx.alistair23.me (2403-580b-97e8-0-82ce-f179-8a79-69f4.ip6.aussiebb.net. [2403:580b:97e8:0:82ce:f179:8a79:69f4]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b62ce55205csm844202a12.18.2025.10.02.20.28.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Oct 2025 20:28:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759462091; x=1760066891; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=y9e72oUIBV4vzVLXmEifMrDsgj+nHpDSwe09C+KGtro=; b=IxY/2aAcMBIduCK9POWooZOI3isaxbiY25LPDzRDwAjVjPqw2/Jpj2rK8FMPwDBNdZ q47X+hMlz0feRiKIj6L5poFleH3uayT9dd/ZyiqJksLMpEHifjxlFFem7zdCQUsXc//n G73V/8+NCcZVTMSrQIfLTFyztNq4eBPHFI0YArp0BhVf9md/KOfT48uYPFkzJ3POyWIj blNxxZifq/KeZL3TadqNgeEBvD9wFDBMW8YDyLej+3Is6QnUrPpESLnbk/aO1qNWjsCN ZJfnVYXt5vANEbTyDZ5h48UxAQwiZV0wHdldyAhCPz47enfIZ2zQaIErMrph/9xGFbq6 n4rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759462091; x=1760066891; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=y9e72oUIBV4vzVLXmEifMrDsgj+nHpDSwe09C+KGtro=; b=SZytnSFEGZYb/JakxYR4680HIE5BefVLM+0ONk4EN/eG80QOCsfdmYh2K4AiLDZ42l 0+7m98OEQS2ZbuyCOrwB3ZR4Cz4ZBihpZ4rkJz3IpHqA8NgZlIcAWzLC8dR/ld3JsG2r eaZtL1DWI4wLdvUzzNfmdbeljNmzzMf9+ej/Ra38ykdkgzj+kuHzZZLpHCNNkyXs/45x T5U2CMacpGAESSJp220x16I5UB57CWKlUIaHnKe2cx9ID5t7gCRR7EziAOFW2bxjpxm3 U+F5eGjrwmpj97dexIkTOc/r8vQkGo/Y7eat7n9617uwHhWC5O+79QSqHbyVoZGaC1gC Jtmw== X-Gm-Message-State: AOJu0YxqWLFD6iDkYe8yJ4KwFB8yij2ZewPAVid5VnIZDLlBCY8sQCc+ L7Rt3Kk9etP5DASl8udhj+1Gl5xA0IAF6sZTdkiTjpcDxMu6nUhgdUQ0dNHXLg== X-Gm-Gg: ASbGncvg2mQJs+7arTEJpwmUitywO4Gw3usJOOSt7VcNj0Z87gL2/uNkqwZPxhP/3ki jTUD8zus96btkaQ+4bkjDp3r5ux6yET74piSrHZ1BRfze2S1/TTb2meVf+ZvgbQfGl/ZcZLa65U sHY94o0VEIRRLcvqw+XDYUojeL3yBLnZk61lC0D+Zf32JwZFLK8UkzQVMUEOX+hEtLsPPD14vJE gPXzKuvY+erSvDeJNuy3S8wzacBjk046PRkmbTucefqSuh3RvmEXbXjIAQokLzx+8r3Id1Ye7qF YPCmKfhHCdZQ1Lx4Mv8jP4zWENsHNYAxl8U2ERNg3GG3yp/bcsibNKYhTSAIsh2t0LhjoKI4aGP 2j0aaDdxpbVNRzSOCoiiRWkTMzVe4DzrcRwO+RC4+LX1Xr3v4G+BqmVygjWBurzAKuHDKjdCqGy TExZAbrGv/+3mhMy+nsF6l93fNNuOS6sVYg5p5xX8EJq/AuRaYZwOy X-Google-Smtp-Source: AGHT+IG3pnF1r84L4DG663uJSXqt9ylMDyCs8ERyZaJxPUiAd3kodRHObQ9+dccFVlY0Ey373IrDug== X-Received: by 2002:a05:6a20:3d95:b0:2e5:c9ee:96ee with SMTP id adf61e73a8af0-32b6208e887mr2003497637.37.1759462090648; Thu, 02 Oct 2025 20:28:10 -0700 (PDT) From: alistair23@gmail.com X-Google-Original-From: alistair.francis@wdc.com To: qemu-devel@nongnu.org Cc: alistair23@gmail.com, TANG Tiancheng , LIU Zhiwei , Daniel Henrique Barboza , Alistair Francis Subject: [PULL 10/26] hw/intc: Save time_delta in RISC-V mtimer VMState Date: Fri, 3 Oct 2025 13:27:02 +1000 Message-ID: <20251003032718.1324734-11-alistair.francis@wdc.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251003032718.1324734-1-alistair.francis@wdc.com> References: <20251003032718.1324734-1-alistair.francis@wdc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::42e; envelope-from=alistair23@gmail.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 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: pass (identity @gmail.com) X-ZM-MESSAGEID: 1759462310870116600 Content-Type: text/plain; charset="utf-8" From: TANG Tiancheng In QEMU's RISC-V ACLINT timer model, 'mtime' is not stored directly as a state variable. It is computed on demand as: mtime =3D rtc_r + time_delta where: - 'rtc_r' is the current VM virtual time (in ticks) obtained via cpu_riscv_read_rtc_raw() from QEMU_CLOCK_VIRTUAL. - 'time_delta' is an offset applied when the guest writes a new 'mtime' value via riscv_aclint_mtimer_write(): time_delta =3D value - rtc_r Under this design, 'rtc_r' is assumed to be monotonically increasing during VM execution. Even if the guest writes an 'mtime' value smaller than the current one (making 'time_delta' negative in signed arithmetic, or underflow in unsigned arithmetic), the computed 'mtime' remains correct because 'rtc_r_new > rtc_r_old': mtime_new =3D rtc_r_new + (value - rtc_r_old) However, this monotonicity assumption breaks on snapshot load. Before restoring a snapshot, QEMU resets the guest, which calls riscv_aclint_mtimer_reset_enter() to set 'mtime' to 0 and recompute 'time_delta' as: time_delta =3D 0 - rtc_r_reset Here, the time_delta differs from the value that was present when the snapshot was saved. As a result, subsequent reads produce a fixed offset from the true mtime. This can be observed with the 'date' command inside the guest: after loading a snapshot, the reported time appears "frozen" at the save point, and only resumes correctly after the guest has run long enough to compensate for the erroneous offset. The fix is to treat 'time_delta' as part of the device's migratable state and save/restore it via vmstate. This preserves the correct relation between 'rtc_r' and 'mtime' across snapshot save/load, ensuring 'mtime' continues incrementing from the precise saved value after restore. Reviewed-by: LIU Zhiwei Reviewed-by: Daniel Henrique Barboza Signed-off-by: TANG Tiancheng Reviewed-by: Alistair Francis Message-ID: <20250911-timers-v3-1-60508f640050@linux.alibaba.com> Signed-off-by: Alistair Francis --- hw/intc/riscv_aclint.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hw/intc/riscv_aclint.c b/hw/intc/riscv_aclint.c index 4623cfa029..318a9c8248 100644 --- a/hw/intc/riscv_aclint.c +++ b/hw/intc/riscv_aclint.c @@ -323,9 +323,10 @@ static void riscv_aclint_mtimer_reset_enter(Object *ob= j, ResetType type) =20 static const VMStateDescription vmstate_riscv_mtimer =3D { .name =3D "riscv_mtimer", - .version_id =3D 1, - .minimum_version_id =3D 1, + .version_id =3D 2, + .minimum_version_id =3D 2, .fields =3D (const VMStateField[]) { + VMSTATE_UINT64(time_delta, RISCVAclintMTimerState), VMSTATE_VARRAY_UINT32(timecmp, RISCVAclintMTimerState, num_harts, 0, vmstate_info_uint64, uint64_t), --=20 2.51.0