ia64/xen-unstable

annotate xen/common/event_channel.c @ 5514:aa52b853c28b

bitkeeper revision 1.1718.1.7 (42b742f8NxTuN2pqCHFAWI78dbEYKw)

Initial MAC (sHype) support from IBM.
Defaults to NULL policy for now.

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