Subversion Repositories HelenOS-historic

Rev

Rev 1138 | Rev 1171 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include <proc/scheduler.h>
30
#include <proc/thread.h>
31
#include <proc/task.h>
1078 jermar 32
#include <proc/uarg.h>
1 jermar 33
#include <mm/frame.h>
34
#include <mm/page.h>
35
#include <arch/asm.h>
36
#include <arch.h>
37
#include <synch/synch.h>
38
#include <synch/spinlock.h>
39
#include <synch/waitq.h>
40
#include <synch/rwlock.h>
41
#include <cpu.h>
42
#include <func.h>
43
#include <context.h>
1158 jermar 44
#include <adt/btree.h>
788 jermar 45
#include <adt/list.h>
1 jermar 46
#include <typedefs.h>
47
#include <time/clock.h>
788 jermar 48
#include <adt/list.h>
7 jermar 49
#include <config.h>
50
#include <arch/interrupt.h>
10 jermar 51
#include <smp/ipi.h>
76 jermar 52
#include <arch/faddr.h>
1104 jermar 53
#include <atomic.h>
195 vana 54
#include <memstr.h>
777 palkovsky 55
#include <print.h>
787 palkovsky 56
#include <mm/slab.h>
57
#include <debug.h>
1066 jermar 58
#include <main/uinit.h>
7 jermar 59
 
107 decky 60
char *thread_states[] = {"Invalid", "Running", "Sleeping", "Ready", "Entering", "Exiting"}; /**< Thread states */
1 jermar 61
 
1158 jermar 62
/** Lock protecting threads_head list. For locking rules, see declaration thereof. */
63
SPINLOCK_INITIALIZE(threads_lock);
64
btree_t threads_btree;          /**< B+tree of all threads. */
1 jermar 65
 
623 jermar 66
SPINLOCK_INITIALIZE(tidlock);
1 jermar 67
__u32 last_tid = 0;
68
 
787 palkovsky 69
static slab_cache_t *thread_slab;
906 palkovsky 70
#ifdef ARCH_HAS_FPU
71
slab_cache_t *fpu_context_slab;
72
#endif
107 decky 73
 
74
/** Thread wrapper
75
 *
76
 * This wrapper is provided to ensure that every thread
1 jermar 77
 * makes a call to thread_exit() when its implementing
78
 * function returns.
79
 *
413 jermar 80
 * interrupts_disable() is assumed.
107 decky 81
 *
1 jermar 82
 */
452 decky 83
static void cushion(void)
1 jermar 84
{
15 jermar 85
    void (*f)(void *) = THREAD->thread_code;
86
    void *arg = THREAD->thread_arg;
1 jermar 87
 
213 jermar 88
    /* this is where each thread wakes up after its creation */
15 jermar 89
    spinlock_unlock(&THREAD->lock);
413 jermar 90
    interrupts_enable();
1 jermar 91
 
92
    f(arg);
93
    thread_exit();
94
    /* not reached */
95
}
96
 
787 palkovsky 97
/** Initialization and allocation for thread_t structure */
98
static int thr_constructor(void *obj, int kmflags)
99
{
100
    thread_t *t = (thread_t *)obj;
814 palkovsky 101
    pfn_t pfn;
842 palkovsky 102
    int status;
107 decky 103
 
787 palkovsky 104
    spinlock_initialize(&t->lock, "thread_t_lock");
105
    link_initialize(&t->rq_link);
106
    link_initialize(&t->wq_link);
107
    link_initialize(&t->th_link);
108
 
906 palkovsky 109
#ifdef ARCH_HAS_FPU
110
#  ifdef CONFIG_FPU_LAZY
111
    t->saved_fpu_context = NULL;
112
#  else
113
    t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags);
114
    if (!t->saved_fpu_context)
115
        return -1;
116
#  endif
117
#endif  
118
 
935 vana 119
    pfn = frame_alloc_rc(STACK_FRAMES, FRAME_KA | kmflags,&status);
906 palkovsky 120
    if (status) {
121
#ifdef ARCH_HAS_FPU
122
        if (t->saved_fpu_context)
123
            slab_free(fpu_context_slab,t->saved_fpu_context);
124
#endif
842 palkovsky 125
        return -1;
906 palkovsky 126
    }
