xref: /illumos-gate/usr/src/uts/i86pc/os/x_call.c (revision 3d7072f8bd27709dba14f6fe336f149d25d9e207)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Facilities for cross-processor subroutine calls using "mailbox" interrupts.
30  *
31  */
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/t_lock.h>
36 #include <sys/thread.h>
37 #include <sys/cpuvar.h>
38 #include <sys/x_call.h>
39 #include <sys/cpu.h>
40 #include <sys/psw.h>
41 #include <sys/sunddi.h>
42 #include <sys/debug.h>
43 #include <sys/systm.h>
44 #include <sys/archsystm.h>
45 #include <sys/machsystm.h>
46 #include <sys/mutex_impl.h>
47 #include <sys/traptrace.h>
48 
49 
50 static struct	xc_mbox xc_mboxes[X_CALL_LEVELS];
51 static kmutex_t xc_mbox_lock[X_CALL_LEVELS];
52 static uint_t 	xc_xlat_xcptoipl[X_CALL_LEVELS] = {
53 	XC_LO_PIL,
54 	XC_MED_PIL,
55 	XC_HI_PIL
56 };
57 
58 static void xc_common(xc_func_t, xc_arg_t, xc_arg_t, xc_arg_t,
59     int, cpuset_t, int);
60 
61 static int	xc_initialized = 0;
62 
63 void
64 xc_init()
65 {
66 	/*
67 	 * By making these mutexes type MUTEX_DRIVER, the ones below
68 	 * LOCK_LEVEL will be implemented as adaptive mutexes, and the
69 	 * ones above LOCK_LEVEL will be spin mutexes.
70 	 */
71 	mutex_init(&xc_mbox_lock[0], NULL, MUTEX_DRIVER,
72 	    (void *)ipltospl(XC_LO_PIL));
73 	mutex_init(&xc_mbox_lock[1], NULL, MUTEX_DRIVER,
74 	    (void *)ipltospl(XC_MED_PIL));
75 	mutex_init(&xc_mbox_lock[2], NULL, MUTEX_DRIVER,
76 	    (void *)ipltospl(XC_HI_PIL));
77 
78 	xc_initialized = 1;
79 }
80 
81 #if defined(TRAPTRACE)
82 
83 /*
84  * When xc_traptrace is on, put x-call records into the trap trace buffer.
85  */
86 int xc_traptrace;
87 
88 void
89 xc_make_trap_trace_entry(uint8_t marker, int pri, ulong_t arg)
90 {
91 	trap_trace_rec_t *ttr;
92 	struct _xc_entry *xce;
93 
94 	if (xc_traptrace == 0)
95 		return;
96 
97 	ttr = trap_trace_get_traceptr(TT_XCALL,
98 	    (ulong_t)caller(), (ulong_t)getfp());
99 	xce = &(ttr->ttr_info.xc_entry);
100 
101 	xce->xce_marker = marker;
102 	xce->xce_pri = pri;
103 	xce->xce_arg = arg;
104 
105 	if ((uint_t)pri < X_CALL_LEVELS) {
106 		struct machcpu *mcpu = &CPU->cpu_m;
107 
108 		xce->xce_pend = mcpu->xc_pend[pri];
109 		xce->xce_ack = mcpu->xc_ack[pri];
110 		xce->xce_state = mcpu->xc_state[pri];
111 		xce->xce_retval = mcpu->xc_retval[pri];
112 		xce->xce_func = (uintptr_t)xc_mboxes[pri].func;
113 	}
114 }
115 #endif
116 
117 #define	CAPTURE_CPU_ARG	~0UL
118 
119 /*
120  * X-call interrupt service routine.
121  *
122  * arg == X_CALL_MEDPRI	-  capture cpus.
123  *
124  * We're protected against changing CPUs by being a high-priority interrupt.
125  */
126 /*ARGSUSED*/
127 uint_t
128 xc_serv(caddr_t arg1, caddr_t arg2)
129 {
130 	int op;
131 	int pri = (int)(uintptr_t)arg1;
132 	struct cpu *cpup = CPU;
133 	xc_arg_t arg2val;
134 
135 	XC_TRACE(TT_XC_SVC_BEGIN, pri, (ulong_t)arg2);
136 
137 	if (pri == X_CALL_MEDPRI) {
138 
139 		arg2val = xc_mboxes[X_CALL_MEDPRI].arg2;
140 
141 		if (arg2val != CAPTURE_CPU_ARG &&
142 		    !CPU_IN_SET((cpuset_t)arg2val, cpup->cpu_id))
143 			goto unclaimed;
144 
145 		ASSERT(arg2val == CAPTURE_CPU_ARG);
146 
147 		if (cpup->cpu_m.xc_pend[pri] == 0)
148 			goto unclaimed;
149 
150 		cpup->cpu_m.xc_pend[X_CALL_MEDPRI] = 0;
151 		cpup->cpu_m.xc_ack[X_CALL_MEDPRI] = 1;
152 
153 		for (;;) {
154 			if ((cpup->cpu_m.xc_state[X_CALL_MEDPRI] == XC_DONE) ||
155 			    (cpup->cpu_m.xc_pend[X_CALL_MEDPRI]))
156 				break;
157 			SMT_PAUSE();
158 		}
159 		XC_TRACE(TT_XC_SVC_END, pri, DDI_INTR_CLAIMED);
160 		return (DDI_INTR_CLAIMED);
161 	}
162 
163 	if (cpup->cpu_m.xc_pend[pri] == 0)
164 		goto unclaimed;
165 
166 	cpup->cpu_m.xc_pend[pri] = 0;
167 	op = cpup->cpu_m.xc_state[pri];
168 
169 	/*
170 	 * Don't invoke a null function.
171 	 */
172 	if (xc_mboxes[pri].func != NULL) {
173 		cpup->cpu_m.xc_retval[pri] =
174 		    (*xc_mboxes[pri].func)(xc_mboxes[pri].arg1,
175 		    xc_mboxes[pri].arg2, xc_mboxes[pri].arg3);
176 	} else
177 		cpup->cpu_m.xc_retval[pri] = 0;
178 
179 	/*
180 	 * Acknowledge that we have completed the x-call operation.
181 	 */
182 	cpup->cpu_m.xc_ack[pri] = 1;
183 
184 	if (op != XC_CALL_OP) {
185 		/*
186 		 * for (op == XC_SYNC_OP)
187 		 * Wait for the initiator of the x-call to indicate
188 		 * that all CPUs involved can proceed.
189 		 */
190 		while (cpup->cpu_m.xc_wait[pri])
191 			SMT_PAUSE();
192 
193 		while (cpup->cpu_m.xc_state[pri] != XC_DONE)
194 			SMT_PAUSE();
195 
196 		/*
197 		 * Acknowledge that we have received the directive to continue.
198 		 */
199 		ASSERT(cpup->cpu_m.xc_ack[pri] == 0);
200 		cpup->cpu_m.xc_ack[pri] = 1;
201 	}
202 
203 	XC_TRACE(TT_XC_SVC_END, pri, DDI_INTR_CLAIMED);
204 	return (DDI_INTR_CLAIMED);
205 
206 unclaimed:
207 	XC_TRACE(TT_XC_SVC_END, pri, DDI_INTR_UNCLAIMED);
208 	return (DDI_INTR_UNCLAIMED);
209 }
210 
211 
212 /*
213  * xc_do_call:
214  */
215 static void
216 xc_do_call(
217 	xc_arg_t arg1,
218 	xc_arg_t arg2,
219 	xc_arg_t arg3,
220 	int pri,
221 	cpuset_t set,
222 	xc_func_t func,
223 	int sync)
224 {
225 	/*
226 	 * If the pri indicates a low priority lock (below LOCK_LEVEL),
227 	 * we must disable preemption to avoid migrating to another CPU
228 	 * during the call.
229 	 */
230 	if (pri == X_CALL_LOPRI) {
231 		kpreempt_disable();
232 	} else {
233 		pri = X_CALL_HIPRI;
234 	}
235 
236 	/* always grab highest mutex to avoid deadlock */
237 	mutex_enter(&xc_mbox_lock[X_CALL_HIPRI]);
238 	xc_common(func, arg1, arg2, arg3, pri, set, sync);
239 	mutex_exit(&xc_mbox_lock[X_CALL_HIPRI]);
240 	if (pri == X_CALL_LOPRI)
241 		kpreempt_enable();
242 }
243 
244 
245 /*
246  * xc_call: call specified function on all processors
247  * remotes may continue after service
248  * we wait here until everybody has completed.
249  */
250 void
251 xc_call(
252 	xc_arg_t arg1,
253 	xc_arg_t arg2,
254 	xc_arg_t arg3,
255 	int pri,
256 	cpuset_t set,
257 	xc_func_t func)
258 {
259 	xc_do_call(arg1, arg2, arg3, pri, set, func, 0);
260 }
261 
262 /*
263  * xc_sync: call specified function on all processors
264  * after doing work, each remote waits until we let
265  * it continue; send the contiunue after everyone has
266  * informed us that they are done.
267  */
268 void
269 xc_sync(
270 	xc_arg_t arg1,
271 	xc_arg_t arg2,
272 	xc_arg_t arg3,
273 	int pri,
274 	cpuset_t set,
275 	xc_func_t func)
276 {
277 	xc_do_call(arg1, arg2, arg3, pri, set, func, 1);
278 }
279 
280 /*
281  * The routines xc_capture_cpus and xc_release_cpus
282  * can be used in place of xc_sync in order to implement a critical
283  * code section where all CPUs in the system can be controlled.
284  * xc_capture_cpus is used to start the critical code section, and
285  * xc_release_cpus is used to end the critical code section.
286  */
287 
288 /*
289  * Capture the CPUs specified in order to start a x-call session,
290  * and/or to begin a critical section.
291  */
292 void
293 xc_capture_cpus(cpuset_t set)
294 {
295 	int cix;
296 	int lcx;
297 	struct cpu *cpup;
298 	int	i;
299 	cpuset_t *cpus;
300 	cpuset_t c;
301 
302 	CPU_STATS_ADDQ(CPU, sys, xcalls, 1);
303 
304 	/*
305 	 * Prevent deadlocks where we take an interrupt and are waiting
306 	 * for a mutex owned by one of the CPUs that is captured for
307 	 * the x-call, while that CPU is waiting for some x-call signal
308 	 * to be set by us.
309 	 *
310 	 * This mutex also prevents preemption, since it raises SPL above
311 	 * LOCK_LEVEL (it is a spin-type driver mutex).
312 	 */
313 	/* always grab highest mutex to avoid deadlock */
314 	mutex_enter(&xc_mbox_lock[X_CALL_HIPRI]);
315 	lcx = CPU->cpu_id;	/* now we're safe */
316 
317 	ASSERT(CPU->cpu_flags & CPU_READY);
318 
319 	/*
320 	 * Wait for all cpus
321 	 */
322 	cpus = (cpuset_t *)&xc_mboxes[X_CALL_MEDPRI].arg2;
323 	if (CPU_IN_SET(*cpus, CPU->cpu_id))
324 		CPUSET_ATOMIC_DEL(*cpus, CPU->cpu_id);
325 	for (;;) {
326 		c = *(volatile cpuset_t *)cpus;
327 		CPUSET_AND(c, cpu_ready_set);
328 		if (CPUSET_ISNULL(c))
329 			break;
330 		SMT_PAUSE();
331 	}
332 
333 	/*
334 	 * Store the set of CPUs involved in the x-call session, so that
335 	 * xc_release_cpus will know what CPUs to act upon.
336 	 */
337 	xc_mboxes[X_CALL_MEDPRI].set = set;
338 	xc_mboxes[X_CALL_MEDPRI].arg2 = CAPTURE_CPU_ARG;
339 
340 	/*
341 	 * Now capture each CPU in the set and cause it to go into a
342 	 * holding pattern.
343 	 */
344 	i = 0;
345 	for (cix = 0; cix < NCPU; cix++) {
346 		if ((cpup = cpu[cix]) == NULL ||
347 		    (cpup->cpu_flags & CPU_READY) == 0) {
348 			/*
349 			 * In case CPU wasn't ready, but becomes ready later,
350 			 * take the CPU out of the set now.
351 			 */
352 			CPUSET_DEL(set, cix);
353 			continue;
354 		}
355 		if (cix != lcx && CPU_IN_SET(set, cix)) {
356 			cpup->cpu_m.xc_ack[X_CALL_MEDPRI] = 0;
357 			cpup->cpu_m.xc_state[X_CALL_MEDPRI] = XC_HOLD;
358 			cpup->cpu_m.xc_pend[X_CALL_MEDPRI] = 1;
359 			XC_TRACE(TT_XC_CAPTURE, X_CALL_MEDPRI, cix);
360 			send_dirint(cix, XC_MED_PIL);
361 		}
362 		i++;
363 		if (i >= ncpus)
364 			break;
365 	}
366 
367 	/*
368 	 * Wait here until all remote calls to acknowledge.
369 	 */
370 	i = 0;
371 	for (cix = 0; cix < NCPU; cix++) {
372 		if (lcx != cix && CPU_IN_SET(set, cix)) {
373 			cpup = cpu[cix];
374 			while (cpup->cpu_m.xc_ack[X_CALL_MEDPRI] == 0)
375 				SMT_PAUSE();
376 			cpup->cpu_m.xc_ack[X_CALL_MEDPRI] = 0;
377 		}
378 		i++;
379 		if (i >= ncpus)
380 			break;
381 	}
382 
383 }
384 
385 /*
386  * Release the CPUs captured by xc_capture_cpus, thus terminating the
387  * x-call session and exiting the critical section.
388  */
389 void
390 xc_release_cpus(void)
391 {
392 	int cix;
393 	int lcx = (int)(CPU->cpu_id);
394 	cpuset_t set = xc_mboxes[X_CALL_MEDPRI].set;
395 	struct cpu *cpup;
396 	int	i;
397 
398 	ASSERT(MUTEX_HELD(&xc_mbox_lock[X_CALL_HIPRI]));
399 
400 	/*
401 	 * Allow each CPU to exit its holding pattern.
402 	 */
403 	i = 0;
404 	for (cix = 0; cix < NCPU; cix++) {
405 		if ((cpup = cpu[cix]) == NULL)
406 			continue;
407 		if ((cpup->cpu_flags & CPU_READY) &&
408 		    (cix != lcx) && CPU_IN_SET(set, cix)) {
409 			/*
410 			 * Clear xc_ack since we will be waiting for it
411 			 * to be set again after we set XC_DONE.
412 			 */
413 			XC_TRACE(TT_XC_RELEASE, X_CALL_MEDPRI, cix);
414 			cpup->cpu_m.xc_state[X_CALL_MEDPRI] = XC_DONE;
415 		}
416 		i++;
417 		if (i >= ncpus)
418 			break;
419 	}
420 
421 	xc_mboxes[X_CALL_MEDPRI].arg2 = 0;
422 	mutex_exit(&xc_mbox_lock[X_CALL_HIPRI]);
423 }
424 
425 /*
426  * Common code to call a specified function on a set of processors.
427  * sync specifies what kind of waiting is done.
428  *	-1 - no waiting, don't release remotes
429  *	0 - no waiting, release remotes immediately
430  *	1 - run service locally w/o waiting for remotes.
431  */
432 static void
433 xc_common(
434 	xc_func_t func,
435 	xc_arg_t arg1,
436 	xc_arg_t arg2,
437 	xc_arg_t arg3,
438 	int pri,
439 	cpuset_t set,
440 	int sync)
441 {
442 	int cix;
443 	int lcx = (int)(CPU->cpu_id);
444 	struct cpu *cpup;
445 
446 	ASSERT(panicstr == NULL);
447 
448 	ASSERT(MUTEX_HELD(&xc_mbox_lock[X_CALL_HIPRI]));
449 	ASSERT(CPU->cpu_flags & CPU_READY);
450 
451 	/*
452 	 * Set up the service definition mailbox.
453 	 */
454 	xc_mboxes[pri].func = func;
455 	xc_mboxes[pri].arg1 = arg1;
456 	xc_mboxes[pri].arg2 = arg2;
457 	xc_mboxes[pri].arg3 = arg3;
458 
459 	/*
460 	 * Request service on all remote processors.
461 	 */
462 	for (cix = 0; cix < NCPU; cix++) {
463 		if ((cpup = cpu[cix]) == NULL ||
464 		    (cpup->cpu_flags & CPU_READY) == 0) {
465 			/*
466 			 * In case the non-local CPU is not ready but becomes
467 			 * ready later, take it out of the set now. The local
468 			 * CPU needs to remain in the set to complete the
469 			 * requested function.
470 			 */
471 			if (cix != lcx)
472 				CPUSET_DEL(set, cix);
473 		} else if (cix != lcx && CPU_IN_SET(set, cix)) {
474 			CPU_STATS_ADDQ(CPU, sys, xcalls, 1);
475 			cpup->cpu_m.xc_ack[pri] = 0;
476 			cpup->cpu_m.xc_wait[pri] = sync;
477 			if (sync > 0)
478 				cpup->cpu_m.xc_state[pri] = XC_SYNC_OP;
479 			else
480 				cpup->cpu_m.xc_state[pri] = XC_CALL_OP;
481 			cpup->cpu_m.xc_pend[pri] = 1;
482 			XC_TRACE(TT_XC_START, pri, cix);
483 			send_dirint(cix, xc_xlat_xcptoipl[pri]);
484 		}
485 	}
486 
487 	/*
488 	 * Run service locally
489 	 */
490 	if (CPU_IN_SET(set, lcx) && func != NULL) {
491 		XC_TRACE(TT_XC_START, pri, CPU->cpu_id);
492 		CPU->cpu_m.xc_retval[pri] = (*func)(arg1, arg2, arg3);
493 	}
494 
495 	if (sync == -1)
496 		return;
497 
498 	/*
499 	 * Wait here until all remote calls acknowledge.
500 	 */
501 	for (cix = 0; cix < NCPU; cix++) {
502 		if (lcx != cix && CPU_IN_SET(set, cix)) {
503 			cpup = cpu[cix];
504 			while (cpup->cpu_m.xc_ack[pri] == 0)
505 				SMT_PAUSE();
506 			XC_TRACE(TT_XC_WAIT, pri, cix);
507 			cpup->cpu_m.xc_ack[pri] = 0;
508 		}
509 	}
510 
511 	if (sync == 0)
512 		return;
513 
514 	/*
515 	 * Release any waiting CPUs
516 	 */
517 	for (cix = 0; cix < NCPU; cix++) {
518 		if (lcx != cix && CPU_IN_SET(set, cix)) {
519 			cpup = cpu[cix];
520 			if (cpup != NULL && (cpup->cpu_flags & CPU_READY)) {
521 				cpup->cpu_m.xc_wait[pri] = 0;
522 				cpup->cpu_m.xc_state[pri] = XC_DONE;
523 			}
524 		}
525 	}
526 
527 	/*
528 	 * Wait for all CPUs to acknowledge completion before we continue.
529 	 * Without this check it's possible (on a VM or hyper-threaded CPUs
530 	 * or in the presence of Service Management Interrupts which can all
531 	 * cause delays) for the remote processor to still be waiting by
532 	 * the time xc_common() is next invoked with the sync flag set
533 	 * resulting in a deadlock.
534 	 */
535 	for (cix = 0; cix < NCPU; cix++) {
536 		if (lcx != cix && CPU_IN_SET(set, cix)) {
537 			cpup = cpu[cix];
538 			if (cpup != NULL && (cpup->cpu_flags & CPU_READY)) {
539 				while (cpup->cpu_m.xc_ack[pri] == 0)
540 					SMT_PAUSE();
541 				XC_TRACE(TT_XC_ACK, pri, cix);
542 				cpup->cpu_m.xc_ack[pri] = 0;
543 			}
544 		}
545 	}
546 }
547 
548 /*
549  * xc_trycall: attempt to call specified function on all processors
550  * remotes may wait for a long time
551  * we continue immediately
552  */
553 void
554 xc_trycall(
555 	xc_arg_t arg1,
556 	xc_arg_t arg2,
557 	xc_arg_t arg3,
558 	cpuset_t set,
559 	xc_func_t func)
560 {
561 	int		save_kernel_preemption;
562 	extern int	IGNORE_KERNEL_PREEMPTION;
563 
564 	/*
565 	 * If we can grab the mutex, we'll do the cross-call.  If not -- if
566 	 * someone else is already doing a cross-call -- we won't.
567 	 */
568 
569 	save_kernel_preemption = IGNORE_KERNEL_PREEMPTION;
570 	IGNORE_KERNEL_PREEMPTION = 1;
571 	if (mutex_tryenter(&xc_mbox_lock[X_CALL_HIPRI])) {
572 		xc_common(func, arg1, arg2, arg3, X_CALL_HIPRI, set, -1);
573 		mutex_exit(&xc_mbox_lock[X_CALL_HIPRI]);
574 	}
575 	IGNORE_KERNEL_PREEMPTION = save_kernel_preemption;
576 }
577 
578 /*
579  * Used by the debugger to cross-call the other CPUs, thus causing them to
580  * enter the debugger.  We can't hold locks, so we spin on the cross-call
581  * lock until we get it.  When we get it, we send the cross-call, and assume
582  * that we successfully stopped the other CPUs.
583  */
584 void
585 kdi_xc_others(int this_cpu, void (*func)(void))
586 {
587 	extern int	IGNORE_KERNEL_PREEMPTION;
588 	int save_kernel_preemption;
589 	mutex_impl_t *lp;
590 	cpuset_t set;
591 	int x;
592 
593 	if (!xc_initialized)
594 		return;
595 
596 	CPUSET_ALL_BUT(set, this_cpu);
597 
598 	save_kernel_preemption = IGNORE_KERNEL_PREEMPTION;
599 	IGNORE_KERNEL_PREEMPTION = 1;
600 
601 	lp = (mutex_impl_t *)&xc_mbox_lock[X_CALL_HIPRI];
602 	for (x = 0; x < 0x400000; x++) {
603 		if (lock_spin_try(&lp->m_spin.m_spinlock)) {
604 			xc_common((xc_func_t)func, 0, 0, 0, X_CALL_HIPRI,
605 			    set, -1);
606 			lp->m_spin.m_spinlock = 0; /* XXX */
607 			break;
608 		}
609 		(void) xc_serv((caddr_t)X_CALL_MEDPRI, NULL);
610 	}
611 	IGNORE_KERNEL_PREEMPTION = save_kernel_preemption;
612 }
613