+ if ( !parent_task->active || parent_task->halting ||
+ ((options & TH_OPTION_NOSUSP) != 0 &&
+ parent_task->suspend_count > 0) ||
+ (parent_task->thread_count >= task_threadmax &&
+ parent_task != kernel_task) ) {
+ task_unlock(parent_task);
+ lck_mtx_unlock(&tasks_threads_lock);
+
+#ifdef MACH_BSD
+ {
+ void *ut = new_thread->uthread;
+
+ new_thread->uthread = NULL;
+ uthread_cleanup(parent_task, ut, parent_task->bsd_info, FALSE);
+ /* cred free may not be necessary */
+ uthread_cred_free(ut);
+ uthread_zone_free(ut);
+ }
+#endif /* MACH_BSD */
+ ipc_thread_disable(new_thread);
+ ipc_thread_terminate(new_thread);
+ kfree(new_thread->thread_io_stats, sizeof(struct io_stat_info));
+ lck_mtx_destroy(&new_thread->mutex, &thread_lck_grp);
+ machine_thread_destroy(new_thread);
+ zfree(thread_zone, new_thread);
+ return (KERN_FAILURE);
+ }
+
+ /* New threads inherit any default state on the task */
+ machine_thread_inherit_taskwide(new_thread, parent_task);
+
+ task_reference_internal(parent_task);
+
+ if (new_thread->task->rusage_cpu_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) {
+ /*
+ * This task has a per-thread CPU limit; make sure this new thread
+ * gets its limit set too, before it gets out of the kernel.
+ */
+ set_astledger(new_thread);
+ }
+
+ /* Instantiate a thread ledger. Do not fail thread creation if ledger creation fails. */
+ if ((new_thread->t_threadledger = ledger_instantiate(thread_ledger_template,
+ LEDGER_CREATE_INACTIVE_ENTRIES)) != LEDGER_NULL) {
+
+ ledger_entry_setactive(new_thread->t_threadledger, thread_ledgers.cpu_time);
+ }
+
+ new_thread->cpu_time_last_qos = 0;
+#ifdef CONFIG_BANK
+ new_thread->t_bankledger = LEDGER_NULL;
+ new_thread->t_deduct_bank_ledger_time = 0;
+#endif
+
+ new_thread->t_ledger = new_thread->task->ledger;
+ if (new_thread->t_ledger)
+ ledger_reference(new_thread->t_ledger);
+
+#if defined(CONFIG_SCHED_MULTIQ)
+ /* Cache the task's sched_group */
+ new_thread->sched_group = parent_task->sched_group;
+#endif /* defined(CONFIG_SCHED_MULTIQ) */
+
+ /* Cache the task's map */
+ new_thread->map = parent_task->map;
+
+ timer_call_setup(&new_thread->wait_timer, thread_timer_expire, new_thread);
+ timer_call_setup(&new_thread->depress_timer, thread_depress_expire, new_thread);
+
+#if KPC
+ kpc_thread_create(new_thread);
+#endif
+
+ /* Only need to update policies pushed from task to thread */
+ new_thread->requested_policy.bg_iotier = parent_task->effective_policy.bg_iotier;
+ new_thread->requested_policy.terminated = parent_task->effective_policy.terminated;
+
+ /* Set the thread's scheduling parameters */
+#if defined(CONFIG_SCHED_TIMESHARE_CORE)
+ new_thread->sched_stamp = sched_tick;
+ new_thread->pri_shift = sched_pri_shift;
+#endif /* defined(CONFIG_SCHED_TIMESHARE_CORE) */
+
+ new_thread->sched_mode = SCHED(initial_thread_sched_mode)(parent_task);
+ new_thread->sched_flags = 0;
+ new_thread->max_priority = parent_task->max_priority;
+ new_thread->task_priority = parent_task->priority;
+
+ int new_priority = (priority < 0) ? parent_task->priority: priority;
+ new_priority = (priority < 0)? parent_task->priority: priority;
+ if (new_priority > new_thread->max_priority)
+ new_priority = new_thread->max_priority;
+
+ new_thread->importance = new_priority - new_thread->task_priority;
+ new_thread->saved_importance = new_thread->importance;
+
+ if (parent_task->max_priority <= MAXPRI_THROTTLE) {
+ sched_set_thread_throttled(new_thread, TRUE);
+ }
+
+ sched_set_thread_base_priority(new_thread, new_priority);
+
+ thread_policy_create(new_thread);
+
+ /* Chain the thread onto the task's list */
+ queue_enter(&parent_task->threads, new_thread, thread_t, task_threads);
+ parent_task->thread_count++;
+
+ /* So terminating threads don't need to take the task lock to decrement */
+ hw_atomic_add(&parent_task->active_thread_count, 1);
+
+ /* Protected by the tasks_threads_lock */
+ new_thread->thread_id = ++thread_unique_id;
+
+ queue_enter(&threads, new_thread, thread_t, threads);
+ threads_count++;
+
+ new_thread->active = TRUE;
+ new_thread->inspection = FALSE;
+ *out_thread = new_thread;
+
+ {
+ long dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4;
+
+ kdbg_trace_data(parent_task->bsd_info, &dbg_arg2);
+
+ KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
+ TRACEDBG_CODE(DBG_TRACE_DATA, 1) | DBG_FUNC_NONE,
+ (vm_address_t)(uintptr_t)thread_tid(new_thread), dbg_arg2, 0, 0, 0);
+
+ kdbg_trace_string(parent_task->bsd_info,
+ &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4);
+
+ KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
+ TRACEDBG_CODE(DBG_TRACE_STRING, 1) | DBG_FUNC_NONE,
+ dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0);
+ }
+
+ DTRACE_PROC1(lwp__create, thread_t, *out_thread);
+
+ return (KERN_SUCCESS);
+}
+
+static kern_return_t
+thread_create_internal2(
+ task_t task,
+ thread_t *new_thread,
+ boolean_t from_user,
+ thread_continue_t continuation)
+{
+ kern_return_t result;
+ thread_t thread;
+
+ if (task == TASK_NULL || task == kernel_task)
+ return (KERN_INVALID_ARGUMENT);
+
+ result = thread_create_internal(task, -1, continuation, TH_OPTION_NONE, &thread);
+ if (result != KERN_SUCCESS)
+ return (result);
+
+ thread->user_stop_count = 1;
+ thread_hold(thread);
+ if (task->suspend_count > 0)
+ thread_hold(thread);
+
+ if (from_user)
+ extmod_statistics_incr_thread_create(task);
+
+ task_unlock(task);
+ lck_mtx_unlock(&tasks_threads_lock);
+
+ *new_thread = thread;
+
+ return (KERN_SUCCESS);
+}
+
+/* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
+kern_return_t
+thread_create(
+ task_t task,
+ thread_t *new_thread);
+
+kern_return_t
+thread_create(
+ task_t task,
+ thread_t *new_thread)
+{
+ return thread_create_internal2(task, new_thread, FALSE, (thread_continue_t)thread_bootstrap_return);
+}
+
+kern_return_t
+thread_create_from_user(
+ task_t task,
+ thread_t *new_thread)
+{
+ return thread_create_internal2(task, new_thread, TRUE, (thread_continue_t)thread_bootstrap_return);
+}
+
+kern_return_t
+thread_create_with_continuation(
+ task_t task,
+ thread_t *new_thread,
+ thread_continue_t continuation)
+{
+ return thread_create_internal2(task, new_thread, FALSE, continuation);
+}
+
+static kern_return_t
+thread_create_running_internal2(
+ register task_t task,
+ int flavor,
+ thread_state_t new_state,
+ mach_msg_type_number_t new_state_count,
+ thread_t *new_thread,
+ boolean_t from_user)
+{
+ register kern_return_t result;
+ thread_t thread;
+
+ if (task == TASK_NULL || task == kernel_task)
+ return (KERN_INVALID_ARGUMENT);
+
+ result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
+ if (result != KERN_SUCCESS)
+ return (result);
+
+ result = machine_thread_set_state(thread, flavor, new_state, new_state_count);
+ if (result != KERN_SUCCESS) {
+ task_unlock(task);
+ lck_mtx_unlock(&tasks_threads_lock);
+
+ thread_terminate(thread);
+ thread_deallocate(thread);
+ return (result);
+ }
+
+ thread_mtx_lock(thread);
+ thread_start_internal(thread);
+ thread_mtx_unlock(thread);
+
+ if (from_user)
+ extmod_statistics_incr_thread_create(task);
+
+ task_unlock(task);
+ lck_mtx_unlock(&tasks_threads_lock);
+
+ *new_thread = thread;
+
+ return (result);
+}
+
+/* Prototype, see justification above */
+kern_return_t
+thread_create_running(
+ register task_t task,
+ int flavor,
+ thread_state_t new_state,
+ mach_msg_type_number_t new_state_count,
+ thread_t *new_thread);
+
+kern_return_t
+thread_create_running(
+ register task_t task,
+ int flavor,
+ thread_state_t new_state,
+ mach_msg_type_number_t new_state_count,
+ thread_t *new_thread)
+{
+ return thread_create_running_internal2(
+ task, flavor, new_state, new_state_count,
+ new_thread, FALSE);
+}
+
+kern_return_t
+thread_create_running_from_user(
+ register task_t task,
+ int flavor,
+ thread_state_t new_state,
+ mach_msg_type_number_t new_state_count,
+ thread_t *new_thread)
+{
+ return thread_create_running_internal2(
+ task, flavor, new_state, new_state_count,
+ new_thread, TRUE);
+}
+
+kern_return_t
+thread_create_workq(
+ task_t task,
+ thread_continue_t thread_return,
+ thread_t *new_thread)
+{
+ kern_return_t result;
+ thread_t thread;
+
+ if (task == TASK_NULL || task == kernel_task)
+ return (KERN_INVALID_ARGUMENT);
+
+ result = thread_create_internal(task, -1, thread_return, TH_OPTION_NOCRED | TH_OPTION_NOSUSP, &thread);
+ if (result != KERN_SUCCESS)
+ return (result);
+
+ thread->user_stop_count = 1;
+ thread_hold(thread);
+ if (task->suspend_count > 0)
+ thread_hold(thread);
+
+ task_unlock(task);
+ lck_mtx_unlock(&tasks_threads_lock);
+
+ *new_thread = thread;
+
+ return (KERN_SUCCESS);
+}
+
+/*
+ * kernel_thread_create:
+ *
+ * Create a thread in the kernel task
+ * to execute in kernel context.
+ */
+kern_return_t
+kernel_thread_create(
+ thread_continue_t continuation,
+ void *parameter,
+ integer_t priority,
+ thread_t *new_thread)
+{
+ kern_return_t result;
+ thread_t thread;
+ task_t task = kernel_task;
+
+ result = thread_create_internal(task, priority, continuation, TH_OPTION_NONE, &thread);
+ if (result != KERN_SUCCESS)
+ return (result);
+
+ task_unlock(task);
+ lck_mtx_unlock(&tasks_threads_lock);
+
+ stack_alloc(thread);
+ assert(thread->kernel_stack != 0);
+ thread->reserved_stack = thread->kernel_stack;
+
+ thread->parameter = parameter;
+
+if(debug_task & 1)
+ kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread, continuation);
+ *new_thread = thread;
+
+ return (result);
+}
+
+kern_return_t
+kernel_thread_start_priority(
+ thread_continue_t continuation,
+ void *parameter,
+ integer_t priority,
+ thread_t *new_thread)
+{
+ kern_return_t result;
+ thread_t thread;
+
+ result = kernel_thread_create(continuation, parameter, priority, &thread);
+ if (result != KERN_SUCCESS)
+ return (result);
+
+ *new_thread = thread;
+
+ thread_mtx_lock(thread);
+ thread_start_internal(thread);
+ thread_mtx_unlock(thread);
+
+ return (result);
+}
+
+kern_return_t
+kernel_thread_start(
+ thread_continue_t continuation,
+ void *parameter,
+ thread_t *new_thread)
+{
+ return kernel_thread_start_priority(continuation, parameter, -1, new_thread);
+}
+
+/* Separated into helper function so it can be used by THREAD_BASIC_INFO and THREAD_EXTENDED_INFO */
+/* it is assumed that the thread is locked by the caller */
+static void
+retrieve_thread_basic_info(thread_t thread, thread_basic_info_t basic_info)
+{
+ int state, flags;
+
+ /* fill in info */
+
+ thread_read_times(thread, &basic_info->user_time,
+ &basic_info->system_time);
+
+ /*
+ * Update lazy-evaluated scheduler info because someone wants it.
+ */
+ if (SCHED(can_update_priority)(thread))
+ SCHED(update_priority)(thread);
+
+ basic_info->sleep_time = 0;
+
+ /*
+ * To calculate cpu_usage, first correct for timer rate,
+ * then for 5/8 ageing. The correction factor [3/5] is
+ * (1/(5/8) - 1).
+ */
+ basic_info->cpu_usage = 0;
+#if defined(CONFIG_SCHED_TIMESHARE_CORE)
+ if (sched_tick_interval) {
+ basic_info->cpu_usage = (integer_t)(((uint64_t)thread->cpu_usage
+ * TH_USAGE_SCALE) / sched_tick_interval);
+ basic_info->cpu_usage = (basic_info->cpu_usage * 3) / 5;
+ }
+#endif
+
+ if (basic_info->cpu_usage > TH_USAGE_SCALE)
+ basic_info->cpu_usage = TH_USAGE_SCALE;
+
+ basic_info->policy = ((thread->sched_mode == TH_MODE_TIMESHARE)?
+ POLICY_TIMESHARE: POLICY_RR);
+
+ flags = 0;
+ if (thread->options & TH_OPT_IDLE_THREAD)
+ flags |= TH_FLAGS_IDLE;
+
+ if (thread->options & TH_OPT_GLOBAL_FORCED_IDLE) {
+ flags |= TH_FLAGS_GLOBAL_FORCED_IDLE;
+ }
+
+ if (!thread->kernel_stack)
+ flags |= TH_FLAGS_SWAPPED;
+
+ state = 0;
+ if (thread->state & TH_TERMINATE)
+ state = TH_STATE_HALTED;
+ else
+ if (thread->state & TH_RUN)
+ state = TH_STATE_RUNNING;
+ else
+ if (thread->state & TH_UNINT)
+ state = TH_STATE_UNINTERRUPTIBLE;
+ else
+ if (thread->state & TH_SUSP)
+ state = TH_STATE_STOPPED;
+ else
+ if (thread->state & TH_WAIT)
+ state = TH_STATE_WAITING;
+
+ basic_info->run_state = state;
+ basic_info->flags = flags;
+
+ basic_info->suspend_count = thread->user_stop_count;
+
+ return;
+}
+
+kern_return_t
+thread_info_internal(
+ register thread_t thread,
+ thread_flavor_t flavor,
+ thread_info_t thread_info_out, /* ptr to OUT array */
+ mach_msg_type_number_t *thread_info_count) /*IN/OUT*/
+{
+ spl_t s;
+
+ if (thread == THREAD_NULL)
+ return (KERN_INVALID_ARGUMENT);