Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Update to the device framework.
[palacios.git] / palacios / src / devices / generic.c
1 /* 
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2008, Peter Dinda <pdinda@northwestern.edu> 
11  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Peter Dinda <pdinda@northwestern.edu>
15  * Contributor: 2008, Jack Lange <jarusl@cs.northwestern.edu>
16  *        
17  *
18  * This is free software.  You are permitted to use,
19  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20  */
21
22 #include <devices/generic.h>
23 #include <palacios/vmm.h>
24 #include <palacios/vmm_types.h>
25 #include <palacios/vmm_list.h>
26
27
28
29 #ifndef DEBUG_GENERIC
30 #undef PrintDebug
31 #define PrintDebug(fmt, args...)
32 #endif
33
34
35 #define PORT_HOOKS 1
36
37
38 struct generic_internal {
39     struct list_head port_list;
40     uint_t num_port_ranges;
41
42 };
43
44
45 struct port_range {
46     uint_t start;
47     uint_t end;
48     uint_t type;
49     struct list_head range_link;
50 };
51
52
53
54
55 static int generic_reset_device(struct vm_device * dev) {
56     PrintDebug("generic: reset device\n");
57     return 0;
58 }
59
60
61
62
63
64 static int generic_start_device(struct vm_device * dev) {
65     PrintDebug("generic: start device\n");
66     return 0;
67 }
68
69
70 static int generic_stop_device(struct vm_device * dev) {
71     PrintDebug("generic: stop device\n");
72     return 0;
73 }
74
75
76
77
78 static int generic_write_port_passthrough(ushort_t port,
79                                           void * src, 
80                                           uint_t length,
81                                           struct vm_device * dev) {
82     uint_t i;
83
84     PrintDebug("generic: writing 0x");
85
86     for (i = 0; i < length; i++) { 
87         PrintDebug("%x", ((uchar_t*)src)[i]);
88     }
89   
90     PrintDebug(" to port 0x%x ... ", port);
91
92
93     switch (length) {
94         case 1:
95             v3_outb(port,((uchar_t*)src)[0]);
96             break;
97         case 2:
98             v3_outw(port,((ushort_t*)src)[0]);
99             break;
100         case 4:
101             v3_outdw(port,((uint_t*)src)[0]);
102             break;
103         default:
104             for (i = 0; i < length; i++) { 
105                 v3_outb(port, ((uchar_t*)src)[i]);
106             }
107     } //switch length
108
109
110     PrintDebug(" done\n");
111   
112     return length;
113 }
114
115 static int generic_read_port_passthrough(ushort_t port,
116                                          void * src, 
117                                          uint_t length,
118                                          struct vm_device * dev) {
119     uint_t i;
120
121     PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
122
123
124     switch (length) {
125         case 1:
126             ((uchar_t*)src)[0] = v3_inb(port);
127             break;
128         case 2:
129             ((ushort_t*)src)[0] = v3_inw(port);
130             break;
131         case 4:
132             ((uint_t*)src)[0] = v3_indw(port);
133             break;
134         default:
135             for (i = 0; i < length; i++) { 
136                 ((uchar_t*)src)[i] = v3_inb(port);
137             }
138     }//switch length
139
140     PrintDebug(" done ... read 0x");
141
142     for (i = 0; i < length; i++) { 
143         PrintDebug("%x", ((uchar_t*)src)[i]);
144     }
145
146     PrintDebug("\n");
147
148     return length;
149 }
150
151 static int generic_write_port_ignore(ushort_t port,
152                                      void * src, 
153                                      uint_t length,
154                                      struct vm_device * dev) {
155     uint_t i;
156
157     PrintDebug("generic: writing 0x");
158
159     for (i = 0; i < length; i++) { 
160         PrintDebug("%x", ((uchar_t*)src)[i]);
161     }
162   
163     PrintDebug(" to port 0x%x ... ignored\n", port);
164  
165     return length;
166 }
167
168 static int generic_read_port_ignore(ushort_t port,
169                                     void * src, 
170                                     uint_t length,
171                                     struct vm_device * dev) {
172
173     PrintDebug("generic: reading 0x%x bytes from port 0x%x ...", length, port);
174
175     memset((char*)src, 0, length);
176     PrintDebug(" ignored (return zeroed buffer)\n");
177
178     return length;
179 }
180
181
182
183
184
185 static int generic_free(struct vm_device * dev) {
186     struct generic_internal * state = (struct generic_internal *)(dev->private_data);
187
188
189     PrintDebug("generic: deinit_device\n");
190
191     if (PORT_HOOKS) {
192         struct port_range * tmp;
193         struct port_range * cur;
194     
195         list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
196             uint_t i;
197
198             PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",
199                        cur->start, cur->end);
200                 
201             for (i = cur->start; i <= cur->end; i++) {
202                 if (v3_dev_unhook_io(dev, i)) {
203                     PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", i);
204                 }
205             }
206
207             list_del(&(cur->range_link));
208             state->num_port_ranges--;
209             V3_Free(cur);
210         }
211     } else {
212         PrintDebug("generic: unhooking ports not supported\n");
213     }
214
215
216
217     generic_reset_device(dev);
218     return 0;
219 }
220
221
222
223
224
225 static struct v3_device_ops dev_ops = { 
226     .free = generic_free, 
227     .reset = generic_reset_device,
228     .start = generic_start_device,
229     .stop = generic_stop_device,
230 };
231
232
233
234
235 int v3_generic_add_port_range(struct vm_device * dev, uint_t start, uint_t end, uint_t type) {
236
237     if (PORT_HOOKS) {
238         struct generic_internal * state = (struct generic_internal *)(dev->private_data);
239
240         struct port_range * range = (struct port_range *)V3_Malloc(sizeof(struct port_range));
241         range->start = start;
242         range->end = end;
243         range->type = type;
244     
245       
246         PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n", 
247                    range->start, range->end, 
248                    (range->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
249     
250         list_add(&(range->range_link), &(state->port_list));
251         state->num_port_ranges++;
252     } else {
253         PrintDebug("generic: hooking IO ports not supported\n");
254         return -1;
255     }
256
257     return 0;
258 }
259
260
261
262
263 static int generic_init(struct guest_info * vm, void * cfg_data) {
264     struct generic_internal * state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
265   
266     state->num_port_ranges = 0;
267
268     INIT_LIST_HEAD(&(state->port_list));
269     
270     struct vm_device * dev = v3_allocate_device("GENERIC", &dev_ops, state);
271
272
273     if (v3_attach_device(vm, dev) == -1) {
274         PrintError("Could not attach device %s\n", "GENERIC");
275         return -1;
276     }
277
278     PrintDebug("generic: init_device\n");
279     generic_reset_device(dev);
280
281
282     if (PORT_HOOKS) { // This is a runtime conditional on a #define
283         struct port_range * tmp = NULL;
284
285         list_for_each_entry(tmp, &(state->port_list), range_link) {
286             uint_t i = 0;
287       
288             PrintDebug("generic: hooking ports 0x%x to 0x%x as %s\n", 
289                        tmp->start, tmp->end, 
290                        (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
291       
292             for (i = tmp->start; i <= tmp->end; i++) { 
293                 if (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) { 
294           
295                     if (v3_dev_hook_io(dev, i, &generic_read_port_passthrough, &generic_write_port_passthrough)) { 
296                         PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
297                     }
298           
299                 } else if (tmp->type == GENERIC_PRINT_AND_IGNORE) { 
300           
301                     if (v3_dev_hook_io(dev, i, &generic_read_port_ignore, &generic_write_port_ignore)) { 
302                         PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
303                     }
304                 } 
305             }
306
307         }
308     } else {
309         PrintDebug("generic: hooking ports not supported\n");
310     }
311
312
313     return 0;
314 }
315
316 device_register("GENERIC", generic_init)