From nobody Thu Sep 19 22:09:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.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=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1726032456; cv=none; d=zohomail.com; s=zohoarc; b=C70iWNbicbCtt3VKrhaPtlQ3QoGzs3sX6kjBzcq0VDTQr4WuNoytgSevMmAxXaSfmOqxtVMfhChxxp5OAqhmMi4FYH8Gg7BY4pFGEYuBt21T2NNXAcqcGDnzNrTdONu+TEUn3EFtgJH25grTHUfAayhFSkhizwWMUvt2GsG9A3c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1726032456; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=mUvROqwTc3xxp+nFQbrIh4o3oWtgn+rpw3ru2uuflog=; b=QrwdUsV+fgE4j/mZOxn/OqV8d2gqSTUV5Jzlm730W3REvGtKMlKf7d8NA87J5JP+WFS4+xFrwDnh/iGvh/o1IOj3aMfCZbmAGeTJwE99S6sHLNrkiazp1/c0AgXjx8nx6u6x9f15CJsbr+s0TccyLoFrzNWabpuxU2flSek0ozs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1726032456369153.49890539829983; Tue, 10 Sep 2024 22:27:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1soFs6-0002N4-1e; Wed, 11 Sep 2024 01:26:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1soFs3-0002Ls-TF for qemu-devel@nongnu.org; Wed, 11 Sep 2024 01:26:27 -0400 Received: from mgamail.intel.com ([192.198.163.11]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1soFs2-0004Mp-6k for qemu-devel@nongnu.org; Wed, 11 Sep 2024 01:26:27 -0400 Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Sep 2024 22:26:25 -0700 Received: from spr-s2600bt.bj.intel.com ([10.240.192.127]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Sep 2024 22:26:21 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1726032386; x=1757568386; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yqWqyth2P/d3i3FGK/vIH9NaHNh3ZvisTnQJSetWMNY=; b=jVw6KgDodf34ytw+040CUWcHc2tZZo5GLL1amOMsJQY1j8jlGzBs80f2 oGdZT6I29v8qMCLh/qYpMgFMMf7WeYlE3lXUeYpvTtfacTMKh6OBg8TvR 8PyLOjmvKe+42UOybfNcI+9jQFTyFaMBJ/+slk5bpvEWrC67AqLtk1irf AQBeODB8mDclHyFzvIBbeHttRYQerHr8r6BG/M9A3DUFfZzAmSbasCJ+G yaXoJbEQ0aKRcn8NW14/3iB19AX4/xUOeoyuc8owPxXdIUPT7+QKOyuQk 9zj7NVGsJzEafld1rwAgQ0NT8rVGld8jIzNj8Xnoe3b4YHbUwr9c50xvW Q==; X-CSE-ConnectionGUID: Sc1jmhrKTtOMfJiGuLgSXA== X-CSE-MsgGUID: 5s1de0RRTLKPOsunCEQ8Vw== X-IronPort-AV: E=McAfee;i="6700,10204,11191"; a="35403537" X-IronPort-AV: E=Sophos;i="6.10,219,1719903600"; d="scan'208";a="35403537" X-CSE-ConnectionGUID: oXRRSOrYT9qyWBD6rZl1iA== X-CSE-MsgGUID: 0U8jvKd3Sb+Loa/IcKN2NA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,219,1719903600"; d="scan'208";a="67536453" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, mst@redhat.com, peterx@redhat.com, jasowang@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, clement.mathieu--drif@eviden.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan , Paolo Bonzini , Richard Henderson , Eduardo Habkost , Marcel Apfelbaum Subject: [PATCH v3 07/17] intel_iommu: Check if the input address is canonical Date: Wed, 11 Sep 2024 13:22:45 +0800 Message-Id: <20240911052255.1294071-8-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240911052255.1294071-1-zhenzhong.duan@intel.com> References: <20240911052255.1294071-1-zhenzhong.duan@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.198.163.11; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.145, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1726032456603116600 From: Cl=C3=A9ment Mathieu--Drif First stage translation must fail if the address to translate is not canonical. Signed-off-by: Cl=C3=A9ment Mathieu--Drif Signed-off-by: Zhenzhong Duan --- hw/i386/intel_iommu_internal.h | 2 ++ hw/i386/intel_iommu.c | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 51e9b1fc43..668583aeca 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -320,6 +320,8 @@ typedef enum VTDFaultReason { VTD_FR_PASID_ENTRY_P =3D 0x59, VTD_FR_PASID_TABLE_ENTRY_INV =3D 0x5b, /*Invalid PASID table entry */ =20 + VTD_FR_FS_NON_CANONICAL =3D 0x80, /* SNG.1 : Address for FS not canoni= cal.*/ + /* Output address in the interrupt address range for scalable mode */ VTD_FR_SM_INTERRUPT_ADDR =3D 0x87, VTD_FR_MAX, /* Guard */ diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 6e31a8d383..be73366a64 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -1821,6 +1821,7 @@ static const bool vtd_qualified_faults[] =3D { [VTD_FR_PASID_ENTRY_P] =3D true, [VTD_FR_PASID_TABLE_ENTRY_INV] =3D true, [VTD_FR_SM_INTERRUPT_ADDR] =3D true, + [VTD_FR_FS_NON_CANONICAL] =3D true, [VTD_FR_MAX] =3D false, }; =20 @@ -1924,6 +1925,22 @@ static inline bool vtd_flpte_present(uint64_t flpte) return !!(flpte & VTD_FL_P); } =20 +/* Return true if IOVA is canonical, otherwise false. */ +static bool vtd_iova_fl_check_canonical(IntelIOMMUState *s, uint64_t iova, + VTDContextEntry *ce, uint32_t pasi= d) +{ + uint64_t iova_limit =3D vtd_iova_limit(s, ce, s->aw_bits, pasid); + uint64_t upper_bits_mask =3D ~(iova_limit - 1); + uint64_t upper_bits =3D iova & upper_bits_mask; + bool msb =3D ((iova & (iova_limit >> 1)) !=3D 0); + + if (msb) { + return upper_bits =3D=3D upper_bits_mask; + } else { + return !upper_bits; + } +} + /* * Given the @iova, get relevant @flptep. @flpte_level will be the last le= vel * of the translation, can be used for deciding the size of large page. @@ -1939,6 +1956,12 @@ static int vtd_iova_to_flpte(IntelIOMMUState *s, VTD= ContextEntry *ce, uint32_t offset; uint64_t flpte; =20 + if (!vtd_iova_fl_check_canonical(s, iova, ce, pasid)) { + error_report_once("%s: detected non canonical IOVA (iova=3D0x%" PR= Ix64 "," + "pasid=3D0x%" PRIx32 ")", __func__, iova, pasid); + return -VTD_FR_FS_NON_CANONICAL; + } + while (true) { offset =3D vtd_iova_level_offset(iova, level); flpte =3D vtd_get_pte(addr, offset); --=20 2.34.1