Skip to contentSkip to conversation

Inter-VM Shared Memory Device

PATCH Applied Cam Macdonell 4 participants 12 messages Jul 27, 2010 – Aug 11, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v8 cover Inter-VM Shared Memory Device

Latest patch for PCI shared memory device that maps a host shared memory object to be shared between guests

new in this series

v8
- rebased to qemu.git
- added qemu_ram_alloc_from_ptr() function
- removed irqfd support as support functions are not yet in qemu

v7
- replace marking memory from v6 with marking device as unmigratable indicating that it should be unplugged before migration and re-added after.
- 'peer' case changed to require removal before migration, only 'master' devices can be migrated while attached.

v6
- migration support with 'master' and 'peer' roles for guest to determine who "owns" memory
- modified phys_ram_dirty array for marking memory as not to be migrated

v5:
- fixed segfault for non-server case
- code style fixes
- removed limit on the number of guests
- shared memory server is now in qemu.git/contrib
- made ioeventfd setup function generic
- removed interrupts when guest joined (let application handle it)

v4:
- moved to single Doorbell register and use datamatch to trigger different VMs rather than one register per eventfd
- remove writing arbitrary values to eventfds. Only values of 1 are now written to ensure correct usage

Cam Macdonell (5):
Provide a function to add an allocated region of memory to the qemu
RAM.
Device specification for shared memory PCI device
Add function to assign ioeventfd to MMIO.
Support marking a device as non-migratable
Inter-VM shared memory PCI device

 Makefile.target                    |    3 +
 cpu-common.h                       |    2 +
 docs/specs/ivshmem_device_spec.txt |   96 ++++
 exec.c                             |   43 ++
 hw/hw.h                            |    2 +
 hw/ivshmem.c                       |  834 ++++++++++++++++++++++++++++++++++++
 kvm-all.c                          |   32 ++
 kvm.h                              |    1 +
 qemu-char.c                        |    6 +
 qemu-char.h                        |    3 +
 qemu-doc.texi                      |   43 ++
 savevm.c                           |   44 ++-
 12 files changed, 1106 insertions(+), 3 deletions(-)
 create mode 100644 docs/specs/ivshmem_device_spec.txt
 create mode 100644 hw/ivshmem.c
#1 lore raw

PATCH v8 1/5 Add qemu_ram_alloc_from_ptr function

Provide a function to add an allocated region of memory to the qemu RAM.

This patch is copied from Marcelo's qemu_ram_map() in qemu-kvm and given the clearer name qemu_ram_alloc_from_ptr().

---
 cpu-common.h |    2 ++
 exec.c       |   43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 0 deletions(-)
cpu-common.h +2 -0
index 71e7933..0426bc8 100644
@@ -40,6 +40,8 @@ static inline void cpu_register_physical_memory(target_phys_addr_t start_addr,
 }
 
 ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr);
+ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,
+                        ram_addr_t size, void *host);
 ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size);
 void qemu_ram_free(ram_addr_t addr);
 /* This should only be used for ram local to a device.  */
exec.c +43 -0

49 lines not on this page · raw message

Signature · 1 line
1.6.3.2.198.g6096d
#2 lore raw

PATCH v8 2/5 Device specification for shared memory PCI device

---
 docs/specs/ivshmem_device_spec.txt |   96 ++++++++++++++++++++++++++++++++++++
 1 files changed, 96 insertions(+), 0 deletions(-)
 create mode 100644 docs/specs/ivshmem_device_spec.txt