814 palkovsky 127
    t->kstack = (__u8 *)PA2KA(PFN2ADDR(pfn));
787 palkovsky 128
 
129
    return 0;
130
}
131
 
132
/** Destruction of thread_t object */
133
static int thr_destructor(void *obj)
134
{
135
    thread_t *t = (thread_t *)obj;
136
 
814 palkovsky 137
    frame_free(ADDR2PFN(KA2PA(t->kstack)));
906 palkovsky 138
#ifdef ARCH_HAS_FPU
139
    if (t->saved_fpu_context)
140
        slab_free(fpu_context_slab,t->saved_fpu_context);
141
#endif
787 palkovsky 142
    return 1; /* One page freed */
143
}
144
 
107 decky 145
/** Initialize threads
146
 *
147
 * Initialize kernel threads support.
148
 *
149
 */
1 jermar 150
void thread_init(void)
151
{
15 jermar 152
    THREAD = NULL;
625 palkovsky 153
    atomic_set(&nrdy,0);
787 palkovsky 154
    thread_slab = slab_cache_create("thread_slab",
155
                    sizeof(thread_t),0,
156
                    thr_constructor, thr_destructor, 0);
906 palkovsky 157
#ifdef ARCH_HAS_FPU
158
    fpu_context_slab = slab_cache_create("fpu_slab",
159
                         sizeof(fpu_context_t),
160
                         FPU_CONTEXT_ALIGN,
161
                         NULL, NULL, 0);
162
#endif
1158 jermar 163
 
164
    btree_create(&threads_btree);
1 jermar 165
}
166
 
107 decky 167
/** Make thread ready
168
 *
169
 * Switch thread t to the ready state.
170
 *
171
 * @param t Thread to make ready.
172
 *
173
 */
1 jermar 174
void thread_ready(thread_t *t)
175
{
176
    cpu_t *cpu;
177
    runq_t *r;
413 jermar 178
    ipl_t ipl;
625 palkovsky 179
    int i, avg;
1 jermar 180
 
413 jermar 181
    ipl = interrupts_disable();
1 jermar 182
 
183
    spinlock_lock(&t->lock);
184
 
1086 palkovsky 185
    ASSERT(! (t->state == Ready));
186
 
413 jermar 187
    i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority;
1 jermar 188
 
16 jermar 189
    cpu = CPU;
1 jermar 190
    if (t->flags & X_WIRED) {
191
        cpu = t->cpu;
192
    }
1083 palkovsky 193
    t->state = Ready;
1 jermar 194
    spinlock_unlock(&t->lock);
195
 
107 decky 196
    /*
1 jermar 197
     * Append t to respective ready queue on respective processor.
198
     */
199
    r = &cpu->rq[i];
200
    spinlock_lock(&r->lock);
201
    list_append(&t->rq_link, &r->rq_head);
202
    r->n++;
203
    spinlock_unlock(&r->lock);
204
 
475 jermar 205
    atomic_inc(&nrdy);
625 palkovsky 206
    avg = atomic_get(&nrdy) / config.cpu_active;
783 palkovsky 207
    atomic_inc(&cpu->nrdy);
1 jermar 208
 
413 jermar 209
    interrupts_restore(ipl);
1 jermar 210
}
211
 
787 palkovsky 212
/** Destroy thread memory structure
213
 *
214
 * Detach thread from all queues, cpus etc. and destroy it.
215
 *
216
 * Assume thread->lock is held!!
217
 */
218
void thread_destroy(thread_t *t)
219
{
220
    ASSERT(t->state == Exiting);
221
    ASSERT(t->task);
222
    ASSERT(t->cpu);
223
 
224
    spinlock_lock(&t->cpu->lock);
225
    if(t->cpu->fpu_owner==t)
226
        t->cpu->fpu_owner=NULL;
227
    spinlock_unlock(&t->cpu->lock);
228
 
229
    /*
230
     * Detach from the containing task.
231
     */
232
    spinlock_lock(&t->task->lock);
233
    list_remove(&t->th_link);
234
    spinlock_unlock(&t->task->lock);
235
 
236
    spinlock_unlock(&t->lock);
237
 
238
    spinlock_lock(&threads_lock);
1158 jermar 239
    btree_remove(&threads_btree, (__native) t, NULL);
787 palkovsky 240
    spinlock_unlock(&threads_lock);
241
 
242
    slab_free(thread_slab, t);
243
}
244
 
