From nobody Fri Nov 15 00:25:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646229283519811.2044612076585; Wed, 2 Mar 2022 05:54:43 -0800 (PST) Received: from localhost ([::1]:59086 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPPRB-00066W-PI for importer@patchew.org; Wed, 02 Mar 2022 08:54:42 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57356) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPOej-0003Xx-K0 for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:37 -0500 Received: from [2607:f8b0:4864:20::42c] (port=39672 helo=mail-pf1-x42c.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPOeh-0004xy-27 for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:37 -0500 Received: by mail-pf1-x42c.google.com with SMTP id y11so1910357pfa.6 for ; Wed, 02 Mar 2022 05:04:33 -0800 (PST) Received: from localhost.localdomain (2001-b011-e000-59d7-a02b-4f1b-c415-11a0.dynamic-ip6.hinet.net. [2001:b011:e000:59d7:a02b:4f1b:c415:11a0]) by smtp.gmail.com with ESMTPSA id l13-20020a056a00140d00b004e13da93eaasm21947964pfu.62.2022.03.02.05.04.30 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Mar 2022 05:04:31 -0800 (PST) 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=XTRZpcX8yriwglYSRcjxYmIcll43aiYpHGHO2GnCCJQ=; b=di263sdB1NbjIoS6wMzhiYwH0FHd99Q+CmRJqhJBPMovEhgvrPt46ie+ZfMrrpug6E waT4UzGi6V3exW9duqhIgi98FoFwuDbstRiPzh5840iswZQ+HqZghS8tq8UcEsG1kzYu ZHmBMT8rV0OM4YfG1/31KMC9b/Ryt6DmireyhQyAXQnX1g9mu/QkR6WmZ8TmiKHG5sVZ ELsPw8Gs4LQ6JMkQObMCvWC/D7I8VSGje3xwVbuJiFT1GgCUmVcJuChj3pfK81u3nsHh GX4jsSRfLUu+44qYeol8pRV8n4KzK0wVGizAltrUpvm8XVZogk46p6uhBO0phv44T88B qS7A== 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=XTRZpcX8yriwglYSRcjxYmIcll43aiYpHGHO2GnCCJQ=; b=wolR2FnEwq42PQviHfF0oFm6gwYpkg0LZHa+vznPfjfyhesU4rXEIYdvlDvw2bvdfm ngDZcw+MJ9zwYcR2bvBpY5WmW/QgSEz02sprUvOtIhSh33zYUymlY8/oNRCuQ+DyRSAk P3USVaxh/1am/JyqghjN0mM/KYsZ+CXDXswaGk5IUr4EAfnLio+KQWiGljWx/X2kMzZE 9pZoX3otD3zXia90QyQfKC6BA2nuFjgQ2tO1ZZP5HMcmusosYKG4PQYWBKZeFyw3/Rj6 prqUU6Y+V+4XXEzmoxmv61lYWV5qdosMHLxu0+n9eijn0p6DD+ypbvgX00HRcb8oehm4 4zLw== X-Gm-Message-State: AOAM532yu262hutGTDVuE/e7fuFcTaIk+DkIz86L+4NbMbIgvgpWdMLm nMv2+hNZPsxdbX7fmeje/lS7jjsgKIjLWw== X-Google-Smtp-Source: ABdhPJwGGoIdmXgrHK9SlKHdAGKpX5ujqNd3ibS17kG8F8wueAR4n04ZXj52th0u26zafHNdIJM1PQ== X-Received: by 2002:aa7:8643:0:b0:4d1:d068:ef15 with SMTP id a3-20020aa78643000000b004d1d068ef15mr33180885pfo.9.1646226271925; Wed, 02 Mar 2022 05:04:31 -0800 (PST) From: Yan-Jie Wang To: qemu-devel@nongnu.org Subject: [PATCH v3 1/9] hvf: move memory related functions from hvf-accel-ops.c to hvf-mem.c Date: Wed, 2 Mar 2022 21:04:09 +0800 Message-Id: <20220302130417.18551-2-ubzeme@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220302130417.18551-1-ubzeme@gmail.com> References: <20220302130417.18551-1-ubzeme@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::42c (failed) 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::42c; envelope-from=ubzeme@gmail.com; helo=mail-pf1-x42c.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Cc: Peter Maydell , Roman Bolshakov , Alexander Graf , Cameron Esfahani , Yan-Jie Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646229286210100001 Content-Type: text/plain; charset="utf-8" Signed-off-by: Yan-Jie Wang Reviewed-by: Peter Maydell --- accel/hvf/hvf-accel-ops.c | 220 +-------------------------------- accel/hvf/hvf-mem.c | 252 ++++++++++++++++++++++++++++++++++++++ accel/hvf/meson.build | 1 + include/sysemu/hvf_int.h | 2 + 4 files changed, 256 insertions(+), 219 deletions(-) create mode 100644 accel/hvf/hvf-mem.c diff --git a/accel/hvf/hvf-accel-ops.c b/accel/hvf/hvf-accel-ops.c index 54457c76c2..50a563bfe0 100644 --- a/accel/hvf/hvf-accel-ops.c +++ b/accel/hvf/hvf-accel-ops.c @@ -48,7 +48,6 @@ */ =20 #include "qemu/osdep.h" -#include "qemu/error-report.h" #include "qemu/main-loop.h" #include "exec/address-spaces.h" #include "exec/exec-all.h" @@ -64,143 +63,6 @@ HVFState *hvf_state; #define HV_VM_DEFAULT NULL #endif =20 -/* Memory slots */ - -hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t size) -{ - hvf_slot *slot; - int x; - for (x =3D 0; x < hvf_state->num_slots; ++x) { - slot =3D &hvf_state->slots[x]; - if (slot->size && start < (slot->start + slot->size) && - (start + size) > slot->start) { - return slot; - } - } - return NULL; -} - -struct mac_slot { - int present; - uint64_t size; - uint64_t gpa_start; - uint64_t gva; -}; - -struct mac_slot mac_slots[32]; - -static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags) -{ - struct mac_slot *macslot; - hv_return_t ret; - - macslot =3D &mac_slots[slot->slot_id]; - - if (macslot->present) { - if (macslot->size !=3D slot->size) { - macslot->present =3D 0; - ret =3D hv_vm_unmap(macslot->gpa_start, macslot->size); - assert_hvf_ok(ret); - } - } - - if (!slot->size) { - return 0; - } - - macslot->present =3D 1; - macslot->gpa_start =3D slot->start; - macslot->size =3D slot->size; - ret =3D hv_vm_map(slot->mem, slot->start, slot->size, flags); - assert_hvf_ok(ret); - return 0; -} - -static void hvf_set_phys_mem(MemoryRegionSection *section, bool add) -{ - hvf_slot *mem; - MemoryRegion *area =3D section->mr; - bool writeable =3D !area->readonly && !area->rom_device; - hv_memory_flags_t flags; - uint64_t page_size =3D qemu_real_host_page_size; - - if (!memory_region_is_ram(area)) { - if (writeable) { - return; - } else if (!memory_region_is_romd(area)) { - /* - * If the memory device is not in romd_mode, then we actually = want - * to remove the hvf memory slot so all accesses will trap. - */ - add =3D false; - } - } - - if (!QEMU_IS_ALIGNED(int128_get64(section->size), page_size) || - !QEMU_IS_ALIGNED(section->offset_within_address_space, page_size))= { - /* Not page aligned, so we can not map as RAM */ - add =3D false; - } - - mem =3D hvf_find_overlap_slot( - section->offset_within_address_space, - int128_get64(section->size)); - - if (mem && add) { - if (mem->size =3D=3D int128_get64(section->size) && - mem->start =3D=3D section->offset_within_address_space && - mem->mem =3D=3D (memory_region_get_ram_ptr(area) + - section->offset_within_region)) { - return; /* Same region was attempted to register, go away. */ - } - } - - /* Region needs to be reset. set the size to 0 and remap it. */ - if (mem) { - mem->size =3D 0; - if (do_hvf_set_memory(mem, 0)) { - error_report("Failed to reset overlapping slot"); - abort(); - } - } - - if (!add) { - return; - } - - if (area->readonly || - (!memory_region_is_ram(area) && memory_region_is_romd(area))) { - flags =3D HV_MEMORY_READ | HV_MEMORY_EXEC; - } else { - flags =3D HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC; - } - - /* Now make a new slot. */ - int x; - - for (x =3D 0; x < hvf_state->num_slots; ++x) { - mem =3D &hvf_state->slots[x]; - if (!mem->size) { - break; - } - } - - if (x =3D=3D hvf_state->num_slots) { - error_report("No free slots"); - abort(); - } - - mem->size =3D int128_get64(section->size); - mem->mem =3D memory_region_get_ram_ptr(area) + section->offset_within_= region; - mem->start =3D section->offset_within_address_space; - mem->region =3D area; - - if (do_hvf_set_memory(mem, flags)) { - error_report("Error registering new memory slot"); - abort(); - } -} - static void do_hvf_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data ar= g) { if (!cpu->vcpu_dirty) { @@ -238,79 +100,6 @@ static void hvf_cpu_synchronize_pre_loadvm(CPUState *c= pu) run_on_cpu(cpu, do_hvf_cpu_synchronize_set_dirty, RUN_ON_CPU_NULL); } =20 -static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on) -{ - hvf_slot *slot; - - slot =3D hvf_find_overlap_slot( - section->offset_within_address_space, - int128_get64(section->size)); - - /* protect region against writes; begin tracking it */ - if (on) { - slot->flags |=3D HVF_SLOT_LOG; - hv_vm_protect((uintptr_t)slot->start, (size_t)slot->size, - HV_MEMORY_READ | HV_MEMORY_EXEC); - /* stop tracking region*/ - } else { - slot->flags &=3D ~HVF_SLOT_LOG; - hv_vm_protect((uintptr_t)slot->start, (size_t)slot->size, - HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC); - } -} - -static void hvf_log_start(MemoryListener *listener, - MemoryRegionSection *section, int old, int new) -{ - if (old !=3D 0) { - return; - } - - hvf_set_dirty_tracking(section, 1); -} - -static void hvf_log_stop(MemoryListener *listener, - MemoryRegionSection *section, int old, int new) -{ - if (new !=3D 0) { - return; - } - - hvf_set_dirty_tracking(section, 0); -} - -static void hvf_log_sync(MemoryListener *listener, - MemoryRegionSection *section) -{ - /* - * sync of dirty pages is handled elsewhere; just make sure we keep - * tracking the region. - */ - hvf_set_dirty_tracking(section, 1); -} - -static void hvf_region_add(MemoryListener *listener, - MemoryRegionSection *section) -{ - hvf_set_phys_mem(section, true); -} - -static void hvf_region_del(MemoryListener *listener, - MemoryRegionSection *section) -{ - hvf_set_phys_mem(section, false); -} - -static MemoryListener hvf_memory_listener =3D { - .name =3D "hvf", - .priority =3D 10, - .region_add =3D hvf_region_add, - .region_del =3D hvf_region_del, - .log_start =3D hvf_log_start, - .log_stop =3D hvf_log_stop, - .log_sync =3D hvf_log_sync, -}; - static void dummy_signal(int sig) { } @@ -319,7 +108,6 @@ bool hvf_allowed; =20 static int hvf_accel_init(MachineState *ms) { - int x; hv_return_t ret; HVFState *s; =20 @@ -328,14 +116,8 @@ static int hvf_accel_init(MachineState *ms) =20 s =3D g_new0(HVFState, 1); =20 - s->num_slots =3D ARRAY_SIZE(s->slots); - for (x =3D 0; x < s->num_slots; ++x) { - s->slots[x].size =3D 0; - s->slots[x].slot_id =3D x; - } - hvf_state =3D s; - memory_listener_register(&hvf_memory_listener, &address_space_memory); + hvf_init_memslots(); =20 return hvf_arch_init(); } diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c new file mode 100644 index 0000000000..3712731ed9 --- /dev/null +++ b/accel/hvf/hvf-mem.c @@ -0,0 +1,252 @@ +/* + * Copyright 2008 IBM Corporation + * 2008 Red Hat, Inc. + * Copyright 2011 Intel Corporation + * Copyright 2016 Veertu, Inc. + * Copyright 2017 The Android Open Source Project + * + * QEMU Hypervisor.framework support + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "qemu/osdep.h" +#include "qemu/error-report.h" +#include "exec/address-spaces.h" +#include "sysemu/hvf.h" +#include "sysemu/hvf_int.h" + +/* Memory slots */ + +hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t size) +{ + hvf_slot *slot; + int x; + for (x =3D 0; x < hvf_state->num_slots; ++x) { + slot =3D &hvf_state->slots[x]; + if (slot->size && start < (slot->start + slot->size) && + (start + size) > slot->start) { + return slot; + } + } + return NULL; +} + +struct mac_slot { + int present; + uint64_t size; + uint64_t gpa_start; + uint64_t gva; +}; + +struct mac_slot mac_slots[32]; + +static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags) +{ + struct mac_slot *macslot; + hv_return_t ret; + + macslot =3D &mac_slots[slot->slot_id]; + + if (macslot->present) { + if (macslot->size !=3D slot->size) { + macslot->present =3D 0; + ret =3D hv_vm_unmap(macslot->gpa_start, macslot->size); + assert_hvf_ok(ret); + } + } + + if (!slot->size) { + return 0; + } + + macslot->present =3D 1; + macslot->gpa_start =3D slot->start; + macslot->size =3D slot->size; + ret =3D hv_vm_map(slot->mem, slot->start, slot->size, flags); + assert_hvf_ok(ret); + return 0; +} + +static void hvf_set_phys_mem(MemoryRegionSection *section, bool add) +{ + hvf_slot *mem; + MemoryRegion *area =3D section->mr; + bool writeable =3D !area->readonly && !area->rom_device; + hv_memory_flags_t flags; + uint64_t page_size =3D qemu_real_host_page_size; + + if (!memory_region_is_ram(area)) { + if (writeable) { + return; + } else if (!memory_region_is_romd(area)) { + /* + * If the memory device is not in romd_mode, then we actually = want + * to remove the hvf memory slot so all accesses will trap. + */ + add =3D false; + } + } + + if (!QEMU_IS_ALIGNED(int128_get64(section->size), page_size) || + !QEMU_IS_ALIGNED(section->offset_within_address_space, page_size))= { + /* Not page aligned, so we can not map as RAM */ + add =3D false; + } + + mem =3D hvf_find_overlap_slot( + section->offset_within_address_space, + int128_get64(section->size)); + + if (mem && add) { + if (mem->size =3D=3D int128_get64(section->size) && + mem->start =3D=3D section->offset_within_address_space && + mem->mem =3D=3D (memory_region_get_ram_ptr(area) + + section->offset_within_region)) { + return; /* Same region was attempted to register, go away. */ + } + } + + /* Region needs to be reset. set the size to 0 and remap it. */ + if (mem) { + mem->size =3D 0; + if (do_hvf_set_memory(mem, 0)) { + error_report("Failed to reset overlapping slot"); + abort(); + } + } + + if (!add) { + return; + } + + if (area->readonly || + (!memory_region_is_ram(area) && memory_region_is_romd(area))) { + flags =3D HV_MEMORY_READ | HV_MEMORY_EXEC; + } else { + flags =3D HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC; + } + + /* Now make a new slot. */ + int x; + + for (x =3D 0; x < hvf_state->num_slots; ++x) { + mem =3D &hvf_state->slots[x]; + if (!mem->size) { + break; + } + } + + if (x =3D=3D hvf_state->num_slots) { + error_report("No free slots"); + abort(); + } + + mem->size =3D int128_get64(section->size); + mem->mem =3D memory_region_get_ram_ptr(area) + section->offset_within_= region; + mem->start =3D section->offset_within_address_space; + mem->region =3D area; + + if (do_hvf_set_memory(mem, flags)) { + error_report("Error registering new memory slot"); + abort(); + } +} + + +static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on) +{ + hvf_slot *slot; + + slot =3D hvf_find_overlap_slot( + section->offset_within_address_space, + int128_get64(section->size)); + + /* protect region against writes; begin tracking it */ + if (on) { + slot->flags |=3D HVF_SLOT_LOG; + hv_vm_protect((uintptr_t)slot->start, (size_t)slot->size, + HV_MEMORY_READ | HV_MEMORY_EXEC); + /* stop tracking region*/ + } else { + slot->flags &=3D ~HVF_SLOT_LOG; + hv_vm_protect((uintptr_t)slot->start, (size_t)slot->size, + HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC); + } +} + +static void hvf_log_start(MemoryListener *listener, + MemoryRegionSection *section, int old, int new) +{ + if (old !=3D 0) { + return; + } + + hvf_set_dirty_tracking(section, 1); +} + +static void hvf_log_stop(MemoryListener *listener, + MemoryRegionSection *section, int old, int new) +{ + if (new !=3D 0) { + return; + } + + hvf_set_dirty_tracking(section, 0); +} + +static void hvf_log_sync(MemoryListener *listener, + MemoryRegionSection *section) +{ + /* + * sync of dirty pages is handled elsewhere; just make sure we keep + * tracking the region. + */ + hvf_set_dirty_tracking(section, 1); +} + +static void hvf_region_add(MemoryListener *listener, + MemoryRegionSection *section) +{ + hvf_set_phys_mem(section, true); +} + +static void hvf_region_del(MemoryListener *listener, + MemoryRegionSection *section) +{ + hvf_set_phys_mem(section, false); +} + +static MemoryListener hvf_memory_listener =3D { + .name =3D "hvf", + .priority =3D 10, + .region_add =3D hvf_region_add, + .region_del =3D hvf_region_del, + .log_start =3D hvf_log_start, + .log_stop =3D hvf_log_stop, + .log_sync =3D hvf_log_sync, +}; + +void hvf_init_memslots(void) +{ + int x; + HVFState *s =3D hvf_state; + + s->num_slots =3D ARRAY_SIZE(s->slots); + for (x =3D 0; x < s->num_slots; ++x) { + s->slots[x].size =3D 0; + s->slots[x].slot_id =3D x; + } + + memory_listener_register(&hvf_memory_listener, &address_space_memory); +} diff --git a/accel/hvf/meson.build b/accel/hvf/meson.build index fc52cb7843..7e7a2034f1 100644 --- a/accel/hvf/meson.build +++ b/accel/hvf/meson.build @@ -2,6 +2,7 @@ hvf_ss =3D ss.source_set() hvf_ss.add(files( 'hvf-all.c', 'hvf-accel-ops.c', + 'hvf-mem.c', )) =20 specific_ss.add_all(when: 'CONFIG_HVF', if_true: hvf_ss) diff --git a/include/sysemu/hvf_int.h b/include/sysemu/hvf_int.h index 6545f7cd61..cef20d750d 100644 --- a/include/sysemu/hvf_int.h +++ b/include/sysemu/hvf_int.h @@ -65,4 +65,6 @@ int hvf_put_registers(CPUState *); int hvf_get_registers(CPUState *); void hvf_kick_vcpu_thread(CPUState *cpu); =20 +void hvf_init_memslots(void); + #endif --=20 2.32.0 (Apple Git-132) From nobody Fri Nov 15 00:25:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646227039614953.7879964799477; Wed, 2 Mar 2022 05:17:19 -0800 (PST) Received: from localhost ([::1]:43346 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPOr0-000537-8w for importer@patchew.org; Wed, 02 Mar 2022 08:17:18 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57382) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPOek-0003ZM-Ns for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:39 -0500 Received: from [2607:f8b0:4864:20::52c] (port=33665 helo=mail-pg1-x52c.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPOei-0004y7-Gf for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:38 -0500 Received: by mail-pg1-x52c.google.com with SMTP id 12so1616200pgd.0 for ; Wed, 02 Mar 2022 05:04:35 -0800 (PST) Received: from localhost.localdomain (2001-b011-e000-59d7-a02b-4f1b-c415-11a0.dynamic-ip6.hinet.net. [2001:b011:e000:59d7:a02b:4f1b:c415:11a0]) by smtp.gmail.com with ESMTPSA id l13-20020a056a00140d00b004e13da93eaasm21947964pfu.62.2022.03.02.05.04.32 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Mar 2022 05:04:33 -0800 (PST) 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=EcGgDc6OGKcQA4p0Ncm8XhFop9pHI02HEIveclFCx2Y=; b=jrJQNsv56FLeICTcYIkl+sx4BN3RV+IcfSoo5uSaoSh7e4gs6gie8MrI7/24oyKB4k tz/e5hXstds8kBWI88HRdGZ17vvJa1lUBzSLVWMMHz1zgV0iXtUMBC5l+koeXTMy3fef EII/uo1X4g2jaIqbJwjc9sh1m57QzuvWT6dv07O9b9E4X1wxgcYRyppOZJ0aUkhoodZw 97OlBWEhIy/E06G/OWzD3NWjhfbg14GIbBgp91+WFmwxg+AW625EKc9neglCsXcu1JOx JfVhtXadKaGBJf+WL6ypXpqdxZZl5fFYPF3hR66PDGKSkeb+U28KTXgqcjWmq0emAJwk YPPg== 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=EcGgDc6OGKcQA4p0Ncm8XhFop9pHI02HEIveclFCx2Y=; b=MZHRpmfocA6L+ufnsSKj8pqWPf9JO+An/ZgJUj07oWvOc02YHk+lyybgCT7h3CbMv8 kuwl3/8sedtj0ViSO3aEG/mr0Qd7i8vDL7VJsgoISz/D50iMibyXHIGc4YrkgB/QpSa6 qFPukMh7WWh6s7Qoo4tIXuM1ZpRA9Pt+HGgEzXSmDgbDwZYfDacjHdxkU7P01xc+AJei u13TxuNv95LXFjDf1jRhd2WiT67/iWm835XAfBfDh2ofj+UTs2k6IlUyWob3/Dj7AmTU 4mi/x3BJFZRUG0jgHU4qOy5cev3zoqUwxG9bGp3X4p3RAGLlAyKU8MvyozdWUtNwYmcY YN/g== X-Gm-Message-State: AOAM530omJm++OtyST2fMSjm5S1a4RrSSBYYw+EW9nDDl0lNv0dBOsWk DA+2SskDAsItQ/C0tV4d7SZEu0Ma1gB99A== X-Google-Smtp-Source: ABdhPJwx51Ee6nJmFVGdc0uORxU9SLfaJND5jhkSCEdw/o3U/1jFR3dLyiN0xM1rcx1k4v49r5uuyQ== X-Received: by 2002:a65:6201:0:b0:369:4a47:aff1 with SMTP id d1-20020a656201000000b003694a47aff1mr25609954pgv.238.1646226274083; Wed, 02 Mar 2022 05:04:34 -0800 (PST) From: Yan-Jie Wang To: qemu-devel@nongnu.org Subject: [PATCH v3 2/9] hvf: simplify data structures and codes of memory related functions Date: Wed, 2 Mar 2022 21:04:10 +0800 Message-Id: <20220302130417.18551-3-ubzeme@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220302130417.18551-1-ubzeme@gmail.com> References: <20220302130417.18551-1-ubzeme@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::52c (failed) 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::52c; envelope-from=ubzeme@gmail.com; helo=mail-pg1-x52c.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Cc: Peter Maydell , Roman Bolshakov , Alexander Graf , Cameron Esfahani , Yan-Jie Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646227042120100001 Content-Type: text/plain; charset="utf-8" * Remove mac_slot and use hvf_slot only. The function of the two structures are similar. * Refactor function hvf_set_phys_mem(): - Remove unnecessary checks because any modified memory sections will be removed first (region_del called first) before being added. Therefore, new sections do not overlap with existing sections. - Try to align memory sections first before giving up sections that are not aligned to host page size. Signed-off-by: Yan-Jie Wang --- accel/hvf/hvf-accel-ops.c | 1 - accel/hvf/hvf-mem.c | 211 +++++++++++++++++++------------------- include/sysemu/hvf_int.h | 8 +- 3 files changed, 107 insertions(+), 113 deletions(-) diff --git a/accel/hvf/hvf-accel-ops.c b/accel/hvf/hvf-accel-ops.c index 50a563bfe0..c77f142f2b 100644 --- a/accel/hvf/hvf-accel-ops.c +++ b/accel/hvf/hvf-accel-ops.c @@ -49,7 +49,6 @@ =20 #include "qemu/osdep.h" #include "qemu/main-loop.h" -#include "exec/address-spaces.h" #include "exec/exec-all.h" #include "sysemu/cpus.h" #include "sysemu/hvf.h" diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c index 3712731ed9..32452696b6 100644 --- a/accel/hvf/hvf-mem.c +++ b/accel/hvf/hvf-mem.c @@ -28,12 +28,16 @@ =20 /* Memory slots */ =20 +#define HVF_NUM_SLOTS 32 + +static hvf_slot memslots[HVF_NUM_SLOTS]; + hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t size) { hvf_slot *slot; int x; - for (x =3D 0; x < hvf_state->num_slots; ++x) { - slot =3D &hvf_state->slots[x]; + for (x =3D 0; x < HVF_NUM_SLOTS; ++x) { + slot =3D &memslots[x]; if (slot->size && start < (slot->start + slot->size) && (start + size) > slot->start) { return slot; @@ -42,128 +46,130 @@ hvf_slot *hvf_find_overlap_slot(uint64_t start, uint6= 4_t size) return NULL; } =20 -struct mac_slot { - int present; - uint64_t size; - uint64_t gpa_start; - uint64_t gva; -}; - -struct mac_slot mac_slots[32]; - -static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags) +static hvf_slot *hvf_find_free_slot(void) { - struct mac_slot *macslot; - hv_return_t ret; - - macslot =3D &mac_slots[slot->slot_id]; - - if (macslot->present) { - if (macslot->size !=3D slot->size) { - macslot->present =3D 0; - ret =3D hv_vm_unmap(macslot->gpa_start, macslot->size); - assert_hvf_ok(ret); + hvf_slot *slot; + int x; + for (x =3D 0; x < HVF_NUM_SLOTS; x++) { + slot =3D &memslots[x]; + if (!slot->size) { + return slot; } } =20 - if (!slot->size) { - return 0; - } - - macslot->present =3D 1; - macslot->gpa_start =3D slot->start; - macslot->size =3D slot->size; - ret =3D hv_vm_map(slot->mem, slot->start, slot->size, flags); - assert_hvf_ok(ret); - return 0; + return NULL; +} + +/* + * Hypervisor.framework requires that the host virtual address, + * the guest physical address and the size of memory regions are aligned + * to the host page size. + * + * The function here tries to align the guest physical address and the siz= e. + * + * The return value is the aligned size. + * The aligned guest physical address will be written to `start'. + * The delta between the aligned address and the original address will be + * written to `delta'. + */ +static hwaddr hvf_align_section(MemoryRegionSection *section, + hwaddr *start, hwaddr *delta) +{ + hwaddr unaligned, _start, size, _delta; + + unaligned =3D section->offset_within_address_space; + size =3D int128_get64(section->size); + _start =3D ROUND_UP(unaligned, qemu_real_host_page_size); + _delta =3D _start - unaligned; + size =3D (size - _delta) & qemu_real_host_page_mask; + + *start =3D _start; + *delta =3D _delta; + + return size; } =20 static void hvf_set_phys_mem(MemoryRegionSection *section, bool add) { - hvf_slot *mem; + hvf_slot *slot; + hwaddr start, size, offset, delta; + uint8_t *host_addr; MemoryRegion *area =3D section->mr; - bool writeable =3D !area->readonly && !area->rom_device; + bool readonly, dirty_tracking; hv_memory_flags_t flags; - uint64_t page_size =3D qemu_real_host_page_size; + hv_return_t ret; =20 - if (!memory_region_is_ram(area)) { - if (writeable) { + if (add && !memory_region_is_ram(area) && !memory_region_is_romd(area)= ) { + /* + * If the memory region is not RAM and is in ROMD mode, + * do not map it to the guest. + */ + return; + } + + size =3D hvf_align_section(section, &start, &delta); + + if (!size) { + /* The size is 0 after aligned. Do not map the region */ + return; + } + + if (add) { + /* add memory region */ + offset =3D section->offset_within_region + delta; + host_addr =3D memory_region_get_ram_ptr(area) + offset; + + if (!QEMU_PTR_IS_ALIGNED(host_addr, qemu_real_host_page_size)) { + /* The host virtual address is not aligned. It cannot be mappe= d */ return; - } else if (!memory_region_is_romd(area)) { - /* - * If the memory device is not in romd_mode, then we actually = want - * to remove the hvf memory slot so all accesses will trap. - */ - add =3D false; } - } =20 - if (!QEMU_IS_ALIGNED(int128_get64(section->size), page_size) || - !QEMU_IS_ALIGNED(section->offset_within_address_space, page_size))= { - /* Not page aligned, so we can not map as RAM */ - add =3D false; - } + dirty_tracking =3D !!memory_region_get_dirty_log_mask(area); + readonly =3D memory_region_is_rom(area) || memory_region_is_romd(a= rea); =20 - mem =3D hvf_find_overlap_slot( - section->offset_within_address_space, - int128_get64(section->size)); - - if (mem && add) { - if (mem->size =3D=3D int128_get64(section->size) && - mem->start =3D=3D section->offset_within_address_space && - mem->mem =3D=3D (memory_region_get_ram_ptr(area) + - section->offset_within_region)) { - return; /* Same region was attempted to register, go away. */ - } - } - - /* Region needs to be reset. set the size to 0 and remap it. */ - if (mem) { - mem->size =3D 0; - if (do_hvf_set_memory(mem, 0)) { - error_report("Failed to reset overlapping slot"); + /* setup a slot */ + slot =3D hvf_find_free_slot(); + if (!slot) { + error_report("No free slots"); abort(); } - } =20 - if (!add) { - return; - } + slot->start =3D start; + slot->size =3D size; + slot->offset =3D offset; + slot->flags =3D 0; + slot->region =3D area; =20 - if (area->readonly || - (!memory_region_is_ram(area) && memory_region_is_romd(area))) { - flags =3D HV_MEMORY_READ | HV_MEMORY_EXEC; + if (readonly) { + slot->flags |=3D HVF_SLOT_READONLY; + } + + if (dirty_tracking) { + slot->flags |=3D HVF_SLOT_LOG; + } + + /* set Hypervisor.framework memory mapping flags */ + if (readonly || dirty_tracking) { + flags =3D HV_MEMORY_READ | HV_MEMORY_EXEC; + } else { + flags =3D HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC; + } + + ret =3D hv_vm_map(host_addr, start, size, flags); + assert_hvf_ok(ret); } else { - flags =3D HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC; - } + /* remove memory region */ + slot =3D hvf_find_overlap_slot(start, size); =20 - /* Now make a new slot. */ - int x; + if (slot) { + ret =3D hv_vm_unmap(start, size); + assert_hvf_ok(ret); =20 - for (x =3D 0; x < hvf_state->num_slots; ++x) { - mem =3D &hvf_state->slots[x]; - if (!mem->size) { - break; + slot->size =3D 0; } } - - if (x =3D=3D hvf_state->num_slots) { - error_report("No free slots"); - abort(); - } - - mem->size =3D int128_get64(section->size); - mem->mem =3D memory_region_get_ram_ptr(area) + section->offset_within_= region; - mem->start =3D section->offset_within_address_space; - mem->region =3D area; - - if (do_hvf_set_memory(mem, flags)) { - error_report("Error registering new memory slot"); - abort(); - } } =20 - static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on) { hvf_slot *slot; @@ -239,14 +245,5 @@ static MemoryListener hvf_memory_listener =3D { =20 void hvf_init_memslots(void) { - int x; - HVFState *s =3D hvf_state; - - s->num_slots =3D ARRAY_SIZE(s->slots); - for (x =3D 0; x < s->num_slots; ++x) { - s->slots[x].size =3D 0; - s->slots[x].slot_id =3D x; - } - memory_listener_register(&hvf_memory_listener, &address_space_memory); } diff --git a/include/sysemu/hvf_int.h b/include/sysemu/hvf_int.h index cef20d750d..8ee31a16ac 100644 --- a/include/sysemu/hvf_int.h +++ b/include/sysemu/hvf_int.h @@ -19,12 +19,12 @@ =20 /* hvf_slot flags */ #define HVF_SLOT_LOG (1 << 0) +#define HVF_SLOT_READONLY (1 << 1) =20 typedef struct hvf_slot { uint64_t start; - uint64_t size; - uint8_t *mem; - int slot_id; + uint64_t size; /* 0 if the slot is free */ + uint64_t offset; /* offset within memory region */ uint32_t flags; MemoryRegion *region; } hvf_slot; @@ -40,8 +40,6 @@ typedef struct hvf_vcpu_caps { =20 struct HVFState { AccelState parent; - hvf_slot slots[32]; - int num_slots; =20 hvf_vcpu_caps *hvf_caps; uint64_t vtimer_offset; --=20 2.32.0 (Apple Git-132) From nobody Fri Nov 15 00:25:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646228836762230.0036591249078; Wed, 2 Mar 2022 05:47:16 -0800 (PST) Received: from localhost ([::1]:45630 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPPK0-00059v-00 for importer@patchew.org; Wed, 02 Mar 2022 08:47:16 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57394) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPOel-0003aT-LH for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:40 -0500 Received: from [2607:f8b0:4864:20::433] (port=40879 helo=mail-pf1-x433.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPOej-0004yK-Rl for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:39 -0500 Received: by mail-pf1-x433.google.com with SMTP id z15so1899060pfe.7 for ; Wed, 02 Mar 2022 05:04:37 -0800 (PST) Received: from localhost.localdomain (2001-b011-e000-59d7-a02b-4f1b-c415-11a0.dynamic-ip6.hinet.net. [2001:b011:e000:59d7:a02b:4f1b:c415:11a0]) by smtp.gmail.com with ESMTPSA id l13-20020a056a00140d00b004e13da93eaasm21947964pfu.62.2022.03.02.05.04.34 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Mar 2022 05:04:35 -0800 (PST) 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=tFB9N0+6/fehwQuRXDdcYFp1qjN2m+rAm/M1d8WEXJE=; b=CJzxVbgPo2PsPEvATmz6xmHchL1VFjsAk2ZZXafL1WLjRnZjRJf0pl8Xyhz6HIm/Ud k4M0rvvbuZnXzYcUrEfHPNUFdLghZZ4TbhBM4Z7xg/h3pVRcIfMyPsB7raWasPUgkZQY K9o1RYbmifBrhqSyIq/NUJlw/+s/bRj1Z7n4PDSVUZA9IOpcOp/PcZhmQ3jkYtMg/tU1 y8mlziDO+vePuz22TAdNEQf8kYkbllgLJN5qP0+PjgH3dgWM8nKePjhmgyqvhSlJCllo u+arEuj+dKvudFqCfQglU9nbORl6Gh96g8uohwcfYMHqkdMSabwZzwDAxu2twLIpkc+x AfPg== 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=tFB9N0+6/fehwQuRXDdcYFp1qjN2m+rAm/M1d8WEXJE=; b=pnpGQREj+9TvL2a7dvNc5VXfPMp71ildEpF89LACt7Vpe6/caJF94vfCnDuD6X3h3Y 9vQsqUvp2s0gYVqPuup5laGdGa0OOOKgHaVKINW13T/w7FrxO7qJAecAUZDjc77HlX+V TQETeYVCu3pw+D0YxCZ2dY0nR18GHFbv7QNsNgdKkT8p4X92t1bsMGDkPmriyNdvhGr7 qFAyPy0Hmmo4f4Sbl600nmCV+tSuamVbkOclRykwbS2QN0sLZgAc6r/vUsmXme2ZSHxd e/opRtXLoqH+kDrAzZSjNByXgYc23wwCxxF233BkTbUO/DhNe9HsFigGje+B8OQpEX5R gA6g== X-Gm-Message-State: AOAM533k1eEevuLKwnrhZd7C5Oz5SHaJ/971FU6T9SvgSutPTJ+FaTfz uDz4Ex/6t1o19mr3iKMKW6rX4+9FjYV8lg== X-Google-Smtp-Source: ABdhPJxHZjwFTfKHPyvKV1Ln44+FXoUPzYwsRDfNac4lVmfKwkDDXb0t7nAT9Ea/ZJswWh3uy743MQ== X-Received: by 2002:a63:a501:0:b0:372:f7dc:6ced with SMTP id n1-20020a63a501000000b00372f7dc6cedmr26300649pgf.26.1646226276119; Wed, 02 Mar 2022 05:04:36 -0800 (PST) From: Yan-Jie Wang To: qemu-devel@nongnu.org Subject: [PATCH v3 3/9] hvf: use correct data types for addresses in memory related functions Date: Wed, 2 Mar 2022 21:04:11 +0800 Message-Id: <20220302130417.18551-4-ubzeme@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220302130417.18551-1-ubzeme@gmail.com> References: <20220302130417.18551-1-ubzeme@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::433 (failed) 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::433; envelope-from=ubzeme@gmail.com; helo=mail-pf1-x433.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Cc: Peter Maydell , Roman Bolshakov , Alexander Graf , Cameron Esfahani , Yan-Jie Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646228839841100001 Content-Type: text/plain; charset="utf-8" Follow the QEMU coding style. Use hwaddr for guest physical address. Signed-off-by: Yan-Jie Wang Reviewed-by: Peter Maydell --- accel/hvf/hvf-mem.c | 2 +- include/sysemu/hvf_int.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c index 32452696b6..6b82be3220 100644 --- a/accel/hvf/hvf-mem.c +++ b/accel/hvf/hvf-mem.c @@ -32,7 +32,7 @@ =20 static hvf_slot memslots[HVF_NUM_SLOTS]; =20 -hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t size) +hvf_slot *hvf_find_overlap_slot(hwaddr start, hwaddr size) { hvf_slot *slot; int x; diff --git a/include/sysemu/hvf_int.h b/include/sysemu/hvf_int.h index 8ee31a16ac..2c4a97debe 100644 --- a/include/sysemu/hvf_int.h +++ b/include/sysemu/hvf_int.h @@ -22,9 +22,9 @@ #define HVF_SLOT_READONLY (1 << 1) =20 typedef struct hvf_slot { - uint64_t start; - uint64_t size; /* 0 if the slot is free */ - uint64_t offset; /* offset within memory region */ + hwaddr start; + hwaddr size; /* 0 if the slot is free */ + hwaddr offset; /* offset within memory region */ uint32_t flags; MemoryRegion *region; } hvf_slot; @@ -58,7 +58,7 @@ int hvf_arch_init(void); int hvf_arch_init_vcpu(CPUState *cpu); void hvf_arch_vcpu_destroy(CPUState *cpu); int hvf_vcpu_exec(CPUState *); -hvf_slot *hvf_find_overlap_slot(uint64_t, uint64_t); +hvf_slot *hvf_find_overlap_slot(hwaddr, hwaddr); int hvf_put_registers(CPUState *); int hvf_get_registers(CPUState *); void hvf_kick_vcpu_thread(CPUState *cpu); --=20 2.32.0 (Apple Git-132) From nobody Fri Nov 15 00:25:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 164622905398064.90899928366798; Wed, 2 Mar 2022 05:50:53 -0800 (PST) Received: from localhost ([::1]:52382 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPPNV-0001Ob-82 for importer@patchew.org; Wed, 02 Mar 2022 08:50:53 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57428) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPOep-0003c7-Ao for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:47 -0500 Received: from [2607:f8b0:4864:20::431] (port=36407 helo=mail-pf1-x431.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPOem-0004yr-PL for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:42 -0500 Received: by mail-pf1-x431.google.com with SMTP id z16so1918247pfh.3 for ; Wed, 02 Mar 2022 05:04:39 -0800 (PST) Received: from localhost.localdomain (2001-b011-e000-59d7-a02b-4f1b-c415-11a0.dynamic-ip6.hinet.net. [2001:b011:e000:59d7:a02b:4f1b:c415:11a0]) by smtp.gmail.com with ESMTPSA id l13-20020a056a00140d00b004e13da93eaasm21947964pfu.62.2022.03.02.05.04.36 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Mar 2022 05:04:37 -0800 (PST) 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=ynqt3y5s/uUAaEi3pDIc2AV9gtv8UZRUMS//euNtFRg=; b=EG+T/BsKj/CIQWhrlgfYicT0nDqRroWMlpXyMMaqCIbPhpw9ftAiehyPk1NEQIFU3R SNGgcaJoIliN4nG0aEYLMe8AU9jYTY0z7ojAVjP1IQMmrDjKoOs/66xzBWDTCBx+9YVL mVlzJOOzr1fQtbvoUAd+OQchVN7BdTFycA+sjIOcD9Mc3ejfuZ0fO92G2i9F8aptQqj6 6vHSeaXIvJfFtDOWDp3qGDRe+MJ4/nOnVr0tNKZQmzBUDmrncW3gHDsSTpYbMJR7wKGO lbqgF+oNXS6UdamqueAdkUBbZVG/TQM8dx8RdmH7QtRs9caAO5K5BcwbITXqx28ywwXO xuJQ== 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=ynqt3y5s/uUAaEi3pDIc2AV9gtv8UZRUMS//euNtFRg=; b=40JYgka99t/6u+Eywrp/kDpRKA7QDpRl1estuLpjMBM0NOXj7ZmkHxwSnqYpVk5d1W cVJNLPLkRSxj4keLkXp1Es5AZaE1t3MoRe+apesvUXevmnO0l3NKOqnLh26P4TAmQCNO J40wzXofgdNLcaMZ6neE9u2lVF6pcGr1dMthixTFtC6upfQt32tIxgl8pUmxOEfQtRvX mWoOMJysM6H8qM3SlduGdKusOQ6jJLeX8wLQc3oPeY5XvCU0uR6Jxcy4IPHMUPkYgzAc 5hw6l6NroHL63U/hJob6skOFBRm3JQktk/kOwN/+6SaORTP4HVjagHneciRrDA9t7Tyn RK/g== X-Gm-Message-State: AOAM530lNFMY1wCgYXuTWnaNKnCoKnYFEVz45OXEGMbgvm7NCxgqYkzB IItWZnD7WIiX56rjLXPrG9KiItvSxhcW/Q== X-Google-Smtp-Source: ABdhPJwiBCSLrfT8gH7lT46FPnmkQJKuyjR1uRjVaQ6HCJwJXcGLKGpshkr38T3NwEbMImK2qVBpCA== X-Received: by 2002:a63:6908:0:b0:372:d919:82ed with SMTP id e8-20020a636908000000b00372d91982edmr26310503pgc.104.1646226278307; Wed, 02 Mar 2022 05:04:38 -0800 (PST) From: Yan-Jie Wang To: qemu-devel@nongnu.org Subject: [PATCH v3 4/9] hvf: rename struct hvf_slot to HVFSlot Date: Wed, 2 Mar 2022 21:04:12 +0800 Message-Id: <20220302130417.18551-5-ubzeme@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220302130417.18551-1-ubzeme@gmail.com> References: <20220302130417.18551-1-ubzeme@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::431 (failed) 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::431; envelope-from=ubzeme@gmail.com; helo=mail-pf1-x431.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Cc: Peter Maydell , Roman Bolshakov , Alexander Graf , Cameron Esfahani , Yan-Jie Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646229055789100001 Content-Type: text/plain; charset="utf-8" Follow the QEMU coding style. Structured type names are in CamelCase. Signed-off-by: Yan-Jie Wang Reviewed-by: Peter Maydell --- accel/hvf/hvf-mem.c | 14 +++++++------- include/sysemu/hvf_int.h | 8 ++++---- target/i386/hvf/hvf.c | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c index 6b82be3220..b8e9f30e4c 100644 --- a/accel/hvf/hvf-mem.c +++ b/accel/hvf/hvf-mem.c @@ -30,11 +30,11 @@ =20 #define HVF_NUM_SLOTS 32 =20 -static hvf_slot memslots[HVF_NUM_SLOTS]; +static HVFSlot memslots[HVF_NUM_SLOTS]; =20 -hvf_slot *hvf_find_overlap_slot(hwaddr start, hwaddr size) +HVFSlot *hvf_find_overlap_slot(hwaddr start, hwaddr size) { - hvf_slot *slot; + HVFSlot *slot; int x; for (x =3D 0; x < HVF_NUM_SLOTS; ++x) { slot =3D &memslots[x]; @@ -46,9 +46,9 @@ hvf_slot *hvf_find_overlap_slot(hwaddr start, hwaddr size) return NULL; } =20 -static hvf_slot *hvf_find_free_slot(void) +static HVFSlot *hvf_find_free_slot(void) { - hvf_slot *slot; + HVFSlot *slot; int x; for (x =3D 0; x < HVF_NUM_SLOTS; x++) { slot =3D &memslots[x]; @@ -91,7 +91,7 @@ static hwaddr hvf_align_section(MemoryRegionSection *sect= ion, =20 static void hvf_set_phys_mem(MemoryRegionSection *section, bool add) { - hvf_slot *slot; + HVFSlot *slot; hwaddr start, size, offset, delta; uint8_t *host_addr; MemoryRegion *area =3D section->mr; @@ -172,7 +172,7 @@ static void hvf_set_phys_mem(MemoryRegionSection *secti= on, bool add) =20 static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on) { - hvf_slot *slot; + HVFSlot *slot; =20 slot =3D hvf_find_overlap_slot( section->offset_within_address_space, diff --git a/include/sysemu/hvf_int.h b/include/sysemu/hvf_int.h index 2c4a97debe..0aafbc9357 100644 --- a/include/sysemu/hvf_int.h +++ b/include/sysemu/hvf_int.h @@ -17,17 +17,17 @@ #include #endif =20 -/* hvf_slot flags */ +/* HVFSlot flags */ #define HVF_SLOT_LOG (1 << 0) #define HVF_SLOT_READONLY (1 << 1) =20 -typedef struct hvf_slot { +typedef struct HVFSlot { hwaddr start; hwaddr size; /* 0 if the slot is free */ hwaddr offset; /* offset within memory region */ uint32_t flags; MemoryRegion *region; -} hvf_slot; +} HVFSlot; =20 typedef struct hvf_vcpu_caps { uint64_t vmx_cap_pinbased; @@ -58,7 +58,7 @@ int hvf_arch_init(void); int hvf_arch_init_vcpu(CPUState *cpu); void hvf_arch_vcpu_destroy(CPUState *cpu); int hvf_vcpu_exec(CPUState *); -hvf_slot *hvf_find_overlap_slot(hwaddr, hwaddr); +HVFSlot *hvf_find_overlap_slot(hwaddr, hwaddr); int hvf_put_registers(CPUState *); int hvf_get_registers(CPUState *); void hvf_kick_vcpu_thread(CPUState *cpu); diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c index 4ba6e82fab..2ddb4fc825 100644 --- a/target/i386/hvf/hvf.c +++ b/target/i386/hvf/hvf.c @@ -113,7 +113,7 @@ void hvf_handle_io(CPUArchState *env, uint16_t port, vo= id *buffer, } } =20 -static bool ept_emulation_fault(hvf_slot *slot, uint64_t gpa, uint64_t ept= _qual) +static bool ept_emulation_fault(HVFSlot *slot, uint64_t gpa, uint64_t ept_= qual) { int read, write; =20 @@ -469,7 +469,7 @@ int hvf_vcpu_exec(CPUState *cpu) /* Need to check if MMIO or unmapped fault */ case EXIT_REASON_EPT_FAULT: { - hvf_slot *slot; + HVFSlot *slot; uint64_t gpa =3D rvmcs(cpu->hvf->fd, VMCS_GUEST_PHYSICAL_ADDRE= SS); =20 if (((idtvec_info & VMCS_IDT_VEC_VALID) =3D=3D 0) && --=20 2.32.0 (Apple Git-132) From nobody Fri Nov 15 00:25:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646230755436671.9765364856312; Wed, 2 Mar 2022 06:19:15 -0800 (PST) Received: from localhost ([::1]:54012 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPPow-0001xr-Bx for importer@patchew.org; Wed, 02 Mar 2022 09:19:14 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57456) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPOer-0003cE-1s for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:47 -0500 Received: from [2607:f8b0:4864:20::52e] (port=40452 helo=mail-pg1-x52e.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPOeo-0004z7-VV for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:44 -0500 Received: by mail-pg1-x52e.google.com with SMTP id w37so1571080pga.7 for ; Wed, 02 Mar 2022 05:04:41 -0800 (PST) Received: from localhost.localdomain (2001-b011-e000-59d7-a02b-4f1b-c415-11a0.dynamic-ip6.hinet.net. [2001:b011:e000:59d7:a02b:4f1b:c415:11a0]) by smtp.gmail.com with ESMTPSA id l13-20020a056a00140d00b004e13da93eaasm21947964pfu.62.2022.03.02.05.04.38 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Mar 2022 05:04:40 -0800 (PST) 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=2T+NCiBAVUTU368d4Yz1z1btdsU5CgR5N/ysNvhlN6k=; b=jkZ1EMzHaP/MQb9DEg2b8LQzt6zQ9yY1lr5zEDd92xanu/xBFgEpGloNqFL9lOHImF crwG5de6mmdq+hF5isI6QpVybv+nAGxcHOwdOrjra9ZuhmPxg+CL38iNcZ9FQlm5DvPg HJ2mJkn02Tyy1HvJrTepE9RyGFOJ4hM21nNqtlGGR0nKcG5e3qRkl5UAwh8h1pxk9Ync SDfX2EWC9TR0wb2BWPAaKuDUBxoOZr01I9HWKgBYD3vkaawnKALKj7g0RPpR/FiY30R0 /GXuu3+HmHL3GBSRWmeUC50ba1YtTtZufaNHkwI5CUyBFpb5qu3VskTX35IgTaLyLNbO 3CSA== 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=2T+NCiBAVUTU368d4Yz1z1btdsU5CgR5N/ysNvhlN6k=; b=Ayw9IZqKFH7NIDhWRKDLJ0/KL5eQ6T9dpa8M0gSNlMgIGq1UOfd9PRShZbqodBasoh erLaniGMDblHxOUj91Qop9rNXVaK1syfCnpIzqM7UuAu4bKiWvsb0x3rtoQcZt192b3X +/fAE1vWluAmwEN2nPNW12TVVMJ5inUM6npxgdmPTOEYP4Prat6F5x+I/uwmN88J3yUR lO+ONnka1wibqm55Yq8QmF7APQIdqW7GGPjfJDnP1+6vNug4jjbm4YLJGa5Vdu7xhdeu Jrtc8HtgP6fnvNxgihhNMv9XiJo2ngSRuWOiPsHjVd+rlPCkih0NZk+lUqQNXrY+4kAu 59CQ== X-Gm-Message-State: AOAM533gbd+7WiYGF1af0OnlTLH7KfzVneqCVCG36+No6l6zfHrb9nLw /+qPHJRixU657zlVuBD9ZZKDnPOXYjgdMQ== X-Google-Smtp-Source: ABdhPJytcbqDkaJlYPTVjd+grY7jX64VL37oaCtUHzmN5TrPXl7M3JbE602JAm4Xb/opfTfnMFcMXQ== X-Received: by 2002:a63:7781:0:b0:372:ba7b:c894 with SMTP id s123-20020a637781000000b00372ba7bc894mr25804873pgc.458.1646226280480; Wed, 02 Mar 2022 05:04:40 -0800 (PST) From: Yan-Jie Wang To: qemu-devel@nongnu.org Subject: [PATCH v3 5/9] hvf: fix memory dirty-tracking Date: Wed, 2 Mar 2022 21:04:13 +0800 Message-Id: <20220302130417.18551-6-ubzeme@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220302130417.18551-1-ubzeme@gmail.com> References: <20220302130417.18551-1-ubzeme@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::52e (failed) 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::52e; envelope-from=ubzeme@gmail.com; helo=mail-pg1-x52e.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Cc: Peter Maydell , Roman Bolshakov , Alexander Graf , Cameron Esfahani , Yan-Jie Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646230757217100001 Content-Type: text/plain; charset="utf-8" Dirty-tracking in HVF is not properly implemented. On Intel Macs, Ubuntu ISO boot menu does not show properly. On Apple Silicon, using bochs-display may cause the guest crashes because the guest may uses load/store instructions on framebuffer which causes vmexits and the exception register does not contain enough information (ESR_EL2.ISV =3D 0) for QEMU to emulate the memory operation. The strategy to log the dirty pages is to write-protect the memory regions that are being dirty-tracked. When the guest is trapped to the host because of memory write, check whether the address being written is being dirty-tracked. If it is being dirty-tracked, restore the write permission of the page and mark the accessed page dirty, and resume the guest without increasing program counter, and then the same instruction will be execute again. This patch fixes the problem and make the dirty-tracking work properly. Buglink: https://bugs.launchpad.net/qemu/+bug/1827005 Signed-off-by: Yan-Jie Wang --- accel/hvf/hvf-mem.c | 62 ++++++++++++++++++++++++++++++++++++---- include/sysemu/hvf_int.h | 14 +-------- target/arm/hvf/hvf.c | 5 ++++ target/i386/hvf/hvf.c | 25 ++++------------ 4 files changed, 68 insertions(+), 38 deletions(-) diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c index b8e9f30e4c..896e718374 100644 --- a/accel/hvf/hvf-mem.c +++ b/accel/hvf/hvf-mem.c @@ -30,9 +30,21 @@ =20 #define HVF_NUM_SLOTS 32 =20 +/* HVFSlot flags */ +#define HVF_SLOT_LOG (1 << 0) +#define HVF_SLOT_READONLY (1 << 1) + +typedef struct HVFSlot { + hwaddr start; + hwaddr size; /* 0 if the slot is free */ + hwaddr offset; /* offset within memory region */ + uint32_t flags; + MemoryRegion *region; +} HVFSlot; + static HVFSlot memslots[HVF_NUM_SLOTS]; =20 -HVFSlot *hvf_find_overlap_slot(hwaddr start, hwaddr size) +static HVFSlot *hvf_find_overlap_slot(hwaddr start, hwaddr size) { HVFSlot *slot; int x; @@ -194,7 +206,7 @@ static void hvf_set_dirty_tracking(MemoryRegionSection = *section, bool on) static void hvf_log_start(MemoryListener *listener, MemoryRegionSection *section, int old, int new) { - if (old !=3D 0) { + if (old =3D=3D new) { return; } =20 @@ -211,12 +223,12 @@ static void hvf_log_stop(MemoryListener *listener, hvf_set_dirty_tracking(section, 0); } =20 -static void hvf_log_sync(MemoryListener *listener, +static void hvf_log_clear(MemoryListener *listener, MemoryRegionSection *section) { /* - * sync of dirty pages is handled elsewhere; just make sure we keep - * tracking the region. + * The dirty bits are being cleared. + * Make the section write-protected again. */ hvf_set_dirty_tracking(section, 1); } @@ -240,9 +252,47 @@ static MemoryListener hvf_memory_listener =3D { .region_del =3D hvf_region_del, .log_start =3D hvf_log_start, .log_stop =3D hvf_log_stop, - .log_sync =3D hvf_log_sync, + .log_clear =3D hvf_log_clear, }; =20 + +/* + * The function is called when the guest is accessing memory causing vmexi= t. + * Check whether the guest can access the memory directly and + * also mark the accessed page being written dirty + * if the page is being dirty-tracked. + * + * Return true if the access is within the mapped region, + * otherwise return false. + */ +bool hvf_access_memory(hwaddr address, bool write) +{ + HVFSlot *slot; + hv_return_t ret; + hwaddr start, size; + + slot =3D hvf_find_overlap_slot(address, 1); + + if (!slot || (write && slot->flags & HVF_SLOT_READONLY)) { + /* MMIO or unmapped area, return false */ + return false; + } + + if (write && (slot->flags & HVF_SLOT_LOG)) { + /* The slot is being dirty-tracked. Mark the accessed page dirty. = */ + start =3D address & qemu_real_host_page_mask; + size =3D qemu_real_host_page_size; + + memory_region_set_dirty(slot->region, + start - slot->start + slot->offset, size); + ret =3D hv_vm_protect(start, size, + HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC); + assert_hvf_ok(ret); + } + + return true; +} + void hvf_init_memslots(void) { memory_listener_register(&hvf_memory_listener, &address_space_memory); diff --git a/include/sysemu/hvf_int.h b/include/sysemu/hvf_int.h index 0aafbc9357..16e5faf0ff 100644 --- a/include/sysemu/hvf_int.h +++ b/include/sysemu/hvf_int.h @@ -17,18 +17,6 @@ #include #endif =20 -/* HVFSlot flags */ -#define HVF_SLOT_LOG (1 << 0) -#define HVF_SLOT_READONLY (1 << 1) - -typedef struct HVFSlot { - hwaddr start; - hwaddr size; /* 0 if the slot is free */ - hwaddr offset; /* offset within memory region */ - uint32_t flags; - MemoryRegion *region; -} HVFSlot; - typedef struct hvf_vcpu_caps { uint64_t vmx_cap_pinbased; uint64_t vmx_cap_procbased; @@ -58,11 +46,11 @@ int hvf_arch_init(void); int hvf_arch_init_vcpu(CPUState *cpu); void hvf_arch_vcpu_destroy(CPUState *cpu); int hvf_vcpu_exec(CPUState *); -HVFSlot *hvf_find_overlap_slot(hwaddr, hwaddr); int hvf_put_registers(CPUState *); int hvf_get_registers(CPUState *); void hvf_kick_vcpu_thread(CPUState *cpu); =20 +bool hvf_access_memory(hwaddr address, bool write); void hvf_init_memslots(void); =20 #endif diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c index 4d4ddab348..398ad50a29 100644 --- a/target/arm/hvf/hvf.c +++ b/target/arm/hvf/hvf.c @@ -1202,6 +1202,11 @@ int hvf_vcpu_exec(CPUState *cpu) break; } =20 + if (iswrite && + hvf_access_memory(hvf_exit->exception.physical_address, 1)) { + break; + } + assert(isv); =20 if (iswrite) { diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c index 2ddb4fc825..c4c544dc54 100644 --- a/target/i386/hvf/hvf.c +++ b/target/i386/hvf/hvf.c @@ -113,7 +113,7 @@ void hvf_handle_io(CPUArchState *env, uint16_t port, vo= id *buffer, } } =20 -static bool ept_emulation_fault(HVFSlot *slot, uint64_t gpa, uint64_t ept_= qual) +static bool ept_emulation_fault(uint64_t gpa, uint64_t ept_qual) { int read, write; =20 @@ -129,14 +129,6 @@ static bool ept_emulation_fault(HVFSlot *slot, uint64_= t gpa, uint64_t ept_qual) return false; } =20 - if (write && slot) { - if (slot->flags & HVF_SLOT_LOG) { - memory_region_set_dirty(slot->region, gpa - slot->start, 1); - hv_vm_protect((hv_gpaddr_t)slot->start, (size_t)slot->size, - HV_MEMORY_READ | HV_MEMORY_WRITE); - } - } - /* * The EPT violation must have been caused by accessing a * guest-physical address that is a translation of a guest-linear @@ -147,14 +139,11 @@ static bool ept_emulation_fault(HVFSlot *slot, uint64= _t gpa, uint64_t ept_qual) return false; } =20 - if (!slot) { - return true; + if (hvf_access_memory(gpa, write)) { + return false; } - if (!memory_region_is_ram(slot->region) && - !(read && memory_region_is_romd(slot->region))) { - return true; - } - return false; + + return true; } =20 void hvf_arch_vcpu_destroy(CPUState *cpu) @@ -469,7 +458,6 @@ int hvf_vcpu_exec(CPUState *cpu) /* Need to check if MMIO or unmapped fault */ case EXIT_REASON_EPT_FAULT: { - HVFSlot *slot; uint64_t gpa =3D rvmcs(cpu->hvf->fd, VMCS_GUEST_PHYSICAL_ADDRE= SS); =20 if (((idtvec_info & VMCS_IDT_VEC_VALID) =3D=3D 0) && @@ -477,9 +465,8 @@ int hvf_vcpu_exec(CPUState *cpu) vmx_set_nmi_blocking(cpu); } =20 - slot =3D hvf_find_overlap_slot(gpa, 1); /* mmio */ - if (ept_emulation_fault(slot, gpa, exit_qual)) { + if (ept_emulation_fault(gpa, exit_qual)) { struct x86_decode decode; =20 load_regs(cpu); --=20 2.32.0 (Apple Git-132) From nobody Fri Nov 15 00:25:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646229439531893.0933946514798; Wed, 2 Mar 2022 05:57:19 -0800 (PST) Received: from localhost ([::1]:38138 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPPTi-00035A-FG for importer@patchew.org; Wed, 02 Mar 2022 08:57:18 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57466) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPOes-0003cI-2I for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:47 -0500 Received: from [2607:f8b0:4864:20::1032] (port=43737 helo=mail-pj1-x1032.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPOeq-0004zM-6b for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:45 -0500 Received: by mail-pj1-x1032.google.com with SMTP id z12-20020a17090ad78c00b001bf022b69d6so606001pju.2 for ; Wed, 02 Mar 2022 05:04:43 -0800 (PST) Received: from localhost.localdomain (2001-b011-e000-59d7-a02b-4f1b-c415-11a0.dynamic-ip6.hinet.net. [2001:b011:e000:59d7:a02b:4f1b:c415:11a0]) by smtp.gmail.com with ESMTPSA id l13-20020a056a00140d00b004e13da93eaasm21947964pfu.62.2022.03.02.05.04.40 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Mar 2022 05:04:42 -0800 (PST) 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=NNU3kDNYeQnPNa4auhryDFqI7kD27cCHuRDqOJvwXMA=; b=C2gMvY1+/nJLvMHmnnPPqSrKyDCHc6J/vul/b+dE98EpkcNqUykZg9sdUsyVzhVR6U 8ftNCUENouBkNMqhmuI4Z8TK36OqGAkHdVRnagFRMNiuOt8A5QDWk7siyJnpIHfNnpe7 5B86G4avh8J4+HEUrMsBzILD2emFKX4YyOaDrlaf90cbrFacxk1WPt5GYcw80HoekGMD dMNYsy/R7o2QMisxWDXxfrKWMPSco5sPTs0x03OMa+23aQRZZLcdfA66c4U6NLPxlfa8 9UAlI7xWL69kpprel+kBLVIIXLUUZRrTiGZ8kp5ak0wJV09hUbFFiqTt4VWp5MHK4++5 KHRA== 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=NNU3kDNYeQnPNa4auhryDFqI7kD27cCHuRDqOJvwXMA=; b=3/MjQKxCetcqjLPDsSxf0FPgRoqDK5C16yFOH+Zq+epWJlms5n6u7/uB66VPq81GLc wSrrnIQSyQnB/8ALoL2WbDaXwJcw0NE/nNMgaDCaBr+DVfBoF5zdFkaj5JZb4IQpIDEj A1DoPyvmCbZW4dAerbdgHbsRobZlHq6k4O1RHhFgvxei/EekHQhcSE5qfYHQPVwXDrAK AWNAFilvGkkFHUF7LEkgbCOKdQkUfudqucZvN/l837vgc2gV0zlxWWgLszap8Mkz0dda /YpNsqCgDIedoHG86JfXVuJ2iTI7Do6r3m5reKKca7sxbQwX8XMx4pcn7FmYuaHeiH8T wTUQ== X-Gm-Message-State: AOAM531mIBtxfDlpMJL5O9y1ANTXmCUxF3mW9CTaE3bsenCCDbgeQlCk F/gzXhiVSRqaC+EwW/GtUK369UpkD2diUg== X-Google-Smtp-Source: ABdhPJzuW0MNUfTRHBsfWoxGZ0RWKYWJMXhyRBhFnAm5CSrxLkDEN4N+K40UlP3UBOJg6jCixjSZwA== X-Received: by 2002:a17:90a:f8c:b0:1bc:cf23:2319 with SMTP id 12-20020a17090a0f8c00b001bccf232319mr27173428pjz.67.1646226282486; Wed, 02 Mar 2022 05:04:42 -0800 (PST) From: Yan-Jie Wang To: qemu-devel@nongnu.org Subject: [PATCH v3 6/9] hvf: add a lock for memory related functions Date: Wed, 2 Mar 2022 21:04:14 +0800 Message-Id: <20220302130417.18551-7-ubzeme@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220302130417.18551-1-ubzeme@gmail.com> References: <20220302130417.18551-1-ubzeme@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::1032 (failed) 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::1032; envelope-from=ubzeme@gmail.com; helo=mail-pj1-x1032.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Cc: Peter Maydell , Roman Bolshakov , Alexander Graf , Cameron Esfahani , Yan-Jie Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646229441807100001 Content-Type: text/plain; charset="utf-8" We follow how KVM accel does in its memory listener (kvm-all.c) and add a lock for the memory related functions. Signed-off-by: Yan-Jie Wang --- accel/hvf/hvf-mem.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c index 896e718374..081029ba98 100644 --- a/accel/hvf/hvf-mem.c +++ b/accel/hvf/hvf-mem.c @@ -43,6 +43,7 @@ typedef struct HVFSlot { } HVFSlot; =20 static HVFSlot memslots[HVF_NUM_SLOTS]; +static QemuMutex memlock; =20 static HVFSlot *hvf_find_overlap_slot(hwaddr start, hwaddr size) { @@ -140,6 +141,8 @@ static void hvf_set_phys_mem(MemoryRegionSection *secti= on, bool add) readonly =3D memory_region_is_rom(area) || memory_region_is_romd(a= rea); =20 /* setup a slot */ + qemu_mutex_lock(&memlock); + slot =3D hvf_find_free_slot(); if (!slot) { error_report("No free slots"); @@ -169,8 +172,12 @@ static void hvf_set_phys_mem(MemoryRegionSection *sect= ion, bool add) =20 ret =3D hv_vm_map(host_addr, start, size, flags); assert_hvf_ok(ret); + + qemu_mutex_unlock(&memlock); } else { /* remove memory region */ + qemu_mutex_lock(&memlock); + slot =3D hvf_find_overlap_slot(start, size); =20 if (slot) { @@ -179,6 +186,8 @@ static void hvf_set_phys_mem(MemoryRegionSection *secti= on, bool add) =20 slot->size =3D 0; } + + qemu_mutex_unlock(&memlock); } } =20 @@ -186,6 +195,8 @@ static void hvf_set_dirty_tracking(MemoryRegionSection = *section, bool on) { HVFSlot *slot; =20 + qemu_mutex_lock(&memlock); + slot =3D hvf_find_overlap_slot( section->offset_within_address_space, int128_get64(section->size)); @@ -201,6 +212,8 @@ static void hvf_set_dirty_tracking(MemoryRegionSection = *section, bool on) hv_vm_protect((uintptr_t)slot->start, (size_t)slot->size, HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC); } + + qemu_mutex_unlock(&memlock); } =20 static void hvf_log_start(MemoryListener *listener, @@ -271,10 +284,13 @@ bool hvf_access_memory(hwaddr address, bool write) hv_return_t ret; hwaddr start, size; =20 + qemu_mutex_lock(&memlock); + slot =3D hvf_find_overlap_slot(address, 1); =20 if (!slot || (write && slot->flags & HVF_SLOT_READONLY)) { /* MMIO or unmapped area, return false */ + qemu_mutex_unlock(&memlock); return false; } =20 @@ -290,10 +306,12 @@ bool hvf_access_memory(hwaddr address, bool write) assert_hvf_ok(ret); } =20 + qemu_mutex_unlock(&memlock); return true; } =20 void hvf_init_memslots(void) { + qemu_mutex_init(&memlock); memory_listener_register(&hvf_memory_listener, &address_space_memory); } --=20 2.32.0 (Apple Git-132) From nobody Fri Nov 15 00:25:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646227377687434.24729669726946; Wed, 2 Mar 2022 05:22:57 -0800 (PST) Received: from localhost ([::1]:51998 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPOwS-0003PE-Ew for importer@patchew.org; Wed, 02 Mar 2022 08:22:56 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57490) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPOet-0003eX-Vq for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:49 -0500 Received: from [2607:f8b0:4864:20::42c] (port=36403 helo=mail-pf1-x42c.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPOes-0004zX-EQ for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:47 -0500 Received: by mail-pf1-x42c.google.com with SMTP id z16so1918498pfh.3 for ; Wed, 02 Mar 2022 05:04:45 -0800 (PST) Received: from localhost.localdomain (2001-b011-e000-59d7-a02b-4f1b-c415-11a0.dynamic-ip6.hinet.net. [2001:b011:e000:59d7:a02b:4f1b:c415:11a0]) by smtp.gmail.com with ESMTPSA id l13-20020a056a00140d00b004e13da93eaasm21947964pfu.62.2022.03.02.05.04.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Mar 2022 05:04:44 -0800 (PST) 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=gV9Cvlo7ft4KghRJ8ZPgxvTZ4fIVH8yA6Q+ea3wVuzI=; b=hoLCZtxpvRqCQc1lggmmuOcsy7rscV7eWzbFit5SpckJgvBL3859Hvql0wGc/wQifT gHWliqynQ/d6w8OQQi4HYsebB0iw9eP6UaXBJi4PkGxQ8N89Xm7495U13Cffkn/69SnP ERSS9ue9Mb1kewmWQQePMrZjALNil2FgnbNNocIufVChlHK+VCIhBI1p344qNAEYInop vJNLEgNQFKCN8rwv0WXZQUDvHmEkCEhn3wOOet/Ouht0EJhIvCUtTF1DXoE9oa8J61ko GOqjEdXSI7ijk6ctCfjvn2unzU9ovGCexwNDOalKVKDd7i2PZUif/6T7MhpYfA1ziiph Wzyw== 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=gV9Cvlo7ft4KghRJ8ZPgxvTZ4fIVH8yA6Q+ea3wVuzI=; b=bvCiQ9SWw2Iq+HKyWl3ilhBJ55gQK0pNkb6bXDuX8HtPORjaydH8Y1UsdPMFso1Ktl wKHgjCsQTkcKKP5sruhVYd18G21XOdig6fnKmWjt55RdbmsdTGSsdLWg/jglYrTqkOsy ci5sblCq8WMJ365+cwTzE0vlVMWtbhxgnv9v34siGJejJVrmydSYuU+JrrqdVlE2xLps 2LhNNsrGaOeKjlw1jwxrOk27InsqnT2482fnDUsnYsWsn3FpZBI/4s01WKAiGWg0xPus ESL/apRNMH/ZjUhIPBRFyrrfIceUypT8hCQO4bbYoyIGKNP1HrCibmBzh3RLXrxg3+SS sN/Q== X-Gm-Message-State: AOAM530GQ3R/A9vpn7xp/VwWA5zQB51X+velEUbkJumG+N9a1OU8HOEb 3ihs4ryNZFoQNptS9iFDsCaNOiVKJt6jZQ== X-Google-Smtp-Source: ABdhPJx4uBsLIPWwG5IX9pRwq5fYlszspY/vY5MaIIM5PuKp/CK53dhC4MK2SJu5tlBYATqc/Ha2EA== X-Received: by 2002:a05:6a00:b92:b0:4f1:4b2:737f with SMTP id g18-20020a056a000b9200b004f104b2737fmr33070701pfj.31.1646226284529; Wed, 02 Mar 2022 05:04:44 -0800 (PST) From: Yan-Jie Wang To: qemu-devel@nongnu.org Subject: [PATCH v3 7/9] hvf: use GTree to store memory slots instead of fixed-size array Date: Wed, 2 Mar 2022 21:04:15 +0800 Message-Id: <20220302130417.18551-8-ubzeme@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220302130417.18551-1-ubzeme@gmail.com> References: <20220302130417.18551-1-ubzeme@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::42c (failed) 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::42c; envelope-from=ubzeme@gmail.com; helo=mail-pf1-x42c.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Cc: Peter Maydell , Roman Bolshakov , Alexander Graf , Cameron Esfahani , Yan-Jie Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646227378451100001 Content-Type: text/plain; charset="utf-8" Currently, there are only 32 memory slots in the fixed size array. It is not scalable. Instead of using fixed size array, use GTree (from glib library) and dynamically-allocated structures to store memory slots. Signed-off-by: Yan-Jie Wang --- accel/hvf/hvf-mem.c | 63 +++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c index 081029ba98..2f70ceb307 100644 --- a/accel/hvf/hvf-mem.c +++ b/accel/hvf/hvf-mem.c @@ -28,8 +28,6 @@ =20 /* Memory slots */ =20 -#define HVF_NUM_SLOTS 32 - /* HVFSlot flags */ #define HVF_SLOT_LOG (1 << 0) #define HVF_SLOT_READONLY (1 << 1) @@ -42,35 +40,24 @@ typedef struct HVFSlot { MemoryRegion *region; } HVFSlot; =20 -static HVFSlot memslots[HVF_NUM_SLOTS]; +static GTree *memslots; static QemuMutex memlock; =20 static HVFSlot *hvf_find_overlap_slot(hwaddr start, hwaddr size) { - HVFSlot *slot; - int x; - for (x =3D 0; x < HVF_NUM_SLOTS; ++x) { - slot =3D &memslots[x]; - if (slot->size && start < (slot->start + slot->size) && - (start + size) > slot->start) { - return slot; - } - } - return NULL; + HVFSlot key =3D {.start =3D start, .size =3D 1}; + return g_tree_lookup(memslots, &key); } =20 -static HVFSlot *hvf_find_free_slot(void) +static void hvf_insert_slot(HVFSlot *slot) { - HVFSlot *slot; - int x; - for (x =3D 0; x < HVF_NUM_SLOTS; x++) { - slot =3D &memslots[x]; - if (!slot->size) { - return slot; - } - } + g_tree_insert(memslots, slot, slot); +} =20 - return NULL; +static bool hvf_remove_slot(hwaddr start) +{ + HVFSlot key =3D {.start =3D start, .size =3D 1}; + return g_tree_remove(memslots, &key); } =20 /* @@ -141,9 +128,7 @@ static void hvf_set_phys_mem(MemoryRegionSection *secti= on, bool add) readonly =3D memory_region_is_rom(area) || memory_region_is_romd(a= rea); =20 /* setup a slot */ - qemu_mutex_lock(&memlock); - - slot =3D hvf_find_free_slot(); + slot =3D g_new0(HVFSlot, 1); if (!slot) { error_report("No free slots"); abort(); @@ -170,6 +155,10 @@ static void hvf_set_phys_mem(MemoryRegionSection *sect= ion, bool add) flags =3D HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC; } =20 + qemu_mutex_lock(&memlock); + + hvf_insert_slot(slot); + ret =3D hv_vm_map(host_addr, start, size, flags); assert_hvf_ok(ret); =20 @@ -178,13 +167,9 @@ static void hvf_set_phys_mem(MemoryRegionSection *sect= ion, bool add) /* remove memory region */ qemu_mutex_lock(&memlock); =20 - slot =3D hvf_find_overlap_slot(start, size); - - if (slot) { + if (hvf_remove_slot(start)) { ret =3D hv_vm_unmap(start, size); assert_hvf_ok(ret); - - slot->size =3D 0; } =20 qemu_mutex_unlock(&memlock); @@ -310,8 +295,24 @@ bool hvf_access_memory(hwaddr address, bool write) return true; } =20 +/* compare function for GTree */ +static gint _hvf_slot_compare(gconstpointer a, gconstpointer b, gpointer d= ata) +{ + const HVFSlot *m1 =3D (const HVFSlot *)a; + const HVFSlot *m2 =3D (const HVFSlot *)b; + + if (m2->start >=3D m1->start + m1->size) { + return -1; + } else if (m1->start >=3D m2->start + m2->size) { + return 1; + } + + return 0; +} + void hvf_init_memslots(void) { qemu_mutex_init(&memlock); + memslots =3D g_tree_new_full(_hvf_slot_compare, NULL, g_free, NULL); memory_listener_register(&hvf_memory_listener, &address_space_memory); } --=20 2.32.0 (Apple Git-132) From nobody Fri Nov 15 00:25:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646229277157388.8145603501076; Wed, 2 Mar 2022 05:54:37 -0800 (PST) Received: from localhost ([::1]:59066 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPPR6-00065k-Cs for importer@patchew.org; Wed, 02 Mar 2022 08:54:36 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57528) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPOev-0003en-TD for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:51 -0500 Received: from [2607:f8b0:4864:20::52e] (port=35702 helo=mail-pg1-x52e.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPOeu-000503-6l for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:49 -0500 Received: by mail-pg1-x52e.google.com with SMTP id e6so1588587pgn.2 for ; Wed, 02 Mar 2022 05:04:47 -0800 (PST) Received: from localhost.localdomain (2001-b011-e000-59d7-a02b-4f1b-c415-11a0.dynamic-ip6.hinet.net. [2001:b011:e000:59d7:a02b:4f1b:c415:11a0]) by smtp.gmail.com with ESMTPSA id l13-20020a056a00140d00b004e13da93eaasm21947964pfu.62.2022.03.02.05.04.44 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Mar 2022 05:04:46 -0800 (PST) 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=w6vJJZnIY85L3jAtHIirBP3LY7sd1XHw2yIyuchY+w0=; b=fYRPxodlkAoc0tMjCk+LMJ5U+vB0E4ZCSCW6UQptyLocpumLf0smMgX53GG3bgz8ev Vr58qG4dwJ5EuQD5uOb2hCTCoRYMsBPDVnf8zMnY0MoTlBMy6btpoPhgqxqalpbk3RME Q7iKOhK5105v/wty7oZxvaVN89jC0dgdm8rKrxtQrr82xbfp4HdQgAJfVn0X6jAv5O39 69X1+foQumGaVUk2EdQIcWZivAN+8oBKyCMo+9sLdEuFLJhMWkWUPNIhzQ8LIJE+ms6c z+2pc2upnoCUbuJMDXU5YfDCjgkgIlWbvYpxhDyvhWaNlqLjhRl+VrRh7OE+jtBfcTSY wEpw== 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=w6vJJZnIY85L3jAtHIirBP3LY7sd1XHw2yIyuchY+w0=; b=IhNDtslVjXQ3RiYhzIpqxurWAUFNj1o55sMkzmoM0jFYA5Sn3t5r2Es93sMVOZq9Ve 6GPDcHHCdSEPeh7k2ZKl32qJFnaOaVXLfBjd6+oOm7LqfX6hqnaSw8QpanPLonyREaky 726iTQhNLUT3rNcotfKviUKxsP3g9ViNUm9OXzhgqNmNFSzcGx6QdlbUc9fDYR0dxkPS Nr9KjnaXWmLm9yRXVyCnWRi0QL6s3mwHsgPpMVxDCVYJoO3YS3RNvgfugdwYOofJs9ka BYj7T87knWAINrbaj1qVujHKWjoJxgTQ3L7gkyOcrc5leywWefFxHa80LVmF6A3Tzaxh MsEA== X-Gm-Message-State: AOAM532c8k4fVZE2IJTdr0MX1J9NJMCNlkyw6u1WKSwazUVLSekCHlol kmk4zrGkzA4DZNYkK75DAk9Q34LeywxmHQ== X-Google-Smtp-Source: ABdhPJzbAVbsLwKlHELq6e1aq/H8F1nximBK77026wR86Yh+YV/6DDXSM/8VeOleoNnW06u1kX3Vwg== X-Received: by 2002:a05:6a00:140b:b0:4e1:2cbd:30ba with SMTP id l11-20020a056a00140b00b004e12cbd30bamr33012871pfu.46.1646226286584; Wed, 02 Mar 2022 05:04:46 -0800 (PST) From: Yan-Jie Wang To: qemu-devel@nongnu.org Subject: [PATCH v3 8/9] hvf: only consider directly writeable memory regions for dirty-tracking Date: Wed, 2 Mar 2022 21:04:16 +0800 Message-Id: <20220302130417.18551-9-ubzeme@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220302130417.18551-1-ubzeme@gmail.com> References: <20220302130417.18551-1-ubzeme@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::52e (failed) 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::52e; envelope-from=ubzeme@gmail.com; helo=mail-pg1-x52e.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Cc: Peter Maydell , Roman Bolshakov , Alexander Graf , Cameron Esfahani , Yan-Jie Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646229301349100001 Content-Type: text/plain; charset="utf-8" It is no need to dirty-track MMIO regions or other readonly regions. Before we start or stop to dirty-track a memory region, check the type of the memory region. The region must be a writeable ram to be dirty-tracked. Signed-off-by: Yan-Jie Wang --- accel/hvf/hvf-mem.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c index 2f70ceb307..60ece20eb4 100644 --- a/accel/hvf/hvf-mem.c +++ b/accel/hvf/hvf-mem.c @@ -180,6 +180,12 @@ static void hvf_set_dirty_tracking(MemoryRegionSection= *section, bool on) { HVFSlot *slot; =20 + if (!memory_region_is_ram(section->mr) || + memory_region_is_rom(section->mr)) { + /* do not consider memory regions which are not directly writeable= */ + return; + } + qemu_mutex_lock(&memlock); =20 slot =3D hvf_find_overlap_slot( --=20 2.32.0 (Apple Git-132) From nobody Fri Nov 15 00:25:55 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646230884971139.227373751374; Wed, 2 Mar 2022 06:21:24 -0800 (PST) Received: from localhost ([::1]:58384 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPPr1-00052n-Tw for importer@patchew.org; Wed, 02 Mar 2022 09:21:23 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57564) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPOey-0003gh-GS for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:53 -0500 Received: from [2607:f8b0:4864:20::52f] (port=46930 helo=mail-pg1-x52f.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPOew-00050V-V4 for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:52 -0500 Received: by mail-pg1-x52f.google.com with SMTP id o23so1553673pgk.13 for ; Wed, 02 Mar 2022 05:04:50 -0800 (PST) Received: from localhost.localdomain (2001-b011-e000-59d7-a02b-4f1b-c415-11a0.dynamic-ip6.hinet.net. [2001:b011:e000:59d7:a02b:4f1b:c415:11a0]) by smtp.gmail.com with ESMTPSA id l13-20020a056a00140d00b004e13da93eaasm21947964pfu.62.2022.03.02.05.04.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Mar 2022 05:04:48 -0800 (PST) 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=5EctREAQDjoYUQJkwsc8J7xA5hFqnlwKFFaMCNutUV4=; b=cCBgxVuszDqhq5Da+mLq+eZuoqXHCONWWcTlvYj3yCaS6v5DJEm4XGYpENBWnlJGpw 3UQW3UlI29LlHB+myr1mYmy5T00YrGGCmuL70QZQuCOp19wO10RzKGMSDY9vqzZV5wsy 0dNKtjGhFBrTAgoVq907pifzD9qAskaAQCkxkisTITNJhpmw2tMAT7nhUnG/JRp1ujlT ca20R9a+c9nviaDh8ZbCL8GtDt3apEoUTM4QS3OZPrp1P7qffXp/ckVWump/JGo33L8V Dutq3vXk9aQis3CHpwi4h7amjVj4bOUXAPubYcsm52trnf8WXCWZWv48RctUKdsmrgNz 0z3Q== 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=5EctREAQDjoYUQJkwsc8J7xA5hFqnlwKFFaMCNutUV4=; b=w1U0IgfdTrdVCecN382PJ3fT3e/jlUIntNDv5mPUGf/r124m2I+qAfjjsJG0hUxRrN hebt4cbYCNyjY0wRVpR+CcFS82XULk+AshuVPar5qLwf5RjY3r8q6bpmqIpKT+3tYMqK 1/67cGXHRXgJ1pqXTxFh0AYFipjWFJAeBNEKvOvdpANT+sI2IMGYmZbM5d7HJmzGZkQ6 ZdUQHpucdE1cW7mk81eKhruq3E2Ba7Ir8zQ7QYoqxOlV/W/eRkam2PbJNi+M2BXigOjM Q5ctkKQ+NRK80ET9y/0CLDIjT1PaWP2BYunsWHpiwW7K+IhEM/2v45RqKHphYovidpCt CVeQ== X-Gm-Message-State: AOAM533FigBj2Ri2K6adzpQvCXYhozmRS7ums1Fz1oeoiY8DKrTIYrta 6Y3/zJzLNfJL0qCG96ytwAKnOsoU737vIw== X-Google-Smtp-Source: ABdhPJxmgR8F+aU7ss85VNykQZaLkvTSFZlI3NSelRYyfMdTnX4z7s9KVpfJcZdMESA5cgYh/dZRxg== X-Received: by 2002:a05:6a00:1d8a:b0:4e1:559d:2f62 with SMTP id z10-20020a056a001d8a00b004e1559d2f62mr33316326pfw.26.1646226289038; Wed, 02 Mar 2022 05:04:49 -0800 (PST) From: Yan-Jie Wang To: qemu-devel@nongnu.org Subject: [PATCH v3 9/9] hvf: remove the need to lookup memory slots when clearing dirty-bits Date: Wed, 2 Mar 2022 21:04:17 +0800 Message-Id: <20220302130417.18551-10-ubzeme@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220302130417.18551-1-ubzeme@gmail.com> References: <20220302130417.18551-1-ubzeme@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::52f (failed) 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::52f; envelope-from=ubzeme@gmail.com; helo=mail-pg1-x52f.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Cc: Peter Maydell , Roman Bolshakov , Alexander Graf , Cameron Esfahani , Yan-Jie Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646230886729100001 Content-Type: text/plain; charset="utf-8" Originally, when log_clear gets called, log_clear calls hvf_set_dirty_tracking to write-protect memory slots whose dirty-bits are cleared. Calling hvf_set_dirty_tracking means that memory slots will be look up and the lock for memory slots will be held during the call. We can use the parameter `section` passed by the caller to determine the pages that need to be write-protected. Compared to the original method, this saves time. Moreover, this makes only pages whose dirty-bits are cleared write-protected instead of making the whole memory slot write-protected. Signed-off-by: Yan-Jie Wang --- accel/hvf/hvf-mem.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c index 60ece20eb4..47d23faec8 100644 --- a/accel/hvf/hvf-mem.c +++ b/accel/hvf/hvf-mem.c @@ -84,7 +84,10 @@ static hwaddr hvf_align_section(MemoryRegionSection *sec= tion, size =3D (size - _delta) & qemu_real_host_page_mask; =20 *start =3D _start; - *delta =3D _delta; + + if (delta) { + *delta =3D _delta; + } =20 return size; } @@ -230,11 +233,27 @@ static void hvf_log_stop(MemoryListener *listener, static void hvf_log_clear(MemoryListener *listener, MemoryRegionSection *section) { + hwaddr start, size; + + if (!memory_region_is_ram(section->mr) || + memory_region_is_rom(section->mr)) { + /* do not consider memory regions which are not directly writeable= */ + return; + } + + if (!memory_region_get_dirty_log_mask(section->mr)) { + /* the region is not being dirty-tracked */ + return; + } + /* * The dirty bits are being cleared. * Make the section write-protected again. */ - hvf_set_dirty_tracking(section, 1); + size =3D hvf_align_section(section, &start, NULL); + if (size) { + hv_vm_protect(start, size, HV_MEMORY_READ | HV_MEMORY_EXEC); + } } =20 static void hvf_region_add(MemoryListener *listener, --=20 2.32.0 (Apple Git-132)