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