Home | History | Annotate | Line # | Download | only in x86
xen_ipi.c revision 1.4.2.2
      1  1.4.2.2  jym /* $NetBSD: xen_ipi.c,v 1.4.2.2 2011/08/27 15:59:49 jym Exp $ */
      2  1.4.2.2  jym 
      3  1.4.2.2  jym /*-
      4  1.4.2.2  jym  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  1.4.2.2  jym  * All rights reserved.
      6  1.4.2.2  jym  *
      7  1.4.2.2  jym  * This code is derived from software contributed to The NetBSD Foundation
      8  1.4.2.2  jym  * by Cherry G. Mathew <cherry (at) zyx.in>
      9  1.4.2.2  jym  *
     10  1.4.2.2  jym  * Redistribution and use in source and binary forms, with or without
     11  1.4.2.2  jym  * modification, are permitted provided that the following conditions
     12  1.4.2.2  jym  * are met:
     13  1.4.2.2  jym  * 1. Redistributions of source code must retain the above copyright
     14  1.4.2.2  jym  *    notice, this list of conditions and the following disclaimer.
     15  1.4.2.2  jym  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.4.2.2  jym  *    notice, this list of conditions and the following disclaimer in the
     17  1.4.2.2  jym  *    documentation and/or other materials provided with the distribution.
     18  1.4.2.2  jym  *
     19  1.4.2.2  jym  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.4.2.2  jym  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.4.2.2  jym  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.4.2.2  jym  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.4.2.2  jym  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.4.2.2  jym  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.4.2.2  jym  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.4.2.2  jym  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.4.2.2  jym  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.4.2.2  jym  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.4.2.2  jym  * POSSIBILITY OF SUCH DAMAGE.
     30  1.4.2.2  jym  */
     31  1.4.2.2  jym 
     32  1.4.2.2  jym #include <sys/cdefs.h>			/* RCS ID macro */
     33  1.4.2.2  jym 
     34  1.4.2.2  jym /*
     35  1.4.2.2  jym  * Based on: x86/ipi.c
     36  1.4.2.2  jym  * __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.4.2.2 2011/08/27 15:59:49 jym Exp $");
     37  1.4.2.2  jym  */
     38  1.4.2.2  jym 
     39  1.4.2.2  jym __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.4.2.2 2011/08/27 15:59:49 jym Exp $");
     40  1.4.2.2  jym 
     41  1.4.2.2  jym #include <sys/types.h>
     42  1.4.2.2  jym 
     43  1.4.2.2  jym #include <sys/atomic.h>
     44  1.4.2.2  jym #include <sys/mutex.h>
     45  1.4.2.2  jym #include <sys/cpu.h>
     46  1.4.2.2  jym #include <sys/device.h>
     47  1.4.2.2  jym #include <sys/xcall.h>
     48  1.4.2.2  jym #include <sys/errno.h>
     49  1.4.2.2  jym #include <sys/systm.h>
     50  1.4.2.2  jym 
     51  1.4.2.2  jym #include <machine/cpu.h>
     52  1.4.2.2  jym #ifdef __x86_64__
     53  1.4.2.2  jym #include <machine/fpu.h>
     54  1.4.2.2  jym #else
     55  1.4.2.2  jym #include <machine/npx.h>
     56  1.4.2.2  jym #endif /* __x86_64__ */
     57  1.4.2.2  jym #include <machine/frame.h>
     58  1.4.2.2  jym #include <machine/segments.h>
     59  1.4.2.2  jym 
     60  1.4.2.2  jym #include <xen/intr.h>
     61  1.4.2.2  jym #include <xen/intrdefs.h>
     62  1.4.2.2  jym #include <xen/hypervisor.h>
     63  1.4.2.2  jym #include <xen/xen3-public/vcpu.h>
     64  1.4.2.2  jym 
     65  1.4.2.2  jym #ifdef __x86_64__
     66  1.4.2.2  jym extern void ddb_ipi(struct trapframe);
     67  1.4.2.2  jym #else
     68  1.4.2.2  jym extern void ddb_ipi(int, struct trapframe);
     69  1.4.2.2  jym #endif /* __x86_64__ */
     70  1.4.2.2  jym 
     71  1.4.2.2  jym static void xen_ipi_halt(struct cpu_info *, struct intrframe *);
     72  1.4.2.2  jym static void xen_ipi_synch_fpu(struct cpu_info *, struct intrframe *);
     73  1.4.2.2  jym static void xen_ipi_ddb(struct cpu_info *, struct intrframe *);
     74  1.4.2.2  jym static void xen_ipi_xcall(struct cpu_info *, struct intrframe *);
     75  1.4.2.2  jym 
     76  1.4.2.2  jym static void (*ipifunc[XEN_NIPIS])(struct cpu_info *, struct intrframe *) =
     77  1.4.2.2  jym {	/* In order of priority (see: xen/include/intrdefs.h */
     78  1.4.2.2  jym 	xen_ipi_halt,
     79  1.4.2.2  jym 	xen_ipi_synch_fpu,
     80  1.4.2.2  jym 	xen_ipi_ddb,
     81  1.4.2.2  jym 	xen_ipi_xcall
     82  1.4.2.2  jym };
     83  1.4.2.2  jym 
     84  1.4.2.2  jym static void
     85  1.4.2.2  jym xen_ipi_handler(struct cpu_info *ci, struct intrframe *regs)
     86  1.4.2.2  jym {
     87  1.4.2.2  jym 	uint32_t pending;
     88  1.4.2.2  jym 	int bit;
     89  1.4.2.2  jym 
     90  1.4.2.2  jym 	pending = atomic_swap_32(&ci->ci_ipis, 0);
     91  1.4.2.2  jym 
     92  1.4.2.2  jym 	KDASSERT((pending >> XEN_NIPIS) == 0);
     93  1.4.2.2  jym 	while ((bit = ffs(pending)) != 0) {
     94  1.4.2.2  jym 		bit--;
     95  1.4.2.2  jym 		pending &= ~(1 << bit);
     96  1.4.2.2  jym 		ci->ci_ipi_events[bit].ev_count++;
     97  1.4.2.2  jym 		if (ipifunc[bit] != NULL) {
     98  1.4.2.2  jym 			(*ipifunc[bit])(ci, regs);
     99  1.4.2.2  jym 		} else {
    100  1.4.2.2  jym 			panic("ipifunc[%d] unsupported!\n", bit);
    101  1.4.2.2  jym 			/* NOTREACHED */
    102  1.4.2.2  jym 		}
    103  1.4.2.2  jym 	}
    104  1.4.2.2  jym }
    105  1.4.2.2  jym 
    106  1.4.2.2  jym /* Must be called once for every cpu that expects to send/recv ipis */
    107  1.4.2.2  jym void
    108  1.4.2.2  jym xen_ipi_init(void)
    109  1.4.2.2  jym {
    110  1.4.2.2  jym 	cpuid_t vcpu;
    111  1.4.2.2  jym 	evtchn_port_t evtchn;
    112  1.4.2.2  jym 	struct cpu_info *ci;
    113  1.4.2.2  jym 
    114  1.4.2.2  jym 	ci = curcpu();
    115  1.4.2.2  jym 
    116  1.4.2.2  jym 	vcpu = ci->ci_cpuid;
    117  1.4.2.2  jym 	KASSERT(vcpu < MAX_VIRT_CPUS);
    118  1.4.2.2  jym 
    119  1.4.2.2  jym 	evtchn = bind_vcpu_to_evtch(vcpu);
    120  1.4.2.2  jym 	ci->ci_ipi_evtchn = evtchn;
    121  1.4.2.2  jym 
    122  1.4.2.2  jym 	KASSERT(evtchn != -1 && evtchn < NR_EVENT_CHANNELS);
    123  1.4.2.2  jym 
    124  1.4.2.2  jym 	if (0 != event_set_handler(evtchn, (int (*)(void *))xen_ipi_handler,
    125  1.4.2.2  jym 				   ci, IPL_HIGH, "ipi")) {
    126  1.4.2.2  jym 		panic("event_set_handler(...) KPI violation\n");
    127  1.4.2.2  jym 		/* NOTREACHED */
    128  1.4.2.2  jym 	}
    129  1.4.2.2  jym 
    130  1.4.2.2  jym 	hypervisor_enable_event(evtchn);
    131  1.4.2.2  jym }
    132  1.4.2.2  jym 
    133  1.4.2.2  jym /* prefer this to global variable */
    134  1.4.2.2  jym static inline u_int max_cpus(void)
    135  1.4.2.2  jym {
    136  1.4.2.2  jym 	return maxcpus;
    137  1.4.2.2  jym }
    138  1.4.2.2  jym 
    139  1.4.2.2  jym static inline bool /* helper */
    140  1.4.2.2  jym valid_ipimask(uint32_t ipimask)
    141  1.4.2.2  jym {
    142  1.4.2.2  jym 	uint32_t masks =  XEN_IPI_XCALL | XEN_IPI_DDB |
    143  1.4.2.2  jym 		XEN_IPI_SYNCH_FPU | XEN_IPI_HALT |
    144  1.4.2.2  jym 		XEN_IPI_KICK;
    145  1.4.2.2  jym 
    146  1.4.2.2  jym 	if (ipimask & ~masks) {
    147  1.4.2.2  jym 		return false;
    148  1.4.2.2  jym 	} else {
    149  1.4.2.2  jym 		return true;
    150  1.4.2.2  jym 	}
    151  1.4.2.2  jym 
    152  1.4.2.2  jym }
    153  1.4.2.2  jym 
    154  1.4.2.2  jym int
    155  1.4.2.2  jym xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
    156  1.4.2.2  jym {
    157  1.4.2.2  jym 	evtchn_port_t evtchn;
    158  1.4.2.2  jym 
    159  1.4.2.2  jym 	KASSERT(ci != NULL || ci != curcpu());
    160  1.4.2.2  jym 
    161  1.4.2.2  jym 	if ((ci->ci_flags & CPUF_RUNNING) == 0) {
    162  1.4.2.2  jym 		return ENOENT;
    163  1.4.2.2  jym 	}
    164  1.4.2.2  jym 
    165  1.4.2.2  jym 	evtchn = ci->ci_ipi_evtchn;
    166  1.4.2.2  jym 
    167  1.4.2.2  jym 	KASSERTMSG(valid_ipimask(ipimask) == true,
    168  1.4.2.2  jym 		("xen_send_ipi() called with invalid ipimask\n"));
    169  1.4.2.2  jym 
    170  1.4.2.2  jym 	atomic_or_32(&ci->ci_ipis, ipimask);
    171  1.4.2.2  jym 	hypervisor_notify_via_evtchn(evtchn);
    172  1.4.2.2  jym 
    173  1.4.2.2  jym 	return 0;
    174  1.4.2.2  jym }
    175  1.4.2.2  jym 
    176  1.4.2.2  jym void
    177  1.4.2.2  jym xen_broadcast_ipi(uint32_t ipimask)
    178  1.4.2.2  jym {
    179  1.4.2.2  jym 	struct cpu_info *ci, *self = curcpu();
    180  1.4.2.2  jym 	CPU_INFO_ITERATOR cii;
    181  1.4.2.2  jym 
    182  1.4.2.2  jym 	KASSERTMSG(valid_ipimask(ipimask) == true,
    183  1.4.2.2  jym 		("xen_broadcast_ipi() called with invalid ipimask\n"));
    184  1.4.2.2  jym 
    185  1.4.2.2  jym 	/*
    186  1.4.2.2  jym 	 * XXX-cherry: there's an implicit broadcast sending order
    187  1.4.2.2  jym 	 * which I dislike. Randomise this ? :-)
    188  1.4.2.2  jym 	 */
    189  1.4.2.2  jym 
    190  1.4.2.2  jym 	for (CPU_INFO_FOREACH(cii, ci)) {
    191  1.4.2.2  jym 		if (ci == NULL)
    192  1.4.2.2  jym 			continue;
    193  1.4.2.2  jym 		if (ci == self)
    194  1.4.2.2  jym 			continue;
    195  1.4.2.2  jym 		if (ci->ci_data.cpu_idlelwp == NULL)
    196  1.4.2.2  jym 			continue;
    197  1.4.2.2  jym 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    198  1.4.2.2  jym 			continue;
    199  1.4.2.2  jym 		if (ci->ci_flags & (CPUF_RUNNING)) {
    200  1.4.2.2  jym 			if (0 != xen_send_ipi(ci, ipimask)) {
    201  1.4.2.2  jym 				panic("xen_ipi of %x from %s to %s failed\n",
    202  1.4.2.2  jym 				      ipimask, cpu_name(curcpu()),
    203  1.4.2.2  jym 				      cpu_name(ci));
    204  1.4.2.2  jym 			}
    205  1.4.2.2  jym 		}
    206  1.4.2.2  jym 	}
    207  1.4.2.2  jym }
    208  1.4.2.2  jym 
    209  1.4.2.2  jym /* MD wrapper for the xcall(9) callback. */
    210  1.4.2.2  jym #define PRIuCPUID	"lu" /* XXX: move this somewhere more appropriate */
    211  1.4.2.2  jym 
    212  1.4.2.2  jym static void
    213  1.4.2.2  jym xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
    214  1.4.2.2  jym {
    215  1.4.2.2  jym 	KASSERT(ci == curcpu());
    216  1.4.2.2  jym 	KASSERT(ci != NULL);
    217  1.4.2.2  jym 	if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL)) {
    218  1.4.2.2  jym 		panic("vcpu%" PRIuCPUID "shutdown failed.\n", ci->ci_cpuid);
    219  1.4.2.2  jym 	}
    220  1.4.2.2  jym 
    221  1.4.2.2  jym }
    222  1.4.2.2  jym 
    223  1.4.2.2  jym static void
    224  1.4.2.2  jym xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
    225  1.4.2.2  jym {
    226  1.4.2.2  jym 	KASSERT(ci != NULL);
    227  1.4.2.2  jym 	KASSERT(intrf != NULL);
    228  1.4.2.2  jym 
    229  1.4.2.2  jym #ifdef __x86_64__
    230  1.4.2.2  jym 	fpusave_cpu(true);
    231  1.4.2.2  jym #else
    232  1.4.2.2  jym 	npxsave_cpu(true);
    233  1.4.2.2  jym #endif /* __x86_64__ */
    234  1.4.2.2  jym }
    235  1.4.2.2  jym 
    236  1.4.2.2  jym static void
    237  1.4.2.2  jym xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
    238  1.4.2.2  jym {
    239  1.4.2.2  jym 	KASSERT(ci != NULL);
    240  1.4.2.2  jym 	KASSERT(intrf != NULL);
    241  1.4.2.2  jym 
    242  1.4.2.2  jym #ifdef __x86_64__
    243  1.4.2.2  jym 	ddb_ipi(intrf->if_tf);
    244  1.4.2.2  jym #else
    245  1.4.2.2  jym 	struct trapframe tf;
    246  1.4.2.2  jym 	tf.tf_gs = intrf->if_gs;
    247  1.4.2.2  jym 	tf.tf_fs = intrf->if_fs;
    248  1.4.2.2  jym 	tf.tf_es = intrf->if_es;
    249  1.4.2.2  jym 	tf.tf_ds = intrf->if_ds;
    250  1.4.2.2  jym 	tf.tf_edi = intrf->if_edi;
    251  1.4.2.2  jym 	tf.tf_esi = intrf->if_esi;
    252  1.4.2.2  jym 	tf.tf_ebp = intrf->if_ebp;
    253  1.4.2.2  jym 	tf.tf_ebx = intrf->if_ebx;
    254  1.4.2.2  jym 	tf.tf_ecx = intrf->if_ecx;
    255  1.4.2.2  jym 	tf.tf_eax = intrf->if_eax;
    256  1.4.2.2  jym 	tf.tf_trapno = intrf->__if_trapno;
    257  1.4.2.2  jym 	tf.tf_err = intrf->__if_err;
    258  1.4.2.2  jym 	tf.tf_eip = intrf->if_eip;
    259  1.4.2.2  jym 	tf.tf_cs = intrf->if_cs;
    260  1.4.2.2  jym 	tf.tf_eflags = intrf->if_eflags;
    261  1.4.2.2  jym 	tf.tf_esp = intrf->if_esp;
    262  1.4.2.2  jym 	tf.tf_ss = intrf->if_ss;
    263  1.4.2.2  jym 
    264  1.4.2.2  jym 	/* XXX: does i386/Xen have vm86 support ?
    265  1.4.2.2  jym 	tf.tf_vm86_es;
    266  1.4.2.2  jym 	tf.tf_vm86_ds;
    267  1.4.2.2  jym 	tf.tf_vm86_fs;
    268  1.4.2.2  jym 	tf.tf_vm86_gs;
    269  1.4.2.2  jym 	   :XXX */
    270  1.4.2.2  jym 
    271  1.4.2.2  jym 	ddb_ipi(SEL_KPL, tf);
    272  1.4.2.2  jym #endif
    273  1.4.2.2  jym }
    274  1.4.2.2  jym 
    275  1.4.2.2  jym static void
    276  1.4.2.2  jym xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
    277  1.4.2.2  jym {
    278  1.4.2.2  jym 	KASSERT(ci != NULL);
    279  1.4.2.2  jym 	KASSERT(intrf != NULL);
    280  1.4.2.2  jym 
    281  1.4.2.2  jym 	xc_ipi_handler();
    282  1.4.2.2  jym }
    283  1.4.2.2  jym 
    284  1.4.2.2  jym void
    285  1.4.2.2  jym xc_send_ipi(struct cpu_info *ci)
    286  1.4.2.2  jym {
    287  1.4.2.2  jym 
    288  1.4.2.2  jym 	KASSERT(kpreempt_disabled());
    289  1.4.2.2  jym 	KASSERT(curcpu() != ci);
    290  1.4.2.2  jym 	printf("xc_send_ipi called \n");
    291  1.4.2.2  jym 	if (ci) {
    292  1.4.2.2  jym 		if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
    293  1.4.2.2  jym 			panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
    294  1.4.2.2  jym 		}
    295  1.4.2.2  jym 	} else {
    296  1.4.2.2  jym 		xen_broadcast_ipi(XEN_IPI_XCALL);
    297  1.4.2.2  jym 	}
    298  1.4.2.2  jym }
    299