Subversion Repositories HelenOS

Rev

Rev 1083 | Rev 1103 | 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>
788 jermar 44
#include <adt/list.h>
1 jermar 45
#include <typedefs.h>
46
#include <time/clock.h>
788 jermar 47
#include <adt/list.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>
111 palkovsky 52
#include <arch/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
 
623 jermar 61
SPINLOCK_INITIALIZE(threads_lock);  /**< Lock protecting threads_head list. For locking rules, see declaration thereof. */
624 jermar 62
LIST_INITIALIZE(threads_head);      /**< List of all threads. */
1 jermar 63
 
623 jermar 64
SPINLOCK_INITIALIZE(tidlock);
1 jermar 65
__u32 last_tid = 0;
66
 
787 palkovsky 67
static slab_cache_t *thread_slab;
906 palkovsky 68
#ifdef ARCH_HAS_FPU
69
slab_cache_t *fpu_context_slab;
70
#endif
107 decky 71
 
787 palkovsky 72
 
107 decky 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 */
22 jermar 88
    before_thread_runs();
89
 
15 jermar 90
    spinlock_unlock(&THREAD->lock);
413 jermar 91
    interrupts_enable();
1 jermar 92
 
93
    f(arg);
94
    thread_exit();
95
    /* not reached */
96
}
97
 
787 palkovsky 98
/** Initialization and allocation for thread_t structure */
99
static int thr_constructor(void *obj, int kmflags)
100
{
101
    thread_t *t = (thread_t *)obj;
814 palkovsky 102
    pfn_t pfn;
842 palkovsky 103
    int status;
107 decky 104
 
787 palkovsky 105
    spinlock_initialize(&t->lock, "thread_t_lock");
106
    link_initialize(&t->rq_link);
107
    link_initialize(&t->wq_link);
108
    link_initialize(&t->th_link);
109
    link_initialize(&t->threads_link);
110
 
906 palkovsky 111
#ifdef ARCH_HAS_FPU
112
#  ifdef CONFIG_FPU_LAZY
113
    t->saved_fpu_context = NULL;
114
#  else
115
    t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags);
116
    if (!t->saved_fpu_context)
117
        return -1;
118
#  endif
119
#endif  
120
 
935 vana 121
    pfn = frame_alloc_rc(STACK_FRAMES, FRAME_KA | kmflags,&status);
906 palkovsky 122
    if (status) {
123
#ifdef ARCH_HAS_FPU
124
        if (t->saved_fpu_context)
125
            slab_free(fpu_context_slab,t->saved_fpu_context);
126
#endif
842 palkovsky 127
        return -1;
906 palkovsky 128
    }
814 palkovsky 129
    t->kstack = (__u8 *)PA2KA(PFN2ADDR(pfn));
787 palkovsky 130
 
131
    return 0;
132
}
133
 
134
/** Destruction of thread_t object */
135
static int thr_destructor(void *obj)
136
{
137
    thread_t *t = (thread_t *)obj;
138
 
814 palkovsky 139
    frame_free(ADDR2PFN(KA2PA(t->kstack)));
906 palkovsky 140
#ifdef ARCH_HAS_FPU
141
    if (t->saved_fpu_context)
142
        slab_free(fpu_context_slab,t->saved_fpu_context);
143
#endif
787 palkovsky 144
    return 1; /* One page freed */
145
}
146
 
107 decky 147
/** Initialize threads
148
 *
149
 * Initialize kernel threads support.
150
 *
151
 */
1 jermar 152
void thread_init(void)
153
{
15 jermar 154
    THREAD = NULL;
625 palkovsky 155
    atomic_set(&nrdy,0);
787 palkovsky 156
    thread_slab = slab_cache_create("thread_slab",
157
                    sizeof(thread_t),0,
158
                    thr_constructor, thr_destructor, 0);
906 palkovsky 159
#ifdef ARCH_HAS_FPU
160
    fpu_context_slab = slab_cache_create("fpu_slab",
161
                         sizeof(fpu_context_t),
162
                         FPU_CONTEXT_ALIGN,
163
                         NULL, NULL, 0);
164
#endif
1 jermar 165
}
166
 
107 decky 167
 
168
/** Make thread ready
169
 *
170
 * Switch thread t to the ready state.
171
 *
172
 * @param t Thread to make ready.
173
 *
174
 */
