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
39
40 /* Configuration values mutable only at init time */
41 typedef struct {
42 uint64_t boot_time_ns;
43 vm_offset_t trace_buff_offset;
44 uint32_t trace_buff_len;
45 } socd_client_cfg_t;
46
47 static SECURITY_READ_ONLY_LATE(socd_client_cfg_t) socd_client_cfg = {0};
48 static SECURITY_READ_ONLY_LATE(bool) socd_client_trace_available = false;
49
50 /* Run-time state */
51 static struct {
52 _Atomic uint32_t trace_idx;
53 } socd_client_state = {0};
54
55 static void
socd_client_init(void)56 socd_client_init(void)
57 {
58 socd_client_hdr_t hdr = {0};
59 bool already_initialized = os_atomic_load(&socd_client_trace_available, relaxed);
60
61 if (!already_initialized) {
62 vm_size_t buff_size;
63 vm_size_t trace_buff_size;
64
65 buff_size = PE_init_socd_client();
66 if (!buff_size) {
67 return;
68 }
69
70 if (os_sub_overflow(buff_size, sizeof(hdr), &trace_buff_size)) {
71 panic("socd buffer size is too small");
72 }
73
74 absolutetime_to_nanoseconds(mach_continuous_time(), &(socd_client_cfg.boot_time_ns));
75 socd_client_cfg.trace_buff_offset = sizeof(hdr);
76 socd_client_cfg.trace_buff_len = (uint32_t)(trace_buff_size / sizeof(socd_client_trace_entry_t));
77 }
78
79 hdr.version = SOCD_CLIENT_HDR_VERSION;
80 hdr.boot_time = socd_client_cfg.boot_time_ns;
81 memcpy(&hdr.kernel_uuid, kernel_uuid, sizeof(hdr.kernel_uuid));
82 PE_write_socd_client_buffer(0, &hdr, sizeof(hdr));
83 if (!already_initialized) {
84 os_atomic_store(&socd_client_trace_available, true, release);
85 }
86 }
87 STARTUP(PMAP_STEAL, 0, socd_client_init);
88
89 static void
socd_client_set_primary_kernelcache_uuid(void)90 socd_client_set_primary_kernelcache_uuid(void)
91 {
92 long available = os_atomic_load(&socd_client_trace_available, relaxed);
93 if (kernelcache_uuid_valid && available) {
94 PE_write_socd_client_buffer(offsetof(socd_client_hdr_t, primary_kernelcache_uuid), &kernelcache_uuid, sizeof(kernelcache_uuid));
95 }
96 }
97 STARTUP(EARLY_BOOT, 0, socd_client_set_primary_kernelcache_uuid);
98
99 void
socd_client_reinit(void)100 socd_client_reinit(void)
101 {
102 socd_client_init();
103 socd_client_set_primary_kernelcache_uuid();
104 }
105
106 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)107 socd_client_trace(
108 uint32_t debugid,
109 socd_client_trace_arg_t arg1,
110 socd_client_trace_arg_t arg2,
111 socd_client_trace_arg_t arg3,
112 socd_client_trace_arg_t arg4)
113 {
114 socd_client_trace_entry_t entry;
115 uint32_t trace_idx, buff_idx, len;
116 uint64_t time_ns;
117 long available;
118 vm_offset_t offset;
119
120 available = os_atomic_load(&socd_client_trace_available, dependency);
121 if (__probable(available)) {
122 len = os_atomic_load_with_dependency_on(&socd_client_cfg.trace_buff_len, available);
123 offset = os_atomic_load_with_dependency_on(&socd_client_cfg.trace_buff_offset, available);
124 /* trace_idx is allowed to overflow */
125 trace_idx = os_atomic_inc_orig(&socd_client_state.trace_idx, relaxed);
126 buff_idx = trace_idx % len;
127
128 absolutetime_to_nanoseconds(mach_continuous_time(), &time_ns);
129 entry.timestamp = time_ns;
130 entry.debugid = debugid;
131 entry.arg1 = arg1;
132 entry.arg2 = arg2;
133 entry.arg3 = arg3;
134 entry.arg4 = arg4;
135 PE_write_socd_client_buffer(offset + (buff_idx * sizeof(entry)), &entry, sizeof(entry));
136 }
137
138 /* Duplicate tracepoint to kdebug */
139 if (!debug_is_current_cpu_in_panic_state()) {
140 KDBG(debugid, arg1, arg2, arg3, arg4);
141 }
142 }
143