From nobody Mon Apr 29 12:34:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494274151586400.77474040624077; Mon, 8 May 2017 13:09:11 -0700 (PDT) Received: from localhost ([::1]:33262 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7oxv-0005Ev-T5 for importer@patchew.org; Mon, 08 May 2017 16:09:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36228) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d7ox9-0004vQ-TI for qemu-devel@nongnu.org; Mon, 08 May 2017 16:08:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d7ox8-0000aT-Vs for qemu-devel@nongnu.org; Mon, 08 May 2017 16:08:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34654) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d7ox8-0000aD-N3 for qemu-devel@nongnu.org; Mon, 08 May 2017 16:08:18 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9C19FC04B32C for ; Mon, 8 May 2017 20:08:17 +0000 (UTC) Received: from localhost (ovpn-116-2.gru2.redhat.com [10.97.116.2]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9906F176A4; Mon, 8 May 2017 20:08:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9C19FC04B32C Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=ehabkost@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 9C19FC04B32C From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Mon, 8 May 2017 17:08:12 -0300 Message-Id: <20170508200812.25296-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 08 May 2017 20:08:17 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH] iommu: Don't crash if machine is not PC_MACHINE X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Marcel Apfelbaum , "Michael S. Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Currently it's possible to crash QEMU using "-device *-iommu" and "-machine none": $ qemu-system-x86_64 -machine none -device amd-iommu qemu/hw/i386/amd_iommu.c:1140:amdvi_realize: Object 0x55627dafbc90 is not= an instance of type generic-pc-machine Aborted (core dumped) $ qemu-system-x86_64 -machine none -device intel-iommu qemu/hw/i386/intel_iommu.c:2972:vtd_realize: Object 0x56292ec0bc90 is not= an instance of type generic-pc-machine Aborted (core dumped) Fix amd-iommu and intel-iommu to ensure the current machine is really a TYPE_PC_MACHINE instance at their realize methods. Resulting error messages: $ qemu-system-x86_64 -machine none -device amd-iommu qemu-system-x86_64: -device amd-iommu: Machine-type 'none' not supported = by amd-iommu $ qemu-system-x86_64 -machine none -device intel-iommu qemu-system-x86_64: -device intel-iommu: Machine-type 'none' not supporte= d by intel-iommu Signed-off-by: Eduardo Habkost --- hw/i386/amd_iommu.c | 15 ++++++++++++++- hw/i386/intel_iommu.c | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c index f86a40aa30..516ebae952 100644 --- a/hw/i386/amd_iommu.c +++ b/hw/i386/amd_iommu.c @@ -21,6 +21,7 @@ */ #include "qemu/osdep.h" #include "hw/i386/amd_iommu.h" +#include "qapi/error.h" #include "qemu/error-report.h" #include "trace.h" =20 @@ -1137,7 +1138,19 @@ static void amdvi_realize(DeviceState *dev, Error **= err) int ret =3D 0; AMDVIState *s =3D AMD_IOMMU_DEVICE(dev); X86IOMMUState *x86_iommu =3D X86_IOMMU_DEVICE(dev); - PCIBus *bus =3D PC_MACHINE(qdev_get_machine())->bus; + MachineState *ms =3D MACHINE(qdev_get_machine()); + MachineClass *mc =3D MACHINE_GET_CLASS(ms); + PCMachineState *pcms =3D + PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE)); + PCIBus *bus; + + if (!pcms) { + error_setg(err, "Machine-type '%s' not supported by amd-iommu", + mc->name); + return; + } + + bus =3D pcms->bus; s->iotlb =3D g_hash_table_new_full(amdvi_uint64_hash, amdvi_uint64_equal, g_free, g_free); =20 diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 02f047c8e3..a12b1761f5 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -2969,11 +2969,21 @@ static bool vtd_decide_config(IntelIOMMUState *s, E= rror **errp) =20 static void vtd_realize(DeviceState *dev, Error **errp) { - PCMachineState *pcms =3D PC_MACHINE(qdev_get_machine()); - PCIBus *bus =3D pcms->bus; + MachineState *ms =3D MACHINE(qdev_get_machine()); + MachineClass *mc =3D MACHINE_GET_CLASS(ms); + PCMachineState *pcms =3D + PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE)); + PCIBus *bus; IntelIOMMUState *s =3D INTEL_IOMMU_DEVICE(dev); X86IOMMUState *x86_iommu =3D X86_IOMMU_DEVICE(dev); =20 + if (!pcms) { + error_setg(errp, "Machine-type '%s' not supported by intel-iommu", + mc->name); + return; + } + + bus =3D pcms->bus; VTD_DPRINTF(GENERAL, ""); x86_iommu->type =3D TYPE_INTEL; =20 --=20 2.11.0.259.g40922b1