+
+ timer_call_quantum_timer_enter(&processor->quantum_timer, thread,
+ processor->quantum_end, ctime);
+
+ /* Tell platform layer that we are still running this thread */
+ urgency = thread_get_urgency(thread, &ignore1, &ignore2);
+ machine_thread_going_on_core(thread, urgency, 0, 0, ctime);
+ machine_switch_perfcontrol_state_update(QUANTUM_EXPIRY, ctime,
+ 0, thread);
+
+#if defined(CONFIG_SCHED_TIMESHARE_CORE)
+ sched_timeshare_consider_maintenance(ctime);
+#endif /* CONFIG_SCHED_TIMESHARE_CORE */
+
+#if __arm__ || __arm64__
+ if (thread->sched_mode == TH_MODE_REALTIME)
+ sched_consider_recommended_cores(ctime, thread);
+#endif /* __arm__ || __arm64__ */
+
+ KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_QUANTUM_EXPIRED) | DBG_FUNC_END, preempt, 0, 0, 0, 0);
+}
+
+/*
+ * sched_set_thread_base_priority:
+ *
+ * Set the base priority of the thread
+ * and reset its scheduled priority.
+ *
+ * This is the only path to change base_pri.
+ *
+ * Called with the thread locked.
+ */
+void
+sched_set_thread_base_priority(thread_t thread, int priority)
+{
+ assert(priority >= MINPRI);
+ uint64_t ctime = 0;
+
+ if (thread->sched_mode == TH_MODE_REALTIME)
+ assert(priority <= BASEPRI_RTQUEUES);
+ else
+ assert(priority < BASEPRI_RTQUEUES);
+
+ int old_base_pri = thread->base_pri;
+ thread->base_pri = priority;
+
+ if ((thread->state & TH_RUN) == TH_RUN) {
+ assert(thread->last_made_runnable_time != THREAD_NOT_RUNNABLE);
+ ctime = mach_approximate_time();
+ thread->last_basepri_change_time = ctime;
+ } else {
+ assert(thread->last_basepri_change_time == THREAD_NOT_RUNNABLE);
+ assert(thread->last_made_runnable_time == THREAD_NOT_RUNNABLE);
+ }
+
+ /*
+ * Currently the perfcontrol_attr depends on the base pri of the
+ * thread. Therefore, we use this function as the hook for the
+ * perfcontrol callout.
+ */
+ if (thread == current_thread() && old_base_pri != priority) {
+ if (!ctime) {
+ ctime = mach_approximate_time();
+ }
+ machine_switch_perfcontrol_state_update(PERFCONTROL_ATTR_UPDATE,
+ ctime, PERFCONTROL_CALLOUT_WAKE_UNSAFE, thread);
+ }
+ sched_update_thread_bucket(thread);
+
+ thread_recompute_sched_pri(thread, FALSE);
+}
+
+/*
+ * thread_recompute_sched_pri:
+ *
+ * Reset the scheduled priority of the thread
+ * according to its base priority if the
+ * thread has not been promoted or depressed.
+ *
+ * This is the standard way to push base_pri changes into sched_pri,
+ * or to recalculate the appropriate sched_pri after clearing
+ * a promotion or depression.
+ *
+ * Called at splsched with the thread locked.
+ */
+void
+thread_recompute_sched_pri(
+ thread_t thread,
+ boolean_t override_depress)
+{
+ int priority;
+
+ if (thread->sched_mode == TH_MODE_TIMESHARE)
+ priority = SCHED(compute_timeshare_priority)(thread);
+ else
+ priority = thread->base_pri;
+
+ if ((!(thread->sched_flags & TH_SFLAG_PROMOTED_MASK) || (priority > thread->sched_pri)) &&
+ (!(thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) || override_depress)) {
+ set_sched_pri(thread, priority);
+ }
+}
+
+void
+sched_default_quantum_expire(thread_t thread __unused)
+{
+ /*
+ * No special behavior when a timeshare, fixed, or realtime thread
+ * uses up its entire quantum
+ */
+}
+
+#if defined(CONFIG_SCHED_TIMESHARE_CORE)
+
+/*
+ * lightweight_update_priority:
+ *
+ * Update the scheduled priority for
+ * a timesharing thread.
+ *
+ * Only for use on the current thread.
+ *
+ * Called with the thread locked.
+ */
+void
+lightweight_update_priority(thread_t thread)
+{
+ assert(thread->runq == PROCESSOR_NULL);
+ assert(thread == current_thread());
+
+ if (thread->sched_mode == TH_MODE_TIMESHARE) {
+ int priority;
+ uint32_t delta;
+
+ thread_timer_delta(thread, delta);
+
+ /*
+ * Accumulate timesharing usage only
+ * during contention for processor
+ * resources.
+ */
+ if (thread->pri_shift < INT8_MAX)
+ thread->sched_usage += delta;
+
+ thread->cpu_delta += delta;
+
+ priority = sched_compute_timeshare_priority(thread);
+
+ /*
+ * Adjust the scheduled priority like thread_recompute_sched_pri,
+ * except with the benefit of knowing the thread is on this core.
+ */
+ if ((!(thread->sched_flags & TH_SFLAG_PROMOTED_MASK) || (priority > thread->sched_pri)) &&
+ (!(thread->sched_flags & TH_SFLAG_DEPRESSED_MASK)) &&
+ priority != thread->sched_pri) {
+
+ thread->sched_pri = priority;
+
+ KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_CHANGE_PRIORITY),
+ (uintptr_t)thread_tid(thread),
+ thread->base_pri,
+ thread->sched_pri,
+ 0, /* eventually, 'reason' */
+ 0);
+ }
+ }
+}
+
+/*
+ * Define shifts for simulating (5/8) ** n
+ *
+ * Shift structures for holding update shifts. Actual computation
+ * is usage = (usage >> shift1) +/- (usage >> abs(shift2)) where the
+ * +/- is determined by the sign of shift 2.
+ */
+struct shift_data {
+ int shift1;
+ int shift2;
+};
+
+#define SCHED_DECAY_TICKS 32
+static struct shift_data sched_decay_shifts[SCHED_DECAY_TICKS] = {
+ {1,1},{1,3},{1,-3},{2,-7},{3,5},{3,-5},{4,-8},{5,7},
+ {5,-7},{6,-10},{7,10},{7,-9},{8,-11},{9,12},{9,-11},{10,-13},
+ {11,14},{11,-13},{12,-15},{13,17},{13,-15},{14,-17},{15,19},{16,18},
+ {16,-19},{17,22},{18,20},{18,-20},{19,26},{20,22},{20,-22},{21,-27}
+};
+
+/*
+ * sched_compute_timeshare_priority:
+ *
+ * Calculate the timesharing priority based upon usage and load.
+ */
+extern int sched_pri_decay_band_limit;
+
+#ifdef CONFIG_EMBEDDED
+
+int
+sched_compute_timeshare_priority(thread_t thread)
+{
+ int decay_amount = (thread->sched_usage >> thread->pri_shift);
+ int decay_limit = sched_pri_decay_band_limit;
+
+ if (thread->base_pri > BASEPRI_FOREGROUND) {
+ decay_limit += (thread->base_pri - BASEPRI_FOREGROUND);
+ }
+
+ if (decay_amount > decay_limit) {
+ decay_amount = decay_limit;
+ }
+
+ /* start with base priority */
+ int priority = thread->base_pri - decay_amount;
+
+ if (priority < MAXPRI_THROTTLE) {
+ if (thread->task->max_priority > MAXPRI_THROTTLE) {
+ priority = MAXPRI_THROTTLE;
+ } else if (priority < MINPRI_USER) {
+ priority = MINPRI_USER;
+ }
+ } else if (priority > MAXPRI_KERNEL) {
+ priority = MAXPRI_KERNEL;
+ }
+
+ return priority;
+}
+
+#else /* CONFIG_EMBEDDED */
+
+int
+sched_compute_timeshare_priority(thread_t thread)
+{
+ /* start with base priority */
+ int priority = thread->base_pri - (thread->sched_usage >> thread->pri_shift);
+
+ if (priority < MINPRI_USER)
+ priority = MINPRI_USER;
+ else if (priority > MAXPRI_KERNEL)
+ priority = MAXPRI_KERNEL;
+
+ return priority;
+}
+
+#endif /* CONFIG_EMBEDDED */
+
+/*
+ * can_update_priority
+ *
+ * Make sure we don't do re-dispatches more frequently than a scheduler tick.
+ *
+ * Called with the thread locked.
+ */
+boolean_t
+can_update_priority(
+ thread_t thread)
+{
+ if (sched_tick == thread->sched_stamp)
+ return (FALSE);
+ else
+ return (TRUE);
+}
+
+/*
+ * update_priority
+ *
+ * Perform housekeeping operations driven by scheduler tick.
+ *
+ * Called with the thread locked.
+ */
+void
+update_priority(
+ thread_t thread)
+{
+ uint32_t ticks, delta;
+
+ ticks = sched_tick - thread->sched_stamp;
+ assert(ticks != 0);
+
+ thread->sched_stamp += ticks;
+
+ thread->pri_shift = sched_pri_shifts[thread->th_sched_bucket];
+
+ /* If requested, accelerate aging of sched_usage */
+ if (sched_decay_usage_age_factor > 1)
+ ticks *= sched_decay_usage_age_factor;
+
+ /*
+ * Gather cpu usage data.
+ */
+ thread_timer_delta(thread, delta);
+ if (ticks < SCHED_DECAY_TICKS) {
+ /*
+ * Accumulate timesharing usage only
+ * during contention for processor
+ * resources.
+ */
+ if (thread->pri_shift < INT8_MAX)
+ thread->sched_usage += delta;
+
+ thread->cpu_usage += delta + thread->cpu_delta;
+ thread->cpu_delta = 0;
+
+ struct shift_data *shiftp = &sched_decay_shifts[ticks];
+
+ if (shiftp->shift2 > 0) {
+ thread->cpu_usage = (thread->cpu_usage >> shiftp->shift1) +
+ (thread->cpu_usage >> shiftp->shift2);
+ thread->sched_usage = (thread->sched_usage >> shiftp->shift1) +
+ (thread->sched_usage >> shiftp->shift2);
+ } else {
+ thread->cpu_usage = (thread->cpu_usage >> shiftp->shift1) -
+ (thread->cpu_usage >> -(shiftp->shift2));
+ thread->sched_usage = (thread->sched_usage >> shiftp->shift1) -
+ (thread->sched_usage >> -(shiftp->shift2));
+ }
+ } else {
+ thread->cpu_usage = thread->cpu_delta = 0;
+ thread->sched_usage = 0;
+ }
+
+ /*
+ * Check for fail-safe release.
+ */
+ if ((thread->sched_flags & TH_SFLAG_FAILSAFE) &&
+ mach_absolute_time() >= thread->safe_release) {
+ sched_thread_mode_undemote(thread, TH_SFLAG_FAILSAFE);
+ }