Subversion Repositories HelenOS

Rev

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