1 /*
2 * Copyright (c) 2023 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 #pragma once
30
31 #if CONFIG_EXCLAVES
32
33 #include <sys/cdefs.h>
34 #include <stdbool.h>
35
36 #include <kern/assert.h>
37 #include <kern/debug.h>
38
39 #include <mach/exclaves.h>
40
41 #include <os/atomic_private.h>
42
43 #if DEVELOPMENT || DEBUG
44 extern unsigned int exclaves_debug;
45 #else
46 #define exclaves_debug 0
47 #endif /* DEVELOPMENT || DEBUG */
48
49 /* Flag values in exclaves_debug boot-arg/sysctl */
50 __options_closed_decl(exclaves_debug_flags, unsigned int, {
51 exclaves_debug_show_errors = 0x1,
52 exclaves_debug_show_progress = 0x2,
53 exclaves_debug_show_scheduler_request_response = 0x4,
54 exclaves_debug_show_storage_upcalls = 0x8,
55 exclaves_debug_show_iokit_upcalls = 0x10,
56 exclaves_debug_show_notification_upcalls = 0x20,
57 exclaves_debug_show_test_output = 0x40,
58 exclaves_debug_show_lifecycle_upcalls = 0x80,
59 });
60
61 #define EXCLAVES_ENABLE_SHOW_ERRORS (DEVELOPMENT || DEBUG)
62 #define EXCLAVES_ENABLE_SHOW_PROGRESS (DEVELOPMENT || DEBUG)
63 #define EXCLAVES_ENABLE_SHOW_SCHEDULER_REQUEST_RESPONSE (DEVELOPMENT || DEBUG)
64 #define EXCLAVES_ENABLE_SHOW_STORAGE_UPCALLS (DEVELOPMENT || DEBUG)
65 #define EXCLAVES_ENABLE_SHOW_IOKIT_UPCALLS (DEVELOPMENT || DEBUG)
66 #define EXCLAVES_ENABLE_SHOW_NOTIFICATION_UPCALLS (DEVELOPMENT || DEBUG)
67 #define EXCLAVES_ENABLE_SHOW_TEST_OUTPUT (DEVELOPMENT || DEBUG)
68 #define EXCLAVES_ENABLE_SHOW_LIFECYCLE_UPCALLS (DEVELOPMENT || DEBUG)
69
70 #if EXCLAVES_ENABLE_SHOW_ERRORS || EXCLAVES_ENABLE_SHOW_TEST_OUTPUT
71 #define exclaves_debug_show_errors_flag (exclaves_debug_show_errors|exclaves_debug_show_test_output)
72 #else
73 #define exclaves_debug_show_errors_flag 0
74 #endif
75 #if EXCLAVES_ENABLE_SHOW_PROGRESS
76 #define exclaves_debug_show_progress_flag exclaves_debug_show_progress
77 #else
78 #define exclaves_debug_show_progress_flag 0
79 #endif
80 #if EXCLAVES_ENABLE_SHOW_SCHEDULER_REQUEST_RESPONSE
81 #define exclaves_debug_show_scheduler_request_response_flag \
82 exclaves_debug_show_scheduler_request_response
83 #else
84 #define exclaves_debug_show_scheduler_request_response_flag 0
85 #endif
86 #if EXCLAVES_ENABLE_SHOW_STORAGE_UPCALLS
87 #define exclaves_debug_show_storage_upcalls_flag \
88 exclaves_debug_show_storage_upcalls
89 #else
90 #define exclaves_debug_show_storage_upcalls_flag 0
91 #endif
92 #if EXCLAVES_ENABLE_SHOW_IOKIT_UPCALLS
93 #define exclaves_debug_show_iokit_upcalls_flag exclaves_debug_show_iokit_upcalls
94 #else
95 #define exclaves_debug_show_iokit_upcalls_flag 0
96 #endif
97 #if EXCLAVES_ENABLE_SHOW_NOTIFICATION_UPCALLS
98 #define exclaves_debug_show_notification_upcalls_flag exclaves_debug_show_notification_upcalls
99 #else
100 #define exclaves_debug_show_notification_upcalls_flag 0
101 #endif
102 #if EXCLAVES_ENABLE_SHOW_TEST_OUTPUT
103 #define exclaves_debug_show_test_output_flag exclaves_debug_show_test_output
104 #else
105 #define exclaves_debug_show_test_output_flag 0
106 #endif
107 #if EXCLAVES_ENABLE_SHOW_LIFECYCLE_UPCALLS
108 #define exclaves_debug_show_lifecycle_upcalls_flag exclaves_debug_show_lifecycle_upcalls
109 #else
110 #define exclaves_debug_show_lifecycle_upcalls_flag 0
111 #endif
112
113 #define exclaves_debug_enabled(flag) \
114 ((bool)(exclaves_debug & exclaves_debug_##flag##_flag))
115 #define exclaves_debug_printf(flag, format, ...) ({ \
116 if (exclaves_debug_enabled(flag)) { \
117 printf(format, ##__VA_ARGS__); \
118 }})
119
120
121 #pragma mark exclaves relaxed requirement management
122
123 #if DEVELOPMENT || DEVELOPMENT
124 extern exclaves_requirement_t exclaves_relaxed_requirements;
125 #else
126 extern const exclaves_requirement_t exclaves_relaxed_requirements;
127 #endif /* DEVELOPMENT || DEBUG */
128
129 /*
130 * Return true if the specified exclaves requirement has been relaxed, false
131 * otherwise.
132 */
133 static inline bool
exclaves_requirement_is_relaxed(exclaves_requirement_t requirement)134 exclaves_requirement_is_relaxed(exclaves_requirement_t requirement)
135 {
136 assert3u(requirement & (requirement - 1), ==, 0);
137 return (requirement & exclaves_relaxed_requirements) != 0;
138 }
139
140 #if DEVELOPMENT || DEBUG
141 static inline void
exclaves_requirement_relax(exclaves_requirement_t requirement)142 exclaves_requirement_relax(exclaves_requirement_t requirement)
143 {
144 assert3u(requirement & (requirement - 1), ==, 0);
145 os_atomic_or(&exclaves_relaxed_requirements, requirement, relaxed);
146 }
147 #else
148 #define exclaves_requirement_relax(req)
149 #endif /* DEVELOPMENT || DEBUG */
150 /*
151 * Called when a requirement has not been met. Produces a log message and
152 * continues if the requirement is relaxed, otherwise panics.
153 */
154 #define exclaves_requirement_assert(requirement, fmt, ...) { \
155 if (exclaves_requirement_is_relaxed(requirement)) { \
156 exclaves_debug_printf(show_errors, \
157 "exclaves: requirement was relaxed, ignoring error: " \
158 fmt "\n", ##__VA_ARGS__); \
159 } else { \
160 panic("exclaves: requirement failed: " fmt, \
161 ##__VA_ARGS__); \
162 } \
163 };
164
165 #endif /* CONFIG_EXCLAVES */
166