debuggers.hg

view tools/libxc/xc_netbsd.c @ 16715:c5deb251b9dc

Update version to 3.2.0-rc4
author Keir Fraser <keir.fraser@citrix.com>
date Sat Dec 29 17:57:37 2007 +0000 (2007-12-29)
parents e948f402c356
children 8a36f7f70859
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 return do_privcmd(xc_handle,
146 IOCTL_PRIVCMD_HYPERCALL,
147 (unsigned long)hypercall);
148 }
150 #define EVTCHN_DEV_NAME "/dev/xenevt"
152 int xc_evtchn_open(void)
153 {
154 return open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
155 }
157 int xc_evtchn_close(int xce_handle)
158 {
159 return close(xce_handle);
160 }
162 int xc_evtchn_fd(int xce_handle)
163 {
164 return xce_handle;
165 }
167 int xc_evtchn_notify(int xce_handle, evtchn_port_t port)
168 {
169 struct ioctl_evtchn_notify notify;
171 notify.port = port;
173 return ioctl(xce_handle, IOCTL_EVTCHN_NOTIFY, &notify);
174 }
176 evtchn_port_or_error_t
177 xc_evtchn_bind_interdomain(int xce_handle, int domid,
178 evtchn_port_t remote_port)
179 {
180 struct ioctl_evtchn_bind_interdomain bind;
181 int ret;
183 bind.remote_domain = domid;
184 bind.remote_port = remote_port;
186 ret = ioctl(xce_handle, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
187 if (ret == 0)
188 return bind.port;
189 else
190 return -1;
191 }
193 int xc_evtchn_unbind(int xce_handle, evtchn_port_t port)
194 {
195 struct ioctl_evtchn_unbind unbind;
197 unbind.port = port;
199 return ioctl(xce_handle, IOCTL_EVTCHN_UNBIND, &unbind);
200 }
202 evtchn_port_or_error_t
203 xc_evtchn_bind_virq(int xce_handle, unsigned int virq)
204 {
205 struct ioctl_evtchn_bind_virq bind;
206 int err;
208 bind.virq = virq;
210 err = ioctl(xce_handle, IOCTL_EVTCHN_BIND_VIRQ, &bind);
211 if (err)
212 return -1;
213 else
214 return bind.port;
215 }
217 evtchn_port_or_error_t
218 xc_evtchn_pending(int xce_handle)
219 {
220 evtchn_port_t port;
222 if ( read_exact(xce_handle, (char *)&port, sizeof(port)) == -1 )
223 return -1;
225 return port;
226 }
228 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port)
229 {
230 return write_exact(xce_handle, (char *)&port, sizeof(port));
231 }
233 /* Optionally flush file to disk and discard page cache */
234 void discard_file_cache(int fd, int flush)
235 {
237 if ( flush && (fsync(fd) < 0) )
238 {
239 /*PERROR("Failed to flush file: %s", strerror(errno));*/
240 }
241 }
243 /*
244 * Local variables:
245 * mode: C
246 * c-set-style: "BSD"
247 * c-basic-offset: 4
248 * tab-width: 4
249 * indent-tabs-mode: nil
250 * End:
251 */