1 /* (c) 2008, Peter Dinda <pdinda@northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
4 #include <devices/generic.h>
5 #include <palacios/vmm.h>
6 #include <palacios/vmm_types.h>
13 #define PrintDebug(fmt, args...)
18 #define MEM_HOOKS 0 // not yet implmented in device model
19 #define IRQ_HOOKS 0 // not yet implemented in device model
21 struct generic_internal {
22 generic_port_range_type *port_ranges;
23 uint_t num_port_ranges;
24 generic_address_range_type *address_ranges;
25 uint_t num_address_ranges;
26 generic_irq_range_type *irq_ranges;
27 uint_t num_irq_ranges;
33 #include <devices/ramdisk.h>
35 static struct ramdisk_t *ramdisk_state;
41 int generic_reset_device(struct vm_device * dev)
43 PrintDebug("generic: reset device\n");
53 int generic_start_device(struct vm_device *dev)
55 PrintDebug("generic: start device\n");
60 int generic_stop_device(struct vm_device *dev)
62 PrintDebug("generic: stop device\n");
69 int generic_write_port_passthrough(ushort_t port,
72 struct vm_device * dev)
76 PrintDebug("generic: writing 0x");
78 for (i = 0; i < length; i++) {
79 PrintDebug("%x", ((uchar_t*)src)[i]);
82 PrintDebug(" to port 0x%x ... ", port);
88 if (((port >= 0x170 && port <= 0x177) || port == 0x376 || port == 0x377)
89 && (dev->vm->cpu_mode == REAL)) {
92 err = ramdisk_state->eops.write_port(port, src, length, dev);
99 v3_outb(port,((uchar_t*)src)[0]);
102 v3_outw(port,((ushort_t*)src)[0]);
105 v3_outdw(port,((uint_t*)src)[0]);
108 for (i = 0; i < length; i++) {
109 v3_outb(port, ((uchar_t*)src)[i]);
115 PrintDebug(" done\n");
120 int generic_read_port_passthrough(ushort_t port,
123 struct vm_device * dev)
127 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
133 if (((port >= 0x170 && port <= 0x177) || port == 0x376 || port == 0x377)
134 && (dev->vm->cpu_mode == REAL)) {
136 err = ramdisk_state->eops.read_port(port, src, length, dev);
143 ((uchar_t*)src)[0] = v3_inb(port);
146 ((ushort_t*)src)[0] = v3_inw(port);
149 ((uint_t*)src)[0] = v3_indw(port);
152 for (i = 0; i < length; i++) {
153 ((uchar_t*)src)[i] = v3_inb(port);
160 PrintDebug(" done ... read 0x");
162 for (i = 0; i < length; i++) {
163 PrintDebug("%x", ((uchar_t*)src)[i]);
171 int generic_write_port_ignore(ushort_t port,
174 struct vm_device * dev)
178 PrintDebug("generic: writing 0x");
180 for (i = 0; i < length; i++) {
181 PrintDebug("%x", ((uchar_t*)src)[i]);
184 PrintDebug(" to port 0x%x ... ", port);
186 PrintDebug(" ignored\n");
192 if (((port >= 0x3e8 && port <= 0x3ef) ||
193 (port >= 0x2e8 && port <= 0x2ef))
194 && (dev->vm->cpu_mode == REAL)) {
196 err = ramdisk_state->eops.write_port_ignore(port, src, length, dev);
203 int generic_read_port_ignore(ushort_t port,
206 struct vm_device * dev)
209 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
211 memset((char*)src,0,length);
212 PrintDebug(" ignored (return zeroed buffer)\n");
218 if (((port >= 0x3e8 && port <= 0x3ef) ||
219 (port >= 0x2e8 && port <= 0x2ef))
220 && (dev->vm->cpu_mode == REAL)) {
222 err = ramdisk_state->eops.read_port_ignore(port, src, length, dev);
231 int generic_interrupt(uint_t irq,
232 struct vm_device * dev)
234 PrintDebug("generic: interrupt 0x%x - injecting into VM\n", irq);
236 dev->vm->vm_ops.raise_irq(dev->vm, irq);
243 int generic_init_device(struct vm_device * dev)
245 struct generic_internal *state = (struct generic_internal *)(dev->private_data);
248 PrintDebug("generic: init_device\n");
250 // Would read state here
252 generic_reset_device(dev);
254 for (i = 0; i < state->num_port_ranges; i++) {
255 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");
258 for (j = state->port_ranges[i][0]; j <= state->port_ranges[i][1]; j++) {
259 if (state->port_ranges[i][2]==GENERIC_PRINT_AND_PASSTHROUGH) {
260 if (dev_hook_io(dev, j, &generic_read_port_passthrough, &generic_write_port_passthrough)) {
261 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", j);
263 } else if (state->port_ranges[i][2]==GENERIC_PRINT_AND_IGNORE) {
264 if (dev_hook_io(dev, j, &generic_read_port_ignore, &generic_write_port_ignore)) {
265 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", j);
270 PrintDebug("generic: hooking ports not supported\n");
275 for (i = 0; i < state->num_address_ranges; i++) {
276 PrintDebug("generic: hooking addresses 0x%x to 0x%x\n",state->address_ranges[i][0],state->address_ranges[i][1]);
279 if (dev_hook_mem(dev, state->address_ranges[i][0], state->address_ranges[i][1])) {
280 PrintDebug("generic: Can't hook addresses 0x%x to 0x%x (already hooked?)\n",
281 state->address_ranges[i][0], state->address_ranges[i][1]);
284 PrintDebug("generic: hooking addresses not supported\n");
289 for (i = 0; i < state->num_irq_ranges; i++) {
290 PrintDebug("generic: hooking irqs 0x%x to 0x%x\n",state->irq_ranges[i][0],state->irq_ranges[i][1]);
293 for (j = state->irq_ranges[i][0]; j <= state->irq_ranges[i][1]; j++) {
294 if (dev_hook_irq(dev, j, &generic_interrupt)) {
295 PrintDebug("generic: can't hook irq 0x%x (already hooked?)\n", j);
299 PrintDebug("generic: hooking irqs not supported\n");
306 ramdisk_state->cops.init(ramdisk_state, dev);
313 int generic_deinit_device(struct vm_device *dev)
315 struct generic_internal *state = (struct generic_internal *)(dev->private_data);
318 PrintDebug("generic: deinit_device\n");
322 ramdisk_state->cops.close(ramdisk_state);
326 for (i = 0; i < state->num_irq_ranges; i++) {
327 PrintDebug("generic: unhooking irqs 0x%x to 0x%x\n", state->irq_ranges[i][0], state->irq_ranges[i][1]);
330 for (j = state->irq_ranges[i][0]; j <= state->irq_ranges[i][1]; j++) {
331 if (dev_unhook_irq(dev, j)) {
332 PrintDebug("generic: can't unhook irq 0x%x (already unhooked?)\n",j);
336 PrintDebug("generic: unhooking irqs not supported\n");
341 for (i = 0; i < state->num_address_ranges; i++) {
342 PrintDebug("generic: unhooking addresses 0x%x to 0x%x\n",state->address_ranges[i][0],state->address_ranges[i][1]);
345 if (dev_unhook_mem(dev, state->address_ranges[i][0], state->address_ranges[i][1])) {
346 PrintDebug("generic: Can't unhook addresses 0x%x to 0x%x (already unhooked?)\n",
347 state->address_ranges[i][0], state->address_ranges[i][1]);
350 PrintDebug("generic: unhooking addresses not supported\n");
355 for (i = 0; i < state->num_port_ranges; i++) {
356 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",state->port_ranges[i][0],state->port_ranges[i][1]);
359 for (j = state->port_ranges[i][0]; j <= state->port_ranges[i][1]; j++) {
360 if (dev_unhook_io(dev, j)) {
361 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", j);
365 PrintDebug("generic: unhooking ports not supported\n");
370 generic_reset_device(dev);
378 static struct vm_device_ops dev_ops = {
379 .init = generic_init_device,
380 .deinit = generic_deinit_device,
381 .reset = generic_reset_device,
382 .start = generic_start_device,
383 .stop = generic_stop_device,
389 struct vm_device *create_generic(generic_port_range_type port_ranges[],
390 generic_address_range_type address_ranges[],
391 generic_irq_range_type irq_ranges[])
393 struct generic_internal * generic_state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
395 uint_t num_port_ranges, num_address_ranges, num_irq_ranges;
398 if (port_ranges!=NULL) {
400 while (port_ranges[i]!=NULL &&
401 !(port_ranges[i][0]==0 && port_ranges[i][1]==0 && port_ranges[i][2]==0))
402 { num_port_ranges++; i++; }
406 num_address_ranges=0;
407 if (address_ranges!=NULL) {
409 while (address_ranges[i]!=NULL &&
410 !(address_ranges[i][0]==0 && address_ranges[i][1]==0 && address_ranges[i][2]==0))
411 { num_address_ranges++; i++; }
415 if (irq_ranges!=NULL) {
417 while (irq_ranges[i]!=NULL &&
418 !(irq_ranges[i][0]==0 && irq_ranges[i][1]==0 && irq_ranges[i][2]==0) )
419 { num_irq_ranges++; i++; }
423 generic_state->num_port_ranges = num_port_ranges;
425 if (num_port_ranges > 0) {
426 generic_state->port_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_port_ranges);
427 memcpy(generic_state->port_ranges, port_ranges, sizeof(generic_port_range_type) * num_port_ranges);
429 generic_state->port_ranges = NULL;
433 generic_state->num_address_ranges = num_address_ranges;
435 if (num_address_ranges > 0) {
436 generic_state->address_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_address_ranges);
437 memcpy(generic_state->address_ranges, address_ranges, sizeof(generic_address_range_type) * num_address_ranges);
439 generic_state->address_ranges = NULL;
443 generic_state->num_irq_ranges = num_irq_ranges;
445 if (num_irq_ranges > 0) {
446 generic_state->irq_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_irq_ranges);
447 memcpy(generic_state->irq_ranges, irq_ranges, sizeof(generic_irq_range_type) * num_port_ranges);
449 generic_state->irq_ranges = NULL;
454 ramdisk_state = create_ramdisk();
455 V3_ASSERT(ramdisk_state != NULL);
459 struct vm_device *device = create_device("GENERIC", &dev_ops, generic_state);