107 decky 245
/** Create new thread
246
 *
247
 * Create a new thread.
248
 *
249
 * @param func  Thread's implementing function.
250
 * @param arg   Thread's implementing function argument.
251
 * @param task  Task to which the thread belongs.
252
 * @param flags Thread flags.
1062 jermar 253
 * @param name  Symbolic name.
107 decky 254
 *
255
 * @return New thread's structure on success, NULL on failure.
256
 *
257
 */
1062 jermar 258
thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name)
1 jermar 259
{
260
    thread_t *t;
822 palkovsky 261
    ipl_t ipl;
262
 
787 palkovsky 263
    t = (thread_t *) slab_alloc(thread_slab, 0);
842 palkovsky 264
    if (!t)
265
        return NULL;
1 jermar 266
 
822 palkovsky 267
    /* Not needed, but good for debugging */
1138 jermar 268
    memsetb((__address)t->kstack, THREAD_STACK_SIZE * 1<<STACK_FRAMES, 0);
822 palkovsky 269
 
270
    ipl = interrupts_disable();
271
    spinlock_lock(&tidlock);
272
    t->tid = ++last_tid;
273
    spinlock_unlock(&tidlock);
274
    interrupts_restore(ipl);
275
 
276
    context_save(&t->saved_context);
277
    context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE);
278
 
279
    the_initialize((the_t *) t->kstack);
280
 
281
    ipl = interrupts_disable();
282
    t->saved_context.ipl = interrupts_read();
283
    interrupts_restore(ipl);
284
 
1066 jermar 285
    memcpy(t->name, name, THREAD_NAME_BUFLEN);
286
 
822 palkovsky 287
    t->thread_code = func;
288
    t->thread_arg = arg;
289
    t->ticks = -1;
290
    t->priority = -1;       /* start in rq[0] */
291
    t->cpu = NULL;
292
    t->flags = 0;
293
    t->state = Entering;
294
    t->call_me = NULL;
295
    t->call_me_with = NULL;
296
 
297
    timeout_initialize(&t->sleep_timeout);
298
    t->sleep_queue = NULL;
299
    t->timeout_pending = 0;
300
 
301
    t->rwlock_holder_type = RWLOCK_NONE;
210 decky 302
 
822 palkovsky 303
    t->task = task;
304
 
860 decky 305
    t->fpu_context_exists = 0;
306
    t->fpu_context_engaged = 0;
822 palkovsky 307
 
308
    /*
309
     * Register this thread in the system-wide list.
310
     */
311
    ipl = interrupts_disable();
312
    spinlock_lock(&threads_lock);
1158 jermar 313
    btree_insert(&threads_btree, (__native) t, (void *) t, NULL);
822 palkovsky 314
    spinlock_unlock(&threads_lock);
315
 
316
    /*
317
     * Attach to the containing task.
318
     */
319
    spinlock_lock(&task->lock);
320
    list_append(&t->th_link, &task->th_head);
321
    spinlock_unlock(&task->lock);
322
 
323
    interrupts_restore(ipl);
860 decky 324
 
1 jermar 325
    return t;
326
}
327
 
107 decky 328
/** Make thread exiting
329
 *
330
 * End current thread execution and switch it to the exiting
331
 * state. All pending timeouts are executed.
332
 *
333
 */
1 jermar 334
void thread_exit(void)
335
{
413 jermar 336
    ipl_t ipl;
1 jermar 337
 
338
restart:
413 jermar 339
    ipl = interrupts_disable();
15 jermar 340
    spinlock_lock(&THREAD->lock);
341
    if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */
342
        spinlock_unlock(&THREAD->lock);
413 jermar 343
        interrupts_restore(ipl);
1 jermar 344
        goto restart;
345
    }
15 jermar 346
    THREAD->state = Exiting;
347
    spinlock_unlock(&THREAD->lock);
1 jermar 348
    scheduler();
349
}
350
 
107 decky 351
 
352
/** Thread sleep
353
 *
354
 * Suspend execution of the current thread.
355
 *
356
 * @param sec Number of seconds to sleep.
357
 *
358
 */
1 jermar 359
void thread_sleep(__u32 sec)
360
{
125 jermar 361
    thread_usleep(sec*1000000);
1 jermar 362
}
107 decky 363
 
