Subversion Repositories HelenOS-historic

Rev

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

Rev 114 Rev 115
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 <cpu.h>
32
#include <cpu.h>
33
#include <mm/vm.h>
33
#include <mm/vm.h>
34
#include <config.h>
34
#include <config.h>
35
#include <context.h>
35
#include <context.h>
36
#include <func.h>
36
#include <func.h>
37
#include <arch.h>
37
#include <arch.h>
38
#include <arch/asm.h>
38
#include <arch/asm.h>
39
#include <list.h>
39
#include <list.h>
40
#include <panic.h>
40
#include <panic.h>
41
#include <typedefs.h>
41
#include <typedefs.h>
42
#include <mm/page.h>
42
#include <mm/page.h>
43
#include <synch/spinlock.h>
43
#include <synch/spinlock.h>
44
#include <arch/faddr.h>
44
#include <arch/faddr.h>
45
#include <arch/atomic.h>
45
#include <arch/atomic.h>
46
 
46
 
47
volatile int nrdy;
47
volatile int nrdy;
48
 
48
 
49
 
49
 
50
/** Initialize context switching
50
/** Initialize context switching
51
 *
51
 *
52
 * Initialize context switching and lazy FPU
52
 * Initialize context switching and lazy FPU
53
 * context switching.
53
 * context switching.
54
 *
54
 *
55
 */
55
 */
56
void before_thread_runs(void)
56
void before_thread_runs(void)
57
{
57
{
58
    before_thread_runs_arch();
58
    before_thread_runs_arch();
59
    fpu_context_restore(&(THREAD->saved_fpu_context));
59
    fpu_context_restore(&(THREAD->saved_fpu_context));
60
}
60
}
61
 
61
 
62
 
62
 
63
/** Initialize scheduler
63
/** Initialize scheduler
64
 *
64
 *
65
 * Initialize kernel scheduler.
65
 * Initialize kernel scheduler.
66
 *
66
 *
67
 */
67
 */
68
void scheduler_init(void)
68
void scheduler_init(void)
69
{
69
{
70
}
70
}
71
 
71
 
72
 
72
 
73
/** Get thread to be scheduled
73
/** Get thread to be scheduled
74
 *
74
 *
75
 * Get the optimal thread to be scheduled
75
 * Get the optimal thread to be scheduled
76
 * according to thread accounting and scheduler
76
 * according to thread accounting and scheduler
77
 * policy.
77
 * policy.
78
 *
78
 *
79
 * @return Thread to be scheduled.
79
 * @return Thread to be scheduled.
80
 *
80
 *
81
 */
81
 */
