source: mainline/kernel/generic/src/proc/thread.c@ cd896e2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cd896e2 was 449dc1ed, checked in by Martin Decky <martin@…>, 19 years ago

proper initialization of last_cycle

  • Property mode set to 100644
File size: 14.8 KB
RevLine 
[f761f1eb]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
[cc73a8a1]29/** @addtogroup genericproc
[b45c443]30 * @{
31 */
32
[9179d0a]33/**
[b45c443]34 * @file
[9179d0a]35 * @brief Thread management functions.
36 */
37
[f761f1eb]38#include <proc/scheduler.h>
39#include <proc/thread.h>
40#include <proc/task.h>
[0f250f9]41#include <proc/uarg.h>
[f761f1eb]42#include <mm/frame.h>
43#include <mm/page.h>
44#include <arch/asm.h>
[cce6acf]45#include <arch/cycle.h>
[f761f1eb]46#include <arch.h>
47#include <synch/synch.h>
48#include <synch/spinlock.h>
49#include <synch/waitq.h>
50#include <synch/rwlock.h>
51#include <cpu.h>
52#include <func.h>
53#include <context.h>
[016acbe]54#include <adt/btree.h>
[5c9a08b]55#include <adt/list.h>
[f761f1eb]56#include <typedefs.h>
57#include <time/clock.h>
[4ffa9e0]58#include <config.h>
59#include <arch/interrupt.h>
[26a8604f]60#include <smp/ipi.h>
[f2ffad4]61#include <arch/faddr.h>
[23684b7]62#include <atomic.h>
[9c0a9b3]63#include <memstr.h>
[55ab0f1]64#include <print.h>
[266294a9]65#include <mm/slab.h>
66#include <debug.h>
[9f52563]67#include <main/uinit.h>
[e3c762cd]68#include <syscall/copy.h>
69#include <errno.h>
[f761f1eb]70
[fe19611]71
72/** Thread states */
73char *thread_states[] = {
74 "Invalid",
75 "Running",
76 "Sleeping",
77 "Ready",
78 "Entering",
79 "Exiting",
80 "Undead"
81};
[f761f1eb]82
[88169d9]83/** Lock protecting the threads_btree B+tree. For locking rules, see declaration thereof. */
[016acbe]84SPINLOCK_INITIALIZE(threads_lock);
[88169d9]85
86/** B+tree of all threads.
87 *
88 * When a thread is found in the threads_btree B+tree, it is guaranteed to exist as long
89 * as the threads_lock is held.
90 */
91btree_t threads_btree;
[f761f1eb]92
[dc747e3]93SPINLOCK_INITIALIZE(tidlock);
[7f1c620]94uint32_t last_tid = 0;
[f761f1eb]95
[266294a9]96static slab_cache_t *thread_slab;
[f76fed4]97#ifdef ARCH_HAS_FPU
98slab_cache_t *fpu_context_slab;
99#endif
[266294a9]100
[70527f1]101/** Thread wrapper
102 *
103 * This wrapper is provided to ensure that every thread
[f761f1eb]104 * makes a call to thread_exit() when its implementing
105 * function returns.
106 *
[22f7769]107 * interrupts_disable() is assumed.
[70527f1]108 *
[f761f1eb]109 */
[e16e036a]110static void cushion(void)
[f761f1eb]111{
[43114c5]112 void (*f)(void *) = THREAD->thread_code;
113 void *arg = THREAD->thread_arg;
[449dc1ed]114 THREAD->last_cycle = get_cycle();
[f761f1eb]115
[3e1607f]116 /* this is where each thread wakes up after its creation */
[43114c5]117 spinlock_unlock(&THREAD->lock);
[22f7769]118 interrupts_enable();
[f761f1eb]119
120 f(arg);
121 thread_exit();
122 /* not reached */
123}
124
[266294a9]125/** Initialization and allocation for thread_t structure */
126static int thr_constructor(void *obj, int kmflags)
127{
[764c302]128 thread_t *t = (thread_t *) obj;
[266294a9]129
130 spinlock_initialize(&t->lock, "thread_t_lock");
131 link_initialize(&t->rq_link);
132 link_initialize(&t->wq_link);
133 link_initialize(&t->th_link);
[32fffef0]134
135 /* call the architecture-specific part of the constructor */
136 thr_constructor_arch(t);
[266294a9]137
[f76fed4]138#ifdef ARCH_HAS_FPU
139# ifdef CONFIG_FPU_LAZY
140 t->saved_fpu_context = NULL;
141# else
142 t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags);
143 if (!t->saved_fpu_context)
144 return -1;
145# endif
146#endif
147
[e45f81a]148 t->kstack = frame_alloc(STACK_FRAMES, FRAME_KA | kmflags);
149 if (! t->kstack) {
[f76fed4]150#ifdef ARCH_HAS_FPU
151 if (t->saved_fpu_context)
152 slab_free(fpu_context_slab,t->saved_fpu_context);
153#endif
[266294a9]154 return -1;
[f76fed4]155 }
[266294a9]156
157 return 0;
158}
159
160/** Destruction of thread_t object */
161static int thr_destructor(void *obj)
162{
[764c302]163 thread_t *t = (thread_t *) obj;
[266294a9]164
[32fffef0]165 /* call the architecture-specific part of the destructor */
166 thr_destructor_arch(t);
167
[2e9eae2]168 frame_free(KA2PA(t->kstack));
[f76fed4]169#ifdef ARCH_HAS_FPU
170 if (t->saved_fpu_context)
171 slab_free(fpu_context_slab,t->saved_fpu_context);
172#endif
[266294a9]173 return 1; /* One page freed */
174}
[70527f1]175
176/** Initialize threads
177 *
178 * Initialize kernel threads support.
179 *
180 */
[f761f1eb]181void thread_init(void)
182{
[43114c5]183 THREAD = NULL;
[80d2bdb]184 atomic_set(&nrdy,0);
[266294a9]185 thread_slab = slab_cache_create("thread_slab",
186 sizeof(thread_t),0,
187 thr_constructor, thr_destructor, 0);
[f76fed4]188#ifdef ARCH_HAS_FPU
189 fpu_context_slab = slab_cache_create("fpu_slab",
190 sizeof(fpu_context_t),
191 FPU_CONTEXT_ALIGN,
192 NULL, NULL, 0);
193#endif
[f761f1eb]194
[016acbe]195 btree_create(&threads_btree);
196}
[70527f1]197
198/** Make thread ready
199 *
200 * Switch thread t to the ready state.
201 *
202 * @param t Thread to make ready.
203 *
204 */
[f761f1eb]205void thread_ready(thread_t *t)
206{
207 cpu_t *cpu;
208 runq_t *r;
[22f7769]209 ipl_t ipl;
[80d2bdb]210 int i, avg;
[f761f1eb]211
[22f7769]212 ipl = interrupts_disable();
[f761f1eb]213
214 spinlock_lock(&t->lock);
215
[fbcfd458]216 ASSERT(! (t->state == Ready));
217
[22f7769]218 i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority;
[f761f1eb]219
[8262010]220 cpu = CPU;
[32fffef0]221 if (t->flags & THREAD_FLAG_WIRED) {
[f761f1eb]222 cpu = t->cpu;
223 }
[81c4c6da]224 t->state = Ready;
[f761f1eb]225 spinlock_unlock(&t->lock);
226
[70527f1]227 /*
[f761f1eb]228 * Append t to respective ready queue on respective processor.
229 */
230 r = &cpu->rq[i];
231 spinlock_lock(&r->lock);
232 list_append(&t->rq_link, &r->rq_head);
233 r->n++;
234 spinlock_unlock(&r->lock);
235
[59e07c91]236 atomic_inc(&nrdy);
[80d2bdb]237 avg = atomic_get(&nrdy) / config.cpu_active;
[248fc1a]238 atomic_inc(&cpu->nrdy);
[76cec1e]239
[22f7769]240 interrupts_restore(ipl);
[f761f1eb]241}
242
[266294a9]243/** Destroy thread memory structure
244 *
245 * Detach thread from all queues, cpus etc. and destroy it.
246 *
247 * Assume thread->lock is held!!
248 */
249void thread_destroy(thread_t *t)
250{
[7509ddc]251 bool destroy_task = false;
252
[c778c1a]253 ASSERT(t->state == Exiting || t->state == Undead);
[266294a9]254 ASSERT(t->task);
255 ASSERT(t->cpu);
256
257 spinlock_lock(&t->cpu->lock);
258 if(t->cpu->fpu_owner==t)
259 t->cpu->fpu_owner=NULL;
260 spinlock_unlock(&t->cpu->lock);
261
[7509ddc]262 spinlock_unlock(&t->lock);
263
264 spinlock_lock(&threads_lock);
[7f1c620]265 btree_remove(&threads_btree, (btree_key_t) ((uintptr_t ) t), NULL);
[7509ddc]266 spinlock_unlock(&threads_lock);
267
[266294a9]268 /*
269 * Detach from the containing task.
270 */
271 spinlock_lock(&t->task->lock);
272 list_remove(&t->th_link);
[7509ddc]273 if (--t->task->refcount == 0) {
274 t->task->accept_new_threads = false;
275 destroy_task = true;
276 }
277 spinlock_unlock(&t->task->lock);
[266294a9]278
[7509ddc]279 if (destroy_task)
280 task_destroy(t->task);
[266294a9]281
282 slab_free(thread_slab, t);
283}
284
[70527f1]285/** Create new thread
286 *
287 * Create a new thread.
288 *
289 * @param func Thread's implementing function.
290 * @param arg Thread's implementing function argument.
291 * @param task Task to which the thread belongs.
292 * @param flags Thread flags.
[ff14c520]293 * @param name Symbolic name.
[70527f1]294 *
295 * @return New thread's structure on success, NULL on failure.
296 *
297 */
[ff14c520]298thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name)
[f761f1eb]299{
300 thread_t *t;
[bb68433]301 ipl_t ipl;
302
[266294a9]303 t = (thread_t *) slab_alloc(thread_slab, 0);
[2a46e10]304 if (!t)
305 return NULL;
[f761f1eb]306
[bb68433]307 /* Not needed, but good for debugging */
[764c302]308 memsetb((uintptr_t) t->kstack, THREAD_STACK_SIZE * 1 << STACK_FRAMES, 0);
[bb68433]309
310 ipl = interrupts_disable();
311 spinlock_lock(&tidlock);
312 t->tid = ++last_tid;
313 spinlock_unlock(&tidlock);
314 interrupts_restore(ipl);
315
316 context_save(&t->saved_context);
[7f1c620]317 context_set(&t->saved_context, FADDR(cushion), (uintptr_t) t->kstack, THREAD_STACK_SIZE);
[bb68433]318
319 the_initialize((the_t *) t->kstack);
320
321 ipl = interrupts_disable();
322 t->saved_context.ipl = interrupts_read();
323 interrupts_restore(ipl);
324
[9f52563]325 memcpy(t->name, name, THREAD_NAME_BUFLEN);
326
[bb68433]327 t->thread_code = func;
328 t->thread_arg = arg;
329 t->ticks = -1;
[cce6acf]330 t->cycles = 0;
[bb68433]331 t->priority = -1; /* start in rq[0] */
332 t->cpu = NULL;
[32fffef0]333 t->flags = flags;
[bb68433]334 t->state = Entering;
335 t->call_me = NULL;
336 t->call_me_with = NULL;
337
338 timeout_initialize(&t->sleep_timeout);
[116d1ef4]339 t->sleep_interruptible = false;
[bb68433]340 t->sleep_queue = NULL;
341 t->timeout_pending = 0;
[e3c762cd]342
343 t->in_copy_from_uspace = false;
344 t->in_copy_to_uspace = false;
[7509ddc]345
346 t->interrupted = false;
[48e7dd6]347 t->join_type = None;
[fe19611]348 t->detached = false;
349 waitq_initialize(&t->join_wq);
350
[bb68433]351 t->rwlock_holder_type = RWLOCK_NONE;
[6a27d63]352
[bb68433]353 t->task = task;
354
[6f8a426]355 t->fpu_context_exists = 0;
356 t->fpu_context_engaged = 0;
[32fffef0]357
358 thread_create_arch(t); /* might depend on previous initialization */
[bb68433]359
[7509ddc]360 /*
361 * Attach to the containing task.
362 */
[0182a665]363 ipl = interrupts_disable();
[7509ddc]364 spinlock_lock(&task->lock);
365 if (!task->accept_new_threads) {
366 spinlock_unlock(&task->lock);
367 slab_free(thread_slab, t);
[0182a665]368 interrupts_restore(ipl);
[7509ddc]369 return NULL;
370 }
371 list_append(&t->th_link, &task->th_head);
[b91bb65]372 if (task->refcount++ == 0)
373 task->main_thread = t;
[7509ddc]374 spinlock_unlock(&task->lock);
375
[bb68433]376 /*
377 * Register this thread in the system-wide list.
378 */
379 spinlock_lock(&threads_lock);
[7f1c620]380 btree_insert(&threads_btree, (btree_key_t) ((uintptr_t) t), (void *) t, NULL);
[bb68433]381 spinlock_unlock(&threads_lock);
382
383 interrupts_restore(ipl);
[6f8a426]384
[f761f1eb]385 return t;
386}
387
[0182a665]388/** Terminate thread.
[70527f1]389 *
390 * End current thread execution and switch it to the exiting
391 * state. All pending timeouts are executed.
392 *
393 */
[f761f1eb]394void thread_exit(void)
395{
[22f7769]396 ipl_t ipl;
[f761f1eb]397
398restart:
[22f7769]399 ipl = interrupts_disable();
[43114c5]400 spinlock_lock(&THREAD->lock);
401 if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */
402 spinlock_unlock(&THREAD->lock);
[22f7769]403 interrupts_restore(ipl);
[f761f1eb]404 goto restart;
405 }
[43114c5]406 THREAD->state = Exiting;
407 spinlock_unlock(&THREAD->lock);
[f761f1eb]408 scheduler();
[874621f]409
410 /* Not reached */
411 while (1)
412 ;
[f761f1eb]413}
414
[70527f1]415
416/** Thread sleep
417 *
418 * Suspend execution of the current thread.
419 *
420 * @param sec Number of seconds to sleep.
421 *
422 */
[7f1c620]423void thread_sleep(uint32_t sec)
[f761f1eb]424{
[76cec1e]425 thread_usleep(sec*1000000);
[f761f1eb]426}
[70527f1]427
[fe19611]428/** Wait for another thread to exit.
429 *
430 * @param t Thread to join on exit.
431 * @param usec Timeout in microseconds.
432 * @param flags Mode of operation.
433 *
434 * @return An error code from errno.h or an error code from synch.h.
435 */
[7f1c620]436int thread_join_timeout(thread_t *t, uint32_t usec, int flags)
[fe19611]437{
438 ipl_t ipl;
439 int rc;
440
441 if (t == THREAD)
442 return EINVAL;
443
444 /*
445 * Since thread join can only be called once on an undetached thread,
446 * the thread pointer is guaranteed to be still valid.
447 */
448
449 ipl = interrupts_disable();
450 spinlock_lock(&t->lock);
451 ASSERT(!t->detached);
452 spinlock_unlock(&t->lock);
[7b3e7f4]453 interrupts_restore(ipl);
[fe19611]454
[0182a665]455 rc = waitq_sleep_timeout(&t->join_wq, usec, flags);
456
[fe19611]457 return rc;
458}
459
460/** Detach thread.
461 *
462 * Mark the thread as detached, if the thread is already in the Undead state,
463 * deallocate its resources.
464 *
465 * @param t Thread to be detached.
466 */
467void thread_detach(thread_t *t)
468{
469 ipl_t ipl;
470
471 /*
472 * Since the thread is expected to not be already detached,
473 * pointer to it must be still valid.
474 */
475 ipl = interrupts_disable();
476 spinlock_lock(&t->lock);
477 ASSERT(!t->detached);
478 if (t->state == Undead) {
479 thread_destroy(t); /* unlocks &t->lock */
480 interrupts_restore(ipl);
481 return;
482 } else {
483 t->detached = true;
484 }
485 spinlock_unlock(&t->lock);
486 interrupts_restore(ipl);
487}
488
[70527f1]489/** Thread usleep
490 *
491 * Suspend execution of the current thread.
492 *
493 * @param usec Number of microseconds to sleep.
494 *
495 */
[7f1c620]496void thread_usleep(uint32_t usec)
[f761f1eb]497{
498 waitq_t wq;
499
500 waitq_initialize(&wq);
501
[116d1ef4]502 (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING);
[f761f1eb]503}
504
[70527f1]505/** Register thread out-of-context invocation
506 *
507 * Register a function and its argument to be executed
508 * on next context switch to the current thread.
509 *
510 * @param call_me Out-of-context function.
511 * @param call_me_with Out-of-context function argument.
512 *
513 */
[f761f1eb]514void thread_register_call_me(void (* call_me)(void *), void *call_me_with)
515{
[22f7769]516 ipl_t ipl;
[f761f1eb]517
[22f7769]518 ipl = interrupts_disable();
[43114c5]519 spinlock_lock(&THREAD->lock);
520 THREAD->call_me = call_me;
521 THREAD->call_me_with = call_me_with;
522 spinlock_unlock(&THREAD->lock);
[22f7769]523 interrupts_restore(ipl);
[f761f1eb]524}
[55ab0f1]525
526/** Print list of threads debug info */
527void thread_print_list(void)
528{
529 link_t *cur;
530 ipl_t ipl;
531
532 /* Messing with thread structures, avoid deadlock */
533 ipl = interrupts_disable();
534 spinlock_lock(&threads_lock);
[cce6acf]535
536 printf("tid name address state task ctx code stack cycles cpu kst wq\n");
537 printf("------ ---------- ---------- -------- ---------- --- ---------- ---------- ---------- ---- ---------- ----------\n");
[55ab0f1]538
[016acbe]539 for (cur = threads_btree.leaf_head.next; cur != &threads_btree.leaf_head; cur = cur->next) {
540 btree_node_t *node;
541 int i;
542
543 node = list_get_instance(cur, btree_node_t, leaf_link);
544 for (i = 0; i < node->keys; i++) {
545 thread_t *t;
546
547 t = (thread_t *) node->value[i];
[cce6acf]548
549 uint64_t cycles;
550 char suffix;
551
552 if (t->cycles > 1000000000000000000LL) {
553 cycles = t->cycles / 1000000000000000000LL;
554 suffix = 'E';
555 } else if (t->cycles > 1000000000000LL) {
556 cycles = t->cycles / 1000000000000LL;
557 suffix = 'T';
558 } else if (t->cycles > 1000000LL) {
559 cycles = t->cycles / 1000000LL;
560 suffix = 'M';
561 } else {
562 cycles = t->cycles;
563 suffix = ' ';
564 }
565
566 printf("%-6zd %-10s %#10zx %-8s %#10zx %-3ld %#10zx %#10zx %9llu%c ", t->tid, t->name, t, thread_states[t->state], t->task, t->task->context, t->thread_code, t->kstack, cycles, suffix);
567
[016acbe]568 if (t->cpu)
[cce6acf]569 printf("%-4zd", t->cpu->id);
[016acbe]570 else
571 printf("none");
[cce6acf]572
573 if (t->state == Sleeping)
574 printf(" %#10zx %#10zx", t->kstack, t->sleep_queue);
575
[016acbe]576 printf("\n");
577 }
[55ab0f1]578 }
579
580 spinlock_unlock(&threads_lock);
[37c57f2]581 interrupts_restore(ipl);
[55ab0f1]582}
[9f52563]583
[016acbe]584/** Check whether thread exists.
585 *
586 * Note that threads_lock must be already held and
587 * interrupts must be already disabled.
588 *
589 * @param t Pointer to thread.
590 *
591 * @return True if thread t is known to the system, false otherwise.
592 */
593bool thread_exists(thread_t *t)
594{
595 btree_node_t *leaf;
596
[7f1c620]597 return btree_search(&threads_btree, (btree_key_t) ((uintptr_t) t), &leaf) != NULL;
[016acbe]598}
599
[cce6acf]600
601/** Update accounting of current thread.
602 *
603 * Note that thread_lock on THREAD must be already held and
604 * interrupts must be already disabled.
605 *
606 * @param t Pointer to thread.
607 *
608 */
609void thread_update_accounting(void)
610{
611 uint64_t time = get_cycle();
612 THREAD->cycles += time - THREAD->last_cycle;
613 THREAD->last_cycle = time;
614}
615
[9f52563]616/** Process syscall to create new thread.
617 *
618 */
[7f1c620]619unative_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name)
[9f52563]620{
[68091bd]621 thread_t *t;
622 char namebuf[THREAD_NAME_BUFLEN];
[45fb65c]623 uspace_arg_t *kernel_uarg;
[7f1c620]624 uint32_t tid;
[e3c762cd]625 int rc;
[9f52563]626
[e3c762cd]627 rc = copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN);
628 if (rc != 0)
[7f1c620]629 return (unative_t) rc;
[0f250f9]630
631 kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
[e3c762cd]632 rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
633 if (rc != 0) {
634 free(kernel_uarg);
[7f1c620]635 return (unative_t) rc;
[e3c762cd]636 }
[9f52563]637
[32fffef0]638 if ((t = thread_create(uinit, kernel_uarg, TASK, THREAD_FLAG_USPACE, namebuf))) {
[9f52563]639 tid = t->tid;
[68091bd]640 thread_ready(t);
[7f1c620]641 return (unative_t) tid;
[68091bd]642 } else {
[0f250f9]643 free(kernel_uarg);
[68091bd]644 }
[9f52563]645
[7f1c620]646 return (unative_t) ENOMEM;
[9f52563]647}
648
649/** Process syscall to terminate thread.
650 *
651 */
[7f1c620]652unative_t sys_thread_exit(int uspace_status)
[9f52563]653{
[68091bd]654 thread_exit();
655 /* Unreachable */
656 return 0;
[9f52563]657}
[b45c443]658
[cc73a8a1]659/** @}
[b45c443]660 */
Note: See TracBrowser for help on using the repository browser.