xref: /titanic_50/usr/src/uts/intel/dtrace/fbt.c (revision 36e5aa2ab5c51d4747a2470e41ccb782056c90e7)
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  */
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 		int j;
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 (desperately) want to avoid erroneously instrumenting a
370 		 * jump table, especially given that our markers are pretty
371 		 * short:  two bytes on x86, and just one byte on amd64.  To
372 		 * determine if we're looking at a true instruction sequence
373 		 * or an inline jump table that happens to contain the same
374 		 * byte sequences, we resort to some heuristic sleeze:  we
375 		 * treat this instruction as being contained within a pointer,
376 		 * and see if that pointer points to within the body of the
377 		 * function.  If it does, we refuse to instrument it.
378 		 */
379 		for (j = 0; j < sizeof (uintptr_t); j++) {
380 			uintptr_t check = (uintptr_t)instr - j;
381 			uint8_t *ptr;
382 
383 			if (check < sym->st_value)
384 				break;
385 
386 			if (check + sizeof (uintptr_t) > (uintptr_t)limit)
387 				continue;
388 
389 			ptr = *(uint8_t **)check;
390 
391 			if (ptr >= (uint8_t *)sym->st_value && ptr < limit) {
392 				instr += size;
393 				goto again;
394 			}
395 		}
396 
397 		/*
398 		 * We have a winner!
399 		 */
400 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
401 		fbt->fbtp_name = name;
402 
403 		if (retfbt == NULL) {
404 			fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
405 			    name, FBT_RETURN, 3, fbt);
406 		} else {
407 			retfbt->fbtp_next = fbt;
408 			fbt->fbtp_id = retfbt->fbtp_id;
409 		}
410 
411 		retfbt = fbt;
412 		fbt->fbtp_patchpoint = instr;
413 		fbt->fbtp_ctl = ctl;
414 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
415 
416 #ifndef __amd64
417 		if (*instr == FBT_POPL_EBP) {
418 			fbt->fbtp_rval = DTRACE_INVOP_POPL_EBP;
419 		} else {
420 			ASSERT(*instr == FBT_LEAVE);
421 			fbt->fbtp_rval = DTRACE_INVOP_LEAVE;
422 		}
423 		fbt->fbtp_roffset =
424 		    (uintptr_t)(instr - (uint8_t *)sym->st_value) + 1;
425 
426 #else
427 		ASSERT(*instr == FBT_RET);
428 		fbt->fbtp_rval = DTRACE_INVOP_RET;
429 		fbt->fbtp_roffset =
430 		    (uintptr_t)(instr - (uint8_t *)sym->st_value);
431 #endif
432 
433 		fbt->fbtp_savedval = *instr;
434 		fbt->fbtp_patchval = FBT_PATCHVAL;
435 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
436 		fbt->fbtp_symndx = i;
437 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
438 
439 		mp->fbt_nentries++;
440 
441 		instr += size;
442 		goto again;
443 	}
444 }
445 
446 /*ARGSUSED*/
447 static void
448 fbt_destroy(void *arg, dtrace_id_t id, void *parg)
449 {
450 	fbt_probe_t *fbt = parg, *next, *hash, *last;
451 	struct modctl *ctl = fbt->fbtp_ctl;
452 	int ndx;
453 
454 	do {
455 		if (ctl != NULL && ctl->mod_loadcnt == fbt->fbtp_loadcnt) {
456 			if ((ctl->mod_loadcnt == fbt->fbtp_loadcnt &&
457 			    ctl->mod_loaded)) {
458 				((struct module *)
459 				    (ctl->mod_mp))->fbt_nentries--;
460 			}
461 		}
462 
463 		/*
464 		 * Now we need to remove this probe from the fbt_probetab.
465 		 */
466 		ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
467 		last = NULL;
468 		hash = fbt_probetab[ndx];
469 
470 		while (hash != fbt) {
471 			ASSERT(hash != NULL);
472 			last = hash;
473 			hash = hash->fbtp_hashnext;
474 		}
475 
476 		if (last != NULL) {
477 			last->fbtp_hashnext = fbt->fbtp_hashnext;
478 		} else {
479 			fbt_probetab[ndx] = fbt->fbtp_hashnext;
480 		}
481 
482 		next = fbt->fbtp_next;
483 		kmem_free(fbt, sizeof (fbt_probe_t));
484 
485 		fbt = next;
486 	} while (fbt != NULL);
487 }
488 
489 /*ARGSUSED*/
490 static void
491 fbt_enable(void *arg, dtrace_id_t id, void *parg)
492 {
493 	fbt_probe_t *fbt = parg;
494 	struct modctl *ctl = fbt->fbtp_ctl;
495 
496 	ctl->mod_nenabled++;
497 
498 	if (!ctl->mod_loaded) {
499 		if (fbt_verbose) {
500 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
501 			    "(module %s unloaded)",
502 			    fbt->fbtp_name, ctl->mod_modname);
503 		}
504 
505 		return;
506 	}
507 
508 	/*
509 	 * Now check that our modctl has the expected load count.  If it
510 	 * doesn't, this module must have been unloaded and reloaded -- and
511 	 * we're not going to touch it.
512 	 */
513 	if (ctl->mod_loadcnt != fbt->fbtp_loadcnt) {
514 		if (fbt_verbose) {
515 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
516 			    "(module %s reloaded)",
517 			    fbt->fbtp_name, ctl->mod_modname);
518 		}
519 
520 		return;
521 	}
522 
523 	for (; fbt != NULL; fbt = fbt->fbtp_next)
524 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
525 }
526 
527 /*ARGSUSED*/
528 static void
529 fbt_disable(void *arg, dtrace_id_t id, void *parg)
530 {
531 	fbt_probe_t *fbt = parg;
532 	struct modctl *ctl = fbt->fbtp_ctl;
533 
534 	ASSERT(ctl->mod_nenabled > 0);
535 	ctl->mod_nenabled--;
536 
537 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
538 		return;
539 
540 	for (; fbt != NULL; fbt = fbt->fbtp_next)
541 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
542 }
543 
544 /*ARGSUSED*/
545 static void
546 fbt_suspend(void *arg, dtrace_id_t id, void *parg)
547 {
548 	fbt_probe_t *fbt = parg;
549 	struct modctl *ctl = fbt->fbtp_ctl;
550 
551 	ASSERT(ctl->mod_nenabled > 0);
552 
553 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
554 		return;
555 
556 	for (; fbt != NULL; fbt = fbt->fbtp_next)
557 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
558 }
559 
560 /*ARGSUSED*/
561 static void
562 fbt_resume(void *arg, dtrace_id_t id, void *parg)
563 {
564 	fbt_probe_t *fbt = parg;
565 	struct modctl *ctl = fbt->fbtp_ctl;
566 
567 	ASSERT(ctl->mod_nenabled > 0);
568 
569 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
570 		return;
571 
572 	for (; fbt != NULL; fbt = fbt->fbtp_next)
573 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
574 }
575 
576 /*ARGSUSED*/
577 static void
578 fbt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
579 {
580 	fbt_probe_t *fbt = parg;
581 	struct modctl *ctl = fbt->fbtp_ctl;
582 	struct module *mp = ctl->mod_mp;
583 	ctf_file_t *fp = NULL, *pfp;
584 	ctf_funcinfo_t f;
585 	int error;
586 	ctf_id_t argv[32], type;
587 	int argc = sizeof (argv) / sizeof (ctf_id_t);
588 	const char *parent;
589 
590 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
591 		goto err;
592 
593 	if (fbt->fbtp_roffset != 0 && desc->dtargd_ndx == 0) {
594 		(void) strcpy(desc->dtargd_native, "int");
595 		return;
596 	}
597 
598 	if ((fp = ctf_modopen(mp, &error)) == NULL) {
599 		/*
600 		 * We have no CTF information for this module -- and therefore
601 		 * no args[] information.
602 		 */
603 		goto err;
604 	}
605 
606 	/*
607 	 * If we have a parent container, we must manually import it.
608 	 */
609 	if ((parent = ctf_parent_name(fp)) != NULL) {
610 		struct modctl *mp = &modules;
611 		struct modctl *mod = NULL;
612 
613 		/*
614 		 * We must iterate over all modules to find the module that
615 		 * is our parent.
616 		 */
617 		do {
618 			if (strcmp(mp->mod_modname, parent) == 0) {
619 				mod = mp;
620 				break;
621 			}
622 		} while ((mp = mp->mod_next) != &modules);
623 
624 		if (mod == NULL)
625 			goto err;
626 
627 		if ((pfp = ctf_modopen(mod->mod_mp, &error)) == NULL) {
628 			goto err;
629 		}
630 
631 		if (ctf_import(fp, pfp) != 0) {
632 			ctf_close(pfp);
633 			goto err;
634 		}
635 
636 		ctf_close(pfp);
637 	}
638 
639 	if (ctf_func_info(fp, fbt->fbtp_symndx, &f) == CTF_ERR)
640 		goto err;
641 
642 	if (fbt->fbtp_roffset != 0) {
643 		if (desc->dtargd_ndx > 1)
644 			goto err;
645 
646 		ASSERT(desc->dtargd_ndx == 1);
647 		type = f.ctc_return;
648 	} else {
649 		if (desc->dtargd_ndx + 1 > f.ctc_argc)
650 			goto err;
651 
652 		if (ctf_func_args(fp, fbt->fbtp_symndx, argc, argv) == CTF_ERR)
653 			goto err;
654 
655 		type = argv[desc->dtargd_ndx];
656 	}
657 
658 	if (ctf_type_name(fp, type, desc->dtargd_native,
659 	    DTRACE_ARGTYPELEN) != NULL) {
660 		ctf_close(fp);
661 		return;
662 	}
663 err:
664 	if (fp != NULL)
665 		ctf_close(fp);
666 
667 	desc->dtargd_ndx = DTRACE_ARGNONE;
668 }
669 
670 static dtrace_pattr_t fbt_attr = {
671 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
672 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
673 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
674 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
675 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
676 };
677 
678 static dtrace_pops_t fbt_pops = {
679 	NULL,
680 	fbt_provide_module,
681 	fbt_enable,
682 	fbt_disable,
683 	fbt_suspend,
684 	fbt_resume,
685 	fbt_getargdesc,
686 	NULL,
687 	NULL,
688 	fbt_destroy
689 };
690 
691 static void
692 fbt_cleanup(dev_info_t *devi)
693 {
694 	dtrace_invop_remove(fbt_invop);
695 	ddi_remove_minor_node(devi, NULL);
696 	kmem_free(fbt_probetab, fbt_probetab_size * sizeof (fbt_probe_t *));
697 	fbt_probetab = NULL;
698 	fbt_probetab_mask = 0;
699 }
700 
701 static int
702 fbt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
703 {
704 	switch (cmd) {
705 	case DDI_ATTACH:
706 		break;
707 	case DDI_RESUME:
708 		return (DDI_SUCCESS);
709 	default:
710 		return (DDI_FAILURE);
711 	}
712 
713 	if (fbt_probetab_size == 0)
714 		fbt_probetab_size = FBT_PROBETAB_SIZE;
715 
716 	fbt_probetab_mask = fbt_probetab_size - 1;
717 	fbt_probetab =
718 	    kmem_zalloc(fbt_probetab_size * sizeof (fbt_probe_t *), KM_SLEEP);
719 
720 	dtrace_invop_add(fbt_invop);
721 
722 	if (ddi_create_minor_node(devi, "fbt", S_IFCHR, 0,
723 	    DDI_PSEUDO, NULL) == DDI_FAILURE ||
724 	    dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_KERNEL, NULL,
725 	    &fbt_pops, NULL, &fbt_id) != 0) {
726 		fbt_cleanup(devi);
727 		return (DDI_FAILURE);
728 	}
729 
730 	ddi_report_dev(devi);
731 	fbt_devi = devi;
732 
733 	return (DDI_SUCCESS);
734 }
735 
736 static int
737 fbt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
738 {
739 	switch (cmd) {
740 	case DDI_DETACH:
741 		break;
742 	case DDI_SUSPEND:
743 		return (DDI_SUCCESS);
744 	default:
745 		return (DDI_FAILURE);
746 	}
747 
748 	if (dtrace_unregister(fbt_id) != 0)
749 		return (DDI_FAILURE);
750 
751 	fbt_cleanup(devi);
752 
753 	return (DDI_SUCCESS);
754 }
755 
756 /*ARGSUSED*/
757 static int
758 fbt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
759 {
760 	int error;
761 
762 	switch (infocmd) {
763 	case DDI_INFO_DEVT2DEVINFO:
764 		*result = (void *)fbt_devi;
765 		error = DDI_SUCCESS;
766 		break;
767 	case DDI_INFO_DEVT2INSTANCE:
768 		*result = (void *)0;
769 		error = DDI_SUCCESS;
770 		break;
771 	default:
772 		error = DDI_FAILURE;
773 	}
774 	return (error);
775 }
776 
777 /*ARGSUSED*/
778 static int
779 fbt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
780 {
781 	return (0);
782 }
783 
784 static struct cb_ops fbt_cb_ops = {
785 	fbt_open,		/* open */
786 	nodev,			/* close */
787 	nulldev,		/* strategy */
788 	nulldev,		/* print */
789 	nodev,			/* dump */
790 	nodev,			/* read */
791 	nodev,			/* write */
792 	nodev,			/* ioctl */
793 	nodev,			/* devmap */
794 	nodev,			/* mmap */
795 	nodev,			/* segmap */
796 	nochpoll,		/* poll */
797 	ddi_prop_op,		/* cb_prop_op */
798 	0,			/* streamtab  */
799 	D_NEW | D_MP		/* Driver compatibility flag */
800 };
801 
802 static struct dev_ops fbt_ops = {
803 	DEVO_REV,		/* devo_rev */
804 	0,			/* refcnt */
805 	fbt_info,		/* get_dev_info */
806 	nulldev,		/* identify */
807 	nulldev,		/* probe */
808 	fbt_attach,		/* attach */
809 	fbt_detach,		/* detach */
810 	nodev,			/* reset */
811 	&fbt_cb_ops,		/* driver operations */
812 	NULL,			/* bus operations */
813 	nodev			/* dev power */
814 };
815 
816 /*
817  * Module linkage information for the kernel.
818  */
819 static struct modldrv modldrv = {
820 	&mod_driverops,		/* module type (this is a pseudo driver) */
821 	"Function Boundary Tracing",	/* name of module */
822 	&fbt_ops,		/* driver ops */
823 };
824 
825 static struct modlinkage modlinkage = {
826 	MODREV_1,
827 	(void *)&modldrv,
828 	NULL
829 };
830 
831 int
832 _init(void)
833 {
834 	return (mod_install(&modlinkage));
835 }
836 
837 int
838 _info(struct modinfo *modinfop)
839 {
840 	return (mod_info(&modlinkage, modinfop));
841 }
842 
843 int
844 _fini(void)
845 {
846 	return (mod_remove(&modlinkage));
847 }
848