82
struct thread *find_best_thread(void)
82
struct thread *find_best_thread(void)
83
{
83
{
84
    thread_t *t;
84
    thread_t *t;
85
    runq_t *r;
85
    runq_t *r;
86
    int i, n;
86
    int i, n;
87
 
87
 
88
loop:
88
loop:
89
    cpu_priority_high();
89
    cpu_priority_high();
90
 
90
 
91
    spinlock_lock(&CPU->lock);
91
    spinlock_lock(&CPU->lock);
92
    n = CPU->nrdy;
92
    n = CPU->nrdy;
93
    spinlock_unlock(&CPU->lock);
93
    spinlock_unlock(&CPU->lock);
94
 
94
 
95
    cpu_priority_low();
95
    cpu_priority_low();
96
   
96
   
97
    if (n == 0) {
97
    if (n == 0) {
98
        #ifdef __SMP__
98
        #ifdef __SMP__
99
        /*
99
        /*
100
         * If the load balancing thread is not running, wake it up and
100
         * If the load balancing thread is not running, wake it up and
101
         * set CPU-private flag that the kcpulb has been started.
101
         * set CPU-private flag that the kcpulb has been started.
102
         */
102
         */
103
        if (test_and_set(&CPU->kcpulbstarted) == 0) {
103
        if (test_and_set(&CPU->kcpulbstarted) == 0) {
104
                waitq_wakeup(&CPU->kcpulb_wq, 0);
104
                waitq_wakeup(&CPU->kcpulb_wq, 0);
105
            goto loop;
105
            goto loop;
106
        }
106
        }
107
        #endif /* __SMP__ */
107
        #endif /* __SMP__ */
108
       
108
       
109
        /*
109
        /*
110
         * For there was nothing to run, the CPU goes to sleep
110
         * For there was nothing to run, the CPU goes to sleep
111
         * until a hardware interrupt or an IPI comes.
111
         * until a hardware interrupt or an IPI comes.
112
         * This improves energy saving and hyperthreading.
112
         * This improves energy saving and hyperthreading.
113
         * On the other hand, several hardware interrupts can be ignored.
113
         * On the other hand, several hardware interrupts can be ignored.
114
         */
114
         */
115
         cpu_sleep();
115
         cpu_sleep();
116
         goto loop;
116
         goto loop;
117
    }
117
    }
118
 
118
 
119
    cpu_priority_high();
119
    cpu_priority_high();
120
   
120
   
121
    i = 0;
121
    i = 0;
122
retry:
122
retry:
123
    for (; i<RQ_COUNT; i++) {
123
    for (; i<RQ_COUNT; i++) {
124
        r = &CPU->rq[i];
124
        r = &CPU->rq[i];
125
        spinlock_lock(&r->lock);
125
        spinlock_lock(&r->lock);
126
        if (r->n == 0) {
126
        if (r->n == 0) {
127
            /*
127
            /*
128
             * If this queue is empty, try a lower-priority queue.
128
             * If this queue is empty, try a lower-priority queue.
129
             */
129
             */
130
            spinlock_unlock(&r->lock);
130
            spinlock_unlock(&r->lock);
131
            continue;
131
            continue;
132
        }
132
        }
133
   
133
   
134
        /* avoid deadlock with relink_rq */
134
        /* avoid deadlock with relink_rq() */
135
        if (!spinlock_trylock(&CPU->lock)) {
135
        if (!spinlock_trylock(&CPU->lock)) {
136
            /*
136
            /*
137
             * Unlock r and try again.
137
             * Unlock r and try again.
138
             */
138
             */
139
            spinlock_unlock(&r->lock);
139
            spinlock_unlock(&r->lock);
140
            goto retry;
140
            goto retry;
141
        }
141
        }
142
        CPU->nrdy--;
142
        CPU->nrdy--;
143
        spinlock_unlock(&CPU->lock);
143
        spinlock_unlock(&CPU->lock);
144
 
144
 
145
        atomic_dec(&nrdy);
145
        atomic_dec(&nrdy);
146
        r->n--;
146
        r->n--;
147
 
147
 
148
        /*
148
        /*
149
         * Take the first thread from the queue.
149
         * Take the first thread from the queue.
150
         */
150
         */
151
        t = list_get_instance(r->rq_head.next, thread_t, rq_link);
151
        t = list_get_instance(r->rq_head.next, thread_t, rq_link);
152
        list_remove(&t->rq_link);
152
        list_remove(&t->rq_link);
153
 
153
 
154
        spinlock_unlock(&r->lock);
154
        spinlock_unlock(&r->lock);
155
 
155
 
156
        spinlock_lock(&t->lock);
156
        spinlock_lock(&t->lock);
157
        t->cpu = CPU;
157
        t->cpu = CPU;
158
 
158
 
159
        t->ticks = us2ticks((i+1)*10000);
159
        t->ticks = us2ticks((i+1)*10000);
160
        t->pri = i; /* eventually correct rq index */
160
        t->pri = i; /* eventually correct rq index */
161
 
161
 
162
        /*
162
        /*
163
         * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge.
163
         * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge.
164
         */
164
         */
165
        t->flags &= ~X_STOLEN;
165
        t->flags &= ~X_STOLEN;
166
        spinlock_unlock(&t->lock);
166
        spinlock_unlock(&t->lock);
167
 
167
 
168
        return t;
168
        return t;
169
    }
169
    }
170
    goto loop;
170
    goto loop;
171
 
171
 
172
}
172
}
173
 
173
 
174
 
174
 
