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