Subversion Repositories HelenOS

Rev

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