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