Subversion Repositories HelenOS-historic

Rev

Rev 1187 | Rev 1221 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1187 Rev 1196
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <proc/scheduler.h>
29
#include <proc/scheduler.h>
30
#include <proc/thread.h>
30
#include <proc/thread.h>
31
#include <proc/task.h>
31
#include <proc/task.h>
32
#include <mm/frame.h>
32
#include <mm/frame.h>
33
#include <mm/page.h>
33
#include <mm/page.h>
34
#include <mm/as.h>
34
#include <mm/as.h>
35
#include <arch/asm.h>
35
#include <arch/asm.h>
36
#include <arch/faddr.h>
36
#include <arch/faddr.h>
37
#include <atomic.h>
37
#include <atomic.h>
38
#include <synch/spinlock.h>
38
#include <synch/spinlock.h>
39
#include <config.h>
39
#include <config.h>
40
#include <context.h>
40
#include <context.h>
41
#include <func.h>
41
#include <func.h>
42
#include <arch.h>
42
#include <arch.h>
43
#include <adt/list.h>
43
#include <adt/list.h>
44
#include <panic.h>
44
#include <panic.h>
45
#include <typedefs.h>
45
#include <typedefs.h>
46
#include <cpu.h>
46
#include <cpu.h>
47
#include <print.h>
47
#include <print.h>
48
#include <debug.h>
48
#include <debug.h>
49
 
49
 
50
static void before_task_runs(void);
50
static void before_task_runs(void);
51
static void before_thread_runs(void);
51
static void before_thread_runs(void);
52
static void after_thread_ran(void);
52
static void after_thread_ran(void);
53
static void scheduler_separated_stack(void);
53
static void scheduler_separated_stack(void);
54
 
54
 
55
atomic_t nrdy;  /**< Number of ready threads in the system. */
55
atomic_t nrdy;  /**< Number of ready threads in the system. */
56
 
56
 
57
/** Carry out actions before new task runs. */
57
/** Carry out actions before new task runs. */
58
void before_task_runs(void)
58
void before_task_runs(void)
59
{
59
{
60
    before_task_runs_arch();
60
    before_task_runs_arch();
61
}
61
}
62
 
62
 
63
/** Take actions before new thread runs.
63
/** Take actions before new thread runs.
64
 *
64
 *
65
 * Perform actions that need to be
65
 * Perform actions that need to be
66
 * taken before the newly selected
66
 * taken before the newly selected
67
 * tread is passed control.
67
 * tread is passed control.
68
 *
68
 *
69
 * THREAD->lock is locked on entry
69
 * THREAD->lock is locked on entry
70
 *
70
 *
71
 */
71
 */
72
void before_thread_runs(void)
72
void before_thread_runs(void)
73
{
73
{
74
    before_thread_runs_arch();
74
    before_thread_runs_arch();
75
#ifdef CONFIG_FPU_LAZY
75
#ifdef CONFIG_FPU_LAZY
76
    if(THREAD==CPU->fpu_owner)
76
    if(THREAD==CPU->fpu_owner)
77
        fpu_enable();
77
        fpu_enable();
78
    else
78
    else
79
        fpu_disable();
79
        fpu_disable();
80
#else
80
#else
81
    fpu_enable();
81
    fpu_enable();
82
    if (THREAD->fpu_context_exists)
82
    if (THREAD->fpu_context_exists)
83
        fpu_context_restore(THREAD->saved_fpu_context);
83
        fpu_context_restore(THREAD->saved_fpu_context);
84
    else {
84
    else {
85
        fpu_init();
85
        fpu_init();
86
        THREAD->fpu_context_exists=1;
86
        THREAD->fpu_context_exists=1;
87
    }
87
    }
88
#endif
88
#endif
89
}
89
}
90
 
90
 
91
/** Take actions after THREAD had run.
91
/** Take actions after THREAD had run.
92
 *
92
 *
93
 * Perform actions that need to be
93
 * Perform actions that need to be
94
 * taken after the running thread
94
 * taken after the running thread
95
 * had been preempted by the scheduler.
95
 * had been preempted by the scheduler.
96
 *
96
 *
97
 * THREAD->lock is locked on entry
97
 * THREAD->lock is locked on entry
98
 *
98
 *
99
 */
99
 */
100
void after_thread_ran(void)
100
void after_thread_ran(void)
101
{
101
{
102
    after_thread_ran_arch();
102
    after_thread_ran_arch();
103
}
103
}
104
 
104
 
