From nobody Sat May 18 20:37:17 2024 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=1658518056; cv=none; d=zohomail.com; s=zohoarc; b=hPiClsChYsAvkZKez0KGYhoXwqBTpoI4hi6nhogt7L1aVh9bARIt2zemKic7j9m4t4+dVLmkoqB6a9evhf3tzzus8bJJXz+WAUl0J22ffGGB1q5bm+cInRSRG6XwDUrJEb8WlF8xZaiyyh9tlqRsHAkJoHkv0Tbdb+Bd/d5QV+g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658518056; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7l6k0l7MFzy3G7hKFTbJ/Zq3iIt+cG4Kjc3pCe8iRZ0=; b=AknF9p8M8rKreJubPPXvyScZlIN3psswRa/h2bDDsQqhhv1rpoEjbnlwK3DmyC++oosk7vOSQxTB9rmdJgAxdN1ko60qlM45FaWHnU4Pjlihr77BQSwU7et2PSDp8CIb8zoAzhCAJ5gSGjcmVIf1Ui3MvdcNFZOOHHGFayYz/jo= 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 1658518056026388.83086569344493; Fri, 22 Jul 2022 12:27:36 -0700 (PDT) Received: from localhost ([::1]:48416 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEyJD-00029T-22 for importer@patchew.org; Fri, 22 Jul 2022 15:27:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43232) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEyDe-0002py-Cr for qemu-devel@nongnu.org; Fri, 22 Jul 2022 15:21:50 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]:37430) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oEyDc-0003LB-GF for qemu-devel@nongnu.org; Fri, 22 Jul 2022 15:21:50 -0400 Received: by mail-pl1-x62f.google.com with SMTP id f11so5346646plr.4 for ; Fri, 22 Jul 2022 12:21:48 -0700 (PDT) Received: from localhost.localdomain ([76.132.29.156]) by smtp.gmail.com with ESMTPSA id h5-20020a17090a2ec500b001f246f2a423sm2006394pjs.17.2022.07.22.12.21.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 12:21:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7l6k0l7MFzy3G7hKFTbJ/Zq3iIt+cG4Kjc3pCe8iRZ0=; b=WT4fAwvYHp4XGty81WL8lRa2zXyC4Wpn1saVtqn387yWiZbQLG+8gcOr4jBXHGeqQL Qtg/pvuSfyIMmMjU922RDNAQZUXFJKI2rDEX/fZHIMf1TKpxYBZh55mGshOCIo/erI0V 8biJSKEftYexVX8Qj474WHgF/DE/pGf7CphahjKHhbdKfpD0rgAqbuEYW+9jh3iwolwF M8Ge0QpgYiOftS2nXa/OPI0wbdOPJFPYlV0+FGudykaw22uVDWcpcP8xz082vCg2syIe zn6YxNk4Bi7aM63w2AbfjIU+HErs2vLHBMGIVT22H12Fl3mnbUhS/yGTLvoMxcOIfE77 C0sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7l6k0l7MFzy3G7hKFTbJ/Zq3iIt+cG4Kjc3pCe8iRZ0=; b=7wobxVT95Vm4FyQVA1k2FFrIrmZVSXjPeVNRJIAW0pSFkRBmpyKg5HFE26/pUObCUS lE7hoidGDjdNvZ4AlapP7ve/6OGRndbdQC0Af2aF1cS/yR9zvGb7fV5PFPfullYmJSq+ HN2PSxaXWkRPezYYQgzmu7X7zTojAg3K58ta04jTCo+cS09cSH20wZfMvLoE4WCK0ojO ggojH8Llhd7/yoJ+24+JE3hAZC6GI2/LoiciC4pM1FRWcoIaoGf46u+6hVJ38GOInbmY cNDG4t7sK7nIY+Uc83s++BCEjiZmiz/7Y4tYh6k36oNe58rVYseCcMTfgOo07gkOIRCp Bn1g== X-Gm-Message-State: AJIora+HjuZk0RjVgE+y9HsSon85BLeENZT+ql4kb5Ryg5fXv11XliDs L/YpxEjXEVZe7o9VNyx4iP++0k5P/4iuMA== X-Google-Smtp-Source: AGRyM1vBq5g556xofebompGVvRMvWejUfKob4QpcRMWqmrrrwIrfxvJhn8dFq0TGN+bH7ijgf2hZhw== X-Received: by 2002:a17:902:bb8a:b0:16d:190d:981d with SMTP id m10-20020a170902bb8a00b0016d190d981dmr989427pls.53.1658517706947; Fri, 22 Jul 2022 12:21:46 -0700 (PDT) From: Richard Liu To: qemu-devel@nongnu.org Cc: alxndr@bu.edu, bsd@redhat.com, darren.kenny@oracle.com, Richard Liu Subject: [RFC 1/3] create skeleton snapshot device and add docs Date: Fri, 22 Jul 2022 12:20:39 -0700 Message-Id: <20220722192041.93006-2-richy.liu.2002@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220722192041.93006-1-richy.liu.2002@gmail.com> References: <20220722192041.93006-1-richy.liu.2002@gmail.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::62f; envelope-from=richy.liu.2002@gmail.com; helo=mail-pl1-x62f.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 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, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-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" X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1658518056338100001 Content-Type: text/plain; charset="utf-8" Added a simple skeleton PCI device for snapshot/restores. Added documentation about the snapshot/restore functionality. Signed-off-by: Richard Liu --- docs/devel/snapshot.rst | 26 +++++++++++++ hw/i386/Kconfig | 1 + hw/misc/Kconfig | 3 ++ hw/misc/meson.build | 1 + hw/misc/snapshot.c | 86 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 docs/devel/snapshot.rst create mode 100644 hw/misc/snapshot.c diff --git a/docs/devel/snapshot.rst b/docs/devel/snapshot.rst new file mode 100644 index 0000000000..a333de69b6 --- /dev/null +++ b/docs/devel/snapshot.rst @@ -0,0 +1,26 @@ +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Snapshot/restore +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The ability to rapidly snapshot and restore guest VM state is a +crucial component of fuzzing applications with QEMU. A special virtual +device can be used by fuzzers to interface with snapshot/restores +commands in QEMU. The virtual device should have the following +commands supported that can be called by the guest: + +- snapshot: save a copy of the guest VM memory, registers, and virtual + device state +- restore: restore the saved copy of guest VM state +- coverage_location: given a location in guest memory, specifying + where the coverage data is to be passed to the fuzzer +- input_location: specify where in the guest memory the fuzzing input + should be stored +- done: indicates whether or not the run succeeded and that the + coverage data has been populated + +The first version of the virtual device will only accept snapshot and +restore commands from the guest. Coverage data will be collected by +code on the guest with source-based coverage tracking. + +Further expansions could include controlling the snapshot/restore from +host and gathering code coverage information directly from TCG. diff --git a/hw/i386/Kconfig b/hw/i386/Kconfig index d22ac4a4b9..55656eddf5 100644 --- a/hw/i386/Kconfig +++ b/hw/i386/Kconfig @@ -46,6 +46,7 @@ config PC select ACPI_VMGENID select VIRTIO_PMEM_SUPPORTED select VIRTIO_MEM_SUPPORTED + select SNAPSHOT =20 config PC_PCI bool diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig index cbabe9f78c..fe84f812f2 100644 --- a/hw/misc/Kconfig +++ b/hw/misc/Kconfig @@ -174,4 +174,7 @@ config VIRT_CTRL config LASI bool =20 +config SNAPSHOT + bool + source macio/Kconfig diff --git a/hw/misc/meson.build b/hw/misc/meson.build index 95268eddc0..ac8fcc5f0b 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -10,6 +10,7 @@ softmmu_ss.add(when: 'CONFIG_UNIMP', if_true: files('unim= p.c')) softmmu_ss.add(when: 'CONFIG_EMPTY_SLOT', if_true: files('empty_slot.c')) softmmu_ss.add(when: 'CONFIG_LED', if_true: files('led.c')) softmmu_ss.add(when: 'CONFIG_PVPANIC_COMMON', if_true: files('pvpanic.c')) +softmmu_ss.add(when: 'CONFIG_SNAPSHOT', if_true: files('snapshot.c')) =20 # ARM devices softmmu_ss.add(when: 'CONFIG_PL310', if_true: files('arm_l2x0.c')) diff --git a/hw/misc/snapshot.c b/hw/misc/snapshot.c new file mode 100644 index 0000000000..2690b331fd --- /dev/null +++ b/hw/misc/snapshot.c @@ -0,0 +1,86 @@ +#include "qemu/osdep.h" +#include "qemu/units.h" +#include "hw/pci/pci.h" +#include "hw/hw.h" +#include "hw/boards.h" +#include "exec/ramblock.h" +#include "qom/object.h" +#include "qemu/module.h" +#include "qapi/visitor.h" +#include "io/channel-buffer.h" +#include "migration/savevm.h" + +#define TYPE_PCI_SNAPSHOT_DEVICE "snapshot" +typedef struct SnapshotState SnapshotState; +DECLARE_INSTANCE_CHECKER(SnapshotState, SNAPSHOT, + TYPE_PCI_SNAPSHOT_DEVICE) + +struct SnapshotState { + PCIDevice pdev; + MemoryRegion mmio; +}; + +static uint64_t snapshot_mmio_read(void *opaque, hwaddr addr, unsigned siz= e) +{ + return 0; +} + +static void snapshot_mmio_write(void *opaque, hwaddr addr, uint64_t val, + unsigned size) +{ +} + +static const MemoryRegionOps snapshot_mmio_ops =3D { + .read =3D snapshot_mmio_read, + .write =3D snapshot_mmio_write, + .endianness =3D DEVICE_NATIVE_ENDIAN, + .valid =3D { + .min_access_size =3D 4, + .max_access_size =3D 8, + }, + .impl =3D { + .min_access_size =3D 4, + .max_access_size =3D 8, + }, + +}; + +static void pci_snapshot_realize(PCIDevice *pdev, Error **errp) +{ + SnapshotState *snapshot =3D SNAPSHOT(pdev); + + memory_region_init_io(&snapshot->mmio, OBJECT(snapshot), &snapshot_mmi= o_ops, snapshot, + "snapshot-mmio", 1 * MiB); + pci_register_bar(pdev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &snapshot->mm= io); +} + +static void snapshot_class_init(ObjectClass *class, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(class); + PCIDeviceClass *k =3D PCI_DEVICE_CLASS(class); + + k->realize =3D pci_snapshot_realize; + k->vendor_id =3D PCI_VENDOR_ID_QEMU; + k->device_id =3D 0xf987; + k->revision =3D 0x10; + k->class_id =3D PCI_CLASS_OTHERS; + set_bit(DEVICE_CATEGORY_MISC, dc->categories); +} + +static void pci_snapshot_register_types(void) +{ + static InterfaceInfo interfaces[] =3D { + { INTERFACE_CONVENTIONAL_PCI_DEVICE }, + { }, + }; + static const TypeInfo snapshot_info =3D { + .name =3D TYPE_PCI_SNAPSHOT_DEVICE, + .parent =3D TYPE_PCI_DEVICE, + .instance_size =3D sizeof(SnapshotState), + .class_init =3D snapshot_class_init, + .interfaces =3D interfaces, + }; + + type_register_static(&snapshot_info); +} +type_init(pci_snapshot_register_types) --=20 2.35.1 From nobody Sat May 18 20:37:17 2024 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=1658517931; cv=none; d=zohomail.com; s=zohoarc; b=A/DM1Qr+EtE2QR7xBi1cNmTFUpUNPRyX5THJPDRPf6/CDhCc8RBivoiXH+uqnGBb4UoHY8QHhw/zwLKkY764dxs1CXf8qBcacWXvJcym3Nxr8kEbBZ4ore7Vl3FqDVjRvvuoc/H3n3rSTImTjUqnBCJR1FjEL4qc6QPtfpY6uxg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658517931; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=gqVMMsXfA7B31E39jHQeupuq+8wMcq0SSeSlJMjkwdw=; b=meD1XlJ3DswH8CwwRAmdJvCCyXuHaHJnw2ft7H/Cb0KvP1BLLptFPnGVn7ySnIFURx4QuRGH4r/n52WjmaguyiALXXufvP73uTglw78FLob8cRc8gfWeEL34917zqFTlG934ogHXpiYwtuI5f1ozlMekrCtguJOiKNsqC+jqUIY= 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 1658517931693555.4598931826456; Fri, 22 Jul 2022 12:25:31 -0700 (PDT) Received: from localhost ([::1]:41998 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEyHB-00067N-Qz for importer@patchew.org; Fri, 22 Jul 2022 15:25:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43252) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEyDf-0002sH-RG for qemu-devel@nongnu.org; Fri, 22 Jul 2022 15:21:51 -0400 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]:37733) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oEyDd-0003LL-IF for qemu-devel@nongnu.org; Fri, 22 Jul 2022 15:21:51 -0400 Received: by mail-pj1-x102a.google.com with SMTP id v16-20020a17090abb9000b001f25244c65dso1599460pjr.2 for ; Fri, 22 Jul 2022 12:21:49 -0700 (PDT) Received: from localhost.localdomain ([76.132.29.156]) by smtp.gmail.com with ESMTPSA id h5-20020a17090a2ec500b001f246f2a423sm2006394pjs.17.2022.07.22.12.21.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 12:21:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gqVMMsXfA7B31E39jHQeupuq+8wMcq0SSeSlJMjkwdw=; b=MYnhdQ6k0KRg6weacgfbSK/gmBFUYuHVTqaZ/v3ZFPtlWTlEKlwQxjIJcF5cK8xIfd RD11dlgzyx1oF5K2cJoK+51PH+tkJBhydr9QQw4h9LXjWsuKoChIS8FVttn9d88o28gn qBPY75EY60m6rBADDwzCDWpUCarjxGQAnWDxWEkBZ92A4hfVPmyQvpOV/RpM196ecJme ltZ3f5D4w3MTXFvkSiFmJtp4O2mlAvOXdnGGZ8VrghoAONkNlrgBrawZVPhcsnnVqMnQ qWFoGVk01k3sL1V5t0y32lCfAecsrI5pw1Dm71Rqk67Jg5JAZE9fHHpsO8TqEUnfhMYP cjmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gqVMMsXfA7B31E39jHQeupuq+8wMcq0SSeSlJMjkwdw=; b=GBZAo547X4tJ1Fz17CbbBqzRQujA5ggeRmTZg7ozL864DdfvdYb6mtEoSvVB8L829s jGROxw5II2bNBPRg1bZ8amivYUwdCzKtrv9XpcaevI6yyp+ZoEfWeKGrN/yswPi+bNp6 ZtnwXx1bQ/N7BlnR67NBuoh5Dh2Xz40noakGUMWtQ8tLTv8j8UBjzF9Bqnc1PV+03GqU aYVO3ElL1E1Z2+4VWL9DwnRZ9b48uHqp5SlQetZcOzuzGR8FouuE1ogIgSnWDtLACKza P1UHnappT896vXGk7B8B4QDgzpnG1d+y1sYBOTq9e7Sr33OsXle/BQjrorKdpgX2c/iq trnA== X-Gm-Message-State: AJIora+plLuFIxmH/zC23l4V4sTWni1m3LpjNT8XFYtCma4NDmBou7/j 0KjzsdLbIsnRKWvKs51blQljilQq/KeAKA== X-Google-Smtp-Source: AGRyM1u9chOlvrerxR6kJFwoTBRqEM6NTsgsx70T9rsGnzdXed10lTgFZXFWXNdnHZW80aAZA+/meQ== X-Received: by 2002:a17:903:2055:b0:16d:2156:7f98 with SMTP id q21-20020a170903205500b0016d21567f98mr1301199pla.24.1658517708012; Fri, 22 Jul 2022 12:21:48 -0700 (PDT) From: Richard Liu To: qemu-devel@nongnu.org Cc: alxndr@bu.edu, bsd@redhat.com, darren.kenny@oracle.com, Richard Liu Subject: [RFC 2/3] implement ram save/restore Date: Fri, 22 Jul 2022 12:20:40 -0700 Message-Id: <20220722192041.93006-3-richy.liu.2002@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220722192041.93006-1-richy.liu.2002@gmail.com> References: <20220722192041.93006-1-richy.liu.2002@gmail.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::102a; envelope-from=richy.liu.2002@gmail.com; helo=mail-pj1-x102a.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 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, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-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" X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1658517932836100001 Content-Type: text/plain; charset="utf-8" Use a file-backed copy-on-write mmap region for snapshots. Restores are handled by remmaping the fixed region. Currently, the snapshot file save path (`filepath`) is hardcoded (to a path that is memory-backed on my machine). Signed-off-by: Richard Liu --- hw/misc/snapshot.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/hw/misc/snapshot.c b/hw/misc/snapshot.c index 2690b331fd..510bf59dce 100644 --- a/hw/misc/snapshot.c +++ b/hw/misc/snapshot.c @@ -18,8 +18,63 @@ DECLARE_INSTANCE_CHECKER(SnapshotState, SNAPSHOT, struct SnapshotState { PCIDevice pdev; MemoryRegion mmio; + + // track saved stated to prevent re-saving + bool is_saved; + + // saved cpu and devices state + QIOChannelBuffer *ioc; }; =20 +// memory save location (for better performance, use tmpfs) +const char *filepath =3D "/Volumes/RAMDisk/snapshot_0"; + +static void save_snapshot(struct SnapshotState *s) { + if (s->is_saved) { + return; + } + s->is_saved =3D true; + + // save memory state to file + int fd =3D -1; + uint8_t *guest_mem =3D current_machine->ram->ram_block->host; + size_t guest_size =3D current_machine->ram->ram_block->max_length; + + fd =3D open(filepath, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600); + ftruncate(fd, guest_size); + + char *map =3D mmap(0, guest_size, PROT_READ | PROT_WRITE, MAP_SHARED, = fd, 0); + memcpy(map, guest_mem, guest_size); + msync(map, guest_size, MS_SYNC); + munmap(map, guest_size); + close(fd); + + // unmap the guest, we will now use a MAP_PRIVATE + munmap(guest_mem, guest_size); + + // map as MAP_PRIVATE to avoid carrying writes back to the saved file + fd =3D open(filepath, O_RDONLY); + mmap(guest_mem, guest_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_= FIXED, fd, 0); +} + +static void restore_snapshot(struct SnapshotState *s) { + int fd =3D -1; + uint8_t *guest_mem =3D current_machine->ram->ram_block->host; + size_t guest_size =3D current_machine->ram->ram_block->max_length; + + if (!s->is_saved) { + fprintf(stderr, "[QEMU] ERROR: attempting to restore but state has= not been saved!\n"); + return; + } + + munmap(guest_mem, guest_size); + + // remap the snapshot at the same location + fd =3D open(filepath, O_RDONLY); + mmap(guest_mem, guest_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_= FIXED, fd, 0); + close(fd); +} + static uint64_t snapshot_mmio_read(void *opaque, hwaddr addr, unsigned siz= e) { return 0; @@ -28,6 +83,21 @@ static uint64_t snapshot_mmio_read(void *opaque, hwaddr = addr, unsigned size) static void snapshot_mmio_write(void *opaque, hwaddr addr, uint64_t val, unsigned size) { + SnapshotState *snapshot =3D opaque; + (void)snapshot; + + switch (addr) { + case 0x00: + switch (val) { + case 0x101: + save_snapshot(snapshot); + break; + case 0x102: + restore_snapshot(snapshot); + break; + } + break; + } } =20 static const MemoryRegionOps snapshot_mmio_ops =3D { @@ -48,6 +118,8 @@ static const MemoryRegionOps snapshot_mmio_ops =3D { static void pci_snapshot_realize(PCIDevice *pdev, Error **errp) { SnapshotState *snapshot =3D SNAPSHOT(pdev); + snapshot->is_saved =3D false; + snapshot->ioc =3D NULL; =20 memory_region_init_io(&snapshot->mmio, OBJECT(snapshot), &snapshot_mmi= o_ops, snapshot, "snapshot-mmio", 1 * MiB); --=20 2.35.1 From nobody Sat May 18 20:37:17 2024 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=1658517929; cv=none; d=zohomail.com; s=zohoarc; b=MvmVUntOzJdZLNCDC9AlAnLeTvjsOD5940cbovltk5gTDxfvQsLubNTlfMMkx6KjXv6EEfvxlo1RSs4dO+h4/a5OTuhUOtenMT7XNP7heaxiKE4DoBzo420ZZ3IvLtwtJb89Pg102YWMZC0ouojckesr35DN1ch9ft4EkDOUz3Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658517929; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=SopgYvM+0FjAMmCKhAHP9BuulNAc3Rfbvq2zx2MdjDk=; b=Adrv6qjyUoGGWGGE3bYQav7EH4Kc3D1a4SRgMWj2PVGUu5Hju5DZBN97D6CkvXpZEZ4aM8XP8h1BYgVB8BF6tVONo7NmMPdbRzLDAVYOsOZ58a3yQVM7aI4R00gd8gQd+O/kzQq2h8ZJPP0tOjHKhwSZb5s7+SMGyssKz74hyCs= 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 1658517929077747.6040130489043; Fri, 22 Jul 2022 12:25:29 -0700 (PDT) Received: from localhost ([::1]:41752 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEyH9-0005w1-HD for importer@patchew.org; Fri, 22 Jul 2022 15:25:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43374) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEyE4-0003Cx-0z for qemu-devel@nongnu.org; Fri, 22 Jul 2022 15:22:16 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]:46943) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oEyDe-0003LQ-KH for qemu-devel@nongnu.org; Fri, 22 Jul 2022 15:22:05 -0400 Received: by mail-pf1-x42e.google.com with SMTP id c3so5211546pfb.13 for ; Fri, 22 Jul 2022 12:21:50 -0700 (PDT) Received: from localhost.localdomain ([76.132.29.156]) by smtp.gmail.com with ESMTPSA id h5-20020a17090a2ec500b001f246f2a423sm2006394pjs.17.2022.07.22.12.21.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 12:21:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SopgYvM+0FjAMmCKhAHP9BuulNAc3Rfbvq2zx2MdjDk=; b=bBIpFmx+H9bwtgbI+0nb8bICbdT0jSrmquwzCymqqDU49cb3rdjKKUebZnaCuPah80 At/ToGme235ukdZucrd3eBTMCW42my1aj0bb1sjQbxy6R/5hc9S/klZGjNEEvBZfdfZh emFBuEKpisSQnQ/ZjJn8KUpTKDYkSJ2TMPRJ8uitrzEDetwpH9yXcgXBnx05JKAYE+C+ R78YlfdC360d6omDZExR+onL+ef1ee0JCAq2JjdOMx6EHJEpMqSLnrWiA24D1Wh/CzHj J8uBI7giCKjFA5vf1WpNzFY0tRzPYOusXruaHf0q+HWQOGbq7xj4zyhpguc6Y9+V6xu7 E1DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SopgYvM+0FjAMmCKhAHP9BuulNAc3Rfbvq2zx2MdjDk=; b=liRpzLyH/jdQt0h+y7p6u82Q5KdtXIOKDG/9oGtNpQ2cLvPkmpAY/qKrJWg/GeR4rA D8BXrAJ+VOL6pHWiWDoMVMWWZaMM33oiH3SbVbr//WWy7zfAopQRRbC0GFP9Yefk7GmJ V+zFxAJ/Cr7zJJk76MpywMFvsUyPRsjc9Ao/N40IeZHkJEb/7rFuElIlTyQ0Ld+NX9j7 1VopDJ10v9F4aQ5BChX6oiHI/qZmPO09c5vg7ARngpSm2aSV4zrz3FsoDvSsxyHeZ78J cBoQntoW7fgt4lqWkyyPtAVar7+sZc14cL3xXPEO4fUt1AbY1EgKI2YtwxPcdLSe4mMU 5y9w== X-Gm-Message-State: AJIora/quST56tXXQeFRuSvYFZOLmuk/k2iq3eG6XtB2IGoMbnB7zDiw kl42oLejUUuWIZUcU49HFw6YFnyUgcvkpA== X-Google-Smtp-Source: AGRyM1tY6oYbWM3YXEyzbvRm2+ftZwSwmDV89aofaofSUdESB+DuXgOoDiaZx2x+0WTun5j9ZSJ4zA== X-Received: by 2002:a63:e547:0:b0:419:8146:1f4f with SMTP id z7-20020a63e547000000b0041981461f4fmr1017325pgj.507.1658517709057; Fri, 22 Jul 2022 12:21:49 -0700 (PDT) From: Richard Liu To: qemu-devel@nongnu.org Cc: alxndr@bu.edu, bsd@redhat.com, darren.kenny@oracle.com, Richard Liu Subject: [RFC 3/3] use migration code for cpu and device save/restore Date: Fri, 22 Jul 2022 12:20:41 -0700 Message-Id: <20220722192041.93006-4-richy.liu.2002@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220722192041.93006-1-richy.liu.2002@gmail.com> References: <20220722192041.93006-1-richy.liu.2002@gmail.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=richy.liu.2002@gmail.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 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, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_HELO_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" X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1658517930770100003 Content-Type: text/plain; charset="utf-8" Reused device migration code for cpu and device state snapshots. In this initial version, I used several hacks to get the device code working. vm_stop doesn't have the intended effect (for qemu_save_device_state) unless called outside the vcpu thread. I trick the function into thinking it is outside the vcpu thread by temporarily setting `current_cpu` to be null. The restore code (qemu_loadvm_state in particular) needs to be called in a bottom half or a coroutine. I am not sure why. Signed-off-by: Richard Liu --- hw/misc/snapshot.c | 6 ++++ migration/savevm.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++ migration/savevm.h | 3 ++ 3 files changed, 93 insertions(+) diff --git a/hw/misc/snapshot.c b/hw/misc/snapshot.c index 510bf59dce..afdc5b7f15 100644 --- a/hw/misc/snapshot.c +++ b/hw/misc/snapshot.c @@ -55,6 +55,9 @@ static void save_snapshot(struct SnapshotState *s) { // map as MAP_PRIVATE to avoid carrying writes back to the saved file fd =3D open(filepath, O_RDONLY); mmap(guest_mem, guest_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_= FIXED, fd, 0); + + // save cpu and device state + s->ioc =3D qemu_snapshot_save_cpu_state(); } =20 static void restore_snapshot(struct SnapshotState *s) { @@ -73,6 +76,9 @@ static void restore_snapshot(struct SnapshotState *s) { fd =3D open(filepath, O_RDONLY); mmap(guest_mem, guest_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_= FIXED, fd, 0); close(fd); + + // restore cpu and device state + qemu_snapshot_load_cpu_state(s->ioc); } =20 static uint64_t snapshot_mmio_read(void *opaque, hwaddr addr, unsigned siz= e) diff --git a/migration/savevm.c b/migration/savevm.c index 48e85c052c..62e5e5b564 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -3309,3 +3309,87 @@ void qmp_snapshot_delete(const char *job_id, =20 job_start(&s->common); } + +// saves the cpu and devices state +QIOChannelBuffer* qemu_snapshot_save_cpu_state(void) +{ + QEMUFile *f; + QIOChannelBuffer *ioc; + MigrationState *ms =3D migrate_get_current(); + int ret; + + /* This is a hack to trick vm_stop() into thinking it is not in vcpu t= hread. + * This is needed to properly stop the VM for a snapshot. + */ + CPUState *cpu =3D current_cpu; + current_cpu =3D NULL; + vm_stop(RUN_STATE_SAVE_VM); + current_cpu =3D cpu; + + global_state_store_running(); + + ioc =3D qio_channel_buffer_new(0x10000); + qio_channel_set_name(QIO_CHANNEL(ioc), "snapshot-buffer"); + f =3D qemu_file_new_output(QIO_CHANNEL(ioc)); + + /* We need to initialize migration otherwise qemu_save_device_state() = will + * complain. + */ + migrate_init(ms); + ms->state =3D MIGRATION_STATUS_NONE; + ms->send_configuration =3D false; + + cpu_synchronize_all_states(); + + ret =3D qemu_save_device_state(f); + if (ret < 0) { + fprintf(stderr, "[QEMU] save device err: %d\n", ret); + } + + // clean up and restart vm + qemu_fflush(f); + g_free(f); + + vm_start(); + + /* Needed so qemu_loadvm_state does not error with: + * qemu-system-x86_64: Expected vmdescription section, but got 0 + */ + ms->state =3D MIGRATION_STATUS_POSTCOPY_ACTIVE; + + return ioc; +} + +// loads the cpu and devices state +static void do_snapshot_load(void* opaque) { + QIOChannelBuffer *ioc =3D opaque; + QEMUFile *f; + int ret; + + vm_stop(RUN_STATE_RESTORE_VM); + + // seek back to beginning of file + qio_channel_io_seek(QIO_CHANNEL(ioc), 0, 0, NULL); + f =3D qemu_file_new_input(QIO_CHANNEL(ioc)); + + ret =3D qemu_loadvm_state(f); + if (ret < 0) { + fprintf(stderr, "[QEMU] loadvm err: %d\n", ret); + } + + vm_start(); + + g_free(f); + + // print time to debug speed + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + fprintf(stderr, "loaded snapshot at %ld.%ld\n", ts.tv_sec, ts.tv_nsec); +} + +void qemu_snapshot_load_cpu_state(QIOChannelBuffer *ioc) { + /* Run in a bh because otherwise qemu_loadvm_state won't work + */ + QEMUBH *bh =3D qemu_bh_new(do_snapshot_load, ioc); + qemu_bh_schedule(bh); +} diff --git a/migration/savevm.h b/migration/savevm.h index 6461342cb4..990bcddd2f 100644 --- a/migration/savevm.h +++ b/migration/savevm.h @@ -68,4 +68,7 @@ int qemu_load_device_state(QEMUFile *f); int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f, bool in_postcopy, bool inactivate_disks); =20 +QIOChannelBuffer* qemu_snapshot_save_cpu_state(void); +void qemu_snapshot_load_cpu_state(QIOChannelBuffer *ioc); + #endif --=20 2.35.1