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.


Cleanup and sanity-checking of use of strncpy/strcpy (Coverity static analysis)
[palacios.git] / palacios / src / devices / pci.c
index b5abc15..ab14422 100644 (file)
@@ -170,19 +170,19 @@ static void pci_dump_state(struct pci_internal * pci_state) {
     struct rb_node * node = v3_rb_first(&(pci_state->bus_list[0].devices));
     struct pci_device * tmp_dev = NULL;
     
-    PrintDebug(core->vm_info, core, "===PCI: Dumping state Begin ==========\n");
+    PrintDebug(VM_NONE, VCORE_NONE, "===PCI: Dumping state Begin ==========\n");
     
     do {
        tmp_dev = rb_entry(node, struct pci_device, dev_tree_node);
 
-       PrintDebug(core->vm_info, core, "PCI Device Number: %d (%s):\n", tmp_dev->dev_num,  tmp_dev->name);
-       PrintDebug(core->vm_info, core, "irq = %d\n", tmp_dev->config_header.intr_line);
-       PrintDebug(core->vm_info, core, "Vend ID: 0x%x\n", tmp_dev->config_header.vendor_id);
-       PrintDebug(core->vm_info, core, "Device ID: 0x%x\n", tmp_dev->config_header.device_id);
+       PrintDebug(VM_NONE, VCORE_NONE, "PCI Device Number: %d (%s):\n", tmp_dev->dev_num,  tmp_dev->name);
+       PrintDebug(VM_NONE, VCORE_NONE, "irq = %d\n", tmp_dev->config_header.intr_line);
+       PrintDebug(VM_NONE, VCORE_NONE, "Vend ID: 0x%x\n", tmp_dev->config_header.vendor_id);
+       PrintDebug(VM_NONE, VCORE_NONE, "Device ID: 0x%x\n", tmp_dev->config_header.device_id);
 
     } while ((node = v3_rb_next(node)));
     
-    PrintDebug(core->vm_info, core, "====PCI: Dumping state End==========\n");
+    PrintDebug(VM_NONE, VCORE_NONE, "====PCI: Dumping state End==========\n");
 }
 
 #endif
