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