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