Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Merge branch 'devel' of palacios@newskysaw.cs.northwestern.edu:/home/palacios/palacio...
Peter Dinda [Fri, 27 Jul 2012 23:18:07 +0000 (18:18 -0500)]
49 files changed:
linux_module/Makefile
linux_module/iface-console.c
linux_module/iface-host-pci-hw.h [new file with mode: 0644]
linux_module/iface-host-pci.c [new file with mode: 0644]
linux_module/linux-exts.c
linux_module/linux-exts.h
linux_module/main.c
linux_module/palacios.h
linux_module/util-queue.c
linux_module/util-queue.h
linux_module/vm.c
linux_module/vm.h
linux_usr/Makefile
linux_usr/ezxml.c [new file with mode: 0644]
linux_usr/ezxml.h [new file with mode: 0644]
linux_usr/v3_create.c
linux_usr/v3_ctrl.h
linux_usr/v3_pci.c [new file with mode: 0644]
palacios/include/interfaces/host_pci.h [new file with mode: 0644]
palacios/include/palacios/svm_exits.h [new file with mode: 0644]
palacios/include/palacios/vm_guest.h
palacios/include/palacios/vmm_bitmap.h
palacios/include/palacios/vmm_events.h [new file with mode: 0644]
palacios/include/palacios/vmm_exits.h [new file with mode: 0644]
palacios/include/palacios/vmm_queue.h
palacios/include/palacios/vmx_exits.h [new file with mode: 0644]
palacios/src/devices/8259a.c
palacios/src/devices/Makefile
palacios/src/devices/host_pci.c [new file with mode: 0644]
palacios/src/extensions/Kconfig
palacios/src/extensions/Makefile
palacios/src/extensions/ext_vmware.c [new file with mode: 0644]
palacios/src/interfaces/Kconfig
palacios/src/interfaces/Makefile
palacios/src/interfaces/host_pci.c [new file with mode: 0644]
palacios/src/palacios/Makefile
palacios/src/palacios/svm.c
palacios/src/palacios/svm_exits.c [new file with mode: 0644]
palacios/src/palacios/svm_handler.c
palacios/src/palacios/vm_guest.c
palacios/src/palacios/vmm.c
palacios/src/palacios/vmm_barrier.c
palacios/src/palacios/vmm_bitmap.c
palacios/src/palacios/vmm_events.c [new file with mode: 0644]
palacios/src/palacios/vmm_exits.c [new file with mode: 0644]
palacios/src/palacios/vmm_paging.c
palacios/src/palacios/vmm_queue.c
palacios/src/palacios/vmx_exits.c [new file with mode: 0644]
palacios/src/vnet/vnet_core.c

index 98416cb..2236f81 100644 (file)
@@ -43,6 +43,8 @@ v3vee-$(V3_CONFIG_EXT_ENV_INJECT) += iface-env-inject.o
 v3vee-$(V3_CONFIG_EXT_SELECTIVE_SYSCALL_EXIT) += iface-syscall.o
 
 
+v3vee-$(V3_CONFIG_HOST_PCI) += iface-host-pci.o
+
 v3vee-objs := $(v3vee-y) ../libv3vee.a
 obj-m := v3vee.o
 
index a11c50e..067cefc 100644 (file)
@@ -414,6 +414,11 @@ static void palacios_tty_close(void * console) {
     struct palacios_console * cons = (struct palacios_console *) console;
 
     cons->open = 0;
+
+    remove_guest_ctrl(cons->guest, V3_VM_CONSOLE_CONNECT);
+    deinit_queue(cons->queue);
+       
+    kfree(cons);
 }
 
 
diff --git a/linux_module/iface-host-pci-hw.h b/linux_module/iface-host-pci-hw.h
new file mode 100644 (file)
index 0000000..1736c90
--- /dev/null
@@ -0,0 +1,453 @@
+/* Linux host side PCI passthrough support
+ * Jack Lange <jacklange@cs.pitt.edu>, 2012
+ */
+
+#include <linux/pci.h>
+#include <linux/iommu.h>
+#include <linux/interrupt.h>
+#include <linux/version.h>
+
+
+#define PCI_HDR_SIZE 256
+
+
+static int setup_hw_pci_dev(struct host_pci_device * host_dev) {
+    int ret = 0;
+    struct pci_dev * dev = NULL;
+    struct v3_host_pci_dev * v3_dev = &(host_dev->v3_dev);
+
+    dev = pci_get_bus_and_slot(host_dev->hw_dev.bus,
+                              host_dev->hw_dev.devfn);
+
+
+    if (dev == NULL) {
+       printk("Could not find HW pci device (bus=%d, devfn=%d)\n", 
+              host_dev->hw_dev.bus, host_dev->hw_dev.devfn); 
+       return -1;
+    }
+
+    // record pointer in dev state
+    host_dev->hw_dev.dev = dev;
+
+    host_dev->hw_dev.intx_disabled = 1;
+    spin_lock_init(&(host_dev->hw_dev.intx_lock));
+
+    if (pci_enable_device(dev)) {
+       printk("Could not enable Device\n");
+       return -1;
+    }
+    
+    ret = pci_request_regions(dev, "v3vee");
+    if (ret != 0) {
+       printk("Could not reservce PCI regions\n");
+       return -1;
+    }
+
+
+    pci_reset_function(host_dev->hw_dev.dev);
+    pci_save_state(host_dev->hw_dev.dev);
+
+
+    {
+       int i = 0;
+       for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
+           printk("Resource %d\n", i);
+           printk("\tflags = 0x%lx\n", pci_resource_flags(dev, i));
+           printk("\t name=%s, start=%lx, size=%d\n", 
+                  host_dev->hw_dev.dev->resource[i].name, (uintptr_t)pci_resource_start(dev, i),
+                  (u32)pci_resource_len(dev, i));
+
+       }
+
+       printk("Rom BAR=%d\n", dev->rom_base_reg);
+    }
+
+    /* Cache first 6 BAR regs */
+    {
+       int i = 0;
+
+       for (i = 0; i < 6; i++) {
+           struct v3_host_pci_bar * bar = &(v3_dev->bars[i]);
+           unsigned long flags;
+           
+           bar->size = pci_resource_len(dev, i);
+           bar->addr = pci_resource_start(dev, i);
+           flags = pci_resource_flags(dev, i);
+
+           if (flags & IORESOURCE_IO) {
+               bar->type = PT_BAR_IO;
+           } else if (flags & IORESOURCE_MEM) {
+               if (flags & IORESOURCE_MEM_64) {
+                   struct v3_host_pci_bar * hi_bar = &(v3_dev->bars[i + 1]); 
+           
+                   bar->type = PT_BAR_MEM64_LO;
+
+                   hi_bar->type = PT_BAR_MEM64_HI;
+                   hi_bar->size = bar->size;
+                   hi_bar->addr = bar->addr;
+                   hi_bar->cacheable = ((flags & IORESOURCE_CACHEABLE) != 0);
+                   hi_bar->prefetchable = ((flags & IORESOURCE_PREFETCH) != 0);
+                   
+                   i++;
+               } else if (flags & IORESOURCE_DMA) {
+                   bar->type = PT_BAR_MEM24;
+               } else {
+                   bar->type = PT_BAR_MEM32;
+               }
+               
+               bar->cacheable = ((flags & IORESOURCE_CACHEABLE) != 0);
+               bar->prefetchable = ((flags & IORESOURCE_PREFETCH) != 0);
+
+           } else {
+               bar->type = PT_BAR_NONE;
+           }
+       }
+    }
+
+    /* Cache expansion rom bar */
+    {
+       struct resource * rom_res = &(dev->resource[PCI_ROM_RESOURCE]);
+       int rom_size = pci_resource_len(dev, PCI_ROM_RESOURCE);
+
+       if (rom_size > 0) {
+           unsigned long flags;
+
+           v3_dev->exp_rom.size = rom_size;
+           v3_dev->exp_rom.addr = pci_resource_start(dev, PCI_ROM_RESOURCE);
+           flags = pci_resource_flags(dev, PCI_ROM_RESOURCE);
+
+           v3_dev->exp_rom.type = PT_EXP_ROM;
+
+           v3_dev->exp_rom.exp_rom_enabled = rom_res->flags & IORESOURCE_ROM_ENABLE;
+       }
+    }
+
+    /* Cache entire configuration space */
+    {
+       int m = 0;
+
+       // Copy the configuration space to the local cached version
+       for (m = 0; m < PCI_HDR_SIZE; m += 4) {
+           pci_read_config_dword(dev, m, (u32 *)&(v3_dev->cfg_space[m]));
+       }
+    }
+
+
+    /* HARDCODED for now but this will need to depend on IOMMU support detection */
+    if (iommu_found()) {
+       printk("Setting host PCI device (%s) as IOMMU\n", host_dev->name);
+       v3_dev->iface = IOMMU;
+    } else {
+       printk("Setting host PCI device (%s) as SYMBIOTIC\n", host_dev->name);
+       v3_dev->iface = SYMBIOTIC;
+    }
+
+    return 0;
+
+}
+
+
+
+static irqreturn_t host_pci_intx_irq_handler(int irq, void * priv_data) {
+    struct host_pci_device * host_dev = priv_data;
+
+    //   printk("Host PCI IRQ handler (%d)\n", irq);
+
+    spin_lock(&(host_dev->hw_dev.intx_lock));
+    disable_irq_nosync(irq);
+    host_dev->hw_dev.intx_disabled = 1;
+    spin_unlock(&(host_dev->hw_dev.intx_lock));
+
+    V3_host_pci_raise_irq(&(host_dev->v3_dev), 0);
+
+    return IRQ_HANDLED;
+}
+
+
+
+static irqreturn_t host_pci_msi_irq_handler(int irq, void * priv_data) {
+    struct host_pci_device * host_dev = priv_data;
+    //    printk("Host PCI MSI IRQ Handler (%d)\n", irq);
+
+    V3_host_pci_raise_irq(&(host_dev->v3_dev), 0);
+
+    return IRQ_HANDLED;
+}
+
+static irqreturn_t host_pci_msix_irq_handler(int irq, void * priv_data) {
+    struct host_pci_device * host_dev = priv_data;
+    int i = 0;
+    
+    //    printk("Host PCI MSIX IRQ Handler (%d)\n", irq);
+    
+    // find vector index
+    for (i = 0; i < host_dev->hw_dev.num_msix_vecs; i++) {
+       if (irq == host_dev->hw_dev.msix_entries[i].vector) {
+           V3_host_pci_raise_irq(&(host_dev->v3_dev), i);
+       } else {
+           printk("Error Could not find matching MSIX vector for IRQ %d\n", irq);
+       }
+    }    
+    return IRQ_HANDLED;
+}
+
+
+static int hw_pci_cmd(struct host_pci_device * host_dev, host_pci_cmd_t cmd, u64 arg) {
+    //struct v3_host_pci_dev * v3_dev = &(host_dev->v3_dev);
+    struct pci_dev * dev = host_dev->hw_dev.dev;
+
+    switch (cmd) {
+       case HOST_PCI_CMD_DMA_DISABLE:
+           printk("Passthrough PCI device disabling BMDMA\n");
+           pci_clear_master(host_dev->hw_dev.dev);
+           break;
+       case HOST_PCI_CMD_DMA_ENABLE:
+           printk("Passthrough PCI device Enabling BMDMA\n");
+           pci_set_master(host_dev->hw_dev.dev);
+           break;
+
+       case HOST_PCI_CMD_INTX_DISABLE:
+           printk("Passthrough PCI device disabling INTx IRQ\n");
+
+           disable_irq(dev->irq);
+           free_irq(dev->irq, (void *)host_dev);
+
+           break;
+       case HOST_PCI_CMD_INTX_ENABLE:
+           printk("Passthrough PCI device Enabling INTx IRQ\n");
+       
+           if (request_threaded_irq(dev->irq, NULL, host_pci_intx_irq_handler, 
+                                    IRQF_ONESHOT, "V3Vee_Host_PCI_INTx", (void *)host_dev)) {
+               printk("ERROR Could not assign IRQ to host PCI device (%s)\n", host_dev->name);
+           }
+
+           break;
+
+       case HOST_PCI_CMD_MSI_DISABLE:
+           printk("Passthrough PCI device Disabling MSIs\n");
+
+           disable_irq(dev->irq);
+           free_irq(dev->irq, (void *)host_dev);
+
+           pci_disable_msi(dev);
+
+           break;
+       case HOST_PCI_CMD_MSI_ENABLE:
+           printk("Passthrough PCI device Enabling MSI\n");
+           
+           if (!dev->msi_enabled) {
+               pci_enable_msi(dev);
+
+               if (request_irq(dev->irq, host_pci_msi_irq_handler, 
+                               0, "V3Vee_host_PCI_MSI", (void *)host_dev)) {
+                   printk("Error Requesting IRQ %d for Passthrough MSI IRQ\n", dev->irq);
+               }
+           }
+
+           break;
+
+
+
+       case HOST_PCI_CMD_MSIX_ENABLE: {
+           int i = 0;
+           
+           printk("Passthrough PCI device Enabling MSIX\n");
+           host_dev->hw_dev.num_msix_vecs = arg;;
+           host_dev->hw_dev.msix_entries = kcalloc(host_dev->hw_dev.num_msix_vecs, 
+                                                   sizeof(struct msix_entry), GFP_KERNEL);
+           
+           for (i = 0; i < host_dev->hw_dev.num_msix_vecs; i++) {
+               host_dev->hw_dev.msix_entries[i].entry = i;
+           }
+           
+           pci_enable_msix(dev, host_dev->hw_dev.msix_entries, 
+                           host_dev->hw_dev.num_msix_vecs);
+           
+           for (i = 0; i < host_dev->hw_dev.num_msix_vecs; i++) {
+               if (request_irq(host_dev->hw_dev.msix_entries[i].vector, 
+                               host_pci_msix_irq_handler, 
+                               0, "V3VEE_host_PCI_MSIX", (void *)host_dev)) {
+                   printk("Error requesting IRQ %d for Passthrough MSIX IRQ\n", 
+                          host_dev->hw_dev.msix_entries[i].vector);
+               }
+           }
+
+           break;
+       }
+
+       case HOST_PCI_CMD_MSIX_DISABLE: {
+           int i = 0;
+
+           printk("Passthrough PCI device Disabling MSIX\n");
+           
+           for (i = 0; i < host_dev->hw_dev.num_msix_vecs; i++) {
+               disable_irq(host_dev->hw_dev.msix_entries[i].vector);
+           }
+
+           for (i = 0; i < host_dev->hw_dev.num_msix_vecs; i++) {
+               free_irq(host_dev->hw_dev.msix_entries[i].vector, (void *)host_dev);
+           }
+
+           host_dev->hw_dev.num_msix_vecs = 0;
+           kfree(host_dev->hw_dev.msix_entries);
+
+           pci_disable_msix(dev);
+
+           break;
+       }
+       default:
+           printk("Error: unhandled passthrough PCI command: %d\n", cmd);
+           return -1;
+          
+    }
+
+    return 0;
+}
+
+
+static int hw_ack_irq(struct host_pci_device * host_dev, u32 vector) {
+    struct pci_dev * dev = host_dev->hw_dev.dev;
+    unsigned long flags;
+
+    //    printk("Acking IRQ vector %d\n", vector);
+
+    spin_lock_irqsave(&(host_dev->hw_dev.intx_lock), flags);
+    //    printk("Enabling IRQ %d\n", dev->irq);
+    enable_irq(dev->irq);
+    host_dev->hw_dev.intx_disabled = 0;
+    spin_unlock_irqrestore(&(host_dev->hw_dev.intx_lock), flags);
+    
+    return 0;
+}
+
+
+
+
+static int reserve_hw_pci_dev(struct host_pci_device * host_dev, void * v3_ctx) {
+    int ret = 0;
+    unsigned long flags;
+    struct v3_host_pci_dev * v3_dev = &(host_dev->v3_dev);
+    struct pci_dev * dev = host_dev->hw_dev.dev;
+
+    spin_lock_irqsave(&lock, flags);
+    if (host_dev->hw_dev.in_use == 0) {
+       host_dev->hw_dev.in_use = 1;
+    } else {
+       ret = -1;
+    }
+    spin_unlock_irqrestore(&lock, flags);
+
+
+    if (v3_dev->iface == IOMMU) {
+       struct v3_guest_mem_region region;
+       int flags = 0;
+
+       host_dev->hw_dev.iommu_domain = iommu_domain_alloc();
+
+       if (V3_get_guest_mem_region(v3_ctx, &region) == -1) {
+           printk("Error getting VM memory region for IOMMU support\n");
+           return -1;
+       }
+       
+       printk("Memory region: start=%p, end=%p\n", (void *)region.start, (void *)region.end);
+
+
+       flags = IOMMU_READ | IOMMU_WRITE; // Need to see what IOMMU_CACHE means
+       
+       /* This version could be wrong */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) 
+       // Guest VAs start at zero and go to end of memory
+       iommu_map_range(host_dev->hw_dev.iommu_domain, 0, region.start, (region.end - region.start), flags);
+#else 
+       /* Linux actually made the interface worse... Now you can only map memory in powers of 2 (meant to only be pages...) */
+       {       
+           u64 size = region.end - region.start;
+           u32 page_size = 512 * 4096; // assume large 64bit pages (2MB)
+           u64 dpa = 0; // same as gpa
+           u64 hpa = region.start;
+
+           do {
+               if (size < page_size) {
+                   page_size = 4096; // less than a 2MB granularity, so we switch to small pages (4KB)
+               }
+               
+               printk("Mapping IOMMU region dpa=%p hpa=%p (size=%d)\n", (void *)dpa, (void *)hpa, page_size);
+
+               if (iommu_map(host_dev->hw_dev.iommu_domain, dpa, hpa, 
+                             get_order(page_size), flags)) {
+                   printk("ERROR: Could not map sub region (DPA=%p) (HPA=%p) (order=%d)\n", 
+                          (void *)dpa, (void *)hpa, get_order(page_size));
+                   break;
+               }
+
+               hpa += page_size;
+               dpa += page_size;
+
+               size -= page_size;
+           } while (size);
+       }
+#endif
+
+       if (iommu_attach_device(host_dev->hw_dev.iommu_domain, &(dev->dev))) {
+           printk("ERROR attaching host PCI device to IOMMU domain\n");
+       }
+
+    }
+
+
+    printk("Requesting Threaded IRQ handler for IRQ %d\n", dev->irq);
+    // setup regular IRQs until advanced IRQ mechanisms are enabled
+    if (request_threaded_irq(dev->irq, NULL, host_pci_intx_irq_handler, 
+                            IRQF_ONESHOT, "V3Vee_Host_PCI_INTx", (void *)host_dev)) {
+       printk("ERROR Could not assign IRQ to host PCI device (%s)\n", host_dev->name);
+    }
+
+
+
+    
+    return ret;
+}
+
+
+
+static int write_hw_pci_config(struct host_pci_device * host_dev, u32 reg, void * data, u32 length) {
+    struct pci_dev * dev = host_dev->hw_dev.dev;
+
+    if (reg < 64) {
+       return 0;
+    }
+       
+    if (length == 1) {
+       pci_write_config_byte(dev, reg, *(u8 *)data);
+    } else if (length == 2) {
+       pci_write_config_word(dev, reg, *(u16 *)data);
+    } else if (length == 4) {
+       pci_write_config_dword(dev, reg, *(u32 *)data);
+    } else {
+       printk("Invalid length of host PCI config update\n");
+       return -1;
+    }
+    
+    return 0;
+}
+
+
+
+static int read_hw_pci_config(struct host_pci_device * host_dev, u32 reg, void * data, u32 length) {
+    struct pci_dev * dev = host_dev->hw_dev.dev;
+
+       
+    if (length == 1) {
+       pci_read_config_byte(dev, reg, data);
+    } else if (length == 2) {
+       pci_read_config_word(dev, reg, data);
+    } else if (length == 4) {
+       pci_read_config_dword(dev, reg, data);
+    } else {
+       printk("Invalid length of host PCI config read\n");
+       return -1;
+    }
+
+
+    return 0; 
+}
diff --git a/linux_module/iface-host-pci.c b/linux_module/iface-host-pci.c
new file mode 100644 (file)
index 0000000..ce6ec7b
--- /dev/null
@@ -0,0 +1,244 @@
+/* Host PCI interface 
+ *  (c) Jack Lange, 2012
+ *  jacklange@cs.pitt.edu 
+ */
+
+#include <linux/uaccess.h>
+#include <linux/fs.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#include "palacios.h"
+#include "linux-exts.h"
+
+
+#include <interfaces/host_pci.h>
+
+static struct list_head device_list;
+static spinlock_t lock;
+
+
+
+
+struct pci_dev;
+struct iommu_domain;
+
+struct host_pci_device {
+    char name[128];
+    
+    enum {PASSTHROUGH, USER} type; 
+  
+    enum {INTX_IRQ, MSI_IRQ, MSIX_IRQ} irq_type;
+    uint32_t num_vecs;
+
+    union {
+       struct {    
+           u8 in_use;
+           u8 iommu_enabled;
+           
+           u32 bus;
+           u32 devfn;
+           
+           spinlock_t intx_lock;
+           u8 intx_disabled;
+
+           u32 num_msix_vecs;
+           struct msix_entry * msix_entries;
+           struct iommu_domain * iommu_domain;
+           
+           struct pci_dev * dev; 
+       } hw_dev;
+
+       //      struct user_dev_state user_dev;
+    };
+
+    struct v3_host_pci_dev v3_dev;
+
+    struct list_head dev_node;
+};
+
+
+//#include "iface-host-pci-user.h"
+#include "iface-host-pci-hw.h"
+
+
+static struct host_pci_device * find_dev_by_name(char * name) {
+    struct host_pci_device * dev = NULL;
+
+    list_for_each_entry(dev, &device_list, dev_node) {
+       if (strncmp(dev->name, name, 128) == 0) {
+           return dev;
+       }
+    }
+
+    return NULL;
+}
+
+
+
+static struct v3_host_pci_dev * request_pci_dev(char * url, void * v3_ctx) {
+   
+    unsigned long flags;
+    struct host_pci_device * host_dev = NULL;
+
+    spin_lock_irqsave(&lock, flags);
+    host_dev = find_dev_by_name(url);
+    spin_unlock_irqrestore(&lock, flags);
+    
+    if (host_dev == NULL) {
+       printk("Could not find host device (%s)\n", url);
+       return NULL;
+    }
+
+    if (host_dev->type == PASSTHROUGH) {
+       if (reserve_hw_pci_dev(host_dev, v3_ctx) == -1) {
+           printk("Could not reserve host device (%s)\n", url);
+           return NULL;
+       }
+    } else {
+       printk("Unsupported Host device type\n");
+       return NULL;
+    }
+
+
+
+    return &(host_dev->v3_dev);
+
+}
+
+
+static int host_pci_config_write(struct v3_host_pci_dev * v3_dev, unsigned int reg_num, 
+                                 void * src, unsigned int length) {
+    struct host_pci_device * host_dev = v3_dev->host_data;
+
+    if (host_dev->type == PASSTHROUGH) {
+       return write_hw_pci_config(host_dev, reg_num, src, length);
+    }
+    printk("Error in config write handler\n");
+    return -1;
+}
+
+static int host_pci_config_read(struct v3_host_pci_dev * v3_dev, unsigned int reg_num, 
+                                 void * dst, unsigned int length) {
+    struct host_pci_device * host_dev = v3_dev->host_data;
+
+    if (host_dev->type == PASSTHROUGH) {
+       return read_hw_pci_config(host_dev, reg_num, dst, length);
+    }
+    printk("Error in config read handler\n");
+    return -1;
+}
+
+
+static int host_pci_ack_irq(struct v3_host_pci_dev * v3_dev, unsigned int vector) {
+    struct host_pci_device * host_dev = v3_dev->host_data;
+
+    if (host_dev->type == PASSTHROUGH) {
+       return hw_ack_irq(host_dev, vector);
+    }
+    printk("Error in config irq ack handler\n");
+    return -1;
+}
+
+
+
+static int host_pci_cmd(struct v3_host_pci_dev * v3_dev, host_pci_cmd_t cmd, u64 arg) {
+    struct host_pci_device * host_dev = v3_dev->host_data;
+
+    if (host_dev->type == PASSTHROUGH) {
+       return hw_pci_cmd(host_dev, cmd, arg);
+    }
+    printk("Error in config pci cmd handler\n");
+    return -1;
+    
+}
+
+static struct v3_host_pci_hooks pci_hooks = {
+    .request_device = request_pci_dev,
+    .config_write = host_pci_config_write,
+    .config_read = host_pci_config_read,
+    .ack_irq = host_pci_ack_irq,
+    .pci_cmd = host_pci_cmd,
+
+};
+
+
+
+static int register_pci_hw_dev(unsigned int cmd, unsigned long arg) {
+    void __user * argp = (void __user *)arg;
+    struct v3_hw_pci_dev hw_dev_arg ;
+    struct host_pci_device * host_dev = NULL;
+    unsigned long flags;
+    int ret = 0;
+
+    if (copy_from_user(&hw_dev_arg, argp, sizeof(struct v3_hw_pci_dev))) {
+       printk("%s(%d): copy from user error...\n", __FILE__, __LINE__);
+       return -EFAULT;
+    }
+
+    host_dev = kzalloc(sizeof(struct host_pci_device), GFP_KERNEL);
+
+    
+    strncpy(host_dev->name, hw_dev_arg.name, 128);
+    host_dev->v3_dev.host_data = host_dev;
+    
+
+    host_dev->type = PASSTHROUGH;
+    host_dev->hw_dev.bus = hw_dev_arg.bus;
+    host_dev->hw_dev.devfn = PCI_DEVFN(hw_dev_arg.dev, hw_dev_arg.func);
+    
+
+    spin_lock_irqsave(&lock, flags);
+    if (!find_dev_by_name(hw_dev_arg.name)) {
+       list_add(&(host_dev->dev_node), &device_list);
+       ret = 1;
+    }
+    spin_unlock_irqrestore(&lock, flags);
+
+    if (ret == 0) {
+       // Error device already exists
+       kfree(host_dev);
+       return -EFAULT;
+    }
+
+    
+    setup_hw_pci_dev(host_dev);
+
+    return 0;
+}
+
+
+static int register_pci_user_dev(unsigned int cmd, unsigned long arg) {
+    return 0;
+}
+
+
+
+
+static int host_pci_init( void ) {
+    INIT_LIST_HEAD(&(device_list));
+    spin_lock_init(&lock);
+
+    V3_Init_Host_PCI(&pci_hooks);
+    
+
+    add_global_ctrl(V3_ADD_PCI_HW_DEV, register_pci_hw_dev);
+    add_global_ctrl(V3_ADD_PCI_USER_DEV, register_pci_user_dev);
+
+    return 0;
+}
+
+
+
+static struct linux_ext host_pci_ext = {
+    .name = "HOST_PCI",
+    .init = host_pci_init,
+};
+
+
+
+register_extension(&host_pci_ext);
index 089e67e..38e2e2b 100644 (file)
@@ -1,15 +1,97 @@
-
 #include "linux-exts.h"
 
 /* 
  * This is a place holder to ensure that the _lnx_exts section gets created by gcc
  */
 
