From patchwork Fri Apr 17 07:44:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 1271936 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=intel.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 493F313hsXz9sSX for ; Fri, 17 Apr 2020 08:56:45 +1000 (AEST) Received: from localhost ([::1]:40120 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jPDR5-0001QJ-Ft for incoming@patchwork.ozlabs.org; Thu, 16 Apr 2020 18:56:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48395) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jPDOX-0007DG-UN for qemu-devel@nongnu.org; Thu, 16 Apr 2020 18:54:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jPDOV-0000an-Rd for qemu-devel@nongnu.org; Thu, 16 Apr 2020 18:54:04 -0400 Received: from mga12.intel.com ([192.55.52.136]:29412) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jPDOV-0000Zi-IL for qemu-devel@nongnu.org; Thu, 16 Apr 2020 18:54:03 -0400 IronPort-SDR: 5C3Zt1F+XbCteA9G4UGrvPH4xO90jO4h6z6ti8yibaPLQgmsdKvPPqGKNjJ2uQfYcc2JW6Dfli cSSoGKRuhUNA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Apr 2020 15:54:02 -0700 IronPort-SDR: tUiSQbjjo4+Lpe86h2bsX/gwU7HXOGTRDMwclGQwaQ9O7uPSHSzrlV/6asAr1f4mPnfv/HFiav xoiwnpjcFetQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,392,1580803200"; d="scan'208";a="246130301" Received: from dpdk-zhirun.sh.intel.com ([10.67.119.121]) by fmsmga008.fm.intel.com with ESMTP; 16 Apr 2020 15:54:00 -0700 From: Yan Zhao To: pbonzini@redhat.com, alex.williamson@redhat.com Subject: [PATCH v4 3/3] hw/vfio: let read-only flag take effect for mmap'd regions Date: Fri, 17 Apr 2020 07:44:54 +0000 Message-Id: <20200417074454.28620-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 192.55.52.136 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: Yan Zhao , xin.zeng@intel.com, philmd@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" along side setting host page table to be read-only, the memory regions are also required to be read-only, so that when guest writes to the read-only & mmap'd regions, vmexits would happen and region write handlers are called. Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Yan Zhao Signed-off-by: Xin Zeng --- hw/vfio/common.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index b6956a8098..0049e97c34 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -979,6 +979,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);