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);
63 static int kpc_initted
= 0;
65 static LCK_GRP_DECLARE(sysctl_lckgrp
, "kpc");
66 static LCK_MTX_DECLARE(sysctl_lock
, &sysctl_lckgrp
);
69 * Another element is needed to hold the CPU number when getting counter values.
71 #define KPC_MAX_BUF_LEN (KPC_MAX_COUNTERS_COPIED + 1)
73 typedef int (*setget_func_t
)(int);
84 kpc_get_bigarray(uint32_t *size_out
)
86 static uint64_t *bigarray
= NULL
;
88 LCK_MTX_ASSERT(&sysctl_lock
, LCK_MTX_ASSERT_OWNED
);
90 uint32_t size
= kpc_get_counterbuf_size() + sizeof(uint64_t);
98 * Another element is needed to hold the CPU number when getting counter
101 bigarray
= kheap_alloc_tag(KHEAP_DATA_BUFFERS
, size
,
102 Z_WAITOK
, VM_KERN_MEMORY_DIAG
);
103 assert(bigarray
!= NULL
);
107 /* abstract sysctl handlers */
109 sysctl_get_int( struct sysctl_oid
*oidp
, struct sysctl_req
*req
,
114 /* copy out the old value */
115 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
121 sysctl_set_int( struct sysctl_req
*req
, int (*set_func
)(int))
126 error
= SYSCTL_IN( req
, &value
, sizeof(value
));
131 error
= set_func( value
);
137 sysctl_getset_int( struct sysctl_oid
*oidp
, struct sysctl_req
*req
,
138 int (*get_func
)(void), int (*set_func
)(int))
143 /* get the old value and process it */
146 /* copy out the old value, get the new value */
147 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
148 if (error
|| !req
->newptr
) {
152 /* if that worked, and we're writing... */
153 error
= set_func( value
);
160 sysctl_setget_int( struct sysctl_req
*req
,
161 int (*setget_func
)(int))
166 error
= SYSCTL_IN( req
, &value
, sizeof(value
));
171 value
= setget_func(value
);
173 error
= SYSCTL_OUT( req
, &value
, sizeof(value
));
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]);
195 *size
= (count
+ 1) * sizeof(uint64_t);
201 sysctl_kpc_get_shadow_counters(uint32_t counters
,
202 uint32_t *size
, void *buf
)
204 uint64_t *ctr_buf
= (uint64_t*)buf
;
208 count
= kpc_get_shadow_counters(counters
& KPC_ALL_CPUS
,
210 &curcpu
, &ctr_buf
[1]);
218 *size
= (count
+ 1) * sizeof(uint64_t);
224 sysctl_kpc_get_thread_counters(uint32_t tid
,
225 uint32_t *size
, void *buf
)
227 uint32_t count
= *size
/ sizeof(uint64_t);
234 r
= kpc_get_curthread_counters(&count
, buf
);
236 *size
= count
* sizeof(uint64_t);
243 sysctl_kpc_get_config(uint32_t classes
, void* buf
)
245 return kpc_get_config( classes
, buf
);
249 sysctl_kpc_set_config(uint32_t classes
, void* buf
)
251 /* userspace cannot reconfigure the power class */
252 if (classes
& KPC_CLASS_POWER_MASK
) {
255 return kpc_set_config( classes
, buf
);
259 sysctl_kpc_get_period(uint32_t classes
, void* buf
)
261 return kpc_get_period( classes
, buf
);
265 sysctl_kpc_set_period(uint32_t classes
, void* buf
)
267 /* userspace cannot reconfigure the power class */
268 if (classes
& KPC_CLASS_POWER_MASK
) {
271 return kpc_set_period( classes
, buf
);
275 sysctl_kpc_get_actionid(uint32_t classes
, void* buf
)
277 return kpc_get_actionid( classes
, buf
);
281 sysctl_kpc_set_actionid(uint32_t classes
, void* buf
)
283 return kpc_set_actionid( classes
, buf
);
288 sysctl_get_bigarray(struct sysctl_req
*req
,
289 int (*get_fn
)(uint32_t, uint32_t*, void*))
291 uint32_t bufsize
= 0;
292 uint64_t *buf
= kpc_get_bigarray(&bufsize
);
295 /* get the argument */
296 int error
= SYSCTL_IN(req
, &arg
, sizeof(arg
));
301 error
= get_fn(arg
, &bufsize
, buf
);
303 error
= SYSCTL_OUT(req
, buf
, bufsize
);
309 /* given a config word, how many bytes does it take? */
311 sysctl_config_size( uint32_t config
)
313 return kpc_get_config_count(config
) * sizeof(kpc_config_t
);
317 sysctl_counter_size( uint32_t classes
)
319 return kpc_get_counter_count(classes
) * sizeof(uint64_t);
323 sysctl_actionid_size( uint32_t classes
)
325 return kpc_get_counter_count(classes
) * sizeof(int32_t);
329 sysctl_getset_bigarray(struct sysctl_req
*req
, int (*size_fn
)(uint32_t arg
),
330 int (*get_fn
)(uint32_t, void*), int (*set_fn
)(uint32_t, void*))
335 uint32_t bufsize
= 0;
336 uint64_t *buf
= kpc_get_bigarray(&bufsize
);
338 /* get the config word */
339 error
= SYSCTL_IN(req
, &arg
, sizeof(arg
));
344 /* Determine the size of registers to modify. */
345 uint32_t regsize
= size_fn((uint32_t)arg
);
346 if (regsize
== 0 || regsize
> bufsize
) {
352 /* copy the rest -- SYSCTL_IN knows the copyin should be shifted */
353 error
= SYSCTL_IN(req
, buf
, regsize
);
355 /* SYSCTL_IN failure means only need to read */
357 error
= set_fn((uint32_t)arg
, buf
);
366 error
= get_fn((uint32_t)arg
, buf
);
371 error
= SYSCTL_OUT(req
, buf
, regsize
);
378 kpc_sysctl SYSCTL_HANDLER_ARGS
382 // __unused struct sysctl_oid *unused_oidp = oidp;
386 panic("kpc_init not called");
389 if (!kpc_supported
) {
395 // Most sysctls require an access check, but a few are public.
396 switch ((uintptr_t) arg1
) {
398 case REQ_CONFIG_COUNT
:
399 case REQ_COUNTER_COUNT
:
400 // These read-only sysctls are public.
404 // Require kperf access to read or write anything else.
405 // This is either root or the blessed pid.
406 if ((ret
= ktrace_read_check())) {
415 lck_mtx_lock(&sysctl_lock
);
418 switch ((uintptr_t) arg1
) {
420 ret
= sysctl_get_int( oidp
, req
,
424 ret
= sysctl_getset_int( oidp
, req
,
425 (getint_t
)kpc_get_running
,
426 (setint_t
)kpc_set_running
);
428 case REQ_THREAD_COUNTING
:
429 ret
= sysctl_getset_int( oidp
, req
,
430 (getint_t
)kpc_get_thread_counting
,
431 (setint_t
)kpc_set_thread_counting
);
434 case REQ_CONFIG_COUNT
:
435 ret
= sysctl_setget_int( req
,
436 (setget_func_t
)kpc_get_config_count
);
439 case REQ_COUNTER_COUNT
:
440 ret
= sysctl_setget_int( req
,
441 (setget_func_t
)kpc_get_counter_count
);
445 case REQ_THREAD_COUNTERS
:
446 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_thread_counters
);
450 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_counters
);
453 case REQ_SHADOW_COUNTERS
:
454 ret
= sysctl_get_bigarray( req
, sysctl_kpc_get_shadow_counters
);
458 ret
= sysctl_getset_bigarray( req
,
460 sysctl_kpc_get_config
,
461 sysctl_kpc_set_config
);
465 ret
= sysctl_getset_bigarray( req
,
467 sysctl_kpc_get_period
,
468 sysctl_kpc_set_period
);
472 ret
= sysctl_getset_bigarray( req
,
473 sysctl_actionid_size
,
474 sysctl_kpc_get_actionid
,
475 sysctl_kpc_set_actionid
);
480 ret
= sysctl_set_int( req
, (setget_func_t
)kpc_set_sw_inc
);
483 case REQ_PMU_VERSION
:
484 ret
= sysctl_get_int(oidp
, req
, kpc_get_pmu_version());
492 lck_mtx_unlock(&sysctl_lock
);
498 /*** sysctl definitions ***/
500 /* root kperf node */
501 SYSCTL_NODE(, OID_AUTO
, kpc
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0,
505 SYSCTL_PROC(_kpc
, OID_AUTO
, classes
,
506 CTLTYPE_INT
| CTLFLAG_RD
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
508 sizeof(int), kpc_sysctl
, "I", "Available classes");
510 SYSCTL_PROC(_kpc
, OID_AUTO
, counting
,
511 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
513 sizeof(int), kpc_sysctl
, "I", "PMCs counting");
515 SYSCTL_PROC(_kpc
, OID_AUTO
, thread_counting
,
516 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
517 (void*)REQ_THREAD_COUNTING
,
518 sizeof(int), kpc_sysctl
, "I", "Thread accumulation");
520 SYSCTL_PROC(_kpc
, OID_AUTO
, pmu_version
,
521 CTLTYPE_INT
| CTLFLAG_RD
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
522 (void *)REQ_PMU_VERSION
,
523 sizeof(int), kpc_sysctl
, "I", "PMU version for hardware");
526 SYSCTL_PROC(_kpc
, OID_AUTO
, config_count
,
527 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
528 (void*)REQ_CONFIG_COUNT
,
529 sizeof(int), kpc_sysctl
, "S", "Config count");
531 SYSCTL_PROC(_kpc
, OID_AUTO
, counter_count
,
532 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
533 (void*)REQ_COUNTER_COUNT
,
534 sizeof(int), kpc_sysctl
, "S", "Counter count");
536 SYSCTL_PROC(_kpc
, OID_AUTO
, sw_inc
,
537 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
539 sizeof(int), kpc_sysctl
, "S", "Software increment");
542 SYSCTL_PROC(_kpc
, OID_AUTO
, thread_counters
,
543 CTLFLAG_RD
| CTLFLAG_WR
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
544 (void*)REQ_THREAD_COUNTERS
,
545 sizeof(uint64_t), kpc_sysctl
,
546 "QU", "Current thread counters");
548 SYSCTL_PROC(_kpc
, OID_AUTO
, counters
,
549 CTLFLAG_RD
| CTLFLAG_WR
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
551 sizeof(uint64_t), kpc_sysctl
,
552 "QU", "Current counters");
554 SYSCTL_PROC(_kpc
, OID_AUTO
, shadow_counters
,
555 CTLFLAG_RD
| CTLFLAG_WR
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
556 (void*)REQ_SHADOW_COUNTERS
,
557 sizeof(uint64_t), kpc_sysctl
,
558 "QU", "Current shadow counters");
560 SYSCTL_PROC(_kpc
, OID_AUTO
, config
,
561 CTLFLAG_RD
| CTLFLAG_WR
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
563 sizeof(uint64_t), kpc_sysctl
,
564 "QU", "Set counter configs");
566 SYSCTL_PROC(_kpc
, OID_AUTO
, period
,
567 CTLFLAG_RD
| CTLFLAG_WR
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
569 sizeof(uint64_t), kpc_sysctl
,
570 "QU", "Set counter periods");
572 SYSCTL_PROC(_kpc
, OID_AUTO
, actionid
,
573 CTLFLAG_RD
| CTLFLAG_WR
| CTLFLAG_ANYBODY
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
575 sizeof(uint32_t), kpc_sysctl
,
576 "QU", "Set counter actionids");