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.


HVM configuration logic (create mptables, acpi tables, etc, based on ROS config)
[palacios.git] / palacios / src / devices / nvram.c
index f2c45a3..f15795f 100644 (file)
@@ -30,7 +30,7 @@
 #include <palacios/vm_guest.h>
 
 
-#ifndef CONFIG_DEBUG_NVRAM
+#ifndef V3_CONFIG_DEBUG_NVRAM
 #undef PrintDebug
 #define PrintDebug(fmt, args...)
 #endif
@@ -98,6 +98,13 @@ typedef enum {NVRAM_READY, NVRAM_REG_POSTED} nvram_state_t;
 #define CHECKSUM_REGION_FIRST_BYTE        0x10
 #define CHECKSUM_REGION_LAST_BYTE         0x2d
 
+// Following fields are used by SEABIOS
+#define NVRAM_REG_HIGHMEM_LOW             0x5b
+#define NVRAM_REG_HIGHMEM_MID             0x5c
+#define NVRAM_REG_HIGHMEM_HIGH            0x5d
+#define NVRAM_REG_SMPCPUS                 0x5f
+
+#define DEFAULT_BOOTSEQ                   "cd,hd"
 
 struct nvram_internal {
     nvram_state_t dev_state;
@@ -127,7 +134,7 @@ struct rtc_stata {
 struct rtc_statb {
     uint8_t        sum    : 1;  // 1=summer (daylight savings)
     uint8_t        h24    : 1;  // 1=24h clock
-    uint8_t        dm     : 1;  // 1=date/time is in bcd, 0=binary
+    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
@@ -297,12 +304,12 @@ static void update_time(struct nvram_internal * data, uint64_t period_us) {
     uint8_t * houra    = (uint8_t *)&(data->mem_state[NVRAM_REG_HOUR_ALARM]);
     uint8_t hour24;
 
-    uint8_t bcd = (statb->dm == 1);
+    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);
+    PrintDebug(VM_NONE, VCORE_NONE, "nvram: update_time by %llu microseocnds\n",period_us);
   
     // We will set these flags on exit
     statc->irq = 0;
@@ -320,7 +327,7 @@ static void update_time(struct nvram_internal * data, uint64_t period_us) {
        carry = add_to(sec, &carry, bcd);
 
        if (carry) { 
-           PrintError("nvram: somehow managed to get a carry in second update\n"); 
+           PrintError(VM_NONE, VCORE_NONE, "nvram: somehow managed to get a carry in second update\n"); 
        }
 
        if ( (bcd && (*sec == 0x60)) || 
@@ -331,7 +338,7 @@ static void update_time(struct nvram_internal * data, uint64_t period_us) {
            carry = 1;
            carry = add_to(min, &carry, bcd);
            if (carry) { 
-               PrintError("nvram: somehow managed to get a carry in minute update\n"); 
+               PrintError(VM_NONE, VCORE_NONE, "nvram: somehow managed to get a carry in minute update\n"); 
            }
 
            if ( (bcd && (*min == 0x60)) || 
@@ -352,7 +359,7 @@ static void update_time(struct nvram_internal * data, uint64_t period_us) {
                carry = 1;
                carry = add_to(&hour24, &carry, bcd);
                if (carry) { 
-                   PrintError("nvram: somehow managed to get a carry in hour update\n"); 
+                   PrintError(VM_NONE, VCORE_NONE, "nvram: somehow managed to get a carry in hour update\n"); 
                }
 
                if ( (bcd && (hour24 == 0x24)) || 
@@ -432,7 +439,7 @@ static void update_time(struct nvram_internal * data, uint64_t period_us) {
        if (statb->ai) { 
            if ((*sec == *seca) && (*min == *mina) && (*hour == *houra)) { 
                statc->af = 1;
-               PrintDebug("nvram: interrupt on alarm\n");
+               PrintDebug(VM_NONE, VCORE_NONE, "nvram: interrupt on alarm\n");
            }
        }
     }
@@ -442,22 +449,22 @@ static void update_time(struct nvram_internal * data, uint64_t period_us) {
        if (data->pus >= periodic_period) { 
            statc->pf = 1;
            data->pus -= periodic_period;
-           PrintDebug("nvram: interrupt on periodic\n");
+           PrintDebug(VM_NONE, VCORE_NONE, "nvram: interrupt on periodic\n");
        }
     }
 
     if (statb->ui) { 
        statc->uf = 1;
-       PrintDebug("nvram: interrupt on update\n");
+       PrintDebug(VM_NONE, VCORE_NONE, "nvram: interrupt on update\n");
     }
 
     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(VM_NONE, VCORE_NONE, "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");
+       PrintDebug(VM_NONE, VCORE_NONE, "nvram: injecting interrupt\n");
        v3_raise_irq(data->vm, NVRAM_RTC_IRQ);
     }
 }
@@ -472,7 +479,8 @@ static void nvram_update_timer(struct guest_info *vm,
     uint64_t period_us;
 
     
-    period_us = (1000000*cpu_cycles/cpu_freq);
+    // cpu freq in khz
+    period_us = (1000*cpu_cycles/cpu_freq);
 
     update_time(nvram_state,period_us);
 
@@ -483,46 +491,64 @@ 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
+    // 4. High Mem: 4G-... in 64K
 
-    if (bytes > 640 * 1024) {
-       set_memory(nvram, NVRAM_REG_BASE_MEMORY_HIGH, 0x02);
-       set_memory(nvram, NVRAM_REG_BASE_MEMORY_LOW, 0x80);
+    // at most 640K of conventional memory
+    {
+       uint16_t memk = 0;
+
+       if (bytes > (640 * 1024)) {
+           memk = 640;
+       } else {
+           memk = bytes / 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;
        set_memory(nvram, NVRAM_REG_BASE_MEMORY_HIGH, (memk >> 8) & 0x00ff);
        set_memory(nvram, NVRAM_REG_BASE_MEMORY_LOW, memk & 0x00ff);
-
-       return;
     }
 
-    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);
-    } else {
-       uint16_t memk = bytes * 1024;
+    // set extended memory - first 1 MB is lost to 640K chunk
+    // extended memory is min(0MB, bytes - 1MB)
+    {
+       uint16_t memk = 0;
 
+       if (bytes >= (1024 * 1024)) {
+           memk = (bytes - (1024 * 1024)) / 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;
     }
 
+    // Set the extended memory beyond 16 MB in 64k chunks
+    // this is min(0, bytes - 16MB)
     {
-       // Set the extended memory beyond 16 MB in 64k chunks
-       uint16_t mem_chunks = (bytes - (1024 * 1024 * 16)) / (1024 * 64);
+       uint16_t mem_chunks = 0;
 
+       if (bytes >= (1024 * 1024 * 16)) {
+           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 high (>4GB) memory size
+    {
+
+       uint32_t high_mem_chunks = 0;
+
+       if (bytes >= (1024LL * 1024LL * 1024LL * 4LL)) {
+           high_mem_chunks = (bytes - (1024LL * 1024LL * 1024LL * 4LL))  / (1024 * 64);
+       }
+
+       set_memory(nvram, NVRAM_REG_HIGHMEM_LOW, high_mem_chunks & 0xff);
+       set_memory(nvram, NVRAM_REG_HIGHMEM_MID, (high_mem_chunks >> 8) & 0xff);
+       set_memory(nvram, NVRAM_REG_HIGHMEM_HIGH, (high_mem_chunks >> 16) & 0xff);
+    }
+
     return;
 }
 
@@ -610,8 +636,15 @@ static uint16_t compute_checksum(struct nvram_internal * nvram) {
     return checksum;
 }
 
-static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvram) {
+static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvram, char *bootseq) {
     uint16_t checksum = 0;
+    uint64_t mem_size=vm->mem_size;
+    uint32_t num_cores=vm->num_cores;
+
+#ifdef V3_CONFIG_HVM
+    mem_size = v3_get_hvm_ros_memsize(vm);
+    num_cores = v3_get_hvm_ros_cores(vm);
+#endif
 
     memset(nvram->mem_state, 0, NVRAM_REG_MAX);
     memset(nvram->reg_map, 0, NVRAM_REG_MAX / 8);
@@ -619,33 +652,41 @@ static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvra
     v3_lock_init(&(nvram->nvram_lock));
 
     //
-    // 2 1.44 MB floppy drives
+    // There are no floppy drives
     //
-#if 1
-    set_memory(nvram, NVRAM_REG_FLOPPY_TYPE, 0x44);
-#else
     set_memory(nvram, NVRAM_REG_FLOPPY_TYPE, 0x00);
-#endif
 
     //
-    // For old boot sequence style, do floppy first
+    // For old boot sequence style, do non-floppy devices first
     //
-    set_memory(nvram, NVRAM_REG_BOOTSEQ_OLD, 0x10);
-
-#if 0
-    // For new boot sequence style, do floppy, cd, then hd
-    set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x31);
-    set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x20);
-#endif
+    set_memory(nvram, NVRAM_REG_BOOTSEQ_OLD, 0x00);
+
+    if (!strcasecmp(bootseq,"cd")) { 
+       // CD only
+       set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x03);
+       set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x00);
+    } else if (!strcasecmp(bootseq,"cd,hd")) { 
+       // CD, then HD
+       set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x23);
+       set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x00);
+    } else if (!strcasecmp(bootseq,"hd")) {
+       // HD only
+       set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x02);
+       set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x00);
+    } else if (!strcasecmp(bootseq,"hd,cd")) { 
+       // HD, then CD
+       set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x32);
+       set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x00);
+    } else {
+       PrintError(vm,VCORE_NONE,"nvram: unknown boot sequence '%s', setting 'cd,hd'\n",bootseq);
+       // CD, then HD
+       set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x23);
+       set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x00);
+    }
 
