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.


changed debug output to be more granular
[palacios.git] / palacios / src / palacios / vmm_io.c
1 #include <palacios/vmm_io.h>
2 #include <palacios/vmm_string.h>
3 #include <palacios/vmm.h>
4
5 extern struct vmm_os_hooks * os_hooks;
6
7
8 #ifndef DEBUG_IO
9 #undef PrintDebug
10 #define PrintDebug(fmt, args...)
11 #endif
12
13
14 void init_vmm_io_map(vmm_io_map_t * io_map) {
15   io_map->num_ports = 0;
16   io_map->head = NULL;
17 }
18
19
20
21 int add_io_hook(vmm_io_map_t * io_map, vmm_io_hook_t * io_hook) {
22
23   if (!(io_map->head)) {
24     io_map->head = io_hook;
25     io_map->num_ports = 1;
26     return 0;
27   } else if (io_map->head->port > io_hook->port) {
28     io_hook->next = io_map->head;
29
30     io_map->head->prev = io_hook;
31     io_map->head = io_hook;
32     io_map->num_ports++;
33
34     return 0;
35   } else {
36     vmm_io_hook_t * tmp_hook = io_map->head;
37     
38     while ((tmp_hook->next)  && 
39            (tmp_hook->next->port <= io_hook->port)) {
40         tmp_hook = tmp_hook->next;
41     }
42     
43     if (tmp_hook->port == io_hook->port) {
44       //tmp_hook->read = io_hook->read;
45       //tmp_hook->write = io_hook->write;
46       //V3_Free(io_hook);
47       return -1;
48     } else {
49       io_hook->prev = tmp_hook;
50       io_hook->next = tmp_hook->next;
51
52       if (tmp_hook->next) {
53         tmp_hook->next->prev = io_hook;
54       }
55
56       tmp_hook->next = io_hook;
57
58       io_map->num_ports++;
59       return 0;
60     }
61   }
62   return -1;
63 }
64
65 int remove_io_hook(vmm_io_map_t * io_map, vmm_io_hook_t * io_hook) {
66   if (io_map->head == io_hook) {
67     io_map->head = io_hook->next;
68   } else if (io_hook->prev) {
69     io_hook->prev->next = io_hook->next;
70   } else {
71     return -1;
72     // data corruption failure
73   }
74   
75   if (io_hook->next) {
76     io_hook->next->prev = io_hook->prev;
77   }
78
79   io_map->num_ports--;
80
81   return 0;
82 }
83
84
85
86 /* FIX ME */
87 static int default_write(ushort_t port, void *src, uint_t length, void * priv_data) {
88   /*
89     
90   if (length == 1) {
91   __asm__ __volatile__ (
92   "outb %b0, %w1"
93   :
94   : "a" (*dst), "Nd" (port)
95   );
96   } else if (length == 2) {
97   __asm__ __volatile__ (
98   "outw %b0, %w1"
99   :
100   : "a" (*dst), "Nd" (port)
101   );
102   } else if (length == 4) {
103   __asm__ __volatile__ (
104   "outw %b0, %w1"
105   :
106   : "a" (*dst), "Nd" (port)
107   );
108   }
109   */
110   return 0;
111 }
112
113 static int default_read(ushort_t port, void * dst, uint_t length, void * priv_data)
114 {
115
116   /*    
117         uchar_t value;
118
119     __asm__ __volatile__ (
120         "inb %w1, %b0"
121         : "=a" (value)
122         : "Nd" (port)
123     );
124
125     return value;
126   */
127
128   return 0;
129 }
130
131 int hook_io_port(vmm_io_map_t * io_map, uint_t port, 
132                  int (*read)(ushort_t port, void * dst, uint_t length, void * priv_data),
133                  int (*write)(ushort_t port, void * src, uint_t length, void * priv_data), 
134                  void * priv_data) {
135   vmm_io_hook_t * io_hook = os_hooks->malloc(sizeof(vmm_io_hook_t));
136
137   io_hook->port = port;
138
139   if (!read) {
140     io_hook->read = &default_read;
141   } else {
142     io_hook->read = read;
143   }
144
145   if (!write) {
146     io_hook->write = &default_write;
147   } else {
148     io_hook->write = write;
149   }
150
151   io_hook->next = NULL;
152   io_hook->prev = NULL;
153
154   io_hook->priv_data = priv_data;
155
156   if (add_io_hook(io_map, io_hook) != 0) {
157     V3_Free(io_hook);
158     return -1;
159   }
160
161   return 0;
162 }
163
164 int unhook_io_port(vmm_io_map_t * io_map, uint_t port) {
165   vmm_io_hook_t * hook = get_io_hook(io_map, port);
166
167   if (hook == NULL) {
168     return -1;
169   }
170
171   remove_io_hook(io_map, hook);
172   return 0;
173 }
174
175
176 vmm_io_hook_t * get_io_hook(vmm_io_map_t * io_map, uint_t port) {
177   vmm_io_hook_t * tmp_hook;
178   FOREACH_IO_HOOK(*io_map, tmp_hook) {
179     if (tmp_hook->port == port) {
180       return tmp_hook;
181     }
182   }
183   return NULL;
184 }
185
186
187
188 void PrintDebugIOMap(vmm_io_map_t * io_map) {
189   vmm_io_hook_t * iter = io_map->head;
190
191   PrintDebug("VMM IO Map (Entries=%d)\n", io_map->num_ports);
192
193   while (iter) {
194     PrintDebug("IO Port: %hu (Read=%x) (Write=%x)\n", iter->port, iter->read, iter->write);
195   }
196 }