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