1 jermar 175
void thread_ready(thread_t *t)
176
{
177
    cpu_t *cpu;
178
    runq_t *r;
413 jermar 179
    ipl_t ipl;
625 palkovsky 180
    int i, avg;
1 jermar 181
 
413 jermar 182
    ipl = interrupts_disable();
1 jermar 183
 
184
    spinlock_lock(&t->lock);
185
 
1086 palkovsky 186
    ASSERT(! (t->state == Ready));
187
 
413 jermar 188
    i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority;
1 jermar 189
 
16 jermar 190
    cpu = CPU;
1 jermar 191
    if (t->flags & X_WIRED) {
192
        cpu = t->cpu;
193
    }
1083 palkovsky 194
    t->state = Ready;
1 jermar 195
    spinlock_unlock(&t->lock);
196
 
107 decky 197
    /*
1 jermar 198
     * Append t to respective ready queue on respective processor.
199
     */
200
    r = &cpu->rq[i];
201
    spinlock_lock(&r->lock);
202
    list_append(&t->rq_link, &r->rq_head);
203
    r->n++;
204
    spinlock_unlock(&r->lock);
205
 
475 jermar 206
    atomic_inc(&nrdy);
625 palkovsky 207
    avg = atomic_get(&nrdy) / config.cpu_active;
783 palkovsky 208
    atomic_inc(&cpu->nrdy);
1 jermar 209
 
413 jermar 210
    interrupts_restore(ipl);
1 jermar 211
}
212
 
107 decky 213
 
787 palkovsky 214
/** Destroy thread memory structure
215
 *
216
 * Detach thread from all queues, cpus etc. and destroy it.
217
 *
218
 * Assume thread->lock is held!!
219
 */
220
void thread_destroy(thread_t *t)
221
{
222
    ASSERT(t->state == Exiting);
223
    ASSERT(t->task);
224
    ASSERT(t->cpu);
225
 
226
    spinlock_lock(&t->cpu->lock);
227
    if(t->cpu->fpu_owner==t)
228
        t->cpu->fpu_owner=NULL;
229
    spinlock_unlock(&t->cpu->lock);
230
 
231
    /*
232
     * Detach from the containing task.
233
     */
234
    spinlock_lock(&t->task->lock);
235
    list_remove(&t->th_link);
236
    spinlock_unlock(&t->task->lock);
237
 
238
    spinlock_unlock(&t->lock);
239
 
240
    spinlock_lock(&threads_lock);
241
    list_remove(&t->threads_link);
242
    spinlock_unlock(&threads_lock);
243
 
244
    slab_free(thread_slab, t);
245
}
246
 
247
 
107 decky 248
/** Create new thread
249
 *
250
 * Create a new thread.
251
 *
252
 * @param func  Thread's implementing function.
253
 * @param arg   Thread's implementing function argument.
254
 * @param task  Task to which the thread belongs.
255
 * @param flags Thread flags.
1062 jermar 256
 * @param name  Symbolic name.
107 decky 257
 *
258
 * @return New thread's structure on success, NULL on failure.
259
 *
260
 */
1062 jermar 261
thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name)
1 jermar 262
{
263
    thread_t *t;
822 palkovsky 264
    ipl_t ipl;
265
 
787 palkovsky 266
    t = (thread_t *) slab_alloc(thread_slab, 0);
842 palkovsky 267
    if (!t)
268
        return NULL;
1 jermar 269
 
822 palkovsky 270
    /* Not needed, but good for debugging */
271
    memsetb((__address)t->kstack, THREAD_STACK_SIZE, 0);
272
 
273
    ipl = interrupts_disable();
274
    spinlock_lock(&tidlock);
275
    t->tid = ++last_tid;
276
    spinlock_unlock(&tidlock);
277
    interrupts_restore(ipl);
278
 
279
    context_save(&t->saved_context);
280
    context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE);
281
 
282
    the_initialize((the_t *) t->kstack);
283
 
284
    ipl = interrupts_disable();
285
    t->saved_context.ipl = interrupts_read();
286
    interrupts_restore(ipl);
287
 
1066 jermar 288
    memcpy(t->name, name, THREAD_NAME_BUFLEN);
289
 
822 palkovsky 290
    t->thread_code = func;
291
    t->thread_arg = arg;
292
    t->ticks = -1;
293
    t->priority = -1;       /* start in rq[0] */
294
    t->cpu = NULL;
295
    t->flags = 0;
296
    t->state = Entering;
297
    t->call_me = NULL;
298
    t->call_me_with = NULL;
299
 
300
    timeout_initialize(&t->sleep_timeout);
301
    t->sleep_queue = NULL;
302
    t->timeout_pending = 0;
303
 
304
    t->rwlock_holder_type = RWLOCK_NONE;
210 decky 305
 
822 palkovsky 306
    t->task = task;
307
 
