2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub
63 * Thread management primitives implementation.
66 * Copyright (c) 1993 The University of Utah and
67 * the Computer Systems Laboratory (CSL). All rights reserved.
69 * Permission to use, copy, modify and distribute this software and its
70 * documentation is hereby granted, provided that both the copyright
71 * notice and this permission notice appear in all copies of the
72 * software, derivative works or modified versions, and any portions
73 * thereof, and that both notices appear in supporting documentation.
75 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
76 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
77 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
79 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
80 * improvements that they make and grant CSL redistribution rights.
84 #include <mach/mach_types.h>
85 #include <mach/boolean.h>
86 #include <mach/policy.h>
87 #include <mach/thread_info.h>
88 #include <mach/thread_special_ports.h>
89 #include <mach/thread_status.h>
90 #include <mach/time_value.h>
91 #include <mach/vm_param.h>
93 #include <machine/thread.h>
94 #include <machine/pal_routines.h>
95 #include <machine/limits.h>
97 #include <kern/kern_types.h>
98 #include <kern/kalloc.h>
99 #include <kern/cpu_data.h>
100 #include <kern/counters.h>
101 #include <kern/extmod_statistics.h>
102 #include <kern/ipc_mig.h>
103 #include <kern/ipc_tt.h>
104 #include <kern/mach_param.h>
105 #include <kern/machine.h>
106 #include <kern/misc_protos.h>
107 #include <kern/processor.h>
108 #include <kern/queue.h>
109 #include <kern/sched.h>
110 #include <kern/sched_prim.h>
111 #include <kern/sync_lock.h>
112 #include <kern/syscall_subr.h>
113 #include <kern/task.h>
114 #include <kern/thread.h>
115 #include <kern/host.h>
116 #include <kern/zalloc.h>
117 #include <kern/assert.h>
118 #include <kern/exc_resource.h>
119 #include <kern/telemetry.h>
121 #include <kern/kpc.h>
124 #include <ipc/ipc_kmsg.h>
125 #include <ipc/ipc_port.h>
126 #include <bank/bank_types.h>
128 #include <vm/vm_kern.h>
129 #include <vm/vm_pageout.h>
131 #include <sys/kdebug.h>
133 #include <mach/sdt.h>
136 * Exported interfaces
138 #include <mach/task_server.h>
139 #include <mach/thread_act_server.h>
140 #include <mach/mach_host_server.h>
141 #include <mach/host_priv_server.h>
142 #include <mach/mach_voucher_server.h>
144 static struct zone
*thread_zone
;
145 static lck_grp_attr_t thread_lck_grp_attr
;
146 lck_attr_t thread_lck_attr
;
147 lck_grp_t thread_lck_grp
;
149 struct zone
*thread_qos_override_zone
;
151 decl_simple_lock_data(static,thread_stack_lock
)
152 static queue_head_t thread_stack_queue
;
154 decl_simple_lock_data(static,thread_terminate_lock
)
155 static queue_head_t thread_terminate_queue
;
157 static struct thread thread_template
, init_thread
;
159 static void sched_call_null(
164 extern void proc_exit(void *);
165 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
166 extern int proc_selfpid(void);
167 extern char * proc_name_address(void *p
);
168 #endif /* MACH_BSD */
170 extern int disable_exc_resource
;
171 extern int audio_active
;
172 extern int debug_task
;
173 int thread_max
= CONFIG_THREAD_MAX
; /* Max number of threads */
174 int task_threadmax
= CONFIG_THREAD_MAX
;
176 static uint64_t thread_unique_id
= 100;
178 struct _thread_ledger_indices thread_ledgers
= { -1 };
179 static ledger_template_t thread_ledger_template
= NULL
;
180 void init_thread_ledgers(void);
181 int task_disable_cpumon(task_t task
);
184 void jetsam_on_ledger_cpulimit_exceeded(void);
188 * Level (in terms of percentage of the limit) at which the CPU usage monitor triggers telemetry.
190 * (ie when any thread's CPU consumption exceeds 70% of the limit, start taking user
191 * stacktraces, aka micro-stackshots)
193 #define CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT 70
195 int cpumon_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
196 void __attribute__((noinline
)) THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void);
199 * The smallest interval over which we support limiting CPU consumption is 1ms
201 #define MINIMUM_CPULIMIT_INTERVAL_MS 1
204 thread_bootstrap(void)
207 * Fill in a template thread for fast initialization.
210 thread_template
.runq
= PROCESSOR_NULL
;
212 thread_template
.ref_count
= 2;
214 thread_template
.reason
= AST_NONE
;
215 thread_template
.at_safe_point
= FALSE
;
216 thread_template
.wait_event
= NO_EVENT64
;
217 thread_template
.wait_queue
= WAIT_QUEUE_NULL
;
218 thread_template
.wait_result
= THREAD_WAITING
;
219 thread_template
.options
= THREAD_ABORTSAFE
;
220 thread_template
.state
= TH_WAIT
| TH_UNINT
;
221 thread_template
.wake_active
= FALSE
;
222 thread_template
.continuation
= THREAD_CONTINUE_NULL
;
223 thread_template
.parameter
= NULL
;
225 thread_template
.importance
= 0;
226 thread_template
.sched_mode
= TH_MODE_NONE
;
227 thread_template
.sched_flags
= 0;
228 thread_template
.saved_mode
= TH_MODE_NONE
;
229 thread_template
.safe_release
= 0;
231 thread_template
.sfi_class
= SFI_CLASS_UNSPECIFIED
;
232 thread_template
.sfi_wait_class
= SFI_CLASS_UNSPECIFIED
;
234 thread_template
.active
= 0;
235 thread_template
.started
= 0;
236 thread_template
.static_param
= 0;
237 thread_template
.policy_reset
= 0;
239 thread_template
.priority
= 0;
240 thread_template
.sched_pri
= 0;
241 thread_template
.max_priority
= 0;
242 thread_template
.task_priority
= 0;
243 thread_template
.promotions
= 0;
244 thread_template
.pending_promoter_index
= 0;
245 thread_template
.pending_promoter
[0] =
246 thread_template
.pending_promoter
[1] = NULL
;
247 thread_template
.rwlock_count
= 0;
250 thread_template
.SHARE_COUNT
= 0;
251 thread_template
.BG_COUNT
= 0;
252 #endif /* MACH_ASSERT */
254 thread_template
.realtime
.deadline
= UINT64_MAX
;
256 thread_template
.quantum_remaining
= 0;
257 thread_template
.last_run_time
= 0;
259 thread_template
.computation_metered
= 0;
260 thread_template
.computation_epoch
= 0;
262 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
263 thread_template
.sched_stamp
= 0;
264 thread_template
.pri_shift
= INT8_MAX
;
265 thread_template
.sched_usage
= 0;
266 thread_template
.cpu_usage
= thread_template
.cpu_delta
= 0;
268 thread_template
.c_switch
= thread_template
.p_switch
= thread_template
.ps_switch
= 0;
270 thread_template
.bound_processor
= PROCESSOR_NULL
;
271 thread_template
.last_processor
= PROCESSOR_NULL
;
273 thread_template
.sched_call
= sched_call_null
;
275 timer_init(&thread_template
.user_timer
);
276 timer_init(&thread_template
.system_timer
);
277 thread_template
.user_timer_save
= 0;
278 thread_template
.system_timer_save
= 0;
279 thread_template
.vtimer_user_save
= 0;
280 thread_template
.vtimer_prof_save
= 0;
281 thread_template
.vtimer_rlim_save
= 0;
283 thread_template
.wait_sfi_begin_time
= 0;
285 thread_template
.wait_timer_is_set
= FALSE
;
286 thread_template
.wait_timer_active
= 0;
288 thread_template
.depress_timer_active
= 0;
290 thread_template
.special_handler
.handler
= special_handler
;
291 thread_template
.special_handler
.next
= NULL
;
293 thread_template
.recover
= (vm_offset_t
)NULL
;
295 thread_template
.map
= VM_MAP_NULL
;
298 thread_template
.t_dtrace_predcache
= 0;
299 thread_template
.t_dtrace_vtime
= 0;
300 thread_template
.t_dtrace_tracing
= 0;
301 #endif /* CONFIG_DTRACE */
304 thread_template
.kpc_buf
= NULL
;
308 thread_template
.hv_thread_target
= NULL
;
309 #endif /* HYPERVISOR */
311 thread_template
.t_chud
= 0;
312 thread_template
.t_page_creation_count
= 0;
313 thread_template
.t_page_creation_time
= 0;
315 thread_template
.affinity_set
= NULL
;
317 thread_template
.syscalls_unix
= 0;
318 thread_template
.syscalls_mach
= 0;
320 thread_template
.t_ledger
= LEDGER_NULL
;
321 thread_template
.t_threadledger
= LEDGER_NULL
;
323 thread_template
.t_bankledger
= LEDGER_NULL
;
324 thread_template
.t_deduct_bank_ledger_time
= 0;
327 thread_template
.requested_policy
= default_task_requested_policy
;
328 thread_template
.effective_policy
= default_task_effective_policy
;
329 thread_template
.pended_policy
= default_task_pended_policy
;
331 bzero(&thread_template
.overrides
, sizeof(thread_template
.overrides
));
333 thread_template
.iotier_override
= THROTTLE_LEVEL_NONE
;
334 thread_template
.thread_io_stats
= NULL
;
335 thread_template
.thread_callout_interrupt_wakeups
= thread_template
.thread_callout_platform_idle_wakeups
= 0;
337 thread_template
.thread_timer_wakeups_bin_1
= thread_template
.thread_timer_wakeups_bin_2
= 0;
338 thread_template
.callout_woken_from_icontext
= thread_template
.callout_woken_from_platform_idle
= 0;
340 thread_template
.thread_tag
= 0;
342 thread_template
.ith_voucher_name
= MACH_PORT_NULL
;
343 thread_template
.ith_voucher
= IPC_VOUCHER_NULL
;
345 init_thread
= thread_template
;
346 machine_set_current_thread(&init_thread
);
349 extern boolean_t allow_qos_policy_set
;
355 sizeof(struct thread
),
356 thread_max
* sizeof(struct thread
),
357 THREAD_CHUNK
* sizeof(struct thread
),
360 thread_qos_override_zone
= zinit(
361 sizeof(struct thread_qos_override
),
362 4 * thread_max
* sizeof(struct thread_qos_override
),
364 "thread qos override");
365 zone_change(thread_qos_override_zone
, Z_EXPAND
, TRUE
);
366 zone_change(thread_qos_override_zone
, Z_COLLECT
, TRUE
);
367 zone_change(thread_qos_override_zone
, Z_CALLERACCT
, FALSE
);
368 zone_change(thread_qos_override_zone
, Z_NOENCRYPT
, TRUE
);
370 lck_grp_attr_setdefault(&thread_lck_grp_attr
);
371 lck_grp_init(&thread_lck_grp
, "thread", &thread_lck_grp_attr
);
372 lck_attr_setdefault(&thread_lck_attr
);
377 * Initialize any machine-dependent
378 * per-thread structures necessary.
380 machine_thread_init();
382 if (!PE_parse_boot_argn("cpumon_ustackshots_trigger_pct", &cpumon_ustackshots_trigger_pct
,
383 sizeof (cpumon_ustackshots_trigger_pct
))) {
384 cpumon_ustackshots_trigger_pct
= CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT
;
387 PE_parse_boot_argn("-qos-policy-allow", &allow_qos_policy_set
, sizeof(allow_qos_policy_set
));
389 init_thread_ledgers();
393 thread_terminate_continue(void)
395 panic("thread_terminate_continue");
400 * thread_terminate_self:
403 thread_terminate_self(void)
405 thread_t thread
= current_thread();
411 pal_thread_terminate_self(thread
);
413 DTRACE_PROC(lwp__exit
);
415 thread_mtx_lock(thread
);
417 ipc_thread_disable(thread
);
419 thread_mtx_unlock(thread
);
424 assert_thread_sched_count(thread
);
427 * Cancel priority depression, wait for concurrent expirations
428 * on other processors.
430 if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
431 thread
->sched_flags
&= ~TH_SFLAG_DEPRESSED_MASK
;
433 /* If our priority was low because of a depressed yield, restore it in case we block below */
434 set_sched_pri(thread
, thread
->priority
);
436 if (timer_call_cancel(&thread
->depress_timer
))
437 thread
->depress_timer_active
--;
440 while (thread
->depress_timer_active
> 0) {
441 thread_unlock(thread
);
450 thread_sched_call(thread
, NULL
);
452 thread_unlock(thread
);
456 thread_mtx_lock(thread
);
458 thread_policy_reset(thread
);
460 thread_mtx_unlock(thread
);
463 uthread_cleanup(task
, thread
->uthread
, task
->bsd_info
);
464 threadcnt
= hw_atomic_sub(&task
->active_thread_count
, 1);
467 * If we are the last thread to terminate and the task is
468 * associated with a BSD process, perform BSD process exit.
470 if (threadcnt
== 0 && task
->bsd_info
!= NULL
)
471 proc_exit(task
->bsd_info
);
473 uthread_cred_free(thread
->uthread
);
479 * Cancel wait timer, and wait for
480 * concurrent expirations.
482 if (thread
->wait_timer_is_set
) {
483 thread
->wait_timer_is_set
= FALSE
;
485 if (timer_call_cancel(&thread
->wait_timer
))
486 thread
->wait_timer_active
--;
489 while (thread
->wait_timer_active
> 0) {
490 thread_unlock(thread
);
500 * If there is a reserved stack, release it.
502 if (thread
->reserved_stack
!= 0) {
503 stack_free_reserved(thread
);
504 thread
->reserved_stack
= 0;
508 * Mark thread as terminating, and block.
510 thread
->state
|= TH_TERMINATE
;
511 thread_mark_wait_locked(thread
, THREAD_UNINT
);
512 assert(thread
->promotions
== 0);
513 assert(thread
->rwlock_count
== 0);
514 thread_unlock(thread
);
517 thread_block((thread_continue_t
)thread_terminate_continue
);
527 if (thread
== THREAD_NULL
)
530 if (thread_deallocate_internal(thread
) > 0)
533 if(!(thread
->state
& TH_TERMINATE2
))
534 panic("thread_deallocate: thread not properly terminated\n");
537 kpc_thread_destroy(thread
);
540 ipc_thread_terminate(thread
);
542 proc_thread_qos_deallocate(thread
);
548 void *ut
= thread
->uthread
;
550 thread
->uthread
= NULL
;
551 uthread_zone_free(ut
);
553 #endif /* MACH_BSD */
555 if (thread
->t_ledger
)
556 ledger_dereference(thread
->t_ledger
);
557 if (thread
->t_threadledger
)
558 ledger_dereference(thread
->t_threadledger
);
560 if (IPC_VOUCHER_NULL
!= thread
->ith_voucher
)
561 ipc_voucher_release(thread
->ith_voucher
);
563 if (thread
->thread_io_stats
)
564 kfree(thread
->thread_io_stats
, sizeof(struct io_stat_info
));
566 if (thread
->kernel_stack
!= 0)
569 lck_mtx_destroy(&thread
->mutex
, &thread_lck_grp
);
570 machine_thread_destroy(thread
);
572 task_deallocate(task
);
574 zfree(thread_zone
, thread
);
578 * thread_terminate_daemon:
580 * Perform final clean up for terminating threads.
583 thread_terminate_daemon(void)
585 thread_t self
, thread
;
588 self
= current_thread();
589 self
->options
|= TH_OPT_SYSTEM_CRITICAL
;
592 simple_lock(&thread_terminate_lock
);
594 while ((thread
= (thread_t
)dequeue_head(&thread_terminate_queue
)) != THREAD_NULL
) {
595 simple_unlock(&thread_terminate_lock
);
598 assert(thread
->SHARE_COUNT
== 0);
599 assert(thread
->BG_COUNT
== 0);
604 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
605 if (thread
->precise_user_kernel_time
) {
606 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
608 task
->total_user_time
+= timer_grab(&thread
->system_timer
);
611 task
->c_switch
+= thread
->c_switch
;
612 task
->p_switch
+= thread
->p_switch
;
613 task
->ps_switch
+= thread
->ps_switch
;
615 task
->syscalls_unix
+= thread
->syscalls_unix
;
616 task
->syscalls_mach
+= thread
->syscalls_mach
;
618 task
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
619 task
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
620 task
->task_gpu_ns
+= ml_gpu_stat(thread
);
622 thread_update_qos_cpu_time(thread
, FALSE
);
623 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
624 task
->thread_count
--;
627 * If the task is being halted, and there is only one thread
628 * left in the task after this one, then wakeup that thread.
630 if (task
->thread_count
== 1 && task
->halting
)
631 thread_wakeup((event_t
)&task
->halting
);
635 lck_mtx_lock(&tasks_threads_lock
);
636 queue_remove(&threads
, thread
, thread_t
, threads
);
638 lck_mtx_unlock(&tasks_threads_lock
);
640 thread_deallocate(thread
);
643 simple_lock(&thread_terminate_lock
);
646 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
647 simple_unlock(&thread_terminate_lock
);
650 self
->options
&= ~TH_OPT_SYSTEM_CRITICAL
;
651 thread_block((thread_continue_t
)thread_terminate_daemon
);
656 * thread_terminate_enqueue:
658 * Enqueue a terminating thread for final disposition.
660 * Called at splsched.
663 thread_terminate_enqueue(
666 KERNEL_DEBUG_CONSTANT(TRACEDBG_CODE(DBG_TRACE_DATA
, TRACE_DATA_THREAD_TERMINATE
) | DBG_FUNC_NONE
, thread
->thread_id
, 0, 0, 0, 0);
668 simple_lock(&thread_terminate_lock
);
669 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
670 simple_unlock(&thread_terminate_lock
);
672 thread_wakeup((event_t
)&thread_terminate_queue
);
676 * thread_stack_daemon:
678 * Perform stack allocation as required due to
682 thread_stack_daemon(void)
688 simple_lock(&thread_stack_lock
);
690 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
691 simple_unlock(&thread_stack_lock
);
694 /* allocate stack with interrupts enabled so that we can call into VM */
699 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
700 thread_unlock(thread
);
702 simple_lock(&thread_stack_lock
);
705 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
706 simple_unlock(&thread_stack_lock
);
709 thread_block((thread_continue_t
)thread_stack_daemon
);
714 * thread_stack_enqueue:
716 * Enqueue a thread for stack allocation.
718 * Called at splsched.
721 thread_stack_enqueue(
724 simple_lock(&thread_stack_lock
);
725 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
726 simple_unlock(&thread_stack_lock
);
728 thread_wakeup((event_t
)&thread_stack_queue
);
732 thread_daemon_init(void)
734 kern_return_t result
;
735 thread_t thread
= NULL
;
737 simple_lock_init(&thread_terminate_lock
, 0);
738 queue_init(&thread_terminate_queue
);
740 result
= kernel_thread_start_priority((thread_continue_t
)thread_terminate_daemon
, NULL
, MINPRI_KERNEL
, &thread
);
741 if (result
!= KERN_SUCCESS
)
742 panic("thread_daemon_init: thread_terminate_daemon");
744 thread_deallocate(thread
);
746 simple_lock_init(&thread_stack_lock
, 0);
747 queue_init(&thread_stack_queue
);
749 result
= kernel_thread_start_priority((thread_continue_t
)thread_stack_daemon
, NULL
, BASEPRI_PREEMPT
, &thread
);
750 if (result
!= KERN_SUCCESS
)
751 panic("thread_daemon_init: thread_stack_daemon");
753 thread_deallocate(thread
);
757 * Create a new thread.
758 * Doesn't start the thread running.
760 * Task and tasks_threads_lock are returned locked on success.
763 thread_create_internal(
766 thread_continue_t continuation
,
768 #define TH_OPTION_NONE 0x00
769 #define TH_OPTION_NOCRED 0x01
770 #define TH_OPTION_NOSUSP 0x02
771 thread_t
*out_thread
)
774 static thread_t first_thread
;
777 * Allocate a thread and initialize static fields
779 if (first_thread
== THREAD_NULL
)
780 new_thread
= first_thread
= current_thread();
782 new_thread
= (thread_t
)zalloc(thread_zone
);
783 if (new_thread
== THREAD_NULL
)
784 return (KERN_RESOURCE_SHORTAGE
);
786 if (new_thread
!= first_thread
)
787 *new_thread
= thread_template
;
790 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
, (options
& TH_OPTION_NOCRED
) != 0);
791 if (new_thread
->uthread
== NULL
) {
792 zfree(thread_zone
, new_thread
);
793 return (KERN_RESOURCE_SHORTAGE
);
795 #endif /* MACH_BSD */
797 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
799 void *ut
= new_thread
->uthread
;
801 new_thread
->uthread
= NULL
;
802 /* cred free may not be necessary */
803 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
804 uthread_cred_free(ut
);
805 uthread_zone_free(ut
);
806 #endif /* MACH_BSD */
808 zfree(thread_zone
, new_thread
);
809 return (KERN_FAILURE
);
812 new_thread
->task
= parent_task
;
814 thread_lock_init(new_thread
);
815 wake_lock_init(new_thread
);
817 lck_mtx_init(&new_thread
->mutex
, &thread_lck_grp
, &thread_lck_attr
);
819 ipc_thread_init(new_thread
);
821 new_thread
->continuation
= continuation
;
823 /* Allocate I/O Statistics structure */
824 new_thread
->thread_io_stats
= (io_stat_info_t
)kalloc(sizeof(struct io_stat_info
));
825 assert(new_thread
->thread_io_stats
!= NULL
);
826 bzero(new_thread
->thread_io_stats
, sizeof(struct io_stat_info
));
829 /* Clear out the I/O Scheduling info for AppleFSCompression */
830 new_thread
->decmp_upl
= NULL
;
831 #endif /* CONFIG_IOSCHED */
833 lck_mtx_lock(&tasks_threads_lock
);
834 task_lock(parent_task
);
836 if ( !parent_task
->active
|| parent_task
->halting
||
837 ((options
& TH_OPTION_NOSUSP
) != 0 &&
838 parent_task
->suspend_count
> 0) ||
839 (parent_task
->thread_count
>= task_threadmax
&&
840 parent_task
!= kernel_task
) ) {
841 task_unlock(parent_task
);
842 lck_mtx_unlock(&tasks_threads_lock
);
846 void *ut
= new_thread
->uthread
;
848 new_thread
->uthread
= NULL
;
849 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
850 /* cred free may not be necessary */
851 uthread_cred_free(ut
);
852 uthread_zone_free(ut
);
854 #endif /* MACH_BSD */
855 ipc_thread_disable(new_thread
);
856 ipc_thread_terminate(new_thread
);
857 kfree(new_thread
->thread_io_stats
, sizeof(struct io_stat_info
));
858 lck_mtx_destroy(&new_thread
->mutex
, &thread_lck_grp
);
859 machine_thread_destroy(new_thread
);
860 zfree(thread_zone
, new_thread
);
861 return (KERN_FAILURE
);
864 /* New threads inherit any default state on the task */
865 machine_thread_inherit_taskwide(new_thread
, parent_task
);
867 task_reference_internal(parent_task
);
869 if (new_thread
->task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
871 * This task has a per-thread CPU limit; make sure this new thread
872 * gets its limit set too, before it gets out of the kernel.
874 set_astledger(new_thread
);
877 /* Instantiate a thread ledger. Do not fail thread creation if ledger creation fails. */
878 if ((new_thread
->t_threadledger
= ledger_instantiate(thread_ledger_template
,
879 LEDGER_CREATE_INACTIVE_ENTRIES
)) != LEDGER_NULL
) {
881 ledger_entry_setactive(new_thread
->t_threadledger
, thread_ledgers
.cpu_time
);
884 new_thread
->cpu_time_last_qos
= 0;
886 new_thread
->t_bankledger
= LEDGER_NULL
;
887 new_thread
->t_deduct_bank_ledger_time
= 0;
890 new_thread
->t_ledger
= new_thread
->task
->ledger
;
891 if (new_thread
->t_ledger
)
892 ledger_reference(new_thread
->t_ledger
);
894 #if defined(CONFIG_SCHED_MULTIQ)
895 /* Cache the task's sched_group */
896 new_thread
->sched_group
= parent_task
->sched_group
;
897 #endif /* defined(CONFIG_SCHED_MULTIQ) */
899 /* Cache the task's map */
900 new_thread
->map
= parent_task
->map
;
902 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
903 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
907 * If parent task has any reservations, they need to be propagated to this
910 new_thread
->t_chud
= (TASK_PMC_FLAG
== (parent_task
->t_chud
& TASK_PMC_FLAG
)) ?
911 THREAD_PMC_FLAG
: 0U;
914 kpc_thread_create(new_thread
);
917 /* Only need to update policies pushed from task to thread */
918 new_thread
->requested_policy
.bg_iotier
= parent_task
->effective_policy
.bg_iotier
;
919 new_thread
->requested_policy
.terminated
= parent_task
->effective_policy
.terminated
;
921 /* Set the thread's scheduling parameters */
922 new_thread
->sched_mode
= SCHED(initial_thread_sched_mode
)(parent_task
);
923 new_thread
->sched_flags
= 0;
924 new_thread
->max_priority
= parent_task
->max_priority
;
925 new_thread
->task_priority
= parent_task
->priority
;
926 new_thread
->priority
= (priority
< 0)? parent_task
->priority
: priority
;
927 if (new_thread
->priority
> new_thread
->max_priority
)
928 new_thread
->priority
= new_thread
->max_priority
;
929 new_thread
->importance
= new_thread
->priority
- new_thread
->task_priority
;
930 new_thread
->saved_importance
= new_thread
->importance
;
932 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
933 new_thread
->sched_stamp
= sched_tick
;
934 new_thread
->pri_shift
= sched_pri_shift
;
935 #endif /* defined(CONFIG_SCHED_TIMESHARE_CORE) */
937 if (parent_task
->max_priority
<= MAXPRI_THROTTLE
) {
938 sched_set_thread_throttled(new_thread
, TRUE
);
941 SCHED(compute_priority
)(new_thread
, FALSE
);
943 thread_policy_create(new_thread
);
945 /* Chain the thread onto the task's list */
946 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
947 parent_task
->thread_count
++;
949 /* So terminating threads don't need to take the task lock to decrement */
950 hw_atomic_add(&parent_task
->active_thread_count
, 1);
952 /* Protected by the tasks_threads_lock */
953 new_thread
->thread_id
= ++thread_unique_id
;
955 queue_enter(&threads
, new_thread
, thread_t
, threads
);
958 new_thread
->active
= TRUE
;
960 *out_thread
= new_thread
;
963 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
965 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
967 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
968 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
969 (vm_address_t
)(uintptr_t)thread_tid(new_thread
), dbg_arg2
, 0, 0, 0);
971 kdbg_trace_string(parent_task
->bsd_info
,
972 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
974 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
975 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
976 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
979 DTRACE_PROC1(lwp__create
, thread_t
, *out_thread
);
981 return (KERN_SUCCESS
);
985 thread_create_internal2(
987 thread_t
*new_thread
,
990 kern_return_t result
;
993 if (task
== TASK_NULL
|| task
== kernel_task
)
994 return (KERN_INVALID_ARGUMENT
);
996 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
997 if (result
!= KERN_SUCCESS
)
1000 thread
->user_stop_count
= 1;
1001 thread_hold(thread
);
1002 if (task
->suspend_count
> 0)
1003 thread_hold(thread
);
1006 extmod_statistics_incr_thread_create(task
);
1009 lck_mtx_unlock(&tasks_threads_lock
);
1011 *new_thread
= thread
;
1013 return (KERN_SUCCESS
);
1016 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
1020 thread_t
*new_thread
);
1025 thread_t
*new_thread
)
1027 return thread_create_internal2(task
, new_thread
, FALSE
);
1031 thread_create_from_user(
1033 thread_t
*new_thread
)
1035 return thread_create_internal2(task
, new_thread
, TRUE
);
1038 static kern_return_t
1039 thread_create_running_internal2(
1040 register task_t task
,
1042 thread_state_t new_state
,
1043 mach_msg_type_number_t new_state_count
,
1044 thread_t
*new_thread
,
1045 boolean_t from_user
)
1047 register kern_return_t result
;
1050 if (task
== TASK_NULL
|| task
== kernel_task
)
1051 return (KERN_INVALID_ARGUMENT
);
1053 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
1054 if (result
!= KERN_SUCCESS
)
1057 result
= machine_thread_set_state(
1058 thread
, flavor
, new_state
, new_state_count
);
1059 if (result
!= KERN_SUCCESS
) {
1061 lck_mtx_unlock(&tasks_threads_lock
);
1063 thread_terminate(thread
);
1064 thread_deallocate(thread
);
1068 thread_mtx_lock(thread
);
1069 thread_start_internal(thread
);
1070 thread_mtx_unlock(thread
);
1073 extmod_statistics_incr_thread_create(task
);
1076 lck_mtx_unlock(&tasks_threads_lock
);
1078 *new_thread
= thread
;
1083 /* Prototype, see justification above */
1085 thread_create_running(
1086 register task_t task
,
1088 thread_state_t new_state
,
1089 mach_msg_type_number_t new_state_count
,
1090 thread_t
*new_thread
);
1093 thread_create_running(
1094 register task_t task
,
1096 thread_state_t new_state
,
1097 mach_msg_type_number_t new_state_count
,
1098 thread_t
*new_thread
)
1100 return thread_create_running_internal2(
1101 task
, flavor
, new_state
, new_state_count
,
1106 thread_create_running_from_user(
1107 register task_t task
,
1109 thread_state_t new_state
,
1110 mach_msg_type_number_t new_state_count
,
1111 thread_t
*new_thread
)
1113 return thread_create_running_internal2(
1114 task
, flavor
, new_state
, new_state_count
,
1119 thread_create_workq(
1121 thread_continue_t thread_return
,
1122 thread_t
*new_thread
)
1124 kern_return_t result
;
1127 if (task
== TASK_NULL
|| task
== kernel_task
)
1128 return (KERN_INVALID_ARGUMENT
);
1130 result
= thread_create_internal(task
, -1, thread_return
, TH_OPTION_NOCRED
| TH_OPTION_NOSUSP
, &thread
);
1131 if (result
!= KERN_SUCCESS
)
1134 thread
->user_stop_count
= 1;
1135 thread_hold(thread
);
1136 if (task
->suspend_count
> 0)
1137 thread_hold(thread
);
1140 lck_mtx_unlock(&tasks_threads_lock
);
1142 *new_thread
= thread
;
1144 return (KERN_SUCCESS
);
1148 * kernel_thread_create:
1150 * Create a thread in the kernel task
1151 * to execute in kernel context.
1154 kernel_thread_create(
1155 thread_continue_t continuation
,
1158 thread_t
*new_thread
)
1160 kern_return_t result
;
1162 task_t task
= kernel_task
;
1164 result
= thread_create_internal(task
, priority
, continuation
, TH_OPTION_NONE
, &thread
);
1165 if (result
!= KERN_SUCCESS
)
1169 lck_mtx_unlock(&tasks_threads_lock
);
1171 stack_alloc(thread
);
1172 assert(thread
->kernel_stack
!= 0);
1173 thread
->reserved_stack
= thread
->kernel_stack
;
1175 thread
->parameter
= parameter
;
1178 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread
, continuation
);
1179 *new_thread
= thread
;
1185 kernel_thread_start_priority(
1186 thread_continue_t continuation
,
1189 thread_t
*new_thread
)
1191 kern_return_t result
;
1194 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
1195 if (result
!= KERN_SUCCESS
)
1198 *new_thread
= thread
;
1200 thread_mtx_lock(thread
);
1201 thread_start_internal(thread
);
1202 thread_mtx_unlock(thread
);
1208 kernel_thread_start(
1209 thread_continue_t continuation
,
1211 thread_t
*new_thread
)
1213 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
1218 thread_info_internal(
1219 register thread_t thread
,
1220 thread_flavor_t flavor
,
1221 thread_info_t thread_info_out
, /* ptr to OUT array */
1222 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
1227 if (thread
== THREAD_NULL
)
1228 return (KERN_INVALID_ARGUMENT
);
1230 if (flavor
== THREAD_BASIC_INFO
) {
1231 register thread_basic_info_t basic_info
;
1233 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
1234 return (KERN_INVALID_ARGUMENT
);
1236 basic_info
= (thread_basic_info_t
) thread_info_out
;
1239 thread_lock(thread
);
1243 thread_read_times(thread
, &basic_info
->user_time
,
1244 &basic_info
->system_time
);
1247 * Update lazy-evaluated scheduler info because someone wants it.
1249 if (SCHED(can_update_priority
)(thread
))
1250 SCHED(update_priority
)(thread
);
1252 basic_info
->sleep_time
= 0;
1255 * To calculate cpu_usage, first correct for timer rate,
1256 * then for 5/8 ageing. The correction factor [3/5] is
1259 basic_info
->cpu_usage
= 0;
1260 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
1261 if (sched_tick_interval
) {
1262 basic_info
->cpu_usage
= (integer_t
)(((uint64_t)thread
->cpu_usage
1263 * TH_USAGE_SCALE
) / sched_tick_interval
);
1264 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
1268 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
1269 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
1271 basic_info
->policy
= ((thread
->sched_mode
== TH_MODE_TIMESHARE
)?
1272 POLICY_TIMESHARE
: POLICY_RR
);
1275 if (thread
->options
& TH_OPT_IDLE_THREAD
)
1276 flags
|= TH_FLAGS_IDLE
;
1278 if (!thread
->kernel_stack
)
1279 flags
|= TH_FLAGS_SWAPPED
;
1282 if (thread
->state
& TH_TERMINATE
)
1283 state
= TH_STATE_HALTED
;
1285 if (thread
->state
& TH_RUN
)
1286 state
= TH_STATE_RUNNING
;
1288 if (thread
->state
& TH_UNINT
)
1289 state
= TH_STATE_UNINTERRUPTIBLE
;
1291 if (thread
->state
& TH_SUSP
)
1292 state
= TH_STATE_STOPPED
;
1294 if (thread
->state
& TH_WAIT
)
1295 state
= TH_STATE_WAITING
;
1297 basic_info
->run_state
= state
;
1298 basic_info
->flags
= flags
;
1300 basic_info
->suspend_count
= thread
->user_stop_count
;
1302 thread_unlock(thread
);
1305 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
1307 return (KERN_SUCCESS
);
1310 if (flavor
== THREAD_IDENTIFIER_INFO
) {
1311 register thread_identifier_info_t identifier_info
;
1313 if (*thread_info_count
< THREAD_IDENTIFIER_INFO_COUNT
)
1314 return (KERN_INVALID_ARGUMENT
);
1316 identifier_info
= (thread_identifier_info_t
) thread_info_out
;
1319 thread_lock(thread
);
1321 identifier_info
->thread_id
= thread
->thread_id
;
1322 identifier_info
->thread_handle
= thread
->machine
.cthread_self
;
1323 if(thread
->task
->bsd_info
) {
1324 identifier_info
->dispatch_qaddr
= identifier_info
->thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1326 thread_unlock(thread
);
1328 return KERN_INVALID_ARGUMENT
;
1331 thread_unlock(thread
);
1333 return KERN_SUCCESS
;
1336 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
1337 policy_timeshare_info_t ts_info
;
1339 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
1340 return (KERN_INVALID_ARGUMENT
);
1342 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
1345 thread_lock(thread
);
1347 if (thread
->sched_mode
!= TH_MODE_TIMESHARE
) {
1348 thread_unlock(thread
);
1351 return (KERN_INVALID_POLICY
);
1354 ts_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1355 if (ts_info
->depressed
) {
1356 ts_info
->base_priority
= DEPRESSPRI
;
1357 ts_info
->depress_priority
= thread
->priority
;
1360 ts_info
->base_priority
= thread
->priority
;
1361 ts_info
->depress_priority
= -1;
1364 ts_info
->cur_priority
= thread
->sched_pri
;
1365 ts_info
->max_priority
= thread
->max_priority
;
1367 thread_unlock(thread
);
1370 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
1372 return (KERN_SUCCESS
);
1375 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
1376 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
1377 return (KERN_INVALID_ARGUMENT
);
1379 return (KERN_INVALID_POLICY
);
1382 if (flavor
== THREAD_SCHED_RR_INFO
) {
1383 policy_rr_info_t rr_info
;
1384 uint32_t quantum_time
;
1385 uint64_t quantum_ns
;
1387 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
1388 return (KERN_INVALID_ARGUMENT
);
1390 rr_info
= (policy_rr_info_t
) thread_info_out
;
1393 thread_lock(thread
);
1395 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
1396 thread_unlock(thread
);
1399 return (KERN_INVALID_POLICY
);
1402 rr_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1403 if (rr_info
->depressed
) {
1404 rr_info
->base_priority
= DEPRESSPRI
;
1405 rr_info
->depress_priority
= thread
->priority
;
1408 rr_info
->base_priority
= thread
->priority
;
1409 rr_info
->depress_priority
= -1;
1412 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
1413 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
1415 rr_info
->max_priority
= thread
->max_priority
;
1416 rr_info
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
1418 thread_unlock(thread
);
1421 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1423 return (KERN_SUCCESS
);
1426 return (KERN_INVALID_ARGUMENT
);
1432 time_value_t
*user_time
,
1433 time_value_t
*system_time
)
1437 uint64_t tval_user
, tval_system
;
1439 tval_user
= timer_grab(&thread
->user_timer
);
1440 tval_system
= timer_grab(&thread
->system_timer
);
1442 if (thread
->precise_user_kernel_time
) {
1443 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1444 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1445 user_time
->microseconds
= usecs
;
1447 absolutetime_to_microtime(tval_system
, &secs
, &usecs
);
1448 system_time
->seconds
= (typeof(system_time
->seconds
))secs
;
1449 system_time
->microseconds
= usecs
;
1451 /* system_timer may represent either sys or user */
1452 tval_user
+= tval_system
;
1453 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1454 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1455 user_time
->microseconds
= usecs
;
1457 system_time
->seconds
= 0;
1458 system_time
->microseconds
= 0;
1462 uint64_t thread_get_runtime_self(void)
1464 boolean_t interrupt_state
;
1466 thread_t thread
= NULL
;
1467 processor_t processor
= NULL
;
1469 thread
= current_thread();
1471 /* Not interrupt safe, as the scheduler may otherwise update timer values underneath us */
1472 interrupt_state
= ml_set_interrupts_enabled(FALSE
);
1473 processor
= current_processor();
1474 timer_switch(PROCESSOR_DATA(processor
, thread_timer
), mach_absolute_time(), PROCESSOR_DATA(processor
, thread_timer
));
1475 runtime
= (timer_grab(&thread
->user_timer
) + timer_grab(&thread
->system_timer
));
1476 ml_set_interrupts_enabled(interrupt_state
);
1483 __unused thread_t thread
,
1484 __unused processor_set_t new_pset
)
1486 return (KERN_FAILURE
);
1490 * thread_assign_default:
1492 * Special version of thread_assign for assigning threads to default
1496 thread_assign_default(
1499 return (thread_assign(thread
, &pset0
));
1503 * thread_get_assignment
1505 * Return current assignment for this thread.
1508 thread_get_assignment(
1510 processor_set_t
*pset
)
1513 return (KERN_INVALID_ARGUMENT
);
1517 return (KERN_SUCCESS
);
1521 * thread_wire_internal:
1523 * Specify that the target thread must always be able
1524 * to run and to allocate memory.
1527 thread_wire_internal(
1528 host_priv_t host_priv
,
1531 boolean_t
*prev_state
)
1533 if (host_priv
== NULL
|| thread
!= current_thread())
1534 return (KERN_INVALID_ARGUMENT
);
1536 assert(host_priv
== &realhost
);
1539 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1542 if (!(thread
->options
& TH_OPT_VMPRIV
))
1543 vm_page_free_reserve(1); /* XXX */
1544 thread
->options
|= TH_OPT_VMPRIV
;
1547 if (thread
->options
& TH_OPT_VMPRIV
)
1548 vm_page_free_reserve(-1); /* XXX */
1549 thread
->options
&= ~TH_OPT_VMPRIV
;
1552 return (KERN_SUCCESS
);
1559 * User-api wrapper for thread_wire_internal()
1563 host_priv_t host_priv
,
1567 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
1572 set_vm_privilege(boolean_t privileged
)
1574 boolean_t was_vmpriv
;
1576 if (current_thread()->options
& TH_OPT_VMPRIV
)
1581 if (privileged
!= FALSE
)
1582 current_thread()->options
|= TH_OPT_VMPRIV
;
1584 current_thread()->options
&= ~TH_OPT_VMPRIV
;
1586 return (was_vmpriv
);
1591 * XXX assuming current thread only, for now...
1594 thread_guard_violation(thread_t thread
, unsigned type
)
1596 assert(thread
== current_thread());
1598 spl_t s
= splsched();
1600 * Use the saved state area of the thread structure
1601 * to store all info required to handle the AST when
1602 * returning to userspace
1604 thread
->guard_exc_info
.type
= type
;
1605 thread_ast_set(thread
, AST_GUARD
);
1606 ast_propagate(thread
->ast
);
1614 * Handle AST_GUARD for a thread. This routine looks at the
1615 * state saved in the thread structure to determine the cause
1616 * of this exception. Based on this value, it invokes the
1617 * appropriate routine which determines other exception related
1618 * info and raises the exception.
1621 guard_ast(thread_t thread
)
1623 if (thread
->guard_exc_info
.type
== GUARD_TYPE_MACH_PORT
)
1624 mach_port_guard_ast(thread
);
1626 fd_guard_ast(thread
);
1630 thread_cputime_callback(int warning
, __unused
const void *arg0
, __unused
const void *arg1
)
1632 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
1633 #if CONFIG_TELEMETRY
1635 * This thread is in danger of violating the CPU usage monitor. Enable telemetry
1636 * on the entire task so there are micro-stackshots available if and when
1637 * EXC_RESOURCE is triggered. We could have chosen to enable micro-stackshots
1638 * for this thread only; but now that this task is suspect, knowing what all of
1639 * its threads are up to will be useful.
1641 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 1);
1646 #if CONFIG_TELEMETRY
1648 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
1649 * exceeded the limit, turn telemetry off for the task.
1651 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 0);
1655 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE();
1659 void __attribute__((noinline
))
1660 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void)
1663 task_t task
= current_task();
1664 thread_t thread
= current_thread();
1665 uint64_t tid
= thread
->thread_id
;
1666 char *procname
= (char *) "unknown";
1667 time_value_t thread_total_time
= {0, 0};
1668 time_value_t thread_system_time
;
1669 time_value_t thread_user_time
;
1672 uint32_t limit_percent
;
1673 uint32_t usage_percent
;
1674 uint32_t interval_sec
;
1675 uint64_t interval_ns
;
1676 uint64_t balance_ns
;
1677 boolean_t fatal
= FALSE
;
1679 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1680 struct ledger_entry_info lei
;
1682 assert(thread
->t_threadledger
!= LEDGER_NULL
);
1685 * Now that a thread has tripped the monitor, disable it for the entire task.
1688 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) {
1690 * The CPU usage monitor has been disabled on our task, so some other
1691 * thread must have gotten here first. We only send one exception per
1692 * task lifetime, so there's nothing left for us to do here.
1697 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_CPUMON
) {
1700 task_disable_cpumon(task
);
1704 pid
= proc_selfpid();
1705 if (task
->bsd_info
!= NULL
)
1706 procname
= proc_name_address(task
->bsd_info
);
1709 thread_get_cpulimit(&action
, &percentage
, &interval_ns
);
1711 interval_sec
= (uint32_t)(interval_ns
/ NSEC_PER_SEC
);
1713 thread_read_times(thread
, &thread_user_time
, &thread_system_time
);
1714 time_value_add(&thread_total_time
, &thread_user_time
);
1715 time_value_add(&thread_total_time
, &thread_system_time
);
1717 ledger_get_entry_info(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &lei
);
1719 absolutetime_to_nanoseconds(lei
.lei_balance
, &balance_ns
);
1720 usage_percent
= (uint32_t) ((balance_ns
* 100ULL) / lei
.lei_last_refill
);
1722 /* Show refill period in the same units as balance, limit, etc */
1723 nanoseconds_to_absolutetime(lei
.lei_refill_period
, &lei
.lei_refill_period
);
1725 limit_percent
= (uint32_t) ((lei
.lei_limit
* 100ULL) / lei
.lei_refill_period
);
1727 /* TODO: show task total runtime as well? see TASK_ABSOLUTETIME_INFO */
1729 if (disable_exc_resource
) {
1730 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1731 "supressed by a boot-arg\n", procname
, pid
, tid
);
1736 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1737 "supressed due to audio playback\n", procname
, pid
, tid
);
1740 printf("process %s[%d] thread %llu caught burning CPU! "
1741 "It used more than %d%% CPU (Actual recent usage: %d%%) over %d seconds. "
1742 "thread lifetime cpu usage %d.%06d seconds, (%d.%06d user, %d.%06d system) "
1743 "ledger info: balance: %lld credit: %lld debit: %lld limit: %llu (%d%%) "
1744 "period: %llu time since last refill (ns): %llu %s\n",
1746 percentage
, usage_percent
, interval_sec
,
1747 thread_total_time
.seconds
, thread_total_time
.microseconds
,
1748 thread_user_time
.seconds
, thread_user_time
.microseconds
,
1749 thread_system_time
.seconds
, thread_system_time
.microseconds
,
1751 lei
.lei_credit
, lei
.lei_debit
,
1752 lei
.lei_limit
, limit_percent
,
1753 lei
.lei_refill_period
, lei
.lei_last_refill
,
1754 (fatal
? "[fatal violation]" : ""));
1757 code
[0] = code
[1] = 0;
1758 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_CPU
);
1760 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_CPU_MONITOR_FATAL
);
1762 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_CPU_MONITOR
);
1764 EXC_RESOURCE_CPUMONITOR_ENCODE_INTERVAL(code
[0], interval_sec
);
1765 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[0], limit_percent
);
1766 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[1], usage_percent
);
1767 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
1771 jetsam_on_ledger_cpulimit_exceeded();
1773 task_terminate_internal(task
);
1778 #define UPDATE_IO_STATS(info, size) \
1781 info.size += size; \
1784 #define UPDATE_IO_STATS_ATOMIC(info, size) \
1786 OSIncrementAtomic64((SInt64 *)&(info.count)); \
1787 OSAddAtomic64(size, (SInt64 *)&(info.size)); \
1790 void thread_update_io_stats(thread_t thread
, int size
, int io_flags
)
1794 if (thread
->thread_io_stats
== NULL
|| thread
->task
->task_io_stats
== NULL
)
1797 if (io_flags
& DKIO_READ
) {
1798 UPDATE_IO_STATS(thread
->thread_io_stats
->disk_reads
, size
);
1799 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->disk_reads
, size
);
1802 if (io_flags
& DKIO_META
) {
1803 UPDATE_IO_STATS(thread
->thread_io_stats
->metadata
, size
);
1804 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->metadata
, size
);
1807 if (io_flags
& DKIO_PAGING
) {
1808 UPDATE_IO_STATS(thread
->thread_io_stats
->paging
, size
);
1809 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->paging
, size
);
1812 io_tier
= ((io_flags
& DKIO_TIER_MASK
) >> DKIO_TIER_SHIFT
);
1813 assert (io_tier
< IO_NUM_PRIORITIES
);
1815 UPDATE_IO_STATS(thread
->thread_io_stats
->io_priority
[io_tier
], size
);
1816 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->io_priority
[io_tier
], size
);
1818 /* Update Total I/O Counts */
1819 UPDATE_IO_STATS(thread
->thread_io_stats
->total_io
, size
);
1820 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->total_io
, size
);
1825 init_thread_ledgers(void) {
1826 ledger_template_t t
;
1829 assert(thread_ledger_template
== NULL
);
1831 if ((t
= ledger_template_create("Per-thread ledger")) == NULL
)
1832 panic("couldn't create thread ledger template");
1834 if ((idx
= ledger_entry_add(t
, "cpu_time", "sched", "ns")) < 0) {
1835 panic("couldn't create cpu_time entry for thread ledger template");
1838 if (ledger_set_callback(t
, idx
, thread_cputime_callback
, NULL
, NULL
) < 0) {
1839 panic("couldn't set thread ledger callback for cpu_time entry");
1842 thread_ledgers
.cpu_time
= idx
;
1844 thread_ledger_template
= t
;
1848 * Returns currently applied CPU usage limit, or 0/0 if none is applied.
1851 thread_get_cpulimit(int *action
, uint8_t *percentage
, uint64_t *interval_ns
)
1853 int64_t abstime
= 0;
1854 uint64_t limittime
= 0;
1855 thread_t thread
= current_thread();
1861 if (thread
->t_threadledger
== LEDGER_NULL
) {
1863 * This thread has no per-thread ledger, so it can't possibly
1864 * have a CPU limit applied.
1866 return (KERN_SUCCESS
);
1869 ledger_get_period(thread
->t_threadledger
, thread_ledgers
.cpu_time
, interval_ns
);
1870 ledger_get_limit(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &abstime
);
1872 if ((abstime
== LEDGER_LIMIT_INFINITY
) || (*interval_ns
== 0)) {
1874 * This thread's CPU time ledger has no period or limit; so it
1875 * doesn't have a CPU limit applied.
1877 return (KERN_SUCCESS
);
1881 * This calculation is the converse to the one in thread_set_cpulimit().
1883 absolutetime_to_nanoseconds(abstime
, &limittime
);
1884 *percentage
= (limittime
* 100ULL) / *interval_ns
;
1885 assert(*percentage
<= 100);
1887 if (thread
->options
& TH_OPT_PROC_CPULIMIT
) {
1888 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
1890 *action
= THREAD_CPULIMIT_BLOCK
;
1891 } else if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
1892 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
1894 *action
= THREAD_CPULIMIT_EXCEPTION
;
1896 *action
= THREAD_CPULIMIT_DISABLE
;
1899 return (KERN_SUCCESS
);
1903 * Set CPU usage limit on a thread.
1905 * Calling with percentage of 0 will unset the limit for this thread.
1908 thread_set_cpulimit(int action
, uint8_t percentage
, uint64_t interval_ns
)
1910 thread_t thread
= current_thread();
1912 uint64_t limittime
= 0;
1913 uint64_t abstime
= 0;
1915 assert(percentage
<= 100);
1917 if (action
== THREAD_CPULIMIT_DISABLE
) {
1919 * Remove CPU limit, if any exists.
1921 if (thread
->t_threadledger
!= LEDGER_NULL
) {
1922 l
= thread
->t_threadledger
;
1923 ledger_set_limit(l
, thread_ledgers
.cpu_time
, LEDGER_LIMIT_INFINITY
, 0);
1924 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_IGNORE
);
1925 thread
->options
&= ~(TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
);
1931 if (interval_ns
< MINIMUM_CPULIMIT_INTERVAL_MS
* NSEC_PER_MSEC
) {
1932 return (KERN_INVALID_ARGUMENT
);
1935 l
= thread
->t_threadledger
;
1936 if (l
== LEDGER_NULL
) {
1938 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
1940 if ((l
= ledger_instantiate(thread_ledger_template
, LEDGER_CREATE_INACTIVE_ENTRIES
)) == LEDGER_NULL
)
1941 return (KERN_RESOURCE_SHORTAGE
);
1944 * We are the first to create this thread's ledger, so only activate our entry.
1946 ledger_entry_setactive(l
, thread_ledgers
.cpu_time
);
1947 thread
->t_threadledger
= l
;
1951 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
1952 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
1954 limittime
= (interval_ns
* percentage
) / 100;
1955 nanoseconds_to_absolutetime(limittime
, &abstime
);
1956 ledger_set_limit(l
, thread_ledgers
.cpu_time
, abstime
, cpumon_ustackshots_trigger_pct
);
1958 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
1960 ledger_set_period(l
, thread_ledgers
.cpu_time
, interval_ns
);
1962 if (action
== THREAD_CPULIMIT_EXCEPTION
) {
1964 * We don't support programming the CPU usage monitor on a task if any of its
1965 * threads have a per-thread blocking CPU limit configured.
1967 if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
1968 panic("CPU usage monitor activated, but blocking thread limit exists");
1972 * Make a note that this thread's CPU limit is being used for the task-wide CPU
1973 * usage monitor. We don't have to arm the callback which will trigger the
1974 * exception, because that was done for us in ledger_instantiate (because the
1975 * ledger template used has a default callback).
1977 thread
->options
|= TH_OPT_PROC_CPULIMIT
;
1980 * We deliberately override any CPU limit imposed by a task-wide limit (eg
1981 * CPU usage monitor).
1983 thread
->options
&= ~TH_OPT_PROC_CPULIMIT
;
1985 thread
->options
|= TH_OPT_PRVT_CPULIMIT
;
1986 /* The per-thread ledger template by default has a callback for CPU time */
1987 ledger_disable_callback(l
, thread_ledgers
.cpu_time
);
1988 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
1997 __unused thread_t thread
)
2007 thread
->sched_call
= (call
!= NULL
)? call
: sched_call_null
;
2011 thread_static_param(
2015 thread_mtx_lock(thread
);
2016 thread
->static_param
= state
;
2017 thread_mtx_unlock(thread
);
2024 return (thread
!= THREAD_NULL
? thread
->thread_id
: 0);
2027 uint16_t thread_set_tag(thread_t th
, uint16_t tag
) {
2028 return thread_set_tag_internal(th
, tag
);
2030 uint16_t thread_get_tag(thread_t th
) {
2031 return thread_get_tag_internal(th
);
2035 thread_dispatchqaddr(
2038 uint64_t dispatchqueue_addr
= 0;
2039 uint64_t thread_handle
= 0;
2041 if (thread
!= THREAD_NULL
) {
2042 thread_handle
= thread
->machine
.cthread_self
;
2044 if (thread
->task
->bsd_info
)
2045 dispatchqueue_addr
= thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
2048 return (dispatchqueue_addr
);
2052 * Export routines to other components for things that are done as macros
2053 * within the osfmk component.
2056 #undef thread_reference
2057 void thread_reference(thread_t thread
);
2062 if (thread
!= THREAD_NULL
)
2063 thread_reference_internal(thread
);
2066 #undef thread_should_halt
2072 return (thread_should_halt_fast(th
));
2076 * thread_set_voucher_name - reset the voucher port name bound to this thread
2078 * Conditions: nothing locked
2080 * If we already converted the previous name to a cached voucher
2081 * reference, then we discard that reference here. The next lookup
2082 * will cache it again.
2086 thread_set_voucher_name(mach_port_name_t voucher_name
)
2088 thread_t thread
= current_thread();
2089 ipc_voucher_t new_voucher
= IPC_VOUCHER_NULL
;
2090 ipc_voucher_t voucher
;
2092 ledger_t bankledger
= NULL
;
2095 if (MACH_PORT_DEAD
== voucher_name
)
2096 return KERN_INVALID_RIGHT
;
2099 * agressively convert to voucher reference
2101 if (MACH_PORT_VALID(voucher_name
)) {
2102 new_voucher
= convert_port_name_to_voucher(voucher_name
);
2103 if (IPC_VOUCHER_NULL
== new_voucher
)
2104 return KERN_INVALID_ARGUMENT
;
2107 bankledger
= bank_get_voucher_ledger(new_voucher
);
2110 thread_mtx_lock(thread
);
2111 voucher
= thread
->ith_voucher
;
2112 thread
->ith_voucher_name
= voucher_name
;
2113 thread
->ith_voucher
= new_voucher
;
2115 bank_swap_thread_bank_ledger(thread
, bankledger
);
2117 thread_mtx_unlock(thread
);
2119 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2120 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2121 (uintptr_t)thread_tid(thread
),
2122 (uintptr_t)voucher_name
,
2123 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher
),
2126 if (IPC_VOUCHER_NULL
!= voucher
)
2127 ipc_voucher_release(voucher
);
2129 return KERN_SUCCESS
;
2133 * thread_get_mach_voucher - return a voucher reference for the specified thread voucher
2135 * Conditions: nothing locked
2137 * A reference to the voucher may be lazily pending, if someone set the voucher name
2138 * but nobody has done a lookup yet. In that case, we'll have to do the equivalent
2141 * NOTE: At the moment, there is no distinction between the current and effective
2142 * vouchers because we only set them at the thread level currently.
2145 thread_get_mach_voucher(
2146 thread_act_t thread
,
2147 mach_voucher_selector_t __unused which
,
2148 ipc_voucher_t
*voucherp
)
2150 ipc_voucher_t voucher
;
2151 mach_port_name_t voucher_name
;
2153 if (THREAD_NULL
== thread
)
2154 return KERN_INVALID_ARGUMENT
;
2156 thread_mtx_lock(thread
);
2157 voucher
= thread
->ith_voucher
;
2159 /* if already cached, just return a ref */
2160 if (IPC_VOUCHER_NULL
!= voucher
) {
2161 ipc_voucher_reference(voucher
);
2162 thread_mtx_unlock(thread
);
2163 *voucherp
= voucher
;
2164 return KERN_SUCCESS
;
2167 voucher_name
= thread
->ith_voucher_name
;
2169 /* convert the name to a port, then voucher reference */
2170 if (MACH_PORT_VALID(voucher_name
)) {
2174 ipc_object_copyin(thread
->task
->itk_space
, voucher_name
,
2175 MACH_MSG_TYPE_COPY_SEND
, (ipc_object_t
*)&port
)) {
2176 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2177 thread_mtx_unlock(thread
);
2178 *voucherp
= IPC_VOUCHER_NULL
;
2179 return KERN_SUCCESS
;
2182 /* convert to a voucher ref to return, and cache a ref on thread */
2183 voucher
= convert_port_to_voucher(port
);
2184 ipc_voucher_reference(voucher
);
2185 thread
->ith_voucher
= voucher
;
2186 thread_mtx_unlock(thread
);
2188 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2189 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2190 (uintptr_t)thread_tid(thread
),
2192 VM_KERNEL_ADDRPERM((uintptr_t)voucher
),
2196 ipc_port_release_send(port
);
2198 thread_mtx_unlock(thread
);
2200 *voucherp
= voucher
;
2201 return KERN_SUCCESS
;
2205 * thread_set_mach_voucher - set a voucher reference for the specified thread voucher
2207 * Conditions: callers holds a reference on the voucher.
2210 * We grab another reference to the voucher and bind it to the thread. Any lazy
2211 * binding is erased. The old voucher reference associated with the thread is
2215 thread_set_mach_voucher(
2217 ipc_voucher_t voucher
)
2219 ipc_voucher_t old_voucher
;
2221 ledger_t bankledger
= NULL
;
2224 if (THREAD_NULL
== thread
)
2225 return KERN_INVALID_ARGUMENT
;
2227 if (thread
!= current_thread() || thread
->started
)
2228 return KERN_INVALID_ARGUMENT
;
2231 ipc_voucher_reference(voucher
);
2233 bankledger
= bank_get_voucher_ledger(voucher
);
2235 thread_mtx_lock(thread
);
2236 old_voucher
= thread
->ith_voucher
;
2237 thread
->ith_voucher
= voucher
;
2238 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2240 bank_swap_thread_bank_ledger(thread
, bankledger
);
2242 thread_mtx_unlock(thread
);
2244 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2245 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2246 (uintptr_t)thread_tid(thread
),
2247 (uintptr_t)MACH_PORT_NULL
,
2248 VM_KERNEL_ADDRPERM((uintptr_t)voucher
),
2251 ipc_voucher_release(old_voucher
);
2253 return KERN_SUCCESS
;
2257 * thread_swap_mach_voucher - swap a voucher reference for the specified thread voucher
2259 * Conditions: callers holds a reference on the new and presumed old voucher(s).
2262 * If the old voucher is still the same as passed in, replace it with new voucher
2263 * and discard the old (and the reference passed in). Otherwise, discard the new
2264 * and return an updated old voucher.
2267 thread_swap_mach_voucher(
2269 ipc_voucher_t new_voucher
,
2270 ipc_voucher_t
*in_out_old_voucher
)
2272 mach_port_name_t old_voucher_name
;
2273 ipc_voucher_t old_voucher
;
2275 ledger_t bankledger
= NULL
;
2278 if (THREAD_NULL
== thread
)
2279 return KERN_INVALID_TASK
;
2281 if (thread
!= current_thread() || thread
->started
)
2282 return KERN_INVALID_ARGUMENT
;
2285 bankledger
= bank_get_voucher_ledger(new_voucher
);
2288 thread_mtx_lock(thread
);
2290 old_voucher
= thread
->ith_voucher
;
2292 if (IPC_VOUCHER_NULL
== old_voucher
) {
2293 old_voucher_name
= thread
->ith_voucher_name
;
2295 /* perform lazy binding if needed */
2296 if (MACH_PORT_VALID(old_voucher_name
)) {
2297 old_voucher
= convert_port_name_to_voucher(old_voucher_name
);
2298 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2299 thread
->ith_voucher
= old_voucher
;
2301 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2302 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2303 (uintptr_t)thread_tid(thread
),
2304 (uintptr_t)old_voucher_name
,
2305 VM_KERNEL_ADDRPERM((uintptr_t)old_voucher
),
2311 /* swap in new voucher, if old voucher matches the one supplied */
2312 if (old_voucher
== *in_out_old_voucher
) {
2313 ipc_voucher_reference(new_voucher
);
2314 thread
->ith_voucher
= new_voucher
;
2315 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2317 bank_swap_thread_bank_ledger(thread
, bankledger
);
2319 thread_mtx_unlock(thread
);
2321 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2322 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2323 (uintptr_t)thread_tid(thread
),
2324 (uintptr_t)MACH_PORT_NULL
,
2325 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher
),
2328 ipc_voucher_release(old_voucher
);
2330 *in_out_old_voucher
= IPC_VOUCHER_NULL
;
2331 return KERN_SUCCESS
;
2334 /* Otherwise, just return old voucher reference */
2335 ipc_voucher_reference(old_voucher
);
2336 thread_mtx_unlock(thread
);
2337 *in_out_old_voucher
= old_voucher
;
2338 return KERN_SUCCESS
;
2342 * thread_get_current_voucher_origin_pid - get the pid of the originator of the current voucher.
2345 thread_get_current_voucher_origin_pid(
2350 thread_t thread
= current_thread();
2352 buf_size
= sizeof(*pid
);
2353 kr
= mach_voucher_attr_command(thread
->ith_voucher
,
2354 MACH_VOUCHER_ATTR_KEY_BANK
,
2355 BANK_ORIGINATOR_PID
,
2358 (mach_voucher_attr_content_t
)pid
,
2365 uint32_t dtrace_get_thread_predcache(thread_t thread
)
2367 if (thread
!= THREAD_NULL
)
2368 return thread
->t_dtrace_predcache
;
2373 int64_t dtrace_get_thread_vtime(thread_t thread
)
2375 if (thread
!= THREAD_NULL
)
2376 return thread
->t_dtrace_vtime
;
2381 int64_t dtrace_get_thread_tracing(thread_t thread
)
2383 if (thread
!= THREAD_NULL
)
2384 return thread
->t_dtrace_tracing
;
2389 boolean_t
dtrace_get_thread_reentering(thread_t thread
)
2391 if (thread
!= THREAD_NULL
)
2392 return (thread
->options
& TH_OPT_DTRACE
) ? TRUE
: FALSE
;
2397 vm_offset_t
dtrace_get_kernel_stack(thread_t thread
)
2399 if (thread
!= THREAD_NULL
)
2400 return thread
->kernel_stack
;
2405 int64_t dtrace_calc_thread_recent_vtime(thread_t thread
)
2407 if (thread
!= THREAD_NULL
) {
2408 processor_t processor
= current_processor();
2409 uint64_t abstime
= mach_absolute_time();
2412 timer
= PROCESSOR_DATA(processor
, thread_timer
);
2414 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
)) +
2415 (abstime
- timer
->tstamp
); /* XXX need interrupts off to prevent missed time? */
2420 void dtrace_set_thread_predcache(thread_t thread
, uint32_t predcache
)
2422 if (thread
!= THREAD_NULL
)
2423 thread
->t_dtrace_predcache
= predcache
;
2426 void dtrace_set_thread_vtime(thread_t thread
, int64_t vtime
)
2428 if (thread
!= THREAD_NULL
)
2429 thread
->t_dtrace_vtime
= vtime
;
2432 void dtrace_set_thread_tracing(thread_t thread
, int64_t accum
)
2434 if (thread
!= THREAD_NULL
)
2435 thread
->t_dtrace_tracing
= accum
;
2438 void dtrace_set_thread_reentering(thread_t thread
, boolean_t vbool
)
2440 if (thread
!= THREAD_NULL
) {
2442 thread
->options
|= TH_OPT_DTRACE
;
2444 thread
->options
&= (~TH_OPT_DTRACE
);
2448 vm_offset_t
dtrace_set_thread_recover(thread_t thread
, vm_offset_t recover
)
2450 vm_offset_t prev
= 0;
2452 if (thread
!= THREAD_NULL
) {
2453 prev
= thread
->recover
;
2454 thread
->recover
= recover
;
2459 void dtrace_thread_bootstrap(void)
2461 task_t task
= current_task();
2463 if (task
->thread_count
== 1) {
2464 thread_t thread
= current_thread();
2465 if (thread
->t_dtrace_flags
& TH_DTRACE_EXECSUCCESS
) {
2466 thread
->t_dtrace_flags
&= ~TH_DTRACE_EXECSUCCESS
;
2467 DTRACE_PROC(exec__success
);
2471 DTRACE_PROC(lwp__start
);
2476 dtrace_thread_didexec(thread_t thread
)
2478 thread
->t_dtrace_flags
|= TH_DTRACE_EXECSUCCESS
;
2480 #endif /* CONFIG_DTRACE */