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))) ;
142 static uchar_t add_to(uchar_t * left, uchar_t * right, uchar_t bcd) {
146 struct bcd_num * bl = (struct bcd_num *)left;
147 struct bcd_num * br = (struct bcd_num *)right;
151 carry = bl->bot / 0xa;
154 bl->top += carry + br->top;
155 carry = bl->top / 0xa;
172 static uchar_t days_in_month(struct vm_device * dev, uchar_t month, uchar_t bcd) {
173 // This completely ignores Julian / Gregorian stuff right now
230 static void update_time(struct vm_device * dev, uint_t period_us) {
231 struct nvram_internal * data = (struct nvram_internal *) (dev->private_data);
232 struct rtc_stata * stata = (struct rtc_stata *) &((data->mem_state[NVRAM_REG_STAT_A]));
233 struct rtc_statb * statb = (struct rtc_statb *) &((data->mem_state[NVRAM_REG_STAT_B]));
234 struct rtc_statc * statc = (struct rtc_statc *) &((data->mem_state[NVRAM_REG_STAT_C]));
235 //struct rtc_statd *statd = (struct rtc_statd *) &((data->mem_state[NVRAM_REG_STAT_D]));
236 uchar_t * sec = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC]);
237 uchar_t * min = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN]);
238 uchar_t * hour = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR]);
239 uchar_t * weekday = (uchar_t *) &(data->mem_state[NVRAM_REG_WEEK_DAY]);
240 uchar_t * monthday = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH_DAY]);
241 uchar_t * month = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH]);
242 uchar_t * year = (uchar_t *) &(data->mem_state[NVRAM_REG_YEAR]);
243 uchar_t * cent = (uchar_t *) &(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
244 uchar_t * seca = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC_ALARM]);
245 uchar_t * mina = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN_ALARM]);
246 uchar_t * houra = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR_ALARM]);
249 uchar_t bcd = (statb->dm == 1);
252 uint_t periodic_period;
254 //PrintDebug("nvram: sizeof(struct rtc_stata)=%d\n", sizeof(struct rtc_stata));
257 //PrintDebug("nvram: update_time\n",statb->pi);
259 // We will set these flags on exit
265 // We will reset us after one second
266 data->us += period_us;
267 // We will reset pus after one periodic_period
268 data->pus += period_us;
270 if (data->us > 1000000) {
272 carry = add_to(sec, &carry, bcd);
275 PrintDebug("nvram: somehow managed to get a carry in second update\n");
278 if ( (bcd && (*sec == 0x60)) ||
279 ((!bcd) && (*sec == 60))) {
284 carry = add_to(min, &carry, bcd);
286 PrintDebug("nvram: somehow managed to get a carry in minute update\n");
289 if ( (bcd && (*min == 0x60)) ||
290 ((!bcd) && (*min == 60))) {
299 uchar_t temp = ((bcd) ? 0x12 : 12);
300 add_to(&hour24, &temp, bcd);
305 carry = add_to(&hour24, &carry, bcd);
307 PrintDebug("nvram: somehow managed to get a carry in hour update\n");
310 if ( (bcd && (hour24 == 0x24)) ||
311 ((!bcd) && (hour24 == 24))) {
323 if ( (bcd && (hour24 < 0x12)) ||
324 ((!bcd) && (hour24 < 12))) {
330 *hour = (hour24 - 12) | 0x80;
333 struct bcd_num * n = (struct bcd_num *)hour;
346 // now see if we need to carry into the days and further
349 add_to(weekday, &carry, bcd);
351 *weekday %= 0x7; // same regardless of bcd
353 if ((*monthday) != days_in_month(dev, *month, bcd)) {
354 add_to(monthday, &carry, bcd);
359 add_to(month, &carry, bcd);
361 if ( (bcd && (*month == 0x13)) ||
362 ((!bcd) && (*month == 13))) {
363 *month = 1; // same for both
366 carry = add_to(year, &carry, bcd);
368 if ( (bcd && carry) ||
369 ((!bcd) && (*year == 100))) {
372 add_to(cent, &carry, bcd);
382 // OK, now check for the alarm, if it is set to interrupt
384 if ((*sec == *seca) && (*min == *mina) && (*hour == *houra)) {
386 PrintDebug("nvram: interrupt on alarm\n");
392 periodic_period = 1000000 / (65536 / (0x1 << stata->rate));
393 if (data->pus >= periodic_period) {
395 data->pus -= periodic_period;
396 PrintDebug("nvram: interrupt on periodic\n");
402 PrintDebug("nvram: interrupt on update\n");
405 statc->irq = (statc->pf || statc->af || statc->uf);
407 //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);
409 // Interrupt associated VM, if needed
411 PrintDebug("nvram: injecting interrupt\n");
412 v3_raise_irq(dev->vm, NVRAM_RTC_IRQ);
417 static int handle_timer_event(struct guest_info * info,
418 struct v3_timer_event * evt,
421 struct vm_device * dev = (struct vm_device *)priv_data;
424 update_time(dev, evt->period_us);
430 static int set_nvram_defaults(struct vm_device * dev) {
431 struct guest_info * info = dev->vm;
432 struct nvram_internal * nvram_state = (struct nvram_internal *)dev->private_data;
435 * The amount of ram in the system is stored in info->mem_size
436 * We need to reflect that value correctly here
438 PrintError("TODO: Set the nvram memory register to reflect info->mem_size (%p)\n", (void *)(info->mem_size));
442 // 2 1.44 MB floppy drives
445 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x44;
447 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x00;
451 // For old boot sequence style, do floppy first
453 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_OLD] = 0x10;
456 // For new boot sequence style, do floppy, cd, then hd
457 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x31;
458 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x20;
461 // For new boot sequence style, do cd, hd, floppy
462 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x23;
463 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x10;
466 // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
467 nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0x4f;
468 //nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0xf;
470 // Set conventional memory to 640K
471 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_HIGH] = 0x02;
472 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_LOW] = 0x80;
474 // Set extended memory to 15 MB
475 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_HIGH] = 0x3C;
476 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_LOW] = 0x00;
477 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= 0x3C;
478 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= 0x00;
480 // Set the extended memory beyond 16 MB to 128-16 MB
481 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH] = 0x7;
482 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW] = 0x00;
484 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH]= 0x00;
485 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW]= 0x00;
488 // This is the harddisk type.... Set accordingly...
489 nvram_state->mem_state[NVRAM_IBM_HD_DATA] = 0x20;
491 // Set the shutdown status gently
493 nvram_state->mem_state[NVRAM_REG_SHUTDOWN_STATUS] = 0x0;
497 // 00100110 = no update in progress, base=32768 Hz, rate = 1024 Hz
498 nvram_state->mem_state[NVRAM_REG_STAT_A] = 0x26;
501 // 00000100 = not setting, no interrupts, blocked rect signal, bcd mode, 24 hour, normal time
502 nvram_state->mem_state[NVRAM_REG_STAT_B] = 0x06;
506 // No IRQ requested, result not do to any source
507 nvram_state->mem_state[NVRAM_REG_STAT_C] = 0x00;
511 nvram_state->mem_state[NVRAM_REG_STAT_D] = 0x80;
514 // january 1, 2008, 00:00:00
515 nvram_state->mem_state[NVRAM_REG_MONTH] = 0x1;
516 nvram_state->mem_state[NVRAM_REG_MONTH_DAY] = 0x1;
517 nvram_state->mem_state[NVRAM_REG_WEEK_DAY] = 0x1;
518 nvram_state->mem_state[NVRAM_REG_YEAR] = 0x08;
521 nvram_state->pus = 0;
527 static int nvram_reset_device(struct vm_device * dev) {
528 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
530 PrintDebug("nvram: reset device\n");
532 data->dev_state = NVRAM_READY;
542 static int nvram_start_device(struct vm_device * dev) {
543 PrintDebug("nvram: start device\n");
548 static int nvram_stop_device(struct vm_device * dev) {
549 PrintDebug("nvram: stop device\n");
556 static int nvram_write_reg_port(ushort_t port,
559 struct vm_device * dev) {
560 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
562 memcpy(&(data->thereg), src, 1);
563 PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
569 static int nvram_read_data_port(ushort_t port,
572 struct vm_device * dev) {
573 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
575 memcpy(dst, &(data->mem_state[data->thereg]), 1);
577 PrintDebug("nvram_read_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
580 if (data->thereg == NVRAM_REG_STAT_A) {
581 data->mem_state[data->thereg] ^= 0x80; // toggle Update in progess
588 static int nvram_write_data_port(ushort_t port,
591 struct vm_device * dev) {
592 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
594 memcpy(&(data->mem_state[data->thereg]), src, 1);
596 PrintDebug("nvram_write_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
603 static int nvram_init_device(struct vm_device * dev) {
605 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
607 PrintDebug("nvram: init_device\n");
609 memset(data->mem_state, 0, NVRAM_REG_MAX);
611 // Would read state here
612 set_nvram_defaults(dev);
616 nvram_reset_device(dev);
619 v3_dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
620 v3_dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
622 v3_hook_host_event(dev->vm, HOST_TIMER_EVT, V3_HOST_EVENT_HANDLER(handle_timer_event), dev);
627 static int nvram_deinit_device(struct vm_device * dev) {
628 v3_dev_unhook_io(dev, NVRAM_REG_PORT);
629 v3_dev_unhook_io(dev, NVRAM_DATA_PORT);
631 nvram_reset_device(dev);
639 static struct vm_device_ops dev_ops = {
640 .init = nvram_init_device,
641 .deinit = nvram_deinit_device,
642 .reset = nvram_reset_device,
643 .start = nvram_start_device,
644 .stop = nvram_stop_device,
650 struct vm_device * v3_create_nvram() {
651 struct nvram_internal * nvram_state = NULL;
653 nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal) + 1000);
655 PrintDebug("nvram: internal at %x\n", nvram_state);
657 struct vm_device * device = v3_create_device("NVRAM", &dev_ops, nvram_state);