175
/** Prevent rq starvation
175
/** Prevent rq starvation
176
 *
176
 *
177
 * Prevent low priority threads from starving in rq's.
177
 * Prevent low priority threads from starving in rq's.
178
 *
178
 *
179
 * When the function decides to relink rq's, it reconnects
179
 * When the function decides to relink rq's, it reconnects
180
 * respective pointers so that in result threads with 'pri'
180
 * respective pointers so that in result threads with 'pri'
181
 * greater or equal 'start' are moved to a higher-priority queue.
181
 * greater or equal 'start' are moved to a higher-priority queue.
182
 *
182
 *
183
 * @param start Threshold priority.
183
 * @param start Threshold priority.
184
 *
184
 *
185
 */
185
 */
186
void relink_rq(int start)
186
void relink_rq(int start)
187
{
187
{
188
    link_t head;
188
    link_t head;
189
    runq_t *r;
189
    runq_t *r;
190
    int i, n;
190
    int i, n;
191
 
191
 
192
    list_initialize(&head);
192
    list_initialize(&head);
193
    spinlock_lock(&CPU->lock);
193
    spinlock_lock(&CPU->lock);
194
    if (CPU->needs_relink > NEEDS_RELINK_MAX) {
194
    if (CPU->needs_relink > NEEDS_RELINK_MAX) {
195
        for (i = start; i<RQ_COUNT-1; i++) {
195
        for (i = start; i<RQ_COUNT-1; i++) {
196
            /* remember and empty rq[i + 1] */
196
            /* remember and empty rq[i + 1] */
197
            r = &CPU->rq[i + 1];
197
            r = &CPU->rq[i + 1];
198
            spinlock_lock(&r->lock);
198
            spinlock_lock(&r->lock);
199
            list_concat(&head, &r->rq_head);
199
            list_concat(&head, &r->rq_head);
200
            n = r->n;
200
            n = r->n;
201
            r->n = 0;
201
            r->n = 0;
202
            spinlock_unlock(&r->lock);
202
            spinlock_unlock(&r->lock);
203
       
203
       
204
            /* append rq[i + 1] to rq[i] */
204
            /* append rq[i + 1] to rq[i] */
205
            r = &CPU->rq[i];
205
            r = &CPU->rq[i];
206
            spinlock_lock(&r->lock);
206
            spinlock_lock(&r->lock);
207
            list_concat(&r->rq_head, &head);
207
            list_concat(&r->rq_head, &head);
208
            r->n += n;
208
            r->n += n;
209
            spinlock_unlock(&r->lock);
209
            spinlock_unlock(&r->lock);
210
        }
210
        }
211
        CPU->needs_relink = 0;
211
        CPU->needs_relink = 0;
212
    }
212
    }
213
    spinlock_unlock(&CPU->lock);               
213
    spinlock_unlock(&CPU->lock);               
214
 
214
 
215
}
215
}
216
 
216
 
217
 
217
 
218
/** The scheduler
218
/** The scheduler
219
 *
219
 *
220
 * The thread scheduling procedure.
220
 * The thread scheduling procedure.
221
 *
221
 *
222
 */
222
 */
