From nobody Wed Nov 12 23:21:16 2025 Delivered-To: importer@patchew.org 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; 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 ARC-Seal: i=1; a=rsa-sha256; t=1574626282; cv=none; d=zohomail.com; s=zohoarc; b=CncnF0H1IeoEQ0alVuj2EssabbuK40b4X1AsRa2K9yOuRjonvLhbV3nlPS354fnrAklTuu2zFGTblTP3pSqJRyYoxdEcNTpI45g5GueIaOzgSIZbsOBw5QQe/52JTX5UtHAIiQrGktPlsaEzngP57zsNcRg5txAbU2JoZm/i7Bg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574626282; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=190VOV5d/wVUGjW4hRluqOG2h2O9F6SznXz4SwcJ7rA=; b=JQCJ52yFQKPgebb4oysEpL9z4Wz+5uKQKu2RM3OHyskEHxB4Fbd8QBA6AtVzBZVFTR08ltze2TH8+Yg44/qFEbvQxrEY7R079f+7E0Jtl+kmcAUm1xMPMwQTVGmA8YlAT+Sa1ns17nudNj4Ch4QMTgG3wRz5x6coeK90I3oOotw= ARC-Authentication-Results: i=1; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574626282138342.39642656748754; Sun, 24 Nov 2019 12:11:22 -0800 (PST) Received: from localhost ([::1]:38576 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYyE5-0005U1-06 for importer@patchew.org; Sun, 24 Nov 2019 15:11:21 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:43997) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iYy8S-0006rr-Vz for qemu-devel@nongnu.org; Sun, 24 Nov 2019 15:05:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iYy8R-0007vI-L7 for qemu-devel@nongnu.org; Sun, 24 Nov 2019 15:05:32 -0500 Received: from ma1-aaemail-dr-lapp02.apple.com ([17.171.2.68]:53909) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iYy8R-0007ua-F1 for qemu-devel@nongnu.org; Sun, 24 Nov 2019 15:05:31 -0500 Received: from pps.filterd (ma1-aaemail-dr-lapp02.apple.com [127.0.0.1]) by ma1-aaemail-dr-lapp02.apple.com (8.16.0.27/8.16.0.27) with SMTP id xAOK1tPU034870; Sun, 24 Nov 2019 12:05:30 -0800 Received: from mr2-mtap-s03.rno.apple.com (mr2-mtap-s03.rno.apple.com [17.179.226.135]) by ma1-aaemail-dr-lapp02.apple.com with ESMTP id 2wf2aw5bw0-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO); Sun, 24 Nov 2019 12:05:29 -0800 Received: from nwk-mmpp-sz10.apple.com (nwk-mmpp-sz10.apple.com [17.128.115.122]) by mr2-mtap-s03.rno.apple.com (Oracle Communications Messaging Server 8.0.2.4.20190507 64bit (built May 7 2019)) with ESMTPS id <0Q1H0042UP55WU20@mr2-mtap-s03.rno.apple.com>; Sun, 24 Nov 2019 12:05:29 -0800 (PST) Received: from process_milters-daemon.nwk-mmpp-sz10.apple.com by nwk-mmpp-sz10.apple.com (Oracle Communications Messaging Server 8.0.2.4.20190507 64bit (built May 7 2019)) id <0Q1H00F00P3BWT00@nwk-mmpp-sz10.apple.com>; Sun, 24 Nov 2019 12:05:29 -0800 (PST) Received: from pontoon.com (unknown [17.234.36.164]) by nwk-mmpp-sz10.apple.com (Oracle Communications Messaging Server 8.0.2.4.20190507 64bit (built May 7 2019)) with ESMTPSA id <0Q1H000C0P54NN30@nwk-mmpp-sz10.apple.com>; Sun, 24 Nov 2019 12:05:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=apple.com; h=sender : from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=20180706; bh=190VOV5d/wVUGjW4hRluqOG2h2O9F6SznXz4SwcJ7rA=; b=jeBGhlrGWttwvi1oKkPi4rl1d57cviDfNYwUyo0Szh6zMDiGtILjIlepgTQnLLRLQqWN hClGlVy3CxFGuTRwRwMzoQ7kdtcsG35fZfez22tZjyAQAGGDcFEuaskWiEFzaSHNIYnB keCpN+qN+zbEvKCeYexmDWZBmfmk7/6svOEit7M8BaZSFIvHVxust08rRhhaipmU3urn MiCCkVD8RkxgA6IDWDmvKMGpAEYNjAVhDA15AQtUByZZiMVMwT/jJ4ik1SIr34pHlhW5 ySi5DJy7elzTyr4NC+3Y+Bg/1one5I10XaHCZU3wVyqYdRPzyy9nff8OzFvmL2nq0HpN vg== X-Va-A: X-Va-T-CD: X-Va-E-CD: X-Va-R-CD: X-Va-CD: 0 X-Va-ID: 45e2f30e-f302-4fb1-921c-e7f47da90177 X-V-A: X-V-T-CD: c56cc4374afe9162bf6a337f3d661529 X-V-E-CD: b8ea6ba6ea75089f272611baa8314f7c X-V-R-CD: 0cffc2e7e4b9540a483226a3303f2f95 X-V-CD: 0 X-V-ID: ab0c7aad-429d-46e4-9d0a-ec6e2f1dcbf7 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-11-24_02:,, signatures=0 To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com Subject: [PATCH v2 1/5] hvf: non-RAM, non-ROMD memory ranges are now correctly mapped in Date: Sun, 24 Nov 2019 12:05:23 -0800 Message-id: <1d8476c8f86959273fbdf23c86f8b4b611f5e2e1.1574625592.git.dirty@apple.com> X-Mailer: git-send-email 2.24.0 In-reply-to: References: MIME-version: 1.0 Content-transfer-encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-11-24_02:, , signatures=0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 17.171.2.68 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 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" Reply-to: Cameron Esfahani From: Cameron Esfahani via X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" If an area is non-RAM and non-ROMD, then remove mappings so accesses will trap and can be emulated. Change hvf_find_overlap_slot() to take a size instead of an end address: it wouldn't return a slot because callers would pass the same address for start and end. Don't always map area as read/write/execute, respect area flags. Signed-off-by: Cameron Esfahani --- target/i386/hvf/hvf.c | 50 ++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c index 231732aaf7..0b50cfcbc6 100644 --- a/target/i386/hvf/hvf.c +++ b/target/i386/hvf/hvf.c @@ -107,14 +107,14 @@ static void assert_hvf_ok(hv_return_t ret) } =20 /* Memory slots */ -hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t end) +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) && - end > slot->start) { + (start + size) > slot->start) { return slot; } } @@ -129,12 +129,10 @@ struct mac_slot { }; =20 struct mac_slot mac_slots[32]; -#define ALIGN(x, y) (((x) + (y) - 1) & ~((y) - 1)) =20 -static int do_hvf_set_memory(hvf_slot *slot) +static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags) { struct mac_slot *macslot; - hv_memory_flags_t flags; hv_return_t ret; =20 macslot =3D &mac_slots[slot->slot_id]; @@ -151,8 +149,6 @@ static int do_hvf_set_memory(hvf_slot *slot) return 0; } =20 - flags =3D HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC; - macslot->present =3D 1; macslot->gpa_start =3D slot->start; macslot->size =3D slot->size; @@ -165,14 +161,24 @@ void hvf_set_phys_mem(MemoryRegionSection *section, b= ool add) { hvf_slot *mem; MemoryRegion *area =3D section->mr; + bool writeable =3D !area->readonly && !area->rom_device; + hv_memory_flags_t flags; =20 if (!memory_region_is_ram(area)) { - return; + 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; + } } =20 mem =3D hvf_find_overlap_slot( section->offset_within_address_space, - section->offset_within_address_space + int128_get64(section->s= ize)); + int128_get64(section->size)); =20 if (mem && add) { if (mem->size =3D=3D int128_get64(section->size) && @@ -186,7 +192,7 @@ void hvf_set_phys_mem(MemoryRegionSection *section, boo= l add) /* 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)) { + if (do_hvf_set_memory(mem, 0)) { error_report("Failed to reset overlapping slot"); abort(); } @@ -196,6 +202,13 @@ void hvf_set_phys_mem(MemoryRegionSection *section, bo= ol add) return; } =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; + } + /* Now make a new slot. */ int x; =20 @@ -216,7 +229,7 @@ void hvf_set_phys_mem(MemoryRegionSection *section, boo= l add) mem->start =3D section->offset_within_address_space; mem->region =3D area; =20 - if (do_hvf_set_memory(mem)) { + if (do_hvf_set_memory(mem, flags)) { error_report("Error registering new memory slot"); abort(); } @@ -345,7 +358,14 @@ static bool ept_emulation_fault(hvf_slot *slot, uint64= _t gpa, uint64_t ept_qual) return false; } =20 - return !slot; + if (!slot) { + return true; + } + if (!memory_region_is_ram(slot->region) && + !(read && memory_region_is_romd(slot->region))) { + return true; + } + return false; } =20 static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on) @@ -354,7 +374,7 @@ static void hvf_set_dirty_tracking(MemoryRegionSection = *section, bool on) =20 slot =3D hvf_find_overlap_slot( section->offset_within_address_space, - section->offset_within_address_space + int128_get64(section->s= ize)); + int128_get64(section->size)); =20 /* protect region against writes; begin tracking it */ if (on) { @@ -720,7 +740,7 @@ int hvf_vcpu_exec(CPUState *cpu) ret =3D EXCP_INTERRUPT; break; } - /* Need to check if MMIO or unmmaped fault */ + /* Need to check if MMIO or unmapped fault */ case EXIT_REASON_EPT_FAULT: { hvf_slot *slot; @@ -731,7 +751,7 @@ int hvf_vcpu_exec(CPUState *cpu) vmx_set_nmi_blocking(cpu); } =20 - slot =3D hvf_find_overlap_slot(gpa, gpa); + slot =3D hvf_find_overlap_slot(gpa, 1); /* mmio */ if (ept_emulation_fault(slot, gpa, exit_qual)) { struct x86_decode decode; --=20 2.24.0