xref: /titanic_44/usr/src/uts/intel/dtrace/fbt.c (revision a1b5e537933659371285214eae1db2603e6364b4)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/modctl.h>
30 #include <sys/dtrace.h>
31 #include <sys/kobj.h>
32 #include <sys/stat.h>
33 #include <sys/ddi.h>
34 #include <sys/sunddi.h>
35 #include <sys/conf.h>
36 
37 #define	FBT_PUSHL_EBP		0x55
38 #define	FBT_MOVL_ESP_EBP0_V0	0x8b
39 #define	FBT_MOVL_ESP_EBP1_V0	0xec
40 #define	FBT_MOVL_ESP_EBP0_V1	0x89
41 #define	FBT_MOVL_ESP_EBP1_V1	0xe5
42 #define	FBT_REX_RSP_RBP		0x48
43 
44 #define	FBT_POPL_EBP		0x5d
45 #define	FBT_RET			0xc3
46 #define	FBT_RET_IMM16		0xc2
47 #define	FBT_LEAVE		0xc9
48 
49 #ifdef __amd64
50 #define	FBT_PATCHVAL		0xcc
51 #else
52 #define	FBT_PATCHVAL		0xf0
53 #endif
54 
55 #define	FBT_ENTRY	"entry"
56 #define	FBT_RETURN	"return"
57 #define	FBT_ADDR2NDX(addr)	((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
58 #define	FBT_PROBETAB_SIZE	0x8000		/* 32k entries -- 128K total */
59 
60 typedef struct fbt_probe {
61 	struct fbt_probe *fbtp_hashnext;
62 	uint8_t		*fbtp_patchpoint;
63 	int8_t		fbtp_rval;
64 	uint8_t		fbtp_patchval;
65 	uint8_t		fbtp_savedval;
66 	uintptr_t	fbtp_roffset;
67 	dtrace_id_t	fbtp_id;
68 	char		*fbtp_name;
69 	struct modctl	*fbtp_ctl;
70 	int		fbtp_loadcnt;
71 	int		fbtp_symndx;
72 	int		fbtp_primary;
73 	struct fbt_probe *fbtp_next;
74 } fbt_probe_t;
75 
76 static dev_info_t		*fbt_devi;
77 static dtrace_provider_id_t	fbt_id;
78 static fbt_probe_t		**fbt_probetab;
79 static int			fbt_probetab_size;
80 static int			fbt_probetab_mask;
81 static int			fbt_verbose = 0;
82 
83 static int
84 fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
85 {
86 	uintptr_t stack0, stack1, stack2, stack3, stack4;
87 	fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
88 
89 	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
90 		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
91 			if (fbt->fbtp_roffset == 0) {
92 				int i = 0;
93 				/*
94 				 * When accessing the arguments on the stack,
95 				 * we must protect against accessing beyond
96 				 * the stack.  We can safely set NOFAULT here
97 				 * -- we know that interrupts are already
98 				 * disabled.
99 				 */
100 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
101 				CPU->cpu_dtrace_caller = stack[i++];
102 #ifdef __amd64
103 				/*
104 				 * On amd64, stack[0] contains the dereferenced
105 				 * stack pointer, stack[1] contains savfp,
106 				 * stack[2] contains savpc.  We want to step
107 				 * over these entries.
108 				 */
109 				i += 2;
110 #endif
111 				stack0 = stack[i++];
112 				stack1 = stack[i++];
113 				stack2 = stack[i++];
114 				stack3 = stack[i++];
115 				stack4 = stack[i++];
116 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
117 				    CPU_DTRACE_BADADDR);
118 
119 				dtrace_probe(fbt->fbtp_id, stack0, stack1,
120 				    stack2, stack3, stack4);
121 
122 				CPU->cpu_dtrace_caller = NULL;
123 			} else {
124 #ifdef __amd64
125 				/*
126 				 * On amd64, we instrument the ret, not the
127 				 * leave.  We therefore need to set the caller
128 				 * to assure that the top frame of a stack()
129 				 * action is correct.
130 				 */
131 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
132 				CPU->cpu_dtrace_caller = stack[0];
133 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
134 				    CPU_DTRACE_BADADDR);
135 #endif
136 
137 				dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset,
138 				    rval, 0, 0, 0);
139 				CPU->cpu_dtrace_caller = NULL;
140 			}
141 
142 			return (fbt->fbtp_rval);
143 		}
144 	}
145 
146 	return (0);
147 }
148 
149 /*ARGSUSED*/
150 static void
151 fbt_provide_module(void *arg, struct modctl *ctl)
152 {
153 	struct module *mp = ctl->mod_mp;
154 	char *str = mp->strings;
155 	int nsyms = mp->nsyms;
156 	Shdr *symhdr = mp->symhdr;
157 	char *modname = ctl->mod_modname;
158 	char *name;
159 	fbt_probe_t *fbt, *retfbt;
160 	size_t symsize;
161 	int i, size;
162 
163 	/*
164 	 * Employees of dtrace and their families are ineligible.  Void
165 	 * where prohibited.
166 	 */
167 	if (strcmp(modname, "dtrace") == 0)
168 		return;
169 
170 	if (ctl->mod_requisites != NULL) {
171 		struct modctl_list *list;
172 
173 		list = (struct modctl_list *)ctl->mod_requisites;
174 
175 		for (; list != NULL; list = list->modl_next) {
176 			if (strcmp(list->modl_modp->mod_modname, "dtrace") == 0)
177 				return;
178 		}
179 	}
180 
181 	/*
182 	 * KMDB is ineligible for instrumentation -- it may execute in
183 	 * any context, including probe context.
184 	 */
185 	if (strcmp(modname, "kmdbmod") == 0)
186 		return;
187 
188 	if (str == NULL || symhdr == NULL || symhdr->sh_addr == NULL) {
189 		/*
190 		 * If this module doesn't (yet) have its string or symbol
191 		 * table allocated, clear out.
192 		 */
193 		return;
194 	}
195 
196 	symsize = symhdr->sh_entsize;
197 
198 	if (mp->fbt_nentries) {
199 		/*
200 		 * This module has some FBT entries allocated; we're afraid
201 		 * to screw with it.
202 		 */
203 		return;
204 	}
205 
206 	for (i = 1; i < nsyms; i++) {
207 		uint8_t *instr, *limit;
208 		Sym *sym = (Sym *)(symhdr->sh_addr + i * symsize);
209 
210 		if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
211 			continue;
212 
213 		/*
214 		 * Weak symbols are not candidates.  This could be made to
215 		 * work (where weak functions and their underlying function
216 		 * appear as two disjoint probes), but it's not simple.
217 		 */
218 		if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
219 			continue;
220 
221 		name = str + sym->st_name;
222 
223 		if (strstr(name, "dtrace_") == name &&
224 		    strstr(name, "dtrace_safe_") != name) {
225 			/*
226 			 * Anything beginning with "dtrace_" may be called
227 			 * from probe context unless it explitly indicates
228 			 * that it won't be called from probe context by
229 			 * using the prefix "dtrace_safe_".
230 			 */
231 			continue;
232 		}
233 
234 		if (strstr(name, "kdi_") == name ||
235 		    strstr(name, "_kdi_") != NULL) {
236 			/*
237 			 * Any function name beginning with "kdi_" or
238 			 * containing the string "_kdi_" is a part of the
239 			 * kernel debugger interface and may be called in
240 			 * arbitrary context -- including probe context.
241 			 */
242 			continue;
243 		}
244 
245 		/*
246 		 * Due to 4524008, _init and _fini may have a bloated st_size.
247 		 * While this bug was fixed quite some time ago, old drivers
248 		 * may be lurking.  We need to develop a better solution to
249 		 * this problem, such that correct _init and _fini functions
250 		 * (the vast majority) may be correctly traced.  One solution
251 		 * may be to scan through the entire symbol table to see if
252 		 * any symbol overlaps with _init.  If none does, set a bit in
253 		 * the module structure that this module has correct _init and
254 		 * _fini sizes.  This will cause some pain the first time a
255 		 * module is scanned, but at least it would be O(N) instead of
256 		 * O(N log N)...
257 		 */
258 		if (strcmp(name, "_init") == 0)
259 			continue;
260 
261 		if (strcmp(name, "_fini") == 0)
262 			continue;
263 
264 		/*
265 		 * In order to be eligible, the function must begin with the
266 		 * following sequence:
267 		 *
268 		 * 	pushl	%esp
269 		 *	movl	%esp, %ebp
270 		 *
271 		 * Note that there are two variants of encodings that generate
272 		 * the movl; we must check for both.  For 64-bit, we would
273 		 * normally insist that a function begin with the following
274 		 * sequence:
275 		 *
276 		 *	pushq	%rbp
277 		 *	movq	%rsp, %rbp
278 		 *
279 		 * However, the compiler for 64-bit often splits these two
280 		 * instructions -- and the first instruction in the function
281 		 * is often not the pushq.  As a result, on 64-bit we look
282 		 * for any "pushq %rbp" in the function and we instrument
283 		 * this with a breakpoint instruction.
284 		 */
285 		instr = (uint8_t *)sym->st_value;
286 		limit = (uint8_t *)(sym->st_value + sym->st_size);
287 
288 #ifdef __amd64
289 		while (instr < limit) {
290 			if (*instr == FBT_PUSHL_EBP)
291 				break;
292 
293 			if ((size = dtrace_instr_size(instr)) <= 0)
294 				break;
295 
296 			instr += size;
297 		}
298 
299 		if (instr >= limit || *instr != FBT_PUSHL_EBP) {
300 			/*
301 			 * We either don't save the frame pointer in this
302 			 * function, or we ran into some disassembly
303 			 * screw-up.  Either way, we bail.
304 			 */
305 			continue;
306 		}
307 #else
308 		if (instr[0] != FBT_PUSHL_EBP)
309 			continue;
310 
311 		if (!(instr[1] == FBT_MOVL_ESP_EBP0_V0 &&
312 		    instr[2] == FBT_MOVL_ESP_EBP1_V0) &&
313 		    !(instr[1] == FBT_MOVL_ESP_EBP0_V1 &&
314 		    instr[2] == FBT_MOVL_ESP_EBP1_V1))
315 			continue;
316 #endif
317 
318 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
319 		fbt->fbtp_name = name;
320 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
321 		    name, FBT_ENTRY, 3, fbt);
322 		fbt->fbtp_patchpoint = instr;
323 		fbt->fbtp_ctl = ctl;
324 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
325 		fbt->fbtp_rval = DTRACE_INVOP_PUSHL_EBP;
326 		fbt->fbtp_savedval = *instr;
327 		fbt->fbtp_patchval = FBT_PATCHVAL;
328 
329 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
330 		fbt->fbtp_symndx = i;
331 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
332 
333 		mp->fbt_nentries++;
334 
335 		retfbt = NULL;
336 again:
337 		if (instr >= limit)
338 			continue;
339 
340 		/*
341 		 * If this disassembly fails, then we've likely walked off into
342 		 * a jump table or some other unsuitable area.  Bail out of the
343 		 * disassembly now.
344 		 */
345 		if ((size = dtrace_instr_size(instr)) <= 0)
346 			continue;
347 
348 #ifdef __amd64
349 		/*
350 		 * We only instrument "ret" on amd64 -- we don't yet instrument
351 		 * ret imm16, largely because the compiler doesn't seem to
352 		 * (yet) emit them in the kernel...
353 		 */
354 		if (*instr != FBT_RET) {
355 			instr += size;
356 			goto again;
357 		}
358 #else
359 		if (!(size == 1 &&
360 		    (*instr == FBT_POPL_EBP || *instr == FBT_LEAVE) &&
361 		    (*(instr + 1) == FBT_RET ||
362 		    *(instr + 1) == FBT_RET_IMM16))) {
363 			instr += size;
364 			goto again;
365 		}
366 #endif
367 
368 		/*
369 		 * We have a winner!
370 		 */
371 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
372 		fbt->fbtp_name = name;
373 
374 		if (retfbt == NULL) {
375 			fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
376 			    name, FBT_RETURN, 3, fbt);
377 		} else {
378 			retfbt->fbtp_next = fbt;
379 			fbt->fbtp_id = retfbt->fbtp_id;
380 		}
381 
382 		retfbt = fbt;
383 		fbt->fbtp_patchpoint = instr;
384 		fbt->fbtp_ctl = ctl;
385 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
386 
387 #ifndef __amd64
388 		if (*instr == FBT_POPL_EBP) {
389 			fbt->fbtp_rval = DTRACE_INVOP_POPL_EBP;
390 		} else {
391 			ASSERT(*instr == FBT_LEAVE);
392 			fbt->fbtp_rval = DTRACE_INVOP_LEAVE;
393 		}
394 		fbt->fbtp_roffset =
395 		    (uintptr_t)(instr - (uint8_t *)sym->st_value) + 1;
396 
397 #else
398 		ASSERT(*instr == FBT_RET);
399 		fbt->fbtp_rval = DTRACE_INVOP_RET;
400 		fbt->fbtp_roffset =
401 		    (uintptr_t)(instr - (uint8_t *)sym->st_value);
402 #endif
403 
404 		fbt->fbtp_savedval = *instr;
405 		fbt->fbtp_patchval = FBT_PATCHVAL;
406 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
407 		fbt->fbtp_symndx = i;
408 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
409 
410 		mp->fbt_nentries++;
411 
412 		instr += size;
413 		goto again;
414 	}
415 }
416 
417 /*ARGSUSED*/
418 static void
419 fbt_destroy(void *arg, dtrace_id_t id, void *parg)
420 {
421 	fbt_probe_t *fbt = parg, *next, *hash, *last;
422 	struct modctl *ctl = fbt->fbtp_ctl;
423 	int ndx;
424 
425 	do {
426 		if (ctl != NULL && ctl->mod_loadcnt == fbt->fbtp_loadcnt) {
427 			if ((ctl->mod_loadcnt == fbt->fbtp_loadcnt &&
428 			    ctl->mod_loaded)) {
429 				((struct module *)
430 				    (ctl->mod_mp))->fbt_nentries--;
431 			}
432 		}
433 
434 		/*
435 		 * Now we need to remove this probe from the fbt_probetab.
436 		 */
437 		ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
438 		last = NULL;
439 		hash = fbt_probetab[ndx];
440 
441 		while (hash != fbt) {
442 			ASSERT(hash != NULL);
443 			last = hash;
444 			hash = hash->fbtp_hashnext;
445 		}
446 
447 		if (last != NULL) {
448 			last->fbtp_hashnext = fbt->fbtp_hashnext;
449 		} else {
450 			fbt_probetab[ndx] = fbt->fbtp_hashnext;
451 		}
452 
453 		next = fbt->fbtp_next;
454 		kmem_free(fbt, sizeof (fbt_probe_t));
455 
456 		fbt = next;
457 	} while (fbt != NULL);
458 }
459 
460 /*ARGSUSED*/
461 static void
462 fbt_enable(void *arg, dtrace_id_t id, void *parg)
463 {
464 	fbt_probe_t *fbt = parg;
465 	struct modctl *ctl = fbt->fbtp_ctl;
466 
467 	ctl->mod_nenabled++;
468 
469 	if (!ctl->mod_loaded) {
470 		if (fbt_verbose) {
471 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
472 			    "(module %s unloaded)",
473 			    fbt->fbtp_name, ctl->mod_modname);
474 		}
475 
476 		return;
477 	}
478 
479 	/*
480 	 * Now check that our modctl has the expected load count.  If it
481 	 * doesn't, this module must have been unloaded and reloaded -- and
482 	 * we're not going to touch it.
483 	 */
484 	if (ctl->mod_loadcnt != fbt->fbtp_loadcnt) {
485 		if (fbt_verbose) {
486 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
487 			    "(module %s reloaded)",
488 			    fbt->fbtp_name, ctl->mod_modname);
489 		}
490 
491 		return;
492 	}
493 
494 	for (; fbt != NULL; fbt = fbt->fbtp_next)
495 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
496 }
497 
498 /*ARGSUSED*/
499 static void
500 fbt_disable(void *arg, dtrace_id_t id, void *parg)
501 {
502 	fbt_probe_t *fbt = parg;
503 	struct modctl *ctl = fbt->fbtp_ctl;
504 
505 	ASSERT(ctl->mod_nenabled > 0);
506 	ctl->mod_nenabled--;
507 
508 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
509 		return;
510 
511 	for (; fbt != NULL; fbt = fbt->fbtp_next)
512 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
513 }
514 
515 /*ARGSUSED*/
516 static void
517 fbt_suspend(void *arg, dtrace_id_t id, void *parg)
518 {
519 	fbt_probe_t *fbt = parg;
520 	struct modctl *ctl = fbt->fbtp_ctl;
521 
522 	ASSERT(ctl->mod_nenabled > 0);
523 
524 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
525 		return;
526 
527 	for (; fbt != NULL; fbt = fbt->fbtp_next)
528 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
529 }
530 
531 /*ARGSUSED*/
532 static void
533 fbt_resume(void *arg, dtrace_id_t id, void *parg)
534 {
535 	fbt_probe_t *fbt = parg;
536 	struct modctl *ctl = fbt->fbtp_ctl;
537 
538 	ASSERT(ctl->mod_nenabled > 0);
539 
540 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
541 		return;
542 
543 	for (; fbt != NULL; fbt = fbt->fbtp_next)
544 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
545 }
546 
547 /*ARGSUSED*/
548 static void
549 fbt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
550 {
551 	fbt_probe_t *fbt = parg;
552 	struct modctl *ctl = fbt->fbtp_ctl;
553 	struct module *mp = ctl->mod_mp;
554 	ctf_file_t *fp = NULL, *pfp;
555 	ctf_funcinfo_t f;
556 	int error;
557 	ctf_id_t argv[32], type;
558 	int argc = sizeof (argv) / sizeof (ctf_id_t);
559 	const char *parent;
560 
561 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
562 		goto err;
563 
564 	if (fbt->fbtp_roffset != 0 && desc->dtargd_ndx == 0) {
565 		(void) strcpy(desc->dtargd_native, "int");
566 		return;
567 	}
568 
569 	if ((fp = ctf_modopen(mp, &error)) == NULL) {
570 		/*
571 		 * We have no CTF information for this module -- and therefore
572 		 * no args[] information.
573 		 */
574 		goto err;
575 	}
576 
577 	/*
578 	 * If we have a parent container, we must manually import it.
579 	 */
580 	if ((parent = ctf_parent_name(fp)) != NULL) {
581 		struct modctl *mod;
582 
583 		/*
584 		 * We must iterate over all modules to find the module that
585 		 * is our parent.
586 		 */
587 		for (mod = &modules; mod != NULL; mod = mod->mod_next) {
588 			if (strcmp(mod->mod_filename, parent) == 0)
589 				break;
590 		}
591 
592 		if (mod == NULL)
593 			goto err;
594 
595 		if ((pfp = ctf_modopen(mod->mod_mp, &error)) == NULL)
596 			goto err;
597 
598 		if (ctf_import(fp, pfp) != 0) {
599 			ctf_close(pfp);
600 			goto err;
601 		}
602 
603 		ctf_close(pfp);
604 	}
605 
606 	if (ctf_func_info(fp, fbt->fbtp_symndx, &f) == CTF_ERR)
607 		goto err;
608 
609 	if (fbt->fbtp_roffset != 0) {
610 		if (desc->dtargd_ndx > 1)
611 			goto err;
612 
613 		ASSERT(desc->dtargd_ndx == 1);
614 		type = f.ctc_return;
615 	} else {
616 		if (desc->dtargd_ndx + 1 > f.ctc_argc)
617 			goto err;
618 
619 		if (ctf_func_args(fp, fbt->fbtp_symndx, argc, argv) == CTF_ERR)
620 			goto err;
621 
622 		type = argv[desc->dtargd_ndx];
623 	}
624 
625 	if (ctf_type_name(fp, type, desc->dtargd_native,
626 	    DTRACE_ARGTYPELEN) != NULL) {
627 		ctf_close(fp);
628 		return;
629 	}
630 err:
631 	if (fp != NULL)
632 		ctf_close(fp);
633 
634 	desc->dtargd_ndx = DTRACE_ARGNONE;
635 }
636 
637 static dtrace_pattr_t fbt_attr = {
638 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
639 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
640 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
641 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
642 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
643 };
644 
645 static dtrace_pops_t fbt_pops = {
646 	NULL,
647 	fbt_provide_module,
648 	fbt_enable,
649 	fbt_disable,
650 	fbt_suspend,
651 	fbt_resume,
652 	fbt_getargdesc,
653 	NULL,
654 	NULL,
655 	fbt_destroy
656 };
657 
658 static void
659 fbt_cleanup(dev_info_t *devi)
660 {
661 	dtrace_invop_remove(fbt_invop);
662 	ddi_remove_minor_node(devi, NULL);
663 	kmem_free(fbt_probetab, fbt_probetab_size * sizeof (fbt_probe_t *));
664 	fbt_probetab = NULL;
665 	fbt_probetab_mask = 0;
666 }
667 
668 static int
669 fbt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
670 {
671 	switch (cmd) {
672 	case DDI_ATTACH:
673 		break;
674 	case DDI_RESUME:
675 		return (DDI_SUCCESS);
676 	default:
677 		return (DDI_FAILURE);
678 	}
679 
680 	if (fbt_probetab_size == 0)
681 		fbt_probetab_size = FBT_PROBETAB_SIZE;
682 
683 	fbt_probetab_mask = fbt_probetab_size - 1;
684 	fbt_probetab =
685 	    kmem_zalloc(fbt_probetab_size * sizeof (fbt_probe_t *), KM_SLEEP);
686 
687 	dtrace_invop_add(fbt_invop);
688 
689 	if (ddi_create_minor_node(devi, "fbt", S_IFCHR, 0,
690 	    DDI_PSEUDO, NULL) == DDI_FAILURE ||
691 	    dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_KERNEL, 0,
692 	    &fbt_pops, NULL, &fbt_id) != 0) {
693 		fbt_cleanup(devi);
694 		return (DDI_FAILURE);
695 	}
696 
697 	ddi_report_dev(devi);
698 	fbt_devi = devi;
699 
700 	return (DDI_SUCCESS);
701 }
702 
703 static int
704 fbt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
705 {
706 	switch (cmd) {
707 	case DDI_DETACH:
708 		break;
709 	case DDI_SUSPEND:
710 		return (DDI_SUCCESS);
711 	default:
712 		return (DDI_FAILURE);
713 	}
714 
715 	if (dtrace_unregister(fbt_id) != 0)
716 		return (DDI_FAILURE);
717 
718 	fbt_cleanup(devi);
719 
720 	return (DDI_SUCCESS);
721 }
722 
723 /*ARGSUSED*/
724 static int
725 fbt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
726 {
727 	int error;
728 
729 	switch (infocmd) {
730 	case DDI_INFO_DEVT2DEVINFO:
731 		*result = (void *)fbt_devi;
732 		error = DDI_SUCCESS;
733 		break;
734 	case DDI_INFO_DEVT2INSTANCE:
735 		*result = (void *)0;
736 		error = DDI_SUCCESS;
737 		break;
738 	default:
739 		error = DDI_FAILURE;
740 	}
741 	return (error);
742 }
743 
744 /*ARGSUSED*/
745 static int
746 fbt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
747 {
748 	return (0);
749 }
750 
751 static struct cb_ops fbt_cb_ops = {
752 	fbt_open,		/* open */
753 	nodev,			/* close */
754 	nulldev,		/* strategy */
755 	nulldev,		/* print */
756 	nodev,			/* dump */
757 	nodev,			/* read */
758 	nodev,			/* write */
759 	nodev,			/* ioctl */
760 	nodev,			/* devmap */
761 	nodev,			/* mmap */
762 	nodev,			/* segmap */
763 	nochpoll,		/* poll */
764 	ddi_prop_op,		/* cb_prop_op */
765 	0,			/* streamtab  */
766 	D_NEW | D_MP		/* Driver compatibility flag */
767 };
768 
769 static struct dev_ops fbt_ops = {
770 	DEVO_REV,		/* devo_rev */
771 	0,			/* refcnt */
772 	fbt_info,		/* get_dev_info */
773 	nulldev,		/* identify */
774 	nulldev,		/* probe */
775 	fbt_attach,		/* attach */
776 	fbt_detach,		/* detach */
777 	nodev,			/* reset */
778 	&fbt_cb_ops,		/* driver operations */
779 	NULL,			/* bus operations */
780 	nodev			/* dev power */
781 };
782 
783 /*
784  * Module linkage information for the kernel.
785  */
786 static struct modldrv modldrv = {
787 	&mod_driverops,		/* module type (this is a pseudo driver) */
788 	"Function Boundary Tracing",	/* name of module */
789 	&fbt_ops,		/* driver ops */
790 };
791 
792 static struct modlinkage modlinkage = {
793 	MODREV_1,
794 	(void *)&modldrv,
795 	NULL
796 };
797 
798 int
799 _init(void)
800 {
801 	return (mod_install(&modlinkage));
802 }
803 
804 int
805 _info(struct modinfo *modinfop)
806 {
807 	return (mod_info(&modlinkage, modinfop));
808 }
809 
810 int
811 _fini(void)
812 {
813 	return (mod_remove(&modlinkage));
814 }
815