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 <devices/nvram.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_types.h>
28 #define PrintDebug(fmt, args...)
32 #define NVRAM_REG_PORT 0x70
33 #define NVRAM_DATA_PORT 0x71
35 #define NVRAM_RTC_IRQ 0x8
38 typedef enum {NVRAM_READY, NVRAM_REG_POSTED} nvram_state_t;
41 #define NVRAM_REG_MAX 256
44 // These are borrowed from Bochs, which borrowed from
45 // Ralf Brown's interupt list, and extended
46 #define NVRAM_REG_SEC 0x00
47 #define NVRAM_REG_SEC_ALARM 0x01
48 #define NVRAM_REG_MIN 0x02
49 #define NVRAM_REG_MIN_ALARM 0x03
50 #define NVRAM_REG_HOUR 0x04
51 #define NVRAM_REG_HOUR_ALARM 0x05
52 #define NVRAM_REG_WEEK_DAY 0x06
53 #define NVRAM_REG_MONTH_DAY 0x07
54 #define NVRAM_REG_MONTH 0x08
55 #define NVRAM_REG_YEAR 0x09
56 #define NVRAM_REG_STAT_A 0x0a
57 #define NVRAM_REG_STAT_B 0x0b
58 #define NVRAM_REG_STAT_C 0x0c
59 #define NVRAM_REG_STAT_D 0x0d
60 #define NVRAM_REG_DIAGNOSTIC_STATUS 0x0e
61 #define NVRAM_REG_SHUTDOWN_STATUS 0x0f
63 #define NVRAM_IBM_HD_DATA 0x12
65 #define NVRAM_REG_FLOPPY_TYPE 0x10
66 #define NVRAM_REG_EQUIPMENT_BYTE 0x14
68 #define NVRAM_REG_BASE_MEMORY_HIGH 0x16
69 #define NVRAM_REG_BASE_MEMORY_LOW 0x15
71 #define NVRAM_REG_EXT_MEMORY_HIGH 0x18
72 #define NVRAM_REG_EXT_MEMORY_LOW 0x17
74 #define NVRAM_REG_EXT_MEMORY_2ND_HIGH 0x31
75 #define NVRAM_REG_EXT_MEMORY_2ND_LOW 0x30
77 #define NVRAM_REG_BOOTSEQ_OLD 0x2d
79 #define NVRAM_REG_AMI_BIG_MEMORY_HIGH 0x35
80 #define NVRAM_REG_AMI_BIG_MEMORY_LOW 0x34
83 #define NVRAM_REG_CSUM_HIGH 0x2e
84 #define NVRAM_REG_CSUM_LOW 0x2f
85 #define NVRAM_REG_IBM_CENTURY_BYTE 0x32
86 #define NVRAM_REG_IBM_PS2_CENTURY_BYTE 0x37
88 #define NVRAM_REG_BOOTSEQ_NEW_FIRST 0x3D
89 #define NVRAM_REG_BOOTSEQ_NEW_SECOND 0x38
92 struct nvram_internal {
93 nvram_state_t dev_state;
95 uchar_t mem_state[NVRAM_REG_MAX];
97 uint_t us; //microseconds - for clock update - zeroed every second
98 uint_t pus; //microseconds - for periodic interrupt - cleared every period
103 uint_t rate: 4; // clock rate = 65536Hz / 2 rate (0110=1024 Hz)
104 uint_t basis: 3; // time base, 010 = 32,768 Hz
105 uint_t uip: 1; // 1=update in progress
106 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
109 uint_t sum: 1; // 1=summer (daylight savings)
110 uint_t h24: 1; // 1=24h clock
111 uint_t dm: 1; // 1=date/time is in bcd, 0=binary
112 uint_t rec: 1; // 1=rectangular signal
113 uint_t ui: 1; // 1=update interrupt
114 uint_t ai: 1; // 1=alarm interrupt
115 uint_t pi: 1; // 1=periodic interrupt
116 uint_t set: 1; // 1=blocked update
117 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
120 uint_t res: 4; // reserved
121 uint_t uf: 1; // 1=source of interrupt is update
122 uint_t af: 1; // 1=source of interrupt is alarm interrupt
123 uint_t pf: 1; // 1=source of interrupt is periodic interrupt
124 uint_t irq: 1; // 1=interrupt requested
125 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
128 uint_t res: 7; // reserved
129 uint_t val: 1; // 1=cmos ram data is OK
130 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
135 struct vm_device * thedev = NULL;
137 /*JRL: A hack and a fatal bug
138 static struct vm_device * demultiplex_timer_interrupt(uint_t period_us)
150 static uchar_t add_to(uchar_t * left, uchar_t * right, uchar_t bcd)
155 struct bcd_num * bl = (struct bcd_num *)left;
156 struct bcd_num * br = (struct bcd_num *)right;
160 carry = bl->bot / 0xa;
163 bl->top += carry + br->top;
164 carry = bl->top / 0xa;
182 static uchar_t days_in_month(struct vm_device *dev, uchar_t month, uchar_t bcd)
184 // This completely ignores Julian / Gregorian stuff right now
241 static void update_time(struct vm_device *dev, uint_t period_us)
243 struct nvram_internal * data = (struct nvram_internal *) (dev->private_data);
244 struct rtc_stata * stata = (struct rtc_stata *) &((data->mem_state[NVRAM_REG_STAT_A]));
245 struct rtc_statb * statb = (struct rtc_statb *) &((data->mem_state[NVRAM_REG_STAT_B]));
246 struct rtc_statc * statc = (struct rtc_statc *) &((data->mem_state[NVRAM_REG_STAT_C]));
247 //struct rtc_statd *statd = (struct rtc_statd *) &((data->mem_state[NVRAM_REG_STAT_D]));
248 uchar_t * sec = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC]);
249 uchar_t * min = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN]);
250 uchar_t * hour = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR]);
251 uchar_t * weekday = (uchar_t *) &(data->mem_state[NVRAM_REG_WEEK_DAY]);
252 uchar_t * monthday = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH_DAY]);
253 uchar_t * month = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH]);
254 uchar_t * year = (uchar_t *) &(data->mem_state[NVRAM_REG_YEAR]);
255 uchar_t * cent = (uchar_t *) &(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
256 uchar_t * seca = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC_ALARM]);
257 uchar_t * mina = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN_ALARM]);
258 uchar_t * houra = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR_ALARM]);
261 uchar_t bcd = (statb->dm == 1);
264 uint_t periodic_period;
266 //PrintDebug("nvram: sizeof(struct rtc_stata)=%d\n", sizeof(struct rtc_stata));
269 //PrintDebug("nvram: update_time\n",statb->pi);
271 // We will set these flags on exit
277 // We will reset us after one second
278 data->us += period_us;
279 // We will reset pus after one periodic_period
280 data->pus += period_us;
282 if (data->us > 1000000) {
284 carry = add_to(sec, &carry, bcd);
287 PrintDebug("nvram: somehow managed to get a carry in second update\n");
290 if ( (bcd && (*sec == 0x60)) ||
291 ((!bcd) && (*sec == 60))) {
296 carry = add_to(min, &carry, bcd);
298 PrintDebug("nvram: somehow managed to get a carry in minute update\n");
301 if ( (bcd && (*min == 0x60)) ||
302 ((!bcd) && (*min == 60))) {
311 uchar_t temp = ((bcd) ? 0x12 : 12);
312 add_to(&hour24, &temp, bcd);
317 carry = add_to(&hour24, &carry, bcd);
319 PrintDebug("nvram: somehow managed to get a carry in hour update\n");
322 if ( (bcd && (hour24 == 0x24)) ||
323 ((!bcd) && (hour24 == 24))) {
335 if ( (bcd && (hour24 < 0x12)) ||
336 ((!bcd) && (hour24 < 12))) {
342 *hour = (hour24 - 12) | 0x80;
345 struct bcd_num * n = (struct bcd_num *)hour;
358 // now see if we need to carry into the days and further
361 add_to(weekday, &carry, bcd);
363 *weekday %= 0x7; // same regardless of bcd
365 if ((*monthday) != days_in_month(dev, *month, bcd)) {
366 add_to(monthday, &carry, bcd);
371 add_to(month, &carry, bcd);
373 if ( (bcd && (*month == 0x13)) ||
374 ((!bcd) && (*month == 13))) {
375 *month = 1; // same for both
378 carry = add_to(year, &carry, bcd);
380 if ( (bcd && carry) ||
381 ((!bcd) && (*year == 100))) {
384 add_to(cent, &carry, bcd);
394 // OK, now check for the alarm, if it is set to interrupt
396 if ((*sec == *seca) && (*min == *mina) && (*hour == *houra)) {
398 PrintDebug("nvram: interrupt on alarm\n");
404 periodic_period = 1000000 / (65536 / (0x1 << stata->rate));
405 if (data->pus >= periodic_period) {
407 data->pus -= periodic_period;
408 PrintDebug("nvram: interrupt on periodic\n");
414 PrintDebug("nvram: interrupt on update\n");
417 statc->irq = (statc->pf || statc->af || statc->uf);
419 //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);
421 // Interrupt associated VM, if needed
423 PrintDebug("nvram: injecting interrupt\n");
424 dev->vm->vm_ops.raise_irq(dev->vm, NVRAM_RTC_IRQ);
430 /* JRL: This is completely broken...
431 void deliver_timer_interrupt_to_vmm(uint_t period_us)
433 struct vm_device * dev = demultiplex_timer_interrupt(period_us);
436 update_time(dev, period_us);
442 static int set_nvram_defaults(struct vm_device * dev)
444 struct nvram_internal * nvram_state = (struct nvram_internal *)dev->private_data;
448 // 2 1.44 MB floppy drives
451 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x44;
453 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x00;
457 // For old boot sequence style, do floppy first
459 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_OLD] = 0x10;
462 // For new boot sequence style, do floppy, cd, then hd
463 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x31;
464 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x20;
467 // For new boot sequence style, do cd, hd, floppy
468 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x23;
469 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x10;
472 // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
473 nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0x4f;
474 //nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0xf;
476 // Set conventional memory to 640K
477 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_HIGH] = 0x02;
478 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_LOW] = 0x80;
480 // Set extended memory to 15 MB
481 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_HIGH] = 0x3C;
482 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_LOW] = 0x00;
483 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= 0x3C;
484 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= 0x00;
486 // Set the extended memory beyond 16 MB to 128-16 MB
487 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH] = 0x7;
488 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW] = 0x00;
490 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH]= 0x00;
491 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW]= 0x00;
494 // This is the harddisk type.... Set accordingly...
495 nvram_state->mem_state[NVRAM_IBM_HD_DATA] = 0x20;
497 // Set the shutdown status gently
499 nvram_state->mem_state[NVRAM_REG_SHUTDOWN_STATUS] = 0x0;
503 // 00100110 = no update in progress, base=32768 Hz, rate = 1024 Hz
504 nvram_state->mem_state[NVRAM_REG_STAT_A] = 0x26;
507 // 00000100 = not setting, no interrupts, blocked rect signal, bcd mode, 24 hour, normal time
508 nvram_state->mem_state[NVRAM_REG_STAT_B] = 0x06;
512 // No IRQ requested, result not do to any source
513 nvram_state->mem_state[NVRAM_REG_STAT_C] = 0x00;
517 nvram_state->mem_state[NVRAM_REG_STAT_D] = 0x80;
520 // january 1, 2008, 00:00:00
521 nvram_state->mem_state[NVRAM_REG_MONTH] = 0x1;
522 nvram_state->mem_state[NVRAM_REG_MONTH_DAY] = 0x1;
523 nvram_state->mem_state[NVRAM_REG_WEEK_DAY] = 0x1;
524 nvram_state->mem_state[NVRAM_REG_YEAR] = 0x08;
527 nvram_state->pus = 0;
534 int nvram_reset_device(struct vm_device * dev)
536 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
538 PrintDebug("nvram: reset device\n");
541 data->dev_state = NVRAM_READY;
551 int nvram_start_device(struct vm_device *dev)
553 PrintDebug("nvram: start device\n");
558 int nvram_stop_device(struct vm_device *dev)
560 PrintDebug("nvram: stop device\n");
567 int nvram_write_reg_port(ushort_t port,
570 struct vm_device * dev)
572 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
574 memcpy(&(data->thereg), src, 1);
575 PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
581 int nvram_read_data_port(ushort_t port,
584 struct vm_device * dev)
586 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
590 memcpy(dst, &(data->mem_state[data->thereg]), 1);
592 PrintDebug("nvram_read_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
595 if (data->thereg == NVRAM_REG_STAT_A) {
596 data->mem_state[data->thereg] ^= 0x80; // toggle Update in progess
603 int nvram_write_data_port(ushort_t port,
606 struct vm_device * dev)
608 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
610 memcpy(&(data->mem_state[data->thereg]), src, 1);
612 PrintDebug("nvram_write_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
619 int nvram_init_device(struct vm_device * dev) {
621 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
623 PrintDebug("nvram: init_device\n");
625 memset(data->mem_state, 0, NVRAM_REG_MAX);
627 // Would read state here
628 set_nvram_defaults(dev);
630 nvram_reset_device(dev);
633 dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
634 dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
639 int nvram_deinit_device(struct vm_device *dev)
643 dev_unhook_io(dev, NVRAM_REG_PORT);
644 dev_unhook_io(dev, NVRAM_DATA_PORT);
646 nvram_reset_device(dev);
654 static struct vm_device_ops dev_ops = {
655 .init = nvram_init_device,
656 .deinit = nvram_deinit_device,
657 .reset = nvram_reset_device,
658 .start = nvram_start_device,
659 .stop = nvram_stop_device,
665 struct vm_device * create_nvram() {
666 struct nvram_internal * nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal) + 1000);
668 PrintDebug("nvram: internal at %x\n",nvram_state);
670 struct vm_device * device = create_device("NVRAM", &dev_ops, nvram_state);
672 if (thedev != NULL) {
673 PrintDebug("nvram: warning! overwriting thedev\n");