debuggers.hg

view tools/libxc/xc_netbsd.c @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents 5cb466187e1f
children 3ffdb094c2c0 779c0ef9682c
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/sys/evtchn.h>
15 #include <unistd.h>
16 #include <fcntl.h>
18 int xc_interface_open(void)
19 {
20 int flags, saved_errno;
21 int fd = open("/kern/xen/privcmd", O_RDWR);
23 if ( fd == -1 )
24 {
25 PERROR("Could not obtain handle on privileged command interface");
26 return -1;
27 }
29 /* Although we return the file handle as the 'xc handle' the API
30 does not specify / guarentee that this integer is in fact
31 a file handle. Thus we must take responsiblity to ensure
32 it doesn't propagate (ie leak) outside the process */
33 if ( (flags = fcntl(fd, F_GETFD)) < 0 )
34 {
35 PERROR("Could not get file handle flags");
36 goto error;
37 }
38 flags |= FD_CLOEXEC;
39 if ( fcntl(fd, F_SETFD, flags) < 0 )
40 {
41 PERROR("Could not set file handle flags");
42 goto error;
43 }
45 return fd;
47 error:
48 saved_errno = errno;
49 close(fd);
50 errno = saved_errno;
51 return -1;
52 }
54 int xc_interface_close(int xc_handle)
55 {
56 return close(xc_handle);
57 }
59 void *xc_map_foreign_batch(int xc_handle, uint32_t dom, int prot,
60 xen_pfn_t *arr, int num)
61 {
62 privcmd_mmapbatch_t ioctlx;
63 void *addr;
64 addr = mmap(NULL, num*PAGE_SIZE, prot, MAP_ANON | MAP_SHARED, -1, 0);
65 if ( addr == MAP_FAILED ) {
66 perror("xc_map_foreign_batch: mmap failed");
67 return NULL;
68 }
70 ioctlx.num=num;
71 ioctlx.dom=dom;
72 ioctlx.addr=(unsigned long)addr;
73 ioctlx.arr=arr;
74 if ( ioctl(xc_handle, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx) < 0 )
75 {
76 int saved_errno = errno;
77 perror("xc_map_foreign_batch: ioctl failed");
78 (void)munmap(addr, num*PAGE_SIZE);
79 errno = saved_errno;
80 return NULL;
81 }
82 return addr;
84 }
86 void *xc_map_foreign_range(int xc_handle, uint32_t dom,
87 int size, int prot,
88 unsigned long mfn)
89 {
90 privcmd_mmap_t ioctlx;
91 privcmd_mmap_entry_t entry;
92 void *addr;
93 addr = mmap(NULL, size, prot, MAP_ANON | MAP_SHARED, -1, 0);
94 if ( addr == MAP_FAILED ) {
95 perror("xc_map_foreign_range: mmap failed");
96 return NULL;
97 }
99 ioctlx.num=1;
100 ioctlx.dom=dom;
101 ioctlx.entry=&entry;
102 entry.va=(unsigned long) addr;
103 entry.mfn=mfn;
104 entry.npages=(size+PAGE_SIZE-1)>>PAGE_SHIFT;
105 if ( ioctl(xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx) < 0 )
106 {
107 int saved_errno = errno;
108 perror("xc_map_foreign_range: ioctl failed");
109 (void)munmap(addr, size);
110 errno = saved_errno;
111 return NULL;
112 }
113 return addr;
114 }
116 void *xc_map_foreign_ranges(int xc_handle, uint32_t dom,
117 size_t size, int prot, size_t chunksize,
118 privcmd_mmap_entry_t entries[], int nentries)
119 {
120 privcmd_mmap_t ioctlx;
121 int i, rc;
122 void *addr;
124 addr = mmap(NULL, size, prot, MAP_ANON | MAP_SHARED, -1, 0);
125 if (addr == MAP_FAILED)
126 goto mmap_failed;
128 for (i = 0; i < nentries; i++) {
129 entries[i].va = (uintptr_t)addr + (i * chunksize);
130 entries[i].npages = chunksize >> PAGE_SHIFT;
131 }
133 ioctlx.num = nentries;
134 ioctlx.dom = dom;
135 ioctlx.entry = entries;
137 rc = ioctl(xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx);
138 if (rc)
139 goto ioctl_failed;
141 return addr;
143 ioctl_failed:
144 rc = munmap(addr, size);
145 if (rc == -1)
146 ERROR("%s: error in error path\n", __FUNCTION__);
148 mmap_failed:
149 return NULL;
150 }
153 static int do_privcmd(int xc_handle, unsigned int cmd, unsigned long data)
154 {
155 int err = ioctl(xc_handle, cmd, data);
156 if (err == 0)
157 return 0;
158 else
159 return -errno;
160 }
162 int do_xen_hypercall(int xc_handle, privcmd_hypercall_t *hypercall)
163 {
164 int error = do_privcmd(xc_handle,
165 IOCTL_PRIVCMD_HYPERCALL,
166 (unsigned long)hypercall);
167 if (error)
168 return error;
169 else
170 return (hypercall->retval);
171 }
173 #define EVTCHN_DEV_NAME "/dev/xenevt"
175 int xc_evtchn_open(void)
176 {
177 return open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
178 }
180 int xc_evtchn_close(int xce_handle)
181 {
182 return close(xce_handle);
183 }
185 int xc_evtchn_fd(int xce_handle)
186 {
187 return xce_handle;
188 }
190 int xc_evtchn_notify(int xce_handle, evtchn_port_t port)
191 {
192 struct ioctl_evtchn_notify notify;
194 notify.port = port;
196 return ioctl(xce_handle, IOCTL_EVTCHN_NOTIFY, &notify);
197 }
199 evtchn_port_or_error_t
200 xc_evtchn_bind_interdomain(int xce_handle, int domid,
201 evtchn_port_t remote_port)
202 {
203 struct ioctl_evtchn_bind_interdomain bind;
204 int ret;
206 bind.remote_domain = domid;
207 bind.remote_port = remote_port;
209 ret = ioctl(xce_handle, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
210 if (ret == 0)
211 return bind.port;
212 else
213 return -1;
214 }
216 int xc_evtchn_unbind(int xce_handle, evtchn_port_t port)
217 {
218 struct ioctl_evtchn_unbind unbind;
220 unbind.port = port;
222 return ioctl(xce_handle, IOCTL_EVTCHN_UNBIND, &unbind);
223 }
225 evtchn_port_or_error_t
226 xc_evtchn_bind_virq(int xce_handle, unsigned int virq)
227 {
228 struct ioctl_evtchn_bind_virq bind;
229 int err;
231 bind.virq = virq;
233 err = ioctl(xce_handle, IOCTL_EVTCHN_BIND_VIRQ, &bind);
234 if (err)
235 return -1;
236 else
237 return bind.port;
238 }
240 evtchn_port_or_error_t
241 xc_evtchn_pending(int xce_handle)
242 {
243 evtchn_port_t port;
245 if ( read_exact(xce_handle, (char *)&port, sizeof(port)) == -1 )
246 return -1;
248 return port;
249 }
251 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port)
252 {
253 return write_exact(xce_handle, (char *)&port, sizeof(port));
254 }
256 /* Optionally flush file to disk and discard page cache */
257 void discard_file_cache(int fd, int flush)
258 {
260 if ( flush && (fsync(fd) < 0) )
261 {
262 /*PERROR("Failed to flush file: %s", strerror(errno));*/
263 }
264 }
266 grant_entry_v1_t *xc_gnttab_map_table_v1(
267 int xc_handle, int domid, int *gnt_num)
268 {
269 return NULL;
270 }
272 grant_entry_v2_t *xc_gnttab_map_table_v2(
273 int xc_handle, int domid, int *gnt_num)
274 {
275 return NULL;
276 }
278 /*
279 * Local variables:
280 * mode: C
281 * c-set-style: "BSD"
282 * c-basic-offset: 4
283 * tab-width: 4
284 * indent-tabs-mode: nil
285 * End:
286 */