ia64/xen-unstable

annotate xen/common/event_channel.c @ 5703:1d375ce8e0e0

Clean up Xen's event-channel interface, and semantics for binding
to VCPUs.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Jul 08 14:17:54 2005 +0000 (2005-07-08)
parents 579d1e771025
children 7c3d7c37dfde 05b63285047c
rev   line source
kaf24@954 1 /******************************************************************************
kaf24@954 2 * event_channel.c
kaf24@954 3 *
kaf24@1509 4 * Event notifications from VIRQs, PIRQs, and other domains.
kaf24@954 5 *
kaf24@1127 6 * Copyright (c) 2003-2004, K A Fraser.
kaf24@954 7 *
kaf24@954 8 * This program is distributed in the hope that it will be useful,
kaf24@954 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
kaf24@954 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kaf24@954 11 * GNU General Public License for more details.
kaf24@954 12 *
kaf24@954 13 * You should have received a copy of the GNU General Public License
kaf24@954 14 * along with this program; if not, write to the Free Software
kaf24@954 15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
kaf24@954 16 */
kaf24@954 17
kaf24@1210 18 #include <xen/config.h>
kaf24@1210 19 #include <xen/init.h>
kaf24@1210 20 #include <xen/lib.h>
kaf24@1210 21 #include <xen/errno.h>
kaf24@1210 22 #include <xen/sched.h>
kaf24@1210 23 #include <xen/event.h>
kaf24@1239 24 #include <xen/irq.h>
cl349@5291 25 #include <asm/current.h>
kaf24@954 26
kaf24@2789 27 #include <public/xen.h>
kaf24@2789 28 #include <public/event_channel.h>
smh22@5514 29 #include <acm/acm_hooks.h>
kaf24@1127 30
kaf24@5308 31 #define bucket_from_port(d,p) \
kaf24@5308 32 ((d)->evtchn[(p)/EVTCHNS_PER_BUCKET])
kaf24@5308 33 #define port_is_valid(d,p) \
kaf24@5308 34 (((p) >= 0) && ((p) < MAX_EVTCHNS) && \
kaf24@5308 35 (bucket_from_port(d,p) != NULL))
kaf24@5308 36 #define evtchn_from_port(d,p) \
kaf24@5308 37 (&(bucket_from_port(d,p))[(p)&(EVTCHNS_PER_BUCKET-1)])
kaf24@1127 38
kaf24@5325 39 #define ERROR_EXIT(_errno) do { rc = (_errno); goto out; } while ( 0 )
kaf24@5325 40
kaf24@5308 41 static int get_free_port(struct domain *d)
kaf24@5308 42 {
kaf24@5308 43 struct evtchn *chn;
kaf24@5308 44 int port;
cl349@3114 45
kaf24@5308 46 for ( port = 0; port_is_valid(d, port); port++ )
kaf24@5308 47 if ( evtchn_from_port(d, port)->state == ECS_FREE )
kaf24@5308 48 return port;
kaf24@1127 49
kaf24@5308 50 if ( port == MAX_EVTCHNS )
kaf24@5308 51 return -ENOSPC;
kaf24@1127 52
kaf24@5308 53 chn = xmalloc_array(struct evtchn, EVTCHNS_PER_BUCKET);
kaf24@5308 54 if ( unlikely(chn == NULL) )
kaf24@5308 55 return -ENOMEM;
kaf24@5308 56 memset(chn, 0, EVTCHNS_PER_BUCKET * sizeof(*chn));
kaf24@5308 57 bucket_from_port(d, port) = chn;
kaf24@1127 58
kaf24@1127 59 return port;
kaf24@1127 60 }
kaf24@1127 61
kaf24@2713 62
kaf24@2713 63 static long evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
kaf24@2713 64 {
kaf24@5308 65 struct evtchn *chn;
cl349@2919 66 struct domain *d = current->domain;
kaf24@5325 67 int port = alloc->port;
kaf24@5325 68 long rc = 0;
kaf24@2713 69
kaf24@5308 70 spin_lock(&d->evtchn_lock);
kaf24@2713 71
kaf24@5325 72 /* Obtain, or ensure that we already have, a valid <port>. */
kaf24@5325 73 if ( port == 0 )
kaf24@2713 74 {
kaf24@5325 75 if ( (port = get_free_port(d)) < 0 )
kaf24@5325 76 ERROR_EXIT(port);
kaf24@5325 77 }
kaf24@5325 78 else if ( !port_is_valid(d, port) )
kaf24@5325 79 ERROR_EXIT(-EINVAL);
kaf24@5325 80 chn = evtchn_from_port(d, port);
kaf24@5325 81
kaf24@5325 82 /* Validate channel's current state. */
kaf24@5325 83 switch ( chn->state )
kaf24@5325 84 {
kaf24@5325 85 case ECS_FREE:
kaf24@5308 86 chn->state = ECS_UNBOUND;
kaf24@5308 87 chn->u.unbound.remote_domid = alloc->dom;
kaf24@5325 88 break;
kaf24@5325 89
kaf24@5325 90 case ECS_UNBOUND:
kaf24@5325 91 if ( chn->u.unbound.remote_domid != alloc->dom )
kaf24@5325 92 ERROR_EXIT(-EINVAL);
kaf24@5325 93 break;
kaf24@5325 94
kaf24@5325 95 default:
kaf24@5325 96 ERROR_EXIT(-EINVAL);
kaf24@2713 97 }
kaf24@2713 98
kaf24@5325 99 out:
kaf24@5308 100 spin_unlock(&d->evtchn_lock);
kaf24@2713 101
kaf24@2713 102 alloc->port = port;
kaf24@5325 103 return rc;
kaf24@2713 104 }
kaf24@2713 105
kaf24@2713 106
kaf24@1218 107 static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
kaf24@1127 108 {
kaf24@5308 109 struct evtchn *chn1, *chn2;
kaf24@1506 110 struct domain *d1, *d2;
kaf24@2713 111 int port1 = bind->port1, port2 = bind->port2;
kaf24@1506 112 domid_t dom1 = bind->dom1, dom2 = bind->dom2;
kaf24@1506 113 long rc = 0;
kaf24@954 114
cl349@2919 115 if ( !IS_PRIV(current->domain) && (dom1 != DOMID_SELF) )
kaf24@1127 116 return -EPERM;
kaf24@954 117
kaf24@1129 118 if ( dom1 == DOMID_SELF )
kaf24@4877 119 dom1 = current->domain->domain_id;
kaf24@1129 120 if ( dom2 == DOMID_SELF )
kaf24@4877 121 dom2 = current->domain->domain_id;
kaf24@1127 122
kaf24@1506 123 if ( ((d1 = find_domain_by_id(dom1)) == NULL) ||
kaf24@1506 124 ((d2 = find_domain_by_id(dom2)) == NULL) )
kaf24@1127 125 {
kaf24@1506 126 if ( d1 != NULL )
kaf24@1506 127 put_domain(d1);
kaf24@1127 128 return -ESRCH;
kaf24@1127 129 }
kaf24@1127 130
kaf24@1127 131 /* Avoid deadlock by first acquiring lock of domain with smaller id. */
kaf24@1542 132 if ( d1 < d2 )
kaf24@954 133 {
kaf24@5308 134 spin_lock(&d1->evtchn_lock);
kaf24@5308 135 spin_lock(&d2->evtchn_lock);
kaf24@954 136 }
kaf24@954 137 else
kaf24@954 138 {
kaf24@1506 139 if ( d1 != d2 )
kaf24@5308 140 spin_lock(&d2->evtchn_lock);
kaf24@5308 141 spin_lock(&d1->evtchn_lock);
kaf24@954 142 }
kaf24@954 143
kaf24@2713 144 /* Obtain, or ensure that we already have, a valid <port1>. */
kaf24@2713 145 if ( port1 == 0 )
kaf24@954 146 {
kaf24@5308 147 if ( (port1 = get_free_port(d1)) < 0 )
kaf24@2713 148 ERROR_EXIT(port1);
kaf24@2713 149 }
kaf24@5308 150 else if ( !port_is_valid(d1, port1) )
kaf24@2713 151 ERROR_EXIT(-EINVAL);
kaf24@5308 152 chn1 = evtchn_from_port(d1, port1);
kaf24@2713 153
kaf24@2713 154 /* Obtain, or ensure that we already have, a valid <port2>. */
kaf24@2713 155 if ( port2 == 0 )
kaf24@2713 156 {
kaf24@2713 157 /* Make port1 non-free while we allocate port2 (in case dom1==dom2). */
kaf24@5308 158 u16 state = chn1->state;
kaf24@5308 159 chn1->state = ECS_INTERDOMAIN;
kaf24@5308 160 port2 = get_free_port(d2);
kaf24@5308 161 chn1->state = state;
kaf24@2713 162 if ( port2 < 0 )
kaf24@2713 163 ERROR_EXIT(port2);
kaf24@2713 164 }
kaf24@5308 165 else if ( !port_is_valid(d2, port2) )
kaf24@2713 166 ERROR_EXIT(-EINVAL);
kaf24@5308 167 chn2 = evtchn_from_port(d2, port2);
kaf24@2713 168
kaf24@2713 169 /* Validate <dom1,port1>'s current state. */
kaf24@5308 170 switch ( chn1->state )
kaf24@2713 171 {
kaf24@2713 172 case ECS_FREE:
kaf24@2713 173 break;
kaf24@2713 174
kaf24@2713 175 case ECS_UNBOUND:
kaf24@5308 176 if ( chn1->u.unbound.remote_domid != dom2 )
kaf24@2713 177 ERROR_EXIT(-EINVAL);
kaf24@2713 178 break;
kaf24@2713 179
kaf24@2713 180 case ECS_INTERDOMAIN:
kaf24@5308 181 if ( chn1->u.interdomain.remote_dom != d2 )
kaf24@2719 182 ERROR_EXIT(-EINVAL);
kaf24@5308 183 if ( (chn1->u.interdomain.remote_port != port2) && (bind->port2 != 0) )
kaf24@2719 184 ERROR_EXIT(-EINVAL);
kaf24@5308 185 port2 = chn1->u.interdomain.remote_port;
kaf24@1127 186 goto out;
kaf24@2713 187
kaf24@2713 188 default:
kaf24@2713 189 ERROR_EXIT(-EINVAL);
kaf24@954 190 }
kaf24@954 191
kaf24@2713 192 /* Validate <dom2,port2>'s current state. */
kaf24@5308 193 switch ( chn2->state )
kaf24@2713 194 {
kaf24@2713 195 case ECS_FREE:
cl349@2919 196 if ( !IS_PRIV(current->domain) && (dom2 != DOMID_SELF) )
kaf24@2717 197 ERROR_EXIT(-EPERM);
kaf24@2713 198 break;
kaf24@1329 199
kaf24@2713 200 case ECS_UNBOUND:
kaf24@5308 201 if ( chn2->u.unbound.remote_domid != dom1 )
kaf24@2713 202 ERROR_EXIT(-EINVAL);
kaf24@2713 203 break;
kaf24@2713 204
kaf24@2719 205 case ECS_INTERDOMAIN:
kaf24@5308 206 if ( chn2->u.interdomain.remote_dom != d1 )
kaf24@2719 207 ERROR_EXIT(-EINVAL);
kaf24@5308 208 if ( (chn2->u.interdomain.remote_port != port1) && (bind->port1 != 0) )
kaf24@2719 209 ERROR_EXIT(-EINVAL);
kaf24@5308 210 port1 = chn2->u.interdomain.remote_port;
kaf24@2719 211 goto out;
kaf24@2719 212
kaf24@2713 213 default:
kaf24@2713 214 ERROR_EXIT(-EINVAL);
kaf24@1127 215 }
kaf24@954 216
kaf24@2713 217 /*
kaf24@2713 218 * Everything checked out okay -- bind <dom1,port1> to <dom2,port2>.
kaf24@2713 219 */
kaf24@2713 220
kaf24@5308 221 chn1->u.interdomain.remote_dom = d2;
kaf24@5308 222 chn1->u.interdomain.remote_port = (u16)port2;
kaf24@5308 223 chn1->state = ECS_INTERDOMAIN;
kaf24@2713 224
kaf24@5308 225 chn2->u.interdomain.remote_dom = d1;
kaf24@5308 226 chn2->u.interdomain.remote_port = (u16)port1;
kaf24@5308 227 chn2->state = ECS_INTERDOMAIN;
kaf24@1127 228
kaf24@954 229 out:
kaf24@5308 230 spin_unlock(&d1->evtchn_lock);
kaf24@1506 231 if ( d1 != d2 )
kaf24@5308 232 spin_unlock(&d2->evtchn_lock);
kaf24@1127 233
kaf24@1506 234 put_domain(d1);
kaf24@1506 235 put_domain(d2);
kaf24@1127 236
kaf24@1218 237 bind->port1 = port1;
kaf24@1218 238 bind->port2 = port2;
kaf24@954 239
kaf24@954 240 return rc;
kaf24@954 241 }
kaf24@954 242
kaf24@954 243
kaf24@1218 244 static long evtchn_bind_virq(evtchn_bind_virq_t *bind)
kaf24@1218 245 {
kaf24@5308 246 struct evtchn *chn;
kaf24@5289 247 struct vcpu *v = current;
kaf24@5289 248 struct domain *d = v->domain;
kaf24@1506 249 int port, virq = bind->virq;
kaf24@1218 250
kaf24@5289 251 if ( virq >= ARRAY_SIZE(v->virq_to_evtchn) )
kaf24@1218 252 return -EINVAL;
kaf24@1218 253
kaf24@5308 254 spin_lock(&d->evtchn_lock);
kaf24@1218 255
kaf24@1218 256 /*
kaf24@1218 257 * Port 0 is the fallback port for VIRQs that haven't been explicitly
cl349@3297 258 * bound yet.
kaf24@1218 259 */
kaf24@5289 260 if ( ((port = v->virq_to_evtchn[virq]) != 0) ||
kaf24@5308 261 ((port = get_free_port(d)) < 0) )
kaf24@1218 262 goto out;
kaf24@1218 263
kaf24@5308 264 chn = evtchn_from_port(d, port);
kaf24@5308 265 chn->state = ECS_VIRQ;
kaf24@5308 266 chn->notify_vcpu_id = v->vcpu_id;
kaf24@5308 267 chn->u.virq = virq;
kaf24@1218 268
kaf24@5289 269 v->virq_to_evtchn[virq] = port;
kaf24@1218 270
kaf24@1218 271 out:
kaf24@5308 272 spin_unlock(&d->evtchn_lock);
kaf24@1218 273
kaf24@1218 274 if ( port < 0 )
kaf24@1218 275 return port;
kaf24@1218 276
kaf24@1218 277 bind->port = port;
cl349@2932 278 return 0;
cl349@2932 279 }
cl349@2932 280
kaf24@5308 281
cl349@2932 282 static long evtchn_bind_ipi(evtchn_bind_ipi_t *bind)
cl349@2932 283 {
kaf24@5308 284 struct evtchn *chn;
kaf24@5308 285 struct domain *d = current->domain;
kaf24@5703 286 int port;
kaf24@4202 287
kaf24@5308 288 spin_lock(&d->evtchn_lock);
cl349@2932 289
kaf24@5308 290 if ( (port = get_free_port(d)) >= 0 )
cl349@2932 291 {
kaf24@5308 292 chn = evtchn_from_port(d, port);
kaf24@5308 293 chn->state = ECS_IPI;
kaf24@5703 294 chn->notify_vcpu_id = current->vcpu_id;
cl349@2932 295 }
cl349@2932 296
kaf24@5308 297 spin_unlock(&d->evtchn_lock);
cl349@2932 298
cl349@2932 299 if ( port < 0 )
cl349@2932 300 return port;
cl349@2932 301
cl349@2932 302 bind->port = port;
kaf24@1218 303 return 0;
kaf24@1218 304 }
kaf24@1218 305
kaf24@1218 306
kaf24@1235 307 static long evtchn_bind_pirq(evtchn_bind_pirq_t *bind)
kaf24@1235 308 {
kaf24@5308 309 struct evtchn *chn;
cl349@2919 310 struct domain *d = current->domain;
kaf24@1506 311 int port, rc, pirq = bind->pirq;
kaf24@1235 312
kaf24@1506 313 if ( pirq >= ARRAY_SIZE(d->pirq_to_evtchn) )
kaf24@1235 314 return -EINVAL;
kaf24@1235 315
kaf24@5308 316 spin_lock(&d->evtchn_lock);
kaf24@1235 317
kaf24@1506 318 if ( ((rc = port = d->pirq_to_evtchn[pirq]) != 0) ||
kaf24@5308 319 ((rc = port = get_free_port(d)) < 0) )
kaf24@1235 320 goto out;
kaf24@1235 321
kaf24@5308 322 chn = evtchn_from_port(d, port);
kaf24@5308 323
kaf24@1506 324 d->pirq_to_evtchn[pirq] = port;
sos22@5700 325 rc = pirq_guest_bind(d->vcpu[0], pirq,
kaf24@1253 326 !!(bind->flags & BIND_PIRQ__WILL_SHARE));
kaf24@1253 327 if ( rc != 0 )
kaf24@1239 328 {
kaf24@1506 329 d->pirq_to_evtchn[pirq] = 0;
kaf24@1239 330 goto out;
kaf24@1239 331 }
kaf24@1239 332
kaf24@5308 333 chn->state = ECS_PIRQ;
kaf24@5308 334 chn->u.pirq = pirq;
kaf24@1235 335
kaf24@1235 336 out:
kaf24@5308 337 spin_unlock(&d->evtchn_lock);
kaf24@1235 338
kaf24@1239 339 if ( rc < 0 )
kaf24@1239 340 return rc;
kaf24@1235 341
kaf24@1235 342 bind->port = port;
kaf24@1235 343 return 0;
kaf24@1235 344 }
kaf24@1235 345
kaf24@1235 346
kaf24@1506 347 static long __evtchn_close(struct domain *d1, int port1)
kaf24@954 348 {
kaf24@5308 349 struct domain *d2 = NULL;
kaf24@5308 350 struct vcpu *v;
kaf24@5308 351 struct evtchn *chn1, *chn2;
kaf24@5308 352 int port2;
kaf24@5308 353 long rc = 0;
kaf24@954 354
kaf24@954 355 again:
kaf24@5308 356 spin_lock(&d1->evtchn_lock);
kaf24@954 357
kaf24@5308 358 if ( !port_is_valid(d1, port1) )
kaf24@954 359 {
kaf24@954 360 rc = -EINVAL;
kaf24@954 361 goto out;
kaf24@954 362 }
kaf24@954 363
kaf24@5308 364 chn1 = evtchn_from_port(d1, port1);
kaf24@5308 365 switch ( chn1->state )
kaf24@954 366 {
kaf24@1218 367 case ECS_FREE:
cl349@3297 368 case ECS_RESERVED:
kaf24@1218 369 rc = -EINVAL;
kaf24@1218 370 goto out;
kaf24@1218 371
kaf24@1218 372 case ECS_UNBOUND:
kaf24@1218 373 break;
kaf24@1218 374
kaf24@1218 375 case ECS_PIRQ:
kaf24@5308 376 if ( (rc = pirq_guest_unbind(d1, chn1->u.pirq)) == 0 )
kaf24@5308 377 d1->pirq_to_evtchn[chn1->u.pirq] = 0;
kaf24@1218 378 break;
kaf24@1218 379
kaf24@1218 380 case ECS_VIRQ:
kaf24@5289 381 for_each_vcpu ( d1, v )
kaf24@5308 382 if ( v->virq_to_evtchn[chn1->u.virq] == port1 )
kaf24@5308 383 v->virq_to_evtchn[chn1->u.virq] = 0;
kaf24@1218 384 break;
kaf24@1218 385
cl349@2932 386 case ECS_IPI:
cl349@2932 387 break;
cl349@2932 388
kaf24@1218 389 case ECS_INTERDOMAIN:
kaf24@1506 390 if ( d2 == NULL )
kaf24@954 391 {
kaf24@5308 392 d2 = chn1->u.interdomain.remote_dom;
kaf24@1505 393
kaf24@1506 394 /* If we unlock d1 then we could lose d2. Must get a reference. */
kaf24@1506 395 if ( unlikely(!get_domain(d2)) )
kaf24@1505 396 {
kaf24@1505 397 /*
kaf24@1506 398 * Failed to obtain a reference. No matter: d2 must be dying
kaf24@1505 399 * and so will close this event channel for us.
kaf24@1505 400 */
kaf24@1506 401 d2 = NULL;
kaf24@1505 402 goto out;
kaf24@1505 403 }
kaf24@1127 404
kaf24@1542 405 if ( d1 < d2 )
kaf24@954 406 {
kaf24@5308 407 spin_lock(&d2->evtchn_lock);
kaf24@954 408 }
kaf24@1506 409 else if ( d1 != d2 )
kaf24@954 410 {
kaf24@5308 411 spin_unlock(&d1->evtchn_lock);
kaf24@5308 412 spin_lock(&d2->evtchn_lock);
kaf24@954 413 goto again;
kaf24@954 414 }
kaf24@954 415 }
kaf24@5308 416 else if ( d2 != chn1->u.interdomain.remote_dom )
kaf24@954 417 {
kaf24@954 418 rc = -EINVAL;
kaf24@954 419 goto out;
kaf24@954 420 }
kaf24@1505 421
kaf24@5308 422 port2 = chn1->u.interdomain.remote_port;
kaf24@5308 423 BUG_ON(!port_is_valid(d2, port2));
kaf24@954 424
kaf24@5308 425 chn2 = evtchn_from_port(d2, port2);
kaf24@5308 426 BUG_ON(chn2->state != ECS_INTERDOMAIN);
kaf24@5308 427 BUG_ON(chn2->u.interdomain.remote_dom != d1);
kaf24@954 428
kaf24@5308 429 chn2->state = ECS_UNBOUND;
kaf24@5308 430 chn2->u.unbound.remote_domid = d1->domain_id;
kaf24@1218 431 break;
kaf24@1218 432
kaf24@1218 433 default:
kaf24@1218 434 BUG();
kaf24@954 435 }
kaf24@954 436
kaf24@5703 437 /* Reset binding to vcpu0 when the channel is freed. */
kaf24@5703 438 chn1->state = ECS_FREE;
kaf24@5703 439 chn1->notify_vcpu_id = 0;
kaf24@1133 440
kaf24@954 441 out:
kaf24@1506 442 if ( d2 != NULL )
kaf24@954 443 {
kaf24@1506 444 if ( d1 != d2 )
kaf24@5308 445 spin_unlock(&d2->evtchn_lock);
kaf24@1506 446 put_domain(d2);
kaf24@954 447 }
kaf24@954 448
kaf24@5308 449 spin_unlock(&d1->evtchn_lock);
kaf24@1145 450
kaf24@954 451 return rc;
kaf24@954 452 }
kaf24@954 453
kaf24@954 454
kaf24@1218 455 static long evtchn_close(evtchn_close_t *close)
kaf24@1127 456 {
kaf24@1506 457 struct domain *d;
kaf24@1506 458 long rc;
kaf24@1506 459 domid_t dom = close->dom;
kaf24@1127 460
kaf24@1129 461 if ( dom == DOMID_SELF )
kaf24@4877 462 dom = current->domain->domain_id;
cl349@2919 463 else if ( !IS_PRIV(current->domain) )
kaf24@1127 464 return -EPERM;
kaf24@1127 465
kaf24@1506 466 if ( (d = find_domain_by_id(dom)) == NULL )
kaf24@1127 467 return -ESRCH;
kaf24@1127 468
kaf24@1506 469 rc = __evtchn_close(d, close->port);
kaf24@1127 470
kaf24@1506 471 put_domain(d);
kaf24@1127 472 return rc;
kaf24@1127 473 }
kaf24@1127 474
kaf24@1127 475
iap10@3290 476 long evtchn_send(int lport)
kaf24@954 477 {
kaf24@5308 478 struct evtchn *lchn, *rchn;
kaf24@5308 479 struct domain *ld = current->domain, *rd;
cl349@2937 480 int rport, ret = 0;
kaf24@954 481
kaf24@5308 482 spin_lock(&ld->evtchn_lock);
kaf24@954 483
kaf24@5308 484 if ( unlikely(!port_is_valid(ld, lport)) )
kaf24@954 485 {
kaf24@5308 486 spin_unlock(&ld->evtchn_lock);
kaf24@954 487 return -EINVAL;
kaf24@954 488 }
kaf24@954 489
kaf24@5308 490 lchn = evtchn_from_port(ld, lport);
kaf24@5308 491 switch ( lchn->state )
cl349@2937 492 {
cl349@2937 493 case ECS_INTERDOMAIN:
kaf24@5308 494 rd = lchn->u.interdomain.remote_dom;
kaf24@5308 495 rport = lchn->u.interdomain.remote_port;
kaf24@5308 496 rchn = evtchn_from_port(rd, rport);
kaf24@5308 497 evtchn_set_pending(rd->vcpu[rchn->notify_vcpu_id], rport);
cl349@2937 498 break;
cl349@2937 499 case ECS_IPI:
kaf24@5308 500 evtchn_set_pending(ld->vcpu[lchn->notify_vcpu_id], lport);
cl349@2937 501 break;
cl349@2937 502 default:
cl349@2937 503 ret = -EINVAL;
cl349@2937 504 }
kaf24@954 505
kaf24@5308 506 spin_unlock(&ld->evtchn_lock);
kaf24@954 507
cl349@2937 508 return ret;
kaf24@954 509 }
kaf24@954 510
kaf24@5308 511 void send_guest_pirq(struct domain *d, int pirq)
kaf24@5308 512 {
kaf24@5308 513 int port = d->pirq_to_evtchn[pirq];
kaf24@5308 514 struct evtchn *chn = evtchn_from_port(d, port);
kaf24@5308 515 evtchn_set_pending(d->vcpu[chn->notify_vcpu_id], port);
kaf24@5308 516 }
kaf24@954 517
kaf24@1218 518 static long evtchn_status(evtchn_status_t *status)
kaf24@954 519 {
kaf24@1506 520 struct domain *d;
kaf24@1506 521 domid_t dom = status->dom;
kaf24@1506 522 int port = status->port;
kaf24@5308 523 struct evtchn *chn;
kaf24@1506 524 long rc = 0;
kaf24@1127 525
kaf24@1129 526 if ( dom == DOMID_SELF )
kaf24@4877 527 dom = current->domain->domain_id;
cl349@2919 528 else if ( !IS_PRIV(current->domain) )
kaf24@1127 529 return -EPERM;
kaf24@1127 530
kaf24@1506 531 if ( (d = find_domain_by_id(dom)) == NULL )
kaf24@1127 532 return -ESRCH;
kaf24@954 533
kaf24@5308 534 spin_lock(&d->evtchn_lock);
kaf24@954 535
kaf24@5308 536 if ( !port_is_valid(d, port) )
kaf24@954 537 {
kaf24@1266 538 rc = -EINVAL;
kaf24@1266 539 goto out;
kaf24@1127 540 }
kaf24@1127 541
kaf24@5308 542 chn = evtchn_from_port(d, port);
kaf24@5308 543 switch ( chn->state )
kaf24@1127 544 {
kaf24@1127 545 case ECS_FREE:
cl349@3297 546 case ECS_RESERVED:
kaf24@1127 547 status->status = EVTCHNSTAT_closed;
kaf24@1127 548 break;
kaf24@1218 549 case ECS_UNBOUND:
kaf24@1218 550 status->status = EVTCHNSTAT_unbound;
kaf24@5308 551 status->u.unbound.dom = chn->u.unbound.remote_domid;
kaf24@1127 552 break;
kaf24@1218 553 case ECS_INTERDOMAIN:
kaf24@1218 554 status->status = EVTCHNSTAT_interdomain;
cl349@2924 555 status->u.interdomain.dom =
kaf24@5308 556 chn->u.interdomain.remote_dom->domain_id;
kaf24@5308 557 status->u.interdomain.port = chn->u.interdomain.remote_port;
kaf24@1218 558 break;
kaf24@1218 559 case ECS_PIRQ:
kaf24@1218 560 status->status = EVTCHNSTAT_pirq;
kaf24@5308 561 status->u.pirq = chn->u.pirq;
kaf24@1218 562 break;
kaf24@1218 563 case ECS_VIRQ:
kaf24@1218 564 status->status = EVTCHNSTAT_virq;
kaf24@5308 565 status->u.virq = chn->u.virq;
kaf24@1127 566 break;
cl349@2932 567 case ECS_IPI:
kaf24@5703 568 status->status = EVTCHNSTAT_ipi;
cl349@2932 569 break;
kaf24@1127 570 default:
kaf24@1127 571 BUG();
kaf24@954 572 }
kaf24@954 573
kaf24@5703 574 status->vcpu = chn->notify_vcpu_id;
kaf24@5703 575
kaf24@1266 576 out:
kaf24@5308 577 spin_unlock(&d->evtchn_lock);
kaf24@1506 578 put_domain(d);
kaf24@1266 579 return rc;
kaf24@954 580 }
kaf24@954 581
kaf24@5703 582 static long evtchn_bind_vcpu(evtchn_bind_vcpu_t *bind)
iap10@5691 583 {
iap10@5691 584 struct domain *d = current->domain;
iap10@5691 585 int port = bind->port;
iap10@5691 586 int vcpu = bind->vcpu;
iap10@5691 587 struct evtchn *chn;
kaf24@5703 588 long rc = 0;
kaf24@5703 589
kaf24@5703 590 if ( (vcpu >= MAX_VIRT_CPUS) || (d->vcpu[vcpu] == NULL) )
kaf24@5703 591 return -EINVAL;
iap10@5691 592
iap10@5691 593 spin_lock(&d->evtchn_lock);
iap10@5691 594
iap10@5691 595 if ( !port_is_valid(d, port) )
iap10@5691 596 {
iap10@5691 597 rc = -EINVAL;
iap10@5691 598 goto out;
iap10@5691 599 }
iap10@5691 600
iap10@5691 601 chn = evtchn_from_port(d, port);
kaf24@5703 602 switch ( chn->state )
kaf24@5703 603 {
kaf24@5703 604 case ECS_UNBOUND:
kaf24@5703 605 case ECS_INTERDOMAIN:
kaf24@5703 606 case ECS_PIRQ:
kaf24@5703 607 chn->notify_vcpu_id = vcpu;
kaf24@5703 608 break;
kaf24@5703 609 default:
kaf24@5703 610 rc = -EINVAL;
kaf24@5703 611 break;
kaf24@5703 612 }
iap10@5691 613
iap10@5691 614 out:
iap10@5691 615 spin_unlock(&d->evtchn_lock);
iap10@5691 616 return rc;
iap10@5691 617 }
kaf24@954 618
kaf24@1127 619 long do_event_channel_op(evtchn_op_t *uop)
kaf24@954 620 {
kaf24@954 621 long rc;
kaf24@1127 622 evtchn_op_t op;
kaf24@954 623
kaf24@1127 624 if ( copy_from_user(&op, uop, sizeof(op)) != 0 )
kaf24@1127 625 return -EFAULT;
kaf24@1127 626
smh22@5514 627 if (acm_pre_event_channel(&op))
smh22@5514 628 return -EACCES;
smh22@5514 629
kaf24@1127 630 switch ( op.cmd )
kaf24@954 631 {
kaf24@2713 632 case EVTCHNOP_alloc_unbound:
kaf24@2713 633 rc = evtchn_alloc_unbound(&op.u.alloc_unbound);
kaf24@2713 634 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@2713 635 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@2713 636 break;
kaf24@2713 637
kaf24@1218 638 case EVTCHNOP_bind_interdomain:
kaf24@1218 639 rc = evtchn_bind_interdomain(&op.u.bind_interdomain);
kaf24@1235 640 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1218 641 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@1218 642 break;
kaf24@1218 643
kaf24@1218 644 case EVTCHNOP_bind_virq:
kaf24@1218 645 rc = evtchn_bind_virq(&op.u.bind_virq);
kaf24@1235 646 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1235 647 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@1235 648 break;
kaf24@1235 649
cl349@2932 650 case EVTCHNOP_bind_ipi:
cl349@2932 651 rc = evtchn_bind_ipi(&op.u.bind_ipi);
cl349@2932 652 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
cl349@2932 653 rc = -EFAULT; /* Cleaning up here would be a mess! */
cl349@2932 654 break;
cl349@2932 655
kaf24@1235 656 case EVTCHNOP_bind_pirq:
kaf24@1235 657 rc = evtchn_bind_pirq(&op.u.bind_pirq);
kaf24@1235 658 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1127 659 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@954 660 break;
kaf24@954 661
kaf24@954 662 case EVTCHNOP_close:
kaf24@1218 663 rc = evtchn_close(&op.u.close);
kaf24@954 664 break;
kaf24@954 665
kaf24@954 666 case EVTCHNOP_send:
kaf24@1218 667 rc = evtchn_send(op.u.send.local_port);
kaf24@954 668 break;
kaf24@954 669
kaf24@954 670 case EVTCHNOP_status:
kaf24@1218 671 rc = evtchn_status(&op.u.status);
kaf24@1235 672 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1127 673 rc = -EFAULT;
kaf24@954 674 break;
kaf24@954 675
kaf24@5703 676 case EVTCHNOP_bind_vcpu:
kaf24@5703 677 rc = evtchn_bind_vcpu(&op.u.bind_vcpu);
iap10@5691 678 break;
iap10@5691 679
kaf24@954 680 default:
kaf24@954 681 rc = -ENOSYS;
kaf24@954 682 break;
kaf24@954 683 }
kaf24@954 684
kaf24@954 685 return rc;
kaf24@954 686 }
kaf24@954 687
kaf24@954 688
kaf24@5308 689 int evtchn_init(struct domain *d)
kaf24@1218 690 {
kaf24@5308 691 spin_lock_init(&d->evtchn_lock);
kaf24@5308 692 if ( get_free_port(d) != 0 )
cl349@3291 693 return -EINVAL;
kaf24@5308 694 evtchn_from_port(d, 0)->state = ECS_RESERVED;
cl349@3291 695 return 0;
kaf24@1218 696 }
kaf24@1218 697
kaf24@1218 698
kaf24@5308 699 void evtchn_destroy(struct domain *d)
kaf24@954 700 {
kaf24@954 701 int i;
kaf24@5308 702
kaf24@5308 703 for ( i = 0; port_is_valid(d, i); i++ )
kaf24@1506 704 (void)__evtchn_close(d, i);
kaf24@5308 705
kaf24@5308 706 for ( i = 0; i < NR_EVTCHN_BUCKETS; i++ )
kaf24@5308 707 if ( d->evtchn[i] != NULL )
kaf24@5308 708 xfree(d->evtchn[i]);
kaf24@954 709 }
kaf24@3914 710
kaf24@3914 711 /*
kaf24@3914 712 * Local variables:
kaf24@3914 713 * mode: C
kaf24@3914 714 * c-set-style: "BSD"
kaf24@3914 715 * c-basic-offset: 4
kaf24@3914 716 * tab-width: 4
kaf24@3914 717 * indent-tabs-mode: nil
kaf24@3988 718 * End:
kaf24@3914 719 */