From patchwork Thu Apr 30 05:19:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 1279927 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=2001:470:142::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 [IPv6:2001:470:142::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 49CP8Y1Tf5z9sRY for ; Thu, 30 Apr 2020 15:29:47 +1000 (AEST) Received: from localhost ([::1]:36786 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU1lX-0003Uf-He for incoming@patchwork.ozlabs.org; Thu, 30 Apr 2020 01:29:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53216) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU1lH-0003Ti-OD for qemu-devel@nongnu.org; Thu, 30 Apr 2020 01:29:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU1lH-00028I-9u for qemu-devel@nongnu.org; Thu, 30 Apr 2020 01:29:27 -0400 Received: from mga18.intel.com ([134.134.136.126]:48100) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU1lG-00028B-PT for qemu-devel@nongnu.org; Thu, 30 Apr 2020 01:29:26 -0400 IronPort-SDR: hcZrCMAzQxXz8+EcMmr0ME/9uKYeKGoRAh/uVbwLT4EokG63aN2YXWcq8cMqyLQ22s4MTxmY3y RL1w79+q+ZIA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2020 22:29:25 -0700 IronPort-SDR: Xv52D3rETX74P8CJw1CHMA0xPGqIKq4I/9uPlXTE/k97zSSoAykr/vkXWGiytgLtjGLJdz7qi9 xlW0sg2AAo8w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,334,1583222400"; d="scan'208";a="249649344" Received: from joy-optiplex-7040.sh.intel.com ([10.239.13.16]) by fmsmga008.fm.intel.com with ESMTP; 29 Apr 2020 22:29:23 -0700 From: Yan Zhao To: pbonzini@redhat.com, alex.williamson@redhat.com Subject: [PATCH v5 1/3] memory: drop guest writes to read-only ram device regions Date: Thu, 30 Apr 2020 01:19:23 -0400 Message-Id: <20200430051923.29159-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200430051558.28991-1-yan.y.zhao@intel.com> References: <20200430051558.28991-1-yan.y.zhao@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.126; envelope-from=yan.y.zhao@intel.com; helo=mga18.intel.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/29 23:42:54 X-ACL-Warn: Detected OS = FreeBSD 9.x or newer [fuzzy] X-Received-From: 134.134.136.126 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" for ram device regions, drop guest writes if the regions is read-only. Cc: Philippe Mathieu-Daudé Signed-off-by: Yan Zhao Signed-off-by: Xin Zeng Reviewed-by: Philippe Mathieu-Daudé --- memory.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/memory.c b/memory.c index 601b749906..90a748912f 100644 --- a/memory.c +++ b/memory.c @@ -34,6 +34,7 @@ #include "sysemu/accel.h" #include "hw/boards.h" #include "migration/vmstate.h" +#include "qemu/log.h" //#define DEBUG_UNASSIGNED @@ -1307,12 +1308,19 @@ static uint64_t memory_region_ram_device_read(void *opaque, return data; } -static void memory_region_ram_device_write(void *opaque, hwaddr addr, - uint64_t data, unsigned size) +static MemTxResult memory_region_ram_device_write(void *opaque, hwaddr addr, + uint64_t data, unsigned size, + MemTxAttrs attrs) { MemoryRegion *mr = opaque; trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size); + if (mr->readonly) { + qemu_log_mask(LOG_GUEST_ERROR, + "Invalid write to read only ram device region addr 0x%" + HWADDR_PRIx" size %u\n", addr, size); + return MEMTX_ERROR; + } switch (size) { case 1: @@ -1328,11 +1336,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr, *(uint64_t *)(mr->ram_block->host + addr) = data; break; } + return MEMTX_OK; } static const MemoryRegionOps ram_device_mem_ops = { .read = memory_region_ram_device_read, - .write = memory_region_ram_device_write, + .write_with_attrs = memory_region_ram_device_write, .endianness = DEVICE_HOST_ENDIAN, .valid = { .min_access_size = 1, From patchwork Thu Apr 30 05:23:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 1279949 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=2001:470:142::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 [IPv6:2001:470:142::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 49CPVt3Xmyz9sPF for ; Thu, 30 Apr 2020 15:45:42 +1000 (AEST) Received: from localhost ([::1]:35160 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU20x-0003cM-Dy for incoming@patchwork.ozlabs.org; Thu, 30 Apr 2020 01:45:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53538) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU1ok-00086h-Q8 for qemu-devel@nongnu.org; Thu, 30 Apr 2020 01:33:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU1ok-0003c3-2F for qemu-devel@nongnu.org; Thu, 30 Apr 2020 01:33:02 -0400 Received: from mga04.intel.com ([192.55.52.120]:42671) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU1oj-0003bw-HR for qemu-devel@nongnu.org; Thu, 30 Apr 2020 01:33:01 -0400 IronPort-SDR: UgYwG7ZcdHIGyEDap1MpQbMJTOC/nXGcnBcOewiYDQLAjKlKnR5ADeYGHizUDusCm3YEI6aLyW IGN1j2rFQbfg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2020 22:32:59 -0700 IronPort-SDR: nb9C0yGBHDvCLaMyT++JRXL0lnif0TfxdQpjrcWRkX7ydQ9lWIvtkMBzThSbRGtfQvHp9kFkIP mALsbJLFcjcw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,334,1583222400"; d="scan'208";a="249649990" Received: from joy-optiplex-7040.sh.intel.com ([10.239.13.16]) by fmsmga008.fm.intel.com with ESMTP; 29 Apr 2020 22:32:57 -0700 From: Yan Zhao To: pbonzini@redhat.com, alex.williamson@redhat.com Subject: [PATCH v5 2/3] hw/vfio: drop guest writes to ro regions Date: Thu, 30 Apr 2020 01:23:07 -0400 Message-Id: <20200430052307.29235-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200430051558.28991-1-yan.y.zhao@intel.com> References: <20200430051558.28991-1-yan.y.zhao@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=192.55.52.120; envelope-from=yan.y.zhao@intel.com; helo=mga04.intel.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/30 01:32:59 X-ACL-Warn: Detected OS = FreeBSD 9.x or newer [fuzzy] X-Received-From: 192.55.52.120 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" for vfio regions that are without write permission, drop guest writes to those regions. Cc: Philippe Mathieu-Daudé Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Yan Zhao Signed-off-by: Xin Zeng --- hw/vfio/common.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 0b3593b3c0..2a4fedfeaa 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -38,6 +38,7 @@ #include "sysemu/reset.h" #include "trace.h" #include "qapi/error.h" +#include "qemu/log.h" VFIOGroupList vfio_group_list = QLIST_HEAD_INITIALIZER(vfio_group_list); @@ -190,6 +191,16 @@ void vfio_region_write(void *opaque, hwaddr addr, uint64_t qword; } buf; + trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size); + if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) { + qemu_log_mask(LOG_GUEST_ERROR, + "Invalid write to read only vfio region (%s:region%d" + "+0x%"HWADDR_PRIx" size %d)\n", vbasedev->name, + region->nr, addr, size); + + return; + } + switch (size) { case 1: buf.byte = data; @@ -215,8 +226,6 @@ void vfio_region_write(void *opaque, hwaddr addr, addr, data, size); } - trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size); - /* * A read or write to a BAR always signals an INTx EOI. This will * do nothing if not pending (including not in INTx mode). We assume From patchwork Thu Apr 30 05:24:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 1279950 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=2001:470:142::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 [IPv6:2001:470:142::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 49CPX70zRHz9sPF for ; Thu, 30 Apr 2020 15:46:45 +1000 (AEST) Received: from localhost ([::1]:37502 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU21x-0004iB-D4 for incoming@patchwork.ozlabs.org; Thu, 30 Apr 2020 01:46:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53618) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU1pa-0001LI-0J for qemu-devel@nongnu.org; Thu, 30 Apr 2020 01:33:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU1pZ-0004VS-Jc for qemu-devel@nongnu.org; Thu, 30 Apr 2020 01:33:53 -0400 Received: from mga18.intel.com ([134.134.136.126]:48291) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU1pZ-0004Of-14 for qemu-devel@nongnu.org; Thu, 30 Apr 2020 01:33:53 -0400 IronPort-SDR: 5ME+KCo09MSl54loFRnToDEZFhmYE+SakJurR7Lpcfq0oHgZ+G4qc84MmzlabsIYA6qcjz3gUf ufsFiHllodoA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2020 22:33:51 -0700 IronPort-SDR: dGqiuojrI12cRxDWZSNbArBBKW0qp3YGxrNao6KDkgH8m+C14xUOUfvU4RpsGiu7Dt7APNH1PI /fa90OfmMdPw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,334,1583222400"; d="scan'208";a="249650098" Received: from joy-optiplex-7040.sh.intel.com ([10.239.13.16]) by fmsmga008.fm.intel.com with ESMTP; 29 Apr 2020 22:33:49 -0700 From: Yan Zhao To: pbonzini@redhat.com, alex.williamson@redhat.com Subject: [PATCH v5 3/3] hw/vfio: let read-only flag take effect for mmap'd regions Date: Thu, 30 Apr 2020 01:24:04 -0400 Message-Id: <20200430052404.29290-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200430051558.28991-1-yan.y.zhao@intel.com> References: <20200430051558.28991-1-yan.y.zhao@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.126; envelope-from=yan.y.zhao@intel.com; helo=mga18.intel.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/29 23:42:54 X-ACL-Warn: Detected OS = FreeBSD 9.x or newer [fuzzy] X-Received-From: 134.134.136.126 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 2a4fedfeaa..bf510e66c0 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -980,6 +980,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);