docs/specs/ivshmem_device_spec.txt new file +96 -0
new file mode 100644
index 0000000..23dd2ba
@@ -0,0 +1,96 @@
+
+Device Specification for Inter-VM shared memory device
+------------------------------------------------------
+
+The Inter-VM shared memory device is designed to share a region of memory to
+userspace in multiple virtual guests.  The memory region does not belong to any
+guest, but is a POSIX memory object on the host.  Optionally, the device may
+support sending interrupts to other guests sharing the same memory region.
+
+
+The Inter-VM PCI device
+-----------------------
+
+*BARs*
+
+The device supports three BARs.  BAR0 is a 1 Kbyte MMIO region to support
+registers.  BAR1 is used for MSI-X when it is enabled in the device.  BAR2 is
+used to map the shared memory object from the host.  The size of BAR2 is
+specified when the guest is started and must be a power of 2 in size.
+
+*Registers*
+
+The device currently supports 4 registers of 32-bits each.  Registers
+are used for synchronization between guests sharing the same memory object when
+interrupts are supported (this requires using the shared memory server).
+
+The server assigns each VM an ID number and sends this ID number to the Qemu
+process when the guest starts.
+
+enum ivshmem_registers {
+    IntrMask = 0,
+    IntrStatus = 4,
+    IVPosition = 8,
+    Doorbell = 12
+};
+
+The first two registers are the interrupt mask and status registers.  Mask and
+status are only used with pin-based interrupts.  They are unused with MSI
+interrupts.
+
+Status Register: The status register is set to 1 when an interrupt occurs.
+
+Mask Register: The mask register is bitwise ANDed with the interrupt status
+and the result will raise an interrupt if it is non-zero.  However, since 1 is
+the only value the status will be set to, it is only the first bit of the mask
+that has any effect.  Therefore interrupts can be masked by setting the first
+bit to 0 and unmasked by setting the first bit to 1.
+
+IVPosition Register: The IVPosition register is read-only and reports the
+guest's ID number.  The guest IDs are non-negative integers.  When using the
+server, since the server is a separate process, the VM ID will only be set when
+the device is ready (shared memory is received from the server and accessible via
+the device).  If the device is not ready, the IVPosition will return -1.
+Applications should ensure that they have a valid VM ID before accessing the
+shared memory.
+
+Doorbell Register:  To interrupt another guest, a guest must write to the
+Doorbell register.  The doorbell register is 32-bits, logically divided into
+two 16-bit fields.  The high 16-bits are the guest ID to interrupt and the low
+16-bits are the interrupt vector to trigger.  The semantics of the value
+written to the doorbell depends on whether the device is using MSI or a regular
+pin-based interrupt.  In short, MSI uses vectors while regular interrupts set the
+status register.
+
+Regular Interrupts
+
+If regular interrupts are used (due to either a guest not supporting MSI or the
+user specifying not to use them on startup) then the value written to the lower
+16-bits of the Doorbell register results is arbitrary and will trigger an
+interrupt in the destination guest.
+
+Message Signalled Interrupts
+
+A ivshmem device may support multiple MSI vectors.  If so, the lower 16-bits
+written to the Doorbell register must be between 0 and the maximum number of
+vectors the guest supports.  The lower 16 bits written to the doorbell is the
+MSI vector that will be raised in the destination guest.  The number of MSI
+vectors is configurable but it is set when the VM is started.
+
+The important thing to remember with MSI is that it is only a signal, no status
+is set (since MSI interrupts are not shared).  All information other than the
+interrupt itself should be communicated via the shared memory region.  Devices
+supporting multiple MSI vectors can use different vectors to indicate different
+events have occurred.  The semantics of interrupt vectors are left to the
+user's discretion.
+
+
+Usage in the Guest
+------------------
+
+The shared memory device is intended to be used with the provided UIO driver.
+Very little configuration is needed.  The guest should map BAR0 to access the
+registers (an array of 32-bit ints allows simple writing) and map BAR2 to
+access the shared memory region itself.  The size of the shared memory region
+is specified when the guest (or shared memory server) is started.  A guest may
+map the whole shared memory region or only part of it.
Signature · 1 line
1.6.3.2.198.g6096d
#3 lore raw

PATCH v8 3/5 Add function to assign ioeventfd to MMIO.

---
 kvm-all.c |   32 ++++++++++++++++++++++++++++++++
 kvm.h     |    1 +
 2 files changed, 33 insertions(+), 0 deletions(-)
kvm-all.c +32 -0
index 7635f2f..d9a5dd0 100644
@@ -1241,6 +1241,38 @@ int kvm_set_signal_mask(CPUState *env, const sigset_t *sigset)
     return r;
 }
 
+int kvm_set_ioeventfd_mmio_long(int fd, uint32_t addr, uint32_t val, bool assign)
+{
+#ifdef KVM_IOEVENTFD
+    int ret;
+    struct kvm_ioeventfd iofd;
+
+    iofd.datamatch = val;
+    iofd.addr = addr;
+    iofd.len = 4;
+    iofd.flags = KVM_IOEVENTFD_FLAG_DATAMATCH;
+    iofd.fd = fd;
+
+    if (!kvm_enabled()) {
+        return -ENOSYS;
+    }
+
+    if (!assign) {
+        iofd.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
+    }
+
+    ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);
+
+    if (ret < 0) {
+        return -errno;
+    }
+
+    return 0;
+#else
+    return -ENOSYS;
+#endif
+}
+
 int kvm_set_ioeventfd_pio_word(int fd, uint16_t addr, uint16_t val, bool assign)
 {
 #ifdef KVM_IOEVENTFD
kvm.h +1 -0
index 93f8187..50b6c01 100644
@@ -175,6 +175,7 @@ static inline void cpu_synchronize_post_init(CPUState *env)
 }
 
 #endif
