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.


Kevin Pedretti: memory calculation fix
[palacios.git] / palacios / src / devices / nvram.c
index 8df8b79..4e9527b 100644 (file)
@@ -1,3 +1,23 @@
+/* 
+ * 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>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
+
 #include <devices/nvram.h>
 #include <palacios/vmm.h>
 #include <palacios/vmm_types.h>
@@ -59,7 +79,6 @@ typedef enum {NVRAM_READY, NVRAM_REG_POSTED} nvram_state_t;
 #define NVRAM_REG_AMI_BIG_MEMORY_HIGH     0x35
 #define NVRAM_REG_AMI_BIG_MEMORY_LOW      0x34
 
-
 #define NVRAM_REG_CSUM_HIGH               0x2e
 #define NVRAM_REG_CSUM_LOW                0x2f
 #define NVRAM_REG_IBM_CENTURY_BYTE        0x32  
@@ -112,126 +131,123 @@ struct rtc_statd {
 
 
 
-struct vm_device *thedev=NULL;
-
+struct bcd_num {
+  uchar_t bot : 4;
+  uchar_t top : 4;
+};
 
-static struct vm_device *demultiplex_timer_interrupt(uint_t period_us)
-{
-  // hack
-  return thedev;
-}
 
-struct bcd_num {
-  uchar_t bot:4;
-  uchar_t top:4;
-} ;
 
-static uchar_t add_to(uchar_t *left, uchar_t *right, uchar_t bcd)
-{
+static uchar_t add_to(uchar_t * left, uchar_t * right, uchar_t bcd) {
   uchar_t temp;
 
   if (bcd) { 
-    struct bcd_num *bl = (struct bcd_num *)left;
-    struct bcd_num *br = (struct bcd_num *)right;
-    uchar_t carry=0;
+    struct bcd_num * bl = (struct bcd_num *)left;
+    struct bcd_num * br = (struct bcd_num *)right;
+    uchar_t carry = 0;
 
     bl->bot += br->bot;
     carry = bl->bot / 0xa;
     bl->bot %= 0xa;
+
     bl->top += carry + br->top;
     carry = bl->top / 0xa;
     bl->top %= 0xa;
+
     return carry;
   } else {
-    temp=*left;
-    *left+=*right;
-    if (*left<temp) { 
+    temp = *left;
+    *left += *right;
+
+    if (*left < temp) { 
       return 1;
     } else {
       return 0;
     }
   }
-  
 }
 
 
-static uchar_t days_in_month(struct vm_device *dev, uchar_t month, uchar_t bcd)
-{
+static uchar_t days_in_month(struct vm_device * dev, uchar_t month, uchar_t bcd) {
   // This completely ignores Julian / Gregorian stuff right now
 
   if (bcd) { 
-    switch (month) {
-    case 0x1: //jan
-    case 0x3: //march
-    case 0x5: //may
-    case 0x7: //july
-    case 0x8: //aug
-    case 0x10: //oct
-    case 0x12: //dec
-      return 0x31;
-      break;
-    case 0x4: //april
-    case 0x6: //june
-    case 0x9: //sept
-    case 0x11: //nov
-      return 0x30;
-      break;
-    case 0x2: //feb
-      return 0x28;
-      break;
-    default:
-      return 0x30;
-    }
+
+    switch (month) 
+      {
+      case 0x1: //jan
+      case 0x3: //march
+      case 0x5: //may
+      case 0x7: //july
+      case 0x8: //aug
+      case 0x10: //oct
+      case 0x12: //dec
+       return 0x31;
+       break;
+      case 0x4: //april
+      case 0x6: //june
+      case 0x9: //sept
+      case 0x11: //nov
+       return 0x30;
+       break;
+      case 0x2: //feb
+       return 0x28;
+       break;
+      default:
+       return 0x30;
+      }
+    
   } else {
-    switch (month) {
-    case 1: //jan
-    case 3: //march
-    case 5: //may
-    case 7: //july
-    case 8: //aug
-    case 10: //oct
-    case 12: //dec
-      return 31;
-      break;
-    case 4: //april
-    case 6: //june
-    case 9: //sept
-    case 11: //nov
-      return 30;
-      break;
-    case 2: //feb
-      return 28;
-      break;
-    default:
-      return 30;
-    }
+
+    switch (month) 
+      {
+      case 1: //jan
+      case 3: //march
+      case 5: //may
+      case 7: //july
+      case 8: //aug
+      case 10: //oct
+      case 12: //dec
+       return 31;
+       break;
+      case 4: //april
+      case 6: //june
+      case 9: //sept
+      case 11: //nov
+       return 30;
+       break;
+      case 2: //feb
+       return 28;
+       break;
+      default:
+       return 30;
+      }
   }
 }
 
 
-static void update_time(struct vm_device *dev, uint_t period_us)
-{
-  struct nvram_internal *data = (struct nvram_internal *) (dev->private_data);
-  struct rtc_stata *stata = (struct rtc_stata *) &((data->mem_state[NVRAM_REG_STAT_A]));
-  struct rtc_statb *statb = (struct rtc_statb *) &((data->mem_state[NVRAM_REG_STAT_B]));
-  struct rtc_statc *statc = (struct rtc_statc *) &((data->mem_state[NVRAM_REG_STAT_C]));
+static void update_time(struct vm_device * dev, uint_t period_us) {
+  struct nvram_internal * data = (struct nvram_internal *) (dev->private_data);
+  struct rtc_stata * stata = (struct rtc_stata *) &((data->mem_state[NVRAM_REG_STAT_A]));
+  struct rtc_statb * statb = (struct rtc_statb *) &((data->mem_state[NVRAM_REG_STAT_B]));
+  struct rtc_statc * statc = (struct rtc_statc *) &((data->mem_state[NVRAM_REG_STAT_C]));
   //struct rtc_statd *statd = (struct rtc_statd *) &((data->mem_state[NVRAM_REG_STAT_D]));
-  uchar_t *sec = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC]);
-  uchar_t *min = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN]);
-  uchar_t *hour = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR]);
-  uchar_t *weekday = (uchar_t *) &(data->mem_state[NVRAM_REG_WEEK_DAY]);
-  uchar_t *monthday = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH_DAY]);
-  uchar_t *month = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH]);
-  uchar_t *year = (uchar_t *) &(data->mem_state[NVRAM_REG_YEAR]);
-  uchar_t *cent = (uchar_t *) &(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
-  uchar_t *seca = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC_ALARM]);
-  uchar_t *mina = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN_ALARM]);
-  uchar_t *houra = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR_ALARM]);
+  uchar_t * sec = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC]);
+  uchar_t * min = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN]);
+  uchar_t * hour = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR]);
+  uchar_t * weekday = (uchar_t *) &(data->mem_state[NVRAM_REG_WEEK_DAY]);
+  uchar_t * monthday = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH_DAY]);
+  uchar_t * month = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH]);
+  uchar_t * year = (uchar_t *) &(data->mem_state[NVRAM_REG_YEAR]);
+  uchar_t * cent = (uchar_t *) &(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
+  uchar_t * seca = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC_ALARM]);
+  uchar_t * mina = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN_ALARM]);
+  uchar_t * houra = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR_ALARM]);
   uchar_t hour24;
 
-  uchar_t bcd = (statb->dm==1);
-  uchar_t carry=0;
-  uchar_t nextday=0;
+  uchar_t bcd = (statb->dm == 1);
+  uchar_t carry = 0;
+  uchar_t nextday = 0;
   uint_t  periodic_period;
 
   //PrintDebug("nvram: sizeof(struct rtc_stata)=%d\n", sizeof(struct rtc_stata));
@@ -240,7 +256,10 @@ static void update_time(struct vm_device *dev, uint_t period_us)
   //PrintDebug("nvram: update_time\n",statb->pi);
   
   // We will set these flags on exit
-  statc->irq=statc->pf=statc->af=statc->uf=0;
+  statc->irq = 0;
+  statc->pf = 0;
+  statc->af = 0;
+  statc->uf = 0;
 
   // We will reset us after one second
   data->us += period_us;
@@ -248,142 +267,228 @@ static void update_time(struct vm_device *dev, uint_t period_us)
   data->pus += period_us;
 
   if (data->us > 1000000) { 
-    carry=1;
-    //PrintDebug("nvram: adding 1 to seconds=0x%x (bcd=%d)...", *sec, bcd);
-    carry=add_to(sec,&carry,bcd);
-    //PrintDebug("got 0x%x with carry=%d\n",*sec,carry);
-    if (carry) { PrintDebug("nvram: somehow managed to get a carry in second update\n"); }
-    if ((bcd && *sec==0x60) || (!bcd && *sec==60)) { 
-      *sec=0;
-      carry=1;
-      carry=add_to(min,&carry,bcd);
-      if (carry) { PrintDebug("nvram: somehow managed to get a carry in minute update\n"); }
-      if ((bcd && *min==0x60) || (!bcd && *min==60)) { 
-       *min=0;
+    carry = 1;
+    carry = add_to(sec, &carry, bcd);
+
+    if (carry) { 
+      PrintDebug("nvram: somehow managed to get a carry in second update\n"); 
+    }
+
+    if ( (bcd && (*sec == 0x60)) || 
+        ((!bcd) && (*sec == 60))) { 
+  
+      *sec = 0;
+
+      carry = 1;
+      carry = add_to(min, &carry, bcd);
+      if (carry) { 
+       PrintDebug("nvram: somehow managed to get a carry in minute update\n"); 
+      }
+
+      if ( (bcd && (*min == 0x60)) || 
+          ((!bcd) && (*min == 60))) { 
+
+       *min = 0;
        hour24 = *hour;
+
        if (!(statb->h24)) { 
-         if (hour24&0x80) { 
-           hour24&=0x8f;
-           uchar_t temp = bcd ? 0x12 : 12;
-           add_to(&hour24,&temp, bcd);
+
+         if (hour24 & 0x80) { 
+           hour24 &= 0x8f;
+           uchar_t temp = ((bcd) ? 0x12 : 12);
+           add_to(&hour24, &temp, bcd);
          }
        }
-       carry=1;
-       carry=add_to(&hour24,&carry,bcd);
-       if (carry) { PrintDebug("nvram: somehow managed to get a carry in hour update\n"); }
-       if ((bcd && hour24==0x24) || (!bcd && hour24==24)) { 
-         carry=1;
-         nextday=1;
-         hour24=0;
+
+       carry = 1;
+       carry = add_to(&hour24, &carry, bcd);
+       if (carry) { 
+         PrintDebug("nvram: somehow managed to get a carry in hour update\n"); 
+       }
+
+       if ( (bcd && (hour24 == 0x24)) || 
+            ((!bcd) && (hour24 == 24))) { 
+         carry = 1;
+         nextday = 1;
+         hour24 = 0;
        } else {
-         carry=0;
+         carry = 0;
        }
+
+
        if (statb->h24) { 
-         *hour=hour24;
+         *hour = hour24;
        } else {
-         if ((bcd && hour24<0x12) || (!bcd && hour24<12)) { 
-           *hour=hour24;
+         if ( (bcd && (hour24 < 0x12)) || 
+              ((!bcd) && (hour24 < 12))) { 
+           *hour = hour24;
+
          } else {
+
            if (!bcd) { 
-             *hour = (hour24-12) | 0x80;
+             *hour = (hour24 - 12) | 0x80;
            } else {
              *hour = hour24;
-             struct bcd_num *n = (struct bcd_num *) hour;
-             if (n->bot<0x2) { 
+             struct bcd_num * n = (struct bcd_num *)hour;
+
+             if (n->bot < 0x2) { 
                n->top--;
-               n->bot+=0xa;
+               n->bot += 0xa;
              }
-             n->bot-=0x2;
-             n->top-=0x1;
+
+             n->bot -= 0x2;
+             n->top -= 0x1;
            }
          }
        }
+
        // now see if we need to carry into the days and further
        if (nextday) { 
-         carry=1;
+         carry = 1;
          add_to(weekday, &carry, bcd);
-         *weekday%=0x7;  // same regardless of bcd
-         if (!(*monthday==days_in_month(dev,*month,bcd))) {
+
+         *weekday %= 0x7;  // same regardless of bcd
+
+         if ((*monthday) != days_in_month(dev, *month, bcd)) {
            add_to(monthday, &carry, bcd);
          } else {
-           *monthday=0x1;
-           carry=1;
-           add_to(month,&carry,bcd);
-           if ((bcd && *month==0x13) || (!bcd && *month==13)) { 
-             *month=1; // same for both 
-             carry=1;
-             carry=add_to(year,&carry,bcd);
-             if ((bcd && carry) || (!bcd && *year==100)) { 
-               *year=0;
-               carry=1;
-               add_to(cent,&carry,bcd);
+           *monthday = 0x1;
+
+           carry = 1;
+           add_to(month, &carry, bcd);
+
+           if ( (bcd && (*month == 0x13)) || 
+                ((!bcd) && (*month == 13))) { 
+             *month = 1; // same for both 
+
+             carry = 1;
+             carry = add_to(year, &carry, bcd);
+
+             if ( (bcd && carry) || 
+                  ((!bcd) && (*year == 100))) { 
+               *year = 0;
+               carry = 1;
+               add_to(cent, &carry, bcd);
              }
            }
          }
        }
       }
     }
-    data->us-=1000000;
+
+
+    data->us -= 1000000;
     // OK, now check for the alarm, if it is set to interrupt
     if (statb->ai) { 
-      if (*sec==*seca && *min==*mina && *hour==*houra) { 
-       statc->af=1;
+      if ((*sec == *seca) && (*min == *mina) && (*hour == *houra)) { 
+       statc->af = 1;
        PrintDebug("nvram: interrupt on alarm\n");
       }
     }
   }
 
   if (statb->pi) { 
-    periodic_period = 1000000/(65536/(0x1 << stata->rate));
+    periodic_period = 1000000 / (65536 / (0x1 << stata->rate));
     if (data->pus >= periodic_period) { 
-      statc->pf=1;
+      statc->pf = 1;
       data->pus -= periodic_period;
       PrintDebug("nvram: interrupt on periodic\n");
     }
   }
 
   if (statb->ui) { 
-    statc->uf=1;
+    statc->uf = 1;
     PrintDebug("nvram: interrupt on update\n");
   }
 
-  statc->irq= statc->pf || statc->af || statc->uf;
+  statc->irq = (statc->pf || statc->af || statc->uf);
   
   //PrintDebug("nvram: time is now: YMDHMS: 0x%x:0x%x:0x%x:0x%x:0x%x,0x%x bcd=%d\n", *year, *month, *monthday, *hour, *min, *sec,bcd);
   
   // Interrupt associated VM, if needed
   if (statc->irq) { 
     PrintDebug("nvram: injecting interrupt\n");
-    dev->vm->vm_ops.raise_irq(dev->vm, NVRAM_RTC_IRQ);
+    v3_raise_irq(dev->vm, NVRAM_RTC_IRQ);
   }
 }
 
 
+static int handle_timer_event(struct guest_info * info, 
+                       struct v3_timer_event * evt, 
+                       void * priv_data) {
 
-
-void deliver_timer_interrupt_to_vmm(uint_t period_us)
-{
-  struct vm_device *dev = demultiplex_timer_interrupt(period_us);
+  struct vm_device * dev = (struct vm_device *)priv_data;
 
   if (dev) {
-    update_time(dev,period_us);
+    update_time(dev, evt->period_us);
   }
   
+  return 0;
 }
 
 
-static int set_nvram_defaults(struct vm_device *dev)
-{
-  struct nvram_internal * nvram_state = (struct nvram_internal*) dev->private_data;
 
+static void set_memory_size(struct nvram_internal * nvram, addr_t bytes) {
+  // 1. Conventional Mem: 0-640k in K
+  // 2. Extended Mem: 0-16MB in K
+  // 3. Big Mem: 0-4G in 64K
+
+  if (bytes > 640 * 1024) {
+    nvram->mem_state[NVRAM_REG_BASE_MEMORY_HIGH] = 0x02;
+    nvram->mem_state[NVRAM_REG_BASE_MEMORY_LOW] = 0x80;
+  } else {
+    uint16_t memk = bytes * 1024;
+    nvram->mem_state[NVRAM_REG_BASE_MEMORY_HIGH] = (memk >> 8) & 0x00ff;
+    nvram->mem_state[NVRAM_REG_BASE_MEMORY_LOW] = memk & 0x00ff;
+
+    return;
+  }
+
+  if (bytes > (16 * 1024 * 1024)) {
+    // Set extended memory to 15 MB
+    nvram->mem_state[NVRAM_REG_EXT_MEMORY_HIGH] = 0x3C;
+    nvram->mem_state[NVRAM_REG_EXT_MEMORY_LOW] = 0x00;
+    nvram->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= 0x3C;
+    nvram->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= 0x00;
+  } else {
+    uint16_t memk = bytes * 1024;
+    nvram->mem_state[NVRAM_REG_EXT_MEMORY_HIGH] = (memk >> 8) & 0x00ff;
+    nvram->mem_state[NVRAM_REG_EXT_MEMORY_LOW] = memk & 0x00ff;
+    nvram->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= (memk >> 8) & 0x00ff;
+    nvram->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= memk & 0x00ff;
 
+    return;
+  }
+
+  {
+    // Set the extended memory beyond 16 MB in 64k chunks
+    uint16_t mem_chunks = (bytes - (1024 * 1024 * 16)) / (1024 * 64);
+    nvram->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH] = (mem_chunks >> 8) & 0x00ff;
+    nvram->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW] = mem_chunks & 0x00ff;
+  }
+
+  return;
+}
+
+static int init_nvram_state(struct vm_device * dev) {
+  struct guest_info * info = dev->vm;
+  struct nvram_internal * nvram_state = (struct nvram_internal *)dev->private_data;
+  
+  /* TODO:
+   * The amount of ram in the system is stored in info->mem_size
+   * We need to reflect that value correctly here
+   */
+  PrintError("TODO: Set the nvram memory register to reflect info->mem_size (%p)\n", (void *)(info->mem_size));
+  
+
+  memset(nvram_state->mem_state, 0, NVRAM_REG_MAX);
 
 
   //
   // 2 1.44 MB floppy drives
   //
 #if 1
