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.


more printf format fixes
[palacios.git] / palacios / src / palacios / vmm_io.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, Jack Lange <jarusl@cs.northwestern.edu> 
11  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmm_io.h>
21 #include <palacios/vmm_string.h>
22 #include <palacios/vmm.h>
23
24
25
26
27 #ifndef DEBUG_IO
28 #undef PrintDebug
29 #define PrintDebug(fmt, args...)
30 #endif
31
32
33 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data);
34 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data);
35
36
37 void v3_init_vmm_io_map(struct guest_info * info) {
38   struct vmm_io_map * io_map = &(info->io_map);
39   io_map->num_ports = 0;
40   io_map->head = NULL;
41 }
42
43
44
45
46
47 static int add_io_hook(struct vmm_io_map * io_map, struct vmm_io_hook * io_hook) {
48
49   if (!(io_map->head)) {
50     io_map->head = io_hook;
51     io_map->num_ports = 1;
52     return 0;
53   } else if (io_map->head->port > io_hook->port) {
54     io_hook->next = io_map->head;
55
56     io_map->head->prev = io_hook;
57     io_map->head = io_hook;
58     io_map->num_ports++;
59
60     return 0;
61   } else {
62     struct vmm_io_hook * tmp_hook = io_map->head;
63     
64     while ((tmp_hook->next)  && 
65            (tmp_hook->next->port <= io_hook->port)) {
66         tmp_hook = tmp_hook->next;
67     }
68     
69     if (tmp_hook->port == io_hook->port) {
70       //tmp_hook->read = io_hook->read;
71       //tmp_hook->write = io_hook->write;
72       //V3_Free(io_hook);
73       return -1;
74     } else {
75       io_hook->prev = tmp_hook;
76       io_hook->next = tmp_hook->next;
77
78       if (tmp_hook->next) {
79         tmp_hook->next->prev = io_hook;
80       }
81
82       tmp_hook->next = io_hook;
83
84       io_map->num_ports++;
85       return 0;
86     }
87   }
88   return -1;
89 }
90
91 static int remove_io_hook(struct vmm_io_map * io_map, struct vmm_io_hook * io_hook) {
92   if (io_map->head == io_hook) {
93     io_map->head = io_hook->next;
94   } else if (io_hook->prev) {
95     io_hook->prev->next = io_hook->next;
96   } else {
97     return -1;
98     // data corruption failure
99   }
100   
101   if (io_hook->next) {
102     io_hook->next->prev = io_hook->prev;
103   }
104
105   io_map->num_ports--;
106
107   return 0;
108 }
109
110
111
112
113
114 int v3_hook_io_port(struct guest_info * info, uint_t port, 
115                     int (*read)(ushort_t port, void * dst, uint_t length, void * priv_data),
116                     int (*write)(ushort_t port, void * src, uint_t length, void * priv_data), 
117                     void * priv_data) {
118   struct vmm_io_map * io_map = &(info->io_map);
119   struct vmm_io_hook * io_hook = (struct vmm_io_hook *)V3_Malloc(sizeof(struct vmm_io_hook));
120
121   io_hook->port = port;
122
123   if (!read) {
124     io_hook->read = &default_read;
125   } else {
126     io_hook->read = read;
127   }
128
129   if (!write) {
130     io_hook->write = &default_write;
131   } else {
132     io_hook->write = write;
133   }
134
135   io_hook->next = NULL;
136   io_hook->prev = NULL;
137
138   io_hook->priv_data = priv_data;
139
140   if (add_io_hook(io_map, io_hook) != 0) {
141     V3_Free(io_hook);
142     return -1;
143   }
144
145   return 0;
146 }
147
148 int v3_unhook_io_port(struct guest_info * info, uint_t port) {
149   struct vmm_io_map * io_map = &(info->io_map);
150   struct vmm_io_hook * hook = v3_get_io_hook(io_map, port);
151
152   if (hook == NULL) {
153     return -1;
154   }
155
156   remove_io_hook(io_map, hook);
157   return 0;
158 }
159
160
161 struct vmm_io_hook * v3_get_io_hook(struct vmm_io_map * io_map, uint_t port) {
162   struct vmm_io_hook * tmp_hook;
163   FOREACH_IO_HOOK(*io_map, tmp_hook) {
164     if (tmp_hook->port == port) {
165       return tmp_hook;
166     }
167   }
168   return NULL;
169 }
170
171
172
173 void v3_print_io_map(struct vmm_io_map * io_map) {
174   struct vmm_io_hook * iter = io_map->head;
175
176   PrintDebug("VMM IO Map (Entries=%d)\n", io_map->num_ports);
177
178   while (iter) {
179     PrintDebug("IO Port: %hu (Read=%p) (Write=%p)\n", 
180                iter->port, 
181                (void *)(iter->read), (void *)(iter->write));
182   }
183 }
184
185
186
187 /*
188  * Write a byte to an I/O port.
189  */
190 void v3_outb(ushort_t port, uchar_t value) {
191     __asm__ __volatile__ (
192         "outb %b0, %w1"
193         :
194         : "a" (value), "Nd" (port)
195     );
196 }
197
198 /*
199  * Read a byte from an I/O port.
200  */
201 uchar_t v3_inb(ushort_t port) {
202     uchar_t value;
203
204     __asm__ __volatile__ (
205         "inb %w1, %b0"
206         : "=a" (value)
207         : "Nd" (port)
208     );
209
210     return value;
211 }
212
213 /*
214  * Write a word to an I/O port.
215  */
216 void v3_outw(ushort_t port, ushort_t value) {
217     __asm__ __volatile__ (
218         "outw %w0, %w1"
219         :
220         : "a" (value), "Nd" (port)
221     );
222 }
223
224 /*
225  * Read a word from an I/O port.
226  */
227 ushort_t v3_inw(ushort_t port) {
228     ushort_t value;
229
230     __asm__ __volatile__ (
231         "inw %w1, %w0"
232         : "=a" (value)
233         : "Nd" (port)
234     );
235
236     return value;
237 }
238
239 /*
240  * Write a double word to an I/O port.
241  */
242 void v3_outdw(ushort_t port, uint_t value) {
243     __asm__ __volatile__ (
244         "outl %0, %1"
245         :
246         : "a" (value), "Nd" (port)
247     );
248 }
249
250 /*
251  * Read a double word from an I/O port.
252  */
253 uint_t v3_indw(ushort_t port) {
254     uint_t value;
255
256     __asm__ __volatile__ (
257         "inl %1, %0"
258         : "=a" (value)
259         : "Nd" (port)
260     );
261
262     return value;
263 }
264
265
266
267
268 /* FIX ME */
269 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
270   /*
271     
272   if (length == 1) {
273   __asm__ __volatile__ (
274   "outb %b0, %w1"
275   :
276   : "a" (*dst), "Nd" (port)
277   );
278   } else if (length == 2) {
279   __asm__ __volatile__ (
280   "outw %b0, %w1"
281   :
282   : "a" (*dst), "Nd" (port)
283   );
284   } else if (length == 4) {
285   __asm__ __volatile__ (
286   "outw %b0, %w1"
287   :
288   : "a" (*dst), "Nd" (port)
289   );
290   }
291   */
292   return 0;
293 }
294
295 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data) {
296
297   /*    
298         uchar_t value;
299
300     __asm__ __volatile__ (
301         "inb %w1, %b0"
302         : "=a" (value)
303         : "Nd" (port)
304     );
305
306     return value;
307   */
308
309   return 0;
310 }