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  * Copyright (c) 2016 by Delphix. All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Implements the kernel side of the debugger/kernel work queue.
31  */
32 
33 #include <kmdb/kmdb_kdi.h>
34 #include <kmdb/kctl/kctl.h>
35 #include <kmdb/kctl/kctl_wr.h>
36 
37 #include <sys/proc.h>
38 #include <sys/disp.h>
39 #include <sys/kdi_impl.h>
40 #include <sys/callb.h>
41 
42 #define	KCTL_WR_PROCESS_NORMAL		(void *)0
43 #define	KCTL_WR_PROCESS_UNLOADING	(void *)1
44 
45 /*
46  * Processes events from the debugger -> driver notification queue.  Returns
47  * 1 if the debugger should be awakened after the queue has been processed.
48  */
49 static int
50 kctl_wr_process_cb(kmdb_wr_t *wn, void *arg)
51 {
52 	int unloading = (arg == KCTL_WR_PROCESS_UNLOADING);
53 
54 	switch (WR_TASK(wn)) {
55 	case WNTASK_DMOD_LOAD: {
56 		/*
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.
61 		 */
62 		kmdb_wr_load_t *dlr = (kmdb_wr_load_t *)wn;
63 
64 		if (WR_ISACK(dlr)) {
65 			kctl_dprintf("received ack for dmod load of %s",
66 			    dlr->dlr_fname);
67 			kctl_dmod_load_ack(dlr);
68 			return (0);
69 		} else
70 			kctl_dprintf("received dmod load request %s",
71 			    dlr->dlr_fname);
72 
73 		if (unloading) {
74 			/*
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.
82 			 */
83 			kctl_dprintf("skipping load of dmod %s due to "
84 			    "in-process unload");
85 		} else
86 			(void) kctl_dmod_load(dlr); /* dlr will have errno */
87 
88 		WR_ACK(dlr);
89 		kmdb_wr_debugger_notify(dlr);
90 		return (1);
91 	}
92 
93 	case WNTASK_DMOD_LOAD_ALL:
94 		/*
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.
98 		 */
99 		ASSERT(!WR_ISACK(wn));
100 
101 		kctl_dprintf("received request to load all dmods");
102 
103 		(void) kctl_dmod_load_all();
104 
105 		WR_ACK(wn);
106 		kmdb_wr_debugger_notify(wn);
107 		return (1);
108 
109 	case WNTASK_DMOD_UNLOAD: {
110 		/*
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.
114 		 */
115 		kmdb_wr_unload_t *dur = (kmdb_wr_unload_t *)wn;
116 
117 		ASSERT(!WR_ISACK(dur));
118 		ASSERT(kctl.kctl_boot_ops == NULL);
119 
120 		kctl_dprintf("received dmod unload message %s",
121 		    dur->dur_modname);
122 
123 		kctl_dmod_unload(dur);
124 
125 		WR_ACK(dur);
126 		kmdb_wr_debugger_notify(dur);
127 		return (1);
128 	}
129 
130 	case WNTASK_DMOD_PATH_CHANGE: {
131 		/*
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.
137 		 */
138 		kmdb_wr_path_t *pth = (kmdb_wr_path_t *)wn;
139 		kmdb_wr_path_t *opth;
140 
141 		ASSERT(!WR_ISACK(pth));
142 
143 		kctl_dprintf("received path change message");
144 
145 		if ((opth = kctl_dmod_path_set(pth)) != NULL) {
146 			/* We have an old path request to return */
147 			WR_ACK(opth);
148 			kmdb_wr_debugger_notify(opth);
149 
150 			/*
151 			 * The debugger can process the returned path change
152 			 * request at its leisure
153 			 */
154 			return (0);
155 		}
156 
157 		/* Nothing to do */
158 		return (0);
159 	}
160 
161 	default:
162 		cmn_err(CE_WARN, "Received unknown work request %d from kmdb\n",
163 		    wn->wn_task);
164 		/* Drop message */
165 		return (0);
166 	}
167 
168 	/*NOTREACHED*/
169 }
170 
171 int
172 kctl_wr_process(void)
173 {
174 	return (kmdb_wr_driver_process(kctl_wr_process_cb,
175 	    KCTL_WR_PROCESS_NORMAL));
176 }
177 
178 /*
179  * Catches the "work to do" soft interrupt, and passes the notification along
180  * to the worker thread.
181  */
182 /*ARGSUSED*/
183 void
184 kctl_wrintr(void)
185 {
186 	kctl.kctl_wr_avail = 0;
187 
188 	sema_v(&kctl.kctl_wr_avail_sem);
189 }
190 
191 /*
192  * This routine is called by the debugger while the world is resuming.
193  */
194 void
195 kctl_wrintr_fire(void)
196 {
197 	kctl.kctl_wr_avail = 1;
198 
199 	kdi_softcall(kctl_wrintr);
200 }
201 
202 /*
203  * Given the possibility of asynchronous unload, the locking semantics are
204  * somewhat tricky.  See kctl_main.c
205  */
206 /*ARGSUSED*/
207 static void
208 kctl_wr_thread(void *arg)
209 {
210 	callb_cpr_t cprinfo;
211 	kmutex_t cprlock;
212 
213 	mutex_init(&cprlock, NULL, MUTEX_DEFAULT, NULL);
214 	CALLB_CPR_INIT(&cprinfo, &cprlock, callb_generic_cpr, "kmdb work");
215 
216 	for (;;) {
217 		/*
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
220 		 * let it exit.
221 		 */
222 		mutex_enter(&cprlock);
223 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
224 		mutex_exit(&cprlock);
225 
226 		sema_p(&kctl.kctl_wr_avail_sem);
227 
228 		mutex_enter(&cprlock);
229 		CALLB_CPR_SAFE_END(&cprinfo, &cprlock);
230 		mutex_exit(&cprlock);
231 
232 		kctl_dprintf("kctl worker thread - waking up");
233 
234 		if (kmdb_kdi_get_unload_request() ||
235 		    kctl.kctl_wr_state != KCTL_WR_ST_RUN) {
236 			/*
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
241 			 * cleaning up.
242 			 */
243 
244 			/*
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.
248 			 */
249 			(void) kmdb_wr_driver_process(kctl_wr_process_cb,
250 			    KCTL_WR_PROCESS_UNLOADING);
251 			break;
252 		}
253 
254 		/*
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.
257 		 */
258 		if (kctl_wr_process() > 0)
259 			kmdb_kdi_kmdb_enter();
260 	}
261 
262 	/*
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.
265 	 */
266 	kctl_dmod_path_reset();
267 
268 	/*
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.
273 	 */
274 	kctl_dmod_unload_all();
275 
276 	kctl.kctl_wr_state = KCTL_WR_ST_STOPPED;
277 
278 	/*
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
281 	 * point.
282 	 */
283 	kctl_cleanup();
284 
285 	mutex_enter(&cprlock);
286 	CALLB_CPR_EXIT(&cprinfo);
287 	mutex_destroy(&cprlock);
288 }
289 
290 void
291 kctl_wr_thr_start(void)
292 {
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);
297 }
298 
299 void
300 kctl_wr_thr_stop(void)
301 {
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);
305 }
306 
307 void
308 kctl_wr_thr_join(void)
309 {
310 	thread_join(kctl.kctl_wr_thr->t_did);
311 }
312