1*699cd480SApple OSS Distributions /*
2*699cd480SApple OSS Distributions * Copyright (c) 2016 Apple Computer, Inc. All rights reserved.
3*699cd480SApple OSS Distributions *
4*699cd480SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*699cd480SApple OSS Distributions *
6*699cd480SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*699cd480SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*699cd480SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*699cd480SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*699cd480SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*699cd480SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*699cd480SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*699cd480SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*699cd480SApple OSS Distributions *
15*699cd480SApple OSS Distributions * Please obtain a copy of the License at
16*699cd480SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*699cd480SApple OSS Distributions *
18*699cd480SApple OSS Distributions * The Original Code and all software distributed under the License are
19*699cd480SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*699cd480SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*699cd480SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*699cd480SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*699cd480SApple OSS Distributions * Please see the License for the specific language governing rights and
24*699cd480SApple OSS Distributions * limitations under the License.
25*699cd480SApple OSS Distributions *
26*699cd480SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*699cd480SApple OSS Distributions */
28*699cd480SApple OSS Distributions
29*699cd480SApple OSS Distributions /*
30*699cd480SApple OSS Distributions * kperf's kdebug trigger is a precise mechanism for taking samples of the
31*699cd480SApple OSS Distributions * thread tracing a kdebug event.
32*699cd480SApple OSS Distributions *
33*699cd480SApple OSS Distributions * The filter used by kperf differs from kdebug's typefilter. kperf's filter
34*699cd480SApple OSS Distributions * is small -- only around 140 bytes, as opposed to kdebug's 8KB filter. It
35*699cd480SApple OSS Distributions * can also target precise debug IDs, instead of only being able to specify
36*699cd480SApple OSS Distributions * an entire subclass in a kdebug typefilter. Function specifiers can be
37*699cd480SApple OSS Distributions * provided to match against along with a class or subclass. For instance, this
38*699cd480SApple OSS Distributions * allows the kperf filter to only trigger a sample if an ending syscall event
39*699cd480SApple OSS Distributions * (DBG_BSD, DBG_BSD_EXCP_SC) occurs.
40*699cd480SApple OSS Distributions *
41*699cd480SApple OSS Distributions * The tradeoff for this flexibility is that only KPERF_KDEBUG_DEBUGIDS_MAX (32)
42*699cd480SApple OSS Distributions * classes, subclasses, or exact debug IDs can be filtered at one time.
43*699cd480SApple OSS Distributions *
44*699cd480SApple OSS Distributions * The filter consists of up to 32 debug IDs and an array of 2-bit type codes
45*699cd480SApple OSS Distributions * packed into a 64-bit value. To determine if a given debug ID should trigger
46*699cd480SApple OSS Distributions * a kperf sample, each debug ID is checked. The type code is unpacked from the
47*699cd480SApple OSS Distributions * 64-bit value to apply a mask to the debug ID. Then, a sample occurs if the
48*699cd480SApple OSS Distributions * masked debug ID is equal to the debug ID in the filter's list.
49*699cd480SApple OSS Distributions */
50*699cd480SApple OSS Distributions
51*699cd480SApple OSS Distributions #include <kern/kalloc.h>
52*699cd480SApple OSS Distributions #include <kperf/action.h>
53*699cd480SApple OSS Distributions #include <kperf/buffer.h>
54*699cd480SApple OSS Distributions #include <kperf/context.h>
55*699cd480SApple OSS Distributions #include <kperf/kdebug_trigger.h>
56*699cd480SApple OSS Distributions #include <kperf/kperf.h>
57*699cd480SApple OSS Distributions #include <sys/errno.h>
58*699cd480SApple OSS Distributions
59*699cd480SApple OSS Distributions boolean_t kperf_kdebug_active = FALSE;
60*699cd480SApple OSS Distributions static void kperf_kdebug_update(void);
61*699cd480SApple OSS Distributions
62*699cd480SApple OSS Distributions static uint8_t kperf_kdebug_action = 0;
63*699cd480SApple OSS Distributions
64*699cd480SApple OSS Distributions static struct kperf_kdebug_filter {
65*699cd480SApple OSS Distributions uint64_t types[2];
66*699cd480SApple OSS Distributions uint32_t debugids[KPERF_KDEBUG_DEBUGIDS_MAX];
67*699cd480SApple OSS Distributions uint8_t n_debugids;
68*699cd480SApple OSS Distributions } __attribute__((packed)) *kperf_kdebug_filter = NULL;
69*699cd480SApple OSS Distributions
70*699cd480SApple OSS Distributions enum kperf_kdebug_filter_type {
71*699cd480SApple OSS Distributions KPERF_KDEBUG_FILTER_CLASS,
72*699cd480SApple OSS Distributions KPERF_KDEBUG_FILTER_CLASS_FN,
73*699cd480SApple OSS Distributions KPERF_KDEBUG_FILTER_CSC,
74*699cd480SApple OSS Distributions KPERF_KDEBUG_FILTER_CSC_FN,
75*699cd480SApple OSS Distributions KPERF_KDEBUG_FILTER_DEBUGID,
76*699cd480SApple OSS Distributions KPERF_KDEBUG_FILTER_DEBUGID_FN
77*699cd480SApple OSS Distributions };
78*699cd480SApple OSS Distributions
79*699cd480SApple OSS Distributions const static uint32_t debugid_masks[] = {
80*699cd480SApple OSS Distributions [KPERF_KDEBUG_FILTER_CLASS] = KDBG_CLASS_MASK,
81*699cd480SApple OSS Distributions [KPERF_KDEBUG_FILTER_CLASS_FN] = KDBG_CLASS_MASK | KDBG_FUNC_MASK,
82*699cd480SApple OSS Distributions [KPERF_KDEBUG_FILTER_CSC] = KDBG_CSC_MASK,
83*699cd480SApple OSS Distributions [KPERF_KDEBUG_FILTER_CSC_FN] = KDBG_CSC_MASK | KDBG_FUNC_MASK,
84*699cd480SApple OSS Distributions [KPERF_KDEBUG_FILTER_DEBUGID] = KDBG_EVENTID_MASK,
85*699cd480SApple OSS Distributions [KPERF_KDEBUG_FILTER_DEBUGID_FN] = UINT32_MAX,
86*699cd480SApple OSS Distributions };
87*699cd480SApple OSS Distributions
88*699cd480SApple OSS Distributions /*
89*699cd480SApple OSS Distributions * Types are packed into 2 64-bit fields in the filter, with 4-bits for each
90*699cd480SApple OSS Distributions * type. Only 3 bits are strictly necessary, but using 4 simplifies the
91*699cd480SApple OSS Distributions * unpacking.
92*699cd480SApple OSS Distributions */
93*699cd480SApple OSS Distributions
94*699cd480SApple OSS Distributions /* UNSAFE */
95*699cd480SApple OSS Distributions #define DECODE_TYPE(TYPES, I) ((((uint8_t *)(TYPES))[(I) / 2] >> ((I) % 2) * 4) & 0xf)
96*699cd480SApple OSS Distributions
97*699cd480SApple OSS Distributions void
kperf_kdebug_setup(void)98*699cd480SApple OSS Distributions kperf_kdebug_setup(void)
99*699cd480SApple OSS Distributions {
100*699cd480SApple OSS Distributions kperf_kdebug_filter = zalloc_permanent_type(struct kperf_kdebug_filter);
101*699cd480SApple OSS Distributions }
102*699cd480SApple OSS Distributions
103*699cd480SApple OSS Distributions void
kperf_kdebug_reset(void)104*699cd480SApple OSS Distributions kperf_kdebug_reset(void)
105*699cd480SApple OSS Distributions {
106*699cd480SApple OSS Distributions kperf_setup();
107*699cd480SApple OSS Distributions
108*699cd480SApple OSS Distributions kperf_kdebug_action = 0;
109*699cd480SApple OSS Distributions bzero(kperf_kdebug_filter, sizeof(*kperf_kdebug_filter));
110*699cd480SApple OSS Distributions kperf_kdebug_update();
111*699cd480SApple OSS Distributions }
112*699cd480SApple OSS Distributions
113*699cd480SApple OSS Distributions boolean_t
kperf_kdebug_should_trigger(uint32_t debugid)114*699cd480SApple OSS Distributions kperf_kdebug_should_trigger(uint32_t debugid)
115*699cd480SApple OSS Distributions {
116*699cd480SApple OSS Distributions /* ignore kperf events */
117*699cd480SApple OSS Distributions if (KDBG_EXTRACT_CLASS(debugid) == DBG_PERF) {
118*699cd480SApple OSS Distributions return FALSE;
119*699cd480SApple OSS Distributions }
120*699cd480SApple OSS Distributions
121*699cd480SApple OSS Distributions /*
122*699cd480SApple OSS Distributions * Search linearly through list of debugids and masks. If the filter
123*699cd480SApple OSS Distributions * gets larger than 128 bytes, change this to either a binary search or
124*699cd480SApple OSS Distributions * a sparse bitmap on the uint32_t range, depending on the new size.
125*699cd480SApple OSS Distributions */
126*699cd480SApple OSS Distributions for (uint8_t i = 0; i < kperf_kdebug_filter->n_debugids; i++) {
127*699cd480SApple OSS Distributions uint32_t check_debugid =
128*699cd480SApple OSS Distributions kperf_kdebug_filter->debugids[i];
129*699cd480SApple OSS Distributions uint32_t mask = debugid_masks[DECODE_TYPE(kperf_kdebug_filter->types, i)];
130*699cd480SApple OSS Distributions
131*699cd480SApple OSS Distributions if ((debugid & mask) == check_debugid) {
132*699cd480SApple OSS Distributions return TRUE;
133*699cd480SApple OSS Distributions }
134*699cd480SApple OSS Distributions }
135*699cd480SApple OSS Distributions
136*699cd480SApple OSS Distributions return FALSE;
137*699cd480SApple OSS Distributions }
138*699cd480SApple OSS Distributions
139*699cd480SApple OSS Distributions int
kperf_kdebug_set_filter(user_addr_t user_filter,uint32_t user_size)140*699cd480SApple OSS Distributions kperf_kdebug_set_filter(user_addr_t user_filter, uint32_t user_size)
141*699cd480SApple OSS Distributions {
142*699cd480SApple OSS Distributions uint32_t n_debugids_provided = 0;
143*699cd480SApple OSS Distributions int err = 0;
144*699cd480SApple OSS Distributions
145*699cd480SApple OSS Distributions kperf_setup();
146*699cd480SApple OSS Distributions
147*699cd480SApple OSS Distributions n_debugids_provided = (uint32_t)KPERF_KDEBUG_N_DEBUGIDS(user_size);
148*699cd480SApple OSS Distributions
149*699cd480SApple OSS Distributions /* detect disabling the filter completely */
150*699cd480SApple OSS Distributions if (n_debugids_provided == 0) {
151*699cd480SApple OSS Distributions bzero(kperf_kdebug_filter, sizeof(*kperf_kdebug_filter));
152*699cd480SApple OSS Distributions goto out;
153*699cd480SApple OSS Distributions }
154*699cd480SApple OSS Distributions
155*699cd480SApple OSS Distributions if ((err = kperf_kdebug_set_n_debugids(n_debugids_provided))) {
156*699cd480SApple OSS Distributions goto out;
157*699cd480SApple OSS Distributions }
158*699cd480SApple OSS Distributions
159*699cd480SApple OSS Distributions if ((err = copyin(user_filter, (char *)kperf_kdebug_filter,
160*699cd480SApple OSS Distributions KPERF_KDEBUG_FILTER_SIZE(n_debugids_provided)))) {
161*699cd480SApple OSS Distributions bzero(kperf_kdebug_filter, sizeof(*kperf_kdebug_filter));
162*699cd480SApple OSS Distributions goto out;
163*699cd480SApple OSS Distributions }
164*699cd480SApple OSS Distributions
165*699cd480SApple OSS Distributions out:
166*699cd480SApple OSS Distributions kperf_kdebug_update();
167*699cd480SApple OSS Distributions
168*699cd480SApple OSS Distributions return err;
169*699cd480SApple OSS Distributions }
170*699cd480SApple OSS Distributions
171*699cd480SApple OSS Distributions uint32_t
kperf_kdebug_get_filter(struct kperf_kdebug_filter ** filter)172*699cd480SApple OSS Distributions kperf_kdebug_get_filter(struct kperf_kdebug_filter **filter)
173*699cd480SApple OSS Distributions {
174*699cd480SApple OSS Distributions kperf_setup();
175*699cd480SApple OSS Distributions
176*699cd480SApple OSS Distributions assert(filter != NULL);
177*699cd480SApple OSS Distributions
178*699cd480SApple OSS Distributions *filter = kperf_kdebug_filter;
179*699cd480SApple OSS Distributions return kperf_kdebug_filter->n_debugids;
180*699cd480SApple OSS Distributions }
181*699cd480SApple OSS Distributions
182*699cd480SApple OSS Distributions int
kperf_kdebug_set_n_debugids(uint32_t n_debugids_in)183*699cd480SApple OSS Distributions kperf_kdebug_set_n_debugids(uint32_t n_debugids_in)
184*699cd480SApple OSS Distributions {
185*699cd480SApple OSS Distributions kperf_setup();
186*699cd480SApple OSS Distributions
187*699cd480SApple OSS Distributions if (n_debugids_in > KPERF_KDEBUG_DEBUGIDS_MAX) {
188*699cd480SApple OSS Distributions return EINVAL;
189*699cd480SApple OSS Distributions }
190*699cd480SApple OSS Distributions
191*699cd480SApple OSS Distributions kperf_kdebug_filter->n_debugids = n_debugids_in;
192*699cd480SApple OSS Distributions
193*699cd480SApple OSS Distributions return 0;
194*699cd480SApple OSS Distributions }
195*699cd480SApple OSS Distributions
196*699cd480SApple OSS Distributions int
kperf_kdebug_set_action(int action_id)197*699cd480SApple OSS Distributions kperf_kdebug_set_action(int action_id)
198*699cd480SApple OSS Distributions {
199*699cd480SApple OSS Distributions if (action_id < 0 || (unsigned int)action_id > kperf_action_get_count()) {
200*699cd480SApple OSS Distributions return EINVAL;
201*699cd480SApple OSS Distributions }
202*699cd480SApple OSS Distributions
203*699cd480SApple OSS Distributions kperf_kdebug_action = action_id;
204*699cd480SApple OSS Distributions kperf_kdebug_update();
205*699cd480SApple OSS Distributions
206*699cd480SApple OSS Distributions return 0;
207*699cd480SApple OSS Distributions }
208*699cd480SApple OSS Distributions
209*699cd480SApple OSS Distributions int
kperf_kdebug_get_action(void)210*699cd480SApple OSS Distributions kperf_kdebug_get_action(void)
211*699cd480SApple OSS Distributions {
212*699cd480SApple OSS Distributions return kperf_kdebug_action;
213*699cd480SApple OSS Distributions }
214*699cd480SApple OSS Distributions
215*699cd480SApple OSS Distributions static void
kperf_kdebug_update(void)216*699cd480SApple OSS Distributions kperf_kdebug_update(void)
217*699cd480SApple OSS Distributions {
218*699cd480SApple OSS Distributions kperf_setup();
219*699cd480SApple OSS Distributions
220*699cd480SApple OSS Distributions if (kperf_kdebug_action != 0 &&
221*699cd480SApple OSS Distributions kperf_kdebug_filter->n_debugids != 0) {
222*699cd480SApple OSS Distributions kperf_kdebug_active = TRUE;
223*699cd480SApple OSS Distributions } else {
224*699cd480SApple OSS Distributions kperf_kdebug_active = FALSE;
225*699cd480SApple OSS Distributions }
226*699cd480SApple OSS Distributions }
227