105
#ifdef CONFIG_FPU_LAZY
105
#ifdef CONFIG_FPU_LAZY
106
void scheduler_fpu_lazy_request(void)
106
void scheduler_fpu_lazy_request(void)
107
{
107
{
108
restart:
108
restart:
109
    fpu_enable();
109
    fpu_enable();
110
    spinlock_lock(&CPU->lock);
110
    spinlock_lock(&CPU->lock);
111
 
111
 
112
    /* Save old context */
112
    /* Save old context */
113
    if (CPU->fpu_owner != NULL) {  
113
    if (CPU->fpu_owner != NULL) {  
114
        spinlock_lock(&CPU->fpu_owner->lock);
114
        spinlock_lock(&CPU->fpu_owner->lock);
115
        fpu_context_save(CPU->fpu_owner->saved_fpu_context);
115
        fpu_context_save(CPU->fpu_owner->saved_fpu_context);
116
        /* don't prevent migration */
116
        /* don't prevent migration */
117
        CPU->fpu_owner->fpu_context_engaged=0;
117
        CPU->fpu_owner->fpu_context_engaged=0;
118
        spinlock_unlock(&CPU->fpu_owner->lock);
118
        spinlock_unlock(&CPU->fpu_owner->lock);
119
        CPU->fpu_owner = NULL;
119
        CPU->fpu_owner = NULL;
120
    }
120
    }
121
 
121
 
122
    spinlock_lock(&THREAD->lock);
122
    spinlock_lock(&THREAD->lock);
123
    if (THREAD->fpu_context_exists) {
123
    if (THREAD->fpu_context_exists) {
124
        fpu_context_restore(THREAD->saved_fpu_context);
124
        fpu_context_restore(THREAD->saved_fpu_context);
125
    } else {
125
    } else {
126
        /* Allocate FPU context */
126
        /* Allocate FPU context */
127
        if (!THREAD->saved_fpu_context) {
127
        if (!THREAD->saved_fpu_context) {
128
            /* Might sleep */
128
            /* Might sleep */
129
            spinlock_unlock(&THREAD->lock);
129
            spinlock_unlock(&THREAD->lock);
130
            spinlock_unlock(&CPU->lock);
130
            spinlock_unlock(&CPU->lock);
131
            THREAD->saved_fpu_context = slab_alloc(fpu_context_slab,
131
            THREAD->saved_fpu_context = slab_alloc(fpu_context_slab,
132
                                   0);
132
                                   0);
133
            /* We may have switched CPUs during slab_alloc */
133
            /* We may have switched CPUs during slab_alloc */
134
            goto restart;
134
            goto restart;
135
        }
135
        }
136
        fpu_init();
136
        fpu_init();
137
        THREAD->fpu_context_exists=1;
137
        THREAD->fpu_context_exists=1;
138
    }
138
    }
139
    CPU->fpu_owner=THREAD;
139
    CPU->fpu_owner=THREAD;
140
    THREAD->fpu_context_engaged = 1;
140
    THREAD->fpu_context_engaged = 1;
141
    spinlock_unlock(&THREAD->lock);
141
    spinlock_unlock(&THREAD->lock);
142
 
142
 
143
    spinlock_unlock(&CPU->lock);
143
    spinlock_unlock(&CPU->lock);
144
}
144
}
145
#endif
145
#endif
146
 
146
 
147
/** Initialize scheduler
147
/** Initialize scheduler
148
 *
148
 *
149
 * Initialize kernel scheduler.
149
 * Initialize kernel scheduler.
150
 *
150
 *
151
 */
151
 */
152
void scheduler_init(void)
152
void scheduler_init(void)
153
{
153
{
154
}
154
}
155
 
155
 
156
/** Get thread to be scheduled
156
/** Get thread to be scheduled
157
 *
157
 *
158
 * Get the optimal thread to be scheduled
158
 * Get the optimal thread to be scheduled
159
 * according to thread accounting and scheduler
159
 * according to thread accounting and scheduler
160
 * policy.
160
 * policy.
161
 *
161
 *
162
 * @return Thread to be scheduled.
162
 * @return Thread to be scheduled.
163
 *
163
 *
164
 */
164
 */
