From nobody Fri Nov 15 03:20:09 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)