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 /* 28 * Implements the kernel side of the debugger/kernel work queue. 29 */ 30 31 #include <kmdb/kmdb_kdi.h> 32 #include <kmdb/kctl/kctl.h> 33 #include <kmdb/kctl/kctl_wr.h> 34 35 #include <sys/proc.h> 36 #include <sys/disp.h> 37 #include <sys/kdi_impl.h> 38 #include <sys/callb.h> 39 40 #define KCTL_WR_PROCESS_NORMAL (void *)0 41 #define KCTL_WR_PROCESS_UNLOADING (void *)1 42 43 /* 44 * Processes events from the debugger -> driver notification queue. Returns 45 * 1 if the debugger should be awakened after the queue has been processed. 46 */ 47 static int 48 kctl_wr_process_cb(kmdb_wr_t *wn, void *arg) 49 { 50 int unloading = (arg == KCTL_WR_PROCESS_UNLOADING); 51 52 switch (WR_TASK(wn)) { 53 case WNTASK_DMOD_LOAD: { 54 /* 55 * If this is an ack, then we're getting back a message from a 56 * load we initiated. Free it. If it's not an ack, we process 57 * the message (attempt to load the requested module) and send 58 * an ack back to the debugger. 59 */ 60 kmdb_wr_load_t *dlr = (kmdb_wr_load_t *)wn; 61 62 if (WR_ISACK(dlr)) { 63 kctl_dprintf("received ack for dmod load of %s", 64 dlr->dlr_fname); 65 kctl_dmod_load_ack(dlr); 66 return (0); 67 } else 68 kctl_dprintf("received dmod load request %s", 69 dlr->dlr_fname); 70 71 if (unloading) { 72 /* 73 * If the user didn't wait for all dmods to load before 74 * they triggered the debugger unload, we may have some 75 * dmod load requests on the queue in front of the 76 * blizzard of dmod unload requests that the debugger 77 * will generate as part of its unload. The debugger 78 * won't have generated unloads for pending dmods, so 79 * we can safely ignore the load requests. 80 */ 81 kctl_dprintf("skipping load of dmod %s due to " 82 "in-process unload"); 83 } else 84 (void) kctl_dmod_load(dlr); /* dlr will have errno */ 85 86 WR_ACK(dlr); 87 kmdb_wr_debugger_notify(dlr); 88 return (1); 89 } 90 91 case WNTASK_DMOD_LOAD_ALL: 92 /* 93 * We don't initiate all-module loads, so this can't be an 94 * ack. We process the load-all, and send the message back 95 * to the driver as an ack. 96 */ 97 ASSERT(!WR_ISACK(wn)); 98 99 kctl_dprintf("received request to load all dmods"); 100 101 (void) kctl_dmod_load_all(); 102 103 WR_ACK(wn); 104 kmdb_wr_debugger_notify(wn); 105 return (1); 106 107 case WNTASK_DMOD_UNLOAD: { 108 /* 109 * The driver received an unload request. We don't initiate 110 * unloads, so this can't be an ack. We process the unload, 111 * and send the message back to the driver as an ack. 112 */ 113 kmdb_wr_unload_t *dur = (kmdb_wr_unload_t *)wn; 114 115 ASSERT(!WR_ISACK(dur)); 116 ASSERT(kctl.kctl_boot_ops == NULL); 117 118 kctl_dprintf("received dmod unload message %s", 119 dur->dur_modname); 120 121 kctl_dmod_unload(dur); 122 123 WR_ACK(dur); 124 kmdb_wr_debugger_notify(dur); 125 return (1); 126 } 127 128 case WNTASK_DMOD_PATH_CHANGE: { 129 /* 130 * We don't initiate path changes, so this can't be an ack. 131 * This request type differs from the others in that we only 132 * return it (as an ack) when we're done with it. We're only 133 * done with it when we receive another one, or when the 134 * debugger is unloading. 135 */ 136 kmdb_wr_path_t *pth = (kmdb_wr_path_t *)wn; 137 kmdb_wr_path_t *opth; 138 139 ASSERT(!WR_ISACK(pth)); 140 141 kctl_dprintf("received path change message"); 142 143 if ((opth = kctl_dmod_path_set(pth)) != NULL) { 144 /* We have an old path request to return */ 145 WR_ACK(opth); 146 kmdb_wr_debugger_notify(opth); 147 148 /* 149 * The debugger can process the returned path change 150 * request at its leisure 151 */ 152 return (0); 153 } 154 155 /* Nothing to do */ 156 return (0); 157 } 158 159 default: 160 cmn_err(CE_WARN, "Received unknown work request %d from kmdb\n", 161 wn->wn_task); 162 /* Drop message */ 163 return (0); 164 } 165 166 /*NOTREACHED*/ 167 } 168 169 int 170 kctl_wr_process(void) 171 { 172 return (kmdb_wr_driver_process(kctl_wr_process_cb, 173 KCTL_WR_PROCESS_NORMAL)); 174 } 175 176 /* 177 * Catches the "work to do" soft interrupt, and passes the notification along 178 * to the worker thread. 179 */ 180 /*ARGSUSED*/ 181 void 182 kctl_wrintr(void) 183 { 184 kctl.kctl_wr_avail = 0; 185 186 sema_v(&kctl.kctl_wr_avail_sem); 187 } 188 189 /* 190 * This routine is called by the debugger while the world is resuming. 191 */ 192 void 193 kctl_wrintr_fire(void) 194 { 195 kctl.kctl_wr_avail = 1; 196 197 kdi_softcall(kctl_wrintr); 198 } 199 200 /* 201 * Given the possibility of asynchronous unload, the locking semantics are 202 * somewhat tricky. See kctl_main.c 203 */ 204 /*ARGSUSED*/ 205 static void 206 kctl_wr_thread(void *arg) 207 { 208 callb_cpr_t cprinfo; 209 kmutex_t cprlock; 210 211 mutex_init(&cprlock, NULL, MUTEX_DEFAULT, NULL); 212 CALLB_CPR_INIT(&cprinfo, &cprlock, callb_generic_cpr, "kmdb work"); 213 214 for (;;) { 215 /* 216 * XXX what should I do here for panic? It'll spin unless I 217 * can figure out a way to park it. Presumably I don't want to 218 * let it exit. 219 */ 220 mutex_enter(&cprlock); 221 CALLB_CPR_SAFE_BEGIN(&cprinfo); 222 mutex_exit(&cprlock); 223 224 sema_p(&kctl.kctl_wr_avail_sem); 225 226 mutex_enter(&cprlock); 227 CALLB_CPR_SAFE_END(&cprinfo, &cprlock); 228 mutex_exit(&cprlock); 229 230 kctl_dprintf("kctl worker thread - waking up"); 231 232 if (kmdb_kdi_get_unload_request() || 233 kctl.kctl_wr_state != KCTL_WR_ST_RUN) { 234 /* 235 * We've either got a debugger-initiated unload (if 236 * unload_request returned true), or we're stopping due 237 * to an error discovered by the driver (if 238 * kctl_worker_run is no longer non-zero). Start 239 * cleaning up. 240 */ 241 242 /* 243 * The debugger has already deactivated itself, and will 244 * have dumped a bunch of stuff on the queue. We need 245 * to process it before exiting. 246 */ 247 (void) kmdb_wr_driver_process(kctl_wr_process_cb, 248 KCTL_WR_PROCESS_UNLOADING); 249 break; 250 } 251 252 /* 253 * A non-zero return means we've passed messages back to the 254 * debugger for processing, so we need to wake the debugger up. 255 */ 256 if (kctl_wr_process() > 0) 257 kmdb_kdi_kmdb_enter(); 258 } 259 260 /* 261 * NULL out the dmod search path, so we can send the current one back 262 * to the debugger. XXX this should probably be somewhere else. 263 */ 264 kctl_dmod_path_reset(); 265 266 /* 267 * The debugger will send us unload notifications for each dmod that it 268 * noticed. If, for example, the debugger is unloaded before the first 269 * start, it won't have noticed any of the dmods we loaded. We'll need 270 * to initiate the unloads ourselves. 271 */ 272 kctl_dmod_unload_all(); 273 274 kctl.kctl_wr_state = KCTL_WR_ST_STOPPED; 275 276 /* 277 * Must be last, as it concludes by setting state to INACTIVE. The 278 * kctl data structure must not be accessed by this thread after that 279 * point. 280 */ 281 kctl_cleanup(); 282 283 mutex_enter(&cprlock); 284 CALLB_CPR_EXIT(&cprinfo); 285 mutex_destroy(&cprlock); 286 } 287 288 void 289 kctl_wr_thr_start(void) 290 { 291 kctl.kctl_wr_avail = 0; 292 kctl.kctl_wr_state = KCTL_WR_ST_RUN; 293 kctl.kctl_wr_thr = thread_create(NULL, 0, kctl_wr_thread, NULL, 0, &p0, 294 TS_RUN, minclsyspri); 295 } 296 297 void 298 kctl_wr_thr_stop(void) 299 { 300 ASSERT(kctl.kctl_wr_state == KCTL_WR_ST_RUN); 301 kctl.kctl_wr_state = KCTL_WR_ST_STOP; 302 sema_v(&kctl.kctl_wr_avail_sem); 303 } 304 305 void 306 kctl_wr_thr_join(void) 307 { 308 thread_join(kctl.kctl_wr_thr->t_did); 309 } 310