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