]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/thread.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / osfmk / kern / thread.c
1 /*
2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_FREE_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: kern/thread.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub
61 * Date: 1986
62 *
63 * Thread management primitives implementation.
64 */
65 /*
66 * Copyright (c) 1993 The University of Utah and
67 * the Computer Systems Laboratory (CSL). All rights reserved.
68 *
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.
74 *
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.
78 *
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.
81 *
82 */
83
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>
92
93 #include <machine/thread.h>
94 #include <machine/pal_routines.h>
95 #include <machine/limits.h>
96
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>
120 #include <corpses/task_corpse.h>
121 #if KPC
122 #include <kern/kpc.h>
123 #endif
124
125 #include <ipc/ipc_kmsg.h>
126 #include <ipc/ipc_port.h>
127 #include <bank/bank_types.h>
128
129 #include <vm/vm_kern.h>
130 #include <vm/vm_pageout.h>
131
132 #include <sys/kdebug.h>
133 #include <sys/bsdtask_info.h>
134 #include <mach/sdt.h>
135
136 /*
137 * Exported interfaces
138 */
139 #include <mach/task_server.h>
140 #include <mach/thread_act_server.h>
141 #include <mach/mach_host_server.h>
142 #include <mach/host_priv_server.h>
143 #include <mach/mach_voucher_server.h>
144
145 static struct zone *thread_zone;
146 static lck_grp_attr_t thread_lck_grp_attr;
147 lck_attr_t thread_lck_attr;
148 lck_grp_t thread_lck_grp;
149
150 struct zone *thread_qos_override_zone;
151
152 decl_simple_lock_data(static,thread_stack_lock)
153 static queue_head_t thread_stack_queue;
154
155 decl_simple_lock_data(static,thread_terminate_lock)
156 static queue_head_t thread_terminate_queue;
157
158 static queue_head_t crashed_threads_queue;
159
160 static struct thread thread_template, init_thread;
161
162 static void sched_call_null(
163 int type,
164 thread_t thread);
165
166 #ifdef MACH_BSD
167 extern void proc_exit(void *);
168 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
169 extern int proc_selfpid(void);
170 extern char * proc_name_address(void *p);
171 #endif /* MACH_BSD */
172
173 extern int disable_exc_resource;
174 extern int audio_active;
175 extern int debug_task;
176 int thread_max = CONFIG_THREAD_MAX; /* Max number of threads */
177 int task_threadmax = CONFIG_THREAD_MAX;
178
179 static uint64_t thread_unique_id = 100;
180
181 struct _thread_ledger_indices thread_ledgers = { -1 };
182 static ledger_template_t thread_ledger_template = NULL;
183 void init_thread_ledgers(void);
184 int task_disable_cpumon(task_t task);
185
186 #if CONFIG_JETSAM
187 void jetsam_on_ledger_cpulimit_exceeded(void);
188 #endif
189
190 /*
191 * Level (in terms of percentage of the limit) at which the CPU usage monitor triggers telemetry.
192 *
193 * (ie when any thread's CPU consumption exceeds 70% of the limit, start taking user
194 * stacktraces, aka micro-stackshots)
195 */
196 #define CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT 70
197
198 int cpumon_ustackshots_trigger_pct; /* Percentage. Level at which we start gathering telemetry. */
199 void __attribute__((noinline)) THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void);
200
201 /*
202 * The smallest interval over which we support limiting CPU consumption is 1ms
203 */
204 #define MINIMUM_CPULIMIT_INTERVAL_MS 1
205
206 void
207 thread_bootstrap(void)
208 {
209 /*
210 * Fill in a template thread for fast initialization.
211 */
212
213 thread_template.runq = PROCESSOR_NULL;
214
215 thread_template.ref_count = 2;
216
217 thread_template.reason = AST_NONE;
218 thread_template.at_safe_point = FALSE;
219 thread_template.wait_event = NO_EVENT64;
220 thread_template.waitq = NULL;
221 thread_template.wait_result = THREAD_WAITING;
222 thread_template.options = THREAD_ABORTSAFE;
223 thread_template.state = TH_WAIT | TH_UNINT;
224 thread_template.wake_active = FALSE;
225 thread_template.continuation = THREAD_CONTINUE_NULL;
226 thread_template.parameter = NULL;
227
228 thread_template.importance = 0;
229 thread_template.sched_mode = TH_MODE_NONE;
230 thread_template.sched_flags = 0;
231 thread_template.saved_mode = TH_MODE_NONE;
232 thread_template.safe_release = 0;
233
234 thread_template.sfi_class = SFI_CLASS_UNSPECIFIED;
235 thread_template.sfi_wait_class = SFI_CLASS_UNSPECIFIED;
236
237 thread_template.active = 0;
238 thread_template.started = 0;
239 thread_template.static_param = 0;
240 thread_template.policy_reset = 0;
241
242 thread_template.base_pri = 0;
243 thread_template.sched_pri = 0;
244 thread_template.max_priority = 0;
245 thread_template.task_priority = 0;
246 thread_template.promotions = 0;
247 thread_template.pending_promoter_index = 0;
248 thread_template.pending_promoter[0] = NULL;
249 thread_template.pending_promoter[1] = NULL;
250 thread_template.rwlock_count = 0;
251
252 #if MACH_ASSERT
253 thread_template.SHARE_COUNT = 0;
254 thread_template.BG_COUNT = 0;
255 #endif /* MACH_ASSERT */
256
257 thread_template.realtime.deadline = UINT64_MAX;
258
259 thread_template.quantum_remaining = 0;
260 thread_template.last_run_time = 0;
261 thread_template.last_made_runnable_time = 0;
262
263 thread_template.computation_metered = 0;
264 thread_template.computation_epoch = 0;
265
266 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
267 thread_template.sched_stamp = 0;
268 thread_template.pri_shift = INT8_MAX;
269 thread_template.sched_usage = 0;
270 thread_template.cpu_usage = thread_template.cpu_delta = 0;
271 #endif
272 thread_template.c_switch = thread_template.p_switch = thread_template.ps_switch = 0;
273
274 thread_template.bound_processor = PROCESSOR_NULL;
275 thread_template.last_processor = PROCESSOR_NULL;
276
277 thread_template.sched_call = sched_call_null;
278
279 timer_init(&thread_template.user_timer);
280 timer_init(&thread_template.system_timer);
281 thread_template.user_timer_save = 0;
282 thread_template.system_timer_save = 0;
283 thread_template.vtimer_user_save = 0;
284 thread_template.vtimer_prof_save = 0;
285 thread_template.vtimer_rlim_save = 0;
286
287 #if CONFIG_SCHED_SFI
288 thread_template.wait_sfi_begin_time = 0;
289 #endif
290
291 thread_template.wait_timer_is_set = FALSE;
292 thread_template.wait_timer_active = 0;
293
294 thread_template.depress_timer_active = 0;
295
296 thread_template.recover = (vm_offset_t)NULL;
297
298 thread_template.map = VM_MAP_NULL;
299
300 #if CONFIG_DTRACE
301 thread_template.t_dtrace_predcache = 0;
302 thread_template.t_dtrace_vtime = 0;
303 thread_template.t_dtrace_tracing = 0;
304 #endif /* CONFIG_DTRACE */
305
306 #if KPC
307 thread_template.kpc_buf = NULL;
308 #endif
309
310 #if HYPERVISOR
311 thread_template.hv_thread_target = NULL;
312 #endif /* HYPERVISOR */
313
314 thread_template.t_chud = 0;
315
316 #if (DEVELOPMENT || DEBUG)
317 thread_template.t_page_creation_throttled_hard = 0;
318 thread_template.t_page_creation_throttled_soft = 0;
319 #endif /* DEVELOPMENT || DEBUG */
320 thread_template.t_page_creation_throttled = 0;
321 thread_template.t_page_creation_count = 0;
322 thread_template.t_page_creation_time = 0;
323
324 thread_template.affinity_set = NULL;
325
326 thread_template.syscalls_unix = 0;
327 thread_template.syscalls_mach = 0;
328
329 thread_template.t_ledger = LEDGER_NULL;
330 thread_template.t_threadledger = LEDGER_NULL;
331 #ifdef CONFIG_BANK
332 thread_template.t_bankledger = LEDGER_NULL;
333 thread_template.t_deduct_bank_ledger_time = 0;
334 #endif
335
336 thread_template.requested_policy = default_task_requested_policy;
337 thread_template.effective_policy = default_task_effective_policy;
338 thread_template.pended_policy = default_task_pended_policy;
339
340 bzero(&thread_template.overrides, sizeof(thread_template.overrides));
341
342 thread_template.iotier_override = THROTTLE_LEVEL_NONE;
343 thread_template.thread_io_stats = NULL;
344 thread_template.thread_callout_interrupt_wakeups = thread_template.thread_callout_platform_idle_wakeups = 0;
345
346 thread_template.thread_timer_wakeups_bin_1 = thread_template.thread_timer_wakeups_bin_2 = 0;
347 thread_template.callout_woken_from_icontext = thread_template.callout_woken_from_platform_idle = 0;
348
349 thread_template.thread_tag = 0;
350
351 thread_template.ith_voucher_name = MACH_PORT_NULL;
352 thread_template.ith_voucher = IPC_VOUCHER_NULL;
353
354 thread_template.work_interval_id = 0;
355
356 init_thread = thread_template;
357 machine_set_current_thread(&init_thread);
358 }
359
360 extern boolean_t allow_qos_policy_set;
361
362 void
363 thread_init(void)
364 {
365 thread_zone = zinit(
366 sizeof(struct thread),
367 thread_max * sizeof(struct thread),
368 THREAD_CHUNK * sizeof(struct thread),
369 "threads");
370
371 thread_qos_override_zone = zinit(
372 sizeof(struct thread_qos_override),
373 4 * thread_max * sizeof(struct thread_qos_override),
374 PAGE_SIZE,
375 "thread qos override");
376 zone_change(thread_qos_override_zone, Z_EXPAND, TRUE);
377 zone_change(thread_qos_override_zone, Z_COLLECT, TRUE);
378 zone_change(thread_qos_override_zone, Z_CALLERACCT, FALSE);
379 zone_change(thread_qos_override_zone, Z_NOENCRYPT, TRUE);
380
381 lck_grp_attr_setdefault(&thread_lck_grp_attr);
382 lck_grp_init(&thread_lck_grp, "thread", &thread_lck_grp_attr);
383 lck_attr_setdefault(&thread_lck_attr);
384
385 stack_init();
386
387 /*
388 * Initialize any machine-dependent
389 * per-thread structures necessary.
390 */
391 machine_thread_init();
392
393 if (!PE_parse_boot_argn("cpumon_ustackshots_trigger_pct", &cpumon_ustackshots_trigger_pct,
394 sizeof (cpumon_ustackshots_trigger_pct))) {
395 cpumon_ustackshots_trigger_pct = CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT;
396 }
397
398 PE_parse_boot_argn("-qos-policy-allow", &allow_qos_policy_set, sizeof(allow_qos_policy_set));
399
400 init_thread_ledgers();
401 }
402
403 static void
404 thread_terminate_continue(void)
405 {
406 panic("thread_terminate_continue");
407 /*NOTREACHED*/
408 }
409
410 /*
411 * thread_terminate_self:
412 */
413 void
414 thread_terminate_self(void)
415 {
416 thread_t thread = current_thread();
417 task_t task;
418 spl_t s;
419 int threadcnt;
420
421 pal_thread_terminate_self(thread);
422
423 DTRACE_PROC(lwp__exit);
424
425 thread_mtx_lock(thread);
426
427 ipc_thread_disable(thread);
428
429 thread_mtx_unlock(thread);
430
431 s = splsched();
432 thread_lock(thread);
433
434 assert_thread_sched_count(thread);
435
436 /*
437 * Cancel priority depression, wait for concurrent expirations
438 * on other processors.
439 */
440 if (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) {
441 thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
442
443 /* If our priority was low because of a depressed yield, restore it in case we block below */
444 thread_recompute_sched_pri(thread, FALSE);
445
446 if (timer_call_cancel(&thread->depress_timer))
447 thread->depress_timer_active--;
448 }
449
450 while (thread->depress_timer_active > 0) {
451 thread_unlock(thread);
452 splx(s);
453
454 delay(1);
455
456 s = splsched();
457 thread_lock(thread);
458 }
459
460 thread_sched_call(thread, NULL);
461
462 thread_unlock(thread);
463 splx(s);
464
465
466 thread_mtx_lock(thread);
467
468 thread_policy_reset(thread);
469
470 thread_mtx_unlock(thread);
471
472 task = thread->task;
473 uthread_cleanup(task, thread->uthread, task->bsd_info, thread->inspection == 1 ? TRUE : FALSE);
474 threadcnt = hw_atomic_sub(&task->active_thread_count, 1);
475
476 /*
477 * If we are the last thread to terminate and the task is
478 * associated with a BSD process, perform BSD process exit.
479 */
480 if (threadcnt == 0 && task->bsd_info != NULL) {
481 proc_exit(task->bsd_info);
482 /*
483 * if there is crash info in task
484 * then do the deliver action since this is
485 * last thread for this task.
486 */
487 if (task->corpse_info) {
488 task_deliver_crash_notification(task);
489 }
490 }
491 uthread_cred_free(thread->uthread);
492
493 s = splsched();
494 thread_lock(thread);
495
496 /*
497 * Cancel wait timer, and wait for
498 * concurrent expirations.
499 */
500 if (thread->wait_timer_is_set) {
501 thread->wait_timer_is_set = FALSE;
502
503 if (timer_call_cancel(&thread->wait_timer))
504 thread->wait_timer_active--;
505 }
506
507 while (thread->wait_timer_active > 0) {
508 thread_unlock(thread);
509 splx(s);
510
511 delay(1);
512
513 s = splsched();
514 thread_lock(thread);
515 }
516
517 /*
518 * If there is a reserved stack, release it.
519 */
520 if (thread->reserved_stack != 0) {
521 stack_free_reserved(thread);
522 thread->reserved_stack = 0;
523 }
524
525 /*
526 * Mark thread as terminating, and block.
527 */
528 thread->state |= TH_TERMINATE;
529 thread_mark_wait_locked(thread, THREAD_UNINT);
530 assert((thread->sched_flags & TH_SFLAG_PROMOTED) == 0);
531 assert(thread->promotions == 0);
532 assert(!(thread->sched_flags & TH_SFLAG_WAITQ_PROMOTED));
533 assert(thread->rwlock_count == 0);
534 thread_unlock(thread);
535 /* splsched */
536
537 thread_block((thread_continue_t)thread_terminate_continue);
538 /*NOTREACHED*/
539 }
540
541 /* Drop a thread refcount that definitely isn't the last one. */
542 void
543 thread_deallocate_safe(thread_t thread)
544 {
545 if (__improbable(hw_atomic_sub(&(thread)->ref_count, 1) == 0))
546 panic("bad thread refcount!");
547 }
548
549 void
550 thread_deallocate(
551 thread_t thread)
552 {
553 task_t task;
554
555 if (thread == THREAD_NULL)
556 return;
557
558 if (__probable(hw_atomic_sub(&(thread)->ref_count, 1) > 0))
559 return;
560
561 if(!(thread->state & TH_TERMINATE2))
562 panic("thread_deallocate: thread not properly terminated\n");
563
564 assert(thread->runq == PROCESSOR_NULL);
565
566 #if KPC
567 kpc_thread_destroy(thread);
568 #endif
569
570 ipc_thread_terminate(thread);
571
572 proc_thread_qos_deallocate(thread);
573
574 task = thread->task;
575
576 #ifdef MACH_BSD
577 {
578 void *ut = thread->uthread;
579
580 thread->uthread = NULL;
581 uthread_zone_free(ut);
582 }
583 #endif /* MACH_BSD */
584
585 if (thread->t_ledger)
586 ledger_dereference(thread->t_ledger);
587 if (thread->t_threadledger)
588 ledger_dereference(thread->t_threadledger);
589
590 if (IPC_VOUCHER_NULL != thread->ith_voucher)
591 ipc_voucher_release(thread->ith_voucher);
592
593 if (thread->thread_io_stats)
594 kfree(thread->thread_io_stats, sizeof(struct io_stat_info));
595
596 if (thread->kernel_stack != 0)
597 stack_free(thread);
598
599 lck_mtx_destroy(&thread->mutex, &thread_lck_grp);
600 machine_thread_destroy(thread);
601
602 task_deallocate(task);
603
604 zfree(thread_zone, thread);
605 }
606
607 /*
608 * thread_terminate_daemon:
609 *
610 * Perform final clean up for terminating threads.
611 */
612 static void
613 thread_terminate_daemon(void)
614 {
615 thread_t self, thread;
616 task_t task;
617
618 self = current_thread();
619 self->options |= TH_OPT_SYSTEM_CRITICAL;
620
621 (void)splsched();
622 simple_lock(&thread_terminate_lock);
623
624 while ((thread = (thread_t)dequeue_head(&thread_terminate_queue)) != THREAD_NULL) {
625
626 /*
627 * if marked for crash reporting, skip reaping.
628 * The corpse delivery thread will clear bit and enqueue
629 * for reaping when done
630 */
631 if (thread->inspection){
632 enqueue_tail(&crashed_threads_queue, (queue_entry_t)thread);
633 continue;
634 }
635
636 simple_unlock(&thread_terminate_lock);
637 (void)spllo();
638
639 assert(thread->SHARE_COUNT == 0);
640 assert(thread->BG_COUNT == 0);
641
642 task = thread->task;
643
644 task_lock(task);
645 task->total_user_time += timer_grab(&thread->user_timer);
646 if (thread->precise_user_kernel_time) {
647 task->total_system_time += timer_grab(&thread->system_timer);
648 } else {
649 task->total_user_time += timer_grab(&thread->system_timer);
650 }
651
652 task->c_switch += thread->c_switch;
653 task->p_switch += thread->p_switch;
654 task->ps_switch += thread->ps_switch;
655
656 task->syscalls_unix += thread->syscalls_unix;
657 task->syscalls_mach += thread->syscalls_mach;
658
659 task->task_timer_wakeups_bin_1 += thread->thread_timer_wakeups_bin_1;
660 task->task_timer_wakeups_bin_2 += thread->thread_timer_wakeups_bin_2;
661 task->task_gpu_ns += ml_gpu_stat(thread);
662
663 thread_update_qos_cpu_time(thread, FALSE);
664 queue_remove(&task->threads, thread, thread_t, task_threads);
665 task->thread_count--;
666
667 /*
668 * If the task is being halted, and there is only one thread
669 * left in the task after this one, then wakeup that thread.
670 */
671 if (task->thread_count == 1 && task->halting)
672 thread_wakeup((event_t)&task->halting);
673
674 task_unlock(task);
675
676 lck_mtx_lock(&tasks_threads_lock);
677 queue_remove(&threads, thread, thread_t, threads);
678 threads_count--;
679 lck_mtx_unlock(&tasks_threads_lock);
680
681 thread_deallocate(thread);
682
683 (void)splsched();
684 simple_lock(&thread_terminate_lock);
685 }
686
687 assert_wait((event_t)&thread_terminate_queue, THREAD_UNINT);
688 simple_unlock(&thread_terminate_lock);
689 /* splsched */
690
691 self->options &= ~TH_OPT_SYSTEM_CRITICAL;
692 thread_block((thread_continue_t)thread_terminate_daemon);
693 /*NOTREACHED*/
694 }
695
696 /*
697 * thread_terminate_enqueue:
698 *
699 * Enqueue a terminating thread for final disposition.
700 *
701 * Called at splsched.
702 */
703 void
704 thread_terminate_enqueue(
705 thread_t thread)
706 {
707 KERNEL_DEBUG_CONSTANT(TRACE_DATA_THREAD_TERMINATE | DBG_FUNC_NONE, thread->thread_id, 0, 0, 0, 0);
708
709 simple_lock(&thread_terminate_lock);
710 enqueue_tail(&thread_terminate_queue, (queue_entry_t)thread);
711 simple_unlock(&thread_terminate_lock);
712
713 thread_wakeup((event_t)&thread_terminate_queue);
714 }
715
716 /*
717 * thread_terminate_crashed_threads:
718 * walk the list of crashed therds and put back set of threads
719 * who are no longer being inspected.
720 */
721 void
722 thread_terminate_crashed_threads()
723 {
724 thread_t th_iter, th_remove;
725 boolean_t should_wake_terminate_queue = FALSE;
726
727 simple_lock(&thread_terminate_lock);
728 /*
729 * loop through the crashed threads queue
730 * to put any threads that are not being inspected anymore
731 */
732 th_iter = (thread_t)queue_first(&crashed_threads_queue);
733 while (!queue_end(&crashed_threads_queue, (queue_entry_t)th_iter)) {
734 th_remove = th_iter;
735 th_iter = (thread_t)queue_next(&th_iter->links);
736
737 /* make sure current_thread is never in crashed queue */
738 assert(th_remove != current_thread());
739 if (th_remove->inspection != TRUE){
740 remque((queue_entry_t)th_remove);
741 enqueue_tail(&thread_terminate_queue, (queue_entry_t)th_remove);
742 should_wake_terminate_queue = TRUE;
743 }
744 }
745
746 simple_unlock(&thread_terminate_lock);
747 if (should_wake_terminate_queue == TRUE) {
748 thread_wakeup((event_t)&thread_terminate_queue);
749 }
750 }
751
752 /*
753 * thread_stack_daemon:
754 *
755 * Perform stack allocation as required due to
756 * invoke failures.
757 */
758 static void
759 thread_stack_daemon(void)
760 {
761 thread_t thread;
762 spl_t s;
763
764 s = splsched();
765 simple_lock(&thread_stack_lock);
766
767 while ((thread = (thread_t)dequeue_head(&thread_stack_queue)) != THREAD_NULL) {
768 simple_unlock(&thread_stack_lock);
769 splx(s);
770
771 /* allocate stack with interrupts enabled so that we can call into VM */
772 stack_alloc(thread);
773
774 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_WAIT) | DBG_FUNC_END, thread_tid(thread), 0, 0, 0, 0);
775
776 s = splsched();
777 thread_lock(thread);
778 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
779 thread_unlock(thread);
780
781 simple_lock(&thread_stack_lock);
782 }
783
784 assert_wait((event_t)&thread_stack_queue, THREAD_UNINT);
785 simple_unlock(&thread_stack_lock);
786 splx(s);
787
788 thread_block((thread_continue_t)thread_stack_daemon);
789 /*NOTREACHED*/
790 }
791
792 /*
793 * thread_stack_enqueue:
794 *
795 * Enqueue a thread for stack allocation.
796 *
797 * Called at splsched.
798 */
799 void
800 thread_stack_enqueue(
801 thread_t thread)
802 {
803 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_STACK_WAIT) | DBG_FUNC_START, thread_tid(thread), 0, 0, 0, 0);
804
805 simple_lock(&thread_stack_lock);
806 enqueue_tail(&thread_stack_queue, (queue_entry_t)thread);
807 simple_unlock(&thread_stack_lock);
808
809 thread_wakeup((event_t)&thread_stack_queue);
810 }
811
812 void
813 thread_daemon_init(void)
814 {
815 kern_return_t result;
816 thread_t thread = NULL;
817
818 simple_lock_init(&thread_terminate_lock, 0);
819 queue_init(&thread_terminate_queue);
820 queue_init(&crashed_threads_queue);
821
822 result = kernel_thread_start_priority((thread_continue_t)thread_terminate_daemon, NULL, MINPRI_KERNEL, &thread);
823 if (result != KERN_SUCCESS)
824 panic("thread_daemon_init: thread_terminate_daemon");
825
826 thread_deallocate(thread);
827
828 simple_lock_init(&thread_stack_lock, 0);
829 queue_init(&thread_stack_queue);
830
831 result = kernel_thread_start_priority((thread_continue_t)thread_stack_daemon, NULL, BASEPRI_PREEMPT, &thread);
832 if (result != KERN_SUCCESS)
833 panic("thread_daemon_init: thread_stack_daemon");
834
835 thread_deallocate(thread);
836 }
837
838 #define TH_OPTION_NONE 0x00
839 #define TH_OPTION_NOCRED 0x01
840 #define TH_OPTION_NOSUSP 0x02
841 /*
842 * Create a new thread.
843 * Doesn't start the thread running.
844 *
845 * Task and tasks_threads_lock are returned locked on success.
846 */
847 static kern_return_t
848 thread_create_internal(
849 task_t parent_task,
850 integer_t priority,
851 thread_continue_t continuation,
852 int options,
853 thread_t *out_thread)
854 {
855 thread_t new_thread;
856 static thread_t first_thread;
857
858 /*
859 * Allocate a thread and initialize static fields
860 */
861 if (first_thread == THREAD_NULL)
862 new_thread = first_thread = current_thread();
863 else
864 new_thread = (thread_t)zalloc(thread_zone);
865 if (new_thread == THREAD_NULL)
866 return (KERN_RESOURCE_SHORTAGE);
867
868 if (new_thread != first_thread)
869 *new_thread = thread_template;
870
871 #ifdef MACH_BSD
872 new_thread->uthread = uthread_alloc(parent_task, new_thread, (options & TH_OPTION_NOCRED) != 0);
873 if (new_thread->uthread == NULL) {
874 zfree(thread_zone, new_thread);
875 return (KERN_RESOURCE_SHORTAGE);
876 }
877 #endif /* MACH_BSD */
878
879 if (machine_thread_create(new_thread, parent_task) != KERN_SUCCESS) {
880 #ifdef MACH_BSD
881 void *ut = new_thread->uthread;
882
883 new_thread->uthread = NULL;
884 /* cred free may not be necessary */
885 uthread_cleanup(parent_task, ut, parent_task->bsd_info, FALSE);
886 uthread_cred_free(ut);
887 uthread_zone_free(ut);
888 #endif /* MACH_BSD */
889
890 zfree(thread_zone, new_thread);
891 return (KERN_FAILURE);
892 }
893
894 new_thread->task = parent_task;
895
896 thread_lock_init(new_thread);
897 wake_lock_init(new_thread);
898
899 lck_mtx_init(&new_thread->mutex, &thread_lck_grp, &thread_lck_attr);
900
901 ipc_thread_init(new_thread);
902
903 new_thread->continuation = continuation;
904
905 /* Allocate I/O Statistics structure */
906 new_thread->thread_io_stats = (io_stat_info_t)kalloc(sizeof(struct io_stat_info));
907 assert(new_thread->thread_io_stats != NULL);
908 bzero(new_thread->thread_io_stats, sizeof(struct io_stat_info));
909
910 #if CONFIG_IOSCHED
911 /* Clear out the I/O Scheduling info for AppleFSCompression */
912 new_thread->decmp_upl = NULL;
913 #endif /* CONFIG_IOSCHED */
914
915 lck_mtx_lock(&tasks_threads_lock);
916 task_lock(parent_task);
917
918 if ( !parent_task->active || parent_task->halting ||
919 ((options & TH_OPTION_NOSUSP) != 0 &&
920 parent_task->suspend_count > 0) ||
921 (parent_task->thread_count >= task_threadmax &&
922 parent_task != kernel_task) ) {
923 task_unlock(parent_task);
924 lck_mtx_unlock(&tasks_threads_lock);
925
926 #ifdef MACH_BSD
927 {
928 void *ut = new_thread->uthread;
929
930 new_thread->uthread = NULL;
931 uthread_cleanup(parent_task, ut, parent_task->bsd_info, FALSE);
932 /* cred free may not be necessary */
933 uthread_cred_free(ut);
934 uthread_zone_free(ut);
935 }
936 #endif /* MACH_BSD */
937 ipc_thread_disable(new_thread);
938 ipc_thread_terminate(new_thread);
939 kfree(new_thread->thread_io_stats, sizeof(struct io_stat_info));
940 lck_mtx_destroy(&new_thread->mutex, &thread_lck_grp);
941 machine_thread_destroy(new_thread);
942 zfree(thread_zone, new_thread);
943 return (KERN_FAILURE);
944 }
945
946 /* New threads inherit any default state on the task */
947 machine_thread_inherit_taskwide(new_thread, parent_task);
948
949 task_reference_internal(parent_task);
950
951 if (new_thread->task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) {
952 /*
953 * This task has a per-thread CPU limit; make sure this new thread
954 * gets its limit set too, before it gets out of the kernel.
955 */
956 set_astledger(new_thread);
957 }
958
959 /* Instantiate a thread ledger. Do not fail thread creation if ledger creation fails. */
960 if ((new_thread->t_threadledger = ledger_instantiate(thread_ledger_template,
961 LEDGER_CREATE_INACTIVE_ENTRIES)) != LEDGER_NULL) {
962
963 ledger_entry_setactive(new_thread->t_threadledger, thread_ledgers.cpu_time);
964 }
965
966 new_thread->cpu_time_last_qos = 0;
967 #ifdef CONFIG_BANK
968 new_thread->t_bankledger = LEDGER_NULL;
969 new_thread->t_deduct_bank_ledger_time = 0;
970 #endif
971
972 new_thread->t_ledger = new_thread->task->ledger;
973 if (new_thread->t_ledger)
974 ledger_reference(new_thread->t_ledger);
975
976 #if defined(CONFIG_SCHED_MULTIQ)
977 /* Cache the task's sched_group */
978 new_thread->sched_group = parent_task->sched_group;
979 #endif /* defined(CONFIG_SCHED_MULTIQ) */
980
981 /* Cache the task's map */
982 new_thread->map = parent_task->map;
983
984 timer_call_setup(&new_thread->wait_timer, thread_timer_expire, new_thread);
985 timer_call_setup(&new_thread->depress_timer, thread_depress_expire, new_thread);
986
987 #if KPC
988 kpc_thread_create(new_thread);
989 #endif
990
991 /* Only need to update policies pushed from task to thread */
992 new_thread->requested_policy.bg_iotier = parent_task->effective_policy.bg_iotier;
993 new_thread->requested_policy.terminated = parent_task->effective_policy.terminated;
994
995 /* Set the thread's scheduling parameters */
996 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
997 new_thread->sched_stamp = sched_tick;
998 new_thread->pri_shift = sched_pri_shift;
999 #endif /* defined(CONFIG_SCHED_TIMESHARE_CORE) */
1000
1001 new_thread->sched_mode = SCHED(initial_thread_sched_mode)(parent_task);
1002 new_thread->sched_flags = 0;
1003 new_thread->max_priority = parent_task->max_priority;
1004 new_thread->task_priority = parent_task->priority;
1005
1006 int new_priority = (priority < 0) ? parent_task->priority: priority;
1007 new_priority = (priority < 0)? parent_task->priority: priority;
1008 if (new_priority > new_thread->max_priority)
1009 new_priority = new_thread->max_priority;
1010
1011 new_thread->importance = new_priority - new_thread->task_priority;
1012 new_thread->saved_importance = new_thread->importance;
1013
1014 if (parent_task->max_priority <= MAXPRI_THROTTLE) {
1015 sched_set_thread_throttled(new_thread, TRUE);
1016 }
1017
1018 sched_set_thread_base_priority(new_thread, new_priority);
1019
1020 thread_policy_create(new_thread);
1021
1022 /* Chain the thread onto the task's list */
1023 queue_enter(&parent_task->threads, new_thread, thread_t, task_threads);
1024 parent_task->thread_count++;
1025
1026 /* So terminating threads don't need to take the task lock to decrement */
1027 hw_atomic_add(&parent_task->active_thread_count, 1);
1028
1029 /* Protected by the tasks_threads_lock */
1030 new_thread->thread_id = ++thread_unique_id;
1031
1032 queue_enter(&threads, new_thread, thread_t, threads);
1033 threads_count++;
1034
1035 new_thread->active = TRUE;
1036 new_thread->inspection = FALSE;
1037 *out_thread = new_thread;
1038
1039 {
1040 long dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4;
1041
1042 kdbg_trace_data(parent_task->bsd_info, &dbg_arg2);
1043
1044 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1045 TRACEDBG_CODE(DBG_TRACE_DATA, 1) | DBG_FUNC_NONE,
1046 (vm_address_t)(uintptr_t)thread_tid(new_thread), dbg_arg2, 0, 0, 0);
1047
1048 kdbg_trace_string(parent_task->bsd_info,
1049 &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4);
1050
1051 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
1052 TRACEDBG_CODE(DBG_TRACE_STRING, 1) | DBG_FUNC_NONE,
1053 dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0);
1054 }
1055
1056 DTRACE_PROC1(lwp__create, thread_t, *out_thread);
1057
1058 return (KERN_SUCCESS);
1059 }
1060
1061 static kern_return_t
1062 thread_create_internal2(
1063 task_t task,
1064 thread_t *new_thread,
1065 boolean_t from_user,
1066 thread_continue_t continuation)
1067 {
1068 kern_return_t result;
1069 thread_t thread;
1070
1071 if (task == TASK_NULL || task == kernel_task)
1072 return (KERN_INVALID_ARGUMENT);
1073
1074 result = thread_create_internal(task, -1, continuation, TH_OPTION_NONE, &thread);
1075 if (result != KERN_SUCCESS)
1076 return (result);
1077
1078 thread->user_stop_count = 1;
1079 thread_hold(thread);
1080 if (task->suspend_count > 0)
1081 thread_hold(thread);
1082
1083 if (from_user)
1084 extmod_statistics_incr_thread_create(task);
1085
1086 task_unlock(task);
1087 lck_mtx_unlock(&tasks_threads_lock);
1088
1089 *new_thread = thread;
1090
1091 return (KERN_SUCCESS);
1092 }
1093
1094 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
1095 kern_return_t
1096 thread_create(
1097 task_t task,
1098 thread_t *new_thread);
1099
1100 kern_return_t
1101 thread_create(
1102 task_t task,
1103 thread_t *new_thread)
1104 {
1105 return thread_create_internal2(task, new_thread, FALSE, (thread_continue_t)thread_bootstrap_return);
1106 }
1107
1108 kern_return_t
1109 thread_create_from_user(
1110 task_t task,
1111 thread_t *new_thread)
1112 {
1113 return thread_create_internal2(task, new_thread, TRUE, (thread_continue_t)thread_bootstrap_return);
1114 }
1115
1116 kern_return_t
1117 thread_create_with_continuation(
1118 task_t task,
1119 thread_t *new_thread,
1120 thread_continue_t continuation)
1121 {
1122 return thread_create_internal2(task, new_thread, FALSE, continuation);
1123 }
1124
1125 static kern_return_t
1126 thread_create_running_internal2(
1127 register task_t task,
1128 int flavor,
1129 thread_state_t new_state,
1130 mach_msg_type_number_t new_state_count,
1131 thread_t *new_thread,
1132 boolean_t from_user)
1133 {
1134 register kern_return_t result;
1135 thread_t thread;
1136
1137 if (task == TASK_NULL || task == kernel_task)
1138 return (KERN_INVALID_ARGUMENT);
1139
1140 result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
1141 if (result != KERN_SUCCESS)
1142 return (result);
1143
1144 result = machine_thread_set_state(thread, flavor, new_state, new_state_count);
1145 if (result != KERN_SUCCESS) {
1146 task_unlock(task);
1147 lck_mtx_unlock(&tasks_threads_lock);
1148
1149 thread_terminate(thread);
1150 thread_deallocate(thread);
1151 return (result);
1152 }
1153
1154 thread_mtx_lock(thread);
1155 thread_start_internal(thread);
1156 thread_mtx_unlock(thread);
1157
1158 if (from_user)
1159 extmod_statistics_incr_thread_create(task);
1160
1161 task_unlock(task);
1162 lck_mtx_unlock(&tasks_threads_lock);
1163
1164 *new_thread = thread;
1165
1166 return (result);
1167 }
1168
1169 /* Prototype, see justification above */
1170 kern_return_t
1171 thread_create_running(
1172 register task_t task,
1173 int flavor,
1174 thread_state_t new_state,
1175 mach_msg_type_number_t new_state_count,
1176 thread_t *new_thread);
1177
1178 kern_return_t
1179 thread_create_running(
1180 register task_t task,
1181 int flavor,
1182 thread_state_t new_state,
1183 mach_msg_type_number_t new_state_count,
1184 thread_t *new_thread)
1185 {
1186 return thread_create_running_internal2(
1187 task, flavor, new_state, new_state_count,
1188 new_thread, FALSE);
1189 }
1190
1191 kern_return_t
1192 thread_create_running_from_user(
1193 register task_t task,
1194 int flavor,
1195 thread_state_t new_state,
1196 mach_msg_type_number_t new_state_count,
1197 thread_t *new_thread)
1198 {
1199 return thread_create_running_internal2(
1200 task, flavor, new_state, new_state_count,
1201 new_thread, TRUE);
1202 }
1203
1204 kern_return_t
1205 thread_create_workq(
1206 task_t task,
1207 thread_continue_t thread_return,
1208 thread_t *new_thread)
1209 {
1210 kern_return_t result;
1211 thread_t thread;
1212
1213 if (task == TASK_NULL || task == kernel_task)
1214 return (KERN_INVALID_ARGUMENT);
1215
1216 result = thread_create_internal(task, -1, thread_return, TH_OPTION_NOCRED | TH_OPTION_NOSUSP, &thread);
1217 if (result != KERN_SUCCESS)
1218 return (result);
1219
1220 thread->user_stop_count = 1;
1221 thread_hold(thread);
1222 if (task->suspend_count > 0)
1223 thread_hold(thread);
1224
1225 task_unlock(task);
1226 lck_mtx_unlock(&tasks_threads_lock);
1227
1228 *new_thread = thread;
1229
1230 return (KERN_SUCCESS);
1231 }
1232
1233 /*
1234 * kernel_thread_create:
1235 *
1236 * Create a thread in the kernel task
1237 * to execute in kernel context.
1238 */
1239 kern_return_t
1240 kernel_thread_create(
1241 thread_continue_t continuation,
1242 void *parameter,
1243 integer_t priority,
1244 thread_t *new_thread)
1245 {
1246 kern_return_t result;
1247 thread_t thread;
1248 task_t task = kernel_task;
1249
1250 result = thread_create_internal(task, priority, continuation, TH_OPTION_NONE, &thread);
1251 if (result != KERN_SUCCESS)
1252 return (result);
1253
1254 task_unlock(task);
1255 lck_mtx_unlock(&tasks_threads_lock);
1256
1257 stack_alloc(thread);
1258 assert(thread->kernel_stack != 0);
1259 thread->reserved_stack = thread->kernel_stack;
1260
1261 thread->parameter = parameter;
1262
1263 if(debug_task & 1)
1264 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread, continuation);
1265 *new_thread = thread;
1266
1267 return (result);
1268 }
1269
1270 kern_return_t
1271 kernel_thread_start_priority(
1272 thread_continue_t continuation,
1273 void *parameter,
1274 integer_t priority,
1275 thread_t *new_thread)
1276 {
1277 kern_return_t result;
1278 thread_t thread;
1279
1280 result = kernel_thread_create(continuation, parameter, priority, &thread);
1281 if (result != KERN_SUCCESS)
1282 return (result);
1283
1284 *new_thread = thread;
1285
1286 thread_mtx_lock(thread);
1287 thread_start_internal(thread);
1288 thread_mtx_unlock(thread);
1289
1290 return (result);
1291 }
1292
1293 kern_return_t
1294 kernel_thread_start(
1295 thread_continue_t continuation,
1296 void *parameter,
1297 thread_t *new_thread)
1298 {
1299 return kernel_thread_start_priority(continuation, parameter, -1, new_thread);
1300 }
1301
1302 /* Separated into helper function so it can be used by THREAD_BASIC_INFO and THREAD_EXTENDED_INFO */
1303 /* it is assumed that the thread is locked by the caller */
1304 static void
1305 retrieve_thread_basic_info(thread_t thread, thread_basic_info_t basic_info)
1306 {
1307 int state, flags;
1308
1309 /* fill in info */
1310
1311 thread_read_times(thread, &basic_info->user_time,
1312 &basic_info->system_time);
1313
1314 /*
1315 * Update lazy-evaluated scheduler info because someone wants it.
1316 */
1317 if (SCHED(can_update_priority)(thread))
1318 SCHED(update_priority)(thread);
1319
1320 basic_info->sleep_time = 0;
1321
1322 /*
1323 * To calculate cpu_usage, first correct for timer rate,
1324 * then for 5/8 ageing. The correction factor [3/5] is
1325 * (1/(5/8) - 1).
1326 */
1327 basic_info->cpu_usage = 0;
1328 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
1329 if (sched_tick_interval) {
1330 basic_info->cpu_usage = (integer_t)(((uint64_t)thread->cpu_usage
1331 * TH_USAGE_SCALE) / sched_tick_interval);
1332 basic_info->cpu_usage = (basic_info->cpu_usage * 3) / 5;
1333 }
1334 #endif
1335
1336 if (basic_info->cpu_usage > TH_USAGE_SCALE)
1337 basic_info->cpu_usage = TH_USAGE_SCALE;
1338
1339 basic_info->policy = ((thread->sched_mode == TH_MODE_TIMESHARE)?
1340 POLICY_TIMESHARE: POLICY_RR);
1341
1342 flags = 0;
1343 if (thread->options & TH_OPT_IDLE_THREAD)
1344 flags |= TH_FLAGS_IDLE;
1345
1346 if (thread->options & TH_OPT_GLOBAL_FORCED_IDLE) {
1347 flags |= TH_FLAGS_GLOBAL_FORCED_IDLE;
1348 }
1349
1350 if (!thread->kernel_stack)
1351 flags |= TH_FLAGS_SWAPPED;
1352
1353 state = 0;
1354 if (thread->state & TH_TERMINATE)
1355 state = TH_STATE_HALTED;
1356 else
1357 if (thread->state & TH_RUN)
1358 state = TH_STATE_RUNNING;
1359 else
1360 if (thread->state & TH_UNINT)
1361 state = TH_STATE_UNINTERRUPTIBLE;
1362 else
1363 if (thread->state & TH_SUSP)
1364 state = TH_STATE_STOPPED;
1365 else
1366 if (thread->state & TH_WAIT)
1367 state = TH_STATE_WAITING;
1368
1369 basic_info->run_state = state;
1370 basic_info->flags = flags;
1371
1372 basic_info->suspend_count = thread->user_stop_count;
1373
1374 return;
1375 }
1376
1377 kern_return_t
1378 thread_info_internal(
1379 register thread_t thread,
1380 thread_flavor_t flavor,
1381 thread_info_t thread_info_out, /* ptr to OUT array */
1382 mach_msg_type_number_t *thread_info_count) /*IN/OUT*/
1383 {
1384 spl_t s;
1385
1386 if (thread == THREAD_NULL)
1387 return (KERN_INVALID_ARGUMENT);
1388
1389 if (flavor == THREAD_BASIC_INFO) {
1390
1391 if (*thread_info_count < THREAD_BASIC_INFO_COUNT)
1392 return (KERN_INVALID_ARGUMENT);
1393
1394 s = splsched();
1395 thread_lock(thread);
1396
1397 retrieve_thread_basic_info(thread, (thread_basic_info_t) thread_info_out);
1398
1399 thread_unlock(thread);
1400 splx(s);
1401
1402 *thread_info_count = THREAD_BASIC_INFO_COUNT;
1403
1404 return (KERN_SUCCESS);
1405 }
1406 else
1407 if (flavor == THREAD_IDENTIFIER_INFO) {
1408 register thread_identifier_info_t identifier_info;
1409
1410 if (*thread_info_count < THREAD_IDENTIFIER_INFO_COUNT)
1411 return (KERN_INVALID_ARGUMENT);
1412
1413 identifier_info = (thread_identifier_info_t) thread_info_out;
1414
1415 s = splsched();
1416 thread_lock(thread);
1417
1418 identifier_info->thread_id = thread->thread_id;
1419 identifier_info->thread_handle = thread->machine.cthread_self;
1420 identifier_info->dispatch_qaddr = thread_dispatchqaddr(thread);
1421
1422 thread_unlock(thread);
1423 splx(s);
1424 return KERN_SUCCESS;
1425 }
1426 else
1427 if (flavor == THREAD_SCHED_TIMESHARE_INFO) {
1428 policy_timeshare_info_t ts_info;
1429
1430 if (*thread_info_count < POLICY_TIMESHARE_INFO_COUNT)
1431 return (KERN_INVALID_ARGUMENT);
1432
1433 ts_info = (policy_timeshare_info_t)thread_info_out;
1434
1435 s = splsched();
1436 thread_lock(thread);
1437
1438 if (thread->sched_mode != TH_MODE_TIMESHARE) {
1439 thread_unlock(thread);
1440 splx(s);
1441 return (KERN_INVALID_POLICY);
1442 }
1443
1444 ts_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
1445 if (ts_info->depressed) {
1446 ts_info->base_priority = DEPRESSPRI;
1447 ts_info->depress_priority = thread->base_pri;
1448 }
1449 else {
1450 ts_info->base_priority = thread->base_pri;
1451 ts_info->depress_priority = -1;
1452 }
1453
1454 ts_info->cur_priority = thread->sched_pri;
1455 ts_info->max_priority = thread->max_priority;
1456
1457 thread_unlock(thread);
1458 splx(s);
1459
1460 *thread_info_count = POLICY_TIMESHARE_INFO_COUNT;
1461
1462 return (KERN_SUCCESS);
1463 }
1464 else
1465 if (flavor == THREAD_SCHED_FIFO_INFO) {
1466 if (*thread_info_count < POLICY_FIFO_INFO_COUNT)
1467 return (KERN_INVALID_ARGUMENT);
1468
1469 return (KERN_INVALID_POLICY);
1470 }
1471 else
1472 if (flavor == THREAD_SCHED_RR_INFO) {
1473 policy_rr_info_t rr_info;
1474 uint32_t quantum_time;
1475 uint64_t quantum_ns;
1476
1477 if (*thread_info_count < POLICY_RR_INFO_COUNT)
1478 return (KERN_INVALID_ARGUMENT);
1479
1480 rr_info = (policy_rr_info_t) thread_info_out;
1481
1482 s = splsched();
1483 thread_lock(thread);
1484
1485 if (thread->sched_mode == TH_MODE_TIMESHARE) {
1486 thread_unlock(thread);
1487 splx(s);
1488
1489 return (KERN_INVALID_POLICY);
1490 }
1491
1492 rr_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
1493 if (rr_info->depressed) {
1494 rr_info->base_priority = DEPRESSPRI;
1495 rr_info->depress_priority = thread->base_pri;
1496 }
1497 else {
1498 rr_info->base_priority = thread->base_pri;
1499 rr_info->depress_priority = -1;
1500 }
1501
1502 quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
1503 absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
1504
1505 rr_info->max_priority = thread->max_priority;
1506 rr_info->quantum = (uint32_t)(quantum_ns / 1000 / 1000);
1507
1508 thread_unlock(thread);
1509 splx(s);
1510
1511 *thread_info_count = POLICY_RR_INFO_COUNT;
1512
1513 return (KERN_SUCCESS);
1514 }
1515 else
1516 if (flavor == THREAD_EXTENDED_INFO) {
1517 thread_basic_info_data_t basic_info;
1518 thread_extended_info_t extended_info = (thread_extended_info_t) thread_info_out;
1519
1520 if (*thread_info_count < THREAD_EXTENDED_INFO_COUNT) {
1521 return (KERN_INVALID_ARGUMENT);
1522 }
1523
1524 s = splsched();
1525 thread_lock(thread);
1526
1527 /* NOTE: This mimics fill_taskthreadinfo(), which is the function used by proc_pidinfo() for
1528 * the PROC_PIDTHREADINFO flavor (which can't be used on corpses)
1529 */
1530 retrieve_thread_basic_info(thread, &basic_info);
1531 extended_info->pth_user_time = ((basic_info.user_time.seconds * (integer_t)NSEC_PER_SEC) + (basic_info.user_time.microseconds * (integer_t)NSEC_PER_USEC));
1532 extended_info->pth_system_time = ((basic_info.system_time.seconds * (integer_t)NSEC_PER_SEC) + (basic_info.system_time.microseconds * (integer_t)NSEC_PER_USEC));
1533
1534 extended_info->pth_cpu_usage = basic_info.cpu_usage;
1535 extended_info->pth_policy = basic_info.policy;
1536 extended_info->pth_run_state = basic_info.run_state;
1537 extended_info->pth_flags = basic_info.flags;
1538 extended_info->pth_sleep_time = basic_info.sleep_time;
1539 extended_info->pth_curpri = thread->sched_pri;
1540 extended_info->pth_priority = thread->base_pri;
1541 extended_info->pth_maxpriority = thread->max_priority;
1542
1543 bsd_getthreadname(thread->uthread,extended_info->pth_name);
1544
1545 thread_unlock(thread);
1546 splx(s);
1547
1548 *thread_info_count = THREAD_EXTENDED_INFO_COUNT;
1549
1550 return (KERN_SUCCESS);
1551 }
1552 else
1553 if (flavor == THREAD_DEBUG_INFO_INTERNAL) {
1554 #if DEVELOPMENT || DEBUG
1555 thread_debug_info_internal_t dbg_info;
1556 if (*thread_info_count < THREAD_DEBUG_INFO_INTERNAL_COUNT)
1557 return (KERN_NOT_SUPPORTED);
1558
1559 if (thread_info_out == NULL)
1560 return (KERN_INVALID_ARGUMENT);
1561
1562 dbg_info = (thread_debug_info_internal_t) thread_info_out;
1563 dbg_info->page_creation_count = thread->t_page_creation_count;
1564
1565 *thread_info_count = THREAD_DEBUG_INFO_INTERNAL_COUNT;
1566 return (KERN_SUCCESS);
1567 #endif /* DEVELOPMENT || DEBUG */
1568 return (KERN_NOT_SUPPORTED);
1569 }
1570
1571 return (KERN_INVALID_ARGUMENT);
1572 }
1573
1574 void
1575 thread_read_times(
1576 thread_t thread,
1577 time_value_t *user_time,
1578 time_value_t *system_time)
1579 {
1580 clock_sec_t secs;
1581 clock_usec_t usecs;
1582 uint64_t tval_user, tval_system;
1583
1584 tval_user = timer_grab(&thread->user_timer);
1585 tval_system = timer_grab(&thread->system_timer);
1586
1587 if (thread->precise_user_kernel_time) {
1588 absolutetime_to_microtime(tval_user, &secs, &usecs);
1589 user_time->seconds = (typeof(user_time->seconds))secs;
1590 user_time->microseconds = usecs;
1591
1592 absolutetime_to_microtime(tval_system, &secs, &usecs);
1593 system_time->seconds = (typeof(system_time->seconds))secs;
1594 system_time->microseconds = usecs;
1595 } else {
1596 /* system_timer may represent either sys or user */
1597 tval_user += tval_system;
1598 absolutetime_to_microtime(tval_user, &secs, &usecs);
1599 user_time->seconds = (typeof(user_time->seconds))secs;
1600 user_time->microseconds = usecs;
1601
1602 system_time->seconds = 0;
1603 system_time->microseconds = 0;
1604 }
1605 }
1606
1607 uint64_t thread_get_runtime_self(void)
1608 {
1609 boolean_t interrupt_state;
1610 uint64_t runtime;
1611 thread_t thread = NULL;
1612 processor_t processor = NULL;
1613
1614 thread = current_thread();
1615
1616 /* Not interrupt safe, as the scheduler may otherwise update timer values underneath us */
1617 interrupt_state = ml_set_interrupts_enabled(FALSE);
1618 processor = current_processor();
1619 timer_switch(PROCESSOR_DATA(processor, thread_timer), mach_absolute_time(), PROCESSOR_DATA(processor, thread_timer));
1620 runtime = (timer_grab(&thread->user_timer) + timer_grab(&thread->system_timer));
1621 ml_set_interrupts_enabled(interrupt_state);
1622
1623 return runtime;
1624 }
1625
1626 kern_return_t
1627 thread_assign(
1628 __unused thread_t thread,
1629 __unused processor_set_t new_pset)
1630 {
1631 return (KERN_FAILURE);
1632 }
1633
1634 /*
1635 * thread_assign_default:
1636 *
1637 * Special version of thread_assign for assigning threads to default
1638 * processor set.
1639 */
1640 kern_return_t
1641 thread_assign_default(
1642 thread_t thread)
1643 {
1644 return (thread_assign(thread, &pset0));
1645 }
1646
1647 /*
1648 * thread_get_assignment
1649 *
1650 * Return current assignment for this thread.
1651 */
1652 kern_return_t
1653 thread_get_assignment(
1654 thread_t thread,
1655 processor_set_t *pset)
1656 {
1657 if (thread == NULL)
1658 return (KERN_INVALID_ARGUMENT);
1659
1660 *pset = &pset0;
1661
1662 return (KERN_SUCCESS);
1663 }
1664
1665 /*
1666 * thread_wire_internal:
1667 *
1668 * Specify that the target thread must always be able
1669 * to run and to allocate memory.
1670 */
1671 kern_return_t
1672 thread_wire_internal(
1673 host_priv_t host_priv,
1674 thread_t thread,
1675 boolean_t wired,
1676 boolean_t *prev_state)
1677 {
1678 if (host_priv == NULL || thread != current_thread())
1679 return (KERN_INVALID_ARGUMENT);
1680
1681 assert(host_priv == &realhost);
1682
1683 if (prev_state)
1684 *prev_state = (thread->options & TH_OPT_VMPRIV) != 0;
1685
1686 if (wired) {
1687 if (!(thread->options & TH_OPT_VMPRIV))
1688 vm_page_free_reserve(1); /* XXX */
1689 thread->options |= TH_OPT_VMPRIV;
1690 }
1691 else {
1692 if (thread->options & TH_OPT_VMPRIV)
1693 vm_page_free_reserve(-1); /* XXX */
1694 thread->options &= ~TH_OPT_VMPRIV;
1695 }
1696
1697 return (KERN_SUCCESS);
1698 }
1699
1700
1701 /*
1702 * thread_wire:
1703 *
1704 * User-api wrapper for thread_wire_internal()
1705 */
1706 kern_return_t
1707 thread_wire(
1708 host_priv_t host_priv,
1709 thread_t thread,
1710 boolean_t wired)
1711 {
1712 return (thread_wire_internal(host_priv, thread, wired, NULL));
1713 }
1714
1715
1716 boolean_t
1717 set_vm_privilege(boolean_t privileged)
1718 {
1719 boolean_t was_vmpriv;
1720
1721 if (current_thread()->options & TH_OPT_VMPRIV)
1722 was_vmpriv = TRUE;
1723 else
1724 was_vmpriv = FALSE;
1725
1726 if (privileged != FALSE)
1727 current_thread()->options |= TH_OPT_VMPRIV;
1728 else
1729 current_thread()->options &= ~TH_OPT_VMPRIV;
1730
1731 return (was_vmpriv);
1732 }
1733
1734
1735 /*
1736 * XXX assuming current thread only, for now...
1737 */
1738 void
1739 thread_guard_violation(thread_t thread, unsigned type)
1740 {
1741 assert(thread == current_thread());
1742
1743 spl_t s = splsched();
1744 /*
1745 * Use the saved state area of the thread structure
1746 * to store all info required to handle the AST when
1747 * returning to userspace
1748 */
1749 thread->guard_exc_info.type = type;
1750 thread_ast_set(thread, AST_GUARD);
1751 ast_propagate(thread->ast);
1752
1753 splx(s);
1754 }
1755
1756 /*
1757 * guard_ast:
1758 *
1759 * Handle AST_GUARD for a thread. This routine looks at the
1760 * state saved in the thread structure to determine the cause
1761 * of this exception. Based on this value, it invokes the
1762 * appropriate routine which determines other exception related
1763 * info and raises the exception.
1764 */
1765 void
1766 guard_ast(thread_t thread)
1767 {
1768 if (thread->guard_exc_info.type == GUARD_TYPE_MACH_PORT)
1769 mach_port_guard_ast(thread);
1770 else
1771 fd_guard_ast(thread);
1772 }
1773
1774 static void
1775 thread_cputime_callback(int warning, __unused const void *arg0, __unused const void *arg1)
1776 {
1777 if (warning == LEDGER_WARNING_ROSE_ABOVE) {
1778 #if CONFIG_TELEMETRY
1779 /*
1780 * This thread is in danger of violating the CPU usage monitor. Enable telemetry
1781 * on the entire task so there are micro-stackshots available if and when
1782 * EXC_RESOURCE is triggered. We could have chosen to enable micro-stackshots
1783 * for this thread only; but now that this task is suspect, knowing what all of
1784 * its threads are up to will be useful.
1785 */
1786 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING, 1);
1787 #endif
1788 return;
1789 }
1790
1791 #if CONFIG_TELEMETRY
1792 /*
1793 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
1794 * exceeded the limit, turn telemetry off for the task.
1795 */
1796 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING, 0);
1797 #endif
1798
1799 if (warning == 0) {
1800 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE();
1801 }
1802 }
1803
1804 void __attribute__((noinline))
1805 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void)
1806 {
1807 int pid = 0;
1808 task_t task = current_task();
1809 thread_t thread = current_thread();
1810 uint64_t tid = thread->thread_id;
1811 const char *procname = "unknown";
1812 time_value_t thread_total_time = {0, 0};
1813 time_value_t thread_system_time;
1814 time_value_t thread_user_time;
1815 int action;
1816 uint8_t percentage;
1817 uint32_t limit_percent;
1818 uint32_t usage_percent;
1819 uint32_t interval_sec;
1820 uint64_t interval_ns;
1821 uint64_t balance_ns;
1822 boolean_t fatal = FALSE;
1823
1824 mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
1825 struct ledger_entry_info lei;
1826
1827 assert(thread->t_threadledger != LEDGER_NULL);
1828
1829 /*
1830 * Now that a thread has tripped the monitor, disable it for the entire task.
1831 */
1832 task_lock(task);
1833 if ((task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) == 0) {
1834 /*
1835 * The CPU usage monitor has been disabled on our task, so some other
1836 * thread must have gotten here first. We only send one exception per
1837 * task lifetime, so there's nothing left for us to do here.
1838 */
1839 task_unlock(task);
1840 return;
1841 }
1842 if (task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_FATAL_CPUMON) {
1843 fatal = TRUE;
1844 }
1845 task_disable_cpumon(task);
1846 task_unlock(task);
1847
1848 #ifdef MACH_BSD
1849 pid = proc_selfpid();
1850 if (task->bsd_info != NULL)
1851 procname = proc_name_address(task->bsd_info);
1852 #endif
1853
1854 thread_get_cpulimit(&action, &percentage, &interval_ns);
1855
1856 interval_sec = (uint32_t)(interval_ns / NSEC_PER_SEC);
1857
1858 thread_read_times(thread, &thread_user_time, &thread_system_time);
1859 time_value_add(&thread_total_time, &thread_user_time);
1860 time_value_add(&thread_total_time, &thread_system_time);
1861
1862 ledger_get_entry_info(thread->t_threadledger, thread_ledgers.cpu_time, &lei);
1863
1864 absolutetime_to_nanoseconds(lei.lei_balance, &balance_ns);
1865 usage_percent = (uint32_t) ((balance_ns * 100ULL) / lei.lei_last_refill);
1866
1867 /* Show refill period in the same units as balance, limit, etc */
1868 nanoseconds_to_absolutetime(lei.lei_refill_period, &lei.lei_refill_period);
1869
1870 limit_percent = (uint32_t) ((lei.lei_limit * 100ULL) / lei.lei_refill_period);
1871
1872 /* TODO: show task total runtime as well? see TASK_ABSOLUTETIME_INFO */
1873
1874 if (disable_exc_resource) {
1875 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1876 "supressed by a boot-arg\n", procname, pid, tid);
1877 return;
1878 }
1879
1880 if (audio_active) {
1881 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1882 "supressed due to audio playback\n", procname, pid, tid);
1883 return;
1884 }
1885 printf("process %s[%d] thread %llu caught burning CPU! "
1886 "It used more than %d%% CPU (Actual recent usage: %d%%) over %d seconds. "
1887 "thread lifetime cpu usage %d.%06d seconds, (%d.%06d user, %d.%06d system) "
1888 "ledger info: balance: %lld credit: %lld debit: %lld limit: %llu (%d%%) "
1889 "period: %llu time since last refill (ns): %llu %s\n",
1890 procname, pid, tid,
1891 percentage, usage_percent, interval_sec,
1892 thread_total_time.seconds, thread_total_time.microseconds,
1893 thread_user_time.seconds, thread_user_time.microseconds,
1894 thread_system_time.seconds, thread_system_time.microseconds,
1895 lei.lei_balance,
1896 lei.lei_credit, lei.lei_debit,
1897 lei.lei_limit, limit_percent,
1898 lei.lei_refill_period, lei.lei_last_refill,
1899 (fatal ? "[fatal violation]" : ""));
1900
1901
1902 code[0] = code[1] = 0;
1903 EXC_RESOURCE_ENCODE_TYPE(code[0], RESOURCE_TYPE_CPU);
1904 if (fatal) {
1905 EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_CPU_MONITOR_FATAL);
1906 }else {
1907 EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_CPU_MONITOR);
1908 }
1909 EXC_RESOURCE_CPUMONITOR_ENCODE_INTERVAL(code[0], interval_sec);
1910 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code[0], limit_percent);
1911 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code[1], usage_percent);
1912 exception_triage(EXC_RESOURCE, code, EXCEPTION_CODE_MAX);
1913
1914 if (fatal) {
1915 #if CONFIG_JETSAM
1916 jetsam_on_ledger_cpulimit_exceeded();
1917 #else
1918 task_terminate_internal(task);
1919 #endif
1920 }
1921 }
1922
1923 #define UPDATE_IO_STATS(info, size) \
1924 { \
1925 info.count++; \
1926 info.size += size; \
1927 }
1928
1929 #define UPDATE_IO_STATS_ATOMIC(info, size) \
1930 { \
1931 OSIncrementAtomic64((SInt64 *)&(info.count)); \
1932 OSAddAtomic64(size, (SInt64 *)&(info.size)); \
1933 }
1934
1935 void thread_update_io_stats(thread_t thread, int size, int io_flags)
1936 {
1937 int io_tier;
1938
1939 if (thread->thread_io_stats == NULL || thread->task->task_io_stats == NULL)
1940 return;
1941
1942 if (io_flags & DKIO_READ) {
1943 UPDATE_IO_STATS(thread->thread_io_stats->disk_reads, size);
1944 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->disk_reads, size);
1945 }
1946
1947 if (io_flags & DKIO_META) {
1948 UPDATE_IO_STATS(thread->thread_io_stats->metadata, size);
1949 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->metadata, size);
1950 }
1951
1952 if (io_flags & DKIO_PAGING) {
1953 UPDATE_IO_STATS(thread->thread_io_stats->paging, size);
1954 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->paging, size);
1955 }
1956
1957 io_tier = ((io_flags & DKIO_TIER_MASK) >> DKIO_TIER_SHIFT);
1958 assert (io_tier < IO_NUM_PRIORITIES);
1959
1960 UPDATE_IO_STATS(thread->thread_io_stats->io_priority[io_tier], size);
1961 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->io_priority[io_tier], size);
1962
1963 /* Update Total I/O Counts */
1964 UPDATE_IO_STATS(thread->thread_io_stats->total_io, size);
1965 UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->total_io, size);
1966
1967 }
1968
1969 void
1970 init_thread_ledgers(void) {
1971 ledger_template_t t;
1972 int idx;
1973
1974 assert(thread_ledger_template == NULL);
1975
1976 if ((t = ledger_template_create("Per-thread ledger")) == NULL)
1977 panic("couldn't create thread ledger template");
1978
1979 if ((idx = ledger_entry_add(t, "cpu_time", "sched", "ns")) < 0) {
1980 panic("couldn't create cpu_time entry for thread ledger template");
1981 }
1982
1983 if (ledger_set_callback(t, idx, thread_cputime_callback, NULL, NULL) < 0) {
1984 panic("couldn't set thread ledger callback for cpu_time entry");
1985 }
1986
1987 thread_ledgers.cpu_time = idx;
1988
1989 thread_ledger_template = t;
1990 }
1991
1992 /*
1993 * Returns currently applied CPU usage limit, or 0/0 if none is applied.
1994 */
1995 int
1996 thread_get_cpulimit(int *action, uint8_t *percentage, uint64_t *interval_ns)
1997 {
1998 int64_t abstime = 0;
1999 uint64_t limittime = 0;
2000 thread_t thread = current_thread();
2001
2002 *percentage = 0;
2003 *interval_ns = 0;
2004 *action = 0;
2005
2006 if (thread->t_threadledger == LEDGER_NULL) {
2007 /*
2008 * This thread has no per-thread ledger, so it can't possibly
2009 * have a CPU limit applied.
2010 */
2011 return (KERN_SUCCESS);
2012 }
2013
2014 ledger_get_period(thread->t_threadledger, thread_ledgers.cpu_time, interval_ns);
2015 ledger_get_limit(thread->t_threadledger, thread_ledgers.cpu_time, &abstime);
2016
2017 if ((abstime == LEDGER_LIMIT_INFINITY) || (*interval_ns == 0)) {
2018 /*
2019 * This thread's CPU time ledger has no period or limit; so it
2020 * doesn't have a CPU limit applied.
2021 */
2022 return (KERN_SUCCESS);
2023 }
2024
2025 /*
2026 * This calculation is the converse to the one in thread_set_cpulimit().
2027 */
2028 absolutetime_to_nanoseconds(abstime, &limittime);
2029 *percentage = (limittime * 100ULL) / *interval_ns;
2030 assert(*percentage <= 100);
2031
2032 if (thread->options & TH_OPT_PROC_CPULIMIT) {
2033 assert((thread->options & TH_OPT_PRVT_CPULIMIT) == 0);
2034
2035 *action = THREAD_CPULIMIT_BLOCK;
2036 } else if (thread->options & TH_OPT_PRVT_CPULIMIT) {
2037 assert((thread->options & TH_OPT_PROC_CPULIMIT) == 0);
2038
2039 *action = THREAD_CPULIMIT_EXCEPTION;
2040 } else {
2041 *action = THREAD_CPULIMIT_DISABLE;
2042 }
2043
2044 return (KERN_SUCCESS);
2045 }
2046
2047 /*
2048 * Set CPU usage limit on a thread.
2049 *
2050 * Calling with percentage of 0 will unset the limit for this thread.
2051 */
2052 int
2053 thread_set_cpulimit(int action, uint8_t percentage, uint64_t interval_ns)
2054 {
2055 thread_t thread = current_thread();
2056 ledger_t l;
2057 uint64_t limittime = 0;
2058 uint64_t abstime = 0;
2059
2060 assert(percentage <= 100);
2061
2062 if (action == THREAD_CPULIMIT_DISABLE) {
2063 /*
2064 * Remove CPU limit, if any exists.
2065 */
2066 if (thread->t_threadledger != LEDGER_NULL) {
2067 l = thread->t_threadledger;
2068 ledger_set_limit(l, thread_ledgers.cpu_time, LEDGER_LIMIT_INFINITY, 0);
2069 ledger_set_action(l, thread_ledgers.cpu_time, LEDGER_ACTION_IGNORE);
2070 thread->options &= ~(TH_OPT_PROC_CPULIMIT | TH_OPT_PRVT_CPULIMIT);
2071 }
2072
2073 return (0);
2074 }
2075
2076 if (interval_ns < MINIMUM_CPULIMIT_INTERVAL_MS * NSEC_PER_MSEC) {
2077 return (KERN_INVALID_ARGUMENT);
2078 }
2079
2080 l = thread->t_threadledger;
2081 if (l == LEDGER_NULL) {
2082 /*
2083 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
2084 */
2085 if ((l = ledger_instantiate(thread_ledger_template, LEDGER_CREATE_INACTIVE_ENTRIES)) == LEDGER_NULL)
2086 return (KERN_RESOURCE_SHORTAGE);
2087
2088 /*
2089 * We are the first to create this thread's ledger, so only activate our entry.
2090 */
2091 ledger_entry_setactive(l, thread_ledgers.cpu_time);
2092 thread->t_threadledger = l;
2093 }
2094
2095 /*
2096 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
2097 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
2098 */
2099 limittime = (interval_ns * percentage) / 100;
2100 nanoseconds_to_absolutetime(limittime, &abstime);
2101 ledger_set_limit(l, thread_ledgers.cpu_time, abstime, cpumon_ustackshots_trigger_pct);
2102 /*
2103 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
2104 */
2105 ledger_set_period(l, thread_ledgers.cpu_time, interval_ns);
2106
2107 if (action == THREAD_CPULIMIT_EXCEPTION) {
2108 /*
2109 * We don't support programming the CPU usage monitor on a task if any of its
2110 * threads have a per-thread blocking CPU limit configured.
2111 */
2112 if (thread->options & TH_OPT_PRVT_CPULIMIT) {
2113 panic("CPU usage monitor activated, but blocking thread limit exists");
2114 }
2115
2116 /*
2117 * Make a note that this thread's CPU limit is being used for the task-wide CPU
2118 * usage monitor. We don't have to arm the callback which will trigger the
2119 * exception, because that was done for us in ledger_instantiate (because the
2120 * ledger template used has a default callback).
2121 */
2122 thread->options |= TH_OPT_PROC_CPULIMIT;
2123 } else {
2124 /*
2125 * We deliberately override any CPU limit imposed by a task-wide limit (eg
2126 * CPU usage monitor).
2127 */
2128 thread->options &= ~TH_OPT_PROC_CPULIMIT;
2129
2130 thread->options |= TH_OPT_PRVT_CPULIMIT;
2131 /* The per-thread ledger template by default has a callback for CPU time */
2132 ledger_disable_callback(l, thread_ledgers.cpu_time);
2133 ledger_set_action(l, thread_ledgers.cpu_time, LEDGER_ACTION_BLOCK);
2134 }
2135
2136 return (0);
2137 }
2138
2139 static void
2140 sched_call_null(
2141 __unused int type,
2142 __unused thread_t thread)
2143 {
2144 return;
2145 }
2146
2147 void
2148 thread_sched_call(
2149 thread_t thread,
2150 sched_call_t call)
2151 {
2152 thread->sched_call = (call != NULL)? call: sched_call_null;
2153 }
2154
2155 void
2156 thread_static_param(
2157 thread_t thread,
2158 boolean_t state)
2159 {
2160 thread_mtx_lock(thread);
2161 thread->static_param = state;
2162 thread_mtx_unlock(thread);
2163 }
2164
2165 uint64_t
2166 thread_tid(
2167 thread_t thread)
2168 {
2169 return (thread != THREAD_NULL? thread->thread_id: 0);
2170 }
2171
2172 uint16_t thread_set_tag(thread_t th, uint16_t tag) {
2173 return thread_set_tag_internal(th, tag);
2174 }
2175 uint16_t thread_get_tag(thread_t th) {
2176 return thread_get_tag_internal(th);
2177 }
2178
2179 uint64_t
2180 thread_dispatchqaddr(
2181 thread_t thread)
2182 {
2183 uint64_t dispatchqueue_addr = 0;
2184 uint64_t thread_handle = 0;
2185
2186 if (thread != THREAD_NULL) {
2187 thread_handle = thread->machine.cthread_self;
2188
2189 if (thread->inspection == TRUE)
2190 dispatchqueue_addr = thread_handle + get_task_dispatchqueue_offset(thread->task);
2191 else if (thread->task->bsd_info)
2192 dispatchqueue_addr = thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
2193 }
2194
2195 return (dispatchqueue_addr);
2196 }
2197
2198 /*
2199 * Export routines to other components for things that are done as macros
2200 * within the osfmk component.
2201 */
2202
2203 #undef thread_reference
2204 void thread_reference(thread_t thread);
2205 void
2206 thread_reference(
2207 thread_t thread)
2208 {
2209 if (thread != THREAD_NULL)
2210 thread_reference_internal(thread);
2211 }
2212
2213 #undef thread_should_halt
2214
2215 boolean_t
2216 thread_should_halt(
2217 thread_t th)
2218 {
2219 return (thread_should_halt_fast(th));
2220 }
2221
2222 /*
2223 * thread_set_voucher_name - reset the voucher port name bound to this thread
2224 *
2225 * Conditions: nothing locked
2226 *
2227 * If we already converted the previous name to a cached voucher
2228 * reference, then we discard that reference here. The next lookup
2229 * will cache it again.
2230 */
2231
2232 kern_return_t
2233 thread_set_voucher_name(mach_port_name_t voucher_name)
2234 {
2235 thread_t thread = current_thread();
2236 ipc_voucher_t new_voucher = IPC_VOUCHER_NULL;
2237 ipc_voucher_t voucher;
2238 #ifdef CONFIG_BANK
2239 ledger_t bankledger = NULL;
2240 #endif
2241
2242 if (MACH_PORT_DEAD == voucher_name)
2243 return KERN_INVALID_RIGHT;
2244
2245 /*
2246 * agressively convert to voucher reference
2247 */
2248 if (MACH_PORT_VALID(voucher_name)) {
2249 new_voucher = convert_port_name_to_voucher(voucher_name);
2250 if (IPC_VOUCHER_NULL == new_voucher)
2251 return KERN_INVALID_ARGUMENT;
2252 }
2253 #ifdef CONFIG_BANK
2254 bankledger = bank_get_voucher_ledger(new_voucher);
2255 #endif
2256
2257 thread_mtx_lock(thread);
2258 voucher = thread->ith_voucher;
2259 thread->ith_voucher_name = voucher_name;
2260 thread->ith_voucher = new_voucher;
2261 #ifdef CONFIG_BANK
2262 bank_swap_thread_bank_ledger(thread, bankledger);
2263 #endif
2264 thread_mtx_unlock(thread);
2265
2266 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2267 MACHDBG_CODE(DBG_MACH_IPC,MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2268 (uintptr_t)thread_tid(thread),
2269 (uintptr_t)voucher_name,
2270 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher),
2271 1, 0);
2272
2273 if (IPC_VOUCHER_NULL != voucher)
2274 ipc_voucher_release(voucher);
2275
2276 return KERN_SUCCESS;
2277 }
2278
2279 /*
2280 * thread_get_mach_voucher - return a voucher reference for the specified thread voucher
2281 *
2282 * Conditions: nothing locked
2283 *
2284 * A reference to the voucher may be lazily pending, if someone set the voucher name
2285 * but nobody has done a lookup yet. In that case, we'll have to do the equivalent
2286 * lookup here.
2287 *
2288 * NOTE: At the moment, there is no distinction between the current and effective
2289 * vouchers because we only set them at the thread level currently.
2290 */
2291 kern_return_t
2292 thread_get_mach_voucher(
2293 thread_act_t thread,
2294 mach_voucher_selector_t __unused which,
2295 ipc_voucher_t *voucherp)
2296 {
2297 ipc_voucher_t voucher;
2298 mach_port_name_t voucher_name;
2299
2300 if (THREAD_NULL == thread)
2301 return KERN_INVALID_ARGUMENT;
2302
2303 thread_mtx_lock(thread);
2304 voucher = thread->ith_voucher;
2305
2306 /* if already cached, just return a ref */
2307 if (IPC_VOUCHER_NULL != voucher) {
2308 ipc_voucher_reference(voucher);
2309 thread_mtx_unlock(thread);
2310 *voucherp = voucher;
2311 return KERN_SUCCESS;
2312 }
2313
2314 voucher_name = thread->ith_voucher_name;
2315
2316 /* convert the name to a port, then voucher reference */
2317 if (MACH_PORT_VALID(voucher_name)) {
2318 ipc_port_t port;
2319
2320 if (KERN_SUCCESS !=
2321 ipc_object_copyin(thread->task->itk_space, voucher_name,
2322 MACH_MSG_TYPE_COPY_SEND, (ipc_object_t *)&port)) {
2323 thread->ith_voucher_name = MACH_PORT_NULL;
2324 thread_mtx_unlock(thread);
2325 *voucherp = IPC_VOUCHER_NULL;
2326 return KERN_SUCCESS;
2327 }
2328
2329 /* convert to a voucher ref to return, and cache a ref on thread */
2330 voucher = convert_port_to_voucher(port);
2331 ipc_voucher_reference(voucher);
2332 thread->ith_voucher = voucher;
2333 thread_mtx_unlock(thread);
2334
2335 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2336 MACHDBG_CODE(DBG_MACH_IPC,MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2337 (uintptr_t)thread_tid(thread),
2338 (uintptr_t)port,
2339 VM_KERNEL_ADDRPERM((uintptr_t)voucher),
2340 2, 0);
2341
2342
2343 ipc_port_release_send(port);
2344 } else
2345 thread_mtx_unlock(thread);
2346
2347 *voucherp = voucher;
2348 return KERN_SUCCESS;
2349 }
2350
2351 /*
2352 * thread_set_mach_voucher - set a voucher reference for the specified thread voucher
2353 *
2354 * Conditions: callers holds a reference on the voucher.
2355 * nothing locked.
2356 *
2357 * We grab another reference to the voucher and bind it to the thread. Any lazy
2358 * binding is erased. The old voucher reference associated with the thread is
2359 * discarded.
2360 */
2361 kern_return_t
2362 thread_set_mach_voucher(
2363 thread_t thread,
2364 ipc_voucher_t voucher)
2365 {
2366 ipc_voucher_t old_voucher;
2367 #ifdef CONFIG_BANK
2368 ledger_t bankledger = NULL;
2369 #endif
2370
2371 if (THREAD_NULL == thread)
2372 return KERN_INVALID_ARGUMENT;
2373
2374 if (thread != current_thread() || thread->started)
2375 return KERN_INVALID_ARGUMENT;
2376
2377
2378 ipc_voucher_reference(voucher);
2379 #ifdef CONFIG_BANK
2380 bankledger = bank_get_voucher_ledger(voucher);
2381 #endif
2382 thread_mtx_lock(thread);
2383 old_voucher = thread->ith_voucher;
2384 thread->ith_voucher = voucher;
2385 thread->ith_voucher_name = MACH_PORT_NULL;
2386 #ifdef CONFIG_BANK
2387 bank_swap_thread_bank_ledger(thread, bankledger);
2388 #endif
2389 thread_mtx_unlock(thread);
2390
2391 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2392 MACHDBG_CODE(DBG_MACH_IPC,MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2393 (uintptr_t)thread_tid(thread),
2394 (uintptr_t)MACH_PORT_NULL,
2395 VM_KERNEL_ADDRPERM((uintptr_t)voucher),
2396 3, 0);
2397
2398 ipc_voucher_release(old_voucher);
2399
2400 return KERN_SUCCESS;
2401 }
2402
2403 /*
2404 * thread_swap_mach_voucher - swap a voucher reference for the specified thread voucher
2405 *
2406 * Conditions: callers holds a reference on the new and presumed old voucher(s).
2407 * nothing locked.
2408 *
2409 * If the old voucher is still the same as passed in, replace it with new voucher
2410 * and discard the old (and the reference passed in). Otherwise, discard the new
2411 * and return an updated old voucher.
2412 */
2413 kern_return_t
2414 thread_swap_mach_voucher(
2415 thread_t thread,
2416 ipc_voucher_t new_voucher,
2417 ipc_voucher_t *in_out_old_voucher)
2418 {
2419 mach_port_name_t old_voucher_name;
2420 ipc_voucher_t old_voucher;
2421 #ifdef CONFIG_BANK
2422 ledger_t bankledger = NULL;
2423 #endif
2424
2425 if (THREAD_NULL == thread)
2426 return KERN_INVALID_TASK;
2427
2428 if (thread != current_thread() || thread->started)
2429 return KERN_INVALID_ARGUMENT;
2430
2431 #ifdef CONFIG_BANK
2432 bankledger = bank_get_voucher_ledger(new_voucher);
2433 #endif
2434
2435 thread_mtx_lock(thread);
2436
2437 old_voucher = thread->ith_voucher;
2438
2439 if (IPC_VOUCHER_NULL == old_voucher) {
2440 old_voucher_name = thread->ith_voucher_name;
2441
2442 /* perform lazy binding if needed */
2443 if (MACH_PORT_VALID(old_voucher_name)) {
2444 old_voucher = convert_port_name_to_voucher(old_voucher_name);
2445 thread->ith_voucher_name = MACH_PORT_NULL;
2446 thread->ith_voucher = old_voucher;
2447
2448 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2449 MACHDBG_CODE(DBG_MACH_IPC,MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2450 (uintptr_t)thread_tid(thread),
2451 (uintptr_t)old_voucher_name,
2452 VM_KERNEL_ADDRPERM((uintptr_t)old_voucher),
2453 4, 0);
2454
2455 }
2456 }
2457
2458 /* swap in new voucher, if old voucher matches the one supplied */
2459 if (old_voucher == *in_out_old_voucher) {
2460 ipc_voucher_reference(new_voucher);
2461 thread->ith_voucher = new_voucher;
2462 thread->ith_voucher_name = MACH_PORT_NULL;
2463 #ifdef CONFIG_BANK
2464 bank_swap_thread_bank_ledger(thread, bankledger);
2465 #endif
2466 thread_mtx_unlock(thread);
2467
2468 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
2469 MACHDBG_CODE(DBG_MACH_IPC,MACH_THREAD_SET_VOUCHER) | DBG_FUNC_NONE,
2470 (uintptr_t)thread_tid(thread),
2471 (uintptr_t)MACH_PORT_NULL,
2472 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher),
2473 5, 0);
2474
2475 ipc_voucher_release(old_voucher);
2476
2477 *in_out_old_voucher = IPC_VOUCHER_NULL;
2478 return KERN_SUCCESS;
2479 }
2480
2481 /* Otherwise, just return old voucher reference */
2482 ipc_voucher_reference(old_voucher);
2483 thread_mtx_unlock(thread);
2484 *in_out_old_voucher = old_voucher;
2485 return KERN_SUCCESS;
2486 }
2487
2488 /*
2489 * thread_get_current_voucher_origin_pid - get the pid of the originator of the current voucher.
2490 */
2491 kern_return_t
2492 thread_get_current_voucher_origin_pid(
2493 int32_t *pid)
2494 {
2495 uint32_t buf_size;
2496 kern_return_t kr;
2497 thread_t thread = current_thread();
2498
2499 buf_size = sizeof(*pid);
2500 kr = mach_voucher_attr_command(thread->ith_voucher,
2501 MACH_VOUCHER_ATTR_KEY_BANK,
2502 BANK_ORIGINATOR_PID,
2503 NULL,
2504 0,
2505 (mach_voucher_attr_content_t)pid,
2506 &buf_size);
2507
2508 return kr;
2509 }
2510
2511 #if CONFIG_DTRACE
2512 uint32_t dtrace_get_thread_predcache(thread_t thread)
2513 {
2514 if (thread != THREAD_NULL)
2515 return thread->t_dtrace_predcache;
2516 else
2517 return 0;
2518 }
2519
2520 int64_t dtrace_get_thread_vtime(thread_t thread)
2521 {
2522 if (thread != THREAD_NULL)
2523 return thread->t_dtrace_vtime;
2524 else
2525 return 0;
2526 }
2527
2528 int dtrace_get_thread_last_cpu_id(thread_t thread)
2529 {
2530 if ((thread != THREAD_NULL) && (thread->last_processor != PROCESSOR_NULL)) {
2531 return thread->last_processor->cpu_id;
2532 } else {
2533 return -1;
2534 }
2535 }
2536
2537 int64_t dtrace_get_thread_tracing(thread_t thread)
2538 {
2539 if (thread != THREAD_NULL)
2540 return thread->t_dtrace_tracing;
2541 else
2542 return 0;
2543 }
2544
2545 boolean_t dtrace_get_thread_reentering(thread_t thread)
2546 {
2547 if (thread != THREAD_NULL)
2548 return (thread->options & TH_OPT_DTRACE) ? TRUE : FALSE;
2549 else
2550 return 0;
2551 }
2552
2553 vm_offset_t dtrace_get_kernel_stack(thread_t thread)
2554 {
2555 if (thread != THREAD_NULL)
2556 return thread->kernel_stack;
2557 else
2558 return 0;
2559 }
2560
2561 int64_t dtrace_calc_thread_recent_vtime(thread_t thread)
2562 {
2563 if (thread != THREAD_NULL) {
2564 processor_t processor = current_processor();
2565 uint64_t abstime = mach_absolute_time();
2566 timer_t timer;
2567
2568 timer = PROCESSOR_DATA(processor, thread_timer);
2569
2570 return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer)) +
2571 (abstime - timer->tstamp); /* XXX need interrupts off to prevent missed time? */
2572 } else
2573 return 0;
2574 }
2575
2576 void dtrace_set_thread_predcache(thread_t thread, uint32_t predcache)
2577 {
2578 if (thread != THREAD_NULL)
2579 thread->t_dtrace_predcache = predcache;
2580 }
2581
2582 void dtrace_set_thread_vtime(thread_t thread, int64_t vtime)
2583 {
2584 if (thread != THREAD_NULL)
2585 thread->t_dtrace_vtime = vtime;
2586 }
2587
2588 void dtrace_set_thread_tracing(thread_t thread, int64_t accum)
2589 {
2590 if (thread != THREAD_NULL)
2591 thread->t_dtrace_tracing = accum;
2592 }
2593
2594 void dtrace_set_thread_reentering(thread_t thread, boolean_t vbool)
2595 {
2596 if (thread != THREAD_NULL) {
2597 if (vbool)
2598 thread->options |= TH_OPT_DTRACE;
2599 else
2600 thread->options &= (~TH_OPT_DTRACE);
2601 }
2602 }
2603
2604 vm_offset_t dtrace_set_thread_recover(thread_t thread, vm_offset_t recover)
2605 {
2606 vm_offset_t prev = 0;
2607
2608 if (thread != THREAD_NULL) {
2609 prev = thread->recover;
2610 thread->recover = recover;
2611 }
2612 return prev;
2613 }
2614
2615 void dtrace_thread_bootstrap(void)
2616 {
2617 task_t task = current_task();
2618
2619 if (task->thread_count == 1) {
2620 thread_t thread = current_thread();
2621 if (thread->t_dtrace_flags & TH_DTRACE_EXECSUCCESS) {
2622 thread->t_dtrace_flags &= ~TH_DTRACE_EXECSUCCESS;
2623 DTRACE_PROC(exec__success);
2624 }
2625 DTRACE_PROC(start);
2626 }
2627 DTRACE_PROC(lwp__start);
2628
2629 }
2630
2631 void
2632 dtrace_thread_didexec(thread_t thread)
2633 {
2634 thread->t_dtrace_flags |= TH_DTRACE_EXECSUCCESS;
2635 }
2636 #endif /* CONFIG_DTRACE */