From patchwork Mon Apr 13 06:37:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 1269676 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 490zVD4qPmz9sPF for ; Mon, 13 Apr 2020 16:39:00 +1000 (AEST) Received: from localhost ([::1]:40850 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jNskE-0003Ke-KL for incoming@patchwork.ozlabs.org; Mon, 13 Apr 2020 02:38:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33120) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jNsjr-0003IR-VI for qemu-devel@nongnu.org; Mon, 13 Apr 2020 02:38:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jNsjq-0006gv-5A for qemu-devel@nongnu.org; Mon, 13 Apr 2020 02:38:35 -0400 Received: from mga06.intel.com ([134.134.136.31]:33140) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jNsjo-0006gb-EC for qemu-devel@nongnu.org; Mon, 13 Apr 2020 02:38:33 -0400 IronPort-SDR: dVctFnm20N05LAnKRfkIcVak6KWnLKo3h/P/sEwloeSXs8FxUGq8MlpHT2OhEZL/MWqukp8qVc G5LhJv19djmg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2020 23:38:30 -0700 IronPort-SDR: v7W8ERY5H+032SlrriuMM451Js3srNohW6xGJMlTsDrjMZ5mJqCfllAj/R+Or27QyDHfB9nPLM hSWDDawfO5SQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,377,1580803200"; d="scan'208";a="453062288" Received: from dpdk-zhirun.sh.intel.com ([10.67.119.121]) by fmsmga005.fm.intel.com with ESMTP; 12 Apr 2020 23:38:28 -0700 From: Yan Zhao To: qemu-devel@nongnu.org Subject: [PATCH v3 1/3] memory: drop guest writes to read-only ram device regions Date: Mon, 13 Apr 2020 06:37:13 +0000 Message-Id: <20200413063713.84659-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200413063627.84608-1-yan.y.zhao@intel.com> References: <20200413063627.84608-1-yan.y.zhao@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.31 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 , alex.williamson@redhat.com, stefanha@redhat.com, pbonzini@redhat.com, philmd@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" for ram device regions, drop guest writes if the regions is read-only. Signed-off-by: Yan Zhao Signed-off-by: Xin Zeng --- memory.c | 6 +++++- trace-events | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/memory.c b/memory.c index 601b749906..a380b59980 100644 --- a/memory.c +++ b/memory.c @@ -1312,7 +1312,11 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr, { MemoryRegion *mr = opaque; - trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size); + trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, + size, mr->readonly); + if (mr->readonly) { + return; + } switch (size) { case 1: diff --git a/trace-events b/trace-events index 42107ebc69..e1de662973 100644 --- a/trace-events +++ b/trace-events @@ -61,7 +61,7 @@ memory_region_ops_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u" memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u" memory_region_ram_device_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u" -memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u" +memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size, bool readonly) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u" " is_readonly_region=%d" flatview_new(void *view, void *root) "%p (root %p)" flatview_destroy(void *view, void *root) "%p (root %p)" flatview_destroy_rcu(void *view, void *root) "%p (root %p)" From patchwork Mon Apr 13 06:37:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 1269677 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 490zVr0DGDz9sT1 for ; Mon, 13 Apr 2020 16:39:32 +1000 (AEST) Received: from localhost ([::1]:40860 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jNskj-0004B4-VD for incoming@patchwork.ozlabs.org; Mon, 13 Apr 2020 02:39:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33177) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jNsk7-0003ev-Ej for qemu-devel@nongnu.org; Mon, 13 Apr 2020 02:38:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jNsk6-0006lf-DU for qemu-devel@nongnu.org; Mon, 13 Apr 2020 02:38:51 -0400 Received: from mga02.intel.com ([134.134.136.20]:22312) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jNsk6-0006kV-66 for qemu-devel@nongnu.org; Mon, 13 Apr 2020 02:38:50 -0400 IronPort-SDR: 9MW1j/5hs9uYUBcVOb4jOoVCDTcY1nvNT9Q25uJxySLlVPN7U9yPYXoChXcpPayIatyKVJUV/T VZeXNWBZh4Yw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2020 23:38:48 -0700 IronPort-SDR: yslwu2qoLm569dHyf9z88swUsTYIIQQ3f0NGb9YHjo2XDhB6+0WA8+DryFm5B4rRJjooGl8fD2 PHg6Bwjc2ZHw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,377,1580803200"; d="scan'208";a="453062347" Received: from dpdk-zhirun.sh.intel.com ([10.67.119.121]) by fmsmga005.fm.intel.com with ESMTP; 12 Apr 2020 23:38:46 -0700 From: Yan Zhao To: qemu-devel@nongnu.org Subject: [PATCH v3 2/3] hw/vfio: drop guest writes to ro regions Date: Mon, 13 Apr 2020 06:37:37 +0000 Message-Id: <20200413063737.84706-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200413063627.84608-1-yan.y.zhao@intel.com> References: <20200413063627.84608-1-yan.y.zhao@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.20 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 , alex.williamson@redhat.com, stefanha@redhat.com, pbonzini@redhat.com, philmd@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" for vfio regions that are without write permission, drop guest writes to those regions. Signed-off-by: Yan Zhao Signed-off-by: Xin Zeng --- hw/vfio/common.c | 8 +++++++- hw/vfio/trace-events | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 0b3593b3c0..fd6ee1fe3e 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -190,6 +190,11 @@ void vfio_region_write(void *opaque, hwaddr addr, uint64_t qword; } buf; + if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) { + trace_vfio_region_write(vbasedev->name, region->nr, + addr, data, size, true); + return; + } switch (size) { case 1: buf.byte = data; @@ -215,7 +220,8 @@ void vfio_region_write(void *opaque, hwaddr addr, addr, data, size); } - trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size); + trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size, + false); /* * A read or write to a BAR always signals an INTx EOI. This will diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index b1ef55a33f..fb9ff604e6 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -91,7 +91,7 @@ vfio_pci_nvlink2_setup_quirk_ssatgt(const char *name, uint64_t tgt, uint64_t siz vfio_pci_nvlink2_setup_quirk_lnkspd(const char *name, uint32_t link_speed) "%s link_speed=0x%x" # common.c -vfio_region_write(const char *name, int index, uint64_t addr, uint64_t data, unsigned size) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)" +vfio_region_write(const char *name, int index, uint64_t addr, uint64_t data, unsigned size, bool readonly) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)" " is_readonly_region=%d." vfio_region_read(char *name, int index, uint64_t addr, unsigned size, uint64_t data) " (%s:region%d+0x%"PRIx64", %d) = 0x%"PRIx64 vfio_iommu_map_notify(const char *op, uint64_t iova_start, uint64_t iova_end) "iommu %s @ 0x%"PRIx64" - 0x%"PRIx64 vfio_listener_region_add_skip(uint64_t start, uint64_t end) "SKIPPING region_add 0x%"PRIx64" - 0x%"PRIx64 From patchwork Mon Apr 13 06:37:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 1269678 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 490zWd1Fp4z9sT1 for ; Mon, 13 Apr 2020 16:40:13 +1000 (AEST) Received: from localhost ([::1]:40866 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jNslP-00052y-4L for incoming@patchwork.ozlabs.org; Mon, 13 Apr 2020 02:40:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33215) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jNskG-0003tD-7j for qemu-devel@nongnu.org; Mon, 13 Apr 2020 02:39:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jNskF-0006oh-94 for qemu-devel@nongnu.org; Mon, 13 Apr 2020 02:39:00 -0400 Received: from mga02.intel.com ([134.134.136.20]:22314) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jNskF-0006oV-1l for qemu-devel@nongnu.org; Mon, 13 Apr 2020 02:38:59 -0400 IronPort-SDR: MUgPNdSCkMn8RcPtjgAvzonZSDxQTiY4xhJDT6aVlszQThC/hDarRAF+EVopL5Wt47UkFVPUc6 6Xda78/h9phA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2020 23:38:58 -0700 IronPort-SDR: e0Egk4H0cnnSkK68El48wiEPxDbxl+nT5PgLWwkjr3HJ4VSD/UX6l4cpG5xonVa1qhdTfJs5pO QcJ93h7/N/bg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,377,1580803200"; d="scan'208";a="453062368" Received: from dpdk-zhirun.sh.intel.com ([10.67.119.121]) by fmsmga005.fm.intel.com with ESMTP; 12 Apr 2020 23:38:56 -0700 From: Yan Zhao To: qemu-devel@nongnu.org Subject: [PATCH v3 3/3] hw/vfio: let read-only flag take effect for mmap'd regions Date: Mon, 13 Apr 2020 06:37:47 +0000 Message-Id: <20200413063747.84753-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200413063627.84608-1-yan.y.zhao@intel.com> References: <20200413063627.84608-1-yan.y.zhao@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.20 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 , alex.williamson@redhat.com, stefanha@redhat.com, pbonzini@redhat.com, philmd@redhat.com 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. Signed-off-by: Yan Zhao Signed-off-by: Xin Zeng Reviewed-by: Philippe Mathieu-Daudé --- hw/vfio/common.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index fd6ee1fe3e..fc7618e041 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -977,6 +977,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);