165
static thread_t *find_best_thread(void)
165
static thread_t *find_best_thread(void)
166
{
166
{
167
    thread_t *t;
167
    thread_t *t;
168
    runq_t *r;
168
    runq_t *r;
169
    int i;
169
    int i;
170
 
170
 
171
    ASSERT(CPU != NULL);
171
    ASSERT(CPU != NULL);
172
 
172
 
173
loop:
173
loop:
174
    interrupts_enable();
174
    interrupts_enable();
175
   
175
   
176
    if (atomic_get(&CPU->nrdy) == 0) {
176
    if (atomic_get(&CPU->nrdy) == 0) {
177
        /*
177
        /*
178
         * For there was nothing to run, the CPU goes to sleep
178
         * For there was nothing to run, the CPU goes to sleep
179
         * until a hardware interrupt or an IPI comes.
179
         * until a hardware interrupt or an IPI comes.
180
         * This improves energy saving and hyperthreading.
180
         * This improves energy saving and hyperthreading.
181
         */
181
         */
182
 
182
 
183
        /*
183
        /*
184
         * An interrupt might occur right now and wake up a thread.
184
         * An interrupt might occur right now and wake up a thread.
185
         * In such case, the CPU will continue to go to sleep
185
         * In such case, the CPU will continue to go to sleep
186
         * even though there is a runnable thread.
186
         * even though there is a runnable thread.
187
         */
187
         */
188
 
188
 
189
         cpu_sleep();
189
         cpu_sleep();
190
         goto loop;
190
         goto loop;
191
    }
191
    }
192
 
192
 
193
    interrupts_disable();
193
    interrupts_disable();
194
   
194
   
195
    for (i = 0; i<RQ_COUNT; i++) {
195
    for (i = 0; i<RQ_COUNT; i++) {
196
        r = &CPU->rq[i];
196
        r = &CPU->rq[i];
197
        spinlock_lock(&r->lock);
197
        spinlock_lock(&r->lock);
198
        if (r->n == 0) {
198
        if (r->n == 0) {
199
            /*
199
            /*
200
             * If this queue is empty, try a lower-priority queue.
200
             * If this queue is empty, try a lower-priority queue.
201
             */
201
             */
202
            spinlock_unlock(&r->lock);
202
            spinlock_unlock(&r->lock);
203
            continue;
203
            continue;
204
        }
204
        }
205
 
205
 
206
        atomic_dec(&CPU->nrdy);
206
        atomic_dec(&CPU->nrdy);
207
        atomic_dec(&nrdy);
207
        atomic_dec(&nrdy);
208
        r->n--;
208
        r->n--;
209
 
209
 
210
        /*
210
        /*
211
         * Take the first thread from the queue.
211
         * Take the first thread from the queue.
212
         */
212
         */
213
        t = list_get_instance(r->rq_head.next, thread_t, rq_link);
213
        t = list_get_instance(r->rq_head.next, thread_t, rq_link);
214
        list_remove(&t->rq_link);
214
        list_remove(&t->rq_link);
215
 
215
 
216
        spinlock_unlock(&r->lock);
216
        spinlock_unlock(&r->lock);
217
 
217
 
218
        spinlock_lock(&t->lock);
218
        spinlock_lock(&t->lock);
219
        t->cpu = CPU;
219
        t->cpu = CPU;
220
 
220
 
221
        t->ticks = us2ticks((i+1)*10000);
221
        t->ticks = us2ticks((i+1)*10000);
222
        t->priority = i;    /* correct rq index */
222
        t->priority = i;    /* correct rq index */
223
 
223
 
224
        /*
224
        /*
225
         * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge.
225
         * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge.
226
         */
226
         */
227
        t->flags &= ~X_STOLEN;
227
        t->flags &= ~X_STOLEN;
228
        spinlock_unlock(&t->lock);
228
        spinlock_unlock(&t->lock);
229
 
229
 
230
        return t;
230
        return t;
231
    }
231
    }
232
    goto loop;
232
    goto loop;
233
 
233
 
234
}
234
}
235
 
235
 
236
/** Prevent rq starvation
236
/** Prevent rq starvation
237
 *
237
 *
238
 * Prevent low priority threads from starving in rq's.
238
 * Prevent low priority threads from starving in rq's.
239
 *
239
 *
240
 * When the function decides to relink rq's, it reconnects
240
 * When the function decides to relink rq's, it reconnects
241
 * respective pointers so that in result threads with 'pri'
241
 * respective pointers so that in result threads with 'pri'
242
 * greater or equal 'start' are moved to a higher-priority queue.
242
 * greater or equal 'start' are moved to a higher-priority queue.
243
 *
243
 *
244
 * @param start Threshold priority.
244
 * @param start Threshold priority.
245
 *
245
 *
246
 */
246
 */
247
static void relink_rq(int start)
247
static void relink_rq(int start)
248
{
248
{
249
    link_t head;
249
    link_t head;
250
    runq_t *r;
250
    runq_t *r;
251
    int i, n;
251
    int i, n;
252
 
252
 
253
    list_initialize(&head);
253
    list_initialize(&head);
254
    spinlock_lock(&CPU->lock);
254
    spinlock_lock(&CPU->lock);
255
    if (CPU->needs_relink > NEEDS_RELINK_MAX) {
255
    if (CPU->needs_relink > NEEDS_RELINK_MAX) {
256
        for (i = start; i<RQ_COUNT-1; i++) {
256
        for (i = start; i<RQ_COUNT-1; i++) {
257
            /* remember and empty rq[i + 1] */
257
            /* remember and empty rq[i + 1] */
258
            r = &CPU->rq[i + 1];
258
            r = &CPU->rq[i + 1];
259
            spinlock_lock(&r->lock);
259
            spinlock_lock(&r->lock);
260
            list_concat(&head, &r->rq_head);
260
            list_concat(&head, &r->rq_head);
261
            n = r->n;
261
            n = r->n;
262
            r->n = 0;
262
            r->n = 0;
263
            spinlock_unlock(&r->lock);
263
            spinlock_unlock(&r->lock);
264
       
264
       
265
            /* append rq[i + 1] to rq[i] */
265
            /* append rq[i + 1] to rq[i] */
266
            r = &CPU->rq[i];
266
            r = &CPU->rq[i];
267
            spinlock_lock(&r->lock);
267
            spinlock_lock(&r->lock);
268
            list_concat(&r->rq_head, &head);
268
            list_concat(&r->rq_head, &head);
269
            r->n += n;
269
            r->n += n;
270
            spinlock_unlock(&r->lock);
270
            spinlock_unlock(&r->lock);
271
        }
271
        }
272
        CPU->needs_relink = 0;
272
        CPU->needs_relink = 0;
273
    }
273
    }
274
    spinlock_unlock(&CPU->lock);
274
    spinlock_unlock(&CPU->lock);
275
 
275
 
276
}
276
}
277
 