-    // For new boot sequence style, do cd, hd, floppy
-    set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x23);
-    set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x10);
-  
-  
-    // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
-    set_memory(nvram, NVRAM_REG_EQUIPMENT_BYTE, 0x4f);
-    // set_memory(nvram, NVRAM_REG_EQUIPMENT_BYTE, 0xf);
+    // Set equipment byte to note no floppies, vga display, keyboard, math
+    set_memory(nvram, NVRAM_REG_EQUIPMENT_BYTE, 0x2e);
   
 
     // Set the shutdown status gently
@@ -658,8 +699,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
@@ -691,8 +732,11 @@ static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvra
     nvram->us = 0;
     nvram->pus = 0;
 
-    set_memory_size(nvram, vm->mem_size);
+
+    set_memory_size(nvram, mem_size);
     init_harddrives(nvram);
+
+    set_memory(nvram, NVRAM_REG_SMPCPUS, num_cores - 1);
     
     /* compute checksum (must follow all assignments here) */
     checksum = compute_checksum(nvram);
@@ -714,11 +758,14 @@ static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvra
 
 static int nvram_write_reg_port(struct guest_info * core, uint16_t port,
                                void * src, uint_t length, void * priv_data) {
-
+    uint8_t reg;
     struct nvram_internal * data = priv_data;
+
+    memcpy(&reg,src,1);
+
+    data->thereg = reg & 0x7f;  //discard NMI bit if it's there
     
-    memcpy(&(data->thereg), src, 1);
-    PrintDebug("nvram: Writing To NVRAM reg: 0x%x\n", data->thereg);
+    PrintDebug(core->vm_info, core, "nvram: Writing To NVRAM reg: 0x%x (NMI_disable=%d)\n", data->thereg,reg>>7);
 
     return 1;
 }