+int kvm_set_ioeventfd_mmio_long(int fd, uint32_t adr, uint32_t val, bool assign);
 
 int kvm_set_ioeventfd_pio_word(int fd, uint16_t adr, uint16_t val, bool assign);
 #endif
Signature · 1 line
1.6.3.2.198.g6096d
#4 lore raw

PATCH v8 4/5 Support marking a device as non-migratable

A non-migratable device should be removed before migration and re-added after.

---
 hw/hw.h  |    2 ++
 savevm.c |   44 +++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 43 insertions(+), 3 deletions(-)
hw/hw.h +2 -0
index c2de6fe..28b4204 100644
@@ -264,6 +264,8 @@ int register_savevm_live(DeviceState *dev,
                          void *opaque);
 
 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque);
+void register_device_unmigratable(DeviceState *dev, const char *idstr,
+                                                                void *opaque);
 
 typedef void QEMUResetHandler(void *opaque);
 
savevm.c +41 -3

85 lines not on this page · raw message

Signature · 1 line
1.6.3.2.198.g6096d
#5 lore raw

PATCH v8 5/5 Inter-VM shared memory PCI device

Support an inter-vm shared memory device that maps a shared-memory object as a PCI device in the guest. This patch also supports interrupts between guest by communicating over a unix domain socket. This patch applies to the qemu-kvm repository.

-device ivshmem,size=<size in format accepted by -m>[,shm=<shm name>]

Interrupts are supported between multiple VMs by using a shared memory server by using a chardev socket.

    -device ivshmem,size=<size in format accepted by -m>[,shm=<shm name>]
           [,chardev=<id>][,msi=on][,ioeventfd=on][,vectors=n][,role=peer|master]
    -chardev socket,path=<path>,id=<id>

The shared memory server, sample programs and init scripts are in a git repo here:

www.gitorious.org/nahanni

---
 Makefile.target |    3 +
 hw/ivshmem.c    |  834 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-char.c     |    6 +
 qemu-char.h     |    3 +
 qemu-doc.texi   |   43 +++
 5 files changed, 889 insertions(+), 0 deletions(-)
 create mode 100644 hw/ivshmem.c
Makefile.target +3 -0
index 3ef4666..4cb3a00 100644
@@ -188,6 +188,9 @@ obj-$(CONFIG_USB_OHCI) += usb-ohci.o
 obj-y += rtl8139.o
 obj-y += e1000.o
 
+# Inter-VM PCI shared memory
+obj-y += ivshmem.o
+
 # Hardware support
 obj-i386-y += vga.o
 obj-i386-y += mc146818rtc.o i8259.o pc.o
hw/ivshmem.c new file +834 -0

834 lines not on this page · raw message

qemu-char.c +6 -0
index 9b69d92..6a3952c 100644
@@ -2087,6 +2087,12 @@ static void tcp_chr_read(void *opaque)
     }
 }
 
+CharDriverState *qemu_chr_open_eventfd(int eventfd){
+
+    return qemu_chr_open_fd(eventfd, eventfd);
+
+}
+
 static void tcp_chr_connect(void *opaque)
 {
     CharDriverState *chr = opaque;
qemu-char.h +3 -0
index e3a0783..6ea01ba 100644
@@ -94,6 +94,9 @@ void qemu_chr_info_print(Monitor *mon, const QObject *ret_data);
 void qemu_chr_info(Monitor *mon, QObject **ret_data);
 CharDriverState *qemu_chr_find(const char *name);
 
+/* add an eventfd to the qemu devices that are polled */
+CharDriverState *qemu_chr_open_eventfd(int eventfd);
+
 extern int term_escape_char;
 
 /* async I/O support */
qemu-doc.texi +43 -0

49 lines not on this page · raw message

Signature · 1 line
1.6.3.2.198.g6096d
#6 lore raw

PATCH v8 5/5 RESEND: Inter-VM shared memory PCI device

resend for bug fix related to removal of irqfd

Support an inter-vm shared memory device that maps a shared-memory object as a PCI device in the guest. This patch also supports interrupts between guest by communicating over a unix domain socket. This patch applies to the qemu-kvm repository.

-device ivshmem,size=<size in format accepted by -m>[,shm=<shm name>]

Interrupts are supported between multiple VMs by using a shared memory server by using a chardev socket.

    -device ivshmem,size=<size in format accepted by -m>[,shm=<shm name>]
           [,chardev=<id>][,msi=on][,ioeventfd=on][,vectors=n][,role=peer|master]
    -chardev socket,path=<path>,id=<id>

The shared memory server, sample programs and init scripts are in a git repo here:

www.gitorious.org/nahanni

---
 Makefile.target |    3 +
 hw/ivshmem.c    |  828 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-char.c     |    6 +
 qemu-char.h     |    3 +
 qemu-doc.texi   |   43 +++
 5 files changed, 883 insertions(+), 0 deletions(-)
 create mode 100644 hw/ivshmem.c
Makefile.target +3 -0
index 8a9c427..b791492 100644
@@ -190,6 +190,9 @@ obj-$(CONFIG_USB_OHCI) += usb-ohci.o
 obj-y += rtl8139.o
 obj-y += e1000.o
 
+# Inter-VM PCI shared memory
+obj-y += ivshmem.o
+
 # Hardware support
 obj-i386-y += vga.o
 obj-i386-y += mc146818rtc.o i8259.o pc.o
hw/ivshmem.c new file +828 -0

828 lines not on this page · raw message

qemu-char.c +6 -0
index 9b69d92..6a3952c 100644
@@ -2087,6 +2087,12 @@ static void tcp_chr_read(void *opaque)
     }
 }
 
