Subversion Repositories HelenOS-historic

Rev

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

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