1 #include <devices/nvram.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmm_types.h>
5 extern struct vmm_os_hooks *os_hooks;
8 #define NVRAM_REG_PORT 0x70
9 #define NVRAM_DATA_PORT 0x71
13 typedef enum {NVRAM_READY,NVRAM_REG_POSTED} nvram_state_t;
16 #define NVRAM_REG_MAX 256
19 // These are borrowed from Bochs, which borrowed from
20 // Ralf Brown's interupt list
21 #define NVRAM_REG_SEC 0x00
22 #define NVRAM_REG_SEC_ALARM 0x01
23 #define NVRAM_REG_MIN 0x02
24 #define NVRAM_REG_MIN_ALARM 0x03
25 #define NVRAM_REG_HOUR 0x04
26 #define NVRAM_REG_HOUR_ALARM 0x05
27 #define NVRAM_REG_WEEK_DAY 0x06
28 #define NVRAM_REG_MONTH_DAY 0x07
29 #define NVRAM_REG_MONTH 0x08
30 #define NVRAM_REG_YEAR 0x09
31 #define NVRAM_REG_STAT_A 0x0a
32 #define NVRAM_REG_STAT_B 0x0b
33 #define NVRAM_REG_STAT_C 0x0c
34 #define NVRAM_REG_STAT_D 0x0d
35 #define NVRAM_REG_DIAGNOSTIC_STATUS 0x0e
36 #define NVRAM_REG_SHUTDOWN_STATUS 0x0f
37 #define NVRAM_REG_EQUIPMENT_BYTE 0x14
38 #define NVRAM_REG_CSUM_HIGH 0x2e
39 #define NVRAM_REG_CSUM_LOW 0x2f
40 #define NVRAM_REG_IBM_CENTURY_BYTE 0x32
41 #define NVRAM_REG_IBM_PS2_CENTURY_BYTE 0x37
45 struct nvram_internal {
46 nvram_state_t dev_state;
48 uchar_t mem_state[NVRAM_REG_MAX];
55 int nvram_reset_device(struct vm_device *dev)
57 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
59 data->dev_state = NVRAM_READY;
66 int nvram_init_device(struct vm_device *dev, struct vm_guest *vm)
68 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
70 memset(data->mem_state,0,NVRAM_REG_MAX);
72 nvram_reset_device(dev);
75 dev_mgr_hook_io(dev->vm,
81 dev_mgr_hook_io(dev->vm,
90 int nvram_deinit_device(struct vm_device *dev)
93 nvram_reset_device(dev);
95 dev_mgr_unhook_device(dev->vm,dev);
105 int nvram_start_device(struct vm_device *dev)
111 int nvram_stop_device(struct vm_device *dev)
118 int nvram_read_io_port(ushort_t port_read,
121 struct vm_device *dev)
123 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
128 memset(address,0,length);
130 case NVRAM_DATA_PORT:
131 memcpy(address,&(data->mem_state[data->thereg]),1);
140 int nvram_write_io_port(ushort_t port_written,
143 struct vm_device *dev)
145 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
147 switch (port_written) {
149 memcpy(&(data->thereg),address,1);
151 case NVRAM_DATA_PORT:
152 memcpy(&(data->mem_state[data->thereg]),address,1);
161 int nvram_read_mapped_memory(void *address_read,
164 struct vm_device *dev)
170 int nvram_write_mapped_memory(void *address_written,
173 struct vm_device *dev)
179 static struct vm_device nvram_template =
180 { .init_device = nvram_init_device,
181 .deinit_device = nvram_deinit_device,
182 .reset_device = nvram_reset_device,
183 .start_device = nvram_start_device,
184 .stop_device = nvram_stop_device,
185 .read_io_port = nvram_read_io_port,
186 .write_io_port = nvram_write_io_port,
187 .read_mapped_memory = nvram_read_mapped_memory,
188 .write_mapped_memory= nvram_write_mapped_memory,
193 struct vm_device *nvram_create()
195 struct vm_device *device = os_hooks->malloc(sizeof(struct vm_device));
197 *device = nvram_template;
199 device->private_data = os_hooks->malloc(sizeof(struct nvram_internal));