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.


Add dynamic VMM Driven/Guest Driven mode switch in VNET devices
[palacios.git] / palacios / src / devices / generic.c
index 924de7c..b7d1a18 100644 (file)
@@ -37,167 +37,149 @@ typedef enum {GENERIC_IGNORE,
              GENERIC_PRINT_AND_IGNORE} generic_mode_t;
 
 struct generic_internal {
-    struct list_head port_list;
-    uint_t num_port_ranges;
-
-};
-
-
-struct port_range {
-    uint_t start;
-    uint_t end;
-    generic_mode_t mode;
-    struct list_head range_link;
 };
 
 
 
 
-static int generic_reset_device(struct vm_device * dev) {
-    PrintDebug("generic: reset device\n");
-    return 0;
-}
-
-
-
-
-
-static int generic_start_device(struct vm_device * dev) {
-    PrintDebug("generic: start device\n");
-    return 0;
-}
-
-
-static int generic_stop_device(struct vm_device * dev) {
-    PrintDebug("generic: stop device\n");
-    return 0;
-}
-
-
-
-
 static int generic_write_port_passthrough(struct guest_info * core, uint16_t port, void * src, 
-                                         uint_t length, struct vm_device * dev) {
+                                         uint_t length, void * priv_data) {
     uint_t i;
 
-    PrintDebug("generic: writing 0x");
-
-    for (i = 0; i < length; i++) { 
-       PrintDebug("%x", ((uchar_t*)src)[i]);
-    }
-  
-    PrintDebug(" to port 0x%x ... ", port);
-
     switch (length) {
        case 1:
-           v3_outb(port,((uchar_t*)src)[0]);
+           v3_outb(port, ((uint8_t *)src)[0]);
            break;
        case 2:
-           v3_outw(port,((uint16_t*)src)[0]);
+           v3_outw(port, ((uint16_t *)src)[0]);
            break;
        case 4:
-           v3_outdw(port,((uint32_t *)src)[0]);
+           v3_outdw(port, ((uint32_t *)src)[0]);
            break;
        default:
            for (i = 0; i < length; i++) { 
-               v3_outb(port, ((uchar_t *)src)[i]);
+               v3_outb(port, ((uint8_t *)src)[i]);
            }
     }
 
-    PrintDebug(" done\n");
-  
     return length;
 }
 
