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.


integrated new configuration system
[palacios.git] / palacios / src / devices / generic.c
index 40b6c00..e4a00ff 100644 (file)
-#include <devices/generic.h>
+/* 
+ * 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) 2008, Peter Dinda <pdinda@northwestern.edu> 
+ * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Peter Dinda <pdinda@northwestern.edu>
+ * Contributor: 2008, 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_types.h>
-#include <geekos/io.h>
-
+#include <palacios/vmm_list.h>
+#include <palacios/vmm_io.h>
+#include <palacios/vmm_dev_mgr.h>
 
-#define GENERIC_DEBUG 1
-
-#if GENERIC_DEBUG
-#define GENERIC_DEBUG_PRINT(first, rest...) PrintDebug(first, ##rest) 
-#else
-#define GENERIC_DEBUG_PRINT(first, rest...)
+#ifndef CONFIG_DEBUG_GENERIC
+#undef PrintDebug
+#define PrintDebug(fmt, args...)
 #endif
 
 
-#define PORT_HOOKS 1
-#define MEM_HOOKS  0   // not yet implmented in device model
-#define IRQ_HOOKS  0   // not yet implemented in device model
+typedef enum {GENERIC_IGNORE, 
+             GENERIC_PASSTHROUGH, 
+             GENERIC_PRINT_AND_PASSTHROUGH, 
+             GENERIC_PRINT_AND_IGNORE} generic_mode_t;
 
 struct generic_internal {
-  generic_port_range_type    *port_ranges;
-  uint_t                     num_port_ranges;
-  generic_address_range_type *address_ranges;
-  uint_t                     num_address_ranges;
-  generic_irq_range_type     *irq_ranges;
-  uint_t                     num_irq_ranges;
-};
+    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;
+};
 
 
 
-int generic_reset_device(struct vm_device * dev)
-{
-  GENERIC_DEBUG_PRINT("generic: reset device\n");
-  return 0;
 
+static int generic_reset_device(struct vm_device * dev) {
+    PrintDebug("generic: reset device\n");
+    return 0;
 }
 
 
 
 
 
-int generic_start_device(struct vm_device *dev)
-{
-  GENERIC_DEBUG_PRINT("generic: start device\n");
-  return 0;
+static int generic_start_device(struct vm_device * dev) {
+    PrintDebug("generic: start device\n");
+    return 0;
 }
 
 
-int generic_stop_device(struct vm_device *dev)
-{
-  GENERIC_DEBUG_PRINT("generic: stop device\n");
-  return 0;
+static int generic_stop_device(struct vm_device * dev) {
+    PrintDebug("generic: stop device\n");
+    return 0;
 }
 
 
 
 
-int generic_write_port(ushort_t port,
-                      void * src, 
-                      uint_t length,
-                      struct vm_device * dev)
-{
-  uint_t i;
+static int generic_write_port_passthrough(uint16_t port, void * src, 
+                                         uint_t length, struct vm_device * dev) {
+    uint_t i;
+
+    PrintDebug("generic: writing 0x");
 
-  GENERIC_DEBUG_PRINT("generic: writing 0x");
-  for (i=0;i<length;i++) { 
-    GENERIC_DEBUG_PRINT("%x",((uchar_t*)src)[i]);
-  }
-  GENERIC_DEBUG_PRINT(" to port 0x%x ... ",port);
-  for (i=0;i<length;i++) { 
-    Out_Byte(port,((uchar_t*)src)[i]);
-  }
-  GENERIC_DEBUG_PRINT(" done\n");
+    for (i = 0; i < length; i++) { 
+       PrintDebug("%x", ((uchar_t*)src)[i]);
+    }
   
-  return length;
-}
+    PrintDebug(" to port 0x%x ... ", port);
+
+    switch (length) {
+       case 1:
+           v3_outb(port,((uchar_t*)src)[0]);
+           break;
+       case 2:
+           v3_outw(port,((uint16_t*)src)[0]);
+           break;
+       case 4:
+           v3_outdw(port,((uint32_t *)src)[0]);
+           break;
+       default:
+           for (i = 0; i < length; i++) { 
+               v3_outb(port, ((uchar_t *)src)[i]);
+           }
+    }
 
-int generic_read_port(ushort_t port,
-                     void * src, 
-                     uint_t length,
-                     struct vm_device * dev)
-{
-  uint_t i;
-
-  GENERIC_DEBUG_PRINT("generic: reading 0x%x bytes from port 0x%x ...",length,port);
-  for (i=0;i<length;i++) { 
-    ((uchar_t*)src)[i] =In_Byte(port);
-  }
-  GENERIC_DEBUG_PRINT(" done ... read 0x");
-  for (i=0;i<length;i++) { 
-    GENERIC_DEBUG_PRINT("%x",((uchar_t*)src)[i]);
-  }
-  GENERIC_DEBUG_PRINT("\n");
-
-  return length;
+    PrintDebug(" done\n");
+  
+    return length;
 }
 
+static int generic_read_port_passthrough(uint16_t port, void * src, 
+                                        uint_t length, struct vm_device * dev) {
+    uint_t i;
+
+    PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
+
+
+    switch (length) {
+       case 1:
+           ((uchar_t*)src)[0] = v3_inb(port);
+           break;
+       case 2:
+           ((uint16_t*)src)[0] = v3_inw(port);
+           break;
+       case 4:
+           ((uint_t*)src)[0] = v3_indw(port);
+           break;
+       default:
+           for (i = 0; i < length; i++) { 
+               ((uchar_t*)src)[i] = v3_inb(port);
+           }
+    }
+
+    PrintDebug(" done ... read 0x");
 
+    for (i = 0; i < length; i++) { 
+       PrintDebug("%x", ((uchar_t*)src)[i]);
+    }
+
+    PrintDebug("\n");
 
-int generic_interrupt(uint_t irq,
-                     struct vm_device * dev) 
-{
-  PrintDebug("generic: interrupt 0x%x - injecting into VM\n",irq);
+    return length;
+}
 
-  dev->vm->vm_ops.raise_irq(dev->vm,irq);
+static int generic_write_port_ignore(uint16_t port, void * src, 
+                                    uint_t length, struct vm_device * dev) {
+    uint_t i;
 
-  return 0;
+    PrintDebug("generic: writing 0x");
 
+    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(uint16_t port, void * src, 
+                                   uint_t length, struct vm_device * dev) {
 
-int generic_init_device(struct vm_device * dev) 
-{
-  struct generic_internal *state = (struct generic_internal *) dev->private_data;
-  uint_t i,j;
+    PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
 
-  GENERIC_DEBUG_PRINT("generic: init_device\n");
+    memset((char*)src, 0, length);
+    PrintDebug(" ignored (return zeroed buffer)\n");
 
-  // Would read state here
+    return length;
+}
 
-  generic_reset_device(dev);
 
-  for (i=0;i<state->num_port_ranges;i++) { 
-    GENERIC_DEBUG_PRINT("generic: hooking ports 0x%x to 0x%x\n",state->port_ranges[i][0],state->port_ranges[i][1]);
-#if PORT_HOOKS
-    for (j=state->port_ranges[i][0]; j<=state->port_ranges[i][1];j++) { 
-      if (dev_hook_io(dev, j, &generic_read_port, &generic_write_port)) { 
-       GENERIC_DEBUG_PRINT("generic: can't hook port 0x%x (already hooked?)\n",j);
-      }
-    }
-#else
-    GENERIC_DEBUG_PRINT("generic: hooking ports not supported\n");
-#endif
 
-  }
 
-  for (i=0;i<state->num_address_ranges;i++) { 
-    GENERIC_DEBUG_PRINT("generic: hooking addresses 0x%x to 0x%x\n",state->address_ranges[i][0],state->address_ranges[i][1]); 
-#if MEM_HOOKS
-    if (dev_hook_mem(dev, state->address_ranges[i][0],state->address_ranges[i][1])) {
-      GENERIC_DEBUG_PRINT("generic: Can't hook addresses 0x%x to 0x%x (already hooked?)\n",
-                 state->address_ranges[i][0],state->address_ranges[i][1]); 
-    }
-#else
-    GENERIC_DEBUG_PRINT("generic: hooking addresses not supported\n");
-#endif
 
-  }
+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;
+
+    PrintDebug("generic: deinit_device\n");
+
+    list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
+       uint_t i;
 
-  for (i=0;i<state->num_irq_ranges;i++) { 
-    GENERIC_DEBUG_PRINT("generic: hooking irqs 0x%x to 0x%x\n",state->irq_ranges[i][0],state->irq_ranges[i][1]);
-#if IRQ_HOOKS
-    for (j=state->irq_ranges[i][0]; j<=state->irq_ranges[i][1];j++) { 
-      if (dev_hook_irq(dev, j,  &generic_interrupt)) { 
-       GENERIC_DEBUG_PRINT("generic: can't hook irq  0x%x (already hooked?)\n",j);
-      }
+       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);
     }
-#else
-    GENERIC_DEBUG_PRINT("generic: hooking irqs not supported\n");
-#endif
-  }
 
-  return 0;
+    generic_reset_device(dev);
+    return 0;
 }
 
-int generic_deinit_device(struct vm_device *dev)
-{
-  struct generic_internal *state = (struct generic_internal *) dev->private_data;
-  uint_t i,j;
 
-  GENERIC_DEBUG_PRINT("generic: deinit_device\n");
 
-  for (i=0;i<state->num_irq_ranges;i++) { 
-    GENERIC_DEBUG_PRINT("generic: unhooking irqs 0x%x to 0x%x\n",state->irq_ranges[i][0],state->irq_ranges[i][1]);
-#if IRQ_HOOKS
-    for (j=state->irq_ranges[i][0]; j<=state->irq_ranges[i][1];j++) { 
-      if (dev_unhook_irq(dev, j)) {
-       GENERIC_DEBUG_PRINT("generic: can't unhook irq 0x%x (already unhooked?)\n",j);
-      }
-    }
-#else
-    GENERIC_DEBUG_PRINT("generic: unhooking irqs not supported\n");
-#endif
 
-  }
 
-  for (i=0;i<state->num_address_ranges;i++) { 
-    GENERIC_DEBUG_PRINT("generic: unhooking addresses 0x%x to 0x%x\n",state->address_ranges[i][0],state->address_ranges[i][1]); 
-#if MEM_HOOKS
-    if (dev_unhook_mem(dev, state->address_ranges[i][0],state->address_ranges[i][1])) {
-      GENERIC_DEBUG_PRINT("generic: Can't unhook addresses 0x%x to 0x%x (already unhooked?)\n",
-                 state->address_ranges[i][0],state->address_ranges[i][1]); 
-    }
-#else
-    GENERIC_DEBUG_PRINT("generic: unhooking addresses not supported\n");
-#endif
-  }
-
-  for (i=0;i<state->num_port_ranges;i++) { 
-    GENERIC_DEBUG_PRINT("generic: unhooking ports 0x%x to 0x%x\n",state->port_ranges[i][0],state->port_ranges[i][1]);
-#if PORT_HOOKS
-    for (j=state->port_ranges[i][0]; j<=state->port_ranges[i][1];j++) { 
-      if (dev_unhook_io(dev, j)) {
-       GENERIC_DEBUG_PRINT("generic: can't unhook port 0x%x (already unhooked?)\n",j);
-      }
-    }
-#else
-    GENERIC_DEBUG_PRINT("generic: unhooking ports not supported\n");
-#endif
+static struct v3_device_ops dev_ops = { 
+    .free = generic_free, 
+    .reset = generic_reset_device,
+    .start = generic_start_device,
+    .stop = generic_stop_device,
+};
 
-  }
 
-  generic_reset_device(dev);
-  return 0;
-}
 
 
+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) { 
+               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) { 
+               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;
+}
 
-static struct vm_device_ops dev_ops = { 
-  .init = generic_init_device, 
-  .deinit = generic_deinit_device,
-  .reset = generic_reset_device,
-  .start = generic_start_device,
-  .stop = generic_stop_device,
-};
 
 
 
 
-struct vm_device *create_generic(generic_port_range_type    port_ranges[], 
-                                uint_t                     num_port_ranges,
-                                generic_address_range_type address_ranges[],
-                                uint_t                     num_address_ranges,
-                                generic_irq_range_type     irq_ranges[],
-                                uint_t                     num_irq_ranges)
-{
-  struct generic_internal * generic_state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
+static int generic_init(struct guest_info * vm, v3_cfg_tree_t * cfg) {
+    struct generic_internal * state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
+    char * name = v3_cfg_val(cfg, "name");
 
+    v3_cfg_tree_t * port_cfg = v3_cfg_subtree(cfg, "ports");
 
-  generic_state->num_port_ranges=num_port_ranges;
-  if (num_port_ranges>0) { 
-    generic_state->port_ranges = V3_Malloc(sizeof(generic_address_range_type)*num_port_ranges);
-    memcpy(generic_state->port_ranges,port_ranges,sizeof(generic_port_range_type)*num_port_ranges);
-  } else {
-    generic_state->port_ranges=NULL;
-  }
 
-  generic_state->num_address_ranges=num_address_ranges;
-  if (num_address_ranges>0) { 
-    generic_state->address_ranges = V3_Malloc(sizeof(generic_address_range_type)*num_address_ranges);
-    memcpy(generic_state->address_ranges,address_ranges,sizeof(generic_address_range_type)*num_address_ranges);
-  } else {
-    generic_state->address_ranges=NULL;
-  }
-  generic_state->num_irq_ranges=num_irq_ranges;
-  if (num_irq_ranges>0) { 
-    generic_state->irq_ranges = V3_Malloc(sizeof(generic_address_range_type)*num_irq_ranges);
-    memcpy(generic_state->irq_ranges,irq_ranges,sizeof(generic_irq_range_type)*num_port_ranges);
-  } else {
-    generic_state->irq_ranges=NULL;
-  }
+    INIT_LIST_HEAD(&(state->port_list));
+    state->num_port_ranges = 0;
 
+    
+    struct vm_device * dev = v3_allocate_device(name, &dev_ops, state);
+
+    if (v3_attach_device(vm, dev) == -1) {
+       PrintError("Could not attach device %s\n", name);
+       return -1;
+    }
+
+    PrintDebug("generic: init_device\n");
+    generic_reset_device(dev);
+
+    // scan port list....
+    while (port_cfg) {
+       uint16_t start = atox(v3_cfg_val(port_cfg, "start"));
+       uint16_t end = atox(v3_cfg_val(port_cfg, "end"));
+       char * mode_str = v3_cfg_val(port_cfg, "mode");
+       generic_mode_t mode = GENERIC_IGNORE;
+
+       if (strcasecmp(mode_str, "print_and_ignore") == 0) {
+           mode = GENERIC_PRINT_AND_IGNORE;
+       } else if (strcasecmp(mode_str, "print_and_passthrough") == 0) {
+           mode = GENERIC_PRINT_AND_PASSTHROUGH;
+       } else {
+           PrintError("Invalid Mode %s\n", mode_str);
+           return -1;
+       }
+       
+       if (add_port_range(dev, start, end, mode) == -1) {
+           PrintError("Could not add port range %d-%d\n", start, end);
+           return -1;
+       }
+
+       port_cfg = v3_cfg_next_branch(port_cfg);
+    }
 
-  struct vm_device *device = create_device("GENERIC", &dev_ops, generic_state);
 
-  return device;
+    return 0;
 }
+
+device_register("GENERIC", generic_init)