xref: /xnu-11417.101.15/bsd/sys/proc_info_private.h (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1 /*
2  * Copyright (c) 2005-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 #ifndef _SYS_PROC_INFO_PRIVATE_H
30 #define _SYS_PROC_INFO_PRIVATE_H
31 
32 #include <mach/coalition.h>
33 #include <mach/machine.h>
34 #include <mach/message.h>
35 #include <stdint.h>
36 #include <sys/cdefs.h>
37 #include <sys/event_private.h>
38 #include <sys/proc_info.h>
39 #include <sys/types.h>
40 #include <uuid/uuid.h>
41 
42 __BEGIN_DECLS
43 
44 
45 struct proc_uniqidentifierinfo {
46 	uint8_t                 p_uuid[16];             /* UUID of the main executable */
47 	uint64_t                p_uniqueid;             /* 64 bit unique identifier for process */
48 	uint64_t                p_puniqueid;            /* unique identifier for process's parent */
49 	int32_t                 p_idversion;            /* pid version */
50 	int32_t                 p_orig_ppidversion;     /* process's original parent's pid version, doesn't change if reparented */
51 	uint64_t                p_reserve2;             /* reserved for future use */
52 	uint64_t                p_reserve3;             /* reserved for future use */
53 };
54 /* This structure is API */
55 _Static_assert(sizeof(struct proc_uniqidentifierinfo) == 56, "sizeof(struct proc_uniqidentifierinfo) == 56");
56 
57 
58 struct proc_bsdinfowithuniqid {
59 	struct proc_bsdinfo             pbsd;
60 	struct proc_uniqidentifierinfo  p_uniqidentifier;
61 };
62 
63 struct proc_archinfo {
64 	cpu_type_t              p_cputype;
65 	cpu_subtype_t           p_cpusubtype;
66 };
67 
68 struct proc_pidcoalitioninfo {
69 	uint64_t coalition_id[COALITION_NUM_TYPES];
70 	uint64_t reserved1;
71 	uint64_t reserved2;
72 	uint64_t reserved3;
73 };
74 
75 struct proc_originatorinfo {
76 	uuid_t                  originator_uuid;        /* UUID of the originator process */
77 	pid_t                   originator_pid;         /* pid of the originator process */
78 	uint64_t                p_reserve2;
79 	uint64_t                p_reserve3;
80 	uint64_t                p_reserve4;
81 };
82 
83 struct proc_ipctableinfo {
84 	uint32_t               table_size;
85 	uint32_t               table_free;
86 };
87 
88 struct proc_threadschedinfo {
89 	uint64_t               int_time_ns;         /* time spent in interrupt context */
90 };
91 
92 // See PROC_PIDTHREADCOUNTS for a description of how to use these structures.
93 
94 struct proc_threadcounts_data {
95 	uint64_t ptcd_instructions;
96 	uint64_t ptcd_cycles;
97 	uint64_t ptcd_user_time_mach;
98 	uint64_t ptcd_system_time_mach;
99 	uint64_t ptcd_energy_nj;
100 };
101 
102 struct proc_threadcounts {
103 	uint16_t ptc_len;
104 	uint16_t ptc_reserved0;
105 	uint32_t ptc_reserved1;
106 	struct proc_threadcounts_data ptc_counts[];
107 };
108 
109 struct proc_delegated_signal_info {
110 	audit_token_t instigator;
111 	audit_token_t target;
112 };
113 
114 #define PROC_FLAG_DARWINBG      0x8000  /* process in darwin background */
115 #define PROC_FLAG_EXT_DARWINBG  0x10000 /* process in darwin background - external enforcement */
116 #define PROC_FLAG_IOS_APPLEDAEMON 0x20000       /* Process is apple daemon  */
117 #define PROC_FLAG_DELAYIDLESLEEP 0x40000        /* Process is marked to delay idle sleep on disk IO */
118 #define PROC_FLAG_IOS_IMPPROMOTION 0x80000      /* Process is daemon which receives importance donation  */
119 #define PROC_FLAG_ADAPTIVE              0x100000         /* Process is adaptive */
120 #define PROC_FLAG_ADAPTIVE_IMPORTANT    0x200000         /* Process is adaptive, and is currently important */
121 #define PROC_FLAG_IMPORTANCE_DONOR   0x400000 /* Process is marked as an importance donor */
122 #define PROC_FLAG_SUPPRESSED         0x800000 /* Process is suppressed */
123 #define PROC_FLAG_APPLICATION 0x1000000 /* Process is an application */
124 #define PROC_FLAG_IOS_APPLICATION PROC_FLAG_APPLICATION /* Process is an application */
125 #define PROC_FLAG_ROSETTA 0x2000000 /* Process is running translated under Rosetta */
126 #define PROC_FLAG_SEC_ENABLED 0x4000000
127 #define PROC_FLAG_SEC_BYPASS_ENABLED 0x8000000
128 
129 /* keep in sync with KQ_* in sys/eventvar.h */
130 #define PROC_KQUEUE_WORKQ       0x0040
131 #define PROC_KQUEUE_WORKLOOP    0x0080
132 
133 struct kevent_extinfo {
134 	struct kevent_qos_s kqext_kev;
135 	uint64_t kqext_sdata;
136 	int kqext_status;
137 	int kqext_sfflags;
138 	uint64_t kqext_reserved[2];
139 };
140 
141 /* Flavors for proc_pidinfo() */
142 #define PROC_PIDUNIQIDENTIFIERINFO      17
143 #define PROC_PIDUNIQIDENTIFIERINFO_SIZE \
144 	                                (sizeof(struct proc_uniqidentifierinfo))
145 
146 #define PROC_PIDT_BSDINFOWITHUNIQID     18
147 #define PROC_PIDT_BSDINFOWITHUNIQID_SIZE \
148 	                                (sizeof(struct proc_bsdinfowithuniqid))
149 
150 #define PROC_PIDARCHINFO                19
151 #define PROC_PIDARCHINFO_SIZE           \
152 	                                (sizeof(struct proc_archinfo))
153 
154 #define PROC_PIDCOALITIONINFO           20
155 #define PROC_PIDCOALITIONINFO_SIZE      (sizeof(struct proc_pidcoalitioninfo))
156 
157 #define PROC_PIDNOTEEXIT                21
158 #define PROC_PIDNOTEEXIT_SIZE           (sizeof(uint32_t))
159 
160 #define PROC_PIDREGIONPATHINFO2         22
161 #define PROC_PIDREGIONPATHINFO2_SIZE    (sizeof(struct proc_regionwithpathinfo))
162 
163 #define PROC_PIDREGIONPATHINFO3         23
164 #define PROC_PIDREGIONPATHINFO3_SIZE    (sizeof(struct proc_regionwithpathinfo))
165 
166 #define PROC_PIDEXITREASONINFO          24
167 #define PROC_PIDEXITREASONINFO_SIZE     (sizeof(struct proc_exitreasoninfo))
168 
169 #define PROC_PIDEXITREASONBASICINFO     25
170 #define PROC_PIDEXITREASONBASICINFOSIZE (sizeof(struct proc_exitreasonbasicinfo))
171 
172 #define PROC_PIDLISTUPTRS      26
173 #define PROC_PIDLISTUPTRS_SIZE (sizeof(uint64_t))
174 
175 #define PROC_PIDLISTDYNKQUEUES      27
176 #define PROC_PIDLISTDYNKQUEUES_SIZE (sizeof(kqueue_id_t))
177 
178 #define PROC_PIDLISTTHREADIDS           28
179 #define PROC_PIDLISTTHREADIDS_SIZE      (2* sizeof(uint32_t))
180 
181 #define PROC_PIDVMRTFAULTINFO           29
182 #define PROC_PIDVMRTFAULTINFO_SIZE (sizeof(vm_rtfault_record_t))
183 
184 #define PROC_PIDPLATFORMINFO 30
185 #define PROC_PIDPLATFORMINFO_SIZE (sizeof(uint32_t))
186 
187 #define PROC_PIDREGIONPATH              31
188 #define PROC_PIDREGIONPATH_SIZE         (sizeof(struct proc_regionpath))
189 
190 #define PROC_PIDIPCTABLEINFO 32
191 #define PROC_PIDIPCTABLEINFO_SIZE (sizeof(struct proc_ipctableinfo))
192 
193 #define PROC_PIDTHREADSCHEDINFO 33
194 #define PROC_PIDTHREADSCHEDINFO_SIZE (sizeof(struct proc_threadschedinfo))
195 
196 // PROC_PIDTHREADCOUNTS returns a list of counters for the given thread,
197 // separated out by the "perf-level" it was running on (typically either
198 // "performance" or "efficiency").
199 //
200 // This interface works a bit differently from the other proc_info(3) flavors.
201 // It copies out a structure with a variable-length array at the end of it.
202 // The start of the `proc_threadcounts` structure contains a header indicating
203 // the length of the subsequent array of `proc_threadcounts_data` elements.
204 //
205 // To use this interface, first read the `hw.nperflevels` sysctl to find out how
206 // large to make the allocation that receives the counter data:
207 //
208 //     sizeof(proc_threadcounts) + nperflevels * sizeof(proc_threadcounts_data)
209 //
210 // Use the `hw.perflevel[0-9].name` sysctl to find out which perf-level maps to
211 // each entry in the array.
212 //
213 // The complete usage would be (omitting error reporting):
214 //
215 //     uint32_t len = 0;
216 //     int ret = sysctlbyname("hw.nperflevels", &len, &len_sz, NULL, 0);
217 //     size_t size = sizeof(struct proc_threadcounts) +
218 //             len * sizeof(struct proc_threadcounts_data);
219 //     struct proc_threadcounts *counts = malloc(size);
220 //     // Fill this in with a thread ID, like from `PROC_PIDLISTTHREADS`.
221 //     uint64_t tid = 0;
222 //     int size_copied = proc_info(getpid(), PROC_PIDTHREADCOUNTS, tid, counts,
223 //             size);
224 
225 #define PROC_PIDTHREADCOUNTS 34
226 #define PROC_PIDTHREADCOUNTS_SIZE (sizeof(struct proc_threadcounts))
227 
228 /* Flavors for proc_pidfdinfo */
229 
230 #define PROC_PIDFDKQUEUE_EXTINFO        9
231 #define PROC_PIDFDKQUEUE_EXTINFO_SIZE   (sizeof(struct kevent_extinfo))
232 #define PROC_PIDFDKQUEUE_KNOTES_MAX     (1024 * 128)
233 #define PROC_PIDDYNKQUEUES_MAX  (1024 * 128)
234 
235 
236 /* Flavors for proc_pidoriginatorinfo */
237 #define PROC_PIDORIGINATOR_UUID         0x1
238 #define PROC_PIDORIGINATOR_UUID_SIZE    (sizeof(uuid_t))
239 
240 #define PROC_PIDORIGINATOR_BGSTATE      0x2
241 #define PROC_PIDORIGINATOR_BGSTATE_SIZE (sizeof(uint32_t))
242 
243 #define PROC_PIDORIGINATOR_PID_UUID     0x3
244 #define PROC_PIDORIGINATOR_PID_UUID_SIZE (sizeof(struct proc_originatorinfo))
245 
246 /* Flavors for proc_listcoalitions */
247 #define LISTCOALITIONS_ALL_COALS        1
248 #define LISTCOALITIONS_ALL_COALS_SIZE   (sizeof(struct procinfo_coalinfo))
249 
250 #define LISTCOALITIONS_SINGLE_TYPE      2
251 #define LISTCOALITIONS_SINGLE_TYPE_SIZE (sizeof(struct procinfo_coalinfo))
252 
253 /* reasons for proc_can_use_foreground_hw */
254 #define PROC_FGHW_OK                     0 /* pid may use foreground HW */
255 #define PROC_FGHW_DAEMON_OK              1
256 #define PROC_FGHW_DAEMON_LEADER         10 /* pid is in a daemon coalition */
257 #define PROC_FGHW_LEADER_NONUI          11 /* coalition leader is in a non-focal state */
258 #define PROC_FGHW_LEADER_BACKGROUND     12 /* coalition leader is in a background state */
259 #define PROC_FGHW_DAEMON_NO_VOUCHER     13 /* pid is a daemon with no adopted voucher */
260 #define PROC_FGHW_NO_VOUCHER_ATTR       14 /* pid has adopted a voucher with no bank/originator attribute */
261 #define PROC_FGHW_NO_ORIGINATOR         15 /* pid has adopted a voucher for a process that's gone away */
262 #define PROC_FGHW_ORIGINATOR_BACKGROUND 16 /* pid has adopted a voucher for an app that's in the background */
263 #define PROC_FGHW_VOUCHER_ERROR         98 /* error in voucher / originator callout */
264 #define PROC_FGHW_ERROR                 99 /* syscall parameter/permissions error */
265 
266 /* flavors for proc_piddynkqueueinfo */
267 #define PROC_PIDDYNKQUEUE_INFO         0
268 #define PROC_PIDDYNKQUEUE_INFO_SIZE    (sizeof(struct kqueue_dyninfo))
269 #define PROC_PIDDYNKQUEUE_EXTINFO      1
270 #define PROC_PIDDYNKQUEUE_EXTINFO_SIZE (sizeof(struct kevent_extinfo))
271 
272 /* __proc_info() call numbers */
273 #define PROC_INFO_CALL_LISTPIDS              0x1
274 #define PROC_INFO_CALL_PIDINFO               0x2
275 #define PROC_INFO_CALL_PIDFDINFO             0x3
276 #define PROC_INFO_CALL_KERNMSGBUF            0x4
277 #define PROC_INFO_CALL_SETCONTROL            0x5
278 #define PROC_INFO_CALL_PIDFILEPORTINFO       0x6
279 #define PROC_INFO_CALL_TERMINATE             0x7
280 #define PROC_INFO_CALL_DIRTYCONTROL          0x8
281 #define PROC_INFO_CALL_PIDRUSAGE             0x9
282 #define PROC_INFO_CALL_PIDORIGINATORINFO     0xa
283 #define PROC_INFO_CALL_LISTCOALITIONS        0xb
284 #define PROC_INFO_CALL_CANUSEFGHW            0xc
285 #define PROC_INFO_CALL_PIDDYNKQUEUEINFO      0xd
286 #define PROC_INFO_CALL_UDATA_INFO            0xe
287 #define PROC_INFO_CALL_SET_DYLD_IMAGES       0xf
288 #define PROC_INFO_CALL_TERMINATE_RSR         0x10
289 #define PROC_INFO_CALL_SIGNAL_AUDITTOKEN     0x11
290 #define PROC_INFO_CALL_TERMINATE_AUDITTOKEN  0x12
291 #define PROC_INFO_CALL_DELEGATE_SIGNAL       0x13
292 #define PROC_INFO_CALL_DELEGATE_TERMINATE    0x14
293 
294 /* __proc_info_extended_id() flags */
295 #define PIF_COMPARE_IDVERSION           0x01
296 #define PIF_COMPARE_UNIQUEID            0x02
297 
298 #ifdef KERNEL_PRIVATE
299 extern int proc_fdlist(proc_t p, struct proc_fdinfo *buf, size_t *count);
300 extern int proc_pidoriginatorpid_uuid(uuid_t uuid, uint32_t buffersize, pid_t *pid);
301 #endif
302 
303 #ifdef XNU_KERNEL_PRIVATE
304 #ifndef pshmnode
305 struct pshmnode;
306 #endif
307 
308 #ifndef psemnode
309 struct psemnode;
310 #endif
311 
312 #ifndef pipe
313 struct pipe;
314 #endif
315 
316 extern int fill_socketinfo(socket_t so, struct socket_info *si);
317 extern int fill_pshminfo(struct pshmnode * pshm, struct pshm_info * pinfo);
318 extern int fill_pseminfo(struct psemnode * psem, struct psem_info * pinfo);
319 extern int fill_pipeinfo(struct pipe * cpipe, struct pipe_info * pinfo);
320 extern int fill_kqueueinfo(struct kqueue * kq, struct kqueue_info * kinfo);
321 extern int pid_kqueue_extinfo(proc_t, struct kqueue * kq, user_addr_t buffer,
322     uint32_t buffersize, int32_t * retval);
323 extern int pid_kqueue_udatainfo(proc_t p, struct kqueue *kq, uint64_t *buf,
324     uint32_t bufsize);
325 extern int pid_kqueue_listdynamickqueues(proc_t p, user_addr_t ubuf,
326     uint32_t bufsize, int32_t *retval);
327 extern int pid_dynamickqueue_extinfo(proc_t p, kqueue_id_t kq_id,
328     user_addr_t ubuf, uint32_t bufsize, int32_t *retval);
329 struct kern_channel;
330 extern int fill_channelinfo(struct kern_channel * chan,
331     struct proc_channel_info *chan_info);
332 extern int fill_procworkqueue(proc_t, struct proc_workqueueinfo *);
333 extern boolean_t workqueue_get_pwq_exceeded(void *v, boolean_t *exceeded_total,
334     boolean_t *exceeded_constrained);
335 extern uint64_t workqueue_get_task_ss_flags_from_pwq_state_kdp(void *proc);
336 
337 #endif /* XNU_KERNEL_PRIVATE */
338 
339 __END_DECLS
340 
341 #endif /*_SYS_PROC_INFO_PRIVATE_H */
342