223
void scheduler(void)
223
void scheduler(void)
224
{
224
{
225
    volatile pri_t pri;
225
    volatile pri_t pri;
226
 
226
 
227
    pri = cpu_priority_high();
227
    pri = cpu_priority_high();
228
 
228
 
229
    if (haltstate)
229
    if (haltstate)
230
        halt();
230
        halt();
231
 
231
 
232
    if (THREAD) {
232
    if (THREAD) {
233
        spinlock_lock(&THREAD->lock);
233
        spinlock_lock(&THREAD->lock);
234
        fpu_context_save(&(THREAD->saved_fpu_context));
234
        fpu_context_save(&(THREAD->saved_fpu_context));
235
        if (!context_save(&THREAD->saved_context)) {
235
        if (!context_save(&THREAD->saved_context)) {
236
            /*
236
            /*
237
             * This is the place where threads leave scheduler();
237
             * This is the place where threads leave scheduler();
238
             */
238
             */
239
            before_thread_runs();
239
            before_thread_runs();
240
                spinlock_unlock(&THREAD->lock);
240
                spinlock_unlock(&THREAD->lock);
241
            cpu_priority_restore(THREAD->saved_context.pri);
241
            cpu_priority_restore(THREAD->saved_context.pri);
242
            return;
242
            return;
243
        }
243
        }
244
        THREAD->saved_context.pri = pri;
244
        THREAD->saved_context.pri = pri;
245
    }
245
    }
246
 
246
 
247
    /*
247
    /*
248
     * We may not keep the old stack.
248
     * We may not keep the old stack.
249
     * Reason: If we kept the old stack and got blocked, for instance, in
249
     * Reason: If we kept the old stack and got blocked, for instance, in
250
     * find_best_thread(), the old thread could get rescheduled by another
250
     * find_best_thread(), the old thread could get rescheduled by another
251
     * CPU and overwrite the part of its own stack that was also used by
251
     * CPU and overwrite the part of its own stack that was also used by
252
     * the scheduler on this CPU.
252
     * the scheduler on this CPU.
253
     *
253
     *
254
     * Moreover, we have to bypass the compiler-generated POP sequence
254
     * Moreover, we have to bypass the compiler-generated POP sequence
255
     * which is fooled by SP being set to the very top of the stack.
255
     * which is fooled by SP being set to the very top of the stack.
256
     * Therefore the scheduler() function continues in
256
     * Therefore the scheduler() function continues in
257
     * scheduler_separated_stack().
257
     * scheduler_separated_stack().
258
     */
258
     */
259
    context_save(&CPU->saved_context);
259
    context_save(&CPU->saved_context);
260
    context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), CPU->stack, CPU_STACK_SIZE);
260
    context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), CPU->stack, CPU_STACK_SIZE);
261
    context_restore(&CPU->saved_context);
261
    context_restore(&CPU->saved_context);
262
    /* not reached */
262
    /* not reached */
263
}
263
}
264
 
264
 
265
 
265
 
266
/** Scheduler stack switch wrapper
266
/** Scheduler stack switch wrapper
267
 *
267
 *
268
 * Second part of the scheduler() function
268
 * Second part of the scheduler() function
269
 * using new stack. Handling the actual context
269
 * using new stack. Handling the actual context
270
 * switch to a new thread.
270
 * switch to a new thread.
271
 *
271
 *
272
 */
272
 */
