ia64/xen-unstable

view tools/xcs/evtchn.c @ 3966:0b9ff7354a95

bitkeeper revision 1.1236.1.47 (4225ff68LvKUt73k78dKjnXxsnQt7A)

Fix tools build. Clean up barrier defs.
Signed-off-by: Jerone Young <jyoung5@us.ibm.com>
Signed-off-by: Keir Fraser <keir.fraser@cl.cam.ac.uk>
author kaf24@scramble.cl.cam.ac.uk
date Wed Mar 02 18:01:12 2005 +0000 (2005-03-02)
parents 0a4b76b6b5a0
children 095b6204d7f7 8ba8ae4eba9b a01199a95070
line source
1 /* evtchn.c
2 *
3 * Interfaces to event channel driver.
4 *
5 * Most of this is directly based on the original xu interface to python
6 * written by Keir Fraser.
7 *
8 * (c) 2004, Andrew Warfield
9 *
10 */
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/ioctl.h>
15 #include <sys/stat.h>
16 #include <sys/sysmacros.h> /* XOPEN drops makedev, this gets it back. */
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include "xcs.h"
22 static int evtchn_fd = -1;
24 /* NB. The following should be kept in sync with the kernel's evtchn driver. */
25 #define EVTCHN_DEV_NAME "/dev/xen/evtchn"
26 #define EVTCHN_DEV_MAJOR 10
27 #define EVTCHN_DEV_MINOR 201
28 /* /dev/xen/evtchn ioctls: */
29 /* EVTCHN_RESET: Clear and reinit the event buffer. Clear error condition. */
30 #define EVTCHN_RESET _IO('E', 1)
31 /* EVTCHN_BIND: Bind to teh specified event-channel port. */
32 #define EVTCHN_BIND _IO('E', 2)
33 /* EVTCHN_UNBIND: Unbind from the specified event-channel port. */
34 #define EVTCHN_UNBIND _IO('E', 3)
36 int evtchn_read()
37 {
38 u16 v;
39 int bytes;
41 while ( (bytes = read(evtchn_fd, &v, sizeof(v))) == -1 )
42 {
43 if ( errno == EINTR )
44 continue;
45 /* EAGAIN was cased to return 'None' in the python version... */
46 return -errno;
47 }
49 if ( bytes == sizeof(v) )
50 return v;
52 /* bad return */
53 return -1;
54 }
56 void evtchn_unmask(u16 idx)
57 {
58 (void)write(evtchn_fd, &idx, sizeof(idx));
59 }
61 int evtchn_bind(int idx)
62 {
63 if ( ioctl(evtchn_fd, EVTCHN_BIND, idx) != 0 )
64 return -errno;
66 return 0;
67 }
69 int evtchn_unbind(int idx)
70 {
71 if ( ioctl(evtchn_fd, EVTCHN_UNBIND, idx) != 0 )
72 return -errno;
74 return 0;
75 }
77 int evtchn_open(void)
78 {
79 struct stat st;
81 /* Make sure any existing device file links to correct device. */
82 if ( (lstat(EVTCHN_DEV_NAME, &st) != 0) ||
83 !S_ISCHR(st.st_mode) ||
84 (st.st_rdev != makedev(EVTCHN_DEV_MAJOR, EVTCHN_DEV_MINOR)) )
85 (void)unlink(EVTCHN_DEV_NAME);
87 reopen:
88 evtchn_fd = open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
89 if ( evtchn_fd == -1 )
90 {
91 if ( (errno == ENOENT) &&
92 ((mkdir("/dev/xen", 0755) == 0) || (errno == EEXIST)) &&
93 (mknod(EVTCHN_DEV_NAME, S_IFCHR|0600,
94 makedev(EVTCHN_DEV_MAJOR,EVTCHN_DEV_MINOR)) == 0) )
95 goto reopen;
96 return -errno;
97 }
98 /*set_cloexec(evtchn_fd); -- no longer required*/
99 printf("Eventchan_fd is %d\n", evtchn_fd);
100 return evtchn_fd;
101 }
103 void evtchn_close()
104 {
105 (void)close(evtchn_fd);
106 evtchn_fd = -1;
107 }