]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sched_prim.c
xnu-344.49.tar.gz
[apple/xnu.git] / osfmk / kern / sched_prim.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_FREE_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55 /*
56 * File: sched_prim.c
57 * Author: Avadis Tevanian, Jr.
58 * Date: 1986
59 *
60 * Scheduling primitives
61 *
62 */
63
64 #include <debug.h>
65 #include <cpus.h>
66 #include <mach_kdb.h>
67 #include <simple_clock.h>
68 #include <power_save.h>
69 #include <task_swapper.h>
70
71 #include <ddb/db_output.h>
72 #include <mach/machine.h>
73 #include <machine/machine_routines.h>
74 #include <machine/sched_param.h>
75 #include <kern/ast.h>
76 #include <kern/clock.h>
77 #include <kern/counters.h>
78 #include <kern/cpu_number.h>
79 #include <kern/cpu_data.h>
80 #include <kern/etap_macros.h>
81 #include <kern/lock.h>
82 #include <kern/macro_help.h>
83 #include <kern/machine.h>
84 #include <kern/misc_protos.h>
85 #include <kern/processor.h>
86 #include <kern/queue.h>
87 #include <kern/sched.h>
88 #include <kern/sched_prim.h>
89 #include <kern/syscall_subr.h>
90 #include <kern/task.h>
91 #include <kern/thread.h>
92 #include <kern/thread_swap.h>
93 #include <vm/pmap.h>
94 #include <vm/vm_kern.h>
95 #include <vm/vm_map.h>
96 #include <mach/policy.h>
97 #include <mach/sync_policy.h>
98 #include <kern/mk_sp.h> /*** ??? fix so this can be removed ***/
99 #include <sys/kdebug.h>
100
101 #if TASK_SWAPPER
102 #include <kern/task_swap.h>
103 extern int task_swap_on;
104 #endif /* TASK_SWAPPER */
105
106 extern int hz;
107
108 #define DEFAULT_PREEMPTION_RATE 100 /* (1/s) */
109 int default_preemption_rate = DEFAULT_PREEMPTION_RATE;
110
111 #define MAX_UNSAFE_QUANTA 800
112 int max_unsafe_quanta = MAX_UNSAFE_QUANTA;
113
114 #define MAX_POLL_QUANTA 2
115 int max_poll_quanta = MAX_POLL_QUANTA;
116
117 #define SCHED_POLL_YIELD_SHIFT 4 /* 1/16 */
118 int sched_poll_yield_shift = SCHED_POLL_YIELD_SHIFT;
119
120 uint32_t std_quantum_us;
121
122 unsigned sched_tick;
123
124 #if SIMPLE_CLOCK
125 int sched_usec;
126 #endif /* SIMPLE_CLOCK */
127
128 /* Forwards */
129 void wait_queues_init(void);
130
131 thread_t choose_pset_thread(
132 processor_t myprocessor,
133 processor_set_t pset);
134
135 thread_t choose_thread(
136 processor_t myprocessor);
137
138 boolean_t run_queue_enqueue(
139 run_queue_t runq,
140 thread_t thread,
141 boolean_t tail);
142
143 void do_thread_scan(void);
144
145 #if DEBUG
146 void dump_run_queues(
147 run_queue_t rq);
148 void dump_run_queue_struct(
149 run_queue_t rq);
150 void dump_processor(
151 processor_t p);
152 void dump_processor_set(
153 processor_set_t ps);
154
155 void checkrq(
156 run_queue_t rq,
157 char *msg);
158
159 void thread_check(
160 thread_t thread,
161 run_queue_t runq);
162
163 static
164 boolean_t thread_runnable(
165 thread_t thread);
166
167 #endif /*DEBUG*/
168
169
170 /*
171 * State machine
172 *
173 * states are combinations of:
174 * R running
175 * W waiting (or on wait queue)
176 * N non-interruptible
177 * O swapped out
178 * I being swapped in
179 *
180 * init action
181 * assert_wait thread_block clear_wait swapout swapin
182 *
183 * R RW, RWN R; setrun - -
184 * RN RWN RN; setrun - -
185 *
186 * RW W R -
187 * RWN WN RN -
188 *
189 * W R; setrun WO
190 * WN RN; setrun -
191 *
192 * RO - - R
193 *
194 */
195
196 /*
197 * Waiting protocols and implementation:
198 *
199 * Each thread may be waiting for exactly one event; this event
200 * is set using assert_wait(). That thread may be awakened either
201 * by performing a thread_wakeup_prim() on its event,
202 * or by directly waking that thread up with clear_wait().
203 *
204 * The implementation of wait events uses a hash table. Each
205 * bucket is queue of threads having the same hash function
206 * value; the chain for the queue (linked list) is the run queue
207 * field. [It is not possible to be waiting and runnable at the
208 * same time.]
209 *
210 * Locks on both the thread and on the hash buckets govern the
211 * wait event field and the queue chain field. Because wakeup
212 * operations only have the event as an argument, the event hash
213 * bucket must be locked before any thread.
214 *
215 * Scheduling operations may also occur at interrupt level; therefore,
216 * interrupts below splsched() must be prevented when holding
217 * thread or hash bucket locks.
218 *
219 * The wait event hash table declarations are as follows:
220 */
221
222 #define NUMQUEUES 59
223
224 struct wait_queue wait_queues[NUMQUEUES];
225
226 #define wait_hash(event) \
227 ((((int)(event) < 0)? ~(int)(event): (int)(event)) % NUMQUEUES)
228
229 void
230 sched_init(void)
231 {
232 /*
233 * Calculate the timeslicing quantum
234 * in us.
235 */
236 if (default_preemption_rate < 1)
237 default_preemption_rate = DEFAULT_PREEMPTION_RATE;
238 std_quantum_us = (1000 * 1000) / default_preemption_rate;
239
240 printf("standard timeslicing quantum is %d us\n", std_quantum_us);
241
242 wait_queues_init();
243 pset_sys_bootstrap(); /* initialize processor mgmt. */
244 processor_action();
245 sched_tick = 0;
246 #if SIMPLE_CLOCK
247 sched_usec = 0;
248 #endif /* SIMPLE_CLOCK */
249 ast_init();
250 }
251
252 void
253 wait_queues_init(void)
254 {
255 register int i;
256
257 for (i = 0; i < NUMQUEUES; i++) {
258 wait_queue_init(&wait_queues[i], SYNC_POLICY_FIFO);
259 }
260 }
261
262 /*
263 * Thread wait timer expiration.
264 */
265 void
266 thread_timer_expire(
267 timer_call_param_t p0,
268 timer_call_param_t p1)
269 {
270 thread_t thread = p0;
271 spl_t s;
272
273 s = splsched();
274 wake_lock(thread);
275 if (--thread->wait_timer_active == 1) {
276 if (thread->wait_timer_is_set) {
277 thread->wait_timer_is_set = FALSE;
278 thread_lock(thread);
279 if (thread->active)
280 clear_wait_internal(thread, THREAD_TIMED_OUT);
281 thread_unlock(thread);
282 }
283 }
284 else
285 if (thread->wait_timer_active == 0)
286 thread_wakeup_one(&thread->wait_timer_active);
287 wake_unlock(thread);
288 splx(s);
289 }
290
291 /*
292 * thread_set_timer:
293 *
294 * Set a timer for the current thread, if the thread
295 * is ready to wait. Must be called between assert_wait()
296 * and thread_block().
297 */
298 void
299 thread_set_timer(
300 uint32_t interval,
301 uint32_t scale_factor)
302 {
303 thread_t thread = current_thread();
304 uint64_t deadline;
305 spl_t s;
306
307 s = splsched();
308 wake_lock(thread);
309 thread_lock(thread);
310 if ((thread->state & TH_WAIT) != 0) {
311 clock_interval_to_deadline(interval, scale_factor, &deadline);
312 timer_call_enter(&thread->wait_timer, deadline);
313 assert(!thread->wait_timer_is_set);
314 thread->wait_timer_active++;
315 thread->wait_timer_is_set = TRUE;
316 }
317 thread_unlock(thread);
318 wake_unlock(thread);
319 splx(s);
320 }
321
322 void
323 thread_set_timer_deadline(
324 uint64_t deadline)
325 {
326 thread_t thread = current_thread();
327 spl_t s;
328
329 s = splsched();
330 wake_lock(thread);
331 thread_lock(thread);
332 if ((thread->state & TH_WAIT) != 0) {
333 timer_call_enter(&thread->wait_timer, deadline);
334 assert(!thread->wait_timer_is_set);
335 thread->wait_timer_active++;
336 thread->wait_timer_is_set = TRUE;
337 }
338 thread_unlock(thread);
339 wake_unlock(thread);
340 splx(s);
341 }
342
343 void
344 thread_cancel_timer(void)
345 {
346 thread_t thread = current_thread();
347 spl_t s;
348
349 s = splsched();
350 wake_lock(thread);
351 if (thread->wait_timer_is_set) {
352 if (timer_call_cancel(&thread->wait_timer))
353 thread->wait_timer_active--;
354 thread->wait_timer_is_set = FALSE;
355 }
356 wake_unlock(thread);
357 splx(s);
358 }
359
360 /*
361 * Set up thread timeout element when thread is created.
362 */
363 void
364 thread_timer_setup(
365 thread_t thread)
366 {
367 extern void thread_depress_expire(
368 timer_call_param_t p0,
369 timer_call_param_t p1);
370
371 timer_call_setup(&thread->wait_timer, thread_timer_expire, thread);
372 thread->wait_timer_is_set = FALSE;
373 thread->wait_timer_active = 1;
374
375 timer_call_setup(&thread->depress_timer, thread_depress_expire, thread);
376 thread->depress_timer_active = 1;
377
378 thread->ref_count++;
379 }
380
381 void
382 thread_timer_terminate(void)
383 {
384 thread_t thread = current_thread();
385 wait_result_t res;
386 spl_t s;
387
388 s = splsched();
389 wake_lock(thread);
390 if (thread->wait_timer_is_set) {
391 if (timer_call_cancel(&thread->wait_timer))
392 thread->wait_timer_active--;
393 thread->wait_timer_is_set = FALSE;
394 }
395
396 thread->wait_timer_active--;
397
398 while (thread->wait_timer_active > 0) {
399 res = assert_wait((event_t)&thread->wait_timer_active, THREAD_UNINT);
400 assert(res == THREAD_WAITING);
401 wake_unlock(thread);
402 splx(s);
403
404 res = thread_block(THREAD_CONTINUE_NULL);
405 assert(res == THREAD_AWAKENED);
406
407 s = splsched();
408 wake_lock(thread);
409 }
410
411 thread->depress_timer_active--;
412
413 while (thread->depress_timer_active > 0) {
414 res = assert_wait((event_t)&thread->depress_timer_active, THREAD_UNINT);
415 assert(res == THREAD_WAITING);
416 wake_unlock(thread);
417 splx(s);
418
419 res = thread_block(THREAD_CONTINUE_NULL);
420 assert(res == THREAD_AWAKENED);
421
422 s = splsched();
423 wake_lock(thread);
424 }
425
426 wake_unlock(thread);
427 splx(s);
428
429 thread_deallocate(thread);
430 }
431
432 /*
433 * Routine: thread_go_locked
434 * Purpose:
435 * Start a thread running.
436 * Conditions:
437 * thread lock held, IPC locks may be held.
438 * thread must have been pulled from wait queue under same lock hold.
439 * Returns:
440 * KERN_SUCCESS - Thread was set running
441 * KERN_NOT_WAITING - Thread was not waiting
442 */
443 kern_return_t
444 thread_go_locked(
445 thread_t thread,
446 wait_result_t result)
447 {
448 assert(thread->at_safe_point == FALSE);
449 assert(thread->wait_event == NO_EVENT64);
450 assert(thread->wait_queue == WAIT_QUEUE_NULL);
451
452 if ((thread->state & (TH_WAIT|TH_TERMINATE)) == TH_WAIT) {
453 thread->state &= ~(TH_WAIT|TH_UNINT);
454 if (!(thread->state & TH_RUN)) {
455 thread->state |= TH_RUN;
456
457 if (thread->active_callout)
458 call_thread_unblock();
459
460 if (!(thread->state & TH_IDLE)) {
461 _mk_sp_thread_unblock(thread);
462 hw_atomic_add(&thread->processor_set->run_count, 1);
463 }
464 }
465
466 thread->wait_result = result;
467 return KERN_SUCCESS;
468 }
469 return KERN_NOT_WAITING;
470 }
471
472 /*
473 * Routine: thread_mark_wait_locked
474 * Purpose:
475 * Mark a thread as waiting. If, given the circumstances,
476 * it doesn't want to wait (i.e. already aborted), then
477 * indicate that in the return value.
478 * Conditions:
479 * at splsched() and thread is locked.
480 */
481 __private_extern__
482 wait_result_t
483 thread_mark_wait_locked(
484 thread_t thread,
485 wait_interrupt_t interruptible)
486 {
487 wait_result_t wait_result;
488 boolean_t at_safe_point;
489
490 assert(thread == current_thread());
491
492 /*
493 * The thread may have certain types of interrupts/aborts masked
494 * off. Even if the wait location says these types of interrupts
495 * are OK, we have to honor mask settings (outer-scoped code may
496 * not be able to handle aborts at the moment).
497 */
498 if (interruptible > thread->interrupt_level)
499 interruptible = thread->interrupt_level;
500
501 at_safe_point = (interruptible == THREAD_ABORTSAFE);
502
503 if ((interruptible == THREAD_UNINT) ||
504 !(thread->state & TH_ABORT) ||
505 (!at_safe_point && (thread->state & TH_ABORT_SAFELY))) {
506 thread->state |= (interruptible) ? TH_WAIT : (TH_WAIT | TH_UNINT);
507 thread->at_safe_point = at_safe_point;
508 thread->sleep_stamp = sched_tick;
509 return (thread->wait_result = THREAD_WAITING);
510 } else if (thread->state & TH_ABORT_SAFELY) {
511 thread->state &= ~(TH_ABORT|TH_ABORT_SAFELY);
512 }
513 return (thread->wait_result = THREAD_INTERRUPTED);
514 }
515
516 /*
517 * Routine: thread_interrupt_level
518 * Purpose:
519 * Set the maximum interruptible state for the
520 * current thread. The effective value of any
521 * interruptible flag passed into assert_wait
522 * will never exceed this.
523 *
524 * Useful for code that must not be interrupted,
525 * but which calls code that doesn't know that.
526 * Returns:
527 * The old interrupt level for the thread.
528 */
529 __private_extern__
530 wait_interrupt_t
531 thread_interrupt_level(
532 wait_interrupt_t new_level)
533 {
534 thread_t thread = current_thread();
535 wait_interrupt_t result = thread->interrupt_level;
536
537 thread->interrupt_level = new_level;
538 return result;
539 }
540
541 /*
542 * Routine: assert_wait_timeout
543 * Purpose:
544 * Assert that the thread intends to block,
545 * waiting for a timeout (no user known event).
546 */
547 unsigned int assert_wait_timeout_event;
548
549 wait_result_t
550 assert_wait_timeout(
551 mach_msg_timeout_t msecs,
552 wait_interrupt_t interruptible)
553 {
554 wait_result_t res;
555
556 res = assert_wait((event_t)&assert_wait_timeout_event, interruptible);
557 if (res == THREAD_WAITING)
558 thread_set_timer(msecs, 1000*NSEC_PER_USEC);
559 return res;
560 }
561
562 /*
563 * Check to see if an assert wait is possible, without actually doing one.
564 * This is used by debug code in locks and elsewhere to verify that it is
565 * always OK to block when trying to take a blocking lock (since waiting
566 * for the actual assert_wait to catch the case may make it hard to detect
567 * this case.
568 */
569 boolean_t
570 assert_wait_possible(void)
571 {
572
573 thread_t thread;
574 extern unsigned int debug_mode;
575
576 #if DEBUG
577 if(debug_mode) return TRUE; /* Always succeed in debug mode */
578 #endif
579
580 thread = current_thread();
581
582 return (thread == NULL || wait_queue_assert_possible(thread));
583 }
584
585 /*
586 * assert_wait:
587 *
588 * Assert that the current thread is about to go to
589 * sleep until the specified event occurs.
590 */
591 wait_result_t
592 assert_wait(
593 event_t event,
594 wait_interrupt_t interruptible)
595 {
596 register wait_queue_t wq;
597 register int index;
598
599 assert(event != NO_EVENT);
600 assert(assert_wait_possible());
601
602 index = wait_hash(event);
603 wq = &wait_queues[index];
604 return wait_queue_assert_wait(wq, event, interruptible);
605 }
606
607
608 /*
609 * thread_sleep_fast_usimple_lock:
610 *
611 * Cause the current thread to wait until the specified event
612 * occurs. The specified simple_lock is unlocked before releasing
613 * the cpu and re-acquired as part of waking up.
614 *
615 * This is the simple lock sleep interface for components that use a
616 * faster version of simple_lock() than is provided by usimple_lock().
617 */
618 __private_extern__ wait_result_t
619 thread_sleep_fast_usimple_lock(
620 event_t event,
621 simple_lock_t lock,
622 wait_interrupt_t interruptible)
623 {
624 wait_result_t res;
625
626 res = assert_wait(event, interruptible);
627 if (res == THREAD_WAITING) {
628 simple_unlock(lock);
629 res = thread_block(THREAD_CONTINUE_NULL);
630 simple_lock(lock);
631 }
632 return res;
633 }
634
635
636 /*
637 * thread_sleep_usimple_lock:
638 *
639 * Cause the current thread to wait until the specified event
640 * occurs. The specified usimple_lock is unlocked before releasing
641 * the cpu and re-acquired as part of waking up.
642 *
643 * This is the simple lock sleep interface for components where
644 * simple_lock() is defined in terms of usimple_lock().
645 */
646 wait_result_t
647 thread_sleep_usimple_lock(
648 event_t event,
649 usimple_lock_t lock,
650 wait_interrupt_t interruptible)
651 {
652 wait_result_t res;
653
654 res = assert_wait(event, interruptible);
655 if (res == THREAD_WAITING) {
656 usimple_unlock(lock);
657 res = thread_block(THREAD_CONTINUE_NULL);
658 usimple_lock(lock);
659 }
660 return res;
661 }
662
663 /*
664 * thread_sleep_mutex:
665 *
666 * Cause the current thread to wait until the specified event
667 * occurs. The specified mutex is unlocked before releasing
668 * the cpu. The mutex will be re-acquired before returning.
669 *
670 * JMM - Add hint to make sure mutex is available before rousting
671 */
672 wait_result_t
673 thread_sleep_mutex(
674 event_t event,
675 mutex_t *mutex,
676 wait_interrupt_t interruptible)
677 {
678 wait_result_t res;
679
680 res = assert_wait(event, interruptible);
681 if (res == THREAD_WAITING) {
682 mutex_unlock(mutex);
683 res = thread_block(THREAD_CONTINUE_NULL);
684 mutex_lock(mutex);
685 }
686 return res;
687 }
688
689 /*
690 * thread_sleep_mutex_deadline:
691 *
692 * Cause the current thread to wait until the specified event
693 * (or deadline) occurs. The specified mutex is unlocked before
694 * releasing the cpu. The mutex will be re-acquired before returning.
695 *
696 * JMM - Add hint to make sure mutex is available before rousting
697 */
698 wait_result_t
699 thread_sleep_mutex_deadline(
700 event_t event,
701 mutex_t *mutex,
702 uint64_t deadline,
703 wait_interrupt_t interruptible)
704 {
705 wait_result_t res;
706
707 res = assert_wait(event, interruptible);
708 if (res == THREAD_WAITING) {
709 mutex_unlock(mutex);
710 thread_set_timer_deadline(deadline);
711 res = thread_block(THREAD_CONTINUE_NULL);
712 if (res != THREAD_TIMED_OUT)
713 thread_cancel_timer();
714 mutex_lock(mutex);
715 }
716 return res;
717 }
718
719 /*
720 * thread_sleep_lock_write:
721 *
722 * Cause the current thread to wait until the specified event
723 * occurs. The specified (write) lock is unlocked before releasing
724 * the cpu. The (write) lock will be re-acquired before returning.
725 *
726 * JMM - Add hint to make sure mutex is available before rousting
727 */
728 wait_result_t
729 thread_sleep_lock_write(
730 event_t event,
731 lock_t *lock,
732 wait_interrupt_t interruptible)
733 {
734 wait_result_t res;
735
736 res = assert_wait(event, interruptible);
737 if (res == THREAD_WAITING) {
738 lock_write_done(lock);
739 res = thread_block(THREAD_CONTINUE_NULL);
740 lock_write(lock);
741 }
742 return res;
743 }
744
745
746 /*
747 * thread_sleep_funnel:
748 *
749 * Cause the current thread to wait until the specified event
750 * occurs. If the thread is funnelled, the funnel will be released
751 * before giving up the cpu. The funnel will be re-acquired before returning.
752 *
753 * JMM - Right now the funnel is dropped and re-acquired inside
754 * thread_block(). At some point, this may give thread_block() a hint.
755 */
756 wait_result_t
757 thread_sleep_funnel(
758 event_t event,
759 wait_interrupt_t interruptible)
760 {
761 wait_result_t res;
762
763 res = assert_wait(event, interruptible);
764 if (res == THREAD_WAITING) {
765 res = thread_block(THREAD_CONTINUE_NULL);
766 }
767 return res;
768 }
769
770 /*
771 * thread_[un]stop(thread)
772 * Once a thread has blocked interruptibly (via assert_wait) prevent
773 * it from running until thread_unstop.
774 *
775 * If someone else has already stopped the thread, wait for the
776 * stop to be cleared, and then stop it again.
777 *
778 * Return FALSE if interrupted.
779 *
780 * NOTE: thread_hold/thread_suspend should be called on the activation
781 * before calling thread_stop. TH_SUSP is only recognized when
782 * a thread blocks and only prevents clear_wait/thread_wakeup
783 * from restarting an interruptible wait. The wake_active flag is
784 * used to indicate that someone is waiting on the thread.
785 */
786 boolean_t
787 thread_stop(
788 thread_t thread)
789 {
790 spl_t s = splsched();
791
792 wake_lock(thread);
793
794 while (thread->state & TH_SUSP) {
795 wait_result_t result;
796
797 thread->wake_active = TRUE;
798 result = assert_wait(&thread->wake_active, THREAD_ABORTSAFE);
799 wake_unlock(thread);
800 splx(s);
801
802 if (result == THREAD_WAITING)
803 result = thread_block(THREAD_CONTINUE_NULL);
804
805 if (result != THREAD_AWAKENED)
806 return (FALSE);
807
808 s = splsched();
809 wake_lock(thread);
810 }
811
812 thread_lock(thread);
813 thread->state |= TH_SUSP;
814
815 while (thread->state & TH_RUN) {
816 wait_result_t result;
817 processor_t processor = thread->last_processor;
818
819 if ( processor != PROCESSOR_NULL &&
820 processor->state == PROCESSOR_RUNNING &&
821 processor->cpu_data->active_thread == thread )
822 cause_ast_check(processor);
823 thread_unlock(thread);
824
825 thread->wake_active = TRUE;
826 result = assert_wait(&thread->wake_active, THREAD_ABORTSAFE);
827 wake_unlock(thread);
828 splx(s);
829
830 if (result == THREAD_WAITING)
831 result = thread_block(THREAD_CONTINUE_NULL);
832
833 if (result != THREAD_AWAKENED) {
834 thread_unstop(thread);
835 return (FALSE);
836 }
837
838 s = splsched();
839 wake_lock(thread);
840 thread_lock(thread);
841 }
842
843 thread_unlock(thread);
844 wake_unlock(thread);
845 splx(s);
846
847 return (TRUE);
848 }
849
850 /*
851 * Clear TH_SUSP and if the thread has been stopped and is now runnable,
852 * put it back on the run queue.
853 */
854 void
855 thread_unstop(
856 thread_t thread)
857 {
858 spl_t s = splsched();
859
860 wake_lock(thread);
861 thread_lock(thread);
862
863 if ((thread->state & (TH_RUN|TH_WAIT|TH_SUSP)) == TH_SUSP) {
864 thread->state &= ~TH_SUSP;
865 thread->state |= TH_RUN;
866
867 assert(!(thread->state & TH_IDLE));
868 _mk_sp_thread_unblock(thread);
869 hw_atomic_add(&thread->processor_set->run_count, 1);
870 }
871 else
872 if (thread->state & TH_SUSP) {
873 thread->state &= ~TH_SUSP;
874
875 if (thread->wake_active) {
876 thread->wake_active = FALSE;
877 thread_unlock(thread);
878 wake_unlock(thread);
879 splx(s);
880
881 thread_wakeup(&thread->wake_active);
882 return;
883 }
884 }
885
886 thread_unlock(thread);
887 wake_unlock(thread);
888 splx(s);
889 }
890
891 /*
892 * Wait for the thread's RUN bit to clear
893 */
894 boolean_t
895 thread_wait(
896 thread_t thread)
897 {
898 spl_t s = splsched();
899
900 wake_lock(thread);
901 thread_lock(thread);
902
903 while (thread->state & TH_RUN) {
904 wait_result_t result;
905 processor_t processor = thread->last_processor;
906
907 if ( processor != PROCESSOR_NULL &&
908 processor->state == PROCESSOR_RUNNING &&
909 processor->cpu_data->active_thread == thread )
910 cause_ast_check(processor);
911 thread_unlock(thread);
912
913 thread->wake_active = TRUE;
914 result = assert_wait(&thread->wake_active, THREAD_ABORTSAFE);
915 wake_unlock(thread);
916 splx(s);
917
918 if (result == THREAD_WAITING)
919 result = thread_block(THREAD_CONTINUE_NULL);
920
921 if (result != THREAD_AWAKENED)
922 return (FALSE);
923
924 s = splsched();
925 wake_lock(thread);
926 thread_lock(thread);
927 }
928
929 thread_unlock(thread);
930 wake_unlock(thread);
931 splx(s);
932
933 return (TRUE);
934 }
935
936 /*
937 * Routine: clear_wait_internal
938 *
939 * Clear the wait condition for the specified thread.
940 * Start the thread executing if that is appropriate.
941 * Arguments:
942 * thread thread to awaken
943 * result Wakeup result the thread should see
944 * Conditions:
945 * At splsched
946 * the thread is locked.
947 * Returns:
948 * KERN_SUCCESS thread was rousted out a wait
949 * KERN_FAILURE thread was waiting but could not be rousted
950 * KERN_NOT_WAITING thread was not waiting
951 */
952 __private_extern__ kern_return_t
953 clear_wait_internal(
954 thread_t thread,
955 wait_result_t result)
956 {
957 wait_queue_t wq = thread->wait_queue;
958 kern_return_t ret;
959 int loop_count;
960
961 loop_count = 0;
962 do {
963 if ((result == THREAD_INTERRUPTED) && (thread->state & TH_UNINT))
964 return KERN_FAILURE;
965
966 if (wq != WAIT_QUEUE_NULL) {
967 if (wait_queue_lock_try(wq)) {
968 wait_queue_pull_thread_locked(wq, thread, TRUE);
969 /* wait queue unlocked, thread still locked */
970 } else {
971 thread_unlock(thread);
972 delay(1);
973 thread_lock(thread);
974
975 if (wq != thread->wait_queue) {
976 return KERN_NOT_WAITING; /* we know it moved */
977 }
978 continue;
979 }
980 }
981 ret = thread_go_locked(thread, result);
982 return ret;
983 } while (++loop_count < LockTimeOut);
984 panic("clear_wait_internal: deadlock: thread=0x%x, wq=0x%x, cpu=%d\n",
985 thread, wq, cpu_number());
986 return KERN_FAILURE;
987 }
988
989
990 /*
991 * clear_wait:
992 *
993 * Clear the wait condition for the specified thread. Start the thread
994 * executing if that is appropriate.
995 *
996 * parameters:
997 * thread thread to awaken
998 * result Wakeup result the thread should see
999 */
1000 kern_return_t
1001 clear_wait(
1002 thread_t thread,
1003 wait_result_t result)
1004 {
1005 kern_return_t ret;
1006 spl_t s;
1007
1008 s = splsched();
1009 thread_lock(thread);
1010 ret = clear_wait_internal(thread, result);
1011 thread_unlock(thread);
1012 splx(s);
1013 return ret;
1014 }
1015
1016
1017 /*
1018 * thread_wakeup_prim:
1019 *
1020 * Common routine for thread_wakeup, thread_wakeup_with_result,
1021 * and thread_wakeup_one.
1022 *
1023 */
1024 kern_return_t
1025 thread_wakeup_prim(
1026 event_t event,
1027 boolean_t one_thread,
1028 wait_result_t result)
1029 {
1030 register wait_queue_t wq;
1031 register int index;
1032
1033 index = wait_hash(event);
1034 wq = &wait_queues[index];
1035 if (one_thread)
1036 return (wait_queue_wakeup_one(wq, event, result));
1037 else
1038 return (wait_queue_wakeup_all(wq, event, result));
1039 }
1040
1041 /*
1042 * thread_bind:
1043 *
1044 * Force a thread to execute on the specified processor.
1045 * If the thread is currently executing, it may wait until its
1046 * time slice is up before switching onto the specified processor.
1047 *
1048 * A processor of PROCESSOR_NULL causes the thread to be unbound.
1049 * xxx - DO NOT export this to users.
1050 */
1051 void
1052 thread_bind(
1053 register thread_t thread,
1054 processor_t processor)
1055 {
1056 spl_t s;
1057
1058 s = splsched();
1059 thread_lock(thread);
1060 thread_bind_locked(thread, processor);
1061 thread_unlock(thread);
1062 splx(s);
1063 }
1064
1065 /*
1066 * Select a thread for this processor (the current processor) to run.
1067 * May select the current thread, which must already be locked.
1068 */
1069 thread_t
1070 thread_select(
1071 register processor_t myprocessor)
1072 {
1073 register thread_t thread;
1074 processor_set_t pset;
1075 register run_queue_t runq = &myprocessor->runq;
1076 boolean_t other_runnable;
1077
1078 /*
1079 * Check for other non-idle runnable threads.
1080 */
1081 pset = myprocessor->processor_set;
1082 thread = myprocessor->cpu_data->active_thread;
1083
1084 /* Update the thread's priority */
1085 if (thread->sched_stamp != sched_tick)
1086 update_priority(thread);
1087
1088 myprocessor->current_pri = thread->sched_pri;
1089
1090 simple_lock(&runq->lock);
1091 simple_lock(&pset->runq.lock);
1092
1093 other_runnable = runq->count > 0 || pset->runq.count > 0;
1094
1095 if ( thread->state == TH_RUN &&
1096 (!other_runnable ||
1097 (runq->highq < thread->sched_pri &&
1098 pset->runq.highq < thread->sched_pri)) &&
1099 thread->processor_set == pset &&
1100 (thread->bound_processor == PROCESSOR_NULL ||
1101 thread->bound_processor == myprocessor) ) {
1102
1103 /* I am the highest priority runnable (non-idle) thread */
1104 simple_unlock(&pset->runq.lock);
1105 simple_unlock(&runq->lock);
1106
1107 myprocessor->slice_quanta =
1108 (thread->sched_mode & TH_MODE_TIMESHARE)? pset->set_quanta: 1;
1109 }
1110 else
1111 if (other_runnable)
1112 thread = choose_thread(myprocessor);
1113 else {
1114 simple_unlock(&pset->runq.lock);
1115 simple_unlock(&runq->lock);
1116
1117 /*
1118 * Nothing is runnable, so set this processor idle if it
1119 * was running. If it was in an assignment or shutdown,
1120 * leave it alone. Return its idle thread.
1121 */
1122 simple_lock(&pset->sched_lock);
1123 if (myprocessor->state == PROCESSOR_RUNNING) {
1124 remqueue(&pset->active_queue, (queue_entry_t)myprocessor);
1125 myprocessor->state = PROCESSOR_IDLE;
1126
1127 if (myprocessor == master_processor)
1128 enqueue_tail(&pset->idle_queue, (queue_entry_t)myprocessor);
1129 else
1130 enqueue_head(&pset->idle_queue, (queue_entry_t)myprocessor);
1131
1132 pset->idle_count++;
1133 }
1134 simple_unlock(&pset->sched_lock);
1135
1136 thread = myprocessor->idle_thread;
1137 }
1138
1139 return (thread);
1140 }
1141
1142
1143 /*
1144 * Stop running the current thread and start running the new thread.
1145 * If continuation is non-zero, and the current thread is blocked,
1146 * then it will resume by executing continuation on a new stack.
1147 * Returns TRUE if the hand-off succeeds.
1148 *
1149 * Assumes splsched.
1150 */
1151
1152 static thread_t
1153 __current_thread(void)
1154 {
1155 return (current_thread());
1156 }
1157
1158 boolean_t
1159 thread_invoke(
1160 register thread_t old_thread,
1161 register thread_t new_thread,
1162 int reason,
1163 thread_continue_t old_cont)
1164 {
1165 thread_continue_t new_cont;
1166 processor_t processor;
1167
1168 if (get_preemption_level() != 0)
1169 panic("thread_invoke: preemption_level %d\n",
1170 get_preemption_level());
1171
1172 /*
1173 * Mark thread interruptible.
1174 */
1175 thread_lock(new_thread);
1176 new_thread->state &= ~TH_UNINT;
1177
1178 assert(thread_runnable(new_thread));
1179
1180 assert(old_thread->continuation == NULL);
1181
1182 /*
1183 * Allow time constraint threads to hang onto
1184 * a stack.
1185 */
1186 if ( (old_thread->sched_mode & TH_MODE_REALTIME) &&
1187 !old_thread->stack_privilege ) {
1188 old_thread->stack_privilege = old_thread->kernel_stack;
1189 }
1190
1191 if (old_cont != NULL) {
1192 if (new_thread->state & TH_STACK_HANDOFF) {
1193 /*
1194 * If the old thread is using a privileged stack,
1195 * check to see whether we can exchange it with
1196 * that of the new thread.
1197 */
1198 if ( old_thread->kernel_stack == old_thread->stack_privilege &&
1199 !new_thread->stack_privilege)
1200 goto need_stack;
1201
1202 new_thread->state &= ~TH_STACK_HANDOFF;
1203 new_cont = new_thread->continuation;
1204 new_thread->continuation = NULL;
1205
1206 /*
1207 * Set up ast context of new thread and switch
1208 * to its timer.
1209 */
1210 processor = current_processor();
1211 new_thread->last_processor = processor;
1212 processor->current_pri = new_thread->sched_pri;
1213 ast_context(new_thread->top_act, processor->slot_num);
1214 timer_switch(&new_thread->system_timer);
1215 thread_unlock(new_thread);
1216
1217 current_task()->csw++;
1218
1219 old_thread->reason = reason;
1220 old_thread->continuation = old_cont;
1221
1222 _mk_sp_thread_done(old_thread, new_thread, processor);
1223
1224 stack_handoff(old_thread, new_thread);
1225
1226 _mk_sp_thread_begin(new_thread, processor);
1227
1228 wake_lock(old_thread);
1229 thread_lock(old_thread);
1230
1231 /*
1232 * Inline thread_dispatch but
1233 * don't free stack.
1234 */
1235
1236 switch (old_thread->state & (TH_RUN|TH_WAIT|TH_UNINT|TH_IDLE)) {
1237
1238 case TH_RUN | TH_UNINT:
1239 case TH_RUN:
1240 /*
1241 * Still running, put back
1242 * onto a run queue.
1243 */
1244 old_thread->state |= TH_STACK_HANDOFF;
1245 _mk_sp_thread_dispatch(old_thread);
1246
1247 thread_unlock(old_thread);
1248 wake_unlock(old_thread);
1249 break;
1250
1251 case TH_RUN | TH_WAIT | TH_UNINT:
1252 case TH_RUN | TH_WAIT:
1253 {
1254 boolean_t reap, wake, callblock;
1255
1256 /*
1257 * Waiting.
1258 */
1259 old_thread->sleep_stamp = sched_tick;
1260 old_thread->state |= TH_STACK_HANDOFF;
1261 old_thread->state &= ~TH_RUN;
1262 hw_atomic_sub(&old_thread->processor_set->run_count, 1);
1263 callblock = old_thread->active_callout;
1264 wake = old_thread->wake_active;
1265 old_thread->wake_active = FALSE;
1266 reap = (old_thread->state & TH_TERMINATE)? TRUE: FALSE;
1267
1268 thread_unlock(old_thread);
1269 wake_unlock(old_thread);
1270
1271 if (callblock)
1272 call_thread_block();
1273
1274 if (wake)
1275 thread_wakeup((event_t)&old_thread->wake_active);
1276
1277 if (reap)
1278 thread_reaper_enqueue(old_thread);
1279 break;
1280 }
1281
1282 case TH_RUN | TH_IDLE:
1283 /*
1284 * The idle threads don't go
1285 * onto a run queue.
1286 */
1287 old_thread->state |= TH_STACK_HANDOFF;
1288 thread_unlock(old_thread);
1289 wake_unlock(old_thread);
1290 break;
1291
1292 default:
1293 panic("thread_invoke: state 0x%x\n", old_thread->state);
1294 }
1295
1296 counter_always(c_thread_invoke_hits++);
1297
1298 if (new_thread->funnel_state & TH_FN_REFUNNEL) {
1299 kern_return_t wait_result = new_thread->wait_result;
1300
1301 new_thread->funnel_state = 0;
1302 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE,
1303 new_thread->funnel_lock, 2, 0, 0, 0);
1304 funnel_lock(new_thread->funnel_lock);
1305 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE,
1306 new_thread->funnel_lock, 2, 0, 0, 0);
1307 new_thread->funnel_state = TH_FN_OWNED;
1308 new_thread->wait_result = wait_result;
1309 }
1310 (void) spllo();
1311
1312 assert(new_cont);
1313 call_continuation(new_cont);
1314 /*NOTREACHED*/
1315 return (TRUE);
1316 }
1317 else
1318 if (new_thread->state & TH_STACK_ALLOC) {
1319 /*
1320 * Waiting for a stack
1321 */
1322 counter_always(c_thread_invoke_misses++);
1323 thread_unlock(new_thread);
1324 return (FALSE);
1325 }
1326 else
1327 if (new_thread == old_thread) {
1328 /* same thread but with continuation */
1329 counter(++c_thread_invoke_same);
1330 thread_unlock(new_thread);
1331
1332 if (new_thread->funnel_state & TH_FN_REFUNNEL) {
1333 kern_return_t wait_result = new_thread->wait_result;
1334
1335 new_thread->funnel_state = 0;
1336 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE,
1337 new_thread->funnel_lock, 3, 0, 0, 0);
1338 funnel_lock(new_thread->funnel_lock);
1339 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE,
1340 new_thread->funnel_lock, 3, 0, 0, 0);
1341 new_thread->funnel_state = TH_FN_OWNED;
1342 new_thread->wait_result = wait_result;
1343 }
1344 (void) spllo();
1345 call_continuation(old_cont);
1346 /*NOTREACHED*/
1347 }
1348 }
1349 else {
1350 /*
1351 * Check that the new thread has a stack
1352 */
1353 if (new_thread->state & TH_STACK_HANDOFF) {
1354 need_stack:
1355 if (!stack_alloc_try(new_thread, thread_continue)) {
1356 counter_always(c_thread_invoke_misses++);
1357 thread_swapin(new_thread);
1358 return (FALSE);
1359 }
1360
1361 new_thread->state &= ~TH_STACK_HANDOFF;
1362 }
1363 else
1364 if (new_thread->state & TH_STACK_ALLOC) {
1365 /*
1366 * Waiting for a stack
1367 */
1368 counter_always(c_thread_invoke_misses++);
1369 thread_unlock(new_thread);
1370 return (FALSE);
1371 }
1372 else
1373 if (old_thread == new_thread) {
1374 counter(++c_thread_invoke_same);
1375 thread_unlock(new_thread);
1376 return (TRUE);
1377 }
1378 }
1379
1380 /*
1381 * Set up ast context of new thread and switch to its timer.
1382 */
1383 processor = current_processor();
1384 new_thread->last_processor = processor;
1385 processor->current_pri = new_thread->sched_pri;
1386 ast_context(new_thread->top_act, processor->slot_num);
1387 timer_switch(&new_thread->system_timer);
1388 assert(thread_runnable(new_thread));
1389 thread_unlock(new_thread);
1390
1391 counter_always(c_thread_invoke_csw++);
1392 current_task()->csw++;
1393
1394 assert(old_thread->runq == RUN_QUEUE_NULL);
1395 old_thread->reason = reason;
1396 old_thread->continuation = old_cont;
1397
1398 _mk_sp_thread_done(old_thread, new_thread, processor);
1399
1400 /*
1401 * switch_context is machine-dependent. It does the
1402 * machine-dependent components of a context-switch, like
1403 * changing address spaces. It updates active_threads.
1404 */
1405 old_thread = switch_context(old_thread, old_cont, new_thread);
1406
1407 /* Now on new thread's stack. Set a local variable to refer to it. */
1408 new_thread = __current_thread();
1409 assert(old_thread != new_thread);
1410
1411 assert(thread_runnable(new_thread));
1412 _mk_sp_thread_begin(new_thread, new_thread->last_processor);
1413
1414 /*
1415 * We're back. Now old_thread is the thread that resumed
1416 * us, and we have to dispatch it.
1417 */
1418 thread_dispatch(old_thread);
1419
1420 if (old_cont) {
1421 if (new_thread->funnel_state & TH_FN_REFUNNEL) {
1422 kern_return_t wait_result = new_thread->wait_result;
1423
1424 new_thread->funnel_state = 0;
1425 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE,
1426 new_thread->funnel_lock, 3, 0, 0, 0);
1427 funnel_lock(new_thread->funnel_lock);
1428 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE,
1429 new_thread->funnel_lock, 3, 0, 0, 0);
1430 new_thread->funnel_state = TH_FN_OWNED;
1431 new_thread->wait_result = wait_result;
1432 }
1433 (void) spllo();
1434 call_continuation(old_cont);
1435 /*NOTREACHED*/
1436 }
1437
1438 return (TRUE);
1439 }
1440
1441 /*
1442 * thread_continue:
1443 *
1444 * Called when a thread gets a new stack, at splsched();
1445 */
1446 void
1447 thread_continue(
1448 register thread_t old_thread)
1449 {
1450 register thread_t self = current_thread();
1451 register thread_continue_t continuation;
1452
1453 continuation = self->continuation;
1454 self->continuation = NULL;
1455
1456 _mk_sp_thread_begin(self, self->last_processor);
1457
1458 /*
1459 * We must dispatch the old thread and then
1460 * call the current thread's continuation.
1461 * There might not be an old thread, if we are
1462 * the first thread to run on this processor.
1463 */
1464 if (old_thread != THREAD_NULL)
1465 thread_dispatch(old_thread);
1466
1467 if (self->funnel_state & TH_FN_REFUNNEL) {
1468 kern_return_t wait_result = self->wait_result;
1469
1470 self->funnel_state = 0;
1471 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE, self->funnel_lock, 4, 0, 0, 0);
1472 funnel_lock(self->funnel_lock);
1473 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE, self->funnel_lock, 4, 0, 0, 0);
1474 self->funnel_state = TH_FN_OWNED;
1475 self->wait_result = wait_result;
1476 }
1477 (void)spllo();
1478 assert(continuation);
1479 call_continuation(continuation);
1480 /*NOTREACHED*/
1481 }
1482
1483 #if MACH_LDEBUG || MACH_KDB
1484
1485 #define THREAD_LOG_SIZE 300
1486
1487 struct t64 {
1488 unsigned long h;
1489 unsigned long l;
1490 };
1491
1492 struct {
1493 struct t64 stamp;
1494 thread_t thread;
1495 long info1;
1496 long info2;
1497 long info3;
1498 char * action;
1499 } thread_log[THREAD_LOG_SIZE];
1500
1501 int thread_log_index;
1502
1503 void check_thread_time(long n);
1504
1505
1506 int check_thread_time_crash;
1507
1508 #if 0
1509 void
1510 check_thread_time(long us)
1511 {
1512 struct t64 temp;
1513
1514 if (!check_thread_time_crash)
1515 return;
1516
1517 temp = thread_log[0].stamp;
1518 cyctm05_diff (&thread_log[1].stamp, &thread_log[0].stamp, &temp);
1519
1520 if (temp.l >= us && thread_log[1].info != 0x49) /* HACK!!! */
1521 panic ("check_thread_time");
1522 }
1523 #endif
1524
1525 void
1526 log_thread_action(char * action, long info1, long info2, long info3)
1527 {
1528 int i;
1529 spl_t x;
1530 static unsigned int tstamp;
1531
1532 x = splhigh();
1533
1534 for (i = THREAD_LOG_SIZE-1; i > 0; i--) {
1535 thread_log[i] = thread_log[i-1];
1536 }
1537
1538 thread_log[0].stamp.h = 0;
1539 thread_log[0].stamp.l = tstamp++;
1540 thread_log[0].thread = current_thread();
1541 thread_log[0].info1 = info1;
1542 thread_log[0].info2 = info2;
1543 thread_log[0].info3 = info3;
1544 thread_log[0].action = action;
1545 /* strcpy (&thread_log[0].action[0], action);*/
1546
1547 splx(x);
1548 }
1549 #endif /* MACH_LDEBUG || MACH_KDB */
1550
1551 #if MACH_KDB
1552 #include <ddb/db_output.h>
1553 void db_show_thread_log(void);
1554
1555 void
1556 db_show_thread_log(void)
1557 {
1558 int i;
1559
1560 db_printf ("%s %s %s %s %s %s\n", " Thread ", " Info1 ", " Info2 ",
1561 " Info3 ", " Timestamp ", "Action");
1562
1563 for (i = 0; i < THREAD_LOG_SIZE; i++) {
1564 db_printf ("%08x %08x %08x %08x %08x/%08x %s\n",
1565 thread_log[i].thread,
1566 thread_log[i].info1,
1567 thread_log[i].info2,
1568 thread_log[i].info3,
1569 thread_log[i].stamp.h,
1570 thread_log[i].stamp.l,
1571 thread_log[i].action);
1572 }
1573 }
1574 #endif /* MACH_KDB */
1575
1576 /*
1577 * thread_block_reason:
1578 *
1579 * Block the current thread if a wait has been asserted,
1580 * otherwise unconditionally yield the remainder of the
1581 * current quantum unless reason contains AST_BLOCK.
1582 *
1583 * If a continuation is specified, then thread_block will
1584 * attempt to discard the thread's kernel stack. When the
1585 * thread resumes, it will execute the continuation function
1586 * on a new kernel stack.
1587 */
1588 counter(mach_counter_t c_thread_block_calls = 0;)
1589
1590 int
1591 thread_block_reason(
1592 thread_continue_t continuation,
1593 ast_t reason)
1594 {
1595 register thread_t thread = current_thread();
1596 register processor_t myprocessor;
1597 register thread_t new_thread;
1598 spl_t s;
1599
1600 counter(++c_thread_block_calls);
1601
1602 check_simple_locks();
1603
1604 machine_clock_assist();
1605
1606 s = splsched();
1607
1608 if ((thread->funnel_state & TH_FN_OWNED) && !(reason & AST_PREEMPT)) {
1609 thread->funnel_state = TH_FN_REFUNNEL;
1610 KERNEL_DEBUG(
1611 0x603242c | DBG_FUNC_NONE, thread->funnel_lock, 2, 0, 0, 0);
1612 funnel_unlock(thread->funnel_lock);
1613 }
1614
1615 myprocessor = current_processor();
1616
1617 /* If we're explicitly yielding, force a subsequent quantum */
1618 if (reason & AST_YIELD)
1619 myprocessor->slice_quanta = 0;
1620
1621 /* We're handling all scheduling AST's */
1622 ast_off(AST_SCHEDULING);
1623
1624 thread_lock(thread);
1625 new_thread = thread_select(myprocessor);
1626 assert(new_thread && thread_runnable(new_thread));
1627 thread_unlock(thread);
1628 while (!thread_invoke(thread, new_thread, reason, continuation)) {
1629 thread_lock(thread);
1630 new_thread = thread_select(myprocessor);
1631 assert(new_thread && thread_runnable(new_thread));
1632 thread_unlock(thread);
1633 }
1634
1635 if (thread->funnel_state & TH_FN_REFUNNEL) {
1636 kern_return_t wait_result = thread->wait_result;
1637
1638 thread->funnel_state = 0;
1639 KERNEL_DEBUG(
1640 0x6032428 | DBG_FUNC_NONE, thread->funnel_lock, 5, 0, 0, 0);
1641 funnel_lock(thread->funnel_lock);
1642 KERNEL_DEBUG(
1643 0x6032430 | DBG_FUNC_NONE, thread->funnel_lock, 5, 0, 0, 0);
1644 thread->funnel_state = TH_FN_OWNED;
1645 thread->wait_result = wait_result;
1646 }
1647
1648 splx(s);
1649
1650 return (thread->wait_result);
1651 }
1652
1653 /*
1654 * thread_block:
1655 *
1656 * Block the current thread if a wait has been asserted.
1657 */
1658 int
1659 thread_block(
1660 thread_continue_t continuation)
1661 {
1662 return thread_block_reason(continuation, AST_NONE);
1663 }
1664
1665 /*
1666 * thread_run:
1667 *
1668 * Switch directly from the current (old) thread to the
1669 * specified thread, handing off our quantum if possible.
1670 *
1671 * New thread must be runnable, and not on a run queue.
1672 *
1673 * Assumption:
1674 * at splsched.
1675 */
1676 int
1677 thread_run(
1678 thread_t old_thread,
1679 thread_continue_t continuation,
1680 thread_t new_thread)
1681 {
1682 ast_t handoff = AST_HANDOFF;
1683
1684 assert(old_thread == current_thread());
1685
1686 machine_clock_assist();
1687
1688 if (old_thread->funnel_state & TH_FN_OWNED) {
1689 old_thread->funnel_state = TH_FN_REFUNNEL;
1690 KERNEL_DEBUG(
1691 0x603242c | DBG_FUNC_NONE, old_thread->funnel_lock, 3, 0, 0, 0);
1692 funnel_unlock(old_thread->funnel_lock);
1693 }
1694
1695 while (!thread_invoke(old_thread, new_thread, handoff, continuation)) {
1696 register processor_t myprocessor = current_processor();
1697
1698 thread_lock(old_thread);
1699 new_thread = thread_select(myprocessor);
1700 thread_unlock(old_thread);
1701 handoff = AST_NONE;
1702 }
1703
1704 /* if we fell thru */
1705 if (old_thread->funnel_state & TH_FN_REFUNNEL) {
1706 kern_return_t wait_result = old_thread->wait_result;
1707
1708 old_thread->funnel_state = 0;
1709 KERNEL_DEBUG(
1710 0x6032428 | DBG_FUNC_NONE, old_thread->funnel_lock, 6, 0, 0, 0);
1711 funnel_lock(old_thread->funnel_lock);
1712 KERNEL_DEBUG(
1713 0x6032430 | DBG_FUNC_NONE, old_thread->funnel_lock, 6, 0, 0, 0);
1714 old_thread->funnel_state = TH_FN_OWNED;
1715 old_thread->wait_result = wait_result;
1716 }
1717
1718 return (old_thread->wait_result);
1719 }
1720
1721 /*
1722 * Dispatches a running thread that is not on a runq.
1723 * Called at splsched.
1724 */
1725 void
1726 thread_dispatch(
1727 register thread_t thread)
1728 {
1729 wake_lock(thread);
1730 thread_lock(thread);
1731
1732 /*
1733 * If we are discarding the thread's stack, we must do it
1734 * before the thread has a chance to run.
1735 */
1736 #ifndef i386
1737 if (thread->continuation != NULL) {
1738 assert((thread->state & TH_STACK_STATE) == 0);
1739 thread->state |= TH_STACK_HANDOFF;
1740 stack_free(thread);
1741 }
1742 #endif
1743
1744 switch (thread->state & (TH_RUN|TH_WAIT|TH_UNINT|TH_IDLE)) {
1745
1746 case TH_RUN | TH_UNINT:
1747 case TH_RUN:
1748 /*
1749 * No reason to stop. Put back on a run queue.
1750 */
1751 _mk_sp_thread_dispatch(thread);
1752 break;
1753
1754 case TH_RUN | TH_WAIT | TH_UNINT:
1755 case TH_RUN | TH_WAIT:
1756 {
1757 boolean_t reap, wake, callblock;
1758
1759 /*
1760 * Waiting
1761 */
1762 thread->sleep_stamp = sched_tick;
1763 thread->state &= ~TH_RUN;
1764 hw_atomic_sub(&thread->processor_set->run_count, 1);
1765 callblock = thread->active_callout;
1766 wake = thread->wake_active;
1767 thread->wake_active = FALSE;
1768 reap = (thread->state & TH_TERMINATE)? TRUE: FALSE;
1769
1770 thread_unlock(thread);
1771 wake_unlock(thread);
1772
1773 if (callblock)
1774 call_thread_block();
1775
1776 if (wake)
1777 thread_wakeup((event_t)&thread->wake_active);
1778
1779 if (reap)
1780 thread_reaper_enqueue(thread);
1781
1782 return;
1783 }
1784
1785 case TH_RUN | TH_IDLE:
1786 /*
1787 * The idle threads don't go
1788 * onto a run queue.
1789 */
1790 break;
1791
1792 default:
1793 panic("thread_dispatch: bad thread state 0x%x\n", thread->state);
1794 }
1795
1796 thread_unlock(thread);
1797 wake_unlock(thread);
1798 }
1799
1800 /*
1801 * Enqueue thread on run queue. Thread must be locked,
1802 * and not already be on a run queue. Returns TRUE iff
1803 * the particular queue level was empty beforehand.
1804 */
1805 boolean_t
1806 run_queue_enqueue(
1807 register run_queue_t rq,
1808 register thread_t thread,
1809 boolean_t tail)
1810 {
1811 register int whichq = thread->sched_pri;
1812 register queue_t queue = &rq->queues[whichq];
1813 boolean_t result = FALSE;
1814
1815 assert(whichq >= MINPRI && whichq <= MAXPRI);
1816
1817 simple_lock(&rq->lock);
1818 assert(thread->runq == RUN_QUEUE_NULL);
1819 if (queue_empty(queue)) {
1820 enqueue_tail(queue, (queue_entry_t)thread);
1821
1822 setbit(MAXPRI - whichq, rq->bitmap);
1823 if (whichq > rq->highq)
1824 rq->highq = whichq;
1825 result = TRUE;
1826 }
1827 else
1828 if (tail)
1829 enqueue_tail(queue, (queue_entry_t)thread);
1830 else
1831 enqueue_head(queue, (queue_entry_t)thread);
1832
1833 thread->runq = rq;
1834 if (thread->sched_mode & TH_MODE_PREEMPT)
1835 rq->urgency++;
1836 rq->count++;
1837 #if DEBUG
1838 thread_check(thread, rq);
1839 #endif /* DEBUG */
1840 simple_unlock(&rq->lock);
1841
1842 return (result);
1843 }
1844
1845 struct {
1846 uint32_t pset_idle_last,
1847 pset_idle_any,
1848 pset_self,
1849 pset_last,
1850 pset_other,
1851 bound_idle,
1852 bound_self,
1853 bound_other;
1854 } dispatch_counts;
1855
1856 /*
1857 * thread_setrun:
1858 *
1859 * Dispatch thread for execution, directly onto an idle
1860 * processor if possible. Else put on appropriate run
1861 * queue. (local if bound, else processor set)
1862 *
1863 * Thread must be locked.
1864 *
1865 * The tail parameter indicates the proper placement of
1866 * the thread on a run queue.
1867 */
1868 void
1869 thread_setrun(
1870 register thread_t new_thread,
1871 boolean_t tail)
1872 {
1873 register processor_t processor;
1874 register processor_set_t pset;
1875 register thread_t thread;
1876 boolean_t try_preempt = FALSE;
1877 ast_t preempt = AST_BLOCK;
1878
1879 assert(thread_runnable(new_thread));
1880
1881 /*
1882 * Update priority if needed.
1883 */
1884 if (new_thread->sched_stamp != sched_tick)
1885 update_priority(new_thread);
1886
1887 /*
1888 * Check for urgent preemption.
1889 */
1890 if (new_thread->sched_mode & TH_MODE_PREEMPT)
1891 preempt |= AST_URGENT;
1892
1893 assert(new_thread->runq == RUN_QUEUE_NULL);
1894
1895 if ((processor = new_thread->bound_processor) == PROCESSOR_NULL) {
1896 /*
1897 * First try to dispatch on
1898 * the last processor.
1899 */
1900 pset = new_thread->processor_set;
1901 processor = new_thread->last_processor;
1902 if ( pset->processor_count > 1 &&
1903 processor != PROCESSOR_NULL &&
1904 processor->state == PROCESSOR_IDLE ) {
1905 simple_lock(&processor->lock);
1906 simple_lock(&pset->sched_lock);
1907 if ( processor->processor_set == pset &&
1908 processor->state == PROCESSOR_IDLE ) {
1909 remqueue(&pset->idle_queue, (queue_entry_t)processor);
1910 pset->idle_count--;
1911 processor->next_thread = new_thread;
1912 processor->state = PROCESSOR_DISPATCHING;
1913 simple_unlock(&pset->sched_lock);
1914 simple_unlock(&processor->lock);
1915 if (processor != current_processor())
1916 machine_signal_idle(processor);
1917 dispatch_counts.pset_idle_last++;
1918 return;
1919 }
1920 simple_unlock(&processor->lock);
1921 }
1922 else
1923 simple_lock(&pset->sched_lock);
1924
1925 /*
1926 * Next pick any idle processor
1927 * in the processor set.
1928 */
1929 if (pset->idle_count > 0) {
1930 processor = (processor_t)dequeue_head(&pset->idle_queue);
1931 pset->idle_count--;
1932 processor->next_thread = new_thread;
1933 processor->state = PROCESSOR_DISPATCHING;
1934 simple_unlock(&pset->sched_lock);
1935 if (processor != current_processor())
1936 machine_signal_idle(processor);
1937 dispatch_counts.pset_idle_any++;
1938 return;
1939 }
1940
1941 /*
1942 * Place thread on run queue.
1943 */
1944 if (run_queue_enqueue(&pset->runq, new_thread, tail))
1945 try_preempt = TRUE;
1946
1947 /*
1948 * Update the timesharing quanta.
1949 */
1950 pset_quanta_update(pset);
1951
1952 /*
1953 * Preempt check.
1954 */
1955 processor = current_processor();
1956 thread = processor->cpu_data->active_thread;
1957 if (try_preempt) {
1958 /*
1959 * First try the current processor
1960 * if it is a member of the correct
1961 * processor set.
1962 */
1963 if ( pset == processor->processor_set &&
1964 csw_needed(thread, processor) ) {
1965 simple_unlock(&pset->sched_lock);
1966
1967 ast_on(preempt);
1968 dispatch_counts.pset_self++;
1969 return;
1970 }
1971
1972 /*
1973 * If that failed and we have other
1974 * processors available keep trying.
1975 */
1976 if ( pset->processor_count > 1 ||
1977 pset != processor->processor_set ) {
1978 queue_t active = &pset->active_queue;
1979 processor_t myprocessor, lastprocessor;
1980 queue_entry_t next;
1981
1982 /*
1983 * Next try the last processor
1984 * dispatched on.
1985 */
1986 myprocessor = processor;
1987 processor = new_thread->last_processor;
1988 if ( processor != myprocessor &&
1989 processor != PROCESSOR_NULL &&
1990 processor->processor_set == pset &&
1991 processor->state == PROCESSOR_RUNNING &&
1992 new_thread->sched_pri > processor->current_pri ) {
1993 cause_ast_check(processor);
1994 simple_unlock(&pset->sched_lock);
1995 dispatch_counts.pset_last++;
1996 return;
1997 }
1998
1999 /*
2000 * Lastly, pick any other
2001 * available processor.
2002 */
2003 lastprocessor = processor;
2004 processor = (processor_t)queue_first(active);
2005 while (!queue_end(active, (queue_entry_t)processor)) {
2006 next = queue_next((queue_entry_t)processor);
2007
2008 if ( processor != myprocessor &&
2009 processor != lastprocessor &&
2010 new_thread->sched_pri > processor->current_pri ) {
2011 if (!queue_end(active, next)) {
2012 remqueue(active, (queue_entry_t)processor);
2013 enqueue_tail(active, (queue_entry_t)processor);
2014 }
2015 cause_ast_check(processor);
2016 simple_unlock(&pset->sched_lock);
2017 dispatch_counts.pset_other++;
2018 return;
2019 }
2020
2021 processor = (processor_t)next;
2022 }
2023 }
2024 }
2025
2026 simple_unlock(&pset->sched_lock);
2027 }
2028 else {
2029 /*
2030 * Bound, can only run on bound processor. Have to lock
2031 * processor here because it may not be the current one.
2032 */
2033 if (processor->state == PROCESSOR_IDLE) {
2034 simple_lock(&processor->lock);
2035 pset = processor->processor_set;
2036 simple_lock(&pset->sched_lock);
2037 if (processor->state == PROCESSOR_IDLE) {
2038 remqueue(&pset->idle_queue, (queue_entry_t)processor);
2039 pset->idle_count--;
2040 processor->next_thread = new_thread;
2041 processor->state = PROCESSOR_DISPATCHING;
2042 simple_unlock(&pset->sched_lock);
2043 simple_unlock(&processor->lock);
2044 if (processor != current_processor())
2045 machine_signal_idle(processor);
2046 dispatch_counts.bound_idle++;
2047 return;
2048 }
2049 simple_unlock(&pset->sched_lock);
2050 simple_unlock(&processor->lock);
2051 }
2052
2053 if (run_queue_enqueue(&processor->runq, new_thread, tail))
2054 try_preempt = TRUE;
2055
2056 if (processor == current_processor()) {
2057 if (try_preempt) {
2058 thread = processor->cpu_data->active_thread;
2059 if (csw_needed(thread, processor)) {
2060 ast_on(preempt);
2061 dispatch_counts.bound_self++;
2062 }
2063 }
2064 }
2065 else {
2066 if (try_preempt) {
2067 if ( processor->state == PROCESSOR_RUNNING &&
2068 new_thread->sched_pri > processor->current_pri ) {
2069 cause_ast_check(processor);
2070 dispatch_counts.bound_other++;
2071 return;
2072 }
2073 }
2074
2075 if (processor->state == PROCESSOR_IDLE) {
2076 machine_signal_idle(processor);
2077 dispatch_counts.bound_idle++;
2078 }
2079 }
2080 }
2081 }
2082
2083 /*
2084 * Called at splsched by a thread on itself.
2085 */
2086 ast_t
2087 csw_check(
2088 thread_t thread,
2089 processor_t processor)
2090 {
2091 int current_pri = thread->sched_pri;
2092 ast_t result = AST_NONE;
2093 run_queue_t runq;
2094
2095 if (first_quantum(processor)) {
2096 runq = &processor->processor_set->runq;
2097 if (runq->highq > current_pri) {
2098 if (runq->urgency > 0)
2099 return (AST_BLOCK | AST_URGENT);
2100
2101 result |= AST_BLOCK;
2102 }
2103
2104 runq = &processor->runq;
2105 if (runq->highq > current_pri) {
2106 if (runq->urgency > 0)
2107 return (AST_BLOCK | AST_URGENT);
2108
2109 result |= AST_BLOCK;
2110 }
2111 }
2112 else {
2113 runq = &processor->processor_set->runq;
2114 if (runq->highq >= current_pri) {
2115 if (runq->urgency > 0)
2116 return (AST_BLOCK | AST_URGENT);
2117
2118 result |= AST_BLOCK;
2119 }
2120
2121 runq = &processor->runq;
2122 if (runq->highq >= current_pri) {
2123 if (runq->urgency > 0)
2124 return (AST_BLOCK | AST_URGENT);
2125
2126 result |= AST_BLOCK;
2127 }
2128 }
2129
2130 if (result != AST_NONE)
2131 return (result);
2132
2133 if (thread->state & TH_SUSP)
2134 result |= AST_BLOCK;
2135
2136 return (result);
2137 }
2138
2139 /*
2140 * set_sched_pri:
2141 *
2142 * Set the current scheduled priority of the specified thread.
2143 * This may cause the thread to change queues.
2144 *
2145 * The thread *must* be locked by the caller.
2146 */
2147 void
2148 set_sched_pri(
2149 thread_t thread,
2150 int priority)
2151 {
2152 register struct run_queue *rq = rem_runq(thread);
2153
2154 if ( !(thread->sched_mode & TH_MODE_TIMESHARE) &&
2155 (priority >= BASEPRI_PREEMPT ||
2156 (thread->task_priority < MINPRI_KERNEL &&
2157 thread->task_priority >= BASEPRI_BACKGROUND &&
2158 priority > thread->task_priority) ||
2159 (thread->sched_mode & TH_MODE_FORCEDPREEMPT) ) )
2160 thread->sched_mode |= TH_MODE_PREEMPT;
2161 else
2162 thread->sched_mode &= ~TH_MODE_PREEMPT;
2163
2164 thread->sched_pri = priority;
2165 if (rq != RUN_QUEUE_NULL)
2166 thread_setrun(thread, TAIL_Q);
2167 else
2168 if ((thread->state & (TH_RUN|TH_WAIT)) == TH_RUN) {
2169 processor_t processor = thread->last_processor;
2170
2171 if (thread == current_thread()) {
2172 ast_t preempt = csw_check(thread, processor);
2173
2174 if (preempt != AST_NONE)
2175 ast_on(preempt);
2176 processor->current_pri = priority;
2177 }
2178 else
2179 if ( processor != PROCESSOR_NULL &&
2180 processor->cpu_data->active_thread == thread )
2181 cause_ast_check(processor);
2182 }
2183 }
2184
2185 /*
2186 * rem_runq:
2187 *
2188 * Remove a thread from its run queue.
2189 * The run queue that the process was on is returned
2190 * (or RUN_QUEUE_NULL if not on a run queue). Thread *must* be locked
2191 * before calling this routine. Unusual locking protocol on runq
2192 * field in thread structure makes this code interesting; see thread.h.
2193 */
2194 run_queue_t
2195 rem_runq(
2196 thread_t thread)
2197 {
2198 register struct run_queue *rq;
2199
2200 rq = thread->runq;
2201 /*
2202 * If rq is RUN_QUEUE_NULL, the thread will stay out of the
2203 * run_queues because the caller locked the thread. Otherwise
2204 * the thread is on a runq, but could leave.
2205 */
2206 if (rq != RUN_QUEUE_NULL) {
2207 simple_lock(&rq->lock);
2208 if (rq == thread->runq) {
2209 /*
2210 * Thread is in a runq and we have a lock on
2211 * that runq.
2212 */
2213 #if DEBUG
2214 thread_check(thread, rq);
2215 #endif /* DEBUG */
2216 remqueue(&rq->queues[0], (queue_entry_t)thread);
2217 rq->count--;
2218 if (thread->sched_mode & TH_MODE_PREEMPT)
2219 rq->urgency--;
2220 assert(rq->urgency >= 0);
2221
2222 if (queue_empty(rq->queues + thread->sched_pri)) {
2223 /* update run queue status */
2224 if (thread->sched_pri != IDLEPRI)
2225 clrbit(MAXPRI - thread->sched_pri, rq->bitmap);
2226 rq->highq = MAXPRI - ffsbit(rq->bitmap);
2227 }
2228 thread->runq = RUN_QUEUE_NULL;
2229 simple_unlock(&rq->lock);
2230 }
2231 else {
2232 /*
2233 * The thread left the runq before we could
2234 * lock the runq. It is not on a runq now, and
2235 * can't move again because this routine's
2236 * caller locked the thread.
2237 */
2238 assert(thread->runq == RUN_QUEUE_NULL);
2239 simple_unlock(&rq->lock);
2240 rq = RUN_QUEUE_NULL;
2241 }
2242 }
2243
2244 return (rq);
2245 }
2246
2247 /*
2248 * choose_thread:
2249 *
2250 * Choose a thread to execute. The thread chosen is removed
2251 * from its run queue. Note that this requires only that the runq
2252 * lock be held.
2253 *
2254 * Strategy:
2255 * Check processor runq first; if anything found, run it.
2256 * Else check pset runq; if nothing found, return idle thread.
2257 *
2258 * Second line of strategy is implemented by choose_pset_thread.
2259 *
2260 * Called with both the local & pset run queues locked, returned
2261 * unlocked.
2262 */
2263 thread_t
2264 choose_thread(
2265 processor_t myprocessor)
2266 {
2267 thread_t thread;
2268 register queue_t q;
2269 register run_queue_t runq;
2270 processor_set_t pset;
2271
2272 runq = &myprocessor->runq;
2273 pset = myprocessor->processor_set;
2274
2275 if (runq->count > 0 && runq->highq >= pset->runq.highq) {
2276 simple_unlock(&pset->runq.lock);
2277 q = runq->queues + runq->highq;
2278 #if MACH_ASSERT
2279 if (!queue_empty(q)) {
2280 #endif /*MACH_ASSERT*/
2281 thread = (thread_t)q->next;
2282 ((queue_entry_t)thread)->next->prev = q;
2283 q->next = ((queue_entry_t)thread)->next;
2284 thread->runq = RUN_QUEUE_NULL;
2285 runq->count--;
2286 if (thread->sched_mode & TH_MODE_PREEMPT)
2287 runq->urgency--;
2288 assert(runq->urgency >= 0);
2289 if (queue_empty(q)) {
2290 if (runq->highq != IDLEPRI)
2291 clrbit(MAXPRI - runq->highq, runq->bitmap);
2292 runq->highq = MAXPRI - ffsbit(runq->bitmap);
2293 }
2294 simple_unlock(&runq->lock);
2295 return (thread);
2296 #if MACH_ASSERT
2297 }
2298 panic("choose_thread");
2299 #endif /*MACH_ASSERT*/
2300 /*NOTREACHED*/
2301 }
2302 simple_unlock(&myprocessor->runq.lock);
2303
2304 return (choose_pset_thread(myprocessor, pset));
2305 }
2306
2307 /*
2308 * choose_pset_thread: choose a thread from processor_set runq or
2309 * set processor idle and choose its idle thread.
2310 *
2311 * This routine chooses and removes a thread from the runq if there
2312 * is one (and returns it), else it sets the processor idle and
2313 * returns its idle thread.
2314 *
2315 * Called with both local & pset run queues locked, returned
2316 * unlocked.
2317 */
2318 thread_t
2319 choose_pset_thread(
2320 register processor_t myprocessor,
2321 processor_set_t pset)
2322 {
2323 register run_queue_t runq;
2324 register thread_t thread;
2325 register queue_t q;
2326
2327 runq = &pset->runq;
2328 if (runq->count > 0) {
2329 q = runq->queues + runq->highq;
2330 #if MACH_ASSERT
2331 if (!queue_empty(q)) {
2332 #endif /*MACH_ASSERT*/
2333 thread = (thread_t)q->next;
2334 ((queue_entry_t)thread)->next->prev = q;
2335 q->next = ((queue_entry_t)thread)->next;
2336 thread->runq = RUN_QUEUE_NULL;
2337 runq->count--;
2338 if (thread->sched_mode & TH_MODE_PREEMPT)
2339 runq->urgency--;
2340 assert(runq->urgency >= 0);
2341 if (queue_empty(q)) {
2342 if (runq->highq != IDLEPRI)
2343 clrbit(MAXPRI - runq->highq, runq->bitmap);
2344 runq->highq = MAXPRI - ffsbit(runq->bitmap);
2345 }
2346 pset_quanta_update(pset);
2347 simple_unlock(&runq->lock);
2348 return (thread);
2349 #if MACH_ASSERT
2350 }
2351 panic("choose_pset_thread");
2352 #endif /*MACH_ASSERT*/
2353 /*NOTREACHED*/
2354 }
2355 simple_unlock(&runq->lock);
2356
2357 /*
2358 * Nothing is runnable, so set this processor idle if it
2359 * was running. If it was in an assignment or shutdown,
2360 * leave it alone. Return its idle thread.
2361 */
2362 simple_lock(&pset->sched_lock);
2363 if (myprocessor->state == PROCESSOR_RUNNING) {
2364 remqueue(&pset->active_queue, (queue_entry_t)myprocessor);
2365 myprocessor->state = PROCESSOR_IDLE;
2366
2367 if (myprocessor == master_processor)
2368 enqueue_tail(&pset->idle_queue, (queue_entry_t)myprocessor);
2369 else
2370 enqueue_head(&pset->idle_queue, (queue_entry_t)myprocessor);
2371
2372 pset->idle_count++;
2373 }
2374 simple_unlock(&pset->sched_lock);
2375
2376 return (myprocessor->idle_thread);
2377 }
2378
2379 /*
2380 * no_dispatch_count counts number of times processors go non-idle
2381 * without being dispatched. This should be very rare.
2382 */
2383 int no_dispatch_count = 0;
2384
2385 /*
2386 * This is the idle thread, which just looks for other threads
2387 * to execute.
2388 */
2389 void
2390 idle_thread_continue(void)
2391 {
2392 register processor_t myprocessor;
2393 register volatile thread_t *threadp;
2394 register volatile int *gcount;
2395 register volatile int *lcount;
2396 register thread_t new_thread;
2397 register int state;
2398 register processor_set_t pset;
2399 int mycpu;
2400
2401 mycpu = cpu_number();
2402 myprocessor = cpu_to_processor(mycpu);
2403 threadp = (volatile thread_t *) &myprocessor->next_thread;
2404 lcount = (volatile int *) &myprocessor->runq.count;
2405
2406 for (;;) {
2407 gcount = (volatile int *)&myprocessor->processor_set->runq.count;
2408
2409 (void)splsched();
2410 while ( (*threadp == (volatile thread_t)THREAD_NULL) &&
2411 (*gcount == 0) && (*lcount == 0) ) {
2412
2413 /* check for ASTs while we wait */
2414 if (need_ast[mycpu] &~ ( AST_SCHEDULING | AST_BSD )) {
2415 /* don't allow scheduling ASTs */
2416 need_ast[mycpu] &= ~( AST_SCHEDULING | AST_BSD );
2417 ast_taken(AST_ALL, TRUE); /* back at spllo */
2418 }
2419 else
2420 #ifdef __ppc__
2421 machine_idle();
2422 #else
2423 (void)spllo();
2424 #endif
2425 machine_clock_assist();
2426
2427 (void)splsched();
2428 }
2429
2430 /*
2431 * This is not a switch statement to avoid the
2432 * bounds checking code in the common case.
2433 */
2434 pset = myprocessor->processor_set;
2435 simple_lock(&pset->sched_lock);
2436 retry:
2437 state = myprocessor->state;
2438 if (state == PROCESSOR_DISPATCHING) {
2439 /*
2440 * Commmon case -- cpu dispatched.
2441 */
2442 new_thread = *threadp;
2443 *threadp = (volatile thread_t) THREAD_NULL;
2444 myprocessor->state = PROCESSOR_RUNNING;
2445 enqueue_tail(&pset->active_queue, (queue_entry_t)myprocessor);
2446 simple_unlock(&pset->sched_lock);
2447
2448 if ( myprocessor->runq.highq > new_thread->sched_pri ||
2449 pset->runq.highq > new_thread->sched_pri ) {
2450 thread_lock(new_thread);
2451 thread_setrun(new_thread, HEAD_Q);
2452 thread_unlock(new_thread);
2453
2454 counter(c_idle_thread_block++);
2455 thread_block(idle_thread_continue);
2456 /* NOTREACHED */
2457 }
2458 else {
2459 counter(c_idle_thread_handoff++);
2460 thread_run(myprocessor->idle_thread,
2461 idle_thread_continue, new_thread);
2462 /* NOTREACHED */
2463 }
2464 }
2465 else
2466 if (state == PROCESSOR_IDLE) {
2467 if (myprocessor->state != PROCESSOR_IDLE) {
2468 /*
2469 * Something happened, try again.
2470 */
2471 goto retry;
2472 }
2473 /*
2474 * Processor was not dispatched (Rare).
2475 * Set it running again.
2476 */
2477 no_dispatch_count++;
2478 pset->idle_count--;
2479 remqueue(&pset->idle_queue, (queue_entry_t)myprocessor);
2480 myprocessor->state = PROCESSOR_RUNNING;
2481 enqueue_tail(&pset->active_queue, (queue_entry_t)myprocessor);
2482 simple_unlock(&pset->sched_lock);
2483
2484 counter(c_idle_thread_block++);
2485 thread_block(idle_thread_continue);
2486 /* NOTREACHED */
2487 }
2488 else
2489 if ( state == PROCESSOR_ASSIGN ||
2490 state == PROCESSOR_SHUTDOWN ) {
2491 /*
2492 * Changing processor sets, or going off-line.
2493 * Release next_thread if there is one. Actual
2494 * thread to run is on a runq.
2495 */
2496 if ((new_thread = (thread_t)*threadp) != THREAD_NULL) {
2497 *threadp = (volatile thread_t) THREAD_NULL;
2498 simple_unlock(&pset->sched_lock);
2499
2500 thread_lock(new_thread);
2501 thread_setrun(new_thread, TAIL_Q);
2502 thread_unlock(new_thread);
2503 }
2504 else
2505 simple_unlock(&pset->sched_lock);
2506
2507 counter(c_idle_thread_block++);
2508 thread_block(idle_thread_continue);
2509 /* NOTREACHED */
2510 }
2511 else {
2512 simple_unlock(&pset->sched_lock);
2513
2514 panic("idle_thread: bad processor state %d\n", cpu_state(mycpu));
2515 }
2516
2517 (void)spllo();
2518 }
2519 }
2520
2521 void
2522 idle_thread(void)
2523 {
2524 thread_t self = current_thread();
2525 spl_t s;
2526
2527 stack_privilege(self);
2528
2529 s = splsched();
2530 thread_lock(self);
2531 self->priority = IDLEPRI;
2532 set_sched_pri(self, self->priority);
2533 thread_unlock(self);
2534 splx(s);
2535
2536 counter(c_idle_thread_block++);
2537 thread_block(idle_thread_continue);
2538 /*NOTREACHED*/
2539 }
2540
2541 static uint64_t sched_tick_interval, sched_tick_deadline;
2542
2543 void sched_tick_thread(void);
2544
2545 void
2546 sched_tick_init(void)
2547 {
2548 kernel_thread_with_priority(
2549 kernel_task, MAXPRI_STANDARD,
2550 sched_tick_thread, TRUE, TRUE);
2551 }
2552
2553 /*
2554 * sched_tick_thread
2555 *
2556 * Update the priorities of all threads periodically.
2557 */
2558 void
2559 sched_tick_thread_continue(void)
2560 {
2561 uint64_t abstime;
2562 #if SIMPLE_CLOCK
2563 int new_usec;
2564 #endif /* SIMPLE_CLOCK */
2565
2566 clock_get_uptime(&abstime);
2567
2568 sched_tick++; /* age usage one more time */
2569 #if SIMPLE_CLOCK
2570 /*
2571 * Compensate for clock drift. sched_usec is an
2572 * exponential average of the number of microseconds in
2573 * a second. It decays in the same fashion as cpu_usage.
2574 */
2575 new_usec = sched_usec_elapsed();
2576 sched_usec = (5*sched_usec + 3*new_usec)/8;
2577 #endif /* SIMPLE_CLOCK */
2578
2579 /*
2580 * Compute the scheduler load factors.
2581 */
2582 compute_mach_factor();
2583
2584 /*
2585 * Scan the run queues for runnable threads that need to
2586 * have their priorities recalculated.
2587 */
2588 do_thread_scan();
2589
2590 clock_deadline_for_periodic_event(sched_tick_interval, abstime,
2591 &sched_tick_deadline);
2592
2593 assert_wait((event_t)sched_tick_thread_continue, THREAD_INTERRUPTIBLE);
2594 thread_set_timer_deadline(sched_tick_deadline);
2595 thread_block(sched_tick_thread_continue);
2596 /*NOTREACHED*/
2597 }
2598
2599 void
2600 sched_tick_thread(void)
2601 {
2602 thread_t self = current_thread();
2603 natural_t rate;
2604 spl_t s;
2605
2606 stack_privilege(self);
2607
2608 rate = (1000 >> SCHED_TICK_SHIFT);
2609 clock_interval_to_absolutetime_interval(rate, USEC_PER_SEC,
2610 &sched_tick_interval);
2611 clock_get_uptime(&sched_tick_deadline);
2612
2613 thread_block(sched_tick_thread_continue);
2614 /*NOTREACHED*/
2615 }
2616
2617 #define MAX_STUCK_THREADS 128
2618
2619 /*
2620 * do_thread_scan: scan for stuck threads. A thread is stuck if
2621 * it is runnable but its priority is so low that it has not
2622 * run for several seconds. Its priority should be higher, but
2623 * won't be until it runs and calls update_priority. The scanner
2624 * finds these threads and does the updates.
2625 *
2626 * Scanner runs in two passes. Pass one squirrels likely
2627 * thread ids away in an array (takes out references for them).
2628 * Pass two does the priority updates. This is necessary because
2629 * the run queue lock is required for the candidate scan, but
2630 * cannot be held during updates.
2631 *
2632 * Array length should be enough so that restart isn't necessary,
2633 * but restart logic is included.
2634 *
2635 */
2636 thread_t stuck_threads[MAX_STUCK_THREADS];
2637 int stuck_count = 0;
2638
2639 /*
2640 * do_runq_scan is the guts of pass 1. It scans a runq for
2641 * stuck threads. A boolean is returned indicating whether
2642 * a retry is needed.
2643 */
2644 boolean_t
2645 do_runq_scan(
2646 run_queue_t runq)
2647 {
2648 register queue_t q;
2649 register thread_t thread;
2650 register int count;
2651 spl_t s;
2652 boolean_t result = FALSE;
2653
2654 s = splsched();
2655 simple_lock(&runq->lock);
2656 if ((count = runq->count) > 0) {
2657 q = runq->queues + runq->highq;
2658 while (count > 0) {
2659 queue_iterate(q, thread, thread_t, links) {
2660 if ( !(thread->state & (TH_WAIT|TH_SUSP)) &&
2661 (thread->sched_mode & TH_MODE_TIMESHARE) ) {
2662 if (thread->sched_stamp != sched_tick) {
2663 /*
2664 * Stuck, save its id for later.
2665 */
2666 if (stuck_count == MAX_STUCK_THREADS) {
2667 /*
2668 * !@#$% No more room.
2669 */
2670 simple_unlock(&runq->lock);
2671 splx(s);
2672
2673 return (TRUE);
2674 }
2675
2676 /*
2677 * Inline version of thread_reference
2678 * XXX - lock ordering problem here:
2679 * thread locks should be taken before runq
2680 * locks: just try and get the thread's locks
2681 * and ignore this thread if we fail, we might
2682 * have better luck next time.
2683 */
2684 if (thread_lock_try(thread)) {
2685 thread->ref_count++;
2686 thread_unlock(thread);
2687 stuck_threads[stuck_count++] = thread;
2688 }
2689 else
2690 result = TRUE;
2691 }
2692 }
2693
2694 count--;
2695 }
2696
2697 q--;
2698 }
2699 }
2700 simple_unlock(&runq->lock);
2701 splx(s);
2702
2703 return (result);
2704 }
2705
2706 boolean_t thread_scan_enabled = TRUE;
2707
2708 void
2709 do_thread_scan(void)
2710 {
2711 register boolean_t restart_needed = FALSE;
2712 register thread_t thread;
2713 register processor_set_t pset = &default_pset;
2714 register processor_t processor;
2715 spl_t s;
2716
2717 if (!thread_scan_enabled)
2718 return;
2719
2720 do {
2721 restart_needed = do_runq_scan(&pset->runq);
2722 if (!restart_needed) {
2723 simple_lock(&pset->processors_lock);
2724 processor = (processor_t)queue_first(&pset->processors);
2725 while (!queue_end(&pset->processors, (queue_entry_t)processor)) {
2726 if (restart_needed = do_runq_scan(&processor->runq))
2727 break;
2728
2729 thread = processor->idle_thread;
2730 if (thread->sched_stamp != sched_tick) {
2731 if (stuck_count == MAX_STUCK_THREADS) {
2732 restart_needed = TRUE;
2733 break;
2734 }
2735
2736 stuck_threads[stuck_count++] = thread;
2737 }
2738
2739 processor = (processor_t)queue_next(&processor->processors);
2740 }
2741 simple_unlock(&pset->processors_lock);
2742 }
2743
2744 /*
2745 * Ok, we now have a collection of candidates -- fix them.
2746 */
2747 while (stuck_count > 0) {
2748 thread = stuck_threads[--stuck_count];
2749 stuck_threads[stuck_count] = THREAD_NULL;
2750 s = splsched();
2751 thread_lock(thread);
2752 if ( (thread->sched_mode & TH_MODE_TIMESHARE) ||
2753 (thread->state & TH_IDLE) ) {
2754 if ( !(thread->state & (TH_WAIT|TH_SUSP)) &&
2755 thread->sched_stamp != sched_tick )
2756 update_priority(thread);
2757 }
2758 thread_unlock(thread);
2759 splx(s);
2760 if (!(thread->state & TH_IDLE))
2761 thread_deallocate(thread);
2762 }
2763
2764 if (restart_needed)
2765 delay(1); /* XXX */
2766
2767 } while (restart_needed);
2768 }
2769
2770 /*
2771 * Just in case someone doesn't use the macro
2772 */
2773 #undef thread_wakeup
2774 void
2775 thread_wakeup(
2776 event_t x);
2777
2778 void
2779 thread_wakeup(
2780 event_t x)
2781 {
2782 thread_wakeup_with_result(x, THREAD_AWAKENED);
2783 }
2784
2785
2786 #if DEBUG
2787
2788 static boolean_t
2789 thread_runnable(
2790 thread_t thread)
2791 {
2792 return ((thread->state & (TH_RUN|TH_WAIT)) == TH_RUN);
2793 }
2794
2795 void
2796 dump_processor_set(
2797 processor_set_t ps)
2798 {
2799 printf("processor_set: %08x\n",ps);
2800 printf("idle_queue: %08x %08x, idle_count: 0x%x\n",
2801 ps->idle_queue.next,ps->idle_queue.prev,ps->idle_count);
2802 printf("processors: %08x %08x, processor_count: 0x%x\n",
2803 ps->processors.next,ps->processors.prev,ps->processor_count);
2804 printf("tasks: %08x %08x, task_count: 0x%x\n",
2805 ps->tasks.next,ps->tasks.prev,ps->task_count);
2806 printf("threads: %08x %08x, thread_count: 0x%x\n",
2807 ps->threads.next,ps->threads.prev,ps->thread_count);
2808 printf("ref_count: 0x%x, active: %x\n",
2809 ps->ref_count,ps->active);
2810 printf("pset_self: %08x, pset_name_self: %08x\n",ps->pset_self, ps->pset_name_self);
2811 printf("set_quanta: 0x%x\n", ps->set_quanta);
2812 }
2813
2814 #define processor_state(s) (((s)>PROCESSOR_SHUTDOWN)?"*unknown*":states[s])
2815
2816 void
2817 dump_processor(
2818 processor_t p)
2819 {
2820 char *states[]={"OFF_LINE","RUNNING","IDLE","DISPATCHING",
2821 "ASSIGN","SHUTDOWN"};
2822
2823 printf("processor: %08x\n",p);
2824 printf("processor_queue: %08x %08x\n",
2825 p->processor_queue.next,p->processor_queue.prev);
2826 printf("state: %8s, next_thread: %08x, idle_thread: %08x\n",
2827 processor_state(p->state), p->next_thread, p->idle_thread);
2828 printf("slice_quanta: %x\n", p->slice_quanta);
2829 printf("processor_set: %08x, processor_set_next: %08x\n",
2830 p->processor_set, p->processor_set_next);
2831 printf("processors: %08x %08x\n", p->processors.next,p->processors.prev);
2832 printf("processor_self: %08x, slot_num: 0x%x\n", p->processor_self, p->slot_num);
2833 }
2834
2835 void
2836 dump_run_queue_struct(
2837 run_queue_t rq)
2838 {
2839 char dump_buf[80];
2840 int i;
2841
2842 for( i=0; i < NRQS; ) {
2843 int j;
2844
2845 printf("%6s",(i==0)?"runq:":"");
2846 for( j=0; (j<8) && (i < NRQS); j++,i++ ) {
2847 if( rq->queues[i].next == &rq->queues[i] )
2848 printf( " --------");
2849 else
2850 printf(" %08x",rq->queues[i].next);
2851 }
2852 printf("\n");
2853 }
2854 for( i=0; i < NRQBM; ) {
2855 register unsigned int mask;
2856 char *d=dump_buf;
2857
2858 mask = ~0;
2859 mask ^= (mask>>1);
2860
2861 do {
2862 *d++ = ((rq->bitmap[i]&mask)?'r':'e');
2863 mask >>=1;
2864 } while( mask );
2865 *d = '\0';
2866 printf("%8s%s\n",((i==0)?"bitmap:":""),dump_buf);
2867 i++;
2868 }
2869 printf("highq: 0x%x, count: %u\n", rq->highq, rq->count);
2870 }
2871
2872 void
2873 dump_run_queues(
2874 run_queue_t runq)
2875 {
2876 register queue_t q1;
2877 register int i;
2878 register queue_entry_t e;
2879
2880 q1 = runq->queues;
2881 for (i = 0; i < NRQS; i++) {
2882 if (q1->next != q1) {
2883 int t_cnt;
2884
2885 printf("[%u]",i);
2886 for (t_cnt=0, e = q1->next; e != q1; e = e->next) {
2887 printf("\t0x%08x",e);
2888 if( (t_cnt = ++t_cnt%4) == 0 )
2889 printf("\n");
2890 }
2891 if( t_cnt )
2892 printf("\n");
2893 }
2894 /* else
2895 printf("[%u]\t<empty>\n",i);
2896 */
2897 q1++;
2898 }
2899 }
2900
2901 void
2902 checkrq(
2903 run_queue_t rq,
2904 char *msg)
2905 {
2906 register queue_t q1;
2907 register int i, j;
2908 register queue_entry_t e;
2909 register int highq;
2910
2911 highq = NRQS;
2912 j = 0;
2913 q1 = rq->queues;
2914 for (i = MAXPRI; i >= 0; i--) {
2915 if (q1->next == q1) {
2916 if (q1->prev != q1) {
2917 panic("checkrq: empty at %s", msg);
2918 }
2919 }
2920 else {
2921 if (highq == -1)
2922 highq = i;
2923
2924 for (e = q1->next; e != q1; e = e->next) {
2925 j++;
2926 if (e->next->prev != e)
2927 panic("checkrq-2 at %s", msg);
2928 if (e->prev->next != e)
2929 panic("checkrq-3 at %s", msg);
2930 }
2931 }
2932 q1++;
2933 }
2934 if (j != rq->count)
2935 panic("checkrq: count wrong at %s", msg);
2936 if (rq->count != 0 && highq > rq->highq)
2937 panic("checkrq: highq wrong at %s", msg);
2938 }
2939
2940 void
2941 thread_check(
2942 register thread_t thread,
2943 register run_queue_t rq)
2944 {
2945 register int whichq = thread->sched_pri;
2946 register queue_entry_t queue, entry;
2947
2948 if (whichq < MINPRI || whichq > MAXPRI)
2949 panic("thread_check: bad pri");
2950
2951 queue = &rq->queues[whichq];
2952 entry = queue_first(queue);
2953 while (!queue_end(queue, entry)) {
2954 if (entry == (queue_entry_t)thread)
2955 return;
2956
2957 entry = queue_next(entry);
2958 }
2959
2960 panic("thread_check: not found");
2961 }
2962
2963 #endif /* DEBUG */
2964
2965 #if MACH_KDB
2966 #include <ddb/db_output.h>
2967 #define printf kdbprintf
2968 extern int db_indent;
2969 void db_sched(void);
2970
2971 void
2972 db_sched(void)
2973 {
2974 iprintf("Scheduling Statistics:\n");
2975 db_indent += 2;
2976 iprintf("Thread invocations: csw %d same %d\n",
2977 c_thread_invoke_csw, c_thread_invoke_same);
2978 #if MACH_COUNTERS
2979 iprintf("Thread block: calls %d\n",
2980 c_thread_block_calls);
2981 iprintf("Idle thread:\n\thandoff %d block %d no_dispatch %d\n",
2982 c_idle_thread_handoff,
2983 c_idle_thread_block, no_dispatch_count);
2984 iprintf("Sched thread blocks: %d\n", c_sched_thread_block);
2985 #endif /* MACH_COUNTERS */
2986 db_indent -= 2;
2987 }
2988 #endif /* MACH_KDB */