xref: /titanic_41/usr/src/uts/intel/dtrace/fbt.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 			/*
236 			 * Anything beginning with "kdi_" is a part of the
237 			 * kernel debugger interface and may be called in
238 			 * arbitrary context -- including probe context.
239 			 */
240 			continue;
241 		}
242 
243 		/*
244 		 * Due to 4524008, _init and _fini may have a bloated st_size.
245 		 * While this bug was fixed quite some time ago, old drivers
246 		 * may be lurking.  We need to develop a better solution to
247 		 * this problem, such that correct _init and _fini functions
248 		 * (the vast majority) may be correctly traced.  One solution
249 		 * may be to scan through the entire symbol table to see if
250 		 * any symbol overlaps with _init.  If none does, set a bit in
251 		 * the module structure that this module has correct _init and
252 		 * _fini sizes.  This will cause some pain the first time a
253 		 * module is scanned, but at least it would be O(N) instead of
254 		 * O(N log N)...
255 		 */
256 		if (strcmp(name, "_init") == 0)
257 			continue;
258 
259 		if (strcmp(name, "_fini") == 0)
260 			continue;
261 
262 		/*
263 		 * In order to be eligible, the function must begin with the
264 		 * following sequence:
265 		 *
266 		 * 	pushl	%esp
267 		 *	movl	%esp, %ebp
268 		 *
269 		 * Note that there are two variants of encodings that generate
270 		 * the movl; we must check for both.  For 64-bit, we would
271 		 * normally insist that a function begin with the following
272 		 * sequence:
273 		 *
274 		 *	pushq	%rbp
275 		 *	movq	%rsp, %rbp
276 		 *
277 		 * However, the compiler for 64-bit often splits these two
278 		 * instructions -- and the first instruction in the function
279 		 * is often not the pushq.  As a result, on 64-bit we look
280 		 * for any "pushq %rbp" in the function and we instrument
281 		 * this with a breakpoint instruction.
282 		 */
283 		instr = (uint8_t *)sym->st_value;
284 		limit = (uint8_t *)(sym->st_value + sym->st_size);
285 
286 #ifdef __amd64
287 		while (instr < limit) {
288 			if (*instr == FBT_PUSHL_EBP)
289 				break;
290 
291 			if ((size = dtrace_instr_size(instr)) <= 0)
292 				break;
293 
294 			instr += size;
295 		}
296 
297 		if (instr >= limit || *instr != FBT_PUSHL_EBP) {
298 			/*
299 			 * We either don't save the frame pointer in this
300 			 * function, or we ran into some disassembly
301 			 * screw-up.  Either way, we bail.
302 			 */
303 			continue;
304 		}
305 #else
306 		if (instr[0] != FBT_PUSHL_EBP)
307 			continue;
308 
309 		if (!(instr[1] == FBT_MOVL_ESP_EBP0_V0 &&
310 		    instr[2] == FBT_MOVL_ESP_EBP1_V0) &&
311 		    !(instr[1] == FBT_MOVL_ESP_EBP0_V1 &&
312 		    instr[2] == FBT_MOVL_ESP_EBP1_V1))
313 			continue;
314 #endif
315 
316 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
317 		fbt->fbtp_name = name;
318 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
319 		    name, FBT_ENTRY, 3, fbt);
320 		fbt->fbtp_patchpoint = instr;
321 		fbt->fbtp_ctl = ctl;
322 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
323 		fbt->fbtp_rval = DTRACE_INVOP_PUSHL_EBP;
324 		fbt->fbtp_savedval = *instr;
325 		fbt->fbtp_patchval = FBT_PATCHVAL;
326 
327 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
328 		fbt->fbtp_symndx = i;
329 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
330 
331 		mp->fbt_nentries++;
332 
333 		retfbt = NULL;
334 again:
335 		if (instr >= limit)
336 			continue;
337 
338 		/*
339 		 * If this disassembly fails, then we've likely walked off into
340 		 * a jump table or some other unsuitable area.  Bail out of the
341 		 * disassembly now.
342 		 */
343 		if ((size = dtrace_instr_size(instr)) <= 0)
344 			continue;
345 
346 #ifdef __amd64
347 		/*
348 		 * We only instrument "ret" on amd64 -- we don't yet instrument
349 		 * ret imm16, largely because the compiler doesn't seem to
350 		 * (yet) emit them in the kernel...
351 		 */
352 		if (*instr != FBT_RET) {
353 			instr += size;
354 			goto again;
355 		}
356 #else
357 		if (!(size == 1 &&
358 		    (*instr == FBT_POPL_EBP || *instr == FBT_LEAVE) &&
359 		    (*(instr + 1) == FBT_RET ||
360 		    *(instr + 1) == FBT_RET_IMM16))) {
361 			instr += size;
362 			goto again;
363 		}
364 #endif
365 
366 		/*
367 		 * We have a winner!
368 		 */
369 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
370 		fbt->fbtp_name = name;
371 
372 		if (retfbt == NULL) {
373 			fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
374 			    name, FBT_RETURN, 3, fbt);
375 		} else {
376 			retfbt->fbtp_next = fbt;
377 			fbt->fbtp_id = retfbt->fbtp_id;
378 		}
379 
380 		retfbt = fbt;
381 		fbt->fbtp_patchpoint = instr;
382 		fbt->fbtp_ctl = ctl;
383 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
384 
385 #ifndef __amd64
386 		if (*instr == FBT_POPL_EBP) {
387 			fbt->fbtp_rval = DTRACE_INVOP_POPL_EBP;
388 		} else {
389 			ASSERT(*instr == FBT_LEAVE);
390 			fbt->fbtp_rval = DTRACE_INVOP_LEAVE;
391 		}
392 		fbt->fbtp_roffset =
393 		    (uintptr_t)(instr - (uint8_t *)sym->st_value) + 1;
394 
395 #else
396 		ASSERT(*instr == FBT_RET);
397 		fbt->fbtp_rval = DTRACE_INVOP_RET;
398 		fbt->fbtp_roffset =
399 		    (uintptr_t)(instr - (uint8_t *)sym->st_value);
400 #endif
401 
402 		fbt->fbtp_savedval = *instr;
403 		fbt->fbtp_patchval = FBT_PATCHVAL;
404 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
405 		fbt->fbtp_symndx = i;
406 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
407 
408 		mp->fbt_nentries++;
409 
410 		instr += size;
411 		goto again;
412 	}
413 }
414 
415 /*ARGSUSED*/
416 static void
417 fbt_destroy(void *arg, dtrace_id_t id, void *parg)
418 {
419 	fbt_probe_t *fbt = parg, *next, *hash, *last;
420 	struct modctl *ctl = fbt->fbtp_ctl;
421 	int ndx;
422 
423 	do {
424 		if (ctl != NULL && ctl->mod_loadcnt == fbt->fbtp_loadcnt) {
425 			if ((ctl->mod_loadcnt == fbt->fbtp_loadcnt &&
426 			    ctl->mod_loaded)) {
427 				((struct module *)
428 				    (ctl->mod_mp))->fbt_nentries--;
429 			}
430 		}
431 
432 		/*
433 		 * Now we need to remove this probe from the fbt_probetab.
434 		 */
435 		ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
436 		last = NULL;
437 		hash = fbt_probetab[ndx];
438 
439 		while (hash != fbt) {
440 			ASSERT(hash != NULL);
441 			last = hash;
442 			hash = hash->fbtp_hashnext;
443 		}
444 
445 		if (last != NULL) {
446 			last->fbtp_hashnext = fbt->fbtp_hashnext;
447 		} else {
448 			fbt_probetab[ndx] = fbt->fbtp_hashnext;
449 		}
450 
451 		next = fbt->fbtp_next;
452 		kmem_free(fbt, sizeof (fbt_probe_t));
453 
454 		fbt = next;
455 	} while (fbt != NULL);
456 }
457 
458 /*ARGSUSED*/
459 static void
460 fbt_enable(void *arg, dtrace_id_t id, void *parg)
461 {
462 	fbt_probe_t *fbt = parg;
463 	struct modctl *ctl = fbt->fbtp_ctl;
464 
465 	ctl->mod_nenabled++;
466 
467 	if (!ctl->mod_loaded) {
468 		if (fbt_verbose) {
469 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
470 			    "(module %s unloaded)",
471 			    fbt->fbtp_name, ctl->mod_modname);
472 		}
473 
474 		return;
475 	}
476 
477 	/*
478 	 * Now check that our modctl has the expected load count.  If it
479 	 * doesn't, this module must have been unloaded and reloaded -- and
480 	 * we're not going to touch it.
481 	 */
482 	if (ctl->mod_loadcnt != fbt->fbtp_loadcnt) {
483 		if (fbt_verbose) {
484 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
485 			    "(module %s reloaded)",
486 			    fbt->fbtp_name, ctl->mod_modname);
487 		}
488 
489 		return;
490 	}
491 
492 	for (; fbt != NULL; fbt = fbt->fbtp_next)
493 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
494 }
495 
496 /*ARGSUSED*/
497 static void
498 fbt_disable(void *arg, dtrace_id_t id, void *parg)
499 {
500 	fbt_probe_t *fbt = parg;
501 	struct modctl *ctl = fbt->fbtp_ctl;
502 
503 	ASSERT(ctl->mod_nenabled > 0);
504 	ctl->mod_nenabled--;
505 
506 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
507 		return;
508 
509 	for (; fbt != NULL; fbt = fbt->fbtp_next)
510 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
511 }
512 
513 /*ARGSUSED*/
514 static void
515 fbt_suspend(void *arg, dtrace_id_t id, void *parg)
516 {
517 	fbt_probe_t *fbt = parg;
518 	struct modctl *ctl = fbt->fbtp_ctl;
519 
520 	ASSERT(ctl->mod_nenabled > 0);
521 
522 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
523 		return;
524 
525 	for (; fbt != NULL; fbt = fbt->fbtp_next)
526 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
527 }
528 
529 /*ARGSUSED*/
530 static void
531 fbt_resume(void *arg, dtrace_id_t id, void *parg)
532 {
533 	fbt_probe_t *fbt = parg;
534 	struct modctl *ctl = fbt->fbtp_ctl;
535 
536 	ASSERT(ctl->mod_nenabled > 0);
537 
538 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
539 		return;
540 
541 	for (; fbt != NULL; fbt = fbt->fbtp_next)
542 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
543 }
544 
545 /*ARGSUSED*/
546 static void
547 fbt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
548 {
549 	fbt_probe_t *fbt = parg;
550 	struct modctl *ctl = fbt->fbtp_ctl;
551 	struct module *mp = ctl->mod_mp;
552 	ctf_file_t *fp = NULL, *pfp;
553 	ctf_funcinfo_t f;
554 	int error;
555 	ctf_id_t argv[32], type;
556 	int argc = sizeof (argv) / sizeof (ctf_id_t);
557 	const char *parent;
558 
559 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
560 		goto err;
561 
562 	if (fbt->fbtp_roffset != 0 && desc->dtargd_ndx == 0) {
563 		(void) strcpy(desc->dtargd_native, "int");
564 		return;
565 	}
566 
567 	if ((fp = ctf_modopen(mp, &error)) == NULL) {
568 		/*
569 		 * We have no CTF information for this module -- and therefore
570 		 * no args[] information.
571 		 */
572 		goto err;
573 	}
574 
575 	/*
576 	 * If we have a parent container, we must manually import it.
577 	 */
578 	if ((parent = ctf_parent_name(fp)) != NULL) {
579 		struct modctl *mod;
580 
581 		/*
582 		 * We must iterate over all modules to find the module that
583 		 * is our parent.
584 		 */
585 		for (mod = &modules; mod != NULL; mod = mod->mod_next) {
586 			if (strcmp(mod->mod_filename, parent) == 0)
587 				break;
588 		}
589 
590 		if (mod == NULL)
591 			goto err;
592 
593 		if ((pfp = ctf_modopen(mod->mod_mp, &error)) == NULL)
594 			goto err;
595 
596 		if (ctf_import(fp, pfp) != 0) {
597 			ctf_close(pfp);
598 			goto err;
599 		}
600 
601 		ctf_close(pfp);
602 	}
603 
604 	if (ctf_func_info(fp, fbt->fbtp_symndx, &f) == CTF_ERR)
605 		goto err;
606 
607 	if (fbt->fbtp_roffset != 0) {
608 		if (desc->dtargd_ndx > 1)
609 			goto err;
610 
611 		ASSERT(desc->dtargd_ndx == 1);
612 		type = f.ctc_return;
613 	} else {
614 		if (desc->dtargd_ndx + 1 > f.ctc_argc)
615 			goto err;
616 
617 		if (ctf_func_args(fp, fbt->fbtp_symndx, argc, argv) == CTF_ERR)
618 			goto err;
619 
620 		type = argv[desc->dtargd_ndx];
621 	}
622 
623 	if (ctf_type_name(fp, type, desc->dtargd_native,
624 	    DTRACE_ARGTYPELEN) != NULL) {
625 		ctf_close(fp);
626 		return;
627 	}
628 err:
629 	if (fp != NULL)
630 		ctf_close(fp);
631 
632 	desc->dtargd_ndx = DTRACE_ARGNONE;
633 }
634 
635 static dtrace_pattr_t fbt_attr = {
636 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
637 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
638 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
639 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
640 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
641 };
642 
643 static dtrace_pops_t fbt_pops = {
644 	NULL,
645 	fbt_provide_module,
646 	fbt_enable,
647 	fbt_disable,
648 	fbt_suspend,
649 	fbt_resume,
650 	fbt_getargdesc,
651 	NULL,
652 	NULL,
653 	fbt_destroy
654 };
655 
656 static void
657 fbt_cleanup(dev_info_t *devi)
658 {
659 	dtrace_invop_remove(fbt_invop);
660 	ddi_remove_minor_node(devi, NULL);
661 	kmem_free(fbt_probetab, fbt_probetab_size * sizeof (fbt_probe_t *));
662 	fbt_probetab = NULL;
663 	fbt_probetab_mask = 0;
664 }
665 
666 static int
667 fbt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
668 {
669 	switch (cmd) {
670 	case DDI_ATTACH:
671 		break;
672 	case DDI_RESUME:
673 		return (DDI_SUCCESS);
674 	default:
675 		return (DDI_FAILURE);
676 	}
677 
678 	if (fbt_probetab_size == 0)
679 		fbt_probetab_size = FBT_PROBETAB_SIZE;
680 
681 	fbt_probetab_mask = fbt_probetab_size - 1;
682 	fbt_probetab =
683 	    kmem_zalloc(fbt_probetab_size * sizeof (fbt_probe_t *), KM_SLEEP);
684 
685 	dtrace_invop_add(fbt_invop);
686 
687 	if (ddi_create_minor_node(devi, "fbt", S_IFCHR, 0,
688 	    DDI_PSEUDO, NULL) == DDI_FAILURE ||
689 	    dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_KERNEL, 0,
690 	    &fbt_pops, NULL, &fbt_id) != 0) {
691 		fbt_cleanup(devi);
692 		return (DDI_FAILURE);
693 	}
694 
695 	ddi_report_dev(devi);
696 	fbt_devi = devi;
697 
698 	return (DDI_SUCCESS);
699 }
700 
701 static int
702 fbt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
703 {
704 	switch (cmd) {
705 	case DDI_DETACH:
706 		break;
707 	case DDI_SUSPEND:
708 		return (DDI_SUCCESS);
709 	default:
710 		return (DDI_FAILURE);
711 	}
712 
713 	if (dtrace_unregister(fbt_id) != 0)
714 		return (DDI_FAILURE);
715 
716 	fbt_cleanup(devi);
717 
718 	return (DDI_SUCCESS);
719 }
720 
721 /*ARGSUSED*/
722 static int
723 fbt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
724 {
725 	int error;
726 
727 	switch (infocmd) {
728 	case DDI_INFO_DEVT2DEVINFO:
729 		*result = (void *)fbt_devi;
730 		error = DDI_SUCCESS;
731 		break;
732 	case DDI_INFO_DEVT2INSTANCE:
733 		*result = (void *)0;
734 		error = DDI_SUCCESS;
735 		break;
736 	default:
737 		error = DDI_FAILURE;
738 	}
739 	return (error);
740 }
741 
742 /*ARGSUSED*/
743 static int
744 fbt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
745 {
746 	return (0);
747 }
748 
749 static struct cb_ops fbt_cb_ops = {
750 	fbt_open,		/* open */
751 	nodev,			/* close */
752 	nulldev,		/* strategy */
753 	nulldev,		/* print */
754 	nodev,			/* dump */
755 	nodev,			/* read */
756 	nodev,			/* write */
757 	nodev,			/* ioctl */
758 	nodev,			/* devmap */
759 	nodev,			/* mmap */
760 	nodev,			/* segmap */
761 	nochpoll,		/* poll */
762 	ddi_prop_op,		/* cb_prop_op */
763 	0,			/* streamtab  */
764 	D_NEW | D_MP		/* Driver compatibility flag */
765 };
766 
767 static struct dev_ops fbt_ops = {
768 	DEVO_REV,		/* devo_rev */
769 	0,			/* refcnt */
770 	fbt_info,		/* get_dev_info */
771 	nulldev,		/* identify */
772 	nulldev,		/* probe */
773 	fbt_attach,		/* attach */
774 	fbt_detach,		/* detach */
775 	nodev,			/* reset */
776 	&fbt_cb_ops,		/* driver operations */
777 	NULL,			/* bus operations */
778 	nodev			/* dev power */
779 };
780 
781 /*
782  * Module linkage information for the kernel.
783  */
784 static struct modldrv modldrv = {
785 	&mod_driverops,		/* module type (this is a pseudo driver) */
786 	"Function Boundary Tracing",	/* name of module */
787 	&fbt_ops,		/* driver ops */
788 };
789 
790 static struct modlinkage modlinkage = {
791 	MODREV_1,
792 	(void *)&modldrv,
793 	NULL
794 };
795 
796 int
797 _init(void)
798 {
799 	return (mod_install(&modlinkage));
800 }
801 
802 int
803 _info(struct modinfo *modinfop)
804 {
805 	return (mod_info(&modlinkage, modinfop));
806 }
807 
808 int
809 _fini(void)
810 {
811 	return (mod_remove(&modlinkage));
812 }
813