Home | History | Annotate | Line # | Download | only in x86
xen_ipi.c revision 1.24
      1 /* $NetBSD: xen_ipi.c,v 1.24 2018/06/23 15:53:14 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.24 2018/06/23 15:53:14 jdolecek Exp $");
     37  */
     38 
     39 __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.24 2018/06/23 15:53:14 jdolecek Exp $");
     40 
     41 #include "opt_ddb.h"
     42 
     43 #include <sys/types.h>
     44 
     45 #include <sys/atomic.h>
     46 #include <sys/cpu.h>
     47 #include <sys/mutex.h>
     48 #include <sys/device.h>
     49 #include <sys/xcall.h>
     50 #include <sys/ipi.h>
     51 #include <sys/errno.h>
     52 #include <sys/systm.h>
     53 
     54 #include <x86/fpu.h>
     55 #include <machine/frame.h>
     56 #include <machine/segments.h>
     57 
     58 #include <xen/evtchn.h>
     59 #include <xen/intr.h>
     60 #include <xen/intrdefs.h>
     61 #include <xen/hypervisor.h>
     62 #include <xen/xen-public/vcpu.h>
     63 
     64 #ifdef DDB
     65 extern void ddb_ipi(struct trapframe);
     66 static void xen_ipi_ddb(struct cpu_info *, struct intrframe *);
     67 #endif
     68 
     69 static void xen_ipi_halt(struct cpu_info *, struct intrframe *);
     70 static void xen_ipi_synch_fpu(struct cpu_info *, struct intrframe *);
     71 static void xen_ipi_xcall(struct cpu_info *, struct intrframe *);
     72 static void xen_ipi_hvcb(struct cpu_info *, struct intrframe *);
     73 static void xen_ipi_generic(struct cpu_info *, struct intrframe *);
     74 
     75 static void (*ipifunc[XEN_NIPIS])(struct cpu_info *, struct intrframe *) =
     76 {	/* In order of priority (see: xen/include/intrdefs.h */
     77 	xen_ipi_halt,
     78 	xen_ipi_synch_fpu,
     79 #ifdef DDB
     80 	xen_ipi_ddb,
     81 #else
     82 	NULL,
     83 #endif
     84 	xen_ipi_xcall,
     85 	xen_ipi_hvcb,
     86 	xen_ipi_generic,
     87 };
     88 
     89 static int
     90 xen_ipi_handler(void *arg)
     91 {
     92 	uint32_t pending;
     93 	int bit;
     94 	struct cpu_info *ci;
     95 	struct intrframe *regs;
     96 
     97 	ci = curcpu();
     98 	regs = arg;
     99 
    100 	pending = atomic_swap_32(&ci->ci_ipis, 0);
    101 
    102 	KDASSERT((pending >> XEN_NIPIS) == 0);
    103 	while ((bit = ffs(pending)) != 0) {
    104 		bit--;
    105 		pending &= ~(1 << bit);
    106 		ci->ci_ipi_events[bit].ev_count++;
    107 		if (ipifunc[bit] != NULL) {
    108 			(*ipifunc[bit])(ci, regs);
    109 		} else {
    110 			panic("ipifunc[%d] unsupported!\n", bit);
    111 			/* NOTREACHED */
    112 		}
    113 	}
    114 
    115 	return 0;
    116 }
    117 
    118 /* Must be called once for every cpu that expects to send/recv ipis */
    119 void
    120 xen_ipi_init(void)
    121 {
    122 	cpuid_t vcpu;
    123 	evtchn_port_t evtchn;
    124 	struct cpu_info *ci;
    125 
    126 	ci = curcpu();
    127 
    128 	vcpu = ci->ci_cpuid;
    129 	KASSERT(vcpu < XEN_LEGACY_MAX_VCPUS);
    130 
    131 	evtchn = bind_vcpu_to_evtch(vcpu);
    132 	ci->ci_ipi_evtchn = evtchn;
    133 
    134 	KASSERT(evtchn != -1 && evtchn < NR_EVENT_CHANNELS);
    135 
    136 	if(intr_establish_xname(0, &xen_pic, evtchn, IST_LEVEL, IPL_HIGH,
    137 		xen_ipi_handler, ci, true, "ipi") == NULL) {
    138 		panic("%s: unable to register ipi handler\n", __func__);
    139 		/* NOTREACHED */
    140 	}
    141 
    142 	hypervisor_enable_event(evtchn);
    143 }
    144 
    145 #ifdef DIAGNOSTIC
    146 static inline bool /* helper */
    147 valid_ipimask(uint32_t ipimask)
    148 {
    149 	uint32_t masks = XEN_IPI_GENERIC | XEN_IPI_HVCB | XEN_IPI_XCALL |
    150 		 XEN_IPI_DDB | XEN_IPI_SYNCH_FPU |
    151 		 XEN_IPI_HALT | XEN_IPI_KICK;
    152 
    153 	if (ipimask & ~masks) {
    154 		return false;
    155 	} else {
    156 		return true;
    157 	}
    158 
    159 }
    160 #endif
    161 
    162 int
    163 xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
    164 {
    165 	evtchn_port_t evtchn;
    166 
    167 	KASSERT(ci != NULL || ci != curcpu());
    168 
    169 	if ((ci->ci_flags & CPUF_RUNNING) == 0) {
    170 		return ENOENT;
    171 	}
    172 
    173 	evtchn = ci->ci_ipi_evtchn;
    174 
    175 	KASSERTMSG(valid_ipimask(ipimask) == true,
    176 		"xen_send_ipi() called with invalid ipimask\n");
    177 
    178 	atomic_or_32(&ci->ci_ipis, ipimask);
    179 	hypervisor_notify_via_evtchn(evtchn);
    180 
    181 	return 0;
    182 }
    183 
    184 void
    185 xen_broadcast_ipi(uint32_t ipimask)
    186 {
    187 	struct cpu_info *ci, *self = curcpu();
    188 	CPU_INFO_ITERATOR cii;
    189 
    190 	KASSERTMSG(valid_ipimask(ipimask) == true,
    191 		"xen_broadcast_ipi() called with invalid ipimask\n");
    192 
    193 	/*
    194 	 * XXX-cherry: there's an implicit broadcast sending order
    195 	 * which I dislike. Randomise this ? :-)
    196 	 */
    197 
    198 	for (CPU_INFO_FOREACH(cii, ci)) {
    199 		if (ci == NULL)
    200 			continue;
    201 		if (ci == self)
    202 			continue;
    203 		if (ci->ci_data.cpu_idlelwp == NULL)
    204 			continue;
    205 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    206 			continue;
    207 		if (ci->ci_flags & (CPUF_RUNNING)) {
    208 			if (0 != xen_send_ipi(ci, ipimask)) {
    209 				panic("xen_ipi of %x from %s to %s failed\n",
    210 				      ipimask, cpu_name(curcpu()),
    211 				      cpu_name(ci));
    212 			}
    213 		}
    214 	}
    215 }
    216 
    217 /* MD wrapper for the xcall(9) callback. */
    218 #define PRIuCPUID	"lu" /* XXX: move this somewhere more appropriate */
    219 
    220 static void
    221 xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
    222 {
    223 	KASSERT(ci == curcpu());
    224 	KASSERT(ci != NULL);
    225 	if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL)) {
    226 		panic("vcpu%" PRIuCPUID "shutdown failed.\n", ci->ci_cpuid);
    227 	}
    228 
    229 }
    230 
    231 static void
    232 xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
    233 {
    234 	KASSERT(ci != NULL);
    235 	KASSERT(intrf != NULL);
    236 
    237 	fpusave_cpu(true);
    238 }
    239 
    240 #ifdef DDB
    241 static void
    242 xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
    243 {
    244 	KASSERT(ci != NULL);
    245 	KASSERT(intrf != NULL);
    246 
    247 #ifdef __x86_64__
    248 	ddb_ipi(intrf->if_tf);
    249 #else
    250 	struct trapframe tf;
    251 	tf.tf_gs = intrf->if_gs;
    252 	tf.tf_fs = intrf->if_fs;
    253 	tf.tf_es = intrf->if_es;
    254 	tf.tf_ds = intrf->if_ds;
    255 	tf.tf_edi = intrf->if_edi;
    256 	tf.tf_esi = intrf->if_esi;
    257 	tf.tf_ebp = intrf->if_ebp;
    258 	tf.tf_ebx = intrf->if_ebx;
    259 	tf.tf_ecx = intrf->if_ecx;
    260 	tf.tf_eax = intrf->if_eax;
    261 	tf.tf_trapno = intrf->__if_trapno;
    262 	tf.tf_err = intrf->__if_err;
    263 	tf.tf_eip = intrf->if_eip;
    264 	tf.tf_cs = intrf->if_cs;
    265 	tf.tf_eflags = intrf->if_eflags;
    266 	tf.tf_esp = intrf->if_esp;
    267 	tf.tf_ss = intrf->if_ss;
    268 
    269 	ddb_ipi(tf);
    270 #endif
    271 }
    272 #endif /* DDB */
    273 
    274 static void
    275 xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
    276 {
    277 	KASSERT(ci != NULL);
    278 	KASSERT(intrf != NULL);
    279 
    280 	xc_ipi_handler();
    281 }
    282 
    283 void
    284 xc_send_ipi(struct cpu_info *ci)
    285 {
    286 
    287 	KASSERT(kpreempt_disabled());
    288 	KASSERT(curcpu() != ci);
    289 	if (ci) {
    290 		if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
    291 			panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
    292 		}
    293 	} else {
    294 		xen_broadcast_ipi(XEN_IPI_XCALL);
    295 	}
    296 }
    297 
    298 static void
    299 xen_ipi_generic(struct cpu_info *ci, struct intrframe *intrf)
    300 {
    301 	KASSERT(ci != NULL);
    302 	KASSERT(intrf != NULL);
    303 
    304 	ipi_cpu_handler();
    305 }
    306 
    307 void
    308 cpu_ipi(struct cpu_info *ci)
    309 {
    310 	KASSERT(kpreempt_disabled());
    311 	KASSERT(curcpu() != ci);
    312 	if (ci) {
    313 		if (0 != xen_send_ipi(ci, XEN_IPI_GENERIC)) {
    314 			panic("xen_send_ipi(XEN_IPI_GENERIC) failed\n");
    315 		}
    316 	} else {
    317 		xen_broadcast_ipi(XEN_IPI_GENERIC);
    318 	}
    319 }
    320 
    321 static void
    322 xen_ipi_hvcb(struct cpu_info *ci, struct intrframe *intrf)
    323 {
    324 	KASSERT(ci != NULL);
    325 	KASSERT(intrf != NULL);
    326 	KASSERT(ci == curcpu());
    327 	KASSERT(!ci->ci_vcpu->evtchn_upcall_mask);
    328 
    329 	hypervisor_force_callback();
    330 }
    331