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 from xc_netbsd.c
20 */
21
22 #include "xc_private.h"
23
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <malloc.h>
27 #include <sys/mman.h>
28
osdep_xencall_open(xencall_handle * xcall)29 int osdep_xencall_open(xencall_handle *xcall)
30 {
31 int flags, saved_errno;
32 int fd = open("/kern/xen/privcmd", O_RDWR);
33
34 if ( fd == -1 )
35 {
36 PERROR("Could not obtain handle on privileged command interface");
37 return -1;
38 }
39
40 /* Although we return the file handle as the 'xc handle' the API
41 does not specify / guarentee that this integer is in fact
42 a file handle. Thus we must take responsiblity to ensure
43 it doesn't propagate (ie leak) outside the process */
44 if ( (flags = fcntl(fd, F_GETFD)) < 0 )
45 {
46 PERROR("Could not get file handle flags");
47 goto error;
48 }
49 flags |= FD_CLOEXEC;
50 if ( fcntl(fd, F_SETFD, flags) < 0 )
51 {
52 PERROR("Could not set file handle flags");
53 goto error;
54 }
55
56 xcall->fd = fd;
57 return 0;
58
59 error:
60 saved_errno = errno;
61 close(fd);
62 errno = saved_errno;
63 return -1;
64 }
65
osdep_xencall_close(xencall_handle * xcall)66 int osdep_xencall_close(xencall_handle *xcall)
67 {
68 int fd = xcall->fd;
69 return close(fd);
70 }
71
osdep_alloc_hypercall_buffer(xencall_handle * xcall,size_t npages)72 void *osdep_alloc_hypercall_buffer(xencall_handle *xcall, size_t npages)
73 {
74 size_t size = npages * XC_PAGE_SIZE;
75 void *p;
76
77 ret = posix_memalign(&p, XC_PAGE_SIZE, size);
78 if ( ret != 0 || !p )
79 return NULL;
80
81 if ( mlock(p, size) < 0 )
82 {
83 free(p);
84 return NULL;
85 }
86 return p;
87 }
88
osdep_free_hypercall_buffer(xencall_handle * xcall,void * ptr,size_t npages)89 void osdep_free_hypercall_buffer(xencall_handle *xcall, void *ptr,
90 size_t npages)
91 {
92 (void) munlock(ptr, npages * XC_PAGE_SIZE);
93 free(ptr);
94 }
95
do_xen_hypercall(xencall_handle * xcall,privcmd_hypercall_t * hypercall)96 int do_xen_hypercall(xencall_handle *xcall, privcmd_hypercall_t *hypercall)
97 {
98 int fd = xcall->fd;
99 int error = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
100
101 /*
102 * Since NetBSD ioctl can only return 0 on success or < 0 on
103 * error, if we want to return a value from ioctl we should
104 * do so by setting hypercall->retval, to mimic Linux ioctl
105 * implementation.
106 */
107 if (error < 0)
108 return error;
109 else
110 return hypercall->retval;
111 }
112
xencall_buffers_never_fault(xencall_handle * xcall)113 int xencall_buffers_never_fault(xencall_handle *xcall)
114 {
115 return 1;
116 }
117
118 /*
119 * Local variables:
120 * mode: C
121 * c-file-style: "BSD"
122 * c-basic-offset: 4
123 * tab-width: 4
124 * indent-tabs-mode: nil
125 * End:
126 */
127