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