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.


removed unused device resources from generic
[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 static int generic_init_device(struct vm_device * dev) {
185     struct generic_internal * state = (struct generic_internal *)(dev->private_data);
186
187     PrintDebug("generic: init_device\n");
188     generic_reset_device(dev);
189
190
191     if (PORT_HOOKS) { // This is a runtime conditional on a #define
192         struct port_range * tmp = NULL;
193
194         list_for_each_entry(tmp, &(state->port_list), range_link) {
195             uint_t i = 0;
196       
197             PrintDebug("generic: hooking ports 0x%x to 0x%x as %s\n", 
198                        tmp->start, tmp->end, 
199                        (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
200       
201             for (i = tmp->start; i <= tmp->end; i++) { 
202                 if (tmp->type == GENERIC_PRINT_AND_PASSTHROUGH) { 
203           
204                     if (v3_dev_hook_io(dev, i, &generic_read_port_passthrough, &generic_write_port_passthrough)) { 
205                         PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
206                     }
207           
208                 } else if (tmp->type == GENERIC_PRINT_AND_IGNORE) { 
209           
210                     if (v3_dev_hook_io(dev, i, &generic_read_port_ignore, &generic_write_port_ignore)) { 
211                         PrintDebug("generic: can't hook port 0x%x (already hooked?)\n", i);
212                     }
213                 } 
214             }
215
216         }
217     } else {
218         PrintDebug("generic: hooking ports not supported\n");
219     }
220
221
222     return 0;
223 }
224
225 static int generic_deinit_device(struct vm_device * dev) {
226     struct generic_internal * state = (struct generic_internal *)(dev->private_data);
227
228
229     PrintDebug("generic: deinit_device\n");
230
231     if (PORT_HOOKS) {
232         struct port_range * tmp;
233         struct port_range * cur;
234     
235         list_for_each_entry_safe(cur, tmp, &(state->port_list), range_link) {
236             uint_t i;
237
238             PrintDebug("generic: unhooking ports 0x%x to 0x%x\n",
239                        cur->start, cur->end);
240                 
241             for (i = cur->start; i <= cur->end; i++) {
242                 if (v3_dev_unhook_io(dev, i)) {
243                     PrintDebug("generic: can't unhook port 0x%x (already unhooked?)\n", i);
244                 }
245             }
246
247             list_del(&(cur->range_link));
248             state->num_port_ranges--;
249             V3_Free(cur);
250         }
251     } else {
252         PrintDebug("generic: unhooking ports not supported\n");
253     }
254
255
256
257     generic_reset_device(dev);
258     return 0;
259 }
260
261
262
263
264
265 static struct vm_device_ops dev_ops = { 
266     .init = generic_init_device, 
267     .deinit = generic_deinit_device,
268     .reset = generic_reset_device,
269     .start = generic_start_device,
270     .stop = generic_stop_device,
271 };
272
273
274
275
276 int v3_generic_add_port_range(struct vm_device * dev, uint_t start, uint_t end, uint_t type) {
277
278     if (PORT_HOOKS) {
279         struct generic_internal * state = (struct generic_internal *)(dev->private_data);
280
281         struct port_range * range = (struct port_range *)V3_Malloc(sizeof(struct port_range));
282         range->start = start;
283         range->end = end;
284         range->type = type;
285     
286       
287         PrintDebug("generic: Adding Port Range: 0x%x to 0x%x as %s\n", 
288                    range->start, range->end, 
289                    (range->type == GENERIC_PRINT_AND_PASSTHROUGH) ? "print-and-passthrough" : "print-and-ignore");
290     
291         list_add(&(range->range_link), &(state->port_list));
292         state->num_port_ranges++;
293     } else {
294         PrintDebug("generic: hooking IO ports not supported\n");
295         return -1;
296     }
297
298     return 0;
299 }
300
301 struct vm_device * v3_create_generic() {
302     struct generic_internal * generic_state = (struct generic_internal *)V3_Malloc(sizeof(struct generic_internal));
303   
304     generic_state->num_port_ranges = 0;
305
306     INIT_LIST_HEAD(&(generic_state->port_list));
307     
308     struct vm_device * device = v3_create_device("GENERIC", &dev_ops, generic_state);
309
310     return device;
311 }