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.


NVRAM fixes: corrections in memory setup for tiny (<16MB VMs)
[palacios.git] / palacios / src / devices / nvram.c
index 352782f..cda167e 100644 (file)
@@ -29,7 +29,8 @@
 #include <palacios/vmm_host_events.h>
 #include <palacios/vm_guest.h>
 
-#ifndef CONFIG_DEBUG_NVRAM
+
+#ifndef V3_CONFIG_DEBUG_NVRAM
 #undef PrintDebug
 #define PrintDebug(fmt, args...)
 #endif
@@ -94,61 +95,66 @@ typedef enum {NVRAM_READY, NVRAM_REG_POSTED} nvram_state_t;
 #define NVRAM_REG_BOOTSEQ_NEW_FIRST       0x3D
 #define NVRAM_REG_BOOTSEQ_NEW_SECOND      0x38
 
+#define CHECKSUM_REGION_FIRST_BYTE        0x10
+#define CHECKSUM_REGION_LAST_BYTE         0x2d
+
 
 struct nvram_internal {
     nvram_state_t dev_state;
-    uchar_t       thereg;
-    uchar_t       mem_state[NVRAM_REG_MAX];
-    uchar_t       reg_map[NVRAM_REG_MAX / 8];
+    uint8_t       thereg;
+    uint8_t       mem_state[NVRAM_REG_MAX];
+    uint8_t       reg_map[NVRAM_REG_MAX / 8];
 
     struct vm_device * ide;
 
     struct v3_vm_info * vm;
+    
+    struct v3_timer   *timer;
 
     v3_lock_t nvram_lock;
 
-    uint_t        us;   //microseconds - for clock update - zeroed every second
-    uint_t        pus;  //microseconds - for periodic interrupt - cleared every period
+    uint64_t        us;   //microseconds - for clock update - zeroed every second
+    uint64_t        pus;  //microseconds - for periodic interrupt - cleared every period
 };
 
 
 struct rtc_stata {
-    uint_t        rate: 4;  // clock rate = 65536Hz / 2 rate (0110=1024 Hz)
-    uint_t        basis: 3; // time base, 010 = 32,768 Hz
-    uint_t        uip: 1;   // 1=update in progress
+    uint8_t        rate   : 4;  // clock rate = 65536Hz / 2 rate (0110=1024 Hz)
+    uint8_t        basis  : 3;  // time base, 010 = 32,768 Hz
+    uint8_t        uip    : 1;  // 1=update in progress
 } __attribute__((__packed__)) __attribute__((__aligned__ (1)))  ;
 
 struct rtc_statb {
-    uint_t        sum: 1;  // 1=summer (daylight savings)
-    uint_t        h24: 1;  // 1=24h clock
-    uint_t        dm: 1;   // 1=date/time is in bcd, 0=binary
-    uint_t        rec: 1;  // 1=rectangular signal
-    uint_t        ui: 1;   // 1=update interrupt
-    uint_t        ai: 1;   // 1=alarm interrupt
-    uint_t        pi: 1;   // 1=periodic interrupt
-    uint_t        set: 1;  // 1=blocked update
+    uint8_t        sum    : 1;  // 1=summer (daylight savings)
+    uint8_t        h24    : 1;  // 1=24h clock
+    uint8_t        dm     : 1;  // 0=date/time is in bcd, 1=binary
+    uint8_t        rec    : 1;  // 1=rectangular signal
+    uint8_t        ui     : 1;  // 1=update interrupt
+    uint8_t        ai     : 1;  // 1=alarm interrupt
+    uint8_t        pi     : 1;  // 1=periodic interrupt
+    uint8_t        set    : 1;  // 1=blocked update
 } __attribute__((__packed__))  __attribute__((__aligned__ (1))) ;
 
 struct rtc_statc {
-    uint_t        res: 4;   // reserved
-    uint_t        uf: 1;    // 1=source of interrupt is update
-    uint_t        af: 1;    // 1=source of interrupt is alarm interrupt
-    uint_t        pf: 1;    // 1=source of interrupt is periodic interrupt
-    uint_t        irq: 1;   // 1=interrupt requested
+    uint8_t        res    : 4;  // reserved
+    uint8_t        uf     : 1;  // 1=source of interrupt is update
+    uint8_t        af     : 1;  // 1=source of interrupt is alarm interrupt
+    uint8_t        pf     : 1;  // 1=source of interrupt is periodic interrupt
+    uint8_t        irq    : 1;  // 1=interrupt requested
 }  __attribute__((__packed__))  __attribute__((__aligned__ (1))) ;
 
 struct rtc_statd {
-    uint_t        res: 7;   // reserved
-    uint_t        val: 1;   // 1=cmos ram data is OK
+    uint8_t        res    : 7;  // reserved
+    uint8_t        val    : 1;  // 1=cmos ram data is OK
 }  __attribute__((__packed__))  __attribute__((__aligned__ (1))) ;
 
 
 
 
 struct bcd_num {
-    uchar_t bot : 4;
-    uchar_t top : 4;
-};
+    uint8_t bot : 4;
+    uint8_t top : 4;
+} __attribute__((packed));;
 
 
 