@@ -731,19 +778,11 @@ static int nvram_read_data_port(struct guest_info * core, uint16_t port,
     addr_t irq_state = v3_lock_irqsave(data->nvram_lock);
 
     if (get_memory(data, data->thereg, (uint8_t *)dst) == -1) {
-       PrintError("nvram: Register %d (0x%x) Not set\n", data->thereg, data->thereg);
-
-       v3_unlock_irqrestore(data->nvram_lock, irq_state);
+       PrintError(core->vm_info, core, "nvram: Register %d (0x%x) Not set - POSSIBLE BUG IN MACHINE INIT - CONTINUING\n", data->thereg, data->thereg);
 
-       /* allow guest to query checksummed bytes; warn but read zero rather than fail in this case */
-       if ((data->thereg >= CHECKSUM_REGION_FIRST_BYTE) && (data->thereg <= CHECKSUM_REGION_LAST_BYTE)) {
-           return 1;
-       } else {        
-           return -1;
-       }
-    }
+    } 
 
-    PrintDebug("nvram: nvram_read_data_port(0x%x)  =  0x%x\n", data->thereg, *(uint8_t *)dst);
+    PrintDebug(core->vm_info, core, "nvram: nvram_read_data_port(0x%x)  =  0x%x\n", data->thereg, *(uint8_t *)dst);
 
     // hack
     if (data->thereg == NVRAM_REG_STAT_A) { 
@@ -767,7 +806,7 @@ static int nvram_write_data_port(struct guest_info * core, uint16_t port,
 
     v3_unlock_irqrestore(data->nvram_lock, irq_state);
 
-    PrintDebug("nvram: nvram_write_data_port(0x%x) = 0x%x\n", 
+    PrintDebug(core->vm_info, core, "nvram: nvram_write_data_port(0x%x) = 0x%x\n", 
               data->thereg, data->mem_state[data->thereg]);
 
     return 1;
@@ -785,6 +824,8 @@ static int nvram_free(struct nvram_internal * nvram_state) {
        v3_remove_timer(info,nvram_state->timer);
     }
 
+    v3_lock_deinit(&(nvram_state->nvram_lock));
+
     V3_Free(nvram_state);
     return 0;
 }
@@ -801,24 +842,53 @@ static struct v3_device_ops dev_ops = {
 };
 
 
+/*
+
+  <device class="NVRAM" id="nvram">
+     <storage>STORAGE</storage>
+     <bootseq>BOOTSEQ</bootseq>
+  </device>
 
+  STORAGE = the id of the storage controller that will be used to populate
+            the legacy storage device info (e.g., cd, hd the bios knows about)
 
+  BOOTSEQ = the boot sequence desired - note lack of spaces:
+            
+            cd         - first cd only
+            hd         - first hd only
+            cd,hd      - first cd, then first hd
+            hd,cd      - first hd, then first cd
+     
+            The default is cd,hd
+*/
 
 static int nvram_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     struct nvram_internal * nvram_state = NULL;
     struct vm_device * ide = v3_find_dev(vm, v3_cfg_val(cfg, "storage"));
     char * dev_id = v3_cfg_val(cfg, "ID");
+    char * bootseq = v3_cfg_val(cfg,"bootseq");
+
     int ret = 0;
 
     if (!ide) {
-       PrintError("nvram: Could not find IDE device\n");
+       PrintError(vm, VCORE_NONE, "nvram: Could not find IDE device\n");
        return -1;
     }
 
-    PrintDebug("nvram: init_device\n");
+    if (!bootseq) { 
+       bootseq=DEFAULT_BOOTSEQ;
+       PrintDebug(vm, VCORE_NONE, "nvram: using default boot sequence %s\n",bootseq);
+    }
+
+    PrintDebug(vm, VCORE_NONE, "nvram: init_device\n");
     nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal) + 1000);
 
-    PrintDebug("nvram: internal at %p\n", (void *)nvram_state);
+    if (!nvram_state) {
+       PrintError(vm, VCORE_NONE, "Cannot allocate in init\n");
+       return -1;
+    }
+
+    PrintDebug(vm, VCORE_NONE, "nvram: internal at %p\n", (void *)nvram_state);
 
     nvram_state->ide = ide;
     nvram_state->vm = vm;
@@ -826,19 +896,19 @@ 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("nvram: Could not attach device %s\n", dev_id);
+       PrintError(vm, VCORE_NONE, "nvram: Could not attach device %s\n", dev_id);
        V3_Free(nvram_state);
        return -1;
     }
 
-    init_nvram_state(vm, nvram_state);
+    init_nvram_state(vm, nvram_state, bootseq);
 
     // hook ports
     ret |= v3_dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
     ret |= v3_dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
   
     if (ret != 0) {
-       PrintError("nvram: Error hooking NVRAM IO ports\n");
+       PrintError(vm, VCORE_NONE, "nvram: Error hooking NVRAM IO ports\n");
        v3_remove_device(dev);
        return -1;
     }