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;
34 int generic_reset_device(struct vm_device * dev)
36 PrintDebug("generic: reset device\n");
46 int generic_start_device(struct vm_device *dev)
48 PrintDebug("generic: start device\n");
53 int generic_stop_device(struct vm_device *dev)
55 PrintDebug("generic: stop device\n");
62 int generic_write_port_passthrough(ushort_t port,
65 struct vm_device * dev)
69 PrintDebug("generic: writing 0x");
71 for (i = 0; i < length; i++) {
72 PrintDebug("%x", ((uchar_t*)src)[i]);
75 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]);
93 PrintDebug(" done\n");
98 int generic_read_port_passthrough(ushort_t port,
101 struct vm_device * dev)
105 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
109 ((uchar_t*)src)[0] = In_Byte(port);
112 ((ushort_t*)src)[0] = In_Word(port);
115 ((uint_t*)src)[0] = In_DWord(port);
118 for (i = 0; i < length; i++) {
119 ((uchar_t*)src)[i] = In_Byte(port);
123 PrintDebug(" done ... read 0x");
125 for (i = 0; i < length; i++) {
126 PrintDebug("%x", ((uchar_t*)src)[i]);
134 int generic_write_port_ignore(ushort_t port,
137 struct vm_device * dev)
141 PrintDebug("generic: writing 0x");
143 for (i = 0; i < length; i++) {
144 PrintDebug("%x", ((uchar_t*)src)[i]);
147 PrintDebug(" to port 0x%x ... ", port);
149 PrintDebug(" ignored\n");
154 int generic_read_port_ignore(ushort_t port,
157 struct vm_device * dev)
160 PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
162 memset((char*)src,0,length);
163 PrintDebug(" ignored (return zeroed buffer)\n");
170 int generic_interrupt(uint_t irq,
171 struct vm_device * dev)
173 PrintDebug("generic: interrupt 0x%x - injecting into VM\n", irq);
175 dev->vm->vm_ops.raise_irq(dev->vm, irq);
182 int generic_init_device(struct vm_device * dev)
184 struct generic_internal *state = (struct generic_internal *)(dev->private_data);
187 PrintDebug("generic: init_device\n");
189 // Would read state here
191 generic_reset_device(dev);
193 for (i = 0; i < state->num_port_ranges; i++) {
194 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");
197 for (j = state->port_ranges[i][0]; j <= state->port_ranges[i][1]; j++) {
198 if (state->port_ranges[i][2]==GENERIC_PRINT_AND_PASSTHROUGH) {
199 if (dev_hook_io(dev, j, &generic_read_port_passthrough, &generic_write_port_passthrough)) {
200 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", j);
202 } else if (state->port_ranges[i][2]==GENERIC_PRINT_AND_IGNORE) {
203 if (dev_hook_io(dev, j, &generic_read_port_ignore, &generic_write_port_ignore)) {
204 PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", j);
209 PrintDebug("generic: hooking ports not supported\n");
214 for (i = 0; i < state->num_address_ranges; i++) {
215 PrintDebug("generic: hooking addresses 0x%x to 0x%x\n",state->address_ranges[i][0],state->address_ranges[i][1]);
218 if (dev_hook_mem(dev, state->address_ranges[i][0], state->address_ranges[i][1])) {
219 PrintDebug("generic: Can't hook addresses 0x%x to 0x%x (already hooked?)\n",
220 state->address_ranges[i][0], state->address_ranges[i][1]);
223 PrintDebug("generic: hooking addresses not supported\n");
228 for (i = 0; i < state->num_irq_ranges; i++) {
229 PrintDebug("generic: hooking irqs 0x%x to 0x%x\n",state->irq_ranges[i][0],state->irq_ranges[i][1]);
232 for (j = state->irq_ranges[i][0]; j <= state->irq_ranges[i][1]; j++) {
233 if (dev_hook_irq(dev, j, &generic_interrupt)) {
234 PrintDebug("generic: can't hook irq 0x%x (already hooked?)\n", j);
238 PrintDebug("generic: hooking irqs not supported\n");
246 int generic_deinit_device(struct vm_device *dev)
248 struct generic_internal *state = (struct generic_internal *)(dev->private_data);
251 PrintDebug("generic: deinit_device\n");
253 for (i = 0; i < state->num_irq_ranges; i++) {
254 PrintDebug("generic: unhooking irqs 0x%x to 0x%x\n", state->irq_ranges[i][0], state->irq_ranges[i][1]);
257 for (j = state->irq_ranges[i][0]; j <= state->irq_ranges[i][1]; j++) {
258 if (dev_unhook_irq(dev, j)) {
259 PrintDebug("generic: can't unhook irq 0x%x (already unhooked?)\n",j);
263 PrintDebug("generic: unhooking irqs not supported\n");
268 for (i = 0; i < state->num_address_ranges; i++) {
269 PrintDebug("generic: unhooking addresses 0x%x to 0x%x\n",state->address_ranges[i][0],state->address_ranges[i][1]);
272 if (dev_unhook_mem(dev, state->address_ranges[i][0], state->address_ranges[i][1])) {
273 PrintDebug("generic: Can't unhook addresses 0x%x to 0x%x (already unhooked?)\n",
274 state->address_ranges[i][0], state->address_ranges[i][1]);
277 PrintDebug("generic: unhooking addresses not supported\n");
282 for (i = 0; i < state->num_port_ranges; i++) {
283 PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",state->port_ranges[i][0],state->port_ranges[i][1]);
286 for (j = state->port_ranges[i][0]; j <= state->port_ranges[i][1]; j++) {
287 if (dev_unhook_io(dev, j)) {
288 PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", j);
292 PrintDebug("generic: unhooking ports not supported\n");
297 generic_reset_device(dev);
305 static struct vm_device_ops dev_ops = {
306 .init = generic_init_device,
307 .deinit = generic_deinit_device,
308 .reset = generic_reset_device,
309 .start = generic_start_device,
310 .stop = generic_stop_device,
316 struct vm_device *create_generic(generic_port_range_type port_ranges[],
317 generic_address_range_type address_ranges[],
318 generic_irq_range_type irq_ranges[])
320 struct generic_internal * generic_state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
322 uint_t num_port_ranges, num_address_ranges, num_irq_ranges;
325 if (port_ranges!=NULL) {
327 while (port_ranges[i]!=NULL &&
328 !(port_ranges[i][0]==0 && port_ranges[i][1]==0 && port_ranges[i][2]==0))
329 { num_port_ranges++; i++; }
333 num_address_ranges=0;
334 if (address_ranges!=NULL) {
336 while (address_ranges[i]!=NULL &&
337 !(address_ranges[i][0]==0 && address_ranges[i][1]==0 && address_ranges[i][2]==0))
338 { num_address_ranges++; i++; }
342 if (irq_ranges!=NULL) {
344 while (irq_ranges[i]!=NULL &&
345 !(irq_ranges[i][0]==0 && irq_ranges[i][1]==0 && irq_ranges[i][2]==0) )
346 { num_irq_ranges++; i++; }
350 generic_state->num_port_ranges = num_port_ranges;
352 if (num_port_ranges > 0) {
353 generic_state->port_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_port_ranges);
354 memcpy(generic_state->port_ranges, port_ranges, sizeof(generic_port_range_type) * num_port_ranges);
356 generic_state->port_ranges = NULL;
360 generic_state->num_address_ranges = num_address_ranges;
362 if (num_address_ranges > 0) {
363 generic_state->address_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_address_ranges);
364 memcpy(generic_state->address_ranges, address_ranges, sizeof(generic_address_range_type) * num_address_ranges);
366 generic_state->address_ranges = NULL;
370 generic_state->num_irq_ranges = num_irq_ranges;
372 if (num_irq_ranges > 0) {
373 generic_state->irq_ranges = V3_Malloc(sizeof(generic_address_range_type) * num_irq_ranges);
374 memcpy(generic_state->irq_ranges, irq_ranges, sizeof(generic_irq_range_type) * num_port_ranges);
376 generic_state->irq_ranges = NULL;
380 struct vm_device *device = create_device("GENERIC", &dev_ops, generic_state);