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