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