-static int generic_read_port_passthrough(struct guest_info * core, uint16_t port, void * src, 
-                                        uint_t length, struct vm_device * dev) {
+static int generic_write_port_print_and_passthrough(struct guest_info * core, uint16_t port, void * src, 
+                                                   uint_t length, void * priv_data) {
     uint_t i;
+    int rc;
 
-    PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
+    PrintDebug("generic: writing 0x");
 
+    for (i = 0; i < length; i++) { 
+       PrintDebug("%x", ((uint8_t *)src)[i]);
+    }
+  
+    PrintDebug(" to port 0x%x ... ", port);
+
+    rc=generic_write_port_passthrough(core,port,src,length,priv_data);
+
+    PrintDebug(" done\n");
+  
+    return rc;
+}
+
+static int generic_read_port_passthrough(struct guest_info * core, uint16_t port, void * src, 
+                                        uint_t length, void * priv_data) {
+    uint_t i;
 
     switch (length) {
        case 1:
-           ((uchar_t*)src)[0] = v3_inb(port);
+           ((uint8_t *)src)[0] = v3_inb(port);
            break;
        case 2:
-           ((uint16_t*)src)[0] = v3_inw(port);
+           ((uint16_t *)src)[0] = v3_inw(port);
            break;
        case 4:
-           ((uint_t*)src)[0] = v3_indw(port);
+           ((uint32_t *)src)[0] = v3_indw(port);
            break;
        default:
            for (i = 0; i < length; i++) { 
-               ((uchar_t*)src)[i] = v3_inb(port);
+               ((uint8_t *)src)[i] = v3_inb(port);
            }
     }
 
+    return length;
+}
+
+static int generic_read_port_print_and_passthrough(struct guest_info * core, uint16_t port, void * src, 
+                                                  uint_t length, void * priv_data) {
+    uint_t i;
+    int rc;
+    
+    PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
+
+
+    rc=generic_read_port_passthrough(core,port,src,length,priv_data);
+
     PrintDebug(" done ... read 0x");
 
-    for (i = 0; i < length; i++) { 
-       PrintDebug("%x", ((uchar_t*)src)[i]);
+    for (i = 0; i < rc; i++) { 
+       PrintDebug("%x", ((uint8_t *)src)[i]);
     }
 
     PrintDebug("\n");
 
-    return length;
+    return rc;
 }
 
-static int generic_write_port_ignore(struct guest_info * core, uint16_t port, void * src, 
-                                    uint_t length, struct vm_device * dev) {
-    uint_t i;
 
-    PrintDebug("generic: writing 0x");
+static int generic_read_port_ignore(struct guest_info * core, uint16_t port, void * src, 
+                                   uint_t length, void * priv_data) {
+
+    memset((uint8_t *)src, 0, length);
 
-    for (i = 0; i < length; i++) { 
-       PrintDebug("%x", ((uchar_t*)src)[i]);
-    }
-  
-    PrintDebug(" to port 0x%x ... ignored\n", port);
     return length;
 }
 
-static int generic_read_port_ignore(struct guest_info * core, uint16_t port, void * src, 
-                                   uint_t length, struct vm_device * dev) {
-
+static int generic_read_port_print_and_ignore(struct guest_info * core, uint16_t port, void * src, 
+                                             uint_t length, void * priv_data) {
+   
     PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
 
-    memset((char*)src, 0, length);
+    memset((uint8_t *)src, 0, length);
     PrintDebug(" ignored (return zeroed buffer)\n");
 
     return length;
 }
 
+static int generic_write_port_ignore(struct guest_info * core, uint16_t port, void * src, 
+                                    uint_t length, void * priv_data) {
 
+    return length;
+}
 
+static int generic_write_port_print_and_ignore(struct guest_info * core, uint16_t port, void * src, 
+                                             uint_t length, void * priv_data) {
+    int i;
 
+    PrintDebug("generic: writing 0x%x bytes to port 0x%x ", length, port);
 
-static int generic_free(struct vm_device * dev) {
-    struct generic_internal * state = (struct generic_internal *)(dev->private_data);
-    struct port_range * tmp;
-    struct port_range * cur;
+    memset((uint8_t *)src, 0, length);
+    PrintDebug(" ignored - data was: 0x");
 
-    PrintDebug("generic: deinit_device\n");
+    for (i = 0; i < length; i++) { 
+       PrintDebug("%x", ((uint8_t *)src)[i]);
+    }
+    
+    PrintDebug("\n");
 
-    list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
-       uint_t i;
+    return length;
+}
 
-       PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",
-                  cur->start, cur->end);
-       
-       for (i = cur->start; i <= cur->end; i++) {
-           if (v3_dev_unhook_io(dev, i)) {
-               PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", i);
-           }
-       }
 
-       list_del(&(cur->range_link));
-       state->num_port_ranges--;
-       V3_Free(cur);
-    }
 
-    generic_reset_device(dev);
+
+static int generic_free(struct generic_internal * state) {
+    PrintDebug("generic: deinit_device\n");
+
+    V3_Free(state);
     return 0;
 }
 
@@ -206,44 +188,50 @@ static int generic_free(struct vm_device * dev) {
 
 
 static struct v3_device_ops dev_ops = { 
-    .free = generic_free, 
-    .reset = generic_reset_device,
-    .start = generic_start_device,
-    .stop = generic_stop_device,
+    .free = (int (*)(void *))generic_free, 
 };
 
 
 
 
 static int add_port_range(struct vm_device * dev, uint_t start, uint_t end, generic_mode_t mode) {
-    struct generic_internal * state = (struct generic_internal *)(dev->private_data);
-    struct port_range * range = (struct port_range *)V3_Malloc(sizeof(struct port_range));
     uint_t i = 0;
 
-    range->start = start;
-    range->end = end;
-    range->mode = mode;
-      
     PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n", 
               start, end, 
               (mode == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
     
     for (i = start; i <= end; i++) { 
        if (mode == GENERIC_PRINT_AND_PASSTHROUGH) { 
-           if (v3_dev_hook_io(dev, i, &generic_read_port_passthrough, &generic_write_port_passthrough) == -1) { 
+           if (v3_dev_hook_io(dev, i, 
+                               &generic_read_port_print_and_passthrough, 
+                               &generic_write_port_print_and_passthrough) == -1) { 
                PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
                return -1;
            }
        } else if (mode == GENERIC_PRINT_AND_IGNORE) { 
-           if (v3_dev_hook_io(dev, i, &generic_read_port_ignore, &generic_write_port_ignore) == -1) { 
+           if (v3_dev_hook_io(dev, i, 
+                               &generic_read_port_print_and_ignore, 
+                               &generic_write_port_print_and_ignore) == -1) { 
+               PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
+               return -1;
+           }
+       } else if (mode == GENERIC_PASSTHROUGH) { 
+           if (v3_dev_hook_io(dev, i, 
+                               &generic_read_port_passthrough, 
+                               &generic_write_port_passthrough) == -1) { 
+               PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
+               return -1;
+           }
+       } else if (mode == GENERIC_IGNORE) { 
+           if (v3_dev_hook_io(dev, i, 
+                               &generic_read_port_ignore, 
+                               &generic_write_port_ignore) == -1) { 
                PrintError("generic: can't hook port 0x%x (already hooked?)\n", i);
                return -1;
            }
        } 
     }
-
-    list_add(&(range->range_link), &(state->port_list));
-    state->num_port_ranges++;
     
     return 0;
 }
@@ -253,25 +241,29 @@ static int add_port_range(struct vm_device * dev, uint_t start, uint_t end, gene
 
 
 static int generic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
-    struct generic_internal * state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
+    struct generic_internal * state = NULL;
     char * dev_id = v3_cfg_val(cfg, "ID");
-
     v3_cfg_tree_t * port_cfg = v3_cfg_subtree(cfg, "ports");
 
 
-    INIT_LIST_HEAD(&(state->port_list));
-    state->num_port_ranges = 0;
+    state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
 
+    if (state == NULL) {
+       PrintError("Could not allocate generic state\n");
+       return -1;
+    }
+    
+    memset(state, 0, sizeof(struct generic_internal));
     
-    struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, state);
+    struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state);
 
-    if (v3_attach_device(vm, dev) == -1) {
+    if (dev == NULL) {
        PrintError("Could not attach device %s\n", dev_id);
+       V3_Free(state);
        return -1;
     }
 
     PrintDebug("generic: init_device\n");
-    generic_reset_device(dev);
 
     // scan port list....
     while (port_cfg) {
@@ -284,13 +276,19 @@ static int generic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
            mode = GENERIC_PRINT_AND_IGNORE;
        } else if (strcasecmp(mode_str, "print_and_passthrough") == 0) {
            mode = GENERIC_PRINT_AND_PASSTHROUGH;
+       } else if (strcasecmp(mode_str, "passthrough") == 0) {
+           mode = GENERIC_PASSTHROUGH;
+       } else if (strcasecmp(mode_str, "ignore") == 0) {
+           mode = GENERIC_IGNORE;
        } else {
            PrintError("Invalid Mode %s\n", mode_str);
+           v3_remove_device(dev);
            return -1;
        }
        
        if (add_port_range(dev, start, end, mode) == -1) {
            PrintError("Could not add port range %d-%d\n", start, end);
+           v3_remove_device(dev);
            return -1;
        }