Re: [Qemu-devel] [PATCH] hw/misc: slavepci_passthru driver

2016-01-19 Thread Francesco Zuliani


Hi Alex,


On 01/18/2016 05:41 PM, Alex Williamson wrote:

On Mon, 2016-01-18 at 10:16 -0500, Marc-André Lureau wrote:

Hi

- Original Message -

Hi there,

I'd like to submit this new pci driver ( hw/misc )for inclusion,
if you think it could be useful to other as well as ourself.

The driver "worked for our needs" BUT we haven't done extensive
testing and this is our first attempt to submit a patch so I kindly
ask for extra-forgiveness .

The "slavepci_passthru" driver is useful in the scenario described
below to implement a simplified passthru when the host CPU does not
support IOMMU and one is interested only in pci target-mode (slave
devices).

Let's CC Alex, who worked on the most recent framework for something related to 
that (VFIO).


Embedded system cpu (e.g. Atom, AMD G-Series) often lack the VT-d
extensions (IOMMU) needed to be able to pass-thru pci peripherals to
the guest machine (i.e. the pci pass-thru feature cannot be used).

If one is only interested in using the pci board as a pci-target
(slave device), this driver mmap(s) the host-pci-bars into the guest
within a virtual pci-device.

What exactly do you mean by pci-target/slave device?  Does this mean
that the device is not DMA capable, ie. cannot enable BusMaster?


Yes, exactly. Our approach  can be used ONLY if one is NOT interested in 
DMA-Capability (i.e. it is not possible to enable BusMaster)

This is useful in our case for debugging via qemu gsbserver facility
(i.e. '-s' option in qemu) a system running barebone-executable .

Currently the driver assumes the custom pci card has four 32-bit bars
to be mapped (in current patch this is mandatory)

HowTo:
To use the new driver one shall:
- define two environment variables for assigning proper VID and DID to
   associate to the guest pci card
- give the host pci bar address to map in the guest.

Example Usage:

Let us suppose that we have in the host a slave pci device with the
following 4 bars (i.e. output of lspci -v -s YOUR-CARD | grep Memory)
   Memory at db80 (32-bit, non-prefetchable) [size=4K]
   Memory at db90 (32-bit, non-prefetchable) [size=8K]
   Memory at dba0 (32-bit, non-prefetchable) [size=4K]
   Memory at dbb0 (32-bit, non-prefetchable) [size=4K]

We can map these bars in a guest-pci with VID=0xe33e DID=0x000a using

SLAVEPASSTHRU_VID="0xe33e" SLAVEPASSTHRU_DID="0xa" qemu-system-x86_64 \
   YOUR-SET-OF-FLAGS \
   -device
   
slavepassthru,size1=4096,baseaddr1=0xdb90,size2=8192,baseaddr2=0xdba0,size3=4096,baseaddr3=0xdbd0,size4=4096,baseaddr4=0xdbe0

Please note that if your device has less than four bars you can give
the same size and baseaddress to the unused bars.

Those are some pretty serious usage restrictions and using /dev/mem is
really not practical.  The resource files in pci-sysfs would even be a
better option.

our was a quick hack to fulfill our needs, the approach via sysfs is
of course the right one and we would implement it if this patch is of 
interest.



I didn't see how IO and MMIO BARs get enabled on the
physical device or whether you support any kind of interrupt scheme.

In our case the IO space is not used.
The MMIO space is already enabled.

Our custom board does not have any interrupt and our quick hack
did not implement it.


   I
had never really intended QEMU use of this, but you might want to
consider vfio no-iommu mode:

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/drivers/vfio/vfio.c?id=03a76b60f8ba27974e2d252bc555d2c103420e15

Using this taints the kernel, but maybe that's nothing you mind if
you're already letting QEMU access /dev/mem.  The QEMU vfio-pci driver
would need to be modified to use the new device and of course it
wouldn't have IOMMU translation capabilities.  That means that the
BusMaster bit should protected and MSI/X capabilities should be hidden
from the VM.  It seems more flexible and featureful than what you have
here.  Thanks,


I was not aware of this interesting patch, I will study it to see if
it fits our use case.

Just for information you mean "taint" in that "security" is broken, not
licensing issues, am I right?

Thanks a lot for your time

Francesco Zuliani


Alex





[Qemu-devel] [PATCH] hw/misc: slavepci_passthru driver

2016-01-18 Thread Francesco Zuliani
Hi there,

I'd like to submit this new pci driver ( hw/misc )for inclusion,
if you think it could be useful to other as well as ourself.

The driver "worked for our needs" BUT we haven't done extensive
testing and this is our first attempt to submit a patch so I kindly
ask for extra-forgiveness .

The "slavepci_passthru" driver is useful in the scenario described
below to implement a simplified passthru when the host CPU does not
support IOMMU and one is interested only in pci target-mode (slave
devices).

Embedded system cpu (e.g. Atom, AMD G-Series) often lack the VT-d
extensions (IOMMU) needed to be able to pass-thru pci peripherals to
the guest machine (i.e. the pci pass-thru feature cannot be used).

If one is only interested in using the pci board as a pci-target 
(slave device), this driver mmap(s) the host-pci-bars into the guest
within a virtual pci-device.

This is useful in our case for debugging via qemu gsbserver facility
(i.e. '-s' option in qemu) a system running barebone-executable .

