xref: /titanic_41/usr/src/uts/intel/dtrace/fbt.c (revision 68ae785055658995f54cb50bc0a4e120c882f11b)
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 		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 *mod;
611 
612 		/*
613 		 * We must iterate over all modules to find the module that
614 		 * is our parent.
615 		 */
616 		for (mod = &modules; mod != NULL; mod = mod->mod_next) {
617 			if (strcmp(mod->mod_filename, parent) == 0)
618 				break;
619 		}
620 
621 		if (mod == NULL)
622 			goto err;
623 
624 		if ((pfp = ctf_modopen(mod->mod_mp, &error)) == NULL)
625 			goto err;
626 
627 		if (ctf_import(fp, pfp) != 0) {
628 			ctf_close(pfp);
629 			goto err;
630 		}
631 
632 		ctf_close(pfp);
633 	}
634 
635 	if (ctf_func_info(fp, fbt->fbtp_symndx, &f) == CTF_ERR)
636 		goto err;
637 
638 	if (fbt->fbtp_roffset != 0) {
639 		if (desc->dtargd_ndx > 1)
640 			goto err;
641 
642 		ASSERT(desc->dtargd_ndx == 1);
643 		type = f.ctc_return;
644 	} else {
645 		if (desc->dtargd_ndx + 1 > f.ctc_argc)
646 			goto err;
647 
648 		if (ctf_func_args(fp, fbt->fbtp_symndx, argc, argv) == CTF_ERR)
649 			goto err;
650 
651 		type = argv[desc->dtargd_ndx];
652 	}
653 
654 	if (ctf_type_name(fp, type, desc->dtargd_native,
655 	    DTRACE_ARGTYPELEN) != NULL) {
656 		ctf_close(fp);
657 		return;
658 	}
659 err:
660 	if (fp != NULL)
661 		ctf_close(fp);
662 
663 	desc->dtargd_ndx = DTRACE_ARGNONE;
664 }
665 
666 static dtrace_pattr_t fbt_attr = {
667 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
668 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
669 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
670 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
671 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
672 };
673 
674 static dtrace_pops_t fbt_pops = {
675 	NULL,
676 	fbt_provide_module,
677 	fbt_enable,
678 	fbt_disable,
679 	fbt_suspend,
680 	fbt_resume,
681 	fbt_getargdesc,
682 	NULL,
683 	NULL,
684 	fbt_destroy
685 };
686 
687 static void
688 fbt_cleanup(dev_info_t *devi)
689 {
690 	dtrace_invop_remove(fbt_invop);
691 	ddi_remove_minor_node(devi, NULL);
692 	kmem_free(fbt_probetab, fbt_probetab_size * sizeof (fbt_probe_t *));
693 	fbt_probetab = NULL;
694 	fbt_probetab_mask = 0;
695 }
696 
697 static int
698 fbt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
699 {
700 	switch (cmd) {
701 	case DDI_ATTACH:
702 		break;
703 	case DDI_RESUME:
704 		return (DDI_SUCCESS);
705 	default:
706 		return (DDI_FAILURE);
707 	}
708 
709 	if (fbt_probetab_size == 0)
710 		fbt_probetab_size = FBT_PROBETAB_SIZE;
711 
712 	fbt_probetab_mask = fbt_probetab_size - 1;
713 	fbt_probetab =
714 	    kmem_zalloc(fbt_probetab_size * sizeof (fbt_probe_t *), KM_SLEEP);
715 
716 	dtrace_invop_add(fbt_invop);
717 
718 	if (ddi_create_minor_node(devi, "fbt", S_IFCHR, 0,
719 	    DDI_PSEUDO, NULL) == DDI_FAILURE ||
720 	    dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_KERNEL, NULL,
721 	    &fbt_pops, NULL, &fbt_id) != 0) {
722 		fbt_cleanup(devi);
723 		return (DDI_FAILURE);
724 	}
725 
726 	ddi_report_dev(devi);
727 	fbt_devi = devi;
728 
729 	return (DDI_SUCCESS);
730 }
731 
732 static int
733 fbt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
734 {
735 	switch (cmd) {
736 	case DDI_DETACH:
737 		break;
738 	case DDI_SUSPEND:
739 		return (DDI_SUCCESS);
740 	default:
741 		return (DDI_FAILURE);
742 	}
743 
744 	if (dtrace_unregister(fbt_id) != 0)
745 		return (DDI_FAILURE);
746 
747 	fbt_cleanup(devi);
748 
749 	return (DDI_SUCCESS);
750 }
751 
752 /*ARGSUSED*/
753 static int
754 fbt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
755 {
756 	int error;
757 
758 	switch (infocmd) {
759 	case DDI_INFO_DEVT2DEVINFO:
760 		*result = (void *)fbt_devi;
761 		error = DDI_SUCCESS;
762 		break;
763 	case DDI_INFO_DEVT2INSTANCE:
764 		*result = (void *)0;
765 		error = DDI_SUCCESS;
766 		break;
767 	default:
768 		error = DDI_FAILURE;
769 	}
770 	return (error);
771 }
772 
773 /*ARGSUSED*/
774 static int
775 fbt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
776 {
777 	return (0);
778 }
779 
780 static struct cb_ops fbt_cb_ops = {
781 	fbt_open,		/* open */
782 	nodev,			/* close */
783 	nulldev,		/* strategy */
784 	nulldev,		/* print */
785 	nodev,			/* dump */
786 	nodev,			/* read */
787 	nodev,			/* write */
788 	nodev,			/* ioctl */
789 	nodev,			/* devmap */
790 	nodev,			/* mmap */
791 	nodev,			/* segmap */
792 	nochpoll,		/* poll */
793 	ddi_prop_op,		/* cb_prop_op */
794 	0,			/* streamtab  */
795 	D_NEW | D_MP		/* Driver compatibility flag */
796 };
797 
798 static struct dev_ops fbt_ops = {
799 	DEVO_REV,		/* devo_rev */
800 	0,			/* refcnt */
801 	fbt_info,		/* get_dev_info */
802 	nulldev,		/* identify */
803 	nulldev,		/* probe */
804 	fbt_attach,		/* attach */
805 	fbt_detach,		/* detach */
806 	nodev,			/* reset */
807 	&fbt_cb_ops,		/* driver operations */
808 	NULL,			/* bus operations */
809 	nodev			/* dev power */
810 };
811 
812 /*
813  * Module linkage information for the kernel.
814  */
815 static struct modldrv modldrv = {
816 	&mod_driverops,		/* module type (this is a pseudo driver) */
817 	"Function Boundary Tracing",	/* name of module */
818 	&fbt_ops,		/* driver ops */
819 };
820 
821 static struct modlinkage modlinkage = {
822 	MODREV_1,
823 	(void *)&modldrv,
824 	NULL
825 };
826 
827 int
828 _init(void)
829 {
830 	return (mod_install(&modlinkage));
831 }
832 
833 int
834 _info(struct modinfo *modinfop)
835 {
836 	return (mod_info(&modlinkage, modinfop));
837 }
838 
839 int
840 _fini(void)
841 {
842 	return (mod_remove(&modlinkage));
843 }
844