]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/thread.c
xnu-2050.48.11.tar.gz
[apple/xnu.git] / osfmk / kern / thread.c
1 /*
2 * Copyright (c) 2000-2010 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
119 #include <ipc/ipc_kmsg.h>
120 #include <ipc/ipc_port.h>
121
122 #include <vm/vm_kern.h>
123 #include <vm/vm_pageout.h>
124
125 #include <sys/kdebug.h>
126
127 #include <mach/sdt.h>
128
129 /*
130 * Exported interfaces
131 */
132 #include <mach/task_server.h>
133 #include <mach/thread_act_server.h>
134 #include <mach/mach_host_server.h>
135 #include <mach/host_priv_server.h>
136
137 static struct zone *thread_zone;
138 static lck_grp_attr_t thread_lck_grp_attr;
139 lck_attr_t thread_lck_attr;
140 lck_grp_t thread_lck_grp;
141
142 decl_simple_lock_data(static,thread_stack_lock)
143 static queue_head_t thread_stack_queue;
144
145 decl_simple_lock_data(static,thread_terminate_lock)
146 static queue_head_t thread_terminate_queue;
147
148 static struct thread thread_template, init_thread;
149
150 static void sched_call_null(
151 int type,
152 thread_t thread);
153
154 #ifdef MACH_BSD
155 extern void proc_exit(void *);
156 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
157 #endif /* MACH_BSD */
158
159 extern int debug_task;
160 int thread_max = CONFIG_THREAD_MAX; /* Max number of threads */
161 int task_threadmax = CONFIG_THREAD_MAX;
162
163 static uint64_t thread_unique_id = 0;
164
165 struct _thread_ledger_indices thread_ledgers = { -1 };
166 static ledger_template_t thread_ledger_template = NULL;
167 void init_thread_ledgers(void);
168
169 void
170 thread_bootstrap(void)
171 {
172 /*
173 * Fill in a template thread for fast initialization.
174 */
175
176 thread_template.runq = PROCESSOR_NULL;
177
178 thread_template.ref_count = 2;
179
180 thread_template.reason = AST_NONE;
181 thread_template.at_safe_point = FALSE;
182 thread_template.wait_event = NO_EVENT64;
183 thread_template.wait_queue = WAIT_QUEUE_NULL;
184 thread_template.wait_result = THREAD_WAITING;
185 thread_template.options = THREAD_ABORTSAFE;
186 thread_template.state = TH_WAIT | TH_UNINT;
187 thread_template.wake_active = FALSE;
188 thread_template.continuation = THREAD_CONTINUE_NULL;
189 thread_template.parameter = NULL;
190
191 thread_template.importance = 0;
192 thread_template.sched_mode = TH_MODE_NONE;
193 thread_template.sched_flags = 0;
194 thread_template.saved_mode = TH_MODE_NONE;
195 thread_template.safe_release = 0;
196
197 thread_template.priority = 0;
198 thread_template.sched_pri = 0;
199 thread_template.max_priority = 0;
200 thread_template.task_priority = 0;
201 thread_template.promotions = 0;
202 thread_template.pending_promoter_index = 0;
203 thread_template.pending_promoter[0] =
204 thread_template.pending_promoter[1] = NULL;
205
206 thread_template.realtime.deadline = UINT64_MAX;
207
208 thread_template.current_quantum = 0;
209 thread_template.last_run_time = 0;
210 thread_template.last_quantum_refill_time = 0;
211
212 thread_template.computation_metered = 0;
213 thread_template.computation_epoch = 0;
214
215 #if defined(CONFIG_SCHED_TRADITIONAL)
216 thread_template.sched_stamp = 0;
217 thread_template.pri_shift = INT8_MAX;
218 thread_template.sched_usage = 0;
219 thread_template.cpu_usage = thread_template.cpu_delta = 0;
220 #endif
221 thread_template.c_switch = thread_template.p_switch = thread_template.ps_switch = 0;
222
223 thread_template.bound_processor = PROCESSOR_NULL;
224 thread_template.last_processor = PROCESSOR_NULL;
225
226 thread_template.sched_call = sched_call_null;
227
228 timer_init(&thread_template.user_timer);
229 timer_init(&thread_template.system_timer);
230 thread_template.user_timer_save = 0;
231 thread_template.system_timer_save = 0;
232 thread_template.vtimer_user_save = 0;
233 thread_template.vtimer_prof_save = 0;
234 thread_template.vtimer_rlim_save = 0;
235
236 thread_template.wait_timer_is_set = FALSE;
237 thread_template.wait_timer_active = 0;
238
239 thread_template.depress_timer_active = 0;
240
241 thread_template.special_handler.handler = special_handler;
242 thread_template.special_handler.next = NULL;
243
244 thread_template.funnel_lock = THR_FUNNEL_NULL;
245 thread_template.funnel_state = 0;
246 thread_template.recover = (vm_offset_t)NULL;
247
248 thread_template.map = VM_MAP_NULL;
249
250 #if CONFIG_DTRACE
251 thread_template.t_dtrace_predcache = 0;
252 thread_template.t_dtrace_vtime = 0;
253 thread_template.t_dtrace_tracing = 0;
254 #endif /* CONFIG_DTRACE */
255
256 thread_template.t_chud = 0;
257 thread_template.t_page_creation_count = 0;
258 thread_template.t_page_creation_time = 0;
259
260 thread_template.affinity_set = NULL;
261
262 thread_template.syscalls_unix = 0;
263 thread_template.syscalls_mach = 0;
264
265 thread_template.t_ledger = LEDGER_NULL;
266 thread_template.t_threadledger = LEDGER_NULL;
267
268 thread_template.appliedstate = default_task_null_policy;
269 thread_template.ext_appliedstate = default_task_null_policy;
270 thread_template.policystate = default_task_proc_policy;
271 thread_template.ext_policystate = default_task_proc_policy;
272 #if CONFIG_EMBEDDED
273 thread_template.taskwatch = NULL;
274 thread_template.saved_importance = 0;
275 #endif /* CONFIG_EMBEDDED */
276
277 init_thread = thread_template;
278 machine_set_current_thread(&init_thread);
279 }
280
281 void
282 thread_init(void)
283 {
284 thread_zone = zinit(
285 sizeof(struct thread),
286 thread_max * sizeof(struct thread),
287 THREAD_CHUNK * sizeof(struct thread),
288 "threads");
289
290 lck_grp_attr_setdefault(&thread_lck_grp_attr);
291 lck_grp_init(&thread_lck_grp, "thread", &thread_lck_grp_attr);
292 lck_attr_setdefault(&thread_lck_attr);
293
294 stack_init();
295
296 /*
297 * Initialize any machine-dependent
298 * per-thread structures necessary.
299 */
300 machine_thread_init();
301
302 init_thread_ledgers();
303 }
304
305 static void
306 thread_terminate_continue(void)
307 {
308 panic("thread_terminate_continue");
309 /*NOTREACHED*/
310 }
311
312 /*
313 * thread_terminate_self:
314 */
315 void
316 thread_terminate_self(void)
317 {
318 thread_t thread = current_thread();
319
320 task_t task;
321 spl_t s;
322 int threadcnt;
323
324 pal_thread_terminate_self(thread);
325
326 DTRACE_PROC(lwp__exit);
327
328 thread_mtx_lock(thread);
329
330 ulock_release_all(thread);
331
332 ipc_thread_disable(thread);
333
334 thread_mtx_unlock(thread);
335
336 s = splsched();
337 thread_lock(thread);
338
339 /*
340 * Cancel priority depression, wait for concurrent expirations
341 * on other processors.
342 */
343 if (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) {
344 thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
345
346 if (timer_call_cancel(&thread->depress_timer))
347 thread->depress_timer_active--;
348 }
349
350 while (thread->depress_timer_active > 0) {
351 thread_unlock(thread);
352 splx(s);
353
354 delay(1);
355
356 s = splsched();
357 thread_lock(thread);
358 }
359
360 thread_sched_call(thread, NULL);
361
362 thread_unlock(thread);
363 splx(s);
364
365 thread_policy_reset(thread);
366
367 #if CONFIG_EMBEDDED
368 thead_remove_taskwatch(thread);
369 #endif /* CONFIG_EMBEDDED */
370
371 task = thread->task;
372 uthread_cleanup(task, thread->uthread, task->bsd_info);
373 threadcnt = hw_atomic_sub(&task->active_thread_count, 1);
374
375 /*
376 * If we are the last thread to terminate and the task is
377 * associated with a BSD process, perform BSD process exit.
378 */
379 if (threadcnt == 0 && task->bsd_info != NULL)
380 proc_exit(task->bsd_info);
381
382 uthread_cred_free(thread->uthread);
383
384 s = splsched();
385 thread_lock(thread);
386
387 /*
388 * Cancel wait timer, and wait for
389 * concurrent expirations.
390 */
391 if (thread->wait_timer_is_set) {
392 thread->wait_timer_is_set = FALSE;
393
394 if (timer_call_cancel(&thread->wait_timer))
395 thread->wait_timer_active--;
396 }
397
398 while (thread->wait_timer_active > 0) {
399 thread_unlock(thread);
400 splx(s);
401
402 delay(1);
403
404 s = splsched();
405 thread_lock(thread);
406 }
407
408 /*
409 * If there is a reserved stack, release it.
410 */
411 if (thread->reserved_stack != 0) {
412 stack_free_reserved(thread);
413 thread->reserved_stack = 0;
414 }
415
416 /*
417 * Mark thread as terminating, and block.
418 */
419 thread->state |= TH_TERMINATE;
420 thread_mark_wait_locked(thread, THREAD_UNINT);
421 assert(thread->promotions == 0);
422 thread_unlock(thread);
423 /* splsched */
424
425 thread_block((thread_continue_t)thread_terminate_continue);
426 /*NOTREACHED*/
427 }
428
429 void
430 thread_deallocate(
431 thread_t thread)
432 {
433 task_t task;
434
435 if (thread == THREAD_NULL)
436 return;
437
438 if (thread_deallocate_internal(thread) > 0)
439 return;
440
441
442 ipc_thread_terminate(thread);
443
444 task = thread->task;
445
446 #ifdef MACH_BSD
447 {
448 void *ut = thread->uthread;
449
450 thread->uthread = NULL;
451 uthread_zone_free(ut);
452 }
453 #endif /* MACH_BSD */
454
455 if (thread->t_ledger)
456 ledger_dereference(thread->t_ledger);
457 if (thread->t_threadledger)
458 ledger_dereference(thread->t_threadledger);
459
460 if (thread->kernel_stack != 0)
461 stack_free(thread);
462
463 lck_mtx_destroy(&thread->mutex, &thread_lck_grp);
464 machine_thread_destroy(thread);
465
466 task_deallocate(task);
467
468 zfree(thread_zone, thread);
469 }
470
471 /*
472 * thread_terminate_daemon:
473 *
474 * Perform final clean up for terminating threads.
475 */
476 static void
477 thread_terminate_daemon(void)
478 {
479 thread_t self, thread;
480 task_t task;
481
482 self = current_thread();
483 self->options |= TH_OPT_SYSTEM_CRITICAL;
484
485 (void)splsched();
486 simple_lock(&thread_terminate_lock);
487
488 while ((thread = (thread_t)dequeue_head(&thread_terminate_queue)) != THREAD_NULL) {
489 simple_unlock(&thread_terminate_lock);
490 (void)spllo();
491
492 task = thread->task;
493
494 task_lock(task);
495 task->total_user_time += timer_grab(&thread->user_timer);
496 if (thread->precise_user_kernel_time) {
497 task->total_system_time += timer_grab(&thread->system_timer);
498 } else {
499 task->total_user_time += timer_grab(&thread->system_timer);
500 }
501
502 task->c_switch += thread->c_switch;
503 task->p_switch += thread->p_switch;
504 task->ps_switch += thread->ps_switch;
505
506 task->syscalls_unix += thread->syscalls_unix;
507 task->syscalls_mach += thread->syscalls_mach;
508
509 task->task_timer_wakeups_bin_1 += thread->thread_timer_wakeups_bin_1;
510 task->task_timer_wakeups_bin_2 += thread->thread_timer_wakeups_bin_2;
511 queue_remove(&task->threads, thread, thread_t, task_threads);
512 task->thread_count--;
513
514 /*
515 * If the task is being halted, and there is only one thread
516 * left in the task after this one, then wakeup that thread.
517 */
518 if (task->thread_count == 1 && task->halting)
519 thread_wakeup((event_t)&task->halting);
520
521 task_unlock(task);
522
523 lck_mtx_lock(&tasks_threads_lock);
524 queue_remove(&threads, thread, thread_t, threads);
525 threads_count--;
526 lck_mtx_unlock(&tasks_threads_lock);
527
528 thread_deallocate(thread);
529
530 (void)splsched();
531 simple_lock(&thread_terminate_lock);
532 }
533
534 assert_wait((event_t)&thread_terminate_queue, THREAD_UNINT);
535 simple_unlock(&thread_terminate_lock);
536 /* splsched */
537
538 self->options &= ~TH_OPT_SYSTEM_CRITICAL;
539 thread_block((thread_continue_t)thread_terminate_daemon);
540 /*NOTREACHED*/
541 }
542
543 /*
544 * thread_terminate_enqueue:
545 *
546 * Enqueue a terminating thread for final disposition.
547 *
548 * Called at splsched.
549 */
550 void
551 thread_terminate_enqueue(
552 thread_t thread)
553 {
554 simple_lock(&thread_terminate_lock);
555 enqueue_tail(&thread_terminate_queue, (queue_entry_t)thread);
556 simple_unlock(&thread_terminate_lock);
557
558 thread_wakeup((event_t)&thread_terminate_queue);
559 }
560
561 /*
562 * thread_stack_daemon:
563 *
564 * Perform stack allocation as required due to
565 * invoke failures.
566 */
567 static void
568 thread_stack_daemon(void)
569 {
570 thread_t thread;
571
572 simple_lock(&thread_stack_lock);
573
574 while ((thread = (thread_t)dequeue_head(&thread_stack_queue)) != THREAD_NULL) {
575 simple_unlock(&thread_stack_lock);
576
577 stack_alloc(thread);
578
579 (void)splsched();
580 thread_lock(thread);
581 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
582 thread_unlock(thread);
583 (void)spllo();
584
585 simple_lock(&thread_stack_lock);
586 }
587
588 assert_wait((event_t)&thread_stack_queue, THREAD_UNINT);
589 simple_unlock(&thread_stack_lock);
590
591 thread_block((thread_continue_t)thread_stack_daemon);
592 /*NOTREACHED*/
593 }
594
595 /*
596 * thread_stack_enqueue:
597 *
598 * Enqueue a thread for stack allocation.
599 *
600 * Called at splsched.
601 */
602 void
603 thread_stack_enqueue(
604 thread_t thread)
605 {
606 simple_lock(&thread_stack_lock);
607 enqueue_tail(&thread_stack_queue, (queue_entry_t)thread);
608 simple_unlock(&thread_stack_lock);
609
610 thread_wakeup((event_t)&thread_stack_queue);
611 }
612
613 void
614 thread_daemon_init(void)
615 {
616 kern_return_t result;
617 thread_t thread = NULL;
618
619 simple_lock_init(&thread_terminate_lock, 0);
620 queue_init(&thread_terminate_queue);
621
622 result = kernel_thread_start_priority((thread_continue_t)thread_terminate_daemon, NULL, MINPRI_KERNEL, &thread);
623 if (result != KERN_SUCCESS)
624 panic("thread_daemon_init: thread_terminate_daemon");
625
626 thread_deallocate(thread);
627
628 simple_lock_init(&thread_stack_lock, 0);
629 queue_init(&thread_stack_queue);
630
631 result = kernel_thread_start_priority((thread_continue_t)thread_stack_daemon, NULL, BASEPRI_PREEMPT, &thread);
632 if (result != KERN_SUCCESS)
633 panic("thread_daemon_init: thread_stack_daemon");
634
635 thread_deallocate(thread);
636 }
637
638 /*
639 * Create a new thread.
640 * Doesn't start the thread running.
641 */
642 static kern_return_t
643 thread_create_internal(
644 task_t parent_task,
645 integer_t priority,
646 thread_continue_t continuation,
647 int options,
648 #define TH_OPTION_NONE 0x00
649 #define TH_OPTION_NOCRED 0x01
650 #define TH_OPTION_NOSUSP 0x02
651 thread_t *out_thread)
652 {
653 thread_t new_thread;
654 static thread_t first_thread;
655
656 /*
657 * Allocate a thread and initialize static fields
658 */
659 if (first_thread == THREAD_NULL)
660 new_thread = first_thread = current_thread();
661 else
662 new_thread = (thread_t)zalloc(thread_zone);
663 if (new_thread == THREAD_NULL)
664 return (KERN_RESOURCE_SHORTAGE);
665
666 if (new_thread != first_thread)
667 *new_thread = thread_template;
668
669 #ifdef MACH_BSD
670 new_thread->uthread = uthread_alloc(parent_task, new_thread, (options & TH_OPTION_NOCRED) != 0);
671 if (new_thread->uthread == NULL) {
672 zfree(thread_zone, new_thread);
673 return (KERN_RESOURCE_SHORTAGE);
674 }
675 #endif /* MACH_BSD */
676
677 if (machine_thread_create(new_thread, parent_task) != KERN_SUCCESS) {
678 #ifdef MACH_BSD
679 void *ut = new_thread->uthread;
680
681 new_thread->uthread = NULL;
682 /* cred free may not be necessary */
683 uthread_cleanup(parent_task, ut, parent_task->bsd_info);
684 uthread_cred_free(ut);
685 uthread_zone_free(ut);
686 #endif /* MACH_BSD */
687
688 zfree(thread_zone, new_thread);
689 return (KERN_FAILURE);
690 }
691
692 new_thread->task = parent_task;
693
694 thread_lock_init(new_thread);
695 wake_lock_init(new_thread);
696
697 lck_mtx_init(&new_thread->mutex, &thread_lck_grp, &thread_lck_attr);
698
699 ipc_thread_init(new_thread);
700 queue_init(&new_thread->held_ulocks);
701
702 new_thread->continuation = continuation;
703
704 lck_mtx_lock(&tasks_threads_lock);
705 task_lock(parent_task);
706
707 if ( !parent_task->active || parent_task->halting ||
708 ((options & TH_OPTION_NOSUSP) != 0 &&
709 parent_task->suspend_count > 0) ||
710 (parent_task->thread_count >= task_threadmax &&
711 parent_task != kernel_task) ) {
712 task_unlock(parent_task);
713 lck_mtx_unlock(&tasks_threads_lock);
714
715 #ifdef MACH_BSD
716 {
717 void *ut = new_thread->uthread;
718
719 new_thread->uthread = NULL;
720 uthread_cleanup(parent_task, ut, parent_task->bsd_info);
721 /* cred free may not be necessary */
722 uthread_cred_free(ut);
723 uthread_zone_free(ut);
724 }
725 #endif /* MACH_BSD */
726 ipc_thread_disable(new_thread);
727 ipc_thread_terminate(new_thread);
728 lck_mtx_destroy(&new_thread->mutex, &thread_lck_grp);
729 machine_thread_destroy(new_thread);
730 zfree(thread_zone, new_thread);
731 return (KERN_FAILURE);
732 }
733
734 /* New threads inherit any default state on the task */
735 machine_thread_inherit_taskwide(new_thread, parent_task);
736
737 task_reference_internal(parent_task);
738
739 if (new_thread->task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) {
740 /*
741 * This task has a per-thread CPU limit; make sure this new thread
742 * gets its limit set too, before it gets out of the kernel.
743 */
744 set_astledger(new_thread);
745 }
746 new_thread->t_threadledger = LEDGER_NULL; /* per thread ledger is not inherited */
747 new_thread->t_ledger = new_thread->task->ledger;
748 if (new_thread->t_ledger)
749 ledger_reference(new_thread->t_ledger);
750
751 /* Cache the task's map */
752 new_thread->map = parent_task->map;
753
754 /* Chain the thread onto the task's list */
755 queue_enter(&parent_task->threads, new_thread, thread_t, task_threads);
756 parent_task->thread_count++;
757
758 /* So terminating threads don't need to take the task lock to decrement */
759 hw_atomic_add(&parent_task->active_thread_count, 1);
760
761 /* Protected by the tasks_threads_lock */
762 new_thread->thread_id = ++thread_unique_id;
763
764 queue_enter(&threads, new_thread, thread_t, threads);
765 threads_count++;
766
767 timer_call_setup(&new_thread->wait_timer, thread_timer_expire, new_thread);
768 timer_call_setup(&new_thread->depress_timer, thread_depress_expire, new_thread);
769
770 #if CONFIG_COUNTERS
771 /*
772 * If parent task has any reservations, they need to be propagated to this
773 * thread.
774 */
775 new_thread->t_chud = (TASK_PMC_FLAG == (parent_task->t_chud & TASK_PMC_FLAG)) ?
776 THREAD_PMC_FLAG : 0U;
777 #endif
778
779 /* Set the thread's scheduling parameters */
780 new_thread->sched_mode = SCHED(initial_thread_sched_mode)(parent_task);
781 new_thread->sched_flags = 0;
782 new_thread->max_priority = parent_task->max_priority;
783 new_thread->task_priority = parent_task->priority;
784 new_thread->priority = (priority < 0)? parent_task->priority: priority;
785 if (new_thread->priority > new_thread->max_priority)
786 new_thread->priority = new_thread->max_priority;
787 #if CONFIG_EMBEDDED
788 if (new_thread->priority < MAXPRI_THROTTLE) {
789 new_thread->priority = MAXPRI_THROTTLE;
790 }
791 #endif /* CONFIG_EMBEDDED */
792 new_thread->importance =
793 new_thread->priority - new_thread->task_priority;
794 #if CONFIG_EMBEDDED
795 new_thread->saved_importance = new_thread->importance;
796 /* apple ios daemon starts all threads in darwin background */
797 if (parent_task->ext_appliedstate.apptype == PROC_POLICY_IOS_APPLE_DAEMON) {
798 /* Cannot use generic routines here so apply darwin bacground directly */
799 new_thread->policystate.hw_bg = TASK_POLICY_BACKGROUND_ATTRIBUTE_ALL;
800 /* set thread self backgrounding */
801 new_thread->appliedstate.hw_bg = new_thread->policystate.hw_bg;
802 /* priority will get recomputed suitably bit later */
803 new_thread->importance = INT_MIN;
804 /* to avoid changes to many pri compute routines, set the effect of those here */
805 new_thread->priority = MAXPRI_THROTTLE;
806 }
807 #endif /* CONFIG_EMBEDDED */
808
809 #if defined(CONFIG_SCHED_TRADITIONAL)
810 new_thread->sched_stamp = sched_tick;
811 new_thread->pri_shift = sched_pri_shift;
812 #endif
813 SCHED(compute_priority)(new_thread, FALSE);
814
815 new_thread->active = TRUE;
816
817 *out_thread = new_thread;
818
819 {
820 long dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4;
821
822 kdbg_trace_data(parent_task->bsd_info, &dbg_arg2);
823
824 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
825 TRACEDBG_CODE(DBG_TRACE_DATA, 1) | DBG_FUNC_NONE,
826 (vm_address_t)(uintptr_t)thread_tid(new_thread), dbg_arg2, 0, 0, 0);
827
828 kdbg_trace_string(parent_task->bsd_info,
829 &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4);
830
831 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
832 TRACEDBG_CODE(DBG_TRACE_STRING, 1) | DBG_FUNC_NONE,
833 dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0);
834 }
835
836 DTRACE_PROC1(lwp__create, thread_t, *out_thread);
837
838 return (KERN_SUCCESS);
839 }
840
841 static kern_return_t
842 thread_create_internal2(
843 task_t task,
844 thread_t *new_thread,
845 boolean_t from_user)
846 {
847 kern_return_t result;
848 thread_t thread;
849
850 if (task == TASK_NULL || task == kernel_task)
851 return (KERN_INVALID_ARGUMENT);
852
853 result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
854 if (result != KERN_SUCCESS)
855 return (result);
856
857 thread->user_stop_count = 1;
858 thread_hold(thread);
859 if (task->suspend_count > 0)
860 thread_hold(thread);
861
862 if (from_user)
863 extmod_statistics_incr_thread_create(task);
864
865 task_unlock(task);
866 lck_mtx_unlock(&tasks_threads_lock);
867
868 *new_thread = thread;
869
870 return (KERN_SUCCESS);
871 }
872
873 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
874 kern_return_t
875 thread_create(
876 task_t task,
877 thread_t *new_thread);
878
879 kern_return_t
880 thread_create(
881 task_t task,
882 thread_t *new_thread)
883 {
884 return thread_create_internal2(task, new_thread, FALSE);
885 }
886
887 kern_return_t
888 thread_create_from_user(
889 task_t task,
890 thread_t *new_thread)
891 {
892 return thread_create_internal2(task, new_thread, TRUE);
893 }
894
895 static kern_return_t
896 thread_create_running_internal2(
897 register task_t task,
898 int flavor,
899 thread_state_t new_state,
900 mach_msg_type_number_t new_state_count,
901 thread_t *new_thread,
902 boolean_t from_user)
903 {
904 register kern_return_t result;
905 thread_t thread;
906
907 if (task == TASK_NULL || task == kernel_task)
908 return (KERN_INVALID_ARGUMENT);
909
910 result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
911 if (result != KERN_SUCCESS)
912 return (result);
913
914 result = machine_thread_set_state(
915 thread, flavor, new_state, new_state_count);
916 if (result != KERN_SUCCESS) {
917 task_unlock(task);
918 lck_mtx_unlock(&tasks_threads_lock);
919
920 thread_terminate(thread);
921 thread_deallocate(thread);
922 return (result);
923 }
924
925 thread_mtx_lock(thread);
926 thread_start_internal(thread);
927 thread_mtx_unlock(thread);
928
929 if (from_user)
930 extmod_statistics_incr_thread_create(task);
931
932 task_unlock(task);
933 lck_mtx_unlock(&tasks_threads_lock);
934
935 *new_thread = thread;
936
937 return (result);
938 }
939
940 /* Prototype, see justification above */
941 kern_return_t
942 thread_create_running(
943 register task_t task,
944 int flavor,
945 thread_state_t new_state,
946 mach_msg_type_number_t new_state_count,
947 thread_t *new_thread);
948
949 kern_return_t
950 thread_create_running(
951 register task_t task,
952 int flavor,
953 thread_state_t new_state,
954 mach_msg_type_number_t new_state_count,
955 thread_t *new_thread)
956 {
957 return thread_create_running_internal2(
958 task, flavor, new_state, new_state_count,
959 new_thread, FALSE);
960 }
961
962 kern_return_t
963 thread_create_running_from_user(
964 register task_t task,
965 int flavor,
966 thread_state_t new_state,
967 mach_msg_type_number_t new_state_count,
968 thread_t *new_thread)
969 {
970 return thread_create_running_internal2(
971 task, flavor, new_state, new_state_count,
972 new_thread, TRUE);
973 }
974
975 kern_return_t
976 thread_create_workq(
977 task_t task,
978 thread_continue_t thread_return,
979 thread_t *new_thread)
980 {
981 kern_return_t result;
982 thread_t thread;
983
984 if (task == TASK_NULL || task == kernel_task)
985 return (KERN_INVALID_ARGUMENT);
986
987 result = thread_create_internal(task, -1, thread_return, TH_OPTION_NOCRED | TH_OPTION_NOSUSP, &thread);
988 if (result != KERN_SUCCESS)
989 return (result);
990
991 thread->user_stop_count = 1;
992 thread_hold(thread);
993 if (task->suspend_count > 0)
994 thread_hold(thread);
995
996 task_unlock(task);
997 lck_mtx_unlock(&tasks_threads_lock);
998
999 *new_thread = thread;
1000
1001 return (KERN_SUCCESS);
1002 }
1003
1004 /*
1005 * kernel_thread_create:
1006 *
1007 * Create a thread in the kernel task
1008 * to execute in kernel context.
1009 */
1010 kern_return_t
1011 kernel_thread_create(
1012 thread_continue_t continuation,
1013 void *parameter,
1014 integer_t priority,
1015 thread_t *new_thread)
1016 {
1017 kern_return_t result;
1018 thread_t thread;
1019 task_t task = kernel_task;
1020
1021 result = thread_create_internal(task, priority, continuation, TH_OPTION_NONE, &thread);
1022 if (result != KERN_SUCCESS)
1023 return (result);
1024
1025 task_unlock(task);
1026 lck_mtx_unlock(&tasks_threads_lock);
1027
1028 stack_alloc(thread);
1029 assert(thread->kernel_stack != 0);
1030 #if CONFIG_EMBEDDED
1031 if (priority > BASEPRI_KERNEL)
1032 #endif
1033 thread->reserved_stack = thread->kernel_stack;
1034
1035 thread->parameter = parameter;
1036
1037 if(debug_task & 1)
1038 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread, continuation);
1039 *new_thread = thread;
1040
1041 return (result);
1042 }
1043
1044 kern_return_t
1045 kernel_thread_start_priority(
1046 thread_continue_t continuation,
1047 void *parameter,
1048 integer_t priority,
1049 thread_t *new_thread)
1050 {
1051 kern_return_t result;
1052 thread_t thread;
1053
1054 result = kernel_thread_create(continuation, parameter, priority, &thread);
1055 if (result != KERN_SUCCESS)
1056 return (result);
1057
1058 *new_thread = thread;
1059
1060 thread_mtx_lock(thread);
1061 thread_start_internal(thread);
1062 thread_mtx_unlock(thread);
1063
1064 return (result);
1065 }
1066
1067 kern_return_t
1068 kernel_thread_start(
1069 thread_continue_t continuation,
1070 void *parameter,
1071 thread_t *new_thread)
1072 {
1073 return kernel_thread_start_priority(continuation, parameter, -1, new_thread);
1074 }
1075
1076 #if defined(__i386__)
1077
1078 thread_t
1079 kernel_thread(
1080 task_t task,
1081 void (*start)(void))
1082 {
1083 kern_return_t result;
1084 thread_t thread;
1085
1086 if (task != kernel_task)
1087 panic("kernel_thread");
1088
1089 result = kernel_thread_start_priority((thread_continue_t)start, NULL, -1, &thread);
1090 if (result != KERN_SUCCESS)
1091 return (THREAD_NULL);
1092
1093 thread_deallocate(thread);
1094
1095 return (thread);
1096 }
1097
1098 #endif /* defined(__i386__) */
1099
1100 kern_return_t
1101 thread_info_internal(
1102 register thread_t thread,
1103 thread_flavor_t flavor,
1104 thread_info_t thread_info_out, /* ptr to OUT array */
1105 mach_msg_type_number_t *thread_info_count) /*IN/OUT*/
1106 {
1107 int state, flags;
1108 spl_t s;
1109
1110 if (thread == THREAD_NULL)
1111 return (KERN_INVALID_ARGUMENT);
1112
1113 if (flavor == THREAD_BASIC_INFO) {
1114 register thread_basic_info_t basic_info;
1115
1116 if (*thread_info_count < THREAD_BASIC_INFO_COUNT)
1117 return (KERN_INVALID_ARGUMENT);
1118
1119 basic_info = (thread_basic_info_t) thread_info_out;
1120
1121 s = splsched();
1122 thread_lock(thread);
1123
1124 /* fill in info */
1125
1126 thread_read_times(thread, &basic_info->user_time,
1127 &basic_info->system_time);
1128
1129 /*
1130 * Update lazy-evaluated scheduler info because someone wants it.
1131 */
1132 if (SCHED(can_update_priority)(thread))
1133 SCHED(update_priority)(thread);
1134
1135 basic_info->sleep_time = 0;
1136
1137 /*
1138 * To calculate cpu_usage, first correct for timer rate,
1139 * then for 5/8 ageing. The correction factor [3/5] is
1140 * (1/(5/8) - 1).
1141 */
1142 basic_info->cpu_usage = 0;
1143 #if defined(CONFIG_SCHED_TRADITIONAL)
1144 if (sched_tick_interval) {
1145 basic_info->cpu_usage = (integer_t)(((uint64_t)thread->cpu_usage
1146 * TH_USAGE_SCALE) / sched_tick_interval);
1147 basic_info->cpu_usage = (basic_info->cpu_usage * 3) / 5;
1148 }
1149 #endif
1150
1151 if (basic_info->cpu_usage > TH_USAGE_SCALE)
1152 basic_info->cpu_usage = TH_USAGE_SCALE;
1153
1154 basic_info->policy = ((thread->sched_mode == TH_MODE_TIMESHARE)?
1155 POLICY_TIMESHARE: POLICY_RR);
1156
1157 flags = 0;
1158 if (thread->bound_processor != PROCESSOR_NULL && thread->bound_processor->idle_thread == thread)
1159 flags |= TH_FLAGS_IDLE;
1160
1161 if (!thread->kernel_stack)
1162 flags |= TH_FLAGS_SWAPPED;
1163
1164 state = 0;
1165 if (thread->state & TH_TERMINATE)
1166 state = TH_STATE_HALTED;
1167 else
1168 if (thread->state & TH_RUN)
1169 state = TH_STATE_RUNNING;
1170 else
1171 if (thread->state & TH_UNINT)
1172 state = TH_STATE_UNINTERRUPTIBLE;
1173 else
1174 if (thread->state & TH_SUSP)
1175 state = TH_STATE_STOPPED;
1176 else
1177 if (thread->state & TH_WAIT)
1178 state = TH_STATE_WAITING;
1179
1180 basic_info->run_state = state;
1181 basic_info->flags = flags;
1182
1183 basic_info->suspend_count = thread->user_stop_count;
1184
1185 thread_unlock(thread);
1186 splx(s);
1187
1188 *thread_info_count = THREAD_BASIC_INFO_COUNT;
1189
1190 return (KERN_SUCCESS);
1191 }
1192 else
1193 if (flavor == THREAD_IDENTIFIER_INFO) {
1194 register thread_identifier_info_t identifier_info;
1195
1196 if (*thread_info_count < THREAD_IDENTIFIER_INFO_COUNT)
1197 return (KERN_INVALID_ARGUMENT);
1198
1199 identifier_info = (thread_identifier_info_t) thread_info_out;
1200
1201 s = splsched();
1202 thread_lock(thread);
1203
1204 identifier_info->thread_id = thread->thread_id;
1205 identifier_info->thread_handle = thread->machine.cthread_self;
1206 if(thread->task->bsd_info) {
1207 identifier_info->dispatch_qaddr = identifier_info->thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
1208 } else {
1209 thread_unlock(thread);
1210 splx(s);
1211 return KERN_INVALID_ARGUMENT;
1212 }
1213
1214 thread_unlock(thread);
1215 splx(s);
1216 return KERN_SUCCESS;
1217 }
1218 else
1219 if (flavor == THREAD_SCHED_TIMESHARE_INFO) {
1220 policy_timeshare_info_t ts_info;
1221
1222 if (*thread_info_count < POLICY_TIMESHARE_INFO_COUNT)
1223 return (KERN_INVALID_ARGUMENT);
1224
1225 ts_info = (policy_timeshare_info_t)thread_info_out;
1226
1227 s = splsched();
1228 thread_lock(thread);
1229
1230 if (thread->sched_mode != TH_MODE_TIMESHARE) {
1231 thread_unlock(thread);
1232 splx(s);
1233
1234 return (KERN_INVALID_POLICY);
1235 }
1236
1237 ts_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
1238 if (ts_info->depressed) {
1239 ts_info->base_priority = DEPRESSPRI;
1240 ts_info->depress_priority = thread->priority;
1241 }
1242 else {
1243 ts_info->base_priority = thread->priority;
1244 ts_info->depress_priority = -1;
1245 }
1246
1247 ts_info->cur_priority = thread->sched_pri;
1248 ts_info->max_priority = thread->max_priority;
1249
1250 thread_unlock(thread);
1251 splx(s);
1252
1253 *thread_info_count = POLICY_TIMESHARE_INFO_COUNT;
1254
1255 return (KERN_SUCCESS);
1256 }
1257 else
1258 if (flavor == THREAD_SCHED_FIFO_INFO) {
1259 if (*thread_info_count < POLICY_FIFO_INFO_COUNT)
1260 return (KERN_INVALID_ARGUMENT);
1261
1262 return (KERN_INVALID_POLICY);
1263 }
1264 else
1265 if (flavor == THREAD_SCHED_RR_INFO) {
1266 policy_rr_info_t rr_info;
1267 uint32_t quantum_time;
1268 uint64_t quantum_ns;
1269
1270 if (*thread_info_count < POLICY_RR_INFO_COUNT)
1271 return (KERN_INVALID_ARGUMENT);
1272
1273 rr_info = (policy_rr_info_t) thread_info_out;
1274
1275 s = splsched();
1276 thread_lock(thread);
1277
1278 if (thread->sched_mode == TH_MODE_TIMESHARE) {
1279 thread_unlock(thread);
1280 splx(s);
1281
1282 return (KERN_INVALID_POLICY);
1283 }
1284
1285 rr_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
1286 if (rr_info->depressed) {
1287 rr_info->base_priority = DEPRESSPRI;
1288 rr_info->depress_priority = thread->priority;
1289 }
1290 else {
1291 rr_info->base_priority = thread->priority;
1292 rr_info->depress_priority = -1;
1293 }
1294
1295 quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
1296 absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
1297
1298 rr_info->max_priority = thread->max_priority;
1299 rr_info->quantum = (uint32_t)(quantum_ns / 1000 / 1000);
1300
1301 thread_unlock(thread);
1302 splx(s);
1303
1304 *thread_info_count = POLICY_RR_INFO_COUNT;
1305
1306 return (KERN_SUCCESS);
1307 }
1308
1309 return (KERN_INVALID_ARGUMENT);
1310 }
1311
1312 void
1313 thread_read_times(
1314 thread_t thread,
1315 time_value_t *user_time,
1316 time_value_t *system_time)
1317 {
1318 clock_sec_t secs;
1319 clock_usec_t usecs;
1320 uint64_t tval_user, tval_system;
1321
1322 tval_user = timer_grab(&thread->user_timer);
1323 tval_system = timer_grab(&thread->system_timer);
1324
1325 if (thread->precise_user_kernel_time) {
1326 absolutetime_to_microtime(tval_user, &secs, &usecs);
1327 user_time->seconds = (typeof(user_time->seconds))secs;
1328 user_time->microseconds = usecs;
1329
1330 absolutetime_to_microtime(tval_system, &secs, &usecs);
1331 system_time->seconds = (typeof(system_time->seconds))secs;
1332 system_time->microseconds = usecs;
1333 } else {
1334 /* system_timer may represent either sys or user */
1335 tval_user += tval_system;
1336 absolutetime_to_microtime(tval_user, &secs, &usecs);
1337 user_time->seconds = (typeof(user_time->seconds))secs;
1338 user_time->microseconds = usecs;
1339
1340 system_time->seconds = 0;
1341 system_time->microseconds = 0;
1342 }
1343 }
1344
1345 kern_return_t
1346 thread_assign(
1347 __unused thread_t thread,
1348 __unused processor_set_t new_pset)
1349 {
1350 return (KERN_FAILURE);
1351 }
1352
1353 /*
1354 * thread_assign_default:
1355 *
1356 * Special version of thread_assign for assigning threads to default
1357 * processor set.
1358 */
1359 kern_return_t
1360 thread_assign_default(
1361 thread_t thread)
1362 {
1363 return (thread_assign(thread, &pset0));
1364 }
1365
1366 /*
1367 * thread_get_assignment
1368 *
1369 * Return current assignment for this thread.
1370 */
1371 kern_return_t
1372 thread_get_assignment(
1373 thread_t thread,
1374 processor_set_t *pset)
1375 {
1376 if (thread == NULL)
1377 return (KERN_INVALID_ARGUMENT);
1378
1379 *pset = &pset0;
1380
1381 return (KERN_SUCCESS);
1382 }
1383
1384 /*
1385 * thread_wire_internal:
1386 *
1387 * Specify that the target thread must always be able
1388 * to run and to allocate memory.
1389 */
1390 kern_return_t
1391 thread_wire_internal(
1392 host_priv_t host_priv,
1393 thread_t thread,
1394 boolean_t wired,
1395 boolean_t *prev_state)
1396 {
1397 if (host_priv == NULL || thread != current_thread())
1398 return (KERN_INVALID_ARGUMENT);
1399
1400 assert(host_priv == &realhost);
1401
1402 if (prev_state)
1403 *prev_state = (thread->options & TH_OPT_VMPRIV) != 0;
1404
1405 if (wired) {
1406 if (!(thread->options & TH_OPT_VMPRIV))
1407 vm_page_free_reserve(1); /* XXX */
1408 thread->options |= TH_OPT_VMPRIV;
1409 }
1410 else {
1411 if (thread->options & TH_OPT_VMPRIV)
1412 vm_page_free_reserve(-1); /* XXX */
1413 thread->options &= ~TH_OPT_VMPRIV;
1414 }
1415
1416 return (KERN_SUCCESS);
1417 }
1418
1419
1420 /*
1421 * thread_wire:
1422 *
1423 * User-api wrapper for thread_wire_internal()
1424 */
1425 kern_return_t
1426 thread_wire(
1427 host_priv_t host_priv,
1428 thread_t thread,
1429 boolean_t wired)
1430 {
1431 return (thread_wire_internal(host_priv, thread, wired, NULL));
1432 }
1433
1434 static void
1435 thread_resource_exception(const void *arg0, __unused const void *arg1)
1436 {
1437 thread_t thread = current_thread();
1438 int code = (int)((uintptr_t)arg0 & ((int)-1));
1439
1440 assert(thread->t_threadledger != LEDGER_NULL);
1441
1442 /*
1443 * Disable the exception notification so we don't overwhelm
1444 * the listener with an endless stream of redundant exceptions.
1445 */
1446 ledger_set_action(thread->t_threadledger, thread_ledgers.cpu_time,
1447 LEDGER_ACTION_IGNORE);
1448 ledger_disable_callback(thread->t_threadledger, thread_ledgers.cpu_time);
1449
1450 /* XXX code should eventually be a user-exported namespace of resources */
1451 (void) task_exception_notify(EXC_RESOURCE, code, 0);
1452 }
1453
1454 void
1455 init_thread_ledgers(void) {
1456 ledger_template_t t;
1457 int idx;
1458
1459 assert(thread_ledger_template == NULL);
1460
1461 if ((t = ledger_template_create("Per-thread ledger")) == NULL)
1462 panic("couldn't create thread ledger template");
1463
1464 if ((idx = ledger_entry_add(t, "cpu_time", "sched", "ns")) < 0) {
1465 panic("couldn't create cpu_time entry for thread ledger template");
1466 }
1467
1468 if (ledger_set_callback(t, idx, thread_resource_exception,
1469 (void *)(uintptr_t)idx, NULL) < 0) {
1470 panic("couldn't set thread ledger callback for cpu_time entry");
1471 }
1472
1473 thread_ledgers.cpu_time = idx;
1474 thread_ledger_template = t;
1475 }
1476
1477 /*
1478 * Set CPU usage limit on a thread.
1479 *
1480 * Calling with percentage of 0 will unset the limit for this thread.
1481 */
1482
1483 int
1484 thread_set_cpulimit(int action, uint8_t percentage, uint64_t interval_ns)
1485 {
1486 thread_t thread = current_thread();
1487 ledger_t l;
1488 uint64_t limittime = 0;
1489 uint64_t abstime = 0;
1490
1491 assert(percentage <= 100);
1492
1493 if (percentage == 0) {
1494 /*
1495 * Remove CPU limit, if any exists.
1496 */
1497 if (thread->t_threadledger != LEDGER_NULL) {
1498 /*
1499 * The only way to get a per-thread ledger is via CPU limits.
1500 */
1501 assert(thread->options & (TH_OPT_PROC_CPULIMIT | TH_OPT_PRVT_CPULIMIT));
1502 ledger_dereference(thread->t_threadledger);
1503 thread->t_threadledger = LEDGER_NULL;
1504 thread->options &= ~(TH_OPT_PROC_CPULIMIT | TH_OPT_PRVT_CPULIMIT);
1505 }
1506
1507 return (0);
1508 }
1509
1510 l = thread->t_threadledger;
1511 if (l == LEDGER_NULL) {
1512 /*
1513 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
1514 */
1515 if ((l = ledger_instantiate(thread_ledger_template, LEDGER_CREATE_INACTIVE_ENTRIES)) == LEDGER_NULL)
1516 return (KERN_RESOURCE_SHORTAGE);
1517
1518 /*
1519 * We are the first to create this thread's ledger, so only activate our entry.
1520 */
1521 ledger_entry_setactive(l, thread_ledgers.cpu_time);
1522 thread->t_threadledger = l;
1523 }
1524
1525 /*
1526 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
1527 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
1528 */
1529 limittime = (interval_ns * percentage) / 100;
1530 nanoseconds_to_absolutetime(limittime, &abstime);
1531 ledger_set_limit(l, thread_ledgers.cpu_time, abstime);
1532 /*
1533 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
1534 */
1535 ledger_set_period(l, thread_ledgers.cpu_time, interval_ns);
1536
1537 /*
1538 * Ledgers supports multiple actions for one ledger entry, so we do too.
1539 */
1540 if (action == THREAD_CPULIMIT_EXCEPTION) {
1541 thread->options |= TH_OPT_PROC_CPULIMIT;
1542 ledger_set_action(l, thread_ledgers.cpu_time, LEDGER_ACTION_EXCEPTION);
1543 }
1544
1545 if (action == THREAD_CPULIMIT_BLOCK) {
1546 thread->options |= TH_OPT_PRVT_CPULIMIT;
1547 /* The per-thread ledger template by default has a callback for CPU time */
1548 ledger_disable_callback(l, thread_ledgers.cpu_time);
1549 ledger_set_action(l, thread_ledgers.cpu_time, LEDGER_ACTION_BLOCK);
1550 }
1551
1552 thread->t_threadledger = l;
1553 return (0);
1554 }
1555
1556 int split_funnel_off = 0;
1557 lck_grp_t *funnel_lck_grp = LCK_GRP_NULL;
1558 lck_grp_attr_t *funnel_lck_grp_attr;
1559 lck_attr_t *funnel_lck_attr;
1560
1561 funnel_t *
1562 funnel_alloc(
1563 int type)
1564 {
1565 lck_mtx_t *m;
1566 funnel_t *fnl;
1567
1568 if (funnel_lck_grp == LCK_GRP_NULL) {
1569 funnel_lck_grp_attr = lck_grp_attr_alloc_init();
1570
1571 funnel_lck_grp = lck_grp_alloc_init("Funnel", funnel_lck_grp_attr);
1572
1573 funnel_lck_attr = lck_attr_alloc_init();
1574 }
1575 if ((fnl = (funnel_t *)kalloc(sizeof(funnel_t))) != 0){
1576 bzero((void *)fnl, sizeof(funnel_t));
1577 if ((m = lck_mtx_alloc_init(funnel_lck_grp, funnel_lck_attr)) == (lck_mtx_t *)NULL) {
1578 kfree(fnl, sizeof(funnel_t));
1579 return(THR_FUNNEL_NULL);
1580 }
1581 fnl->fnl_mutex = m;
1582 fnl->fnl_type = type;
1583 }
1584 return(fnl);
1585 }
1586
1587 void
1588 funnel_free(
1589 funnel_t * fnl)
1590 {
1591 lck_mtx_free(fnl->fnl_mutex, funnel_lck_grp);
1592 if (fnl->fnl_oldmutex)
1593 lck_mtx_free(fnl->fnl_oldmutex, funnel_lck_grp);
1594 kfree(fnl, sizeof(funnel_t));
1595 }
1596
1597 void
1598 funnel_lock(
1599 funnel_t * fnl)
1600 {
1601 lck_mtx_lock(fnl->fnl_mutex);
1602 fnl->fnl_mtxholder = current_thread();
1603 }
1604
1605 void
1606 funnel_unlock(
1607 funnel_t * fnl)
1608 {
1609 lck_mtx_unlock(fnl->fnl_mutex);
1610 fnl->fnl_mtxholder = NULL;
1611 fnl->fnl_mtxrelease = current_thread();
1612 }
1613
1614 funnel_t *
1615 thread_funnel_get(
1616 void)
1617 {
1618 thread_t th = current_thread();
1619
1620 if (th->funnel_state & TH_FN_OWNED) {
1621 return(th->funnel_lock);
1622 }
1623 return(THR_FUNNEL_NULL);
1624 }
1625
1626 boolean_t
1627 thread_funnel_set(
1628 funnel_t * fnl,
1629 boolean_t funneled)
1630 {
1631 thread_t cur_thread;
1632 boolean_t funnel_state_prev;
1633 boolean_t intr;
1634
1635 cur_thread = current_thread();
1636 funnel_state_prev = ((cur_thread->funnel_state & TH_FN_OWNED) == TH_FN_OWNED);
1637
1638 if (funnel_state_prev != funneled) {
1639 intr = ml_set_interrupts_enabled(FALSE);
1640
1641 if (funneled == TRUE) {
1642 if (cur_thread->funnel_lock)
1643 panic("Funnel lock called when holding one %p", cur_thread->funnel_lock);
1644 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE,
1645 fnl, 1, 0, 0, 0);
1646 funnel_lock(fnl);
1647 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE,
1648 fnl, 1, 0, 0, 0);
1649 cur_thread->funnel_state |= TH_FN_OWNED;
1650 cur_thread->funnel_lock = fnl;
1651 } else {
1652 if(cur_thread->funnel_lock->fnl_mutex != fnl->fnl_mutex)
1653 panic("Funnel unlock when not holding funnel");
1654 cur_thread->funnel_state &= ~TH_FN_OWNED;
1655 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE,
1656 fnl, 1, 0, 0, 0);
1657
1658 cur_thread->funnel_lock = THR_FUNNEL_NULL;
1659 funnel_unlock(fnl);
1660 }
1661 (void)ml_set_interrupts_enabled(intr);
1662 } else {
1663 /* if we are trying to acquire funnel recursively
1664 * check for funnel to be held already
1665 */
1666 if (funneled && (fnl->fnl_mutex != cur_thread->funnel_lock->fnl_mutex)) {
1667 panic("thread_funnel_set: already holding a different funnel");
1668 }
1669 }
1670 return(funnel_state_prev);
1671 }
1672
1673 static void
1674 sched_call_null(
1675 __unused int type,
1676 __unused thread_t thread)
1677 {
1678 return;
1679 }
1680
1681 void
1682 thread_sched_call(
1683 thread_t thread,
1684 sched_call_t call)
1685 {
1686 thread->sched_call = (call != NULL)? call: sched_call_null;
1687 }
1688
1689 void
1690 thread_static_param(
1691 thread_t thread,
1692 boolean_t state)
1693 {
1694 thread_mtx_lock(thread);
1695 thread->static_param = state;
1696 thread_mtx_unlock(thread);
1697 }
1698
1699 uint64_t
1700 thread_tid(
1701 thread_t thread)
1702 {
1703 return (thread != THREAD_NULL? thread->thread_id: 0);
1704 }
1705
1706 uint16_t
1707 thread_set_tag(thread_t th, uint16_t tag) {
1708 return thread_set_tag_internal(th, tag);
1709 }
1710 uint16_t
1711 thread_get_tag(thread_t th) {
1712 return thread_get_tag_internal(th);
1713 }
1714
1715 uint64_t
1716 thread_dispatchqaddr(
1717 thread_t thread)
1718 {
1719 uint64_t dispatchqueue_addr = 0;
1720 uint64_t thread_handle = 0;
1721
1722 if (thread != THREAD_NULL) {
1723 thread_handle = thread->machine.cthread_self;
1724
1725 if (thread->task->bsd_info)
1726 dispatchqueue_addr = thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
1727 }
1728
1729 return (dispatchqueue_addr);
1730 }
1731
1732 /*
1733 * Export routines to other components for things that are done as macros
1734 * within the osfmk component.
1735 */
1736
1737 #undef thread_reference
1738 void thread_reference(thread_t thread);
1739 void
1740 thread_reference(
1741 thread_t thread)
1742 {
1743 if (thread != THREAD_NULL)
1744 thread_reference_internal(thread);
1745 }
1746
1747 #undef thread_should_halt
1748
1749 boolean_t
1750 thread_should_halt(
1751 thread_t th)
1752 {
1753 return (thread_should_halt_fast(th));
1754 }
1755
1756 #if CONFIG_DTRACE
1757 uint32_t dtrace_get_thread_predcache(thread_t thread)
1758 {
1759 if (thread != THREAD_NULL)
1760 return thread->t_dtrace_predcache;
1761 else
1762 return 0;
1763 }
1764
1765 int64_t dtrace_get_thread_vtime(thread_t thread)
1766 {
1767 if (thread != THREAD_NULL)
1768 return thread->t_dtrace_vtime;
1769 else
1770 return 0;
1771 }
1772
1773 int64_t dtrace_get_thread_tracing(thread_t thread)
1774 {
1775 if (thread != THREAD_NULL)
1776 return thread->t_dtrace_tracing;
1777 else
1778 return 0;
1779 }
1780
1781 boolean_t dtrace_get_thread_reentering(thread_t thread)
1782 {
1783 if (thread != THREAD_NULL)
1784 return (thread->options & TH_OPT_DTRACE) ? TRUE : FALSE;
1785 else
1786 return 0;
1787 }
1788
1789 vm_offset_t dtrace_get_kernel_stack(thread_t thread)
1790 {
1791 if (thread != THREAD_NULL)
1792 return thread->kernel_stack;
1793 else
1794 return 0;
1795 }
1796
1797 int64_t dtrace_calc_thread_recent_vtime(thread_t thread)
1798 {
1799 if (thread != THREAD_NULL) {
1800 processor_t processor = current_processor();
1801 uint64_t abstime = mach_absolute_time();
1802 timer_t timer;
1803
1804 timer = PROCESSOR_DATA(processor, thread_timer);
1805
1806 return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer)) +
1807 (abstime - timer->tstamp); /* XXX need interrupts off to prevent missed time? */
1808 } else
1809 return 0;
1810 }
1811
1812 void dtrace_set_thread_predcache(thread_t thread, uint32_t predcache)
1813 {
1814 if (thread != THREAD_NULL)
1815 thread->t_dtrace_predcache = predcache;
1816 }
1817
1818 void dtrace_set_thread_vtime(thread_t thread, int64_t vtime)
1819 {
1820 if (thread != THREAD_NULL)
1821 thread->t_dtrace_vtime = vtime;
1822 }
1823
1824 void dtrace_set_thread_tracing(thread_t thread, int64_t accum)
1825 {
1826 if (thread != THREAD_NULL)
1827 thread->t_dtrace_tracing = accum;
1828 }
1829
1830 void dtrace_set_thread_reentering(thread_t thread, boolean_t vbool)
1831 {
1832 if (thread != THREAD_NULL) {
1833 if (vbool)
1834 thread->options |= TH_OPT_DTRACE;
1835 else
1836 thread->options &= (~TH_OPT_DTRACE);
1837 }
1838 }
1839
1840 vm_offset_t dtrace_set_thread_recover(thread_t thread, vm_offset_t recover)
1841 {
1842 vm_offset_t prev = 0;
1843
1844 if (thread != THREAD_NULL) {
1845 prev = thread->recover;
1846 thread->recover = recover;
1847 }
1848 return prev;
1849 }
1850
1851 void dtrace_thread_bootstrap(void)
1852 {
1853 task_t task = current_task();
1854 if(task->thread_count == 1) {
1855 DTRACE_PROC(start);
1856 }
1857 DTRACE_PROC(lwp__start);
1858
1859 }
1860 #endif /* CONFIG_DTRACE */