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;
313 #if (DEVELOPMENT || DEBUG)
314 thread_template
.t_page_creation_throttled_hard
= 0;
315 thread_template
.t_page_creation_throttled_soft
= 0;
316 #endif /* DEVELOPMENT || DEBUG */
317 thread_template
.t_page_creation_throttled
= 0;
318 thread_template
.t_page_creation_count
= 0;
319 thread_template
.t_page_creation_time
= 0;
321 thread_template
.affinity_set
= NULL
;
323 thread_template
.syscalls_unix
= 0;
324 thread_template
.syscalls_mach
= 0;
326 thread_template
.t_ledger
= LEDGER_NULL
;
327 thread_template
.t_threadledger
= LEDGER_NULL
;
329 thread_template
.t_bankledger
= LEDGER_NULL
;
330 thread_template
.t_deduct_bank_ledger_time
= 0;
333 thread_template
.requested_policy
= default_task_requested_policy
;
334 thread_template
.effective_policy
= default_task_effective_policy
;
335 thread_template
.pended_policy
= default_task_pended_policy
;
337 bzero(&thread_template
.overrides
, sizeof(thread_template
.overrides
));
339 thread_template
.iotier_override
= THROTTLE_LEVEL_NONE
;
340 thread_template
.thread_io_stats
= NULL
;
341 thread_template
.thread_callout_interrupt_wakeups
= thread_template
.thread_callout_platform_idle_wakeups
= 0;
343 thread_template
.thread_timer_wakeups_bin_1
= thread_template
.thread_timer_wakeups_bin_2
= 0;
344 thread_template
.callout_woken_from_icontext
= thread_template
.callout_woken_from_platform_idle
= 0;
346 thread_template
.thread_tag
= 0;
348 thread_template
.ith_voucher_name
= MACH_PORT_NULL
;
349 thread_template
.ith_voucher
= IPC_VOUCHER_NULL
;
351 init_thread
= thread_template
;
352 machine_set_current_thread(&init_thread
);
355 extern boolean_t allow_qos_policy_set
;
361 sizeof(struct thread
),
362 thread_max
* sizeof(struct thread
),
363 THREAD_CHUNK
* sizeof(struct thread
),
366 thread_qos_override_zone
= zinit(
367 sizeof(struct thread_qos_override
),
368 4 * thread_max
* sizeof(struct thread_qos_override
),
370 "thread qos override");
371 zone_change(thread_qos_override_zone
, Z_EXPAND
, TRUE
);
372 zone_change(thread_qos_override_zone
, Z_COLLECT
, TRUE
);
373 zone_change(thread_qos_override_zone
, Z_CALLERACCT
, FALSE
);
374 zone_change(thread_qos_override_zone
, Z_NOENCRYPT
, TRUE
);
376 lck_grp_attr_setdefault(&thread_lck_grp_attr
);
377 lck_grp_init(&thread_lck_grp
, "thread", &thread_lck_grp_attr
);
378 lck_attr_setdefault(&thread_lck_attr
);
383 * Initialize any machine-dependent
384 * per-thread structures necessary.
386 machine_thread_init();
388 if (!PE_parse_boot_argn("cpumon_ustackshots_trigger_pct", &cpumon_ustackshots_trigger_pct
,
389 sizeof (cpumon_ustackshots_trigger_pct
))) {
390 cpumon_ustackshots_trigger_pct
= CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT
;
393 PE_parse_boot_argn("-qos-policy-allow", &allow_qos_policy_set
, sizeof(allow_qos_policy_set
));
395 init_thread_ledgers();
399 thread_terminate_continue(void)
401 panic("thread_terminate_continue");
406 * thread_terminate_self:
409 thread_terminate_self(void)
411 thread_t thread
= current_thread();
417 pal_thread_terminate_self(thread
);
419 DTRACE_PROC(lwp__exit
);
421 thread_mtx_lock(thread
);
423 ipc_thread_disable(thread
);
425 thread_mtx_unlock(thread
);
430 assert_thread_sched_count(thread
);
433 * Cancel priority depression, wait for concurrent expirations
434 * on other processors.
436 if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
437 thread
->sched_flags
&= ~TH_SFLAG_DEPRESSED_MASK
;
439 /* If our priority was low because of a depressed yield, restore it in case we block below */
440 set_sched_pri(thread
, thread
->priority
);
442 if (timer_call_cancel(&thread
->depress_timer
))
443 thread
->depress_timer_active
--;
446 while (thread
->depress_timer_active
> 0) {
447 thread_unlock(thread
);
456 thread_sched_call(thread
, NULL
);
458 thread_unlock(thread
);
462 thread_mtx_lock(thread
);
464 thread_policy_reset(thread
);
466 thread_mtx_unlock(thread
);
469 uthread_cleanup(task
, thread
->uthread
, task
->bsd_info
);
470 threadcnt
= hw_atomic_sub(&task
->active_thread_count
, 1);
473 * If we are the last thread to terminate and the task is
474 * associated with a BSD process, perform BSD process exit.
476 if (threadcnt
== 0 && task
->bsd_info
!= NULL
)
477 proc_exit(task
->bsd_info
);
479 uthread_cred_free(thread
->uthread
);
485 * Cancel wait timer, and wait for
486 * concurrent expirations.
488 if (thread
->wait_timer_is_set
) {
489 thread
->wait_timer_is_set
= FALSE
;
491 if (timer_call_cancel(&thread
->wait_timer
))
492 thread
->wait_timer_active
--;
495 while (thread
->wait_timer_active
> 0) {
496 thread_unlock(thread
);
506 * If there is a reserved stack, release it.
508 if (thread
->reserved_stack
!= 0) {
509 stack_free_reserved(thread
);
510 thread
->reserved_stack
= 0;
514 * Mark thread as terminating, and block.
516 thread
->state
|= TH_TERMINATE
;
517 thread_mark_wait_locked(thread
, THREAD_UNINT
);
518 assert(thread
->promotions
== 0);
519 assert(thread
->rwlock_count
== 0);
520 thread_unlock(thread
);
523 thread_block((thread_continue_t
)thread_terminate_continue
);
533 if (thread
== THREAD_NULL
)
536 if (thread_deallocate_internal(thread
) > 0)
539 if(!(thread
->state
& TH_TERMINATE2
))
540 panic("thread_deallocate: thread not properly terminated\n");
543 kpc_thread_destroy(thread
);
546 ipc_thread_terminate(thread
);
548 proc_thread_qos_deallocate(thread
);
554 void *ut
= thread
->uthread
;
556 thread
->uthread
= NULL
;
557 uthread_zone_free(ut
);
559 #endif /* MACH_BSD */
561 if (thread
->t_ledger
)
562 ledger_dereference(thread
->t_ledger
);
563 if (thread
->t_threadledger
)
564 ledger_dereference(thread
->t_threadledger
);
566 if (IPC_VOUCHER_NULL
!= thread
->ith_voucher
)
567 ipc_voucher_release(thread
->ith_voucher
);
569 if (thread
->thread_io_stats
)
570 kfree(thread
->thread_io_stats
, sizeof(struct io_stat_info
));
572 if (thread
->kernel_stack
!= 0)
575 lck_mtx_destroy(&thread
->mutex
, &thread_lck_grp
);
576 machine_thread_destroy(thread
);
578 task_deallocate(task
);
580 zfree(thread_zone
, thread
);
584 * thread_terminate_daemon:
586 * Perform final clean up for terminating threads.
589 thread_terminate_daemon(void)
591 thread_t self
, thread
;
594 self
= current_thread();
595 self
->options
|= TH_OPT_SYSTEM_CRITICAL
;
598 simple_lock(&thread_terminate_lock
);
600 while ((thread
= (thread_t
)dequeue_head(&thread_terminate_queue
)) != THREAD_NULL
) {
601 simple_unlock(&thread_terminate_lock
);
604 assert(thread
->SHARE_COUNT
== 0);
605 assert(thread
->BG_COUNT
== 0);
610 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
611 if (thread
->precise_user_kernel_time
) {
612 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
614 task
->total_user_time
+= timer_grab(&thread
->system_timer
);
617 task
->c_switch
+= thread
->c_switch
;
618 task
->p_switch
+= thread
->p_switch
;
619 task
->ps_switch
+= thread
->ps_switch
;
621 task
->syscalls_unix
+= thread
->syscalls_unix
;
622 task
->syscalls_mach
+= thread
->syscalls_mach
;
624 task
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
625 task
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
626 task
->task_gpu_ns
+= ml_gpu_stat(thread
);
628 thread_update_qos_cpu_time(thread
, FALSE
);
629 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
630 task
->thread_count
--;
633 * If the task is being halted, and there is only one thread
634 * left in the task after this one, then wakeup that thread.
636 if (task
->thread_count
== 1 && task
->halting
)
637 thread_wakeup((event_t
)&task
->halting
);
641 lck_mtx_lock(&tasks_threads_lock
);
642 queue_remove(&threads
, thread
, thread_t
, threads
);
644 lck_mtx_unlock(&tasks_threads_lock
);
646 thread_deallocate(thread
);
649 simple_lock(&thread_terminate_lock
);
652 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
653 simple_unlock(&thread_terminate_lock
);
656 self
->options
&= ~TH_OPT_SYSTEM_CRITICAL
;
657 thread_block((thread_continue_t
)thread_terminate_daemon
);
662 * thread_terminate_enqueue:
664 * Enqueue a terminating thread for final disposition.
666 * Called at splsched.
669 thread_terminate_enqueue(
672 KERNEL_DEBUG_CONSTANT(TRACE_DATA_THREAD_TERMINATE
| DBG_FUNC_NONE
, thread
->thread_id
, 0, 0, 0, 0);
674 simple_lock(&thread_terminate_lock
);
675 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
676 simple_unlock(&thread_terminate_lock
);
678 thread_wakeup((event_t
)&thread_terminate_queue
);
682 * thread_stack_daemon:
684 * Perform stack allocation as required due to
688 thread_stack_daemon(void)
694 simple_lock(&thread_stack_lock
);
696 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
697 simple_unlock(&thread_stack_lock
);
700 /* allocate stack with interrupts enabled so that we can call into VM */
705 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
706 thread_unlock(thread
);
708 simple_lock(&thread_stack_lock
);
711 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
712 simple_unlock(&thread_stack_lock
);
715 thread_block((thread_continue_t
)thread_stack_daemon
);
720 * thread_stack_enqueue:
722 * Enqueue a thread for stack allocation.
724 * Called at splsched.
727 thread_stack_enqueue(
730 simple_lock(&thread_stack_lock
);
731 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
732 simple_unlock(&thread_stack_lock
);
734 thread_wakeup((event_t
)&thread_stack_queue
);
738 thread_daemon_init(void)
740 kern_return_t result
;
741 thread_t thread
= NULL
;
743 simple_lock_init(&thread_terminate_lock
, 0);
744 queue_init(&thread_terminate_queue
);
746 result
= kernel_thread_start_priority((thread_continue_t
)thread_terminate_daemon
, NULL
, MINPRI_KERNEL
, &thread
);
747 if (result
!= KERN_SUCCESS
)
748 panic("thread_daemon_init: thread_terminate_daemon");
750 thread_deallocate(thread
);
752 simple_lock_init(&thread_stack_lock
, 0);
753 queue_init(&thread_stack_queue
);
755 result
= kernel_thread_start_priority((thread_continue_t
)thread_stack_daemon
, NULL
, BASEPRI_PREEMPT
, &thread
);
756 if (result
!= KERN_SUCCESS
)
757 panic("thread_daemon_init: thread_stack_daemon");
759 thread_deallocate(thread
);
763 * Create a new thread.
764 * Doesn't start the thread running.
766 * Task and tasks_threads_lock are returned locked on success.
769 thread_create_internal(
772 thread_continue_t continuation
,
774 #define TH_OPTION_NONE 0x00
775 #define TH_OPTION_NOCRED 0x01
776 #define TH_OPTION_NOSUSP 0x02
777 thread_t
*out_thread
)
780 static thread_t first_thread
;
783 * Allocate a thread and initialize static fields
785 if (first_thread
== THREAD_NULL
)
786 new_thread
= first_thread
= current_thread();
788 new_thread
= (thread_t
)zalloc(thread_zone
);
789 if (new_thread
== THREAD_NULL
)
790 return (KERN_RESOURCE_SHORTAGE
);
792 if (new_thread
!= first_thread
)
793 *new_thread
= thread_template
;
796 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
, (options
& TH_OPTION_NOCRED
) != 0);
797 if (new_thread
->uthread
== NULL
) {
798 zfree(thread_zone
, new_thread
);
799 return (KERN_RESOURCE_SHORTAGE
);
801 #endif /* MACH_BSD */
803 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
805 void *ut
= new_thread
->uthread
;
807 new_thread
->uthread
= NULL
;
808 /* cred free may not be necessary */
809 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
810 uthread_cred_free(ut
);
811 uthread_zone_free(ut
);
812 #endif /* MACH_BSD */
814 zfree(thread_zone
, new_thread
);
815 return (KERN_FAILURE
);
818 new_thread
->task
= parent_task
;
820 thread_lock_init(new_thread
);
821 wake_lock_init(new_thread
);
823 lck_mtx_init(&new_thread
->mutex
, &thread_lck_grp
, &thread_lck_attr
);
825 ipc_thread_init(new_thread
);
827 new_thread
->continuation
= continuation
;
829 /* Allocate I/O Statistics structure */
830 new_thread
->thread_io_stats
= (io_stat_info_t
)kalloc(sizeof(struct io_stat_info
));
831 assert(new_thread
->thread_io_stats
!= NULL
);
832 bzero(new_thread
->thread_io_stats
, sizeof(struct io_stat_info
));
835 /* Clear out the I/O Scheduling info for AppleFSCompression */
836 new_thread
->decmp_upl
= NULL
;
837 #endif /* CONFIG_IOSCHED */
839 lck_mtx_lock(&tasks_threads_lock
);
840 task_lock(parent_task
);
842 if ( !parent_task
->active
|| parent_task
->halting
||
843 ((options
& TH_OPTION_NOSUSP
) != 0 &&
844 parent_task
->suspend_count
> 0) ||
845 (parent_task
->thread_count
>= task_threadmax
&&
846 parent_task
!= kernel_task
) ) {
847 task_unlock(parent_task
);
848 lck_mtx_unlock(&tasks_threads_lock
);
852 void *ut
= new_thread
->uthread
;
854 new_thread
->uthread
= NULL
;
855 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
856 /* cred free may not be necessary */
857 uthread_cred_free(ut
);
858 uthread_zone_free(ut
);
860 #endif /* MACH_BSD */
861 ipc_thread_disable(new_thread
);
862 ipc_thread_terminate(new_thread
);
863 kfree(new_thread
->thread_io_stats
, sizeof(struct io_stat_info
));
864 lck_mtx_destroy(&new_thread
->mutex
, &thread_lck_grp
);
865 machine_thread_destroy(new_thread
);
866 zfree(thread_zone
, new_thread
);
867 return (KERN_FAILURE
);
870 /* New threads inherit any default state on the task */
871 machine_thread_inherit_taskwide(new_thread
, parent_task
);
873 task_reference_internal(parent_task
);
875 if (new_thread
->task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
877 * This task has a per-thread CPU limit; make sure this new thread
878 * gets its limit set too, before it gets out of the kernel.
880 set_astledger(new_thread
);
883 /* Instantiate a thread ledger. Do not fail thread creation if ledger creation fails. */
884 if ((new_thread
->t_threadledger
= ledger_instantiate(thread_ledger_template
,
885 LEDGER_CREATE_INACTIVE_ENTRIES
)) != LEDGER_NULL
) {
887 ledger_entry_setactive(new_thread
->t_threadledger
, thread_ledgers
.cpu_time
);
890 new_thread
->cpu_time_last_qos
= 0;
892 new_thread
->t_bankledger
= LEDGER_NULL
;
893 new_thread
->t_deduct_bank_ledger_time
= 0;
896 new_thread
->t_ledger
= new_thread
->task
->ledger
;
897 if (new_thread
->t_ledger
)
898 ledger_reference(new_thread
->t_ledger
);
900 #if defined(CONFIG_SCHED_MULTIQ)
901 /* Cache the task's sched_group */
902 new_thread
->sched_group
= parent_task
->sched_group
;
903 #endif /* defined(CONFIG_SCHED_MULTIQ) */
905 /* Cache the task's map */
906 new_thread
->map
= parent_task
->map
;
908 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
909 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
913 * If parent task has any reservations, they need to be propagated to this
916 new_thread
->t_chud
= (TASK_PMC_FLAG
== (parent_task
->t_chud
& TASK_PMC_FLAG
)) ?
917 THREAD_PMC_FLAG
: 0U;
920 kpc_thread_create(new_thread
);
923 /* Only need to update policies pushed from task to thread */
924 new_thread
->requested_policy
.bg_iotier
= parent_task
->effective_policy
.bg_iotier
;
925 new_thread
->requested_policy
.terminated
= parent_task
->effective_policy
.terminated
;
927 /* Set the thread's scheduling parameters */
928 new_thread
->sched_mode
= SCHED(initial_thread_sched_mode
)(parent_task
);
929 new_thread
->sched_flags
= 0;
930 new_thread
->max_priority
= parent_task
->max_priority
;
931 new_thread
->task_priority
= parent_task
->priority
;
932 new_thread
->priority
= (priority
< 0)? parent_task
->priority
: priority
;
933 if (new_thread
->priority
> new_thread
->max_priority
)
934 new_thread
->priority
= new_thread
->max_priority
;
935 new_thread
->importance
= new_thread
->priority
- new_thread
->task_priority
;
936 new_thread
->saved_importance
= new_thread
->importance
;
938 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
939 new_thread
->sched_stamp
= sched_tick
;
940 new_thread
->pri_shift
= sched_pri_shift
;
941 #endif /* defined(CONFIG_SCHED_TIMESHARE_CORE) */
943 if (parent_task
->max_priority
<= MAXPRI_THROTTLE
) {
944 sched_set_thread_throttled(new_thread
, TRUE
);
947 SCHED(compute_priority
)(new_thread
, FALSE
);
949 thread_policy_create(new_thread
);
951 /* Chain the thread onto the task's list */
952 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
953 parent_task
->thread_count
++;
955 /* So terminating threads don't need to take the task lock to decrement */
956 hw_atomic_add(&parent_task
->active_thread_count
, 1);
958 /* Protected by the tasks_threads_lock */
959 new_thread
->thread_id
= ++thread_unique_id
;
961 queue_enter(&threads
, new_thread
, thread_t
, threads
);
964 new_thread
->active
= TRUE
;
966 *out_thread
= new_thread
;
969 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
971 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
973 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
974 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
975 (vm_address_t
)(uintptr_t)thread_tid(new_thread
), dbg_arg2
, 0, 0, 0);
977 kdbg_trace_string(parent_task
->bsd_info
,
978 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
980 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
981 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
982 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
985 DTRACE_PROC1(lwp__create
, thread_t
, *out_thread
);
987 return (KERN_SUCCESS
);
991 thread_create_internal2(
993 thread_t
*new_thread
,
996 kern_return_t result
;
999 if (task
== TASK_NULL
|| task
== kernel_task
)
1000 return (KERN_INVALID_ARGUMENT
);
1002 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
1003 if (result
!= KERN_SUCCESS
)
1006 thread
->user_stop_count
= 1;
1007 thread_hold(thread
);
1008 if (task
->suspend_count
> 0)
1009 thread_hold(thread
);
1012 extmod_statistics_incr_thread_create(task
);
1015 lck_mtx_unlock(&tasks_threads_lock
);
1017 *new_thread
= thread
;
1019 return (KERN_SUCCESS
);
1022 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
1026 thread_t
*new_thread
);
1031 thread_t
*new_thread
)
1033 return thread_create_internal2(task
, new_thread
, FALSE
);
1037 thread_create_from_user(
1039 thread_t
*new_thread
)
1041 return thread_create_internal2(task
, new_thread
, TRUE
);
1044 static kern_return_t
1045 thread_create_running_internal2(
1046 register task_t task
,
1048 thread_state_t new_state
,
1049 mach_msg_type_number_t new_state_count
,
1050 thread_t
*new_thread
,
1051 boolean_t from_user
)
1053 register kern_return_t result
;
1056 if (task
== TASK_NULL
|| task
== kernel_task
)
1057 return (KERN_INVALID_ARGUMENT
);
1059 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
1060 if (result
!= KERN_SUCCESS
)
1063 result
= machine_thread_set_state(
1064 thread
, flavor
, new_state
, new_state_count
);
1065 if (result
!= KERN_SUCCESS
) {
1067 lck_mtx_unlock(&tasks_threads_lock
);
1069 thread_terminate(thread
);
1070 thread_deallocate(thread
);
1074 thread_mtx_lock(thread
);
1075 thread_start_internal(thread
);
1076 thread_mtx_unlock(thread
);
1079 extmod_statistics_incr_thread_create(task
);
1082 lck_mtx_unlock(&tasks_threads_lock
);
1084 *new_thread
= thread
;
1089 /* Prototype, see justification above */
1091 thread_create_running(
1092 register task_t task
,
1094 thread_state_t new_state
,
1095 mach_msg_type_number_t new_state_count
,
1096 thread_t
*new_thread
);
1099 thread_create_running(
1100 register task_t task
,
1102 thread_state_t new_state
,
1103 mach_msg_type_number_t new_state_count
,
1104 thread_t
*new_thread
)
1106 return thread_create_running_internal2(
1107 task
, flavor
, new_state
, new_state_count
,
1112 thread_create_running_from_user(
1113 register task_t task
,
1115 thread_state_t new_state
,
1116 mach_msg_type_number_t new_state_count
,
1117 thread_t
*new_thread
)
1119 return thread_create_running_internal2(
1120 task
, flavor
, new_state
, new_state_count
,
1125 thread_create_workq(
1127 thread_continue_t thread_return
,
1128 thread_t
*new_thread
)
1130 kern_return_t result
;
1133 if (task
== TASK_NULL
|| task
== kernel_task
)
1134 return (KERN_INVALID_ARGUMENT
);
1136 result
= thread_create_internal(task
, -1, thread_return
, TH_OPTION_NOCRED
| TH_OPTION_NOSUSP
, &thread
);
1137 if (result
!= KERN_SUCCESS
)
1140 thread
->user_stop_count
= 1;
1141 thread_hold(thread
);
1142 if (task
->suspend_count
> 0)
1143 thread_hold(thread
);
1146 lck_mtx_unlock(&tasks_threads_lock
);
1148 *new_thread
= thread
;
1150 return (KERN_SUCCESS
);
1154 * kernel_thread_create:
1156 * Create a thread in the kernel task
1157 * to execute in kernel context.
1160 kernel_thread_create(
1161 thread_continue_t continuation
,
1164 thread_t
*new_thread
)
1166 kern_return_t result
;
1168 task_t task
= kernel_task
;
1170 result
= thread_create_internal(task
, priority
, continuation
, TH_OPTION_NONE
, &thread
);
1171 if (result
!= KERN_SUCCESS
)
1175 lck_mtx_unlock(&tasks_threads_lock
);
1177 stack_alloc(thread
);
1178 assert(thread
->kernel_stack
!= 0);
1179 thread
->reserved_stack
= thread
->kernel_stack
;
1181 thread
->parameter
= parameter
;
1184 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread
, continuation
);
1185 *new_thread
= thread
;
1191 kernel_thread_start_priority(
1192 thread_continue_t continuation
,
1195 thread_t
*new_thread
)
1197 kern_return_t result
;
1200 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
1201 if (result
!= KERN_SUCCESS
)
1204 *new_thread
= thread
;
1206 thread_mtx_lock(thread
);
1207 thread_start_internal(thread
);
1208 thread_mtx_unlock(thread
);
1214 kernel_thread_start(
1215 thread_continue_t continuation
,
1217 thread_t
*new_thread
)
1219 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
1224 thread_info_internal(
1225 register thread_t thread
,
1226 thread_flavor_t flavor
,
1227 thread_info_t thread_info_out
, /* ptr to OUT array */
1228 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
1233 if (thread
== THREAD_NULL
)
1234 return (KERN_INVALID_ARGUMENT
);
1236 if (flavor
== THREAD_BASIC_INFO
) {
1237 register thread_basic_info_t basic_info
;
1239 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
1240 return (KERN_INVALID_ARGUMENT
);
1242 basic_info
= (thread_basic_info_t
) thread_info_out
;
1245 thread_lock(thread
);
1249 thread_read_times(thread
, &basic_info
->user_time
,
1250 &basic_info
->system_time
);
1253 * Update lazy-evaluated scheduler info because someone wants it.
1255 if (SCHED(can_update_priority
)(thread
))
1256 SCHED(update_priority
)(thread
);
1258 basic_info
->sleep_time
= 0;
1261 * To calculate cpu_usage, first correct for timer rate,
1262 * then for 5/8 ageing. The correction factor [3/5] is
1265 basic_info
->cpu_usage
= 0;
1266 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
1267 if (sched_tick_interval
) {
1268 basic_info
->cpu_usage
= (integer_t
)(((uint64_t)thread
->cpu_usage
1269 * TH_USAGE_SCALE
) / sched_tick_interval
);
1270 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
1274 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
1275 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
1277 basic_info
->policy
= ((thread
->sched_mode
== TH_MODE_TIMESHARE
)?
1278 POLICY_TIMESHARE
: POLICY_RR
);
1281 if (thread
->options
& TH_OPT_IDLE_THREAD
)
1282 flags
|= TH_FLAGS_IDLE
;
1284 if (!thread
->kernel_stack
)
1285 flags
|= TH_FLAGS_SWAPPED
;
1288 if (thread
->state
& TH_TERMINATE
)
1289 state
= TH_STATE_HALTED
;
1291 if (thread
->state
& TH_RUN
)
1292 state
= TH_STATE_RUNNING
;
1294 if (thread
->state
& TH_UNINT
)
1295 state
= TH_STATE_UNINTERRUPTIBLE
;
1297 if (thread
->state
& TH_SUSP
)
1298 state
= TH_STATE_STOPPED
;
1300 if (thread
->state
& TH_WAIT
)
1301 state
= TH_STATE_WAITING
;
1303 basic_info
->run_state
= state
;
1304 basic_info
->flags
= flags
;
1306 basic_info
->suspend_count
= thread
->user_stop_count
;
1308 thread_unlock(thread
);
1311 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
1313 return (KERN_SUCCESS
);
1316 if (flavor
== THREAD_IDENTIFIER_INFO
) {
1317 register thread_identifier_info_t identifier_info
;
1319 if (*thread_info_count
< THREAD_IDENTIFIER_INFO_COUNT
)
1320 return (KERN_INVALID_ARGUMENT
);
1322 identifier_info
= (thread_identifier_info_t
) thread_info_out
;
1325 thread_lock(thread
);
1327 identifier_info
->thread_id
= thread
->thread_id
;
1328 identifier_info
->thread_handle
= thread
->machine
.cthread_self
;
1329 if(thread
->task
->bsd_info
) {
1330 identifier_info
->dispatch_qaddr
= identifier_info
->thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1332 thread_unlock(thread
);
1334 return KERN_INVALID_ARGUMENT
;
1337 thread_unlock(thread
);
1339 return KERN_SUCCESS
;
1342 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
1343 policy_timeshare_info_t ts_info
;
1345 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
1346 return (KERN_INVALID_ARGUMENT
);
1348 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
1351 thread_lock(thread
);
1353 if (thread
->sched_mode
!= TH_MODE_TIMESHARE
) {
1354 thread_unlock(thread
);
1357 return (KERN_INVALID_POLICY
);
1360 ts_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1361 if (ts_info
->depressed
) {
1362 ts_info
->base_priority
= DEPRESSPRI
;
1363 ts_info
->depress_priority
= thread
->priority
;
1366 ts_info
->base_priority
= thread
->priority
;
1367 ts_info
->depress_priority
= -1;
1370 ts_info
->cur_priority
= thread
->sched_pri
;
1371 ts_info
->max_priority
= thread
->max_priority
;
1373 thread_unlock(thread
);
1376 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
1378 return (KERN_SUCCESS
);
1381 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
1382 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
1383 return (KERN_INVALID_ARGUMENT
);
1385 return (KERN_INVALID_POLICY
);
1388 if (flavor
== THREAD_SCHED_RR_INFO
) {
1389 policy_rr_info_t rr_info
;
1390 uint32_t quantum_time
;
1391 uint64_t quantum_ns
;
1393 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
1394 return (KERN_INVALID_ARGUMENT
);
1396 rr_info
= (policy_rr_info_t
) thread_info_out
;
1399 thread_lock(thread
);
1401 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
1402 thread_unlock(thread
);
1405 return (KERN_INVALID_POLICY
);
1408 rr_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1409 if (rr_info
->depressed
) {
1410 rr_info
->base_priority
= DEPRESSPRI
;
1411 rr_info
->depress_priority
= thread
->priority
;
1414 rr_info
->base_priority
= thread
->priority
;
1415 rr_info
->depress_priority
= -1;
1418 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
1419 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
1421 rr_info
->max_priority
= thread
->max_priority
;
1422 rr_info
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
1424 thread_unlock(thread
);
1427 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1429 return (KERN_SUCCESS
);
1432 return (KERN_INVALID_ARGUMENT
);
1438 time_value_t
*user_time
,
1439 time_value_t
*system_time
)
1443 uint64_t tval_user
, tval_system
;
1445 tval_user
= timer_grab(&thread
->user_timer
);
1446 tval_system
= timer_grab(&thread
->system_timer
);
1448 if (thread
->precise_user_kernel_time
) {
1449 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1450 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1451 user_time
->microseconds
= usecs
;
1453 absolutetime_to_microtime(tval_system
, &secs
, &usecs
);
1454 system_time
->seconds
= (typeof(system_time
->seconds
))secs
;
1455 system_time
->microseconds
= usecs
;
1457 /* system_timer may represent either sys or user */
1458 tval_user
+= tval_system
;
1459 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1460 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1461 user_time
->microseconds
= usecs
;
1463 system_time
->seconds
= 0;
1464 system_time
->microseconds
= 0;
1468 uint64_t thread_get_runtime_self(void)
1470 boolean_t interrupt_state
;
1472 thread_t thread
= NULL
;
1473 processor_t processor
= NULL
;
1475 thread
= current_thread();
1477 /* Not interrupt safe, as the scheduler may otherwise update timer values underneath us */
1478 interrupt_state
= ml_set_interrupts_enabled(FALSE
);
1479 processor
= current_processor();
1480 timer_switch(PROCESSOR_DATA(processor
, thread_timer
), mach_absolute_time(), PROCESSOR_DATA(processor
, thread_timer
));
1481 runtime
= (timer_grab(&thread
->user_timer
) + timer_grab(&thread
->system_timer
));
1482 ml_set_interrupts_enabled(interrupt_state
);
1489 __unused thread_t thread
,
1490 __unused processor_set_t new_pset
)
1492 return (KERN_FAILURE
);
1496 * thread_assign_default:
1498 * Special version of thread_assign for assigning threads to default
1502 thread_assign_default(
1505 return (thread_assign(thread
, &pset0
));
1509 * thread_get_assignment
1511 * Return current assignment for this thread.
1514 thread_get_assignment(
1516 processor_set_t
*pset
)
1519 return (KERN_INVALID_ARGUMENT
);
1523 return (KERN_SUCCESS
);
1527 * thread_wire_internal:
1529 * Specify that the target thread must always be able
1530 * to run and to allocate memory.
1533 thread_wire_internal(
1534 host_priv_t host_priv
,
1537 boolean_t
*prev_state
)
1539 if (host_priv
== NULL
|| thread
!= current_thread())
1540 return (KERN_INVALID_ARGUMENT
);
1542 assert(host_priv
== &realhost
);
1545 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1548 if (!(thread
->options
& TH_OPT_VMPRIV
))
1549 vm_page_free_reserve(1); /* XXX */
1550 thread
->options
|= TH_OPT_VMPRIV
;
1553 if (thread
->options
& TH_OPT_VMPRIV
)
1554 vm_page_free_reserve(-1); /* XXX */
1555 thread
->options
&= ~TH_OPT_VMPRIV
;
1558 return (KERN_SUCCESS
);
1565 * User-api wrapper for thread_wire_internal()
1569 host_priv_t host_priv
,
1573 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
1578 set_vm_privilege(boolean_t privileged
)
1580 boolean_t was_vmpriv
;
1582 if (current_thread()->options
& TH_OPT_VMPRIV
)
1587 if (privileged
!= FALSE
)
1588 current_thread()->options
|= TH_OPT_VMPRIV
;
1590 current_thread()->options
&= ~TH_OPT_VMPRIV
;
1592 return (was_vmpriv
);
1597 * XXX assuming current thread only, for now...
1600 thread_guard_violation(thread_t thread
, unsigned type
)
1602 assert(thread
== current_thread());
1604 spl_t s
= splsched();
1606 * Use the saved state area of the thread structure
1607 * to store all info required to handle the AST when
1608 * returning to userspace
1610 thread
->guard_exc_info
.type
= type
;
1611 thread_ast_set(thread
, AST_GUARD
);
1612 ast_propagate(thread
->ast
);
1620 * Handle AST_GUARD for a thread. This routine looks at the
1621 * state saved in the thread structure to determine the cause
1622 * of this exception. Based on this value, it invokes the
1623 * appropriate routine which determines other exception related
1624 * info and raises the exception.
1627 guard_ast(thread_t thread
)
1629 if (thread
->guard_exc_info
.type
== GUARD_TYPE_MACH_PORT
)
1630 mach_port_guard_ast(thread
);
1632 fd_guard_ast(thread
);
1636 thread_cputime_callback(int warning
, __unused
const void *arg0
, __unused
const void *arg1
)
1638 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
1639 #if CONFIG_TELEMETRY
1641 * This thread is in danger of violating the CPU usage monitor. Enable telemetry
1642 * on the entire task so there are micro-stackshots available if and when
1643 * EXC_RESOURCE is triggered. We could have chosen to enable micro-stackshots
1644 * for this thread only; but now that this task is suspect, knowing what all of
1645 * its threads are up to will be useful.
1647 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 1);
1652 #if CONFIG_TELEMETRY
1654 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
1655 * exceeded the limit, turn telemetry off for the task.
1657 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 0);
1661 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE();
1665 void __attribute__((noinline
))
1666 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void)
1669 task_t task
= current_task();
1670 thread_t thread
= current_thread();
1671 uint64_t tid
= thread
->thread_id
;
1672 char *procname
= (char *) "unknown";
1673 time_value_t thread_total_time
= {0, 0};
1674 time_value_t thread_system_time
;
1675 time_value_t thread_user_time
;
1678 uint32_t limit_percent
;
1679 uint32_t usage_percent
;
1680 uint32_t interval_sec
;
1681 uint64_t interval_ns
;
1682 uint64_t balance_ns
;
1683 boolean_t fatal
= FALSE
;
1685 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1686 struct ledger_entry_info lei
;
1688 assert(thread
->t_threadledger
!= LEDGER_NULL
);
1691 * Now that a thread has tripped the monitor, disable it for the entire task.
1694 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) {
1696 * The CPU usage monitor has been disabled on our task, so some other
1697 * thread must have gotten here first. We only send one exception per
1698 * task lifetime, so there's nothing left for us to do here.
1703 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_CPUMON
) {
1706 task_disable_cpumon(task
);
1710 pid
= proc_selfpid();
1711 if (task
->bsd_info
!= NULL
)
1712 procname
= proc_name_address(task
->bsd_info
);
1715 thread_get_cpulimit(&action
, &percentage
, &interval_ns
);
1717 interval_sec
= (uint32_t)(interval_ns
/ NSEC_PER_SEC
);
1719 thread_read_times(thread
, &thread_user_time
, &thread_system_time
);
1720 time_value_add(&thread_total_time
, &thread_user_time
);
1721 time_value_add(&thread_total_time
, &thread_system_time
);
1723 ledger_get_entry_info(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &lei
);
1725 absolutetime_to_nanoseconds(lei
.lei_balance
, &balance_ns
);
1726 usage_percent
= (uint32_t) ((balance_ns
* 100ULL) / lei
.lei_last_refill
);
1728 /* Show refill period in the same units as balance, limit, etc */
1729 nanoseconds_to_absolutetime(lei
.lei_refill_period
, &lei
.lei_refill_period
);
1731 limit_percent
= (uint32_t) ((lei
.lei_limit
* 100ULL) / lei
.lei_refill_period
);
1733 /* TODO: show task total runtime as well? see TASK_ABSOLUTETIME_INFO */
1735 if (disable_exc_resource
) {
1736 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1737 "supressed by a boot-arg\n", procname
, pid
, tid
);
1742 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1743 "supressed due to audio playback\n", procname
, pid
, tid
);
1746 printf("process %s[%d] thread %llu caught burning CPU! "
1747 "It used more than %d%% CPU (Actual recent usage: %d%%) over %d seconds. "
1748 "thread lifetime cpu usage %d.%06d seconds, (%d.%06d user, %d.%06d system) "
1749 "ledger info: balance: %lld credit: %lld debit: %lld limit: %llu (%d%%) "
1750 "period: %llu time since last refill (ns): %llu %s\n",
1752 percentage
, usage_percent
, interval_sec
,
1753 thread_total_time
.seconds
, thread_total_time
.microseconds
,
1754 thread_user_time
.seconds
, thread_user_time
.microseconds
,
1755 thread_system_time
.seconds
, thread_system_time
.microseconds
,
1757 lei
.lei_credit
, lei
.lei_debit
,
1758 lei
.lei_limit
, limit_percent
,
1759 lei
.lei_refill_period
, lei
.lei_last_refill
,
1760 (fatal
? "[fatal violation]" : ""));
1763 code
[0] = code
[1] = 0;
1764 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_CPU
);
1766 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_CPU_MONITOR_FATAL
);
1768 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_CPU_MONITOR
);
1770 EXC_RESOURCE_CPUMONITOR_ENCODE_INTERVAL(code
[0], interval_sec
);
1771 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[0], limit_percent
);
1772 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[1], usage_percent
);
1773 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
1777 jetsam_on_ledger_cpulimit_exceeded();
1779 task_terminate_internal(task
);
1784 #define UPDATE_IO_STATS(info, size) \
1787 info.size += size; \
1790 #define UPDATE_IO_STATS_ATOMIC(info, size) \
1792 OSIncrementAtomic64((SInt64 *)&(info.count)); \
1793 OSAddAtomic64(size, (SInt64 *)&(info.size)); \
1796 void thread_update_io_stats(thread_t thread
, int size
, int io_flags
)
1800 if (thread
->thread_io_stats
== NULL
|| thread
->task
->task_io_stats
== NULL
)
1803 if (io_flags
& DKIO_READ
) {
1804 UPDATE_IO_STATS(thread
->thread_io_stats
->disk_reads
, size
);
1805 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->disk_reads
, size
);
1808 if (io_flags
& DKIO_META
) {
1809 UPDATE_IO_STATS(thread
->thread_io_stats
->metadata
, size
);
1810 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->metadata
, size
);
1813 if (io_flags
& DKIO_PAGING
) {
1814 UPDATE_IO_STATS(thread
->thread_io_stats
->paging
, size
);
1815 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->paging
, size
);
1818 io_tier
= ((io_flags
& DKIO_TIER_MASK
) >> DKIO_TIER_SHIFT
);
1819 assert (io_tier
< IO_NUM_PRIORITIES
);
1821 UPDATE_IO_STATS(thread
->thread_io_stats
->io_priority
[io_tier
], size
);
1822 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->io_priority
[io_tier
], size
);
1824 /* Update Total I/O Counts */
1825 UPDATE_IO_STATS(thread
->thread_io_stats
->total_io
, size
);
1826 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->total_io
, size
);
1831 init_thread_ledgers(void) {
1832 ledger_template_t t
;
1835 assert(thread_ledger_template
== NULL
);
1837 if ((t
= ledger_template_create("Per-thread ledger")) == NULL
)
1838 panic("couldn't create thread ledger template");
1840 if ((idx
= ledger_entry_add(t
, "cpu_time", "sched", "ns")) < 0) {
1841 panic("couldn't create cpu_time entry for thread ledger template");
1844 if (ledger_set_callback(t
, idx
, thread_cputime_callback
, NULL
, NULL
) < 0) {
1845 panic("couldn't set thread ledger callback for cpu_time entry");
1848 thread_ledgers
.cpu_time
= idx
;
1850 thread_ledger_template
= t
;
1854 * Returns currently applied CPU usage limit, or 0/0 if none is applied.
1857 thread_get_cpulimit(int *action
, uint8_t *percentage
, uint64_t *interval_ns
)
1859 int64_t abstime
= 0;
1860 uint64_t limittime
= 0;
1861 thread_t thread
= current_thread();
1867 if (thread
->t_threadledger
== LEDGER_NULL
) {
1869 * This thread has no per-thread ledger, so it can't possibly
1870 * have a CPU limit applied.
1872 return (KERN_SUCCESS
);
1875 ledger_get_period(thread
->t_threadledger
, thread_ledgers
.cpu_time
, interval_ns
);
1876 ledger_get_limit(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &abstime
);
1878 if ((abstime
== LEDGER_LIMIT_INFINITY
) || (*interval_ns
== 0)) {
1880 * This thread's CPU time ledger has no period or limit; so it
1881 * doesn't have a CPU limit applied.
1883 return (KERN_SUCCESS
);
1887 * This calculation is the converse to the one in thread_set_cpulimit().
1889 absolutetime_to_nanoseconds(abstime
, &limittime
);
1890 *percentage
= (limittime
* 100ULL) / *interval_ns
;
1891 assert(*percentage
<= 100);
1893 if (thread
->options
& TH_OPT_PROC_CPULIMIT
) {
1894 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
1896 *action
= THREAD_CPULIMIT_BLOCK
;
1897 } else if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
1898 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
1900 *action
= THREAD_CPULIMIT_EXCEPTION
;
1902 *action
= THREAD_CPULIMIT_DISABLE
;
1905 return (KERN_SUCCESS
);
1909 * Set CPU usage limit on a thread.
1911 * Calling with percentage of 0 will unset the limit for this thread.
1914 thread_set_cpulimit(int action
, uint8_t percentage
, uint64_t interval_ns
)
1916 thread_t thread
= current_thread();
1918 uint64_t limittime
= 0;
1919 uint64_t abstime
= 0;
1921 assert(percentage
<= 100);
1923 if (action
== THREAD_CPULIMIT_DISABLE
) {
1925 * Remove CPU limit, if any exists.
1927 if (thread
->t_threadledger
!= LEDGER_NULL
) {
1928 l
= thread
->t_threadledger
;
1929 ledger_set_limit(l
, thread_ledgers
.cpu_time
, LEDGER_LIMIT_INFINITY
, 0);
1930 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_IGNORE
);
1931 thread
->options
&= ~(TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
);
1937 if (interval_ns
< MINIMUM_CPULIMIT_INTERVAL_MS
* NSEC_PER_MSEC
) {
1938 return (KERN_INVALID_ARGUMENT
);
1941 l
= thread
->t_threadledger
;
1942 if (l
== LEDGER_NULL
) {
1944 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
1946 if ((l
= ledger_instantiate(thread_ledger_template
, LEDGER_CREATE_INACTIVE_ENTRIES
)) == LEDGER_NULL
)
1947 return (KERN_RESOURCE_SHORTAGE
);
1950 * We are the first to create this thread's ledger, so only activate our entry.
1952 ledger_entry_setactive(l
, thread_ledgers
.cpu_time
);
1953 thread
->t_threadledger
= l
;
1957 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
1958 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
1960 limittime
= (interval_ns
* percentage
) / 100;
1961 nanoseconds_to_absolutetime(limittime
, &abstime
);
1962 ledger_set_limit(l
, thread_ledgers
.cpu_time
, abstime
, cpumon_ustackshots_trigger_pct
);
1964 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
1966 ledger_set_period(l
, thread_ledgers
.cpu_time
, interval_ns
);
1968 if (action
== THREAD_CPULIMIT_EXCEPTION
) {
1970 * We don't support programming the CPU usage monitor on a task if any of its
1971 * threads have a per-thread blocking CPU limit configured.
1973 if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
1974 panic("CPU usage monitor activated, but blocking thread limit exists");
1978 * Make a note that this thread's CPU limit is being used for the task-wide CPU
1979 * usage monitor. We don't have to arm the callback which will trigger the
1980 * exception, because that was done for us in ledger_instantiate (because the
1981 * ledger template used has a default callback).
1983 thread
->options
|= TH_OPT_PROC_CPULIMIT
;
1986 * We deliberately override any CPU limit imposed by a task-wide limit (eg
1987 * CPU usage monitor).
1989 thread
->options
&= ~TH_OPT_PROC_CPULIMIT
;
1991 thread
->options
|= TH_OPT_PRVT_CPULIMIT
;
1992 /* The per-thread ledger template by default has a callback for CPU time */
1993 ledger_disable_callback(l
, thread_ledgers
.cpu_time
);
1994 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
2003 __unused thread_t thread
)
2013 thread
->sched_call
= (call
!= NULL
)? call
: sched_call_null
;
2017 thread_static_param(
2021 thread_mtx_lock(thread
);
2022 thread
->static_param
= state
;
2023 thread_mtx_unlock(thread
);
2030 return (thread
!= THREAD_NULL
? thread
->thread_id
: 0);
2033 uint16_t thread_set_tag(thread_t th
, uint16_t tag
) {
2034 return thread_set_tag_internal(th
, tag
);
2036 uint16_t thread_get_tag(thread_t th
) {
2037 return thread_get_tag_internal(th
);
2041 thread_dispatchqaddr(
2044 uint64_t dispatchqueue_addr
= 0;
2045 uint64_t thread_handle
= 0;
2047 if (thread
!= THREAD_NULL
) {
2048 thread_handle
= thread
->machine
.cthread_self
;
2050 if (thread
->task
->bsd_info
)
2051 dispatchqueue_addr
= thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
2054 return (dispatchqueue_addr
);
2058 * Export routines to other components for things that are done as macros
2059 * within the osfmk component.
2062 #undef thread_reference
2063 void thread_reference(thread_t thread
);
2068 if (thread
!= THREAD_NULL
)
2069 thread_reference_internal(thread
);
2072 #undef thread_should_halt
2078 return (thread_should_halt_fast(th
));
2082 * thread_set_voucher_name - reset the voucher port name bound to this thread
2084 * Conditions: nothing locked
2086 * If we already converted the previous name to a cached voucher
2087 * reference, then we discard that reference here. The next lookup
2088 * will cache it again.
2092 thread_set_voucher_name(mach_port_name_t voucher_name
)
2094 thread_t thread
= current_thread();
2095 ipc_voucher_t new_voucher
= IPC_VOUCHER_NULL
;
2096 ipc_voucher_t voucher
;
2098 ledger_t bankledger
= NULL
;
2101 if (MACH_PORT_DEAD
== voucher_name
)
2102 return KERN_INVALID_RIGHT
;
2105 * agressively convert to voucher reference
2107 if (MACH_PORT_VALID(voucher_name
)) {
2108 new_voucher
= convert_port_name_to_voucher(voucher_name
);
2109 if (IPC_VOUCHER_NULL
== new_voucher
)
2110 return KERN_INVALID_ARGUMENT
;
2113 bankledger
= bank_get_voucher_ledger(new_voucher
);
2116 thread_mtx_lock(thread
);
2117 voucher
= thread
->ith_voucher
;
2118 thread
->ith_voucher_name
= voucher_name
;
2119 thread
->ith_voucher
= new_voucher
;
2121 bank_swap_thread_bank_ledger(thread
, bankledger
);
2123 thread_mtx_unlock(thread
);
2125 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2126 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2127 (uintptr_t)thread_tid(thread
),
2128 (uintptr_t)voucher_name
,
2129 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher
),
2132 if (IPC_VOUCHER_NULL
!= voucher
)
2133 ipc_voucher_release(voucher
);
2135 return KERN_SUCCESS
;
2139 * thread_get_mach_voucher - return a voucher reference for the specified thread voucher
2141 * Conditions: nothing locked
2143 * A reference to the voucher may be lazily pending, if someone set the voucher name
2144 * but nobody has done a lookup yet. In that case, we'll have to do the equivalent
2147 * NOTE: At the moment, there is no distinction between the current and effective
2148 * vouchers because we only set them at the thread level currently.
2151 thread_get_mach_voucher(
2152 thread_act_t thread
,
2153 mach_voucher_selector_t __unused which
,
2154 ipc_voucher_t
*voucherp
)
2156 ipc_voucher_t voucher
;
2157 mach_port_name_t voucher_name
;
2159 if (THREAD_NULL
== thread
)
2160 return KERN_INVALID_ARGUMENT
;
2162 thread_mtx_lock(thread
);
2163 voucher
= thread
->ith_voucher
;
2165 /* if already cached, just return a ref */
2166 if (IPC_VOUCHER_NULL
!= voucher
) {
2167 ipc_voucher_reference(voucher
);
2168 thread_mtx_unlock(thread
);
2169 *voucherp
= voucher
;
2170 return KERN_SUCCESS
;
2173 voucher_name
= thread
->ith_voucher_name
;
2175 /* convert the name to a port, then voucher reference */
2176 if (MACH_PORT_VALID(voucher_name
)) {
2180 ipc_object_copyin(thread
->task
->itk_space
, voucher_name
,
2181 MACH_MSG_TYPE_COPY_SEND
, (ipc_object_t
*)&port
)) {
2182 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2183 thread_mtx_unlock(thread
);
2184 *voucherp
= IPC_VOUCHER_NULL
;
2185 return KERN_SUCCESS
;
2188 /* convert to a voucher ref to return, and cache a ref on thread */
2189 voucher
= convert_port_to_voucher(port
);
2190 ipc_voucher_reference(voucher
);
2191 thread
->ith_voucher
= voucher
;
2192 thread_mtx_unlock(thread
);
2194 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2195 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2196 (uintptr_t)thread_tid(thread
),
2198 VM_KERNEL_ADDRPERM((uintptr_t)voucher
),
2202 ipc_port_release_send(port
);
2204 thread_mtx_unlock(thread
);
2206 *voucherp
= voucher
;
2207 return KERN_SUCCESS
;
2211 * thread_set_mach_voucher - set a voucher reference for the specified thread voucher
2213 * Conditions: callers holds a reference on the voucher.
2216 * We grab another reference to the voucher and bind it to the thread. Any lazy
2217 * binding is erased. The old voucher reference associated with the thread is
2221 thread_set_mach_voucher(
2223 ipc_voucher_t voucher
)
2225 ipc_voucher_t old_voucher
;
2227 ledger_t bankledger
= NULL
;
2230 if (THREAD_NULL
== thread
)
2231 return KERN_INVALID_ARGUMENT
;
2233 if (thread
!= current_thread() || thread
->started
)
2234 return KERN_INVALID_ARGUMENT
;
2237 ipc_voucher_reference(voucher
);
2239 bankledger
= bank_get_voucher_ledger(voucher
);
2241 thread_mtx_lock(thread
);
2242 old_voucher
= thread
->ith_voucher
;
2243 thread
->ith_voucher
= voucher
;
2244 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2246 bank_swap_thread_bank_ledger(thread
, bankledger
);
2248 thread_mtx_unlock(thread
);
2250 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2251 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2252 (uintptr_t)thread_tid(thread
),
2253 (uintptr_t)MACH_PORT_NULL
,
2254 VM_KERNEL_ADDRPERM((uintptr_t)voucher
),
2257 ipc_voucher_release(old_voucher
);
2259 return KERN_SUCCESS
;
2263 * thread_swap_mach_voucher - swap a voucher reference for the specified thread voucher
2265 * Conditions: callers holds a reference on the new and presumed old voucher(s).
2268 * If the old voucher is still the same as passed in, replace it with new voucher
2269 * and discard the old (and the reference passed in). Otherwise, discard the new
2270 * and return an updated old voucher.
2273 thread_swap_mach_voucher(
2275 ipc_voucher_t new_voucher
,
2276 ipc_voucher_t
*in_out_old_voucher
)
2278 mach_port_name_t old_voucher_name
;
2279 ipc_voucher_t old_voucher
;
2281 ledger_t bankledger
= NULL
;
2284 if (THREAD_NULL
== thread
)
2285 return KERN_INVALID_TASK
;
2287 if (thread
!= current_thread() || thread
->started
)
2288 return KERN_INVALID_ARGUMENT
;
2291 bankledger
= bank_get_voucher_ledger(new_voucher
);
2294 thread_mtx_lock(thread
);
2296 old_voucher
= thread
->ith_voucher
;
2298 if (IPC_VOUCHER_NULL
== old_voucher
) {
2299 old_voucher_name
= thread
->ith_voucher_name
;
2301 /* perform lazy binding if needed */
2302 if (MACH_PORT_VALID(old_voucher_name
)) {
2303 old_voucher
= convert_port_name_to_voucher(old_voucher_name
);
2304 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2305 thread
->ith_voucher
= old_voucher
;
2307 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2308 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2309 (uintptr_t)thread_tid(thread
),
2310 (uintptr_t)old_voucher_name
,
2311 VM_KERNEL_ADDRPERM((uintptr_t)old_voucher
),
2317 /* swap in new voucher, if old voucher matches the one supplied */
2318 if (old_voucher
== *in_out_old_voucher
) {
2319 ipc_voucher_reference(new_voucher
);
2320 thread
->ith_voucher
= new_voucher
;
2321 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2323 bank_swap_thread_bank_ledger(thread
, bankledger
);
2325 thread_mtx_unlock(thread
);
2327 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2328 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2329 (uintptr_t)thread_tid(thread
),
2330 (uintptr_t)MACH_PORT_NULL
,
2331 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher
),
2334 ipc_voucher_release(old_voucher
);
2336 *in_out_old_voucher
= IPC_VOUCHER_NULL
;
2337 return KERN_SUCCESS
;
2340 /* Otherwise, just return old voucher reference */
2341 ipc_voucher_reference(old_voucher
);
2342 thread_mtx_unlock(thread
);
2343 *in_out_old_voucher
= old_voucher
;
2344 return KERN_SUCCESS
;
2348 * thread_get_current_voucher_origin_pid - get the pid of the originator of the current voucher.
2351 thread_get_current_voucher_origin_pid(
2356 thread_t thread
= current_thread();
2358 buf_size
= sizeof(*pid
);
2359 kr
= mach_voucher_attr_command(thread
->ith_voucher
,
2360 MACH_VOUCHER_ATTR_KEY_BANK
,
2361 BANK_ORIGINATOR_PID
,
2364 (mach_voucher_attr_content_t
)pid
,
2371 uint32_t dtrace_get_thread_predcache(thread_t thread
)
2373 if (thread
!= THREAD_NULL
)
2374 return thread
->t_dtrace_predcache
;
2379 int64_t dtrace_get_thread_vtime(thread_t thread
)
2381 if (thread
!= THREAD_NULL
)
2382 return thread
->t_dtrace_vtime
;
2387 int64_t dtrace_get_thread_tracing(thread_t thread
)
2389 if (thread
!= THREAD_NULL
)
2390 return thread
->t_dtrace_tracing
;
2395 boolean_t
dtrace_get_thread_reentering(thread_t thread
)
2397 if (thread
!= THREAD_NULL
)
2398 return (thread
->options
& TH_OPT_DTRACE
) ? TRUE
: FALSE
;
2403 vm_offset_t
dtrace_get_kernel_stack(thread_t thread
)
2405 if (thread
!= THREAD_NULL
)
2406 return thread
->kernel_stack
;
2411 int64_t dtrace_calc_thread_recent_vtime(thread_t thread
)
2413 if (thread
!= THREAD_NULL
) {
2414 processor_t processor
= current_processor();
2415 uint64_t abstime
= mach_absolute_time();
2418 timer
= PROCESSOR_DATA(processor
, thread_timer
);
2420 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
)) +
2421 (abstime
- timer
->tstamp
); /* XXX need interrupts off to prevent missed time? */
2426 void dtrace_set_thread_predcache(thread_t thread
, uint32_t predcache
)
2428 if (thread
!= THREAD_NULL
)
2429 thread
->t_dtrace_predcache
= predcache
;
2432 void dtrace_set_thread_vtime(thread_t thread
, int64_t vtime
)
2434 if (thread
!= THREAD_NULL
)
2435 thread
->t_dtrace_vtime
= vtime
;
2438 void dtrace_set_thread_tracing(thread_t thread
, int64_t accum
)
2440 if (thread
!= THREAD_NULL
)
2441 thread
->t_dtrace_tracing
= accum
;
2444 void dtrace_set_thread_reentering(thread_t thread
, boolean_t vbool
)
2446 if (thread
!= THREAD_NULL
) {
2448 thread
->options
|= TH_OPT_DTRACE
;
2450 thread
->options
&= (~TH_OPT_DTRACE
);
2454 vm_offset_t
dtrace_set_thread_recover(thread_t thread
, vm_offset_t recover
)
2456 vm_offset_t prev
= 0;
2458 if (thread
!= THREAD_NULL
) {
2459 prev
= thread
->recover
;
2460 thread
->recover
= recover
;
2465 void dtrace_thread_bootstrap(void)
2467 task_t task
= current_task();
2469 if (task
->thread_count
== 1) {
2470 thread_t thread
= current_thread();
2471 if (thread
->t_dtrace_flags
& TH_DTRACE_EXECSUCCESS
) {
2472 thread
->t_dtrace_flags
&= ~TH_DTRACE_EXECSUCCESS
;
2473 DTRACE_PROC(exec__success
);
2477 DTRACE_PROC(lwp__start
);
2482 dtrace_thread_didexec(thread_t thread
)
2484 thread
->t_dtrace_flags
|= TH_DTRACE_EXECSUCCESS
;
2486 #endif /* CONFIG_DTRACE */