]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/coalition.c
xnu-3248.30.4.tar.gz
[apple/xnu.git] / osfmk / kern / coalition.c
CommitLineData
fe8ab488
A
1/*
2 * Copyright (c) 2013 Apple Computer, 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/kern_types.h>
30#include <mach/mach_types.h>
31#include <mach/boolean.h>
32
33#include <kern/coalition.h>
34#include <kern/host.h>
fe8ab488 35#include <kern/kalloc.h>
3e170ce0 36#include <kern/ledger.h>
fe8ab488
A
37#include <kern/mach_param.h> /* for TASK_CHUNK */
38#include <kern/task.h>
39#include <kern/zalloc.h>
40
41#include <libkern/OSAtomic.h>
42
43#include <mach/coalition_notification_server.h>
44#include <mach/host_priv.h>
45#include <mach/host_special_ports.h>
46
47#include <sys/errno.h>
48
3e170ce0
A
49/*
50 * BSD interface functions
51 */
52int coalitions_get_list(int type, struct procinfo_coalinfo *coal_list, int list_sz);
53boolean_t coalition_is_leader(task_t task, int coal_type, coalition_t *coal);
4bd07ac2 54task_t coalition_get_leader(coalition_t coal);
3e170ce0
A
55int coalition_get_task_count(coalition_t coal);
56uint64_t coalition_get_page_count(coalition_t coal, int *ntasks);
57int coalition_get_pid_list(coalition_t coal, uint32_t rolemask, int sort_order,
58 int *pid_list, int list_sz);
59
fe8ab488
A
60/* defined in task.c */
61extern ledger_template_t task_ledger_template;
62
63/*
64 * Coalition zone needs limits. We expect there will be as many coalitions as
65 * tasks (same order of magnitude), so use the task zone's limits.
66 * */
67#define CONFIG_COALITION_MAX CONFIG_TASK_MAX
68#define COALITION_CHUNK TASK_CHUNK
69
70int unrestrict_coalition_syscalls;
71
72lck_attr_t coalitions_lck_attr;
73lck_grp_t coalitions_lck_grp;
74lck_grp_attr_t coalitions_lck_grp_attr;
75
76/* coalitions_list_lock protects coalition_count, coalitions queue, next_coalition_id. */
77decl_lck_mtx_data(static,coalitions_list_lock);
78static uint64_t coalition_count;
79static uint64_t coalition_next_id = 1;
3e170ce0 80static queue_head_t coalitions_q;
fe8ab488 81
3e170ce0 82coalition_t init_coalition[COALITION_NUM_TYPES];
fe8ab488
A
83
84zone_t coalition_zone;
85
3e170ce0
A
86static const char *coal_type_str(int type)
87{
88 switch(type) {
89 case COALITION_TYPE_RESOURCE:
90 return "RESOURCE";
91 case COALITION_TYPE_JETSAM:
92 return "JETSAM";
93 default:
94 return "<unknown>";
95 }
96}
97
98struct coalition_type {
99 int type;
100 int has_default;
101 /*
102 * init
103 * pre-condition: coalition just allocated (unlocked), unreferenced,
104 * type field set
105 */
106 kern_return_t (*init)(coalition_t coal, boolean_t privileged);
107
108 /*
109 * dealloc
110 * pre-condition: coalition unlocked
111 * pre-condition: coalition refcount=0, active_count=0,
112 * termrequested=1, terminated=1, reaped=1
113 */
114 void (*dealloc)(coalition_t coal);
115
116 /*
117 * adopt_task
118 * pre-condition: coalition locked
119 * pre-condition: coalition !repead and !terminated
120 */
121 kern_return_t (*adopt_task)(coalition_t coal, task_t task);
122
123 /*
124 * remove_task
125 * pre-condition: coalition locked
126 * pre-condition: task has been removed from coalition's task list
127 */
128 kern_return_t (*remove_task)(coalition_t coal, task_t task);
129
130 /*
131 * set_taskrole
132 * pre-condition: coalition locked
133 * pre-condition: task added to coalition's task list,
134 * active_count >= 1 (at least the given task is active)
135 */
136 kern_return_t (*set_taskrole)(coalition_t coal, task_t task, int role);
137
138 /*
139 * get_taskrole
140 * pre-condition: coalition locked
141 * pre-condition: task added to coalition's task list,
142 * active_count >= 1 (at least the given task is active)
143 */
144 int (*get_taskrole)(coalition_t coal, task_t task);
145
146 /*
147 * iterate_tasks
148 * pre-condition: coalition locked
149 */
150 void (*iterate_tasks)(coalition_t coal, void *ctx, void (*callback)(coalition_t, void *, task_t));
151};
152
153/*
154 * COALITION_TYPE_RESOURCE
155 */
fe8ab488 156
3e170ce0
A
157static kern_return_t i_coal_resource_init(coalition_t coal, boolean_t privileged);
158static void i_coal_resource_dealloc(coalition_t coal);
159static kern_return_t i_coal_resource_adopt_task(coalition_t coal, task_t task);
160static kern_return_t i_coal_resource_remove_task(coalition_t coal, task_t task);
161static kern_return_t i_coal_resource_set_taskrole(coalition_t coal,
162 task_t task, int role);
163static int i_coal_resource_get_taskrole(coalition_t coal, task_t task);
164static void i_coal_resource_iterate_tasks(coalition_t coal, void *ctx,
165 void (*callback)(coalition_t, void *, task_t));
166
167struct i_resource_coalition {
fe8ab488
A
168 ledger_t ledger;
169 uint64_t bytesread;
170 uint64_t byteswritten;
171 uint64_t gpu_time;
4bd07ac2
A
172 uint64_t logical_immediate_writes;
173 uint64_t logical_deferred_writes;
174 uint64_t logical_invalidated_writes;
175 uint64_t logical_metadata_writes;
fe8ab488 176
3e170ce0
A
177 uint64_t task_count; /* tasks that have started in this coalition */
178 uint64_t dead_task_count; /* tasks that have exited in this coalition;
179 subtract from task_count to get count
180 of "active" tasks */
fe8ab488
A
181 /*
182 * Count the length of time this coalition had at least one active task.
183 * This can be a 'denominator' to turn e.g. cpu_time to %cpu.
184 * */
185 uint64_t last_became_nonempty_time;
186 uint64_t time_nonempty;
187
3e170ce0
A
188 queue_head_t tasks; /* List of active tasks in the coalition */
189};
fe8ab488 190
3e170ce0
A
191/*
192 * COALITION_TYPE_JETSAM
193 */
fe8ab488 194
3e170ce0
A
195static kern_return_t i_coal_jetsam_init(coalition_t coal, boolean_t privileged);
196static void i_coal_jetsam_dealloc(coalition_t coal);
197static kern_return_t i_coal_jetsam_adopt_task(coalition_t coal, task_t task);
198static kern_return_t i_coal_jetsam_remove_task(coalition_t coal, task_t task);
199static kern_return_t i_coal_jetsam_set_taskrole(coalition_t coal,
200 task_t task, int role);
201static int i_coal_jetsam_get_taskrole(coalition_t coal, task_t task);
202static void i_coal_jetsam_iterate_tasks(coalition_t coal, void *ctx,
203 void (*callback)(coalition_t, void *, task_t));
204
205struct i_jetsam_coalition {
206 task_t leader;
207 queue_head_t extensions;
208 queue_head_t services;
209 queue_head_t other;
210};
fe8ab488 211
fe8ab488 212
3e170ce0
A
213/*
214 * main coalition structure
215 */
216struct coalition {
217 uint64_t id; /* monotonically increasing */
218 uint32_t type;
219 uint32_t ref_count; /* Number of references to the memory containing this struct */
220 uint32_t active_count; /* Number of members of (tasks in) the
221 coalition, plus vouchers referring
222 to the coalition */
223 uint32_t focal_task_count; /* Number of TASK_FOREGROUND_APPLICATION tasks in the coalition */
224 uint32_t nonfocal_task_count; /* Number of TASK_BACKGROUND_APPLICATION tasks in the coalition */
225
226 /* coalition flags */
227 uint32_t privileged : 1; /* Members of this coalition may create
228 and manage coalitions and may posix_spawn
229 processes into selected coalitions */
fe8ab488 230 /* ast? */
fe8ab488 231 /* voucher */
3e170ce0
A
232 uint32_t termrequested : 1; /* launchd has requested termination when coalition becomes empty */
233 uint32_t terminated : 1; /* coalition became empty and spawns are now forbidden */
234 uint32_t reaped : 1; /* reaped, invisible to userspace, but waiting for ref_count to go to zero */
235 uint32_t notified : 1; /* no-more-processes notification was sent via special port */
236#if defined(DEVELOPMENT) || defined(DEBUG)
237 uint32_t should_notify : 1; /* should this coalition send notifications (default: yes) */
238#endif
fe8ab488 239
3e170ce0 240 queue_chain_t coalitions; /* global list of coalitions */
a1c7dba1 241
3e170ce0
A
242 decl_lck_mtx_data(,lock) /* Coalition lock. */
243
244 /* put coalition type-specific structures here */
245 union {
246 struct i_resource_coalition r;
247 struct i_jetsam_coalition j;
248 };
249};
250
251/*
252 * register different coalition types:
253 * these must be kept in the order specified in coalition.h
254 */
255static const struct coalition_type
256s_coalition_types[COALITION_NUM_TYPES] = {
257 {
258 COALITION_TYPE_RESOURCE,
259 1,
260 i_coal_resource_init,
261 i_coal_resource_dealloc,
262 i_coal_resource_adopt_task,
263 i_coal_resource_remove_task,
264 i_coal_resource_set_taskrole,
265 i_coal_resource_get_taskrole,
266 i_coal_resource_iterate_tasks,
267 },
268 {
269 COALITION_TYPE_JETSAM,
270 1,
271 i_coal_jetsam_init,
272 i_coal_jetsam_dealloc,
273 i_coal_jetsam_adopt_task,
274 i_coal_jetsam_remove_task,
275 i_coal_jetsam_set_taskrole,
276 i_coal_jetsam_get_taskrole,
277 i_coal_jetsam_iterate_tasks,
278 },
fe8ab488
A
279};
280
3e170ce0
A
281#define coal_call(coal, func, ...) \
282 (s_coalition_types[(coal)->type].func)(coal, ## __VA_ARGS__)
283
284
fe8ab488
A
285#define coalition_lock(c) do{ lck_mtx_lock(&c->lock); }while(0)
286#define coalition_unlock(c) do{ lck_mtx_unlock(&c->lock); }while(0)
287
288static void
289coalition_notify_user(uint64_t id, uint32_t flags)
290{
291 mach_port_t user_port;
292 kern_return_t kr;
293
294 kr = host_get_coalition_port(host_priv_self(), &user_port);
295 if ((kr != KERN_SUCCESS) || !IPC_PORT_VALID(user_port)) {
296 return;
297 }
298
299 coalition_notification(user_port, id, flags);
300}
301
302/*
3e170ce0
A
303 *
304 * COALITION_TYPE_RESOURCE
305 *
fe8ab488 306 */
3e170ce0
A
307static kern_return_t
308i_coal_resource_init(coalition_t coal, boolean_t privileged)
fe8ab488 309{
3e170ce0
A
310 (void)privileged;
311 assert(coal && coal->type == COALITION_TYPE_RESOURCE);
312 coal->r.ledger = ledger_instantiate(task_ledger_template,
313 LEDGER_CREATE_ACTIVE_ENTRIES);
314 if (coal->r.ledger == NULL)
315 return KERN_RESOURCE_SHORTAGE;
316
317 queue_init(&coal->r.tasks);
318
319 return KERN_SUCCESS;
320}
321
322static void
323i_coal_resource_dealloc(coalition_t coal)
324{
325 assert(coal && coal->type == COALITION_TYPE_RESOURCE);
326 ledger_dereference(coal->r.ledger);
327}
328
329static kern_return_t
330i_coal_resource_adopt_task(coalition_t coal, task_t task)
331{
332 struct i_resource_coalition *cr;
333
334 assert(coal && coal->type == COALITION_TYPE_RESOURCE);
335 assert(queue_empty(&task->task_coalition[COALITION_TYPE_RESOURCE]));
336
337 cr = &coal->r;
338 cr->task_count++;
339
340 if (cr->task_count < cr->dead_task_count) {
341 panic("%s: coalition %p id:%llu type:%s task_count(%llu) < dead_task_count(%llu)",
342 __func__, coal, coal->id, coal_type_str(coal->type),
343 cr->task_count, cr->dead_task_count);
fe8ab488
A
344 }
345
3e170ce0
A
346 /* If moving from 0->1 active tasks */
347 if (cr->task_count - cr->dead_task_count == 1) {
348 cr->last_became_nonempty_time = mach_absolute_time();
fe8ab488 349 }
3e170ce0
A
350
351 /* put the task on the coalition's list of tasks */
352 enqueue_tail(&cr->tasks, &task->task_coalition[COALITION_TYPE_RESOURCE]);
353
354 coal_dbg("Added PID:%d to id:%llu, task_count:%llu, dead_count:%llu, nonempty_time:%llu",
355 task_pid(task), coal->id, cr->task_count, cr->dead_task_count,
356 cr->last_became_nonempty_time);
357
358 return KERN_SUCCESS;
359}
360
361static kern_return_t
362i_coal_resource_remove_task(coalition_t coal, task_t task)
363{
364 struct i_resource_coalition *cr;
365
366 assert(coal && coal->type == COALITION_TYPE_RESOURCE);
367 assert(task->coalition[COALITION_TYPE_RESOURCE] == coal);
368 assert(!queue_empty(&task->task_coalition[COALITION_TYPE_RESOURCE]));
369
370 /*
371 * handle resource coalition accounting rollup for dead tasks
372 */
373 cr = &coal->r;
374
375 cr->dead_task_count++;
376
377 if (cr->task_count < cr->dead_task_count) {
378 panic("%s: coalition %p id:%llu type:%s task_count(%llu) < dead_task_count(%llu)",
379 __func__, coal, coal->id, coal_type_str(coal->type), cr->task_count, cr->dead_task_count);
380 }
381
382 /* If moving from 1->0 active tasks */
383 if (cr->task_count - cr->dead_task_count == 0) {
384 uint64_t last_time_nonempty = mach_absolute_time() - cr->last_became_nonempty_time;
385 cr->last_became_nonempty_time = 0;
386 cr->time_nonempty += last_time_nonempty;
387 }
388
389 ledger_rollup(cr->ledger, task->ledger);
390 cr->bytesread += task->task_io_stats->disk_reads.size;
391 cr->byteswritten += task->task_io_stats->total_io.size - task->task_io_stats->disk_reads.size;
392 cr->gpu_time += task_gpu_utilisation(task);
4bd07ac2
A
393 cr->logical_immediate_writes += task->task_immediate_writes;
394 cr->logical_deferred_writes += task->task_deferred_writes;
395 cr->logical_invalidated_writes += task->task_invalidated_writes;
396 cr->logical_metadata_writes += task->task_metadata_writes;
3e170ce0
A
397
398 /* remove the task from the coalition's list */
399 remqueue(&task->task_coalition[COALITION_TYPE_RESOURCE]);
400 queue_chain_init(task->task_coalition[COALITION_TYPE_RESOURCE]);
401
402 coal_dbg("removed PID:%d from id:%llu, task_count:%llu, dead_count:%llu",
403 task_pid(task), coal->id, cr->task_count, cr->dead_task_count);
404
405 return KERN_SUCCESS;
406}
407
408static kern_return_t
409i_coal_resource_set_taskrole(__unused coalition_t coal,
410 __unused task_t task, __unused int role)
411{
412 return KERN_SUCCESS;
413}
414
415static int
416i_coal_resource_get_taskrole(__unused coalition_t coal, __unused task_t task)
417{
418 task_t t;
419
420 assert(coal && coal->type == COALITION_TYPE_RESOURCE);
421
422 qe_foreach_element(t, &coal->r.tasks, task_coalition[COALITION_TYPE_RESOURCE]) {
423 if (t == task)
424 return COALITION_TASKROLE_UNDEF;
425 }
426
427 return -1;
428}
429
430static void
431i_coal_resource_iterate_tasks(coalition_t coal, void *ctx, void (*callback)(coalition_t, void *, task_t))
432{
433 task_t t;
434 assert(coal && coal->type == COALITION_TYPE_RESOURCE);
435
436 qe_foreach_element(t, &coal->r.tasks, task_coalition[COALITION_TYPE_RESOURCE])
437 callback(coal, ctx, t);
fe8ab488
A
438}
439
440kern_return_t
441coalition_resource_usage_internal(coalition_t coal, struct coalition_resource_usage *cru_out)
442{
443 kern_return_t kr;
444 ledger_amount_t credit, debit;
445
3e170ce0
A
446 if (coal->type != COALITION_TYPE_RESOURCE)
447 return KERN_INVALID_ARGUMENT;
448
fe8ab488 449 ledger_t sum_ledger = ledger_instantiate(task_ledger_template, LEDGER_CREATE_ACTIVE_ENTRIES);
3e170ce0 450 if (sum_ledger == LEDGER_NULL)
fe8ab488 451 return KERN_RESOURCE_SHORTAGE;
fe8ab488
A
452
453 coalition_lock(coal);
454
455 /*
456 * Start with the coalition's ledger, which holds the totals from all
457 * the dead tasks.
458 */
3e170ce0
A
459 ledger_rollup(sum_ledger, coal->r.ledger);
460 uint64_t bytesread = coal->r.bytesread;
461 uint64_t byteswritten = coal->r.byteswritten;
462 uint64_t gpu_time = coal->r.gpu_time;
4bd07ac2
A
463 uint64_t logical_immediate_writes = coal->r.logical_immediate_writes;
464 uint64_t logical_deferred_writes = coal->r.logical_deferred_writes;
465 uint64_t logical_invalidated_writes = coal->r.logical_invalidated_writes;
466 uint64_t logical_metadata_writes = coal->r.logical_metadata_writes;
3e170ce0
A
467 int64_t cpu_time_billed_to_me = 0;
468 int64_t cpu_time_billed_to_others = 0;
469
470 kr = ledger_get_balance(sum_ledger, task_ledgers.cpu_time_billed_to_me, (int64_t *)&cpu_time_billed_to_me);
471 if (kr != KERN_SUCCESS || cpu_time_billed_to_me < 0) {
472#if DEVELOPMENT || DEBUG
473 printf("ledger_get_balance failed or ledger negative in coalition_resource_usage_internal: %lld\n", cpu_time_billed_to_me);
474#endif /* DEVELOPMENT || DEBUG */
475 cpu_time_billed_to_me = 0;
476 }
477
478 kr = ledger_get_balance(sum_ledger, task_ledgers.cpu_time_billed_to_others, (int64_t *)&cpu_time_billed_to_others);
479 if (kr != KERN_SUCCESS || cpu_time_billed_to_others < 0) {
480#if DEVELOPMENT || DEBUG
481 printf("ledger_get_balance failed or ledger negative in coalition_resource_usage_internal: %lld\n", cpu_time_billed_to_others);
482#endif /* DEVELOPMENT || DEBUG */
483 cpu_time_billed_to_others = 0;
484 }
fe8ab488
A
485
486 /*
487 * Add to that all the active tasks' ledgers. Tasks cannot deallocate
488 * out from under us, since we hold the coalition lock.
3e170ce0
A
489 * Do not use the on-behalf of cpu time from ledger for live tasks, since
490 * it will not have cpu time for active linkages between tasks.
fe8ab488
A
491 */
492 task_t task;
3e170ce0 493 qe_foreach_element(task, &coal->r.tasks, task_coalition[COALITION_TYPE_RESOURCE]) {
fe8ab488
A
494 ledger_rollup(sum_ledger, task->ledger);
495 bytesread += task->task_io_stats->disk_reads.size;
496 byteswritten += task->task_io_stats->total_io.size - task->task_io_stats->disk_reads.size;
497 gpu_time += task_gpu_utilisation(task);
4bd07ac2
A
498 logical_immediate_writes += task->task_immediate_writes;
499 logical_deferred_writes += task->task_deferred_writes;
500 logical_invalidated_writes += task->task_invalidated_writes;
501 logical_metadata_writes += task->task_metadata_writes;
3e170ce0
A
502 cpu_time_billed_to_me += (int64_t)bank_billed_time(task->bank_context);
503 cpu_time_billed_to_others += (int64_t)bank_serviced_time(task->bank_context);
fe8ab488
A
504 }
505
506 /* collect information from the coalition itself */
3e170ce0
A
507 cru_out->tasks_started = coal->r.task_count;
508 cru_out->tasks_exited = coal->r.dead_task_count;
fe8ab488 509
3e170ce0
A
510 uint64_t time_nonempty = coal->r.time_nonempty;
511 uint64_t last_became_nonempty_time = coal->r.last_became_nonempty_time;
fe8ab488
A
512
513 coalition_unlock(coal);
514
515 /* Copy the totals out of sum_ledger */
516 kr = ledger_get_entries(sum_ledger, task_ledgers.cpu_time,
517 &credit, &debit);
518 if (kr != KERN_SUCCESS) {
519 credit = 0;
520 }
521 cru_out->cpu_time = credit;
3e170ce0
A
522 cru_out->cpu_time_billed_to_me = (uint64_t)cpu_time_billed_to_me;
523 cru_out->cpu_time_billed_to_others = (uint64_t)cpu_time_billed_to_others;
fe8ab488
A
524
525 kr = ledger_get_entries(sum_ledger, task_ledgers.interrupt_wakeups,
526 &credit, &debit);
527 if (kr != KERN_SUCCESS) {
528 credit = 0;
529 }
530 cru_out->interrupt_wakeups = credit;
531
532 kr = ledger_get_entries(sum_ledger, task_ledgers.platform_idle_wakeups,
533 &credit, &debit);
534 if (kr != KERN_SUCCESS) {
535 credit = 0;
536 }
537 cru_out->platform_idle_wakeups = credit;
538
539 cru_out->bytesread = bytesread;
540 cru_out->byteswritten = byteswritten;
541 cru_out->gpu_time = gpu_time;
4bd07ac2
A
542 cru_out->logical_immediate_writes = logical_immediate_writes;
543 cru_out->logical_deferred_writes = logical_deferred_writes;
544 cru_out->logical_invalidated_writes = logical_invalidated_writes;
545 cru_out->logical_metadata_writes = logical_metadata_writes;
fe8ab488
A
546
547 ledger_dereference(sum_ledger);
548 sum_ledger = LEDGER_NULL;
549
550 if (last_became_nonempty_time) {
551 time_nonempty += mach_absolute_time() - last_became_nonempty_time;
552 }
553 absolutetime_to_nanoseconds(time_nonempty, &cru_out->time_nonempty);
554
555 return KERN_SUCCESS;
556}
557
3e170ce0
A
558/*
559 *
560 * COALITION_TYPE_JETSAM
561 *
562 */
563static kern_return_t
564i_coal_jetsam_init(coalition_t coal, boolean_t privileged)
565{
566 assert(coal && coal->type == COALITION_TYPE_JETSAM);
567 (void)privileged;
568
569 coal->j.leader= TASK_NULL;
570 queue_head_init(coal->j.extensions);
571 queue_head_init(coal->j.services);
572 queue_head_init(coal->j.other);
573
574 return KERN_SUCCESS;
575}
576
577static void
578i_coal_jetsam_dealloc(__unused coalition_t coal)
579{
580 assert(coal && coal->type == COALITION_TYPE_JETSAM);
581
582 /* the coalition should be completely clear at this point */
583 assert(queue_empty(&coal->j.extensions));
584 assert(queue_empty(&coal->j.services));
585 assert(queue_empty(&coal->j.other));
586 assert(coal->j.leader == TASK_NULL);
587}
588
589static kern_return_t
590i_coal_jetsam_adopt_task(coalition_t coal, task_t task)
591{
592 struct i_jetsam_coalition *cj;
593 assert(coal && coal->type == COALITION_TYPE_JETSAM);
594
595 cj = &coal->j;
596
597 assert(queue_empty(&task->task_coalition[COALITION_TYPE_JETSAM]));
598
599 /* put each task initially in the "other" list */
600 enqueue_tail(&cj->other, &task->task_coalition[COALITION_TYPE_JETSAM]);
601 coal_dbg("coalition %lld adopted PID:%d as UNDEF",
602 coal->id, task_pid(task));
603
604 return KERN_SUCCESS;
605}
606
607static kern_return_t
608i_coal_jetsam_remove_task(coalition_t coal, task_t task)
609{
610 assert(coal && coal->type == COALITION_TYPE_JETSAM);
611 assert(task->coalition[COALITION_TYPE_JETSAM] == coal);
612
613 coal_dbg("removing PID:%d from coalition id:%lld",
614 task_pid(task), coal->id);
615
616 if (task == coal->j.leader) {
617 coal->j.leader = NULL;
618 coal_dbg(" PID:%d was the leader!", task_pid(task));
619 } else {
620 assert(!queue_empty(&task->task_coalition[COALITION_TYPE_JETSAM]));
621 }
622
623 /* remove the task from the specific coalition role queue */
624 remqueue(&task->task_coalition[COALITION_TYPE_JETSAM]);
625 queue_chain_init(task->task_coalition[COALITION_TYPE_RESOURCE]);
626
627 return KERN_SUCCESS;
628}
629
630static kern_return_t
631i_coal_jetsam_set_taskrole(coalition_t coal, task_t task, int role)
632{
633 struct i_jetsam_coalition *cj;
634 queue_t q = NULL;
635 assert(coal && coal->type == COALITION_TYPE_JETSAM);
636 assert(task->coalition[COALITION_TYPE_JETSAM] == coal);
637
638 cj = &coal->j;
639
640 switch (role) {
641 case COALITION_TASKROLE_LEADER:
642 coal_dbg("setting PID:%d as LEADER of %lld",
643 task_pid(task), coal->id);
644 if (cj->leader != TASK_NULL) {
645 /* re-queue the exiting leader onto the "other" list */
646 coal_dbg(" re-queue existing leader (%d) as OTHER",
647 task_pid(cj->leader));
648 re_queue_tail(&cj->other, &cj->leader->task_coalition[COALITION_TYPE_JETSAM]);
649 }
650 /*
651 * remove the task from the "other" list
652 * (where it was put by default)
653 */
654 remqueue(&task->task_coalition[COALITION_TYPE_JETSAM]);
655 queue_chain_init(task->task_coalition[COALITION_TYPE_JETSAM]);
656
657 /* set the coalition leader */
658 cj->leader = task;
659 break;
660 case COALITION_TASKROLE_UNDEF:
661 coal_dbg("setting PID:%d as UNDEF in %lld",
662 task_pid(task), coal->id);
663 q = (queue_t)&cj->other;
664 break;
665 case COALITION_TASKROLE_XPC:
666 coal_dbg("setting PID:%d as XPC in %lld",
667 task_pid(task), coal->id);
668 q = (queue_t)&cj->services;
669 break;
670 case COALITION_TASKROLE_EXT:
671 coal_dbg("setting PID:%d as EXT in %lld",
672 task_pid(task), coal->id);
673 q = (queue_t)&cj->extensions;
674 break;
675 default:
676 panic("%s: invalid role(%d) for task", __func__, role);
677 return KERN_INVALID_ARGUMENT;
678 }
679
680 if (q != NULL)
681 re_queue_tail(q, &task->task_coalition[COALITION_TYPE_JETSAM]);
682
683 return KERN_SUCCESS;
684}
685
686static int
687i_coal_jetsam_get_taskrole(coalition_t coal, task_t task)
688{
689 struct i_jetsam_coalition *cj;
690 task_t t;
691
692 assert(coal && coal->type == COALITION_TYPE_JETSAM);
693 assert(task->coalition[COALITION_TYPE_JETSAM] == coal);
694
695 cj = &coal->j;
696
697 if (task == cj->leader)
698 return COALITION_TASKROLE_LEADER;
699
700 qe_foreach_element(t, &cj->services, task_coalition[COALITION_TYPE_JETSAM]) {
701 if (t == task)
702 return COALITION_TASKROLE_XPC;
703 }
704
705 qe_foreach_element(t, &cj->extensions, task_coalition[COALITION_TYPE_JETSAM]) {
706 if (t == task)
707 return COALITION_TASKROLE_EXT;
708 }
709
710 qe_foreach_element(t, &cj->other, task_coalition[COALITION_TYPE_JETSAM]) {
711 if (t == task)
712 return COALITION_TASKROLE_UNDEF;
713 }
714
715 /* task not in the coalition?! */
716 return -1;
717}
718
719static void
720i_coal_jetsam_iterate_tasks(coalition_t coal, void *ctx, void (*callback)(coalition_t, void *, task_t))
721{
722 struct i_jetsam_coalition *cj;
723 task_t t;
724
725 assert(coal && coal->type == COALITION_TYPE_JETSAM);
726
727 cj = &coal->j;
728
729 if (cj->leader)
730 callback(coal, ctx, cj->leader);
731
732 qe_foreach_element(t, &cj->services, task_coalition[COALITION_TYPE_JETSAM])
733 callback(coal, ctx, t);
734
735 qe_foreach_element(t, &cj->extensions, task_coalition[COALITION_TYPE_JETSAM])
736 callback(coal, ctx, t);
737
738 qe_foreach_element(t, &cj->other, task_coalition[COALITION_TYPE_JETSAM])
739 callback(coal, ctx, t);
740}
741
742
743/*
744 *
745 * Main Coalition implementation
746 *
747 */
748
fe8ab488
A
749/*
750 * coalition_create_internal
751 * Returns: New coalition object, referenced for the caller and unlocked.
752 * Condition: coalitions_list_lock must be UNLOCKED.
753 */
754kern_return_t
3e170ce0 755coalition_create_internal(int type, boolean_t privileged, coalition_t *out)
fe8ab488 756{
3e170ce0
A
757 kern_return_t kr;
758 struct coalition *new_coal;
759
760 if (type < 0 || type > COALITION_TYPE_MAX)
761 return KERN_INVALID_ARGUMENT;
762
763 new_coal = (struct coalition *)zalloc(coalition_zone);
764 if (new_coal == COALITION_NULL)
fe8ab488 765 return KERN_RESOURCE_SHORTAGE;
fe8ab488
A
766 bzero(new_coal, sizeof(*new_coal));
767
3e170ce0
A
768 new_coal->type = type;
769
770 /* initialize type-specific resources */
771 kr = coal_call(new_coal, init, privileged);
772 if (kr != KERN_SUCCESS) {
fe8ab488 773 zfree(coalition_zone, new_coal);
3e170ce0 774 return kr;
fe8ab488
A
775 }
776
777 /* One for caller, one for coalitions list */
778 new_coal->ref_count = 2;
779
780 new_coal->privileged = privileged ? TRUE : FALSE;
3e170ce0
A
781#if defined(DEVELOPMENT) || defined(DEBUG)
782 new_coal->should_notify = 1;
783#endif
fe8ab488
A
784
785 lck_mtx_init(&new_coal->lock, &coalitions_lck_grp, &coalitions_lck_attr);
fe8ab488
A
786
787 lck_mtx_lock(&coalitions_list_lock);
788 new_coal->id = coalition_next_id++;
789 coalition_count++;
3e170ce0 790 enqueue_tail(&coalitions_q, &new_coal->coalitions);
fe8ab488
A
791 lck_mtx_unlock(&coalitions_list_lock);
792
3e170ce0 793 coal_dbg("id:%llu, type:%s", new_coal->id, coal_type_str(new_coal->type));
fe8ab488
A
794
795 *out = new_coal;
796 return KERN_SUCCESS;
797}
798
799/*
800 * coalition_release
801 * Condition: coalition must be UNLOCKED.
802 * */
803void
804coalition_release(coalition_t coal)
805{
fe8ab488
A
806 /* TODO: This can be done with atomics. */
807 coalition_lock(coal);
808 coal->ref_count--;
3e170ce0 809
fe8ab488
A
810#if COALITION_DEBUG
811 uint32_t rc = coal->ref_count;
3e170ce0 812 uint32_t ac = coal->active_count;
fe8ab488
A
813#endif /* COALITION_DEBUG */
814
3e170ce0
A
815 coal_dbg("id:%llu type:%s ref_count:%u active_count:%u%s",
816 coal->id, coal_type_str(coal->type), rc, ac,
817 rc <= 0 ? ", will deallocate now" : "");
818
819 if (coal->ref_count > 0) {
820 coalition_unlock(coal);
821 return;
822 }
823
824 assert(coal->termrequested);
825 assert(coal->terminated);
826 assert(coal->active_count == 0);
827 assert(coal->reaped);
828 assert(coal->focal_task_count == 0);
829 assert(coal->nonfocal_task_count == 0);
830
831 coal_call(coal, dealloc);
832
fe8ab488
A
833 coalition_unlock(coal);
834
3e170ce0 835 lck_mtx_destroy(&coal->lock, &coalitions_lck_grp);
fe8ab488 836
3e170ce0
A
837 zfree(coalition_zone, coal);
838}
fe8ab488 839
3e170ce0
A
840/*
841 * coalition_find_by_id_internal
842 * Returns: Coalition object with specified id, NOT referenced.
843 * If not found, returns COALITION_NULL.
844 * Condition: coalitions_list_lock must be LOCKED.
845 */
846static coalition_t
847coalition_find_by_id_internal(uint64_t coal_id)
848{
849 if (coal_id == 0) {
850 return COALITION_NULL;
fe8ab488 851 }
3e170ce0
A
852
853 lck_mtx_assert(&coalitions_list_lock, LCK_MTX_ASSERT_OWNED);
854 coalition_t coal;
855 qe_foreach_element(coal, &coalitions_q, coalitions) {
856 if (coal->id == coal_id) {
857 return coal;
858 }
859 }
860 return COALITION_NULL;
fe8ab488
A
861}
862
863/*
864 * coalition_find_by_id
865 * Returns: Coalition object with specified id, referenced.
866 * Condition: coalitions_list_lock must be UNLOCKED.
867 */
868coalition_t
869coalition_find_by_id(uint64_t cid)
870{
871 if (cid == 0) {
872 return COALITION_NULL;
873 }
874
875 lck_mtx_lock(&coalitions_list_lock);
876
877 coalition_t coal = coalition_find_by_id_internal(cid);
878 if (coal == COALITION_NULL) {
879 lck_mtx_unlock(&coalitions_list_lock);
880 return COALITION_NULL;
881 }
882
883 coalition_lock(coal);
884
885 if (coal->reaped) {
886 coalition_unlock(coal);
887 lck_mtx_unlock(&coalitions_list_lock);
888 return COALITION_NULL;
889 }
890
891 if (coal->ref_count == 0) {
3e170ce0
A
892 panic("resurrecting coalition %p id:%llu type:%s, active_count:%u\n",
893 coal, coal->id, coal_type_str(coal->type), coal->active_count);
fe8ab488
A
894 }
895 coal->ref_count++;
896#if COALITION_DEBUG
897 uint32_t rc = coal->ref_count;
898#endif
899
900 coalition_unlock(coal);
901 lck_mtx_unlock(&coalitions_list_lock);
902
3e170ce0
A
903 coal_dbg("id:%llu type:%s ref_count:%u",
904 coal->id, coal_type_str(coal->type), rc);
905
fe8ab488
A
906 return coal;
907}
908
909/*
910 * coalition_find_and_activate_by_id
911 * Returns: Coalition object with specified id, referenced, and activated.
912 * Condition: coalitions_list_lock must be UNLOCKED.
913 * This is the function to use when putting a 'new' thing into a coalition,
914 * like posix_spawn of an XPC service by launchd.
915 * See also coalition_extend_active.
916 */
917coalition_t
918coalition_find_and_activate_by_id(uint64_t cid)
919{
920 if (cid == 0) {
921 return COALITION_NULL;
922 }
923
924 lck_mtx_lock(&coalitions_list_lock);
925
926 coalition_t coal = coalition_find_by_id_internal(cid);
927 if (coal == COALITION_NULL) {
928 lck_mtx_unlock(&coalitions_list_lock);
929 return COALITION_NULL;
930 }
931
932 coalition_lock(coal);
933
934 if (coal->reaped || coal->terminated) {
935 /* Too late to put something new into this coalition, it's
936 * already on its way out the door */
937 coalition_unlock(coal);
938 lck_mtx_unlock(&coalitions_list_lock);
939 return COALITION_NULL;
940 }
941
942 if (coal->ref_count == 0) {
3e170ce0
A
943 panic("resurrecting coalition %p id:%llu type:%s, active_count:%u\n",
944 coal, coal->id, coal_type_str(coal->type), coal->active_count);
fe8ab488
A
945 }
946
947 coal->ref_count++;
948 coal->active_count++;
949
950#if COALITION_DEBUG
951 uint32_t rc = coal->ref_count;
952 uint32_t ac = coal->active_count;
953#endif
954
955 coalition_unlock(coal);
956 lck_mtx_unlock(&coalitions_list_lock);
957
3e170ce0
A
958 coal_dbg("id:%llu type:%s ref_count:%u, active_count:%u",
959 coal->id, coal_type_str(coal->type), rc, ac);
960
fe8ab488
A
961 return coal;
962}
963
964uint64_t
965coalition_id(coalition_t coal)
966{
967 return coal->id;
968}
969
3e170ce0
A
970void
971task_coalition_ids(task_t task, uint64_t ids[COALITION_NUM_TYPES])
972{
973 int i;
974 for (i = 0; i < COALITION_NUM_TYPES; i++) {
975 if (task->coalition[i])
976 ids[i] = task->coalition[i]->id;
977 else
978 ids[i] = 0;
979 }
980}
981
982void
983task_coalition_roles(task_t task, int roles[COALITION_NUM_TYPES])
fe8ab488 984{
3e170ce0
A
985 int i;
986 memset(roles, 0, COALITION_NUM_TYPES * sizeof(roles[0]));
987
988 for (i = 0; i < COALITION_NUM_TYPES; i++) {
989 if (task->coalition[i]) {
990 coalition_lock(task->coalition[i]);
991 roles[i] = coal_call(task->coalition[i],
992 get_taskrole, task);
993 coalition_unlock(task->coalition[i]);
994 } else {
995 roles[i] = -1;
996 }
997 }
998}
999
1000
1001int
1002coalition_type(coalition_t coal)
1003{
1004 return coal->type;
fe8ab488
A
1005}
1006
1007boolean_t
1008coalition_is_privileged(coalition_t coal)
1009{
1010 return coal->privileged || unrestrict_coalition_syscalls;
1011}
1012
1013boolean_t
3e170ce0 1014task_is_in_privileged_coalition(task_t task, int type)
fe8ab488 1015{
3e170ce0
A
1016 if (type < 0 || type > COALITION_TYPE_MAX)
1017 return FALSE;
1018 if (unrestrict_coalition_syscalls)
1019 return TRUE;
1020 if (!task->coalition[type])
1021 return FALSE;
1022 return task->coalition[type]->privileged;
fe8ab488
A
1023}
1024
3e170ce0 1025void task_coalition_update_gpu_stats(task_t task, uint64_t gpu_ns_delta)
fe8ab488 1026{
3e170ce0
A
1027 coalition_t coal;
1028
1029 assert(task != TASK_NULL);
1030 if (gpu_ns_delta == 0)
1031 return;
1032
1033 coal = task->coalition[COALITION_TYPE_RESOURCE];
1034 assert(coal != COALITION_NULL);
1035
1036 coalition_lock(coal);
1037 coal->r.gpu_time += gpu_ns_delta;
1038 coalition_unlock(coal);
fe8ab488
A
1039}
1040
3e170ce0 1041uint32_t task_coalition_adjust_focal_count(task_t task, int count)
fe8ab488 1042{
3e170ce0
A
1043 coalition_t coal;
1044 uint32_t ret;
fe8ab488 1045
3e170ce0
A
1046 /*
1047 * For now: only use the resource coalition. Perhaps in the
1048 * future we may combine all coalition types, or even make
1049 * a special coalition type just for this.
1050 */
1051 coal = task->coalition[COALITION_TYPE_RESOURCE];
1052 assert(coal != COALITION_NULL);
fe8ab488 1053
3e170ce0 1054 ret = hw_atomic_add(&coal->focal_task_count, count);
fe8ab488 1055
3e170ce0
A
1056 /* catch underflow */
1057 assert(ret != UINT32_MAX);
1058 return ret;
1059}
1060
1061uint32_t task_coalition_focal_count(task_t task)
1062{
1063 coalition_t coal;
1064 coal = task->coalition[COALITION_TYPE_RESOURCE];
1065 assert(coal != COALITION_NULL);
1066
1067 return coal->focal_task_count;
1068}
1069
1070uint32_t task_coalition_adjust_nonfocal_count(task_t task, int count)
1071{
1072 coalition_t coal;
1073 uint32_t ret;
1074
1075 /*
1076 * For now: only use the resource coalition. Perhaps in the
1077 * future we may combine all coalition types, or even make
1078 * a special coalition type just for this.
1079 */
1080 coal = task->coalition[COALITION_TYPE_RESOURCE];
1081 assert(coal != COALITION_NULL);
1082
1083 ret = hw_atomic_add(&coal->nonfocal_task_count, count);
1084
1085 /* catch underflow */
1086 assert(ret != UINT32_MAX);
1087 return ret;
1088}
1089
1090uint32_t task_coalition_nonfocal_count(task_t task)
1091{
1092 coalition_t coal;
1093 coal = task->coalition[COALITION_TYPE_RESOURCE];
1094 assert(coal != COALITION_NULL);
1095
1096 return coal->nonfocal_task_count;
1097}
1098
1099void coalition_for_each_task(coalition_t coal, void *ctx,
1100 void (*callback)(coalition_t, void *, task_t))
1101{
1102 assert(coal != COALITION_NULL);
1103
1104 coal_dbg("iterating tasks in coalition %p id:%llu type:%s, active_count:%u",
1105 coal, coal->id, coal_type_str(coal->type), coal->active_count);
1106
1107 coalition_lock(coal);
1108
1109 coal_call(coal, iterate_tasks, ctx, callback);
fe8ab488
A
1110
1111 coalition_unlock(coal);
fe8ab488
A
1112}
1113
3e170ce0 1114
fe8ab488
A
1115void
1116coalition_remove_active(coalition_t coal)
1117{
1118 coalition_lock(coal);
1119
1120 assert(!coal->reaped);
1121 assert(coal->active_count > 0);
1122
1123 coal->active_count--;
1124
1125 boolean_t do_notify = FALSE;
1126 uint64_t notify_id = 0;
1127 uint32_t notify_flags = 0;
1128 if (coal->termrequested && coal->active_count == 0) {
1129 /* We only notify once, when active_count reaches zero.
1130 * We just decremented, so if it reached zero, we mustn't have
1131 * notified already.
1132 */
1133 assert(!coal->terminated);
1134 coal->terminated = TRUE;
1135
1136 assert(!coal->notified);
1137
1138 coal->notified = TRUE;
3e170ce0
A
1139#if defined(DEVELOPMENT) || defined(DEBUG)
1140 do_notify = coal->should_notify;
1141#else
fe8ab488 1142 do_notify = TRUE;
3e170ce0 1143#endif
fe8ab488
A
1144 notify_id = coal->id;
1145 notify_flags = 0;
1146 }
1147
3e170ce0
A
1148#if COALITION_DEBUG
1149 uint64_t cid = coal->id;
1150 uint32_t rc = coal->ref_count;
1151 int ac = coal->active_count;
1152 int ct = coal->type;
1153#endif
fe8ab488
A
1154 coalition_unlock(coal);
1155
3e170ce0
A
1156 coal_dbg("id:%llu type:%s ref_count:%u, active_count:%u,%s",
1157 cid, coal_type_str(ct), rc, ac, do_notify ? " NOTIFY" : " ");
1158
fe8ab488
A
1159 if (do_notify) {
1160 coalition_notify_user(notify_id, notify_flags);
1161 }
1162}
1163
1164/* Used for kernel_task, launchd, launchd's early boot tasks... */
1165kern_return_t
3e170ce0 1166coalitions_adopt_init_task(task_t task)
fe8ab488
A
1167{
1168 kern_return_t kr;
3e170ce0 1169 kr = coalitions_adopt_task(init_coalition, task);
fe8ab488
A
1170 if (kr != KERN_SUCCESS) {
1171 panic("failed to adopt task %p into default coalition: %d", task, kr);
1172 }
1173 return kr;
1174}
1175
1176/*
3e170ce0 1177 * coalition_adopt_task_internal
fe8ab488
A
1178 * Condition: Coalition must be referenced and unlocked. Will fail if coalition
1179 * is already terminated.
1180 */
3e170ce0
A
1181static kern_return_t
1182coalition_adopt_task_internal(coalition_t coal, task_t task)
fe8ab488 1183{
3e170ce0
A
1184 kern_return_t kr;
1185
1186 if (task->coalition[coal->type]) {
fe8ab488
A
1187 return KERN_ALREADY_IN_SET;
1188 }
1189
1190 coalition_lock(coal);
1191
1192 if (coal->reaped || coal->terminated) {
1193 coalition_unlock(coal);
1194 return KERN_TERMINATED;
1195 }
1196
3e170ce0
A
1197 kr = coal_call(coal, adopt_task, task);
1198 if (kr != KERN_SUCCESS)
1199 goto out_unlock;
1200
fe8ab488
A
1201 coal->active_count++;
1202
1203 coal->ref_count++;
fe8ab488 1204
3e170ce0 1205 task->coalition[coal->type] = coal;
fe8ab488 1206
3e170ce0 1207out_unlock:
fe8ab488 1208#if COALITION_DEBUG
3e170ce0
A
1209 (void)coal; /* need expression after label */
1210 uint64_t cid = coal->id;
fe8ab488 1211 uint32_t rc = coal->ref_count;
3e170ce0 1212 uint32_t ct = coal->type;
fe8ab488 1213#endif
fe8ab488
A
1214 coalition_unlock(coal);
1215
3e170ce0
A
1216 coal_dbg("task:%d, id:%llu type:%s ref_count:%u, kr=%d",
1217 task_pid(task), cid, coal_type_str(ct), rc, kr);
1218 return kr;
1219}
1220
1221static kern_return_t
1222coalition_remove_task_internal(task_t task, int type)
1223{
1224 kern_return_t kr;
1225
1226 coalition_t coal = task->coalition[type];
1227
1228 if (!coal)
1229 return KERN_SUCCESS;
1230
1231 assert(coal->type == (uint32_t)type);
1232
1233 coalition_lock(coal);
1234
1235 kr = coal_call(coal, remove_task, task);
1236
fe8ab488 1237#if COALITION_DEBUG
3e170ce0
A
1238 uint64_t cid = coal->id;
1239 uint32_t rc = coal->ref_count;
1240 int ac = coal->active_count;
1241 int ct = coal->type;
fe8ab488 1242#endif
3e170ce0
A
1243 coalition_unlock(coal);
1244
1245 coal_dbg("id:%llu type:%s ref_count:%u, active_count:%u, kr=%d",
1246 cid, coal_type_str(ct), rc, ac, kr);
1247
1248 coalition_remove_active(coal);
1249
1250 return kr;
fe8ab488
A
1251}
1252
1253/*
3e170ce0
A
1254 * coalitions_adopt_task
1255 * Condition: All coalitions must be referenced and unlocked.
1256 * Will fail if any coalition is already terminated.
fe8ab488
A
1257 */
1258kern_return_t
3e170ce0 1259coalitions_adopt_task(coalition_t *coals, task_t task)
fe8ab488 1260{
3e170ce0
A
1261 int i;
1262 kern_return_t kr;
fe8ab488 1263
3e170ce0
A
1264 if (!coals || coals[COALITION_TYPE_RESOURCE] == COALITION_NULL)
1265 return KERN_INVALID_ARGUMENT;
1266
1267 /* verify that the incoming coalitions are what they say they are */
1268 for (i = 0; i < COALITION_NUM_TYPES; i++)
1269 if (coals[i] && coals[i]->type != (uint32_t)i)
1270 return KERN_INVALID_ARGUMENT;
1271
1272 for (i = 0; i < COALITION_NUM_TYPES; i++) {
1273 kr = KERN_SUCCESS;
1274 if (coals[i])
1275 kr = coalition_adopt_task_internal(coals[i], task);
1276 if (kr != KERN_SUCCESS) {
1277 /* dis-associate any coalitions that just adopted this task */
1278 while (--i >= 0) {
1279 if (task->coalition[i])
1280 coalition_remove_task_internal(task, i);
1281 }
1282 break;
1283 }
1284 }
1285 return kr;
1286}
fe8ab488 1287
3e170ce0
A
1288/*
1289 * coalitions_remove_task
1290 * Condition: task must be referenced and UNLOCKED; all task's coalitions must be UNLOCKED
1291 */
1292kern_return_t
1293coalitions_remove_task(task_t task)
1294{
1295 kern_return_t kr;
1296 int i;
fe8ab488 1297
3e170ce0
A
1298 for (i = 0; i < COALITION_NUM_TYPES; i++) {
1299 kr = coalition_remove_task_internal(task, i);
1300 assert(kr == KERN_SUCCESS);
fe8ab488
A
1301 }
1302
3e170ce0
A
1303 return kr;
1304}
1305
1306/*
1307 * task_release_coalitions
1308 * helper function to release references to all coalitions in which
1309 * 'task' is a member.
1310 */
1311void
1312task_release_coalitions(task_t task)
1313{
1314 int i;
1315 for (i = 0; i < COALITION_NUM_TYPES; i++) {
1316 if (task->coalition[i])
1317 coalition_release(task->coalition[i]);
fe8ab488 1318 }
3e170ce0 1319}
fe8ab488 1320
3e170ce0
A
1321/*
1322 * coalitions_set_roles
1323 * for each type of coalition, if the task is a member of a coalition of
1324 * that type (given in the coalitions parameter) then set the role of
1325 * the task within that that coalition.
1326 */
1327kern_return_t coalitions_set_roles(coalition_t coalitions[COALITION_NUM_TYPES],
1328 task_t task, int roles[COALITION_NUM_TYPES])
1329{
1330 kern_return_t kr = KERN_SUCCESS;
1331 int i;
fe8ab488 1332
3e170ce0
A
1333 for (i = 0; i < COALITION_NUM_TYPES; i++) {
1334 if (!coalitions[i])
1335 continue;
1336 coalition_lock(coalitions[i]);
1337 kr = coal_call(coalitions[i], set_taskrole, task, roles[i]);
1338 coalition_unlock(coalitions[i]);
1339 assert(kr == KERN_SUCCESS);
1340 }
fe8ab488 1341
3e170ce0 1342 return kr;
fe8ab488
A
1343}
1344
1345/*
1346 * coalition_terminate_internal
1347 * Condition: Coalition must be referenced and UNLOCKED.
1348 */
1349kern_return_t
1350coalition_request_terminate_internal(coalition_t coal)
1351{
3e170ce0
A
1352 assert(coal->type >= 0 && coal->type <= COALITION_TYPE_MAX);
1353
1354 if (coal == init_coalition[coal->type]) {
fe8ab488
A
1355 return KERN_DEFAULT_SET;
1356 }
1357
1358 coalition_lock(coal);
1359
1360 if (coal->reaped) {
1361 coalition_unlock(coal);
1362 return KERN_INVALID_NAME;
1363 }
1364
1365 if (coal->terminated || coal->termrequested) {
1366 coalition_unlock(coal);
1367 return KERN_TERMINATED;
1368 }
1369
1370 coal->termrequested = TRUE;
1371
1372 boolean_t do_notify = FALSE;
1373 uint64_t note_id = 0;
1374 uint32_t note_flags = 0;
1375
1376 if (coal->active_count == 0) {
1377 /*
1378 * We only notify once, when active_count reaches zero.
3e170ce0
A
1379 * We just set termrequested to zero. If the active count
1380 * was already at zero (tasks died before we could request
1381 * a termination notification), we should notify.
fe8ab488
A
1382 */
1383 assert(!coal->terminated);
1384 coal->terminated = TRUE;
1385
1386 assert(!coal->notified);
1387
1388 coal->notified = TRUE;
3e170ce0
A
1389#if defined(DEVELOPMENT) || defined(DEBUG)
1390 do_notify = coal->should_notify;
1391#else
fe8ab488 1392 do_notify = TRUE;
3e170ce0 1393#endif
fe8ab488
A
1394 note_id = coal->id;
1395 note_flags = 0;
1396 }
1397
1398 coalition_unlock(coal);
1399
1400 if (do_notify) {
1401 coalition_notify_user(note_id, note_flags);
1402 }
1403
1404 return KERN_SUCCESS;
1405}
1406
1407/*
1408 * coalition_reap_internal
1409 * Condition: Coalition must be referenced and UNLOCKED.
1410 */
1411kern_return_t
1412coalition_reap_internal(coalition_t coal)
1413{
3e170ce0
A
1414 assert(coal->type <= COALITION_TYPE_MAX);
1415
1416 if (coal == init_coalition[coal->type]) {
fe8ab488
A
1417 return KERN_DEFAULT_SET;
1418 }
1419
1420 coalition_lock(coal);
1421 if (coal->reaped) {
1422 coalition_unlock(coal);
1423 return KERN_TERMINATED;
1424 }
1425 if (!coal->terminated) {
1426 coalition_unlock(coal);
1427 return KERN_FAILURE;
1428 }
1429 assert(coal->termrequested);
1430 if (coal->active_count > 0) {
1431 coalition_unlock(coal);
1432 return KERN_FAILURE;
1433 }
1434
1435 coal->reaped = TRUE;
1436
1437 /* Caller, launchd, and coalitions list should each have a reference */
1438 assert(coal->ref_count > 2);
1439
1440 coalition_unlock(coal);
1441
1442 lck_mtx_lock(&coalitions_list_lock);
1443 coalition_count--;
3e170ce0 1444 remqueue(&coal->coalitions);
fe8ab488
A
1445 lck_mtx_unlock(&coalitions_list_lock);
1446
1447 /* Release the list's reference and launchd's reference. */
1448 coalition_release(coal);
1449 coalition_release(coal);
1450
1451 return KERN_SUCCESS;
1452}
1453
3e170ce0
A
1454#if defined(DEVELOPMENT) || defined(DEBUG)
1455int coalition_should_notify(coalition_t coal)
1456{
1457 int should;
1458 if (!coal)
1459 return -1;
1460 coalition_lock(coal);
1461 should = coal->should_notify;
1462 coalition_unlock(coal);
1463
1464 return should;
1465}
1466
1467void coalition_set_notify(coalition_t coal, int notify)
1468{
1469 if (!coal)
1470 return;
1471 coalition_lock(coal);
1472 coal->should_notify = !!notify;
1473 coalition_unlock(coal);
1474}
1475#endif
1476
fe8ab488 1477void
3e170ce0 1478coalitions_init(void)
fe8ab488 1479{
3e170ce0
A
1480 kern_return_t kr;
1481 int i;
1482 const struct coalition_type *ctype;
1483
fe8ab488
A
1484 coalition_zone = zinit(
1485 sizeof(struct coalition),
1486 CONFIG_COALITION_MAX * sizeof(struct coalition),
1487 COALITION_CHUNK * sizeof(struct coalition),
1488 "coalitions");
1489 zone_change(coalition_zone, Z_NOENCRYPT, TRUE);
3e170ce0 1490 queue_head_init(coalitions_q);
fe8ab488
A
1491
1492 if (!PE_parse_boot_argn("unrestrict_coalition_syscalls", &unrestrict_coalition_syscalls,
1493 sizeof (unrestrict_coalition_syscalls))) {
1494 unrestrict_coalition_syscalls = 0;
1495 }
1496
1497 lck_grp_attr_setdefault(&coalitions_lck_grp_attr);
1498 lck_grp_init(&coalitions_lck_grp, "coalition", &coalitions_lck_grp_attr);
1499 lck_attr_setdefault(&coalitions_lck_attr);
1500 lck_mtx_init(&coalitions_list_lock, &coalitions_lck_grp, &coalitions_lck_attr);
1501
1502 init_task_ledgers();
1503
3e170ce0
A
1504 for (i = 0, ctype = &s_coalition_types[0]; i < COALITION_NUM_TYPES; ctype++, i++) {
1505 /* verify the entry in the global coalition types array */
1506 if (ctype->type != i ||
1507 !ctype->init ||
1508 !ctype->dealloc ||
1509 !ctype->adopt_task ||
1510 !ctype->remove_task) {
1511 panic("%s: Malformed coalition type %s(%d) in slot for type:%s(%d)",
1512 __func__, coal_type_str(ctype->type), ctype->type, coal_type_str(i), i);
1513 }
1514 if (!ctype->has_default)
1515 continue;
1516 kr = coalition_create_internal(ctype->type, TRUE, &init_coalition[ctype->type]);
1517 if (kr != KERN_SUCCESS)
1518 panic("%s: could not create init %s coalition: kr:%d",
1519 __func__, coal_type_str(i), kr);
fe8ab488 1520 }
3e170ce0 1521
fe8ab488
A
1522 /* "Leak" our reference to the global object */
1523}
1524
3e170ce0
A
1525/*
1526 * BSD Kernel interface functions
1527 *
1528 */
1529static void coalition_fill_procinfo(struct coalition *coal,
1530 struct procinfo_coalinfo *coalinfo)
a1c7dba1 1531{
3e170ce0
A
1532 coalinfo->coalition_id = coal->id;
1533 coalinfo->coalition_type = coal->type;
1534 coalinfo->coalition_tasks = coalition_get_task_count(coal);
a1c7dba1
A
1535}
1536
3e170ce0
A
1537
1538int coalitions_get_list(int type, struct procinfo_coalinfo *coal_list, int list_sz)
a1c7dba1 1539{
3e170ce0
A
1540 int ncoals = 0;
1541 struct coalition *coal;
1542
1543 lck_mtx_lock(&coalitions_list_lock);
1544 qe_foreach_element(coal, &coalitions_q, coalitions) {
1545 if (!coal->reaped && (type < 0 || type == (int)coal->type)) {
1546 if (coal_list && ncoals < list_sz)
1547 coalition_fill_procinfo(coal, &coal_list[ncoals]);
1548 ++ncoals;
1549 }
1550 }
1551 lck_mtx_unlock(&coalitions_list_lock);
1552
1553 return ncoals;
a1c7dba1
A
1554}
1555
3e170ce0
A
1556/*
1557 * Jetsam coalition interface
1558 *
1559 */
1560boolean_t coalition_is_leader(task_t task, int coal_type, coalition_t *coal)
a1c7dba1 1561{
3e170ce0
A
1562 coalition_t c;
1563 boolean_t ret;
1564
1565 if (coal) /* handle the error cases gracefully */
1566 *coal = COALITION_NULL;
1567
1568 if (!task)
1569 return FALSE;
1570
1571 if (coal_type > COALITION_TYPE_MAX)
1572 return FALSE;
1573
1574 c = task->coalition[coal_type];
1575 if (!c)
1576 return FALSE;
1577
1578 assert((int)c->type == coal_type);
1579
1580 coalition_lock(c);
1581
1582 if (coal)
1583 *coal = c;
1584
1585 ret = FALSE;
1586 if (c->type == COALITION_TYPE_JETSAM && c->j.leader == task)
1587 ret = TRUE;
1588
1589 coalition_unlock(c);
1590
1591 return ret;
a1c7dba1
A
1592}
1593
3e170ce0 1594
4bd07ac2
A
1595task_t coalition_get_leader(coalition_t coal)
1596{
1597 task_t leader = TASK_NULL;
1598
1599 if (!coal)
1600 return TASK_NULL;
1601
1602 coalition_lock(coal);
1603 if (coal->type != COALITION_TYPE_JETSAM)
1604 goto out_unlock;
1605
1606 leader = coal->j.leader;
1607 if (leader != TASK_NULL)
1608 task_reference(leader);
1609
1610out_unlock:
1611 coalition_unlock(coal);
1612 return leader;
1613}
1614
1615
3e170ce0 1616int coalition_get_task_count(coalition_t coal)
a1c7dba1 1617{
3e170ce0
A
1618 int ntasks = 0;
1619 struct queue_entry *qe;
1620 if (!coal)
1621 return 0;
1622
1623 coalition_lock(coal);
1624 switch (coal->type) {
1625 case COALITION_TYPE_RESOURCE:
1626 qe_foreach(qe, &coal->r.tasks)
1627 ntasks++;
1628 break;
1629 case COALITION_TYPE_JETSAM:
1630 if (coal->j.leader)
1631 ntasks++;
1632 qe_foreach(qe, &coal->j.other)
1633 ntasks++;
1634 qe_foreach(qe, &coal->j.extensions)
1635 ntasks++;
1636 qe_foreach(qe, &coal->j.services)
1637 ntasks++;
1638 break;
1639 default:
1640 break;
1641 }
1642 coalition_unlock(coal);
1643
1644 return ntasks;
a1c7dba1
A
1645}
1646
3e170ce0
A
1647
1648static uint64_t i_get_list_footprint(queue_t list, int type, int *ntasks)
1649{
a1c7dba1 1650 task_t task;
3e170ce0 1651 uint64_t bytes = 0;
a1c7dba1 1652
3e170ce0
A
1653 qe_foreach_element(task, list, task_coalition[type]) {
1654 bytes += get_task_phys_footprint(task);
1655 coal_dbg(" [%d] task_pid:%d, type:%d, footprint:%lld",
1656 *ntasks, task_pid(task), type, bytes);
1657 *ntasks += 1;
1658 }
a1c7dba1 1659
3e170ce0
A
1660 return bytes;
1661}
a1c7dba1 1662
3e170ce0
A
1663uint64_t coalition_get_page_count(coalition_t coal, int *ntasks)
1664{
1665 uint64_t bytes = 0;
1666 int num_tasks = 0;
1667
1668 if (ntasks)
1669 *ntasks = 0;
1670 if (!coal)
1671 return bytes;
a1c7dba1 1672
3e170ce0 1673 coalition_lock(coal);
a1c7dba1 1674
3e170ce0
A
1675 switch (coal->type) {
1676 case COALITION_TYPE_RESOURCE:
1677 bytes += i_get_list_footprint(&coal->r.tasks, COALITION_TYPE_RESOURCE, &num_tasks);
1678 break;
1679 case COALITION_TYPE_JETSAM:
1680 if (coal->j.leader) {
1681 bytes += get_task_phys_footprint(coal->j.leader);
1682 num_tasks = 1;
a1c7dba1 1683 }
3e170ce0
A
1684 bytes += i_get_list_footprint(&coal->j.extensions, COALITION_TYPE_JETSAM, &num_tasks);
1685 bytes += i_get_list_footprint(&coal->j.services, COALITION_TYPE_JETSAM, &num_tasks);
1686 bytes += i_get_list_footprint(&coal->j.other, COALITION_TYPE_JETSAM, &num_tasks);
1687 break;
1688 default:
1689 break;
a1c7dba1 1690 }
3e170ce0 1691
a1c7dba1 1692 coalition_unlock(coal);
3e170ce0
A
1693
1694 if (ntasks)
1695 *ntasks = num_tasks;
1696
1697 return bytes / PAGE_SIZE_64;
a1c7dba1
A
1698}
1699
3e170ce0
A
1700struct coal_sort_s {
1701 int pid;
1702 int usr_order;
1703 uint64_t bytes;
1704};
1705
1706/*
1707 * return < 0 for a < b
1708 * 0 for a == b
1709 * > 0 for a > b
1710 */
1711typedef int (*cmpfunc_t)(const void *a, const void *b);
1712
1713extern void
1714qsort(void *a, size_t n, size_t es, cmpfunc_t cmp);
1715
1716static int dflt_cmp(const void *a, const void *b)
1717{
1718 const struct coal_sort_s *csA = (const struct coal_sort_s *)a;
1719 const struct coal_sort_s *csB = (const struct coal_sort_s *)b;
1720
1721 /*
1722 * if both A and B are equal, use a memory descending sort
1723 */
1724 if (csA->usr_order == csB->usr_order)
1725 return (int)((int64_t)csB->bytes - (int64_t)csA->bytes);
1726
1727 /* otherwise, return the relationship between user specified orders */
1728 return (csA->usr_order - csB->usr_order);
1729}
1730
1731static int mem_asc_cmp(const void *a, const void *b)
1732{
1733 const struct coal_sort_s *csA = (const struct coal_sort_s *)a;
1734 const struct coal_sort_s *csB = (const struct coal_sort_s *)b;
1735
1736 return (int)((int64_t)csA->bytes - (int64_t)csB->bytes);
1737}
1738
1739static int mem_dec_cmp(const void *a, const void *b)
1740{
1741 const struct coal_sort_s *csA = (const struct coal_sort_s *)a;
1742 const struct coal_sort_s *csB = (const struct coal_sort_s *)b;
1743
1744 return (int)((int64_t)csB->bytes - (int64_t)csA->bytes);
1745}
1746
1747static int usr_asc_cmp(const void *a, const void *b)
1748{
1749 const struct coal_sort_s *csA = (const struct coal_sort_s *)a;
1750 const struct coal_sort_s *csB = (const struct coal_sort_s *)b;
1751
1752 return (csA->usr_order - csB->usr_order);
1753}
1754
1755static int usr_dec_cmp(const void *a, const void *b)
1756{
1757 const struct coal_sort_s *csA = (const struct coal_sort_s *)a;
1758 const struct coal_sort_s *csB = (const struct coal_sort_s *)b;
1759
1760 return (csB->usr_order - csA->usr_order);
1761}
1762
1763/* avoid dynamic allocation in this path */
1764#define MAX_SORTED_PIDS 80
1765
1766static int coalition_get_sort_list(coalition_t coal, int sort_order, queue_t list,
1767 struct coal_sort_s *sort_array, int array_sz)
1768{
1769 int ntasks = 0;
1770 task_t task;
1771
1772 assert(sort_array != NULL);
1773
1774 if (array_sz <= 0)
1775 return 0;
1776
1777 if (!list) {
1778 /*
1779 * this function will only be called with a NULL
1780 * list for JETSAM-type coalitions, and is intended
1781 * to investigate the leader process
1782 */
1783 if (coal->type != COALITION_TYPE_JETSAM ||
1784 coal->j.leader == TASK_NULL)
1785 return 0;
1786 sort_array[0].pid = task_pid(coal->j.leader);
1787 switch (sort_order) {
1788 case COALITION_SORT_DEFAULT:
1789 sort_array[0].usr_order = 0;
1790 /* fall-through */
1791 case COALITION_SORT_MEM_ASC:
1792 case COALITION_SORT_MEM_DEC:
1793 sort_array[0].bytes = get_task_phys_footprint(coal->j.leader);
1794 break;
1795 case COALITION_SORT_USER_ASC:
1796 case COALITION_SORT_USER_DEC:
1797 sort_array[0].usr_order = 0;
1798 break;
1799 default:
1800 break;
1801 }
1802 return 1;
1803 }
1804
1805 qe_foreach_element(task, list, task_coalition[coal->type]) {
1806 if (ntasks >= array_sz) {
1807 printf("WARNING: more than %d pids in coalition %llu\n",
1808 MAX_SORTED_PIDS, coal->id);
1809 break;
1810 }
1811
1812 sort_array[ntasks].pid = task_pid(task);
1813
1814 switch (sort_order) {
1815 case COALITION_SORT_DEFAULT:
1816 sort_array[ntasks].usr_order = 0;
1817 /* fall-through */
1818 case COALITION_SORT_MEM_ASC:
1819 case COALITION_SORT_MEM_DEC:
1820 sort_array[ntasks].bytes = get_task_phys_footprint(task);
1821 break;
1822 case COALITION_SORT_USER_ASC:
1823 case COALITION_SORT_USER_DEC:
1824 sort_array[ntasks].usr_order = 0;
1825 break;
1826 default:
1827 break;
1828 }
1829
1830 ntasks++;
1831 }
1832
1833 return ntasks;
1834}
1835
1836int coalition_get_pid_list(coalition_t coal, uint32_t rolemask, int sort_order,
1837 int *pid_list, int list_sz)
1838{
1839 struct i_jetsam_coalition *cj;
1840 int ntasks = 0;
1841 cmpfunc_t cmp_func = NULL;
1842 struct coal_sort_s sort_array[MAX_SORTED_PIDS] = { {0,0,0} }; /* keep to < 2k */
1843
1844 if (!coal ||
1845 !(rolemask & COALITION_ROLEMASK_ALLROLES) ||
1846 !pid_list || list_sz < 1) {
1847 coal_dbg("Invalid parameters: coal:%p, type:%d, rolemask:0x%x, "
1848 "pid_list:%p, list_sz:%d", coal, coal ? coal->type : -1,
1849 rolemask, pid_list, list_sz);
1850 return -EINVAL;
1851 }
1852
1853 switch (sort_order) {
1854 case COALITION_SORT_NOSORT:
1855 cmp_func = NULL;
1856 break;
1857 case COALITION_SORT_DEFAULT:
1858 cmp_func = dflt_cmp;
1859 break;
1860 case COALITION_SORT_MEM_ASC:
1861 cmp_func = mem_asc_cmp;
1862 break;
1863 case COALITION_SORT_MEM_DEC:
1864 cmp_func = mem_dec_cmp;
1865 break;
1866 case COALITION_SORT_USER_ASC:
1867 cmp_func = usr_asc_cmp;
1868 break;
1869 case COALITION_SORT_USER_DEC:
1870 cmp_func = usr_dec_cmp;
1871 break;
1872 default:
1873 return -ENOTSUP;
1874 }
1875
1876 coalition_lock(coal);
1877
1878 if (coal->type == COALITION_TYPE_RESOURCE) {
1879 ntasks += coalition_get_sort_list(coal, sort_order, &coal->r.tasks,
1880 sort_array, MAX_SORTED_PIDS);
1881 goto unlock_coal;
1882 }
1883
1884 cj = &coal->j;
1885
1886 if (rolemask & COALITION_ROLEMASK_UNDEF)
1887 ntasks += coalition_get_sort_list(coal, sort_order, &cj->other,
1888 sort_array + ntasks,
1889 MAX_SORTED_PIDS - ntasks);
1890
1891 if (rolemask & COALITION_ROLEMASK_XPC)
1892 ntasks += coalition_get_sort_list(coal, sort_order, &cj->services,
1893 sort_array + ntasks,
1894 MAX_SORTED_PIDS - ntasks);
1895
1896 if (rolemask & COALITION_ROLEMASK_EXT)
1897 ntasks += coalition_get_sort_list(coal, sort_order, &cj->extensions,
1898 sort_array + ntasks,
1899 MAX_SORTED_PIDS - ntasks);
1900
1901 if (rolemask & COALITION_ROLEMASK_LEADER)
1902 ntasks += coalition_get_sort_list(coal, sort_order, NULL,
1903 sort_array + ntasks,
1904 MAX_SORTED_PIDS - ntasks);
1905
1906unlock_coal:
1907 coalition_unlock(coal);
1908
1909 /* sort based on the chosen criterion (no sense sorting 1 item) */
1910 if (cmp_func && ntasks > 1)
1911 qsort(sort_array, ntasks, sizeof(struct coal_sort_s), cmp_func);
1912
1913 for (int i = 0; i < ntasks; i++) {
1914 if (i >= list_sz)
1915 break;
1916 coal_dbg(" [%d] PID:%d, footprint:%lld, usr_order:%d",
1917 i, sort_array[i].pid, sort_array[i].bytes,
1918 sort_array[i].usr_order);
1919 pid_list[i] = sort_array[i].pid;
1920 }
1921
1922 return ntasks;
1923}