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