| 1 |
/* |
| 2 |
* Copyright (C) 2006-2007 XenSource Ltd. |
| 3 |
* Copyright (C) 2008 Citrix Ltd. |
| 4 |
* Author Vincent Hanquez <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as published |
| 8 |
* by the Free Software Foundation; version 2.1 only. with the special |
| 9 |
* exception on linking described in file LICENSE. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Lesser General Public License for more details. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <sys/types.h> |
| 18 |
#include <sys/stat.h> |
| 19 |
#include <fcntl.h> |
| 20 |
#include <unistd.h> |
| 21 |
#include <errno.h> |
| 22 |
#include <stdint.h> |
| 23 |
#include <sys/ioctl.h> |
| 24 |
#include <xen/xen.h> |
| 25 |
#include <xenevtchn.h> |
| 26 |
|
| 27 |
#define CAML_NAME_SPACE |
| 28 |
#include <caml/mlvalues.h> |
| 29 |
#include <caml/memory.h> |
| 30 |
#include <caml/alloc.h> |
| 31 |
#include <caml/custom.h> |
| 32 |
#include <caml/callback.h> |
| 33 |
#include <caml/fail.h> |
| 34 |
#include <caml/signals.h> |
| 35 |
|
| 36 |
static inline xenevtchn_handle *xce_of_val(value v) |
| 37 |
{ |
| 38 |
return *(xenevtchn_handle **)Data_custom_val(v); |
| 39 |
} |
| 40 |
|
| 41 |
static void stub_evtchn_finalize(value v) |
| 42 |
{ |
| 43 |
xenevtchn_close(xce_of_val(v)); |
| 44 |
} |
| 45 |
|
| 46 |
static struct custom_operations xenevtchn_ops = { |
| 47 |
.identifier = "xenevtchn", |
| 48 |
.finalize = stub_evtchn_finalize, |
| 49 |
.compare = custom_compare_default, /* Can't compare */ |
| 50 |
.hash = custom_hash_default, /* Can't hash */ |
| 51 |
.serialize = custom_serialize_default, /* Can't serialize */ |
| 52 |
.deserialize = custom_deserialize_default, /* Can't deserialize */ |
| 53 |
.compare_ext = custom_compare_ext_default, /* Can't compare */ |
| 54 |
}; |
| 55 |
|
| 56 |
CAMLprim value stub_eventchn_init(value cloexec) |
| 57 |
{ |
| 58 |
CAMLparam1(cloexec); |
| 59 |
CAMLlocal1(result); |
| 60 |
xenevtchn_handle *xce; |
| 61 |
unsigned int flags = 0; |
| 62 |
|
| 63 |
if ( !Bool_val(cloexec) ) |
| 64 |
flags |= XENEVTCHN_NO_CLOEXEC; |
| 65 |
|
| 66 |
result = caml_alloc_custom(&xenevtchn_ops, sizeof(xce), 0, 1); |
| 67 |
|
| 68 |
caml_enter_blocking_section(); |
| 69 |
xce = xenevtchn_open(NULL, flags); |
| 70 |
caml_leave_blocking_section(); |
| 71 |
|
| 72 |
if (xce == NULL) |
| 73 |
caml_failwith("open failed"); |
| 74 |
|
| 75 |
*(xenevtchn_handle **)Data_custom_val(result) = xce; |
| 76 |
|
| 77 |
CAMLreturn(result); |
| 78 |
} |
| 79 |
|
| 80 |
CAMLprim value stub_eventchn_fdopen(value fdval) |
| 81 |
{ |
| 82 |
CAMLparam1(fdval); |
| 83 |
CAMLlocal1(result); |
| 84 |
xenevtchn_handle *xce; |
| 85 |
|
| 86 |
result = caml_alloc_custom(&xenevtchn_ops, sizeof(xce), 0, 1); |
| 87 |
|
| 88 |
caml_enter_blocking_section(); |
| 89 |
xce = xenevtchn_fdopen(NULL, Int_val(fdval), 0); |
| 90 |
caml_leave_blocking_section(); |
| 91 |
|
| 92 |
if (xce == NULL) |
| 93 |
caml_failwith("evtchn fdopen failed"); |
| 94 |
|
| 95 |
*(xenevtchn_handle **)Data_custom_val(result) = xce; |
| 96 |
|
| 97 |
CAMLreturn(result); |
| 98 |
} |
| 99 |
|
| 100 |
CAMLprim value stub_eventchn_fd(value xce_val) |
| 101 |
{ |
| 102 |
CAMLparam1(xce_val); |
| 103 |
xenevtchn_handle *xce = xce_of_val(xce_val); |
| 104 |
int fd; |
| 105 |
|
| 106 |
/* Don't drop the GC lock. This is a simple read out of memory */ |
| 107 |
fd = xenevtchn_fd(xce); |
| 108 |
if (fd == -1) |
| 109 |
caml_failwith("evtchn fd failed"); |
| 110 |
|
| 111 |
CAMLreturn(Val_int(fd)); |
| 112 |
} |
| 113 |
|
| 114 |
CAMLprim value stub_eventchn_notify(value xce_val, value port) |
| 115 |
{ |
| 116 |
CAMLparam2(xce_val, port); |
| 117 |
xenevtchn_handle *xce = xce_of_val(xce_val); |
| 118 |
int rc; |
| 119 |
|
| 120 |
caml_enter_blocking_section(); |
| 121 |
rc = xenevtchn_notify(xce, Int_val(port)); |
| 122 |
caml_leave_blocking_section(); |
| 123 |
|
| 124 |
if (rc == -1) |
| 125 |
caml_failwith("evtchn notify failed"); |
| 126 |
|
| 127 |
CAMLreturn(Val_unit); |
| 128 |
} |
| 129 |
|
| 130 |
CAMLprim value stub_eventchn_bind_interdomain(value xce_val, value domid, |
| 131 |
value remote_port) |
| 132 |
{ |
| 133 |
CAMLparam3(xce_val, domid, remote_port); |
| 134 |
xenevtchn_handle *xce = xce_of_val(xce_val); |
| 135 |
xenevtchn_port_or_error_t rc; |
| 136 |
|
| 137 |
caml_enter_blocking_section(); |
| 138 |
rc = xenevtchn_bind_interdomain(xce, Int_val(domid), |
| 139 |
Int_val(remote_port)); |
| 140 |
caml_leave_blocking_section(); |
| 141 |
|
| 142 |
if (rc == -1) |
| 143 |
caml_failwith("evtchn bind_interdomain failed"); |
| 144 |
|
| 145 |
CAMLreturn(Val_int(rc)); |
| 146 |
} |
| 147 |
|
| 148 |
CAMLprim value stub_eventchn_bind_virq(value xce_val, value virq) |
| 149 |
{ |
| 150 |
CAMLparam2(xce_val, virq); |
| 151 |
xenevtchn_handle *xce = xce_of_val(xce_val); |
| 152 |
xenevtchn_port_or_error_t rc; |
| 153 |
|
| 154 |
caml_enter_blocking_section(); |
| 155 |
rc = xenevtchn_bind_virq(xce, Int_val(virq)); |
| 156 |
caml_leave_blocking_section(); |
| 157 |
|
| 158 |
if (rc == -1) |
| 159 |
caml_failwith("evtchn bind_virq failed"); |
| 160 |
|
| 161 |
CAMLreturn(Val_int(rc)); |
| 162 |
} |
| 163 |
|
| 164 |
CAMLprim value stub_eventchn_unbind(value xce_val, value port) |
| 165 |
{ |
| 166 |
CAMLparam2(xce_val, port); |
| 167 |
xenevtchn_handle *xce = xce_of_val(xce_val); |
| 168 |
int rc; |
| 169 |
|
| 170 |
caml_enter_blocking_section(); |
| 171 |
rc = xenevtchn_unbind(xce, Int_val(port)); |
| 172 |
caml_leave_blocking_section(); |
| 173 |
|
| 174 |
if (rc == -1) |
| 175 |
caml_failwith("evtchn unbind failed"); |
| 176 |
|
| 177 |
CAMLreturn(Val_unit); |
| 178 |
} |
| 179 |
|
| 180 |
CAMLprim value stub_eventchn_pending(value xce_val) |
| 181 |
{ |
| 182 |
CAMLparam1(xce_val); |
| 183 |
xenevtchn_handle *xce = xce_of_val(xce_val); |
| 184 |
xenevtchn_port_or_error_t rc; |
| 185 |
|
| 186 |
caml_enter_blocking_section(); |
| 187 |
rc = xenevtchn_pending(xce); |
| 188 |
caml_leave_blocking_section(); |
| 189 |
|
| 190 |
if (rc == -1) |
| 191 |
caml_failwith("evtchn pending failed"); |
| 192 |
|
| 193 |
CAMLreturn(Val_int(rc)); |
| 194 |
} |
| 195 |
|
| 196 |
CAMLprim value stub_eventchn_unmask(value xce_val, value port) |
| 197 |
{ |
| 198 |
CAMLparam2(xce_val, port); |
| 199 |
xenevtchn_handle *xce = xce_of_val(xce_val); |
| 200 |
int rc; |
| 201 |
|
| 202 |
caml_enter_blocking_section(); |
| 203 |
rc = xenevtchn_unmask(xce, Int_val(port)); |
| 204 |
caml_leave_blocking_section(); |
| 205 |
|
| 206 |
if (rc == -1) |
| 207 |
caml_failwith("evtchn unmask failed"); |
| 208 |
|
| 209 |
CAMLreturn(Val_unit); |
| 210 |
} |