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 static struct vm_device * demultiplex_timer_interrupt(uint_t period_us)
148 static uchar_t add_to(uchar_t * left, uchar_t * right, uchar_t bcd)
153 struct bcd_num * bl = (struct bcd_num *)left;
154 struct bcd_num * br = (struct bcd_num *)right;
158 carry = bl->bot / 0xa;
161 bl->top += carry + br->top;
162 carry = bl->top / 0xa;
180 static uchar_t days_in_month(struct vm_device *dev, uchar_t month, uchar_t bcd)
182 // This completely ignores Julian / Gregorian stuff right now
239 static void update_time(struct vm_device *dev, uint_t period_us)
241 struct nvram_internal * data = (struct nvram_internal *) (dev->private_data);
242 struct rtc_stata * stata = (struct rtc_stata *) &((data->mem_state[NVRAM_REG_STAT_A]));
243 struct rtc_statb * statb = (struct rtc_statb *) &((data->mem_state[NVRAM_REG_STAT_B]));
244 struct rtc_statc * statc = (struct rtc_statc *) &((data->mem_state[NVRAM_REG_STAT_C]));
245 //struct rtc_statd *statd = (struct rtc_statd *) &((data->mem_state[NVRAM_REG_STAT_D]));
246 uchar_t * sec = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC]);
247 uchar_t * min = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN]);
248 uchar_t * hour = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR]);
249 uchar_t * weekday = (uchar_t *) &(data->mem_state[NVRAM_REG_WEEK_DAY]);
250 uchar_t * monthday = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH_DAY]);
251 uchar_t * month = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH]);
252 uchar_t * year = (uchar_t *) &(data->mem_state[NVRAM_REG_YEAR]);
253 uchar_t * cent = (uchar_t *) &(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
254 uchar_t * seca = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC_ALARM]);
255 uchar_t * mina = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN_ALARM]);
256 uchar_t * houra = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR_ALARM]);
259 uchar_t bcd = (statb->dm == 1);
262 uint_t periodic_period;
264 //PrintDebug("nvram: sizeof(struct rtc_stata)=%d\n", sizeof(struct rtc_stata));
267 //PrintDebug("nvram: update_time\n",statb->pi);
269 // We will set these flags on exit
275 // We will reset us after one second
276 data->us += period_us;
277 // We will reset pus after one periodic_period
278 data->pus += period_us;
280 if (data->us > 1000000) {
282 carry = add_to(sec, &carry, bcd);
285 PrintDebug("nvram: somehow managed to get a carry in second update\n");
288 if ( (bcd && (*sec == 0x60)) ||
289 ((!bcd) && (*sec == 60))) {
294 carry = add_to(min, &carry, bcd);
296 PrintDebug("nvram: somehow managed to get a carry in minute update\n");
299 if ( (bcd && (*min == 0x60)) ||
300 ((!bcd) && (*min == 60))) {
309 uchar_t temp = ((bcd) ? 0x12 : 12);
310 add_to(&hour24, &temp, bcd);
315 carry = add_to(&hour24, &carry, bcd);
317 PrintDebug("nvram: somehow managed to get a carry in hour update\n");
320 if ( (bcd && (hour24 == 0x24)) ||
321 ((!bcd) && (hour24 == 24))) {
333 if ( (bcd && (hour24 < 0x12)) ||
334 ((!bcd) && (hour24 < 12))) {
340 *hour = (hour24 - 12) | 0x80;
343 struct bcd_num * n = (struct bcd_num *)hour;
356 // now see if we need to carry into the days and further
359 add_to(weekday, &carry, bcd);
361 *weekday %= 0x7; // same regardless of bcd
363 if ((*monthday) != days_in_month(dev, *month, bcd)) {
364 add_to(monthday, &carry, bcd);
369 add_to(month, &carry, bcd);
371 if ( (bcd && (*month == 0x13)) ||
372 ((!bcd) && (*month == 13))) {
373 *month = 1; // same for both
376 carry = add_to(year, &carry, bcd);
378 if ( (bcd && carry) ||
379 ((!bcd) && (*year == 100))) {
382 add_to(cent, &carry, bcd);
392 // OK, now check for the alarm, if it is set to interrupt
394 if ((*sec == *seca) && (*min == *mina) && (*hour == *houra)) {
396 PrintDebug("nvram: interrupt on alarm\n");
402 periodic_period = 1000000 / (65536 / (0x1 << stata->rate));
403 if (data->pus >= periodic_period) {
405 data->pus -= periodic_period;
406 PrintDebug("nvram: interrupt on periodic\n");
412 PrintDebug("nvram: interrupt on update\n");
415 statc->irq = (statc->pf || statc->af || statc->uf);
417 //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);
419 // Interrupt associated VM, if needed
421 PrintDebug("nvram: injecting interrupt\n");
422 dev->vm->vm_ops.raise_irq(dev->vm, NVRAM_RTC_IRQ);
429 void deliver_timer_interrupt_to_vmm(uint_t period_us)
431 struct vm_device * dev = demultiplex_timer_interrupt(period_us);
434 update_time(dev, period_us);
440 static int set_nvram_defaults(struct vm_device * dev)
442 struct nvram_internal * nvram_state = (struct nvram_internal *)dev->private_data;
446 // 2 1.44 MB floppy drives
449 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x44;
451 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x00;
455 // For old boot sequence style, do floppy first
457 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_OLD] = 0x10;
460 // For new boot sequence style, do floppy, cd, then hd
461 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x31;
462 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x20;
465 // For new boot sequence style, do cd, hd, floppy
466 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x23;
467 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x10;
470 // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
471 nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0x4f;
472 //nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0xf;
474 // Set conventional memory to 640K
475 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_HIGH] = 0x02;
476 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_LOW] = 0x80;
478 // Set extended memory to 15 MB
479 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_HIGH] = 0x3C;
480 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_LOW] = 0x00;
481 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= 0x3C;
482 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= 0x00;
484 // Set the extended memory beyond 16 MB to 128-16 MB
485 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH] = 0x7;
486 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW] = 0x00;
488 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH]= 0x00;
489 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW]= 0x00;
492 // This is the harddisk type.... Set accordingly...
493 nvram_state->mem_state[NVRAM_IBM_HD_DATA] = 0x20;
495 // Set the shutdown status gently
497 nvram_state->mem_state[NVRAM_REG_SHUTDOWN_STATUS] = 0x0;
501 // 00100110 = no update in progress, base=32768 Hz, rate = 1024 Hz
502 nvram_state->mem_state[NVRAM_REG_STAT_A] = 0x26;
505 // 00000100 = not setting, no interrupts, blocked rect signal, bcd mode, 24 hour, normal time
506 nvram_state->mem_state[NVRAM_REG_STAT_B] = 0x06;
510 // No IRQ requested, result not do to any source
511 nvram_state->mem_state[NVRAM_REG_STAT_C] = 0x00;
515 nvram_state->mem_state[NVRAM_REG_STAT_D] = 0x80;
518 // january 1, 2008, 00:00:00
519 nvram_state->mem_state[NVRAM_REG_MONTH] = 0x1;
520 nvram_state->mem_state[NVRAM_REG_MONTH_DAY] = 0x1;
521 nvram_state->mem_state[NVRAM_REG_WEEK_DAY] = 0x1;
522 nvram_state->mem_state[NVRAM_REG_YEAR] = 0x08;
525 nvram_state->pus = 0;
532 int nvram_reset_device(struct vm_device * dev)
534 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
536 PrintDebug("nvram: reset device\n");
539 data->dev_state = NVRAM_READY;
549 int nvram_start_device(struct vm_device *dev)
551 PrintDebug("nvram: start device\n");
556 int nvram_stop_device(struct vm_device *dev)
558 PrintDebug("nvram: stop device\n");
565 int nvram_write_reg_port(ushort_t port,
568 struct vm_device * dev)
570 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
572 memcpy(&(data->thereg), src, 1);
573 PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
579 int nvram_read_data_port(ushort_t port,
582 struct vm_device * dev)
584 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
588 memcpy(dst, &(data->mem_state[data->thereg]), 1);
590 PrintDebug("nvram_read_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
593 if (data->thereg == NVRAM_REG_STAT_A) {
594 data->mem_state[data->thereg] ^= 0x80; // toggle Update in progess
601 int nvram_write_data_port(ushort_t port,
604 struct vm_device * dev)
606 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
608 memcpy(&(data->mem_state[data->thereg]), src, 1);
610 PrintDebug("nvram_write_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
617 int nvram_init_device(struct vm_device * dev) {
619 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
621 PrintDebug("nvram: init_device\n");
623 memset(data->mem_state, 0, NVRAM_REG_MAX);
625 // Would read state here
626 set_nvram_defaults(dev);
628 nvram_reset_device(dev);
631 dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
632 dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
637 int nvram_deinit_device(struct vm_device *dev)
641 dev_unhook_io(dev, NVRAM_REG_PORT);
642 dev_unhook_io(dev, NVRAM_DATA_PORT);
644 nvram_reset_device(dev);
652 static struct vm_device_ops dev_ops = {
653 .init = nvram_init_device,
654 .deinit = nvram_deinit_device,
655 .reset = nvram_reset_device,
656 .start = nvram_start_device,
657 .stop = nvram_stop_device,
663 struct vm_device * create_nvram() {
664 struct nvram_internal * nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal) + 1000);
666 PrintDebug("nvram: internal at %x\n",nvram_state);
668 struct vm_device * device = create_device("NVRAM", &dev_ops, nvram_state);
670 if (thedev != NULL) {
671 PrintDebug("nvram: warning! overwriting thedev\n");