-  nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE]= 0x44;
+  nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x44;
 #else
   nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x00;
 #endif
@@ -391,41 +496,24 @@ static int set_nvram_defaults(struct vm_device *dev)
   //
   // For old boot sequence style, do floppy first
   //
-  nvram_state->mem_state[NVRAM_REG_BOOTSEQ_OLD]= 0x10;
+  nvram_state->mem_state[NVRAM_REG_BOOTSEQ_OLD] = 0x10;
 
 #if 0
   // For new boot sequence style, do floppy, cd, then hd
-  nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST]= 0x31;
-  nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND]= 0x20;
+  nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x31;
+  nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x20;
 #endif
 
   // For new boot sequence style, do cd, hd, floppy
-  nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST]= 0x23;
-  nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND]= 0x10;
-
+  nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x23;
+  nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x10;
+  
+  
   // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
-  nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE]= 0x4f;
-  //nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0xf;
-
-  // Set conventional memory to 640K
-  nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_HIGH]= 0x02;
-  nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_LOW]= 0x80;
-
-  // Set extended memory to 15 MB
-  nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_HIGH]= 0x3C;
-  nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_LOW]= 0x00;
-  nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= 0x3C;
-  nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= 0x00;
-
-  // Set the extended memory beyond 16 MB to 128-16 MB
-  nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH] = 0x7;
-  nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW] = 0x00;
-
-  //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH]= 0x00;
-  //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW]= 0x00;
-
+  nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0x4f;
+  // nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0xf;
   
