aboutsummaryrefslogtreecommitdiffstats
path: root/tools/debugger/pdb/pdb_caml_evtchn.c
blob: 62707739cab0e5a31dc8a314665c1a03dc78be30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>


#include <errno.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int xen_evtchn_bind (int evtchn_fd, int idx);
int xen_evtchn_unbind (int evtchn_fd, int idx);

int
__evtchn_open (char *filename, int major, int minor)
{
    int   evtchn_fd;
    struct stat st;
    
    /* Make sure any existing device file links to correct device. */
    if ( (lstat(filename, &st) != 0) ||
         !S_ISCHR(st.st_mode) ||
         (st.st_rdev != makedev(major, minor)) )
    {
        (void)unlink(filename);
    }

 reopen:
    evtchn_fd = open(filename, O_RDWR); 
    if ( evtchn_fd == -1 )
    {
        if ( (errno == ENOENT) &&
             ((mkdir("/dev/xen", 0755) == 0) || (errno == EEXIST)) &&
             (mknod(filename, S_IFCHR|0600, makedev(major,minor)) == 0) )
        {
            goto reopen;
        }
        return -errno;
    }

    return evtchn_fd;
}

/*
 * evtchn_open : string -> int -> int -> Unix.file_descr
 *
 * OCaml's Unix library doesn't have mknod, so it makes more sense just write
 * this in C.  This code is from Keir/Andy.
 */
value
evtchn_open (value filename, value major, value minor)
{
    CAMLparam3(filename, major, minor);

    char *myfilename = String_val(filename);
    int   mymajor = Int_val(major);
    int   myminor = Int_val(minor);
    int   evtchn_fd;

    evtchn_fd = __evtchn_open(myfilename, mymajor, myminor);

    CAMLreturn(Val_int(evtchn_fd));
}

/*
 * evtchn_bind : Unix.file_descr -> int -> unit
 */
value
evtchn_bind (value fd, value idx)
{
    CAMLparam2(fd, idx);

    int myfd = Int_val(fd);
    int myidx = Int_val(idx);

    if ( xen_evtchn_bind(myfd, myidx) < 0 )
    {
        printf("(pdb) evtchn_bind error!\n");  fflush(stdout);
        failwith("evtchn_bind error");
    }

    CAMLreturn(Val_unit);
}

/*
 * evtchn_unbind : Unix.file_descr -> int -> unit
 */
value
evtchn_unbind (value fd, value idx)
{
    CAMLparam2(fd, idx);

    int myfd = Int_val(fd);
    int myidx = Int_val(idx);

    if ( xen_evtchn_unbind(myfd, myidx) < 0 )
    {
        printf("(pdb) evtchn_unbind error!\n");  fflush(stdout);
        failwith("evtchn_unbind error");
    }

    CAMLreturn(Val_unit);
}

/*
 * evtchn_read : Unix.file_descr -> int
 */
value
evtchn_read (value fd)
{
    CAMLparam1(fd);

    u16 v;
    int bytes;
    int rc = -1;
    int myfd = Int_val(fd);

    while ( (bytes = read(myfd, &v, sizeof(v))) == -1 )
    {
        if ( errno == EINTR )  continue;
        rc = -errno;
        goto exit;
    }
    
    if ( bytes == sizeof(v) )
        rc = v;
    
 exit:
    CAMLreturn(Val_int(rc));
}


/*
 * evtchn_close : Unix.file_descr -> unit
 */
value
evtchn_close (value fd)
{
    CAMLparam1(fd);
    int myfd = Int_val(fd);

    (void)close(myfd);

    CAMLreturn(Val_unit);
}

/*
 * evtchn_unmask : Unix.file_descr -> int -> unit
 */
value
evtchn_unmask (value fd, value idx)
{
    CAMLparam1(fd);

    int myfd = Int_val(fd);
    u16 myidx = Int_val(idx);

    (void)write(myfd, &myidx, sizeof(myidx));

    CAMLreturn(Val_unit);
}

/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */