Debian Codegraph @

tools/libs/evtchn/minios.c

raw
1 /******************************************************************************
2 *
3 * Copyright 2007-2008 Samuel Thibault <[email protected]>.
4 * All rights reserved.
5 * Use is subject to license terms.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation;
10 * version 2.1 of the License.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; If not, see <http://www.gnu.org/licenses/>.
19 *
20 * Split off from xc_minios.c
21 */
22
23 #include "xen_list.h"
24 #include <mini-os/types.h>
25 #include <mini-os/os.h>
26 #include <mini-os/lib.h>
27 #include <mini-os/events.h>
28 #include <mini-os/wait.h>
29
30 #include <assert.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdint.h>
35 #include <unistd.h>
36 #include <inttypes.h>
37 #include <malloc.h>
38
39 #include "private.h"
40
41 XEN_LIST_HEAD(port_list, struct port_info);
42
43 struct ports {
44 struct port_list list;
45 };
46
47 struct port_info {
48 XEN_LIST_ENTRY(struct port_info) list;
49 evtchn_port_t port;
50 bool pending;
51 bool bound;
52 };
53
54 static struct port_info *port_alloc(xenevtchn_handle *xce)
55 {
56 struct port_info *port_info;
57 struct file *file = get_file_from_fd(xce->fd);
58 struct ports *ports = file->dev;
59 unsigned long flags;
60
61 port_info = malloc(sizeof(struct port_info));
62 if ( port_info == NULL )
63 return NULL;
64
65 port_info->pending = false;
66 port_info->port = -1;
67 port_info->bound = false;
68
69 local_irq_save(flags);
70 XEN_LIST_INSERT_HEAD(&ports->list, port_info, list);
71 local_irq_restore(flags);
72
73 return port_info;
74 }
75
76 static void port_dealloc(struct port_info *port_info)
77 {
78 unsigned long flags;
79
80 if ( port_info->bound )
81 unbind_evtchn(port_info->port);
82
83 local_irq_save(flags);
84 XEN_LIST_REMOVE(port_info, list);
85 local_irq_restore(flags);
86
87 free(port_info);
88 }
89
90 static int evtchn_close_fd(struct file *file)
91 {
92 struct port_info *port_info, *tmp;
93 struct ports *ports = file->dev;
94
95 XEN_LIST_FOREACH_SAFE(port_info, &ports->list, list, tmp)
96 port_dealloc(port_info);
97
98 free(ports);
99
100 return 0;
101 }
102
103 static const struct file_ops evtchn_ops = {
104 .name = "evtchn",
105 .close = evtchn_close_fd,
106 .select_rd = select_read_flag,
107 };
108
109 static unsigned int ftype_evtchn;
110
111 __attribute__((constructor))
112 static void evtchn_initialize(void)
113 {
114 ftype_evtchn = alloc_file_type(&evtchn_ops);
115 }
116
117 /*
118 * XENEVTCHN_NO_CLOEXEC is being ignored, as there is no exec() call supported
119 * in Mini-OS.
120 */
121 int osdep_evtchn_open(xenevtchn_handle *xce, unsigned int flags)
122 {
123 int fd;
124 struct file *file;
125 struct ports *ports;
126
127 ports = malloc(sizeof(*ports));
128 if ( !ports )
129 return -1;
130
131 fd = alloc_fd(ftype_evtchn);
132 file = get_file_from_fd(fd);
133
134 if ( !file )
135 {
136 free(ports);
137 return -1;
138 }
139
140 file->dev = ports;
141 XEN_LIST_INIT(&ports->list);
142 xce->fd = fd;
143 printf("evtchn_open() -> %d\n", fd);
144
145 return 0;
146 }
147
148 int osdep_evtchn_close(xenevtchn_handle *xce)
149 {
150 if ( xce->fd == -1 )
151 return 0;
152
153 return close(xce->fd);
154 }
155
156 int osdep_evtchn_restrict(xenevtchn_handle *xce, domid_t domid)
157 {
158 errno = EOPNOTSUPP;
159
160 return -1;
161 }
162
163 int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
164 {
165 int ret;
166
167 ret = notify_remote_via_evtchn(port);
168
169 if ( ret < 0 )
170 {
171 errno = -ret;
172 ret = -1;
173 }
174
175 return ret;
176 }
177
178 static void evtchn_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
179 {
180 xenevtchn_handle *xce = data;
181 struct file *file = get_file_from_fd(xce->fd);
182 struct port_info *port_info;
183 struct ports *ports;
184
185 assert(file);
186 ports = file->dev;
187 mask_evtchn(port);
188
189 XEN_LIST_FOREACH(port_info, &ports->list, list)
190 {
191 if ( port_info->port == port )
192 goto found;
193 }
194
195 printk("Unknown port %d for handle %d\n", port, xce->fd);
196 return;
197
198 found:
199 port_info->pending = true;
200 file->read = true;
201 wake_up(&event_queue);
202 }
203
204 xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
205 uint32_t domid)
206 {
207 struct port_info *port_info;
208 int ret;
209 evtchn_port_t port;
210
211 port_info = port_alloc(xce);
212 if ( port_info == NULL )
213 return -1;
214
215 printf("xenevtchn_bind_unbound_port(%d)", domid);
216 ret = evtchn_alloc_unbound(domid, evtchn_handler, xce, &port);
217 printf(" = %d\n", ret);
218
219 if ( ret < 0 )
220 {
221 port_dealloc(port_info);
222 errno = -ret;
223 return -1;
224 }
225
226 port_info->bound = true;
227 port_info->port = port;
228 unmask_evtchn(port);
229
230 return port;
231 }
232
233 xenevtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce,
234 uint32_t domid,
235 evtchn_port_t remote_port)
236 {
237 struct port_info *port_info;
238 evtchn_port_t local_port;
239 int ret;
240
241 port_info = port_alloc(xce);
242 if ( port_info == NULL )
243 return -1;
244
245 printf("xenevtchn_bind_interdomain(%d, %"PRId32")", domid, remote_port);
246 ret = evtchn_bind_interdomain(domid, remote_port, evtchn_handler,
247 xce, &local_port);
248 printf(" = %d\n", ret);
249
250 if ( ret < 0 )
251 {
252 port_dealloc(port_info);
253 errno = -ret;
254 return -1;
255 }
256
257 port_info->bound = true;
258 port_info->port = local_port;
259 unmask_evtchn(local_port);
260
261 return local_port;
262 }
263
264 int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
265 {
266 int fd = xce->fd;
267 struct file *file = get_file_from_fd(fd);
268 struct port_info *port_info;
269 struct ports *ports = file->dev;
270 unsigned long flags;
271
272 local_irq_save(flags);
273 XEN_LIST_FOREACH(port_info, &ports->list, list)
274 {
275 if ( port_info->port == port )
276 {
277 port_dealloc(port_info);
278 local_irq_restore(flags);
279 return 0;
280 }
281 }
282 local_irq_restore(flags);
283
284 printf("Warning: couldn't find port %"PRId32" for xc handle %x\n",
285 port, fd);
286 errno = EINVAL;
287
288 return -1;
289 }
290
291 xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce,
292 unsigned int virq)
293 {
294 struct port_info *port_info;
295 evtchn_port_t port;
296
297 port_info = port_alloc(xce);
298 if ( port_info == NULL )
299 return -1;
300
301 printf("xenevtchn_bind_virq(%d)", virq);
302 port = bind_virq(virq, evtchn_handler, xce);
303 printf(" = %d\n", port);
304
305 if ( port < 0 )
306 {
307 port_dealloc(port_info);
308 errno = -port;
309 return -1;
310 }
311
312 port_info->bound = true;
313 port_info->port = port;
314 unmask_evtchn(port);
315
316 return port;
317 }
318
319 xenevtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
320 {
321 struct file *file = get_file_from_fd(xce->fd);
322 struct port_info *port_info;
323 struct ports *ports = file->dev;
324 unsigned long flags;
325 evtchn_port_t ret = -1;
326
327 local_irq_save(flags);
328
329 file->read = false;
330
331 XEN_LIST_FOREACH(port_info, &ports->list, list)
332 {
333 if ( port_info->port != -1 && port_info->pending )
334 {
335 if ( ret == -1 )
336 {
337 ret = port_info->port;
338 port_info->pending = false;
339 }
340 else
341 {
342 file->read = true;
343 break;
344 }
345 }
346 }
347
348 local_irq_restore(flags);
349
350 return ret;
351 }
352
353 int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
354 {
355 unmask_evtchn(port);
356
357 return 0;
358 }
359
360 /*
361 * Local variables:
362 * mode: C
363 * c-file-style: "BSD"
364 * c-basic-offset: 4
365 * tab-width: 4
366 * indent-tabs-mode: nil
367 * End:
368 */