Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/tnf/tnf.c
+++ new/usr/src/uts/common/tnf/tnf.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26
27 27 /*
28 28 * tnf driver - provides probe control and kernel trace buffer access
29 29 * to the user programs prex and tnfxtract.
30 30 */
31 31
32 32 #include <sys/types.h>
33 33 #include <sys/param.h>
34 34 #include <sys/sysmacros.h>
35 35 #include <sys/file.h>
36 36 #include <sys/cmn_err.h>
37 37 #include <sys/fcntl.h>
38 38 #include <sys/uio.h>
39 39 #include <sys/kmem.h>
40 40 #include <sys/cred.h>
41 41 #include <sys/mman.h>
42 42 #include <sys/errno.h>
43 43 #include <sys/stat.h>
44 44 #include <sys/conf.h>
45 45 #include <sys/ddi.h>
46 46 #include <sys/sunddi.h>
47 47 #include <sys/modctl.h>
48 48 #include <sys/tnf.h>
49 49 #include <sys/debug.h>
50 50 #include <sys/devops.h>
51 51 #include <vm/as.h>
52 52 #include <vm/seg_kp.h>
53 53 #include <sys/tnf_probe.h>
54 54 #include <sys/kobj.h>
55 55
56 56 #include "tnf_buf.h"
57 57 #include "tnf_types.h"
58 58 #include "tnf_trace.h"
59 59
60 60 #ifndef NPROBE
61 61
62 62 /*
63 63 * Each probe is independently put in the kernel, prex uses
64 64 * __tnf_probe_list_head and __tnf_tag_list_head as pointers to linked list
65 65 * for probes and static tnf_tag_data_t, respectively.
66 66 * tnf used the elf relocation record to build a separate linked list for
67 67 * the probes and tnf_tag_data_t. We will describe how the linked list for
68 68 * __tnf_tag_list_head is made, the probe list is very similar.
69 69 * During the dynamic relocation(in uts/sparc/krtld/kobj_reloc.c),
70 70 * the &__tnf_tag_version_1(the first member in tnf_tag_data_t data struct)
71 71 * (and since it is a global variable which was never defined) will be filled
72 72 * with 0. The following code in kobj_reloc.c will get the address of current
73 73 * __tnf_tag_list_head and put it in value_p:
74 74 * #define TAG_MARKER_SYMBOL "__tnf_tag_version_1"
75 75 * if (strcmp(symname, TAG_MARKER_SYMBOL) == 0) {
76 76 * *addend_p = 0;
77 77 * *value_p = (Addr) __tnf_tag_list_head; (value_p points to list head)
78 78 * __tnf_tag_list_head = (void *)*offset_p;(list head is the next record)
79 79 * return (0);
80 80 * }
81 81 *
82 82 * the function do_reloc(in the kobj_reloc.c) will put vlaue_p into
83 83 * &__tnf_tag_version_1
84 84 * Now the &__tnf_tag_version_1 points to the last list head
85 85 * and __tnf_tag_list_head points to the new list head.
86 86 * This is equivalent to attatch a node at the beginning of the list.
87 87 *
88 88 */
89 89 extern tnf_probe_control_t *__tnf_probe_list_head;
90 90 extern tnf_tag_data_t *__tnf_tag_list_head;
91 91 extern int tnf_changed_probe_list;
92 92
93 93 static int tnf_attach(dev_info_t *, ddi_attach_cmd_t);
94 94 static int tnf_detach(dev_info_t *, ddi_detach_cmd_t);
95 95 static int tnf_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
96 96 static int tnf_open(dev_t *, int, int, struct cred *);
97 97 static int tnf_close(dev_t, int, int, struct cred *);
98 98 #ifdef UNUSED
99 99 static int tnf_mmap(dev_t, off_t, int);
100 100 #endif
101 101 static int tnf_ioctl(dev_t, int, intptr_t, int, struct cred *, int *);
102 102 #ifdef UNUSED
103 103 static int tnf_prop_op(dev_t, dev_info_t *, ddi_prop_op_t,
104 104 int, char *, caddr_t, int *);
105 105 #endif
106 106 static dev_info_t *tnf_devi;
107 107
108 108 static struct {
109 109 int tnf_probe_count;
110 110 boolean_t tnf_pidfilter_mode;
111 111 boolean_t ctldev_is_open;
112 112 int mapdev_open_count;
113 113 kmutex_t tnf_mtx;
114 114 } tnf_drv_state = { 0, B_FALSE, B_FALSE, 0 };
115 115
116 116 static int tnf_getmaxprobe(caddr_t, int);
117 117 static int tnf_getprobevals(caddr_t, int);
118 118 static int tnf_getprobestring(caddr_t, int);
119 119 static int tnf_setprobevals(caddr_t, int);
120 120 static int tnf_getstate(caddr_t, int);
121 121 static int tnf_allocbuf(intptr_t);
122 122 static int tnf_deallocbuf(void);
123 123 static int tnf_settracing(int);
124 124 static int tnf_pidfilterset(int);
125 125 static int tnf_pidfilterget(caddr_t, int);
126 126 static int tnf_getpidstate(caddr_t, int);
127 127 static int tnf_setpidstate(int, pid_t, int);
128 128 static int tnf_getheader(caddr_t, int);
129 129 static int tnf_getblock(caddr_t, int);
130 130 static int tnf_getfwzone(caddr_t, int);
131 131
132 132 static void *tnf_test_1(void *, tnf_probe_control_t *, tnf_probe_setup_t *);
133 133 static void *tnf_test_2(void *, tnf_probe_control_t *, tnf_probe_setup_t *);
134 134
135 135 #define TNFCTL_MINOR 0
136 136 #define TNFMAP_MINOR 1
137 137
138 138 struct cb_ops tnf_cb_ops = {
139 139 tnf_open, /* open */
140 140 tnf_close, /* close */
141 141 nodev, /* strategy */
142 142 nodev, /* print */
143 143 nodev, /* dump */
144 144 nodev, /* read */
145 145 nodev, /* write */
146 146 tnf_ioctl, /* ioctl */
147 147 nodev, /* devmap */
148 148 nodev, /* mmap */
149 149 nodev, /* segmap */
150 150 nochpoll, /* poll */
151 151 ddi_prop_op, /* prop_op */
152 152 0, /* streamtab */
153 153 D_NEW | D_MP /* Driver compatibility flag */
154 154 };
155 155
156 156 struct dev_ops tnf_ops = {
157 157 DEVO_REV, /* devo_rev, */
158 158 0, /* refcnt */
159 159 tnf_info, /* info */
160 160 nulldev, /* identify */
161 161 nulldev, /* probe */
162 162 tnf_attach, /* attach */
163 163 tnf_detach, /* detach */
164 164 nodev, /* reset */
165 165 &tnf_cb_ops, /* driver operations */
166 166 (struct bus_ops *)0, /* no bus operations */
167 167 NULL, /* power */
168 168 ddi_quiesce_not_needed, /* quiesce */
169 169 };
170 170
↓ open down ↓ |
170 lines elided |
↑ open up ↑ |
171 171 extern struct mod_ops mod_driverops;
172 172
173 173 static struct modldrv modldrv = {
174 174 &mod_driverops,
175 175 "kernel probes driver",
176 176 &tnf_ops,
177 177 };
178 178
179 179 static struct modlinkage modlinkage = {
180 180 MODREV_1,
181 - (void *)&modldrv,
182 - NULL
181 + { (void *)&modldrv, NULL }
183 182 };
184 183
185 184 int
186 185 _init()
187 186 {
188 187 register int error;
189 188
190 189 mutex_init(&tnf_drv_state.tnf_mtx, NULL, MUTEX_DEFAULT, NULL);
191 190
192 191 if ((error = mod_install(&modlinkage)) != 0) {
193 192 mutex_destroy(&tnf_drv_state.tnf_mtx);
194 193 return (error);
195 194 }
196 195
197 196 /* Give t0 a tpdp */
198 197 if (!t0.t_tnf_tpdp)
199 198 t0.t_tnf_tpdp = kmem_zalloc(sizeof (tnf_ops_t), KM_SLEEP);
200 199 /* Initialize tag system */
201 200 tnf_tag_core_init();
202 201 tnf_tag_trace_init();
203 202 tnf_changed_probe_list = 1;
204 203 return (0);
205 204 }
206 205
207 206 int
208 207 _fini()
209 208 {
210 209 /* Not safe to unload this module, currently */
211 210 return (EBUSY);
212 211 }
213 212
214 213 int
215 214 _info(struct modinfo *modinfop)
216 215 {
217 216 return (mod_info(&modlinkage, modinfop));
218 217 }
219 218
220 219 /* ARGSUSED */
221 220 static int
222 221 tnf_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
223 222 {
224 223 register int error;
225 224
226 225 switch (infocmd) {
227 226 case DDI_INFO_DEVT2DEVINFO:
228 227 *result = (void *)tnf_devi;
229 228 error = DDI_SUCCESS;
230 229 break;
231 230 case DDI_INFO_DEVT2INSTANCE:
232 231 *result = (void *)0;
233 232 error = DDI_SUCCESS;
234 233 break;
235 234 default:
236 235 error = DDI_FAILURE;
237 236 }
238 237 return (error);
239 238 }
240 239
241 240 static int
242 241 tnf_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
243 242 {
244 243 if (cmd != DDI_ATTACH)
245 244 return (DDI_FAILURE);
246 245 if ((ddi_create_minor_node(devi, "tnfctl", S_IFCHR, TNFCTL_MINOR,
247 246 DDI_PSEUDO, NULL) == DDI_FAILURE) ||
248 247 (ddi_create_minor_node(devi, "tnfmap", S_IFCHR, TNFMAP_MINOR,
249 248 DDI_PSEUDO, NULL) == DDI_FAILURE)) {
250 249 ddi_remove_minor_node(devi, NULL);
251 250 return (DDI_FAILURE);
252 251 }
253 252 tnf_devi = devi;
254 253 return (DDI_SUCCESS);
255 254 }
256 255
257 256 static int
258 257 tnf_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
259 258 {
260 259 if (cmd != DDI_DETACH)
261 260 return (DDI_FAILURE);
262 261 ddi_remove_minor_node(devi, NULL);
263 262 return (DDI_SUCCESS);
264 263 }
265 264
266 265 /*
267 266 * property operations. Return the size of the kernel trace buffer. We
268 267 * only handle size property requests. Others are passed on.
269 268 */
270 269 #ifdef UNUSED
271 270 static int
272 271 tnf_prop_op(dev_t dev, dev_info_t *di, ddi_prop_op_t prop,
273 272 int m, char *name, caddr_t valuep, int *lengthp)
274 273 {
275 274 int length, *retbuf, size;
276 275
277 276 if (strcmp(name, "size") == 0) {
278 277
279 278 /* Don't need tnf_mtx, since mapdev_open_count > 0 */
280 279 size = tnf_trace_file_size;
281 280
282 281 length = *lengthp; /* get caller's length */
283 282 *lengthp = sizeof (int); /* set caller's length */
284 283
285 284 switch (prop) {
286 285
287 286 case PROP_LEN:
288 287 return (DDI_PROP_SUCCESS);
289 288
290 289 case PROP_LEN_AND_VAL_ALLOC:
291 290 retbuf = kmem_alloc(sizeof (int),
292 291 (m & DDI_PROP_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
293 292 if (retbuf == NULL)
294 293 return (DDI_PROP_NO_MEMORY);
295 294 *(int **)valuep = retbuf; /* set caller's buf */
296 295 *retbuf = size;
297 296 return (DDI_PROP_SUCCESS);
298 297
299 298 case PROP_LEN_AND_VAL_BUF:
300 299 if (length < sizeof (int))
301 300 return (DDI_PROP_BUF_TOO_SMALL);
302 301 *(int *)valuep = size;
303 302 return (DDI_PROP_SUCCESS);
304 303 }
305 304 }
306 305 return (ddi_prop_op(dev, dip, prop, m, name, valuep, lengthp));
307 306 }
308 307 #endif
309 308
310 309 /* ARGSUSED */
311 310 static int
312 311 tnf_open(dev_t *devp, int flag, int otyp, struct cred *cred)
313 312 {
314 313 int err = 0;
315 314 mutex_enter(&tnf_drv_state.tnf_mtx);
316 315 if (getminor(*devp) == TNFCTL_MINOR) {
317 316 if (tnf_drv_state.ctldev_is_open)
318 317 err = EBUSY;
319 318 else {
320 319 tnf_drv_state.ctldev_is_open = B_TRUE;
321 320 /* stop autounloading -- XXX temporary */
322 321 modunload_disable();
323 322 }
324 323 } else {
325 324 /* ASSERT(getminor(*devp) == TNFMAP_MINOR) */
326 325 ++tnf_drv_state.mapdev_open_count;
327 326 }
328 327 mutex_exit(&tnf_drv_state.tnf_mtx);
329 328 return (err);
330 329 }
331 330
332 331 /* ARGSUSED */
333 332 static int
334 333 tnf_close(dev_t dev, int flag, int otyp, struct cred *cred)
335 334 {
336 335 if (getminor(dev) == TNFCTL_MINOR) {
337 336 /*
338 337 * Request the reenablement of autounloading
339 338 */
340 339 modunload_enable();
341 340 tnf_drv_state.ctldev_is_open = B_FALSE;
342 341 } else {
343 342 /* ASSERT(getminor(dev) == TNFMAP_MINOR) */
344 343 /*
345 344 * Unconditionally zero the open count since close()
346 345 * is called when last client closes the device.
347 346 */
348 347 tnf_drv_state.mapdev_open_count = 0;
349 348 }
350 349 return (0);
351 350 }
352 351
353 352 /*
354 353 * return the address of the image referenced by dev.
355 354 *
356 355 * 1191344: aliasing problem on VAC machines. It could be made to
357 356 * work by ensuring that tnf_buf is allocated on a vac_size boundary.
358 357 */
359 358 #ifdef UNUSED
360 359 /*ARGSUSED*/
361 360 static int
362 361 tnf_mmap(dev_t dev, off_t off, int prot)
363 362 {
364 363 register caddr_t addr;
365 364 register caddr_t pg_offset;
366 365
367 366 if (getminor(dev) != TNFMAP_MINOR)
368 367 return (-1);
369 368 if (tnf_buf == 0 || off >= tnf_trace_file_size) {
370 369 return (-1);
371 370 }
372 371
373 372 addr = tnf_buf;
374 373 pg_offset = (caddr_t)((ulong_t)addr + (ulong_t)off);
375 374 return ((int)hat_getpfnum(kas.a_hat, pg_offset));
376 375 }
377 376 #endif
378 377
379 378 /*ARGSUSED4*/
380 379 static int
381 380 tnf_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
382 381 cred_t *credp, int *rvalp)
383 382 {
384 383 int filterval = 1;
385 384
386 385 if ((mode & FMODELS) != FNATIVE)
387 386 return (ENOTSUP);
388 387
389 388 if (getminor(dev) != TNFCTL_MINOR &&
390 389 cmd != TIFIOCGSTATE &&
391 390 cmd != TIFIOCGHEADER &&
392 391 cmd != TIFIOCGBLOCK &&
393 392 cmd != TIFIOCGFWZONE)
394 393 return (EINVAL);
395 394
396 395 switch (cmd) {
397 396 case TIFIOCGMAXPROBE:
398 397 return (tnf_getmaxprobe((caddr_t)arg, mode));
399 398 case TIFIOCGPROBEVALS:
400 399 return (tnf_getprobevals((caddr_t)arg, mode));
401 400 case TIFIOCGPROBESTRING:
402 401 return (tnf_getprobestring((caddr_t)arg, mode));
403 402 case TIFIOCSPROBEVALS:
404 403 return (tnf_setprobevals((caddr_t)arg, mode));
405 404 case TIFIOCGSTATE:
406 405 return (tnf_getstate((caddr_t)arg, mode));
407 406 case TIFIOCALLOCBUF:
408 407 return (tnf_allocbuf(arg));
409 408 case TIFIOCDEALLOCBUF:
410 409 return (tnf_deallocbuf());
411 410 case TIFIOCSTRACING:
412 411 /* LINTED cast from 64-bit integer to 32-bit integer */
413 412 return (tnf_settracing((int)arg));
414 413 case TIFIOCSPIDFILTER:
415 414 /* LINTED cast from 64-bit integer to 32-bit integer */
416 415 return (tnf_pidfilterset((int)arg));
417 416 case TIFIOCGPIDSTATE:
418 417 return (tnf_getpidstate((caddr_t)arg, mode));
419 418 case TIFIOCSPIDOFF:
420 419 filterval = 0;
421 420 /*FALLTHROUGH*/
422 421 case TIFIOCSPIDON:
423 422 /* LINTED cast from 64-bit integer to 32-bit integer */
424 423 return (tnf_setpidstate(filterval, (pid_t)arg, mode));
425 424 case TIFIOCPIDFILTERGET:
426 425 return (tnf_pidfilterget((caddr_t)arg, mode));
427 426 case TIFIOCGHEADER:
428 427 return (tnf_getheader((caddr_t)arg, mode));
429 428 case TIFIOCGBLOCK:
430 429 return (tnf_getblock((caddr_t)arg, mode));
431 430 case TIFIOCGFWZONE:
432 431 return (tnf_getfwzone((caddr_t)arg, mode));
433 432 default:
434 433 return (EINVAL);
435 434 }
436 435 }
437 436
438 437 /*
439 438 * ioctls
440 439 */
441 440
442 441 static int
443 442 tnf_getmaxprobe(caddr_t arg, int mode)
444 443 {
445 444 tnf_probe_control_t *p;
446 445 /*
447 446 * XXX Still not right for module unload -- just counting
448 447 * the probes is not enough
449 448 */
450 449 if (tnf_changed_probe_list) {
451 450 mutex_enter(&mod_lock);
452 451 tnf_changed_probe_list = 0;
453 452 tnf_drv_state.tnf_probe_count = 0;
454 453 for (p = (tnf_probe_control_t *)__tnf_probe_list_head;
455 454 p != 0; p = p->next)
456 455 ++tnf_drv_state.tnf_probe_count;
457 456 mutex_exit(&mod_lock);
458 457 }
459 458 if (ddi_copyout((caddr_t)&tnf_drv_state.tnf_probe_count,
460 459 arg, sizeof (tnf_drv_state.tnf_probe_count), mode))
461 460 return (EFAULT);
462 461 return (0);
463 462 }
464 463
465 464 static int
466 465 tnf_getprobevals(caddr_t arg, int mode)
467 466 {
468 467 tnf_probevals_t probebuf;
469 468 tnf_probe_control_t *p;
470 469 int i, retval = 0;
471 470
472 471 if (ddi_copyin(arg, (caddr_t)&probebuf, sizeof (probebuf), mode))
473 472 return (EFAULT);
474 473
475 474 mutex_enter(&mod_lock);
476 475 for (i = 1, p = (tnf_probe_control_t *)__tnf_probe_list_head;
477 476 p != NULL && i != probebuf.probenum;
478 477 ++i, p = p->next)
479 478 ;
480 479 if (p == NULL)
481 480 retval = ENOENT;
482 481 else {
483 482 probebuf.enabled = (p->test_func != NULL);
484 483 probebuf.traced = (p->probe_func == tnf_trace_commit);
485 484 /* LINTED assignment of 64-bit integer to 32-bit integer */
486 485 probebuf.attrsize = strlen(p->attrs) + 1;
487 486 if (ddi_copyout((caddr_t)&probebuf,
488 487 arg, sizeof (probebuf), mode))
489 488 retval = EFAULT;
490 489 }
491 490 mutex_exit(&mod_lock);
492 491 return (retval);
493 492 }
494 493
495 494 static int
496 495 tnf_getprobestring(caddr_t arg, int mode)
497 496 {
498 497 tnf_probevals_t probebuf;
499 498 tnf_probe_control_t *p;
500 499 int i, retval = 0;
501 500
502 501 if (ddi_copyin(arg, (caddr_t)&probebuf, sizeof (probebuf), mode))
503 502 return (EFAULT);
504 503
505 504 mutex_enter(&mod_lock);
506 505 for (i = 1, p = (tnf_probe_control_t *)__tnf_probe_list_head;
507 506 p != NULL && i != probebuf.probenum;
508 507 ++i, p = p->next)
509 508 ;
510 509 if (p == NULL)
511 510 retval = ENOENT;
512 511 else if (ddi_copyout((caddr_t)p->attrs,
513 512 arg, strlen(p->attrs) + 1, mode))
514 513 retval = EFAULT;
515 514 mutex_exit(&mod_lock);
516 515 return (retval);
517 516 }
518 517
519 518 static int
520 519 tnf_setprobevals(caddr_t arg, int mode)
521 520 {
522 521 tnf_probevals_t probebuf;
523 522 tnf_probe_control_t *p;
524 523 int i, retval = 0;
525 524
526 525 if (ddi_copyin(arg, (caddr_t)&probebuf, sizeof (probebuf), mode))
527 526 return (EFAULT);
528 527
529 528 mutex_enter(&mod_lock);
530 529 for (i = 1, p = (tnf_probe_control_t *)__tnf_probe_list_head;
531 530 p != NULL && i != probebuf.probenum;
532 531 ++i, p = p->next)
533 532 ;
534 533 if (p == NULL)
535 534 retval = ENOENT;
536 535 else {
537 536 /*
538 537 * First do trace, then enable.
539 538 * Set test_func last.
540 539 */
541 540 if (probebuf.traced)
542 541 p->probe_func = tnf_trace_commit;
543 542 else
544 543 p->probe_func = tnf_trace_rollback;
545 544 if (probebuf.enabled) {
546 545 p->alloc_func = tnf_trace_alloc;
547 546 /* this must be set last */
548 547 if (tnf_drv_state.tnf_pidfilter_mode)
549 548 p->test_func = tnf_test_2;
550 549 else
551 550 p->test_func = tnf_test_1;
552 551 } else
553 552 p->test_func = NULL;
554 553 }
555 554 mutex_exit(&mod_lock);
556 555 return (retval);
557 556 }
558 557
559 558 static int
560 559 tnf_getstate(caddr_t arg, int mode)
561 560 {
562 561 tifiocstate_t tstate;
563 562 proc_t *procp;
564 563
565 564 if (tnf_buf == NULL) {
566 565 tstate.buffer_state = TIFIOCBUF_NONE;
567 566 tstate.buffer_size = 0;
568 567 } else {
569 568 switch (tnfw_b_state & ~TNFW_B_STOPPED) {
570 569 case TNFW_B_RUNNING:
571 570 tstate.buffer_state = TIFIOCBUF_OK;
572 571 break;
573 572 case TNFW_B_NOBUFFER:
574 573 tstate.buffer_state = TIFIOCBUF_UNINIT;
575 574 break;
576 575 case TNFW_B_BROKEN:
577 576 tstate.buffer_state = TIFIOCBUF_BROKEN;
578 577 break;
579 578 }
580 579 /* LINTED assignment of 64-bit integer to 32-bit integer */
581 580 tstate.buffer_size = tnf_trace_file_size;
582 581 }
583 582 tstate.trace_stopped = tnfw_b_state & TNFW_B_STOPPED;
584 583 tstate.pidfilter_mode = tnf_drv_state.tnf_pidfilter_mode;
585 584 tstate.pidfilter_size = 0;
586 585
587 586 mutex_enter(&pidlock);
588 587 for (procp = practive; procp != NULL; procp = procp->p_next)
589 588 if (PROC_IS_FILTER(procp))
590 589 tstate.pidfilter_size++;
591 590 mutex_exit(&pidlock);
592 591
593 592 if (ddi_copyout((caddr_t)&tstate, arg, sizeof (tstate), mode))
594 593 return (EFAULT);
595 594 return (0);
596 595 }
597 596
598 597 static int
599 598 tnf_allocbuf(intptr_t arg)
600 599 {
601 600 size_t bufsz;
602 601
603 602 if (tnf_buf != NULL)
604 603 return (EBUSY);
605 604
606 605 bufsz = roundup((size_t)arg, PAGESIZE);
607 606 /*
608 607 * Validate size
609 608 * XXX Take kernel VM into consideration as well
610 609 */
611 610 /* bug fix #4057599 if (bufsz > (physmem << PAGESHIFT) / 2) */
612 611 if (btop(bufsz) > (physmem / 2))
613 612 return (ENOMEM);
614 613 if (bufsz < TNF_TRACE_FILE_MIN)
615 614 bufsz = TNF_TRACE_FILE_MIN;
616 615
617 616 #if TNF_USE_KMA
618 617 tnf_buf = kmem_zalloc(bufsz, KM_SLEEP);
619 618 #else
620 619 /* LINTED cast from 64-bit integer to 32-bit intege */
621 620 tnf_buf = segkp_get(segkp, (int)bufsz,
622 621 KPD_ZERO | KPD_LOCKED | KPD_NO_ANON);
623 622 #endif
624 623 if (tnf_buf == NULL)
625 624 return (ENOMEM);
626 625
627 626 tnf_trace_file_size = bufsz;
628 627 tnf_trace_init();
629 628 return (0);
630 629 }
631 630
632 631 /*
633 632 * Process a "deallocate buffer" ioctl request. Tracing must be turned
634 633 * off. We must clear references to the buffer from the tag sites;
635 634 * invalidate all threads' notions of block ownership; make sure nobody
636 635 * is executing a probe (they might have started before tracing was
637 636 * turned off); and free the buffer.
638 637 */
639 638 static int
640 639 tnf_deallocbuf(void)
641 640 {
642 641 tnf_ops_t *tpdp;
643 642 kthread_t *t;
644 643 tnf_probe_control_t *probep;
645 644 tnf_tag_data_t *tagp;
646 645
647 646 if (tnf_drv_state.mapdev_open_count > 0 || tnf_tracing_active)
648 647 return (EBUSY);
649 648 if (tnf_buf == NULL)
650 649 return (ENOMEM);
651 650
652 651 /*
653 652 * Make sure nobody is executing a probe.
654 653 * (They could be if they got started while
655 654 * tnf_tracing_active was still on.) Grab
656 655 * pidlock, and check the busy flag in all
657 656 * TPDP's.
658 657 */
659 658 mutex_enter(&pidlock);
660 659 t = curthread;
661 660 do {
662 661 if (t->t_tnf_tpdp != NULL) {
663 662 /* LINTED pointer cast may result in improper alignment */
664 663 tpdp = (tnf_ops_t *)t->t_tnf_tpdp;
665 664 if (LOCK_HELD(&tpdp->busy)) {
666 665 mutex_exit(&pidlock);
667 666 return (EBUSY);
668 667 }
669 668 tpdp->wcb.tnfw_w_pos.tnfw_w_block = NULL;
670 669 tpdp->wcb.tnfw_w_tag_pos.tnfw_w_block = NULL;
671 670 tpdp->schedule.record_p = NULL;
672 671 }
673 672 t = t->t_next;
674 673 } while (t != curthread);
675 674 mutex_exit(&pidlock);
676 675
677 676 /*
678 677 * Zap all references to the buffer we're freeing.
679 678 * Grab mod_lock while walking list to keep it
680 679 * consistent.
681 680 */
682 681 mutex_enter(&mod_lock);
683 682 tagp = (tnf_tag_data_t *)__tnf_tag_list_head;
684 683 while (tagp != NULL) {
685 684 tagp->tag_index = 0;
686 685 tagp = (tnf_tag_data_t *)tagp->tag_version;
687 686 }
688 687 probep = (tnf_probe_control_t *)__tnf_probe_list_head;
689 688 while (probep != NULL) {
690 689 probep->index = 0;
691 690 probep = probep->next;
692 691 }
693 692 mutex_exit(&mod_lock);
694 693
695 694 tnfw_b_state = TNFW_B_NOBUFFER | TNFW_B_STOPPED;
696 695 #if TNF_USE_KMA
697 696 kmem_free(tnf_buf, tnf_trace_file_size);
698 697 #else
699 698 segkp_release(segkp, tnf_buf);
700 699 #endif
701 700 tnf_buf = NULL;
702 701
703 702 return (0);
704 703 }
705 704
706 705 static int
707 706 tnf_settracing(int arg)
708 707 {
709 708 if (arg)
710 709 if (tnf_buf == NULL)
711 710 return (ENOMEM);
712 711 else
713 712 tnf_trace_on();
714 713 else
715 714 tnf_trace_off();
716 715
717 716 #ifdef _TNF_SPEED_TEST
718 717 #define NITER 255
719 718 {
720 719 int i;
721 720
722 721 for (i = 0; i < NITER; i++)
723 722 TNF_PROBE_0(tnf_speed_0, "tnf", /* CSTYLED */);
724 723 for (i = 0; i < NITER; i++)
725 724 TNF_PROBE_1(tnf_speed_1, "tnf", /* CSTYLED */,
726 725 tnf_long, long, i);
727 726 for (i = 0; i < NITER; i++)
728 727 TNF_PROBE_2(tnf_speed_2, "tnf", /* CSTYLED */,
729 728 tnf_long, long1, i,
730 729 tnf_long, long2, i);
731 730 }
732 731 #endif /* _TNF_SPEED_TEST */
733 732
734 733 return (0);
735 734 }
736 735
737 736 static int
738 737 tnf_getpidstate(caddr_t arg, int mode)
739 738 {
740 739 int err = 0;
741 740 pid_t pid;
742 741 proc_t *procp;
743 742 int result;
744 743
745 744 if (ddi_copyin(arg, (caddr_t)&pid, sizeof (pid), mode))
746 745 return (EFAULT);
747 746
748 747 mutex_enter(&pidlock);
749 748 if ((procp = prfind(pid)) != NULL)
750 749 result = PROC_IS_FILTER(procp);
751 750 else
752 751 err = ESRCH;
753 752 mutex_exit(&pidlock);
754 753
755 754 if (!err)
756 755 if (ddi_copyout((caddr_t)&result, (caddr_t)arg,
757 756 sizeof (result), mode))
758 757 return (EFAULT);
759 758 return (err);
760 759 }
761 760
762 761 /*ARGSUSED*/
763 762 static int
764 763 tnf_setpidstate(int filterval, pid_t pid, int mode)
765 764 {
766 765 int err = 0;
767 766 proc_t *procp;
768 767
769 768 mutex_enter(&pidlock);
770 769 if ((procp = prfind(pid)) != NULL)
771 770 if (filterval)
772 771 PROC_FILTER_SET(procp);
773 772 else
774 773 PROC_FILTER_CLR(procp);
775 774 else
776 775 err = ESRCH;
777 776 mutex_exit(&pidlock);
778 777
779 778 return (err);
780 779 }
781 780
782 781 static int
783 782 tnf_pidfilterset(int mode)
784 783 {
785 784 tnf_probe_control_t *p;
786 785 tnf_probe_test_func_t func;
787 786
788 787 tnf_drv_state.tnf_pidfilter_mode = mode;
789 788
790 789 /* Establish correct test func for each probe */
791 790 if (mode)
792 791 func = tnf_test_2;
793 792 else
794 793 func = tnf_test_1;
795 794
796 795 mutex_enter(&mod_lock);
797 796 p = (tnf_probe_control_t *)__tnf_probe_list_head;
798 797 while (p != NULL) {
799 798 if (p->test_func != NULL)
800 799 p->test_func = func;
801 800 p = p->next;
802 801 }
803 802 mutex_exit(&mod_lock);
804 803
805 804 return (0);
806 805 }
807 806
808 807 static int
809 808 tnf_pidfilterget(caddr_t dest, int mode)
810 809 {
811 810 int err = 0;
812 811 int filtercount = 0;
813 812 size_t sz;
814 813 pid_t *filterbuf, *bufp;
815 814 proc_t *procp;
816 815
817 816 /* Count how many processes in filter set (upper bound) */
818 817 mutex_enter(&pidlock);
819 818 for (procp = practive; procp != NULL; procp = procp->p_next)
820 819 if (PROC_IS_FILTER(procp))
821 820 filtercount++;
822 821 mutex_exit(&pidlock);
823 822
824 823 /* Allocate temp space to hold filter set (upper bound) */
825 824 sz = sizeof (pid_t) * (filtercount + 1);
826 825 filterbuf = kmem_zalloc(sz, KM_SLEEP);
827 826
828 827 /*
829 828 * NOTE: The filter set cannot grow between the first and
830 829 * second acquisitions of pidlock. This is currently true
831 830 * because:
832 831 * 1. /dev/tnfctl is exclusive open, so all driver
833 832 * control operations, including changing the filter
834 833 * set and this code, are effectively single-threaded.
835 834 * 2. There is no in-kernel API to manipulate the filter
836 835 * set (i.e. toggle the on/off bit in a proc struct).
837 836 * 3. The proc filter bit is not inherited across a fork()
838 837 * operation; the child starts with the bit off.
839 838 * If any of these assumptions is invalidated, a possible
840 839 * solution is to check whether we're overflowing the allocated
841 840 * filterbuf below, and back out and restart from the beginning
842 841 * if so.
843 842 *
844 843 * The code below handles the case when the filter set shrinks
845 844 * due to processes exiting.
846 845 */
847 846
848 847 /* Fill in filter set */
849 848 bufp = filterbuf + 1; /* first word is for count */
850 849 filtercount = 0; /* recomputed below */
851 850 mutex_enter(&pidlock);
852 851 for (procp = practive; procp != NULL; procp = procp->p_next) {
853 852 if (PROC_IS_FILTER(procp)) {
854 853 filtercount++;
855 854 *bufp++ = procp->p_pid;
856 855 }
857 856 }
858 857 mutex_exit(&pidlock);
859 858
860 859 /* Set filtercount */
861 860 *filterbuf = (pid_t)filtercount;
862 861
863 862 /* Copy out result */
864 863 if (ddi_copyout((caddr_t)filterbuf, dest, sz, mode))
865 864 err = EFAULT;
866 865
867 866 /* Free temp space */
868 867 kmem_free(filterbuf, sz);
869 868
870 869 return (err);
871 870 }
872 871
873 872 static int
874 873 tnf_getheader(caddr_t arg, int mode)
875 874 {
876 875 if (tnf_buf == NULL)
877 876 return (ENOMEM);
878 877 if (ddi_copyout(tnf_buf, arg, TNF_BLOCK_SIZE, mode))
879 878 return (EFAULT);
880 879 return (0);
881 880 }
882 881
883 882 static int
884 883 tnf_getblock(caddr_t arg, int mode)
885 884 {
886 885 int err = 0;
887 886 tifiocgblock_t parms;
888 887 caddr_t area;
889 888 tnf_block_header_t *blk;
890 889
891 890 if (tnf_buf == NULL)
892 891 return (ENOMEM);
893 892 if (ddi_copyin(arg, (caddr_t)&parms, sizeof (parms), mode))
894 893 return (EFAULT);
895 894 area = tnf_buf + TNF_DIRECTORY_SIZE +
896 895 parms.block_num * TNF_BLOCK_SIZE;
897 896 if (area < tnf_buf + TNF_DIRECTORY_SIZE ||
898 897 area >= tnf_buf + tnf_trace_file_size)
899 898 return (EFAULT);
900 899 /* LINTED pointer cast */
901 900 blk = (tnf_block_header_t *)area;
902 901 /*
903 902 * B-lock the block while we're reading
904 903 */
905 904 if (!lock_try(&blk->B_lock))
906 905 return (EBUSY);
907 906 if (ddi_copyout(area, parms.dst_addr, TNF_BLOCK_SIZE, mode))
908 907 err = EFAULT;
909 908 lock_clear(&blk->B_lock);
910 909 return (err);
911 910 }
912 911
913 912 static int
914 913 tnf_getfwzone(caddr_t arg, int mode)
915 914 {
916 915 tifiocgfw_t parms;
917 916
918 917 if (tnf_buf == NULL)
919 918 return (ENOMEM);
920 919 if (ddi_copyin(arg, (caddr_t)&parms, sizeof (parms), mode))
921 920 return (EFAULT);
922 921 if (ddi_copyout(tnf_buf + TNF_BLOCK_SIZE + parms.start *
923 922 sizeof (tnf_ref32_t), (caddr_t)parms.dst_addr,
924 923 parms.slots * (int)(sizeof (tnf_ref32_t)), mode))
925 924 return (EFAULT);
926 925 return (0);
927 926 }
928 927
929 928 /*ARGSUSED*/
930 929 static void *
931 930 tnf_test_1(void *tpdp, tnf_probe_control_t *probe_p, tnf_probe_setup_t *sp)
932 931 {
933 932 tpdp = (void *)curthread->t_tnf_tpdp;
934 933 if (tpdp != NULL)
935 934 return (tnf_trace_alloc((tnf_ops_t *)tpdp, probe_p, sp));
936 935 return (NULL);
937 936 }
938 937
939 938 /*ARGSUSED*/
940 939 static void *
941 940 tnf_test_2(void *tpdp, tnf_probe_control_t *probe_p, tnf_probe_setup_t *sp)
942 941 {
943 942 tpdp = (void *)curthread->t_tnf_tpdp;
944 943 if (tpdp != NULL && PROC_IS_FILTER(curproc))
945 944 return (tnf_trace_alloc((tnf_ops_t *)tpdp, probe_p, sp));
946 945 return (NULL);
947 946 }
948 947
949 948 #endif /* !NPROBE */
↓ open down ↓ |
757 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX