From nobody Mon Feb 9 01:26:24 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=git.sr.ht Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646057883434500.64423491490834; Mon, 28 Feb 2022 06:18:03 -0800 (PST) Received: from localhost ([::1]:50610 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOgqg-0000jJ-MO for importer@patchew.org; Mon, 28 Feb 2022 09:18:02 -0500 Received: from eggs.gnu.org ([209.51.188.92]:47828) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn7-0001Z0-7J; Mon, 28 Feb 2022 01:45:53 -0500 Received: from mail-b.sr.ht ([173.195.146.151]:58194) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn2-0003IM-Gj; Mon, 28 Feb 2022 01:45:52 -0500 Received: from git.sr.ht (unknown [173.195.146.142]) by mail-b.sr.ht (Postfix) with ESMTPSA id C8D6A11EEFE; Mon, 28 Feb 2022 06:45:45 +0000 (UTC) Authentication-Results: mail-b.sr.ht; dkim=none From: ~ubzeme Date: Mon, 28 Feb 2022 08:41:28 +0800 Subject: [PATCH qemu 1/7] hvf: move memory related functions from hvf-accel-ops.c to hvf-mem.c Message-ID: <164603074537.20094.1732342403585879912-1@git.sr.ht> X-Mailer: git.sr.ht In-Reply-To: <164603074537.20094.1732342403585879912-0@git.sr.ht> To: qemu-devel@nongnu.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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=173.195.146.151; envelope-from=outgoing@sr.ht; helo=mail-b.sr.ht X-Spam_score_int: 17 X-Spam_score: 1.7 X-Spam_bar: + X-Spam_report: (1.7 / 5.0 requ) BAYES_00=-1.9, DATE_IN_PAST_06_12=1.543, FREEMAIL_FORGED_REPLYTO=2.095, 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-Mailman-Approved-At: Mon, 28 Feb 2022 09:07:40 -0500 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: , Reply-To: ~ubzeme Cc: Yan-Jie Wang , qemu-devel@nongnu.org, Cameron Esfahani , Roman Bolshakov , Alexander Graf , qemu-arm@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1646057884380100003 From: Yan-Jie Wang Signed-off-by: Yan-Jie Wang --- 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.34.1 From nobody Mon Feb 9 01:26:24 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=git.sr.ht Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646057492952650.9991978885271; Mon, 28 Feb 2022 06:11:32 -0800 (PST) Received: from localhost ([::1]:35030 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOgkN-0006TQ-SY for importer@patchew.org; Mon, 28 Feb 2022 09:11:31 -0500 Received: from eggs.gnu.org ([209.51.188.92]:47764) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn4-0001Ya-Oj; Mon, 28 Feb 2022 01:45:52 -0500 Received: from mail-b.sr.ht ([173.195.146.151]:58196) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn2-0003IN-E9; Mon, 28 Feb 2022 01:45:50 -0500 Received: from git.sr.ht (unknown [173.195.146.142]) by mail-b.sr.ht (Postfix) with ESMTPSA id E6A5711EF24; Mon, 28 Feb 2022 06:45:45 +0000 (UTC) Authentication-Results: mail-b.sr.ht; dkim=none From: ~ubzeme Date: Mon, 28 Feb 2022 09:07:49 +0800 Subject: [PATCH qemu 2/7] hvf: simplify data structures and codes of memory related functions Message-ID: <164603074537.20094.1732342403585879912-2@git.sr.ht> X-Mailer: git.sr.ht In-Reply-To: <164603074537.20094.1732342403585879912-0@git.sr.ht> To: qemu-devel@nongnu.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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=173.195.146.151; envelope-from=outgoing@sr.ht; helo=mail-b.sr.ht X-Spam_score_int: 18 X-Spam_score: 1.8 X-Spam_bar: + X-Spam_report: (1.8 / 5.0 requ) BAYES_00=-1.9, DATE_IN_PAST_03_06=1.592, FREEMAIL_FORGED_REPLYTO=2.095, 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-Mailman-Approved-At: Mon, 28 Feb 2022 09:07:40 -0500 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: , Reply-To: ~ubzeme Cc: Yan-Jie Wang , qemu-devel@nongnu.org, Cameron Esfahani , Roman Bolshakov , Alexander Graf , qemu-arm@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1646057494662100001 From: Yan-Jie Wang * 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 | 197 +++++++++++++++++++------------------- include/sysemu/hvf_int.h | 8 +- 3 files changed, 100 insertions(+), 106 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..c60303c84e 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; -}; +static hvf_slot *hvf_find_free_slot(void) +{ + hvf_slot *slot; + int x; + for (x =3D 0; x < HVF_NUM_SLOTS; x++) { + slot =3D &memslots[x]; + if (!slot->size) { + return slot; + } + } =20 -struct mac_slot mac_slots[32]; + return slot; +} =20 -static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags) +/* + * 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) { - struct mac_slot *macslot; - hv_return_t ret; - - macslot =3D &mac_slots[slot->slot_id]; + hwaddr unaligned, _start, size, _delta; =20 - 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); - } - } + 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; =20 - if (!slot->size) { - return 0; - } + *start =3D _start; + *delta =3D _delta; =20 - 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 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) { - 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 (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; } =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; + size =3D hvf_align_section(section, &start, &delta); + + if (!size) { + /* The size is 0 after aligned. Do not map the region */ + return; } =20 - mem =3D hvf_find_overlap_slot( - section->offset_within_address_space, - int128_get64(section->size)); + if (add) { + /* add memory region */ + offset =3D section->offset_within_region + delta; + host_addr =3D memory_region_get_ram_ptr(area) + offset; =20 - 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. */ + 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; } - } =20 - /* 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"); + dirty_tracking =3D !!memory_region_get_dirty_log_mask(area); + readonly =3D memory_region_is_rom(area) || memory_region_is_romd(a= rea); + + /* 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; - } else { - flags =3D HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC; - } + if (readonly) { + slot->flags |=3D HVF_SLOT_READONLY; + } =20 - /* Now make a new slot. */ - int x; + if (dirty_tracking) { + slot->flags |=3D HVF_SLOT_LOG; + } =20 - for (x =3D 0; x < hvf_state->num_slots; ++x) { - mem =3D &hvf_state->slots[x]; - if (!mem->size) { - break; + /* 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; } - } =20 - if (x =3D=3D hvf_state->num_slots) { - error_report("No free slots"); - abort(); - } + ret =3D hv_vm_map(host_addr, start, size, flags); + assert_hvf_ok(ret); + } else { + /* remove memory region */ + slot =3D hvf_find_overlap_slot(start, size); =20 - 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 (slot) { + ret =3D hv_vm_unmap(start, size); + assert_hvf_ok(ret); =20 - if (do_hvf_set_memory(mem, flags)) { - error_report("Error registering new memory slot"); - abort(); + slot->size =3D 0; + } } } =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.34.1 From nobody Mon Feb 9 01:26:24 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=git.sr.ht Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646057696430760.1357610909378; Mon, 28 Feb 2022 06:14:56 -0800 (PST) Received: from localhost ([::1]:43162 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOgnf-0003d8-8o for importer@patchew.org; Mon, 28 Feb 2022 09:14:55 -0500 Received: from eggs.gnu.org ([209.51.188.92]:47806) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn6-0001Yd-B4; Mon, 28 Feb 2022 01:45:52 -0500 Received: from mail-b.sr.ht ([173.195.146.151]:58198) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn2-0003IO-LJ; Mon, 28 Feb 2022 01:45:51 -0500 Received: from git.sr.ht (unknown [173.195.146.142]) by mail-b.sr.ht (Postfix) with ESMTPSA id 1120D11EF38; Mon, 28 Feb 2022 06:45:46 +0000 (UTC) Authentication-Results: mail-b.sr.ht; dkim=none From: ~ubzeme Date: Mon, 28 Feb 2022 09:12:14 +0800 Subject: [PATCH qemu 3/7] hvf: use correct data types for addresses in memory related functions Message-ID: <164603074537.20094.1732342403585879912-3@git.sr.ht> X-Mailer: git.sr.ht In-Reply-To: <164603074537.20094.1732342403585879912-0@git.sr.ht> To: qemu-devel@nongnu.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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=173.195.146.151; envelope-from=outgoing@sr.ht; helo=mail-b.sr.ht X-Spam_score_int: 18 X-Spam_score: 1.8 X-Spam_bar: + X-Spam_report: (1.8 / 5.0 requ) BAYES_00=-1.9, DATE_IN_PAST_03_06=1.592, FREEMAIL_FORGED_REPLYTO=2.095, 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-Mailman-Approved-At: Mon, 28 Feb 2022 09:07:40 -0500 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: , Reply-To: ~ubzeme Cc: Yan-Jie Wang , qemu-devel@nongnu.org, Cameron Esfahani , Roman Bolshakov , Alexander Graf , qemu-arm@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1646057697148100001 From: Yan-Jie Wang Follow the QEMU coding style. Use hwaddr for guest physical address. Signed-off-by: Yan-Jie Wang --- 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 c60303c84e..e8cd6b9251 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.34.1 From nobody Mon Feb 9 01:26:24 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=git.sr.ht Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646057722539755.4854910994296; Mon, 28 Feb 2022 06:15:22 -0800 (PST) Received: from localhost ([::1]:43838 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOgo5-00044a-Dn for importer@patchew.org; Mon, 28 Feb 2022 09:15:21 -0500 Received: from eggs.gnu.org ([209.51.188.92]:47816) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn6-0001Ye-Ks; Mon, 28 Feb 2022 01:45:52 -0500 Received: from mail-b.sr.ht ([173.195.146.151]:58200) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn2-0003IP-Lm; Mon, 28 Feb 2022 01:45:51 -0500 Received: from git.sr.ht (unknown [173.195.146.142]) by mail-b.sr.ht (Postfix) with ESMTPSA id 37CDE11EF41; Mon, 28 Feb 2022 06:45:46 +0000 (UTC) Authentication-Results: mail-b.sr.ht; dkim=none From: ~ubzeme Date: Mon, 28 Feb 2022 09:15:36 +0800 Subject: [PATCH qemu 4/7] hvf: rename struct hvf_slot to HVFSlot Message-ID: <164603074537.20094.1732342403585879912-4@git.sr.ht> X-Mailer: git.sr.ht In-Reply-To: <164603074537.20094.1732342403585879912-0@git.sr.ht> To: qemu-devel@nongnu.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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=173.195.146.151; envelope-from=outgoing@sr.ht; helo=mail-b.sr.ht X-Spam_score_int: 18 X-Spam_score: 1.8 X-Spam_bar: + X-Spam_report: (1.8 / 5.0 requ) BAYES_00=-1.9, DATE_IN_PAST_03_06=1.592, FREEMAIL_FORGED_REPLYTO=2.095, 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-Mailman-Approved-At: Mon, 28 Feb 2022 09:07:40 -0500 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: , Reply-To: ~ubzeme Cc: Yan-Jie Wang , qemu-devel@nongnu.org, Cameron Esfahani , Roman Bolshakov , Alexander Graf , qemu-arm@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1646057723136100001 From: Yan-Jie Wang Follow the QEMU coding style. Structured type names are in CamelCase. Signed-off-by: Yan-Jie Wang --- 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 e8cd6b9251..36ae1abc06 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.34.1 From nobody Mon Feb 9 01:26:24 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=git.sr.ht Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646058050750306.0463735650609; Mon, 28 Feb 2022 06:20:50 -0800 (PST) Received: from localhost ([::1]:57156 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOgtN-0005Rb-3M for importer@patchew.org; Mon, 28 Feb 2022 09:20:49 -0500 Received: from eggs.gnu.org ([209.51.188.92]:47838) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn8-0001ZY-8r; Mon, 28 Feb 2022 01:45:54 -0500 Received: from mail-b.sr.ht ([173.195.146.151]:58204) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn5-0003JL-9c; Mon, 28 Feb 2022 01:45:53 -0500 Received: from git.sr.ht (unknown [173.195.146.142]) by mail-b.sr.ht (Postfix) with ESMTPSA id 5478211EF52; Mon, 28 Feb 2022 06:45:46 +0000 (UTC) Authentication-Results: mail-b.sr.ht; dkim=none From: ~ubzeme Date: Mon, 28 Feb 2022 09:28:00 +0800 Subject: [PATCH qemu 5/7] hvf: fix memory dirty-tracking Message-ID: <164603074537.20094.1732342403585879912-5@git.sr.ht> X-Mailer: git.sr.ht In-Reply-To: <164603074537.20094.1732342403585879912-0@git.sr.ht> To: qemu-devel@nongnu.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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=173.195.146.151; envelope-from=outgoing@sr.ht; helo=mail-b.sr.ht X-Spam_score_int: 18 X-Spam_score: 1.8 X-Spam_bar: + X-Spam_report: (1.8 / 5.0 requ) BAYES_00=-1.9, DATE_IN_PAST_03_06=1.592, FREEMAIL_FORGED_REPLYTO=2.095, 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-Mailman-Approved-At: Mon, 28 Feb 2022 09:07:40 -0500 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: , Reply-To: ~ubzeme Cc: Yan-Jie Wang , qemu-devel@nongnu.org, Cameron Esfahani , Roman Bolshakov , Alexander Graf , qemu-arm@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1646058052396100001 From: Yan-Jie Wang 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 36ae1abc06..b8d8bf6799 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 0dc96560d3..e40efbc33e 100644 --- a/target/arm/hvf/hvf.c +++ b/target/arm/hvf/hvf.c @@ -1163,6 +1163,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 (!memory_region_is_ram(slot->region) && - !(read && memory_region_is_romd(slot->region))) { - return true; + if (hvf_access_memory(gpa, write)) { + return false; } - 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.34.1 From nobody Mon Feb 9 01:26:24 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=git.sr.ht Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646057357297967.6563979315097; Mon, 28 Feb 2022 06:09:17 -0800 (PST) Received: from localhost ([::1]:56950 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOgiC-00026m-5e for importer@patchew.org; Mon, 28 Feb 2022 09:09:16 -0500 Received: from eggs.gnu.org ([209.51.188.92]:47826) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn7-0001Yv-6c; Mon, 28 Feb 2022 01:45:53 -0500 Received: from mail-b.sr.ht ([173.195.146.151]:58202) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn5-0003JJ-9c; Mon, 28 Feb 2022 01:45:52 -0500 Received: from git.sr.ht (unknown [173.195.146.142]) by mail-b.sr.ht (Postfix) with ESMTPSA id 70DFD11EF53; Mon, 28 Feb 2022 06:45:46 +0000 (UTC) Authentication-Results: mail-b.sr.ht; dkim=none From: ~ubzeme Date: Mon, 28 Feb 2022 09:41:52 +0800 Subject: [PATCH qemu 6/7] hvf: add a lock for memory related functions Message-ID: <164603074537.20094.1732342403585879912-6@git.sr.ht> X-Mailer: git.sr.ht In-Reply-To: <164603074537.20094.1732342403585879912-0@git.sr.ht> To: qemu-devel@nongnu.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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=173.195.146.151; envelope-from=outgoing@sr.ht; helo=mail-b.sr.ht X-Spam_score_int: 18 X-Spam_score: 1.8 X-Spam_bar: + X-Spam_report: (1.8 / 5.0 requ) BAYES_00=-1.9, DATE_IN_PAST_03_06=1.592, FREEMAIL_FORGED_REPLYTO=2.095, 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-Mailman-Approved-At: Mon, 28 Feb 2022 09:07:40 -0500 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: , Reply-To: ~ubzeme Cc: Yan-Jie Wang , qemu-devel@nongnu.org, Cameron Esfahani , Roman Bolshakov , Alexander Graf , qemu-arm@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1646057360210100003 From: Yan-Jie Wang 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 b8d8bf6799..4948578adf 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.34.1 From nobody Mon Feb 9 01:26:24 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=git.sr.ht Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646058154821535.8475066000402; Mon, 28 Feb 2022 06:22:34 -0800 (PST) Received: from localhost ([::1]:59466 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOgv3-0007Bu-R0 for importer@patchew.org; Mon, 28 Feb 2022 09:22:33 -0500 Received: from eggs.gnu.org ([209.51.188.92]:47842) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn9-0001af-4i; Mon, 28 Feb 2022 01:45:55 -0500 Received: from mail-b.sr.ht ([173.195.146.151]:58206) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOZn6-0003Ji-Ma; Mon, 28 Feb 2022 01:45:54 -0500 Received: from git.sr.ht (unknown [173.195.146.142]) by mail-b.sr.ht (Postfix) with ESMTPSA id 97B9E11EF54; Mon, 28 Feb 2022 06:45:46 +0000 (UTC) Authentication-Results: mail-b.sr.ht; dkim=none From: ~ubzeme Date: Mon, 28 Feb 2022 09:47:25 +0800 Subject: [PATCH qemu 7/7] hvf: use GTree to store memory slots instead of fixed-size array Message-ID: <164603074537.20094.1732342403585879912-7@git.sr.ht> X-Mailer: git.sr.ht In-Reply-To: <164603074537.20094.1732342403585879912-0@git.sr.ht> To: qemu-devel@nongnu.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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=173.195.146.151; envelope-from=outgoing@sr.ht; helo=mail-b.sr.ht X-Spam_score_int: 18 X-Spam_score: 1.8 X-Spam_bar: + X-Spam_report: (1.8 / 5.0 requ) BAYES_00=-1.9, DATE_IN_PAST_03_06=1.592, FREEMAIL_FORGED_REPLYTO=2.095, 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-Mailman-Approved-At: Mon, 28 Feb 2022 09:07:40 -0500 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: , Reply-To: ~ubzeme Cc: Yan-Jie Wang , qemu-devel@nongnu.org, Cameron Esfahani , Roman Bolshakov , Alexander Graf , qemu-arm@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1646058156184100001 From: Yan-Jie Wang 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 4948578adf..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 slot; +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.34.1