273
void scheduler_separated_stack(void)
273
void scheduler_separated_stack(void)
274
{
274
{
275
    int priority;
275
    int priority;
276
 
276
 
277
    if (THREAD) {
277
    if (THREAD) {
278
        switch (THREAD->state) {
278
        switch (THREAD->state) {
279
            case Running:
279
            case Running:
280
                THREAD->state = Ready;
280
                THREAD->state = Ready;
281
                spinlock_unlock(&THREAD->lock);
281
                spinlock_unlock(&THREAD->lock);
282
                thread_ready(THREAD);
282
                thread_ready(THREAD);
283
                break;
283
                break;
284
 
284
 
285
            case Exiting:
285
            case Exiting:
286
                frame_free((__address) THREAD->kstack);
286
                frame_free((__address) THREAD->kstack);
287
                if (THREAD->ustack) {
287
                if (THREAD->ustack) {
288
                    frame_free((__address) THREAD->ustack);
288
                    frame_free((__address) THREAD->ustack);
289
                }
289
                }
290
               
290
               
291
                /*
291
                /*
292
                 * Detach from the containing task.
292
                 * Detach from the containing task.
293
                 */
293
                 */
294
                spinlock_lock(&TASK->lock);
294
                spinlock_lock(&TASK->lock);
295
                list_remove(&THREAD->th_link);
295
                list_remove(&THREAD->th_link);
296
                spinlock_unlock(&TASK->lock);
296
                spinlock_unlock(&TASK->lock);
297
 
297
 
298
                spinlock_unlock(&THREAD->lock);
298
                spinlock_unlock(&THREAD->lock);
299
               
299
               
300
                spinlock_lock(&threads_lock);
300
                spinlock_lock(&threads_lock);
301
                list_remove(&THREAD->threads_link);
301
                list_remove(&THREAD->threads_link);
302
                spinlock_unlock(&threads_lock);
302
                spinlock_unlock(&threads_lock);
303
 
303
 
304
                spinlock_lock(&CPU->lock);
304
                spinlock_lock(&CPU->lock);
305
                if(CPU->fpu_owner==THREAD) CPU->fpu_owner=NULL;
305
                if(CPU->fpu_owner==THREAD) CPU->fpu_owner=NULL;
306
                spinlock_unlock(&CPU->lock);
306
                spinlock_unlock(&CPU->lock);
307
 
307
 
308
               
308
               
309
                free(THREAD);
309
                free(THREAD);
310
               
310
               
311
                break;
311
                break;
312
               
312
               
313
            case Sleeping:
313
            case Sleeping:
314
                /*
314
                /*
315
                 * Prefer the thread after it's woken up.
315
                 * Prefer the thread after it's woken up.
316
                 */
316
                 */
317
                THREAD->pri = -1;
317
                THREAD->pri = -1;
318
 
318
 
319
                /*
319
                /*
320
                 * We need to release wq->lock which we locked in waitq_sleep().
320
                 * We need to release wq->lock which we locked in waitq_sleep().
321
                 * Address of wq->lock is kept in THREAD->sleep_queue.
321
                 * Address of wq->lock is kept in THREAD->sleep_queue.
322
                 */
322
                 */
323
                spinlock_unlock(&THREAD->sleep_queue->lock);
323
                spinlock_unlock(&THREAD->sleep_queue->lock);
324
 
324
 
325
                /*
325
                /*
326
                 * Check for possible requests for out-of-context invocation.
326
                 * Check for possible requests for out-of-context invocation.
327
                 */
327
                 */
328
                if (THREAD->call_me) {
328
                if (THREAD->call_me) {
329
                    THREAD->call_me(THREAD->call_me_with);
329
                    THREAD->call_me(THREAD->call_me_with);
330
                    THREAD->call_me = NULL;
330
                    THREAD->call_me = NULL;
331
                    THREAD->call_me_with = NULL;
331
                    THREAD->call_me_with = NULL;
332
                }
332
                }
333
 
333
 
334
                spinlock_unlock(&THREAD->lock);
334
                spinlock_unlock(&THREAD->lock);
335
               
335
               
336
                break;
336
                break;
337
 
337
 
338
            default:
338
            default:
339
                /*
339
                /*
340
                 * Entering state is unexpected.
340
                 * Entering state is unexpected.
341
                 */
341
                 */
342
                panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]);
342
                panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]);
343
                break;
343
                break;
344
        }
344
        }
345
        THREAD = NULL;
345
        THREAD = NULL;
346
    }
346
    }
347
   
347
   
348
    THREAD = find_best_thread();
348
    THREAD = find_best_thread();
349
   
349
   
350
    spinlock_lock(&THREAD->lock);
350
    spinlock_lock(&THREAD->lock);
351
    priority = THREAD->pri;
351
    priority = THREAD->pri;
352
    spinlock_unlock(&THREAD->lock);
352
    spinlock_unlock(&THREAD->lock);
353
   
353
   
354
    relink_rq(priority);       
354
    relink_rq(priority);       
355
 
355
 
356
    spinlock_lock(&THREAD->lock);  
356
    spinlock_lock(&THREAD->lock);  
357
 
357
 
358
    /*
358
    /*
359
     * If both the old and the new task are the same, lots of work is avoided.
359
     * If both the old and the new task are the same, lots of work is avoided.
360
     */
360
     */