277
 
278
/** The scheduler
278
/** The scheduler
279
 *
279
 *
280
 * The thread scheduling procedure.
280
 * The thread scheduling procedure.
281
 * Passes control directly to
281
 * Passes control directly to
282
 * scheduler_separated_stack().
282
 * scheduler_separated_stack().
283
 *
283
 *
284
 */
284
 */
285
void scheduler(void)
285
void scheduler(void)
286
{
286
{
287
    volatile ipl_t ipl;
287
    volatile ipl_t ipl;
288
 
288
 
289
    ASSERT(CPU != NULL);
289
    ASSERT(CPU != NULL);
290
 
290
 
291
    ipl = interrupts_disable();
291
    ipl = interrupts_disable();
292
 
292
 
293
    if (atomic_get(&haltstate))
293
    if (atomic_get(&haltstate))
294
        halt();
294
        halt();
295
   
295
   
296
    if (THREAD) {
296
    if (THREAD) {
297
        spinlock_lock(&THREAD->lock);
297
        spinlock_lock(&THREAD->lock);
298
#ifndef CONFIG_FPU_LAZY
298
#ifndef CONFIG_FPU_LAZY
299
        fpu_context_save(THREAD->saved_fpu_context);
299
        fpu_context_save(THREAD->saved_fpu_context);
300
#endif
300
#endif
301
        if (!context_save(&THREAD->saved_context)) {
301
        if (!context_save(&THREAD->saved_context)) {
302
            /*
302
            /*
303
             * This is the place where threads leave scheduler();
303
             * This is the place where threads leave scheduler();
304
             */
304
             */
305
            spinlock_unlock(&THREAD->lock);
305
            spinlock_unlock(&THREAD->lock);
306
            interrupts_restore(THREAD->saved_context.ipl);
306
            interrupts_restore(THREAD->saved_context.ipl);
307
           
307
           
308
            return;
308
            return;
309
        }
309
        }
310
 
310
 
311
        /*
311
        /*
312
         * Interrupt priority level of preempted thread is recorded here
312
         * Interrupt priority level of preempted thread is recorded here
313
         * to facilitate scheduler() invocations from interrupts_disable()'d
313
         * to facilitate scheduler() invocations from interrupts_disable()'d
314
         * code (e.g. waitq_sleep_timeout()).
314
         * code (e.g. waitq_sleep_timeout()).
315
         */
315
         */
316
        THREAD->saved_context.ipl = ipl;
316
        THREAD->saved_context.ipl = ipl;
317
    }
317
    }
318
 
318
 
319
    /*
319
    /*
320
     * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, VM
320
     * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, VM
321
     * and preemption counter. At this point THE could be coming either
321
     * and preemption counter. At this point THE could be coming either
322
     * from THREAD's or CPU's stack.
322
     * from THREAD's or CPU's stack.
323
     */
323
     */
324
    the_copy(THE, (the_t *) CPU->stack);
324
    the_copy(THE, (the_t *) CPU->stack);
325
 
325
 
326
    /*
326
    /*
327
     * We may not keep the old stack.
327
     * We may not keep the old stack.
328
     * Reason: If we kept the old stack and got blocked, for instance, in
328
     * Reason: If we kept the old stack and got blocked, for instance, in
329
     * find_best_thread(), the old thread could get rescheduled by another
329
     * find_best_thread(), the old thread could get rescheduled by another
330
     * CPU and overwrite the part of its own stack that was also used by
330
     * CPU and overwrite the part of its own stack that was also used by
331
     * the scheduler on this CPU.
331
     * the scheduler on this CPU.
332
     *
332
     *
333
     * Moreover, we have to bypass the compiler-generated POP sequence
333
     * Moreover, we have to bypass the compiler-generated POP sequence
334
     * which is fooled by SP being set to the very top of the stack.
334
     * which is fooled by SP being set to the very top of the stack.
335
     * Therefore the scheduler() function continues in
335
     * Therefore the scheduler() function continues in
336
     * scheduler_separated_stack().
336
     * scheduler_separated_stack().
337
     */
337
     */
338
    context_save(&CPU->saved_context);
338
    context_save(&CPU->saved_context);
339
    context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
339
    context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
340
    context_restore(&CPU->saved_context);
340
    context_restore(&CPU->saved_context);
341
    /* not reached */
341
    /* not reached */
342
}
342
}
343
 
343
 