+CharDriverState *qemu_chr_open_eventfd(int eventfd){
+
+    return qemu_chr_open_fd(eventfd, eventfd);
+
+}
+
 static void tcp_chr_connect(void *opaque)
 {
     CharDriverState *chr = opaque;
qemu-char.h +3 -0
index e3a0783..6ea01ba 100644
@@ -94,6 +94,9 @@ void qemu_chr_info_print(Monitor *mon, const QObject *ret_data);
 void qemu_chr_info(Monitor *mon, QObject **ret_data);
 CharDriverState *qemu_chr_find(const char *name);
 
+/* add an eventfd to the qemu devices that are polled */
+CharDriverState *qemu_chr_open_eventfd(int eventfd);
+
 extern int term_escape_char;
 
 /* async I/O support */
qemu-doc.texi +43 -0

49 lines not on this page · raw message

Signature · 1 line
1.6.3.2.198.g6096d
#7 lore raw

Re: [Qemu-devel] [PATCH v8 5/5] RESEND: Inter-VM shared memory PCI device

Am 27.07.2010 18:54, schrieb Cam Macdonell:

Quoted 21 lines

resend for bug fix related to removal of irqfd

Support an inter-vm shared memory device that maps a shared-memory object as a PCI device in the guest. This patch also supports interrupts between guest by communicating over a unix domain socket. This patch applies to the qemu-kvm repository.

-device ivshmem,size=<size in format accepted by -m>[,shm=<shm name>]

Interrupts are supported between multiple VMs by using a shared memory server by using a chardev socket.

-device ivshmem,size=<size in format accepted by -m>[,shm=<shm name>] [,chardev=<id>][,msi=on][,ioeventfd=on][,vectors=n][,role=peer|master] -chardev socket,path=<path>,id=<id>

The shared memory server, sample programs and init scripts are in a git repo here:

www.gitorious.org/nahanni

Quoted 9 lines

---
Makefile.target | 3 +
hw/ivshmem.c | 828 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-char.c | 6 +
qemu-char.h | 3 +
qemu-doc.texi | 43 +++

5 files changed, 883 insertions(+), 0 deletions(-)

create mode 100644 hw/ivshmem.c

Hi,

hw/ivshmem.c breaks compilation on 32 bit hosts, for targets without kvm support and for win32 environments.

I sent patches to qemu-devel which fix the first two problems.

The win32 problems (missing mmap, maybe more) remain. Could you please fix them?

Regards
Stefan

#8 lore raw

Re: [Qemu-devel] [PATCH v8 5/5] RESEND: Inter-VM shared memory PCI device

Quoted 40 lines · Stefan Weil

Am 27.07.2010 18:54, schrieb Cam Macdonell:

Quoted 27 lines

resend for bug fix related to removal of irqfd

Support an inter-vm shared memory device that maps a shared-memory object as a
PCI device in the guest. This patch also supports interrupts between guest by
communicating over a unix domain socket. This patch applies to the qemu-kvm
repository.

-device ivshmem,size=<size in format accepted by -m>[,shm=<shm name>]

Interrupts are supported between multiple VMs by using a shared memory server
by using a chardev socket.

-device ivshmem,size=<size in format accepted by -m>[,shm=<shm name>] [,chardev=<id>][,msi=on][,ioeventfd=on][,vectors=n][,role=peer|master] -chardev socket,path=<path>,id=<id>

The shared memory server, sample programs and init scripts are in a git repo here:

www.gitorious.org/nahanni

Quoted 9 lines

---
Makefile.target | 3 +
hw/ivshmem.c | 828 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-char.c | 6 +
qemu-char.h | 3 +
qemu-doc.texi | 43 +++

5 files changed, 883 insertions(+), 0 deletions(-)

create mode 100644 hw/ivshmem.c

Hi,

Quoted 8 lines

hw/ivshmem.c breaks compilation on 32 bit hosts, for targets without kvm support
and for win32 environments.

I sent patches to qemu-devel which fix the first two problems.

The win32 problems (missing mmap, maybe more) remain. Could you please fix them?

Could that be accomplished with excluding it on Windows on the makefiles?

Thanks,
Cam

Quoted 5 lines

Regards
Stefan

maintainer
#9 lore raw

Re: [Qemu-devel] [PATCH v8 5/5] RESEND: Inter-VM shared memory PCI device

Quoted 4 lines · Cam Macdonell
Quoted 2 lines

The win32 problems (missing mmap, maybe more) remain. Could you please fix them?

Could that be accomplished with excluding it on Windows on the makefiles?

Yes, just use obj-$(CONFIG_POSIX).

Paolo

#10 lore raw

Re: [Qemu-devel] [PATCH v8 5/5] RESEND: Inter-VM shared memory PCI device

Am 11.08.2010 19:07, schrieb Paolo Bonzini:

Quoted 10 lines · Cam Macdonell
Quoted 4 lines
Quoted 2 lines

The win32 problems (missing mmap, maybe more) remain. Could you please fix them?

Could that be accomplished with excluding it on Windows on the makefiles?

Yes, just use obj-$(CONFIG_POSIX).

Paolo

Or use obj-$(CONFIG_KVM) if that file is only reasonable when KVM is supported.

Stefan

#11 lore raw

Re: [Qemu-devel] [PATCH v8 1/5] Add qemu_ram_alloc_from_ptr function

In reply to Cam Macdonell
Quoted 7 lines · Cam Macdonell

Provide a function to add an allocated region of memory to the qemu RAM.

This patch is copied from Marcelo's qemu_ram_map() in qemu-kvm and given the clearer name qemu_ram_alloc_from_ptr().

Applied all. Thanks.

Regards,

Anthony Liguori

Quoted 73 lines
---
  cpu-common.h |    2 ++
  exec.c       |   43 +++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 45 insertions(+), 0 deletions(-)
cpu-common.h +2 -0
index 71e7933..0426bc8 100644
@@ -40,6 +40,8 @@ static inline void cpu_register_physical_memory(target_phys_addr_t start_addr,
  }
 
  ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr);
+ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,
+                        ram_addr_t size, void *host);
  ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size);
  void qemu_ram_free(ram_addr_t addr);
  /* This should only be used for ram local to a device.  */
exec.c +43 -0
index 868cd7f..8636316 100644
@@ -2808,6 +2808,49 @@ static ram_addr_t last_ram_offset(void)
      return last;
  }
 
+ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,
+                        ram_addr_t size, void *host)
+{
+    RAMBlock *new_block, *block;
+
+    size = TARGET_PAGE_ALIGN(size);
+    new_block = qemu_mallocz(sizeof(*new_block));
+
+    if (dev&&  dev->parent_bus&&  dev->parent_bus->info->get_dev_path) {
+        char *id = dev->parent_bus->info->get_dev_path(dev);
+        if (id) {
+            snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
+            qemu_free(id);
+        }
+    }
+    pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
+
+    QLIST_FOREACH(block,&ram_list.blocks, next) {
+        if (!strcmp(block->idstr, new_block->idstr)) {
+            fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
+                    new_block->idstr);
+            abort();
+        }
+    }
+
+    new_block->host = host;
+
+    new_block->offset = find_ram_offset(size);
+    new_block->length = size;
+
+    QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
+
+    ram_list.phys_dirty = qemu_realloc(ram_list.phys_dirty,
+                                       last_ram_offset()>>  TARGET_PAGE_BITS);
+    memset(ram_list.phys_dirty + (new_block->offset>>  TARGET_PAGE_BITS),
+           0xff, size>>  TARGET_PAGE_BITS);
+
+    if (kvm_enabled())
+        kvm_setup_guest_memory(new_block->host, size);
+
+    return new_block->offset;
+}
+
  ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
  {
      RAMBlock *new_block, *block;