361
    if (TASK != THREAD->task) {
361
    if (TASK != THREAD->task) {
362
        vm_t *m1 = NULL;
362
        vm_t *m1 = NULL;
363
        vm_t *m2;
363
        vm_t *m2;
364
 
364
 
365
        if (TASK) {
365
        if (TASK) {
366
            spinlock_lock(&TASK->lock);
366
            spinlock_lock(&TASK->lock);
367
            m1 = TASK->vm;
367
            m1 = TASK->vm;
368
            spinlock_unlock(&TASK->lock);
368
            spinlock_unlock(&TASK->lock);
369
        }
369
        }
370
 
370
 
371
        spinlock_lock(&THREAD->task->lock);
371
        spinlock_lock(&THREAD->task->lock);
372
        m2 = THREAD->task->vm;
372
        m2 = THREAD->task->vm;
373
        spinlock_unlock(&THREAD->task->lock);
373
        spinlock_unlock(&THREAD->task->lock);
374
       
374
       
375
        /*
375
        /*
376
         * Note that it is possible for two tasks to share one vm mapping.
376
         * Note that it is possible for two tasks to share one vm mapping.
377
         */
377
         */
378
        if (m1 != m2) {
378
        if (m1 != m2) {
379
            /*
379
            /*
380
             * Both tasks and vm mappings are different.
380
             * Both tasks and vm mappings are different.
381
             * Replace the old one with the new one.
381
             * Replace the old one with the new one.
382
             */
382
             */
383
            if (m1) {
383
            if (m1) {
384
                vm_uninstall(m1);
384
                vm_uninstall(m1);
385
            }
385
            }
386
            vm_install(m2);
386
            vm_install(m2);
387
        }
387
        }
388
        TASK = THREAD->task;   
388
        TASK = THREAD->task;   
389
    }
389
    }
390
 
390
 
391
    THREAD->state = Running;
391
    THREAD->state = Running;
392
 
392
 
393
    #ifdef SCHEDULER_VERBOSE
393
    #ifdef SCHEDULER_VERBOSE
394
    printf("cpu%d: tid %d (pri=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->pri, THREAD->ticks, CPU->nrdy);
394
    printf("cpu%d: tid %d (pri=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->pri, THREAD->ticks, CPU->nrdy);
395
    #endif  
395
    #endif  
396
 
396
 
397
    context_restore(&THREAD->saved_context);
397
    context_restore(&THREAD->saved_context);
398
    /* not reached */
398
    /* not reached */
399
}
399
}
400
 
400
 
401
 
401
 
402
#ifdef __SMP__
402
#ifdef __SMP__
403
/** Load balancing thread
403
/** Load balancing thread
404
 *
404
 *
405
 * SMP load balancing thread, supervising thread supplies
405
 * SMP load balancing thread, supervising thread supplies
406
 * for the CPU it's wired to.
406
 * for the CPU it's wired to.
407
 *
407
 *
408
 * @param arg Generic thread argument (unused).
408
 * @param arg Generic thread argument (unused).
409
 *
409
 *
410
 */
410
 */
