Userspace patches for PCI device assignment
PATCH v9 1/1 Userspace patches for PCI device assignment
This patchset enables device assignment for KVM hosts for PCI devices. It uses the Intel IOMMU by default if available.
Changes from v8->v9 in no particular order:
- fixed DEBUG, removed uneeded headers
- introduce USE_KVM_DEVICE_ASSIGNMENT modeled after KVM_USE_PIT, per aliguori's suggestion
- only call term_printf from the monitor
- implement markmc's suggestions: don't overflow dev->regions, fix disable_iommu bogosity, fix usage and comment with regards to -pcidevice format, and some other bits
Changes from v7->v8 in no particular order:
- various formatting fixes, DEBUG cleanups, cast removals, etc.
- s/strncpy/snprintf/
- split initialization in two phases per aliguori's suggestion
- bail out on errors when we can't limp on
- do ioperm on every cpu and vcpu (Weidong Han)
- use pwrite/pread where applicable
- split r_virtbase into different fields for memory and IO
- fix destruction of MMIO regions (Disheng Su and Weidong Han)
Changes from v6->v7 in no particular order:
- formatting changes: adhere to qemu style
- use strncmp, strncpy etc. instead of the insecure ones
- move from array to linked list
- change iopl() to ioperm() (Weidong Han)
- other small changes as suggested during the review of v6.
PATCH 1/6 device assignment: add ioctl wrappers
From: Amit Shah <[email protected]>
[muli: return -errno instead of ioctl retval]
--- libkvm/libkvm.c | 25 +++++++++++++++++++++++++ libkvm/libkvm.h | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 0 deletions(-)
libkvm/libkvm.c +25 -0
@@ -1112,3 +1112,28 @@ int kvm_unregister_coalesced_mmio(kvm_context_t kvm, uint64_t addr, uint32_t sizreturn -ENOSYS ; } +#ifdef KVM_CAP_DEVICE_ASSIGNMENT +int kvm_assign_pci_device (kvm_context_t kvm, +struct kvm_assigned_pci_dev *assigned_dev) +{ +int ret; + + ret =ioctl (kvm->vm_fd,KVM_ASSIGN_PCI_DEVICE , assigned_dev); +if (ret <0 ) +return -errno; + +return ret; +} + +int kvm_assign_irq (kvm_context_t kvm, +struct kvm_assigned_irq *assigned_irq) +{ +int ret; + + ret =ioctl (kvm->vm_fd,KVM_ASSIGN_IRQ , assigned_irq); +if (ret <0 ) +return -errno; + +return ret; +} +#endif
libkvm/libkvm.h +27 -0
@@ -686,4 +686,31 @@ int kvm_s390_interrupt(kvm_context_t kvm, int slot,int kvm_s390_set_initial_psw (kvm_context_t kvm,int slot,psw_t psw);int kvm_s390_store_status (kvm_context_t kvm,int slot,unsigned long addr);#endif + +#ifdef KVM_CAP_DEVICE_ASSIGNMENT +/*! +* \brief Notifies host kernel about a PCI device to be assigned to a guest +* +* Used for PCI device assignment, this function notifies the host +* kernel about the assigning of the physical PCI device to a guest. +* +* \param kvm Pointer to the current kvm_context +* \param assigned_dev Parameters, like bus, devfn number, etc +*/ +int kvm_assign_pci_device (kvm_context_t kvm, +struct kvm_assigned_pci_dev *assigned_dev); + +/*! +* \brief Notifies host kernel about changes to IRQ for an assigned device +* +* Used for PCI device assignment, this function notifies the host +* kernel about the changes in IRQ number for an assigned physical +* PCI device. +* +* \param kvm Pointer to the current kvm_context +* \param assigned_irq Parameters, like dev id, host irq, guest irq, etc +*/ +int kvm_assign_irq (kvm_context_t kvm, +struct kvm_assigned_irq *assigned_irq); +#endif #endif
Signature · 1 line
1.5.6.5
PATCH 2/6 device assignment: introduce pci_map_irq to get irq nr from pin number
From: Amit Shah <[email protected]>
--- qemu/hw/pci.c | 5 +++++ qemu/hw/pci.h | 1 + 2 files changed, 6 insertions(+), 0 deletions(-)
qemu/hw/pci.c +5 -0
@@ -560,6 +560,11 @@ static void pci_set_irq(void *opaque, int irq_num, int level) bus->set_irq (bus->irq_opaque, irq_num, bus->irq_count[irq_num] !=0 ); } +int pci_map_irq (PCIDevice *pci_dev,int pin) +{ +return pci_dev->bus->map_irq (pci_dev, pin); +} +/***********************************************************/ /* monitor info on PCI */
qemu/hw/pci.h +1 -0
@@ -81,6 +81,7 @@ void pci_register_io_region(PCIDevice *pci_dev, int region_num, uint32_t size, int type, PCIMapIORegionFunc *map_func); +int pci_map_irq (PCIDevice *pci_dev,int pin);uint32_t pci_default_read_config (PCIDevice *d,uint32_t address,int len); void pci_default_write_config(PCIDevice *d,
Signature · 1 line
1.5.6.5
PATCH 3/6 device assignment: introduce functions to correlate pin number and irq
From: Amit Shah <[email protected]>
[muli: use define for PIIX IRQ routing register (thanks markmc)]
--- qemu/hw/pc.h | 5 +++++ qemu/hw/piix_pci.c | 10 ++++++++++ 2 files changed, 15 insertions(+), 0 deletions(-)
qemu/hw/pc.h +5 -0
@@ -105,6 +105,9 @@ void pcspk_init(PITState *);int pcspk_audio_init (AudioState *,qemu_irq *pic);/* piix_pci.c */ +/* config space register for IRQ routing */ +#define PIIX_CONFIG_IRQ_ROUTE 0x60 +PCIBus *i440fx_init (PCIDevice **pi440fx_state,qemu_irq *pic);void i440fx_set_smm (PCIDevice *d,int val);int piix3_init (PCIBus *bus,int devfn);
@@ -112,6 +115,8 @@ void i440fx_init_memory_mappings(PCIDevice *d);int piix4_init (PCIBus *bus,int devfn); +int piix_get_irq (int pin); +/* vga.c */ enum vga_retrace_method {VGA_RETRACE_DUMB ,
qemu/hw/piix_pci.c +10 -0
@@ -243,6 +243,16 @@ static void piix3_set_irq(qemu_irq *pic, int irq_num, int level) } } +int piix_get_irq (int pin) +{ +if (piix3_dev) +return piix3_dev->config[PIIX_CONFIG_IRQ_ROUTE + pin]; +if (piix4_dev) +return piix4_dev->config[PIIX_CONFIG_IRQ_ROUTE + pin]; + +return 0 ; +} +static void piix3_reset (PCIDevice *d) {uint8_t *pci_conf = d->config;
Signature · 1 line
1.5.6.5
PATCH 4/6 device assignment: build vtd.c for Intel IOMMU support
From: Amit Shah <[email protected]>
--- kernel/x86/Kbuild | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/x86/Kbuild +3 -0
@@ -9,6 +9,9 @@ kvm-objs := kvm_main.o x86.o mmu.o x86_emulate.o ../anon_inodes.o irq.o i8259.o ifeq ($(EXT_CONFIG_KVM_TRACE),y) kvm-objs += kvm_trace.o endif +ifeq ($(CONFIG_DMAR),y) +kvm-objs += vtd.o +endif kvm-intel-objs := vmx.o vmx-debug.o ../external-module-compat.o kvm-amd-objs := svm.o ../external-module-compat.o
Signature · 1 line
1.5.6.5
PATCH 5/6 device assignment: support for assigning PCI devices to guests
From: Muli Ben-Yehuda <[email protected]>
This patch has been contributed to by the following people:
Or Sagi <[email protected]>
Nir Peleg <[email protected]>
Amit Shah <[email protected]>
Ben-Ami Yassour <[email protected]>
Weidong Han <[email protected]>
Glauber de Oliveira Costa <[email protected]>
Muli Ben-Yehuda <[email protected]>
With this patch, we can assign a device on the host machine to a guest.
A new command-line option, -pcidevice is added.
To invoke it for a device sitting at PCI bus:dev.fn 04:08.0, use this:
-pcidevice host=04:08.0
* The host driver for the device, if any, is to be removed before assigning the device (else device assignment will fail).
* A device that shares IRQ with another host device cannot currently be assigned.
* The RAW_IO capability is needed for this to work
This works only with the in-kernel irqchip method; to use the userspace irqchip, a kernel module (irqhook) and some extra changes are needed.
[muli: lots of fixes addressing review comments]
--- qemu/Makefile.target | 3 + qemu/configure | 21 ++ qemu/hw/device-assignment.c | 618 +++++++++++++++++++++++++++++++++++++++++++ qemu/hw/device-assignment.h | 108 ++++++++ qemu/hw/pc.c | 18 ++ qemu/hw/pci.c | 8 + qemu/qemu-kvm.c | 13 + qemu/qemu-kvm.h | 8 + qemu/vl.c | 28 ++ 9 files changed, 825 insertions(+), 0 deletions(-) create mode 100644 qemu/hw/device-assignment.c create mode 100644 qemu/hw/device-assignment.h
qemu/Makefile.target +3 -0
@@ -621,6 +621,9 @@ OBJS+= ide.o pckbd.o ps2.o vga.o $(SOUND_HW) dma.o OBJS+= fdc.o mc146818rtc.o serial.o i8259.o i8254.o pcspk.o pc.o OBJS+= cirrus_vga.o apic.o parallel.o acpi.o piix_pci.o OBJS+= usb-uhci.o vmmouse.o vmport.o vmware_vga.o extboot.o +ifeq ($(USE_KVM_DEVICE_ASSIGNMENT), 1) +OBJS+= device-assignment.o +endif ifeq ($(USE_KVM_PIT), 1) OBJS+= i8254-kvm.o endif
qemu/configure +21 -0
@@ -101,6 +101,7 @@ linux="no" kqemu="no" kvm="no" kvm_cap_pit="no" +kvm_cap_device_assignment="no" profiler="no" kernel_path="" cocoa="no"
@@ -749,6 +750,9 @@ fi # KVM probe if test "$kvm" = "yes" ; then + +# test for KVM_CAP_PIT + cat > $TMPC <<EOF #include <libkvm.h> #ifndef KVM_CAP_PIT
@@ -759,6 +763,19 @@ EOF if $cc $ARCH_CFLAGS $CFLAGS -I"$kernel_path"/include -o $TMPE ${OS_CFLAGS} $TMPC 2> /dev/null ; then kvm_cap_pit="yes" fi + +# test for KVM_CAP_DEVICE_ASSIGNMENT + +cat > $TMPC <<EOF +#include <libkvm.h> +#ifndef KVM_CAP_DEVICE_ASSIGNMENT +#error "kvm no device assignment capability" +#endif +int main(void) { return 0; } +EOF + if $cc $ARCH_CFLAGS $CFLAGS -I"$kernel_path"/include -o $TMPE ${OS_CFLAGS} $TMPC 2> /dev/null ; then + kvm_cap_device_assignment="yes" + fi fi ##########################################
@@ -1515,6 +1532,10 @@ configure_kvm() { echo "USE_KVM_PIT=1" >> $config_mak echo "#define USE_KVM_PIT 1" >> $config_h fi + if test $kvm_cap_device_assignment = "yes" ; then + echo "USE_KVM_DEVICE_ASSIGNMENT=1" >> $config_mak + echo "#define USE_KVM_DEVICE_ASSIGNMENT 1" >> $config_h + fi disable_cpu_emulation fi }
qemu/hw/device-assignment.c new file +618 -0
618 lines not on this page · raw message
qemu/hw/device-assignment.h new file +108 -0
108 lines not on this page · raw message
qemu/hw/pc.c +18 -0
@@ -33,6 +33,7 @@#include "boards.h" #include "console.h" #include "fw_cfg.h" +#include "device-assignment.h" #include "qemu-kvm.h"
@@ -1157,6 +1158,23 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,if (pci_enabled)virtio_balloon_init (pci_bus); + +#ifdef USE_KVM_DEVICE_ASSIGNMENT +if (kvm_enabled () && device_assignment_enabled) { +int i; +for (i =0 ; i < assigned_devices_index; i++) { +if (add_assigned_device (assigned_devices[i]) <0 ) { +fprintf (stderr,"Warning: could not add assigned device %s\n" , + assigned_devices[i]); + } + } + +if (init_all_assigned_devices (pci_bus)) { +fprintf (stderr,"Failed to initialize assigned devices\n" ); +exit (1 ); + } + } +#endif /* USE_KVM_DEVICE_ASSIGNMENT */ } static void pc_init_pci(ram_addr_t ram_size, int vga_ram_size,
qemu/hw/pci.c +8 -0
@@ -50,6 +50,7 @@ struct PCIBus {static void pci_update_mappings (PCIDevice *d);static void pci_set_irq (void *opaque,int irq_num,int level); +void assigned_dev_update_irq (PCIDevice *d);target_phys_addr_t pci_mem_base;static int pci_irq_index;
@@ -453,6 +454,13 @@ void pci_default_write_config(PCIDevice *d, val >>=8 ; } +#ifdef USE_KVM_DEVICE_ASSIGNMENT +if (kvm_enabled () &&qemu_kvm_irqchip_in_kernel () && + address >=PIIX_CONFIG_IRQ_ROUTE && + address <PIIX_CONFIG_IRQ_ROUTE +4 ) +assigned_dev_update_irq (d); +#endif /* USE_KVM_DEVICE_ASSIGNMENT */ + end = address + len;if (end >PCI_COMMAND && address < (PCI_COMMAND +2 )) {/* if the command register is modified, we must modify the mappings */
qemu/qemu-kvm.c +13 -0
@@ -27,6 +27,7 @@ int kvm_pit = 1;#include <sys/utsname.h> #include <sys/syscall.h> #include <sys/mman.h> +#include <sys/io.h> #define bool _Bool#define false 0
@@ -1047,3 +1048,15 @@ int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr, {return kvm_unregister_coalesced_mmio (kvm_context, addr, size); } + +static void kvm_do_ioperm (void *_data) +{ +struct ioperm_data *data = _data; +ioperm (data->start_port, data->num, data->turn_on); +} + +void kvm_ioperm (CPUState *env,void *data) +{ +if (kvm_enabled () && qemu_system_ready) +on_vcpu (env, kvm_do_ioperm, data); +}
qemu/qemu-kvm.h +8 -0
@@ -93,6 +93,8 @@ int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr,void qemu_kvm_system_reset_request (void ); +void kvm_ioperm (CPUState *env,void *data); +#ifdef TARGET_PPC int handle_powerpc_dcr_read (int vcpu,uint32_t dcrn,uint32_t *data);int handle_powerpc_dcr_write (int vcpu,uint32_t dcrn,uint32_t data);
@@ -107,6 +109,12 @@ int handle_powerpc_dcr_write(int vcpu,uint32_t dcrn, uint32_t data);extern int kvm_allowed;extern kvm_context_t kvm_context; +struct ioperm_data { +unsigned long start_port; +unsigned long num; +int turn_on; +}; +#define kvm_enabled () (kvm_allowed)#define qemu_kvm_irqchip_in_kernel () kvm_irqchip_in_kernel(kvm_context)#define qemu_kvm_pit_in_kernel () kvm_pit_in_kernel(kvm_context)
qemu/vl.c +28 -0
@@ -38,6 +38,7 @@#include "qemu-char.h" #include "block.h" #include "audio/audio.h" +#include "hw/device-assignment.h" #include "migration.h" #include "balloon.h" #include "qemu-kvm.h"
@@ -215,6 +216,9 @@ CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];int win2k_install_hack =0 ;#endif int usb_enabled =0 ; +int device_assignment_enabled =0 ; +const char *assigned_devices[MAX_DEV_ASSIGN_CMDLINE ]; +int assigned_devices_index;static VLANState *first_vlan;int smp_cpus =1 ;const char *vnc_display;
@@ -8692,6 +8696,12 @@ static void help(int exitcode)#endif "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n" "-no-kvm-pit disable KVM kernel mode PIT\n" +#if defined(TARGET_I386 ) || defined(TARGET_X86_64 ) || defined(__linux__) +"-pcidevice host=bus:dev.func[,dma=none][,name=string]\n" +" expose a PCI device to the guest OS.\n" +" dma=none: don't perform any dma translations (default is to use an iommu)\n" + " 'string' is used in log output.\n" +#endif #endif #ifdef TARGET_I386 "-no-acpi disable ACPI\n"
@@ -8811,6 +8821,9 @@ enum { QEMU_OPTION_no_kvm, QEMU_OPTION_no_kvm_irqchip, QEMU_OPTION_no_kvm_pit, +#if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(__linux__) + QEMU_OPTION_pcidevice, +#endif QEMU_OPTION_no_reboot, QEMU_OPTION_no_shutdown, QEMU_OPTION_show_cursor,
@@ -8900,6 +8913,9 @@ static const QEMUOption qemu_options[] = { #endif { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip }, { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit }, +#if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(__linux__) + { "pcidevice", HAS_ARG, QEMU_OPTION_pcidevice }, +#endif #endif #if defined(TARGET_PPC) || defined(TARGET_SPARC) { "g", 1, QEMU_OPTION_g },
@@ -9411,6 +9427,7 @@ int main(int argc, char **argv) parallel_device_index =0 ; usb_devices_index =0 ; + assigned_devices_index =0 ; nb_net_clients =0 ; nb_drives =0 ;
@@ -9844,6 +9861,17 @@ int main(int argc, char **argv) kvm_pit =0 ; break; } +#if defined(TARGET_I386 ) || defined(TARGET_X86_64 ) || defined(__linux__) +case QEMU_OPTION_pcidevice: + device_assignment_enabled =1 ; +if (assigned_devices_index >=MAX_DEV_ASSIGN_CMDLINE ) { +fprintf (stderr,"Too many assigned devices\n" ); +exit (1 ); + } + assigned_devices[assigned_devices_index] = optarg; + assigned_devices_index++; +break ; +#endif #endif case QEMU_OPTION_usb: usb_enabled =1 ;
Signature · 1 line
1.5.6.5
PATCH 6/6 device assignment: support for hot-plugging PCI devices
From: Amit Shah <[email protected]>
This patch adds support for hot-plugging host PCI devices into guests
[muli: various small fixes for review comments]
--- qemu/hw/device-hotplug.c | 37 +++++++++++++++++++++++++++++++++++++ qemu/monitor.c | 2 +- 2 files changed, 38 insertions(+), 1 deletions(-)
qemu/hw/device-hotplug.c +37 -0
@@ -6,6 +6,8 @@#include "pc.h" #include "console.h" #include "block_int.h" +#include "device-assignment.h" +#include "config.h" #define PCI_BASE_CLASS_STORAGE 0x01#define PCI_BASE_CLASS_NETWORK 0x02
@@ -27,6 +29,37 @@ static PCIDevice *qemu_system_hot_add_nic(const char *opts, int bus_nr)return pci_nic_init (pci_bus, &nd_table[ret],-1 ); } +#ifdef USE_KVM_DEVICE_ASSIGNMENT +static PCIDevice *qemu_system_hot_assign_device (const char *opts,int bus_nr) +{ +PCIBus *pci_bus; +AssignedDevInfo *adev; +PCIDevice *ret; + + pci_bus =pci_find_bus (bus_nr); +if (!pci_bus) { +term_printf ("Can't find pci_bus %d\n" , bus_nr); +return NULL ; + } + adev =add_assigned_device (opts); +if (adev ==NULL ) { +term_printf ("Error adding device; check syntax\n" ); +return NULL ; + } + + ret =init_assigned_device (adev, pci_bus); + +term_printf ("Registered host PCI device %02x:%02x.%1x " +"(\"%s\") as guest device %02x:%02x.%1x\n" , + adev->bus, adev->dev, adev->func, adev->name, +pci_bus_num (pci_bus), (ret->devfn >>3 ) &0x1f , + adev->func); + +return ret; +} + +#endif /* USE_KVM_DEVICE_ASSIGNMENT */ +static int add_init_drive (const char *opts) {int drive_opt_idx, drive_idx;
@@ -143,6 +176,10 @@ void device_hot_add(int pcibus, const char *type, const char *opts) dev =qemu_system_hot_add_nic (opts, pcibus); else if (strcmp(type, "storage") == 0) dev =qemu_system_hot_add_storage (opts, pcibus); +#ifdef USE_KVM_DEVICE_ASSIGNMENT + else if (strcmp(type, "host") == 0) + dev =qemu_system_hot_assign_device (opts, pcibus); +#endif /* USE_KVM_DEVICE_ASSIGNMENT */ else term_printf("invalid type: %s\n", type);
qemu/monitor.c +1 -1
@@ -1529,7 +1529,7 @@ static const term_cmd_t term_cmds[] = { "[,cyls=c,heads=h,secs=s[,trans=t]]\n" "[snapshot=on|off][,cache=on|off]", "add drive to PCI storage controller" }, - { "pci_add", "iss", device_hot_add, "bus nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" }, + { "pci_add", "iss", device_hot_add, "bus nic|storage|host [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]... [host=02:00.0[,name=string][,dma=none]" "hot-add PCI device" }, { "pci_del", "ii", device_hot_remove, "bus slot-number", "hot remove PCI device" }, #endif { "balloon", "i", do_balloon,
Signature · 1 line
1.5.6.5