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