1 /*
2  * Copyright (c) 2017 Citrix Systems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation;
7  * version 2.1 of the License.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #include <sys/ioctl.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 
29 #include <xen/xen.h>
30 #include <xen/sys/privcmd.h>
31 
32 #include "private.h"
33 
34 #ifndef O_CLOEXEC
35 #define O_CLOEXEC 0
36 #endif
37 
osdep_xendevicemodel_open(xendevicemodel_handle * dmod)38 int osdep_xendevicemodel_open(xendevicemodel_handle *dmod)
39 {
40     int fd = open("/dev/xen/privcmd", O_RDWR | O_CLOEXEC);
41     privcmd_dm_op_t uop;
42     int rc;
43 
44     if (fd < 0) {
45         /*
46          * If the 'new' privcmd interface doesn't exist then don't treat
47          * this as an error, but an old privcmd clearly won't implement
48          * IOCTL_PRIVCMD_DM_OP so don't bother trying to open it.
49          */
50         if (errno == ENOENT || errno == ENXIO || errno == ENODEV)
51             goto out;
52 
53         PERROR("Could not obtain handle on privileged command interface");
54         return -1;
55     }
56 
57     /*
58      * Check to see if IOCTL_PRIVCMD_DM_OP is implemented as we want to
59      * use that in preference to libxencall.
60      */
61     uop.dom = DOMID_INVALID;
62     uop.num = 0;
63     uop.ubufs = NULL;
64 
65     rc = ioctl(fd, IOCTL_PRIVCMD_DM_OP, &uop);
66     if (rc < 0) {
67         close(fd);
68         fd = -1;
69     }
70 
71 out:
72     dmod->fd = fd;
73     return 0;
74 }
75 
osdep_xendevicemodel_close(xendevicemodel_handle * dmod)76 int osdep_xendevicemodel_close(xendevicemodel_handle *dmod)
77 {
78     if (dmod->fd < 0)
79         return 0;
80 
81     return close(dmod->fd);
82 }
83 
osdep_xendevicemodel_op(xendevicemodel_handle * dmod,domid_t domid,unsigned int nr_bufs,struct xendevicemodel_buf bufs[])84 int osdep_xendevicemodel_op(xendevicemodel_handle *dmod,
85                             domid_t domid, unsigned int nr_bufs,
86                             struct xendevicemodel_buf bufs[])
87 {
88     privcmd_dm_op_buf_t *ubufs;
89     privcmd_dm_op_t uop;
90     unsigned int i;
91     int rc;
92 
93     if (dmod->fd < 0)
94         return xendevicemodel_xcall(dmod, domid, nr_bufs, bufs);
95 
96     ubufs = calloc(nr_bufs, sizeof (*ubufs));
97     if (!ubufs)
98         return -1;
99 
100     for (i = 0; i < nr_bufs; i++) {
101         ubufs[i].uptr = bufs[i].ptr;
102         ubufs[i].size = bufs[i].size;
103     }
104 
105     uop.dom = domid;
106     uop.num = nr_bufs;
107     uop.ubufs = ubufs;
108 
109     rc = ioctl(dmod->fd, IOCTL_PRIVCMD_DM_OP, &uop);
110 
111     free(ubufs);
112 
113     if (rc < 0)
114         return -1;
115 
116     return 0;
117 }
118 
osdep_xendevicemodel_restrict(xendevicemodel_handle * dmod,domid_t domid)119 int osdep_xendevicemodel_restrict(xendevicemodel_handle *dmod,
120                                   domid_t domid)
121 {
122     if (dmod->fd < 0) {
123         errno = EOPNOTSUPP;
124         return -1;
125     }
126 
127     return ioctl(dmod->fd, IOCTL_PRIVCMD_RESTRICT, &domid);
128 }
129 
130 /*
131  * Local variables:
132  * mode: C
133  * c-file-style: "BSD"
134  * c-basic-offset: 4
135  * tab-width: 4
136  * indent-tabs-mode: nil
137  * End:
138  */
139