From nobody Sun Dec 14 20:29:15 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1751624357; cv=none; d=zohomail.com; s=zohoarc; b=a2Fx6HDMp76g7IzbrTQn0K0n3VxU8OnQUrkI7pqEsCu5F/2FvsOY3U1FQB8/w+L/a0eMLA1PZHqhOaTbmsVHrMl2//29UtuBfHNKcHX7vzfQ5WKn5EUWtqdSJSWkK3hIJSMa7rLxp/sPCSjCxH1tVj9e5qjUz7a4KRGyXDJuP/4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751624357; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=IXkfdD9TUd+4AfH72kU5It9cVyo8SKVvdnuAYEy/qHo=; b=hKJZLNXoGUAa9USiKSPBuPclanbPCtplx5b2Fo0oPly0XEvWT67/jzRmfv39DhJzOW76RlWGzD1NYRP9LLURbn0giYKuO9RkLzlCNoSaKozwF5JuGE6CdskIjuSxF9H8524m8PmgH4LH5Q7jgjiP56McyhHNoyyCZFZ4bq96JVY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1751624357457581.8836027609058; Fri, 4 Jul 2025 03:19:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uXdSA-0006DV-FY; Fri, 04 Jul 2025 06:15:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uXdS4-0006D4-Bs for qemu-devel@nongnu.org; Fri, 04 Jul 2025 06:15:29 -0400 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uXdS1-0006fR-H6 for qemu-devel@nongnu.org; Fri, 04 Jul 2025 06:15:27 -0400 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-3a51481a598so389700f8f.3 for ; Fri, 04 Jul 2025 03:15:25 -0700 (PDT) Received: from localhost.localdomain (88-187-86-199.subs.proxad.net. [88.187.86.199]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3b471b97f10sm2130940f8f.57.2025.07.04.03.15.21 for (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 04 Jul 2025 03:15:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1751624123; x=1752228923; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=IXkfdD9TUd+4AfH72kU5It9cVyo8SKVvdnuAYEy/qHo=; b=hEy5eyq+mPnpskkefVvxhV95ao7LOgGrD3xCkZ4CqT/yo1cXgc1vLHyU/MK0OywG2F 2zNpdUNjSOgD86g6qOlThvcJ5YSExp5qrcujPzwAtow3hEGXWVB1Z7pEUhP4Tz3x7YXP 2gGLcFGmISw4hcLDIAJOZ0UjkKe2b/q7SHG7YgT6gj6IN2CkXKv3YAI+bnpqSzzkDBRP 6R9cPdCxHfiR0CUbEwV357wir70zQ1MnHC2X17raA8694TpiubepBLNXynA+/ROMAqfZ 9lkmi0LzQSzYpUTBbZrg18qIgXP4uC3nbEKx9UjzHYp0y6UKBCngxyPI8vho1sTkVIqu QP2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751624123; x=1752228923; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IXkfdD9TUd+4AfH72kU5It9cVyo8SKVvdnuAYEy/qHo=; b=JOpTdA9m1C+d7LxP9K+VaqoCo8xHIKw7SgVd0ahirun4EDgEI297zSrF3Kbt8hobUz FeNE4MvHeI2Ao0pPDbn/z5DZGbsbpHFkE2gnqw7Cbq3YezndAVJmOqWt7fhdMvnwv/oW q6rPH32JCwMBwbhxpuGdHArEEBw5//PffCr9SJAsNGTiTDju7QXC2XNT+5+yD7h4XeUn c4dqxD/MExbn/zQmPLG+3citUOgp6BeE2Cf61Qzhzd8sZ24mkB1NRxb3vnLGqXX7FTbD EqebFpJEMbhSmtH4HWhiaNYaQG28cjs7J3O0rPSXGYC2dvPQbsNxYywE2g+htH75wEi2 LEpg== X-Gm-Message-State: AOJu0Yzpkdwv8dcYaZ2BrAGTU/+dqNfNOvDKcX84Wk1wLqbIQrmc0RKI AtnwzZ/Mkp4juUsADo/qUmVx7R+vhEZUftcuH3dBdeYFy70CrAMKDRwwb7u5PmWt8PqCdnyOkyQ F1/z65n0= X-Gm-Gg: ASbGncv8e6PiwPZZt2GPaFB/L8LRqwTc3YlxlYah7sYmCPZmw/T9IXplUJN/rFaFGOC yfLAUJx0PU+bwMtt/w7ifK7fe0gjl5wT2TaKzmOpJkVNG8LkJIh5XvuQAuaoGOJrw3XmS7mXNUn 1m7hDltdClX5ZoH4/Kx5yCH+gyrE9dUCZ8pX2mo3T6fU7Crna6jUojcuqfUcT9Hq0gMoJiLKR2G d/9lTaKHRsW+cEws7I1LhSZhuqP+/WZLfR1neXrUfx+Smw9KB0Zkvf5khGTUeeQaFZ2hm6YDrSs Ad/x6woGqH/nubfykvkQoANZi0/9jSXXOlxe773gQVTYyzcsIbXQam9gsh/FJfqaiysbK9egbH3 YL2Mez3SEzej4lMAuiDg/xyUyMfO+GIuDwwdB X-Google-Smtp-Source: AGHT+IEvga4MUMWI//f63QiPtCRwIkkXmLRDaO+uZ4qA7V/17FyKDbAFLJWWaIVOehh6tEAawgvnSg== X-Received: by 2002:a05:6000:1789:b0:3a4:d0fe:42b2 with SMTP id ffacd0b85a97d-3b49701f78dmr1065371f8f.19.1751624122550; Fri, 04 Jul 2025 03:15:22 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Subject: [PULL 10/31] accel/hvf: Move generic method declarations to hvf-all.c Date: Fri, 4 Jul 2025 12:14:11 +0200 Message-ID: <20250704101433.8813-11-philmd@linaro.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250704101433.8813-1-philmd@linaro.org> References: <20250704101433.8813-1-philmd@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::434; envelope-from=philmd@linaro.org; helo=mail-wr1-x434.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1751624359841116600 hvf-all.c aims to contain the generic accel methods (TYPE_ACCEL), while hvf-accel-ops.c the per-vcpu methods (TYPE_ACCEL_OPS). Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Richard Henderson Reviewed-by: Zhao Liu Message-Id: <20250703173248.44995-17-philmd@linaro.org> --- accel/hvf/hvf-accel-ops.c | 273 +------------------------------------- accel/hvf/hvf-all.c | 267 +++++++++++++++++++++++++++++++++++++ 2 files changed, 271 insertions(+), 269 deletions(-) diff --git a/accel/hvf/hvf-accel-ops.c b/accel/hvf/hvf-accel-ops.c index 3752334688b..2c0715a17f7 100644 --- a/accel/hvf/hvf-accel-ops.c +++ b/accel/hvf/hvf-accel-ops.c @@ -48,20 +48,16 @@ */ =20 #include "qemu/osdep.h" -#include "qemu/error-report.h" +#include "qemu/guest-random.h" #include "qemu/main-loop.h" #include "qemu/queue.h" -#include "system/address-spaces.h" #include "gdbstub/enums.h" -#include "hw/boards.h" +#include "exec/cpu-common.h" #include "hw/core/cpu.h" #include "system/accel-ops.h" #include "system/cpus.h" #include "system/hvf.h" #include "system/hvf_int.h" -#include "system/runstate.h" -#include "qemu/guest-random.h" -#include "trace.h" =20 HVFState *hvf_state; =20 @@ -81,132 +77,6 @@ hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_= 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) -{ - 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; - trace_hvf_vm_unmap(macslot->gpa_start, macslot->size); - 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; - trace_hvf_vm_map(slot->start, slot->size, slot->mem, flags, - flags & HV_MEMORY_READ ? 'R' : '-', - flags & HV_MEMORY_WRITE ? 'W' : '-', - flags & HV_MEMORY_EXEC ? 'E' : '-'); - 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 writable =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 (writable) { - 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->accel->dirty) { @@ -244,147 +114,10 @@ static void hvf_cpu_synchronize_pre_loadvm(CPUState = *cpu) 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 MEMORY_LISTENER_PRIORITY_ACCEL, - .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) { } =20 -bool hvf_allowed; - -static int hvf_accel_init(MachineState *ms) -{ - int x; - hv_return_t ret; - HVFState *s; - int pa_range =3D 36; - MachineClass *mc =3D MACHINE_GET_CLASS(ms); - - if (mc->hvf_get_physical_address_range) { - pa_range =3D mc->hvf_get_physical_address_range(ms); - if (pa_range < 0) { - return -EINVAL; - } - } - - ret =3D hvf_arch_vm_create(ms, (uint32_t)pa_range); - assert_hvf_ok(ret); - - s =3D g_new0(HVFState, 1); - - 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; - } - - QTAILQ_INIT(&s->hvf_sw_breakpoints); - - hvf_state =3D s; - memory_listener_register(&hvf_memory_listener, &address_space_memory); - - return hvf_arch_init(); -} - -static inline int hvf_gdbstub_sstep_flags(void) -{ - return SSTEP_ENABLE | SSTEP_NOIRQ; -} - -static void hvf_accel_class_init(ObjectClass *oc, const void *data) -{ - AccelClass *ac =3D ACCEL_CLASS(oc); - ac->name =3D "HVF"; - ac->init_machine =3D hvf_accel_init; - ac->allowed =3D &hvf_allowed; - ac->gdbstub_supported_sstep_flags =3D hvf_gdbstub_sstep_flags; -} - -static const TypeInfo hvf_accel_type =3D { - .name =3D TYPE_HVF_ACCEL, - .parent =3D TYPE_ACCEL, - .instance_size =3D sizeof(HVFState), - .class_init =3D hvf_accel_class_init, -}; - -static void hvf_type_init(void) -{ - type_register_static(&hvf_accel_type); -} - -type_init(hvf_type_init); - static void hvf_vcpu_destroy(CPUState *cpu) { hv_return_t ret =3D hv_vcpu_destroy(cpu->accel->fd); @@ -639,8 +372,10 @@ static const TypeInfo hvf_accel_ops_type =3D { .class_init =3D hvf_accel_ops_class_init, .abstract =3D true, }; + static void hvf_accel_ops_register_types(void) { type_register_static(&hvf_accel_ops_type); } + type_init(hvf_accel_ops_register_types); diff --git a/accel/hvf/hvf-all.c b/accel/hvf/hvf-all.c index 1c72c43ddb4..ddc77e629f5 100644 --- a/accel/hvf/hvf-all.c +++ b/accel/hvf/hvf-all.c @@ -10,9 +10,24 @@ =20 #include "qemu/osdep.h" #include "qemu/error-report.h" +#include "system/address-spaces.h" +#include "system/memory.h" #include "system/hvf.h" #include "system/hvf_int.h" #include "hw/core/cpu.h" +#include "hw/boards.h" +#include "trace.h" + +bool hvf_allowed; + +struct mac_slot { + int present; + uint64_t size; + uint64_t gpa_start; + uint64_t gva; +}; + +struct mac_slot mac_slots[32]; =20 const char *hvf_return_string(hv_return_t ret) { @@ -41,3 +56,255 @@ void assert_hvf_ok_impl(hv_return_t ret, const char *fi= le, unsigned int line, =20 abort(); } + +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; + trace_hvf_vm_unmap(macslot->gpa_start, macslot->size); + 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; + trace_hvf_vm_map(slot->start, slot->size, slot->mem, flags, + flags & HV_MEMORY_READ ? 'R' : '-', + flags & HV_MEMORY_WRITE ? 'W' : '-', + flags & HV_MEMORY_EXEC ? 'E' : '-'); + 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 writable =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 (writable) { + 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 MEMORY_LISTENER_PRIORITY_ACCEL, + .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 int hvf_accel_init(MachineState *ms) +{ + int x; + hv_return_t ret; + HVFState *s; + int pa_range =3D 36; + MachineClass *mc =3D MACHINE_GET_CLASS(ms); + + if (mc->hvf_get_physical_address_range) { + pa_range =3D mc->hvf_get_physical_address_range(ms); + if (pa_range < 0) { + return -EINVAL; + } + } + + ret =3D hvf_arch_vm_create(ms, (uint32_t)pa_range); + assert_hvf_ok(ret); + + s =3D g_new0(HVFState, 1); + + 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; + } + + QTAILQ_INIT(&s->hvf_sw_breakpoints); + + hvf_state =3D s; + memory_listener_register(&hvf_memory_listener, &address_space_memory); + + return hvf_arch_init(); +} + +static int hvf_gdbstub_sstep_flags(void) +{ + return SSTEP_ENABLE | SSTEP_NOIRQ; +} + +static void hvf_accel_class_init(ObjectClass *oc, const void *data) +{ + AccelClass *ac =3D ACCEL_CLASS(oc); + ac->name =3D "HVF"; + ac->init_machine =3D hvf_accel_init; + ac->allowed =3D &hvf_allowed; + ac->gdbstub_supported_sstep_flags =3D hvf_gdbstub_sstep_flags; +} + +static const TypeInfo hvf_accel_type =3D { + .name =3D TYPE_HVF_ACCEL, + .parent =3D TYPE_ACCEL, + .instance_size =3D sizeof(HVFState), + .class_init =3D hvf_accel_class_init, +}; + +static void hvf_type_init(void) +{ + type_register_static(&hvf_accel_type); +} + +type_init(hvf_type_init); --=20 2.49.0