]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/bsd_kern.c
xnu-3789.70.16.tar.gz
[apple/xnu.git] / osfmk / kern / bsd_kern.c
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28#include <mach/mach_types.h>
b0d623f7 29#include <mach/machine/vm_param.h>
39236c6e 30#include <mach/task.h>
91447636
A
31
32#include <kern/kern_types.h>
39236c6e 33#include <kern/ledger.h>
91447636 34#include <kern/processor.h>
1c79356b 35#include <kern/thread.h>
1c79356b
A
36#include <kern/task.h>
37#include <kern/spl.h>
91447636 38#include <kern/ast.h>
1c79356b
A
39#include <ipc/ipc_port.h>
40#include <ipc/ipc_object.h>
91447636 41#include <vm/vm_map.h>
0c530ab8 42#include <vm/vm_kern.h>
91447636
A
43#include <vm/pmap.h>
44#include <vm/vm_protos.h> /* last */
39236c6e 45#include <sys/resource.h>
3e170ce0 46#include <sys/signal.h>
1c79356b
A
47
48#undef thread_should_halt
1c79356b 49
1c79356b
A
50/* BSD KERN COMPONENT INTERFACE */
51
55e303ae 52extern unsigned int not_in_kdp; /* Skip acquiring locks if we're in kdp */
1c79356b 53
91447636 54thread_t get_firstthread(task_t);
1c79356b 55int get_task_userstop(task_t);
91447636 56int get_thread_userstop(thread_t);
1c79356b 57boolean_t current_thread_aborted(void);
91447636 58void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *);
91447636 59kern_return_t get_signalact(task_t , thread_t *, int);
fe8ab488
A
60int fill_task_rusage(task_t task, rusage_info_current *ri);
61int fill_task_io_rusage(task_t task, rusage_info_current *ri);
62int fill_task_qos_rusage(task_t task, rusage_info_current *ri);
63void fill_task_billed_usage(task_t task, rusage_info_current *ri);
3e170ce0
A
64void task_bsdtask_kill(task_t);
65
39037602
A
66extern uint64_t get_dispatchqueue_serialno_offset_from_proc(void *p);
67extern uint64_t proc_uniqueid(void *p);
68
3e170ce0
A
69#if MACH_BSD
70extern void psignal(void *, int);
71#endif
1c79356b 72
1c79356b
A
73/*
74 *
75 */
76void *get_bsdtask_info(task_t t)
77{
78 return(t->bsd_info);
79}
80
3e170ce0
A
81void task_bsdtask_kill(task_t t)
82{
83 void * bsd_info = get_bsdtask_info(t);
84 if (bsd_info != NULL) {
85 psignal(bsd_info, SIGKILL);
86 }
87}
2d21ac55
A
88/*
89 *
90 */
91void *get_bsdthreadtask_info(thread_t th)
92{
93 return(th->task != TASK_NULL ? th->task->bsd_info : NULL);
94}
95
1c79356b
A
96/*
97 *
98 */
99void set_bsdtask_info(task_t t,void * v)
100{
101 t->bsd_info=v;
102}
103
104/*
105 *
106 */
91447636 107void *get_bsdthread_info(thread_t th)
1c79356b
A
108{
109 return(th->uthread);
110}
111
6d2010ae
A
112/*
113 * XXX
114 */
115int get_thread_lock_count(thread_t th); /* forced forward */
116int get_thread_lock_count(thread_t th)
117{
118 return(th->mutex_count);
119}
120
1c79356b
A
121/*
122 * XXX: wait for BSD to fix signal code
123 * Until then, we cannot block here. We know the task
124 * can't go away, so we make sure it is still active after
125 * retrieving the first thread for extra safety.
126 */
91447636 127thread_t get_firstthread(task_t task)
1c79356b 128{
39236c6e 129 thread_t thread = (thread_t)(void *)queue_first(&task->threads);
91447636
A
130
131 if (queue_end(&task->threads, (queue_entry_t)thread))
132 thread = THREAD_NULL;
1c79356b 133
1c79356b 134 if (!task->active)
91447636
A
135 return (THREAD_NULL);
136
137 return (thread);
1c79356b
A
138}
139
91447636
A
140kern_return_t
141get_signalact(
142 task_t task,
143 thread_t *result_out,
144 int setast)
1c79356b 145{
91447636
A
146 kern_return_t result = KERN_SUCCESS;
147 thread_t inc, thread = THREAD_NULL;
1c79356b
A
148
149 task_lock(task);
91447636 150
1c79356b
A
151 if (!task->active) {
152 task_unlock(task);
91447636
A
153
154 return (KERN_FAILURE);
1c79356b
A
155 }
156
39236c6e 157 for (inc = (thread_t)(void *)queue_first(&task->threads);
91447636 158 !queue_end(&task->threads, (queue_entry_t)inc); ) {
2d21ac55
A
159 thread_mtx_lock(inc);
160 if (inc->active &&
6d2010ae 161 (inc->sched_flags & TH_SFLAG_ABORTED_MASK) != TH_SFLAG_ABORT) {
2d21ac55
A
162 thread = inc;
163 break;
164 }
165 thread_mtx_unlock(inc);
166
39236c6e 167 inc = (thread_t)(void *)queue_next(&inc->task_threads);
91447636
A
168 }
169
170 if (result_out)
171 *result_out = thread;
172
173 if (thread) {
174 if (setast)
175 act_set_astbsd(thread);
176
177 thread_mtx_unlock(thread);
178 }
179 else
180 result = KERN_FAILURE;
181
1c79356b
A
182 task_unlock(task);
183
91447636 184 return (result);
1c79356b
A
185}
186
0b4e3aa0 187
91447636
A
188kern_return_t
189check_actforsig(
190 task_t task,
191 thread_t thread,
192 int setast)
0b4e3aa0 193{
91447636
A
194 kern_return_t result = KERN_FAILURE;
195 thread_t inc;
0b4e3aa0
A
196
197 task_lock(task);
91447636 198
0b4e3aa0
A
199 if (!task->active) {
200 task_unlock(task);
91447636
A
201
202 return (KERN_FAILURE);
0b4e3aa0
A
203 }
204
39236c6e 205 for (inc = (thread_t)(void *)queue_first(&task->threads);
91447636
A
206 !queue_end(&task->threads, (queue_entry_t)inc); ) {
207 if (inc == thread) {
208 thread_mtx_lock(inc);
209
210 if (inc->active &&
6d2010ae 211 (inc->sched_flags & TH_SFLAG_ABORTED_MASK) != TH_SFLAG_ABORT) {
91447636 212 result = KERN_SUCCESS;
0b4e3aa0 213 break;
91447636 214 }
0b4e3aa0 215
91447636
A
216 thread_mtx_unlock(inc);
217 break;
218 }
219
39236c6e 220 inc = (thread_t)(void *)queue_next(&inc->task_threads);
91447636
A
221 }
222
223 if (result == KERN_SUCCESS) {
224 if (setast)
225 act_set_astbsd(thread);
226
227 thread_mtx_unlock(thread);
228 }
229
230 task_unlock(task);
231
232 return (result);
0b4e3aa0
A
233}
234
316670eb
A
235ledger_t get_task_ledger(task_t t)
236{
237 return(t->ledger);
238}
239
1c79356b 240/*
91447636 241 * This is only safe to call from a thread executing in
3e170ce0 242 * in the task's context or if the task is locked. Otherwise,
91447636 243 * the map could be switched for the task (and freed) before
3e170ce0 244 * we go to return it here.
1c79356b
A
245 */
246vm_map_t get_task_map(task_t t)
247{
248 return(t->map);
249}
250
91447636
A
251vm_map_t get_task_map_reference(task_t t)
252{
253 vm_map_t m;
254
255 if (t == NULL)
256 return VM_MAP_NULL;
257
258 task_lock(t);
259 if (!t->active) {
260 task_unlock(t);
261 return VM_MAP_NULL;
262 }
263 m = t->map;
264 vm_map_reference_swap(m);
265 task_unlock(t);
266 return m;
267}
268
1c79356b
A
269/*
270 *
271 */
272ipc_space_t get_task_ipcspace(task_t t)
273{
274 return(t->itk_space);
275}
276
593a1d5f
A
277int get_task_numactivethreads(task_t task)
278{
279 thread_t inc;
280 int num_active_thr=0;
281 task_lock(task);
282
39236c6e
A
283 for (inc = (thread_t)(void *)queue_first(&task->threads);
284 !queue_end(&task->threads, (queue_entry_t)inc); inc = (thread_t)(void *)queue_next(&inc->task_threads))
593a1d5f
A
285 {
286 if(inc->active)
287 num_active_thr++;
288 }
289 task_unlock(task);
290 return num_active_thr;
291}
292
1c79356b
A
293int get_task_numacts(task_t t)
294{
55e303ae
A
295 return(t->thread_count);
296}
297
298/* does this machine need 64bit register set for signal handler */
299int is_64signalregset(void)
300{
39236c6e 301 if (task_has_64BitData(current_task())) {
55e303ae 302 return(1);
39236c6e
A
303 }
304
305 return(0);
1c79356b
A
306}
307
308/*
b0d623f7 309 * Swap in a new map for the task/thread pair; the old map reference is
d190cdc3 310 * returned. Also does a pmap switch if thread provided is current thread.
1c79356b
A
311 */
312vm_map_t
d190cdc3 313swap_task_map(task_t task, thread_t thread, vm_map_t map)
1c79356b
A
314{
315 vm_map_t old_map;
d190cdc3 316 boolean_t doswitch = (thread == current_thread()) ? TRUE : FALSE;
1c79356b 317
91447636 318 if (task != thread->task)
55e303ae
A
319 panic("swap_task_map");
320
1c79356b 321 task_lock(task);
6d2010ae 322 mp_disable_preemption();
39037602 323
1c79356b 324 old_map = task->map;
91447636 325 thread->map = task->map = map;
39037602
A
326 vm_commit_pagezero_status(map);
327
316670eb 328 if (doswitch) {
6d2010ae 329 pmap_switch(map->pmap);
316670eb 330 }
6d2010ae 331 mp_enable_preemption();
1c79356b 332 task_unlock(task);
0c530ab8 333
b0d623f7 334#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
0c530ab8 335 inval_copy_windows(thread);
b0d623f7 336#endif
0c530ab8 337
1c79356b
A
338 return old_map;
339}
340
1c79356b
A
341/*
342 *
3e170ce0
A
343 * This is only safe to call from a thread executing in
344 * in the task's context or if the task is locked. Otherwise,
345 * the map could be switched for the task (and freed) before
346 * we go to return it here.
1c79356b
A
347 */
348pmap_t get_task_pmap(task_t t)
349{
350 return(t->map->pmap);
351}
352
b0d623f7
A
353/*
354 *
355 */
356uint64_t get_task_resident_size(task_t task)
357{
358 vm_map_t map;
359
360 map = (task == kernel_task) ? kernel_map: task->map;
361 return((uint64_t)pmap_resident_count(map->pmap) * PAGE_SIZE_64);
362}
363
fe8ab488
A
364uint64_t get_task_compressed(task_t task)
365{
366 vm_map_t map;
367
368 map = (task == kernel_task) ? kernel_map: task->map;
369 return((uint64_t)pmap_compressed(map->pmap) * PAGE_SIZE_64);
370}
371
372uint64_t get_task_resident_max(task_t task)
373{
374 vm_map_t map;
375
376 map = (task == kernel_task) ? kernel_map: task->map;
377 return((uint64_t)pmap_resident_max(map->pmap) * PAGE_SIZE_64);
378}
379
380uint64_t get_task_purgeable_size(task_t task)
381{
3e170ce0
A
382 kern_return_t ret;
383 ledger_amount_t credit, debit;
384 uint64_t volatile_size = 0;
385
386 ret = ledger_get_entries(task->ledger, task_ledgers.purgeable_volatile, &credit, &debit);
387 if (ret != KERN_SUCCESS) {
388 return 0;
389 }
390
391 volatile_size += (credit - debit);
392
393 ret = ledger_get_entries(task->ledger, task_ledgers.purgeable_volatile_compressed, &credit, &debit);
394 if (ret != KERN_SUCCESS) {
395 return 0;
396 }
fe8ab488 397
3e170ce0
A
398 volatile_size += (credit - debit);
399
400 return volatile_size;
fe8ab488 401}
3e170ce0 402
39236c6e
A
403/*
404 *
405 */
406uint64_t get_task_phys_footprint(task_t task)
407{
408 kern_return_t ret;
409 ledger_amount_t credit, debit;
410
411 ret = ledger_get_entries(task->ledger, task_ledgers.phys_footprint, &credit, &debit);
412 if (KERN_SUCCESS == ret) {
413 return (credit - debit);
414 }
415
416 return 0;
417}
418
419/*
420 *
421 */
422uint64_t get_task_phys_footprint_max(task_t task)
423{
424 kern_return_t ret;
425 ledger_amount_t max;
426
427 ret = ledger_get_maximum(task->ledger, task_ledgers.phys_footprint, &max);
428 if (KERN_SUCCESS == ret) {
429 return max;
430 }
431
432 return 0;
433}
434
39037602
A
435/*
436 *
437 */
438uint64_t get_task_phys_footprint_limit(task_t task)
439{
440 kern_return_t ret;
441 ledger_amount_t max;
442
443 ret = ledger_get_limit(task->ledger, task_ledgers.phys_footprint, &max);
444 if (KERN_SUCCESS == ret) {
445 return max;
446 }
447
448 return 0;
449}
450
451uint64_t get_task_internal(task_t task)
452{
453 kern_return_t ret;
454 ledger_amount_t credit, debit;
455
456 ret = ledger_get_entries(task->ledger, task_ledgers.internal, &credit, &debit);
457 if (KERN_SUCCESS == ret) {
458 return (credit - debit);
459 }
460
461 return 0;
462}
463
464uint64_t get_task_internal_compressed(task_t task)
465{
466 kern_return_t ret;
467 ledger_amount_t credit, debit;
468
469 ret = ledger_get_entries(task->ledger, task_ledgers.internal_compressed, &credit, &debit);
470 if (KERN_SUCCESS == ret) {
471 return (credit - debit);
472 }
473
474 return 0;
475}
476
477uint64_t get_task_purgeable_nonvolatile(task_t task)
478{
479 kern_return_t ret;
480 ledger_amount_t credit, debit;
481
482 ret = ledger_get_entries(task->ledger, task_ledgers.purgeable_nonvolatile, &credit, &debit);
483 if (KERN_SUCCESS == ret) {
484 return (credit - debit);
485 }
486
487 return 0;
488}
489
490uint64_t get_task_purgeable_nonvolatile_compressed(task_t task)
491{
492 kern_return_t ret;
493 ledger_amount_t credit, debit;
494
495 ret = ledger_get_entries(task->ledger, task_ledgers.purgeable_nonvolatile_compressed, &credit, &debit);
496 if (KERN_SUCCESS == ret) {
497 return (credit - debit);
498 }
499
500 return 0;
501}
502
503uint64_t get_task_alternate_accounting(task_t task)
504{
505 kern_return_t ret;
506 ledger_amount_t credit, debit;
507
508 ret = ledger_get_entries(task->ledger, task_ledgers.alternate_accounting, &credit, &debit);
509 if (KERN_SUCCESS == ret) {
510 return (credit - debit);
511 }
512
513 return 0;
514}
515
516uint64_t get_task_alternate_accounting_compressed(task_t task)
517{
518 kern_return_t ret;
519 ledger_amount_t credit, debit;
520
521 ret = ledger_get_entries(task->ledger, task_ledgers.alternate_accounting_compressed, &credit, &debit);
522 if (KERN_SUCCESS == ret) {
523 return (credit - debit);
524 }
525
526 return 0;
527}
528
529uint64_t get_task_page_table(task_t task)
530{
531 kern_return_t ret;
532 ledger_amount_t credit, debit;
533
534 ret = ledger_get_entries(task->ledger, task_ledgers.page_table, &credit, &debit);
535 if (KERN_SUCCESS == ret) {
536 return (credit - debit);
537 }
538
539 return 0;
540}
541
542uint64_t get_task_iokit_mapped(task_t task)
543{
544 kern_return_t ret;
545 ledger_amount_t credit, debit;
546
547 ret = ledger_get_entries(task->ledger, task_ledgers.iokit_mapped, &credit, &debit);
548 if (KERN_SUCCESS == ret) {
549 return (credit - debit);
550 }
551
552 return 0;
553}
554
fe8ab488
A
555uint64_t get_task_cpu_time(task_t task)
556{
557 kern_return_t ret;
558 ledger_amount_t credit, debit;
559
560 ret = ledger_get_entries(task->ledger, task_ledgers.cpu_time, &credit, &debit);
561 if (KERN_SUCCESS == ret) {
562 return (credit - debit);
563 }
564
565 return 0;
566}
567
1c79356b
A
568/*
569 *
570 */
91447636 571task_t get_threadtask(thread_t th)
1c79356b
A
572{
573 return(th->task);
574}
575
1c79356b
A
576/*
577 *
578 */
91447636 579vm_map_offset_t
1c79356b
A
580get_map_min(
581 vm_map_t map)
582{
583 return(vm_map_min(map));
584}
585
586/*
587 *
588 */
91447636 589vm_map_offset_t
1c79356b
A
590get_map_max(
591 vm_map_t map)
592{
593 return(vm_map_max(map));
594}
91447636 595vm_map_size_t
1c79356b
A
596get_vmmap_size(
597 vm_map_t map)
598{
599 return(map->size);
600}
601
39037602
A
602#if CONFIG_COREDUMP
603
604static int
1c79356b
A
605get_vmsubmap_entries(
606 vm_map_t map,
607 vm_object_offset_t start,
608 vm_object_offset_t end)
609{
610 int total_entries = 0;
611 vm_map_entry_t entry;
612
55e303ae
A
613 if (not_in_kdp)
614 vm_map_lock(map);
1c79356b
A
615 entry = vm_map_first_entry(map);
616 while((entry != vm_map_to_entry(map)) && (entry->vme_start < start)) {
617 entry = entry->vme_next;
618 }
619
620 while((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
621 if(entry->is_sub_map) {
622 total_entries +=
3e170ce0
A
623 get_vmsubmap_entries(VME_SUBMAP(entry),
624 VME_OFFSET(entry),
625 (VME_OFFSET(entry) +
626 entry->vme_end -
627 entry->vme_start));
1c79356b
A
628 } else {
629 total_entries += 1;
630 }
631 entry = entry->vme_next;
632 }
55e303ae
A
633 if (not_in_kdp)
634 vm_map_unlock(map);
1c79356b
A
635 return(total_entries);
636}
637
638int
639get_vmmap_entries(
640 vm_map_t map)
641{
642 int total_entries = 0;
643 vm_map_entry_t entry;
644
55e303ae
A
645 if (not_in_kdp)
646 vm_map_lock(map);
1c79356b
A
647 entry = vm_map_first_entry(map);
648
649 while(entry != vm_map_to_entry(map)) {
650 if(entry->is_sub_map) {
651 total_entries +=
3e170ce0
A
652 get_vmsubmap_entries(VME_SUBMAP(entry),
653 VME_OFFSET(entry),
654 (VME_OFFSET(entry) +
655 entry->vme_end -
656 entry->vme_start));
1c79356b
A
657 } else {
658 total_entries += 1;
659 }
660 entry = entry->vme_next;
661 }
55e303ae
A
662 if (not_in_kdp)
663 vm_map_unlock(map);
1c79356b
A
664 return(total_entries);
665}
39037602 666#endif /* CONFIG_COREDUMP */
1c79356b
A
667
668/*
669 *
670 */
671/*
672 *
673 */
674int
675get_task_userstop(
676 task_t task)
677{
678 return(task->user_stop_count);
679}
680
681/*
682 *
683 */
684int
685get_thread_userstop(
91447636 686 thread_t th)
1c79356b
A
687{
688 return(th->user_stop_count);
689}
690
316670eb
A
691/*
692 *
693 */
694boolean_t
695get_task_pidsuspended(
696 task_t task)
697{
698 return (task->pidsuspended);
699}
700
701/*
702 *
703 */
704boolean_t
705get_task_frozen(
706 task_t task)
707{
708 return (task->frozen);
709}
710
1c79356b
A
711/*
712 *
713 */
714boolean_t
715thread_should_abort(
55e303ae 716 thread_t th)
1c79356b 717{
6d2010ae 718 return ((th->sched_flags & TH_SFLAG_ABORTED_MASK) == TH_SFLAG_ABORT);
1c79356b
A
719}
720
721/*
9bccf70c
A
722 * This routine is like thread_should_abort() above. It checks to
723 * see if the current thread is aborted. But unlike above, it also
724 * checks to see if thread is safely aborted. If so, it returns
725 * that fact, and clears the condition (safe aborts only should
726 * have a single effect, and a poll of the abort status
727 * qualifies.
1c79356b
A
728 */
729boolean_t
730current_thread_aborted (
731 void)
732{
733 thread_t th = current_thread();
9bccf70c
A
734 spl_t s;
735
6d2010ae 736 if ((th->sched_flags & TH_SFLAG_ABORTED_MASK) == TH_SFLAG_ABORT &&
91447636 737 (th->options & TH_OPT_INTMASK) != THREAD_UNINT)
9bccf70c 738 return (TRUE);
6d2010ae 739 if (th->sched_flags & TH_SFLAG_ABORTSAFELY) {
9bccf70c
A
740 s = splsched();
741 thread_lock(th);
6d2010ae
A
742 if (th->sched_flags & TH_SFLAG_ABORTSAFELY)
743 th->sched_flags &= ~TH_SFLAG_ABORTED_MASK;
9bccf70c
A
744 thread_unlock(th);
745 splx(s);
746 }
747 return FALSE;
1c79356b
A
748}
749
750/*
751 *
752 */
753void
754task_act_iterate_wth_args(
91447636
A
755 task_t task,
756 void (*func_callback)(thread_t, void *),
757 void *func_arg)
1c79356b 758{
91447636 759 thread_t inc;
1c79356b
A
760
761 task_lock(task);
91447636 762
39236c6e 763 for (inc = (thread_t)(void *)queue_first(&task->threads);
91447636
A
764 !queue_end(&task->threads, (queue_entry_t)inc); ) {
765 (void) (*func_callback)(inc, func_arg);
39236c6e 766 inc = (thread_t)(void *)queue_next(&inc->task_threads);
91447636
A
767 }
768
1c79356b
A
769 task_unlock(task);
770}
771
1c79356b 772
0c530ab8
A
773#include <sys/bsdtask_info.h>
774
775void
776fill_taskprocinfo(task_t task, struct proc_taskinfo_internal * ptinfo)
777{
778 vm_map_t map;
779 task_absolutetime_info_data_t tinfo;
780 thread_t thread;
6d2010ae
A
781 uint32_t cswitch = 0, numrunning = 0;
782 uint32_t syscalls_unix = 0;
783 uint32_t syscalls_mach = 0;
39236c6e 784
3e170ce0
A
785 task_lock(task);
786
0c530ab8
A
787 map = (task == kernel_task)? kernel_map: task->map;
788
789 ptinfo->pti_virtual_size = map->size;
2d21ac55
A
790 ptinfo->pti_resident_size =
791 (mach_vm_size_t)(pmap_resident_count(map->pmap))
792 * PAGE_SIZE_64;
0c530ab8 793
0c530ab8
A
794 ptinfo->pti_policy = ((task != kernel_task)?
795 POLICY_TIMESHARE: POLICY_RR);
796
797 tinfo.threads_user = tinfo.threads_system = 0;
798 tinfo.total_user = task->total_user_time;
799 tinfo.total_system = task->total_system_time;
800
801 queue_iterate(&task->threads, thread, thread_t, task_threads) {
802 uint64_t tval;
316670eb
A
803 spl_t x;
804
39236c6e
A
805 if (thread->options & TH_OPT_IDLE_THREAD)
806 continue;
807
316670eb
A
808 x = splsched();
809 thread_lock(thread);
0c530ab8
A
810
811 if ((thread->state & TH_RUN) == TH_RUN)
812 numrunning++;
2d21ac55 813 cswitch += thread->c_switch;
0c530ab8
A
814 tval = timer_grab(&thread->user_timer);
815 tinfo.threads_user += tval;
816 tinfo.total_user += tval;
817
818 tval = timer_grab(&thread->system_timer);
316670eb
A
819
820 if (thread->precise_user_kernel_time) {
821 tinfo.threads_system += tval;
822 tinfo.total_system += tval;
823 } else {
824 /* system_timer may represent either sys or user */
825 tinfo.threads_user += tval;
826 tinfo.total_user += tval;
827 }
6d2010ae
A
828
829 syscalls_unix += thread->syscalls_unix;
830 syscalls_mach += thread->syscalls_mach;
316670eb
A
831
832 thread_unlock(thread);
833 splx(x);
0c530ab8
A
834 }
835
836 ptinfo->pti_total_system = tinfo.total_system;
837 ptinfo->pti_total_user = tinfo.total_user;
838 ptinfo->pti_threads_system = tinfo.threads_system;
839 ptinfo->pti_threads_user = tinfo.threads_user;
840
841 ptinfo->pti_faults = task->faults;
842 ptinfo->pti_pageins = task->pageins;
843 ptinfo->pti_cow_faults = task->cow_faults;
844 ptinfo->pti_messages_sent = task->messages_sent;
845 ptinfo->pti_messages_received = task->messages_received;
6d2010ae
A
846 ptinfo->pti_syscalls_mach = task->syscalls_mach + syscalls_mach;
847 ptinfo->pti_syscalls_unix = task->syscalls_unix + syscalls_unix;
2d21ac55 848 ptinfo->pti_csw = task->c_switch + cswitch;
0c530ab8
A
849 ptinfo->pti_threadnum = task->thread_count;
850 ptinfo->pti_numrunning = numrunning;
851 ptinfo->pti_priority = task->priority;
852
853 task_unlock(task);
854}
855
856int
316670eb 857fill_taskthreadinfo(task_t task, uint64_t thaddr, int thuniqueid, struct proc_threadinfo_internal * ptinfo, void * vpp, int *vidp)
0c530ab8
A
858{
859 thread_t thact;
2d21ac55
A
860 int err=0;
861 mach_msg_type_number_t count;
0c530ab8
A
862 thread_basic_info_data_t basic_info;
863 kern_return_t kret;
316670eb 864 uint64_t addr = 0;
0c530ab8
A
865
866 task_lock(task);
867
39236c6e 868 for (thact = (thread_t)(void *)queue_first(&task->threads);
0c530ab8 869 !queue_end(&task->threads, (queue_entry_t)thact); ) {
316670eb
A
870 addr = (thuniqueid==0)?thact->machine.cthread_self: thact->thread_id;
871 if (addr == thaddr)
0c530ab8
A
872 {
873
874 count = THREAD_BASIC_INFO_COUNT;
2d21ac55 875 if ((kret = thread_info_internal(thact, THREAD_BASIC_INFO, (thread_info_t)&basic_info, &count)) != KERN_SUCCESS) {
0c530ab8
A
876 err = 1;
877 goto out;
878 }
39236c6e
A
879 ptinfo->pth_user_time = ((basic_info.user_time.seconds * (integer_t)NSEC_PER_SEC) + (basic_info.user_time.microseconds * (integer_t)NSEC_PER_USEC));
880 ptinfo->pth_system_time = ((basic_info.system_time.seconds * (integer_t)NSEC_PER_SEC) + (basic_info.system_time.microseconds * (integer_t)NSEC_PER_USEC));
0c530ab8 881
0c530ab8
A
882 ptinfo->pth_cpu_usage = basic_info.cpu_usage;
883 ptinfo->pth_policy = basic_info.policy;
884 ptinfo->pth_run_state = basic_info.run_state;
885 ptinfo->pth_flags = basic_info.flags;
886 ptinfo->pth_sleep_time = basic_info.sleep_time;
887 ptinfo->pth_curpri = thact->sched_pri;
3e170ce0 888 ptinfo->pth_priority = thact->base_pri;
0c530ab8
A
889 ptinfo->pth_maxpriority = thact->max_priority;
890
2d21ac55
A
891 if ((vpp != NULL) && (thact->uthread != NULL))
892 bsd_threadcdir(thact->uthread, vpp, vidp);
b0d623f7 893 bsd_getthreadname(thact->uthread,ptinfo->pth_name);
0c530ab8
A
894 err = 0;
895 goto out;
896 }
39236c6e 897 thact = (thread_t)(void *)queue_next(&thact->task_threads);
0c530ab8
A
898 }
899 err = 1;
900
901out:
902 task_unlock(task);
903 return(err);
904}
905
906int
907fill_taskthreadlist(task_t task, void * buffer, int thcount)
908{
909 int numthr=0;
910 thread_t thact;
911 uint64_t * uptr;
912 uint64_t thaddr;
913
914 uptr = (uint64_t *)buffer;
915
916 task_lock(task);
917
39236c6e 918 for (thact = (thread_t)(void *)queue_first(&task->threads);
0c530ab8 919 !queue_end(&task->threads, (queue_entry_t)thact); ) {
0c530ab8 920 thaddr = thact->machine.cthread_self;
0c530ab8
A
921 *uptr++ = thaddr;
922 numthr++;
923 if (numthr >= thcount)
924 goto out;
39236c6e 925 thact = (thread_t)(void *)queue_next(&thact->task_threads);
0c530ab8
A
926 }
927
928out:
929 task_unlock(task);
b0d623f7 930 return (int)(numthr * sizeof(uint64_t));
0c530ab8
A
931
932}
933
934int
935get_numthreads(task_t task)
936{
937 return(task->thread_count);
938}
939
39236c6e
A
940/*
941 * Gather the various pieces of info about the designated task,
942 * and collect it all into a single rusage_info.
943 */
944int
fe8ab488 945fill_task_rusage(task_t task, rusage_info_current *ri)
39236c6e
A
946{
947 struct task_power_info powerinfo;
948
949 assert(task != TASK_NULL);
950 task_lock(task);
951
39037602 952 task_power_info_locked(task, &powerinfo, NULL, NULL);
39236c6e
A
953 ri->ri_pkg_idle_wkups = powerinfo.task_platform_idle_wakeups;
954 ri->ri_interrupt_wkups = powerinfo.task_interrupt_wakeups;
955 ri->ri_user_time = powerinfo.total_user;
956 ri->ri_system_time = powerinfo.total_system;
957
958 ledger_get_balance(task->ledger, task_ledgers.phys_footprint,
959 (ledger_amount_t *)&ri->ri_phys_footprint);
960 ledger_get_balance(task->ledger, task_ledgers.phys_mem,
961 (ledger_amount_t *)&ri->ri_resident_size);
962 ledger_get_balance(task->ledger, task_ledgers.wired_mem,
963 (ledger_amount_t *)&ri->ri_wired_size);
964
965 ri->ri_pageins = task->pageins;
966
967 task_unlock(task);
968 return (0);
969}
fe8ab488
A
970
971void
972fill_task_billed_usage(task_t task __unused, rusage_info_current *ri)
973{
974#if CONFIG_BANK
39037602
A
975 ri->ri_billed_system_time = bank_billed_time_safe(task);
976 ri->ri_serviced_system_time = bank_serviced_time_safe(task);
fe8ab488
A
977#else
978 ri->ri_billed_system_time = 0;
979 ri->ri_serviced_system_time = 0;
980#endif
981}
982
983int
984fill_task_io_rusage(task_t task, rusage_info_current *ri)
985{
986 assert(task != TASK_NULL);
987 task_lock(task);
988
989 if (task->task_io_stats) {
990 ri->ri_diskio_bytesread = task->task_io_stats->disk_reads.size;
991 ri->ri_diskio_byteswritten = (task->task_io_stats->total_io.size - task->task_io_stats->disk_reads.size);
992 } else {
993 /* I/O Stats unavailable */
994 ri->ri_diskio_bytesread = 0;
995 ri->ri_diskio_byteswritten = 0;
996 }
997 task_unlock(task);
998 return (0);
999}
1000
1001int
1002fill_task_qos_rusage(task_t task, rusage_info_current *ri)
1003{
1004 thread_t thread;
1005
1006 assert(task != TASK_NULL);
1007 task_lock(task);
1008
1009 /* Rollup Qos time of all the threads to task */
1010 queue_iterate(&task->threads, thread, thread_t, task_threads) {
1011 if (thread->options & TH_OPT_IDLE_THREAD)
1012 continue;
1013
39037602 1014 thread_update_qos_cpu_time(thread);
fe8ab488
A
1015 }
1016 ri->ri_cpu_time_qos_default = task->cpu_time_qos_stats.cpu_time_qos_default;
1017 ri->ri_cpu_time_qos_maintenance = task->cpu_time_qos_stats.cpu_time_qos_maintenance;
1018 ri->ri_cpu_time_qos_background = task->cpu_time_qos_stats.cpu_time_qos_background;
1019 ri->ri_cpu_time_qos_utility = task->cpu_time_qos_stats.cpu_time_qos_utility;
1020 ri->ri_cpu_time_qos_legacy = task->cpu_time_qos_stats.cpu_time_qos_legacy;
1021 ri->ri_cpu_time_qos_user_initiated = task->cpu_time_qos_stats.cpu_time_qos_user_initiated;
1022 ri->ri_cpu_time_qos_user_interactive = task->cpu_time_qos_stats.cpu_time_qos_user_interactive;
1023
1024 task_unlock(task);
1025 return (0);
1026}
39037602
A
1027
1028uint64_t
1029get_task_dispatchqueue_serialno_offset(task_t task)
1030{
1031 uint64_t dq_serialno_offset = 0;
1032
1033 if (task->bsd_info) {
1034 dq_serialno_offset = get_dispatchqueue_serialno_offset_from_proc(task->bsd_info);
1035 }
1036
1037 return dq_serialno_offset;
1038}
1039
1040uint64_t
1041get_task_uniqueid(task_t task)
1042{
1043 if (task->bsd_info) {
1044 return proc_uniqueid(task->bsd_info);
1045 } else {
1046 return UINT64_MAX;
1047 }
1048}
1049
1050#if CONFIG_MACF
1051struct label *
1052get_task_crash_label(task_t task)
1053{
1054 return task->crash_label;
1055}
1056
1057void
1058set_task_crash_label(task_t task, struct label *label)
1059{
1060 task->crash_label = label;
1061}
1062#endif