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

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

rename order() to order_suffix(), make it a generic libc string function in user space

  • Property mode set to 100644
File size: 20.2 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]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>
[e535eeb]52#include <str.h>
[f761f1eb]53#include <context.h>
[5dcee525]54#include <adt/avl.h>
[5c9a08b]55#include <adt/list.h>
[f761f1eb]56#include <time/clock.h>
[b3f8fb7]57#include <time/timeout.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>
[52755f1]70
71
72#ifndef LOADED_PROG_STACK_PAGES_NO
73#define LOADED_PROG_STACK_PAGES_NO 1
74#endif
[f761f1eb]75
[fe19611]76
77/** Thread states */
[a000878c]78const char *thread_states[] = {
[fe19611]79 "Invalid",
80 "Running",
81 "Sleeping",
82 "Ready",
83 "Entering",
84 "Exiting",
[48d14222]85 "Lingering"
[e1b6742]86};
87
88typedef struct {
89 thread_id_t thread_id;
90 thread_t *thread;
91} thread_iterator_t;
[f761f1eb]92
[5dcee525]93/** Lock protecting the threads_tree AVL tree.
[4e33b6b]94 *
95 * For locking rules, see declaration thereof.
96 */
[016acbe]97SPINLOCK_INITIALIZE(threads_lock);
[88169d9]98
[81c0171e]99/** AVL tree of all threads.
[88169d9]100 *
[5dcee525]101 * When a thread is found in the threads_tree AVL tree, it is guaranteed to
[4e33b6b]102 * exist as long as the threads_lock is held.
[88169d9]103 */
[5dcee525]104avltree_t threads_tree;
[f761f1eb]105
[dc747e3]106SPINLOCK_INITIALIZE(tidlock);
[201abde]107thread_id_t last_tid = 0;
[f761f1eb]108
[266294a9]109static slab_cache_t *thread_slab;
[0f81ceb7]110#ifdef CONFIG_FPU
[f76fed4]111slab_cache_t *fpu_context_slab;
112#endif
[266294a9]113
[4e33b6b]114/** Thread wrapper.
[70527f1]115 *
[4e33b6b]116 * This wrapper is provided to ensure that every thread makes a call to
117 * thread_exit() when its implementing function returns.
[f761f1eb]118 *
[22f7769]119 * interrupts_disable() is assumed.
[70527f1]120 *
[f761f1eb]121 */
[e16e036a]122static void cushion(void)
[f761f1eb]123{
[43114c5]124 void (*f)(void *) = THREAD->thread_code;
125 void *arg = THREAD->thread_arg;
[449dc1ed]126 THREAD->last_cycle = get_cycle();
[f761f1eb]127
[0313ff0]128 /* This is where each thread wakes up after its creation */
[43114c5]129 spinlock_unlock(&THREAD->lock);
[22f7769]130 interrupts_enable();
[f761f1eb]131
132 f(arg);
[0313ff0]133
134 /* Accumulate accounting to the task */
135 ipl_t ipl = interrupts_disable();
136
137 spinlock_lock(&THREAD->lock);
[62b6d17]138 if (!THREAD->uncounted) {
[a2a00e8]139 thread_update_accounting(true);
140 uint64_t ucycles = THREAD->ucycles;
141 THREAD->ucycles = 0;
142 uint64_t kcycles = THREAD->kcycles;
143 THREAD->kcycles = 0;
144
[62b6d17]145 spinlock_unlock(&THREAD->lock);
146
147 spinlock_lock(&TASK->lock);
[a2a00e8]148 TASK->ucycles += ucycles;
149 TASK->kcycles += kcycles;
[62b6d17]150 spinlock_unlock(&TASK->lock);
151 } else
152 spinlock_unlock(&THREAD->lock);
[0313ff0]153
154 interrupts_restore(ipl);
155
[f761f1eb]156 thread_exit();
157 /* not reached */
158}
159
[266294a9]160/** Initialization and allocation for thread_t structure */
161static int thr_constructor(void *obj, int kmflags)
162{
[764c302]163 thread_t *t = (thread_t *) obj;
[266294a9]164
165 spinlock_initialize(&t->lock, "thread_t_lock");
166 link_initialize(&t->rq_link);
167 link_initialize(&t->wq_link);
168 link_initialize(&t->th_link);
[32fffef0]169
170 /* call the architecture-specific part of the constructor */
171 thr_constructor_arch(t);
[266294a9]172
[0f81ceb7]173#ifdef CONFIG_FPU
[d8431986]174#ifdef CONFIG_FPU_LAZY
[f76fed4]175 t->saved_fpu_context = NULL;
[d8431986]176#else
177 t->saved_fpu_context = slab_alloc(fpu_context_slab, kmflags);
[f76fed4]178 if (!t->saved_fpu_context)
179 return -1;
[d8431986]180#endif
[0f81ceb7]181#endif
[f76fed4]182
[4184e76]183 t->kstack = (uint8_t *) frame_alloc(STACK_FRAMES, FRAME_KA | kmflags);
[d8431986]184 if (!t->kstack) {
[0f81ceb7]185#ifdef CONFIG_FPU
[f76fed4]186 if (t->saved_fpu_context)
[d8431986]187 slab_free(fpu_context_slab, t->saved_fpu_context);
[f76fed4]188#endif
[266294a9]189 return -1;
[f76fed4]190 }
[266294a9]191
[9a1b20c]192#ifdef CONFIG_UDEBUG
193 mutex_initialize(&t->udebug.lock, MUTEX_PASSIVE);
194#endif
195
[266294a9]196 return 0;
197}
198
199/** Destruction of thread_t object */
200static int thr_destructor(void *obj)
201{
[764c302]202 thread_t *t = (thread_t *) obj;
[266294a9]203
[32fffef0]204 /* call the architecture-specific part of the destructor */
205 thr_destructor_arch(t);
206
[2e9eae2]207 frame_free(KA2PA(t->kstack));
[0f81ceb7]208#ifdef CONFIG_FPU
[f76fed4]209 if (t->saved_fpu_context)
[d8431986]210 slab_free(fpu_context_slab, t->saved_fpu_context);
[f76fed4]211#endif
[266294a9]212 return 1; /* One page freed */
213}
[70527f1]214
215/** Initialize threads
216 *
217 * Initialize kernel threads support.
218 *
219 */
[f761f1eb]220void thread_init(void)
221{
[43114c5]222 THREAD = NULL;
[7217199]223 atomic_set(&nrdy, 0);
[4e33b6b]224 thread_slab = slab_cache_create("thread_slab", sizeof(thread_t), 0,
[6f4495f5]225 thr_constructor, thr_destructor, 0);
[4e33b6b]226
[0f81ceb7]227#ifdef CONFIG_FPU
[4e33b6b]228 fpu_context_slab = slab_cache_create("fpu_slab", sizeof(fpu_context_t),
[6f4495f5]229 FPU_CONTEXT_ALIGN, NULL, NULL, 0);
[f76fed4]230#endif
[f761f1eb]231
[5dcee525]232 avltree_create(&threads_tree);
[016acbe]233}
[70527f1]234
235/** Make thread ready
236 *
237 * Switch thread t to the ready state.
238 *
239 * @param t Thread to make ready.
240 *
241 */
[f761f1eb]242void thread_ready(thread_t *t)
243{
244 cpu_t *cpu;
245 runq_t *r;
[22f7769]246 ipl_t ipl;
[80d2bdb]247 int i, avg;
[f761f1eb]248
[22f7769]249 ipl = interrupts_disable();
[f761f1eb]250
251 spinlock_lock(&t->lock);
252
[d8431986]253 ASSERT(!(t->state == Ready));
[fbcfd458]254
[4e33b6b]255 i = (t->priority < RQ_COUNT - 1) ? ++t->priority : t->priority;
[f761f1eb]256
[8262010]257 cpu = CPU;
[32fffef0]258 if (t->flags & THREAD_FLAG_WIRED) {
[4365d10]259 ASSERT(t->cpu != NULL);
[f761f1eb]260 cpu = t->cpu;
261 }
[81c4c6da]262 t->state = Ready;
[f761f1eb]263 spinlock_unlock(&t->lock);
264
[70527f1]265 /*
[f761f1eb]266 * Append t to respective ready queue on respective processor.
267 */
268 r = &cpu->rq[i];
269 spinlock_lock(&r->lock);
270 list_append(&t->rq_link, &r->rq_head);
271 r->n++;
272 spinlock_unlock(&r->lock);
273
[59e07c91]274 atomic_inc(&nrdy);
[137691a]275 // FIXME: Why is the avg value never read?
[80d2bdb]276 avg = atomic_get(&nrdy) / config.cpu_active;
[248fc1a]277 atomic_inc(&cpu->nrdy);
[76cec1e]278
[22f7769]279 interrupts_restore(ipl);
[f761f1eb]280}
281
[70527f1]282/** Create new thread
283 *
284 * Create a new thread.
285 *
[5d12283]286 * @param func Thread's implementing function.
287 * @param arg Thread's implementing function argument.
288 * @param task Task to which the thread belongs. The caller must
289 * guarantee that the task won't cease to exist during the
290 * call. The task's lock may not be held.
291 * @param flags Thread flags.
[24345a5]292 * @param name Symbolic name (a copy is made).
[5d12283]293 * @param uncounted Thread's accounting doesn't affect accumulated task
294 * accounting.
[70527f1]295 *
[5d12283]296 * @return New thread's structure on success, NULL on failure.
[70527f1]297 *
298 */
[4e33b6b]299thread_t *thread_create(void (* func)(void *), void *arg, task_t *task,
[a000878c]300 int flags, const char *name, bool uncounted)
[f761f1eb]301{
302 thread_t *t;
[bb68433]303 ipl_t ipl;
304
[266294a9]305 t = (thread_t *) slab_alloc(thread_slab, 0);
[2a46e10]306 if (!t)
307 return NULL;
[aa8d0f7]308
[bb68433]309 /* Not needed, but good for debugging */
[e32e092]310 memsetb(t->kstack, THREAD_STACK_SIZE * 1 << STACK_FRAMES, 0);
[bb68433]311
312 ipl = interrupts_disable();
313 spinlock_lock(&tidlock);
314 t->tid = ++last_tid;
315 spinlock_unlock(&tidlock);
316 interrupts_restore(ipl);
317
318 context_save(&t->saved_context);
[4e33b6b]319 context_set(&t->saved_context, FADDR(cushion), (uintptr_t) t->kstack,
[6f4495f5]320 THREAD_STACK_SIZE);
[bb68433]321
322 the_initialize((the_t *) t->kstack);
323
324 ipl = interrupts_disable();
325 t->saved_context.ipl = interrupts_read();
326 interrupts_restore(ipl);
327
[9f52563]328 memcpy(t->name, name, THREAD_NAME_BUFLEN);
[b60c582]329 t->name[THREAD_NAME_BUFLEN - 1] = 0;
[9f52563]330
[bb68433]331 t->thread_code = func;
332 t->thread_arg = arg;
333 t->ticks = -1;
[a2a00e8]334 t->ucycles = 0;
335 t->kcycles = 0;
[62b6d17]336 t->uncounted = uncounted;
[bb68433]337 t->priority = -1; /* start in rq[0] */
338 t->cpu = NULL;
[32fffef0]339 t->flags = flags;
[bb68433]340 t->state = Entering;
341 t->call_me = NULL;
342 t->call_me_with = NULL;
343
344 timeout_initialize(&t->sleep_timeout);
[116d1ef4]345 t->sleep_interruptible = false;
[bb68433]346 t->sleep_queue = NULL;
347 t->timeout_pending = 0;
[e3c762cd]348
349 t->in_copy_from_uspace = false;
350 t->in_copy_to_uspace = false;
[7509ddc]351
352 t->interrupted = false;
[fe19611]353 t->detached = false;
354 waitq_initialize(&t->join_wq);
355
[bb68433]356 t->rwlock_holder_type = RWLOCK_NONE;
[6a27d63]357
[bb68433]358 t->task = task;
359
[6f8a426]360 t->fpu_context_exists = 0;
361 t->fpu_context_engaged = 0;
[32fffef0]362
[5dcee525]363 avltree_node_initialize(&t->threads_tree_node);
364 t->threads_tree_node.key = (uintptr_t) t;
365
[9a1b20c]366#ifdef CONFIG_UDEBUG
367 /* Init debugging stuff */
368 udebug_thread_initialize(&t->udebug);
369#endif
370
[4e33b6b]371 /* might depend on previous initialization */
372 thread_create_arch(t);
[d8431986]373
374 if (!(flags & THREAD_FLAG_NOATTACH))
375 thread_attach(t, task);
376
377 return t;
378}
379
380/** Destroy thread memory structure
381 *
382 * Detach thread from all queues, cpus etc. and destroy it.
383 *
384 * Assume thread->lock is held!!
385 */
386void thread_destroy(thread_t *t)
387{
[48d14222]388 ASSERT(t->state == Exiting || t->state == Lingering);
[d8431986]389 ASSERT(t->task);
390 ASSERT(t->cpu);
391
392 spinlock_lock(&t->cpu->lock);
393 if (t->cpu->fpu_owner == t)
394 t->cpu->fpu_owner = NULL;
395 spinlock_unlock(&t->cpu->lock);
396
397 spinlock_unlock(&t->lock);
398
399 spinlock_lock(&threads_lock);
[5dcee525]400 avltree_delete(&threads_tree, &t->threads_tree_node);
[d8431986]401 spinlock_unlock(&threads_lock);
402
403 /*
404 * Detach from the containing task.
405 */
406 spinlock_lock(&t->task->lock);
407 list_remove(&t->th_link);
408 spinlock_unlock(&t->task->lock);
[ea7890e7]409
410 /*
411 * t is guaranteed to be the very last thread of its task.
412 * It is safe to destroy the task.
413 */
414 if (atomic_predec(&t->task->refcount) == 0)
[d8431986]415 task_destroy(t->task);
416
417 slab_free(thread_slab, t);
418}
419
420/** Make the thread visible to the system.
421 *
422 * Attach the thread structure to the current task and make it visible in the
[5dcee525]423 * threads_tree.
[d8431986]424 *
425 * @param t Thread to be attached to the task.
426 * @param task Task to which the thread is to be attached.
427 */
428void thread_attach(thread_t *t, task_t *task)
429{
430 ipl_t ipl;
431
432 /*
[9a1b20c]433 * Attach to the specified task.
[d8431986]434 */
[ea7890e7]435 ipl = interrupts_disable();
[d8431986]436 spinlock_lock(&task->lock);
[9a1b20c]437
[ea7890e7]438 atomic_inc(&task->refcount);
[9a1b20c]439
440 /* Must not count kbox thread into lifecount */
441 if (t->flags & THREAD_FLAG_USPACE)
442 atomic_inc(&task->lifecount);
443
[7509ddc]444 list_append(&t->th_link, &task->th_head);
445 spinlock_unlock(&task->lock);
446
[bb68433]447 /*
448 * Register this thread in the system-wide list.
449 */
450 spinlock_lock(&threads_lock);
[5dcee525]451 avltree_insert(&threads_tree, &t->threads_tree_node);
[bb68433]452 spinlock_unlock(&threads_lock);
453
454 interrupts_restore(ipl);
[f761f1eb]455}
456
[0182a665]457/** Terminate thread.
[70527f1]458 *
[4e33b6b]459 * End current thread execution and switch it to the exiting state. All pending
460 * timeouts are executed.
[70527f1]461 */
[f761f1eb]462void thread_exit(void)
463{
[22f7769]464 ipl_t ipl;
[f761f1eb]465
[9a1b20c]466 if (THREAD->flags & THREAD_FLAG_USPACE) {
467#ifdef CONFIG_UDEBUG
468 /* Generate udebug THREAD_E event */
469 udebug_thread_e_event();
470#endif
471 if (atomic_predec(&TASK->lifecount) == 0) {
472 /*
473 * We are the last userspace thread in the task that
474 * still has not exited. With the exception of the
475 * moment the task was created, new userspace threads
476 * can only be created by threads of the same task.
477 * We are safe to perform cleanup.
478 */
[ea7890e7]479 ipc_cleanup();
[52755f1]480 futex_cleanup();
481 LOG("Cleanup of task %" PRIu64" completed.", TASK->taskid);
[ea7890e7]482 }
483 }
484
[f761f1eb]485restart:
[22f7769]486 ipl = interrupts_disable();
[43114c5]487 spinlock_lock(&THREAD->lock);
[4e33b6b]488 if (THREAD->timeout_pending) {
489 /* busy waiting for timeouts in progress */
[43114c5]490 spinlock_unlock(&THREAD->lock);
[22f7769]491 interrupts_restore(ipl);
[f761f1eb]492 goto restart;
493 }
[ea7890e7]494
[43114c5]495 THREAD->state = Exiting;
496 spinlock_unlock(&THREAD->lock);
[f761f1eb]497 scheduler();
[874621f]498
499 /* Not reached */
500 while (1)
501 ;
[f761f1eb]502}
503
[70527f1]504
505/** Thread sleep
506 *
507 * Suspend execution of the current thread.
508 *
509 * @param sec Number of seconds to sleep.
510 *
511 */
[7f1c620]512void thread_sleep(uint32_t sec)
[f761f1eb]513{
[22e6802]514 /* Sleep in 1000 second steps to support
515 full argument range */
516 while (sec > 0) {
517 uint32_t period = (sec > 1000) ? 1000 : sec;
518
519 thread_usleep(period * 1000000);
520 sec -= period;
521 }
[f761f1eb]522}
[70527f1]523
[fe19611]524/** Wait for another thread to exit.
525 *
526 * @param t Thread to join on exit.
527 * @param usec Timeout in microseconds.
528 * @param flags Mode of operation.
529 *
530 * @return An error code from errno.h or an error code from synch.h.
531 */
[7f1c620]532int thread_join_timeout(thread_t *t, uint32_t usec, int flags)
[fe19611]533{
534 ipl_t ipl;
535 int rc;
536
537 if (t == THREAD)
538 return EINVAL;
539
540 /*
541 * Since thread join can only be called once on an undetached thread,
542 * the thread pointer is guaranteed to be still valid.
543 */
544
545 ipl = interrupts_disable();
546 spinlock_lock(&t->lock);
547 ASSERT(!t->detached);
548 spinlock_unlock(&t->lock);
[7b3e7f4]549 interrupts_restore(ipl);
[fe19611]550
[0182a665]551 rc = waitq_sleep_timeout(&t->join_wq, usec, flags);
552
[fe19611]553 return rc;
554}
555
556/** Detach thread.
557 *
[48d14222]558 * Mark the thread as detached, if the thread is already in the Lingering
559 * state, deallocate its resources.
[fe19611]560 *
561 * @param t Thread to be detached.
562 */
563void thread_detach(thread_t *t)
564{
565 ipl_t ipl;
566
567 /*
[31d8e10]568 * Since the thread is expected not to be already detached,
[fe19611]569 * pointer to it must be still valid.
570 */
571 ipl = interrupts_disable();
572 spinlock_lock(&t->lock);
573 ASSERT(!t->detached);
[48d14222]574 if (t->state == Lingering) {
[fe19611]575 thread_destroy(t); /* unlocks &t->lock */
576 interrupts_restore(ipl);
577 return;
578 } else {
579 t->detached = true;
580 }
581 spinlock_unlock(&t->lock);
582 interrupts_restore(ipl);
583}
584
[70527f1]585/** Thread usleep
586 *
587 * Suspend execution of the current thread.
588 *
589 * @param usec Number of microseconds to sleep.
590 *
591 */
[7f1c620]592void thread_usleep(uint32_t usec)
[f761f1eb]593{
594 waitq_t wq;
[22e6802]595
[f761f1eb]596 waitq_initialize(&wq);
[22e6802]597
[116d1ef4]598 (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING);
[f761f1eb]599}
600
[70527f1]601/** Register thread out-of-context invocation
602 *
603 * Register a function and its argument to be executed
604 * on next context switch to the current thread.
605 *
606 * @param call_me Out-of-context function.
607 * @param call_me_with Out-of-context function argument.
608 *
609 */
[f761f1eb]610void thread_register_call_me(void (* call_me)(void *), void *call_me_with)
611{
[22f7769]612 ipl_t ipl;
[f761f1eb]613
[22f7769]614 ipl = interrupts_disable();
[43114c5]615 spinlock_lock(&THREAD->lock);
616 THREAD->call_me = call_me;
617 THREAD->call_me_with = call_me_with;
618 spinlock_unlock(&THREAD->lock);
[22f7769]619 interrupts_restore(ipl);
[f761f1eb]620}
[55ab0f1]621
[b76a2217]622static bool thread_walker(avltree_node_t *node, void *arg)
[5dcee525]623{
[52755f1]624 thread_t *t = avltree_get_instance(node, thread_t, threads_tree_node);
625
[1ba37fa]626 uint64_t ucycles, kcycles;
627 char usuffix, ksuffix;
[e535eeb]628 order_suffix(t->ucycles, &ucycles, &usuffix);
629 order_suffix(t->kcycles, &kcycles, &ksuffix);
[52755f1]630
631#ifdef __32_BITS__
[07640dfd]632 printf("%-6" PRIu64" %-10s %10p %-8s %10p %-3" PRIu32 " %10p %10p %9"
633 PRIu64 "%c %9" PRIu64 "%c ", t->tid, t->name, t,
634 thread_states[t->state], t->task, t->task->context, t->thread_code,
635 t->kstack, ucycles, usuffix, kcycles, ksuffix);
[52755f1]636#endif
637
638#ifdef __64_BITS__
[07640dfd]639 printf("%-6" PRIu64" %-10s %18p %-8s %18p %-3" PRIu32 " %18p %18p %9"
640 PRIu64 "%c %9" PRIu64 "%c ", t->tid, t->name, t,
641 thread_states[t->state], t->task, t->task->context, t->thread_code,
642 t->kstack, ucycles, usuffix, kcycles, ksuffix);
[52755f1]643#endif
[5dcee525]644
645 if (t->cpu)
[52755f1]646 printf("%-4u", t->cpu->id);
[5dcee525]647 else
648 printf("none");
649
[43b1e86]650 if (t->state == Sleeping) {
[52755f1]651#ifdef __32_BITS__
652 printf(" %10p", t->sleep_queue);
653#endif
654
655#ifdef __64_BITS__
656 printf(" %18p", t->sleep_queue);
657#endif
[43b1e86]658 }
[5dcee525]659
660 printf("\n");
[b76a2217]661
662 return true;
[5dcee525]663}
664
[55ab0f1]665/** Print list of threads debug info */
666void thread_print_list(void)
667{
668 ipl_t ipl;
669
670 /* Messing with thread structures, avoid deadlock */
671 ipl = interrupts_disable();
672 spinlock_lock(&threads_lock);
[52755f1]673
674#ifdef __32_BITS__
675 printf("tid name address state task "
[07640dfd]676 "ctx code stack ucycles kcycles cpu "
[52755f1]677 "waitqueue\n");
678 printf("------ ---------- ---------- -------- ---------- "
[07640dfd]679 "--- ---------- ---------- ---------- ---------- ---- "
[52755f1]680 "----------\n");
681#endif
682
683#ifdef __64_BITS__
684 printf("tid name address state task "
[07640dfd]685 "ctx code stack ucycles kcycles cpu "
[52755f1]686 "waitqueue\n");
687 printf("------ ---------- ------------------ -------- ------------------ "
[07640dfd]688 "--- ------------------ ------------------ ---------- ---------- ---- "
[52755f1]689 "------------------\n");
690#endif
[55ab0f1]691
[b76a2217]692 avltree_walk(&threads_tree, thread_walker, NULL);
[55ab0f1]693
694 spinlock_unlock(&threads_lock);
[37c57f2]695 interrupts_restore(ipl);
[55ab0f1]696}
[9f52563]697
[016acbe]698/** Check whether thread exists.
699 *
700 * Note that threads_lock must be already held and
701 * interrupts must be already disabled.
702 *
703 * @param t Pointer to thread.
704 *
705 * @return True if thread t is known to the system, false otherwise.
706 */
707bool thread_exists(thread_t *t)
708{
[5dcee525]709 avltree_node_t *node;
710
711 node = avltree_search(&threads_tree, (avltree_key_t) ((uintptr_t) t));
[016acbe]712
[5dcee525]713 return node != NULL;
[016acbe]714}
715
[cce6acf]716/** Update accounting of current thread.
717 *
718 * Note that thread_lock on THREAD must be already held and
719 * interrupts must be already disabled.
720 *
[a2a00e8]721 * @param user True to update user accounting, false for kernel.
[cce6acf]722 */
[a2a00e8]723void thread_update_accounting(bool user)
[cce6acf]724{
725 uint64_t time = get_cycle();
[a2a00e8]726 if (user) {
727 THREAD->ucycles += time - THREAD->last_cycle;
728 } else {
729 THREAD->kcycles += time - THREAD->last_cycle;
730 }
[cce6acf]731 THREAD->last_cycle = time;
732}
733
[e1b6742]734static bool thread_search_walker(avltree_node_t *node, void *arg)
735{
736 thread_t *thread =
737 (thread_t *) avltree_get_instance(node, thread_t, threads_tree_node);
738 thread_iterator_t *iterator = (thread_iterator_t *) arg;
739
740 if (thread->tid == iterator->thread_id) {
741 iterator->thread = thread;
742 return false;
743 }
744
745 return true;
746}
747
748/** Find thread structure corresponding to thread ID.
749 *
750 * The threads_lock must be already held by the caller of this function and
751 * interrupts must be disabled.
752 *
753 * @param id Thread ID.
754 *
755 * @return Thread structure address or NULL if there is no such thread ID.
756 *
757 */
758thread_t *thread_find_by_id(thread_id_t thread_id)
759{
760 thread_iterator_t iterator;
761
762 iterator.thread_id = thread_id;
763 iterator.thread = NULL;
764
765 avltree_walk(&threads_tree, thread_search_walker, (void *) &iterator);
766
767 return iterator.thread;
768}
769
770
[9f52563]771/** Process syscall to create new thread.
772 *
773 */
[f6d2c81]774unative_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name,
[7faabb7]775 size_t name_len, thread_id_t *uspace_thread_id)
[9f52563]776{
[68091bd]777 thread_t *t;
778 char namebuf[THREAD_NAME_BUFLEN];
[45fb65c]779 uspace_arg_t *kernel_uarg;
[e3c762cd]780 int rc;
[9f52563]781
[24345a5]782 if (name_len > THREAD_NAME_BUFLEN - 1)
[7faabb7]783 name_len = THREAD_NAME_BUFLEN - 1;
784
785 rc = copy_from_uspace(namebuf, uspace_name, name_len);
[e3c762cd]786 if (rc != 0)
[7f1c620]787 return (unative_t) rc;
[0f250f9]788
[b60c582]789 namebuf[name_len] = 0;
[7faabb7]790
[4680ef5]791 /*
792 * In case of failure, kernel_uarg will be deallocated in this function.
793 * In case of success, kernel_uarg will be freed in uinit().
794 */
795 kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
796
[e3c762cd]797 rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
798 if (rc != 0) {
799 free(kernel_uarg);
[7f1c620]800 return (unative_t) rc;
[e3c762cd]801 }
[9f52563]802
[d8431986]803 t = thread_create(uinit, kernel_uarg, TASK,
804 THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf, false);
[6f4495f5]805 if (t) {
[d8431986]806 if (uspace_thread_id != NULL) {
807 int rc;
808
809 rc = copy_to_uspace(uspace_thread_id, &t->tid,
810 sizeof(t->tid));
811 if (rc != 0) {
812 /*
813 * We have encountered a failure, but the thread
814 * has already been created. We need to undo its
815 * creation now.
816 */
817
818 /*
[ea7890e7]819 * The new thread structure is initialized, but
820 * is still not visible to the system.
[d8431986]821 * We can safely deallocate it.
822 */
823 slab_free(thread_slab, t);
824 free(kernel_uarg);
825
826 return (unative_t) rc;
827 }
828 }
[9a1b20c]829#ifdef CONFIG_UDEBUG
[13964ef]830 /*
831 * Generate udebug THREAD_B event and attach the thread.
832 * This must be done atomically (with the debug locks held),
833 * otherwise we would either miss some thread or receive
834 * THREAD_B events for threads that already existed
835 * and could be detected with THREAD_READ before.
836 */
837 udebug_thread_b_event_attach(t, TASK);
838#else
839 thread_attach(t, TASK);
[9a1b20c]840#endif
[13964ef]841 thread_ready(t);
[9a1b20c]842
[d8431986]843 return 0;
[201abde]844 } else
[0f250f9]845 free(kernel_uarg);
[9f52563]846
[7f1c620]847 return (unative_t) ENOMEM;
[9f52563]848}
849
850/** Process syscall to terminate thread.
851 *
852 */
[7f1c620]853unative_t sys_thread_exit(int uspace_status)
[9f52563]854{
[68091bd]855 thread_exit();
856 /* Unreachable */
857 return 0;
[9f52563]858}
[b45c443]859
[3ce7f082]860/** Syscall for getting TID.
861 *
[201abde]862 * @param uspace_thread_id Userspace address of 8-byte buffer where to store
863 * current thread ID.
864 *
865 * @return 0 on success or an error code from @ref errno.h.
[b45c443]866 */
[201abde]867unative_t sys_thread_get_id(thread_id_t *uspace_thread_id)
[3ce7f082]868{
869 /*
870 * No need to acquire lock on THREAD because tid
871 * remains constant for the lifespan of the thread.
872 */
[201abde]873 return (unative_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
874 sizeof(THREAD->tid));
[3ce7f082]875}
[6f4495f5]876
[d9ece1cb]877/** Syscall wrapper for sleeping. */
878unative_t sys_thread_usleep(uint32_t usec)
879{
[22e6802]880 thread_usleep(usec);
[d9ece1cb]881 return 0;
882}
883
[3ce7f082]884/** @}
885 */
Note: See TracBrowser for help on using the repository browser.