2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Peter Dinda <pdinda@northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Peter Dinda <pdinda@northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #include <palacios/vmm_dev_mgr.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_types.h>
25 #include <palacios/vmm_lock.h>
27 #include <devices/ide.h>
28 #include <palacios/vmm_intr.h>
29 #include <palacios/vmm_host_events.h>
30 #include <palacios/vm_guest.h>
32 #ifndef CONFIG_DEBUG_NVRAM
34 #define PrintDebug(fmt, args...)
38 #define NVRAM_REG_PORT 0x70
39 #define NVRAM_DATA_PORT 0x71
41 #define NVRAM_RTC_IRQ 0x8
44 typedef enum {NVRAM_READY, NVRAM_REG_POSTED} nvram_state_t;
47 #define NVRAM_REG_MAX 256
50 // These are borrowed from Bochs, which borrowed from
51 // Ralf Brown's interupt list, and extended
52 #define NVRAM_REG_SEC 0x00
53 #define NVRAM_REG_SEC_ALARM 0x01
54 #define NVRAM_REG_MIN 0x02
55 #define NVRAM_REG_MIN_ALARM 0x03
56 #define NVRAM_REG_HOUR 0x04
57 #define NVRAM_REG_HOUR_ALARM 0x05
58 #define NVRAM_REG_WEEK_DAY 0x06
59 #define NVRAM_REG_MONTH_DAY 0x07
60 #define NVRAM_REG_MONTH 0x08
61 #define NVRAM_REG_YEAR 0x09
62 #define NVRAM_REG_STAT_A 0x0a
63 #define NVRAM_REG_STAT_B 0x0b
64 #define NVRAM_REG_STAT_C 0x0c
65 #define NVRAM_REG_STAT_D 0x0d
66 #define NVRAM_REG_DIAGNOSTIC_STATUS 0x0e
67 #define NVRAM_REG_SHUTDOWN_STATUS 0x0f
69 #define NVRAM_IBM_HD_DATA 0x12
70 #define NVRAM_IDE_TRANSLATION 0x39
72 #define NVRAM_REG_FLOPPY_TYPE 0x10
73 #define NVRAM_REG_EQUIPMENT_BYTE 0x14
75 #define NVRAM_REG_BASE_MEMORY_HIGH 0x16
76 #define NVRAM_REG_BASE_MEMORY_LOW 0x15
78 #define NVRAM_REG_EXT_MEMORY_HIGH 0x18
79 #define NVRAM_REG_EXT_MEMORY_LOW 0x17
81 #define NVRAM_REG_EXT_MEMORY_2ND_HIGH 0x31
82 #define NVRAM_REG_EXT_MEMORY_2ND_LOW 0x30
84 #define NVRAM_REG_BOOTSEQ_OLD 0x2d
86 #define NVRAM_REG_AMI_BIG_MEMORY_HIGH 0x35
87 #define NVRAM_REG_AMI_BIG_MEMORY_LOW 0x34
89 #define NVRAM_REG_CSUM_HIGH 0x2e
90 #define NVRAM_REG_CSUM_LOW 0x2f
91 #define NVRAM_REG_IBM_CENTURY_BYTE 0x32
92 #define NVRAM_REG_IBM_PS2_CENTURY_BYTE 0x37
94 #define NVRAM_REG_BOOTSEQ_NEW_FIRST 0x3D
95 #define NVRAM_REG_BOOTSEQ_NEW_SECOND 0x38
98 struct nvram_internal {
99 nvram_state_t dev_state;
101 uchar_t mem_state[NVRAM_REG_MAX];
102 uchar_t reg_map[NVRAM_REG_MAX / 8];
104 struct vm_device * ide;
106 struct v3_vm_info * vm;
108 v3_lock_t nvram_lock;
110 uint_t us; //microseconds - for clock update - zeroed every second
111 uint_t pus; //microseconds - for periodic interrupt - cleared every period
116 uint_t rate: 4; // clock rate = 65536Hz / 2 rate (0110=1024 Hz)
117 uint_t basis: 3; // time base, 010 = 32,768 Hz
118 uint_t uip: 1; // 1=update in progress
119 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
122 uint_t sum: 1; // 1=summer (daylight savings)
123 uint_t h24: 1; // 1=24h clock
124 uint_t dm: 1; // 1=date/time is in bcd, 0=binary
125 uint_t rec: 1; // 1=rectangular signal
126 uint_t ui: 1; // 1=update interrupt
127 uint_t ai: 1; // 1=alarm interrupt
128 uint_t pi: 1; // 1=periodic interrupt
129 uint_t set: 1; // 1=blocked update
130 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
133 uint_t res: 4; // reserved
134 uint_t uf: 1; // 1=source of interrupt is update
135 uint_t af: 1; // 1=source of interrupt is alarm interrupt
136 uint_t pf: 1; // 1=source of interrupt is periodic interrupt
137 uint_t irq: 1; // 1=interrupt requested
138 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
141 uint_t res: 7; // reserved
142 uint_t val: 1; // 1=cmos ram data is OK
143 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
155 static void set_reg_num(struct nvram_internal * nvram, uint8_t reg_num) {
156 int major = (reg_num / 8);
157 int minor = reg_num % 8;
159 nvram->reg_map[major] |= (0x1 << minor);
162 static int is_reg_set(struct nvram_internal * nvram, uint8_t reg_num) {
163 int major = (reg_num / 8);
164 int minor = reg_num % 8;
166 return (nvram->reg_map[major] & (0x1 << minor)) ? 1 : 0;
170 static void set_memory(struct nvram_internal * nvram, uint8_t reg, uint8_t val) {
171 set_reg_num(nvram, reg);
172 nvram->mem_state[reg] = val;
175 static int get_memory(struct nvram_internal * nvram, uint8_t reg, uint8_t * val) {
177 if (!is_reg_set(nvram, reg)) {
182 *val = nvram->mem_state[reg];
187 static uchar_t add_to(uchar_t * left, uchar_t * right, uchar_t bcd) {
191 struct bcd_num * bl = (struct bcd_num *)left;
192 struct bcd_num * br = (struct bcd_num *)right;
196 carry = bl->bot / 0xa;
199 bl->top += carry + br->top;
200 carry = bl->top / 0xa;
217 static uchar_t days_in_month(uchar_t month, uchar_t bcd) {
218 // This completely ignores Julian / Gregorian stuff right now
275 static void update_time(struct nvram_internal * data, uint_t period_us) {
276 struct rtc_stata * stata = (struct rtc_stata *) &((data->mem_state[NVRAM_REG_STAT_A]));
277 struct rtc_statb * statb = (struct rtc_statb *) &((data->mem_state[NVRAM_REG_STAT_B]));
278 struct rtc_statc * statc = (struct rtc_statc *) &((data->mem_state[NVRAM_REG_STAT_C]));
279 //struct rtc_statd *statd = (struct rtc_statd *) &((data->mem_state[NVRAM_REG_STAT_D]));
280 uchar_t * sec = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC]);
281 uchar_t * min = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN]);
282 uchar_t * hour = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR]);
283 uchar_t * weekday = (uchar_t *) &(data->mem_state[NVRAM_REG_WEEK_DAY]);
284 uchar_t * monthday = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH_DAY]);
285 uchar_t * month = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH]);
286 uchar_t * year = (uchar_t *) &(data->mem_state[NVRAM_REG_YEAR]);
287 uchar_t * cent = (uchar_t *) &(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
288 uchar_t * seca = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC_ALARM]);
289 uchar_t * mina = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN_ALARM]);
290 uchar_t * houra = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR_ALARM]);
293 uchar_t bcd = (statb->dm == 1);
296 uint_t periodic_period;
298 //PrintDebug("nvram: sizeof(struct rtc_stata)=%d\n", sizeof(struct rtc_stata));
301 //PrintDebug("nvram: update_time\n",statb->pi);
303 // We will set these flags on exit
309 // We will reset us after one second
310 data->us += period_us;
311 // We will reset pus after one periodic_period
312 data->pus += period_us;
314 if (data->us > 1000000) {
316 carry = add_to(sec, &carry, bcd);
319 PrintDebug("nvram: somehow managed to get a carry in second update\n");
322 if ( (bcd && (*sec == 0x60)) ||
323 ((!bcd) && (*sec == 60))) {
328 carry = add_to(min, &carry, bcd);
330 PrintDebug("nvram: somehow managed to get a carry in minute update\n");
333 if ( (bcd && (*min == 0x60)) ||
334 ((!bcd) && (*min == 60))) {
343 uchar_t temp = ((bcd) ? 0x12 : 12);
344 add_to(&hour24, &temp, bcd);
349 carry = add_to(&hour24, &carry, bcd);
351 PrintDebug("nvram: somehow managed to get a carry in hour update\n");
354 if ( (bcd && (hour24 == 0x24)) ||
355 ((!bcd) && (hour24 == 24))) {
367 if ( (bcd && (hour24 < 0x12)) ||
368 ((!bcd) && (hour24 < 12))) {
374 *hour = (hour24 - 12) | 0x80;
377 struct bcd_num * n = (struct bcd_num *)hour;
390 // now see if we need to carry into the days and further
393 add_to(weekday, &carry, bcd);
395 *weekday %= 0x7; // same regardless of bcd
397 if ((*monthday) != days_in_month(*month, bcd)) {
398 add_to(monthday, &carry, bcd);
403 add_to(month, &carry, bcd);
405 if ( (bcd && (*month == 0x13)) ||
406 ((!bcd) && (*month == 13))) {
407 *month = 1; // same for both
410 carry = add_to(year, &carry, bcd);
412 if ( (bcd && carry) ||
413 ((!bcd) && (*year == 100))) {
416 add_to(cent, &carry, bcd);
426 // OK, now check for the alarm, if it is set to interrupt
428 if ((*sec == *seca) && (*min == *mina) && (*hour == *houra)) {
430 PrintDebug("nvram: interrupt on alarm\n");
436 periodic_period = 1000000 / (65536 / (0x1 << stata->rate));
437 if (data->pus >= periodic_period) {
439 data->pus -= periodic_period;
440 PrintDebug("nvram: interrupt on periodic\n");
446 PrintDebug("nvram: interrupt on update\n");
449 statc->irq = (statc->pf || statc->af || statc->uf);
451 //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);
453 // Interrupt associated VM, if needed
455 PrintDebug("nvram: injecting interrupt\n");
456 v3_raise_irq(data->vm, NVRAM_RTC_IRQ);
461 static int handle_timer_event(struct v3_vm_info * vm,
462 struct v3_timer_event * evt,
466 struct nvram_internal * data = priv_data;
469 addr_t irq_state = v3_lock_irqsave(data->nvram_lock);
470 update_time(data, evt->period_us);
471 v3_unlock_irqrestore(data->nvram_lock, irq_state);
479 static void set_memory_size(struct nvram_internal * nvram, addr_t bytes) {
480 // 1. Conventional Mem: 0-640k in K
481 // 2. Extended Mem: 0-16MB in K
482 // 3. Big Mem: 0-4G in 64K
484 if (bytes > 640 * 1024) {
485 set_memory(nvram, NVRAM_REG_BASE_MEMORY_HIGH, 0x02);
486 set_memory(nvram, NVRAM_REG_BASE_MEMORY_LOW, 0x80);
488 // nvram->mem_state[NVRAM_REG_BASE_MEMORY_HIGH] = 0x02;
489 // nvram->mem_state[NVRAM_REG_BASE_MEMORY_LOW] = 0x80;
491 uint16_t memk = bytes * 1024;
492 set_memory(nvram, NVRAM_REG_BASE_MEMORY_HIGH, (memk >> 8) & 0x00ff);
493 set_memory(nvram, NVRAM_REG_BASE_MEMORY_LOW, memk & 0x00ff);
498 if (bytes > (16 * 1024 * 1024)) {
499 // Set extended memory to 15 MB
500 set_memory(nvram, NVRAM_REG_EXT_MEMORY_HIGH, 0x3C);
501 set_memory(nvram, NVRAM_REG_EXT_MEMORY_LOW, 0x00);
502 set_memory(nvram, NVRAM_REG_EXT_MEMORY_2ND_HIGH, 0x3C);
503 set_memory(nvram, NVRAM_REG_EXT_MEMORY_2ND_LOW, 0x00);
505 uint16_t memk = bytes * 1024;
507 set_memory(nvram, NVRAM_REG_EXT_MEMORY_HIGH, (memk >> 8) & 0x00ff);
508 set_memory(nvram, NVRAM_REG_EXT_MEMORY_LOW, memk & 0x00ff);
509 set_memory(nvram, NVRAM_REG_EXT_MEMORY_2ND_HIGH, (memk >> 8) & 0x00ff);
510 set_memory(nvram, NVRAM_REG_EXT_MEMORY_2ND_LOW, memk & 0x00ff);
516 // Set the extended memory beyond 16 MB in 64k chunks
517 uint16_t mem_chunks = (bytes - (1024 * 1024 * 16)) / (1024 * 64);
519 set_memory(nvram, NVRAM_REG_AMI_BIG_MEMORY_HIGH, (mem_chunks >> 8) & 0x00ff);
520 set_memory(nvram, NVRAM_REG_AMI_BIG_MEMORY_LOW, mem_chunks & 0x00ff);
528 static void init_harddrives(struct nvram_internal * nvram) {
534 int info_base_reg = 0x1b;
537 // 0x19 == first drive type
538 // 0x1a == second drive type
540 // 0x1b == first drive geometry base
541 // 0x24 == second drive geometry base
543 // It looks like the BIOS only tracks the disks on the first channel at 0x12?
544 for (i = 0; i < 2; i++) {
545 if (v3_ide_get_geometry(nvram->ide, 0, i, &cyls, &heads, §s) == 0) {
547 int info_reg = info_base_reg + (i * 9);
549 set_memory(nvram, type_reg + i, 0x2f);
551 set_memory(nvram, info_reg, cyls & 0xff);
552 set_memory(nvram, info_reg + 1, (cyls >> 8) & 0xff);
553 set_memory(nvram, info_reg + 2, heads & 0xff);
555 // Write precomp cylinder (1 and 2)
556 set_memory(nvram, info_reg + 3, 0xff);
557 set_memory(nvram, info_reg + 4, 0xff);
559 // harddrive control byte
560 set_memory(nvram, info_reg + 5, 0xc0 | ((heads > 8) << 3));
562 set_memory(nvram, info_reg + 6, cyls & 0xff);
563 set_memory(nvram, info_reg + 7, (cyls >> 8) & 0xff);
565 set_memory(nvram, info_reg + 8, sects & 0xff);
567 hd_data |= (0xf0 >> (i * 4));
571 set_memory(nvram, NVRAM_IBM_HD_DATA, hd_data);
574 #define TRANSLATE_NONE 0x0
575 #define TRANSLATE_LBA 0x1
576 #define TRANSLATE_LARGE 0x2
577 #define TRANSLATE_RECHS 0x3
578 // We're going to do LBA translation for everything...
581 for (i = 0; i < 4; i++) {
582 int chan_num = i / 2;
583 int drive_num = i % 2;
586 if (v3_ide_get_geometry(nvram->ide, chan_num, drive_num, &tmp[0], &tmp[1], &tmp[2]) == 0) {
587 trans |= TRANSLATE_LBA << (i * 2);
591 set_memory(nvram, NVRAM_IDE_TRANSLATION, trans);
595 static int init_nvram_state(struct v3_vm_info * vm, struct nvram_internal * nvram) {
597 memset(nvram->mem_state, 0, NVRAM_REG_MAX);
598 memset(nvram->reg_map, 0, NVRAM_REG_MAX / 8);
600 v3_lock_init(&(nvram->nvram_lock));
603 // 2 1.44 MB floppy drives
606 set_memory(nvram, NVRAM_REG_FLOPPY_TYPE, 0x44);
608 set_memory(nvram, NVRAM_REG_FLOPPY_TYPE, 0x00);
612 // For old boot sequence style, do floppy first
614 set_memory(nvram, NVRAM_REG_BOOTSEQ_OLD, 0x10);
617 // For new boot sequence style, do floppy, cd, then hd
618 set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x31);
619 set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x20);
622 // For new boot sequence style, do cd, hd, floppy
623 set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x23);
624 set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x10);
627 // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
628 set_memory(nvram, NVRAM_REG_EQUIPMENT_BYTE, 0x4f);
629 // set_memory(nvram, NVRAM_REG_EQUIPMENT_BYTE, 0xf);
632 // Set the shutdown status gently
634 set_memory(nvram, NVRAM_REG_SHUTDOWN_STATUS, 0x0);
638 // 00100110 = no update in progress, base=32768 Hz, rate = 1024 Hz
639 set_memory(nvram, NVRAM_REG_STAT_A, 0x26);
642 // 00000100 = not setting, no interrupts, blocked rect signal, bcd mode, 24 hour, normal time
643 set_memory(nvram, NVRAM_REG_STAT_B, 0x06);
647 // No IRQ requested, result not do to any source
648 set_memory(nvram, NVRAM_REG_STAT_C, 0x00);
652 set_memory(nvram, NVRAM_REG_STAT_D, 0x80);
655 // january 1, 2008, 00:00:00
656 set_memory(nvram, NVRAM_REG_SEC, 0x00);
657 set_memory(nvram, NVRAM_REG_SEC_ALARM, 0x00);
658 set_memory(nvram, NVRAM_REG_MIN, 0x00);
659 set_memory(nvram, NVRAM_REG_MIN_ALARM, 0x00);
660 set_memory(nvram, NVRAM_REG_HOUR, 0x00);
661 set_memory(nvram, NVRAM_REG_HOUR_ALARM, 0x00);
663 set_memory(nvram, NVRAM_REG_MONTH, 0x01);
664 set_memory(nvram, NVRAM_REG_MONTH_DAY, 0x1);
665 set_memory(nvram, NVRAM_REG_WEEK_DAY, 0x1);
666 set_memory(nvram, NVRAM_REG_YEAR, 0x08);
668 set_memory(nvram, NVRAM_REG_DIAGNOSTIC_STATUS, 0x00);
673 set_memory_size(nvram, vm->mem_size);
674 init_harddrives(nvram);
676 nvram->dev_state = NVRAM_READY;
687 static int nvram_write_reg_port(struct guest_info * core, ushort_t port,
688 void * src, uint_t length, void * priv_data) {
690 struct nvram_internal * data = priv_data;
692 memcpy(&(data->thereg), src, 1);
693 PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
698 static int nvram_read_data_port(struct guest_info * core, ushort_t port,
699 void * dst, uint_t length, void * priv_data) {
701 struct nvram_internal * data = priv_data;
703 addr_t irq_state = v3_lock_irqsave(data->nvram_lock);
705 if (get_memory(data, data->thereg, (uint8_t *)dst) == -1) {
706 PrintError("Register %d (0x%x) Not set\n", data->thereg, data->thereg);
708 v3_unlock_irqrestore(data->nvram_lock, irq_state);
713 PrintDebug("nvram_read_data_port(0x%x) = 0x%x\n", data->thereg, *(uint8_t *)dst);
716 if (data->thereg == NVRAM_REG_STAT_A) {
717 data->mem_state[data->thereg] ^= 0x80; // toggle Update in progess
720 v3_unlock_irqrestore(data->nvram_lock, irq_state);
726 static int nvram_write_data_port(struct guest_info * core, ushort_t port,
727 void * src, uint_t length, void * priv_data) {
729 struct nvram_internal * data = priv_data;
731 addr_t irq_state = v3_lock_irqsave(data->nvram_lock);
733 set_memory(data, data->thereg, *(uint8_t *)src);
735 v3_unlock_irqrestore(data->nvram_lock, irq_state);
737 PrintDebug("nvram_write_data_port(0x%x) = 0x%x\n",
738 data->thereg, data->mem_state[data->thereg]);
746 static int nvram_free(struct nvram_internal * nvram_state) {
748 // unregister host events
750 V3_Free(nvram_state);
758 static struct v3_device_ops dev_ops = {
759 .free = (int (*)(void *))nvram_free,
766 static int nvram_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
767 struct nvram_internal * nvram_state = NULL;
768 struct vm_device * ide = v3_find_dev(vm, v3_cfg_val(cfg, "storage"));
769 char * dev_id = v3_cfg_val(cfg, "ID");
773 PrintError("Could not find IDE device\n");
777 PrintDebug("nvram: init_device\n");
778 nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal) + 1000);
780 PrintDebug("nvram: internal at %p\n", (void *)nvram_state);
782 nvram_state->ide = ide;
783 nvram_state->vm = vm;
785 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, nvram_state);
788 PrintError("Could not attach device %s\n", dev_id);
789 V3_Free(nvram_state);
793 init_nvram_state(vm, nvram_state);
796 ret |= v3_dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
797 ret |= v3_dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
800 PrintError("Error hooking NVRAM IO ports\n");
801 v3_remove_device(dev);
805 v3_hook_host_event(vm, HOST_TIMER_EVT, V3_HOST_EVENT_HANDLER(handle_timer_event), nvram_state);
810 device_register("NVRAM", nvram_init)