ia64/xen-unstable

view tools/debugger/pdb/pdb_caml_evtchn.c @ 6427:3428d58a85e1

merge?
author cl349@firebug.cl.cam.ac.uk
date Thu Aug 25 14:41:52 2005 +0000 (2005-08-25)
parents 4abd299ef2f6 6e899a3840b2
children b54144915ae6
line source
1 /*
2 * pdb_caml_evtchn.c
3 *
4 * http://www.cl.cam.ac.uk/netos/pdb
5 *
6 * PDB's OCaml interface library for event channels
7 */
9 #include <xenctrl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include <caml/alloc.h>
15 #include <caml/fail.h>
16 #include <caml/memory.h>
17 #include <caml/mlvalues.h>
20 #include <errno.h>
21 #include <sys/ioctl.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
26 int xen_evtchn_bind (int evtchn_fd, int idx);
27 int xen_evtchn_unbind (int evtchn_fd, int idx);
29 int
30 __evtchn_open (char *filename, int major, int minor)
31 {
32 int evtchn_fd;
33 struct stat st;
35 /* Make sure any existing device file links to correct device. */
36 if ( (lstat(filename, &st) != 0) ||
37 !S_ISCHR(st.st_mode) ||
38 (st.st_rdev != makedev(major, minor)) )
39 {
40 (void)unlink(filename);
41 }
43 reopen:
44 evtchn_fd = open(filename, O_RDWR);
45 if ( evtchn_fd == -1 )
46 {
47 if ( (errno == ENOENT) &&
48 ((mkdir("/dev/xen", 0755) == 0) || (errno == EEXIST)) &&
49 (mknod(filename, S_IFCHR|0600, makedev(major,minor)) == 0) )
50 {
51 goto reopen;
52 }
53 return -errno;
54 }
56 return evtchn_fd;
57 }
59 /*
60 * evtchn_open : string -> int -> int -> Unix.file_descr
61 *
62 * OCaml's Unix library doesn't have mknod, so it makes more sense just write
63 * this in C. This code is from Keir/Andy.
64 */
65 value
66 evtchn_open (value filename, value major, value minor)
67 {
68 CAMLparam3(filename, major, minor);
70 char *myfilename = String_val(filename);
71 int mymajor = Int_val(major);
72 int myminor = Int_val(minor);
73 int evtchn_fd;
75 evtchn_fd = __evtchn_open(myfilename, mymajor, myminor);
77 CAMLreturn(Val_int(evtchn_fd));
78 }
80 /*
81 * evtchn_bind : Unix.file_descr -> int -> unit
82 */
83 value
84 evtchn_bind (value fd, value idx)
85 {
86 CAMLparam2(fd, idx);
88 int myfd = Int_val(fd);
89 int myidx = Int_val(idx);
91 if ( xen_evtchn_bind(myfd, myidx) < 0 )
92 {
93 printf("(pdb) evtchn_bind error!\n"); fflush(stdout);
94 failwith("evtchn_bind error");
95 }
97 CAMLreturn(Val_unit);
98 }
100 /*
101 * evtchn_unbind : Unix.file_descr -> int -> unit
102 */
103 value
104 evtchn_unbind (value fd, value idx)
105 {
106 CAMLparam2(fd, idx);
108 int myfd = Int_val(fd);
109 int myidx = Int_val(idx);
111 if ( xen_evtchn_unbind(myfd, myidx) < 0 )
112 {
113 printf("(pdb) evtchn_unbind error!\n"); fflush(stdout);
114 failwith("evtchn_unbind error");
115 }
117 CAMLreturn(Val_unit);
118 }
120 /*
121 * evtchn_read : Unix.file_descr -> int
122 */
123 value
124 evtchn_read (value fd)
125 {
126 CAMLparam1(fd);
128 u16 v;
129 int bytes;
130 int rc = -1;
131 int myfd = Int_val(fd);
133 while ( (bytes = read(myfd, &v, sizeof(v))) == -1 )
134 {
135 if ( errno == EINTR ) continue;
136 rc = -errno;
137 goto exit;
138 }
140 if ( bytes == sizeof(v) )
141 rc = v;
143 exit:
144 CAMLreturn(Val_int(rc));
145 }
148 /*
149 * evtchn_close : Unix.file_descr -> unit
150 */
151 value
152 evtchn_close (value fd)
153 {
154 CAMLparam1(fd);
155 int myfd = Int_val(fd);
157 (void)close(myfd);
159 CAMLreturn(Val_unit);
160 }
162 /*
163 * evtchn_unmask : Unix.file_descr -> int -> unit
164 */
165 value
166 evtchn_unmask (value fd, value idx)
167 {
168 CAMLparam1(fd);
170 int myfd = Int_val(fd);
171 u16 myidx = Int_val(idx);
173 (void)write(myfd, &myidx, sizeof(myidx));
175 CAMLreturn(Val_unit);
176 }
178 /*
179 * Local variables:
180 * mode: C
181 * c-set-style: "BSD"
182 * c-basic-offset: 4
183 * tab-width: 4
184 * indent-tabs-mode: nil
185 * End:
186 */