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