344
/** Scheduler stack switch wrapper
344
/** Scheduler stack switch wrapper
345
 *
345
 *
346
 * Second part of the scheduler() function
346
 * Second part of the scheduler() function
347
 * using new stack. Handling the actual context
347
 * using new stack. Handling the actual context
348
 * switch to a new thread.
348
 * switch to a new thread.
349
 *
349
 *
350
 * Assume THREAD->lock is held.
350
 * Assume THREAD->lock is held.
351
 */
351
 */
352
void scheduler_separated_stack(void)
352
void scheduler_separated_stack(void)
353
{
353
{
354
    int priority;
354
    int priority;
355
   
355
   
356
    ASSERT(CPU != NULL);
356
    ASSERT(CPU != NULL);
357
   
357
   
358
    if (THREAD) {
358
    if (THREAD) {
359
        /* must be run after the switch to scheduler stack */
359
        /* must be run after the switch to scheduler stack */
360
        after_thread_ran();
360
        after_thread_ran();
361
 
361
 
362
        switch (THREAD->state) {
362
        switch (THREAD->state) {
363
            case Running:
363
            case Running:
364
            spinlock_unlock(&THREAD->lock);
364
            spinlock_unlock(&THREAD->lock);
365
            thread_ready(THREAD);
365
            thread_ready(THREAD);
366
            break;
366
            break;
367
 
367
 
368
            case Exiting:
368
            case Exiting:
369
            thread_destroy(THREAD);
369
            thread_destroy(THREAD);
370
            break;
370
            break;
371
           
371
           
372
            case Sleeping:
372
            case Sleeping:
373
            /*
373
            /*
374
             * Prefer the thread after it's woken up.
374
             * Prefer the thread after it's woken up.
375
             */
375
             */
376
            THREAD->priority = -1;
376
            THREAD->priority = -1;
377
 
377
 
378
            /*
378
            /*
379
             * We need to release wq->lock which we locked in waitq_sleep().
379
             * We need to release wq->lock which we locked in waitq_sleep().
380
             * Address of wq->lock is kept in THREAD->sleep_queue.
380
             * Address of wq->lock is kept in THREAD->sleep_queue.
381
             */
381
             */
382
            spinlock_unlock(&THREAD->sleep_queue->lock);
382
            spinlock_unlock(&THREAD->sleep_queue->lock);
383
 
383
 
384
            /*
384
            /*
385
             * Check for possible requests for out-of-context invocation.
385
             * Check for possible requests for out-of-context invocation.
386
             */
386
             */
387
            if (THREAD->call_me) {
387
            if (THREAD->call_me) {
388
                THREAD->call_me(THREAD->call_me_with);
388
                THREAD->call_me(THREAD->call_me_with);
389
                THREAD->call_me = NULL;
389
                THREAD->call_me = NULL;
390
                THREAD->call_me_with = NULL;
390
                THREAD->call_me_with = NULL;
391
            }
391
            }
392
 
392
 
393
            spinlock_unlock(&THREAD->lock);
393
            spinlock_unlock(&THREAD->lock);
394
 
394
 
395
            break;
395
            break;
396
 
396
 
397
            default:
397
            default:
398
            /*
398
            /*
399
             * Entering state is unexpected.
399
             * Entering state is unexpected.
400
             */
400
             */
401
            panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]);
401
            panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]);
402
            break;
402
            break;
403
        }
403
        }
404
 
404
 
405
        THREAD = NULL;
405
        THREAD = NULL;
406
    }
406
    }
407
 
407
 
408
    THREAD = find_best_thread();
408
    THREAD = find_best_thread();
409
   
409
   
410
    spinlock_lock(&THREAD->lock);
410
    spinlock_lock(&THREAD->lock);
411
    priority = THREAD->priority;
411
    priority = THREAD->priority;
412
    spinlock_unlock(&THREAD->lock);
412
    spinlock_unlock(&THREAD->lock);
413
 
413
 
414
    relink_rq(priority);       
414
    relink_rq(priority);       
415
 
415
 
416
    spinlock_lock(&THREAD->lock);  
416
    spinlock_lock(&THREAD->lock);  
417
 
417
 
418
    /*
418
    /*
419
     * If both the old and the new task are the same, lots of work is avoided.
419
     * If both the old and the new task are the same, lots of work is avoided.
420
     */
420
     */