Currently the driver assumes the custom pci card has four 32-bit bars
to be mapped (in current patch this is mandatory)

HowTo:
To use the new driver one shall:
- define two environment variables for assigning proper VID and DID to
  associate to the guest pci card
- give the host pci bar address to map in the guest. 

Example Usage:

Let us suppose that we have in the host a slave pci device with the
following 4 bars (i.e. output of lspci -v -s YOUR-CARD | grep Memory)
  Memory at db80 (32-bit, non-prefetchable) [size=4K]
  Memory at db90 (32-bit, non-prefetchable) [size=8K]
  Memory at dba0 (32-bit, non-prefetchable) [size=4K]
  Memory at dbb0 (32-bit, non-prefetchable) [size=4K]

We can map these bars in a guest-pci with VID=0xe33e DID=0x000a using

SLAVEPASSTHRU_VID="0xe33e" SLAVEPASSTHRU_DID="0xa" qemu-system-x86_64 \
  YOUR-SET-OF-FLAGS \
  -device 
slavepassthru,size1=4096,baseaddr1=0xdb90,size2=8192,baseaddr2=0xdba0,size3=4096,baseaddr3=0xdbd0,size4=4096,baseaddr4=0xdbe0

Please note that if your device has less than four bars you can give
the same size and baseaddress to the unused bars.

Thanks
Francesco Zuliani

Actual commit patch:

>From 1371bc4e4681f43a2d02b91ec5d7b84f7ccb1f32 Mon Sep 17 00:00:00 2001
From: Francesco Zuliani <francesco.zuli...@neat.it>
Date: Mon, 18 Jan 2016 14:26:54 +0100
Subject: [PATCH] hw/misc: slave_pci_passthru driver
Added a slavepci_passthru hw/misc pci-device-driver.

It enables pass-thru in system missing IOMMU feature (e.g. Intel Atom
lacks Vt-d extension). It maps hosts target-mode pci-board bars onto
the guests virtual pci-device bars.

Signed-off-by: Francesco Zuliani <francesco.zuli...@neat.it>
---
 default-configs/pci.mak |   1 +
 hw/misc/Makefile.objs   |   1 +
 hw/misc/slavepci_passthru.c | 453 
 3 files changed, 455 insertions(+)
 create mode 100644 hw/misc/slavepci_passthru.c

diff --git a/default-configs/pci.mak b/default-configs/pci.mak
index f250119..699a5a5 100644
--- a/default-configs/pci.mak
+++ b/default-configs/pci.mak
@@ -36,4 +36,5 @@ CONFIG_EDU=y
 CONFIG_VGA=y
 CONFIG_VGA_PCI=y
 CONFIG_IVSHMEM=$(CONFIG_POSIX)
+CONFIG_SLAVEPCIPASSTHRU=$(CONFIG_POSIX)
 CONFIG_ROCKER=y
diff --git a/hw/misc/Makefile.objs b/hw/misc/Makefile.objs
index d4765c2..e346a15 100644
--- a/hw/misc/Makefile.objs
+++ b/hw/misc/Makefile.objs
@@ -20,6 +20,7 @@ common-obj-$(CONFIG_PUV3) += puv3_pm.o
 common-obj-$(CONFIG_MACIO) += macio/
 
 obj-$(CONFIG_IVSHMEM) += ivshmem.o
+obj-$(CONFIG_SLAVEPCIPASSTHRU) += slavepci_passthru.o
 
 obj-$(CONFIG_REALVIEW) += arm_sysctl.o
 obj-$(CONFIG_NSERIES) += cbus.o
diff --git a/hw/misc/slavepci_passthru.c b/hw/misc/slavepci_passthru.c
new file mode 100644
index 000..ee709b7
--- /dev/null
+++ b/hw/misc/slavepci_passthru.c
@@ -0,0 +1,453 @@
+/*
+ * Host Device PCI-Card Slave Pass-Thru: based on ivshmem "qemu-device"
+ * 
+ *
+ * Author:
+ *  By Francesco Zuliani AT Neat S.r.l. <francesco.zuli...@neat.it>
+ *
+ * Based On: ivshmem.c
+ *  Original Author Cam Macdonell 
+ *
+ * This code is licensed under the GNU GPL v2.
+ *
+ * Contributions after 2012-01-13 are licensed under the terms of the
+ * GNU GPL, version 2 or (at your option) any later version.
+ */
+
+#include "hw/hw.h"
+#include "hw/i386/pc.h"
+#include "hw/pci/pci.h"
+#include "hw/pci/msix.h"
+#include "sysemu/kvm.h"
+#include "migration/migration.h"
+#include "qemu/error-report.h"
+#include "qemu/event_notifier.h"
+#include "sysemu/char.h"
+
+#include 
+#include 
+#include 
+#include 
+
+#define PCI_VENDOR_ID_SLAVEPASSTHRU_DEFAULT PCI_VENDOR_ID_REDHAT_QUMRANET
+#define PCI_DEVICE_ID_SLAVEPASSTHRU_DEFAULT 0x
+
+#define STRINGIFY(a)  #a
+
+//#define DEBUG_SLAVEPASSTHRU
+#ifdef DEBUG_SLAVEPASSTHRU
+#define SLAVEPASSTHRU_