]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/reason.h
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / sys / reason.h
CommitLineData
39037602 1/*
cb323159 2 * Copyright (c) 2019 Apple Inc. All rights reserved.
39037602
A
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#ifndef _REASON_H_
30#define _REASON_H_
31
32#include <stdint.h>
33
34__BEGIN_DECLS
35
d190cdc3 36#ifdef KERNEL_PRIVATE
39037602
A
37
38#include <kern/kern_cdata.h>
cb323159 39#include <os/refcnt.h>
39037602
A
40
41#ifdef XNU_KERNEL_PRIVATE
42#include <kern/locks.h>
43
44typedef struct os_reason {
cb323159
A
45 decl_lck_mtx_data(, osr_lock);
46 os_refcnt_t osr_refcount;
0a7de745
A
47 uint32_t osr_namespace;
48 uint64_t osr_code;
49 uint64_t osr_flags;
50 uint32_t osr_bufsize;
51 struct kcdata_descriptor osr_kcd_descriptor;
52 char *osr_kcd_buf;
39037602
A
53} *os_reason_t;
54
55#define OS_REASON_NULL ((os_reason_t) 0)
56
57/* We only include 800 bytes of the exit reason description to not blow through the panic buffer */
58#define LAUNCHD_PANIC_REASON_STRING_MAXLEN "800"
59
60void os_reason_init(void);
61
62os_reason_t build_userspace_exit_reason(uint32_t reason_namespace, uint64_t reason_code, user_addr_t payload, uint32_t payload_size,
0a7de745 63 user_addr_t reason_string, uint64_t reason_flags);
39037602
A
64char *launchd_exit_reason_get_string_desc(os_reason_t exit_reason);
65
d190cdc3
A
66/* The blocking allocation is currently not exported to KEXTs */
67int os_reason_alloc_buffer(os_reason_t cur_reason, uint32_t osr_bufsize);
68
39037602
A
69#else /* XNU_KERNEL_PRIVATE */
70
71typedef void * os_reason_t;
72
73#endif /* XNU_KERNEL_PRIVATE */
74
75os_reason_t os_reason_create(uint32_t osr_namespace, uint64_t osr_code);
d190cdc3 76int os_reason_alloc_buffer_noblock(os_reason_t cur_reason, uint32_t osr_bufsize);
39037602
A
77struct kcdata_descriptor * os_reason_get_kcdata_descriptor(os_reason_t cur_reason);
78void os_reason_ref(os_reason_t cur_reason);
79void os_reason_free(os_reason_t cur_reason);
cb323159
A
80void os_reason_set_flags(os_reason_t cur_reason, uint64_t flags);
81void os_reason_set_description_data(os_reason_t cur_reason, uint32_t type, void *reason_data, uint32_t reason_data_len);
d190cdc3 82#endif /* KERNEL_PRIVATE */
39037602
A
83
84/*
85 * Reason namespaces.
86 */
87#define OS_REASON_INVALID 0
88#define OS_REASON_JETSAM 1
89#define OS_REASON_SIGNAL 2
90#define OS_REASON_CODESIGNING 3
91#define OS_REASON_HANGTRACER 4
92#define OS_REASON_TEST 5
93#define OS_REASON_DYLD 6
94#define OS_REASON_LIBXPC 7
95#define OS_REASON_OBJC 8
96#define OS_REASON_EXEC 9
97#define OS_REASON_SPRINGBOARD 10
98#define OS_REASON_TCC 11
99#define OS_REASON_REPORTCRASH 12
100#define OS_REASON_COREANIMATION 13
101#define OS_REASON_AGGREGATED 14
cb323159
A
102#define OS_REASON_RUNNINGBOARD 15
103#define OS_REASON_ASSERTIOND OS_REASON_RUNNINGBOARD /* old name */
5ba3f43e
A
104#define OS_REASON_SKYWALK 16
105#define OS_REASON_SETTINGS 17
106#define OS_REASON_LIBSYSTEM 18
107#define OS_REASON_FOUNDATION 19
108#define OS_REASON_WATCHDOG 20
109#define OS_REASON_METAL 21
5c9f4661 110#define OS_REASON_WATCHKIT 22
d9a64523
A
111#define OS_REASON_GUARD 23
112#define OS_REASON_ANALYTICS 24
cb323159
A
113#define OS_REASON_SANDBOX 25
114#define OS_REASON_SECURITY 26
115#define OS_REASON_ENDPOINTSECURITY 27
eb6b6ca3 116#define OS_REASON_PAC_EXCEPTION 28
f427ee49 117#define OS_REASON_BLUETOOTH_CHIP 29
39037602
A
118
119/*
120 * Update whenever new OS_REASON namespaces are added.
121 */
f427ee49 122#define OS_REASON_MAX_VALID_NAMESPACE OS_REASON_BLUETOOTH_CHIP
39037602
A
123
124#define OS_REASON_BUFFER_MAX_SIZE 5120
125
5ba3f43e
A
126#define OS_REASON_FLAG_NO_CRASH_REPORT 0x1 /* Don't create a crash report */
127#define OS_REASON_FLAG_GENERATE_CRASH_REPORT 0x2 /* Create a crash report - the default for userspace requests */
128#define OS_REASON_FLAG_FROM_USERSPACE 0x4 /* Reason created from a userspace syscall */
129#define OS_REASON_FLAG_FAILED_DATA_COPYIN 0x8 /* We failed to copyin data from userspace */
130#define OS_REASON_FLAG_PAYLOAD_TRUNCATED 0x10 /* The payload was truncated because it was longer than allowed */
131#define OS_REASON_FLAG_BAD_PARAMS 0x20 /* Invalid parameters were passed involved with creating this reason */
132#define OS_REASON_FLAG_CONSISTENT_FAILURE 0x40 /* Whatever caused this reason to be created will happen again */
133#define OS_REASON_FLAG_ONE_TIME_FAILURE 0x80 /* Whatever caused this reason to be created was a one time issue */
134#define OS_REASON_FLAG_NO_CRASHED_TID 0x100 /* Don't include the TID that processed the exit in the crash report */
d9a64523 135#define OS_REASON_FLAG_ABORT 0x200 /* Reason created from abort_* rather than terminate_* */
f427ee49 136#define OS_REASON_FLAG_SHAREDREGION_FAULT 0x400 /* Fault happened within the shared cache region */
39037602
A
137
138/*
139 * Set of flags that are allowed to be passed from userspace
140 */
d9a64523 141#define OS_REASON_FLAG_MASK_ALLOWED_FROM_USER (OS_REASON_FLAG_CONSISTENT_FAILURE | OS_REASON_FLAG_ONE_TIME_FAILURE | OS_REASON_FLAG_NO_CRASH_REPORT | OS_REASON_FLAG_ABORT)
39037602
A
142
143/*
144 * Macros to encode the exit reason namespace and first 32 bits of code in exception code
145 * which is used by Report Crash as a hint. It should be only used as a hint since it
146 * looses higher 32 bits of exit reason code.
147 */
148#define ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(code, osr_namespace) \
149 (code) = (code) | (((osr_namespace) & ((uint64_t)UINT32_MAX)) << 32)
150#define ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(code, osr_code) \
151 (code) = (code) | ((osr_code) & ((uint64_t)UINT32_MAX))
152
153#ifndef KERNEL
154/*
155 * abort_with_reason: Used to exit the current process and pass along
156 * specific information about why it is being terminated.
157 *
158 * Inputs: args->reason_namespace - OS_REASON namespace specified for the reason
159 * args->reason_code - code in the specified namespace for the reason
160 * args->reason_string - additional string formatted information about the request
161 * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above).
162 *
163 * Outputs: Does not return.
164 */
cb323159
A
165void abort_with_reason(uint32_t reason_namespace, uint64_t reason_code, const char *reason_string, uint64_t reason_flags)
166__attribute__((noreturn, cold));
39037602
A
167
168/*
169 * abort_with_payload: Used to exit the current process and pass along
170 * specific information about why it is being terminated. The payload pointer
171 * should point to structured data that can be interpreted by the consumer of
172 * exit reason information.
173 *
174 * Inputs: args->reason_namespace - OS_REASON namespace specified for the reason
175 * args->reason_code - code in the specified namespace for the reason
176 * args->payload - pointer to payload structure in user space
177 * args->payload_size - length of payload buffer (this will be truncated to EXIT_REASON_PAYLOAD_MAX_LEN)
178 * args->reason_string - additional string formatted information about the request
179 * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above).
180 *
181 * Outputs: Does not return.
182 */
183void abort_with_payload(uint32_t reason_namespace, uint64_t reason_code, void *payload, uint32_t payload_size, const char *reason_string,
cb323159 184 uint64_t reason_flags) __attribute__((noreturn, cold));
39037602
A
185
186/*
187 * terminate_with_reason: Used to terminate a specific process and pass along
188 * specific information about why it is being terminated.
189 *
190 * Inputs: args->pid - the PID of the process to be terminated
191 * args->reason_namespace - OS_REASON namespace specified for the reason
192 * args->reason_code - code in the specified namespace for the reason
193 * args->reason_string - additional string formatted information about the request
194 * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above)
195 *
196 * Outputs: EINVAL if the PID requested is the same as that of the calling process, invalid or the namespace provided is invalid.
197 * ESRCH if we couldn't find a live process with the requested PID
198 * EPERM if the caller is not privileged enough to kill the process with the requested PID
199 * returns 0 otherwise
200 */
201int terminate_with_reason(int pid, uint32_t reason_namespace, uint64_t reason_code, const char *reason_string, uint64_t reason_flags);
202
203/*
204 * terminate_with_payload: Used to terminate a specific process and pass along
205 * specific information about why it is being terminated. The payload pointer
206 * should point to structured data that can be interpreted by the consumer of
207 * exit reason information.
208 *
209 * Inputs: args->pid - the PID of the process to be terminated.
210 * args->reason_namespace - OS_REASON namespace specified for the reason
211 * args->reason_code - code in the specified namespace for the reason
212 * args->payload - pointer to payload structure in user space
213 * args->payload_size - length of payload buffer (this will be truncated to EXIT_REASON_PAYLOAD_MAX_LEN)
214 * args->reason_string - additional string formatted information about the request
215 * args->reason_flags - options requested for how the process should be terminated (see OS_REASON_FLAG_* above)
216 *
217 * Outputs: EINVAL if the PID requested is the same as that of the calling process, is invalid or the namespace provided is invalid.
218 * ESRCH if we couldn't find a live process with the requested PID
219 * EPERM if the caller is not privileged enough to kill the process with the requested PID
220 * returns 0 otherwise
221 */
222int terminate_with_payload(int pid, uint32_t reason_namespace, uint64_t reason_code, void *payload, uint32_t payload_size,
0a7de745 223 const char *reason_string, uint64_t reason_flags);
39037602
A
224#endif /* KERNEL */
225
226/*
227 * codesigning exit reasons
228 */
229#define CODESIGNING_EXIT_REASON_TASKGATED_INVALID_SIG 1
230#define CODESIGNING_EXIT_REASON_INVALID_PAGE 2
231#define CODESIGNING_EXIT_REASON_TASK_ACCESS_PORT 3
232
233/*
234 * exec path specific exit reasons
235 */
236#define EXEC_EXIT_REASON_BAD_MACHO 1
237#define EXEC_EXIT_REASON_SUGID_FAILURE 2
238#define EXEC_EXIT_REASON_ACTV_THREADSTATE 3
239#define EXEC_EXIT_REASON_STACK_ALLOC 4
240#define EXEC_EXIT_REASON_APPLE_STRING_INIT 5
241#define EXEC_EXIT_REASON_COPYOUT_STRINGS 6
242#define EXEC_EXIT_REASON_COPYOUT_DYNLINKER 7
243#define EXEC_EXIT_REASON_SECURITY_POLICY 8
244#define EXEC_EXIT_REASON_TASKGATED_OTHER 9
245#define EXEC_EXIT_REASON_FAIRPLAY_DECRYPT 10
246#define EXEC_EXIT_REASON_DECRYPT 11
247#define EXEC_EXIT_REASON_UPX 12
a39ff7e2 248#define EXEC_EXIT_REASON_NO32EXEC 13
f427ee49
A
249#define EXEC_EXIT_REASON_WRONG_PLATFORM 14
250#define EXEC_EXIT_REASON_MAIN_FD_ALLOC 15
251#define EXEC_EXIT_REASON_COPYOUT_ROSETTA 16
d9a64523
A
252/*
253 * guard reasons
254 */
255#define GUARD_REASON_VNODE 1
256#define GUARD_REASON_VIRT_MEMORY 2
257#define GUARD_REASON_MACH_PORT 3
258
39037602
A
259__END_DECLS
260
261#endif /* _REASON_H_ */