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.


9756eb4140dbb46b6d71e6e289cd164ca73de6dc
[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_io_map(struct guest_info * info) {
38   info->io_map.rb_node = NULL;
39 }
40
41
42
43
44 static inline struct v3_io_hook * __insert_io_hook(struct guest_info * info, struct v3_io_hook * hook) {
45   struct rb_node ** p = &(info->io_map.rb_node);
46   struct rb_node * parent = NULL;
47   struct v3_io_hook * tmp_hook = NULL;
48
49   while (*p) {
50     parent = *p;
51     tmp_hook = rb_entry(parent, struct v3_io_hook, tree_node);
52
53     if (hook->port < tmp_hook->port) {
54       p = &(*p)->rb_left;
55     } else if (hook->port > tmp_hook->port) {
56       p = &(*p)->rb_right;
57     } else {
58       return tmp_hook;
59     }
60   }
61   rb_link_node(&(hook->tree_node), parent, p);
62
63   return NULL;
64 }
65
66
67 static inline struct v3_io_hook * insert_io_hook(struct guest_info * info, struct v3_io_hook * hook) {
68   struct v3_io_hook * ret;
69
70   if ((ret = __insert_io_hook(info, hook))) {
71     return ret;
72   }
73
74   v3_rb_insert_color(&(hook->tree_node), &(info->io_map));
75
76   return NULL;
77 }
78
79
80 struct v3_io_hook * v3_get_io_hook(struct guest_info * info, uint_t port) {
81   struct rb_node * n = info->io_map.rb_node;
82   struct v3_io_hook * hook = NULL;
83
84   while (n) {
85     hook = rb_entry(n, struct v3_io_hook, tree_node);
86     
87     if (port < hook->port) {
88       n = n->rb_left;
89     } else if (port > hook->port) {
90       n = n->rb_right;
91     } else {
92       return hook;
93     }
94   }
95
96   return NULL;
97 }
98
99
100
101
102
103
104 int v3_hook_io_port(struct guest_info * info, uint_t port, 
105                     int (*read)(ushort_t port, void * dst, uint_t length, void * priv_data),
106                     int (*write)(ushort_t port, void * src, uint_t length, void * priv_data), 
107                     void * priv_data) {
108   struct v3_io_hook * io_hook = (struct v3_io_hook *)V3_Malloc(sizeof(struct v3_io_hook));
109
110   io_hook->port = port;
111
112   if (!read) {
113     io_hook->read = &default_read;
114   } else {
115     io_hook->read = read;
116   }
117
118   if (!write) {
119     io_hook->write = &default_write;
120   } else {
121     io_hook->write = write;
122   }
123
124
125   io_hook->priv_data = priv_data;
126
127   if (insert_io_hook(info, io_hook)) {
128     V3_Free(io_hook);
129     return -1;
130   }
131
132   return 0;
133 }
134
135 int v3_unhook_io_port(struct guest_info * info, uint_t port) {
136   struct v3_io_hook * hook = v3_get_io_hook(info, port);
137
138   if (hook == NULL) {
139     return -1;
140   }
141
142   v3_rb_erase(&(hook->tree_node), &(info->io_map));
143
144   V3_Free(hook);
145
146   return 0;
147 }
148
149
150
151
152
153
154 void v3_print_io_map(struct guest_info * info) {
155   struct v3_io_hook * tmp_hook = NULL;
156   struct rb_node * node = v3_rb_first(&(info->io_map));
157
158   PrintDebug("VMM IO Map\n");
159
160   do {
161     tmp_hook = rb_entry(node, struct v3_io_hook, tree_node);
162
163     PrintDebug("IO Port: %hu (Read=%p) (Write=%p)\n", 
164                tmp_hook->port, 
165                (void *)(tmp_hook->read), (void *)(tmp_hook->write));
166   } while ((node = v3_rb_next(node)));
167 }
168
169
170
171 /*
172  * Write a byte to an I/O port.
173  */
174 void v3_outb(ushort_t port, uchar_t value) {
175     __asm__ __volatile__ (
176         "outb %b0, %w1"
177         :
178         : "a" (value), "Nd" (port)
179     );
180 }
181
182 /*
183  * Read a byte from an I/O port.
184  */
185 uchar_t v3_inb(ushort_t port) {
186     uchar_t value;
187
188     __asm__ __volatile__ (
189         "inb %w1, %b0"
190         : "=a" (value)
191         : "Nd" (port)
192     );
193
194     return value;
195 }
196
197 /*
198  * Write a word to an I/O port.
199  */
200 void v3_outw(ushort_t port, ushort_t value) {
201     __asm__ __volatile__ (
202         "outw %w0, %w1"
203         :
204         : "a" (value), "Nd" (port)
205     );
206 }
207
208 /*
209  * Read a word from an I/O port.
210  */
211 ushort_t v3_inw(ushort_t port) {
212     ushort_t value;
213
214     __asm__ __volatile__ (
215         "inw %w1, %w0"
216         : "=a" (value)
217         : "Nd" (port)
218     );
219
220     return value;
221 }
222
223 /*
224  * Write a double word to an I/O port.
225  */
226 void v3_outdw(ushort_t port, uint_t value) {
227     __asm__ __volatile__ (
228         "outl %0, %1"
229         :
230         : "a" (value), "Nd" (port)
231     );
232 }
233
234 /*
235  * Read a double word from an I/O port.
236  */
237 uint_t v3_indw(ushort_t port) {
238     uint_t value;
239
240     __asm__ __volatile__ (
241         "inl %1, %0"
242         : "=a" (value)
243         : "Nd" (port)
244     );
245
246     return value;
247 }
248
249
250
251
252 /* FIX ME */
253 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
254   /*
255     
256   if (length == 1) {
257   __asm__ __volatile__ (
258   "outb %b0, %w1"
259   :
260   : "a" (*dst), "Nd" (port)
261   );
262   } else if (length == 2) {
263   __asm__ __volatile__ (
264   "outw %b0, %w1"
265   :
266   : "a" (*dst), "Nd" (port)
267   );
268   } else if (length == 4) {
269   __asm__ __volatile__ (
270   "outw %b0, %w1"
271   :
272   : "a" (*dst), "Nd" (port)
273   );
274   }
275   */
276   return 0;
277 }
278
279 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data) {
280
281   /*    
282         uchar_t value;
283
284     __asm__ __volatile__ (
285         "inb %w1, %b0"
286         : "=a" (value)
287         : "Nd" (port)
288     );
289
290     return value;
291   */
292
293   return 0;
294 }