]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/bsd_kern.c
xnu-3247.1.106.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
9bccf70c 52task_t bsd_init_task = TASK_NULL;
04b8595b 53boolean_t init_task_died;
55e303ae 54extern unsigned int not_in_kdp; /* Skip acquiring locks if we're in kdp */
1c79356b 55
91447636 56thread_t get_firstthread(task_t);
1c79356b 57int get_task_userstop(task_t);
91447636 58int get_thread_userstop(thread_t);
1c79356b 59boolean_t current_thread_aborted(void);
91447636 60void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *);
91447636
A
61kern_return_t get_signalact(task_t , thread_t *, int);
62int get_vmsubmap_entries(vm_map_t, vm_object_offset_t, vm_object_offset_t);
fe8ab488
A
63int fill_task_rusage(task_t task, rusage_info_current *ri);
64int fill_task_io_rusage(task_t task, rusage_info_current *ri);
65int fill_task_qos_rusage(task_t task, rusage_info_current *ri);
66void fill_task_billed_usage(task_t task, rusage_info_current *ri);
3e170ce0
A
67void task_bsdtask_kill(task_t);
68
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
A
309 * Swap in a new map for the task/thread pair; the old map reference is
310 * returned.
1c79356b
A
311 */
312vm_map_t
6d2010ae 313swap_task_map(task_t task, thread_t thread, vm_map_t map, boolean_t doswitch)
1c79356b
A
314{
315 vm_map_t old_map;
316
91447636 317 if (task != thread->task)
55e303ae
A
318 panic("swap_task_map");
319
1c79356b 320 task_lock(task);
6d2010ae 321 mp_disable_preemption();
1c79356b 322 old_map = task->map;
91447636 323 thread->map = task->map = map;
316670eb 324 if (doswitch) {
6d2010ae 325 pmap_switch(map->pmap);
316670eb 326 }
6d2010ae 327 mp_enable_preemption();
1c79356b 328 task_unlock(task);
0c530ab8 329
b0d623f7 330#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
0c530ab8 331 inval_copy_windows(thread);
b0d623f7 332#endif
0c530ab8 333
1c79356b
A
334 return old_map;
335}
336
1c79356b
A
337/*
338 *
3e170ce0
A
339 * This is only safe to call from a thread executing in
340 * in the task's context or if the task is locked. Otherwise,
341 * the map could be switched for the task (and freed) before
342 * we go to return it here.
1c79356b
A
343 */
344pmap_t get_task_pmap(task_t t)
345{
346 return(t->map->pmap);
347}
348
b0d623f7
A
349/*
350 *
351 */
352uint64_t get_task_resident_size(task_t task)
353{
354 vm_map_t map;
355
356 map = (task == kernel_task) ? kernel_map: task->map;
357 return((uint64_t)pmap_resident_count(map->pmap) * PAGE_SIZE_64);
358}
359
fe8ab488
A
360uint64_t get_task_compressed(task_t task)
361{
362 vm_map_t map;
363
364 map = (task == kernel_task) ? kernel_map: task->map;
365 return((uint64_t)pmap_compressed(map->pmap) * PAGE_SIZE_64);
366}
367
368uint64_t get_task_resident_max(task_t task)
369{
370 vm_map_t map;
371
372 map = (task == kernel_task) ? kernel_map: task->map;
373 return((uint64_t)pmap_resident_max(map->pmap) * PAGE_SIZE_64);
374}
375
376uint64_t get_task_purgeable_size(task_t task)
377{
3e170ce0
A
378 kern_return_t ret;
379 ledger_amount_t credit, debit;
380 uint64_t volatile_size = 0;
381
382 ret = ledger_get_entries(task->ledger, task_ledgers.purgeable_volatile, &credit, &debit);
383 if (ret != KERN_SUCCESS) {
384 return 0;
385 }
386
387 volatile_size += (credit - debit);
388
389 ret = ledger_get_entries(task->ledger, task_ledgers.purgeable_volatile_compressed, &credit, &debit);
390 if (ret != KERN_SUCCESS) {
391 return 0;
392 }
fe8ab488 393
3e170ce0
A
394 volatile_size += (credit - debit);
395
396 return volatile_size;
fe8ab488 397}
3e170ce0 398
39236c6e
A
399/*
400 *
401 */
402uint64_t get_task_phys_footprint(task_t task)
403{
404 kern_return_t ret;
405 ledger_amount_t credit, debit;
406
407 ret = ledger_get_entries(task->ledger, task_ledgers.phys_footprint, &credit, &debit);
408 if (KERN_SUCCESS == ret) {
409 return (credit - debit);
410 }
411
412 return 0;
413}
414
415/*
416 *
417 */
418uint64_t get_task_phys_footprint_max(task_t task)
419{
420 kern_return_t ret;
421 ledger_amount_t max;
422
423 ret = ledger_get_maximum(task->ledger, task_ledgers.phys_footprint, &max);
424 if (KERN_SUCCESS == ret) {
425 return max;
426 }
427
428 return 0;
429}
430
fe8ab488
A
431uint64_t get_task_cpu_time(task_t task)
432{
433 kern_return_t ret;
434 ledger_amount_t credit, debit;
435
436 ret = ledger_get_entries(task->ledger, task_ledgers.cpu_time, &credit, &debit);
437 if (KERN_SUCCESS == ret) {
438 return (credit - debit);
439 }
440
441 return 0;
442}
443
1c79356b
A
444/*
445 *
446 */
91447636 447task_t get_threadtask(thread_t th)
1c79356b
A
448{
449 return(th->task);
450}
451
1c79356b
A
452/*
453 *
454 */
91447636 455vm_map_offset_t
1c79356b
A
456get_map_min(
457 vm_map_t map)
458{
459 return(vm_map_min(map));
460}
461
462/*
463 *
464 */
91447636 465vm_map_offset_t
1c79356b
A
466get_map_max(
467 vm_map_t map)
468{
469 return(vm_map_max(map));
470}
91447636 471vm_map_size_t
1c79356b
A
472get_vmmap_size(
473 vm_map_t map)
474{
475 return(map->size);
476}
477
478int
479get_vmsubmap_entries(
480 vm_map_t map,
481 vm_object_offset_t start,
482 vm_object_offset_t end)
483{
484 int total_entries = 0;
485 vm_map_entry_t entry;
486
55e303ae
A
487 if (not_in_kdp)
488 vm_map_lock(map);
1c79356b
A
489 entry = vm_map_first_entry(map);
490 while((entry != vm_map_to_entry(map)) && (entry->vme_start < start)) {
491 entry = entry->vme_next;
492 }
493
494 while((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
495 if(entry->is_sub_map) {
496 total_entries +=
3e170ce0
A
497 get_vmsubmap_entries(VME_SUBMAP(entry),
498 VME_OFFSET(entry),
499 (VME_OFFSET(entry) +
500 entry->vme_end -
501 entry->vme_start));
1c79356b
A
502 } else {
503 total_entries += 1;
504 }
505 entry = entry->vme_next;
506 }
55e303ae
A
507 if (not_in_kdp)
508 vm_map_unlock(map);
1c79356b
A
509 return(total_entries);
510}
511
512int
513get_vmmap_entries(
514 vm_map_t map)
515{
516 int total_entries = 0;
517 vm_map_entry_t entry;
518
55e303ae
A
519 if (not_in_kdp)
520 vm_map_lock(map);
1c79356b
A
521 entry = vm_map_first_entry(map);
522
523 while(entry != vm_map_to_entry(map)) {
524 if(entry->is_sub_map) {
525 total_entries +=
3e170ce0
A
526 get_vmsubmap_entries(VME_SUBMAP(entry),
527 VME_OFFSET(entry),
528 (VME_OFFSET(entry) +
529 entry->vme_end -
530 entry->vme_start));
1c79356b
A
531 } else {
532 total_entries += 1;
533 }
534 entry = entry->vme_next;
535 }
55e303ae
A
536 if (not_in_kdp)
537 vm_map_unlock(map);
1c79356b
A
538 return(total_entries);
539}
540
541/*
542 *
543 */
544/*
545 *
546 */
547int
548get_task_userstop(
549 task_t task)
550{
551 return(task->user_stop_count);
552}
553
554/*
555 *
556 */
557int
558get_thread_userstop(
91447636 559 thread_t th)
1c79356b
A
560{
561 return(th->user_stop_count);
562}
563
316670eb
A
564/*
565 *
566 */
567boolean_t
568get_task_pidsuspended(
569 task_t task)
570{
571 return (task->pidsuspended);
572}
573
574/*
575 *
576 */
577boolean_t
578get_task_frozen(
579 task_t task)
580{
581 return (task->frozen);
582}
583
1c79356b
A
584/*
585 *
586 */
587boolean_t
588thread_should_abort(
55e303ae 589 thread_t th)
1c79356b 590{
6d2010ae 591 return ((th->sched_flags & TH_SFLAG_ABORTED_MASK) == TH_SFLAG_ABORT);
1c79356b
A
592}
593
594/*
9bccf70c
A
595 * This routine is like thread_should_abort() above. It checks to
596 * see if the current thread is aborted. But unlike above, it also
597 * checks to see if thread is safely aborted. If so, it returns
598 * that fact, and clears the condition (safe aborts only should
599 * have a single effect, and a poll of the abort status
600 * qualifies.
1c79356b
A
601 */
602boolean_t
603current_thread_aborted (
604 void)
605{
606 thread_t th = current_thread();
9bccf70c
A
607 spl_t s;
608
6d2010ae 609 if ((th->sched_flags & TH_SFLAG_ABORTED_MASK) == TH_SFLAG_ABORT &&
91447636 610 (th->options & TH_OPT_INTMASK) != THREAD_UNINT)
9bccf70c 611 return (TRUE);
6d2010ae 612 if (th->sched_flags & TH_SFLAG_ABORTSAFELY) {
9bccf70c
A
613 s = splsched();
614 thread_lock(th);
6d2010ae
A
615 if (th->sched_flags & TH_SFLAG_ABORTSAFELY)
616 th->sched_flags &= ~TH_SFLAG_ABORTED_MASK;
9bccf70c
A
617 thread_unlock(th);
618 splx(s);
619 }
620 return FALSE;
1c79356b
A
621}
622
623/*
624 *
625 */
626void
627task_act_iterate_wth_args(
91447636
A
628 task_t task,
629 void (*func_callback)(thread_t, void *),
630 void *func_arg)
1c79356b 631{
91447636 632 thread_t inc;
1c79356b
A
633
634 task_lock(task);
91447636 635
39236c6e 636 for (inc = (thread_t)(void *)queue_first(&task->threads);
91447636
A
637 !queue_end(&task->threads, (queue_entry_t)inc); ) {
638 (void) (*func_callback)(inc, func_arg);
39236c6e 639 inc = (thread_t)(void *)queue_next(&inc->task_threads);
91447636
A
640 }
641
1c79356b
A
642 task_unlock(task);
643}
644
1c79356b 645
0c530ab8
A
646#include <sys/bsdtask_info.h>
647
648void
649fill_taskprocinfo(task_t task, struct proc_taskinfo_internal * ptinfo)
650{
651 vm_map_t map;
652 task_absolutetime_info_data_t tinfo;
653 thread_t thread;
6d2010ae
A
654 uint32_t cswitch = 0, numrunning = 0;
655 uint32_t syscalls_unix = 0;
656 uint32_t syscalls_mach = 0;
39236c6e 657
3e170ce0
A
658 task_lock(task);
659
0c530ab8
A
660 map = (task == kernel_task)? kernel_map: task->map;
661
662 ptinfo->pti_virtual_size = map->size;
2d21ac55
A
663 ptinfo->pti_resident_size =
664 (mach_vm_size_t)(pmap_resident_count(map->pmap))
665 * PAGE_SIZE_64;
0c530ab8 666
0c530ab8
A
667 ptinfo->pti_policy = ((task != kernel_task)?
668 POLICY_TIMESHARE: POLICY_RR);
669
670 tinfo.threads_user = tinfo.threads_system = 0;
671 tinfo.total_user = task->total_user_time;
672 tinfo.total_system = task->total_system_time;
673
674 queue_iterate(&task->threads, thread, thread_t, task_threads) {
675 uint64_t tval;
316670eb
A
676 spl_t x;
677
39236c6e
A
678 if (thread->options & TH_OPT_IDLE_THREAD)
679 continue;
680
316670eb
A
681 x = splsched();
682 thread_lock(thread);
0c530ab8
A
683
684 if ((thread->state & TH_RUN) == TH_RUN)
685 numrunning++;
2d21ac55 686 cswitch += thread->c_switch;
0c530ab8
A
687 tval = timer_grab(&thread->user_timer);
688 tinfo.threads_user += tval;
689 tinfo.total_user += tval;
690
691 tval = timer_grab(&thread->system_timer);
316670eb
A
692
693 if (thread->precise_user_kernel_time) {
694 tinfo.threads_system += tval;
695 tinfo.total_system += tval;
696 } else {
697 /* system_timer may represent either sys or user */
698 tinfo.threads_user += tval;
699 tinfo.total_user += tval;
700 }
6d2010ae
A
701
702 syscalls_unix += thread->syscalls_unix;
703 syscalls_mach += thread->syscalls_mach;
316670eb
A
704
705 thread_unlock(thread);
706 splx(x);
0c530ab8
A
707 }
708
709 ptinfo->pti_total_system = tinfo.total_system;
710 ptinfo->pti_total_user = tinfo.total_user;
711 ptinfo->pti_threads_system = tinfo.threads_system;
712 ptinfo->pti_threads_user = tinfo.threads_user;
713
714 ptinfo->pti_faults = task->faults;
715 ptinfo->pti_pageins = task->pageins;
716 ptinfo->pti_cow_faults = task->cow_faults;
717 ptinfo->pti_messages_sent = task->messages_sent;
718 ptinfo->pti_messages_received = task->messages_received;
6d2010ae
A
719 ptinfo->pti_syscalls_mach = task->syscalls_mach + syscalls_mach;
720 ptinfo->pti_syscalls_unix = task->syscalls_unix + syscalls_unix;
2d21ac55 721 ptinfo->pti_csw = task->c_switch + cswitch;
0c530ab8
A
722 ptinfo->pti_threadnum = task->thread_count;
723 ptinfo->pti_numrunning = numrunning;
724 ptinfo->pti_priority = task->priority;
725
726 task_unlock(task);
727}
728
729int
316670eb 730fill_taskthreadinfo(task_t task, uint64_t thaddr, int thuniqueid, struct proc_threadinfo_internal * ptinfo, void * vpp, int *vidp)
0c530ab8
A
731{
732 thread_t thact;
2d21ac55
A
733 int err=0;
734 mach_msg_type_number_t count;
0c530ab8
A
735 thread_basic_info_data_t basic_info;
736 kern_return_t kret;
316670eb 737 uint64_t addr = 0;
0c530ab8
A
738
739 task_lock(task);
740
39236c6e 741 for (thact = (thread_t)(void *)queue_first(&task->threads);
0c530ab8 742 !queue_end(&task->threads, (queue_entry_t)thact); ) {
316670eb
A
743 addr = (thuniqueid==0)?thact->machine.cthread_self: thact->thread_id;
744 if (addr == thaddr)
0c530ab8
A
745 {
746
747 count = THREAD_BASIC_INFO_COUNT;
2d21ac55 748 if ((kret = thread_info_internal(thact, THREAD_BASIC_INFO, (thread_info_t)&basic_info, &count)) != KERN_SUCCESS) {
0c530ab8
A
749 err = 1;
750 goto out;
751 }
39236c6e
A
752 ptinfo->pth_user_time = ((basic_info.user_time.seconds * (integer_t)NSEC_PER_SEC) + (basic_info.user_time.microseconds * (integer_t)NSEC_PER_USEC));
753 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 754
0c530ab8
A
755 ptinfo->pth_cpu_usage = basic_info.cpu_usage;
756 ptinfo->pth_policy = basic_info.policy;
757 ptinfo->pth_run_state = basic_info.run_state;
758 ptinfo->pth_flags = basic_info.flags;
759 ptinfo->pth_sleep_time = basic_info.sleep_time;
760 ptinfo->pth_curpri = thact->sched_pri;
3e170ce0 761 ptinfo->pth_priority = thact->base_pri;
0c530ab8
A
762 ptinfo->pth_maxpriority = thact->max_priority;
763
2d21ac55
A
764 if ((vpp != NULL) && (thact->uthread != NULL))
765 bsd_threadcdir(thact->uthread, vpp, vidp);
b0d623f7 766 bsd_getthreadname(thact->uthread,ptinfo->pth_name);
0c530ab8
A
767 err = 0;
768 goto out;
769 }
39236c6e 770 thact = (thread_t)(void *)queue_next(&thact->task_threads);
0c530ab8
A
771 }
772 err = 1;
773
774out:
775 task_unlock(task);
776 return(err);
777}
778
779int
780fill_taskthreadlist(task_t task, void * buffer, int thcount)
781{
782 int numthr=0;
783 thread_t thact;
784 uint64_t * uptr;
785 uint64_t thaddr;
786
787 uptr = (uint64_t *)buffer;
788
789 task_lock(task);
790
39236c6e 791 for (thact = (thread_t)(void *)queue_first(&task->threads);
0c530ab8 792 !queue_end(&task->threads, (queue_entry_t)thact); ) {
0c530ab8 793 thaddr = thact->machine.cthread_self;
0c530ab8
A
794 *uptr++ = thaddr;
795 numthr++;
796 if (numthr >= thcount)
797 goto out;
39236c6e 798 thact = (thread_t)(void *)queue_next(&thact->task_threads);
0c530ab8
A
799 }
800
801out:
802 task_unlock(task);
b0d623f7 803 return (int)(numthr * sizeof(uint64_t));
0c530ab8
A
804
805}
806
807int
808get_numthreads(task_t task)
809{
810 return(task->thread_count);
811}
812
39236c6e
A
813/*
814 * Gather the various pieces of info about the designated task,
815 * and collect it all into a single rusage_info.
816 */
817int
fe8ab488 818fill_task_rusage(task_t task, rusage_info_current *ri)
39236c6e
A
819{
820 struct task_power_info powerinfo;
821
822 assert(task != TASK_NULL);
823 task_lock(task);
824
fe8ab488 825 task_power_info_locked(task, &powerinfo, NULL);
39236c6e
A
826 ri->ri_pkg_idle_wkups = powerinfo.task_platform_idle_wakeups;
827 ri->ri_interrupt_wkups = powerinfo.task_interrupt_wakeups;
828 ri->ri_user_time = powerinfo.total_user;
829 ri->ri_system_time = powerinfo.total_system;
830
831 ledger_get_balance(task->ledger, task_ledgers.phys_footprint,
832 (ledger_amount_t *)&ri->ri_phys_footprint);
833 ledger_get_balance(task->ledger, task_ledgers.phys_mem,
834 (ledger_amount_t *)&ri->ri_resident_size);
835 ledger_get_balance(task->ledger, task_ledgers.wired_mem,
836 (ledger_amount_t *)&ri->ri_wired_size);
837
838 ri->ri_pageins = task->pageins;
839
840 task_unlock(task);
841 return (0);
842}
fe8ab488
A
843
844void
845fill_task_billed_usage(task_t task __unused, rusage_info_current *ri)
846{
847#if CONFIG_BANK
848 ri->ri_billed_system_time = bank_billed_time(task->bank_context);
849 ri->ri_serviced_system_time = bank_serviced_time(task->bank_context);
850#else
851 ri->ri_billed_system_time = 0;
852 ri->ri_serviced_system_time = 0;
853#endif
854}
855
856int
857fill_task_io_rusage(task_t task, rusage_info_current *ri)
858{
859 assert(task != TASK_NULL);
860 task_lock(task);
861
862 if (task->task_io_stats) {
863 ri->ri_diskio_bytesread = task->task_io_stats->disk_reads.size;
864 ri->ri_diskio_byteswritten = (task->task_io_stats->total_io.size - task->task_io_stats->disk_reads.size);
865 } else {
866 /* I/O Stats unavailable */
867 ri->ri_diskio_bytesread = 0;
868 ri->ri_diskio_byteswritten = 0;
869 }
870 task_unlock(task);
871 return (0);
872}
873
874int
875fill_task_qos_rusage(task_t task, rusage_info_current *ri)
876{
877 thread_t thread;
878
879 assert(task != TASK_NULL);
880 task_lock(task);
881
882 /* Rollup Qos time of all the threads to task */
883 queue_iterate(&task->threads, thread, thread_t, task_threads) {
884 if (thread->options & TH_OPT_IDLE_THREAD)
885 continue;
886
887 thread_mtx_lock(thread);
888 thread_update_qos_cpu_time(thread, TRUE);
889 thread_mtx_unlock(thread);
890
891 }
892 ri->ri_cpu_time_qos_default = task->cpu_time_qos_stats.cpu_time_qos_default;
893 ri->ri_cpu_time_qos_maintenance = task->cpu_time_qos_stats.cpu_time_qos_maintenance;
894 ri->ri_cpu_time_qos_background = task->cpu_time_qos_stats.cpu_time_qos_background;
895 ri->ri_cpu_time_qos_utility = task->cpu_time_qos_stats.cpu_time_qos_utility;
896 ri->ri_cpu_time_qos_legacy = task->cpu_time_qos_stats.cpu_time_qos_legacy;
897 ri->ri_cpu_time_qos_user_initiated = task->cpu_time_qos_stats.cpu_time_qos_user_initiated;
898 ri->ri_cpu_time_qos_user_interactive = task->cpu_time_qos_stats.cpu_time_qos_user_interactive;
899
900 task_unlock(task);
901 return (0);
902}