17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Implements the kernel side of the debugger/kernel work queue.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
327c478bd9Sstevel@tonic-gate #include <kmdb/kctl/kctl.h>
337c478bd9Sstevel@tonic-gate #include <kmdb/kctl/kctl_wr.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/proc.h>
367c478bd9Sstevel@tonic-gate #include <sys/disp.h>
377c478bd9Sstevel@tonic-gate #include <sys/kdi_impl.h>
387c478bd9Sstevel@tonic-gate #include <sys/callb.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	KCTL_WR_PROCESS_NORMAL		(void *)0
417c478bd9Sstevel@tonic-gate #define	KCTL_WR_PROCESS_UNLOADING	(void *)1
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * Processes events from the debugger -> driver notification queue.  Returns
457c478bd9Sstevel@tonic-gate  * 1 if the debugger should be awakened after the queue has been processed.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate static int
kctl_wr_process_cb(kmdb_wr_t * wn,void * arg)487c478bd9Sstevel@tonic-gate kctl_wr_process_cb(kmdb_wr_t *wn, void *arg)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	int unloading = (arg == KCTL_WR_PROCESS_UNLOADING);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	switch (WR_TASK(wn)) {
537c478bd9Sstevel@tonic-gate 	case WNTASK_DMOD_LOAD: {
547c478bd9Sstevel@tonic-gate 		/*
557c478bd9Sstevel@tonic-gate 		 * If this is an ack, then we're getting back a message from a
567c478bd9Sstevel@tonic-gate 		 * load we initiated.  Free it.  If it's not an ack, we process
577c478bd9Sstevel@tonic-gate 		 * the message (attempt to load the requested module) and send
587c478bd9Sstevel@tonic-gate 		 * an ack back to the debugger.
597c478bd9Sstevel@tonic-gate 		 */
607c478bd9Sstevel@tonic-gate 		kmdb_wr_load_t *dlr = (kmdb_wr_load_t *)wn;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 		if (WR_ISACK(dlr)) {
637c478bd9Sstevel@tonic-gate 			kctl_dprintf("received ack for dmod load of %s",
647c478bd9Sstevel@tonic-gate 			    dlr->dlr_fname);
657c478bd9Sstevel@tonic-gate 			kctl_dmod_load_ack(dlr);
667c478bd9Sstevel@tonic-gate 			return (0);
677c478bd9Sstevel@tonic-gate 		} else
687c478bd9Sstevel@tonic-gate 			kctl_dprintf("received dmod load request %s",
697c478bd9Sstevel@tonic-gate 			    dlr->dlr_fname);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 		if (unloading) {
727c478bd9Sstevel@tonic-gate 			/*
737c478bd9Sstevel@tonic-gate 			 * If the user didn't wait for all dmods to load before
74*48bbca81SDaniel Hoffman 			 * they triggered the debugger unload, we may have some
757c478bd9Sstevel@tonic-gate 			 * dmod load requests on the queue in front of the
767c478bd9Sstevel@tonic-gate 			 * blizzard of dmod unload requests that the debugger
777c478bd9Sstevel@tonic-gate 			 * will generate as part of its unload.  The debugger
787c478bd9Sstevel@tonic-gate 			 * won't have generated unloads for pending dmods, so
797c478bd9Sstevel@tonic-gate 			 * we can safely ignore the load requests.
807c478bd9Sstevel@tonic-gate 			 */
817c478bd9Sstevel@tonic-gate 			kctl_dprintf("skipping load of dmod %s due to "
827c478bd9Sstevel@tonic-gate 			    "in-process unload");
837c478bd9Sstevel@tonic-gate 		} else
847c478bd9Sstevel@tonic-gate 			(void) kctl_dmod_load(dlr); /* dlr will have errno */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		WR_ACK(dlr);
877c478bd9Sstevel@tonic-gate 		kmdb_wr_debugger_notify(dlr);
887c478bd9Sstevel@tonic-gate 		return (1);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	case WNTASK_DMOD_LOAD_ALL:
927c478bd9Sstevel@tonic-gate 		/*
937c478bd9Sstevel@tonic-gate 		 * We don't initiate all-module loads, so this can't be an
947c478bd9Sstevel@tonic-gate 		 * ack.  We process the load-all, and send the message back
957c478bd9Sstevel@tonic-gate 		 * to the driver as an ack.
967c478bd9Sstevel@tonic-gate 		 */
977c478bd9Sstevel@tonic-gate 		ASSERT(!WR_ISACK(wn));
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 		kctl_dprintf("received request to load all dmods");
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 		(void) kctl_dmod_load_all();
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		WR_ACK(wn);
1047c478bd9Sstevel@tonic-gate 		kmdb_wr_debugger_notify(wn);
1057c478bd9Sstevel@tonic-gate 		return (1);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	case WNTASK_DMOD_UNLOAD: {
1087c478bd9Sstevel@tonic-gate 		/*
1097c478bd9Sstevel@tonic-gate 		 * The driver received an unload request.  We don't initiate
1107c478bd9Sstevel@tonic-gate 		 * unloads, so this can't be an ack.  We process the unload,
1117c478bd9Sstevel@tonic-gate 		 * and send the message back to the driver as an ack.
1127c478bd9Sstevel@tonic-gate 		 */
1137c478bd9Sstevel@tonic-gate 		kmdb_wr_unload_t *dur = (kmdb_wr_unload_t *)wn;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		ASSERT(!WR_ISACK(dur));
1167c478bd9Sstevel@tonic-gate 		ASSERT(kctl.kctl_boot_ops == NULL);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		kctl_dprintf("received dmod unload message %s",
1197c478bd9Sstevel@tonic-gate 		    dur->dur_modname);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		kctl_dmod_unload(dur);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		WR_ACK(dur);
1247c478bd9Sstevel@tonic-gate 		kmdb_wr_debugger_notify(dur);
1257c478bd9Sstevel@tonic-gate 		return (1);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	case WNTASK_DMOD_PATH_CHANGE: {
1297c478bd9Sstevel@tonic-gate 		/*
1307c478bd9Sstevel@tonic-gate 		 * We don't initiate path changes, so this can't be an ack.
1317c478bd9Sstevel@tonic-gate 		 * This request type differs from the others in that we only
1327c478bd9Sstevel@tonic-gate 		 * return it (as an ack) when we're done with it.  We're only
1337c478bd9Sstevel@tonic-gate 		 * done with it when we receive another one, or when the
1347c478bd9Sstevel@tonic-gate 		 * debugger is unloading.
1357c478bd9Sstevel@tonic-gate 		 */
1367c478bd9Sstevel@tonic-gate 		kmdb_wr_path_t *pth = (kmdb_wr_path_t *)wn;
1377c478bd9Sstevel@tonic-gate 		kmdb_wr_path_t *opth;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		ASSERT(!WR_ISACK(pth));
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		kctl_dprintf("received path change message");
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		if ((opth = kctl_dmod_path_set(pth)) != NULL) {
1447c478bd9Sstevel@tonic-gate 			/* We have an old path request to return */
1457c478bd9Sstevel@tonic-gate 			WR_ACK(opth);
1467c478bd9Sstevel@tonic-gate 			kmdb_wr_debugger_notify(opth);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 			/*
1497c478bd9Sstevel@tonic-gate 			 * The debugger can process the returned path change
1507c478bd9Sstevel@tonic-gate 			 * request at its leisure
1517c478bd9Sstevel@tonic-gate 			 */
1527c478bd9Sstevel@tonic-gate 			return (0);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		/* Nothing to do */
1567c478bd9Sstevel@tonic-gate 		return (0);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	default:
1607c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Received unknown work request %d from kmdb\n",
1617c478bd9Sstevel@tonic-gate 		    wn->wn_task);
1627c478bd9Sstevel@tonic-gate 		/* Drop message */
1637c478bd9Sstevel@tonic-gate 		return (0);
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate int
kctl_wr_process(void)1707c478bd9Sstevel@tonic-gate kctl_wr_process(void)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	return (kmdb_wr_driver_process(kctl_wr_process_cb,
1737c478bd9Sstevel@tonic-gate 	    KCTL_WR_PROCESS_NORMAL));
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Catches the "work to do" soft interrupt, and passes the notification along
1787c478bd9Sstevel@tonic-gate  * to the worker thread.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1817c478bd9Sstevel@tonic-gate void
kctl_wrintr(void)1827c478bd9Sstevel@tonic-gate kctl_wrintr(void)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	kctl.kctl_wr_avail = 0;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	sema_v(&kctl.kctl_wr_avail_sem);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * This routine is called by the debugger while the world is resuming.
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate void
kctl_wrintr_fire(void)1937c478bd9Sstevel@tonic-gate kctl_wrintr_fire(void)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	kctl.kctl_wr_avail = 1;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	kdi_softcall(kctl_wrintr);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  * Given the possibility of asynchronous unload, the locking semantics are
2027c478bd9Sstevel@tonic-gate  * somewhat tricky.  See kctl_main.c
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2057c478bd9Sstevel@tonic-gate static void
kctl_wr_thread(void * arg)2067c478bd9Sstevel@tonic-gate kctl_wr_thread(void *arg)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
2097c478bd9Sstevel@tonic-gate 	kmutex_t cprlock;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	mutex_init(&cprlock, NULL, MUTEX_DEFAULT, NULL);
2127c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &cprlock, callb_generic_cpr, "kmdb work");
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	for (;;) {
2157c478bd9Sstevel@tonic-gate 		/*
2167c478bd9Sstevel@tonic-gate 		 * XXX what should I do here for panic?  It'll spin unless I
2177c478bd9Sstevel@tonic-gate 		 * can figure out a way to park it.  Presumably I don't want to
2187c478bd9Sstevel@tonic-gate 		 * let it exit.
2197c478bd9Sstevel@tonic-gate 		 */
2207c478bd9Sstevel@tonic-gate 		mutex_enter(&cprlock);
2217c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2227c478bd9Sstevel@tonic-gate 		mutex_exit(&cprlock);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		sema_p(&kctl.kctl_wr_avail_sem);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		mutex_enter(&cprlock);
2277c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &cprlock);
2287c478bd9Sstevel@tonic-gate 		mutex_exit(&cprlock);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		kctl_dprintf("kctl worker thread - waking up");
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		if (kmdb_kdi_get_unload_request() ||
2337c478bd9Sstevel@tonic-gate 		    kctl.kctl_wr_state != KCTL_WR_ST_RUN) {
2347c478bd9Sstevel@tonic-gate 			/*
2357c478bd9Sstevel@tonic-gate 			 * We've either got a debugger-initiated unload (if
2367c478bd9Sstevel@tonic-gate 			 * unload_request returned true), or we're stopping due
2377c478bd9Sstevel@tonic-gate 			 * to an error discovered by the driver (if
2387c478bd9Sstevel@tonic-gate 			 * kctl_worker_run is no longer non-zero).  Start
2397c478bd9Sstevel@tonic-gate 			 * cleaning up.
2407c478bd9Sstevel@tonic-gate 			 */
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 			/*
2437c478bd9Sstevel@tonic-gate 			 * The debugger has already deactivated itself, and will
2447c478bd9Sstevel@tonic-gate 			 * have dumped a bunch of stuff on the queue.  We need
2457c478bd9Sstevel@tonic-gate 			 * to process it before exiting.
2467c478bd9Sstevel@tonic-gate 			 */
2477c478bd9Sstevel@tonic-gate 			(void) kmdb_wr_driver_process(kctl_wr_process_cb,
2487c478bd9Sstevel@tonic-gate 			    KCTL_WR_PROCESS_UNLOADING);
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		/*
2537c478bd9Sstevel@tonic-gate 		 * A non-zero return means we've passed messages back to the
2547c478bd9Sstevel@tonic-gate 		 * debugger for processing, so we need to wake the debugger up.
2557c478bd9Sstevel@tonic-gate 		 */
2567c478bd9Sstevel@tonic-gate 		if (kctl_wr_process() > 0)
257ae115bc7Smrj 			kmdb_kdi_kmdb_enter();
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/*
2617c478bd9Sstevel@tonic-gate 	 * NULL out the dmod search path, so we can send the current one back
2627c478bd9Sstevel@tonic-gate 	 * to the debugger.  XXX this should probably be somewhere else.
2637c478bd9Sstevel@tonic-gate 	 */
2647c478bd9Sstevel@tonic-gate 	kctl_dmod_path_reset();
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * The debugger will send us unload notifications for each dmod that it
2687c478bd9Sstevel@tonic-gate 	 * noticed.  If, for example, the debugger is unloaded before the first
2697c478bd9Sstevel@tonic-gate 	 * start, it won't have noticed any of the dmods we loaded.  We'll need
2707c478bd9Sstevel@tonic-gate 	 * to initiate the unloads ourselves.
2717c478bd9Sstevel@tonic-gate 	 */
2727c478bd9Sstevel@tonic-gate 	kctl_dmod_unload_all();
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	kctl.kctl_wr_state = KCTL_WR_ST_STOPPED;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * Must be last, as it concludes by setting state to INACTIVE.  The
2787c478bd9Sstevel@tonic-gate 	 * kctl data structure must not be accessed by this thread after that
2797c478bd9Sstevel@tonic-gate 	 * point.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	kctl_cleanup();
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	mutex_enter(&cprlock);
2847c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
2857c478bd9Sstevel@tonic-gate 	mutex_destroy(&cprlock);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate void
kctl_wr_thr_start(void)2897c478bd9Sstevel@tonic-gate kctl_wr_thr_start(void)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	kctl.kctl_wr_avail = 0;
2927c478bd9Sstevel@tonic-gate 	kctl.kctl_wr_state = KCTL_WR_ST_RUN;
2937c478bd9Sstevel@tonic-gate 	kctl.kctl_wr_thr = thread_create(NULL, 0, kctl_wr_thread, NULL, 0, &p0,
2947c478bd9Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate void
kctl_wr_thr_stop(void)2987c478bd9Sstevel@tonic-gate kctl_wr_thr_stop(void)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	ASSERT(kctl.kctl_wr_state == KCTL_WR_ST_RUN);
3017c478bd9Sstevel@tonic-gate 	kctl.kctl_wr_state = KCTL_WR_ST_STOP;
3027c478bd9Sstevel@tonic-gate 	sema_v(&kctl.kctl_wr_avail_sem);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate void
kctl_wr_thr_join(void)3067c478bd9Sstevel@tonic-gate kctl_wr_thr_join(void)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	thread_join(kctl.kctl_wr_thr->t_did);
3097c478bd9Sstevel@tonic-gate }
310