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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright (c) 2016 by Delphix. All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Implements the kernel side of the debugger/kernel work queue.
33 #include <kmdb/kmdb_kdi.h>
34 #include <kmdb/kctl/kctl.h>
35 #include <kmdb/kctl/kctl_wr.h>
39 #include <sys/kdi_impl.h>
40 #include <sys/callb.h>
42 #define KCTL_WR_PROCESS_NORMAL NULL
43 #define KCTL_WR_PROCESS_UNLOADING (void *)1
46 * Processes events from the debugger -> driver notification queue. Returns
47 * 1 if the debugger should be awakened after the queue has been processed.
50 kctl_wr_process_cb(kmdb_wr_t
*wn
, void *arg
)
52 int unloading
= (arg
== KCTL_WR_PROCESS_UNLOADING
);
54 switch (WR_TASK(wn
)) {
55 case WNTASK_DMOD_LOAD
: {
57 * If this is an ack, then we're getting back a message from a
58 * load we initiated. Free it. If it's not an ack, we process
59 * the message (attempt to load the requested module) and send
60 * an ack back to the debugger.
62 kmdb_wr_load_t
*dlr
= (kmdb_wr_load_t
*)wn
;
65 kctl_dprintf("received ack for dmod load of %s",
67 kctl_dmod_load_ack(dlr
);
70 kctl_dprintf("received dmod load request %s",
75 * If the user didn't wait for all dmods to load before
76 * they triggered the debugger unload, we may have some
77 * dmod load requests on the queue in front of the
78 * blizzard of dmod unload requests that the debugger
79 * will generate as part of its unload. The debugger
80 * won't have generated unloads for pending dmods, so
81 * we can safely ignore the load requests.
83 kctl_dprintf("skipping load of dmod %s due to "
86 (void) kctl_dmod_load(dlr
); /* dlr will have errno */
89 kmdb_wr_debugger_notify(dlr
);
93 case WNTASK_DMOD_LOAD_ALL
:
95 * We don't initiate all-module loads, so this can't be an
96 * ack. We process the load-all, and send the message back
97 * to the driver as an ack.
99 ASSERT(!WR_ISACK(wn
));
101 kctl_dprintf("received request to load all dmods");
103 (void) kctl_dmod_load_all();
106 kmdb_wr_debugger_notify(wn
);
109 case WNTASK_DMOD_UNLOAD
: {
111 * The driver received an unload request. We don't initiate
112 * unloads, so this can't be an ack. We process the unload,
113 * and send the message back to the driver as an ack.
115 kmdb_wr_unload_t
*dur
= (kmdb_wr_unload_t
*)wn
;
117 ASSERT(!WR_ISACK(dur
));
118 ASSERT(kctl
.kctl_boot_ops
== NULL
);
120 kctl_dprintf("received dmod unload message %s",
123 kctl_dmod_unload(dur
);
126 kmdb_wr_debugger_notify(dur
);
130 case WNTASK_DMOD_PATH_CHANGE
: {
132 * We don't initiate path changes, so this can't be an ack.
133 * This request type differs from the others in that we only
134 * return it (as an ack) when we're done with it. We're only
135 * done with it when we receive another one, or when the
136 * debugger is unloading.
138 kmdb_wr_path_t
*pth
= (kmdb_wr_path_t
*)wn
;
139 kmdb_wr_path_t
*opth
;
141 ASSERT(!WR_ISACK(pth
));
143 kctl_dprintf("received path change message");
145 if ((opth
= kctl_dmod_path_set(pth
)) != NULL
) {
146 /* We have an old path request to return */
148 kmdb_wr_debugger_notify(opth
);
151 * The debugger can process the returned path change
152 * request at its leisure
162 cmn_err(CE_WARN
, "Received unknown work request %d from kmdb\n",
172 kctl_wr_process(void)
174 return (kmdb_wr_driver_process(kctl_wr_process_cb
,
175 KCTL_WR_PROCESS_NORMAL
));
179 * Catches the "work to do" soft interrupt, and passes the notification along
180 * to the worker thread.
186 kctl
.kctl_wr_avail
= 0;
188 sema_v(&kctl
.kctl_wr_avail_sem
);
192 * This routine is called by the debugger while the world is resuming.
195 kctl_wrintr_fire(void)
197 kctl
.kctl_wr_avail
= 1;
199 kdi_softcall(kctl_wrintr
);
203 * Given the possibility of asynchronous unload, the locking semantics are
204 * somewhat tricky. See kctl_main.c
208 kctl_wr_thread(void *arg
)
213 mutex_init(&cprlock
, NULL
, MUTEX_DEFAULT
, NULL
);
214 CALLB_CPR_INIT(&cprinfo
, &cprlock
, callb_generic_cpr
, "kmdb work");
218 * XXX what should I do here for panic? It'll spin unless I
219 * can figure out a way to park it. Presumably I don't want to
222 mutex_enter(&cprlock
);
223 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
224 mutex_exit(&cprlock
);
226 sema_p(&kctl
.kctl_wr_avail_sem
);
228 mutex_enter(&cprlock
);
229 CALLB_CPR_SAFE_END(&cprinfo
, &cprlock
);
230 mutex_exit(&cprlock
);
232 kctl_dprintf("kctl worker thread - waking up");
234 if (kmdb_kdi_get_unload_request() ||
235 kctl
.kctl_wr_state
!= KCTL_WR_ST_RUN
) {
237 * We've either got a debugger-initiated unload (if
238 * unload_request returned true), or we're stopping due
239 * to an error discovered by the driver (if
240 * kctl_worker_run is no longer non-zero). Start
245 * The debugger has already deactivated itself, and will
246 * have dumped a bunch of stuff on the queue. We need
247 * to process it before exiting.
249 (void) kmdb_wr_driver_process(kctl_wr_process_cb
,
250 KCTL_WR_PROCESS_UNLOADING
);
255 * A non-zero return means we've passed messages back to the
256 * debugger for processing, so we need to wake the debugger up.
258 if (kctl_wr_process() > 0)
259 kmdb_kdi_kmdb_enter();
263 * NULL out the dmod search path, so we can send the current one back
264 * to the debugger. XXX this should probably be somewhere else.
266 kctl_dmod_path_reset();
269 * The debugger will send us unload notifications for each dmod that it
270 * noticed. If, for example, the debugger is unloaded before the first
271 * start, it won't have noticed any of the dmods we loaded. We'll need
272 * to initiate the unloads ourselves.
274 kctl_dmod_unload_all();
276 kctl
.kctl_wr_state
= KCTL_WR_ST_STOPPED
;
279 * Must be last, as it concludes by setting state to INACTIVE. The
280 * kctl data structure must not be accessed by this thread after that
285 mutex_enter(&cprlock
);
286 CALLB_CPR_EXIT(&cprinfo
);
287 mutex_destroy(&cprlock
);
291 kctl_wr_thr_start(void)
293 kctl
.kctl_wr_avail
= 0;
294 kctl
.kctl_wr_state
= KCTL_WR_ST_RUN
;
295 kctl
.kctl_wr_thr
= thread_create(NULL
, 0, kctl_wr_thread
, NULL
, 0, &p0
,
296 TS_RUN
, minclsyspri
);
300 kctl_wr_thr_stop(void)
302 ASSERT(kctl
.kctl_wr_state
== KCTL_WR_ST_RUN
);
303 kctl
.kctl_wr_state
= KCTL_WR_ST_STOP
;
304 sema_v(&kctl
.kctl_wr_avail_sem
);
308 kctl_wr_thr_join(void)
310 thread_join(kctl
.kctl_wr_thr
->t_did
);