1 #include <devices/generic.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmm_types.h>
10 #define PrintDebug(fmt, args...)
15 #define MEM_HOOKS 0 // not yet implmented in device model
16 #define IRQ_HOOKS 0 // not yet implemented in device model
18 struct generic_internal {
19 generic_port_range_type *port_ranges;
20 uint_t num_port_ranges;
21 generic_address_range_type *address_ranges;
22 uint_t num_address_ranges;
23 generic_irq_range_type *irq_ranges;
24 uint_t num_irq_ranges;
30 #include <devices/ramdisk.h>
32 static struct ramdisk_t *ramdisk_state;
38 int generic_reset_device(struct vm_device * dev)
40 PrintDebug("generic: reset device\n");
50 int generic_start_device(struct vm_device *dev)
52 PrintDebug("generic: start device\n");
57 int generic_stop_device(struct vm_device *dev)
59 PrintDebug("generic: stop device\n");
66 int generic_write_port_passthrough(ushort_t port,
69 struct vm_device * dev)
73 PrintDebug("generic: writing 0x");
75 for (i = 0; i < length; i++) {
76 PrintDebug("%x", ((uchar_t*)src)[i]);
79 PrintDebug(" to port 0x%x ... ", port);
85 if (((port >= 0x170 && port <= 0x177) || port == 0x376 || port == 0x377)
86 && (dev->vm->cpu_mode == REAL)) {
89 err = ramdisk_state->eops.write_port(port, src, length, dev);
96 Out_Byte(port,((uchar_t*)src)[0]);
99 Out_Word(port,((ushort_t*)src)[0]);
102 Out_DWord(port,((uint_t*)src)[0]);
105 for (i = 0; i < length; i++) {
106 Out_Byte(port, ((uchar_t*)src)[i]);
112 PrintDebug(" done\n");
117 int generic_read_port_passthrough(ushort_t port,
120 struct vm_device * dev)
124 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
130 if (((port >= 0x170 && port <= 0x177) || port == 0x376 || port == 0x377)
131 && (dev->vm->cpu_mode == REAL)) {
133 err = ramdisk_state->eops.read_port(port, src, length, dev);
140 ((uchar_t*)src)[0] = In_Byte(port);
143 ((ushort_t*)src)[0] = In_Word(port);
146 ((uint_t*)src)[0] = In_DWord(port);
149 for (i = 0; i < length; i++) {
150 ((uchar_t*)src)[i] = In_Byte(port);
157 PrintDebug(" done ... read 0x");
159 for (i = 0; i < length; i++) {
160 PrintDebug("%x", ((uchar_t*)src)[i]);
168 int generic_write_port_ignore(ushort_t port,
171 struct vm_device * dev)
175 PrintDebug("generic: writing 0x");
177 for (i = 0; i < length; i++) {
178 PrintDebug("%x", ((uchar_t*)src)[i]);
181 PrintDebug(" to port 0x%x ... ", port);
183 PrintDebug(" ignored\n");
189 if (((port >= 0x3e8 && port <= 0x3ef) ||
190 (port >= 0x2e8 && port <= 0x2ef))
191 && (dev->vm->cpu_mode == REAL)) {
193 err = ramdisk_state->eops.write_port_ignore(port, src, length, dev);
200 int generic_read_port_ignore(ushort_t port,
203 struct vm_device * dev)
206 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
208 memset((char*)src,0,length);
209 PrintDebug(" ignored (return zeroed buffer)\n");
215 if (((port >= 0x3e8 && port <= 0x3ef) ||
216 (port >= 0x2e8 && port <= 0x2ef))
217 && (dev->vm->cpu_mode == REAL)) {
219 err = ramdisk_state->eops.read_port_ignore(port, src, length, dev);
228 int generic_interrupt(uint_t irq,
229 struct vm_device * dev)
231 PrintDebug("generic: interrupt 0x%x - injecting into VM\n", irq);
233 dev->vm->vm_ops.raise_irq(dev->vm, irq);
240 int generic_init_device(struct vm_device * dev)
242 struct generic_internal *state = (struct generic_internal *)(dev->private_data);
245 PrintDebug("generic: init_device\n");
247 // Would read state here
249 generic_reset_device(dev);
251 for (i = 0; i < state->num_port_ranges; i++) {
252 PrintDebug("generic: hooking ports 0x%x to 0x%x as %x\n", state->port_ranges[i][0], state->port_ranges[i][1], state->port_ranges[i][2]==GENERIC_PRINT_AND_PASSTHROUGH ? "print-and-passthrough" : "print-and-ignore");
255 for (j = state->port_ranges[i][0]; j <= state->port_ranges[i][1]; j++) {
256 if (state->port_ranges[i][2]==GENERIC_PRINT_AND_PASSTHROUGH) {
257 if (dev_hook_io(dev, j, &generic_read_port_passthrough, &generic_write_port_passthrough)) {
258 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", j);
260 } else if (state->port_ranges[i][2]==GENERIC_PRINT_AND_IGNORE) {
261 if (dev_hook_io(dev, j, &generic_read_port_ignore, &generic_write_port_ignore)) {
262 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", j);
267 PrintDebug("generic: hooking ports not supported\n");
272 for (i = 0; i < state->num_address_ranges; i++) {
273 PrintDebug("generic: hooking addresses 0x%x to 0x%x\n",state->address_ranges[i][0],state->address_ranges[i][1]);
276 if (dev_hook_mem(dev, state->address_ranges[i][0], state->address_ranges[i][1])) {
277 PrintDebug("generic: Can't hook addresses 0x%x to 0x%x (already hooked?)\n",
278 state->address_ranges[i][0], state->address_ranges[i][1]);
281 PrintDebug("generic: hooking addresses not supported\n");
286 for (i = 0; i < state->num_irq_ranges; i++) {
287 PrintDebug("generic: hooking irqs 0x%x to 0x%x\n",state->irq_ranges[i][0],state->irq_ranges[i][1]);
290 for (j = state->irq_ranges[i][0]; j <= state->irq_ranges[i][1]; j++) {
291 if (dev_hook_irq(dev, j, &generic_interrupt)) {
292 PrintDebug("generic: can't hook irq 0x%x (already hooked?)\n", j);
296 PrintDebug("generic: hooking irqs not supported\n");
303 ramdisk_state->cops.init(ramdisk_state, dev);
310 int generic_deinit_device(struct vm_device *dev)
312 struct generic_internal *state = (struct generic_internal *)(dev->private_data);
315 PrintDebug("generic: deinit_device\n");
319 ramdisk_state->cops.close(ramdisk_state);
323 for (i = 0; i < state->num_irq_ranges; i++) {
324 PrintDebug("generic: unhooking irqs 0x%x to 0x%x\n", state->irq_ranges[i][0], state->irq_ranges[i][1]);
327 for (j = state->irq_ranges[i][0]; j <= state->irq_ranges[i][1]; j++) {
328 if (dev_unhook_irq(dev, j)) {
329 PrintDebug("generic: can't unhook irq 0x%x (already unhooked?)\n",j);
333 PrintDebug("generic: unhooking irqs not supported\n");
338 for (i = 0; i < state->num_address_ranges; i++) {
339 PrintDebug("generic: unhooking addresses 0x%x to 0x%x\n",state->address_ranges[i][0],state->address_ranges[i][1]);
342 if (dev_unhook_mem(dev, state->address_ranges[i][0], state->address_ranges[i][1])) {
343 PrintDebug("generic: Can't unhook addresses 0x%x to 0x%x (already unhooked?)\n",
344 state->address_ranges[i][0], state->address_ranges[i][1]);
347 PrintDebug("generic: unhooking addresses not supported\n");
352 for (i = 0; i < state->num_port_ranges; i++) {
353 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",state->port_ranges[i][0],state->port_ranges[i][1]);
356 for (j = state->port_ranges[i][0]; j <= state->port_ranges[i][1]; j++) {
357 if (dev_unhook_io(dev, j)) {
358 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", j);
362 PrintDebug("generic: unhooking ports not supported\n");
367 generic_reset_device(dev);
375 static struct vm_device_ops dev_ops = {
376 .init = generic_init_device,
377 .deinit = generic_deinit_device,
378 .reset = generic_reset_device,
379 .start = generic_start_device,
380 .stop = generic_stop_device,
386 struct vm_device *create_generic(generic_port_range_type port_ranges[],
387 generic_address_range_type address_ranges[],
388 generic_irq_range_type irq_ranges[])
390 struct generic_internal * generic_state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
392 uint_t num_port_ranges, num_address_ranges, num_irq_ranges;
395 if (port_ranges!=NULL) {
397 while (port_ranges[i]!=NULL &&
398 !(port_ranges[i][0]==0 && port_ranges[i][1]==0 && port_ranges[i][2]==0))
399 { num_port_ranges++; i++; }
403 num_address_ranges=0;
404 if (address_ranges!=NULL) {
406 while (address_ranges[i]!=NULL &&
407 !(address_ranges[i][0]==0 && address_ranges[i][1]==0 && address_ranges[i][2]==0))
408 { num_address_ranges++; i++; }
412 if (irq_ranges!=NULL) {
414 while (irq_ranges[i]!=NULL &&
415 !(irq_ranges[i][0]==0 && irq_ranges[i][1]==0 && irq_ranges[i][2]==0) )
416 { num_irq_ranges++; i++; }
420 generic_state->num_port_ranges = num_port_ranges;
422 if (num_port_ranges > 0) {
423 generic_state->port_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_port_ranges);
424 memcpy(generic_state->port_ranges, port_ranges, sizeof(generic_port_range_type) * num_port_ranges);
426 generic_state->port_ranges = NULL;
430 generic_state->num_address_ranges = num_address_ranges;
432 if (num_address_ranges > 0) {
433 generic_state->address_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_address_ranges);
434 memcpy(generic_state->address_ranges, address_ranges, sizeof(generic_address_range_type) * num_address_ranges);
436 generic_state->address_ranges = NULL;
440 generic_state->num_irq_ranges = num_irq_ranges;
442 if (num_irq_ranges > 0) {
443 generic_state->irq_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_irq_ranges);
444 memcpy(generic_state->irq_ranges, irq_ranges, sizeof(generic_irq_range_type) * num_port_ranges);
446 generic_state->irq_ranges = NULL;
451 ramdisk_state = create_ramdisk();
452 V3_ASSERT(ramdisk_state != NULL);
456 struct vm_device *device = create_device("GENERIC", &dev_ops, generic_state);