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