From nobody Sun Sep 28 17:06:23 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=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1757516831; cv=none; d=zohomail.com; s=zohoarc; b=ijpSifh0vWww4ejYrosXLO9cZgth1o+E6wn4DUepZbN+9wwg4B78Ju79240RFgiQ1vOWS/2eGkBtEnPqGXeB6xKdSHelDM9UHn3rRLe8CrvOhuKbMJjgby7NHyhNjijhYjhA9ith3IkTLkq+vRLiSbsvzeKeUt7oLnuyRQVQ4qI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757516831; h=Content-Type: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=EfbDjTEkKmQSHkupOEfM7fT9Z4bCEggf+NrkPcGjW4U=; b=DuS0gna6y4zV4Q4LnUcI+z0xVDEPR59GzWK96M/xsaEd/C7nQcNUOmeKFMZJ3ZoXuMVzeTAuI/v9NhTfm00Qby2F3xl+GD7+F5ZWC29xe/3yH9GTE2ww6Ggl0C6kEW6S5/EEiJwvrqFe6Rl56TxLnOtaaeXC4CTJ1NMN212KVo8= 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 1757516831362959.5130515853801; Wed, 10 Sep 2025 08:07:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uwMOP-0005as-QY; Wed, 10 Sep 2025 11:05:53 -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 1uwMOM-0005XD-HT; Wed, 10 Sep 2025 11:05:50 -0400 Received: from [115.124.30.124] (helo=out30-124.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uwMOF-0003f2-8R; Wed, 10 Sep 2025 11:05:50 -0400 Received: from ea134-sw06.eng.xrvm.cn(mailfrom:lyndra@linux.alibaba.com fp:SMTPD_---0WniJAyf_1757516720 cluster:ay36) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 23:05:20 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1757516722; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To; bh=EfbDjTEkKmQSHkupOEfM7fT9Z4bCEggf+NrkPcGjW4U=; b=g6O5v7AvDwvvuomWPpIhONv0xhfX5osZyFxG+E4xDX4gKRq5QxCQl/2B1SDbePiOG8Aoo34BH/7Q8ZMGMG09YsY+d1ZPsJwjA2jXGaPIFoASTJcITuTpCRVesCpnrQYgGCqVzYUuYggMKX8zPiabx3BdPA/n2Pp1TwjAx2q6RDg= From: TANG Tiancheng Date: Wed, 10 Sep 2025 23:04:25 +0800 Subject: [PATCH v2 1/4] hw/intc: Save time_delta in RISC-V mtimer VMState MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250910-timers-v2-1-31359f1f6ee8@linux.alibaba.com> References: <20250910-timers-v2-0-31359f1f6ee8@linux.alibaba.com> In-Reply-To: <20250910-timers-v2-0-31359f1f6ee8@linux.alibaba.com> To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-riscv@nongnu.org, Peter Xu , Fabiano Rosas , TANG Tiancheng X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1757516719; l=2874; i=lyndra@linux.alibaba.com; s=20250909; h=from:subject:message-id; bh=V0J1o0BTyrUgt1B4k2jqg8yY3dbSFHQu27VONEAoIxs=; b=jxgJeMQuj0365JyrIMRQCwJPtuK7H9Z0Y9ItM+ZkpO4pwHJlOefU/LvLsLrntLqN9VapBgH/d llJgPZe+H/CDNPTWbvAGDoNMGI8gcUXHe8JFtUGO4dq81bKMyO05Bxm X-Developer-Key: i=lyndra@linux.alibaba.com; a=ed25519; pk=GQh4uOSLVucXGkaZfEuQ956CrYS14cn1TA3N8AiIjBw= X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.124 (deferred) 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=115.124.30.124; envelope-from=lyndra@linux.alibaba.com; helo=out30-124.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1757516833817116600 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 --- 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 4623cfa029365c6cbdead4bd4a9f0d8b9e88b939..318a9c8248432a8cd4c3f3fa990= 739917ecf7ca1 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.43.0 From nobody Sun Sep 28 17:06:23 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=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1757516801; cv=none; d=zohomail.com; s=zohoarc; b=Ay+okzRRFHs+VrIXtsbUu7qgIZor3QgjP1pX3MoM334Z27ZX84h9YtoNsXOwA5wSq/G5bSNicIp6kFM0+LJ/z9phAkQltEFQhJi/q/8AwRlAr+R3usPcBzimlV813tmkgyXv+ixkqc8RYRnv54KS/PBwPJgdr97cuk6t1wfvANA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757516801; h=Content-Type: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=3eJXQGDyKrjoVEbgU++P1UdqRU23dwNj5mCUqVkkEmY=; b=RgHJTT+++/3Y95QGW9FOzETDZXELk5mq6tnn/x0jnnrYf5pWzrRGfDkTCkYZS/fhlTR0XPKYD0W2nAEvRO9cImrtHWBfvHVXfXkXQLQzl9ARMwNmNr70q1pnJOM1UKkU8g+X5Xh4mEQ8xfP4P61S71tbkn8EuIbCCrl9otY7Lho= 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 1757516801504105.71305011152526; Wed, 10 Sep 2025 08:06:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uwMOV-0005ce-U9; Wed, 10 Sep 2025 11:06:00 -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 1uwMOK-0005VY-EK; Wed, 10 Sep 2025 11:05:48 -0400 Received: from [115.124.30.97] (helo=out30-97.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uwMOF-0003f0-Cj; Wed, 10 Sep 2025 11:05:48 -0400 Received: from ea134-sw06.eng.xrvm.cn(mailfrom:lyndra@linux.alibaba.com fp:SMTPD_---0WniJAyk_1757516720 cluster:ay36) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 23:05:20 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1757516722; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To; bh=3eJXQGDyKrjoVEbgU++P1UdqRU23dwNj5mCUqVkkEmY=; b=sPH/0uyd0UNEqMeOASOWef4YduzCUDq3qGgkw2EXZuVWRVR9voawgEwR+cuSiu+T336jqAPudWcog3r57tMmw0BwV1Ax3kOxEB8Qf1w/dIzzIz/to9u2J4hqWgfferqUhPrzwy36/Tr3buSI+Yt1E7dlCGSC36ki1pFmoxAYxgk= From: TANG Tiancheng Date: Wed, 10 Sep 2025 23:04:26 +0800 Subject: [PATCH v2 2/4] include/migration: Add support for a variable-length array of UINT32 pointers MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250910-timers-v2-2-31359f1f6ee8@linux.alibaba.com> References: <20250910-timers-v2-0-31359f1f6ee8@linux.alibaba.com> In-Reply-To: <20250910-timers-v2-0-31359f1f6ee8@linux.alibaba.com> To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-riscv@nongnu.org, Peter Xu , Fabiano Rosas , TANG Tiancheng X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1757516719; l=1789; i=lyndra@linux.alibaba.com; s=20250909; h=from:subject:message-id; bh=FV73I8V6zn8M9tZwjd1PTSpPGeNGYww/YMBgIop9wb4=; b=7sb0bHoGyV0V/y+TNQIS3Q240+nj+1wyTDCnn7ZWMsp3DTslTOjC8tkWcECOOKAbzX0kEmit1 yfBJpu5/6dfA9tUpOr0esLqrRoeNWiFxPbYEYjLEE/bpRMKU/I1+AhM X-Developer-Key: i=lyndra@linux.alibaba.com; a=ed25519; pk=GQh4uOSLVucXGkaZfEuQ956CrYS14cn1TA3N8AiIjBw= X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.97 (deferred) 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=115.124.30.97; envelope-from=lyndra@linux.alibaba.com; helo=out30-97.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1757516803379116600 Add support for defining a vmstate field which is a variable-length array of pointers, and use this to define a VMSTATE_TIMER_PTR_VARRAY() which allo= ws a variable-length array of QEMUTimer* to be used by devices. Message-id: 20250909-timers-v1-0-7ee18a9d8f4b@linux.alibaba.com Reviewed-by: LIU Zhiwei Signed-off-by: TANG Tiancheng Reviewed-by: Peter Xu --- include/migration/vmstate.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index 1ff7bd9ac425ba67cd5ca7ad97bcf570f9e19abe..1cfddf31b54090f0f6338079409= 2858b4bed8bcf 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -522,6 +522,16 @@ extern const VMStateInfo vmstate_info_qlist; .offset =3D vmstate_offset_array(_s, _f, _type*, _n), \ } =20 +#define VMSTATE_VARRAY_OF_POINTER_UINT32(_field, _state, _field_num, _vers= ion, _info, _type) { \ + .name =3D (stringify(_field)), = \ + .version_id =3D (_version), = \ + .num_offset =3D vmstate_offset_value(_state, _field_num, uint32_t), = \ + .info =3D &(_info), = \ + .size =3D sizeof(_type), = \ + .flags =3D VMS_VARRAY_UINT32 | VMS_ARRAY_OF_POINTER | VMS_POINTER= , \ + .offset =3D vmstate_offset_pointer(_state, _field, _type), = \ +} + #define VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, _num, _version, _= vmsd, _type) { \ .name =3D (stringify(_field)), = \ .version_id =3D (_version), = \ --=20 2.43.0 From nobody Sun Sep 28 17:06:23 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=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1757516813; cv=none; d=zohomail.com; s=zohoarc; b=SPjZDq4gsP9edcQYCX5kzPrNLHqgGlUKPOfZxk+e9IXbmtRfLQ3HY7k4E/y6wGXLZHbtzOCS/faoOtmKucXN8q6nEHuENqBsJfoqZqDVPmSiddWo+7vxfOpb5Uwg6nX70/mDgw0/4zMbsZ4P/6IGrFzLSsgEmuaRXY+UCpPHgVw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757516813; h=Content-Type: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=CgPzvRW3PYzQdn11ycTE+/tC0brKHXf/hxY7dZiqdA0=; b=UeDhcmq5dginr255D3PwFsN13cKkrT8aGV3m1PJvdvkwour13dgeAVQbk1F8tIwa+doQaXftyeQPS7xpq2pYw3TxcFYca20ICYQT7I6B8j4lebYWkQAqrZ6sgdJeJrrtUcben2p/ieI1F3CYKcMRxmAbBZOIbmMhViI3S54YpD8= 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 1757516813447953.0848098730796; Wed, 10 Sep 2025 08:06:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uwMOV-0005cz-TQ; Wed, 10 Sep 2025 11:05:59 -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 1uwMOM-0005Xb-WA; Wed, 10 Sep 2025 11:05:51 -0400 Received: from [115.124.30.100] (helo=out30-100.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uwMOF-0003fC-9r; Wed, 10 Sep 2025 11:05:50 -0400 Received: from ea134-sw06.eng.xrvm.cn(mailfrom:lyndra@linux.alibaba.com fp:SMTPD_---0WniJAyp_1757516721 cluster:ay36) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 23:05:21 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1757516723; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To; bh=CgPzvRW3PYzQdn11ycTE+/tC0brKHXf/hxY7dZiqdA0=; b=Tjw6Am5sewwLcWJEYipGmOi4/JmNy8v06HwgDCMUvSyWp2eHSPtni+uneZylLWB5AQQ/+s+UNTr8ZEdvvd27f0YGBvKkDr9tKqEtSXKDGKr94hlNm3o4iWTjMKWqEnEb/9HgCugPNsBnq37s334QeyDCp85MrL4cLNLvdqFk4aQ= From: TANG Tiancheng Date: Wed, 10 Sep 2025 23:04:27 +0800 Subject: [PATCH v2 3/4] hw/intc: Save timers array in RISC-V mtimer VMState MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250910-timers-v2-3-31359f1f6ee8@linux.alibaba.com> References: <20250910-timers-v2-0-31359f1f6ee8@linux.alibaba.com> In-Reply-To: <20250910-timers-v2-0-31359f1f6ee8@linux.alibaba.com> To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-riscv@nongnu.org, Peter Xu , Fabiano Rosas , TANG Tiancheng X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1757516719; l=2900; i=lyndra@linux.alibaba.com; s=20250909; h=from:subject:message-id; bh=YkJJfhz9vbR/O68Q0eP/tZoK/MlSnuQnZJEPg8ettsM=; b=kc9KThWIHBuncNXfMrv6t+T95XxKcXZ3hkDsMcpDRouD0/h9Z8xSPseofOBsLYmHJRdOzZ+5t cAN3xkQrhCrB/GJvrzCDFIDtZ6CmmzJhBke3TXtkDZtbclNBb4SMHl4 X-Developer-Key: i=lyndra@linux.alibaba.com; a=ed25519; pk=GQh4uOSLVucXGkaZfEuQ956CrYS14cn1TA3N8AiIjBw= X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.100 (deferred) 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=115.124.30.100; envelope-from=lyndra@linux.alibaba.com; helo=out30-100.freemail.mail.aliyun.com X-Spam_score_int: -86 X-Spam_score: -8.7 X-Spam_bar: -------- X-Spam_report: (-8.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1757516815638116600 The current 'timecmp' field in vmstate_riscv_mtimer is insufficient to keep timers functional after migration. If an mtimer's entry in 'mtimer->timers' is active at the time the snapshot is taken, it means riscv_aclint_mtimer_write_timecmp() has written to 'mtimecmp' and scheduled a timer into QEMU's main loop 'timer_list'. During snapshot save, these active timers must also be migrated; otherwise, after snapshot load there is no mechanism to restore 'mtimer->timers' back into the 'timer_list', and any pending timer events would be lost. QEMU's migration framework commonly uses VMSTATE_TIMER_xxx macros to save and restore 'QEMUTimer' variables. However, 'timers' is a pointer array with variable length, and vmstate.h did not previously provide a helper macro for such type. This commit adds a new macro, 'VMSTATE_TIMER_PTR_VARRAY', to handle saving and restoring a variable-length array of 'QEMUTimer *'. We then use this macro to migrate the 'mtimer->timers' array, ensuring that timer events remain scheduled correctly after snapshot load. Reviewed-by: LIU Zhiwei Signed-off-by: TANG Tiancheng --- hw/intc/riscv_aclint.c | 6 ++++-- include/hw/intc/riscv_aclint.h | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/hw/intc/riscv_aclint.c b/hw/intc/riscv_aclint.c index 318a9c8248432a8cd4c3f3fa990739917ecf7ca1..9f4c36e965e2aa379d75c0a9f65= 6177f0dd82a45 100644 --- a/hw/intc/riscv_aclint.c +++ b/hw/intc/riscv_aclint.c @@ -323,13 +323,15 @@ static void riscv_aclint_mtimer_reset_enter(Object *o= bj, ResetType type) =20 static const VMStateDescription vmstate_riscv_mtimer =3D { .name =3D "riscv_mtimer", - .version_id =3D 2, - .minimum_version_id =3D 2, + .version_id =3D 3, + .minimum_version_id =3D 3, .fields =3D (const VMStateField[]) { VMSTATE_UINT64(time_delta, RISCVAclintMTimerState), VMSTATE_VARRAY_UINT32(timecmp, RISCVAclintMTimerState, num_harts, 0, vmstate_info_uint64, uint64_t), + VMSTATE_TIMER_PTR_VARRAY(timers, RISCVAclintMTimerState, + num_harts), VMSTATE_END_OF_LIST() } }; diff --git a/include/hw/intc/riscv_aclint.h b/include/hw/intc/riscv_aclint.h index 693415eb6defe4454e5731a681e025f3bac3ad2e..4b7406eec005a06b7c040d8483a= 8790866a39297 100644 --- a/include/hw/intc/riscv_aclint.h +++ b/include/hw/intc/riscv_aclint.h @@ -80,4 +80,8 @@ enum { RISCV_ACLINT_SWI_SIZE =3D 0x4000 }; =20 +#define VMSTATE_TIMER_PTR_VARRAY(_f, _s, _f_n) \ +VMSTATE_VARRAY_OF_POINTER_UINT32(_f, _s, _f_n, 0, vmstate_info_timer, \ + QEMUTimer *) + #endif --=20 2.43.0 From nobody Sun Sep 28 17:06:23 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=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1757516817; cv=none; d=zohomail.com; s=zohoarc; b=DurW3Qp9PXGhCl3HYzna+3xW1hZzmigHLvOvtvTlRgy1MbgHVLC1BKJ9W6YQSfXvJ5FxPTKPZK6wPrCBIy67VnE7eBSiDcimGZ8tr9tu1XTsE11c6zP5CrVI4F62GmPqqYkRczwxHrXEeRHnwUwWkTrrJNrK4z5JbVwVwLLCg8A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757516817; h=Content-Type: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=1gqd1TzCEIyvFYfjyBctacJ1+0G9407EGdXdIQ5mOdw=; b=T4PMWCPh9SwoAemJCRW3efmODXZn4YslCmrYHLp7u0q9uRk80tuKlts+32rGS/uMuR12D8F3p6xfSHeuINoqu8jcXlXn+gJrvCvoR18frdLxVqKgzqPpGalufsqOrTpxFEG0T9qQIiGX78bJ8CtLDtJD2VxDO8elibm4WFRQrGc= 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 1757516817035791.1699075734857; Wed, 10 Sep 2025 08:06:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uwMOO-0005Zo-Ng; Wed, 10 Sep 2025 11:05:52 -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 1uwMOL-0005Wl-QI; Wed, 10 Sep 2025 11:05:50 -0400 Received: from [115.124.30.97] (helo=out30-97.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uwMOF-0003fG-SP; Wed, 10 Sep 2025 11:05:49 -0400 Received: from ea134-sw06.eng.xrvm.cn(mailfrom:lyndra@linux.alibaba.com fp:SMTPD_---0WniJAyz_1757516721 cluster:ay36) by smtp.aliyun-inc.com; Wed, 10 Sep 2025 23:05:21 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1757516724; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To; bh=1gqd1TzCEIyvFYfjyBctacJ1+0G9407EGdXdIQ5mOdw=; b=mn8vwOGX7MGjS/e/rcvDjaRzyw3GZ95In0qElQq08tF5bsFy7CauO18rtnU/2xSe7ZM7gjdFFnjWJZHnQb6HxmZo7s5mxIkVWZkvE+HwoWY+7Y87SsdRymgjbeJ+EANHKX/au8JdwzI71TKW8UYwiiOMjATPYMVUOFcRilyJzt8= From: TANG Tiancheng Date: Wed, 10 Sep 2025 23:04:28 +0800 Subject: [PATCH v2 4/4] target/riscv: Save stimer and vstimer in CPU vmstate MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250910-timers-v2-4-31359f1f6ee8@linux.alibaba.com> References: <20250910-timers-v2-0-31359f1f6ee8@linux.alibaba.com> In-Reply-To: <20250910-timers-v2-0-31359f1f6ee8@linux.alibaba.com> To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-riscv@nongnu.org, Peter Xu , Fabiano Rosas , TANG Tiancheng X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1757516719; l=1731; i=lyndra@linux.alibaba.com; s=20250909; h=from:subject:message-id; bh=db8HgAaEdZbHQou94hhdJN2K2puP3nx87MpC33YWDYQ=; b=+1hGhz821Vyki9a4hYBmSESip1sNmiTuxeSAuzOgWAuEfSkhzdTSkfC6Qw90ZGS3ztyMb4gsI fl/vn/FjHRSBtZyBSVO+tMdp8s4vAuBurrw4d2jeZMhKb7MSKbeNJJJ X-Developer-Key: i=lyndra@linux.alibaba.com; a=ed25519; pk=GQh4uOSLVucXGkaZfEuQ956CrYS14cn1TA3N8AiIjBw= X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.97 (deferred) 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=115.124.30.97; envelope-from=lyndra@linux.alibaba.com; helo=out30-97.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1757516817541116600 vmstate_riscv_cpu was missing env.stimer and env.vstimer. Without migrating these QEMUTimer fields, active S/VS-mode timer events are lost after snapshot or migration. Add VMSTATE_TIMER_PTR() entries to save and restore them. Reviewed-by: LIU Zhiwei Reviewed-by: Daniel Henrique Barboza Signed-off-by: TANG Tiancheng --- target/riscv/machine.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/target/riscv/machine.c b/target/riscv/machine.c index 1600ec44f0b755fdd49fc0df47c2288c9940afe0..51e0567ed30cbab5e791ea90416= 5bc1854709192 100644 --- a/target/riscv/machine.c +++ b/target/riscv/machine.c @@ -400,6 +400,30 @@ static const VMStateDescription vmstate_ssp =3D { } }; =20 +static bool sstc_timer_needed(void *opaque) +{ + RISCVCPU *cpu =3D opaque; + CPURISCVState *env =3D &cpu->env; + + if (!cpu->cfg.ext_sstc) { + return false; + } + + return env->stimer !=3D NULL || env->vstimer !=3D NULL; +} + +static const VMStateDescription vmstate_sstc =3D { + .name =3D "cpu/timer", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D sstc_timer_needed, + .fields =3D (const VMStateField[]) { + VMSTATE_TIMER_PTR(env.stimer, RISCVCPU), + VMSTATE_TIMER_PTR(env.vstimer, RISCVCPU), + VMSTATE_END_OF_LIST() + } +}; + const VMStateDescription vmstate_riscv_cpu =3D { .name =3D "cpu", .version_id =3D 10, @@ -476,6 +500,7 @@ const VMStateDescription vmstate_riscv_cpu =3D { &vmstate_elp, &vmstate_ssp, &vmstate_ctr, + &vmstate_sstc, NULL } }; --=20 2.43.0