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.


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