From patchwork Fri May 11 06:59:59 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Kardashevskiy X-Patchwork-Id: 158458 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id C7CDBB6FC3 for ; Fri, 11 May 2012 18:16:45 +1000 (EST) Received: from localhost ([::1]:44259 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSl1n-0004ux-E3 for incoming@patchwork.ozlabs.org; Fri, 11 May 2012 04:16:43 -0400 Received: from eggs.gnu.org ([208.118.235.92]:36878) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSjpo-00035y-9z for qemu-devel@nongnu.org; Fri, 11 May 2012 03:00:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SSjpj-0006Gj-AV for qemu-devel@nongnu.org; Fri, 11 May 2012 03:00:15 -0400 Received: from mail-yw0-f45.google.com ([209.85.213.45]:43460) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSjpj-0006GD-64 for qemu-devel@nongnu.org; Fri, 11 May 2012 03:00:11 -0400 Received: by yhoo21 with SMTP id o21so2950202yho.4 for ; Fri, 11 May 2012 00:00:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :content-type:content-transfer-encoding:x-gm-message-state; bh=gt7g/GNX8+8ZIPgY/PJSG2WCBe3eSCxMS5JHzGMySQs=; b=OYLCwTcJeKzuAbewrO1BQjCc4LUg7nPRdAyhYhO8TJsEHKnL1fXTD6lBeM/rp7fyD1 AMd5xr59cv4o0wjS8i1em17Tj+yXUupi6sCpNYDOUSAa3OErmtXaT5fmKrDqyjJupReU L1wKOFCway4PzGMQ9lVzTrVQ/rTIytROSHUqgS38zpp5Wa1BTeHLQmYWPBM2zc2Y2+SU /Gre2oMY1LfSufg31gtPDH1+c/VpB6rraYnpd9d9X/tUMYCHnckkzRzSZ57ZDnls0rbl yRgsdaGY3S7apFtXCRrqpKhCtT/h6Qc+mN7Px/GamsQFQmTW6p9aRSCm8UYpgyb+dXFg rvsQ== Received: by 10.50.34.200 with SMTP id b8mr958265igj.50.1336719609151; Fri, 11 May 2012 00:00:09 -0700 (PDT) Received: from [10.61.2.175] (ibmaus65.lnk.telstra.net. [165.228.126.9]) by mx.google.com with ESMTPS id gj10sm2855003igc.9.2012.05.11.00.00.06 (version=SSLv3 cipher=OTHER); Fri, 11 May 2012 00:00:08 -0700 (PDT) Message-ID: <4FACB8EF.1040104@ozlabs.ru> Date: Fri, 11 May 2012 16:59:59 +1000 From: Alexey Kardashevskiy User-Agent: Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 MIME-Version: 1.0 To: qemu-devel@nongnu.org X-Gm-Message-State: ALoCoQkzUWzgL4tkem73oqInJVffregAbBI3+euiTiw0XEVzpGHCapHJEDoHJUsd8lGzmjzB7eyO X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.213.45 X-Mailman-Approved-At: Fri, 11 May 2012 04:16:31 -0400 Cc: aik@ozlabs.ru, kvm@vger.kernel.org Subject: [Qemu-devel] [RFC PATCH] qemu pci: pci_add_capability enhancement to prevent damaging config space X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Normally the pci_add_capability is called on devices to add new capability. This is ok for emulated devices which capabilities list is being built by QEMU. In the case of VFIO the capability may already exist and adding new capability into the beginning of the linked list may create a loop. For example, the old code destroys the following config of PCIe Intel E1000E: before adding PCI_CAP_ID_MSI (0x05): 0x34: 0xC8 0xC8: 0x01 0xD0 0xD0: 0x05 0xE0 0xE0: 0x10 0x00 after: 0x34: 0xD0 0xC8: 0x01 0xD0 0xD0: 0x05 0xC8 0xE0: 0x10 0x00 As result capabilities 0x01 and 0x05 point to each other. The proposed patch does not change capability pointers when the same type capability is about to add. Signed-off-by: Alexey Kardashevskiy --- hw/pci.c | 10 ++++++---- 1 files changed, 6 insertions(+), 4 deletions(-) diff --git a/hw/pci.c b/hw/pci.c index aa0c0b8..1f7c924 100644 --- a/hw/pci.c +++ b/hw/pci.c @@ -1794,10 +1794,12 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, } config = pdev->config + offset; - config[PCI_CAP_LIST_ID] = cap_id; - config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; - pdev->config[PCI_CAPABILITY_LIST] = offset; - pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; + if (config[PCI_CAP_LIST_ID] != cap_id) { + config[PCI_CAP_LIST_ID] = cap_id; + config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; + pdev->config[PCI_CAPABILITY_LIST] = offset; + pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; + } memset(pdev->used + offset, 0xFF, size); /* Make capability read-only by default */ memset(pdev->wmask + offset, 0, size);