1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation;
5  * version 2.1 of the License.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
14  *
15  * Split out from xc_linus_osdep.c:
16  *
17  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
18  */
19 
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 
26 #include <sys/ioctl.h>
27 
28 #include <xen/xen.h>
29 #include <xen/sys/evtchn.h>
30 
31 #include "private.h"
32 
33 #ifndef O_CLOEXEC
34 #define O_CLOEXEC 0
35 #endif
36 
osdep_evtchn_open(xenevtchn_handle * xce,unsigned int flags)37 int osdep_evtchn_open(xenevtchn_handle *xce, unsigned int flags)
38 {
39     int open_flags = O_RDWR;
40     int fd;
41 
42     if ( !(flags & XENEVTCHN_NO_CLOEXEC) )
43         open_flags |= O_CLOEXEC;
44 
45     fd = open("/dev/xen/evtchn", open_flags);
46     if ( fd == -1 )
47         return -1;
48 
49     xce->fd = fd;
50 
51     return 0;
52 }
53 
osdep_evtchn_close(xenevtchn_handle * xce)54 int osdep_evtchn_close(xenevtchn_handle *xce)
55 {
56     if ( xce->fd == -1 )
57         return 0;
58 
59     return close(xce->fd);
60 }
61 
osdep_evtchn_restrict(xenevtchn_handle * xce,domid_t domid)62 int osdep_evtchn_restrict(xenevtchn_handle *xce, domid_t domid)
63 {
64     struct ioctl_evtchn_restrict_domid restrict_domid = { domid };
65 
66     return ioctl(xce->fd, IOCTL_EVTCHN_RESTRICT_DOMID, &restrict_domid);
67 }
68 
xenevtchn_notify(xenevtchn_handle * xce,evtchn_port_t port)69 int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
70 {
71     int fd = xce->fd;
72     struct ioctl_evtchn_notify notify;
73 
74     notify.port = port;
75 
76     return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
77 }
78 
xenevtchn_bind_unbound_port(xenevtchn_handle * xce,uint32_t domid)79 xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
80                                                       uint32_t domid)
81 {
82     int fd = xce->fd;
83     struct ioctl_evtchn_bind_unbound_port bind;
84 
85     bind.remote_domain = domid;
86 
87     return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
88 }
89 
xenevtchn_bind_interdomain(xenevtchn_handle * xce,uint32_t domid,evtchn_port_t remote_port)90 xenevtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce,
91                                                      uint32_t domid,
92                                                      evtchn_port_t remote_port)
93 {
94     int fd = xce->fd;
95     struct ioctl_evtchn_bind_interdomain bind;
96 
97     bind.remote_domain = domid;
98     bind.remote_port = remote_port;
99 
100     return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
101 }
102 
xenevtchn_bind_virq(xenevtchn_handle * xce,unsigned int virq)103 xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce,
104                                               unsigned int virq)
105 {
106     int fd = xce->fd;
107     struct ioctl_evtchn_bind_virq bind;
108 
109     bind.virq = virq;
110 
111     return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
112 }
113 
xenevtchn_unbind(xenevtchn_handle * xce,evtchn_port_t port)114 int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
115 {
116     int fd = xce->fd;
117     struct ioctl_evtchn_unbind unbind;
118 
119     unbind.port = port;
120 
121     return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
122 }
123 
xenevtchn_pending(xenevtchn_handle * xce)124 xenevtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
125 {
126     int fd = xce->fd;
127     evtchn_port_t port;
128 
129     if ( read(fd, &port, sizeof(port)) != sizeof(port) )
130         return -1;
131 
132     return port;
133 }
134 
xenevtchn_unmask(xenevtchn_handle * xce,evtchn_port_t port)135 int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
136 {
137     int fd = xce->fd;
138 
139     if ( write(fd, &port, sizeof(port)) != sizeof(port) )
140         return -1;
141 
142     return 0;
143 }
144 
145 /*
146  * Local variables:
147  * mode: C
148  * c-file-style: "BSD"
149  * c-basic-offset: 4
150  * tab-width: 4
151  * indent-tabs-mode: nil
152  * End:
153  */
154