421
    if (TASK != THREAD->task) {
421
    if (TASK != THREAD->task) {
422
        as_t *as1 = NULL;
422
        as_t *as1 = NULL;
423
        as_t *as2;
423
        as_t *as2;
424
 
424
 
425
        if (TASK) {
425
        if (TASK) {
426
            spinlock_lock(&TASK->lock);
426
            spinlock_lock(&TASK->lock);
427
            as1 = TASK->as;
427
            as1 = TASK->as;
428
            spinlock_unlock(&TASK->lock);
428
            spinlock_unlock(&TASK->lock);
429
        }
429
        }
430
 
430
 
431
        spinlock_lock(&THREAD->task->lock);
431
        spinlock_lock(&THREAD->task->lock);
432
        as2 = THREAD->task->as;
432
        as2 = THREAD->task->as;
433
        spinlock_unlock(&THREAD->task->lock);
433
        spinlock_unlock(&THREAD->task->lock);
434
       
434
       
435
        /*
435
        /*
436
         * Note that it is possible for two tasks to share one address space.
436
         * Note that it is possible for two tasks to share one address space.
437
         */
437
         */
438
        if (as1 != as2) {
438
        if (as1 != as2) {
439
            /*
439
            /*
440
             * Both tasks and address spaces are different.
440
             * Both tasks and address spaces are different.
441
             * Replace the old one with the new one.
441
             * Replace the old one with the new one.
442
             */
442
             */
443
            as_switch(as1, as2);
443
            as_switch(as1, as2);
444
        }
444
        }
445
        TASK = THREAD->task;
445
        TASK = THREAD->task;
446
        before_task_runs();
446
        before_task_runs();
447
    }
447
    }
448
 
448
 
449
    THREAD->state = Running;
449
    THREAD->state = Running;
450
 
450
 
451
#ifdef SCHEDULER_VERBOSE
451
#ifdef SCHEDULER_VERBOSE
452
    printf("cpu%d: tid %d (priority=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, atomic_get(&CPU->nrdy));
452
    printf("cpu%d: tid %d (priority=%d,ticks=%lld,nrdy=%ld)\n", CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, atomic_get(&CPU->nrdy));
453
#endif  
453
#endif  
454
 
454
 
455
    /*
455
    /*
456
     * Some architectures provide late kernel PA2KA(identity)
456
     * Some architectures provide late kernel PA2KA(identity)
457
     * mapping in a page fault handler. However, the page fault
457
     * mapping in a page fault handler. However, the page fault
458
     * handler uses the kernel stack of the running thread and
458
     * handler uses the kernel stack of the running thread and
459
     * therefore cannot be used to map it. The kernel stack, if
459
     * therefore cannot be used to map it. The kernel stack, if
460
     * necessary, is to be mapped in before_thread_runs(). This
460
     * necessary, is to be mapped in before_thread_runs(). This
461
     * function must be executed before the switch to the new stack.
461
     * function must be executed before the switch to the new stack.
462
     */
462
     */
463
    before_thread_runs();
463
    before_thread_runs();
464
 
464
 
465
    /*
465
    /*
466
     * Copy the knowledge of CPU, TASK, THREAD and preemption counter to thread's stack.
466
     * Copy the knowledge of CPU, TASK, THREAD and preemption counter to thread's stack.
467
     */
467
     */
468
    the_copy(THE, (the_t *) THREAD->kstack);
468
    the_copy(THE, (the_t *) THREAD->kstack);
469
   
469
   
470
    context_restore(&THREAD->saved_context);
470
    context_restore(&THREAD->saved_context);
471
    /* not reached */
471
    /* not reached */
472
}
472
}
473
 
473
 
474
#ifdef CONFIG_SMP
474
#ifdef CONFIG_SMP
475
/** Load balancing thread
475
/** Load balancing thread
476
 *
476
 *
477
 * SMP load balancing thread, supervising thread supplies
477
 * SMP load balancing thread, supervising thread supplies
478
 * for the CPU it's wired to.
478
 * for the CPU it's wired to.
479
 *
479
 *
480
 * @param arg Generic thread argument (unused).
480
 * @param arg Generic thread argument (unused).
481
 *
481
 *
482
 */
482
 */
