2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <kern/debug.h>
30 #include <kern/kalloc.h>
31 #include <sys/param.h>
34 #include <sys/sysctl.h>
35 #include <libkern/libkern.h>
36 #include <kern/assert.h>
40 #include <pexpert/pexpert.h>
41 #include <kperf/kperf.h>
43 /* Various sysctl requests */
44 #define REQ_CLASSES (1)
45 #define REQ_COUNTING (2)
46 #define REQ_THREAD_COUNTING (3)
47 #define REQ_CONFIG_COUNT (4)
48 #define REQ_COUNTER_COUNT (5)
49 #define REQ_THREAD_COUNTERS (6)
50 #define REQ_COUNTERS (7)
51 #define REQ_SHADOW_COUNTERS (8)
52 #define REQ_CONFIG (9)
53 #define REQ_PERIOD (10)
54 #define REQ_ACTIONID (11)
55 #define REQ_SW_INC (14)
57 /* Type-munging casts */
58 typedef int (*getint_t
)(void);
59 typedef int (*setint_t
)(int);
62 static int kpc_initted
= 0;
64 /* locking and buffer for large data requests */
65 #define SYSCTL_BUFFER_SIZE (33 * sizeof(uint64_t))
66 static lck_grp_attr_t
*sysctl_buffer_lckgrp_attr
= NULL
;
67 static lck_grp_t
*sysctl_buffer_lckgrp
= NULL
;
68 static lck_mtx_t sysctl_buffer_lock
;
69 static void *sysctl_buffer
= NULL
;
71 typedef int (*setget_func_t
)(int);
74 extern void kpc_arch_init(void);
75 extern void kpc_common_init(void);
76 extern void kpc_thread_init(void); /* osfmk/kern/kpc_thread.c */
81 sysctl_buffer_lckgrp_attr
= lck_grp_attr_alloc_init();
82 sysctl_buffer_lckgrp
= lck_grp_alloc_init("kpc",
83 sysctl_buffer_lckgrp_attr
);
84 lck_mtx_init(&sysctl_buffer_lock
, sysctl_buffer_lckgrp
, LCK_ATTR_NULL
);
93 /* abstract sysctl handlers */
95 sysctl_get_int( struct sysctl_oid
*oidp
, struct sysctl_req
*req
,
100 /* copy out the old value */
101 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
107 sysctl_set_int( struct sysctl_req
*req
, int (*set_func
)(int))
112 error
= SYSCTL_IN( req
, &value
, sizeof(value
) );
116 error
= set_func( value
);
122 sysctl_getset_int( struct sysctl_oid
*oidp
, struct sysctl_req
*req
,
123 int (*get_func
)(void), int (*set_func
)(int) )
128 /* get the old value and process it */
131 /* copy out the old value, get the new value */
132 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
133 if (error
|| !req
->newptr
)
136 /* if that worked, and we're writing... */
137 error
= set_func( value
);
144 sysctl_setget_int( struct sysctl_req
*req
,
145 int (*setget_func
)(int) )
150 error
= SYSCTL_IN( req
, &value
, sizeof(value
) );
154 value
= setget_func(value
);
156 error
= SYSCTL_OUT( req
, &value
, sizeof(value
) );
162 kpc_sysctl_acquire_buffer(void)
164 if( sysctl_buffer
== NULL
)
166 sysctl_buffer
= kalloc(SYSCTL_BUFFER_SIZE
);
169 bzero( sysctl_buffer
, SYSCTL_BUFFER_SIZE
);
182 sysctl_kpc_get_counters(uint32_t counters
,
183 uint32_t *size
, void *buf
)
185 uint64_t *ctr_buf
= (uint64_t*)buf
;
189 count
= kpc_get_cpu_counters(counters
& KPC_ALL_CPUS
,
191 &curcpu
, &ctr_buf
[1]);
197 *size
= (count
+1) * sizeof(uint64_t);
203 sysctl_kpc_get_shadow_counters(uint32_t counters
,
204 uint32_t *size
, void *buf
)
206 uint64_t *ctr_buf
= (uint64_t*)buf
;
210 count
= kpc_get_shadow_counters(counters
& KPC_ALL_CPUS
,
212 &curcpu
, &ctr_buf
[1]);
219 *size
= (count
+1) * sizeof(uint64_t);
225 sysctl_kpc_get_thread_counters(uint32_t tid
,
226 uint32_t *size
, void *buf
)
228 uint32_t count
= *size
/ sizeof(uint64_t);
234 r
= kpc_get_curthread_counters(&count
, buf
);
236 *size
= count
* sizeof(uint64_t);
242 sysctl_kpc_get_config(uint32_t classes
, void* buf
)
244 return kpc_get_config( classes
, buf
);
248 sysctl_kpc_set_config(uint32_t classes
, void* buf
)
250 return kpc_set_config( classes
, buf
);
254 sysctl_kpc_get_period(uint32_t classes
, void* buf
)
256 return kpc_get_period( classes
, buf
);
260 sysctl_kpc_set_period(uint32_t classes
, void* buf
)
262 return kpc_set_period( classes
, buf
);
266 sysctl_kpc_get_actionid(uint32_t classes
, void* buf
)
268 return kpc_get_actionid( classes
, buf
);
272 sysctl_kpc_set_actionid(uint32_t classes
, void* buf
)
274 return kpc_set_actionid( classes
, buf
);
279 sysctl_get_bigarray( struct sysctl_req
*req
,
280 int (*get_fn
)(uint32_t, uint32_t*, void*) )
283 uint32_t bufsize
= SYSCTL_BUFFER_SIZE
;
286 /* get the argument */
287 error
= SYSCTL_IN( req
, &arg
, sizeof(arg
) );
290 printf( "kpc: no arg?\n" );
294 /* get the wired buffer */
295 error
= kpc_sysctl_acquire_buffer();
299 /* atomically get the array into the wired buffer. We have a double
300 * copy, but this is better than page faulting / interrupting during
303 error
= get_fn( arg
, &bufsize
, sysctl_buffer
);
305 /* do the copy out */
307 error
= SYSCTL_OUT( req
, sysctl_buffer
, bufsize
);
312 /* given a config word, how many bytes does it take? */
314 sysctl_config_size( uint32_t config
)
316 return kpc_get_config_count(config
) * sizeof(kpc_config_t
);
320 sysctl_counter_size( uint32_t classes
)
322 return kpc_get_counter_count(classes
) * sizeof(uint64_t);
326 sysctl_actionid_size( uint32_t classes
)
328 return kpc_get_counter_count(classes
) * sizeof(int32_t);
332 sysctl_getset_bigarray( struct sysctl_req
*req
,
333 int (*size_fn
)(uint32_t arg
),
334 int (*get_fn
)(uint32_t, void*),
335 int (*set_fn
)(uint32_t, void*) )
338 uint32_t bufsize
= SYSCTL_BUFFER_SIZE
;
339 uint32_t regsize
= 0;
342 /* get the config word */
343 error
= SYSCTL_IN( req
, &arg
, sizeof(arg
) );
346 printf( "kpc: no arg?\n" );
350 /* Work out size of registers */
351 regsize
= size_fn((uint32_t)arg
);
353 /* Ignore NULL requests */
357 /* ensure not too big */
358 if( regsize
> bufsize
)
361 /* get the wired buffer */
362 error
= kpc_sysctl_acquire_buffer();
369 // copy in the rest in -- sysctl remembers we did one already
370 error
= SYSCTL_IN( req
, sysctl_buffer
,
373 // if SYSCTL_IN fails it means we are only doing a read
376 error
= set_fn( (uint32_t)arg
, sysctl_buffer
);
386 error
= get_fn( (uint32_t)arg
, sysctl_buffer
);
390 // copy out the full set
391 error
= SYSCTL_OUT( req
, sysctl_buffer
, regsize
);
401 * #define SYSCTL_HANDLER_ARGS (struct sysctl_oid *oidp, \
402 * void *arg1, int arg2, \
403 * struct sysctl_req *req )
406 kpc_sysctl SYSCTL_HANDLER_ARGS
410 // __unused struct sysctl_oid *unused_oidp = oidp;
414 panic("kpc_init not called");
416 // Most sysctls require an access check, but a few are public.
417 switch( (uintptr_t) arg1
) {
419 case REQ_CONFIG_COUNT
:
420 case REQ_COUNTER_COUNT
:
421 // These read-only sysctls are public.
425 // Require kperf access to read or write anything else.
426 // This is either root or the blessed pid.
427 ret
= kperf_access_check();
434 lck_mtx_lock(&sysctl_buffer_lock
);
437 switch( (uintptr_t) arg1
)
440 ret
= sysctl_get_int( oidp
, req
,
444 ret
= sysctl_getset_int( oidp
, req
,
445 (getint_t
)kpc_get_running
,
446 (setint_t
)kpc_set_running
);
448 case REQ_THREAD_COUNTING
:
449 ret
= sysctl_getset_int( oidp
, req
,
450 (getint_t
)kpc_get_thread_counting
,
451 (setint_t
)kpc_set_thread_counting
);
454 case REQ_CONFIG_COUNT
:
455 ret
= sysctl_setget_int( req
,
456 (setget_func_t
)kpc_get_config_count
);
459 case REQ_COUNTER_COUNT
:
460 ret
= sysctl_setget_int( req
,
461 (setget_func_t
)kpc_get_counter_count
);
465 case REQ_THREAD_COUNTERS
:
466 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_thread_counters
);
470 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_counters
);
473 case REQ_SHADOW_COUNTERS
:
474 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_shadow_counters
);
478 ret
= sysctl_getset_bigarray( req
,
480 sysctl_kpc_get_config
,
481 sysctl_kpc_set_config
);
485 ret
= sysctl_getset_bigarray( req
,
487 sysctl_kpc_get_period
,
488 sysctl_kpc_set_period
);
492 ret
= sysctl_getset_bigarray( req
,
493 sysctl_actionid_size
,
494 sysctl_kpc_get_actionid
,
495 sysctl_kpc_set_actionid
);
500 ret
= sysctl_set_int( req
, (setget_func_t
)kpc_set_sw_inc
);
508 lck_mtx_unlock(&sysctl_buffer_lock
);
514 /*** sysctl definitions ***/
516 /* root kperf node */
517 SYSCTL_NODE(, OID_AUTO
, kpc
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
521 SYSCTL_PROC(_kpc
, OID_AUTO
, classes
,
522 CTLTYPE_INT
|CTLFLAG_RD
|CTLFLAG_ANYBODY
,
524 sizeof(int), kpc_sysctl
, "I", "Available classes");
526 SYSCTL_PROC(_kpc
, OID_AUTO
, counting
,
527 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
529 sizeof(int), kpc_sysctl
, "I", "PMCs counting");
531 SYSCTL_PROC(_kpc
, OID_AUTO
, thread_counting
,
532 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
533 (void*)REQ_THREAD_COUNTING
,
534 sizeof(int), kpc_sysctl
, "I", "Thread accumulation");
537 SYSCTL_PROC(_kpc
, OID_AUTO
, config_count
,
538 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
539 (void*)REQ_CONFIG_COUNT
,
540 sizeof(int), kpc_sysctl
, "S", "Config count");
542 SYSCTL_PROC(_kpc
, OID_AUTO
, counter_count
,
543 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
544 (void*)REQ_COUNTER_COUNT
,
545 sizeof(int), kpc_sysctl
, "S", "Counter count");
547 SYSCTL_PROC(_kpc
, OID_AUTO
, sw_inc
,
548 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
550 sizeof(int), kpc_sysctl
, "S", "Software increment");
553 SYSCTL_PROC(_kpc
, OID_AUTO
, thread_counters
,
554 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
555 (void*)REQ_THREAD_COUNTERS
,
556 sizeof(uint64_t), kpc_sysctl
,
557 "QU", "Current thread counters");
559 SYSCTL_PROC(_kpc
, OID_AUTO
, counters
,
560 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
562 sizeof(uint64_t), kpc_sysctl
,
563 "QU", "Current counters");
565 SYSCTL_PROC(_kpc
, OID_AUTO
, shadow_counters
,
566 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
567 (void*)REQ_SHADOW_COUNTERS
,
568 sizeof(uint64_t), kpc_sysctl
,
569 "QU", "Current shadow counters");
571 SYSCTL_PROC(_kpc
, OID_AUTO
, config
,
572 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
574 sizeof(uint64_t), kpc_sysctl
,
575 "QU", "Set counter configs");
577 SYSCTL_PROC(_kpc
, OID_AUTO
, period
,
578 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
580 sizeof(uint64_t), kpc_sysctl
,
581 "QU", "Set counter periods");
583 SYSCTL_PROC(_kpc
, OID_AUTO
, actionid
,
584 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
586 sizeof(uint32_t), kpc_sysctl
,
587 "QU", "Set counter actionids");