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