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