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