ia64/xen-unstable

changeset 17965:14fd83fe71c3

Add facility to get notification of domain suspend by event channel.
This event channel will be notified when the domain transitions to the
suspended state, which can be much faster than raising VIRQ_DOM_EXC
and waiting for the notification to be propagated via xenstore.

No attempt is made here to prevent multiple subscribers (last one
wins), or to detect that the subscriber has gone away. Userspace tools
should take care.

Signed-off-by: Brendan Cully <brendan@cs.ubc.ca>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jul 04 12:00:24 2008 +0100 (2008-07-04)
parents 9cf72db44ee9
children 2922c423a1aa
files tools/libxc/xc_domain.c tools/libxc/xenctrl.h xen/arch/x86/traps.c xen/arch/x86/x86_32/traps.c xen/arch/x86/x86_64/compat/traps.c xen/arch/x86/x86_64/traps.c xen/common/domain.c xen/common/domctl.c xen/include/public/domctl.h xen/include/xen/sched.h
line diff
     1.1 --- a/tools/libxc/xc_domain.c	Fri Jul 04 11:54:21 2008 +0100
     1.2 +++ b/tools/libxc/xc_domain.c	Fri Jul 04 12:00:24 2008 +0100
     1.3 @@ -981,6 +981,17 @@ int xc_domain_set_target(
     1.4      return do_domctl(xc_handle, &domctl);
     1.5  }
     1.6  
     1.7 +int xc_dom_subscribe(int xc_handle, domid_t dom, evtchn_port_t port)
     1.8 +{
     1.9 +    DECLARE_DOMCTL;
    1.10 +
    1.11 +    domctl.cmd = XEN_DOMCTL_subscribe;
    1.12 +    domctl.domain = dom;
    1.13 +    domctl.u.subscribe.port = port;
    1.14 +
    1.15 +    return do_domctl(xc_handle, &domctl);
    1.16 +}
    1.17 +
    1.18  /*
    1.19   * Local variables:
    1.20   * mode: C
     2.1 --- a/tools/libxc/xenctrl.h	Fri Jul 04 11:54:21 2008 +0100
     2.2 +++ b/tools/libxc/xenctrl.h	Fri Jul 04 12:00:24 2008 +0100
     2.3 @@ -810,6 +810,12 @@ int xc_acm_op(int xc_handle, int cmd, vo
     2.4  
     2.5  int xc_flask_op(int xc_handle, flask_op_t *op);
     2.6  
     2.7 +/*
     2.8 + * Subscribe to state changes in a domain via evtchn.
     2.9 + * Returns -1 on failure, in which case errno will be set appropriately.
    2.10 + */
    2.11 +int xc_dom_subscribe(int xc_handle, domid_t domid, evtchn_port_t port);
    2.12 +
    2.13  /**************************
    2.14   * GRANT TABLE OPERATIONS *
    2.15   **************************/
     3.1 --- a/xen/arch/x86/traps.c	Fri Jul 04 11:54:21 2008 +0100
     3.2 +++ b/xen/arch/x86/traps.c	Fri Jul 04 12:00:24 2008 +0100
     3.3 @@ -61,6 +61,7 @@
     3.4  #include <asm/msr.h>
     3.5  #include <asm/shared.h>
     3.6  #include <asm/x86_emulate.h>
     3.7 +#include <asm/traps.h>
     3.8  #include <asm/hvm/vpt.h>
     3.9  #include <public/arch-x86/cpuid.h>
    3.10  
    3.11 @@ -2678,25 +2679,51 @@ asmlinkage void do_general_protection(st
    3.12      panic("GENERAL PROTECTION FAULT\n[error_code=%04x]\n", regs->error_code);
    3.13  }
    3.14  
    3.15 +static DEFINE_PER_CPU(struct softirq_trap, softirq_trap);
    3.16 +
    3.17  static void nmi_mce_softirq(void)
    3.18  {
    3.19 -    /* Only used to defer wakeup of dom0,vcpu0 to a safe (non-NMI) context. */
    3.20 -    vcpu_kick(dom0->vcpu[0]);
    3.21 +    int cpu = smp_processor_id();
    3.22 +    struct softirq_trap *st = &per_cpu(softirq_trap, cpu);
    3.23 +    cpumask_t affinity;
    3.24 +
    3.25 +    BUG_ON(st == NULL);
    3.26 +    BUG_ON(st->vcpu == NULL);
    3.27 +
    3.28 +    /* Set the tmp value unconditionally, so that
    3.29 +     * the check in the iret hypercall works. */
    3.30 +    st->vcpu->cpu_affinity_tmp = st->vcpu->cpu_affinity;
    3.31 +
    3.32 +    if ((cpu != st->processor)
    3.33 +       || (st->processor != st->vcpu->processor))
    3.34 +    {
    3.35 +        /* We are on a different physical cpu.
    3.36 +         * Make sure to wakeup the vcpu on the
    3.37 +         * specified processor.
    3.38 +         */
    3.39 +        cpus_clear(affinity);
    3.40 +        cpu_set(st->processor, affinity);
    3.41 +        vcpu_set_affinity(st->vcpu, &affinity);
    3.42 +
    3.43 +        /* Affinity is restored in the iret hypercall. */
    3.44 +    }
    3.45 +
    3.46 +    /* Only used to defer wakeup of domain/vcpu to
    3.47 +     * a safe (non-NMI/MCE) context.
    3.48 +     */
    3.49 +    vcpu_kick(st->vcpu);
    3.50  }
    3.51  
    3.52  static void nmi_dom0_report(unsigned int reason_idx)
    3.53  {
    3.54 -    struct domain *d;
    3.55 -    struct vcpu   *v;
    3.56 -
    3.57 -    if ( ((d = dom0) == NULL) || ((v = d->vcpu[0]) == NULL) )
    3.58 +    struct domain *d = dom0;
    3.59 +
    3.60 +    if ( (d == NULL) || (d->vcpu[0] == NULL) )
    3.61          return;
    3.62  
    3.63      set_bit(reason_idx, nmi_reason(d));
    3.64  
    3.65 -    /* Not safe to wake a vcpu here, or even to schedule a tasklet! */
    3.66 -    if ( !test_and_set_bool(v->nmi_pending) )
    3.67 -        raise_softirq(NMI_MCE_SOFTIRQ);
    3.68 +    send_guest_trap(d, 0, TRAP_nmi);
    3.69  }
    3.70  
    3.71  asmlinkage void mem_parity_error(struct cpu_user_regs *regs)
    3.72 @@ -3010,6 +3037,35 @@ long unregister_guest_nmi_callback(void)
    3.73      return 0;
    3.74  }
    3.75  
    3.76 +int send_guest_trap(struct domain *d, uint16_t vcpuid, unsigned int trap_nr)
    3.77 +{
    3.78 +    struct vcpu *v;
    3.79 +    struct softirq_trap *st;
    3.80 +
    3.81 +    BUG_ON(d == NULL);
    3.82 +    BUG_ON(vcpuid >= MAX_VIRT_CPUS);
    3.83 +    v = d->vcpu[vcpuid];
    3.84 +
    3.85 +    switch (trap_nr) {
    3.86 +    case TRAP_nmi:
    3.87 +        if ( !test_and_set_bool(v->nmi_pending) ) {
    3.88 +               st = &per_cpu(softirq_trap, smp_processor_id());
    3.89 +               st->domain = dom0;
    3.90 +               st->vcpu = dom0->vcpu[0];
    3.91 +               st->processor = st->vcpu->processor;
    3.92 +
    3.93 +               /* not safe to wake up a vcpu here */
    3.94 +               raise_softirq(NMI_MCE_SOFTIRQ);
    3.95 +               return 0;
    3.96 +        }
    3.97 +        break;
    3.98 +    }
    3.99 +
   3.100 +    /* delivery failed */
   3.101 +    return -EIO;
   3.102 +}
   3.103 +
   3.104 +
   3.105  long do_set_trap_table(XEN_GUEST_HANDLE(const_trap_info_t) traps)
   3.106  {
   3.107      struct trap_info cur;
     4.1 --- a/xen/arch/x86/x86_32/traps.c	Fri Jul 04 11:54:21 2008 +0100
     4.2 +++ b/xen/arch/x86/x86_32/traps.c	Fri Jul 04 12:00:24 2008 +0100
     4.3 @@ -255,6 +255,10 @@ unsigned long do_iret(void)
     4.4              goto exit_and_crash;
     4.5      }
     4.6  
     4.7 +    /* Restore affinity.  */
     4.8 +    if (!cpus_equal(v->cpu_affinity_tmp, v->cpu_affinity))
     4.9 +        vcpu_set_affinity(v, &v->cpu_affinity_tmp);
    4.10 +
    4.11      /* No longer in NMI context. */
    4.12      v->nmi_masked = 0;
    4.13  
     5.1 --- a/xen/arch/x86/x86_64/compat/traps.c	Fri Jul 04 11:54:21 2008 +0100
     5.2 +++ b/xen/arch/x86/x86_64/compat/traps.c	Fri Jul 04 12:00:24 2008 +0100
     5.3 @@ -121,6 +121,10 @@ unsigned int compat_iret(void)
     5.4      else
     5.5          regs->_esp += 16;
     5.6  
     5.7 +    /* Restore affinity.  */
     5.8 +    if (!cpus_equal(v->cpu_affinity_tmp, v->cpu_affinity))
     5.9 +        vcpu_set_affinity(v, &v->cpu_affinity_tmp);
    5.10 +
    5.11      /* No longer in NMI context. */
    5.12      v->nmi_masked = 0;
    5.13  
     6.1 --- a/xen/arch/x86/x86_64/traps.c	Fri Jul 04 11:54:21 2008 +0100
     6.2 +++ b/xen/arch/x86/x86_64/traps.c	Fri Jul 04 12:00:24 2008 +0100
     6.3 @@ -288,6 +288,10 @@ unsigned long do_iret(void)
     6.4          regs->rcx = iret_saved.rcx;
     6.5      }
     6.6  
     6.7 +    /* Restore affinity.  */
     6.8 +    if (!cpus_equal(v->cpu_affinity_tmp, v->cpu_affinity))
     6.9 +        vcpu_set_affinity(v, &v->cpu_affinity_tmp);
    6.10 +
    6.11      /* No longer in NMI context. */
    6.12      v->nmi_masked = 0;
    6.13  
     7.1 --- a/xen/common/domain.c	Fri Jul 04 11:54:21 2008 +0100
     7.2 +++ b/xen/common/domain.c	Fri Jul 04 12:00:24 2008 +0100
     7.3 @@ -97,7 +97,13 @@ static void __domain_finalise_shutdown(s
     7.4              return;
     7.5  
     7.6      d->is_shut_down = 1;
     7.7 -    send_guest_global_virq(dom0, VIRQ_DOM_EXC);
     7.8 +    if ( d->shutdown_code == SHUTDOWN_suspend
     7.9 +         && d->suspend_evtchn > 0 )
    7.10 +    {
    7.11 +        evtchn_set_pending(dom0->vcpu[0], d->suspend_evtchn);
    7.12 +    }
    7.13 +    else
    7.14 +        send_guest_global_virq(dom0, VIRQ_DOM_EXC);
    7.15  }
    7.16  
    7.17  static void vcpu_check_shutdown(struct vcpu *v)
     8.1 --- a/xen/common/domctl.c	Fri Jul 04 11:54:21 2008 +0100
     8.2 +++ b/xen/common/domctl.c	Fri Jul 04 12:00:24 2008 +0100
     8.3 @@ -824,6 +824,21 @@ long do_domctl(XEN_GUEST_HANDLE(xen_domc
     8.4      }
     8.5      break;
     8.6  
     8.7 +    case XEN_DOMCTL_subscribe:
     8.8 +    {
     8.9 +        struct domain *d;
    8.10 +
    8.11 +        ret = -ESRCH;
    8.12 +        d = rcu_lock_domain_by_id(op->domain);
    8.13 +        if ( d != NULL )
    8.14 +        {
    8.15 +            d->suspend_evtchn = op->u.subscribe.port;
    8.16 +            rcu_unlock_domain(d);
    8.17 +            ret = 0;
    8.18 +        }
    8.19 +    }
    8.20 +    break;
    8.21 +
    8.22      default:
    8.23          ret = arch_do_domctl(op, u_domctl);
    8.24          break;
     9.1 --- a/xen/include/public/domctl.h	Fri Jul 04 11:54:21 2008 +0100
     9.2 +++ b/xen/include/public/domctl.h	Fri Jul 04 12:00:24 2008 +0100
     9.3 @@ -600,6 +600,13 @@ typedef struct xen_domctl_cpuid xen_domc
     9.4  DEFINE_XEN_GUEST_HANDLE(xen_domctl_cpuid_t);
     9.5  #endif
     9.6  
     9.7 +#define XEN_DOMCTL_subscribe          29
     9.8 +struct xen_domctl_subscribe {
     9.9 +    uint32_t port; /* IN */
    9.10 +};
    9.11 +typedef struct xen_domctl_subscribe xen_domctl_subscribe_t;
    9.12 +DEFINE_XEN_GUEST_HANDLE(xen_domctl_subscribe_t);
    9.13 +
    9.14  struct xen_domctl {
    9.15      uint32_t cmd;
    9.16      uint32_t interface_version; /* XEN_DOMCTL_INTERFACE_VERSION */
    9.17 @@ -638,6 +645,7 @@ struct xen_domctl {
    9.18          struct xen_domctl_ext_vcpucontext   ext_vcpucontext;
    9.19          struct xen_domctl_set_opt_feature   set_opt_feature;
    9.20          struct xen_domctl_set_target        set_target;
    9.21 +        struct xen_domctl_subscribe         subscribe;
    9.22  #if defined(__i386__) || defined(__x86_64__)
    9.23          struct xen_domctl_cpuid             cpuid;
    9.24  #endif
    10.1 --- a/xen/include/xen/sched.h	Fri Jul 04 11:54:21 2008 +0100
    10.2 +++ b/xen/include/xen/sched.h	Fri Jul 04 12:00:24 2008 +0100
    10.3 @@ -130,6 +130,8 @@ struct vcpu
    10.4  
    10.5      /* Bitmask of CPUs on which this VCPU may run. */
    10.6      cpumask_t        cpu_affinity;
    10.7 +    /* Used to change affinity temporarily. */
    10.8 +    cpumask_t        cpu_affinity_tmp;
    10.9  
   10.10      /* Bitmask of CPUs which are holding onto this VCPU's state. */
   10.11      cpumask_t        vcpu_dirty_cpumask;
   10.12 @@ -209,6 +211,10 @@ struct domain
   10.13      bool_t           is_shut_down;     /* fully shut down? */
   10.14      int              shutdown_code;
   10.15  
   10.16 +    /* If this is not 0, send suspend notification here instead of
   10.17 +     * raising DOM_EXC */
   10.18 +    int              suspend_evtchn;
   10.19 +
   10.20      atomic_t         pause_count;
   10.21  
   10.22      unsigned long    vm_assist;