xref: /xnu-11417.121.6/osfmk/kern/socd_client.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1 /*
2  * Copyright (c) 2021, 2024 Apple 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 #include <kern/clock.h>
30 #include <kern/cpu_data.h>
31 #include <kern/debug.h>
32 #include <kern/socd_client.h>
33 #include <kern/startup.h>
34 #include <os/overflow.h>
35 #include <os/atomic_private.h>
36 #include <libkern/section_keywords.h>
37 
38 // #define SOCD_CLIENT_HDR_VERSION 0x1 // original implementation
39 #define SOCD_CLIENT_HDR_VERSION 0x2 // add 'mode' bits to debugid to support sticky tracepoints
40 
41 /* Configuration values mutable only at init time */
42 typedef struct {
43 	uint64_t boot_time_ns;
44 	vm_offset_t trace_buff_offset;
45 	uint32_t trace_buff_len;
46 } socd_client_cfg_t;
47 
48 static SECURITY_READ_ONLY_LATE(socd_client_cfg_t) socd_client_cfg = {0};
49 static SECURITY_READ_ONLY_LATE(bool) socd_client_trace_available = false;
50 static SECURITY_READ_WRITE(bool) socd_client_trace_has_sticky_events = false;
51 
52 /* Run-time state */
53 static struct {
54 	_Atomic uint32_t trace_idx;
55 } socd_client_state = {0};
56 
57 static void
socd_client_init(void)58 socd_client_init(void)
59 {
60 	socd_client_hdr_t hdr = {0};
61 	bool already_initialized = os_atomic_load(&socd_client_trace_available, relaxed);
62 
63 	if (!already_initialized) {
64 		vm_size_t buff_size;
65 		vm_size_t trace_buff_size;
66 
67 		buff_size = PE_init_socd_client();
68 		if (!buff_size) {
69 			return;
70 		}
71 
72 		if (os_sub_overflow(buff_size, sizeof(hdr), &trace_buff_size)) {
73 			panic("socd buffer size is too small");
74 		}
75 
76 		absolutetime_to_nanoseconds(mach_continuous_time(), &(socd_client_cfg.boot_time_ns));
77 		socd_client_cfg.trace_buff_offset = sizeof(hdr);
78 		socd_client_cfg.trace_buff_len = (uint32_t)(trace_buff_size / sizeof(socd_client_trace_entry_t));
79 	}
80 
81 	hdr.version = SOCD_CLIENT_HDR_VERSION;
82 	hdr.boot_time = socd_client_cfg.boot_time_ns;
83 	memcpy(&hdr.kernel_uuid, kernel_uuid, sizeof(hdr.kernel_uuid));
84 	PE_write_socd_client_buffer(0, &hdr, sizeof(hdr));
85 	if (!already_initialized) {
86 		os_atomic_store(&socd_client_trace_available, true, release);
87 	}
88 }
89 STARTUP(PMAP_STEAL, STARTUP_RANK_FIRST, socd_client_init);
90 
91 static void
socd_client_set_primary_kernelcache_uuid(void)92 socd_client_set_primary_kernelcache_uuid(void)
93 {
94 	long available = os_atomic_load(&socd_client_trace_available, relaxed);
95 	if (kernelcache_uuid_valid && available) {
96 		PE_write_socd_client_buffer(offsetof(socd_client_hdr_t, primary_kernelcache_uuid), &kernelcache_uuid, sizeof(kernelcache_uuid));
97 	}
98 }
99 STARTUP(EARLY_BOOT, STARTUP_RANK_FIRST, socd_client_set_primary_kernelcache_uuid);
100 
101 void
socd_client_reinit(void)102 socd_client_reinit(void)
103 {
104 	socd_client_init();
105 	socd_client_set_primary_kernelcache_uuid();
106 }
107 
108 void
socd_client_trace(uint32_t debugid,socd_client_trace_arg_t arg1,socd_client_trace_arg_t arg2,socd_client_trace_arg_t arg3,socd_client_trace_arg_t arg4)109 socd_client_trace(
110 	uint32_t                 debugid,
111 	socd_client_trace_arg_t  arg1,
112 	socd_client_trace_arg_t  arg2,
113 	socd_client_trace_arg_t  arg3,
114 	socd_client_trace_arg_t  arg4)
115 {
116 	socd_client_trace_entry_t entry;
117 	uint32_t trace_idx, buff_idx, len;
118 	uint64_t time_ns;
119 	long available;
120 	vm_offset_t offset;
121 	bool has_sticky;
122 	uint32_t tries = 0;
123 
124 	available = os_atomic_load(&socd_client_trace_available, dependency);
125 
126 	if (__improbable(!available)) {
127 		return;
128 	}
129 
130 	len = os_atomic_load_with_dependency_on(&socd_client_cfg.trace_buff_len, available);
131 	offset = os_atomic_load_with_dependency_on(&socd_client_cfg.trace_buff_offset, available);
132 	has_sticky = os_atomic_load_with_dependency_on(&socd_client_trace_has_sticky_events, available);
133 
134 	/* protect against the case where the buffer is full of sticky events */
135 	while (tries++ < len) {
136 		/* trace_idx is allowed to overflow */
137 		trace_idx = os_atomic_inc_orig(&socd_client_state.trace_idx, relaxed);
138 		buff_idx = trace_idx % len;
139 
140 		/* if there are no sticky events then we don't need the read */
141 		if (has_sticky) {
142 			/* skip if this slot is sticky.  Read only the debugid to reduce perf impact */
143 			PE_read_socd_client_buffer(offset + (buff_idx * sizeof(entry)) + offsetof(socd_client_trace_entry_t, debugid), &(entry.debugid), sizeof(entry.debugid));
144 			if (SOCD_TRACE_EXTRACT_MODE(entry.debugid) & SOCD_TRACE_MODE_STICKY_TRACEPOINT) {
145 				continue;
146 			}
147 		}
148 
149 		/* slot is available, write it */
150 		absolutetime_to_nanoseconds(mach_continuous_time(), &time_ns);
151 		entry.timestamp = time_ns;
152 		entry.debugid = debugid;
153 		entry.arg1 = arg1;
154 		entry.arg2 = arg2;
155 		entry.arg3 = arg3;
156 		entry.arg4 = arg4;
157 
158 		PE_write_socd_client_buffer(offset + (buff_idx * sizeof(entry)), &entry, sizeof(entry));
159 
160 		if (SOCD_TRACE_EXTRACT_MODE(entry.debugid) & SOCD_TRACE_MODE_STICKY_TRACEPOINT) {
161 			os_atomic_store(&socd_client_trace_has_sticky_events, true, relaxed);
162 		}
163 
164 		break;
165 	}
166 
167 	/* Duplicate tracepoint to kdebug */
168 	if (!debug_is_current_cpu_in_panic_state()) {
169 		KDBG(debugid, arg1, arg2, arg3, arg4);
170 	}
171 }
172