From nobody Sat May 4 08:32:28 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1579259359; cv=none; d=zohomail.com; s=zohoarc; b=WomE6Wp7ov1VfNFyKJTLDA073g2yR80u7Zk46IXuM0alhXeiJGiSvxqKWqJe+mq7qdizIwlkAGUETtKDhRGtaLfGvIWtefarne8UFo+gOfQTz7u0kg3xSQWVfGCEYRT00l+8qvBh++QsuOiTFVoE8NKkOwUD64JaYrfzaw0dgjI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1579259359; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=ZvKSwKpxAJXlKi7lV6Qli8MbYCa5gBVnNbEn+9eXhBk=; b=J+w3PPvs0gAbmCcrJSEpUaGZihA7O3kcSjKDIjFl0P00vz2Eruve24r9fjFnpLGqGOUs64jQwg5p5YW8Vk0YAp9FjEHxXyQxxCuTUsok/k5YZu4b7JhXOjrvr/7m+Ubbjh6zzoE9JKsyqD4+Y8T6EHj4DnZYba6ClUQcX53mKN0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1579259359861735.7150547253074; Fri, 17 Jan 2020 03:09:19 -0800 (PST) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1isPUV-0006y6-Mw; Fri, 17 Jan 2020 11:08:39 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1isPUT-0006xx-Lm for xen-devel@lists.xenproject.org; Fri, 17 Jan 2020 11:08:37 +0000 Received: from esa5.hc3370-68.iphmx.com (unknown [216.71.155.168]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id b0677c32-3919-11ea-9fd7-bc764e2007e4; Fri, 17 Jan 2020 11:08:29 +0000 (UTC) X-Inumbo-ID: b0677c32-3919-11ea-9fd7-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1579259308; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=zRgZwAlgCLlHqcyugkjDfGixPQFRcLT1wcxk5jV01xo=; b=BfIPosqfnNUIc4jJ8krH9xTFZtv87PD1z4o6+ikJpBXuq6chYVU8bfUM DZ//8VfxXvflSwfNtP2GkmgPAGhit6r1HU4EDfv6MpAcUxlLGyVRHpOUL 56OxKH2p7hmIH1cODkD6hdZgJnJSbClVPeTTJYPNeOcny1xO7Os2PbkJ5 k=; Authentication-Results: esa5.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=roger.pau@citrix.com; spf=Pass smtp.mailfrom=roger.pau@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: None (esa5.hc3370-68.iphmx.com: no sender authenticity information available from domain of roger.pau@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa5.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="roger.pau@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa5.hc3370-68.iphmx.com: domain of roger.pau@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa5.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="roger.pau@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ip4:168.245.78.127 ~all" Received-SPF: None (esa5.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa5.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: v+nR0J7QqBkRZeR2M1oQUrGmng8YXVl+mz9kL2l6qFzqO0lr9S5va4C9i6MV7mnBy4OpBaWUI1 SFKhGE6JSmbZ0g7UOfv86hg575u18W1GF8nYfc3lWYK9L3ju0/v3JsD9LpCydnuQ7H38rpDyvx Bgy2Av11IPPIjMybykPUp19yB2ugKQf/wLxqTQnEw1p4S1ogHl1xXF5Ouq6rPEXQ9TgE4nQrUk oyHk6wfs01F+dr9M+glNgPE3lU9stQSxOemX5pY0uYiUshlLJU3ULY1smdiz8InjiXZtjgOfV1 2S0= X-SBRS: 2.7 X-MesageID: 11432894 X-Ironport-Server: esa5.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.70,329,1574139600"; d="scan'208";a="11432894" From: Roger Pau Monne To: Date: Fri, 17 Jan 2020 12:08:11 +0100 Message-ID: <20200117110811.43321-1-roger.pau@citrix.com> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v2] x86/hvmloader: round up memory BAR size to 4K X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Wei Liu , Jason Andryuk , Andrew Cooper , Ian Jackson , Jan Beulich , Roger Pau Monne Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) When placing memory BARs with sizes smaller than 4K multiple memory BARs can end up mapped to the same guest physical address, and thus won't work correctly. Round up all memory BAR sizes to be at least 4K, so that they are naturally aligned to a page size and thus don't end up sharing a page. Also add a couple of asserts to the current code to make sure the MMIO hole is properly sized and aligned. Note that the guest can still move the BARs around and create this collisions, and that BARs not filling up a physical page might leak access to other MMIO regions placed in the same host physical page. This is however no worse than what's currently done, and hence should be considered an improvement over the current state. Reported-by: Jason Andryuk Signed-off-by: Roger Pau Monn=C3=A9 Tested-by: Jason Andryuk --- Cc: Jason Andryuk --- Changes since v1: - Do the round up when sizing the BARs, so that the MMIO hole is correctly sized. - Add some asserts that the hole is properly sized and size-aligned. - Dropped Jason Tested-by since the code has changed. --- Jason, can you give this a spin? Thanks. --- tools/firmware/hvmloader/pci.c | 12 ++++++++++++ tools/firmware/hvmloader/util.h | 3 +++ 2 files changed, 15 insertions(+) diff --git a/tools/firmware/hvmloader/pci.c b/tools/firmware/hvmloader/pci.c index 0b708bf578..ac4fb5aa82 100644 --- a/tools/firmware/hvmloader/pci.c +++ b/tools/firmware/hvmloader/pci.c @@ -253,6 +253,15 @@ void pci_setup(void) if ( bar_sz =3D=3D 0 ) continue; =20 + if ( ((bar_data & PCI_BASE_ADDRESS_SPACE) =3D=3D + PCI_BASE_ADDRESS_SPACE_MEMORY) || + (bar_reg =3D=3D PCI_ROM_ADDRESS) ) + /* + * Always roundup memory BAR sizes to the size of a page in + * order to prevent BARs being placed in the same page. + */ + bar_sz =3D ROUNDUP(bar_sz, PAGE_SIZE); + for ( i =3D 0; i < nr_bars; i++ ) if ( bars[i].bar_sz < bar_sz ) break; @@ -295,6 +304,8 @@ void pci_setup(void) pci_writew(devfn, PCI_COMMAND, cmd); } =20 + ASSERT(IS_ALIGNED(mmio_total, PAGE_SIZE)); + if ( mmio_hole_size ) { uint64_t max_ram_below_4g =3D GB(4) - mmio_hole_size; @@ -448,6 +459,7 @@ void pci_setup(void) resource =3D &mem_resource; bar_data &=3D ~PCI_BASE_ADDRESS_MEM_MASK; } + ASSERT(bar_sz <=3D mmio_total); mmio_total -=3D bar_sz; } else diff --git a/tools/firmware/hvmloader/util.h b/tools/firmware/hvmloader/uti= l.h index 7bca6418d2..e1ebfd6647 100644 --- a/tools/firmware/hvmloader/util.h +++ b/tools/firmware/hvmloader/util.h @@ -51,6 +51,9 @@ void __bug(char *file, int line) __attribute__((noreturn)= ); #define MB(mb) (mb##ULL << 20) #define GB(gb) (gb##ULL << 30) =20 +#define ROUNDUP(x, a) (((x) + (a) - 1) & ~((a) - 1)) +#define IS_ALIGNED(val, align) (((val) & ((align) - 1)) =3D=3D 0) + static inline int test_bit(unsigned int b, const void *p) { return !!(((const uint8_t *)p)[b>>3] & (1u<<(b&7))); --=20 2.25.0 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel