debuggers.hg

view tools/libxc/xc_netbsd.c @ 16408:f669bf5c6720

libxc: Consolidate read()/write() syscall wrappers to read/write an
exact number of bytes. The consolidated versions are more watertight
than the various versions previously distributed around the library
source code.
Signed-off-by: Keir Fraser <keir@xensource.com>
author Keir Fraser <keir@xensource.com>
date Sun Nov 11 18:22:33 2007 +0000 (2007-11-11)
parents e73ea74cba30
children e948f402c356
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 return NULL;
69 ioctlx.num=num;
70 ioctlx.dom=dom;
71 ioctlx.addr=(unsigned long)addr;
72 ioctlx.arr=arr;
73 if ( ioctl(xc_handle, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx) < 0 )
74 {
75 int saved_errno = errno;
76 perror("XXXXXXXX");
77 (void)munmap(addr, num*PAGE_SIZE);
78 errno = saved_errno;
79 return NULL;
80 }
81 return addr;
83 }
85 void *xc_map_foreign_range(int xc_handle, uint32_t dom,
86 int size, int prot,
87 unsigned long mfn)
88 {
89 privcmd_mmap_t ioctlx;
90 privcmd_mmap_entry_t entry;
91 void *addr;
92 addr = mmap(NULL, size, prot, MAP_ANON | MAP_SHARED, -1, 0);
93 if ( addr == MAP_FAILED )
94 return NULL;
96 ioctlx.num=1;
97 ioctlx.dom=dom;
98 ioctlx.entry=&entry;
99 entry.va=(unsigned long) addr;
100 entry.mfn=mfn;
101 entry.npages=(size+PAGE_SIZE-1)>>PAGE_SHIFT;
102 if ( ioctl(xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx) < 0 )
103 {
104 int saved_errno = errno;
105 (void)munmap(addr, size);
106 errno = saved_errno;
107 return NULL;
108 }
109 return addr;
110 }
112 int xc_map_foreign_ranges(int xc_handle, uint32_t dom,
113 privcmd_mmap_entry_t *entries, int nr)
114 {
115 privcmd_mmap_t ioctlx;
116 int err;
118 ioctlx.num = nr;
119 ioctlx.dom = dom;
120 ioctlx.entry = entries;
122 err = ioctl(xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx);
123 if (err == 0)
124 return 0;
125 else
126 return -errno;
127 }
129 static int do_privcmd(int xc_handle, unsigned int cmd, unsigned long data)
130 {
131 int err = ioctl(xc_handle, cmd, data);
132 if (err == 0)
133 return 0;
134 else
135 return -errno;
136 }
138 int do_xen_hypercall(int xc_handle, privcmd_hypercall_t *hypercall)
139 {
140 return do_privcmd(xc_handle,
141 IOCTL_PRIVCMD_HYPERCALL,
142 (unsigned long)hypercall);
143 }
145 #define EVTCHN_DEV_NAME "/dev/xenevt"
147 int xc_evtchn_open(void)
148 {
149 return open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
150 }
152 int xc_evtchn_close(int xce_handle)
153 {
154 return close(xce_handle);
155 }
157 int xc_evtchn_fd(int xce_handle)
158 {
159 return xce_handle;
160 }
162 int xc_evtchn_notify(int xce_handle, evtchn_port_t port)
163 {
164 struct ioctl_evtchn_notify notify;
166 notify.port = port;
168 return ioctl(xce_handle, IOCTL_EVTCHN_NOTIFY, &notify);
169 }
171 evtchn_port_or_error_t
172 xc_evtchn_bind_interdomain(int xce_handle, int domid,
173 evtchn_port_t remote_port)
174 {
175 struct ioctl_evtchn_bind_interdomain bind;
176 int ret;
178 bind.remote_domain = domid;
179 bind.remote_port = remote_port;
181 ret = ioctl(xce_handle, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
182 if (ret == 0)
183 return bind.port;
184 else
185 return -1;
186 }
188 int xc_evtchn_unbind(int xce_handle, evtchn_port_t port)
189 {
190 struct ioctl_evtchn_unbind unbind;
192 unbind.port = port;
194 return ioctl(xce_handle, IOCTL_EVTCHN_UNBIND, &unbind);
195 }
197 evtchn_port_or_error_t
198 xc_evtchn_bind_virq(int xce_handle, unsigned int virq)
199 {
200 struct ioctl_evtchn_bind_virq bind;
201 int err;
203 bind.virq = virq;
205 err = ioctl(xce_handle, IOCTL_EVTCHN_BIND_VIRQ, &bind);
206 if (err)
207 return -1;
208 else
209 return bind.port;
210 }
212 evtchn_port_or_error_t
213 xc_evtchn_pending(int xce_handle)
214 {
215 evtchn_port_t port;
217 if ( read_exact(xce_handle, (char *)&port, sizeof(port)) == -1 )
218 return -1;
220 return port;
221 }
223 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port)
224 {
225 return write_exact(xce_handle, (char *)&port, sizeof(port));
226 }
228 /* Optionally flush file to disk and discard page cache */
229 void discard_file_cache(int fd, int flush)
230 {
232 if ( flush && (fsync(fd) < 0) )
233 {
234 /*PERROR("Failed to flush file: %s", strerror(errno));*/
235 }
236 }
238 /*
239 * Local variables:
240 * mode: C
241 * c-set-style: "BSD"
242 * c-basic-offset: 4
243 * tab-width: 4
244 * indent-tabs-mode: nil
245 * End:
246 */