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>
39 #include <sys/ktrace.h>
41 #include <pexpert/pexpert.h>
42 #include <kperf/kperf.h>
44 /* Various sysctl requests */
45 #define REQ_CLASSES (1)
46 #define REQ_COUNTING (2)
47 #define REQ_THREAD_COUNTING (3)
48 #define REQ_CONFIG_COUNT (4)
49 #define REQ_COUNTER_COUNT (5)
50 #define REQ_THREAD_COUNTERS (6)
51 #define REQ_COUNTERS (7)
52 #define REQ_SHADOW_COUNTERS (8)
53 #define REQ_CONFIG (9)
54 #define REQ_PERIOD (10)
55 #define REQ_ACTIONID (11)
56 #define REQ_SW_INC (14)
57 #define REQ_PMU_VERSION (15)
59 /* Type-munging casts */
60 typedef int (*getint_t
)(void);
61 typedef int (*setint_t
)(int);
64 static int kpc_initted
= 0;
66 /* locking and buffer for large data requests */
67 #define SYSCTL_BUFFER_SIZE (33 * sizeof(uint64_t))
68 static lck_grp_attr_t
*sysctl_buffer_lckgrp_attr
= NULL
;
69 static lck_grp_t
*sysctl_buffer_lckgrp
= NULL
;
70 static lck_mtx_t sysctl_buffer_lock
;
71 static void *sysctl_buffer
= NULL
;
73 typedef int (*setget_func_t
)(int);
78 sysctl_buffer_lckgrp_attr
= lck_grp_attr_alloc_init();
79 sysctl_buffer_lckgrp
= lck_grp_alloc_init("kpc",
80 sysctl_buffer_lckgrp_attr
);
81 lck_mtx_init(&sysctl_buffer_lock
, sysctl_buffer_lckgrp
, LCK_ATTR_NULL
);
90 /* abstract sysctl handlers */
92 sysctl_get_int( struct sysctl_oid
*oidp
, struct sysctl_req
*req
,
97 /* copy out the old value */
98 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
104 sysctl_set_int( struct sysctl_req
*req
, int (*set_func
)(int))
109 error
= SYSCTL_IN( req
, &value
, sizeof(value
) );
113 error
= set_func( value
);
119 sysctl_getset_int( struct sysctl_oid
*oidp
, struct sysctl_req
*req
,
120 int (*get_func
)(void), int (*set_func
)(int) )
125 /* get the old value and process it */
128 /* copy out the old value, get the new value */
129 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
130 if (error
|| !req
->newptr
)
133 /* if that worked, and we're writing... */
134 error
= set_func( value
);
141 sysctl_setget_int( struct sysctl_req
*req
,
142 int (*setget_func
)(int) )
147 error
= SYSCTL_IN( req
, &value
, sizeof(value
) );
151 value
= setget_func(value
);
153 error
= SYSCTL_OUT( req
, &value
, sizeof(value
) );
159 kpc_sysctl_acquire_buffer(void)
161 if( sysctl_buffer
== NULL
)
163 sysctl_buffer
= kalloc(SYSCTL_BUFFER_SIZE
);
166 bzero( sysctl_buffer
, SYSCTL_BUFFER_SIZE
);
179 sysctl_kpc_get_counters(uint32_t counters
,
180 uint32_t *size
, void *buf
)
182 uint64_t *ctr_buf
= (uint64_t*)buf
;
186 count
= kpc_get_cpu_counters(counters
& KPC_ALL_CPUS
,
188 &curcpu
, &ctr_buf
[1]);
194 *size
= (count
+1) * sizeof(uint64_t);
200 sysctl_kpc_get_shadow_counters(uint32_t counters
,
201 uint32_t *size
, void *buf
)
203 uint64_t *ctr_buf
= (uint64_t*)buf
;
207 count
= kpc_get_shadow_counters(counters
& KPC_ALL_CPUS
,
209 &curcpu
, &ctr_buf
[1]);
216 *size
= (count
+1) * sizeof(uint64_t);
222 sysctl_kpc_get_thread_counters(uint32_t tid
,
223 uint32_t *size
, void *buf
)
225 uint32_t count
= *size
/ sizeof(uint64_t);
231 r
= kpc_get_curthread_counters(&count
, buf
);
233 *size
= count
* sizeof(uint64_t);
239 sysctl_kpc_get_config(uint32_t classes
, void* buf
)
241 return kpc_get_config( classes
, buf
);
245 sysctl_kpc_set_config(uint32_t classes
, void* buf
)
247 /* userspace cannot reconfigure the power class */
248 if (classes
& KPC_CLASS_POWER_MASK
)
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 /* userspace cannot reconfigure the power class */
263 if (classes
& KPC_CLASS_POWER_MASK
)
265 return kpc_set_period( classes
, buf
);
269 sysctl_kpc_get_actionid(uint32_t classes
, void* buf
)
271 return kpc_get_actionid( classes
, buf
);
275 sysctl_kpc_set_actionid(uint32_t classes
, void* buf
)
277 return kpc_set_actionid( classes
, buf
);
282 sysctl_get_bigarray( struct sysctl_req
*req
,
283 int (*get_fn
)(uint32_t, uint32_t*, void*) )
286 uint32_t bufsize
= SYSCTL_BUFFER_SIZE
;
289 /* get the argument */
290 error
= SYSCTL_IN( req
, &arg
, sizeof(arg
) );
293 printf( "kpc: no arg?\n" );
297 /* get the wired buffer */
298 error
= kpc_sysctl_acquire_buffer();
302 /* atomically get the array into the wired buffer. We have a double
303 * copy, but this is better than page faulting / interrupting during
306 error
= get_fn( arg
, &bufsize
, sysctl_buffer
);
308 /* do the copy out */
310 error
= SYSCTL_OUT( req
, sysctl_buffer
, bufsize
);
315 /* given a config word, how many bytes does it take? */
317 sysctl_config_size( uint32_t config
)
319 return kpc_get_config_count(config
) * sizeof(kpc_config_t
);
323 sysctl_counter_size( uint32_t classes
)
325 return kpc_get_counter_count(classes
) * sizeof(uint64_t);
329 sysctl_actionid_size( uint32_t classes
)
331 return kpc_get_counter_count(classes
) * sizeof(int32_t);
335 sysctl_getset_bigarray( struct sysctl_req
*req
,
336 int (*size_fn
)(uint32_t arg
),
337 int (*get_fn
)(uint32_t, void*),
338 int (*set_fn
)(uint32_t, void*) )
341 uint32_t bufsize
= SYSCTL_BUFFER_SIZE
;
342 uint32_t regsize
= 0;
345 /* get the config word */
346 error
= SYSCTL_IN( req
, &arg
, sizeof(arg
) );
349 printf( "kpc: no arg?\n" );
353 /* Work out size of registers */
354 regsize
= size_fn((uint32_t)arg
);
356 /* Ignore NULL requests */
360 /* ensure not too big */
361 if( regsize
> bufsize
)
364 /* get the wired buffer */
365 error
= kpc_sysctl_acquire_buffer();
372 // copy in the rest in -- sysctl remembers we did one already
373 error
= SYSCTL_IN( req
, sysctl_buffer
,
376 // if SYSCTL_IN fails it means we are only doing a read
379 error
= set_fn( (uint32_t)arg
, sysctl_buffer
);
389 error
= get_fn( (uint32_t)arg
, sysctl_buffer
);
393 // copy out the full set
394 error
= SYSCTL_OUT( req
, sysctl_buffer
, regsize
);
404 * #define SYSCTL_HANDLER_ARGS (struct sysctl_oid *oidp, \
405 * void *arg1, int arg2, \
406 * struct sysctl_req *req )
409 kpc_sysctl SYSCTL_HANDLER_ARGS
413 // __unused struct sysctl_oid *unused_oidp = oidp;
417 panic("kpc_init not called");
419 lck_mtx_lock(ktrace_lock
);
421 // Most sysctls require an access check, but a few are public.
422 switch( (uintptr_t) arg1
) {
424 case REQ_CONFIG_COUNT
:
425 case REQ_COUNTER_COUNT
:
426 // These read-only sysctls are public.
430 // Require kperf access to read or write anything else.
431 // This is either root or the blessed pid.
432 if ((ret
= ktrace_read_check())) {
433 lck_mtx_unlock(ktrace_lock
);
439 lck_mtx_unlock(ktrace_lock
);
441 lck_mtx_lock(&sysctl_buffer_lock
);
444 switch( (uintptr_t) arg1
)
447 ret
= sysctl_get_int( oidp
, req
,
451 ret
= sysctl_getset_int( oidp
, req
,
452 (getint_t
)kpc_get_running
,
453 (setint_t
)kpc_set_running
);
455 case REQ_THREAD_COUNTING
:
456 ret
= sysctl_getset_int( oidp
, req
,
457 (getint_t
)kpc_get_thread_counting
,
458 (setint_t
)kpc_set_thread_counting
);
461 case REQ_CONFIG_COUNT
:
462 ret
= sysctl_setget_int( req
,
463 (setget_func_t
)kpc_get_config_count
);
466 case REQ_COUNTER_COUNT
:
467 ret
= sysctl_setget_int( req
,
468 (setget_func_t
)kpc_get_counter_count
);
472 case REQ_THREAD_COUNTERS
:
473 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_thread_counters
);
477 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_counters
);
480 case REQ_SHADOW_COUNTERS
:
481 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_shadow_counters
);
485 ret
= sysctl_getset_bigarray( req
,
487 sysctl_kpc_get_config
,
488 sysctl_kpc_set_config
);
492 ret
= sysctl_getset_bigarray( req
,
494 sysctl_kpc_get_period
,
495 sysctl_kpc_set_period
);
499 ret
= sysctl_getset_bigarray( req
,
500 sysctl_actionid_size
,
501 sysctl_kpc_get_actionid
,
502 sysctl_kpc_set_actionid
);
507 ret
= sysctl_set_int( req
, (setget_func_t
)kpc_set_sw_inc
);
510 case REQ_PMU_VERSION
:
511 ret
= sysctl_get_int(oidp
, req
, kpc_get_pmu_version());
519 lck_mtx_unlock(&sysctl_buffer_lock
);
525 /*** sysctl definitions ***/
527 /* root kperf node */
528 SYSCTL_NODE(, OID_AUTO
, kpc
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
532 SYSCTL_PROC(_kpc
, OID_AUTO
, classes
,
533 CTLTYPE_INT
|CTLFLAG_RD
|CTLFLAG_ANYBODY
,
535 sizeof(int), kpc_sysctl
, "I", "Available classes");
537 SYSCTL_PROC(_kpc
, OID_AUTO
, counting
,
538 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
540 sizeof(int), kpc_sysctl
, "I", "PMCs counting");
542 SYSCTL_PROC(_kpc
, OID_AUTO
, thread_counting
,
543 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
544 (void*)REQ_THREAD_COUNTING
,
545 sizeof(int), kpc_sysctl
, "I", "Thread accumulation");
547 SYSCTL_PROC(_kpc
, OID_AUTO
, pmu_version
,
548 CTLTYPE_INT
| CTLFLAG_RD
| CTLFLAG_ANYBODY
,
549 (void *)REQ_PMU_VERSION
,
550 sizeof(int), kpc_sysctl
, "I", "PMU version for hardware");
553 SYSCTL_PROC(_kpc
, OID_AUTO
, config_count
,
554 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
555 (void*)REQ_CONFIG_COUNT
,
556 sizeof(int), kpc_sysctl
, "S", "Config count");
558 SYSCTL_PROC(_kpc
, OID_AUTO
, counter_count
,
559 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
560 (void*)REQ_COUNTER_COUNT
,
561 sizeof(int), kpc_sysctl
, "S", "Counter count");
563 SYSCTL_PROC(_kpc
, OID_AUTO
, sw_inc
,
564 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
566 sizeof(int), kpc_sysctl
, "S", "Software increment");
569 SYSCTL_PROC(_kpc
, OID_AUTO
, thread_counters
,
570 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
571 (void*)REQ_THREAD_COUNTERS
,
572 sizeof(uint64_t), kpc_sysctl
,
573 "QU", "Current thread counters");
575 SYSCTL_PROC(_kpc
, OID_AUTO
, counters
,
576 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
578 sizeof(uint64_t), kpc_sysctl
,
579 "QU", "Current counters");
581 SYSCTL_PROC(_kpc
, OID_AUTO
, shadow_counters
,
582 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
583 (void*)REQ_SHADOW_COUNTERS
,
584 sizeof(uint64_t), kpc_sysctl
,
585 "QU", "Current shadow counters");
587 SYSCTL_PROC(_kpc
, OID_AUTO
, config
,
588 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
590 sizeof(uint64_t), kpc_sysctl
,
591 "QU", "Set counter configs");
593 SYSCTL_PROC(_kpc
, OID_AUTO
, period
,
594 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
596 sizeof(uint64_t), kpc_sysctl
,
597 "QU", "Set counter periods");
599 SYSCTL_PROC(_kpc
, OID_AUTO
, actionid
,
600 CTLFLAG_RD
|CTLFLAG_WR
|CTLFLAG_ANYBODY
,
602 sizeof(uint32_t), kpc_sysctl
,
603 "QU", "Set counter actionids");