1 /*
2  * Copyright (c) 2014 Apple Inc. All rights reserved.
3  *
4  * @APPLE_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. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
13  * The Original Code and all software distributed under the License are
14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18  * Please see the License for the specific language governing rights and
19  * limitations under the License.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23 
24 #include <mach/mach_time.h>
25 #include <stdint.h>
26 #include <stdbool.h>
27 #include <stdlib.h>
28 #include <stdatomic.h>
29 #include <machine/cpu_capabilities.h>
30 #include <sys/kdebug.h>
31 #include <sys/kdebug_private.h>
32 #include <sys/kdebug_signpost.h>
33 #include <sys/errno.h>
34 #include <sys/param.h>
35 #include <sys/sysctl.h>
36 #include <mach/mach.h>
37 #include <mach/mach_vm.h>
38 
39 extern int __kdebug_typefilter(void** addr, size_t* size);
40 extern int __kdebug_trace64(uint32_t code, uint64_t arg1, uint64_t arg2,
41     uint64_t arg3, uint64_t arg4);
42 extern uint64_t __kdebug_trace_string(uint32_t debugid, uint64_t str_id,
43     const char *str);
44 
45 static int kdebug_signpost_internal(uint32_t debugid, uintptr_t arg1,
46     uintptr_t arg2, uintptr_t arg3, uintptr_t arg4);
47 
48 /*
49  * GENERAL API DESIGN NOTE!
50  *
51  * Trace API's are expected to avoid performing checks until tracing has
52  * been enabled. This includes checks that might cause error codes to be
53  * returned.
54  *
55  * Trace invocations via wrapper and syscall must have the same behavior.
56  *
57  * Note that the userspace API is chosing to optimize fastpath, non-error
58  * performance by eliding validation of each debugid. This means that error
59  * cases which could have been caught in userspace will make a syscall
60  * before returning with the correct error code. This tradeoff in performance
61  * is intentional.
62  */
63 
64 void *
kdebug_typefilter(void)65 kdebug_typefilter(void)
66 {
67 	static void* typefilter;
68 
69 	/* We expect kdebug_typefilter_bitmap to be valid (the if is not executed) */
70 	if (__builtin_expect(!typefilter, 0)) {
71 		// Map the typefilter if it can be mapped.
72 		void* ptr = NULL;
73 		size_t ptr_size = 0;
74 
75 		if (__kdebug_typefilter(&ptr, &ptr_size) == 0) {
76 			void* old_value = NULL;
77 			if (ptr && !atomic_compare_exchange_strong((void* _Atomic volatile *)&typefilter, &old_value, ptr)) {
78 				mach_vm_deallocate(mach_task_self(), (mach_vm_offset_t)ptr, KDBG_TYPEFILTER_BITMAP_SIZE);
79 			}
80 		}
81 	}
82 
83 	return typefilter;
84 }
85 
86 bool
kdebug_is_enabled(uint32_t debugid)87 kdebug_is_enabled(uint32_t debugid)
88 {
89 	uint32_t state = COMM_PAGE_READ(uint32_t, KDEBUG_ENABLE);
90 
91 	if (state == 0) {
92 		return FALSE;
93 	}
94 
95 	if ((state & KDEBUG_COMMPAGE_ENABLE_TYPEFILTER) > 0) {
96 		/*
97 		 * Typefilter rules...
98 		 *
99 		 * If no typefilter is available (even if due to error),
100 		 * debugids are allowed.
101 		 *
102 		 * The typefilter will always allow DBG_TRACE; this is a kernel
103 		 * invariant. There is no need for an explicit check here.
104 		 *
105 		 * NOTE: The typefilter will always allow DBG_TRACE, but
106 		 * it is not legal to inject DBG_TRACE via kdebug_trace.
107 		 * Attempts to do so will not be detected here, but will be
108 		 * detected in the kernel, and an error will be returned. Per
109 		 * the API design note at the top of this file, this is a
110 		 * deliberate choice.
111 		 */
112 		uint8_t* typefilter = kdebug_typefilter();
113 		if (typefilter && isset(typefilter, KDBG_EXTRACT_CSC(debugid)) == 0) {
114 			return FALSE;
115 		}
116 	}
117 
118 	return TRUE;
119 }
120 
121 bool
kdebug_using_continuous_time(void)122 kdebug_using_continuous_time(void)
123 {
124 	uint32_t state = COMM_PAGE_READ(uint32_t, KDEBUG_ENABLE);
125 	return state & KDEBUG_COMMPAGE_CONTINUOUS;
126 }
127 
128 uint64_t
kdebug_timestamp(void)129 kdebug_timestamp(void)
130 {
131 	return kdebug_using_continuous_time() ? mach_continuous_time() :
132 	       mach_absolute_time();
133 }
134 
135 uint64_t
kdebug_timestamp_from_absolute(uint64_t abstime)136 kdebug_timestamp_from_absolute(uint64_t abstime)
137 {
138 	if (kdebug_using_continuous_time()) {
139 		return abstime + *(volatile uint64_t*)_COMM_PAGE_CONT_TIMEBASE;
140 	} else {
141 		return abstime;
142 	}
143 }
144 
145 uint64_t
kdebug_timestamp_from_continuous(uint64_t conttime)146 kdebug_timestamp_from_continuous(uint64_t conttime)
147 {
148 	if (kdebug_using_continuous_time()) {
149 		return conttime;
150 	} else {
151 		return conttime - *(volatile uint64_t*)_COMM_PAGE_CONT_TIMEBASE;
152 	}
153 }
154 
155 int
kdebug_trace(uint32_t debugid,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4)156 kdebug_trace(uint32_t debugid, uint64_t arg1, uint64_t arg2, uint64_t arg3,
157     uint64_t arg4)
158 {
159 	if (!kdebug_is_enabled(debugid)) {
160 		return 0;
161 	} else {
162 		return __kdebug_trace64(debugid, arg1, arg2, arg3, arg4);
163 	}
164 }
165 
166 uint64_t
kdebug_trace_string(uint32_t debugid,uint64_t str_id,const char * str)167 kdebug_trace_string(uint32_t debugid, uint64_t str_id, const char *str)
168 {
169 	if (!kdebug_is_enabled(debugid)) {
170 		return 0;
171 	}
172 
173 	if ((int64_t)str_id == -1) {
174 		errno = EINVAL;
175 		return (uint64_t)-1;
176 	}
177 
178 	if (str_id == 0 && str == NULL) {
179 		errno = EINVAL;
180 		return (uint64_t)-1;
181 	}
182 
183 	return __kdebug_trace_string(debugid, str_id, str);
184 }
185 
186 static int
kdebug_signpost_internal(uint32_t debugid,uintptr_t arg1,uintptr_t arg2,uintptr_t arg3,uintptr_t arg4)187 kdebug_signpost_internal(uint32_t debugid, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
188 {
189 	if (KDBG_EXTRACT_CSC(debugid) != 0) {
190 		errno = EINVAL;
191 		return -1;
192 	}
193 
194 	debugid |= APPSDBG_CODE(DBG_APP_SIGNPOST, 0);
195 
196 	return kdebug_trace(debugid, arg1, arg2, arg3, arg4);
197 }
198 
199 int
kdebug_signpost(uint32_t code,uintptr_t arg1,uintptr_t arg2,uintptr_t arg3,uintptr_t arg4)200 kdebug_signpost(uint32_t code, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
201 {
202 	return kdebug_signpost_internal(code << KDBG_CODE_OFFSET, arg1, arg2, arg3, arg4);
203 }
204 
205 int
kdebug_signpost_start(uint32_t code,uintptr_t arg1,uintptr_t arg2,uintptr_t arg3,uintptr_t arg4)206 kdebug_signpost_start(uint32_t code, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
207 {
208 	return kdebug_signpost_internal((code << KDBG_CODE_OFFSET) | DBG_FUNC_START, arg1, arg2, arg3, arg4);
209 }
210 
211 int
kdebug_signpost_end(uint32_t code,uintptr_t arg1,uintptr_t arg2,uintptr_t arg3,uintptr_t arg4)212 kdebug_signpost_end(uint32_t code, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
213 {
214 	return kdebug_signpost_internal((code << KDBG_CODE_OFFSET) | DBG_FUNC_END, arg1, arg2, arg3, arg4);
215 }
216