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.


checkpoint changes to get "reset to BIOS" working.
[palacios.git] / palacios / src / devices / pci.c
index 06ccb6b..05272a8 100644 (file)
 #include <devices/pci.h>
 #include <devices/pci_types.h>
 
+#include <palacios/vm_guest.h>
 
 
-#ifndef CONFIG_DEBUG_PCI
+
+#ifndef V3_CONFIG_DEBUG_PCI
 #undef PrintDebug
 #define PrintDebug(fmt, args...)
 #endif
@@ -80,9 +82,9 @@ struct pci_bus {
     uint8_t dev_map[MAX_BUS_DEVICES / 8];
 
 
-    int (*raise_pci_irq)(struct vm_device * dev, struct pci_device * pci_dev);
-    int (*lower_pci_irq)(struct vm_device * dev, struct pci_device * pci_dev);
-    struct vm_device * irq_bridge_dev;
+    int (*raise_pci_irq)(struct pci_device * pci_dev, void * dev_data);
+    int (*lower_pci_irq)(struct pci_device * pci_dev, void * dev_data);
+    void * irq_dev_data;
 };
 
 
@@ -102,7 +104,7 @@ struct pci_internal {
 
 
 
-#ifdef CONFIG_DEBUG_PCI
+#ifdef V3_CONFIG_DEBUG_PCI
 
 static void pci_dump_state(struct pci_internal * pci_state) {
     struct rb_node * node = v3_rb_first(&(pci_state->bus_list[0].devices));
@@ -226,8 +228,8 @@ static struct pci_device * get_device(struct pci_bus * bus, uint8_t dev_num, uin
 
 
 
-static int addr_port_read(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
-    struct pci_internal * pci_state = (struct pci_internal *)dev->private_data;
+static int addr_port_read(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
+    struct pci_internal * pci_state = priv_data;
     int reg_offset = port & 0x3;
     uint8_t * reg_addr = ((uint8_t *)&(pci_state->addr_reg.val)) + reg_offset;
 
@@ -257,8 +259,8 @@ static int addr_port_read(ushort_t port, void * dst, uint_t length, struct vm_de
 }
 
 
-static int addr_port_write(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
-    struct pci_internal * pci_state = (struct pci_internal *)dev->private_data;
+static int addr_port_write(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
+    struct pci_internal * pci_state = priv_data;
     int reg_offset = port & 0x3; 
     uint8_t * reg_addr = ((uint8_t *)&(pci_state->addr_reg.val)) + reg_offset;
 
@@ -295,8 +297,8 @@ static int addr_port_write(ushort_t port, void * src, uint_t length, struct vm_d
 }
 
 
-static int data_port_read(ushort_t port, void * dst, uint_t length, struct vm_device * vmdev) {
-    struct pci_internal * pci_state =  (struct pci_internal *)(vmdev->private_data);
+static int data_port_read(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
+    struct pci_internal * pci_state =  priv_data;
     struct pci_device * pci_dev = NULL;
     uint_t reg_num = (pci_state->addr_reg.reg_num << 2) + (port & 0x3);
     int i;
@@ -393,7 +395,6 @@ static inline int is_cfg_reg_writable(uchar_t header_type, int reg_num) {
     }
 }
 
-
 static int bar_update(struct guest_info * info, struct pci_device * pci, int bar_num, uint32_t new_val) {
     struct v3_pci_bar * bar = &(pci->bar[bar_num]);
 
@@ -418,15 +419,15 @@ static int bar_update(struct guest_info * info, struct pci_device * pci, int bar
                PrintDebug("Rehooking PCI IO port (old port=%u) (new port=%u)\n",  
                           PCI_IO_BASE(bar->val) + i, PCI_IO_BASE(new_val) + i);
 
-               v3_unhook_io_port(info, PCI_IO_BASE(bar->val) + i);
+               v3_unhook_io_port(info->vm_info, PCI_IO_BASE(bar->val) + i);
 
-               if (v3_hook_io_port(info, PCI_IO_BASE(new_val) + i, 
+               if (v3_hook_io_port(info->vm_info, PCI_IO_BASE(new_val) + i, 
                                    bar->io_read, bar->io_write, 
                                    bar->private_data) == -1) {
 
                    PrintError("Could not hook PCI IO port (old port=%u) (new port=%u)\n",  
                               PCI_IO_BASE(bar->val) + i, PCI_IO_BASE(new_val) + i);
-                   v3_print_io_map(info);
+                   v3_print_io_map(info->vm_info);
                    return -1;
                }
            }
@@ -436,10 +437,10 @@ static int bar_update(struct guest_info * info, struct pci_device * pci, int bar
            break;
        }
        case PCI_BAR_MEM32: {
-           v3_unhook_mem(info, (addr_t)(bar->val));
+           v3_unhook_mem(info->vm_info, V3_MEM_CORE_ANY, (addr_t)(bar->val));
            
            if (bar->mem_read) {
-               v3_hook_full_mem(info, PCI_MEM32_BASE(new_val), 
+               v3_hook_full_mem(info->vm_info, V3_MEM_CORE_ANY, PCI_MEM32_BASE(new_val), 
                                 PCI_MEM32_BASE(new_val) + (bar->num_pages * PAGE_SIZE_4KB),
                                 bar->mem_read, bar->mem_write, pci->priv_data);
            } else {
@@ -465,8 +466,8 @@ static int bar_update(struct guest_info * info, struct pci_device * pci, int bar
 }
 
 
-static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_device * vmdev) {
-    struct pci_internal * pci_state = (struct pci_internal *)vmdev->private_data;
+static int data_port_write(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
+    struct pci_internal * pci_state = priv_data;
     struct pci_device * pci_dev = NULL;
     uint_t reg_num = (pci_state->addr_reg.reg_num << 2) + (port & 0x3);
     int i;
@@ -527,7 +528,7 @@ static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_d
            } else if ((cur_reg >= 0x30) && (cur_reg < 0x34)) {
                // Extension ROM update
 
-               pci_dev->ext_rom_update_flag = 1;
+               pci_dev->exp_rom_update_flag = 1;
            } else if (cur_reg == 0x04) {
                // COMMAND update            
                uint8_t command = *((uint8_t *)src + i);
@@ -573,7 +574,7 @@ static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_d
                    // check special flags....
 
                    // bar_update
-                   if (bar_update(vmdev->vm, pci_dev, i, *(uint32_t *)(pci_dev->config_space + bar_offset)) == -1) {
+                   if (bar_update(core, pci_dev, i, *(uint32_t *)(pci_dev->config_space + bar_offset)) == -1) {
                        PrintError("PCI Device %s: Bar update Error Bar=%d\n", pci_dev->name, i);
                        return -1;
                    }
@@ -585,9 +586,9 @@ static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_d
        pci_dev->bar_update_flag = 0;
     }
 
-    if ((pci_dev->ext_rom_update_flag) && (pci_dev->ext_rom_update)) {
-       pci_dev->ext_rom_update(pci_dev);
-       pci_dev->ext_rom_update_flag = 0;
+    if ((pci_dev->exp_rom_update_flag) && (pci_dev->exp_rom_update)) {
+       pci_dev->exp_rom_update(pci_dev, &(pci_dev->config_header.expansion_rom_address), pci_dev->priv_data);
+       pci_dev->exp_rom_update_flag = 0;
     }
 
 
@@ -596,72 +597,160 @@ static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_d
 
 
 
-static int pci_reset_device(struct vm_device * dev) {
-    PrintDebug("pci: reset device\n");    
-    return 0;
-}
 
 
-static int pci_start_device(struct vm_device * dev) {
-    PrintDebug("pci: start device\n");
-    return 0;
+static void init_pci_busses(struct pci_internal * pci_state) {
+    int i;
+
+    for (i = 0; i < PCI_BUS_COUNT; i++) {
+       pci_state->bus_list[i].bus_num = i;
+       pci_state->bus_list[i].devices.rb_node = NULL;
+       memset(pci_state->bus_list[i].dev_map, 0, sizeof(pci_state->bus_list[i].dev_map));
+    }
 }
 
 
-static int pci_stop_device(struct vm_device * dev) {
-    PrintDebug("pci: stop device\n");  
+static int pci_free(struct pci_internal * pci_state) {
+    int i;
+
+
+    // cleanup devices
+    for (i = 0; i < PCI_BUS_COUNT; i++) {
+       struct pci_bus * bus = &(pci_state->bus_list[i]);
+       struct rb_node * node = v3_rb_first(&(bus->devices));
+       struct pci_device * dev = NULL;
+
+       while (node) {
+           dev = rb_entry(node, struct pci_device, dev_tree_node);
+           node = v3_rb_next(node);
+           
+           v3_rb_erase(&(dev->dev_tree_node), &(bus->devices));
+           V3_Free(dev);
+       }
+
+    }
+    
+    V3_Free(pci_state);
     return 0;
 }
 
+#ifdef V3_CONFIG_CHECKPOINT
 
+#include <palacios/vmm_sprintf.h>
 
-static int pci_free(struct vm_device * dev) {
-    int i = 0;
+static int pci_save(struct v3_chkpt_ctx * ctx, void * private_data) {
+    struct pci_internal * pci = (struct pci_internal *)private_data;
+    char buf[128];
+    int i = 0;    
     
-    for (i = 0; i < 4; i++){
-       v3_dev_unhook_io(dev, CONFIG_ADDR_PORT + i);
-       v3_dev_unhook_io(dev, CONFIG_DATA_PORT + i);
+    v3_chkpt_save_32(ctx, "ADDR_REG", &(pci->addr_reg.val));
+    v3_chkpt_save_16(ctx, "IO_BASE", &(pci->dev_io_base));
+
+    for (i = 0; i < PCI_BUS_COUNT; i++) {
+       struct pci_bus * bus = &(pci->bus_list[i]);
+       struct rb_node * node = v3_rb_first(&(bus->devices));
+       struct pci_device * dev = NULL;
+       struct v3_chkpt_ctx * bus_ctx = NULL;
+
+       snprintf(buf, 128, "pci-%d\n", i);
+       
+       bus_ctx = v3_chkpt_open_ctx(ctx->chkpt, ctx, buf);
+
+       while (node) {
+           struct v3_chkpt_ctx * dev_ctx = NULL;
+           int bar_idx = 0;
+           dev = rb_entry(node, struct pci_device, dev_tree_node);
+
+           snprintf(buf, 128, "pci-%d.%d-%d", i, dev->dev_num, dev->fn_num);
+           dev_ctx = v3_chkpt_open_ctx(bus_ctx->chkpt, bus_ctx, buf);
+           
+           v3_chkpt_save(dev_ctx, "CONFIG_SPACE", 256, dev->config_space);
+
+           for (bar_idx = 0; bar_idx < 6; bar_idx++) {
+               snprintf(buf, 128, "BAR-%d", bar_idx);
+               v3_chkpt_save_32(dev_ctx, buf, &(dev->bar[bar_idx].val));
+           }
+
+           node = v3_rb_next(node);
+       }
     }
-    
+
+
     return 0;
 }
 
 
-
-static void init_pci_busses(struct pci_internal * pci_state) {
-    int i;
+static int pci_load(struct v3_chkpt_ctx * ctx, void * private_data) {
+    struct pci_internal * pci = (struct pci_internal *)private_data;
+    char buf[128];
+    int i = 0;    
+    
+    v3_chkpt_load_32(ctx, "ADDR_REG", &(pci->addr_reg.val));
+    v3_chkpt_load_16(ctx, "IO_BASE", &(pci->dev_io_base));
 
     for (i = 0; i < PCI_BUS_COUNT; i++) {
-       pci_state->bus_list[i].bus_num = i;
-       pci_state->bus_list[i].devices.rb_node = NULL;
-       memset(pci_state->bus_list[i].dev_map, 0, sizeof(pci_state->bus_list[i].dev_map));
+       struct pci_bus * bus = &(pci->bus_list[i]);
+       struct rb_node * node = v3_rb_first(&(bus->devices));
+       struct pci_device * dev = NULL;
+       struct v3_chkpt_ctx * bus_ctx = NULL;
+
+       snprintf(buf, 128, "pci-%d\n", i);
+       
+       bus_ctx = v3_chkpt_open_ctx(ctx->chkpt, ctx, buf);
+
+       while (node) {
+           struct v3_chkpt_ctx * dev_ctx = NULL;
+           int bar_idx = 0;
+           dev = rb_entry(node, struct pci_device, dev_tree_node);
+
+           snprintf(buf, 128, "pci-%d.%d-%d", i, dev->dev_num, dev->fn_num);
+           dev_ctx = v3_chkpt_open_ctx(bus_ctx->chkpt, bus_ctx, buf);
+           
+           v3_chkpt_load(dev_ctx, "CONFIG_SPACE", 256, dev->config_space);
+
+           for (bar_idx = 0; bar_idx < 6; bar_idx++) {
+               snprintf(buf, 128, "BAR-%d", bar_idx);
+               v3_chkpt_load_32(dev_ctx, buf, &(dev->bar[bar_idx].val));
+           }
+
+           node = v3_rb_next(node);
+       }
     }
+
+
+    return 0;
 }
 
 
+#endif
+
+
 
 
 static struct v3_device_ops dev_ops = {
-    .free = pci_free,
-    .reset = pci_reset_device,
-    .start = pci_start_device,
-    .stop = pci_stop_device,
+    .free = (int (*)(void *))pci_free,
+#ifdef V3_CONFIG_CHECKPOINT
+    .save = pci_save,
+    .load = pci_load
+#endif
 };
 
 
 
 
-static int pci_init(struct guest_info * vm, v3_cfg_tree_t * cfg) {
+static int pci_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     struct pci_internal * pci_state = V3_Malloc(sizeof(struct pci_internal));
     int i = 0;
-    char * name = v3_cfg_val(cfg, "name");
+    char * dev_id = v3_cfg_val(cfg, "ID");
+    int ret = 0;
     
     PrintDebug("PCI internal at %p\n",(void *)pci_state);
     
-    struct vm_device * dev = v3_allocate_device(name, &dev_ops, pci_state);
+    struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, pci_state);
     
-    if (v3_attach_device(vm, dev) == -1) {
-       PrintError("Could not attach device %s\n", name);
+    if (dev == NULL) {
+       PrintError("Could not attach device %s\n", dev_id);
+       V3_Free(pci_state);
        return -1;
     }
 
@@ -674,8 +763,14 @@ static int pci_init(struct guest_info * vm, v3_cfg_tree_t * cfg) {
     PrintDebug("Sizeof config header=%d\n", (int)sizeof(struct pci_config_header));
     
     for (i = 0; i < 4; i++) {
-       v3_dev_hook_io(dev, CONFIG_ADDR_PORT + i, &addr_port_read, &addr_port_write);
-       v3_dev_hook_io(dev, CONFIG_DATA_PORT + i, &data_port_read, &data_port_write);
+       ret |= v3_dev_hook_io(dev, CONFIG_ADDR_PORT + i, &addr_port_read, &addr_port_write);
+       ret |= v3_dev_hook_io(dev, CONFIG_DATA_PORT + i, &data_port_read, &data_port_write);
+    }
+    
+    if (ret != 0) {
+       PrintError("Error hooking PCI IO ports\n");
+       v3_remove_device(dev);
+       return -1;
     }
 
     return 0;
@@ -685,7 +780,7 @@ static int pci_init(struct guest_info * vm, v3_cfg_tree_t * cfg) {
 device_register("PCI", pci_init)
 
 
-static inline int init_bars(struct guest_info * info, struct pci_device * pci_dev) {
+static inline int init_bars(struct v3_vm_info * vm, struct pci_device * pci_dev) {
     int i = 0;
 
     for (i = 0; i < 6; i++) {
@@ -706,7 +801,7 @@ static inline int init_bars(struct guest_info * info, struct pci_device * pci_de
            for (j = 0; j < pci_dev->bar[i].num_ports; j++) {
                // hook IO
                if (pci_dev->bar[i].default_base_port != 0xffff) {
-                   if (v3_hook_io_port(info, pci_dev->bar[i].default_base_port + j,
+                   if (v3_hook_io_port(vm, pci_dev->bar[i].default_base_port + j,
                                        pci_dev->bar[i].io_read, pci_dev->bar[i].io_write, 
                                        pci_dev->bar[i].private_data) == -1) {
                        PrintError("Could not hook default io port %x\n", pci_dev->bar[i].default_base_port + j);
@@ -730,7 +825,7 @@ static inline int init_bars(struct guest_info * info, struct pci_device * pci_de
            // hook memory
            if (pci_dev->bar[i].mem_read) {
                // full hook
-               v3_hook_full_mem(info, pci_dev->bar[i].default_base_addr,
+               v3_hook_full_mem(vm, V3_MEM_CORE_ANY, pci_dev->bar[i].default_base_addr,
                                 pci_dev->bar[i].default_base_addr + (pci_dev->bar[i].num_pages * PAGE_SIZE_4KB),
                                 pci_dev->bar[i].mem_read, pci_dev->bar[i].mem_write, pci_dev->priv_data);
            } else if (pci_dev->bar[i].mem_write) {
@@ -773,15 +868,15 @@ static inline int init_bars(struct guest_info * info, struct pci_device * pci_de
 
 
 int v3_pci_set_irq_bridge(struct  vm_device * pci_bus, int bus_num, 
-                         int (*raise_pci_irq)(struct vm_device * dev, struct pci_device * pci_dev),
-                         int (*lower_pci_irq)(struct vm_device * dev, struct pci_device * pci_dev),
-                         struct vm_device * bridge_dev) {
+                         int (*raise_pci_irq)(struct pci_device * pci_dev, void * dev_data),
+                         int (*lower_pci_irq)(struct pci_device * pci_dev, void * dev_data),
+                         void * priv_data) {
     struct pci_internal * pci_state = (struct pci_internal *)pci_bus->private_data;
 
 
     pci_state->bus_list[bus_num].raise_pci_irq = raise_pci_irq;
     pci_state->bus_list[bus_num].lower_pci_irq = lower_pci_irq;
-    pci_state->bus_list[bus_num].irq_bridge_dev = bridge_dev;
+    pci_state->bus_list[bus_num].irq_dev_data = priv_data;
 
     return 0;
 }
@@ -790,14 +885,14 @@ int v3_pci_raise_irq(struct vm_device * pci_bus, int bus_num, struct pci_device
    struct pci_internal * pci_state = (struct pci_internal *)pci_bus->private_data;
    struct pci_bus * bus = &(pci_state->bus_list[bus_num]);
 
-   return bus->raise_pci_irq(bus->irq_bridge_dev, dev);
+   return bus->raise_pci_irq(dev, bus->irq_dev_data);
 }
 
 int v3_pci_lower_irq(struct vm_device * pci_bus, int bus_num, struct pci_device * dev) {
    struct pci_internal * pci_state = (struct pci_internal *)pci_bus->private_data;
    struct pci_bus * bus = &(pci_state->bus_list[bus_num]);
 
-   return bus->lower_pci_irq(bus->irq_bridge_dev, dev);
+   return bus->lower_pci_irq(dev, bus->irq_dev_data);
 }
 
 // if dev_num == -1, auto assign 
@@ -809,8 +904,8 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
                                           const char * name,
                                           struct v3_pci_bar * bars,
                                           int (*config_update)(uint_t reg_num, void * src, uint_t length, void * priv_data),
-                                          int (*cmd_update)(struct pci_device *pci_dev, uchar_t io_enabled, uchar_t mem_enabled),
-                                          int (*ext_rom_update)(struct pci_device * pci_dev),
+                                          int (*cmd_update)(struct pci_device * pci_dev, uchar_t io_enabled, uchar_t mem_enabled),
+                                          int (*exp_rom_update)(struct pci_device * pci_dev, uint32_t * src, void * priv_data),
                                           void * priv_data) {
 
     struct pci_internal * pci_state = (struct pci_internal *)pci->private_data;
@@ -876,7 +971,7 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
     // register update callbacks
     pci_dev->config_update = config_update;
     pci_dev->cmd_update = cmd_update;
-    pci_dev->ext_rom_update = ext_rom_update;
+    pci_dev->exp_rom_update = exp_rom_update;
 
 
     //copy bars
@@ -922,7 +1017,7 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
     // add the device
     add_device_to_bus(bus, pci_dev);
 
-#ifdef CONFIG_DEBUG_PCI
+#ifdef V3_CONFIG_DEBUG_PCI
     pci_dump_state(pci_state);
 #endif
 
@@ -990,7 +1085,7 @@ struct pci_device * v3_pci_register_passthrough_device(struct vm_device * pci,
     // add the device
     add_device_to_bus(bus, pci_dev);
 
-#ifdef CONFIG_DEBUG_PCI
+#ifdef V3_CONFIG_DEBUG_PCI
     pci_dump_state(pci_state);
 #endif