483
void kcpulb(void *arg)
483
void kcpulb(void *arg)
484
{
484
{
485
    thread_t *t;
485
    thread_t *t;
486
    int count, average, i, j, k = 0;
486
    int count, average, i, j, k = 0;
487
    ipl_t ipl;
487
    ipl_t ipl;
488
 
488
 
489
loop:
489
loop:
490
    /*
490
    /*
491
     * Work in 1s intervals.
491
     * Work in 1s intervals.
492
     */
492
     */
493
    thread_sleep(1);
493
    thread_sleep(1);
494
 
494
 
495
not_satisfied:
495
not_satisfied:
496
    /*
496
    /*
497
     * Calculate the number of threads that will be migrated/stolen from
497
     * Calculate the number of threads that will be migrated/stolen from
498
     * other CPU's. Note that situation can have changed between two
498
     * other CPU's. Note that situation can have changed between two
499
     * passes. Each time get the most up to date counts.
499
     * passes. Each time get the most up to date counts.
500
     */
500
     */
501
    average = atomic_get(&nrdy) / config.cpu_active + 1;
501
    average = atomic_get(&nrdy) / config.cpu_active + 1;
502
    count = average - atomic_get(&CPU->nrdy);
502
    count = average - atomic_get(&CPU->nrdy);
503
 
503
 
504
    if (count <= 0)
504
    if (count <= 0)
505
        goto satisfied;
505
        goto satisfied;
506
 
506
 
507
    /*
507
    /*
508
     * Searching least priority queues on all CPU's first and most priority queues on all CPU's last.
508
     * Searching least priority queues on all CPU's first and most priority queues on all CPU's last.
509
     */
509
     */
510
    for (j=RQ_COUNT-1; j >= 0; j--) {
510
    for (j=RQ_COUNT-1; j >= 0; j--) {
511
        for (i=0; i < config.cpu_active; i++) {
511
        for (i=0; i < config.cpu_active; i++) {
512
            link_t *l;
512
            link_t *l;
513
            runq_t *r;
513
            runq_t *r;
514
            cpu_t *cpu;
514
            cpu_t *cpu;
515
 
515
 
516
            cpu = &cpus[(i + k) % config.cpu_active];
516
            cpu = &cpus[(i + k) % config.cpu_active];
517
 
517
 
518
            /*
518
            /*
519
             * Not interested in ourselves.
519
             * Not interested in ourselves.
520
             * Doesn't require interrupt disabling for kcpulb is X_WIRED.
520
             * Doesn't require interrupt disabling for kcpulb is X_WIRED.
521
             */
521
             */
522
            if (CPU == cpu)
522
            if (CPU == cpu)
523
                continue;
523
                continue;
524
            if (atomic_get(&cpu->nrdy) <= average)
524
            if (atomic_get(&cpu->nrdy) <= average)
525
                continue;
525
                continue;
526
 
526
 
527
            ipl = interrupts_disable();
527
            ipl = interrupts_disable();
528
            r = &cpu->rq[j];
528
            r = &cpu->rq[j];
529
            spinlock_lock(&r->lock);
529
            spinlock_lock(&r->lock);
530
            if (r->n == 0) {
530
            if (r->n == 0) {
531
                spinlock_unlock(&r->lock);
531
                spinlock_unlock(&r->lock);
532
                interrupts_restore(ipl);
532
                interrupts_restore(ipl);
533
                continue;
533
                continue;
534
            }
534
            }
535
       
535
       
536
            t = NULL;
536
            t = NULL;
537
            l = r->rq_head.prev;    /* search rq from the back */
537
            l = r->rq_head.prev;    /* search rq from the back */
538
            while (l != &r->rq_head) {
538
            while (l != &r->rq_head) {
539
                t = list_get_instance(l, thread_t, rq_link);
539
                t = list_get_instance(l, thread_t, rq_link);
540
                /*
540
                /*
541
                 * We don't want to steal CPU-wired threads neither threads already stolen.
541
                 * We don't want to steal CPU-wired threads neither threads already stolen.
542
                 * The latter prevents threads from migrating between CPU's without ever being run.
542
                 * The latter prevents threads from migrating between CPU's without ever being run.
543
                 * We don't want to steal threads whose FPU context is still in CPU.
543
                 * We don't want to steal threads whose FPU context is still in CPU.
544
                 */
544
                 */
545
                spinlock_lock(&t->lock);
545
                spinlock_lock(&t->lock);
546
                if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) {
546
                if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) {
547
                    /*
547
                    /*
548
                     * Remove t from r.
548
                     * Remove t from r.
549
                     */
549
                     */
550
                    spinlock_unlock(&t->lock);
550
                    spinlock_unlock(&t->lock);
551
                   
551
                   
552
                    atomic_dec(&cpu->nrdy);
552
                    atomic_dec(&cpu->nrdy);
553
                    atomic_dec(&nrdy);
553
                    atomic_dec(&nrdy);
554
 
554
 
555
                    r->n--;
555
                    r->n--;
556
                    list_remove(&t->rq_link);
556
                    list_remove(&t->rq_link);
557
 
557
 
558
                    break;
558
                    break;
559
                }
559
                }
560
                spinlock_unlock(&t->lock);
560
                spinlock_unlock(&t->lock);
561
                l = l->prev;
561
                l = l->prev;
562
                t = NULL;
562
                t = NULL;
563
            }
563
            }
564
            spinlock_unlock(&r->lock);
564
            spinlock_unlock(&r->lock);
565
 
565
 
566
            if (t) {
566
            if (t) {
567
                /*
567
                /*
568
                 * Ready t on local CPU
568
                 * Ready t on local CPU
569
                 */
569
                 */
570
                spinlock_lock(&t->lock);
570
                spinlock_lock(&t->lock);
571
#ifdef KCPULB_VERBOSE
571
#ifdef KCPULB_VERBOSE
572
                printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, atomic_get(&CPU->nrdy), atomic_get(&nrdy) / config.cpu_active);
572
                printf("kcpulb%d: TID %d -> cpu%d, nrdy=%ld, avg=%nd\n", CPU->id, t->tid, CPU->id, atomic_get(&CPU->nrdy), atomic_get(&nrdy) / config.cpu_active);
573
#endif
573
#endif
574
                t->flags |= X_STOLEN;
574
                t->flags |= X_STOLEN;
575
                t->state = Entering;
575
                t->state = Entering;
576
                spinlock_unlock(&t->lock);
576
                spinlock_unlock(&t->lock);
577
   
577
   
578
                thread_ready(t);
578
                thread_ready(t);
579
 
579
 
580
                interrupts_restore(ipl);
580
                interrupts_restore(ipl);
581
   
581
   
582
                if (--count == 0)
582
                if (--count == 0)
583
                    goto satisfied;
583
                    goto satisfied;
584
                   
584
                   
585
                /*
585
                /*
586
                 * We are not satisfied yet, focus on another CPU next time.
586
                 * We are not satisfied yet, focus on another CPU next time.
587
                 */
587
                 */
588
                k++;
588
                k++;
589
               
589
               
590
                continue;
590
                continue;
591
            }
591
            }
592
            interrupts_restore(ipl);
592
            interrupts_restore(ipl);
593
        }
593
        }
594
    }
