From nobody Thu May 16 01:21:41 2024 Delivered-To: importer@patchew.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=1588234824; cv=none; d=zohomail.com; s=zohoarc; b=CIEKsPiryJfcbyNFvN8x2Sn2TGWEDOqTDf0X1/1NtLMoUwS7VU8wYqvS66kILOejlzLVZQMf7KfvleZIXCxoKeUNyNA8zMbRsh9IczhriTZPJ/hR8i0PNOiRYu6FVzVhso1hd/josd77VK5oMcm9rJ/ckQrWSzmbL6ULe/m2Ksw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588234824; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=yL9sZLySVhns927NNQ57A6wbmIKVYofy+ZhhA3XVzI0=; b=l3lA2fqj78XSOfCS+/C5usMfINyfav+/hkOrCtvSh6fQ3njMaZo8NYVHK0Y5LmvVENBurQeCWmNI6vEgldzCQYGGmIrRNojyNP+TInvEId4TD3Vb6CvvTHJGKPVrhomdVkYUCYT9DI7MAJICIPaOuyGg6FniDHbTKXHYF7yEaWk= 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 1588234824928441.4146428765796; Thu, 30 Apr 2020 01:20:24 -0700 (PDT) Received: from localhost ([::1]:36810 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU4Qh-00061E-No for importer@patchew.org; Thu, 30 Apr 2020 04:20:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40674) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU4Q6-0005U5-Ed for qemu-devel@nongnu.org; Thu, 30 Apr 2020 04:19:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU4Q5-00084r-PU for qemu-devel@nongnu.org; Thu, 30 Apr 2020 04:19:46 -0400 Received: from mga09.intel.com ([134.134.136.24]:45612) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU4Q5-00084e-5e for qemu-devel@nongnu.org; Thu, 30 Apr 2020 04:19:45 -0400 Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2020 01:19:43 -0700 Received: from joy-optiplex-7040.sh.intel.com ([10.239.13.16]) by fmsmga004.fm.intel.com with ESMTP; 30 Apr 2020 01:19:41 -0700 IronPort-SDR: zYhjvBPICBSTIGqljfwibIk5BTPI+yRhc4XRDfBg/qcQQpN384VBHuhB7jvKTM5B4o7Whzj3VF aKMc7Jowb2Bw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: bKjx2f66f+JQDqh3/rW47byhRPVNoaCmzmkWoN83/ib6j4zoBJl/bMk6q0isdJXx/F6X3Knb+t cvTh/+BwZicw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,334,1583222400"; d="scan'208";a="282783941" From: Yan Zhao To: pbonzini@redhat.com, alex.williamson@redhat.com Subject: [PATCH v6 1/3] memory: drop guest writes to read-only ram device regions Date: Thu, 30 Apr 2020 04:09:46 -0400 Message-Id: <20200430080946.31286-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200430080744.31232-1-yan.y.zhao@intel.com> References: <20200430080744.31232-1-yan.y.zhao@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=134.134.136.24; envelope-from=yan.y.zhao@intel.com; helo=mga09.intel.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/30 04:19:43 X-ACL-Warn: Detected OS = FreeBSD 9.x or newer [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: Yan Zhao , xin.zeng@intel.com, philmd@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" for ram device regions, drop guest writes if the region is read-only. Cc: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Yan Zhao Signed-off-by: Xin Zeng --- memory.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/memory.c b/memory.c index 601b749906..a1bba985b9 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" =20 //#define DEBUG_UNASSIGNED =20 @@ -1307,12 +1308,19 @@ static uint64_t memory_region_ram_device_read(void = *opaque, return data; } =20 -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 add= r, + uint64_t data, unsigned = size, + MemTxAttrs attrs) { MemoryRegion *mr =3D opaque; =20 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 0= x%" + HWADDR_PRIx" size %u\n", addr, size); + return MEMTX_ERROR; + } =20 switch (size) { case 1: @@ -1328,11 +1336,12 @@ static void memory_region_ram_device_write(void *op= aque, hwaddr addr, *(uint64_t *)(mr->ram_block->host + addr) =3D data; break; } + return MEMTX_OK; } =20 static const MemoryRegionOps ram_device_mem_ops =3D { .read =3D memory_region_ram_device_read, - .write =3D memory_region_ram_device_write, + .write_with_attrs =3D memory_region_ram_device_write, .endianness =3D DEVICE_HOST_ENDIAN, .valid =3D { .min_access_size =3D 1, --=20 2.17.1 From nobody Thu May 16 01:21:41 2024 Delivered-To: importer@patchew.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=1588235015; cv=none; d=zohomail.com; s=zohoarc; b=WAUrvjo4QfJRNlemPBIRxMJdJ9jdb0+7b+SZ3dgY7flQFMX+znnn152TyFXyp8PCXc9qufpfgainKpt8bedhLxK5ojhSgXlBVFzrV5DS43PNCIuXKIbs5WjbUdIuiu0Tv0xe0TsiBGKIe2rnhWDPwUtdbjzteeGphhhbtTMgGbc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588235015; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0gcks/4zEnka6J/ArYzJ6ZcDlnwPfe/aw4A/Ni1GdNU=; b=kodn003GG5akJE+QgjMCLM3YFGveyeSLXCwUi6isYG1HFneKtRgKLfZ1rJK7ExMN94DnETb2nhZSWHaeyDbbiz8G9eQqjcNkb9FiFrYYk/zkDP9uD+EeA97GZ6O9lzlYW8kWGfphBgAlKM5D7goqFIT/825CMI+ct0F7xL4kEdA= 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 1588235015705311.83491883475835; Thu, 30 Apr 2020 01:23:35 -0700 (PDT) Received: from localhost ([::1]:41330 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU4Tm-00082l-Gq for importer@patchew.org; Thu, 30 Apr 2020 04:23:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41022) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU4TC-0007c9-F8 for qemu-devel@nongnu.org; Thu, 30 Apr 2020 04:22:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU4TB-0001tx-RD for qemu-devel@nongnu.org; Thu, 30 Apr 2020 04:22:58 -0400 Received: from mga05.intel.com ([192.55.52.43]:53988) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU4TB-0001tn-AJ for qemu-devel@nongnu.org; Thu, 30 Apr 2020 04:22:57 -0400 Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2020 01:22:55 -0700 Received: from joy-optiplex-7040.sh.intel.com ([10.239.13.16]) by fmsmga004.fm.intel.com with ESMTP; 30 Apr 2020 01:22:53 -0700 IronPort-SDR: sK1H+wDbanb6v1xiQoo0pxh5unhS0bBmxQQUKVR7Y8ED6WdN4WsU8bydhMlq+OS9h1lrQPYJ7j 08jipi0Nan1g== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: BB/YkbjgCS7MSD/zRHMjwcdqejlAWPErR/rsiva5vycjiH3kFwmShaGEvYOvljy9LMl5qkPzEX 2P7i24tcS4EQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,334,1583222400"; d="scan'208";a="282784801" From: Yan Zhao To: pbonzini@redhat.com, alex.williamson@redhat.com Subject: [PATCH v6 2/3] hw/vfio: drop guest writes to ro regions Date: Thu, 30 Apr 2020 04:13:04 -0400 Message-Id: <20200430081304.31346-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200430080744.31232-1-yan.y.zhao@intel.com> References: <20200430080744.31232-1-yan.y.zhao@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=192.55.52.43; envelope-from=yan.y.zhao@intel.com; helo=mga05.intel.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/30 04:22:55 X-ACL-Warn: Detected OS = FreeBSD 9.x or newer [fuzzy] X-Received-From: 192.55.52.43 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" for vfio regions that are without write permission, drop guest writes to those regions. Cc: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Philippe Mathieu-Daud=C3=A9 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" =20 VFIOGroupList vfio_group_list =3D QLIST_HEAD_INITIALIZER(vfio_group_list); @@ -190,6 +191,16 @@ void vfio_region_write(void *opaque, hwaddr addr, uint64_t qword; } buf; =20 + 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 =3D data; @@ -215,8 +226,6 @@ void vfio_region_write(void *opaque, hwaddr addr, addr, data, size); } =20 - 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 --=20 2.17.1 From nobody Thu May 16 01:21:41 2024 Delivered-To: importer@patchew.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=1588235268; cv=none; d=zohomail.com; s=zohoarc; b=hAXAxCT/R9ykyLZzNRDz6WJbBnH7yhYAEnMSL7QTmuFYVWZewWygFod09tpIIr91zfBfm3yg/838mMD806sg65NUovyaB5hQILEiqe2Tn9DtehX8QqxJJLnRcaF3kqmoYn3r/8v2OL4X9gt/Ur94+lmeBOMGCRUiUhJ0/gbkxNA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588235268; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Ha4LSkRs+FV4XQfYz+Fk72LLyMrIOSx/L/VRcKQ8CNA=; b=IXDMoEaWXEI2HY8MqptLvAXrjznecZrTT7llJ5sW+sWg4b0QGjj+ayfAeVRAOuNpDiLLGqmEbEwflmbYm4AUy5WqOfjYdBvc1//P4m80rWFzaD6yomw71ayZ5JfzZ5pKEWU63trg6PmyzJEFQRe5z4d7uu+cUMqYaePgxbo5GJ0= 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 1588235268769912.9502479916871; Thu, 30 Apr 2020 01:27:48 -0700 (PDT) Received: from localhost ([::1]:44550 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU4Xr-0002Aw-1c for importer@patchew.org; Thu, 30 Apr 2020 04:27:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41292) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jU4Wt-000104-Al for qemu-devel@nongnu.org; Thu, 30 Apr 2020 04:27:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jU4U7-0002EG-MA for qemu-devel@nongnu.org; Thu, 30 Apr 2020 04:26:47 -0400 Received: from mga14.intel.com ([192.55.52.115]:10658) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jU4U7-0002E0-4v for qemu-devel@nongnu.org; Thu, 30 Apr 2020 04:23:55 -0400 Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2020 01:23:53 -0700 Received: from joy-optiplex-7040.sh.intel.com ([10.239.13.16]) by fmsmga004.fm.intel.com with ESMTP; 30 Apr 2020 01:23:50 -0700 IronPort-SDR: mQfk6FIm7S1rFrHx+3Y7yv1J7lCeilJQNrOIRw7yNOqAUk/9xF4zJdgJzKxqPaTVaiy+TLcYoJ N7+63h1de9Kw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: sor0dj/ImWEAu+nCB0L0H3+VmscKb5QjBSTKC71GyDWOCMdrdM0YZSw+DQ83sXrTWRZCdh14L6 604yrLkklAGA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,334,1583222400"; d="scan'208";a="282785028" From: Yan Zhao To: pbonzini@redhat.com, alex.williamson@redhat.com Subject: [PATCH v6 3/3] hw/vfio: let read-only flag take effect for mmap'd regions Date: Thu, 30 Apr 2020 04:13:53 -0400 Message-Id: <20200430081353.31400-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200430080744.31232-1-yan.y.zhao@intel.com> References: <20200430080744.31232-1-yan.y.zhao@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=192.55.52.115; envelope-from=yan.y.zhao@intel.com; helo=mga14.intel.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/04/30 03:10:52 X-ACL-Warn: Detected OS = FreeBSD 9.x or newer [fuzzy] X-Received-From: 192.55.52.115 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+importer=patchew.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=C3=A9 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); =20 --=20 2.17.1