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