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_solaris.c
20  */
21 
22 #include "xc_private.h"
23 
24 #include <xen/memory.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <malloc.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("/dev/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     return memalign(XC_PAGE_SIZE, npages * XC_PAGE_SIZE);
75 }
76 
osdep_free_hypercall_buffer(xencall_handle * xcall,void * ptr,size_t npages)77 void osdep_free_hypercall_buffer(xencall_handle *xcall, void *ptr,
78                                  size_t npages)
79 {
80     free(ptr);
81 }
82 
do_xen_hypercall(xencall_handle * xcall,privcmd_hypercall_t * hypercall)83 int do_xen_hypercall(xencall_handle *xcall, privcmd_hypercall_t *hypercall)
84 {
85     int fd = xcall->fd;
86     return ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
87 }
88 
xencall_buffers_never_fault(xencall_handle * xcall)89 int xencall_buffers_never_fault(xencall_handle *xcall)
90 {
91     return 1;
92 }
93 
94 /*
95  * Local variables:
96  * mode: C
97  * c-file-style: "BSD"
98  * c-basic-offset: 4
99  * tab-width: 4
100  * indent-tabs-mode: nil
101  * End:
102  */
103