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