+
   // This is the harddisk type.... Set accordingly...
   nvram_state->mem_state[NVRAM_IBM_HD_DATA] = 0x20;
 
@@ -458,42 +546,36 @@ static int set_nvram_defaults(struct vm_device *dev)
   nvram_state->mem_state[NVRAM_REG_WEEK_DAY] = 0x1;
   nvram_state->mem_state[NVRAM_REG_YEAR] = 0x08;
 
-  nvram_state->us=nvram_state->pus=0;
+  nvram_state->us = 0;
+  nvram_state->pus = 0;
 
-  return 0;
+  set_memory_size(nvram_state, dev->vm->mem_size);
 
+  nvram_state->dev_state = NVRAM_READY;
+  nvram_state->thereg = 0;
+
+  return 0;
 }
 
 
-int nvram_reset_device(struct vm_device * dev)
-{
-  struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
-  
-  PrintDebug("nvram: reset device\n");
 
 
-  data->dev_state = NVRAM_READY;
-  data->thereg=0;
+static int nvram_reset_device(struct vm_device * dev) {
 
-  
   return 0;
-
 }
 
 
 
 
 
-int nvram_start_device(struct vm_device *dev)
-{
+static int nvram_start_device(struct vm_device * dev) {
   PrintDebug("nvram: start device\n");
   return 0;
 }
 
 
-int nvram_stop_device(struct vm_device *dev)
-{
+static int nvram_stop_device(struct vm_device * dev) {
   PrintDebug("nvram: stop device\n");
   return 0;
 }
@@ -501,12 +583,11 @@ int nvram_stop_device(struct vm_device *dev)
 
 
 
-int nvram_write_reg_port(ushort_t port,
-                       void * src, 
-                       uint_t length,
-                       struct vm_device * dev)
-{
-  struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
+static int nvram_write_reg_port(ushort_t port,
+                               void * src, 
+                               uint_t length,
+                               struct vm_device * dev) {
+  struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
 
   memcpy(&(data->thereg), src, 1);
   PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
@@ -515,21 +596,18 @@ int nvram_write_reg_port(ushort_t port,
   return 1;
 }
 
-int nvram_read_data_port(ushort_t port,
-                      void   * dst, 
-                      uint_t length,
-                      struct vm_device * dev)
-{
-  struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
-
-
+static int nvram_read_data_port(ushort_t port,
+                               void * dst, 
+                               uint_t length,
+                               struct vm_device * dev) {
+  struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
 
   memcpy(dst, &(data->mem_state[data->thereg]), 1);
 
   PrintDebug("nvram_read_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
 
   // hack
-  if (data->thereg==NVRAM_REG_STAT_A) { 
+  if (data->thereg == NVRAM_REG_STAT_A) { 
     data->mem_state[data->thereg] ^= 0x80;  // toggle Update in progess
   }
 
@@ -537,12 +615,11 @@ int nvram_read_data_port(ushort_t port,
   return 1;
 }
 
-int nvram_write_data_port(ushort_t port,
-                       void * src, 
-                       uint_t length,
-                       struct vm_device * dev)
-{
-  struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
+static int nvram_write_data_port(ushort_t port,
+                                void * src, 
+                                uint_t length,
+                                struct vm_device * dev) {
+  struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
 
   memcpy(&(data->mem_state[data->thereg]), src, 1);
 
@@ -553,32 +630,23 @@ int nvram_write_data_port(ushort_t port,
 
 
 
-int nvram_init_device(struct vm_device * dev) {
-  struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
-
+static int nvram_init_device(struct vm_device * dev) {
   PrintDebug("nvram: init_device\n");
 
-  memset(data->mem_state, 0, NVRAM_REG_MAX);
-
-  // Would read state here
-  set_nvram_defaults(dev);
-
-  nvram_reset_device(dev);
+  init_nvram_state(dev);
 
   // hook ports
-  dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
-  dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
+  v3_dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
+  v3_dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
   
+  v3_hook_host_event(dev->vm, HOST_TIMER_EVT, V3_HOST_EVENT_HANDLER(handle_timer_event), dev);
+
   return 0;
 }
 
-int nvram_deinit_device(struct vm_device *dev)
-{
-
-
-  dev_unhook_io(dev, NVRAM_REG_PORT);
-  dev_unhook_io(dev, NVRAM_DATA_PORT);
+static int nvram_deinit_device(struct vm_device * dev) {
+  v3_dev_unhook_io(dev, NVRAM_REG_PORT);
+  v3_dev_unhook_io(dev, NVRAM_DATA_PORT);
 
   nvram_reset_device(dev);
   return 0;
@@ -599,18 +667,14 @@ static struct vm_device_ops dev_ops = {
 
 
 
-struct vm_device *create_nvram() {
-  struct nvram_internal * nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal)+1000);
+struct vm_device * v3_create_nvram() {
+  struct nvram_internal * nvram_state = NULL;
 
-  PrintDebug("nvram: internal at %x\n",nvram_state);
+  nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal) + 1000);
 
-  struct vm_device *device = create_device("NVRAM", &dev_ops, nvram_state);
-
-  if (thedev!=NULL) {
-    PrintDebug("nvram: warning! overwriting thedev\n");
-  }
+  PrintDebug("nvram: internal at %p\n", (void *)nvram_state);
 
-  thedev=device;
+  struct vm_device * device = v3_create_device("NVRAM", &dev_ops, nvram_state);
 
   return device;
 }