xref: /xnu-12377.61.12/osfmk/kern/socd_client.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
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 static bool PERCPU_DATA(is_in_buffer_write); // = false
52 
53 /* Run-time state */
54 static struct {
55 	_Atomic uint32_t trace_idx;
56 } socd_client_state = {0};
57 
58 static void
socd_client_init(void)59 socd_client_init(void)
60 {
61 	socd_client_hdr_t hdr = {0};
62 	bool already_initialized = os_atomic_load(&socd_client_trace_available, relaxed);
63 
64 	if (!already_initialized) {
65 		vm_size_t buff_size;
66 		vm_size_t trace_buff_size;
67 
68 		buff_size = PE_init_socd_client();
69 		if (!buff_size) {
70 			return;
71 		}
72 
73 		if (os_sub_overflow(buff_size, sizeof(hdr), &trace_buff_size)) {
74 			panic("socd buffer size is too small");
75 		}
76 
77 		absolutetime_to_nanoseconds(mach_continuous_time(), &(socd_client_cfg.boot_time_ns));
78 		socd_client_cfg.trace_buff_offset = sizeof(hdr);
79 		socd_client_cfg.trace_buff_len = (uint32_t)(trace_buff_size / sizeof(socd_client_trace_entry_t));
80 	}
81 
82 	hdr.version = SOCD_CLIENT_HDR_VERSION;
83 	hdr.boot_time = socd_client_cfg.boot_time_ns;
84 	memcpy(&hdr.kernel_uuid, kernel_uuid, sizeof(hdr.kernel_uuid));
85 	PE_write_socd_client_buffer(0, &hdr, sizeof(hdr));
86 	if (!already_initialized) {
87 		os_atomic_store(&socd_client_trace_available, true, release);
88 	}
89 }
90 STARTUP(PMAP_STEAL, STARTUP_RANK_FIRST, socd_client_init);
91 
92 static void
socd_client_set_primary_kernelcache_uuid(void)93 socd_client_set_primary_kernelcache_uuid(void)
94 {
95 	long available = os_atomic_load(&socd_client_trace_available, relaxed);
96 	if (kernelcache_uuid_valid && available) {
97 		PE_write_socd_client_buffer(offsetof(socd_client_hdr_t, primary_kernelcache_uuid), &kernelcache_uuid, sizeof(kernelcache_uuid));
98 	}
99 }
100 STARTUP(EARLY_BOOT, STARTUP_RANK_FIRST, socd_client_set_primary_kernelcache_uuid);
101 
102 void
socd_client_reinit(void)103 socd_client_reinit(void)
104 {
105 	socd_client_init();
106 	socd_client_set_primary_kernelcache_uuid();
107 }
108 
109 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)110 socd_client_trace(
111 	uint32_t                 debugid,
112 	socd_client_trace_arg_t  arg1,
113 	socd_client_trace_arg_t  arg2,
114 	socd_client_trace_arg_t  arg3,
115 	socd_client_trace_arg_t  arg4)
116 {
117 	socd_client_trace_entry_t entry;
118 	uint32_t trace_idx, buff_idx, len;
119 	uint64_t time_ns;
120 	long available;
121 	vm_offset_t offset;
122 	bool has_sticky;
123 	uint32_t tries = 0;
124 	bool *is_buf_wr;
125 
126 	available = os_atomic_load(&socd_client_trace_available, dependency);
127 
128 	if (__improbable(!available)) {
129 		return;
130 	}
131 
132 	/* is_in_buffer_write is an indicator that the code is in SOCD buffer write routine */
133 	is_buf_wr = PERCPU_GET(is_in_buffer_write);
134 	if (*is_buf_wr) {
135 		/* If we are here this means previously code already entered SOCD buffer write routine but never exited meaning it caused a panic.
136 		 * To avoid recursive panic returning here */
137 		return;
138 	}
139 	*is_buf_wr = true;
140 
141 	len = os_atomic_load_with_dependency_on(&socd_client_cfg.trace_buff_len, available);
142 	offset = os_atomic_load_with_dependency_on(&socd_client_cfg.trace_buff_offset, available);
143 	has_sticky = os_atomic_load_with_dependency_on(&socd_client_trace_has_sticky_events, available);
144 
145 	/* protect against the case where the buffer is full of sticky events */
146 	while (tries++ < len) {
147 		/* trace_idx is allowed to overflow */
148 		trace_idx = os_atomic_inc_orig(&socd_client_state.trace_idx, relaxed);
149 		buff_idx = trace_idx % len;
150 
151 		/* if there are no sticky events then we don't need the read */
152 		if (has_sticky) {
153 			/* skip if this slot is sticky.  Read only the debugid to reduce perf impact */
154 			PE_read_socd_client_buffer(offset + (buff_idx * sizeof(entry)) + offsetof(socd_client_trace_entry_t, debugid), &(entry.debugid), sizeof(entry.debugid));
155 			if (SOCD_TRACE_EXTRACT_MODE(entry.debugid) & SOCD_TRACE_MODE_STICKY_TRACEPOINT) {
156 				continue;
157 			}
158 		}
159 
160 		/* slot is available, write it */
161 		absolutetime_to_nanoseconds(mach_continuous_time(), &time_ns);
162 		entry.timestamp = time_ns;
163 		entry.debugid = debugid;
164 		entry.arg1 = arg1;
165 		entry.arg2 = arg2;
166 		entry.arg3 = arg3;
167 		entry.arg4 = arg4;
168 
169 		PE_write_socd_client_buffer(offset + (buff_idx * sizeof(entry)), &entry, sizeof(entry));
170 
171 		if (SOCD_TRACE_EXTRACT_MODE(entry.debugid) & SOCD_TRACE_MODE_STICKY_TRACEPOINT) {
172 			os_atomic_store(&socd_client_trace_has_sticky_events, true, relaxed);
173 		}
174 
175 		break;
176 	}
177 
178 	*is_buf_wr = false;
179 
180 	/* Duplicate tracepoint to kdebug */
181 	if (!debug_is_current_cpu_in_panic_state()) {
182 		KDBG(debugid, arg1, arg2, arg3, arg4);
183 	}
184 }
185