]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kperf/kdebug_trigger.c
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / osfmk / kperf / kdebug_trigger.c
CommitLineData
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
59boolean_t kperf_kdebug_active = FALSE;
60static void kperf_kdebug_update(void);
61
62static uint8_t kperf_kdebug_action = 0;
63
64static 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
70enum 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
79const 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
f427ee49
A
97void
98kperf_kdebug_setup(void)
39037602
A
99{
100 kperf_kdebug_filter = kalloc_tag(sizeof(*kperf_kdebug_filter),
0a7de745 101 VM_KERN_MEMORY_DIAG);
39037602 102 bzero(kperf_kdebug_filter, sizeof(*kperf_kdebug_filter));
39037602
A
103}
104
105void
106kperf_kdebug_reset(void)
107{
f427ee49 108 kperf_setup();
39037602
A
109
110 kperf_kdebug_action = 0;
111 bzero(kperf_kdebug_filter, sizeof(*kperf_kdebug_filter));
112 kperf_kdebug_update();
113}
114
115boolean_t
116kperf_kdebug_should_trigger(uint32_t debugid)
117{
118 /* ignore kperf events */
119 if (KDBG_EXTRACT_CLASS(debugid) == DBG_PERF) {
120 return FALSE;
121 }
122
123 /*
124 * Search linearly through list of debugids and masks. If the filter
125 * gets larger than 128 bytes, change this to either a binary search or
126 * a sparse bitmap on the uint32_t range, depending on the new size.
127 */
128 for (uint8_t i = 0; i < kperf_kdebug_filter->n_debugids; i++) {
129 uint32_t check_debugid =
0a7de745 130 kperf_kdebug_filter->debugids[i];
39037602
A
131 uint32_t mask = debugid_masks[DECODE_TYPE(kperf_kdebug_filter->types, i)];
132
133 if ((debugid & mask) == check_debugid) {
134 return TRUE;
135 }
136 }
137
138 return FALSE;
139}
140
141int
142kperf_kdebug_set_filter(user_addr_t user_filter, uint32_t user_size)
143{
144 uint32_t n_debugids_provided = 0;
145 int err = 0;
146
f427ee49 147 kperf_setup();
39037602 148
d9a64523
A
149 n_debugids_provided = (uint32_t)KPERF_KDEBUG_N_DEBUGIDS(user_size);
150
39037602 151 /* detect disabling the filter completely */
d9a64523 152 if (n_debugids_provided == 0) {
39037602
A
153 bzero(kperf_kdebug_filter, sizeof(*kperf_kdebug_filter));
154 goto out;
155 }
156
39037602
A
157 if ((err = kperf_kdebug_set_n_debugids(n_debugids_provided))) {
158 goto out;
159 }
160
161 if ((err = copyin(user_filter, (char *)kperf_kdebug_filter,
0a7de745 162 KPERF_KDEBUG_FILTER_SIZE(n_debugids_provided)))) {
39037602
A
163 bzero(kperf_kdebug_filter, sizeof(*kperf_kdebug_filter));
164 goto out;
165 }
166
167out:
168 kperf_kdebug_update();
169
170 return err;
171}
172
173uint32_t
174kperf_kdebug_get_filter(struct kperf_kdebug_filter **filter)
175{
f427ee49 176 kperf_setup();
39037602
A
177
178 assert(filter != NULL);
179
180 *filter = kperf_kdebug_filter;
181 return kperf_kdebug_filter->n_debugids;
182}
183
184int
185kperf_kdebug_set_n_debugids(uint32_t n_debugids_in)
186{
f427ee49 187 kperf_setup();
39037602
A
188
189 if (n_debugids_in > KPERF_KDEBUG_DEBUGIDS_MAX) {
190 return EINVAL;
191 }
192
193 kperf_kdebug_filter->n_debugids = n_debugids_in;
194
195 return 0;
196}
197
198int
199kperf_kdebug_set_action(int action_id)
200{
201 if (action_id < 0 || (unsigned int)action_id > kperf_action_get_count()) {
202 return EINVAL;
203 }
204
205 kperf_kdebug_action = action_id;
206 kperf_kdebug_update();
207
208 return 0;
209}
210
211int
212kperf_kdebug_get_action(void)
213{
214 return kperf_kdebug_action;
215}
216
217static void
218kperf_kdebug_update(void)
219{
f427ee49 220 kperf_setup();
39037602
A
221
222 if (kperf_kdebug_action != 0 &&
0a7de745 223 kperf_kdebug_filter->n_debugids != 0) {
39037602
A
224 kperf_kdebug_active = TRUE;
225 } else {
226 kperf_kdebug_active = FALSE;
227 }
228}