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