ia64/xen-unstable

annotate xen/common/event_channel.c @ 6680:4309a1fd8447

Always bind dom0 virqs to vcpu 0.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Wed Sep 07 16:50:55 2005 +0000 (2005-09-07)
parents dd668f7527cb
children 422fee1de8e7
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
cl349@6680 254 if ( d->domain_id == 0 && virq >= VIRQ_CONSOLE )
cl349@6680 255 v = d->vcpu[0];
cl349@6680 256
kaf24@5308 257 spin_lock(&d->evtchn_lock);
kaf24@1218 258
kaf24@1218 259 /*
kaf24@1218 260 * Port 0 is the fallback port for VIRQs that haven't been explicitly
cl349@3297 261 * bound yet.
kaf24@1218 262 */
kaf24@5289 263 if ( ((port = v->virq_to_evtchn[virq]) != 0) ||
kaf24@5308 264 ((port = get_free_port(d)) < 0) )
kaf24@1218 265 goto out;
kaf24@1218 266
kaf24@5308 267 chn = evtchn_from_port(d, port);
kaf24@5308 268 chn->state = ECS_VIRQ;
kaf24@5308 269 chn->notify_vcpu_id = v->vcpu_id;
kaf24@5308 270 chn->u.virq = virq;
kaf24@1218 271
kaf24@5289 272 v->virq_to_evtchn[virq] = port;
kaf24@1218 273
kaf24@1218 274 out:
kaf24@5308 275 spin_unlock(&d->evtchn_lock);
kaf24@1218 276
cl349@6680 277 printk("evtchn_bind_virq %d port %d\n", virq, port);
kaf24@1218 278 if ( port < 0 )
kaf24@1218 279 return port;
kaf24@1218 280
kaf24@1218 281 bind->port = port;
cl349@2932 282 return 0;
cl349@2932 283 }
cl349@2932 284
kaf24@5308 285
cl349@2932 286 static long evtchn_bind_ipi(evtchn_bind_ipi_t *bind)
cl349@2932 287 {
kaf24@5308 288 struct evtchn *chn;
kaf24@5308 289 struct domain *d = current->domain;
kaf24@5703 290 int port;
kaf24@4202 291
kaf24@5308 292 spin_lock(&d->evtchn_lock);
cl349@2932 293
kaf24@5308 294 if ( (port = get_free_port(d)) >= 0 )
cl349@2932 295 {
kaf24@5308 296 chn = evtchn_from_port(d, port);
kaf24@5308 297 chn->state = ECS_IPI;
kaf24@5703 298 chn->notify_vcpu_id = current->vcpu_id;
cl349@2932 299 }
cl349@2932 300
kaf24@5308 301 spin_unlock(&d->evtchn_lock);
cl349@2932 302
cl349@2932 303 if ( port < 0 )
cl349@2932 304 return port;
cl349@2932 305
cl349@2932 306 bind->port = port;
kaf24@1218 307 return 0;
kaf24@1218 308 }
kaf24@1218 309
kaf24@1218 310
kaf24@1235 311 static long evtchn_bind_pirq(evtchn_bind_pirq_t *bind)
kaf24@1235 312 {
kaf24@5308 313 struct evtchn *chn;
cl349@2919 314 struct domain *d = current->domain;
kaf24@1506 315 int port, rc, pirq = bind->pirq;
kaf24@1235 316
kaf24@1506 317 if ( pirq >= ARRAY_SIZE(d->pirq_to_evtchn) )
kaf24@1235 318 return -EINVAL;
kaf24@1235 319
kaf24@5308 320 spin_lock(&d->evtchn_lock);
kaf24@1235 321
kaf24@1506 322 if ( ((rc = port = d->pirq_to_evtchn[pirq]) != 0) ||
kaf24@5308 323 ((rc = port = get_free_port(d)) < 0) )
kaf24@1235 324 goto out;
kaf24@1235 325
kaf24@5308 326 chn = evtchn_from_port(d, port);
kaf24@5308 327
kaf24@1506 328 d->pirq_to_evtchn[pirq] = port;
sos22@5700 329 rc = pirq_guest_bind(d->vcpu[0], pirq,
kaf24@1253 330 !!(bind->flags & BIND_PIRQ__WILL_SHARE));
kaf24@1253 331 if ( rc != 0 )
kaf24@1239 332 {
kaf24@1506 333 d->pirq_to_evtchn[pirq] = 0;
kaf24@1239 334 goto out;
kaf24@1239 335 }
kaf24@1239 336
kaf24@5308 337 chn->state = ECS_PIRQ;
kaf24@5308 338 chn->u.pirq = pirq;
kaf24@1235 339
kaf24@1235 340 out:
kaf24@5308 341 spin_unlock(&d->evtchn_lock);
kaf24@1235 342
kaf24@1239 343 if ( rc < 0 )
kaf24@1239 344 return rc;
kaf24@1235 345
kaf24@1235 346 bind->port = port;
kaf24@1235 347 return 0;
kaf24@1235 348 }
kaf24@1235 349
kaf24@1235 350
kaf24@1506 351 static long __evtchn_close(struct domain *d1, int port1)
kaf24@954 352 {
kaf24@5308 353 struct domain *d2 = NULL;
kaf24@5308 354 struct vcpu *v;
kaf24@5308 355 struct evtchn *chn1, *chn2;
kaf24@5308 356 int port2;
kaf24@5308 357 long rc = 0;
kaf24@954 358
kaf24@954 359 again:
kaf24@5308 360 spin_lock(&d1->evtchn_lock);
kaf24@954 361
kaf24@5308 362 if ( !port_is_valid(d1, port1) )
kaf24@954 363 {
kaf24@954 364 rc = -EINVAL;
kaf24@954 365 goto out;
kaf24@954 366 }
kaf24@954 367
kaf24@5308 368 chn1 = evtchn_from_port(d1, port1);
kaf24@5308 369 switch ( chn1->state )
kaf24@954 370 {
kaf24@1218 371 case ECS_FREE:
cl349@3297 372 case ECS_RESERVED:
kaf24@1218 373 rc = -EINVAL;
kaf24@1218 374 goto out;
kaf24@1218 375
kaf24@1218 376 case ECS_UNBOUND:
kaf24@1218 377 break;
kaf24@1218 378
kaf24@1218 379 case ECS_PIRQ:
kaf24@5308 380 if ( (rc = pirq_guest_unbind(d1, chn1->u.pirq)) == 0 )
kaf24@5308 381 d1->pirq_to_evtchn[chn1->u.pirq] = 0;
kaf24@1218 382 break;
kaf24@1218 383
kaf24@1218 384 case ECS_VIRQ:
kaf24@5289 385 for_each_vcpu ( d1, v )
kaf24@5308 386 if ( v->virq_to_evtchn[chn1->u.virq] == port1 )
kaf24@5308 387 v->virq_to_evtchn[chn1->u.virq] = 0;
kaf24@1218 388 break;
kaf24@1218 389
cl349@2932 390 case ECS_IPI:
cl349@2932 391 break;
cl349@2932 392
kaf24@1218 393 case ECS_INTERDOMAIN:
kaf24@1506 394 if ( d2 == NULL )
kaf24@954 395 {
kaf24@5308 396 d2 = chn1->u.interdomain.remote_dom;
kaf24@1505 397
kaf24@1506 398 /* If we unlock d1 then we could lose d2. Must get a reference. */
kaf24@1506 399 if ( unlikely(!get_domain(d2)) )
kaf24@1505 400 {
kaf24@1505 401 /*
kaf24@1506 402 * Failed to obtain a reference. No matter: d2 must be dying
kaf24@1505 403 * and so will close this event channel for us.
kaf24@1505 404 */
kaf24@1506 405 d2 = NULL;
kaf24@1505 406 goto out;
kaf24@1505 407 }
kaf24@1127 408
kaf24@1542 409 if ( d1 < d2 )
kaf24@954 410 {
kaf24@5308 411 spin_lock(&d2->evtchn_lock);
kaf24@954 412 }
kaf24@1506 413 else if ( d1 != d2 )
kaf24@954 414 {
kaf24@5308 415 spin_unlock(&d1->evtchn_lock);
kaf24@5308 416 spin_lock(&d2->evtchn_lock);
kaf24@954 417 goto again;
kaf24@954 418 }
kaf24@954 419 }
kaf24@5308 420 else if ( d2 != chn1->u.interdomain.remote_dom )
kaf24@954 421 {
kaf24@954 422 rc = -EINVAL;
kaf24@954 423 goto out;
kaf24@954 424 }
kaf24@1505 425
kaf24@5308 426 port2 = chn1->u.interdomain.remote_port;
kaf24@5308 427 BUG_ON(!port_is_valid(d2, port2));
kaf24@954 428
kaf24@5308 429 chn2 = evtchn_from_port(d2, port2);
kaf24@5308 430 BUG_ON(chn2->state != ECS_INTERDOMAIN);
kaf24@5308 431 BUG_ON(chn2->u.interdomain.remote_dom != d1);
kaf24@954 432
kaf24@5308 433 chn2->state = ECS_UNBOUND;
kaf24@5308 434 chn2->u.unbound.remote_domid = d1->domain_id;
kaf24@1218 435 break;
kaf24@1218 436
kaf24@1218 437 default:
kaf24@1218 438 BUG();
kaf24@954 439 }
kaf24@954 440
kaf24@5703 441 /* Reset binding to vcpu0 when the channel is freed. */
kaf24@5703 442 chn1->state = ECS_FREE;
kaf24@5703 443 chn1->notify_vcpu_id = 0;
kaf24@1133 444
kaf24@954 445 out:
kaf24@1506 446 if ( d2 != NULL )
kaf24@954 447 {
kaf24@1506 448 if ( d1 != d2 )
kaf24@5308 449 spin_unlock(&d2->evtchn_lock);
kaf24@1506 450 put_domain(d2);
kaf24@954 451 }
kaf24@954 452
kaf24@5308 453 spin_unlock(&d1->evtchn_lock);
kaf24@1145 454
kaf24@954 455 return rc;
kaf24@954 456 }
kaf24@954 457
kaf24@954 458
kaf24@1218 459 static long evtchn_close(evtchn_close_t *close)
kaf24@1127 460 {
kaf24@1506 461 struct domain *d;
kaf24@1506 462 long rc;
kaf24@1506 463 domid_t dom = close->dom;
kaf24@1127 464
kaf24@1129 465 if ( dom == DOMID_SELF )
kaf24@4877 466 dom = current->domain->domain_id;
cl349@2919 467 else if ( !IS_PRIV(current->domain) )
kaf24@1127 468 return -EPERM;
kaf24@1127 469
kaf24@1506 470 if ( (d = find_domain_by_id(dom)) == NULL )
kaf24@1127 471 return -ESRCH;
kaf24@1127 472
kaf24@1506 473 rc = __evtchn_close(d, close->port);
kaf24@1127 474
kaf24@1506 475 put_domain(d);
kaf24@1127 476 return rc;
kaf24@1127 477 }
kaf24@1127 478
kaf24@1127 479
iap10@3290 480 long evtchn_send(int lport)
kaf24@954 481 {
kaf24@5308 482 struct evtchn *lchn, *rchn;
kaf24@5308 483 struct domain *ld = current->domain, *rd;
cl349@2937 484 int rport, ret = 0;
kaf24@954 485
kaf24@5308 486 spin_lock(&ld->evtchn_lock);
kaf24@954 487
kaf24@5308 488 if ( unlikely(!port_is_valid(ld, lport)) )
kaf24@954 489 {
kaf24@5308 490 spin_unlock(&ld->evtchn_lock);
kaf24@954 491 return -EINVAL;
kaf24@954 492 }
kaf24@954 493
kaf24@5308 494 lchn = evtchn_from_port(ld, lport);
kaf24@5308 495 switch ( lchn->state )
cl349@2937 496 {
cl349@2937 497 case ECS_INTERDOMAIN:
kaf24@5308 498 rd = lchn->u.interdomain.remote_dom;
kaf24@5308 499 rport = lchn->u.interdomain.remote_port;
kaf24@5308 500 rchn = evtchn_from_port(rd, rport);
kaf24@5308 501 evtchn_set_pending(rd->vcpu[rchn->notify_vcpu_id], rport);
cl349@2937 502 break;
cl349@2937 503 case ECS_IPI:
kaf24@5308 504 evtchn_set_pending(ld->vcpu[lchn->notify_vcpu_id], lport);
cl349@2937 505 break;
cl349@2937 506 default:
cl349@2937 507 ret = -EINVAL;
cl349@2937 508 }
kaf24@954 509
kaf24@5308 510 spin_unlock(&ld->evtchn_lock);
kaf24@954 511
cl349@2937 512 return ret;
kaf24@954 513 }
kaf24@954 514
kaf24@5308 515 void send_guest_pirq(struct domain *d, int pirq)
kaf24@5308 516 {
kaf24@5308 517 int port = d->pirq_to_evtchn[pirq];
kaf24@5308 518 struct evtchn *chn = evtchn_from_port(d, port);
kaf24@5308 519 evtchn_set_pending(d->vcpu[chn->notify_vcpu_id], port);
kaf24@5308 520 }
kaf24@954 521
kaf24@1218 522 static long evtchn_status(evtchn_status_t *status)
kaf24@954 523 {
kaf24@1506 524 struct domain *d;
kaf24@1506 525 domid_t dom = status->dom;
kaf24@1506 526 int port = status->port;
kaf24@5308 527 struct evtchn *chn;
kaf24@1506 528 long rc = 0;
kaf24@1127 529
kaf24@1129 530 if ( dom == DOMID_SELF )
kaf24@4877 531 dom = current->domain->domain_id;
cl349@2919 532 else if ( !IS_PRIV(current->domain) )
kaf24@1127 533 return -EPERM;
kaf24@1127 534
kaf24@1506 535 if ( (d = find_domain_by_id(dom)) == NULL )
kaf24@1127 536 return -ESRCH;
kaf24@954 537
kaf24@5308 538 spin_lock(&d->evtchn_lock);
kaf24@954 539
kaf24@5308 540 if ( !port_is_valid(d, port) )
kaf24@954 541 {
kaf24@1266 542 rc = -EINVAL;
kaf24@1266 543 goto out;
kaf24@1127 544 }
kaf24@1127 545
kaf24@5308 546 chn = evtchn_from_port(d, port);
kaf24@5308 547 switch ( chn->state )
kaf24@1127 548 {
kaf24@1127 549 case ECS_FREE:
cl349@3297 550 case ECS_RESERVED:
kaf24@1127 551 status->status = EVTCHNSTAT_closed;
kaf24@1127 552 break;
kaf24@1218 553 case ECS_UNBOUND:
kaf24@1218 554 status->status = EVTCHNSTAT_unbound;
kaf24@5308 555 status->u.unbound.dom = chn->u.unbound.remote_domid;
kaf24@1127 556 break;
kaf24@1218 557 case ECS_INTERDOMAIN:
kaf24@1218 558 status->status = EVTCHNSTAT_interdomain;
cl349@2924 559 status->u.interdomain.dom =
kaf24@5308 560 chn->u.interdomain.remote_dom->domain_id;
kaf24@5308 561 status->u.interdomain.port = chn->u.interdomain.remote_port;
kaf24@1218 562 break;
kaf24@1218 563 case ECS_PIRQ:
kaf24@1218 564 status->status = EVTCHNSTAT_pirq;
kaf24@5308 565 status->u.pirq = chn->u.pirq;
kaf24@1218 566 break;
kaf24@1218 567 case ECS_VIRQ:
kaf24@1218 568 status->status = EVTCHNSTAT_virq;
kaf24@5308 569 status->u.virq = chn->u.virq;
kaf24@1127 570 break;
cl349@2932 571 case ECS_IPI:
kaf24@5703 572 status->status = EVTCHNSTAT_ipi;
cl349@2932 573 break;
kaf24@1127 574 default:
kaf24@1127 575 BUG();
kaf24@954 576 }
kaf24@954 577
kaf24@5703 578 status->vcpu = chn->notify_vcpu_id;
kaf24@5703 579
kaf24@1266 580 out:
kaf24@5308 581 spin_unlock(&d->evtchn_lock);
kaf24@1506 582 put_domain(d);
kaf24@1266 583 return rc;
kaf24@954 584 }
kaf24@954 585
kaf24@5703 586 static long evtchn_bind_vcpu(evtchn_bind_vcpu_t *bind)
iap10@5691 587 {
iap10@5691 588 struct domain *d = current->domain;
iap10@5691 589 int port = bind->port;
iap10@5691 590 int vcpu = bind->vcpu;
iap10@5691 591 struct evtchn *chn;
kaf24@5703 592 long rc = 0;
kaf24@5703 593
sos22@5705 594 if ( (vcpu >= MAX_VIRT_CPUS) || (d->vcpu[vcpu] == NULL) ) {
kaf24@5703 595 return -EINVAL;
sos22@5705 596 }
iap10@5691 597
iap10@5691 598 spin_lock(&d->evtchn_lock);
iap10@5691 599
iap10@5691 600 if ( !port_is_valid(d, port) )
iap10@5691 601 {
iap10@5691 602 rc = -EINVAL;
iap10@5691 603 goto out;
iap10@5691 604 }
iap10@5691 605
iap10@5691 606 chn = evtchn_from_port(d, port);
kaf24@5703 607 switch ( chn->state )
kaf24@5703 608 {
kaf24@5703 609 case ECS_UNBOUND:
kaf24@5703 610 case ECS_INTERDOMAIN:
kaf24@5703 611 case ECS_PIRQ:
kaf24@5703 612 chn->notify_vcpu_id = vcpu;
kaf24@5703 613 break;
kaf24@5703 614 default:
kaf24@5703 615 rc = -EINVAL;
kaf24@5703 616 break;
kaf24@5703 617 }
iap10@5691 618
iap10@5691 619 out:
iap10@5691 620 spin_unlock(&d->evtchn_lock);
iap10@5691 621 return rc;
iap10@5691 622 }
kaf24@954 623
kaf24@1127 624 long do_event_channel_op(evtchn_op_t *uop)
kaf24@954 625 {
kaf24@954 626 long rc;
kaf24@1127 627 evtchn_op_t op;
kaf24@954 628
kaf24@1127 629 if ( copy_from_user(&op, uop, sizeof(op)) != 0 )
kaf24@1127 630 return -EFAULT;
kaf24@1127 631
smh22@5514 632 if (acm_pre_event_channel(&op))
smh22@5514 633 return -EACCES;
smh22@5514 634
kaf24@1127 635 switch ( op.cmd )
kaf24@954 636 {
kaf24@2713 637 case EVTCHNOP_alloc_unbound:
kaf24@2713 638 rc = evtchn_alloc_unbound(&op.u.alloc_unbound);
kaf24@2713 639 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@2713 640 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@2713 641 break;
kaf24@2713 642
kaf24@1218 643 case EVTCHNOP_bind_interdomain:
kaf24@1218 644 rc = evtchn_bind_interdomain(&op.u.bind_interdomain);
kaf24@1235 645 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1218 646 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@1218 647 break;
kaf24@1218 648
kaf24@1218 649 case EVTCHNOP_bind_virq:
kaf24@1218 650 rc = evtchn_bind_virq(&op.u.bind_virq);
kaf24@1235 651 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1235 652 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@1235 653 break;
kaf24@1235 654
cl349@2932 655 case EVTCHNOP_bind_ipi:
cl349@2932 656 rc = evtchn_bind_ipi(&op.u.bind_ipi);
cl349@2932 657 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
cl349@2932 658 rc = -EFAULT; /* Cleaning up here would be a mess! */
cl349@2932 659 break;
cl349@2932 660
kaf24@1235 661 case EVTCHNOP_bind_pirq:
kaf24@1235 662 rc = evtchn_bind_pirq(&op.u.bind_pirq);
kaf24@1235 663 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1127 664 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@954 665 break;
kaf24@954 666
kaf24@954 667 case EVTCHNOP_close:
kaf24@1218 668 rc = evtchn_close(&op.u.close);
kaf24@954 669 break;
kaf24@954 670
kaf24@954 671 case EVTCHNOP_send:
kaf24@1218 672 rc = evtchn_send(op.u.send.local_port);
kaf24@954 673 break;
kaf24@954 674
kaf24@954 675 case EVTCHNOP_status:
kaf24@1218 676 rc = evtchn_status(&op.u.status);
kaf24@1235 677 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1127 678 rc = -EFAULT;
kaf24@954 679 break;
kaf24@954 680
kaf24@5703 681 case EVTCHNOP_bind_vcpu:
kaf24@5703 682 rc = evtchn_bind_vcpu(&op.u.bind_vcpu);
iap10@5691 683 break;
iap10@5691 684
kaf24@954 685 default:
kaf24@954 686 rc = -ENOSYS;
kaf24@954 687 break;
kaf24@954 688 }
kaf24@954 689
kaf24@954 690 return rc;
kaf24@954 691 }
kaf24@954 692
kaf24@954 693
kaf24@5308 694 int evtchn_init(struct domain *d)
kaf24@1218 695 {
kaf24@5308 696 spin_lock_init(&d->evtchn_lock);
kaf24@5308 697 if ( get_free_port(d) != 0 )
cl349@3291 698 return -EINVAL;
kaf24@5308 699 evtchn_from_port(d, 0)->state = ECS_RESERVED;
cl349@3291 700 return 0;
kaf24@1218 701 }
kaf24@1218 702
kaf24@1218 703
kaf24@5308 704 void evtchn_destroy(struct domain *d)
kaf24@954 705 {
kaf24@954 706 int i;
kaf24@5308 707
kaf24@5308 708 for ( i = 0; port_is_valid(d, i); i++ )
kaf24@1506 709 (void)__evtchn_close(d, i);
kaf24@5308 710
kaf24@5308 711 for ( i = 0; i < NR_EVTCHN_BUCKETS; i++ )
kaf24@5308 712 if ( d->evtchn[i] != NULL )
kaf24@5308 713 xfree(d->evtchn[i]);
kaf24@954 714 }
kaf24@3914 715
kaf24@3914 716 /*
kaf24@3914 717 * Local variables:
kaf24@3914 718 * mode: C
kaf24@3914 719 * c-set-style: "BSD"
kaf24@3914 720 * c-basic-offset: 4
kaf24@3914 721 * tab-width: 4
kaf24@3914 722 * indent-tabs-mode: nil
kaf24@3988 723 * End:
kaf24@3914 724 */