ia64/xen-unstable

view xen/common/event_channel.c @ 18594:5e4e234d58be

x86: Define __per_cpu_shift label to help kdump/crashdump.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Oct 08 13:11:06 2008 +0100 (2008-10-08)
parents 9a7b46546e05
children 0033c944318f
line source
1 /******************************************************************************
2 * event_channel.c
3 *
4 * Event notifications from VIRQs, PIRQs, and other domains.
5 *
6 * Copyright (c) 2003-2006, K A Fraser.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
18 #include <xen/config.h>
19 #include <xen/init.h>
20 #include <xen/lib.h>
21 #include <xen/errno.h>
22 #include <xen/sched.h>
23 #include <xen/event.h>
24 #include <xen/irq.h>
25 #include <xen/iocap.h>
26 #include <xen/compat.h>
27 #include <xen/guest_access.h>
28 #include <xen/keyhandler.h>
29 #include <asm/current.h>
31 #include <public/xen.h>
32 #include <public/event_channel.h>
33 #include <xsm/xsm.h>
35 #define bucket_from_port(d,p) \
36 ((d)->evtchn[(p)/EVTCHNS_PER_BUCKET])
37 #define port_is_valid(d,p) \
38 (((p) >= 0) && ((p) < MAX_EVTCHNS(d)) && \
39 (bucket_from_port(d,p) != NULL))
40 #define evtchn_from_port(d,p) \
41 (&(bucket_from_port(d,p))[(p)&(EVTCHNS_PER_BUCKET-1)])
43 #define ERROR_EXIT(_errno) \
44 do { \
45 gdprintk(XENLOG_WARNING, \
46 "EVTCHNOP failure: error %d\n", \
47 (_errno)); \
48 rc = (_errno); \
49 goto out; \
50 } while ( 0 )
51 #define ERROR_EXIT_DOM(_errno, _dom) \
52 do { \
53 gdprintk(XENLOG_WARNING, \
54 "EVTCHNOP failure: domain %d, error %d\n", \
55 (_dom)->domain_id, (_errno)); \
56 rc = (_errno); \
57 goto out; \
58 } while ( 0 )
60 static int evtchn_set_pending(struct vcpu *v, int port);
62 static int virq_is_global(int virq)
63 {
64 int rc;
66 ASSERT((virq >= 0) && (virq < NR_VIRQS));
68 switch ( virq )
69 {
70 case VIRQ_TIMER:
71 case VIRQ_DEBUG:
72 case VIRQ_XENOPROF:
73 rc = 0;
74 break;
75 case VIRQ_ARCH_0 ... VIRQ_ARCH_7:
76 rc = arch_virq_is_global(virq);
77 break;
78 default:
79 rc = 1;
80 break;
81 }
83 return rc;
84 }
87 static int get_free_port(struct domain *d)
88 {
89 struct evtchn *chn;
90 int port;
91 int i, j;
93 if ( d->is_dying )
94 return -EINVAL;
96 for ( port = 0; port_is_valid(d, port); port++ )
97 if ( evtchn_from_port(d, port)->state == ECS_FREE )
98 return port;
100 if ( port == MAX_EVTCHNS(d) )
101 return -ENOSPC;
103 chn = xmalloc_array(struct evtchn, EVTCHNS_PER_BUCKET);
104 if ( unlikely(chn == NULL) )
105 return -ENOMEM;
106 memset(chn, 0, EVTCHNS_PER_BUCKET * sizeof(*chn));
107 bucket_from_port(d, port) = chn;
109 for ( i = 0; i < EVTCHNS_PER_BUCKET; i++ )
110 {
111 if ( xsm_alloc_security_evtchn(&chn[i]) )
112 {
113 for ( j = 0; j < i; j++ )
114 xsm_free_security_evtchn(&chn[j]);
115 xfree(chn);
116 return -ENOMEM;
117 }
118 }
120 return port;
121 }
124 static long evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
125 {
126 struct evtchn *chn;
127 struct domain *d;
128 int port;
129 domid_t dom = alloc->dom;
130 long rc;
132 rc = rcu_lock_target_domain_by_id(dom, &d);
133 if ( rc )
134 return rc;
136 spin_lock(&d->evtchn_lock);
138 if ( (port = get_free_port(d)) < 0 )
139 ERROR_EXIT_DOM(port, d);
140 chn = evtchn_from_port(d, port);
142 rc = xsm_evtchn_unbound(d, chn, alloc->remote_dom);
143 if ( rc )
144 goto out;
146 chn->state = ECS_UNBOUND;
147 if ( (chn->u.unbound.remote_domid = alloc->remote_dom) == DOMID_SELF )
148 chn->u.unbound.remote_domid = current->domain->domain_id;
150 alloc->port = port;
152 out:
153 spin_unlock(&d->evtchn_lock);
154 rcu_unlock_domain(d);
156 return rc;
157 }
160 static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
161 {
162 struct evtchn *lchn, *rchn;
163 struct domain *ld = current->domain, *rd;
164 int lport, rport = bind->remote_port;
165 domid_t rdom = bind->remote_dom;
166 long rc;
168 if ( rdom == DOMID_SELF )
169 rdom = current->domain->domain_id;
171 if ( (rd = rcu_lock_domain_by_id(rdom)) == NULL )
172 return -ESRCH;
174 /* Avoid deadlock by first acquiring lock of domain with smaller id. */
175 if ( ld < rd )
176 {
177 spin_lock(&ld->evtchn_lock);
178 spin_lock(&rd->evtchn_lock);
179 }
180 else
181 {
182 if ( ld != rd )
183 spin_lock(&rd->evtchn_lock);
184 spin_lock(&ld->evtchn_lock);
185 }
187 if ( (lport = get_free_port(ld)) < 0 )
188 ERROR_EXIT(lport);
189 lchn = evtchn_from_port(ld, lport);
191 if ( !port_is_valid(rd, rport) )
192 ERROR_EXIT_DOM(-EINVAL, rd);
193 rchn = evtchn_from_port(rd, rport);
194 if ( (rchn->state != ECS_UNBOUND) ||
195 (rchn->u.unbound.remote_domid != ld->domain_id) )
196 ERROR_EXIT_DOM(-EINVAL, rd);
198 rc = xsm_evtchn_interdomain(ld, lchn, rd, rchn);
199 if ( rc )
200 goto out;
202 lchn->u.interdomain.remote_dom = rd;
203 lchn->u.interdomain.remote_port = (u16)rport;
204 lchn->state = ECS_INTERDOMAIN;
206 rchn->u.interdomain.remote_dom = ld;
207 rchn->u.interdomain.remote_port = (u16)lport;
208 rchn->state = ECS_INTERDOMAIN;
210 /*
211 * We may have lost notifications on the remote unbound port. Fix that up
212 * here by conservatively always setting a notification on the local port.
213 */
214 evtchn_set_pending(ld->vcpu[lchn->notify_vcpu_id], lport);
216 bind->local_port = lport;
218 out:
219 spin_unlock(&ld->evtchn_lock);
220 if ( ld != rd )
221 spin_unlock(&rd->evtchn_lock);
223 rcu_unlock_domain(rd);
225 return rc;
226 }
229 static long evtchn_bind_virq(evtchn_bind_virq_t *bind)
230 {
231 struct evtchn *chn;
232 struct vcpu *v;
233 struct domain *d = current->domain;
234 int port, virq = bind->virq, vcpu = bind->vcpu;
235 long rc = 0;
237 if ( (virq < 0) || (virq >= ARRAY_SIZE(v->virq_to_evtchn)) )
238 return -EINVAL;
240 if ( virq_is_global(virq) && (vcpu != 0) )
241 return -EINVAL;
243 if ( (vcpu < 0) || (vcpu >= ARRAY_SIZE(d->vcpu)) ||
244 ((v = d->vcpu[vcpu]) == NULL) )
245 return -ENOENT;
247 spin_lock(&d->evtchn_lock);
249 if ( v->virq_to_evtchn[virq] != 0 )
250 ERROR_EXIT(-EEXIST);
252 if ( (port = get_free_port(d)) < 0 )
253 ERROR_EXIT(port);
255 chn = evtchn_from_port(d, port);
256 chn->state = ECS_VIRQ;
257 chn->notify_vcpu_id = vcpu;
258 chn->u.virq = virq;
260 v->virq_to_evtchn[virq] = bind->port = port;
262 out:
263 spin_unlock(&d->evtchn_lock);
265 return rc;
266 }
269 static long evtchn_bind_ipi(evtchn_bind_ipi_t *bind)
270 {
271 struct evtchn *chn;
272 struct domain *d = current->domain;
273 int port, vcpu = bind->vcpu;
274 long rc = 0;
276 if ( (vcpu < 0) || (vcpu >= ARRAY_SIZE(d->vcpu)) ||
277 (d->vcpu[vcpu] == NULL) )
278 return -ENOENT;
280 spin_lock(&d->evtchn_lock);
282 if ( (port = get_free_port(d)) < 0 )
283 ERROR_EXIT(port);
285 chn = evtchn_from_port(d, port);
286 chn->state = ECS_IPI;
287 chn->notify_vcpu_id = vcpu;
289 bind->port = port;
291 out:
292 spin_unlock(&d->evtchn_lock);
294 return rc;
295 }
298 static long evtchn_bind_pirq(evtchn_bind_pirq_t *bind)
299 {
300 struct evtchn *chn;
301 struct domain *d = current->domain;
302 int port, pirq = bind->pirq;
303 long rc;
305 if ( (pirq < 0) || (pirq >= ARRAY_SIZE(d->pirq_to_evtchn)) )
306 return -EINVAL;
308 if ( !irq_access_permitted(d, pirq) )
309 return -EPERM;
311 spin_lock(&d->evtchn_lock);
313 if ( d->pirq_to_evtchn[pirq] != 0 )
314 ERROR_EXIT(-EEXIST);
316 if ( (port = get_free_port(d)) < 0 )
317 ERROR_EXIT(port);
319 chn = evtchn_from_port(d, port);
321 d->pirq_to_evtchn[pirq] = port;
322 rc = pirq_guest_bind(d->vcpu[0], pirq,
323 !!(bind->flags & BIND_PIRQ__WILL_SHARE));
324 if ( rc != 0 )
325 {
326 d->pirq_to_evtchn[pirq] = 0;
327 goto out;
328 }
330 chn->state = ECS_PIRQ;
331 chn->u.pirq = pirq;
333 bind->port = port;
335 out:
336 spin_unlock(&d->evtchn_lock);
338 return rc;
339 }
342 static long __evtchn_close(struct domain *d1, int port1)
343 {
344 struct domain *d2 = NULL;
345 struct vcpu *v;
346 struct evtchn *chn1, *chn2;
347 int port2;
348 long rc = 0;
350 again:
351 spin_lock(&d1->evtchn_lock);
353 if ( !port_is_valid(d1, port1) )
354 {
355 rc = -EINVAL;
356 goto out;
357 }
359 chn1 = evtchn_from_port(d1, port1);
361 /* Guest cannot close a Xen-attached event channel. */
362 if ( unlikely(chn1->consumer_is_xen) )
363 {
364 rc = -EINVAL;
365 goto out;
366 }
368 switch ( chn1->state )
369 {
370 case ECS_FREE:
371 case ECS_RESERVED:
372 rc = -EINVAL;
373 goto out;
375 case ECS_UNBOUND:
376 break;
378 case ECS_PIRQ:
379 pirq_guest_unbind(d1, chn1->u.pirq);
380 d1->pirq_to_evtchn[chn1->u.pirq] = 0;
381 break;
383 case ECS_VIRQ:
384 for_each_vcpu ( d1, v )
385 {
386 if ( v->virq_to_evtchn[chn1->u.virq] != port1 )
387 continue;
388 v->virq_to_evtchn[chn1->u.virq] = 0;
389 spin_barrier(&v->virq_lock);
390 }
391 break;
393 case ECS_IPI:
394 break;
396 case ECS_INTERDOMAIN:
397 if ( d2 == NULL )
398 {
399 d2 = chn1->u.interdomain.remote_dom;
401 /* If we unlock d1 then we could lose d2. Must get a reference. */
402 if ( unlikely(!get_domain(d2)) )
403 BUG();
405 if ( d1 < d2 )
406 {
407 spin_lock(&d2->evtchn_lock);
408 }
409 else if ( d1 != d2 )
410 {
411 spin_unlock(&d1->evtchn_lock);
412 spin_lock(&d2->evtchn_lock);
413 goto again;
414 }
415 }
416 else if ( d2 != chn1->u.interdomain.remote_dom )
417 {
418 /*
419 * We can only get here if the port was closed and re-bound after
420 * unlocking d1 but before locking d2 above. We could retry but
421 * it is easier to return the same error as if we had seen the
422 * port in ECS_CLOSED. It must have passed through that state for
423 * us to end up here, so it's a valid error to return.
424 */
425 rc = -EINVAL;
426 goto out;
427 }
429 port2 = chn1->u.interdomain.remote_port;
430 BUG_ON(!port_is_valid(d2, port2));
432 chn2 = evtchn_from_port(d2, port2);
433 BUG_ON(chn2->state != ECS_INTERDOMAIN);
434 BUG_ON(chn2->u.interdomain.remote_dom != d1);
436 chn2->state = ECS_UNBOUND;
437 chn2->u.unbound.remote_domid = d1->domain_id;
438 break;
440 default:
441 BUG();
442 }
444 /* Clear pending event to avoid unexpected behavior on re-bind. */
445 clear_bit(port1, &shared_info(d1, evtchn_pending));
447 /* Reset binding to vcpu0 when the channel is freed. */
448 chn1->state = ECS_FREE;
449 chn1->notify_vcpu_id = 0;
451 xsm_evtchn_close_post(chn1);
453 out:
454 if ( d2 != NULL )
455 {
456 if ( d1 != d2 )
457 spin_unlock(&d2->evtchn_lock);
458 put_domain(d2);
459 }
461 spin_unlock(&d1->evtchn_lock);
463 return rc;
464 }
467 static long evtchn_close(evtchn_close_t *close)
468 {
469 return __evtchn_close(current->domain, close->port);
470 }
472 int evtchn_send(struct domain *d, unsigned int lport)
473 {
474 struct evtchn *lchn, *rchn;
475 struct domain *ld = d, *rd;
476 struct vcpu *rvcpu;
477 int rport, ret = 0;
479 spin_lock(&ld->evtchn_lock);
481 if ( unlikely(!port_is_valid(ld, lport)) )
482 {
483 spin_unlock(&ld->evtchn_lock);
484 return -EINVAL;
485 }
487 lchn = evtchn_from_port(ld, lport);
489 /* Guest cannot send via a Xen-attached event channel. */
490 if ( unlikely(lchn->consumer_is_xen) )
491 {
492 spin_unlock(&ld->evtchn_lock);
493 return -EINVAL;
494 }
496 ret = xsm_evtchn_send(ld, lchn);
497 if ( ret )
498 goto out;
500 switch ( lchn->state )
501 {
502 case ECS_INTERDOMAIN:
503 rd = lchn->u.interdomain.remote_dom;
504 rport = lchn->u.interdomain.remote_port;
505 rchn = evtchn_from_port(rd, rport);
506 rvcpu = rd->vcpu[rchn->notify_vcpu_id];
507 if ( rchn->consumer_is_xen )
508 {
509 /* Xen consumers need notification only if they are blocked. */
510 if ( test_and_clear_bit(_VPF_blocked_in_xen,
511 &rvcpu->pause_flags) )
512 vcpu_wake(rvcpu);
513 }
514 else
515 {
516 evtchn_set_pending(rvcpu, rport);
517 }
518 break;
519 case ECS_IPI:
520 evtchn_set_pending(ld->vcpu[lchn->notify_vcpu_id], lport);
521 break;
522 case ECS_UNBOUND:
523 /* silently drop the notification */
524 break;
525 default:
526 ret = -EINVAL;
527 }
529 out:
530 spin_unlock(&ld->evtchn_lock);
532 return ret;
533 }
535 static int evtchn_set_pending(struct vcpu *v, int port)
536 {
537 struct domain *d = v->domain;
538 int vcpuid;
540 /*
541 * The following bit operations must happen in strict order.
542 * NB. On x86, the atomic bit operations also act as memory barriers.
543 * There is therefore sufficiently strict ordering for this architecture --
544 * others may require explicit memory barriers.
545 */
547 if ( test_and_set_bit(port, &shared_info(d, evtchn_pending)) )
548 return 1;
550 if ( !test_bit (port, &shared_info(d, evtchn_mask)) &&
551 !test_and_set_bit(port / BITS_PER_GUEST_LONG(d),
552 &vcpu_info(v, evtchn_pending_sel)) )
553 {
554 vcpu_mark_events_pending(v);
555 }
557 /* Check if some VCPU might be polling for this event. */
558 if ( likely(bitmap_empty(d->poll_mask, MAX_VIRT_CPUS)) )
559 return 0;
561 /* Wake any interested (or potentially interested) pollers. */
562 for ( vcpuid = find_first_bit(d->poll_mask, MAX_VIRT_CPUS);
563 vcpuid < MAX_VIRT_CPUS;
564 vcpuid = find_next_bit(d->poll_mask, MAX_VIRT_CPUS, vcpuid+1) )
565 {
566 v = d->vcpu[vcpuid];
567 if ( ((v->poll_evtchn <= 0) || (v->poll_evtchn == port)) &&
568 test_and_clear_bit(vcpuid, d->poll_mask) )
569 {
570 v->poll_evtchn = 0;
571 vcpu_unblock(v);
572 }
573 }
575 return 0;
576 }
578 int guest_enabled_event(struct vcpu *v, int virq)
579 {
580 return ((v != NULL) && (v->virq_to_evtchn[virq] != 0));
581 }
583 void send_guest_vcpu_virq(struct vcpu *v, int virq)
584 {
585 unsigned long flags;
586 int port;
588 ASSERT(!virq_is_global(virq));
590 spin_lock_irqsave(&v->virq_lock, flags);
592 port = v->virq_to_evtchn[virq];
593 if ( unlikely(port == 0) )
594 goto out;
596 evtchn_set_pending(v, port);
598 out:
599 spin_unlock_irqrestore(&v->virq_lock, flags);
600 }
602 void send_guest_global_virq(struct domain *d, int virq)
603 {
604 unsigned long flags;
605 int port;
606 struct vcpu *v;
607 struct evtchn *chn;
609 ASSERT(virq_is_global(virq));
611 if ( unlikely(d == NULL) )
612 return;
614 v = d->vcpu[0];
615 if ( unlikely(v == NULL) )
616 return;
618 spin_lock_irqsave(&v->virq_lock, flags);
620 port = v->virq_to_evtchn[virq];
621 if ( unlikely(port == 0) )
622 goto out;
624 chn = evtchn_from_port(d, port);
625 evtchn_set_pending(d->vcpu[chn->notify_vcpu_id], port);
627 out:
628 spin_unlock_irqrestore(&v->virq_lock, flags);
629 }
631 int send_guest_pirq(struct domain *d, int pirq)
632 {
633 int port = d->pirq_to_evtchn[pirq];
634 struct evtchn *chn;
636 /*
637 * It should not be possible to race with __evtchn_close():
638 * The caller of this function must synchronise with pirq_guest_unbind().
639 */
640 ASSERT(port != 0);
642 chn = evtchn_from_port(d, port);
643 return evtchn_set_pending(d->vcpu[chn->notify_vcpu_id], port);
644 }
647 static long evtchn_status(evtchn_status_t *status)
648 {
649 struct domain *d;
650 domid_t dom = status->dom;
651 int port = status->port;
652 struct evtchn *chn;
653 long rc = 0;
655 rc = rcu_lock_target_domain_by_id(dom, &d);
656 if ( rc )
657 return rc;
659 spin_lock(&d->evtchn_lock);
661 if ( !port_is_valid(d, port) )
662 {
663 rc = -EINVAL;
664 goto out;
665 }
667 chn = evtchn_from_port(d, port);
669 rc = xsm_evtchn_status(d, chn);
670 if ( rc )
671 goto out;
673 switch ( chn->state )
674 {
675 case ECS_FREE:
676 case ECS_RESERVED:
677 status->status = EVTCHNSTAT_closed;
678 break;
679 case ECS_UNBOUND:
680 status->status = EVTCHNSTAT_unbound;
681 status->u.unbound.dom = chn->u.unbound.remote_domid;
682 break;
683 case ECS_INTERDOMAIN:
684 status->status = EVTCHNSTAT_interdomain;
685 status->u.interdomain.dom =
686 chn->u.interdomain.remote_dom->domain_id;
687 status->u.interdomain.port = chn->u.interdomain.remote_port;
688 break;
689 case ECS_PIRQ:
690 status->status = EVTCHNSTAT_pirq;
691 status->u.pirq = chn->u.pirq;
692 break;
693 case ECS_VIRQ:
694 status->status = EVTCHNSTAT_virq;
695 status->u.virq = chn->u.virq;
696 break;
697 case ECS_IPI:
698 status->status = EVTCHNSTAT_ipi;
699 break;
700 default:
701 BUG();
702 }
704 status->vcpu = chn->notify_vcpu_id;
706 out:
707 spin_unlock(&d->evtchn_lock);
708 rcu_unlock_domain(d);
710 return rc;
711 }
714 long evtchn_bind_vcpu(unsigned int port, unsigned int vcpu_id)
715 {
716 struct domain *d = current->domain;
717 struct evtchn *chn;
718 long rc = 0;
720 if ( (vcpu_id >= ARRAY_SIZE(d->vcpu)) || (d->vcpu[vcpu_id] == NULL) )
721 return -ENOENT;
723 spin_lock(&d->evtchn_lock);
725 if ( !port_is_valid(d, port) )
726 {
727 rc = -EINVAL;
728 goto out;
729 }
731 chn = evtchn_from_port(d, port);
733 /* Guest cannot re-bind a Xen-attached event channel. */
734 if ( unlikely(chn->consumer_is_xen) )
735 {
736 rc = -EINVAL;
737 goto out;
738 }
740 switch ( chn->state )
741 {
742 case ECS_VIRQ:
743 if ( virq_is_global(chn->u.virq) )
744 chn->notify_vcpu_id = vcpu_id;
745 else
746 rc = -EINVAL;
747 break;
748 case ECS_UNBOUND:
749 case ECS_INTERDOMAIN:
750 case ECS_PIRQ:
751 chn->notify_vcpu_id = vcpu_id;
752 break;
753 default:
754 rc = -EINVAL;
755 break;
756 }
758 out:
759 spin_unlock(&d->evtchn_lock);
761 return rc;
762 }
765 static long evtchn_unmask(evtchn_unmask_t *unmask)
766 {
767 struct domain *d = current->domain;
768 int port = unmask->port;
769 struct vcpu *v;
771 spin_lock(&d->evtchn_lock);
773 if ( unlikely(!port_is_valid(d, port)) )
774 {
775 spin_unlock(&d->evtchn_lock);
776 return -EINVAL;
777 }
779 v = d->vcpu[evtchn_from_port(d, port)->notify_vcpu_id];
781 /*
782 * These operations must happen in strict order. Based on
783 * include/xen/event.h:evtchn_set_pending().
784 */
785 if ( test_and_clear_bit(port, &shared_info(d, evtchn_mask)) &&
786 test_bit (port, &shared_info(d, evtchn_pending)) &&
787 !test_and_set_bit (port / BITS_PER_GUEST_LONG(d),
788 &vcpu_info(v, evtchn_pending_sel)) )
789 {
790 vcpu_mark_events_pending(v);
791 }
793 spin_unlock(&d->evtchn_lock);
795 return 0;
796 }
799 static long evtchn_reset(evtchn_reset_t *r)
800 {
801 domid_t dom = r->dom;
802 struct domain *d;
803 int i, rc;
805 rc = rcu_lock_target_domain_by_id(dom, &d);
806 if ( rc )
807 return rc;
809 rc = xsm_evtchn_reset(current->domain, d);
810 if ( rc )
811 goto out;
813 for ( i = 0; port_is_valid(d, i); i++ )
814 (void)__evtchn_close(d, i);
816 rc = 0;
818 out:
819 rcu_unlock_domain(d);
821 return rc;
822 }
825 long do_event_channel_op(int cmd, XEN_GUEST_HANDLE(void) arg)
826 {
827 long rc;
829 switch ( cmd )
830 {
831 case EVTCHNOP_alloc_unbound: {
832 struct evtchn_alloc_unbound alloc_unbound;
833 if ( copy_from_guest(&alloc_unbound, arg, 1) != 0 )
834 return -EFAULT;
835 rc = evtchn_alloc_unbound(&alloc_unbound);
836 if ( (rc == 0) && (copy_to_guest(arg, &alloc_unbound, 1) != 0) )
837 rc = -EFAULT; /* Cleaning up here would be a mess! */
838 break;
839 }
841 case EVTCHNOP_bind_interdomain: {
842 struct evtchn_bind_interdomain bind_interdomain;
843 if ( copy_from_guest(&bind_interdomain, arg, 1) != 0 )
844 return -EFAULT;
845 rc = evtchn_bind_interdomain(&bind_interdomain);
846 if ( (rc == 0) && (copy_to_guest(arg, &bind_interdomain, 1) != 0) )
847 rc = -EFAULT; /* Cleaning up here would be a mess! */
848 break;
849 }
851 case EVTCHNOP_bind_virq: {
852 struct evtchn_bind_virq bind_virq;
853 if ( copy_from_guest(&bind_virq, arg, 1) != 0 )
854 return -EFAULT;
855 rc = evtchn_bind_virq(&bind_virq);
856 if ( (rc == 0) && (copy_to_guest(arg, &bind_virq, 1) != 0) )
857 rc = -EFAULT; /* Cleaning up here would be a mess! */
858 break;
859 }
861 case EVTCHNOP_bind_ipi: {
862 struct evtchn_bind_ipi bind_ipi;
863 if ( copy_from_guest(&bind_ipi, arg, 1) != 0 )
864 return -EFAULT;
865 rc = evtchn_bind_ipi(&bind_ipi);
866 if ( (rc == 0) && (copy_to_guest(arg, &bind_ipi, 1) != 0) )
867 rc = -EFAULT; /* Cleaning up here would be a mess! */
868 break;
869 }
871 case EVTCHNOP_bind_pirq: {
872 struct evtchn_bind_pirq bind_pirq;
873 if ( copy_from_guest(&bind_pirq, arg, 1) != 0 )
874 return -EFAULT;
875 rc = evtchn_bind_pirq(&bind_pirq);
876 if ( (rc == 0) && (copy_to_guest(arg, &bind_pirq, 1) != 0) )
877 rc = -EFAULT; /* Cleaning up here would be a mess! */
878 break;
879 }
881 case EVTCHNOP_close: {
882 struct evtchn_close close;
883 if ( copy_from_guest(&close, arg, 1) != 0 )
884 return -EFAULT;
885 rc = evtchn_close(&close);
886 break;
887 }
889 case EVTCHNOP_send: {
890 struct evtchn_send send;
891 if ( copy_from_guest(&send, arg, 1) != 0 )
892 return -EFAULT;
893 rc = evtchn_send(current->domain, send.port);
894 break;
895 }
897 case EVTCHNOP_status: {
898 struct evtchn_status status;
899 if ( copy_from_guest(&status, arg, 1) != 0 )
900 return -EFAULT;
901 rc = evtchn_status(&status);
902 if ( (rc == 0) && (copy_to_guest(arg, &status, 1) != 0) )
903 rc = -EFAULT;
904 break;
905 }
907 case EVTCHNOP_bind_vcpu: {
908 struct evtchn_bind_vcpu bind_vcpu;
909 if ( copy_from_guest(&bind_vcpu, arg, 1) != 0 )
910 return -EFAULT;
911 rc = evtchn_bind_vcpu(bind_vcpu.port, bind_vcpu.vcpu);
912 break;
913 }
915 case EVTCHNOP_unmask: {
916 struct evtchn_unmask unmask;
917 if ( copy_from_guest(&unmask, arg, 1) != 0 )
918 return -EFAULT;
919 rc = evtchn_unmask(&unmask);
920 break;
921 }
923 case EVTCHNOP_reset: {
924 struct evtchn_reset reset;
925 if ( copy_from_guest(&reset, arg, 1) != 0 )
926 return -EFAULT;
927 rc = evtchn_reset(&reset);
928 break;
929 }
931 default:
932 rc = -ENOSYS;
933 break;
934 }
936 return rc;
937 }
940 int alloc_unbound_xen_event_channel(
941 struct vcpu *local_vcpu, domid_t remote_domid)
942 {
943 struct evtchn *chn;
944 struct domain *d = local_vcpu->domain;
945 int port;
947 spin_lock(&d->evtchn_lock);
949 if ( (port = get_free_port(d)) < 0 )
950 goto out;
951 chn = evtchn_from_port(d, port);
953 chn->state = ECS_UNBOUND;
954 chn->consumer_is_xen = 1;
955 chn->notify_vcpu_id = local_vcpu->vcpu_id;
956 chn->u.unbound.remote_domid = remote_domid;
958 out:
959 spin_unlock(&d->evtchn_lock);
961 return port;
962 }
965 void free_xen_event_channel(
966 struct vcpu *local_vcpu, int port)
967 {
968 struct evtchn *chn;
969 struct domain *d = local_vcpu->domain;
971 spin_lock(&d->evtchn_lock);
973 if ( unlikely(d->is_dying) )
974 {
975 spin_unlock(&d->evtchn_lock);
976 return;
977 }
979 BUG_ON(!port_is_valid(d, port));
980 chn = evtchn_from_port(d, port);
981 BUG_ON(!chn->consumer_is_xen);
982 chn->consumer_is_xen = 0;
984 spin_unlock(&d->evtchn_lock);
986 (void)__evtchn_close(d, port);
987 }
990 void notify_via_xen_event_channel(int lport)
991 {
992 struct evtchn *lchn, *rchn;
993 struct domain *ld = current->domain, *rd;
994 int rport;
996 spin_lock(&ld->evtchn_lock);
998 ASSERT(port_is_valid(ld, lport));
999 lchn = evtchn_from_port(ld, lport);
1000 ASSERT(lchn->consumer_is_xen);
1002 if ( likely(lchn->state == ECS_INTERDOMAIN) )
1004 rd = lchn->u.interdomain.remote_dom;
1005 rport = lchn->u.interdomain.remote_port;
1006 rchn = evtchn_from_port(rd, rport);
1007 evtchn_set_pending(rd->vcpu[rchn->notify_vcpu_id], rport);
1010 spin_unlock(&ld->evtchn_lock);
1014 int evtchn_init(struct domain *d)
1016 spin_lock_init(&d->evtchn_lock);
1017 if ( get_free_port(d) != 0 )
1018 return -EINVAL;
1019 evtchn_from_port(d, 0)->state = ECS_RESERVED;
1020 return 0;
1024 void evtchn_destroy(struct domain *d)
1026 int i;
1028 /* After this barrier no new event-channel allocations can occur. */
1029 BUG_ON(!d->is_dying);
1030 spin_barrier(&d->evtchn_lock);
1032 /* Close all existing event channels. */
1033 for ( i = 0; port_is_valid(d, i); i++ )
1035 evtchn_from_port(d, i)->consumer_is_xen = 0;
1036 (void)__evtchn_close(d, i);
1039 /* Free all event-channel buckets. */
1040 spin_lock(&d->evtchn_lock);
1041 for ( i = 0; i < NR_EVTCHN_BUCKETS; i++ )
1043 xsm_free_security_evtchn(d->evtchn[i]);
1044 xfree(d->evtchn[i]);
1045 d->evtchn[i] = NULL;
1047 spin_unlock(&d->evtchn_lock);
1050 static void domain_dump_evtchn_info(struct domain *d)
1052 unsigned int port;
1054 printk("Domain %d polling vCPUs: %08lx\n", d->domain_id, d->poll_mask[0]);
1056 if ( !spin_trylock(&d->evtchn_lock) )
1057 return;
1059 printk("Event channel information for domain %d:\n",
1060 d->domain_id);
1062 for ( port = 1; port < MAX_EVTCHNS(d); ++port )
1064 const struct evtchn *chn;
1066 if ( !port_is_valid(d, port) )
1067 continue;
1068 chn = evtchn_from_port(d, port);
1069 if ( chn->state == ECS_FREE )
1070 continue;
1072 printk(" %4u[%d/%d]: s=%d n=%d",
1073 port,
1074 test_bit(port, &shared_info(d, evtchn_pending)),
1075 test_bit(port, &shared_info(d, evtchn_mask)),
1076 chn->state, chn->notify_vcpu_id);
1077 switch ( chn->state )
1079 case ECS_UNBOUND:
1080 printk(" d=%d", chn->u.unbound.remote_domid);
1081 break;
1082 case ECS_INTERDOMAIN:
1083 printk(" d=%d p=%d",
1084 chn->u.interdomain.remote_dom->domain_id,
1085 chn->u.interdomain.remote_port);
1086 break;
1087 case ECS_PIRQ:
1088 printk(" p=%d", chn->u.pirq);
1089 break;
1090 case ECS_VIRQ:
1091 printk(" v=%d", chn->u.virq);
1092 break;
1094 printk(" x=%d\n", chn->consumer_is_xen);
1097 spin_unlock(&d->evtchn_lock);
1100 static void dump_evtchn_info(unsigned char key)
1102 struct domain *d;
1104 printk("'%c' pressed -> dumping event-channel info\n", key);
1106 rcu_read_lock(&domlist_read_lock);
1108 for_each_domain ( d )
1109 domain_dump_evtchn_info(d);
1111 rcu_read_unlock(&domlist_read_lock);
1114 static int __init dump_evtchn_info_key_init(void)
1116 register_keyhandler('e', dump_evtchn_info, "dump evtchn info");
1117 return 0;
1119 __initcall(dump_evtchn_info_key_init);
1121 /*
1122 * Local variables:
1123 * mode: C
1124 * c-set-style: "BSD"
1125 * c-basic-offset: 4
1126 * tab-width: 4
1127 * indent-tabs-mode: nil
1128 * End:
1129 */