ia64/xen-unstable

view tools/libxc/xc_netbsd.c @ 17821:8a36f7f70859

libxc: Update for NetBSD

returns hypercall return value when ioctl was successful.

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jun 10 13:49:02 2008 +0100 (2008-06-10)
parents e948f402c356
children 7299346111fb
line source
1 /******************************************************************************
2 *
3 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
4 * Use is subject to license terms.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2 of the
9 * License.
10 */
12 #include "xc_private.h"
14 #include <xen/memory.h>
15 #include <xen/sys/evtchn.h>
16 #include <unistd.h>
17 #include <fcntl.h>
19 int xc_interface_open(void)
20 {
21 int flags, saved_errno;
22 int fd = open("/kern/xen/privcmd", O_RDWR);
24 if ( fd == -1 )
25 {
26 PERROR("Could not obtain handle on privileged command interface");
27 return -1;
28 }
30 /* Although we return the file handle as the 'xc handle' the API
31 does not specify / guarentee that this integer is in fact
32 a file handle. Thus we must take responsiblity to ensure
33 it doesn't propagate (ie leak) outside the process */
34 if ( (flags = fcntl(fd, F_GETFD)) < 0 )
35 {
36 PERROR("Could not get file handle flags");
37 goto error;
38 }
39 flags |= FD_CLOEXEC;
40 if ( fcntl(fd, F_SETFD, flags) < 0 )
41 {
42 PERROR("Could not set file handle flags");
43 goto error;
44 }
46 return fd;
48 error:
49 saved_errno = errno;
50 close(fd);
51 errno = saved_errno;
52 return -1;
53 }
55 int xc_interface_close(int xc_handle)
56 {
57 return close(xc_handle);
58 }
60 void *xc_map_foreign_batch(int xc_handle, uint32_t dom, int prot,
61 xen_pfn_t *arr, int num)
62 {
63 privcmd_mmapbatch_t ioctlx;
64 void *addr;
65 addr = mmap(NULL, num*PAGE_SIZE, prot, MAP_ANON | MAP_SHARED, -1, 0);
66 if ( addr == MAP_FAILED ) {
67 perror("xc_map_foreign_batch: mmap failed");
68 return NULL;
69 }
71 ioctlx.num=num;
72 ioctlx.dom=dom;
73 ioctlx.addr=(unsigned long)addr;
74 ioctlx.arr=arr;
75 if ( ioctl(xc_handle, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx) < 0 )
76 {
77 int saved_errno = errno;
78 perror("xc_map_foreign_batch: ioctl failed");
79 (void)munmap(addr, num*PAGE_SIZE);
80 errno = saved_errno;
81 return NULL;
82 }
83 return addr;
85 }
87 void *xc_map_foreign_range(int xc_handle, uint32_t dom,
88 int size, int prot,
89 unsigned long mfn)
90 {
91 privcmd_mmap_t ioctlx;
92 privcmd_mmap_entry_t entry;
93 void *addr;
94 addr = mmap(NULL, size, prot, MAP_ANON | MAP_SHARED, -1, 0);
95 if ( addr == MAP_FAILED ) {
96 perror("xc_map_foreign_range: mmap failed");
97 return NULL;
98 }
100 ioctlx.num=1;
101 ioctlx.dom=dom;
102 ioctlx.entry=&entry;
103 entry.va=(unsigned long) addr;
104 entry.mfn=mfn;
105 entry.npages=(size+PAGE_SIZE-1)>>PAGE_SHIFT;
106 if ( ioctl(xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx) < 0 )
107 {
108 int saved_errno = errno;
109 perror("xc_map_foreign_range: ioctl failed");
110 (void)munmap(addr, size);
111 errno = saved_errno;
112 return NULL;
113 }
114 return addr;
115 }
117 int xc_map_foreign_ranges(int xc_handle, uint32_t dom,
118 privcmd_mmap_entry_t *entries, int nr)
119 {
120 privcmd_mmap_t ioctlx;
121 int err;
123 ioctlx.num = nr;
124 ioctlx.dom = dom;
125 ioctlx.entry = entries;
127 err = ioctl(xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx);
128 if (err == 0)
129 return 0;
130 else
131 return -errno;
132 }
134 static int do_privcmd(int xc_handle, unsigned int cmd, unsigned long data)
135 {
136 int err = ioctl(xc_handle, cmd, data);
137 if (err == 0)
138 return 0;
139 else
140 return -errno;
141 }
143 int do_xen_hypercall(int xc_handle, privcmd_hypercall_t *hypercall)
144 {
145 int error = do_privcmd(xc_handle,
146 IOCTL_PRIVCMD_HYPERCALL,
147 (unsigned long)hypercall);
148 if (error)
149 return error;
150 else
151 return (hypercall->retval);
152 }
154 #define EVTCHN_DEV_NAME "/dev/xenevt"
156 int xc_evtchn_open(void)
157 {
158 return open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
159 }
161 int xc_evtchn_close(int xce_handle)
162 {
163 return close(xce_handle);
164 }
166 int xc_evtchn_fd(int xce_handle)
167 {
168 return xce_handle;
169 }
171 int xc_evtchn_notify(int xce_handle, evtchn_port_t port)
172 {
173 struct ioctl_evtchn_notify notify;
175 notify.port = port;
177 return ioctl(xce_handle, IOCTL_EVTCHN_NOTIFY, &notify);
178 }
180 evtchn_port_or_error_t
181 xc_evtchn_bind_interdomain(int xce_handle, int domid,
182 evtchn_port_t remote_port)
183 {
184 struct ioctl_evtchn_bind_interdomain bind;
185 int ret;
187 bind.remote_domain = domid;
188 bind.remote_port = remote_port;
190 ret = ioctl(xce_handle, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
191 if (ret == 0)
192 return bind.port;
193 else
194 return -1;
195 }
197 int xc_evtchn_unbind(int xce_handle, evtchn_port_t port)
198 {
199 struct ioctl_evtchn_unbind unbind;
201 unbind.port = port;
203 return ioctl(xce_handle, IOCTL_EVTCHN_UNBIND, &unbind);
204 }
206 evtchn_port_or_error_t
207 xc_evtchn_bind_virq(int xce_handle, unsigned int virq)
208 {
209 struct ioctl_evtchn_bind_virq bind;
210 int err;
212 bind.virq = virq;
214 err = ioctl(xce_handle, IOCTL_EVTCHN_BIND_VIRQ, &bind);
215 if (err)
216 return -1;
217 else
218 return bind.port;
219 }
221 evtchn_port_or_error_t
222 xc_evtchn_pending(int xce_handle)
223 {
224 evtchn_port_t port;
226 if ( read_exact(xce_handle, (char *)&port, sizeof(port)) == -1 )
227 return -1;
229 return port;
230 }
232 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port)
233 {
234 return write_exact(xce_handle, (char *)&port, sizeof(port));
235 }
237 /* Optionally flush file to disk and discard page cache */
238 void discard_file_cache(int fd, int flush)
239 {
241 if ( flush && (fsync(fd) < 0) )
242 {
243 /*PERROR("Failed to flush file: %s", strerror(errno));*/
244 }
245 }
247 /*
248 * Local variables:
249 * mode: C
250 * c-set-style: "BSD"
251 * c-basic-offset: 4
252 * tab-width: 4
253 * indent-tabs-mode: nil
254 * End:
255 */