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