1 /* (c) 2008, Peter Dinda <pdinda@northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
5 #include <devices/generic.h>
6 #include <palacios/vmm.h>
7 #include <palacios/vmm_types.h>
14 #define PrintDebug(fmt, args...)
19 #define MEM_HOOKS 0 // not yet implmented in device model
20 #define IRQ_HOOKS 0 // not yet implemented in device model
22 struct generic_internal {
23 generic_port_range_type *port_ranges;
24 uint_t num_port_ranges;
25 generic_address_range_type *address_ranges;
26 uint_t num_address_ranges;
27 generic_irq_range_type *irq_ranges;
28 uint_t num_irq_ranges;
33 int generic_reset_device(struct vm_device * dev)
35 PrintDebug("generic: reset device\n");
45 int generic_start_device(struct vm_device *dev)
47 PrintDebug("generic: start device\n");
52 int generic_stop_device(struct vm_device *dev)
54 PrintDebug("generic: stop device\n");
61 int generic_write_port_passthrough(ushort_t port,
64 struct vm_device * dev)
68 PrintDebug("generic: writing 0x");
70 for (i = 0; i < length; i++) {
71 PrintDebug("%x", ((uchar_t*)src)[i]);
74 PrintDebug(" to port 0x%x ... ", port);
79 Out_Byte(port,((uchar_t*)src)[0]);
82 Out_Word(port,((ushort_t*)src)[0]);
85 Out_DWord(port,((uint_t*)src)[0]);
88 for (i = 0; i < length; i++) {
89 Out_Byte(port, ((uchar_t*)src)[i]);
94 PrintDebug(" done\n");
99 int generic_read_port_passthrough(ushort_t port,
102 struct vm_device * dev)
106 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
111 ((uchar_t*)src)[0] = In_Byte(port);
114 ((ushort_t*)src)[0] = In_Word(port);
117 ((uint_t*)src)[0] = In_DWord(port);
120 for (i = 0; i < length; i++) {
121 ((uchar_t*)src)[i] = In_Byte(port);
125 PrintDebug(" done ... read 0x");
127 for (i = 0; i < length; i++) {
128 PrintDebug("%x", ((uchar_t*)src)[i]);
136 int generic_write_port_ignore(ushort_t port,
139 struct vm_device * dev)
143 PrintDebug("generic: writing 0x");
145 for (i = 0; i < length; i++) {
146 PrintDebug("%x", ((uchar_t*)src)[i]);
149 PrintDebug(" to port 0x%x ... ", port);
151 PrintDebug(" ignored\n");
156 int generic_read_port_ignore(ushort_t port,
159 struct vm_device * dev)
162 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
164 memset((char*)src,0,length);
165 PrintDebug(" ignored (return zeroed buffer)\n");
172 int generic_interrupt(uint_t irq,
173 struct vm_device * dev)
175 PrintDebug("generic: interrupt 0x%x - injecting into VM\n", irq);
177 dev->vm->vm_ops.raise_irq(dev->vm, irq);
184 int generic_init_device(struct vm_device * dev)
186 struct generic_internal *state = (struct generic_internal *)(dev->private_data);
189 PrintDebug("generic: init_device\n");
191 // Would read state here
193 generic_reset_device(dev);
195 for (i = 0; i < state->num_port_ranges; i++) {
196 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");
199 for (j = state->port_ranges[i][0]; j <= state->port_ranges[i][1]; j++) {
200 if (state->port_ranges[i][2]==GENERIC_PRINT_AND_PASSTHROUGH) {
201 if (dev_hook_io(dev, j, &generic_read_port_passthrough, &generic_write_port_passthrough)) {
202 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", j);
204 } else if (state->port_ranges[i][2]==GENERIC_PRINT_AND_IGNORE) {
205 if (dev_hook_io(dev, j, &generic_read_port_ignore, &generic_write_port_ignore)) {
206 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", j);
211 PrintDebug("generic: hooking ports not supported\n");
216 for (i = 0; i < state->num_address_ranges; i++) {
217 PrintDebug("generic: hooking addresses 0x%x to 0x%x\n",state->address_ranges[i][0],state->address_ranges[i][1]);
220 if (dev_hook_mem(dev, state->address_ranges[i][0], state->address_ranges[i][1])) {
221 PrintDebug("generic: Can't hook addresses 0x%x to 0x%x (already hooked?)\n",
222 state->address_ranges[i][0], state->address_ranges[i][1]);
225 PrintDebug("generic: hooking addresses not supported\n");
230 for (i = 0; i < state->num_irq_ranges; i++) {
231 PrintDebug("generic: hooking irqs 0x%x to 0x%x\n",state->irq_ranges[i][0],state->irq_ranges[i][1]);
234 for (j = state->irq_ranges[i][0]; j <= state->irq_ranges[i][1]; j++) {
235 if (dev_hook_irq(dev, j, &generic_interrupt)) {
236 PrintDebug("generic: can't hook irq 0x%x (already hooked?)\n", j);
240 PrintDebug("generic: hooking irqs not supported\n");
248 int generic_deinit_device(struct vm_device *dev)
250 struct generic_internal *state = (struct generic_internal *)(dev->private_data);
253 PrintDebug("generic: deinit_device\n");
256 for (i = 0; i < state->num_irq_ranges; i++) {
257 PrintDebug("generic: unhooking irqs 0x%x to 0x%x\n", state->irq_ranges[i][0], state->irq_ranges[i][1]);
260 for (j = state->irq_ranges[i][0]; j <= state->irq_ranges[i][1]; j++) {
261 if (dev_unhook_irq(dev, j)) {
262 PrintDebug("generic: can't unhook irq 0x%x (already unhooked?)\n",j);
266 PrintDebug("generic: unhooking irqs not supported\n");
271 for (i = 0; i < state->num_address_ranges; i++) {
272 PrintDebug("generic: unhooking addresses 0x%x to 0x%x\n",state->address_ranges[i][0],state->address_ranges[i][1]);
275 if (dev_unhook_mem(dev, state->address_ranges[i][0], state->address_ranges[i][1])) {
276 PrintDebug("generic: Can't unhook addresses 0x%x to 0x%x (already unhooked?)\n",
277 state->address_ranges[i][0], state->address_ranges[i][1]);
280 PrintDebug("generic: unhooking addresses not supported\n");
285 for (i = 0; i < state->num_port_ranges; i++) {
286 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",state->port_ranges[i][0],state->port_ranges[i][1]);
289 for (j = state->port_ranges[i][0]; j <= state->port_ranges[i][1]; j++) {
290 if (dev_unhook_io(dev, j)) {
291 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", j);
295 PrintDebug("generic: unhooking ports not supported\n");
300 generic_reset_device(dev);
308 static struct vm_device_ops dev_ops = {
309 .init = generic_init_device,
310 .deinit = generic_deinit_device,
311 .reset = generic_reset_device,
312 .start = generic_start_device,
313 .stop = generic_stop_device,
319 struct vm_device *create_generic(generic_port_range_type port_ranges[],
320 generic_address_range_type address_ranges[],
321 generic_irq_range_type irq_ranges[])
323 struct generic_internal * generic_state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
325 uint_t num_port_ranges, num_address_ranges, num_irq_ranges;
328 if (port_ranges!=NULL) {
330 while (port_ranges[i]!=NULL &&
331 !(port_ranges[i][0]==0 && port_ranges[i][1]==0 && port_ranges[i][2]==0))
332 { num_port_ranges++; i++; }
336 num_address_ranges=0;
337 if (address_ranges!=NULL) {
339 while (address_ranges[i]!=NULL &&
340 !(address_ranges[i][0]==0 && address_ranges[i][1]==0 && address_ranges[i][2]==0))
341 { num_address_ranges++; i++; }
345 if (irq_ranges!=NULL) {
347 while (irq_ranges[i]!=NULL &&
348 !(irq_ranges[i][0]==0 && irq_ranges[i][1]==0 && irq_ranges[i][2]==0) )
349 { num_irq_ranges++; i++; }
353 generic_state->num_port_ranges = num_port_ranges;
355 if (num_port_ranges > 0) {
356 generic_state->port_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_port_ranges);
357 memcpy(generic_state->port_ranges, port_ranges, sizeof(generic_port_range_type) * num_port_ranges);
359 generic_state->port_ranges = NULL;
363 generic_state->num_address_ranges = num_address_ranges;
365 if (num_address_ranges > 0) {
366 generic_state->address_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_address_ranges);
367 memcpy(generic_state->address_ranges, address_ranges, sizeof(generic_address_range_type) * num_address_ranges);
369 generic_state->address_ranges = NULL;
373 generic_state->num_irq_ranges = num_irq_ranges;
375 if (num_irq_ranges > 0) {
376 generic_state->irq_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_irq_ranges);
377 memcpy(generic_state->irq_ranges, irq_ranges, sizeof(generic_irq_range_type) * num_port_ranges);
379 generic_state->irq_ranges = NULL;
382 struct vm_device *device = create_device("GENERIC", &dev_ops, generic_state);