@@ -195,11 +195,11 @@ static int get_free_dev_num(struct pci_bus * bus) {
     int i, j;
 
     for (i = 0; i < sizeof(bus->dev_map); i++) {
-       PrintDebug(core->vm_info, core, "i=%d\n", i);
+       PrintDebug(VM_NONE, VCORE_NONE, "i=%d\n", i);
        if (bus->dev_map[i] != 0xff) {
            // availability
            for (j = 0; j < 8; j++) {
-               PrintDebug(core->vm_info, core, "\tj=%d\n", j);
+               PrintDebug(VM_NONE, VCORE_NONE, "\tj=%d\n", j);
                if (!(bus->dev_map[i] & (0x1 << j))) {
                    return ((i * 8) + j);
                }
@@ -770,6 +770,7 @@ static int data_port_read(struct guest_info * core, uint16_t port, void * dst, u
 
 
     pci_dev = get_device(&(pci_state->bus_list[0]), pci_state->addr_reg.dev_num, pci_state->addr_reg.fn_num);
+
     
     if (pci_dev == NULL) {
        memset(dst, 0xff, length);
@@ -794,15 +795,21 @@ static int data_port_read(struct guest_info * core, uint16_t port, void * dst, u
 
            if (cfg_hook->read) {
                cfg_hook->read(pci_dev, reg_num + i, cfg_dst, range_len, cfg_hook->private_data);
-           }
+           } else {
+            if (pci_dev->config_read) {
+                if (pci_dev->config_read(pci_dev, reg_num + i, cfg_dst, range_len, pci_dev->priv_data) != 0) {
+                    PrintError(core->vm_info, core, "Error in config_read from PCI device (%s)\n", pci_dev->name);
+                }
+            }
+        }
            
            bytes_left -= range_len;
            i += range_len;
        } else {
            if (pci_dev->config_read) {
-               if (pci_dev->config_read(pci_dev, reg_num + i, cfg_dst, 1, pci_dev->priv_data) != 0) {
-                   PrintError(core->vm_info, core, "Error in config_read from PCI device (%s)\n", pci_dev->name);
-               }
+            if (pci_dev->config_read(pci_dev, reg_num + i, cfg_dst, 1, pci_dev->priv_data) != 0) {
+                PrintError(core->vm_info, core, "Error in config_read from PCI device (%s)\n", pci_dev->name);
+            }
            }
 
            bytes_left--;
@@ -826,8 +833,8 @@ static int bar_update(struct pci_device * pci_dev, uint32_t offset,
     int bar_num = (bar_offset - 0x10) / 4;
     uint32_t new_val = *(uint32_t *)src;
     
-    PrintDebug(VM_NONE, VCORE_NONE, "Updating BAR Register  (Dev=%s) (bar=%d) (old_val=0x%x) (new_val=0x%x)\n", 
-              pci_dev->name, bar_num, bar->val, new_val);
+    PrintDebug(VM_NONE, VCORE_NONE, "Updating BAR Register  (Dev=%s) (bar=%d) (old_val=0x%x) (new_val=0x%x) (length=%d)\n", 
+              pci_dev->name, bar_num, bar->val, new_val, length);
 
     // Cache the changes locally
     memcpy(&(pci_dev->config_space[offset]), src, length);
@@ -845,6 +852,16 @@ static int bar_update(struct pci_device * pci_dev, uint32_t offset,
 
     *(uint32_t *)(pci_dev->config_space + offset) &= bar->mask;
 
+    // Handle buggy code that discards the freaking I/O bit...
+    if (bar->type == PCI_BAR_IO && !(new_val & 0x1) ) {
+       PrintError(VM_NONE,VCORE_NONE,"Buggy guest:  Updating BAR %d of device %s discards the I/O bit...\n", bar_num, pci_dev->name);
+       *(uint32_t *)(pci_dev->config_space + offset) |= 0x1;
+       new_val |= 0x1;
+    }
+
+
+    // V3_Print(VM_NONE, VCORE_NONE,"mask=%x written val=%x\n", bar->mask, *(uint32_t *)(pci_dev->config_space + offset));
+
     switch (bar->type) {
        case PCI_BAR_IO: {
            int i = 0;
@@ -852,11 +869,6 @@ static int bar_update(struct pci_device * pci_dev, uint32_t offset,
            PrintDebug(VM_NONE, VCORE_NONE, "\tRehooking %d IO ports from base 0x%x to 0x%x for %d ports\n",
                       bar->num_ports, PCI_IO_BASE(bar->val), PCI_IO_BASE(new_val),
                       bar->num_ports);
-               
-           // only do this if pci device is enabled....
-           if (!(pci_dev->config_header.status & 0x1)) {
-               PrintError(VM_NONE, VCORE_NONE, "PCI Device IO space not enabled\n");
-           }
 
            for (i = 0; i < bar->num_ports; i++) {
 
@@ -871,7 +883,7 @@ static int bar_update(struct pci_device * pci_dev, uint32_t offset,
 
                    PrintError(VM_NONE, VCORE_NONE, "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(pci_dev->vm);
+                   //v3_print_io_map(pci_dev->vm);
                    return -1;
                }
            }
@@ -950,7 +962,7 @@ static int data_port_write(struct guest_info * core, uint16_t port, void * src,
            
            if (cfg_hook->write) {
                cfg_hook->write(pci_dev, reg_num + i, (void *)(src + i), range_len, cfg_hook->private_data);
-           }
+           } 
 
            length -= range_len;
            i += range_len;
@@ -987,6 +999,10 @@ static int exp_rom_write(struct pci_device * pci_dev, uint32_t offset,
                         void * src, uint_t length, void * private_data) {
     int bar_offset = offset & ~0x03;
 
+    if (pci_dev->config_write) {
+        pci_dev->config_write(pci_dev, offset, src, length, pci_dev->priv_data);
+    }
+
     if (pci_dev->exp_rom_update) {
        pci_dev->exp_rom_update(pci_dev, (void *)(pci_dev->config_space + bar_offset), pci_dev->priv_data);
        
@@ -1002,10 +1018,15 @@ static int exp_rom_write(struct pci_device * pci_dev, uint32_t offset,
 static int cmd_write(struct pci_device * pci_dev, uint32_t offset, 
                     void * src, uint_t length, void * private_data) {
 
+    PrintDebug(VM_NONE, VCORE_NONE, "PCI command update!\n");
+
     int i = 0;
 
     struct pci_cmd_reg old_cmd;
     struct pci_cmd_reg new_cmd;
+    if (pci_dev->config_write) {
+        pci_dev->config_write(pci_dev, offset, src, length, pci_dev->priv_data);
+    }
     old_cmd.val = pci_dev->config_header.command;
 
     for (i = 0; i < length; i++) {
@@ -1018,22 +1039,21 @@ static int cmd_write(struct pci_device * pci_dev, uint32_t offset,
 
     new_cmd.val = pci_dev->config_header.command;
 
-
     if (pci_dev->cmd_update) {
-       if ((new_cmd.intx_disable == 1) && (old_cmd.intx_disable == 0)) {
-           pci_dev->irq_type = IRQ_NONE;
-           pci_dev->cmd_update(pci_dev, PCI_CMD_INTX_DISABLE, 0, pci_dev->priv_data);
-       } else if ((new_cmd.intx_disable == 0) && (old_cmd.intx_disable == 1)) {
-           pci_dev->irq_type = IRQ_INTX;
-           pci_dev->cmd_update(pci_dev, PCI_CMD_INTX_ENABLE, 0, pci_dev->priv_data);
-       }
-
-
-       if ((new_cmd.dma_enable == 1) && (old_cmd.dma_enable == 0)) {
-           pci_dev->cmd_update(pci_dev, PCI_CMD_DMA_ENABLE, 0, pci_dev->priv_data);
-       } else if ((new_cmd.dma_enable == 0) && (old_cmd.dma_enable == 1)) {
-           pci_dev->cmd_update(pci_dev, PCI_CMD_DMA_DISABLE, 0, pci_dev->priv_data);
-       }
+        if ((new_cmd.intx_disable == 1) && (old_cmd.intx_disable == 0)) {
+            pci_dev->irq_type = IRQ_NONE;
+            pci_dev->cmd_update(pci_dev, PCI_CMD_INTX_DISABLE, 0, pci_dev->priv_data);
+        } else if ((new_cmd.intx_disable == 0) && (old_cmd.intx_disable == 1)) {
+            pci_dev->irq_type = IRQ_INTX;
+            pci_dev->cmd_update(pci_dev, PCI_CMD_INTX_ENABLE, 0, pci_dev->priv_data);
+        }
+
+
+        if ((new_cmd.dma_enable == 1) && (old_cmd.dma_enable == 0)) {
+            pci_dev->cmd_update(pci_dev, PCI_CMD_DMA_ENABLE, 0, pci_dev->priv_data);
+        } else if ((new_cmd.dma_enable == 0) && (old_cmd.dma_enable == 1)) {
+            pci_dev->cmd_update(pci_dev, PCI_CMD_DMA_DISABLE, 0, pci_dev->priv_data);
+        }
     }
 
     return 0;
@@ -1348,6 +1368,7 @@ static inline int init_bars(struct v3_vm_info * vm, struct pci_device * pci_dev)
            bar->mask = ~((bar->num_pages << 12) - 1);
            bar->mask |= 0xf; // preserve the configuration flags
 
+
            if (bar->default_base_addr != 0xffffffff) {
                bar->val = bar->default_base_addr & bar->mask;
            } else {
@@ -1389,6 +1410,9 @@ static inline int init_bars(struct v3_vm_info * vm, struct pci_device * pci_dev)
            // Call the bar init function to get the local cached value
            bar->bar_init(i, &(bar->val), bar->private_data);
 
+           // Copy back changes it made
+            *(uint32_t *)(pci_dev->config_space + bar_offset) = bar->val;
+
        } else {
            PrintError(vm, VCORE_NONE, "Invalid BAR type for bar #%d\n", i);
            return -1;
@@ -1570,6 +1594,7 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
            PrintError(VM_NONE, VCORE_NONE, "No more available PCI slots on bus %d\n", bus->bus_num);
            return NULL;
        }
+       V3_Print(VM_NONE, VCORE_NONE,"assigning dev num %d to device (%s, busnum=%d,fnnum=%d)\n", dev_num, name, bus->bus_num, fn_num);
     }
     
     PrintDebug(VM_NONE, VCORE_NONE, "Checking for PCI Device\n");
@@ -1612,6 +1637,7 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci,
     pci_dev->fn_num = fn_num;
 
     strncpy(pci_dev->name, name, sizeof(pci_dev->name));
+    pci_dev->name[sizeof(pci_dev->name)-1] = 0;
     pci_dev->vm = pci->vm;
     pci_dev->priv_data = priv_data;