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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 55b77d9 was 55b77d9, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Separate list_t typedef from link_t (kernel part).

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