411
void kcpulb(void *arg)
411
void kcpulb(void *arg)
412
{
412
{
413
    thread_t *t;
413
    thread_t *t;
414
    int count, i, j, k = 0;
414
    int count, i, j, k = 0;
415
    pri_t pri;
415
    pri_t pri;
416
 
416
 
417
loop:
417
loop:
418
    /*
418
    /*
419
     * Sleep until there's some work to do.
419
     * Sleep until there's some work to do.
420
     */
420
     */
421
    waitq_sleep(&CPU->kcpulb_wq);
421
    waitq_sleep(&CPU->kcpulb_wq);
422
 
422
 
423
not_satisfied:
423
not_satisfied:
424
    /*
424
    /*
425
     * Calculate the number of threads that will be migrated/stolen from
425
     * Calculate the number of threads that will be migrated/stolen from
426
     * other CPU's. Note that situation can have changed between two
426
     * other CPU's. Note that situation can have changed between two
427
     * passes. Each time get the most up to date counts.
427
     * passes. Each time get the most up to date counts.
428
     */
428
     */
429
    pri = cpu_priority_high();
429
    pri = cpu_priority_high();
430
    spinlock_lock(&CPU->lock);
430
    spinlock_lock(&CPU->lock);
431
    count = nrdy / config.cpu_active;
431
    count = nrdy / config.cpu_active;
432
    count -= CPU->nrdy;
432
    count -= CPU->nrdy;
433
    spinlock_unlock(&CPU->lock);
433
    spinlock_unlock(&CPU->lock);
434
    cpu_priority_restore(pri);
434
    cpu_priority_restore(pri);
435
 
435
 
436
    if (count <= 0)
436
    if (count <= 0)
437
        goto satisfied;
437
        goto satisfied;
438
 
438
 
439
    /*
439
    /*
440
     * Searching least priority queues on all CPU's first and most priority queues on all CPU's last.
440
     * Searching least priority queues on all CPU's first and most priority queues on all CPU's last.
441
     */
441
     */
442
    for (j=RQ_COUNT-1; j >= 0; j--) {
442
    for (j=RQ_COUNT-1; j >= 0; j--) {
443
        for (i=0; i < config.cpu_active; i++) {
443
        for (i=0; i < config.cpu_active; i++) {
444
            link_t *l;
444
            link_t *l;
445
            runq_t *r;
445
            runq_t *r;
446
            cpu_t *cpu;
446
            cpu_t *cpu;
447
 
447
 
448
            cpu = &cpus[(i + k) % config.cpu_active];
448
            cpu = &cpus[(i + k) % config.cpu_active];
449
            r = &cpu->rq[j];
-
 
450
 
449
 
451
            /*
450
            /*
452
             * Not interested in ourselves.
451
             * Not interested in ourselves.
453
             * Doesn't require interrupt disabling for kcpulb is X_WIRED.
452
             * Doesn't require interrupt disabling for kcpulb is X_WIRED.
454
             */
453
             */
455
            if (CPU == cpu)
454
            if (CPU == cpu)
456
                continue;
455
                continue;              
457
 
456
 
458
restart:        pri = cpu_priority_high();
457
restart:        pri = cpu_priority_high();
-
 
458
            r = &cpu->rq[j];
459
            spinlock_lock(&r->lock);
459
            spinlock_lock(&r->lock);
460
            if (r->n == 0) {
460
            if (r->n == 0) {
461
                spinlock_unlock(&r->lock);
461
                spinlock_unlock(&r->lock);
462
                cpu_priority_restore(pri);
462
                cpu_priority_restore(pri);
463
                continue;
463
                continue;
464
            }
464
            }
465
       
465
       
466
            t = NULL;
466
            t = NULL;
467
            l = r->rq_head.prev;    /* search rq from the back */
467
            l = r->rq_head.prev;    /* search rq from the back */
468
            while (l != &r->rq_head) {
468
            while (l != &r->rq_head) {
469
                t = list_get_instance(l, thread_t, rq_link);
469
                t = list_get_instance(l, thread_t, rq_link);
470
                /*
470
                /*
471
                     * We don't want to steal CPU-wired threads neither threads already stolen.
471
                     * We don't want to steal CPU-wired threads neither threads already stolen.
472
                 * The latter prevents threads from migrating between CPU's without ever being run.
472
                 * The latter prevents threads from migrating between CPU's without ever being run.
473
                     * We don't want to steal threads whose FPU context is still in CPU
473
                     * We don't want to steal threads whose FPU context is still in CPU.
474
                 */
474
                 */
475
                spinlock_lock(&t->lock);
475
                spinlock_lock(&t->lock);
476
                if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) {
476
                if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) {
-
 
477
               
477
                    /*
478
                    /*
478
                     * Remove t from r.
479
                     * Remove t from r.
479
                     */
480
                     */
480
 
481
 
481
                    spinlock_unlock(&t->lock);
482
                    spinlock_unlock(&t->lock);
482
                   
483
                   
483
                    /*
484
                    /*
484
                     * Here we have to avoid deadlock with relink_rq(),
485
                     * Here we have to avoid deadlock with relink_rq(),
485
                     * because it locks cpu and r in a different order than we do.
486
                     * because it locks cpu and r in a different order than we do.
486
                     */
487
                     */
487
                    if (!spinlock_trylock(&cpu->lock)) {
488
                    if (!spinlock_trylock(&cpu->lock)) {
488
                        /* Release all locks and try again. */
489
                        /* Release all locks and try again. */
489
                        spinlock_unlock(&r->lock);
490
                        spinlock_unlock(&r->lock);
490
                        cpu_priority_restore(pri);
491
                        cpu_priority_restore(pri);
491
                        goto restart;
492
                        goto restart;
492
                    }
493
                    }
493
                    cpu->nrdy--;
494
                    cpu->nrdy--;
494
                    spinlock_unlock(&cpu->lock);
495
                    spinlock_unlock(&cpu->lock);
495
 
496
 
496
                    atomic_dec(&nrdy);
497
                    atomic_dec(&nrdy);
497
 
498
 
498
                        r->n--;
499
                        r->n--;
499
                    list_remove(&t->rq_link);
500
                    list_remove(&t->rq_link);
500
 
501
 
501
                    break;
502
                    break;
502
                }
503
                }
503
                spinlock_unlock(&t->lock);
504
                spinlock_unlock(&t->lock);
504
                l = l->prev;
505
                l = l->prev;
505
                t = NULL;
506
                t = NULL;
506
            }
507
            }
507
            spinlock_unlock(&r->lock);
508
            spinlock_unlock(&r->lock);
508
 
509
 
509
            if (t) {
510
            if (t) {
510
                /*
511
                /*
511
                 * Ready t on local CPU
512
                 * Ready t on local CPU
512
                 */
513
                 */
513
                spinlock_lock(&t->lock);
514
                spinlock_lock(&t->lock);
514
                #ifdef KCPULB_VERBOSE
515
                #ifdef KCPULB_VERBOSE
515
                printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, CPU->nrdy, nrdy / config.cpu_active);
516
                printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, CPU->nrdy, nrdy / config.cpu_active);
516
                #endif
517
                #endif
517
                t->flags |= X_STOLEN;
518
                t->flags |= X_STOLEN;
518
                spinlock_unlock(&t->lock);
519
                spinlock_unlock(&t->lock);
519
   
520
   
520
                thread_ready(t);
521
                thread_ready(t);
521
 
522
 
522
                cpu_priority_restore(pri);
523
                cpu_priority_restore(pri);
523
   
524
   
524
                if (--count == 0)
525
                if (--count == 0)
525
                    goto satisfied;
526
                    goto satisfied;
526
                   
527
                   
527
                /*
528
                /*
528
                             * We are not satisfied yet, focus on another CPU next time.
529
                             * We are not satisfied yet, focus on another CPU next time.
529
                 */
530
                 */
530
                k++;
531
                k++;
531
               
532
               
532
                continue;
533
                continue;
533
            }
534
            }
534
            cpu_priority_restore(pri);
535
            cpu_priority_restore(pri);
535
        }
536
        }
536
    }
537
    }
537
 
538
 
538
    if (CPU->nrdy) {
539
    if (CPU->nrdy) {
539
        /*
540
        /*
540
         * Be a little bit light-weight and let migrated threads run.
541
         * Be a little bit light-weight and let migrated threads run.
541
         */
542
         */
542
        scheduler();
543
        scheduler();
543
    }
544
    }
544
    else {
545
    else {
545
        /*
546
        /*
546
         * We failed to migrate a single thread.
547
         * We failed to migrate a single thread.
547
         * Something more sophisticated should be done.
548
         * Something more sophisticated should be done.
548
         */
549
         */
549
        scheduler();
550
        scheduler();
550
    }
551
    }
551
       
552
       
552
    goto not_satisfied;
553
    goto not_satisfied;
553
   
554
   
554
satisfied:
555
satisfied:
555
    /*
556
    /*
556
     * Tell find_best_thread() to wake us up later again.
557
     * Tell find_best_thread() to wake us up later again.
557
     */
558
     */
558
    CPU->kcpulbstarted = 0;
559
    CPU->kcpulbstarted = 0;
559
    goto loop;
560
    goto loop;
560
}
561
}
561
 
562
 
562
#endif /* __SMP__ */
563
#endif /* __SMP__ */
563
 
564