{"id":806340,"url":"http://patchwork.ozlabs.org/api/1.0/patches/806340/?format=json","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/1.0/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20170828015654.2530-12-Sergio.G.DelReal@gmail.com>","date":"2017-08-28T01:56:51","name":"[11/14] hvf: implement vga dirty page tracking","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"cdcb0dbced229c58f34706729aed6c3e6a8395e7","submitter":{"id":70675,"url":"http://patchwork.ozlabs.org/api/1.0/people/70675/?format=json","name":"Sergio Andres Gomez Del Real","email":"sergio.g.delreal@gmail.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170828015654.2530-12-Sergio.G.DelReal@gmail.com/mbox/","series":[{"id":56,"url":"http://patchwork.ozlabs.org/api/1.0/series/56/?format=json","date":"2017-08-28T01:56:40","name":"add support for Hypervisor.framework in QEMU","version":1,"mbox":"http://patchwork.ozlabs.org/series/56/mbox/"}],"check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/806340/checks/","tags":{},"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ozlabs.org;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=gmail.com header.i=@gmail.com\n\theader.b=\"PDZWo8Xd\"; dkim-atps=neutral"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xgZkG4ZXqz9sNw\n\tfor <incoming@patchwork.ozlabs.org>;\n\tMon, 28 Aug 2017 12:01:10 +1000 (AEST)","from localhost ([::1]:36510 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dm9MS-0007Qc-D2\n\tfor incoming@patchwork.ozlabs.org; Sun, 27 Aug 2017 22:01:08 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:42107)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <sergio.g.delreal@gmail.com>) id 1dm9JD-00054b-Vc\n\tfor qemu-devel@nongnu.org; Sun, 27 Aug 2017 21:57:49 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <sergio.g.delreal@gmail.com>) id 1dm9JC-0001FR-SU\n\tfor qemu-devel@nongnu.org; Sun, 27 Aug 2017 21:57:48 -0400","from mail-ua0-x243.google.com ([2607:f8b0:400c:c08::243]:37337)\n\tby eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16)\n\t(Exim 4.71) (envelope-from <sergio.g.delreal@gmail.com>)\n\tid 1dm9JC-0001FH-Ng\n\tfor qemu-devel@nongnu.org; Sun, 27 Aug 2017 21:57:46 -0400","by mail-ua0-x243.google.com with SMTP id 104so848971uas.4\n\tfor <qemu-devel@nongnu.org>; Sun, 27 Aug 2017 18:57:46 -0700 (PDT)","from localhost.localdomain ([191.109.6.191])\n\tby smtp.gmail.com with ESMTPSA id\n\ty12sm2696824uad.22.2017.08.27.18.57.44\n\t(version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\n\tSun, 27 Aug 2017 18:57:45 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references;\n\tbh=qM+YCzUmM1npnKwHgvb6aUdqLdaBqZaMnfRuqHsM17o=;\n\tb=PDZWo8XdHFyM6OcwZBpu4NtkVws0ByqocWFdWJVy1etfuLiGvYnJM/CIK1KX9w2ORi\n\tPmXnE7nJQJcKavxvQMmlFvguE5x4MQnYMcbcCEhMiyEiUk8eKpFgcCrlHyyL+mjntvRp\n\tBwz4/RO7s7BLjj02u8YRC4jW5t4k55SaeK3IX5F6bDkq2042eF6+vz0jSeopblM28k+1\n\tHAUb9NHPqMuhFkbk3MyDTtL5x1nykxuuX21WWJE6t+oRNl7RPCXddXAzfTeke3CRpi40\n\tW84hb1rqU0X4m3gVbykMroJYf1VNueK3YvUufJED7ZvKH2qqvzls8jmgOhB3cx4heIiY\n\tZtWg==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references;\n\tbh=qM+YCzUmM1npnKwHgvb6aUdqLdaBqZaMnfRuqHsM17o=;\n\tb=peJMA71Kfg/u8m854TNxPKVSZEGL2E7mejKLg7LlLWm6XZrABzgQGgthbtWUkQwMzZ\n\tYzftOc68kqdAbpvq0knhWJ689ZR6hohUMNxOO0O3XFWShEB1Zt8U2CCuW+HBVj7KP9Cx\n\tHCq34DwFNvEISIOUZQPm96gxJHDqjteZtC+p+7E2scjGJgBMn5OifP03zK6Kyon5LwqR\n\t7q09NX5e2ddKO4G3a++sln3MCyR5hgsuM5Sm6V0guy30+xgnpnNHeKxMKZhi00yBe3iC\n\tY1nN0159hQ2Pgrpgytr9iFUV735r3W2O8S1ukej4tUC8Q6dU7A4nkuOObu5Kim1hIFnp\n\t2f9A==","X-Gm-Message-State":"AHYfb5i4QgZuqQq+TCX/LhHTwYBZeFvZCqnE6/cil3e5bmMymoPJoRhG\n\t/YGTgkbel/dZM3ib","X-Received":"by 10.159.33.97 with SMTP id 88mr3667280uab.5.1503885465939;\n\tSun, 27 Aug 2017 18:57:45 -0700 (PDT)","From":"Sergio Andres Gomez Del Real <sergio.g.delreal@gmail.com>","X-Google-Original-From":"Sergio Andres Gomez Del Real\n\t<Sergio.G.DelReal@gmail.com>","To":"qemu-devel@nongnu.org","Date":"Sun, 27 Aug 2017 20:56:51 -0500","Message-Id":"<20170828015654.2530-12-Sergio.G.DelReal@gmail.com>","X-Mailer":"git-send-email 2.11.0","In-Reply-To":"<20170828015654.2530-1-Sergio.G.DelReal@gmail.com>","References":"<20170828015654.2530-1-Sergio.G.DelReal@gmail.com>","X-detected-operating-system":"by eggs.gnu.org: Genre and OS details not\n\trecognized.","X-Received-From":"2607:f8b0:400c:c08::243","Subject":"[Qemu-devel] [PATCH 11/14] hvf: implement vga dirty page tracking","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://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\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"Sergio Andres Gomez Del Real <Sergio.G.DelReal@gmail.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"This commit implements setting the tracking of dirty pages, using hvf's\ninterface to protect guest memory. It uses the MemoryListener callback\nmechanism through .log_start/stop/sync\n\nSigned-off-by: Sergio Andres Gomez Del Real <Sergio.G.DelReal@gmail.com>\n---\n include/sysemu/hvf.h  |  5 ++++\n target/i386/hvf-all.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++-----\n 2 files changed, 70 insertions(+), 7 deletions(-)","diff":"diff --git a/include/sysemu/hvf.h b/include/sysemu/hvf.h\nindex 5b92769b16..90e9ec174e 100644\n--- a/include/sysemu/hvf.h\n+++ b/include/sysemu/hvf.h\n@@ -27,11 +27,16 @@\n #include <Hypervisor/hv_error.h>\n #endif\n \n+/* hvf_slot flags */\n+#define HVF_SLOT_LOG (1 << 0)\n+\n typedef struct hvf_slot {\n     uint64_t start;\n     uint64_t size;\n     uint8_t *mem;\n     int slot_id;\n+    uint32_t flags;\n+    MemoryRegion *region;\n } hvf_slot;\n \n struct hvf_vcpu_caps {\ndiff --git a/target/i386/hvf-all.c b/target/i386/hvf-all.c\nindex 11d20671f7..20c796089d 100644\n--- a/target/i386/hvf-all.c\n+++ b/target/i386/hvf-all.c\n@@ -172,6 +172,7 @@ void hvf_set_phys_mem(MemoryRegionSection *section, bool add)\n     mem->size = int128_get64(section->size);\n     mem->mem = memory_region_get_ram_ptr(area) + section->offset_within_region;\n     mem->start = section->offset_within_address_space;\n+    mem->region = area;\n \n     if (do_hvf_set_memory(mem)) {\n         fprintf(stderr, \"Error registering new memory slot\\n\");\n@@ -441,8 +442,7 @@ void hvf_cpu_synchronize_post_init(CPUState *cpu_state)\n     run_on_cpu(cpu_state, _hvf_cpu_synchronize_post_init, RUN_ON_CPU_NULL);\n }\n \n-/* TODO: ept fault handlig */\n-static bool ept_emulation_fault(uint64_t ept_qual)\n+static bool ept_emulation_fault(hvf_slot *slot, addr_t gpa, uint64_t ept_qual)\n {\n     int read, write;\n \n@@ -458,6 +458,14 @@ static bool ept_emulation_fault(uint64_t ept_qual)\n         return false;\n     }\n \n+    if (write && slot) {\n+        if (slot->flags & HVF_SLOT_LOG) {\n+            memory_region_set_dirty(slot->region, gpa - slot->start, 1);\n+            hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size,\n+                          HV_MEMORY_READ | HV_MEMORY_WRITE);\n+        }\n+    }\n+\n     /*\n      * The EPT violation must have been caused by accessing a\n      * guest-physical address that is a translation of a guest-linear\n@@ -468,7 +476,57 @@ static bool ept_emulation_fault(uint64_t ept_qual)\n         return false;\n     }\n \n-    return true;\n+    return !slot;\n+}\n+\n+static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on)\n+{\n+    struct mac_slot *macslot;\n+    hvf_slot *slot;\n+\n+    slot = hvf_find_overlap_slot(\n+            section->offset_within_address_space,\n+            section->offset_within_address_space + int128_get64(section->size));\n+\n+    /* protect region against writes; begin tracking it */\n+    if (on) {\n+        slot->flags |= HVF_SLOT_LOG;\n+        hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size,\n+                      HV_MEMORY_READ);\n+    /* stop tracking region*/\n+    } else {\n+        slot->flags &= ~HVF_SLOT_LOG;\n+        hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size,\n+                      HV_MEMORY_READ | HV_MEMORY_WRITE);\n+    }\n+}\n+\n+static void hvf_log_start(MemoryListener *listener,\n+                          MemoryRegionSection *section, int old, int new)\n+{\n+    if (old != 0)\n+        return;\n+\n+    hvf_set_dirty_tracking(section, 1);\n+}\n+\n+static void hvf_log_stop(MemoryListener *listener,\n+                         MemoryRegionSection *section, int old, int new)\n+{\n+    if (new != 0)\n+        return;\n+\n+    hvf_set_dirty_tracking(section, 0);\n+}\n+\n+static void hvf_log_sync(MemoryListener *listener,\n+                         MemoryRegionSection *section)\n+{\n+    /*\n+     * sync of dirty pages is handled elsewhere; just make sure we keep\n+     * tracking the region.\n+     */\n+    hvf_set_dirty_tracking(section, 1);\n }\n \n static void hvf_region_add(MemoryListener *listener,\n@@ -487,6 +545,9 @@ static MemoryListener hvf_memory_listener = {\n     .priority = 10,\n     .region_add = hvf_region_add,\n     .region_del = hvf_region_del,\n+    .log_start = hvf_log_start,\n+    .log_stop = hvf_log_stop,\n+    .log_sync = hvf_log_sync,\n };\n \n void vmx_reset_vcpu(CPUState *cpu) {\n@@ -763,7 +824,7 @@ int hvf_vcpu_exec(CPUState *cpu)\n \n             slot = hvf_find_overlap_slot(gpa, gpa);\n             /* mmio */\n-            if (ept_emulation_fault(exit_qual) && !slot) {\n+            if (ept_emulation_fault(slot, gpa, exit_qual)) {\n                 struct x86_decode decode;\n \n                 load_regs(cpu);\n@@ -774,9 +835,6 @@ int hvf_vcpu_exec(CPUState *cpu)\n                 store_regs(cpu);\n                 break;\n             }\n-#ifdef DIRTY_VGA_TRACKING\n-            /* TODO: handle dirty page tracking */\n-#endif\n             break;\n         }\n         case EXIT_REASON_INOUT:\n","prefixes":["11/14"]}