860 decky 308
    t->fpu_context_exists = 0;
309
    t->fpu_context_engaged = 0;
822 palkovsky 310
 
311
    /*
312
     * Register this thread in the system-wide list.
313
     */
314
    ipl = interrupts_disable();
315
    spinlock_lock(&threads_lock);
316
    list_append(&t->threads_link, &threads_head);
317
    spinlock_unlock(&threads_lock);
318
 
319
    /*
320
     * Attach to the containing task.
321
     */
322
    spinlock_lock(&task->lock);
323
    list_append(&t->th_link, &task->th_head);
324
    spinlock_unlock(&task->lock);
325
 
326
    interrupts_restore(ipl);
860 decky 327
 
1 jermar 328
    return t;
329
}
330
 
107 decky 331
 
332
/** Make thread exiting
333
 *
334
 * End current thread execution and switch it to the exiting
335
 * state. All pending timeouts are executed.
336
 *
337
 */
1 jermar 338
void thread_exit(void)
339
{
413 jermar 340
    ipl_t ipl;
1 jermar 341
 
342
restart:
413 jermar 343
    ipl = interrupts_disable();
15 jermar 344
    spinlock_lock(&THREAD->lock);
345
    if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */
346
        spinlock_unlock(&THREAD->lock);
413 jermar 347
        interrupts_restore(ipl);
1 jermar 348
        goto restart;
349
    }
15 jermar 350
    THREAD->state = Exiting;
351
    spinlock_unlock(&THREAD->lock);
1 jermar 352
    scheduler();
353
}
354
 
107 decky 355
 
356
/** Thread sleep
357
 *
358
 * Suspend execution of the current thread.
359
 *
360
 * @param sec Number of seconds to sleep.
361
 *
362
 */
1 jermar 363
void thread_sleep(__u32 sec)
364
{
125 jermar 365
    thread_usleep(sec*1000000);
1 jermar 366
}
107 decky 367
 
368
 
369
/** Thread usleep
370
 *
371
 * Suspend execution of the current thread.
372
 *
373
 * @param usec Number of microseconds to sleep.
374
 *
375
 */
1 jermar 376
void thread_usleep(__u32 usec)
377
{
378
    waitq_t wq;
379
 
380
    waitq_initialize(&wq);
381
 
382
    (void) waitq_sleep_timeout(&wq, usec, SYNCH_NON_BLOCKING);
383
}
384
 
107 decky 385
 
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
    thread_t *t;
412
    ipl_t ipl;
413
 
414
    /* Messing with thread structures, avoid deadlock */
415
    ipl = interrupts_disable();
416
    spinlock_lock(&threads_lock);
417
 
418
    for (cur=threads_head.next; cur!=&threads_head; cur=cur->next) {
419
        t = list_get_instance(cur, thread_t, threads_link);
1086 palkovsky 420
        printf("%s: address=%P, tid=%d, state=%s\n\ttask=%P, code=%P, stack=%P, cpu=",
1062 jermar 421
            t->name, t, t->tid, thread_states[t->state], t->task, t->thread_code, t->kstack);
777 palkovsky 422
        if (t->cpu)
423
            printf("cpu%d ", t->cpu->id);
1062 jermar 424
        else
425
            printf("none");
777 palkovsky 426
        printf("\n");
427
    }
428
 
429
    spinlock_unlock(&threads_lock);
1060 palkovsky 430
    interrupts_restore(ipl);
777 palkovsky 431
}
1066 jermar 432
 
433
/** Process syscall to create new thread.
434
 *
435
 */
1078 jermar 436
__native sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name)
1066 jermar 437
{
438
        thread_t *t;
439
        char namebuf[THREAD_NAME_BUFLEN];
1078 jermar 440
    uspace_arg_t *kernel_uarg;      /* TODO: store kernel_uarg in thread_t */
1066 jermar 441
    __u32 tid;
442
 
1078 jermar 443
        copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN);
1066 jermar 444
 
1078 jermar 445
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
446
    copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
447
 
448
        if ((t = thread_create(uinit, kernel_uarg, TASK, 0, namebuf))) {
1066 jermar 449
        tid = t->tid;
450
                thread_ready(t);
451
        return (__native) tid;
452
        } else {
1078 jermar 453
        free(kernel_uarg);
1066 jermar 454
        }
455
 
456
        return (__native) -1;
457
}
458
 
459
/** Process syscall to terminate thread.
460
 *
461
 */
1078 jermar 462
__native sys_thread_exit(int uspace_status)
1066 jermar 463
{
464
        thread_exit();
465
        /* Unreachable */
466
        return 0;
467
}