ia64/xen-unstable

annotate xen/common/event_channel.c @ 5700:f3f845297821

Quick hack to make sure that pirqs and interdomain event channels are
all bound to vcpu0, rather vcpu0 99% of the time and an entirely
random otherwise. This make sure things work nicely when you start
taking vcpus offline, since vcpu0 can't go offline; the correct
solution is to allow domains to change evtchn<->vcpu bindings.

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