364
/** Thread usleep
365
 *
366
 * Suspend execution of the current thread.
367
 *
368
 * @param usec Number of microseconds to sleep.
369
 *
370
 */
1 jermar 371
void thread_usleep(__u32 usec)
372
{
373
    waitq_t wq;
374
 
375
    waitq_initialize(&wq);
376
 
377
    (void) waitq_sleep_timeout(&wq, usec, SYNCH_NON_BLOCKING);
378
}
379
 
107 decky 380
/** Register thread out-of-context invocation
381
 *
382
 * Register a function and its argument to be executed
383
 * on next context switch to the current thread.
384
 *
385
 * @param call_me      Out-of-context function.
386
 * @param call_me_with Out-of-context function argument.
387
 *
388
 */
1 jermar 389
void thread_register_call_me(void (* call_me)(void *), void *call_me_with)
390
{
413 jermar 391
    ipl_t ipl;
1 jermar 392
 
413 jermar 393
    ipl = interrupts_disable();
15 jermar 394
    spinlock_lock(&THREAD->lock);
395
    THREAD->call_me = call_me;
396
    THREAD->call_me_with = call_me_with;
397
    spinlock_unlock(&THREAD->lock);
413 jermar 398
    interrupts_restore(ipl);
1 jermar 399
}
777 palkovsky 400
 
401
/** Print list of threads debug info */
402
void thread_print_list(void)
403
{
404
    link_t *cur;
405
    ipl_t ipl;
406
 
407
    /* Messing with thread structures, avoid deadlock */
408
    ipl = interrupts_disable();
409
    spinlock_lock(&threads_lock);
410
 
1158 jermar 411
    for (cur = threads_btree.leaf_head.next; cur != &threads_btree.leaf_head; cur = cur->next) {
412
        btree_node_t *node;
413
        int i;
414
 
415
        node = list_get_instance(cur, btree_node_t, leaf_link);
416
        for (i = 0; i < node->keys; i++) {
417
            thread_t *t;
418
 
419
            t = (thread_t *) node->value[i];
420
            printf("%s: address=%P, tid=%d, state=%s, task=%P, code=%P, stack=%P, cpu=",
421
                t->name, t, t->tid, thread_states[t->state], t->task, t->thread_code, t->kstack);
422
            if (t->cpu)
423
                printf("cpu%d ", t->cpu->id);
424
            else
425
                printf("none");
426
            printf("\n");
427
        }
777 palkovsky 428
    }
429
 
430
    spinlock_unlock(&threads_lock);
1060 palkovsky 431
    interrupts_restore(ipl);
777 palkovsky 432
}
1066 jermar 433
 
1158 jermar 434
/** Check whether thread exists.
435
 *
436
 * Note that threads_lock must be already held and
437
 * interrupts must be already disabled.
438
 *
439
 * @param t Pointer to thread.
440
 *
441
 * @return True if thread t is known to the system, false otherwise.
442
 */
443
bool thread_exists(thread_t *t)
444
{
445
    btree_node_t *leaf;
446
 
447
    return btree_search(&threads_btree, (__native) t, &leaf) != NULL;
448
}
449
 
1066 jermar 450
/** Process syscall to create new thread.
451
 *
452
 */
1078 jermar 453
__native sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name)
1066 jermar 454
{
455
        thread_t *t;
456
        char namebuf[THREAD_NAME_BUFLEN];
1103 jermar 457
    uspace_arg_t *kernel_uarg;
1066 jermar 458
    __u32 tid;
459
 
1078 jermar 460
        copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN);
1066 jermar 461
 
1078 jermar 462
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
463
    copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
464
 
465
        if ((t = thread_create(uinit, kernel_uarg, TASK, 0, namebuf))) {
1066 jermar 466
        tid = t->tid;
467
                thread_ready(t);
468
        return (__native) tid;
469
        } else {
1078 jermar 470
        free(kernel_uarg);
1066 jermar 471
        }
472
 
473
        return (__native) -1;
474
}
475
 
476
/** Process syscall to terminate thread.
477
 *
478
 */
1078 jermar 479
__native sys_thread_exit(int uspace_status)
1066 jermar 480
{
481
        thread_exit();
482
        /* Unreachable */
483
        return 0;
484
}