From patchwork Tue Oct 30 11:47:50 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avi Kivity X-Patchwork-Id: 195464 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 404F42C009F for ; Tue, 30 Oct 2012 22:49:36 +1100 (EST) Received: from localhost ([::1]:35584 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTAK5-00030F-Rh for incoming@patchwork.ozlabs.org; Tue, 30 Oct 2012 07:49:33 -0400 Received: from eggs.gnu.org ([208.118.235.92]:40828) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTAIz-0000CC-DJ for qemu-devel@nongnu.org; Tue, 30 Oct 2012 07:48:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TTAIt-0003yD-Bs for qemu-devel@nongnu.org; Tue, 30 Oct 2012 07:48:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:12623) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTAIt-0003y8-45 for qemu-devel@nongnu.org; Tue, 30 Oct 2012 07:48:19 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q9UBmGbJ017325 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 30 Oct 2012 07:48:16 -0400 Received: from s01.tlv.redhat.com (s01.tlv.redhat.com [10.35.255.8]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q9UBlrJL028808; Tue, 30 Oct 2012 07:48:13 -0400 From: Avi Kivity To: qemu-devel@nongnu.org Date: Tue, 30 Oct 2012 13:47:50 +0200 Message-Id: <1351597670-23031-8-git-send-email-avi@redhat.com> In-Reply-To: <1351597670-23031-1-git-send-email-avi@redhat.com> References: <1351597670-23031-1-git-send-email-avi@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: "Michael S. Tsirkin" , Alexander Graf , Blue Swirl , Alex Williamson , Anthony Liguori Subject: [Qemu-devel] [PATCH v2 7/7] i440fx: add an iommu 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 This iommu encrypts addresses on the device bus to avoid divuling information to hackers equipped with bus analyzers. Following 3DES, addresses are encrypted multiple times. A XOR cypher is employed for efficiency. Signed-off-by: Avi Kivity --- hw/piix_pci.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/hw/piix_pci.c b/hw/piix_pci.c index 9af5847..99601f4 100644 --- a/hw/piix_pci.c +++ b/hw/piix_pci.c @@ -30,6 +30,7 @@ #include "sysbus.h" #include "range.h" #include "xen.h" +#include "exec-memory.h" /* * I440FX chipset data sheet. @@ -248,6 +249,81 @@ static int i440fx_initfn(PCIDevice *dev) return 0; } +typedef struct SillyIOMMU SillyIOMMU; + +struct SillyIOMMU { + MemoryRegion fault; + MemoryRegion l1; + MemoryRegion l2; + hwaddr mask; + hwaddr secret; +}; + +static IOMMUTLBEntry silly_l1_translate(MemoryRegion *l1, hwaddr addr) +{ + SillyIOMMU *s = container_of(l1, SillyIOMMU, l1); + hwaddr xlat = addr ^ s->secret; + + printf("l1: %" HWADDR_PRIx " -> %" HWADDR_PRIx "\n", addr, xlat); + + return (IOMMUTLBEntry) { + .device_addr = addr & ~s->mask, + .translated_addr = xlat & ~s->mask, + .addr_mask = s->mask, + .perm = { true, true }, + }; +} + +static MemoryRegionIOMMUOps silly_l1_iommu_ops = { + .translate = silly_l1_translate, +}; + +static IOMMUTLBEntry silly_l2_translate(MemoryRegion *l2, hwaddr addr) +{ + SillyIOMMU *s = container_of(l2, SillyIOMMU, l2); + hwaddr xlat = addr ^ s->secret; + + printf("l2: %" HWADDR_PRIx " -> %" HWADDR_PRIx "\n", addr, xlat); + + return (IOMMUTLBEntry) { + .device_addr = addr & ~s->mask, + .translated_addr = xlat & ~s->mask, + .addr_mask = s->mask, + .perm = { true, true }, + }; +} + +static MemoryRegionIOMMUOps silly_l2_iommu_ops = { + .translate = silly_l2_translate, +}; + +static MemoryRegion *silly_iommu_new(PCIBus *bus, void *opaque, int devfn) +{ + SillyIOMMU *s = g_new(SillyIOMMU, 1); + MemoryRegion *sysmem = get_system_memory(); + + s->mask = (0x1000 << (devfn >> 3)) - 1; + s->secret = ((devfn << 24) | 0x00aabbccdd) & ~s->mask; + memory_region_init(&s->fault, "silly-fault", INT64_MAX); + memory_region_init_iommu(&s->l2, &silly_l2_iommu_ops, sysmem, &s->fault, + "silly-l2", INT64_MAX); + memory_region_init_iommu(&s->l1, &silly_l1_iommu_ops, &s->l2, &s->fault, + "silly-l1", INT64_MAX); + return &s->l1; +} + +static void silly_iommu_del(MemoryRegion *l1) +{ + SillyIOMMU *s = container_of(l1, SillyIOMMU, l1); + + memory_region_del_subregion(&s->l2, get_system_memory()); + memory_region_del_subregion(&s->l1, &s->l2); + memory_region_destroy(&s->l2); + memory_region_destroy(&s->l1); + memory_region_destroy(&s->fault); + g_free(s); +} + static PCIBus *i440fx_common_init(const char *device_name, PCII440FXState **pi440fx_state, int *piix3_devfn, @@ -275,6 +351,7 @@ static PCIBus *i440fx_common_init(const char *device_name, s->address_space = address_space_mem; b = pci_bus_new(dev, NULL, pci_address_space, address_space_io, 0); + pci_setup_iommu(b, silly_iommu_new, silly_iommu_del, NULL); s->bus = b; object_property_add_child(qdev_get_machine(), "i440fx", OBJECT(dev), NULL); qdev_init_nofail(dev);