-
 static struct {} null_ext  __attribute__((__used__))                    \
     __attribute__((unused, __section__ ("_lnx_exts"),                  \
                   aligned(sizeof(void *))));
 
+
+
+/*                 */
+/* Global controls */
+/*                 */
+
+struct rb_root global_ctrls;
+
+static inline struct global_ctrl * __insert_global_ctrl(struct global_ctrl * ctrl) {
+    struct rb_node ** p = &(global_ctrls.rb_node);
+    struct rb_node * parent = NULL;
+    struct global_ctrl * tmp_ctrl = NULL;
+
+    while (*p) {
+        parent = *p;
+        tmp_ctrl = rb_entry(parent, struct global_ctrl, tree_node);
+
+        if (ctrl->cmd < tmp_ctrl->cmd) {
+            p = &(*p)->rb_left;
+        } else if (ctrl->cmd > tmp_ctrl->cmd) {
+            p = &(*p)->rb_right;
+        } else {
+            return tmp_ctrl;
+        }
+    }
+
+    rb_link_node(&(ctrl->tree_node), parent, p);
+
+    return NULL;
+}
+
+
+
+int add_global_ctrl(unsigned int cmd, 
+                   int (*handler)(unsigned int cmd, unsigned long arg)) {
+    struct global_ctrl * ctrl = kmalloc(sizeof(struct global_ctrl), GFP_KERNEL);
+
+    if (ctrl == NULL) {
+        printk("Error: Could not allocate global ctrl %d\n", cmd);
+        return -1;
+    }
+
+    ctrl->cmd = cmd;
+    ctrl->handler = handler;
+
+    if (__insert_global_ctrl(ctrl) != NULL) {
+        printk("Could not insert guest ctrl %d\n", cmd);
+        kfree(ctrl);
+        return -1;
+    }
+    
+    rb_insert_color(&(ctrl->tree_node), &(global_ctrls));
+
+    return 0;
+}
+
+
+struct global_ctrl * get_global_ctrl(unsigned int cmd) {
+    struct rb_node * n = global_ctrls.rb_node;
+    struct global_ctrl * ctrl = NULL;
+
+    while (n) {
+        ctrl = rb_entry(n, struct global_ctrl, tree_node);
+
+        if (cmd < ctrl->cmd) {
+            n = n->rb_left;
+        } else if (cmd > ctrl->cmd) {
+            n = n->rb_right;
+        } else {
+            return ctrl;
+        }
+    }
+
+    return NULL;
+}
+
+
+
+
+
+/*             */
+/* VM Controls */
+/*             */
+
 struct vm_ext {
     struct linux_ext * impl;
     void * vm_data;
@@ -86,6 +168,7 @@ int deinit_vm_extensions(struct v3_guest * guest) {
     return 0;
 }
 
+
 int init_lnx_extensions( void ) {
     extern struct linux_ext * __start__lnx_exts[];
     extern struct linux_ext * __stop__lnx_exts[];
@@ -116,6 +199,7 @@ int deinit_lnx_extensions( void ) {
 
     while (tmp_ext != __stop__lnx_exts[0]) {
        INFO("Cleaning up Linux Extension (%s)\n", tmp_ext->name);
+
        if (tmp_ext->deinit != NULL) {
            tmp_ext->deinit();
        } else {
@@ -127,3 +211,4 @@ int deinit_lnx_extensions( void ) {
     
     return 0;
 }
+
index c5aeea1..289d68a 100644 (file)
@@ -1,9 +1,7 @@
 #include "palacios.h"
 
 
-int add_mod_cmd(struct v3_guest * guest, unsigned int cmd, 
-               int (*handler)(struct v3_guest * guest, 
-                              unsigned int cmd, unsigned long arg));
+
 
 
 struct linux_ext {
@@ -26,6 +24,21 @@ void * get_vm_ext_data(struct v3_guest * guest, char * ext_name);
 
 
 
+struct global_ctrl {
+    unsigned int cmd;
+
+    int (*handler)(unsigned int cmd, unsigned long arg);
+
+    struct rb_node tree_node;
+};
+
+int add_global_ctrl(unsigned int cmd, 
+                   int (*handler)(unsigned int cmd, unsigned long arg));
+
+struct global_ctrl * get_global_ctrl(unsigned int cmd);
+
+
+
 #define register_extension(ext)                                        \
     static struct linux_ext * _lnx_ext                         \
     __attribute__((used))                                      \
index 74301e9..098d8f1 100644 (file)
@@ -174,9 +174,18 @@ out_err:
            break;
        }
 
-       default: 
-           ERROR("\tUnhandled\n");
+
+       default: {
+           struct global_ctrl * ctrl = get_global_ctrl(ioctl);
+           
+           if (ctrl) {
+               return ctrl->handler(ioctl, arg);
+           }
+
+           WARNING("\tUnhandled global ctrl cmd: %d\n", ioctl);
+
            return -EINVAL;
+       }
     }
 
     return 0;
index fd5a188..0141e38 100644 (file)
@@ -12,6 +12,8 @@
 #define V3_FREE_GUEST 13
 
 #define V3_ADD_MEMORY 50
+#define V3_ADD_PCI_HW_DEV 55
+#define V3_ADD_PCI_USER_DEV 56
 
 /* VM Specific IOCTLs */
 #define V3_VM_CONSOLE_CONNECT 20
@@ -69,6 +71,19 @@ struct v3_chkpt_info {
 } __attribute__((packed));
 
 
+struct v3_hw_pci_dev {
+    char name[128];
+    unsigned int bus;
+    unsigned int dev;
+    unsigned int func;
+} __attribute__((packed));
+
+struct v3_user_pci_dev {
+    char name[128];
+    unsigned short vendor_id;
+    unsigned short dev_id;
+} __attribute__((packed));
+
 
 
 void * trace_malloc(size_t size, gfp_t flags);
index f7f6146..e9379bf 100644 (file)
@@ -16,6 +16,12 @@ void init_queue(struct gen_queue * queue, unsigned int max_entries) {
     spin_lock_init(&(queue->lock));
 }
 
+void deinit_queue(struct gen_queue * queue) {
+    while (dequeue(queue)) {
+       ERROR("Freeing non-empty queue. PROBABLE MEMORY LEAK DETECTED\n");
+    }
+}
+
 struct gen_queue * create_queue(unsigned int max_entries) {
     struct gen_queue * tmp_queue = palacios_alloc(sizeof(struct gen_queue));
     if (!tmp_queue) { 
index d4c8ddd..2a60d9a 100644 (file)
@@ -7,12 +7,14 @@
 #define __PALACIOS_QUEUE_H__
 
 
+#include "palacios.h"
 
 #include <linux/list.h>
 #include <linux/spinlock.h>
 
 
 
+
 struct queue_entry {
     void * entry;
     struct list_head node;
@@ -29,6 +31,8 @@ struct gen_queue {
 
 struct gen_queue * create_queue(unsigned int max_entries);
 void init_queue(struct gen_queue * queue, unsigned int max_entries);
+void deinit_queue(struct gen_queue * queue);
+
 
 int enqueue(struct gen_queue * queue, void * entry);
 void * dequeue(struct gen_queue * queue);
index b14cff4..624cc39 100644 (file)
@@ -93,6 +93,8 @@ int add_guest_ctrl(struct v3_guest * guest, unsigned int cmd,
 }
 
 
+
+
 static struct vm_ctrl * get_ctrl(struct v3_guest * guest, unsigned int cmd) {
     struct rb_node * n = guest->vm_ctrls.rb_node;
     struct vm_ctrl * ctrl = NULL;
@@ -112,6 +114,36 @@ static struct vm_ctrl * get_ctrl(struct v3_guest * guest, unsigned int cmd) {
     return NULL;
 }
 
+int remove_guest_ctrl(struct v3_guest * guest, unsigned int cmd) {
+    struct vm_ctrl * ctrl = get_ctrl(guest, cmd);
+
+    if (ctrl == NULL) {
+       INFO("Could not find control (%d) to remove\n", cmd);
+       return -1;
+    }
+
+    rb_erase(&(ctrl->tree_node), &(guest->vm_ctrls));
+
+    kfree(ctrl);
+
+    return 0;
+}
+
+static void free_guest_ctrls(struct v3_guest * guest) {
+    struct rb_node * node = rb_first(&(guest->vm_ctrls));
+    struct vm_ctrl * ctrl = NULL;
+    struct rb_node * tmp_node = NULL;
+
+    while (node) {
+       ctrl = rb_entry(node, struct vm_ctrl, tree_node);
+       tmp_node = node;
+       node = rb_next(node);
+       
+       WARNING("Cleaning up guest ctrl that was not removed explicitly (%d)\n", ctrl->cmd);
+
+       kfree(ctrl);
+    }
+}
 
 
 
@@ -408,6 +440,9 @@ int free_palacios_vm(struct v3_guest * guest) {
 
     cdev_del(&(guest->cdev));
 
+    free_guest_ctrls(guest);
+
+
     vfree(guest->img);
     palacios_free(guest);
 
index 53af2af..fbf1713 100644 (file)
@@ -18,6 +18,8 @@ int add_guest_ctrl(struct v3_guest * guest,  unsigned int cmd,
                                 void * priv_data),
                  void * priv_data);
 
+int remove_guest_ctrl(struct v3_guest * guest, unsigned int cmd);
+
 
 
 #endif
index 5da0c44..888e956 100644 (file)
@@ -5,14 +5,13 @@
 
 # define this at the command-line or here
 # to get dynamically linked versions
-STATIC = 1
+STATIC = 0
 
 #
 # Executables that implement core functionality
 #
 BASE_EXECS =   v3_mem \
                 v3_mem_free \
-               v3_create \
                 v3_create_bind \
                v3_free  \
                v3_launch \
@@ -30,6 +29,8 @@ BASE_EXECS =  v3_mem \
                v3_debug \
                v3_send \
                v3_receive \
+               v3_pci
+
 
 #
 # Examples
@@ -101,6 +102,9 @@ v3_cons: v3_cons.c
 v3_cons_sc: v3_cons_sc.c
        $(CC) $(CFLAGS) $(CURSES_CFLAGS) $< -lcurses -o $@
 
+v3_create: v3_create.c ezxml.c
+       $(CC) $(CFLAGS) $^ -o $@
+
 
 
 #
diff --git a/linux_usr/ezxml.c b/linux_usr/ezxml.c
new file mode 100644 (file)
index 0000000..82b11fb
--- /dev/null
@@ -0,0 +1,1015 @@
+/* ezxml.c
+ *
+ * Copyright 2004-2006 Aaron Voisine <aaron@voisine.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <sys/types.h>
+#ifndef EZXML_NOMMAP
+#include <sys/mman.h>
+#endif // EZXML_NOMMAP
+#include <sys/stat.h>
+#include "ezxml.h"
+
+#define EZXML_WS   "\t\r\n "  // whitespace
+#define EZXML_ERRL 128        // maximum error string length
+
+typedef struct ezxml_root *ezxml_root_t;
+struct ezxml_root {       // additional data for the root tag
+    struct ezxml xml;     // is a super-struct built on top of ezxml struct
+    ezxml_t cur;          // current xml tree insertion point
+    char *m;              // original xml string
+    size_t len;           // length of allocated memory for mmap, -1 for malloc
+    char *u;              // UTF-8 conversion of string if original was UTF-16
+    char *s;              // start of work area
+    char *e;              // end of work area
+    char **ent;           // general entities (ampersand sequences)
+    char ***attr;         // default attributes
+    char ***pi;           // processing instructions
+    short standalone;     // non-zero if <?xml standalone="yes"?>
+    char err[EZXML_ERRL]; // error string
+};
+
+char *EZXML_NIL[] = { NULL }; // empty, null terminated array of strings
+
+// returns the first child tag with the given name or NULL if not found
+ezxml_t ezxml_child(ezxml_t xml, const char *name)
+{
+    xml = (xml) ? xml->child : NULL;
+    while (xml && strcmp(name, xml->name)) xml = xml->sibling;
+    return xml;
+}
+
+// returns the Nth tag with the same name in the same subsection or NULL if not
+// found
+ezxml_t ezxml_idx(ezxml_t xml, int idx)
+{
+    for (; xml && idx; idx--) xml = xml->next;
+    return xml;
+}
+
+// returns the value of the requested tag attribute or NULL if not found
+const char *ezxml_attr(ezxml_t xml, const char *attr)
+{
+    int i = 0, j = 1;
+    ezxml_root_t root = (ezxml_root_t)xml;
+
+    if (! xml || ! xml->attr) return NULL;
+    while (xml->attr[i] && strcmp(attr, xml->attr[i])) i += 2;
+    if (xml->attr[i]) return xml->attr[i + 1]; // found attribute
+
+    while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
+    for (i = 0; root->attr[i] && strcmp(xml->name, root->attr[i][0]); i++);
+    if (! root->attr[i]) return NULL; // no matching default attributes
+    while (root->attr[i][j] && strcmp(attr, root->attr[i][j])) j += 3;
+    return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; // found default
+}
+
+// same as ezxml_get but takes an already initialized va_list
+ezxml_t ezxml_vget(ezxml_t xml, va_list ap)
+{
+    char *name = va_arg(ap, char *);
+    int idx = -1;
+
+    if (name && *name) {
+        idx = va_arg(ap, int);    
+        xml = ezxml_child(xml, name);
+    }
+    return (idx < 0) ? xml : ezxml_vget(ezxml_idx(xml, idx), ap);
+}
+
+// Traverses the xml tree to retrieve a specific subtag. Takes a variable
+// length list of tag names and indexes. The argument list must be terminated
+// by either an index of -1 or an empty string tag name. Example: 
+// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
+// This retrieves the title of the 3rd book on the 1st shelf of library.
+// Returns NULL if not found.
+ezxml_t ezxml_get(ezxml_t xml, ...)
+{
+    va_list ap;
+    ezxml_t r;
+
+    va_start(ap, xml);
+    r = ezxml_vget(xml, ap);
+    va_end(ap);
+    return r;
+}
+
+// returns a null terminated array of processing instructions for the given
+// target
+const char **ezxml_pi(ezxml_t xml, const char *target)
+{
+    ezxml_root_t root = (ezxml_root_t)xml;
+    int i = 0;
+
+    if (! root) return (const char **)EZXML_NIL;
+    while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
+    while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target
+    return (const char **)((root->pi[i]) ? root->pi[i] + 1 : EZXML_NIL);
+}
+
+// set an error string and return root
+ezxml_t ezxml_err(ezxml_root_t root, char *s, const char *err, ...)
+{
+    va_list ap;
+    int line = 1;
+    char *t, fmt[EZXML_ERRL];
+    
+    for (t = root->s; t < s; t++) if (*t == '\n') line++;
+    snprintf(fmt, EZXML_ERRL, "[error near line %d]: %s", line, err);
+
+    va_start(ap, err);
+    vsnprintf(root->err, EZXML_ERRL, fmt, ap);
+    va_end(ap);
+
+    return &root->xml;
+}
+
+// Recursively decodes entity and character references and normalizes new lines
+// ent is a null terminated array of alternating entity names and values. set t
+// to '&' for general entity decoding, '%' for parameter entity decoding, 'c'
+// for cdata sections, ' ' for attribute normalization, or '*' for non-cdata
+// attribute normalization. Returns s, or if the decoded string is longer than
+// s, returns a malloced string that must be freed.
+char *ezxml_decode(char *s, char **ent, char t)
+{
+    char *e, *r = s, *m = s;
+    long b, c, d, l;
+
+    for (; *s; s++) { // normalize line endings
+        while (*s == '\r') {
+            *(s++) = '\n';
+            if (*s == '\n') memmove(s, (s + 1), strlen(s));
+        }
+    }
+    
+    for (s = r; ; ) {
+        while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++;
+
+        if (! *s) break;
+        else if (t != 'c' && ! strncmp(s, "&#", 2)) { // character reference
+            if (s[2] == 'x') c = strtol(s + 3, &e, 16); // base 16
+            else c = strtol(s + 2, &e, 10); // base 10
+            if (! c || *e != ';') { s++; continue; } // not a character ref
+
+            if (c < 0x80) *(s++) = c; // US-ASCII subset
+            else { // multi-byte UTF-8 sequence
+                for (b = 0, d = c; d; d /= 2) b++; // number of bits in c
+                b = (b - 2) / 5; // number of bytes in payload
+                *(s++) = (0xFF << (7 - b)) | (c >> (6 * b)); // head
+                while (b) *(s++) = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
+            }
+
+            memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
+        }
+        else if ((*s == '&' && (t == '&' || t == ' ' || t == '*')) ||
+                 (*s == '%' && t == '%')) { // entity reference
+            for (b = 0; ent[b] && strncmp(s + 1, ent[b], strlen(ent[b]));
+                 b += 2); // find entity in entity list
+
+            if (ent[b++]) { // found a match
+                if ((c = strlen(ent[b])) - 1 > (e = strchr(s, ';')) - s) {
+                    l = (d = (s - r)) + c + strlen(e); // new length
+                    r = (r == m) ? strcpy(malloc(l), r) : realloc(r, l);
+                    e = strchr((s = r + d), ';'); // fix up pointers
+                }
+
+                memmove(s + c, e + 1, strlen(e)); // shift rest of string
+                strncpy(s, ent[b], c); // copy in replacement text
+            }
+            else s++; // not a known entity
+        }
+        else if ((t == ' ' || t == '*') && isspace(*s)) *(s++) = ' ';
+        else s++; // no decoding needed
+    }
+
+    if (t == '*') { // normalize spaces for non-cdata attributes
+        for (s = r; *s; s++) {
+            if ((l = strspn(s, " "))) memmove(s, s + l, strlen(s + l) + 1);
+            while (*s && *s != ' ') s++;
+        }
+        if (--s >= r && *s == ' ') *s = '\0'; // trim any trailing space
+    }
+    return r;
+}
+
+// called when parser finds start of new tag
+void ezxml_open_tag(ezxml_root_t root, char *name, char **attr)
+{
+    ezxml_t xml = root->cur;
+    
+    if (xml->name) xml = ezxml_add_child(xml, name, strlen(xml->txt));
+    else xml->name = name; // first open tag
+
+    xml->attr = attr;
+    root->cur = xml; // update tag insertion point
+}
+
+// called when parser finds character content between open and closing tag
+void ezxml_char_content(ezxml_root_t root, char *s, size_t len, char t)
+{
+    ezxml_t xml = root->cur;
+    char *m = s;
+    size_t l;
+
+    if (! xml || ! xml->name || ! len) return; // sanity check
+
+    s[len] = '\0'; // null terminate text (calling functions anticipate this)
+    len = strlen(s = ezxml_decode(s, root->ent, t)) + 1;
+
+    if (! *(xml->txt)) xml->txt = s; // initial character content
+    else { // allocate our own memory and make a copy
+        xml->txt = (xml->flags & EZXML_TXTM) // allocate some space
+                   ? realloc(xml->txt, (l = strlen(xml->txt)) + len)
+                   : strcpy(malloc((l = strlen(xml->txt)) + len), xml->txt);
+        strcpy(xml->txt + l, s); // add new char content
+        if (s != m) free(s); // free s if it was malloced by ezxml_decode()
+    }
+
+    if (xml->txt != m) ezxml_set_flag(xml, EZXML_TXTM);
+}
+
+// called when parser finds closing tag
+ezxml_t ezxml_close_tag(ezxml_root_t root, char *name, char *s)
+{
+    if (! root->cur || ! root->cur->name || strcmp(name, root->cur->name))
+        return ezxml_err(root, s, "unexpected closing tag </%s>", name);
+
+    root->cur = root->cur->parent;
+    return NULL;
+}
+
+// checks for circular entity references, returns non-zero if no circular
+// references are found, zero otherwise
+int ezxml_ent_ok(char *name, char *s, char **ent)
+{
+    int i;
+
+    for (; ; s++) {
+        while (*s && *s != '&') s++; // find next entity reference
+        if (! *s) return 1;
+        if (! strncmp(s + 1, name, strlen(name))) return 0; // circular ref.
+        for (i = 0; ent[i] && strncmp(ent[i], s + 1, strlen(ent[i])); i += 2);
+        if (ent[i] && ! ezxml_ent_ok(name, ent[i + 1], ent)) return 0;
+    }
+}
+
+// called when the parser finds a processing instruction
+void ezxml_proc_inst(ezxml_root_t root, char *s, size_t len)
+{
+    int i = 0, j = 1;
+    char *target = s;
+
+    s[len] = '\0'; // null terminate instruction
+    if (*(s += strcspn(s, EZXML_WS))) {
+        *s = '\0'; // null terminate target
+        s += strspn(s + 1, EZXML_WS) + 1; // skip whitespace after target
+    }
+
+    if (! strcmp(target, "xml")) { // <?xml ... ?>
+        if ((s = strstr(s, "standalone")) && ! strncmp(s + strspn(s + 10,
+            EZXML_WS "='\"") + 10, "yes", 3)) root->standalone = 1;
+        return;
+    }
+
+    if (! root->pi[0]) *(root->pi = malloc(sizeof(char **))) = NULL; //first pi
+
+    while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target
+    if (! root->pi[i]) { // new target
+        root->pi = realloc(root->pi, sizeof(char **) * (i + 2));
+        root->pi[i] = malloc(sizeof(char *) * 3);
+        root->pi[i][0] = target;
+        root->pi[i][1] = (char *)(root->pi[i + 1] = NULL); // terminate pi list
+        root->pi[i][2] = strdup(""); // empty document position list
+    }
+
+    while (root->pi[i][j]) j++; // find end of instruction list for this target
+    root->pi[i] = realloc(root->pi[i], sizeof(char *) * (j + 3));
+    root->pi[i][j + 2] = realloc(root->pi[i][j + 1], j + 1);
+    strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<");
+    root->pi[i][j + 1] = NULL; // null terminate pi list for this target
+    root->pi[i][j] = s; // set instruction
+}
+
+// called when the parser finds an internal doctype subset
+short ezxml_internal_dtd(ezxml_root_t root, char *s, size_t len)
+{
+    char q, *c, *t, *n = NULL, *v, **ent, **pe;
+    int i, j;
+    
+    pe = memcpy(malloc(sizeof(EZXML_NIL)), EZXML_NIL, sizeof(EZXML_NIL));
+
+    for (s[len] = '\0'; s; ) {
+        while (*s && *s != '<' && *s != '%') s++; // find next declaration
+
+        if (! *s) break;
+        else if (! strncmp(s, "<!ENTITY", 8)) { // parse entity definitions
+            c = s += strspn(s + 8, EZXML_WS) + 8; // skip white space separator
+            n = s + strspn(s, EZXML_WS "%"); // find name
+            *(s = n + strcspn(n, EZXML_WS)) = ';'; // append ; to name
+
+            v = s + strspn(s + 1, EZXML_WS) + 1; // find value
+            if ((q = *(v++)) != '"' && q != '\'') { // skip externals
+                s = strchr(s, '>');
+                continue;
+            }
+
+            for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++);
+            ent = realloc(ent, (i + 3) * sizeof(char *)); // space for next ent
+            if (*c == '%') pe = ent;
+            else root->ent = ent;
+
+            *(++s) = '\0'; // null terminate name
+            if ((s = strchr(v, q))) *(s++) = '\0'; // null terminate value
+            ent[i + 1] = ezxml_decode(v, pe, '%'); // set value
+            ent[i + 2] = NULL; // null terminate entity list
+            if (! ezxml_ent_ok(n, ent[i + 1], ent)) { // circular reference
+                if (ent[i + 1] != v) free(ent[i + 1]);
+                ezxml_err(root, v, "circular entity declaration &%s", n);
+                break;
+            }
+            else ent[i] = n; // set entity name
+        }
+        else if (! strncmp(s, "<!ATTLIST", 9)) { // parse default attributes
+            t = s + strspn(s + 9, EZXML_WS) + 9; // skip whitespace separator
+            if (! *t) { ezxml_err(root, t, "unclosed <!ATTLIST"); break; }
+            if (*(s = t + strcspn(t, EZXML_WS ">")) == '>') continue;
+            else *s = '\0'; // null terminate tag name
+            for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++);
+
+            while (*(n = ++s + strspn(s, EZXML_WS)) && *n != '>') {
+                if (*(s = n + strcspn(n, EZXML_WS))) *s = '\0'; // attr name
+                else { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
+
+                s += strspn(s + 1, EZXML_WS) + 1; // find next token
+                c = (strncmp(s, "CDATA", 5)) ? "*" : " "; // is it cdata?
+                if (! strncmp(s, "NOTATION", 8))
+                    s += strspn(s + 8, EZXML_WS) + 8;
+                s = (*s == '(') ? strchr(s, ')') : s + strcspn(s, EZXML_WS);
+                if (! s) { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
+
+                s += strspn(s, EZXML_WS ")"); // skip white space separator
+                if (! strncmp(s, "#FIXED", 6))
+                    s += strspn(s + 6, EZXML_WS) + 6;
+                if (*s == '#') { // no default value
+                    s += strcspn(s, EZXML_WS ">") - 1;
+                    if (*c == ' ') continue; // cdata is default, nothing to do
+                    v = NULL;
+                }
+                else if ((*s == '"' || *s == '\'')  &&  // default value
+                         (s = strchr(v = s + 1, *s))) *s = '\0';
+                else { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
+
+                if (! root->attr[i]) { // new tag name
+                    root->attr = (! i) ? malloc(2 * sizeof(char **))
+                                       : realloc(root->attr,
+                                                 (i + 2) * sizeof(char **));
+                    root->attr[i] = malloc(2 * sizeof(char *));
+                    root->attr[i][0] = t; // set tag name
+                    root->attr[i][1] = (char *)(root->attr[i + 1] = NULL);
+                }
+
+                for (j = 1; root->attr[i][j]; j += 3); // find end of list
+                root->attr[i] = realloc(root->attr[i],
+                                        (j + 4) * sizeof(char *));
+
+                root->attr[i][j + 3] = NULL; // null terminate list
+                root->attr[i][j + 2] = c; // is it cdata?
+                root->attr[i][j + 1] = (v) ? ezxml_decode(v, root->ent, *c)
+                                           : NULL;
+                root->attr[i][j] = n; // attribute name 
+            }
+        }
+        else if (! strncmp(s, "<!--", 4)) s = strstr(s + 4, "-->"); // comments
+        else if (! strncmp(s, "<?", 2)) { // processing instructions
+            if ((s = strstr(c = s + 2, "?>")))
+                ezxml_proc_inst(root, c, s++ - c);
+        }
+        else if (*s == '<') s = strchr(s, '>'); // skip other declarations
+        else if (*(s++) == '%' && ! root->standalone) break;
+    }
+
+    free(pe);
+    return ! *root->err;
+}
+
+// Converts a UTF-16 string to UTF-8. Returns a new string that must be freed
+// or NULL if no conversion was needed.
+char *ezxml_str2utf8(char **s, size_t *len)
+{
+    char *u;
+    size_t l = 0, sl, max = *len;
+    long c, d;
+    int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1;
+
+    if (be == -1) return NULL; // not UTF-16
+
+    u = malloc(max);
+    for (sl = 2; sl < *len - 1; sl += 2) {
+        c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF)  //UTF-16BE
+                 : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); //UTF-16LE
+        if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { // high-half
+            d = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF)
+                     : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF);
+            c = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000;
+        }
+
+        while (l + 6 > max) u = realloc(u, max += EZXML_BUFSIZE);
+        if (c < 0x80) u[l++] = c; // US-ASCII subset
+        else { // multi-byte UTF-8 sequence
+            for (b = 0, d = c; d; d /= 2) b++; // bits in c
+            b = (b - 2) / 5; // bytes in payload
+            u[l++] = (0xFF << (7 - b)) | (c >> (6 * b)); // head
+            while (b) u[l++] = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
+        }
+    }
+    return *s = realloc(u, *len = l);
+}
+
+// frees a tag attribute list
+void ezxml_free_attr(char **attr) {
+    int i = 0;
+    char *m;
+    
+    if (! attr || attr == EZXML_NIL) return; // nothing to free
+    while (attr[i]) i += 2; // find end of attribute list
+    m = attr[i + 1]; // list of which names and values are malloced
+    for (i = 0; m[i]; i++) {
+        if (m[i] & EZXML_NAMEM) free(attr[i * 2]);
+        if (m[i] & EZXML_TXTM) free(attr[(i * 2) + 1]);
+    }
+    free(m);
+    free(attr);
+}
+
+// parse the given xml string and return an ezxml structure
+ezxml_t ezxml_parse_str(char *s, size_t len)
+{
+    ezxml_root_t root = (ezxml_root_t)ezxml_new(NULL);
+    char q, e, *d, **attr, **a = NULL; // initialize a to avoid compile warning
+    int l, i, j;
+
+    root->m = s;
+    if (! len) return ezxml_err(root, NULL, "root tag missing");
+    root->u = ezxml_str2utf8(&s, &len); // convert utf-16 to utf-8
+    root->e = (root->s = s) + len; // record start and end of work area
+    
+    e = s[len - 1]; // save end char
+    s[len - 1] = '\0'; // turn end char into null terminator
+
+    while (*s && *s != '<') s++; // find first tag
+    if (! *s) return ezxml_err(root, s, "root tag missing");
+
+    for (; ; ) {
+        attr = (char **)EZXML_NIL;
+        d = ++s;
+        
+        if (isalpha(*s) || *s == '_' || *s == ':' || *s < '\0') { // new tag
+            if (! root->cur)
+                return ezxml_err(root, d, "markup outside of root element");
+
+            s += strcspn(s, EZXML_WS "/>");
+            while (isspace(*s)) *(s++) = '\0'; // null terminate tag name
+  
+            if (*s && *s != '/' && *s != '>') // find tag in default attr list
+                for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++);
+
+            for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { // new attrib
+                attr = (l) ? realloc(attr, (l + 4) * sizeof(char *))
+                           : malloc(4 * sizeof(char *)); // allocate space
+                attr[l + 3] = (l) ? realloc(attr[l + 1], (l / 2) + 2)
+                                  : malloc(2); // mem for list of maloced vals
+                strcpy(attr[l + 3] + (l / 2), " "); // value is not malloced
+                attr[l + 2] = NULL; // null terminate list
+                attr[l + 1] = ""; // temporary attribute value
+                attr[l] = s; // set attribute name
+
+                s += strcspn(s, EZXML_WS "=/>");
+                if (*s == '=' || isspace(*s)) { 
+                    *(s++) = '\0'; // null terminate tag attribute name
+                    q = *(s += strspn(s, EZXML_WS "="));
+                    if (q == '"' || q == '\'') { // attribute value
+                        attr[l + 1] = ++s;
+                        while (*s && *s != q) s++;
+                        if (*s) *(s++) = '\0'; // null terminate attribute val
+                        else {
+                            ezxml_free_attr(attr);
+                            return ezxml_err(root, d, "missing %c", q);
+                        }
+
+                        for (j = 1; a && a[j] && strcmp(a[j], attr[l]); j +=3);
+                        attr[l + 1] = ezxml_decode(attr[l + 1], root->ent, (a
+                                                   && a[j]) ? *a[j + 2] : ' ');
+                        if (attr[l + 1] < d || attr[l + 1] > s)
+                            attr[l + 3][l / 2] = EZXML_TXTM; // value malloced
+                    }
+                }
+                while (isspace(*s)) s++;
+            }
+
+            if (*s == '/') { // self closing tag
+                *(s++) = '\0';
+                if ((*s && *s != '>') || (! *s && e != '>')) {
+                    if (l) ezxml_free_attr(attr);
+                    return ezxml_err(root, d, "missing >");
+                }
+                ezxml_open_tag(root, d, attr);
+                ezxml_close_tag(root, d, s);
+            }
+            else if ((q = *s) == '>' || (! *s && e == '>')) { // open tag
+                *s = '\0'; // temporarily null terminate tag name
+                ezxml_open_tag(root, d, attr);
+                *s = q;
+            }
+            else {
+                if (l) ezxml_free_attr(attr);
+                return ezxml_err(root, d, "missing >"); 
+            }
+        }
+        else if (*s == '/') { // close tag
+            s += strcspn(d = s + 1, EZXML_WS ">") + 1;
+            if (! (q = *s) && e != '>') return ezxml_err(root, d, "missing >");
+            *s = '\0'; // temporarily null terminate tag name
+            if (ezxml_close_tag(root, d, s)) return &root->xml;
+            if (isspace(*s = q)) s += strspn(s, EZXML_WS);
+        }
+        else if (! strncmp(s, "!--", 3)) { // xml comment
+            if (! (s = strstr(s + 3, "--")) || (*(s += 2) != '>' && *s) ||
+                (! *s && e != '>')) return ezxml_err(root, d, "unclosed <!--");
+        }
+        else if (! strncmp(s, "![CDATA[", 8)) { // cdata
+            if ((s = strstr(s, "]]>")))
+                ezxml_char_content(root, d + 8, (s += 2) - d - 10, 'c');
+            else return ezxml_err(root, d, "unclosed <![CDATA[");
+        }
+        else if (! strncmp(s, "!DOCTYPE", 8)) { // dtd
+            for (l = 0; *s && ((! l && *s != '>') || (l && (*s != ']' || 
+                 *(s + strspn(s + 1, EZXML_WS) + 1) != '>')));
+                 l = (*s == '[') ? 1 : l) s += strcspn(s + 1, "[]>") + 1;
+            if (! *s && e != '>')
+                return ezxml_err(root, d, "unclosed <!DOCTYPE");
+            d = (l) ? strchr(d, '[') + 1 : d;
+            if (l && ! ezxml_internal_dtd(root, d, s++ - d)) return &root->xml;
+        }
+        else if (*s == '?') { // <?...?> processing instructions
+            do { s = strchr(s, '?'); } while (s && *(++s) && *s != '>');
+            if (! s || (! *s && e != '>')) 
+                return ezxml_err(root, d, "unclosed <?");
+            else ezxml_proc_inst(root, d + 1, s - d - 2);
+        }
+        else return ezxml_err(root, d, "unexpected <");
+        
+        if (! s || ! *s) break;
+        *s = '\0';
+        d = ++s;
+        if (*s && *s != '<') { // tag character content
+            while (*s && *s != '<') s++;
+            if (*s) ezxml_char_content(root, d, s - d, '&');
+            else break;
+        }
+        else if (! *s) break;
+    }
+
+    if (! root->cur) return &root->xml;
+    else if (! root->cur->name) return ezxml_err(root, d, "root tag missing");
+    else return ezxml_err(root, d, "unclosed tag <%s>", root->cur->name);
+}
+
+// Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
+// stream into memory and then parses it. For xml files, use ezxml_parse_file()
+// or ezxml_parse_fd()
+ezxml_t ezxml_parse_fp(FILE *fp)
+{
+    ezxml_root_t root;
+    size_t l, len = 0;
+    char *s;
+
+    if (! (s = malloc(EZXML_BUFSIZE))) return NULL;
+    do {
+        len += (l = fread((s + len), 1, EZXML_BUFSIZE, fp));
+        if (l == EZXML_BUFSIZE) s = realloc(s, len + EZXML_BUFSIZE);
+    } while (s && l == EZXML_BUFSIZE);
+
+    if (! s) return NULL;
+    root = (ezxml_root_t)ezxml_parse_str(s, len);
+    root->len = -1; // so we know to free s in ezxml_free()
+    return &root->xml;
+}
+
+// A wrapper for ezxml_parse_str() that accepts a file descriptor. First
+// attempts to mem map the file. Failing that, reads the file into memory.
+// Returns NULL on failure.
+ezxml_t ezxml_parse_fd(int fd)
+{
+    ezxml_root_t root;
+    struct stat st;
+    size_t l;
+    void *m;
+
+    if (fd < 0) return NULL;
+    fstat(fd, &st);
+
+#ifndef EZXML_NOMMAP
+    l = (st.st_size + sysconf(_SC_PAGESIZE) - 1) & ~(sysconf(_SC_PAGESIZE) -1);
+    if ((m = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) !=
+        MAP_FAILED) {
+        madvise(m, l, MADV_SEQUENTIAL); // optimize for sequential access
+        root = (ezxml_root_t)ezxml_parse_str(m, st.st_size);
+        madvise(m, root->len = l, MADV_NORMAL); // put it back to normal
+    }
+    else { // mmap failed, read file into memory
+#endif // EZXML_NOMMAP
+        l = read(fd, m = malloc(st.st_size), st.st_size);
+        root = (ezxml_root_t)ezxml_parse_str(m, l);
+        root->len = -1; // so we know to free s in ezxml_free()
+#ifndef EZXML_NOMMAP
+    }
+#endif // EZXML_NOMMAP
+    return &root->xml;
+}
+
+// a wrapper for ezxml_parse_fd that accepts a file name
+ezxml_t ezxml_parse_file(const char *file)
+{
+    int fd = open(file, O_RDONLY, 0);
+    ezxml_t xml = ezxml_parse_fd(fd);
+    
+    if (fd >= 0) close(fd);
+    return xml;
+}
+
+// Encodes ampersand sequences appending the results to *dst, reallocating *dst
+// if length excedes max. a is non-zero for attribute encoding. Returns *dst
+char *ezxml_ampencode(const char *s, size_t len, char **dst, size_t *dlen,
+                      size_t *max, short a)
+{
+    const char *e;
+    
+    for (e = s + len; s != e; s++) {
+        while (*dlen + 10 > *max) *dst = realloc(*dst, *max += EZXML_BUFSIZE);
+
+        switch (*s) {
+        case '\0': return *dst;
+        case '&': *dlen += sprintf(*dst + *dlen, "&amp;"); break;
+        case '<': *dlen += sprintf(*dst + *dlen, "&lt;"); break;
+        case '>': *dlen += sprintf(*dst + *dlen, "&gt;"); break;
+        case '"': *dlen += sprintf(*dst + *dlen, (a) ? "&quot;" : "\""); break;
+        case '\n': *dlen += sprintf(*dst + *dlen, (a) ? "&#xA;" : "\n"); break;
+        case '\t': *dlen += sprintf(*dst + *dlen, (a) ? "&#x9;" : "\t"); break;
+        case '\r': *dlen += sprintf(*dst + *dlen, "&#xD;"); break;
+        default: (*dst)[(*dlen)++] = *s;
+        }
+    }
+    return *dst;
+}
+
+// Recursively converts each tag to xml appending it to *s. Reallocates *s if
+// its length excedes max. start is the location of the previous tag in the
+// parent tag's character content. Returns *s.
+char *ezxml_toxml_r(ezxml_t xml, char **s, size_t *len, size_t *max,
+                    size_t start, char ***attr)
+{
+    int i, j;
+    char *txt = (xml->parent) ? xml->parent->txt : "";
+    size_t off = 0;
+
+    // parent character content up to this tag
+    *s = ezxml_ampencode(txt + start, xml->off - start, s, len, max, 0);
+
+    while (*len + strlen(xml->name) + 4 > *max) // reallocate s
+        *s = realloc(*s, *max += EZXML_BUFSIZE);
+
+    *len += sprintf(*s + *len, "<%s", xml->name); // open tag
+    for (i = 0; xml->attr[i]; i += 2) { // tag attributes
+        if (ezxml_attr(xml, xml->attr[i]) != xml->attr[i + 1]) continue;
+        while (*len + strlen(xml->attr[i]) + 7 > *max) // reallocate s
+            *s = realloc(*s, *max += EZXML_BUFSIZE);
+
+        *len += sprintf(*s + *len, " %s=\"", xml->attr[i]);
+        ezxml_ampencode(xml->attr[i + 1], -1, s, len, max, 1);
+        *len += sprintf(*s + *len, "\"");
+    }
+
+    for (i = 0; attr[i] && strcmp(attr[i][0], xml->name); i++);
+    for (j = 1; attr[i] && attr[i][j]; j += 3) { // default attributes
+        if (! attr[i][j + 1] || ezxml_attr(xml, attr[i][j]) != attr[i][j + 1])
+            continue; // skip duplicates and non-values
+        while (*len + strlen(attr[i][j]) + 7 > *max) // reallocate s
+            *s = realloc(*s, *max += EZXML_BUFSIZE);
+
+        *len += sprintf(*s + *len, " %s=\"", attr[i][j]);
+        ezxml_ampencode(attr[i][j + 1], -1, s, len, max, 1);
+        *len += sprintf(*s + *len, "\"");
+    }
+    *len += sprintf(*s + *len, ">");
+
+    *s = (xml->child) ? ezxml_toxml_r(xml->child, s, len, max, 0, attr) //child
+                      : ezxml_ampencode(xml->txt, -1, s, len, max, 0);  //data
+    
+    while (*len + strlen(xml->name) + 4 > *max) // reallocate s
+        *s = realloc(*s, *max += EZXML_BUFSIZE);
+
+    *len += sprintf(*s + *len, "</%s>", xml->name); // close tag
+
+    while (txt[off] && off < xml->off) off++; // make sure off is within bounds
+    return (xml->ordered) ? ezxml_toxml_r(xml->ordered, s, len, max, off, attr)
+                          : ezxml_ampencode(txt + off, -1, s, len, max, 0);
+}
+
+// Converts an ezxml structure back to xml. Returns a string of xml data that
+// must be freed.
+char *ezxml_toxml(ezxml_t xml)
+{
+    ezxml_t p = (xml) ? xml->parent : NULL, o = (xml) ? xml->ordered : NULL;
+    ezxml_root_t root = (ezxml_root_t)xml;
+    size_t len = 0, max = EZXML_BUFSIZE;
+    char *s = strcpy(malloc(max), ""), *t, *n;
+    int i, j, k;
+
+    if (! xml || ! xml->name) return realloc(s, len + 1);
+    while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
+
+    for (i = 0; ! p && root->pi[i]; i++) { // pre-root processing instructions
+        for (k = 2; root->pi[i][k - 1]; k++);
+        for (j = 1; (n = root->pi[i][j]); j++) {
+            if (root->pi[i][k][j - 1] == '>') continue; // not pre-root
+            while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max)
+                s = realloc(s, max += EZXML_BUFSIZE);
+            len += sprintf(s + len, "<?%s%s%s?>\n", t, *n ? " " : "", n);
+        }
+    }
+
+    xml->parent = xml->ordered = NULL;
+    s = ezxml_toxml_r(xml, &s, &len, &max, 0, root->attr);
+    xml->parent = p;
+    xml->ordered = o;
+
+    for (i = 0; ! p && root->pi[i]; i++) { // post-root processing instructions
+        for (k = 2; root->pi[i][k - 1]; k++);
+        for (j = 1; (n = root->pi[i][j]); j++) {
+            if (root->pi[i][k][j - 1] == '<') continue; // not post-root
+            while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max)
+                s = realloc(s, max += EZXML_BUFSIZE);
+            len += sprintf(s + len, "\n<?%s%s%s?>", t, *n ? " " : "", n);
+        }
+    }
+    return realloc(s, len + 1);
+}
+
+// free the memory allocated for the ezxml structure
+void ezxml_free(ezxml_t xml)
+{
+    ezxml_root_t root = (ezxml_root_t)xml;
+    int i, j;
+    char **a, *s;
+
+    if (! xml) return;
+    ezxml_free(xml->child);
+    ezxml_free(xml->ordered);
+
+    if (! xml->parent) { // free root tag allocations
+        for (i = 10; root->ent[i]; i += 2) // 0 - 9 are default entites (<>&"')
+            if ((s = root->ent[i + 1]) < root->s || s > root->e) free(s);
+        free(root->ent); // free list of general entities
+
+        for (i = 0; (a = root->attr[i]); i++) {
+            for (j = 1; a[j++]; j += 2) // free malloced attribute values
+                if (a[j] && (a[j] < root->s || a[j] > root->e)) free(a[j]);
+            free(a);
+        }
+        if (root->attr[0]) free(root->attr); // free default attribute list
+
+        for (i = 0; root->pi[i]; i++) {
+            for (j = 1; root->pi[i][j]; j++);
+            free(root->pi[i][j + 1]);
+            free(root->pi[i]);
+        }            
+        if (root->pi[0]) free(root->pi); // free processing instructions
+
+        if (root->len == -1) free(root->m); // malloced xml data
+#ifndef EZXML_NOMMAP
+        else if (root->len) munmap(root->m, root->len); // mem mapped xml data
+#endif // EZXML_NOMMAP
+        if (root->u) free(root->u); // utf8 conversion
+    }
+
+    ezxml_free_attr(xml->attr); // tag attributes
+    if ((xml->flags & EZXML_TXTM)) free(xml->txt); // character content
+    if ((xml->flags & EZXML_NAMEM)) free(xml->name); // tag name
+    free(xml);
+}
+
+// return parser error message or empty string if none
+const char *ezxml_error(ezxml_t xml)
+{
+    while (xml && xml->parent) xml = xml->parent; // find root tag
+    return (xml) ? ((ezxml_root_t)xml)->err : "";
+}
+
+// returns a new empty ezxml structure with the given root tag name
+ezxml_t ezxml_new(const char *name)
+{
+    static char *ent[] = { "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
+                           "apos;", "&#39;", "amp;", "&#38;", NULL };
+    ezxml_root_t root = (ezxml_root_t)memset(malloc(sizeof(struct ezxml_root)), 
+                                             '\0', sizeof(struct ezxml_root));
+    root->xml.name = (char *)name;
+    root->cur = &root->xml;
+    strcpy(root->err, root->xml.txt = "");
+    root->ent = memcpy(malloc(sizeof(ent)), ent, sizeof(ent));
+    root->attr = root->pi = (char ***)(root->xml.attr = EZXML_NIL);
+    return &root->xml;
+}
+
+// inserts an existing tag into an ezxml structure
+ezxml_t ezxml_insert(ezxml_t xml, ezxml_t dest, size_t off)
+{
+    ezxml_t cur, prev, head;
+
+    xml->next = xml->sibling = xml->ordered = NULL;
+    xml->off = off;
+    xml->parent = dest;
+
+    if ((head = dest->child)) { // already have sub tags
+        if (head->off <= off) { // not first subtag
+            for (cur = head; cur->ordered && cur->ordered->off <= off;
+                 cur = cur->ordered);
+            xml->ordered = cur->ordered;
+            cur->ordered = xml;
+        }
+        else { // first subtag
+            xml->ordered = head;
+            dest->child = xml;
+        }
+
+        for (cur = head, prev = NULL; cur && strcmp(cur->name, xml->name);
+             prev = cur, cur = cur->sibling); // find tag type
+        if (cur && cur->off <= off) { // not first of type
+            while (cur->next && cur->next->off <= off) cur = cur->next;
+            xml->next = cur->next;
+            cur->next = xml;
+        }
+        else { // first tag of this type
+            if (prev && cur) prev->sibling = cur->sibling; // remove old first
+            xml->next = cur; // old first tag is now next
+            for (cur = head, prev = NULL; cur && cur->off <= off;
+                 prev = cur, cur = cur->sibling); // new sibling insert point
+            xml->sibling = cur;
+            if (prev) prev->sibling = xml;
+        }
+    }
+    else dest->child = xml; // only sub tag
+
+    return xml;
+}
+
+// Adds a child tag. off is the offset of the child tag relative to the start
+// of the parent tag's character content. Returns the child tag.
+ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off)
+{
+    ezxml_t child;
+
+    if (! xml) return NULL;
+    child = (ezxml_t)memset(malloc(sizeof(struct ezxml)), '\0',
+                            sizeof(struct ezxml));
+    child->name = (char *)name;
+    child->attr = EZXML_NIL;
+    child->txt = "";
+
+    return ezxml_insert(child, xml, off);
+}
+
+// sets the character content for the given tag and returns the tag
+ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt)
+{
+    if (! xml) return NULL;
+    if (xml->flags & EZXML_TXTM) free(xml->txt); // existing txt was malloced
+    xml->flags &= ~EZXML_TXTM;
+    xml->txt = (char *)txt;
+    return xml;
+}
+
+// Sets the given tag attribute or adds a new attribute if not found. A value
+// of NULL will remove the specified attribute. Returns the tag given.
+ezxml_t ezxml_set_attr(ezxml_t xml, const char *name, const char *value)
+{
+    int l = 0, c;
+
+    if (! xml) return NULL;
+    while (xml->attr[l] && strcmp(xml->attr[l], name)) l += 2;
+    if (! xml->attr[l]) { // not found, add as new attribute
+        if (! value) return xml; // nothing to do
+        if (xml->attr == EZXML_NIL) { // first attribute
+            xml->attr = malloc(4 * sizeof(char *));
+            xml->attr[1] = strdup(""); // empty list of malloced names/vals
+        }
+        else xml->attr = realloc(xml->attr, (l + 4) * sizeof(char *));
+
+        xml->attr[l] = (char *)name; // set attribute name
+        xml->attr[l + 2] = NULL; // null terminate attribute list
+        xml->attr[l + 3] = realloc(xml->attr[l + 1],
+                                   (c = strlen(xml->attr[l + 1])) + 2);
+        strcpy(xml->attr[l + 3] + c, " "); // set name/value as not malloced
+        if (xml->flags & EZXML_DUP) xml->attr[l + 3][c] = EZXML_NAMEM;
+    }
+    else if (xml->flags & EZXML_DUP) free((char *)name); // name was strduped
+
+    for (c = l; xml->attr[c]; c += 2); // find end of attribute list
+    if (xml->attr[c + 1][l / 2] & EZXML_TXTM) free(xml->attr[l + 1]); //old val
+    if (xml->flags & EZXML_DUP) xml->attr[c + 1][l / 2] |= EZXML_TXTM;
+    else xml->attr[c + 1][l / 2] &= ~EZXML_TXTM;
+
+    if (value) xml->attr[l + 1] = (char *)value; // set attribute value
+    else { // remove attribute
+        if (xml->attr[c + 1][l / 2] & EZXML_NAMEM) free(xml->attr[l]);
+        memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char*));
+        xml->attr = realloc(xml->attr, (c + 2) * sizeof(char *));
+        memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1,
+                (c / 2) - (l / 2)); // fix list of which name/vals are malloced
+    }
+    xml->flags &= ~EZXML_DUP; // clear strdup() flag
+    return xml;
+}
+
+// sets a flag for the given tag and returns the tag
+ezxml_t ezxml_set_flag(ezxml_t xml, short flag)
+{
+    if (xml) xml->flags |= flag;
+    return xml;
+}
+
+// removes a tag along with its subtags without freeing its memory
+ezxml_t ezxml_cut(ezxml_t xml)
+{
+    ezxml_t cur;
+
+    if (! xml) return NULL; // nothing to do
+    if (xml->next) xml->next->sibling = xml->sibling; // patch sibling list
+
+    if (xml->parent) { // not root tag
+        cur = xml->parent->child; // find head of subtag list
+        if (cur == xml) xml->parent->child = xml->ordered; // first subtag
+        else { // not first subtag
+            while (cur->ordered != xml) cur = cur->ordered;
+            cur->ordered = cur->ordered->ordered; // patch ordered list
+
+            cur = xml->parent->child; // go back to head of subtag list
+            if (strcmp(cur->name, xml->name)) { // not in first sibling list
+                while (strcmp(cur->sibling->name, xml->name))
+                    cur = cur->sibling;
+                if (cur->sibling == xml) { // first of a sibling list
+                    cur->sibling = (xml->next) ? xml->next
+                                               : cur->sibling->sibling;
+                }
+                else cur = cur->sibling; // not first of a sibling list
+            }
+
+            while (cur->next && cur->next != xml) cur = cur->next;
+            if (cur->next) cur->next = cur->next->next; // patch next list
+        }        
+    }
+    xml->ordered = xml->sibling = xml->next = NULL;
+    return xml;
+}
+
+#ifdef EZXML_TEST // test harness
+int main(int argc, char **argv)
+{
+    ezxml_t xml;
+    char *s;
+    int i;
+
+    if (argc != 2) return fprintf(stderr, "usage: %s xmlfile\n", argv[0]);
+
+    xml = ezxml_parse_file(argv[1]);
+    printf("%s\n", (s = ezxml_toxml(xml)));
+    free(s);
+    i = fprintf(stderr, "%s", ezxml_error(xml));
+    ezxml_free(xml);
+    return (i) ? 1 : 0;
+}
+#endif // EZXML_TEST
diff --git a/linux_usr/ezxml.h b/linux_usr/ezxml.h
new file mode 100644 (file)
index 0000000..3e02078
--- /dev/null
@@ -0,0 +1,167 @@
+/* ezxml.h
+ *
+ * Copyright 2004-2006 Aaron Voisine <aaron@voisine.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef _EZXML_H
+#define _EZXML_H
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <fcntl.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define EZXML_BUFSIZE 1024 // size of internal memory buffers
+#define EZXML_NAMEM   0x80 // name is malloced
+#define EZXML_TXTM    0x40 // txt is malloced
+#define EZXML_DUP     0x20 // attribute name and value are strduped
+
+typedef struct ezxml *ezxml_t;
+struct ezxml {
+    char *name;      // tag name
+    char **attr;     // tag attributes { name, value, name, value, ... NULL }
+    char *txt;       // tag character content, empty string if none
+    size_t off;      // tag offset from start of parent tag character content
+    ezxml_t next;    // next tag with same name in this section at this depth
+    ezxml_t sibling; // next tag with different name in same section and depth
+    ezxml_t ordered; // next tag, same section and depth, in original order
+    ezxml_t child;   // head of sub tag list, NULL if none
+    ezxml_t parent;  // parent tag, NULL if current tag is root tag
+    short flags;     // additional information
+};
+
+// Given a string of xml data and its length, parses it and creates an ezxml
+// structure. For efficiency, modifies the data by adding null terminators
+// and decoding ampersand sequences. If you don't want this, copy the data and
+// pass in the copy. Returns NULL on failure.
+ezxml_t ezxml_parse_str(char *s, size_t len);
+
+// A wrapper for ezxml_parse_str() that accepts a file descriptor. First
+// attempts to mem map the file. Failing that, reads the file into memory.
+// Returns NULL on failure.
+ezxml_t ezxml_parse_fd(int fd);
+
+// a wrapper for ezxml_parse_fd() that accepts a file name
+ezxml_t ezxml_parse_file(const char *file);
+    
+// Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
+// stream into memory and then parses it. For xml files, use ezxml_parse_file()
+// or ezxml_parse_fd()
+ezxml_t ezxml_parse_fp(FILE *fp);
+
+// returns the first child tag (one level deeper) with the given name or NULL
+// if not found
+ezxml_t ezxml_child(ezxml_t xml, const char *name);
+
+// returns the next tag of the same name in the same section and depth or NULL
+// if not found
+#define ezxml_next(xml) ((xml) ? xml->next : NULL)
+
+// Returns the Nth tag with the same name in the same section at the same depth
+// or NULL if not found. An index of 0 returns the tag given.
+ezxml_t ezxml_idx(ezxml_t xml, int idx);
+
+// returns the name of the given tag
+#define ezxml_name(xml) ((xml) ? xml->name : NULL)
+
+// returns the given tag's character content or empty string if none
+#define ezxml_txt(xml) ((xml) ? xml->txt : "")
+
+// returns the value of the requested tag attribute, or NULL if not found
+const char *ezxml_attr(ezxml_t xml, const char *attr);
+
+// Traverses the ezxml sturcture to retrieve a specific subtag. Takes a
+// variable length list of tag names and indexes. The argument list must be
+// terminated by either an index of -1 or an empty string tag name. Example: 
+// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
+// This retrieves the title of the 3rd book on the 1st shelf of library.
+// Returns NULL if not found.
+ezxml_t ezxml_get(ezxml_t xml, ...);
+
+// Converts an ezxml structure back to xml. Returns a string of xml data that
+// must be freed.
+char *ezxml_toxml(ezxml_t xml);
+
+// returns a NULL terminated array of processing instructions for the given
+// target
+const char **ezxml_pi(ezxml_t xml, const char *target);
+
+// frees the memory allocated for an ezxml structure
+void ezxml_free(ezxml_t xml);
+    
+// returns parser error message or empty string if none
+const char *ezxml_error(ezxml_t xml);
+
+// returns a new empty ezxml structure with the given root tag name
+ezxml_t ezxml_new(const char *name);
+
+// wrapper for ezxml_new() that strdup()s name
+#define ezxml_new_d(name) ezxml_set_flag(ezxml_new(strdup(name)), EZXML_NAMEM)
+
+// Adds a child tag. off is the offset of the child tag relative to the start
+// of the parent tag's character content. Returns the child tag.
+ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off);
+
+// wrapper for ezxml_add_child() that strdup()s name
+#define ezxml_add_child_d(xml, name, off) \
+    ezxml_set_flag(ezxml_add_child(xml, strdup(name), off), EZXML_NAMEM)
+
+// sets the character content for the given tag and returns the tag
+ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt);
+
+// wrapper for ezxml_set_txt() that strdup()s txt
+#define ezxml_set_txt_d(xml, txt) \
+    ezxml_set_flag(ezxml_set_txt(xml, strdup(txt)), EZXML_TXTM)
+
+// Sets the given tag attribute or adds a new attribute if not found. A value
+// of NULL will remove the specified attribute. Returns the tag given.
+ezxml_t ezxml_set_attr(ezxml_t xml, const char *name, const char *value);
+
+// Wrapper for ezxml_set_attr() that strdup()s name/value. Value cannot be NULL
+#define ezxml_set_attr_d(xml, name, value) \
+    ezxml_set_attr(ezxml_set_flag(xml, EZXML_DUP), strdup(name), strdup(value))
+
+// sets a flag for the given tag and returns the tag
+ezxml_t ezxml_set_flag(ezxml_t xml, short flag);
+
+// removes a tag along with its subtags without freeing its memory
+ezxml_t ezxml_cut(ezxml_t xml);
+
+// inserts an existing tag into an ezxml structure
+ezxml_t ezxml_insert(ezxml_t xml, ezxml_t dest, size_t off);
+
+// Moves an existing tag to become a subtag of dest at the given offset from
+// the start of dest's character content. Returns the moved tag.
+#define ezxml_move(xml, dest, off) ezxml_insert(ezxml_cut(xml), dest, off)
+
+// removes a tag along with all its subtags
+#define ezxml_remove(xml) ezxml_free(ezxml_cut(xml))
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // _EZXML_H
index 3605702..02bb873 100644 (file)
@@ -3,7 +3,6 @@
  * (c) Jack lange, 2010
  */
 
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h> 
 #include <sys/types.h> 
 #include <unistd.h> 
 #include <string.h>
+#include <getopt.h> 
+
+
 #include "v3_ctrl.h"
+#include "ezxml.h"
+
+struct cfg_value {
+    char * tag;
+    char * value;
+};
+
+struct xml_option {
+    char * tag;
+    ezxml_t location;
+    struct xml_option * next;
+};
+
+
+struct file_info {
+    int size;
+    char filename[2048];
+    char id[256];
+};
+
+#define MAX_FILES 256
+unsigned long long num_files = 0;
+struct file_info files[MAX_FILES];
+
 
 int read_file(int fd, int size, unsigned char * buf);
 
-int main(int argc, char* argv[]) {
-    char * filename = argv[1];
-    char * name = argv[2];
-    int guest_fd = 0;
-    int v3_fd = 0;
+
+
+int create_vm(char * vm_name, void * img_data, unsigned int img_size) {
     struct v3_guest_img guest_img;
-    struct stat guest_stats;
+    int v3_fd = 0;
     int dev_idx = 0;
 
     memset(&guest_img, 0, sizeof(struct v3_guest_img));
 
-    if (argc <= 2) {
-       printf("usage: v3_create <guest_img> <vm name>\n");
+    guest_img.size = img_size;
+    guest_img.guest_data = img_data;
+    strncpy(guest_img.name, vm_name, 127);
+
+
+    v3_fd = open(v3_dev, O_RDONLY);
+
+    if (v3_fd == -1) {
+       printf("Error opening V3Vee control device\n");
        return -1;
     }
 
-    printf("Creating guest: %s\n", filename);
+    dev_idx = ioctl(v3_fd, V3_CREATE_GUEST, &guest_img); 
+
+
+    if (dev_idx < 0) {
+       printf("Error (%d) creating VM\n", dev_idx);
+       return -1;
+    }
+
+    printf("VM (%s) created at /dev/v3-vm%d\n", vm_name, dev_idx);
+
+    /* Close the file descriptor.  */ 
+    close(v3_fd); 
+
+    return 0;
+}
+
+
+int load_image(char * vm_name, char * filename) {
+    int guest_fd = 0;
+    struct stat guest_stats;
+    int img_size = 0;
+    void * img_data = NULL;
 
     guest_fd = open(filename, O_RDONLY); 
 
@@ -46,47 +97,342 @@ int main(int argc, char* argv[]) {
        printf("ERROR: Could not stat guest image file -- %s\n", filename);
        return -1;
     }
+    
+    img_size = guest_stats.st_size;
+
+    // load guest image into user memory
+    img_data = malloc(img_size);
 
+    read_file(guest_fd, img_size, img_data);
     
-    guest_img.size = guest_stats.st_size;
+    close(guest_fd);
+
+    printf("Guest image Loaded (size=%u)\n", img_size);
+    return create_vm(vm_name, img_data, img_size);
+
+}
+
+
+ezxml_t open_xml_file(char * filename) {
+
+    ezxml_t xml_input = ezxml_parse_file(filename);
     
-    // load guest image into user memory
-    guest_img.guest_data = malloc(guest_img.size);
-    if (!guest_img.guest_data) {
-            printf("ERROR: could not allocate memory for guest image\n");
-            return -1;
+    if (xml_input == NULL) {
+       printf("Error: Could not open XML input file (%s)\n", filename);
+       return NULL;
+    } else if (strcmp("", ezxml_error(xml_input)) != 0) {
+       printf("%s\n", ezxml_error(xml_input));
+       return NULL;
     }
 
-    read_file(guest_fd, guest_img.size, guest_img.guest_data);
+    return xml_input;
+}
+
+
+int find_xml_options(ezxml_t xml,  struct xml_option ** opts) {
+    int num_opts = 0;
+    ezxml_t child = xml->child;
+    struct xml_option * next_opt = NULL;
+
+    char * opt = (char *)ezxml_attr(xml, "opt_tag");
+
+    if (opt) {
+       next_opt = malloc(sizeof(struct xml_option));
+
+       memset(next_opt, 0, sizeof(struct xml_option));
+
+       next_opt->tag = opt;
+       next_opt->location = xml;
+       next_opt->next = NULL;
+
+//     printf("Option found: %s\n", opt);
+
+       *opts = next_opt;
+       num_opts++;
+    }
+
+
+    while (child) {
+
+       fflush(stdout);
+
+       if (next_opt != 0x0) {
+           num_opts += find_xml_options(child, &(next_opt->next));
+       } else {
+           num_opts += find_xml_options(child, opts);
+
+           if (*opts) {
+               next_opt = *opts;
+           }
+       }
+
+       if (next_opt) {
+           while (next_opt->next) {
+               next_opt = next_opt->next;
+           }
+       }
+
+       child = child->ordered;
+    }
     
-    close(guest_fd);
+    return num_opts;
+
+}
+
+
+char * get_val(ezxml_t cfg, char * tag) {
+    char * attrib = (char *)ezxml_attr(cfg, tag);
+    ezxml_t txt = ezxml_child(cfg, tag);
+
+    if ((txt != NULL) && (attrib != NULL)) {
+       printf("Invalid Cfg file: Duplicate value for %s (attr=%s, txt=%s)\n", 
+              tag, attrib, ezxml_txt(txt));
+       exit(-1);
+    }
+
+    return (attrib == NULL) ? ezxml_txt(txt) : attrib;
+}
+
+
+int parse_aux_files(ezxml_t cfg_input) {
+    ezxml_t file_tags = NULL;
+    ezxml_t tmp_file_tag = NULL;
+
+    // files are transformed into blobs that are slapped to the end of the file
+        
+    file_tags = ezxml_child(cfg_input, "files");
 
-    printf("Loaded guest image. Creation begins.\n");
+    tmp_file_tag = ezxml_child(file_tags, "file");
+
+    while (tmp_file_tag) {
+       char * filename = get_val(tmp_file_tag, "filename");
+       struct stat file_stats;
+       char * id = get_val(tmp_file_tag, "id");
+       char index_buf[256];
+
+
+       if (stat(filename, &file_stats) != 0) {
+           perror(filename);
+           exit(-1);
+       }
+
+       files[num_files].size = (unsigned int)file_stats.st_size;
+       strncpy(files[num_files].id, id, 256);
+       strncpy(files[num_files].filename, filename, 2048);
+
+       snprintf(index_buf, 256, "%llu", num_files);
+       ezxml_set_attr_d(tmp_file_tag, "index", index_buf);
+
+       num_files++;
+       tmp_file_tag = ezxml_next(tmp_file_tag);
+    }
+
+
+    return 0;
+}
+
+int build_image(char * vm_name, char * filename, struct cfg_value * cfg_vals, int num_options) {
+    int i = 0;
+    ezxml_t xml = NULL;
+    struct xml_option * xml_opts = NULL;
+    int num_xml_opts = 0;
+    void * guest_img_data = NULL;
+    int guest_img_size = 0;
+
+
+    xml = open_xml_file(filename);
+    
+    // parse options
+    num_xml_opts = find_xml_options(xml, &xml_opts);
     
-    strncpy(guest_img.name, name, 127);
+    //  printf("%d options\n", num_xml_opts);
 
+    // apply options
+    for (i = 0; i < num_options; i++) {
+       struct cfg_value * cfg_val = &cfg_vals[i];
+       struct xml_option * xml_opt = xml_opts;
 
-    v3_fd = open(v3_dev, O_RDONLY);
+       while (xml_opt) {
+           if (strcasecmp(cfg_val->tag, xml_opt->tag) == 0) {
+               break;
+           }
+           
+           xml_opt = xml_opt->next;
+       }
 
-    if (v3_fd == -1) {
-       printf("Error opening V3Vee control device\n");
-       return -1;
+
+       if (!xml_opt) {
+           printf("Could not find Config option (%s) in XML file\n", cfg_val->tag);
+           return -1;
+       }
+
+       ezxml_set_txt(xml_opt->location, cfg_val->value);
     }
+    
 
-    dev_idx = ioctl(v3_fd, V3_CREATE_GUEST, &guest_img); 
 
+    // parse files
+    parse_aux_files(xml);
+    
+    // create image data blob
+    {
+       char * new_xml_str = ezxml_toxml(xml);
+       int file_data_size = 0;
+       int i = 0;
+       int offset = 0;
+       unsigned long long file_offset = 0;
 
-    if (dev_idx < 0) {
-       printf("Error (%d) creating VM\n", dev_idx);
+       /* Image size is: 
+          8 byte header + 
+          4 byte xml length + 
+          xml strlen + 
+          8 bytes of zeros + 
+          8 bytes (number of files) + 
+          num_files * 16 byte file header + 
+          8 bytes of zeroes + 
+          file data
+       */
+       for (i = 0; i < num_files; i++) {
+           file_data_size += files[i].size;
+       }
+
+       guest_img_size = 8 + 4 + strlen(new_xml_str) + 8 + 8 + 
+           (num_files * 16) + 8 + file_data_size;
+           
+
+       guest_img_data = malloc(guest_img_size);
+       memset(guest_img_data, 0, guest_img_size);
+
+       memcpy(guest_img_data, "v3vee\0\0\0", 8);
+       offset += 8;
+
+       *(unsigned int *)(guest_img_data + offset) = strlen(new_xml_str);
+       offset += 4;
+
+       memcpy(guest_img_data + offset, new_xml_str, strlen(new_xml_str));
+       offset += strlen(new_xml_str);
+
+       memset(guest_img_data + offset, 0, 8);
+       offset += 8;
+       
+       *(unsigned long long *)(guest_img_data + offset) = num_files;
+       offset += 8;
+
+       
+       // The file offset starts at the end of the file list
+       file_offset = offset + (16 * num_files) + 8;
+
+       for (i = 0; i < num_files; i++) {
+           *(unsigned int *)(guest_img_data + offset) = i;
+           offset += 4;
+           *(unsigned int *)(guest_img_data + offset) = files[i].size;
+           offset += 4;
+           *(unsigned long long *)(guest_img_data + offset) = file_offset;
+           offset += 8;
+
+           file_offset += files[i].size;
+
+       }
+
+       memset(guest_img_data + offset, 0, 8);
+       offset += 8;
+
+
+       for (i = 0; i < num_files; i++) {
+           int fd = open(files[i].filename, O_RDONLY);
+
+           if (fd == -1) {
+               printf("Error: Could not open aux file (%s)\n", files[i].filename);
+               return -1;
+           }
+
+           read_file(fd, files[i].size, (unsigned char *)(guest_img_data + offset));
+
+           close(fd);
+
+           offset += files[i].size;
+
+       }
+       
+       free(new_xml_str);
+
+    }
+
+    printf("Guest Image Created (size=%u)\n", guest_img_size);
+    return create_vm(vm_name, guest_img_data, guest_img_size);
+}
+
+
+
+
+
+
+int main(int argc, char** argv) {
+    char * filename = NULL;
+    char * name = NULL;
+    int build_flag  = 0;
+    int c = 0;
+
+    opterr = 0;
+
+    while (( c = getopt(argc, argv, "b")) != -1) {
+       switch (c) {
+       case 'b':
+           build_flag = 1;
+           break;
+       }
+    }
+
+    if (argc - optind + 1 < 3) {
+       printf("usage: v3_create [-b] <guest_img> <vm name> [cfg options]\n");
        return -1;
     }
 
-    printf("VM (%s) created at /dev/v3-vm%d\n", name, dev_idx);
+    filename = argv[optind];
+    name = argv[optind + 1];
 
-    /* Close the file descriptor.  */ 
-    close(v3_fd); 
 
+    if (build_flag == 1) {
+       int cfg_idx = optind + 2;
+       int i = 0;
+       struct cfg_value * cfg_vals = NULL;
+       
+       printf("Building VM Image (cfg=%s) (name=%s) (%d config options)\n", filename, name, argc - cfg_idx);
+
+       cfg_vals = malloc(sizeof(struct cfg_value) * argc - cfg_idx);
+       
+       
+
+       while (i < argc - cfg_idx) {
+           char * tag = NULL;
+           char * value = NULL;
+
+           value = argv[cfg_idx + i];
+
+           tag = strsep(&value, "=");
+           // parse
+
+           if (value == NULL) {
+               printf("Invalid Option format: %s\n", argv[cfg_idx + i]);
+               return -1;
+           }
+
+           cfg_vals[i].tag = tag;
+           cfg_vals[i].value = value;
+
+           printf("Config option: %s: %s\n", tag, value);
+
+
+           i++;
+       }
+
+       return build_image(name, filename, cfg_vals, argc - cfg_idx);
+
+
+    } else {
+       printf("Loading VM Image (img=%s) (name=%s)\n", filename, name);
+       return load_image(name, filename);
+    }
 
     return 0; 
 } 
index 6522b34..aae3af6 100644 (file)
@@ -6,9 +6,16 @@
 #ifndef _v3_ctrl_h
 #define _v3_ctrl_h
 
+
+/* Global Control IOCTLs */
 #define V3_CREATE_GUEST 12
 #define V3_FREE_GUEST 13
 
+#define V3_ADD_MEMORY 50
+#define V3_ADD_PCI_HW_DEV 55
+#define V3_ADD_PCI_USER_DEV 56
+
+/* VM Specific IOCTLs */
 
 /* VM Specific ioctls */
 #define V3_VM_CONSOLE_CONNECT 20
@@ -24,7 +31,6 @@
 #define V3_VM_INSPECT 30
 #define V3_VM_DEBUG 31
 
-#define V3_ADD_MEMORY 50
 
 #define V3_VM_MOVE_CORE 33
 
@@ -69,4 +75,14 @@ struct v3_chkpt_info {
     char url[256]; /* This might need to be bigger... */
 } __attribute__((packed));
 
+
+
+struct v3_hw_pci_dev {
+    char url[128];
+    unsigned int bus;
+    unsigned int dev;
+    unsigned int func;
+} __attribute__((packed));
+
+
 #endif
diff --git a/linux_usr/v3_pci.c b/linux_usr/v3_pci.c
new file mode 100644 (file)
index 0000000..5bbf951
--- /dev/null
@@ -0,0 +1,58 @@
+/* Host PCI User space tool
+ *  (c) Jack Lange, 2012
+ *  jacklange@cs.pitt.edu 
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h> 
+#include <sys/ioctl.h> 
+#include <sys/stat.h> 
+#include <sys/types.h> 
+#include <unistd.h>
+#include <string.h>
+
+
+#include "v3_ctrl.h"
+
+
+int main(int argc, char ** argv) {
+    int v3_fd = 0;
+    struct v3_hw_pci_dev dev_info;
+    unsigned int bus = 0;
+    unsigned int dev = 0;
+    unsigned int func = 0;
+    int ret = 0;
+
+    if (argc < 3) {
+       printf("Usage: ./v3_pci <name> <bus> <dev> <func>\n");
+       return -1;
+    }
+
+    bus = atoi(argv[2]);
+    dev = atoi(argv[3]);
+    func = atoi(argv[4]);
+
+    strncpy(dev_info.url, argv[1], 128);
+    dev_info.bus = bus;
+    dev_info.dev = dev;
+    dev_info.func = func;
+    
+
+    v3_fd = open("/dev/v3vee", O_RDONLY);
+
+    if (v3_fd == -1) {
+       printf("Error opening V3Vee device file\n");
+       return -1;
+    }
+
+
+    ret = ioctl(v3_fd, V3_ADD_PCI_HW_DEV, &dev_info);
+    
+
+    if (ret < 0) {
+       printf("Error registering PCI device\n");
+       return -1;
+    }
+
+    close(v3_fd);
+}
diff --git a/palacios/include/interfaces/host_pci.h b/palacios/include/interfaces/host_pci.h
new file mode 100644 (file)
index 0000000..e426533
--- /dev/null
@@ -0,0 +1,132 @@
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jacklange@cs.pitt.edu>
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jacklange@cs.pitt.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+
+#include <palacios/vmm.h>
+#include <palacios/vmm_types.h>
+
+
+struct v3_vm_info;
+
+typedef enum { PT_BAR_NONE,
+              PT_BAR_IO, 
+              PT_BAR_MEM32, 
+              PT_BAR_MEM24, 
+              PT_BAR_MEM64_LO, 
+              PT_BAR_MEM64_HI,
+              PT_EXP_ROM } pt_bar_type_t;
+
+
+typedef enum { HOST_PCI_CMD_DMA_DISABLE = 1,
+              HOST_PCI_CMD_DMA_ENABLE = 2,
+              HOST_PCI_CMD_INTX_DISABLE = 3,
+              HOST_PCI_CMD_INTX_ENABLE = 4,
+              HOST_PCI_CMD_MSI_DISABLE = 5,
+              HOST_PCI_CMD_MSI_ENABLE = 6,
+              HOST_PCI_CMD_MSIX_DISABLE = 7,
+              HOST_PCI_CMD_MSIX_ENABLE = 8 } host_pci_cmd_t;
+
+struct v3_host_pci_bar {
+    uint32_t size;
+    pt_bar_type_t type;
+
+    /*  We store 64 bit memory bar addresses in the high BAR
+     *  because they are the last to be updated
+     *  This means that the addr field must be 64 bits
+     */
+    uint64_t addr; 
+
+    union {
+       uint32_t flags;
+       struct {
+           uint32_t prefetchable    : 1;
+           uint32_t cacheable       : 1;
+           uint32_t exp_rom_enabled : 1;
+           uint32_t rsvd            : 29;
+       } __attribute__((packed));
+    } __attribute__((packed));
+
+
+};
+
+
+
+struct v3_host_pci_dev {
+    struct v3_host_pci_bar bars[6];
+    struct v3_host_pci_bar exp_rom;
+
+    uint8_t cfg_space[256];
+
+    enum {IOMMU, SYMBIOTIC, EMULATED} iface;
+
+    int (*irq_handler)(void * guest_data, uint32_t vec_index);
+
+    void * host_data;
+    void * guest_data;
+};
+
+// For now we just support the single contiguous region
+// This can be updated in the future to support non-contiguous guests
+struct v3_guest_mem_region {
+    uint64_t start;
+    uint64_t end;
+};
+
+
+#ifdef __V3VEE__
+
+#include <devices/pci.h>
+
+
+struct v3_host_pci_dev * v3_host_pci_get_dev(struct v3_vm_info * vm, char * url, void * priv_data);
+
+
+int v3_host_pci_config_write(struct v3_host_pci_dev * v3_dev, uint32_t reg_num, void * src, uint32_t length);
+int v3_host_pci_config_read(struct v3_host_pci_dev * v3_dev, uint32_t reg_num, void * dst, uint32_t length);
+
+int v3_host_pci_cmd_update(struct v3_host_pci_dev * v3_dev, pci_cmd_t cmd, uint64_t arg);
+
+int v3_host_pci_ack_irq(struct v3_host_pci_dev * v3_dev, uint32_t vector);
+
+
+#endif
+
+
+struct v3_host_pci_hooks {
+    struct v3_host_pci_dev * (*request_device)(char * url, void * v3_ctx);
+
+    // emulated interface
+
+    int (*config_write)(struct v3_host_pci_dev * v3_dev, uint32_t reg_num, void * src, uint32_t length);
+    int (*config_read)(struct v3_host_pci_dev * v3_dev, uint32_t reg_num, void * dst, uint32_t length);
+
+    int (*pci_cmd)(struct v3_host_pci_dev * v3_dev, host_pci_cmd_t cmd, uint64_t arg);
+    
+    int (*ack_irq)(struct v3_host_pci_dev * v3_dev, uint32_t vector);
+
+
+};
+
+
+
+void V3_Init_Host_PCI(struct v3_host_pci_hooks * hooks);
+
+int V3_get_guest_mem_region(struct v3_vm_info * vm, struct v3_guest_mem_region * region);
+int V3_host_pci_raise_irq(struct v3_host_pci_dev * v3_dev, uint32_t vec_index);
+
diff --git a/palacios/include/palacios/svm_exits.h b/palacios/include/palacios/svm_exits.h
new file mode 100644 (file)
index 0000000..9052c24
--- /dev/null
@@ -0,0 +1,34 @@
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jarusl@cs.northwestern.edu> 
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jarusl@cs.northwestern.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+
+
+#ifndef __SVM_EXITS_H__
+#define __SVM_EXITS_H__
+
+
+#ifdef __V3VEE__
+
+int v3_init_svm_exits(struct v3_vm_info * vm);
+
+
+
+#endif
+
+#endif
index fb91c1d..9e6ea21 100644 (file)
@@ -39,7 +39,8 @@
 #include <palacios/vmm_extensions.h>
 #include <palacios/vmm_barrier.h>
 #include <palacios/vmm_timeout.h>
-
+#include <palacios/vmm_exits.h>
+#include <palacios/vmm_events.h>
 
 #ifdef V3_CONFIG_TELEMETRY
 #include <palacios/vmm_telemetry.h>
@@ -160,6 +161,8 @@ struct v3_vm_info {
     struct v3_io_map io_map;
     struct v3_msr_map msr_map;
     struct v3_cpuid_map cpuid_map;
+    struct v3_exit_map exit_map;
+    struct v3_event_map event_map;
 
     v3_hypercall_map_t hcall_map;
 
index 29cabb7..04ae22d 100644 (file)
 
 #ifdef __V3VEE__
 #include <palacios/vmm_types.h>
+#include <palacios/vmm_lock.h>
 
 
 
 struct v3_bitmap {
+    v3_lock_t lock; 
     int num_bits;      // number of valid bit positions in the bitmap
     uint8_t * bits;   // actual bitmap. Dynamically allocated... ugly
 };
diff --git a/palacios/include/palacios/vmm_events.h b/palacios/include/palacios/vmm_events.h
new file mode 100644 (file)
index 0000000..296fe81
--- /dev/null
@@ -0,0 +1,104 @@
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jarusl@cs.northwestern.edu> 
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jarusl@cs.northwestern.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+#ifndef __VMM_EVENTS_H__
+#define __VMM_EVENTS_H__
+
+#ifdef __V3VEE__
+
+#include <palacios/vmm_types.h>
+#include <palacios/vmm_list.h>
+
+struct guest_info;
+struct v3_vm_info;
+
+typedef enum { 
+              V3_EVENT_INVALID /* This entry must always be last */
+} v3_event_type_t;
+
+
+struct v3_event_map {
+    struct list_head * events; // array of events
+
+};
+
+
+int v3_init_events(struct v3_vm_info * vm);
+int v3_deinit_events(struct v3_vm_info * vm);
+
+
+struct v3_notifier {
+
+    v3_event_type_t event_type;
+
+    void (*notify)(struct guest_info * core, 
+                  v3_event_type_t event_type,
+                  void * priv_data, 
+                  void * event_data);
+    void * priv_data;
+    
+    struct list_head node;
+
+};
+
+
+struct v3_notifier * v3_subscribe_event(struct v3_vm_info * vm, 
+                                      v3_event_type_t event_type, 
+                                      void (*notify)(struct guest_info * core, 
+                                                     v3_event_type_t event_type,
+                                                     void * priv_data, 
+                                                     void * event_data),
+                                      void * priv_data, 
+                                      struct guest_info * current_core);
+
+int v3_unsubscribe_event(struct v3_vm_info * vm, struct v3_notifier * notifier, 
+                        struct guest_info * current_core);
+
+
+
+#include <palacios/vm_guest.h>
+
+#ifdef __VM_EVENTS_H2___ /* Just ignore the man behind the curtain.... */
+
+static void inline v3_dispatch_event(struct guest_info * core, 
+                                    v3_event_type_t event_type, 
+                                    void * event_data) {
+    struct v3_notifier * tmp_notifier = NULL;
+
+    if (event_type >= V3_EVENT_INVALID) {
+       PrintError("Tried to dispatch illegal event (%d)\n", event_type);
+       return;
+    }
+
+    list_for_each_entry(tmp_notifier, &(core->vm_info->event_map.events[event_type]), node) {
+       tmp_notifier->notify(core, event_type, tmp_notifier->priv_data, event_data);
+    }
+
+}
+
+#endif
+#define __VM_EVENTS_H2___
+
+
+
+
+
+#endif
+
+#endif
diff --git a/palacios/include/palacios/vmm_exits.h b/palacios/include/palacios/vmm_exits.h
new file mode 100644 (file)
index 0000000..899bf0b
--- /dev/null
@@ -0,0 +1,106 @@
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jarusl@cs.northwestern.edu> 
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jarusl@cs.northwestern.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+#ifndef __VMM_EXITS_H__
+#define __VMM_EXITS_H__
+
+
+#ifdef __V3VEE__
+
+#include <palacios/vmm_types.h>
+
+
+
+struct guest_info;
+struct v3_vm_info;
+
+typedef enum { V3_EXIT_RDTSC,
+              V3_EXIT_RDTSCP,
+              V3_EXIT_SWINTR,    
+              V3_EXIT_INVALID } v3_exit_type_t;
+
+
+
+
+struct v3_exit_hook {
+
+    int (*enable)(struct guest_info * core, v3_exit_type_t exit_type);
+    int (*disable)(struct guest_info * core, v3_exit_type_t exit_type);
+
+    struct {
+       union {
+           uint32_t flags;
+           struct {
+               uint32_t hooked      : 1;
+               uint32_t registered  : 1;
+               uint32_t rsvd        : 30;
+           } __attribute__((packed));
+       } __attribute__((packed));
+       
+    } __attribute__((packed));
+
+
+
+    int (*handler)(struct guest_info * core, v3_exit_type_t exit_type, 
+               void * priv_data, void * exit_data);
+    void * priv_data;
+
+};
+
+
+struct v3_exit_map {
+    
+    struct v3_exit_hook * exits; 
+};
+
+
+
+
+int v3_init_exit_hooks(struct v3_vm_info * vm);
+int v3_deinit_exit_hooks(struct v3_vm_info * vm);
+
+int v3_init_exit_hooks_core(struct guest_info * core);
+
+
+
+int v3_register_exit(struct v3_vm_info * vm, v3_exit_type_t exit_type,
+                    int (*enable)(struct guest_info * core, v3_exit_type_t exit_type),
+                    int (*disable)(struct guest_info * core, v3_exit_type_t exit_type));
+                    
+
+
+int v3_dispatch_exit_hook(struct guest_info * core, v3_exit_type_t exit_type, void * exit_data);
+
+
+
+
+
+
+int v3_hook_exit(struct v3_vm_info * vm, v3_exit_type_t exit_type,
+                int (*handler)(struct guest_info * core, v3_exit_type_t exit_type, 
+                            void * priv_data, void * exit_data),
+                void * priv_data, 
+                struct guest_info * current_core);
+
+int v3_unhook_exit(struct v3_vm_info * vm, v3_exit_type_t exit_type, struct guest_info * current_core);
+                  
+
+#endif
+
+#endif
index 811f19d..6135ab0 100644 (file)
@@ -45,6 +45,7 @@ struct v3_queue {
 
 struct v3_queue * v3_create_queue();
 void v3_init_queue(struct v3_queue * queue);
+void v3_deinit_queue(struct v3_queue * queue);
 
 void v3_enqueue(struct v3_queue * queue, addr_t entry);
 addr_t v3_dequeue(struct v3_queue * queue);
diff --git a/palacios/include/palacios/vmx_exits.h b/palacios/include/palacios/vmx_exits.h
new file mode 100644 (file)
index 0000000..8858f97
--- /dev/null
@@ -0,0 +1,34 @@
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jarusl@cs.northwestern.edu> 
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jarusl@cs.northwestern.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+
+
+#ifndef __VMX_EXITS_H__
+#define __VMX_EXITS_H__
+
+
+#ifdef __V3VEE__
+
+int v3_init_vmx_exits(struct v3_vm_info * vm);
+
+
+
+#endif
+
+#endif
index bfe90e5..0521c51 100644 (file)
@@ -493,7 +493,7 @@ static int write_master_port1(struct guest_info * core, ushort_t port, void * sr
                         state->master_isr &= ~(0x01 << i);
                         break;
                     }
-                }      
+                }
                 PrintDebug("8259 PIC: Post ISR = %x (wr_Master1)\n", state->master_isr);
             } else if (!(cw2->EOI) && (cw2->R) && (cw2->SL)) {
                 PrintDebug("8259 PIC: Ignoring set-priority, priorities not implemented (level=%d, wr_Master1)\n", cw2->level);
@@ -504,6 +504,13 @@ static int write_master_port1(struct guest_info * core, ushort_t port, void * sr
                 return -1;
             }
 
+           if (cw2->EOI) {
+               if (pic_get_intr_number(core,  state) != -1) {
+                   PrintError("Interrupt pending after EOI\n");
+               }
+           }
+
+
             state->master_ocw2 = cw;
         } else if (IS_OCW3(cw)) {
             PrintDebug("8259 PIC: Handling OCW3 = %x (wr_Master1)\n", cw);
@@ -627,6 +634,14 @@ static int write_slave_port1(struct guest_info * core, ushort_t port, void * src
                return -1;
            }
 
+           if (cw2->EOI) {
+               if (pic_get_intr_number(core,  state) != -1) {
+                   PrintError("Interrupt pending after EOI\n");
+               }
+           }
+
+
+
            state->slave_ocw2 = cw;
        } else if (IS_OCW3(cw)) {
            // Basically sets the IRR/ISR read flag
index 61d00c5..67ca1f9 100644 (file)
@@ -20,6 +20,7 @@ obj-$(V3_CONFIG_VNET_NIC) += vnet_nic.o
 obj-$(V3_CONFIG_NVRAM) += nvram.o
 obj-$(V3_CONFIG_OS_DEBUG) += os_debug.o
 obj-$(V3_CONFIG_PCI) += pci.o
+obj-$(V3_CONFIG_HOST_PCI) += host_pci.o
 obj-$(V3_CONFIG_PIIX3) += piix3.o
 obj-$(V3_CONFIG_SWAPBYPASS_DISK_CACHE) += swapbypass_cache.o
 obj-$(V3_CONFIG_SWAPBYPASS_DISK_CACHE2) += swapbypass_cache2.o
@@ -45,6 +46,5 @@ obj-$(V3_CONFIG_CHAR_STREAM) += char_stream.o
 
 obj-$(V3_CONFIG_VGA) += vga.o
 
-obj-$(V3_CONFIG_PCI_FRONT) += pci_front.o
 
 obj-$(V3_CONFIG_VNET_GUEST_IFACE) += vnet_guest_iface.o
diff --git a/palacios/src/devices/host_pci.c b/palacios/src/devices/host_pci.c
new file mode 100644 (file)
index 0000000..8db3c3a
--- /dev/null
@@ -0,0 +1,553 @@
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jacklange@cs.pitt.edu>
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jacklange@cs.pitt.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+
+/* This is the generic passthrough PCI virtual device */
+
+/* 
+ * The basic idea is that we do not change the hardware PCI configuration
+ * Instead we modify the guest environment to map onto the physical configuration
+ * 
+ * The pci subsystem handles most of the configuration space, except for the bar registers.
+ * We handle them here, by either letting them go directly to hardware or remapping through virtual hooks
+ * 
+ * Memory Bars are always remapped via the shadow map, 
+ * IO Bars are selectively remapped through hooks if the guest changes them 
+ */
+
+#include <palacios/vmm.h>
+#include <palacios/vmm_dev_mgr.h>
+#include <palacios/vmm_sprintf.h>
+#include <palacios/vmm_lowlevel.h>
+#include <palacios/vm_guest.h> // must include this to avoid dependency issue
+#include <palacios/vmm_symspy.h>
+
+#include <devices/pci.h>
+#include <devices/pci_types.h>
+#include <interfaces/host_pci.h>
+
+#define PCI_BUS_MAX  7
+#define PCI_DEV_MAX 32
+#define PCI_FN_MAX   7
+
+#define PCI_DEVICE 0x0
+#define PCI_PCI_BRIDGE 0x1
+#define PCI_CARDBUS_BRIDGE 0x2
+
+#define PCI_HDR_SIZE 256
+
+
+
+
+struct host_pci_state {
+    // This holds the description of the host PCI device configuration
+    struct v3_host_pci_dev * host_dev;
+
+
+    struct v3_host_pci_bar virt_bars[6];
+    struct v3_host_pci_bar virt_exp_rom;
+     
+    struct vm_device * pci_bus;
+    struct pci_device * pci_dev;
+
+    char name[32];
+};
+
+
+
+/*
+static int pci_exp_rom_init(struct vm_device * dev, struct host_pci_state * state) {
+    struct pci_device * pci_dev = state->pci_dev;
+    struct v3_host_pci_bar * hrom = &(state->host_dev->exp_rom);
+
+
+    
+    PrintDebug("Adding 32 bit PCI mem region: start=%p, end=%p\n",
+              (void *)(addr_t)hrom->addr, 
+              (void *)(addr_t)(hrom->addr + hrom->size));
+
+    if (hrom->exp_rom_enabled) {
+       // only map shadow memory if the ROM is enabled 
+
+       v3_add_shadow_mem(dev->vm, V3_MEM_CORE_ANY, 
+                         hrom->addr, 
+                         hrom->addr + hrom->size - 1,
+                         hrom->addr);
+
+       // Initially the virtual location matches the physical ones
+       memcpy(&(state->virt_exp_rom), hrom, sizeof(struct v3_host_pci_bar));
+
+
+       PrintDebug("phys exp_rom: addr=%p, size=%u\n", 
+                  (void *)(addr_t)hrom->addr, 
+                  hrom->size);
+
+
+       // Update the pci subsystem versions
+       pci_dev->config_header.expansion_rom_address = PCI_EXP_ROM_VAL(hrom->addr, hrom->exp_rom_enabled);
+    }
+
+
+
+    return 0;
+}
+*/
+
+
+static int pt_io_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data) {
+    struct v3_host_pci_bar * pbar = (struct v3_host_pci_bar *)priv_data;
+    int port_offset = port % pbar->size;
+
+    if (length == 1) {
+       *(uint8_t *)dst = v3_inb(pbar->addr + port_offset);
+    } else if (length == 2) {
+       *(uint16_t *)dst = v3_inw(pbar->addr + port_offset);
+    } else if (length == 4) {
+       *(uint32_t *)dst = v3_indw(pbar->addr + port_offset);
+    } else {
+       PrintError("Invalid PCI passthrough IO Redirection size read\n");
+       return -1;
+    }
+
+    return length;
+}
+
+
+static int pt_io_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data) {
+    struct v3_host_pci_bar * pbar = (struct v3_host_pci_bar *)priv_data;
+    int port_offset = port % pbar->size;
+    
+    if (length == 1) {
+       v3_outb(pbar->addr + port_offset, *(uint8_t *)src);
+    } else if (length == 2) {
+       v3_outw(pbar->addr + port_offset, *(uint16_t *)src);
+    } else if (length == 4) {
+       v3_outdw(pbar->addr + port_offset, *(uint32_t *)src);
+    } else {
+       PrintError("Invalid PCI passthrough IO Redirection size write\n");
+       return -1;
+    }
+    
+    return length;
+
+}
+
+
+
+static int pci_bar_init(int bar_num, uint32_t * dst, void * private_data) {
+    struct vm_device * dev = (struct vm_device *)private_data;
+    struct host_pci_state * state = (struct host_pci_state *)dev->private_data;
+    struct v3_host_pci_bar * hbar = &(state->host_dev->bars[bar_num]);
+    uint32_t bar_val = 0;
+
+    if (hbar->type == PT_BAR_IO) {
+       int i = 0;
+
+       bar_val = PCI_IO_BAR_VAL(hbar->addr);
+
+       for (i = 0; i < hbar->size; i++) {
+           v3_hook_io_port(dev->vm, hbar->addr + i, NULL, NULL, NULL);
+       }
+    } else if (hbar->type == PT_BAR_MEM32) {
+       bar_val = PCI_MEM32_BAR_VAL(hbar->addr, hbar->prefetchable);
+
+       v3_add_shadow_mem(dev->vm, V3_MEM_CORE_ANY, 
+                         hbar->addr, hbar->addr + hbar->size - 1,
+                         hbar->addr);
+       
+    } else if (hbar->type == PT_BAR_MEM24) {
+       bar_val = PCI_MEM24_BAR_VAL(hbar->addr, hbar->prefetchable);
+
+       v3_add_shadow_mem(dev->vm, V3_MEM_CORE_ANY, 
+                         hbar->addr, hbar->addr + hbar->size - 1,
+                         hbar->addr);
+    } else if (hbar->type == PT_BAR_MEM64_LO) {
+       struct v3_host_pci_bar * hi_hbar = &(state->host_dev->bars[bar_num + 1]);
+       bar_val = PCI_MEM64_LO_BAR_VAL(hi_hbar->addr, hbar->prefetchable);
+    } else if (hbar->type == PT_BAR_MEM64_HI) {
+       bar_val = PCI_MEM64_HI_BAR_VAL(hbar->addr, hbar->prefetchable);
+
+       v3_add_shadow_mem(dev->vm, V3_MEM_CORE_ANY, 
+                         hbar->addr, hbar->addr + hbar->size - 1,
+                         hbar->addr);  
+    }
+
+
+    memcpy(&(state->virt_bars[bar_num]), hbar, sizeof(struct v3_host_pci_bar));
+
+    *dst = bar_val;
+
+    return 0;
+}
+
+
+
+static int pci_bar_write(int bar_num, uint32_t * src, void * private_data) {
+    struct vm_device * dev = (struct vm_device *)private_data;
+    struct host_pci_state * state = (struct host_pci_state *)dev->private_data;
+    
+    struct v3_host_pci_bar * hbar = &(state->host_dev->bars[bar_num]);
+    struct v3_host_pci_bar * vbar = &(state->virt_bars[bar_num]);
+
+
+
+    if (vbar->type == PT_BAR_NONE) {
+       return 0;
+    } else if (vbar->type == PT_BAR_IO) {
+       int i = 0;
+
+       // unhook old ports
+       for (i = 0; i < vbar->size; i++) {
+           if (v3_unhook_io_port(dev->vm, vbar->addr + i) == -1) {
+               PrintError("Could not unhook previously hooked port.... %d (0x%x)\n", 
+                          (uint32_t)vbar->addr + i, (uint32_t)vbar->addr + i);
+               return -1;
+           }
+       }
+
+       // clear the low bits to match the size
+       vbar->addr = *src & ~(hbar->size - 1);
+
+       // udpate source version
+       *src = PCI_IO_BAR_VAL(vbar->addr);
+
+       PrintDebug("Rehooking passthrough IO ports starting at %d (0x%x)\n", 
+                  (uint32_t)vbar->addr, (uint32_t)vbar->addr);
+
+       if (vbar->addr == hbar->addr) {
+           // Map the io ports as passthrough
+           for (i = 0; i < hbar->size; i++) {
+               v3_hook_io_port(dev->vm, hbar->addr + i, NULL, NULL, NULL); 
+           }
+       } else {
+           // We have to manually handle the io redirection
+           for (i = 0; i < vbar->size; i++) {
+               v3_hook_io_port(dev->vm, vbar->addr + i, pt_io_read, pt_io_write, hbar); 
+           }
+       }
+    } else if (vbar->type == PT_BAR_MEM32) {
+       // remove old mapping
+       struct v3_mem_region * old_reg = v3_get_mem_region(dev->vm, V3_MEM_CORE_ANY, vbar->addr);
+
+       if (old_reg == NULL) {
+           // uh oh...
+           PrintError("Could not find PCI Passthrough memory redirection region (addr=0x%x)\n", (uint32_t)vbar->addr);
+           return -1;
+       }
+
+       v3_delete_mem_region(dev->vm, old_reg);
+
+       // clear the low bits to match the size
+       vbar->addr = *src & ~(hbar->size - 1);
+
+       // Set reserved bits
+       *src = PCI_MEM32_BAR_VAL(vbar->addr, hbar->prefetchable);
+
+       PrintDebug("Adding pci Passthrough remapping: start=0x%x, size=%d, end=0x%x (hpa=%p)\n", 
+                  (uint32_t)vbar->addr, vbar->size, (uint32_t)vbar->addr + vbar->size, (void *)hbar->addr);
+
+       v3_add_shadow_mem(dev->vm, V3_MEM_CORE_ANY, 
+                         vbar->addr, 
+                         vbar->addr + vbar->size - 1,
+                         hbar->addr);
+
+    } else if (vbar->type == PT_BAR_MEM64_LO) {
+       // We only store the written values here, the actual reconfig comes when the high BAR is updated
+
+       vbar->addr = *src & ~(hbar->size - 1);
+
+       *src = PCI_MEM64_LO_BAR_VAL(vbar->addr, hbar->prefetchable);
+
+
+    } else if (vbar->type == PT_BAR_MEM64_HI) {
+       struct v3_host_pci_bar * lo_vbar = &(state->virt_bars[bar_num - 1]);
+       struct v3_mem_region * old_reg =  v3_get_mem_region(dev->vm, V3_MEM_CORE_ANY, vbar->addr);
+
+       if (old_reg == NULL) {
+           // uh oh...
+           PrintError("Could not find PCI Passthrough memory redirection region (addr=%p)\n", 
+                      (void *)(addr_t)vbar->addr);
+           return -1;
+       }
+
+       // remove old mapping
+       v3_delete_mem_region(dev->vm, old_reg);
+
+       vbar->addr = (((uint64_t)*src) << 32) + lo_vbar->addr;
+
+       // We don't set size, because we assume region is less than 4GB
+       // src does not change, because there are no reserved bits
+       
+
+       PrintDebug("Adding pci Passthrough remapping: start=%p, size=%p, end=%p\n", 
+                  (void *)(addr_t)vbar->addr, (void *)(addr_t)vbar->size, 
+                  (void *)(addr_t)(vbar->addr + vbar->size));
+
+       if (v3_add_shadow_mem(dev->vm, V3_MEM_CORE_ANY, vbar->addr, 
+                             vbar->addr + vbar->size - 1, hbar->addr) == -1) {
+
+           PrintDebug("Fail to insert shadow region (%p, %p)  -> %p\n",
+                      (void *)(addr_t)vbar->addr,
+                      (void *)(addr_t)(vbar->addr + vbar->size - 1),
+                      (void *)(addr_t)hbar->addr);
+           return -1;
+       }
+       
+    } else {
+       PrintError("Unhandled Pasthrough PCI Bar type %d\n", vbar->type);
+       return -1;
+    }
+
+
+    return 0;
+}
+
+
+static int pt_config_write(struct pci_device * pci_dev, uint32_t reg_num, void * src, uint_t length, void * private_data) {
+    struct vm_device * dev = (struct vm_device *)private_data;
+    struct host_pci_state * state = (struct host_pci_state *)dev->private_data;
+    
+//    V3_Print("Writing host PCI config space update\n");
+
+    // We will mask all operations to the config header itself, 
+    // and only allow direct access to the device specific config space
+    if (reg_num < 64) {
+       return 0;
+    }
+
+    return v3_host_pci_config_write(state->host_dev, reg_num, src, length);
+}
+
+
+
+static int pt_config_read(struct pci_device * pci_dev, uint32_t reg_num, void * dst, uint_t length, void * private_data) {
+    struct vm_device * dev = (struct vm_device *)private_data;
+    struct host_pci_state * state = (struct host_pci_state *)dev->private_data;
+    
+  //  V3_Print("Reading host PCI config space update\n");
+
+    return v3_host_pci_config_read(state->host_dev, reg_num, dst, length);
+}
+
+
+
+
+/* This is really iffy....
+ * It was totally broken before, but it's _not_ totally fixed now
+ * The Expansion rom can be enabled/disabled via software using the low order bit
+ * We should probably handle that somehow here... 
+ */
+static int pt_exp_rom_write(struct pci_device * pci_dev, uint32_t * src, void * priv_data) {
+    struct vm_device * dev = (struct vm_device *)(priv_data);
+    struct host_pci_state * state = (struct host_pci_state *)dev->private_data;
+    
+    struct v3_host_pci_bar * hrom = &(state->host_dev->exp_rom);
+    struct v3_host_pci_bar * vrom = &(state->virt_exp_rom);
+
+    PrintDebug("exp_rom update: src=0x%x\n", *src);
+    PrintDebug("vrom is size=%u, addr=0x%x\n", vrom->size, (uint32_t)vrom->addr);
+    PrintDebug("hrom is size=%u, addr=0x%x\n", hrom->size, (uint32_t)hrom->addr);
+
+    if (hrom->exp_rom_enabled) {
+       // only remove old mapping if present, I.E. if the rom was enabled previously 
+       if (vrom->exp_rom_enabled) {
+           struct v3_mem_region * old_reg = v3_get_mem_region(dev->vm, V3_MEM_CORE_ANY, vrom->addr);
+         
+           if (old_reg == NULL) {
+               // uh oh...
+               PrintError("Could not find PCI Passthrough exp_rom_base redirection region (addr=0x%x)\n", (uint32_t)vrom->addr);
+               return -1;
+           }
+         
+           v3_delete_mem_region(dev->vm, old_reg);
+       }
+      
+      
+       vrom->addr = *src & ~(hrom->size - 1);
+      
+       // Set flags in actual register value
+       *src = PCI_EXP_ROM_VAL(vrom->addr, (*src & 0x00000001));
+      
+       PrintDebug("Cooked src=0x%x\n", *src);
+      
+  
+       PrintDebug("Adding pci Passthrough exp_rom_base remapping: start=0x%x, size=%u, end=0x%x\n", 
+                  (uint32_t)vrom->addr, vrom->size, (uint32_t)vrom->addr + vrom->size);
+      
+       if (v3_add_shadow_mem(dev->vm, V3_MEM_CORE_ANY, vrom->addr, 
+                             vrom->addr + vrom->size - 1, hrom->addr) == -1) {
+           PrintError("Failed to remap pci exp_rom: start=0x%x, size=%u, end=0x%x\n", 
+                      (uint32_t)vrom->addr, vrom->size, (uint32_t)vrom->addr + vrom->size);
+           return -1;
+       }
+    }
+    
+    return 0;
+}
+
+
+static int pt_cmd_update(struct pci_device * pci, pci_cmd_t cmd, uint64_t arg, void * priv_data) {
+    struct vm_device * dev = (struct vm_device *)(priv_data);
+    struct host_pci_state * state = (struct host_pci_state *)dev->private_data;
+
+    V3_Print("Host PCI Device: CMD update (%d)(arg=%llu)\n", cmd, arg);
+    
+    v3_host_pci_cmd_update(state->host_dev, cmd, arg);
+
+    return 0;
+}
+
+
+static int setup_virt_pci_dev(struct v3_vm_info * vm_info, struct vm_device * dev) {
+    struct host_pci_state * state = (struct host_pci_state *)dev->private_data;
+    struct pci_device * pci_dev = NULL;
+    struct v3_pci_bar bars[6];
+    int bus_num = 0;
+    int i;
+
+    for (i = 0; i < 6; i++) {
+       bars[i].type = PCI_BAR_PASSTHROUGH;
+       bars[i].private_data = dev;
+       bars[i].bar_init = pci_bar_init;
+       bars[i].bar_write = pci_bar_write;
+    }
+
+    pci_dev = v3_pci_register_device(state->pci_bus,
+                                    PCI_STD_DEVICE,
+                                    bus_num, -1, 0, 
+                                    state->name, bars,
+                                    pt_config_write,
+                                    pt_config_read,
+                                    pt_cmd_update,
+                                    pt_exp_rom_write,               
+                                    dev);
+
+
+    state->pci_dev = pci_dev;
+
+    //    pci_exp_rom_init(dev, state);
+    pci_dev->config_header.expansion_rom_address = 0;
+    
+    v3_pci_enable_capability(pci_dev, PCI_CAP_MSI);
+//    v3_pci_enable_capability(pci_dev, PCI_CAP_MSIX);
+    v3_pci_enable_capability(pci_dev, PCI_CAP_PCIE);
+    v3_pci_enable_capability(pci_dev, PCI_CAP_PM);
+
+
+
+    if (state->host_dev->iface == SYMBIOTIC) {
+#ifdef V3_CONFIG_SYMBIOTIC
+       v3_sym_map_pci_passthrough(vm_info, pci_dev->bus_num, pci_dev->dev_num, pci_dev->fn_num);
+#else
+       PrintError("ERROR Symbiotic Passthrough is not enabled\n");
+       return -1;
+#endif
+    }
+
+    return 0;
+}
+
+
+static struct v3_device_ops dev_ops = {
+    .free = NULL,
+};
+
+
+static int irq_ack(struct guest_info * core, uint32_t irq, void * private_data) {
+    struct host_pci_state * state = (struct host_pci_state *)private_data;
+
+    
+    //    V3_Print("Acking IRQ %d\n", irq);
+    v3_host_pci_ack_irq(state->host_dev, irq);
+
+    return 0;
+}
+
+
+static int irq_handler(void * private_data, uint32_t vec_index) {
+    struct host_pci_state * state = (struct host_pci_state *)private_data;
+    struct v3_irq vec;
+
+    vec.irq = vec_index;
+    vec.ack = irq_ack;
+    vec.private_data = state;
+
+
+    //    V3_Print("Raising host PCI IRQ %d\n", vec_index);
+
+    if (state->pci_dev->irq_type == IRQ_NONE) {
+       return 0;
+    } else if (state->pci_dev->irq_type == IRQ_INTX) {
+       v3_pci_raise_acked_irq(state->pci_bus, state->pci_dev, vec);
+    } else {
+       v3_pci_raise_irq(state->pci_bus, state->pci_dev, vec_index);
+    }
+
+    return 0;
+}
+
+
+static int host_pci_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
+    struct host_pci_state * state = V3_Malloc(sizeof(struct host_pci_state));
+    struct vm_device * dev = NULL;
+    struct vm_device * pci = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
+    char * dev_id = v3_cfg_val(cfg, "ID");    
+    char * url = v3_cfg_val(cfg, "url");
+
+    memset(state, 0, sizeof(struct host_pci_state));
+
+    if (!pci) {
+       PrintError("PCI bus not specified in config file\n");
+       return -1;
+    }
+    
+    state->pci_bus = pci;
+    strncpy(state->name, dev_id, 32);
+
+
+    dev = v3_add_device(vm, dev_id, &dev_ops, state);
+
+    if (dev == NULL) {
+       PrintError("Could not attach device %s\n", dev_id);
+       V3_Free(state);
+       return -1;
+    }
+
+    state->host_dev = v3_host_pci_get_dev(vm, url, state);
+
+    if (state->host_dev == NULL) {
+       PrintError("Could not connect to host pci device (%s)\n", url);
+       return -1;
+    }
+
+
+    state->host_dev->irq_handler = irq_handler;
+
+    if (setup_virt_pci_dev(vm, dev) == -1) {
+       PrintError("Could not setup virtual host PCI device\n");
+       return -1;
+    }
+
+    return 0;
+}
+
+
+
+
+device_register("HOST_PCI", host_pci_init)
index 98f35d8..6b497b9 100644 (file)
@@ -25,4 +25,10 @@ config EXT_INSPECTOR
        help
          Provides the inspection extension
 
+config EXT_VMWARE
+       bool "VMWare Persona"
+       default n
+       help
+         Provides a VMWare persona to allow TSC calibration
+
 endmenu
index abf382a..24d68c3 100644 (file)
@@ -4,4 +4,4 @@ obj-$(V3_CONFIG_EXT_VTSC) += ext_vtsc.o
 obj-$(V3_CONFIG_EXT_VTIME) += ext_vtime.o
 obj-$(V3_CONFIG_EXT_INSPECTOR) += ext_inspector.o
 obj-$(V3_CONFIG_EXT_MACH_CHECK) += ext_mcheck.o
-
+obj-$(V3_CONFIG_EXT_VMWARE) += ext_vmware.o
diff --git a/palacios/src/extensions/ext_vmware.c b/palacios/src/extensions/ext_vmware.c
new file mode 100644 (file)
index 0000000..9026db9
--- /dev/null
@@ -0,0 +1,133 @@
+
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jarusl@cs.pitt.edu> 
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jarusl@cs.pitt.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+#include <palacios/vmm.h>
+#include <palacios/vmm_extensions.h>
+#include <palacios/vmm_io.h>
+#include <palacios/vmm_cpuid.h>
+#include <palacios/vm_guest.h>
+
+
+#define VMWARE_CPUID_LEAF 0x40000000
+#define VMWARE_MAGIC 0x564D5868
+#define VMWARE_IO_PORT  0x5658
+
+#define VMWARE_IO_VERSION 10
+#define VMWARE_IO_GETHZ 45
+
+
+static int io_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * priv_data) {
+    uint64_t cpu_hz = V3_CPU_KHZ() * 1000;
+    uint32_t magic = (uint32_t)(core->vm_regs.rax);
+    uint32_t cmd = (uint32_t)(core->vm_regs.rcx);
+
+    PrintError("VMWARE IO READ of size %d (command=%d)\n", length, cmd);
+
+    
+    if (magic != VMWARE_MAGIC) {
+       PrintError("Invalid VMWARE MAgic number in Persona interface, ignoring for now\n");
+       return length;
+    }
+    
+    if (cmd == VMWARE_IO_GETHZ) {
+       // EAX Takes low bytes
+       // EBX takes high bytes
+       core->vm_regs.rax = cpu_hz & 0x00000000ffffffffLL;
+       core->vm_regs.rbx = (cpu_hz >> 32) & 0x00000000ffffffffLL;
+    } else {
+       PrintError("Unhandled VMWARE IO operation\n");
+       return -1;
+    }
+
+    return length;
+}
+
+
+static int io_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * priv_data) {
+
+    PrintError("VMWARE IO PORT WRITE\n");
+    return -1;
+}
+
+
+static int vmware_cpuid_handler(struct guest_info * core, uint32_t cpuid, 
+                               uint32_t * eax, uint32_t * ebx, 
+                               uint32_t * ecx, uint32_t * edx, 
+                               void * priv_data) {
+
+    // Don't Care (?)
+    *eax = 0;
+
+    // Set VMWARE Vendor string in EBX,ECX,EDX
+    memcpy(ebx, "VMwa", 4);
+    memcpy(ecx, "reVM", 4);
+    memcpy(edx, "ware", 4);
+    
+    return 0;
+}
+
+
+
+static int vmware_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg, void ** priv_data) {
+
+    V3_Print("Using VMWARE virtualization persona\n");
+
+    v3_cpuid_add_fields(vm, 0x00000001, 
+                       0, 0, 
+                       0, 0, 
+                       0x80000000, 0x80000000,
+                       0, 0
+                       );
+
+
+    v3_hook_io_port(vm, VMWARE_IO_PORT, 
+                   io_read, io_write, 
+                   NULL);
+
+    v3_hook_cpuid(vm, VMWARE_CPUID_LEAF, 
+                 vmware_cpuid_handler, NULL);
+                        
+
+    // hook io port 
+    // set CPUID hypervisor enabled
+    // set VMWare CPUID 
+
+    
+    return 0;
+
+
+}
+
+
+
+
+static struct v3_extension_impl vmware_impl = {
+    .name = "VMWARE_IFACE",
+    .init = vmware_init,
+    .deinit = NULL,
+    .core_init = NULL,
+    .core_deinit = NULL,
+    .on_entry = NULL,
+    .on_exit = NULL
+};
+
+
+
+register_extension(&vmware_impl);
index d2962a6..6553f40 100644 (file)
@@ -60,6 +60,13 @@ config PACKET
           to support the internal networking features of Palacios.
 
 
+config HOST_PCI
+       bool "Host PCI Support"
+       depends on EXPERIMENTAL
+       default y
+       help 
+         This enables host support for passthrough PCI devices
+
 config HOST_DEVICE
         bool "Host device support"
         default n
index 3c340de..c04193f 100644 (file)
@@ -7,6 +7,7 @@ obj-$(V3_CONFIG_GRAPHICS_CONSOLE) += vmm_graphics_console.o
 obj-$(V3_CONFIG_KEYED_STREAMS) += vmm_keyed_stream.o
 obj-$(V3_CONFIG_HOST_DEVICE) += vmm_host_dev.o
 obj-$(V3_CONFIG_HOST_HYPERCALL) += vmm_host_hypercall.o
+obj-$(V3_CONFIG_HOST_PCI) += host_pci.o
 
 obj-y += null.o
 
diff --git a/palacios/src/interfaces/host_pci.c b/palacios/src/interfaces/host_pci.c
new file mode 100644 (file)
index 0000000..e945b92
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jacklange@cs.pitt.edu>
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jacklange@cs.pitt.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+
+#include <interfaces/host_pci.h>
+#include <palacios/vmm.h>
+#include <palacios/vm_guest.h>
+#include <palacios/vmm_mem.h>
+
+
+
+static struct v3_host_pci_hooks * pci_hooks = NULL;
+
+
+
+void V3_Init_Host_PCI(struct v3_host_pci_hooks * hooks) {
+    pci_hooks = hooks;
+    V3_Print("V3 host PCI interface intialized\n");
+    return;
+}
+
+
+/* This is ugly and should be abstracted out to a function in the memory manager */
+int V3_get_guest_mem_region(struct v3_vm_info * vm, struct v3_guest_mem_region * region) {
+
+    if (!vm) {
+       PrintError("Tried to get a nenregion from a NULL vm pointer\n");
+       return -1;
+    }
+
+
+    region->start = vm->mem_map.base_region.host_addr;
+    region->end = vm->mem_map.base_region.host_addr + (vm->mem_map.base_region.guest_end - vm->mem_map.base_region.guest_start);
+
+    return 0;
+}
+
+
+struct v3_host_pci_dev * v3_host_pci_get_dev(struct v3_vm_info * vm, 
+                                            char * url, void * priv_data) {
+
+    struct v3_host_pci_dev * host_dev = NULL;
+
+    if ((!pci_hooks) || (!pci_hooks->request_device)) {
+       PrintError("Host PCI Hooks not initialized\n");
+       return NULL;
+    }
+
+    host_dev = pci_hooks->request_device(url, vm);
+
+    if (host_dev == NULL) {
+       PrintError("Could not find host PCI device (%s)\n", url);
+       return NULL;
+    }
+
+    host_dev->guest_data = priv_data;
+    
+    return host_dev;
+    
+}
+
+
+int v3_host_pci_config_write(struct v3_host_pci_dev * v3_dev, 
+                            uint32_t reg_num, void * src, 
+                            uint32_t length) {
+
+    if ((!pci_hooks) || (!pci_hooks->config_write)) {
+       PrintError("Host PCI hooks not initialized\n");
+       return -1;
+    }
+
+    return pci_hooks->config_write(v3_dev, reg_num, src, length);
+}
+
+
+int v3_host_pci_config_read(struct v3_host_pci_dev * v3_dev, 
+                            uint32_t reg_num, void * dst, 
+                            uint32_t length) {
+
+    if ((!pci_hooks) || (!pci_hooks->config_read)) {
+       PrintError("Host PCI hooks not initialized\n");
+       return -1;
+    }
+
+    return pci_hooks->config_read(v3_dev, reg_num, dst, length);
+}
+
+int v3_host_pci_ack_irq(struct v3_host_pci_dev * v3_dev, uint32_t vec_index) {
+
+    if ((!pci_hooks) || (!pci_hooks->ack_irq)) {
+       PrintError("Host PCI hooks not initialized\n");
+       return -1;
+    }
+
+    return pci_hooks->ack_irq(v3_dev, vec_index);
+}
+
+
+
+int v3_host_pci_cmd_update(struct v3_host_pci_dev * v3_dev, pci_cmd_t cmd, uint64_t arg ) {
+
+    if ((!pci_hooks) || (!pci_hooks->pci_cmd)) {
+       PrintError("Host PCI hooks not initialized\n");
+       return -1;
+    }
+
+    return pci_hooks->pci_cmd(v3_dev, cmd, arg);
+}
+
+
+
+
+
+int V3_host_pci_raise_irq(struct v3_host_pci_dev * v3_dev, uint32_t vec_index) {
+    if (!v3_dev->irq_handler) {
+       PrintError("No interrupt registerd for host pci device\n");
+       return -1;
+    }
+
+    return v3_dev->irq_handler(v3_dev->guest_data, vec_index);
+}
+
index cdbf81a..6d14934 100644 (file)
@@ -37,6 +37,8 @@ obj-y := \
        vmm_bitmap.o \
        vmm_barrier.o \
        vmm_timeout.o \
+       vmm_exits.o \
+       vmm_events.o
 
 
 obj-$(V3_CONFIG_XED) +=        vmm_xed.o
@@ -52,6 +54,7 @@ obj-$(V3_CONFIG_SVM) +=    svm.o \
                           svm_pause.o \
                           svm_wbinvd.o \
                           svm_handler.o \
+                          svm_exits.o \
                           vmcb.o
 
 obj-$(V3_CONFIG_VMX) +=        vmx.o \
@@ -63,7 +66,8 @@ obj-$(V3_CONFIG_VMX) +=       vmx.o \
                                vmcs.o \
                                vmx_ctrl_regs.o \
                                vmx_assist.o \
-                               vmx_ept.o 
+                               vmx_ept.o \
+                               vmx_exits.o
 
 
 
index 8a1b3e4..996ed2b 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 
+
 #include <palacios/svm.h>
 #include <palacios/vmm.h>
 
@@ -38,6 +39,8 @@
 #include <palacios/vmm_barrier.h>
 #include <palacios/vmm_debug.h>
 
+
+
 #ifdef V3_CONFIG_CHECKPOINT
 #include <palacios/vmm_checkpoint.h>
 #endif
@@ -534,12 +537,14 @@ int
 v3_svm_config_tsc_virtualization(struct guest_info * info) {
     vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data));
 
+
     if (info->time_state.flags & VM_TIME_TRAP_RDTSC) {
        ctrl_area->instrs.RDTSC = 1;
        ctrl_area->svm_instrs.RDTSCP = 1;
     } else {
        ctrl_area->instrs.RDTSC = 0;
        ctrl_area->svm_instrs.RDTSCP = 0;
+
        if (info->time_state.flags & VM_TIME_TSC_PASSTHROUGH) {
                ctrl_area->TSC_OFFSET = 0;
        } else {
diff --git a/palacios/src/palacios/svm_exits.c b/palacios/src/palacios/svm_exits.c
new file mode 100644 (file)
index 0000000..b47507d
--- /dev/null
@@ -0,0 +1,78 @@
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jarusl@cs.northwestern.edu> 
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jarusl@cs.northwestern.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+
+#include <palacios/vmm.h>
+#include <palacios/vmcb.h>
+#include <palacios/vmm_exits.h>
+
+
+
+static int enable_exit(struct guest_info * core, v3_exit_type_t exit_type) {
+    vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(core->vmm_data));
+
+    switch (exit_type) {
+
+       case V3_EXIT_RDTSC:
+           ctrl_area->instrs.RDTSC = 1;
+           break;
+       case V3_EXIT_RDTSCP:
+           ctrl_area->svm_instrs.RDTSCP = 1;
+           break;
+
+       default:
+           PrintError("Unhandled Exit Type (%d)\n", exit_type);
+           return -1;
+    }
+
+    return 0;
+}
+
+
+static int disable_exit(struct guest_info * core, v3_exit_type_t exit_type) {
+    vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(core->vmm_data));
+
+    switch (exit_type) {
+
+       case V3_EXIT_RDTSC:
+           ctrl_area->instrs.RDTSC = 0;
+           break;
+       case V3_EXIT_RDTSCP:
+           ctrl_area->svm_instrs.RDTSCP = 0;
+           break;
+
+       default:
+           PrintError("Unhandled Exit Type (%d)\n", exit_type);
+           return -1;
+    }
+
+    return 0;
+
+}
+
+
+int v3_init_svm_exits(struct v3_vm_info * vm) {
+
+    int ret = 0;
+
+    ret |= v3_register_exit(vm, V3_EXIT_RDTSC, enable_exit, disable_exit);
+    ret |= v3_register_exit(vm, V3_EXIT_RDTSCP, enable_exit, disable_exit);
+    
+    return ret;
+}
index 5849055..14f4454 100644 (file)
@@ -266,9 +266,10 @@ int v3_handle_svm_exit(struct guest_info * info, addr_t exit_code, addr_t exit_i
            PrintDebug("RDTSCP\n");
 #endif 
            if (v3_handle_rdtscp(info) == -1) {
-               PrintError("Error Handling RDTSCP instruction\n");
+               PrintError("Error handling RDTSCP instruction\n");
                return -1;
            }
+           
            break;
        case SVM_EXIT_SHUTDOWN:
            PrintDebug("Guest-initiated shutdown\n");
index 957c3dd..dc47a46 100644 (file)
@@ -204,29 +204,33 @@ static int info_hcall(struct guest_info * core, uint_t hcall_id, void * priv_dat
 #include <palacios/svm.h>
 #include <palacios/svm_io.h>
 #include <palacios/svm_msr.h>
+#include <palacios/svm_exits.h>
 #endif
 
 #ifdef V3_CONFIG_VMX
 #include <palacios/vmx.h>
 #include <palacios/vmx_io.h>
 #include <palacios/vmx_msr.h>
+#include <palacios/vmx_exits.h>
 #endif
 
 
 int v3_init_vm(struct v3_vm_info * vm) {
     extern v3_cpu_arch_t v3_mach_type;
 
-
+    v3_init_events(vm);
 
 #ifdef V3_CONFIG_TELEMETRY
     v3_init_telemetry(vm);
 #endif
 
+    v3_init_exit_hooks(vm);
     v3_init_hypercall_map(vm);
     v3_init_io_map(vm);
     v3_init_msr_map(vm);
     v3_init_cpuid_map(vm);
     v3_init_host_events(vm);
+
     v3_init_intr_routers(vm);
     v3_init_ext_manager(vm);
 
@@ -265,6 +269,7 @@ int v3_init_vm(struct v3_vm_info * vm) {
        case V3_SVM_REV3_CPU:
            v3_init_svm_io_map(vm);
            v3_init_svm_msr_map(vm);
+           v3_init_svm_exits(vm);
            break;
 #endif
 #ifdef V3_CONFIG_VMX
@@ -273,6 +278,7 @@ int v3_init_vm(struct v3_vm_info * vm) {
        case V3_VMX_EPT_UG_CPU:
            v3_init_vmx_io_map(vm);
            v3_init_vmx_msr_map(vm);
+           v3_init_vmx_exits(vm);
            break;
 #endif
        default:
@@ -340,10 +346,13 @@ int v3_free_vm_internal(struct v3_vm_info * vm) {
     v3_deinit_io_map(vm);
     v3_deinit_hypercall_map(vm);
 
+    v3_deinit_exit_hooks(vm);
+
 #ifdef V3_CONFIG_TELEMETRY
     v3_deinit_telemetry(vm);
 #endif
 
+    v3_deinit_events(vm);
 
 
     return 0;
@@ -405,6 +414,9 @@ int v3_init_core(struct guest_info * core) {
            PrintError("Invalid CPU Type 0x%x\n", v3_mach_type);
            return -1;
     }
+    
+    v3_init_exit_hooks_core(core);
+
 
     return 0;
 }
index 18871b0..1d8cd07 100644 (file)
@@ -457,6 +457,12 @@ int v3_move_vm_core(struct v3_vm_info * vm, int vcore_id, int target_cpu) {
 
 int v3_stop_vm(struct v3_vm_info * vm) {
 
+    if ((vm->run_state |= VM_RUNNING) && 
+       (vm->run_state != VM_SIMULATING)) {
+       PrintError("Tried to stop VM in invalid runstate (%d)\n", vm->run_state);
+       return -1;
+    }
+
     vm->run_state = VM_STOPPED;
 
     // Sanity check to catch any weird execution states
@@ -649,6 +655,12 @@ int v3_free_vm(struct v3_vm_info * vm) {
     int i = 0;
     // deinitialize guest (free memory, etc...)
 
+    if ((vm->run_state != VM_STOPPED) &&
+       (vm->run_state != VM_ERROR)) {
+       PrintError("Tried to Free VM in invalid runstate (%d)\n", vm->run_state);
+       return -1;
+    }
+
     v3_free_vm_devices(vm);
 
     // free cores
index e6f1221..19b2db2 100644 (file)
@@ -141,6 +141,12 @@ int v3_wait_for_barrier(struct v3_vm_info * vm_info, struct guest_info * local_c
 int v3_raise_barrier(struct v3_vm_info * vm_info, struct guest_info * local_core) {
     int ret = 0;
 
+
+    if ((vm_info->run_state != VM_RUNNING) || 
+       (vm_info->run_state != VM_SIMULATING)) {
+       return 0;
+    }
+
     ret = v3_raise_barrier_nowait(vm_info, local_core);
 
     if (ret != 0) {
@@ -162,6 +168,12 @@ int v3_raise_barrier(struct v3_vm_info * vm_info, struct guest_info * local_core
 int v3_lower_barrier(struct v3_vm_info * vm_info) {
     struct v3_barrier * barrier = &(vm_info->barrier);
 
+    
+    if ((vm_info->run_state != VM_RUNNING) || 
+       (vm_info->run_state != VM_SIMULATING)) {
+       return 0;
+    }
+
     // Clear the active flag, so cores won't wait 
     barrier->active = 0;
 
index 14b042d..a94d557 100644 (file)
 int v3_bitmap_init(struct v3_bitmap * bitmap, int num_bits) {
     int num_bytes = (num_bits / 8) + ((num_bits % 8) > 0);
 
+    v3_lock_init(&(bitmap->lock));
     bitmap->num_bits = num_bits;
     bitmap->bits = V3_Malloc(num_bytes);
 
+
     if (bitmap->bits == NULL) {
        PrintError("Could not allocate bitmap of %d bits\n", num_bits);
        return -1;
@@ -39,6 +41,7 @@ int v3_bitmap_init(struct v3_bitmap * bitmap, int num_bits) {
 
 
 void v3_bitmap_deinit(struct v3_bitmap * bitmap) {
+    v3_lock_deinit(&(bitmap->lock));
     V3_Free(bitmap->bits);
 }
 
@@ -55,6 +58,7 @@ int v3_bitmap_set(struct v3_bitmap * bitmap, int index) {
     int major = index / 8;
     int minor = index % 8;
     int old_val = 0;
+    uint32_t flags = 0;
 
     if (index > (bitmap->num_bits - 1)) {
        PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
@@ -62,9 +66,14 @@ int v3_bitmap_set(struct v3_bitmap * bitmap, int index) {
        return -1;
     }
 
+
+    flags = v3_lock_irqsave(bitmap->lock);
+
     old_val = (bitmap->bits[major] & (0x1 << minor));
     bitmap->bits[major] |= (0x1 << minor);
 
+    v3_unlock_irqrestore(bitmap->lock, flags);
+
     return old_val;
 }
 
@@ -73,6 +82,7 @@ int v3_bitmap_clear(struct v3_bitmap * bitmap, int index) {
     int major = index / 8;
     int minor = index % 8;
     int old_val = 0;
+    uint32_t flags = 0;
 
     if (index > (bitmap->num_bits - 1)) {
        PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
@@ -80,9 +90,13 @@ int v3_bitmap_clear(struct v3_bitmap * bitmap, int index) {
        return -1;
     }
 
+    flags = v3_lock_irqsave(bitmap->lock);
+
     old_val = (bitmap->bits[major] & (0x1 << minor));
     bitmap->bits[major] &= ~(0x1 << minor);
 
+    v3_unlock_irqrestore(bitmap->lock, flags);
+
     return old_val;
 }
 
diff --git a/palacios/src/palacios/vmm_events.c b/palacios/src/palacios/vmm_events.c
new file mode 100644 (file)
index 0000000..a001605
--- /dev/null
@@ -0,0 +1,133 @@
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jarusl@cs.northwestern.edu> 
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jarusl@cs.northwestern.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+
+
+#include <palacios/vmm_events.h>
+#include <palacios/vm_guest.h>
+
+
+
+int v3_init_events(struct v3_vm_info * vm) {
+    struct v3_event_map * map = &(vm->event_map);
+    int i = 0;
+
+    map->events = V3_Malloc(sizeof(struct list_head) * V3_EVENT_INVALID);
+
+    if (map->events == NULL) {
+       PrintError("Error: could not allocate event map\n");
+       return -1;
+    }
+
+    for (i = 0; i < V3_EVENT_INVALID; i++) {
+       INIT_LIST_HEAD(&(map->events[i]));
+    }
+
+    return 0;
+}
+
+int v3_deinit_events(struct v3_vm_info * vm) {
+    struct v3_event_map * map = &(vm->event_map);
+    int i = 0;
+
+    for (i = 0; i < V3_EVENT_INVALID; i++) {
+       if (!list_empty(&(map->events[i]))) {
+           struct v3_notifier * tmp_notifier = NULL;
+           struct v3_notifier * safe_notifier = NULL;
+           PrintError("Found orphan notifier for event %d. Probable memory leak detected.\n", i);
+           
+           list_for_each_entry_safe(tmp_notifier, safe_notifier, &(map->events[i]), node) {
+               list_del(&(tmp_notifier->node));
+               V3_Free(tmp_notifier);
+           }
+       }
+    }
+
+
+    V3_Free(map->events);
+
+    return 0;
+
+}
+
+
+struct v3_notifier * v3_subscribe_event(struct v3_vm_info * vm, 
+                    v3_event_type_t event_type, 
+                    void (*notify)(struct guest_info * core, 
+                                   v3_event_type_t event_type,
+                                   void * priv_data, 
+                                   void * event_data),
+                    void * priv_data, 
+                    struct guest_info * current_core) {
+    struct v3_event_map * map = &(vm->event_map);
+    struct v3_notifier * notifier = NULL;
+
+    if (event_type >= V3_EVENT_INVALID) {
+       PrintError("Tried to request illegal event (%d)\n", event_type);
+       return NULL;
+    }
+
+    notifier = V3_Malloc(sizeof(struct v3_notifier));
+
+    if (notifier == NULL) {
+       PrintError("Error: Could not allocate notifier\n");
+       return NULL;
+    }
+
+    memset(notifier, 0, sizeof(struct v3_notifier));
+    
+    notifier->notify = notify;
+    notifier->priv_data = priv_data;
+    notifier->event_type = event_type;
+
+    while (v3_raise_barrier(vm, current_core) == -1);
+    list_add(&(notifier->node), &(map->events[event_type]));
+    v3_lower_barrier(vm);
+
+    return notifier;;
+}
+
+
+int v3_unsubscribe_event(struct v3_vm_info * vm, struct v3_notifier * notifier, 
+                        struct guest_info * current_core) {
+    struct v3_event_map * map = &(vm->event_map);
+    struct v3_notifier * tmp_notifier = NULL;
+    struct v3_notifier * safe_notifier = NULL;    
+
+    if (notifier == NULL) {
+       PrintError("Could not unsubscribe invalid event notifier\n");
+       return -1;
+    }
+    
+    if (notifier->event_type >= V3_EVENT_INVALID) {
+       PrintError("Could not unsubscribe from invalid event\n");
+       return -1;
+    }
+
+    while (v3_raise_barrier(vm, current_core) == -1);
+    list_for_each_entry_safe(tmp_notifier, safe_notifier, &(map->events[notifier->event_type]), node) {
+       if (tmp_notifier == notifier) {
+           list_del(&(tmp_notifier->node));
+           V3_Free(tmp_notifier);
+       }
+    }
+    v3_lower_barrier(vm);
+
+    return 0;
+}
diff --git a/palacios/src/palacios/vmm_exits.c b/palacios/src/palacios/vmm_exits.c
new file mode 100644 (file)
index 0000000..97504bc
--- /dev/null
@@ -0,0 +1,227 @@
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jarusl@cs.northwestern.edu> 
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jarusl@cs.northwestern.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+
+#include <palacios/vmm.h>
+#include <palacios/vmm_exits.h>
+#include <palacios/vm_guest.h>
+
+
+int v3_init_exit_hooks(struct v3_vm_info * vm) {
+    struct v3_exit_map * map = &(vm->exit_map);
+    
+    map->exits = V3_Malloc(sizeof(struct v3_exit_hook) * V3_EXIT_INVALID);
+
+    if (map->exits == NULL) {
+       PrintError("Error allocating exit map\n");
+       return -1;
+    }
+    
+    memset(map->exits, 0, sizeof(struct v3_exit_hook) * V3_EXIT_INVALID);
+
+
+    return 0;
+}
+
+int v3_deinit_exit_hooks(struct v3_vm_info * vm) {
+    struct v3_exit_map * map = &(vm->exit_map);
+
+    V3_Free(map->exits);
+
+    return 0;
+}
+
+
+
+
+int v3_init_exit_hooks_core(struct guest_info * core) {
+    struct v3_vm_info * vm = core->vm_info;
+    struct v3_exit_map * map = &(vm->exit_map);         
+    struct v3_exit_hook * hook = NULL;
+    int i = 0;
+
+    for (i = 0; i < V3_EXIT_INVALID; i++) {
+       hook = &(map->exits[i]);
+
+       if (hook->hooked) {
+           if (hook->enable(core, i) != 0) {
+               PrintError("Error could not enable exit hook %d on core %d\n", i, core->vcpu_id);
+               return -1;
+           }
+       }
+    }
+
+    return 0;
+}
+
+int v3_deinit_exit_hooks_core(struct guest_info * core) {
+
+    return 0;
+}
+
+
+
+int v3_dispatch_exit_hook(struct guest_info * core, v3_exit_type_t exit_type, void * exit_data) {
+    struct v3_exit_map * map = &(core->vm_info->exit_map);      
+    struct v3_exit_hook * hook = NULL;
+
+    if (exit_type >= V3_EXIT_INVALID) {
+       PrintError("Error: Tried to dispatch invalid exit type (%d)\n", exit_type);
+       return -1;
+    }
+  
+    hook = &(map->exits[exit_type]);
+
+    if (hook->hooked == 0) {
+       PrintError("Tried to dispatch an unhooked exit (%d)\n", exit_type);
+       return -1;
+    }
+
+    return hook->handler(core, exit_type, hook->priv_data, exit_data);
+   
+}
+
+
+int v3_register_exit(struct v3_vm_info * vm, v3_exit_type_t exit_type,
+                    int (*enable)(struct guest_info * core, v3_exit_type_t exit_type),
+                    int (*disable)(struct guest_info * core, v3_exit_type_t exit_type)) {
+    struct v3_exit_map * map = &(vm->exit_map);         
+    struct v3_exit_hook * hook = NULL;
+
+    if (exit_type >= V3_EXIT_INVALID) {
+       PrintError("Error: Tried to register invalid exit type (%d)\n", exit_type);
+       return -1;
+    }
+  
+    hook = &(map->exits[exit_type]);
+
+    if (hook->registered == 1) {
+       PrintError("Tried to reregister an exit (%d)\n", exit_type);
+       return -1;
+    }
+
+    hook->registered = 1;
+    hook->enable = enable;
+    hook->disable = disable;
+    
+
+    return 0;
+}
+
+
+int v3_hook_exit(struct v3_vm_info * vm, v3_exit_type_t exit_type,
+                int (*handler)(struct guest_info * core, v3_exit_type_t exit_type, 
+                            void * priv_data, void * exit_data),
+                void * priv_data, 
+                struct guest_info * current_core) {
+    struct v3_exit_map * map = &(vm->exit_map);         
+    struct v3_exit_hook * hook = NULL;
+    
+    
+    if (exit_type >= V3_EXIT_INVALID) {
+       PrintError("Error: Tried to hook invalid exit type (%d)\n", exit_type);
+       return -1;
+    }
+  
+    hook = &(map->exits[exit_type]);
+
+    if (hook->registered == 0) {
+       PrintError("Tried to hook unregistered exit (%d)\n", exit_type);
+       return -1;
+    } 
+
+    if (hook->hooked != 0) {
+       PrintError("Tried to rehook exit (%d)\n", exit_type);
+       return -1;
+    }
+
+
+    hook->hooked = 1;
+    hook->handler = handler;
+    hook->priv_data = priv_data;
+
+    if (vm->run_state != VM_INVALID) {
+       int i = 0;
+
+       while (v3_raise_barrier(vm, current_core) == -1);
+       
+       for (i = 0; i < vm->num_cores; i++) {
+           
+           if (hook->enable(&(vm->cores[i]), exit_type) != 0) {
+               PrintError("Error could not enable exit hook %d on core %d\n", exit_type, i);
+               v3_lower_barrier(vm);
+               return -1;
+           }   
+       }
+
+       v3_lower_barrier(vm);
+    }
+    
+    return 0;
+}
+
+
+
+int v3_unhook_exit(struct v3_vm_info * vm, v3_exit_type_t exit_type, struct guest_info * current_core) {
+    struct v3_exit_map * map = &(vm->exit_map);         
+    struct v3_exit_hook * hook = NULL;
+    
+    
+    if (exit_type >= V3_EXIT_INVALID) {
+       PrintError("Error: Tried to unhook invalid exit type (%d)\n", exit_type);
+       return -1;
+    }
+  
+    hook = &(map->exits[exit_type]);
+
+    if (hook->registered == 0) {
+       PrintError("Tried to unhook an unregistered exit (%d)\n", exit_type);
+       return -1;
+    } 
+
+    if (hook->hooked == 0) {
+       PrintError("Tried to unhook and unhooked exit (%d)\n", exit_type);
+       return -1;
+    }
+
+
+    hook->hooked = 0;
+    hook->handler = NULL;
+    hook->priv_data = NULL;
+
+    
+    if (vm->run_state != VM_INVALID) {
+       int i = 0;
+
+       while (v3_raise_barrier(vm, current_core) == -1);
+       
+       for (i = 0; i < vm->num_cores; i++) {
+           
+           if (hook->disable(&(vm->cores[i]), exit_type) != 0) {
+               PrintError("Error could not enable exit hook %d on core %d\n", exit_type, i);
+               v3_lower_barrier(vm);
+               return -1;
+           }   
+       }
+
+       v3_lower_barrier(vm);
+    }
+    
+    return 0;
+}
index 972c800..417c2f9 100644 (file)
@@ -656,9 +656,11 @@ static pt_entry_type_t pdpe64_lookup(pdpe64_t * pdp, addr_t addr, addr_t * entry
        *entry = 0;
        return PT_ENTRY_NOT_PRESENT;
     } else if (pdpe_entry->large_page) {
-       PrintError("1 Gigabyte pages not supported\n");
-       V3_ASSERT(0);
-       return -1;
+       pdpe64_1GB_t * large_pdp = (pdpe64_1GB_t *)pdpe_entry;
+
+       *entry = BASE_TO_PAGE_ADDR_1GB(large_pdp->page_base_addr);
+
+       return PT_ENTRY_LARGE_PAGE;
     } else {
        *entry = BASE_TO_PAGE_ADDR(pdpe_entry->pd_base_addr);
        return PT_ENTRY_PAGE;
index 28d41ac..5414ce9 100644 (file)
@@ -19,6 +19,9 @@
 
 #include <palacios/vmm_queue.h>
 
+
+
+
 void v3_init_queue(struct v3_queue * queue) {
     queue->num_entries = 0;
     INIT_LIST_HEAD(&(queue->entries));
@@ -37,6 +40,17 @@ struct v3_queue * v3_create_queue() {
     return tmp_queue;
 }
 
+void v3_deinit_queue(struct v3_queue * queue) {
+    while (v3_dequeue(queue)) {
+       PrintError("ERROR: Freeing non-empty queue. PROBABLE MEMORY LEAK DETECTED\n");
+    }
+
+    v3_lock_deinit(&(queue->lock));
+}
+
+
+
+
 void v3_enqueue(struct v3_queue * queue, addr_t entry) {
     struct v3_queue_entry * q_entry = V3_Malloc(sizeof(struct v3_queue_entry));
     unsigned int flags = 0;
diff --git a/palacios/src/palacios/vmx_exits.c b/palacios/src/palacios/vmx_exits.c
new file mode 100644 (file)
index 0000000..b3f5ad9
--- /dev/null
@@ -0,0 +1,78 @@
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2012, Jack Lange <jarusl@cs.northwestern.edu> 
+ * Copyright (c) 2012, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jarusl@cs.northwestern.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+
+#include <palacios/vmm.h>
+#include <palacios/vmcb.h>
+#include <palacios/vmm_exits.h>
+
+
+
+static int enable_exit(struct guest_info * core, v3_exit_type_t exit_type) {
+    vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(core->vmm_data));
+
+    switch (exit_type) {
+
+       case V3_EXIT_RDTSC:
+           ctrl_area->instrs.RDTSC = 1;
+           break;
+       case V3_EXIT_RDTSCP:
+           ctrl_area->svm_instrs.RDTSCP = 1;
+           break;
+
+       default:
+           PrintError("Unhandled Exit Type (%d)\n", exit_type);
+           return -1;
+    }
+
+    return 0;
+}
+
+
+static int disable_exit(struct guest_info * core, v3_exit_type_t exit_type) {
+    vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(core->vmm_data));
+
+    switch (exit_type) {
+
+       case V3_EXIT_RDTSC:
+           ctrl_area->instrs.RDTSC = 0;
+           break;
+       case V3_EXIT_RDTSCP:
+           ctrl_area->svm_instrs.RDTSCP = 0;
+           break;
+
+       default:
+           PrintError("Unhandled Exit Type (%d)\n", exit_type);
+           return -1;
+    }
+
+    return 0;
+
+}
+
+
+int v3_init_vmx_exits(struct v3_vm_info * vm) {
+
+    int ret = 0;
+
+    ret |= v3_register_exit(vm, V3_EXIT_RDTSC, enable_exit, disable_exit);
+    ret |= v3_register_exit(vm, V3_EXIT_RDTSCP, enable_exit, disable_exit);
+    
+    return ret;
+}
index 3e4dee3..c8ce677 100644 (file)
@@ -919,7 +919,10 @@ int v3_init_vnet() {
 }
 
 
-void v3_deinit_vnet(){
+void v3_deinit_vnet() {
+
+    v3_deinit_queue(vnet_state.poll_devs);
+    Vnet_Free(vnet_state.poll_devs);
 
     PrintDebug("Stopping flush thread\n");
     // This will pause until the flush thread is gone