10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3446Smrj * Common Development and Distribution License (the "License").
6*3446Smrj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*3446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Implements the kernel side of the debugger/kernel work queue.
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
330Sstevel@tonic-gate #include <kmdb/kctl/kctl.h>
340Sstevel@tonic-gate #include <kmdb/kctl/kctl_wr.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <sys/proc.h>
370Sstevel@tonic-gate #include <sys/disp.h>
380Sstevel@tonic-gate #include <sys/kdi_impl.h>
390Sstevel@tonic-gate #include <sys/callb.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gate #define KCTL_WR_PROCESS_NORMAL (void *)0
420Sstevel@tonic-gate #define KCTL_WR_PROCESS_UNLOADING (void *)1
430Sstevel@tonic-gate
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate * Processes events from the debugger -> driver notification queue. Returns
460Sstevel@tonic-gate * 1 if the debugger should be awakened after the queue has been processed.
470Sstevel@tonic-gate */
480Sstevel@tonic-gate static int
kctl_wr_process_cb(kmdb_wr_t * wn,void * arg)490Sstevel@tonic-gate kctl_wr_process_cb(kmdb_wr_t *wn, void *arg)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate int unloading = (arg == KCTL_WR_PROCESS_UNLOADING);
520Sstevel@tonic-gate
530Sstevel@tonic-gate switch (WR_TASK(wn)) {
540Sstevel@tonic-gate case WNTASK_DMOD_LOAD: {
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate * If this is an ack, then we're getting back a message from a
570Sstevel@tonic-gate * load we initiated. Free it. If it's not an ack, we process
580Sstevel@tonic-gate * the message (attempt to load the requested module) and send
590Sstevel@tonic-gate * an ack back to the debugger.
600Sstevel@tonic-gate */
610Sstevel@tonic-gate kmdb_wr_load_t *dlr = (kmdb_wr_load_t *)wn;
620Sstevel@tonic-gate
630Sstevel@tonic-gate if (WR_ISACK(dlr)) {
640Sstevel@tonic-gate kctl_dprintf("received ack for dmod load of %s",
650Sstevel@tonic-gate dlr->dlr_fname);
660Sstevel@tonic-gate kctl_dmod_load_ack(dlr);
670Sstevel@tonic-gate return (0);
680Sstevel@tonic-gate } else
690Sstevel@tonic-gate kctl_dprintf("received dmod load request %s",
700Sstevel@tonic-gate dlr->dlr_fname);
710Sstevel@tonic-gate
720Sstevel@tonic-gate if (unloading) {
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate * If the user didn't wait for all dmods to load before
750Sstevel@tonic-gate * she triggered the debugger unload, we may have some
760Sstevel@tonic-gate * dmod load requests on the queue in front of the
770Sstevel@tonic-gate * blizzard of dmod unload requests that the debugger
780Sstevel@tonic-gate * will generate as part of its unload. The debugger
790Sstevel@tonic-gate * won't have generated unloads for pending dmods, so
800Sstevel@tonic-gate * we can safely ignore the load requests.
810Sstevel@tonic-gate */
820Sstevel@tonic-gate kctl_dprintf("skipping load of dmod %s due to "
830Sstevel@tonic-gate "in-process unload");
840Sstevel@tonic-gate } else
850Sstevel@tonic-gate (void) kctl_dmod_load(dlr); /* dlr will have errno */
860Sstevel@tonic-gate
870Sstevel@tonic-gate WR_ACK(dlr);
880Sstevel@tonic-gate kmdb_wr_debugger_notify(dlr);
890Sstevel@tonic-gate return (1);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
920Sstevel@tonic-gate case WNTASK_DMOD_LOAD_ALL:
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate * We don't initiate all-module loads, so this can't be an
950Sstevel@tonic-gate * ack. We process the load-all, and send the message back
960Sstevel@tonic-gate * to the driver as an ack.
970Sstevel@tonic-gate */
980Sstevel@tonic-gate ASSERT(!WR_ISACK(wn));
990Sstevel@tonic-gate
1000Sstevel@tonic-gate kctl_dprintf("received request to load all dmods");
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate (void) kctl_dmod_load_all();
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate WR_ACK(wn);
1050Sstevel@tonic-gate kmdb_wr_debugger_notify(wn);
1060Sstevel@tonic-gate return (1);
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate case WNTASK_DMOD_UNLOAD: {
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate * The driver received an unload request. We don't initiate
1110Sstevel@tonic-gate * unloads, so this can't be an ack. We process the unload,
1120Sstevel@tonic-gate * and send the message back to the driver as an ack.
1130Sstevel@tonic-gate */
1140Sstevel@tonic-gate kmdb_wr_unload_t *dur = (kmdb_wr_unload_t *)wn;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate ASSERT(!WR_ISACK(dur));
1170Sstevel@tonic-gate ASSERT(kctl.kctl_boot_ops == NULL);
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate kctl_dprintf("received dmod unload message %s",
1200Sstevel@tonic-gate dur->dur_modname);
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate kctl_dmod_unload(dur);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate WR_ACK(dur);
1250Sstevel@tonic-gate kmdb_wr_debugger_notify(dur);
1260Sstevel@tonic-gate return (1);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate case WNTASK_DMOD_PATH_CHANGE: {
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate * We don't initiate path changes, so this can't be an ack.
1320Sstevel@tonic-gate * This request type differs from the others in that we only
1330Sstevel@tonic-gate * return it (as an ack) when we're done with it. We're only
1340Sstevel@tonic-gate * done with it when we receive another one, or when the
1350Sstevel@tonic-gate * debugger is unloading.
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate kmdb_wr_path_t *pth = (kmdb_wr_path_t *)wn;
1380Sstevel@tonic-gate kmdb_wr_path_t *opth;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate ASSERT(!WR_ISACK(pth));
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate kctl_dprintf("received path change message");
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if ((opth = kctl_dmod_path_set(pth)) != NULL) {
1450Sstevel@tonic-gate /* We have an old path request to return */
1460Sstevel@tonic-gate WR_ACK(opth);
1470Sstevel@tonic-gate kmdb_wr_debugger_notify(opth);
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * The debugger can process the returned path change
1510Sstevel@tonic-gate * request at its leisure
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate return (0);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /* Nothing to do */
1570Sstevel@tonic-gate return (0);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate default:
1610Sstevel@tonic-gate cmn_err(CE_WARN, "Received unknown work request %d from kmdb\n",
1620Sstevel@tonic-gate wn->wn_task);
1630Sstevel@tonic-gate /* Drop message */
1640Sstevel@tonic-gate return (0);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /*NOTREACHED*/
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate int
kctl_wr_process(void)1710Sstevel@tonic-gate kctl_wr_process(void)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate return (kmdb_wr_driver_process(kctl_wr_process_cb,
1740Sstevel@tonic-gate KCTL_WR_PROCESS_NORMAL));
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate * Catches the "work to do" soft interrupt, and passes the notification along
1790Sstevel@tonic-gate * to the worker thread.
1800Sstevel@tonic-gate */
1810Sstevel@tonic-gate /*ARGSUSED*/
1820Sstevel@tonic-gate void
kctl_wrintr(void)1830Sstevel@tonic-gate kctl_wrintr(void)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate kctl.kctl_wr_avail = 0;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate sema_v(&kctl.kctl_wr_avail_sem);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate * This routine is called by the debugger while the world is resuming.
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate void
kctl_wrintr_fire(void)1940Sstevel@tonic-gate kctl_wrintr_fire(void)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate kctl.kctl_wr_avail = 1;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate kdi_softcall(kctl_wrintr);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate * Given the possibility of asynchronous unload, the locking semantics are
2030Sstevel@tonic-gate * somewhat tricky. See kctl_main.c
2040Sstevel@tonic-gate */
2050Sstevel@tonic-gate /*ARGSUSED*/
2060Sstevel@tonic-gate static void
kctl_wr_thread(void * arg)2070Sstevel@tonic-gate kctl_wr_thread(void *arg)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate callb_cpr_t cprinfo;
2100Sstevel@tonic-gate kmutex_t cprlock;
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate mutex_init(&cprlock, NULL, MUTEX_DEFAULT, NULL);
2130Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &cprlock, callb_generic_cpr, "kmdb work");
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate for (;;) {
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate * XXX what should I do here for panic? It'll spin unless I
2180Sstevel@tonic-gate * can figure out a way to park it. Presumably I don't want to
2190Sstevel@tonic-gate * let it exit.
2200Sstevel@tonic-gate */
2210Sstevel@tonic-gate mutex_enter(&cprlock);
2220Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
2230Sstevel@tonic-gate mutex_exit(&cprlock);
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate sema_p(&kctl.kctl_wr_avail_sem);
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate mutex_enter(&cprlock);
2280Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &cprlock);
2290Sstevel@tonic-gate mutex_exit(&cprlock);
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate kctl_dprintf("kctl worker thread - waking up");
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (kmdb_kdi_get_unload_request() ||
2340Sstevel@tonic-gate kctl.kctl_wr_state != KCTL_WR_ST_RUN) {
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate * We've either got a debugger-initiated unload (if
2370Sstevel@tonic-gate * unload_request returned true), or we're stopping due
2380Sstevel@tonic-gate * to an error discovered by the driver (if
2390Sstevel@tonic-gate * kctl_worker_run is no longer non-zero). Start
2400Sstevel@tonic-gate * cleaning up.
2410Sstevel@tonic-gate */
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate * The debugger has already deactivated itself, and will
2450Sstevel@tonic-gate * have dumped a bunch of stuff on the queue. We need
2460Sstevel@tonic-gate * to process it before exiting.
2470Sstevel@tonic-gate */
2480Sstevel@tonic-gate (void) kmdb_wr_driver_process(kctl_wr_process_cb,
2490Sstevel@tonic-gate KCTL_WR_PROCESS_UNLOADING);
2500Sstevel@tonic-gate break;
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate /*
2540Sstevel@tonic-gate * A non-zero return means we've passed messages back to the
2550Sstevel@tonic-gate * debugger for processing, so we need to wake the debugger up.
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate if (kctl_wr_process() > 0)
258*3446Smrj kmdb_kdi_kmdb_enter();
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate * NULL out the dmod search path, so we can send the current one back
2630Sstevel@tonic-gate * to the debugger. XXX this should probably be somewhere else.
2640Sstevel@tonic-gate */
2650Sstevel@tonic-gate kctl_dmod_path_reset();
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate * The debugger will send us unload notifications for each dmod that it
2690Sstevel@tonic-gate * noticed. If, for example, the debugger is unloaded before the first
2700Sstevel@tonic-gate * start, it won't have noticed any of the dmods we loaded. We'll need
2710Sstevel@tonic-gate * to initiate the unloads ourselves.
2720Sstevel@tonic-gate */
2730Sstevel@tonic-gate kctl_dmod_unload_all();
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate kctl.kctl_wr_state = KCTL_WR_ST_STOPPED;
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate * Must be last, as it concludes by setting state to INACTIVE. The
2790Sstevel@tonic-gate * kctl data structure must not be accessed by this thread after that
2800Sstevel@tonic-gate * point.
2810Sstevel@tonic-gate */
2820Sstevel@tonic-gate kctl_cleanup();
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate mutex_enter(&cprlock);
2850Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo);
2860Sstevel@tonic-gate mutex_destroy(&cprlock);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate void
kctl_wr_thr_start(void)2900Sstevel@tonic-gate kctl_wr_thr_start(void)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate kctl.kctl_wr_avail = 0;
2930Sstevel@tonic-gate kctl.kctl_wr_state = KCTL_WR_ST_RUN;
2940Sstevel@tonic-gate kctl.kctl_wr_thr = thread_create(NULL, 0, kctl_wr_thread, NULL, 0, &p0,
2950Sstevel@tonic-gate TS_RUN, minclsyspri);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate void
kctl_wr_thr_stop(void)2990Sstevel@tonic-gate kctl_wr_thr_stop(void)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate ASSERT(kctl.kctl_wr_state == KCTL_WR_ST_RUN);
3020Sstevel@tonic-gate kctl.kctl_wr_state = KCTL_WR_ST_STOP;
3030Sstevel@tonic-gate sema_v(&kctl.kctl_wr_avail_sem);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate void
kctl_wr_thr_join(void)3070Sstevel@tonic-gate kctl_wr_thr_join(void)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate thread_join(kctl.kctl_wr_thr->t_did);
3100Sstevel@tonic-gate }
311