From nobody Fri May 3 10:48:16 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585276161; cv=none; d=zohomail.com; s=zohoarc; b=eHf3rkOjGYvMfVIMRXVdO9dTFZ1xVwbvYC1yzGTKfebBC48a2X762NaUKmVtlk5mbW9RESFM9ChcN7z9WPPJzklWCsmChI1xh7K6KMy71/FkCO0TV+Lce9hV3DeIaxTkuYtG+gnmg/3npwdH2l/nvivViALfeFKSaTf4Ldc9N+Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585276161; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=YEw201SMH2Qv005fN8FbjSRRQVo2gWhISoQ+2W/E6BE=; b=mcThptOu8mGPs0DO1i+r9FgVpuKBS41OLmsjbSPg1pjWbeX+HTo4sCqEYammRyoz8dmM4RRX3elJ/iNsNbF3c4A9e1T4iqTessIewgVWuGI4Oc0LDJQQ5lSm97d0ab4FdKehox0Svq2+cpecP/lwX+RltQJd/PsdNkKVMP3w2RQ= ARC-Authentication-Results: i=1; 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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 158527616149477.41379022677268; Thu, 26 Mar 2020 19:29:21 -0700 (PDT) Received: from localhost ([::1]:36258 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jHekJ-0003OU-VG for importer@patchew.org; Thu, 26 Mar 2020 22:29:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53599) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jHejb-0002zT-Cz for qemu-devel@nongnu.org; Thu, 26 Mar 2020 22:28:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jHejZ-0003b0-7M for qemu-devel@nongnu.org; Thu, 26 Mar 2020 22:28:34 -0400 Received: from mga09.intel.com ([134.134.136.24]:3746) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jHejY-0003Uf-VN for qemu-devel@nongnu.org; Thu, 26 Mar 2020 22:28:33 -0400 Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Mar 2020 19:28:29 -0700 Received: from dpdk-zhirun.sh.intel.com ([10.67.119.121]) by fmsmga004.fm.intel.com with ESMTP; 26 Mar 2020 19:28:27 -0700 IronPort-SDR: IOedDJpSRYLd9osv3xPNnGyzmfiaPUF/6l8qJ5Oq32XmpDS7AJWjrfJS66YXp4gQCn9F8ACgAm AZaUe6jhH2iQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: uvhGRG1O0rRJTVS3RJ97k0hyZgxHA6nBr3AONuOK53pHjdSBcOrQOtCLp5nLGoPQSY+xjLyBj5 WWAvucfQGtSg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,310,1580803200"; d="scan'208";a="271404410" From: yan.y.zhao@intel.com To: qemu-devel@nongnu.org Subject: [PATCH] hw/vfio: let readonly flag take effect for mmaped regions Date: Fri, 27 Mar 2020 11:19:34 +0000 Message-Id: <20200327111934.71066-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.24 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: , Cc: pbonzini@redhat.com, alex.williamson@redhat.com, Yan Zhao , Xin Zeng Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Yan Zhao currently, vfio regions without VFIO_REGION_INFO_FLAG_WRITE are only read-only when VFIO_REGION_INFO_FLAG_MMAP is not set. regions with flag VFIO_REGION_INFO_FLAG_READ | VFIO_REGION_INFO_FLAG_MMAP are only read-only in host page table for qemu. This patch sets corresponding ept page entries read-only for regions with flag VFIO_REGION_INFO_FLAG_READ | VFIO_REGION_INFO_FLAG_MMAP. accordingly, it ignores guest write when guest writes to the read-only regions are trapped. Signed-off-by: Yan Zhao Signed-off-by: Xin Zeng --- hw/vfio/common.c | 4 ++++ memory.c | 3 +++ 2 files changed, 7 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 0b3593b3c0..e901621ca0 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -971,6 +971,10 @@ int vfio_region_mmap(VFIORegion *region) name, region->mmaps[i].size, region->mmaps[i].mmap); g_free(name); + + if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) { + memory_region_set_readonly(®ion->mmaps[i].mem, true); + } memory_region_add_subregion(region->mem, region->mmaps[i].offset, ®ion->mmaps[i].mem); =20 diff --git a/memory.c b/memory.c index 601b749906..4b1071dc74 100644 --- a/memory.c +++ b/memory.c @@ -1313,6 +1313,9 @@ static void memory_region_ram_device_write(void *opaq= ue, hwaddr addr, MemoryRegion *mr =3D opaque; =20 trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, = size); + if (mr->readonly) { + return; + } =20 switch (size) { case 1: --=20 2.17.1