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 v3_lock_t nvram_lock;
108 uint_t us; //microseconds - for clock update - zeroed every second
109 uint_t pus; //microseconds - for periodic interrupt - cleared every period
114 uint_t rate: 4; // clock rate = 65536Hz / 2 rate (0110=1024 Hz)
115 uint_t basis: 3; // time base, 010 = 32,768 Hz
116 uint_t uip: 1; // 1=update in progress
117 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
120 uint_t sum: 1; // 1=summer (daylight savings)
121 uint_t h24: 1; // 1=24h clock
122 uint_t dm: 1; // 1=date/time is in bcd, 0=binary
123 uint_t rec: 1; // 1=rectangular signal
124 uint_t ui: 1; // 1=update interrupt
125 uint_t ai: 1; // 1=alarm interrupt
126 uint_t pi: 1; // 1=periodic interrupt
127 uint_t set: 1; // 1=blocked update
128 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
131 uint_t res: 4; // reserved
132 uint_t uf: 1; // 1=source of interrupt is update
133 uint_t af: 1; // 1=source of interrupt is alarm interrupt
134 uint_t pf: 1; // 1=source of interrupt is periodic interrupt
135 uint_t irq: 1; // 1=interrupt requested
136 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
139 uint_t res: 7; // reserved
140 uint_t val: 1; // 1=cmos ram data is OK
141 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
153 static void set_reg_num(struct nvram_internal * nvram, uint8_t reg_num) {
154 int major = (reg_num / 8);
155 int minor = reg_num % 8;
157 nvram->reg_map[major] |= (0x1 << minor);
160 static int is_reg_set(struct nvram_internal * nvram, uint8_t reg_num) {
161 int major = (reg_num / 8);
162 int minor = reg_num % 8;
164 return (nvram->reg_map[major] & (0x1 << minor)) ? 1 : 0;
168 static void set_memory(struct nvram_internal * nvram, uint8_t reg, uint8_t val) {
169 set_reg_num(nvram, reg);
170 nvram->mem_state[reg] = val;
173 static int get_memory(struct nvram_internal * nvram, uint8_t reg, uint8_t * val) {
175 if (!is_reg_set(nvram, reg)) {
180 *val = nvram->mem_state[reg];
185 static uchar_t add_to(uchar_t * left, uchar_t * right, uchar_t bcd) {
189 struct bcd_num * bl = (struct bcd_num *)left;
190 struct bcd_num * br = (struct bcd_num *)right;
194 carry = bl->bot / 0xa;
197 bl->top += carry + br->top;
198 carry = bl->top / 0xa;
215 static uchar_t days_in_month(uchar_t month, uchar_t bcd) {
216 // This completely ignores Julian / Gregorian stuff right now
273 static void update_time( struct vm_device * dev, uint_t period_us) {
274 struct nvram_internal * data = (struct nvram_internal *) (dev->private_data);
275 struct rtc_stata * stata = (struct rtc_stata *) &((data->mem_state[NVRAM_REG_STAT_A]));
276 struct rtc_statb * statb = (struct rtc_statb *) &((data->mem_state[NVRAM_REG_STAT_B]));
277 struct rtc_statc * statc = (struct rtc_statc *) &((data->mem_state[NVRAM_REG_STAT_C]));
278 //struct rtc_statd *statd = (struct rtc_statd *) &((data->mem_state[NVRAM_REG_STAT_D]));
279 uchar_t * sec = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC]);
280 uchar_t * min = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN]);
281 uchar_t * hour = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR]);
282 uchar_t * weekday = (uchar_t *) &(data->mem_state[NVRAM_REG_WEEK_DAY]);
283 uchar_t * monthday = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH_DAY]);
284 uchar_t * month = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH]);
285 uchar_t * year = (uchar_t *) &(data->mem_state[NVRAM_REG_YEAR]);
286 uchar_t * cent = (uchar_t *) &(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
287 uchar_t * seca = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC_ALARM]);
288 uchar_t * mina = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN_ALARM]);
289 uchar_t * houra = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR_ALARM]);
292 uchar_t bcd = (statb->dm == 1);
295 uint_t periodic_period;
297 //PrintDebug("nvram: sizeof(struct rtc_stata)=%d\n", sizeof(struct rtc_stata));
300 //PrintDebug("nvram: update_time\n",statb->pi);
302 // We will set these flags on exit
308 // We will reset us after one second
309 data->us += period_us;
310 // We will reset pus after one periodic_period
311 data->pus += period_us;
313 if (data->us > 1000000) {
315 carry = add_to(sec, &carry, bcd);
318 PrintDebug("nvram: somehow managed to get a carry in second update\n");
321 if ( (bcd && (*sec == 0x60)) ||
322 ((!bcd) && (*sec == 60))) {
327 carry = add_to(min, &carry, bcd);
329 PrintDebug("nvram: somehow managed to get a carry in minute update\n");
332 if ( (bcd && (*min == 0x60)) ||
333 ((!bcd) && (*min == 60))) {
342 uchar_t temp = ((bcd) ? 0x12 : 12);
343 add_to(&hour24, &temp, bcd);
348 carry = add_to(&hour24, &carry, bcd);
350 PrintDebug("nvram: somehow managed to get a carry in hour update\n");
353 if ( (bcd && (hour24 == 0x24)) ||
354 ((!bcd) && (hour24 == 24))) {
366 if ( (bcd && (hour24 < 0x12)) ||
367 ((!bcd) && (hour24 < 12))) {
373 *hour = (hour24 - 12) | 0x80;
376 struct bcd_num * n = (struct bcd_num *)hour;
389 // now see if we need to carry into the days and further
392 add_to(weekday, &carry, bcd);
394 *weekday %= 0x7; // same regardless of bcd
396 if ((*monthday) != days_in_month(*month, bcd)) {
397 add_to(monthday, &carry, bcd);
402 add_to(month, &carry, bcd);
404 if ( (bcd && (*month == 0x13)) ||
405 ((!bcd) && (*month == 13))) {
406 *month = 1; // same for both
409 carry = add_to(year, &carry, bcd);
411 if ( (bcd && carry) ||
412 ((!bcd) && (*year == 100))) {
415 add_to(cent, &carry, bcd);
425 // OK, now check for the alarm, if it is set to interrupt
427 if ((*sec == *seca) && (*min == *mina) && (*hour == *houra)) {
429 PrintDebug("nvram: interrupt on alarm\n");
435 periodic_period = 1000000 / (65536 / (0x1 << stata->rate));
436 if (data->pus >= periodic_period) {
438 data->pus -= periodic_period;
439 PrintDebug("nvram: interrupt on periodic\n");
445 PrintDebug("nvram: interrupt on update\n");
448 statc->irq = (statc->pf || statc->af || statc->uf);
450 //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);
452 // Interrupt associated VM, if needed
454 PrintDebug("nvram: injecting interrupt\n");
455 v3_raise_irq(dev->vm, NVRAM_RTC_IRQ);
460 static int handle_timer_event(struct v3_vm_info * vm,
461 struct v3_timer_event * evt,
464 struct vm_device * dev = (struct vm_device *)priv_data;
467 struct nvram_internal * data = (struct nvram_internal *) (dev->private_data);
469 addr_t irq_state = v3_lock_irqsave(data->nvram_lock);
470 update_time(dev, 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 vm_device * dev) {
597 struct nvram_internal * nvram = (struct nvram_internal *)dev->private_data;
599 memset(nvram->mem_state, 0, NVRAM_REG_MAX);
600 memset(nvram->reg_map, 0, NVRAM_REG_MAX / 8);
602 v3_lock_init(&(nvram->nvram_lock));
605 // 2 1.44 MB floppy drives
608 set_memory(nvram, NVRAM_REG_FLOPPY_TYPE, 0x44);
610 set_memory(nvram, NVRAM_REG_FLOPPY_TYPE, 0x00);
614 // For old boot sequence style, do floppy first
616 set_memory(nvram, NVRAM_REG_BOOTSEQ_OLD, 0x10);
619 // For new boot sequence style, do floppy, cd, then hd
620 set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x31);
621 set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x20);
624 // For new boot sequence style, do cd, hd, floppy
625 set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_FIRST, 0x23);
626 set_memory(nvram, NVRAM_REG_BOOTSEQ_NEW_SECOND, 0x10);
629 // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
630 set_memory(nvram, NVRAM_REG_EQUIPMENT_BYTE, 0x4f);
631 // set_memory(nvram, NVRAM_REG_EQUIPMENT_BYTE, 0xf);
634 // Set the shutdown status gently
636 set_memory(nvram, NVRAM_REG_SHUTDOWN_STATUS, 0x0);
640 // 00100110 = no update in progress, base=32768 Hz, rate = 1024 Hz
641 set_memory(nvram, NVRAM_REG_STAT_A, 0x26);
644 // 00000100 = not setting, no interrupts, blocked rect signal, bcd mode, 24 hour, normal time
645 set_memory(nvram, NVRAM_REG_STAT_B, 0x06);
649 // No IRQ requested, result not do to any source
650 set_memory(nvram, NVRAM_REG_STAT_C, 0x00);
654 set_memory(nvram, NVRAM_REG_STAT_D, 0x80);
657 // january 1, 2008, 00:00:00
658 set_memory(nvram, NVRAM_REG_SEC, 0x00);
659 set_memory(nvram, NVRAM_REG_SEC_ALARM, 0x00);
660 set_memory(nvram, NVRAM_REG_MIN, 0x00);
661 set_memory(nvram, NVRAM_REG_MIN_ALARM, 0x00);
662 set_memory(nvram, NVRAM_REG_HOUR, 0x00);
663 set_memory(nvram, NVRAM_REG_HOUR_ALARM, 0x00);
665 set_memory(nvram, NVRAM_REG_MONTH, 0x01);
666 set_memory(nvram, NVRAM_REG_MONTH_DAY, 0x1);
667 set_memory(nvram, NVRAM_REG_WEEK_DAY, 0x1);
668 set_memory(nvram, NVRAM_REG_YEAR, 0x08);
670 set_memory(nvram, NVRAM_REG_DIAGNOSTIC_STATUS, 0x00);
675 set_memory_size(nvram, vm->mem_size);
676 init_harddrives(nvram);
678 nvram->dev_state = NVRAM_READY;
687 static int nvram_reset_device(struct vm_device * dev) {
696 static int nvram_start_device(struct vm_device * dev) {
697 PrintDebug("nvram: start device\n");
702 static int nvram_stop_device(struct vm_device * dev) {
703 PrintDebug("nvram: stop device\n");
710 static int nvram_write_reg_port(struct guest_info * core, ushort_t port,
711 void * src, uint_t length, struct vm_device * dev) {
713 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
715 memcpy(&(data->thereg), src, 1);
716 PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
721 static int nvram_read_data_port(struct guest_info * core, ushort_t port,
722 void * dst, uint_t length, struct vm_device * dev) {
724 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
726 addr_t irq_state = v3_lock_irqsave(data->nvram_lock);
728 if (get_memory(data, data->thereg, (uint8_t *)dst) == -1) {
729 PrintError("Register %d (0x%x) Not set\n", data->thereg, data->thereg);
731 v3_unlock_irqrestore(data->nvram_lock, irq_state);
736 PrintDebug("nvram_read_data_port(0x%x) = 0x%x\n", data->thereg, *(uint8_t *)dst);
739 if (data->thereg == NVRAM_REG_STAT_A) {
740 data->mem_state[data->thereg] ^= 0x80; // toggle Update in progess
743 v3_unlock_irqrestore(data->nvram_lock, irq_state);
749 static int nvram_write_data_port(struct guest_info * core, ushort_t port,
750 void * src, uint_t length, struct vm_device * dev) {
752 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
754 addr_t irq_state = v3_lock_irqsave(data->nvram_lock);
756 set_memory(data, data->thereg, *(uint8_t *)src);
758 v3_unlock_irqrestore(data->nvram_lock, irq_state);
760 PrintDebug("nvram_write_data_port(0x%x) = 0x%x\n",
761 data->thereg, data->mem_state[data->thereg]);
769 static int nvram_free(struct vm_device * dev) {
770 v3_dev_unhook_io(dev, NVRAM_REG_PORT);
771 v3_dev_unhook_io(dev, NVRAM_DATA_PORT);
780 static struct v3_device_ops dev_ops = {
782 .reset = nvram_reset_device,
783 .start = nvram_start_device,
784 .stop = nvram_stop_device,
791 static int nvram_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
792 struct nvram_internal * nvram_state = NULL;
793 struct vm_device * ide = v3_find_dev(vm, v3_cfg_val(cfg, "storage"));
794 char * dev_id = v3_cfg_val(cfg, "ID");
797 PrintError("Could not find IDE device\n");
801 PrintDebug("nvram: init_device\n");
802 nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal) + 1000);
804 PrintDebug("nvram: internal at %p\n", (void *)nvram_state);
806 nvram_state->ide = ide;
808 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, nvram_state);
811 if (v3_attach_device(vm, dev) == -1) {
812 PrintError("Could not attach device %s\n", dev_id);
816 init_nvram_state(vm, dev);
819 v3_dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
820 v3_dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
822 v3_hook_host_event(vm, HOST_TIMER_EVT, V3_HOST_EVENT_HANDLER(handle_timer_event), dev);
827 device_register("NVRAM", nvram_init)