594
    }
595
 
595
 
596
    if (atomic_get(&CPU->nrdy)) {
596
    if (atomic_get(&CPU->nrdy)) {
597
        /*
597
        /*
598
         * Be a little bit light-weight and let migrated threads run.
598
         * Be a little bit light-weight and let migrated threads run.
599
         */
599
         */
600
        scheduler();
600
        scheduler();
601
    } else {
601
    } else {
602
        /*
602
        /*
603
         * We failed to migrate a single thread.
603
         * We failed to migrate a single thread.
604
         * Give up this turn.
604
         * Give up this turn.
605
         */
605
         */
606
        goto loop;
606
        goto loop;
607
    }
607
    }
608
       
608
       
609
    goto not_satisfied;
609
    goto not_satisfied;
610
 
610
 
611
satisfied:
611
satisfied:
612
    goto loop;
612
    goto loop;
613
}
613
}
614
 
614
 
615
#endif /* CONFIG_SMP */
615
#endif /* CONFIG_SMP */
616
 
616
 
617
 
617
 
618
/** Print information about threads & scheduler queues */
618
/** Print information about threads & scheduler queues */
619
void sched_print_list(void)
619
void sched_print_list(void)
620
{
620
{
621
    ipl_t ipl;
621
    ipl_t ipl;
622
    int cpu,i;
622
    int cpu,i;
623
    runq_t *r;
623
    runq_t *r;
624
    thread_t *t;
624
    thread_t *t;
625
    link_t *cur;
625
    link_t *cur;
626
 
626
 
627
    /* We are going to mess with scheduler structures,
627
    /* We are going to mess with scheduler structures,
628
     * let's not be interrupted */
628
     * let's not be interrupted */
629
    ipl = interrupts_disable();
629
    ipl = interrupts_disable();
630
    for (cpu=0;cpu < config.cpu_count; cpu++) {
630
    for (cpu=0;cpu < config.cpu_count; cpu++) {
631
 
631
 
632
        if (!cpus[cpu].active)
632
        if (!cpus[cpu].active)
633
            continue;
633
            continue;
634
 
634
 
635
        spinlock_lock(&cpus[cpu].lock);
635
        spinlock_lock(&cpus[cpu].lock);
636
        printf("cpu%d: address=%P, nrdy=%d, needs_relink=%d\n",
636
        printf("cpu%d: address=%P, nrdy=%ld, needs_relink=%ld\n",
637
               cpus[cpu].id, &cpus[cpu], atomic_get(&cpus[cpu].nrdy), cpus[cpu].needs_relink);
637
               cpus[cpu].id, &cpus[cpu], atomic_get(&cpus[cpu].nrdy), cpus[cpu].needs_relink);
638
       
638
       
639
        for (i=0; i<RQ_COUNT; i++) {
639
        for (i=0; i<RQ_COUNT; i++) {
640
            r = &cpus[cpu].rq[i];
640
            r = &cpus[cpu].rq[i];
641
            spinlock_lock(&r->lock);
641
            spinlock_lock(&r->lock);
642
            if (!r->n) {
642
            if (!r->n) {
643
                spinlock_unlock(&r->lock);
643
                spinlock_unlock(&r->lock);
644
                continue;
644
                continue;
645
            }
645
            }
646
            printf("\trq[%d]: ", i);
646
            printf("\trq[%d]: ", i);
647
            for (cur=r->rq_head.next; cur!=&r->rq_head; cur=cur->next) {
647
            for (cur=r->rq_head.next; cur!=&r->rq_head; cur=cur->next) {
648
                t = list_get_instance(cur, thread_t, rq_link);
648
                t = list_get_instance(cur, thread_t, rq_link);
649
                printf("%d(%s) ", t->tid,
649
                printf("%d(%s) ", t->tid,
650
                       thread_states[t->state]);
650
                       thread_states[t->state]);
651
            }
651
            }
652
            printf("\n");
652
            printf("\n");
653
            spinlock_unlock(&r->lock);
653
            spinlock_unlock(&r->lock);
654
        }
654
        }
655
        spinlock_unlock(&cpus[cpu].lock);
655
        spinlock_unlock(&cpus[cpu].lock);
656
    }
656
    }
657
   
657
   
658
    interrupts_restore(ipl);
658
    interrupts_restore(ipl);
659
}
659
}
660
 
660