@@ -184,13 +190,13 @@ static int get_memory(struct nvram_internal * nvram, uint8_t reg, uint8_t * val)
 }
 
 
-static uchar_t add_to(uchar_t * left, uchar_t * right, uchar_t bcd) {
-    uchar_t temp;
+static uint8_t add_to(uint8_t * left, uint8_t * right, uint8_t bcd) {
+    uint8_t temp;
 
     if (bcd) { 
        struct bcd_num * bl = (struct bcd_num *)left;
        struct bcd_num * br = (struct bcd_num *)right;
-       uchar_t carry = 0;
+       uint8_t carry = 0;
 
        bl->bot += br->bot;
        carry = bl->bot / 0xa;
@@ -214,7 +220,7 @@ static uchar_t add_to(uchar_t * left, uchar_t * right, uchar_t bcd) {
 }
 
 
-static uchar_t days_in_month(uchar_t month, uchar_t bcd) {
+static uint8_t days_in_month(uint8_t month, uint8_t bcd) {
     // This completely ignores Julian / Gregorian stuff right now
 
     if (bcd) { 
@@ -272,33 +278,31 @@ static uchar_t days_in_month(uchar_t month, uchar_t bcd) {
 }
 
 
-static void update_time(struct nvram_internal * data, uint_t period_us) {
-    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 nvram_internal * data, uint64_t period_us) {
+    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 hour24;
-
-    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));
-
-
-    //PrintDebug("nvram: update_time\n",statb->pi);
+    uint8_t * sec      = (uint8_t *)&(data->mem_state[NVRAM_REG_SEC]);
+    uint8_t * min      = (uint8_t *)&(data->mem_state[NVRAM_REG_MIN]);
+    uint8_t * hour     = (uint8_t *)&(data->mem_state[NVRAM_REG_HOUR]);
+    uint8_t * weekday  = (uint8_t *)&(data->mem_state[NVRAM_REG_WEEK_DAY]);
+    uint8_t * monthday = (uint8_t *)&(data->mem_state[NVRAM_REG_MONTH_DAY]);
+    uint8_t * month    = (uint8_t *)&(data->mem_state[NVRAM_REG_MONTH]);
+    uint8_t * year     = (uint8_t *)&(data->mem_state[NVRAM_REG_YEAR]);
+    uint8_t * cent     = (uint8_t *)&(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
+    uint8_t * cent_ps2 = (uint8_t *)&(data->mem_state[NVRAM_REG_IBM_PS2_CENTURY_BYTE]);
+    uint8_t * seca     = (uint8_t *)&(data->mem_state[NVRAM_REG_SEC_ALARM]);
+    uint8_t * mina     = (uint8_t *)&(data->mem_state[NVRAM_REG_MIN_ALARM]);
+    uint8_t * houra    = (uint8_t *)&(data->mem_state[NVRAM_REG_HOUR_ALARM]);
+    uint8_t hour24;
+
+    uint8_t bcd = (statb->dm == 0);
+    uint8_t carry = 0;
+    uint8_t nextday = 0;
+    uint32_t  periodic_period;
+
+    PrintDebug("nvram: update_time by %llu microseocnds\n",period_us);
   
     // We will set these flags on exit
     statc->irq = 0;
@@ -316,7 +320,7 @@ static void update_time(struct nvram_internal * data, uint_t period_us) {
        carry = add_to(sec, &carry, bcd);
 
        if (carry) { 
-           PrintDebug("nvram: somehow managed to get a carry in second update\n"); 
+           PrintError("nvram: somehow managed to get a carry in second update\n"); 
        }
 
        if ( (bcd && (*sec == 0x60)) || 
@@ -327,7 +331,7 @@ static void update_time(struct nvram_internal * data, uint_t period_us) {
            carry = 1;
            carry = add_to(min, &carry, bcd);
            if (carry) { 
-               PrintDebug("nvram: somehow managed to get a carry in minute update\n"); 
+               PrintError("nvram: somehow managed to get a carry in minute update\n"); 
            }
 
            if ( (bcd && (*min == 0x60)) || 
@@ -340,7 +344,7 @@ static void update_time(struct nvram_internal * data, uint_t period_us) {
 
                    if (hour24 & 0x80) { 
                        hour24 &= 0x8f;
-                       uchar_t temp = ((bcd) ? 0x12 : 12);
+                       uint8_t temp = ((bcd) ? 0x12 : 12);
                        add_to(&hour24, &temp, bcd);
                    }
                }
@@ -348,7 +352,7 @@ static void update_time(struct nvram_internal * data, uint_t period_us) {
                carry = 1;
                carry = add_to(&hour24, &carry, bcd);
                if (carry) { 
-                   PrintDebug("nvram: somehow managed to get a carry in hour update\n"); 
+                   PrintError("nvram: somehow managed to get a carry in hour update\n"); 
                }
 
                if ( (bcd && (hour24 == 0x24)) || 
@@ -414,6 +418,7 @@ static void update_time(struct nvram_internal * data, uint_t period_us) {
                                *year = 0;
                                carry = 1;
                                add_to(cent, &carry, bcd);
+                               *cent_ps2 = *cent;
                            }
                        }
                    }
@@ -448,7 +453,7 @@ static void update_time(struct nvram_internal * data, uint_t period_us) {
 
     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);
+    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) { 
@@ -458,68 +463,65 @@ static void update_time(struct nvram_internal * data, uint_t period_us) {
 }
 
 
-static int handle_timer_event(struct v3_vm_info * vm, 
-                             struct v3_timer_event * evt, 
-                             void * priv_data) {
+static void nvram_update_timer(struct guest_info *vm,
+                              ullong_t           cpu_cycles,
+                              ullong_t           cpu_freq,
+                              void              *priv_data)
+{
+    struct nvram_internal *nvram_state = (struct nvram_internal *)priv_data;
+    uint64_t period_us;
 
+    
+    // cpu freq in khz
+    period_us = (1000*cpu_cycles/cpu_freq);
 
-    struct nvram_internal * data = priv_data;
+    update_time(nvram_state,period_us);
 
-    if (data) {
-       addr_t irq_state = v3_lock_irqsave(data->nvram_lock);
-       update_time(data, evt->period_us);
-       v3_unlock_irqrestore(data->nvram_lock, irq_state);
-    }
-  
-    return 0;
 }
 
 
-
 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
+    uint16_t memk;
+    uint16_t mem_chunks;
 
+    // at most 640K of conventional memory
     if (bytes > 640 * 1024) {
-       set_memory(nvram, NVRAM_REG_BASE_MEMORY_HIGH, 0x02);
-       set_memory(nvram, NVRAM_REG_BASE_MEMORY_LOW, 0x80);
-
-       //      nvram->mem_state[NVRAM_REG_BASE_MEMORY_HIGH] = 0x02;
-       //      nvram->mem_state[NVRAM_REG_BASE_MEMORY_LOW] = 0x80;
+       memk=640;
     } else {
-       uint16_t memk = bytes * 1024;
-       set_memory(nvram, NVRAM_REG_BASE_MEMORY_HIGH, (memk >> 8) & 0x00ff);
-       set_memory(nvram, NVRAM_REG_BASE_MEMORY_LOW, memk & 0x00ff);
-
-       return;
+       memk = bytes/1024;
     }
 
-    if (bytes > (16 * 1024 * 1024)) {
-       // Set extended memory to 15 MB
-       set_memory(nvram, NVRAM_REG_EXT_MEMORY_HIGH, 0x3C);
-       set_memory(nvram, NVRAM_REG_EXT_MEMORY_LOW, 0x00);
-       set_memory(nvram, NVRAM_REG_EXT_MEMORY_2ND_HIGH, 0x3C);
-       set_memory(nvram, NVRAM_REG_EXT_MEMORY_2ND_LOW, 0x00);
+    set_memory(nvram, NVRAM_REG_BASE_MEMORY_HIGH, (memk >> 8) & 0x00ff);
+    set_memory(nvram, NVRAM_REG_BASE_MEMORY_LOW, memk & 0x00ff);
+    
+    // set extended memory - first 1 MB is lost to 640K chunk
+    // extended memory is min(0MB, bytes-1MB)
+    if (bytes < 1024*1024) { 
+       // no extended memory
+       memk = 0;
     } else {
-       uint16_t memk = bytes * 1024;
-
-       set_memory(nvram, NVRAM_REG_EXT_MEMORY_HIGH, (memk >> 8) & 0x00ff);
-       set_memory(nvram, NVRAM_REG_EXT_MEMORY_LOW, memk & 0x00ff);
-       set_memory(nvram, NVRAM_REG_EXT_MEMORY_2ND_HIGH, (memk >> 8) & 0x00ff);
-       set_memory(nvram, NVRAM_REG_EXT_MEMORY_2ND_LOW, memk & 0x00ff);
-
-       return;
+       memk = (bytes - 1024 * 1024 ) / 1024;
     }
 
-    {
-       // Set the extended memory beyond 16 MB in 64k chunks
-       uint16_t mem_chunks = (bytes - (1024 * 1024 * 16)) / (1024 * 64);
-
-       set_memory(nvram, NVRAM_REG_AMI_BIG_MEMORY_HIGH, (mem_chunks >> 8) & 0x00ff);
-       set_memory(nvram, NVRAM_REG_AMI_BIG_MEMORY_LOW, mem_chunks & 0x00ff);
+    set_memory(nvram, NVRAM_REG_EXT_MEMORY_HIGH, (memk >> 8) & 0x00ff);
+    set_memory(nvram, NVRAM_REG_EXT_MEMORY_LOW, memk & 0x00ff);
+    set_memory(nvram, NVRAM_REG_EXT_MEMORY_2ND_HIGH, (memk >> 8) & 0x00ff);
+    set_memory(nvram, NVRAM_REG_EXT_MEMORY_2ND_LOW, memk & 0x00ff);
+    
+    // Set the extended memory beyond 16 MB in 64k chunks
+    // this is min(0, bytes-16MB)
+    if (bytes<(1024*1024*16)) { 
+       mem_chunks=0;
+    } else {
+       mem_chunks = (bytes - (1024 * 1024 * 16)) / (1024 * 64);
     }
 
+    set_memory(nvram, NVRAM_REG_AMI_BIG_MEMORY_HIGH, (mem_chunks >> 8) & 0x00ff);
+    set_memory(nvram, NVRAM_REG_AMI_BIG_MEMORY_LOW, mem_chunks & 0x00ff);
+
     return;
 }
 
@@ -592,7 +594,23 @@ static void init_harddrives(struct nvram_internal * nvram) {
     }
 }
 
+static uint16_t compute_checksum(struct nvram_internal * nvram) {
+    uint16_t checksum = 0;
+    uint8_t reg = 0;
+    uint8_t val = 0;
+    
+    /* add all fields between the RTC and the checksum fields */
+    for (reg = CHECKSUM_REGION_FIRST_BYTE; reg < CHECKSUM_REGION_LAST_BYTE; reg++) {
+        /* unset fields are considered zero so get_memory can be ignored */
+        get_memory(nvram, reg, &val);
+       checksum += val;
+    }
+               
+    return checksum;
+}
+
 static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvram) {
+    uint16_t checksum = 0;
 
     memset(nvram->mem_state, 0, NVRAM_REG_MAX);
     memset(nvram->reg_map, 0, NVRAM_REG_MAX / 8);
@@ -639,8 +657,8 @@ static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvra
     set_memory(nvram, NVRAM_REG_STAT_A, 0x26); 
 
     // RTC status B
-    // 00000100 = not setting, no interrupts, blocked rect signal, bcd mode, 24 hour, normal time
-    set_memory(nvram, NVRAM_REG_STAT_B, 0x06); 
+    // 00000010 = not setting, no interrupts, blocked rect signal, bcd mode (bit 3 = 0), 24 hour, normal time
+    set_memory(nvram, NVRAM_REG_STAT_B, 0x02); 
 
 
     // RTC status C
@@ -665,6 +683,7 @@ static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvra
     set_memory(nvram, NVRAM_REG_WEEK_DAY, 0x1);
     set_memory(nvram, NVRAM_REG_YEAR, 0x08);
     set_memory(nvram, NVRAM_REG_IBM_CENTURY_BYTE, 0x20);
+    set_memory(nvram, NVRAM_REG_IBM_PS2_CENTURY_BYTE, 0x20);
 
     set_memory(nvram, NVRAM_REG_DIAGNOSTIC_STATUS, 0x00);
     
@@ -674,6 +693,13 @@ static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvra
     set_memory_size(nvram, vm->mem_size);
     init_harddrives(nvram);
     
+    /* compute checksum (must follow all assignments here) */
+    checksum = compute_checksum(nvram);
+    set_memory(nvram, NVRAM_REG_CSUM_HIGH, (checksum >> 8) & 0xff);
+    set_memory(nvram, NVRAM_REG_CSUM_LOW, checksum & 0xff);
+
+    
+    
     nvram->dev_state = NVRAM_READY;
     nvram->thereg = 0;
 
@@ -685,18 +711,18 @@ static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvra
 
 
 
-static int nvram_write_reg_port(struct guest_info * core, ushort_t port,
+static int nvram_write_reg_port(struct guest_info * core, uint16_t port,
                                void * src, uint_t length, void * priv_data) {
 
     struct nvram_internal * data = priv_data;
     
     memcpy(&(data->thereg), src, 1);
-    PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
+    PrintDebug("nvram: Writing To NVRAM reg: 0x%x\n", data->thereg);
 
     return 1;
 }
 
-static int nvram_read_data_port(struct guest_info * core, ushort_t port,
+static int nvram_read_data_port(struct guest_info * core, uint16_t port,
                                void * dst, uint_t length, void * priv_data) {
 
     struct nvram_internal * data = priv_data;
@@ -704,14 +730,11 @@ static int nvram_read_data_port(struct guest_info * core, ushort_t port,
     addr_t irq_state = v3_lock_irqsave(data->nvram_lock);
 
     if (get_memory(data, data->thereg, (uint8_t *)dst) == -1) {
-       PrintError("Register %d (0x%x) Not set\n", data->thereg, data->thereg);
-
-       v3_unlock_irqrestore(data->nvram_lock, irq_state);
+       PrintError("nvram: Register %d (0x%x) Not set - POSSIBLE BUG IN MACHINE INIT - CONTINUING\n", data->thereg, data->thereg);
 
-       return -1;
-    }
+    } 
 
-    PrintDebug("nvram_read_data_port(0x%x)  =  0x%x\n", data->thereg, *(uint8_t *)dst);
+    PrintDebug("nvram: nvram_read_data_port(0x%x)  =  0x%x\n", data->thereg, *(uint8_t *)dst);
 
     // hack
     if (data->thereg == NVRAM_REG_STAT_A) { 
@@ -724,7 +747,7 @@ static int nvram_read_data_port(struct guest_info * core, ushort_t port,
 }
 
 
-static int nvram_write_data_port(struct guest_info * core, ushort_t port,
+static int nvram_write_data_port(struct guest_info * core, uint16_t port,
                                 void * src, uint_t length, void * priv_data) {
 
     struct nvram_internal * data = priv_data;
@@ -735,7 +758,7 @@ static int nvram_write_data_port(struct guest_info * core, ushort_t port,
 
     v3_unlock_irqrestore(data->nvram_lock, irq_state);
 
-    PrintDebug("nvram_write_data_port(0x%x) = 0x%x\n", 
+    PrintDebug("nvram: nvram_write_data_port(0x%x) = 0x%x\n", 
               data->thereg, data->mem_state[data->thereg]);
 
     return 1;
@@ -745,8 +768,13 @@ static int nvram_write_data_port(struct guest_info * core, ushort_t port,
 
 
 static int nvram_free(struct nvram_internal * nvram_state) {
-
+    
     // unregister host events
+    struct guest_info *info = &(nvram_state->vm->cores[0]);
+
+    if (nvram_state->timer) { 
+       v3_remove_timer(info,nvram_state->timer);
+    }
 
     V3_Free(nvram_state);
     return 0;
@@ -754,6 +782,9 @@ static int nvram_free(struct nvram_internal * nvram_state) {
 
 
 
+static struct v3_timer_ops timer_ops = {
+    .update_timer = nvram_update_timer,
+};
 
 
 static struct v3_device_ops dev_ops = {  
@@ -771,7 +802,7 @@ static int nvram_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     int ret = 0;
 
     if (!ide) {
-       PrintError("Could not find IDE device\n");
+       PrintError("nvram: Could not find IDE device\n");
        return -1;
     }
 
@@ -786,7 +817,7 @@ static int nvram_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, nvram_state);
 
     if (dev == NULL) {
-       PrintError("Could not attach device %s\n", dev_id);
+       PrintError("nvram: Could not attach device %s\n", dev_id);
        V3_Free(nvram_state);
        return -1;
     }
@@ -798,12 +829,17 @@ static int nvram_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     ret |= v3_dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
   
     if (ret != 0) {
-       PrintError("Error hooking NVRAM IO ports\n");
+       PrintError("nvram: Error hooking NVRAM IO ports\n");
        v3_remove_device(dev);
        return -1;
     }
 
-    v3_hook_host_event(vm, HOST_TIMER_EVT, V3_HOST_EVENT_HANDLER(handle_timer_event), nvram_state);
+    nvram_state->timer = v3_add_timer(&(vm->cores[0]),&timer_ops,nvram_state);
+
+    if (nvram_state->timer == NULL ) { 
+       v3_remove_device(dev);
+       return -1;
+    }
 
     return 0;
 }