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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8d6c1f1 was 8d6c1f1, checked in by Jakub Jermar <jakub@…>, 14 years ago

Merge USB support.

Changes from bzr://helenos-usb.bzr.sourceforge.net/bzrroot/helenos-usb/mainline:

  • replaced '-' with '_' in new driver names
  • USB libs are built for each architecture
  • devman starts early
  • sys_thread_udelay() uses generic delay()
  • sys_as_create_area() now creates cacheable areas by default
  • Property mode set to 100644
File size: 22.0 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
262 list_append(&thread->rq_link, &cpu->rq[i].rq_head);
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;
324 thread->state = Entering;
325
326 timeout_initialize(&thread->sleep_timeout);
327 thread->sleep_interruptible = false;
328 thread->sleep_queue = NULL;
329 thread->timeout_pending = false;
330
331 thread->in_copy_from_uspace = false;
332 thread->in_copy_to_uspace = false;
333
334 thread->interrupted = false;
335 thread->detached = false;
336 waitq_initialize(&thread->join_wq);
337
338 thread->task = task;
339
340 thread->fpu_context_exists = 0;
341 thread->fpu_context_engaged = 0;
342
343 avltree_node_initialize(&thread->threads_tree_node);
344 thread->threads_tree_node.key = (uintptr_t) thread;
[bb68433]345
[9a1b20c]346#ifdef CONFIG_UDEBUG
[5b7a107]347 /* Initialize debugging stuff */
348 thread->btrace = false;
[da1bafb]349 udebug_thread_initialize(&thread->udebug);
[9a1b20c]350#endif
[da1bafb]351
352 /* Might depend on previous initialization */
353 thread_create_arch(thread);
354
[d8431986]355 if (!(flags & THREAD_FLAG_NOATTACH))
[da1bafb]356 thread_attach(thread, task);
357
358 return thread;
[d8431986]359}
360
361/** Destroy thread memory structure
362 *
363 * Detach thread from all queues, cpus etc. and destroy it.
[da1bafb]364 *
365 * @param thread Thread to be destroyed.
366 * @param irq_res Indicate whether it should unlock thread->lock
367 * in interrupts-restore mode.
[d8431986]368 *
369 */
[da1bafb]370void thread_destroy(thread_t *thread, bool irq_res)
[d8431986]371{
[2d3ddad]372 ASSERT(irq_spinlock_locked(&thread->lock));
[da1bafb]373 ASSERT((thread->state == Exiting) || (thread->state == Lingering));
374 ASSERT(thread->task);
375 ASSERT(thread->cpu);
376
377 irq_spinlock_lock(&thread->cpu->lock, false);
378 if (thread->cpu->fpu_owner == thread)
379 thread->cpu->fpu_owner = NULL;
380 irq_spinlock_unlock(&thread->cpu->lock, false);
381
382 irq_spinlock_pass(&thread->lock, &threads_lock);
383
384 avltree_delete(&threads_tree, &thread->threads_tree_node);
385
386 irq_spinlock_pass(&threads_lock, &thread->task->lock);
387
[d8431986]388 /*
389 * Detach from the containing task.
390 */
[da1bafb]391 list_remove(&thread->th_link);
392 irq_spinlock_unlock(&thread->task->lock, irq_res);
393
[ea7890e7]394 /*
[7ed8530]395 * Drop the reference to the containing task.
[ea7890e7]396 */
[da1bafb]397 task_release(thread->task);
398 slab_free(thread_slab, thread);
[d8431986]399}
400
401/** Make the thread visible to the system.
402 *
403 * Attach the thread structure to the current task and make it visible in the
[5dcee525]404 * threads_tree.
[d8431986]405 *
[da1bafb]406 * @param t Thread to be attached to the task.
407 * @param task Task to which the thread is to be attached.
408 *
[d8431986]409 */
[da1bafb]410void thread_attach(thread_t *thread, task_t *task)
[d8431986]411{
412 /*
[9a1b20c]413 * Attach to the specified task.
[d8431986]414 */
[da1bafb]415 irq_spinlock_lock(&task->lock, true);
416
[7ed8530]417 /* Hold a reference to the task. */
418 task_hold(task);
[da1bafb]419
[9a1b20c]420 /* Must not count kbox thread into lifecount */
[da1bafb]421 if (thread->flags & THREAD_FLAG_USPACE)
[9a1b20c]422 atomic_inc(&task->lifecount);
[da1bafb]423
424 list_append(&thread->th_link, &task->th_head);
425
426 irq_spinlock_pass(&task->lock, &threads_lock);
427
[bb68433]428 /*
429 * Register this thread in the system-wide list.
430 */
[da1bafb]431 avltree_insert(&threads_tree, &thread->threads_tree_node);
432 irq_spinlock_unlock(&threads_lock, true);
[f761f1eb]433}
434
[0182a665]435/** Terminate thread.
[70527f1]436 *
[da1bafb]437 * End current thread execution and switch it to the exiting state.
438 * All pending timeouts are executed.
439 *
[70527f1]440 */
[f761f1eb]441void thread_exit(void)
442{
[9a1b20c]443 if (THREAD->flags & THREAD_FLAG_USPACE) {
444#ifdef CONFIG_UDEBUG
445 /* Generate udebug THREAD_E event */
446 udebug_thread_e_event();
[0ac99db]447
448 /*
449 * This thread will not execute any code or system calls from
450 * now on.
451 */
452 udebug_stoppable_begin();
[9a1b20c]453#endif
454 if (atomic_predec(&TASK->lifecount) == 0) {
455 /*
456 * We are the last userspace thread in the task that
457 * still has not exited. With the exception of the
458 * moment the task was created, new userspace threads
459 * can only be created by threads of the same task.
460 * We are safe to perform cleanup.
[da1bafb]461 *
[9a1b20c]462 */
[ea7890e7]463 ipc_cleanup();
[52755f1]464 futex_cleanup();
465 LOG("Cleanup of task %" PRIu64" completed.", TASK->taskid);
[ea7890e7]466 }
467 }
[da1bafb]468
[f761f1eb]469restart:
[da1bafb]470 irq_spinlock_lock(&THREAD->lock, true);
471 if (THREAD->timeout_pending) {
472 /* Busy waiting for timeouts in progress */
473 irq_spinlock_unlock(&THREAD->lock, true);
[f761f1eb]474 goto restart;
475 }
[ea7890e7]476
[43114c5]477 THREAD->state = Exiting;
[da1bafb]478 irq_spinlock_unlock(&THREAD->lock, true);
479
[f761f1eb]480 scheduler();
[da1bafb]481
[874621f]482 /* Not reached */
[da1bafb]483 while (true);
[f761f1eb]484}
485
[70527f1]486/** Thread sleep
487 *
488 * Suspend execution of the current thread.
489 *
490 * @param sec Number of seconds to sleep.
491 *
492 */
[7f1c620]493void thread_sleep(uint32_t sec)
[f761f1eb]494{
[22e6802]495 /* Sleep in 1000 second steps to support
496 full argument range */
497 while (sec > 0) {
498 uint32_t period = (sec > 1000) ? 1000 : sec;
[da1bafb]499
[22e6802]500 thread_usleep(period * 1000000);
501 sec -= period;
502 }
[f761f1eb]503}
[70527f1]504
[fe19611]505/** Wait for another thread to exit.
506 *
[da1bafb]507 * @param thread Thread to join on exit.
508 * @param usec Timeout in microseconds.
509 * @param flags Mode of operation.
[fe19611]510 *
511 * @return An error code from errno.h or an error code from synch.h.
[da1bafb]512 *
[fe19611]513 */
[da1bafb]514int thread_join_timeout(thread_t *thread, uint32_t usec, unsigned int flags)
[fe19611]515{
[da1bafb]516 if (thread == THREAD)
[fe19611]517 return EINVAL;
[da1bafb]518
[fe19611]519 /*
520 * Since thread join can only be called once on an undetached thread,
521 * the thread pointer is guaranteed to be still valid.
522 */
523
[da1bafb]524 irq_spinlock_lock(&thread->lock, true);
525 ASSERT(!thread->detached);
526 irq_spinlock_unlock(&thread->lock, true);
[0182a665]527
[da1bafb]528 return waitq_sleep_timeout(&thread->join_wq, usec, flags);
[fe19611]529}
530
531/** Detach thread.
532 *
[df58e44]533 * Mark the thread as detached. If the thread is already
534 * in the Lingering state, deallocate its resources.
[fe19611]535 *
[da1bafb]536 * @param thread Thread to be detached.
537 *
[fe19611]538 */
[da1bafb]539void thread_detach(thread_t *thread)
[fe19611]540{
541 /*
[31d8e10]542 * Since the thread is expected not to be already detached,
[fe19611]543 * pointer to it must be still valid.
544 */
[da1bafb]545 irq_spinlock_lock(&thread->lock, true);
546 ASSERT(!thread->detached);
547
548 if (thread->state == Lingering) {
549 /*
550 * Unlock &thread->lock and restore
551 * interrupts in thread_destroy().
552 */
553 thread_destroy(thread, true);
[fe19611]554 return;
555 } else {
[da1bafb]556 thread->detached = true;
[fe19611]557 }
[da1bafb]558
559 irq_spinlock_unlock(&thread->lock, true);
[fe19611]560}
561
[70527f1]562/** Thread usleep
563 *
564 * Suspend execution of the current thread.
565 *
566 * @param usec Number of microseconds to sleep.
567 *
568 */
[7f1c620]569void thread_usleep(uint32_t usec)
[f761f1eb]570{
571 waitq_t wq;
[22e6802]572
[f761f1eb]573 waitq_initialize(&wq);
[22e6802]574
[116d1ef4]575 (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING);
[f761f1eb]576}
577
[b76a2217]578static bool thread_walker(avltree_node_t *node, void *arg)
[5dcee525]579{
[48dcc69]580 bool *additional = (bool *) arg;
[da1bafb]581 thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
[52755f1]582
[1ba37fa]583 uint64_t ucycles, kcycles;
584 char usuffix, ksuffix;
[da1bafb]585 order_suffix(thread->ucycles, &ucycles, &usuffix);
586 order_suffix(thread->kcycles, &kcycles, &ksuffix);
587
[577f042a]588 char *name;
589 if (str_cmp(thread->name, "uinit") == 0)
590 name = thread->task->name;
591 else
592 name = thread->name;
593
[52755f1]594#ifdef __32_BITS__
[48dcc69]595 if (*additional)
[ae0300b5]596 printf("%-8" PRIu64 " %10p %10p %9" PRIu64 "%c %9" PRIu64 "%c ",
[577f042a]597 thread->tid, thread->thread_code, thread->kstack,
598 ucycles, usuffix, kcycles, ksuffix);
[48dcc69]599 else
[ae0300b5]600 printf("%-8" PRIu64 " %-14s %10p %-8s %10p %-5" PRIu32 "\n",
[577f042a]601 thread->tid, name, thread, thread_states[thread->state],
[26aafe8]602 thread->task, thread->task->container);
[52755f1]603#endif
[da1bafb]604
[52755f1]605#ifdef __64_BITS__
[48dcc69]606 if (*additional)
[ae0300b5]607 printf("%-8" PRIu64 " %18p %18p\n"
[48dcc69]608 " %9" PRIu64 "%c %9" PRIu64 "%c ",
609 thread->tid, thread->thread_code, thread->kstack,
610 ucycles, usuffix, kcycles, ksuffix);
[5dcee525]611 else
[ae0300b5]612 printf("%-8" PRIu64 " %-14s %18p %-8s %18p %-5" PRIu32 "\n",
[577f042a]613 thread->tid, name, thread, thread_states[thread->state],
[26aafe8]614 thread->task, thread->task->container);
[48dcc69]615#endif
[da1bafb]616
[48dcc69]617 if (*additional) {
618 if (thread->cpu)
619 printf("%-5u", thread->cpu->id);
620 else
621 printf("none ");
622
623 if (thread->state == Sleeping) {
[52755f1]624#ifdef __32_BITS__
[48dcc69]625 printf(" %10p", thread->sleep_queue);
[52755f1]626#endif
[48dcc69]627
[52755f1]628#ifdef __64_BITS__
[48dcc69]629 printf(" %18p", thread->sleep_queue);
[52755f1]630#endif
[48dcc69]631 }
632
633 printf("\n");
[43b1e86]634 }
[da1bafb]635
[b76a2217]636 return true;
[5dcee525]637}
638
[da1bafb]639/** Print list of threads debug info
[48dcc69]640 *
641 * @param additional Print additional information.
[da1bafb]642 *
643 */
[48dcc69]644void thread_print_list(bool additional)
[55ab0f1]645{
646 /* Messing with thread structures, avoid deadlock */
[da1bafb]647 irq_spinlock_lock(&threads_lock, true);
648
649#ifdef __32_BITS__
[48dcc69]650 if (additional)
[577f042a]651 printf("[id ] [code ] [stack ] [ucycles ] [kcycles ]"
652 " [cpu] [waitqueue]\n");
[48dcc69]653 else
654 printf("[id ] [name ] [address ] [state ] [task ]"
[26aafe8]655 " [ctn]\n");
[52755f1]656#endif
[da1bafb]657
[52755f1]658#ifdef __64_BITS__
[48dcc69]659 if (additional) {
660 printf("[id ] [code ] [stack ]\n"
661 " [ucycles ] [kcycles ] [cpu] [waitqueue ]\n");
662 } else
663 printf("[id ] [name ] [address ] [state ]"
[26aafe8]664 " [task ] [ctn]\n");
[52755f1]665#endif
[da1bafb]666
[48dcc69]667 avltree_walk(&threads_tree, thread_walker, &additional);
[da1bafb]668
669 irq_spinlock_unlock(&threads_lock, true);
[55ab0f1]670}
[9f52563]671
[016acbe]672/** Check whether thread exists.
673 *
674 * Note that threads_lock must be already held and
675 * interrupts must be already disabled.
676 *
[da1bafb]677 * @param thread Pointer to thread.
[016acbe]678 *
679 * @return True if thread t is known to the system, false otherwise.
[da1bafb]680 *
[016acbe]681 */
[da1bafb]682bool thread_exists(thread_t *thread)
[016acbe]683{
[1d432f9]684 ASSERT(interrupts_disabled());
685 ASSERT(irq_spinlock_locked(&threads_lock));
686
[da1bafb]687 avltree_node_t *node =
688 avltree_search(&threads_tree, (avltree_key_t) ((uintptr_t) thread));
[016acbe]689
[5dcee525]690 return node != NULL;
[016acbe]691}
692
[cce6acf]693/** Update accounting of current thread.
694 *
695 * Note that thread_lock on THREAD must be already held and
696 * interrupts must be already disabled.
697 *
[da1bafb]698 * @param user True to update user accounting, false for kernel.
699 *
[cce6acf]700 */
[a2a00e8]701void thread_update_accounting(bool user)
[cce6acf]702{
703 uint64_t time = get_cycle();
[1d432f9]704
705 ASSERT(interrupts_disabled());
706 ASSERT(irq_spinlock_locked(&THREAD->lock));
[da1bafb]707
708 if (user)
[a2a00e8]709 THREAD->ucycles += time - THREAD->last_cycle;
[da1bafb]710 else
[a2a00e8]711 THREAD->kcycles += time - THREAD->last_cycle;
[da1bafb]712
[cce6acf]713 THREAD->last_cycle = time;
714}
715
[e1b6742]716static bool thread_search_walker(avltree_node_t *node, void *arg)
717{
718 thread_t *thread =
719 (thread_t *) avltree_get_instance(node, thread_t, threads_tree_node);
720 thread_iterator_t *iterator = (thread_iterator_t *) arg;
721
722 if (thread->tid == iterator->thread_id) {
723 iterator->thread = thread;
724 return false;
725 }
726
727 return true;
728}
729
730/** Find thread structure corresponding to thread ID.
731 *
732 * The threads_lock must be already held by the caller of this function and
733 * interrupts must be disabled.
734 *
735 * @param id Thread ID.
736 *
737 * @return Thread structure address or NULL if there is no such thread ID.
738 *
739 */
740thread_t *thread_find_by_id(thread_id_t thread_id)
741{
[1d432f9]742 ASSERT(interrupts_disabled());
743 ASSERT(irq_spinlock_locked(&threads_lock));
[df58e44]744
[e1b6742]745 thread_iterator_t iterator;
746
747 iterator.thread_id = thread_id;
748 iterator.thread = NULL;
749
750 avltree_walk(&threads_tree, thread_search_walker, (void *) &iterator);
751
752 return iterator.thread;
753}
754
[5b7a107]755#ifdef CONFIG_UDEBUG
756
[df58e44]757void thread_stack_trace(thread_id_t thread_id)
758{
759 irq_spinlock_lock(&threads_lock, true);
760
761 thread_t *thread = thread_find_by_id(thread_id);
762 if (thread == NULL) {
763 printf("No such thread.\n");
764 irq_spinlock_unlock(&threads_lock, true);
765 return;
766 }
767
768 irq_spinlock_lock(&thread->lock, false);
769
770 /*
771 * Schedule a stack trace to be printed
772 * just before the thread is scheduled next.
773 *
774 * If the thread is sleeping then try to interrupt
775 * the sleep. Any request for printing an uspace stack
776 * trace from within the kernel should be always
777 * considered a last resort debugging means, therefore
778 * forcing the thread's sleep to be interrupted
779 * is probably justifiable.
780 */
781
782 bool sleeping = false;
783 istate_t *istate = thread->udebug.uspace_state;
784 if (istate != NULL) {
785 printf("Scheduling thread stack trace.\n");
786 thread->btrace = true;
787 if (thread->state == Sleeping)
788 sleeping = true;
789 } else
790 printf("Thread interrupt state not available.\n");
791
792 irq_spinlock_unlock(&thread->lock, false);
793
794 if (sleeping)
795 waitq_interrupt_sleep(thread);
796
797 irq_spinlock_unlock(&threads_lock, true);
798}
[e1b6742]799
[5b7a107]800#endif /* CONFIG_UDEBUG */
[e1b6742]801
[9f52563]802/** Process syscall to create new thread.
803 *
804 */
[96b02eb9]805sysarg_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name,
[7faabb7]806 size_t name_len, thread_id_t *uspace_thread_id)
[9f52563]807{
[24345a5]808 if (name_len > THREAD_NAME_BUFLEN - 1)
[7faabb7]809 name_len = THREAD_NAME_BUFLEN - 1;
[da1bafb]810
811 char namebuf[THREAD_NAME_BUFLEN];
812 int rc = copy_from_uspace(namebuf, uspace_name, name_len);
[e3c762cd]813 if (rc != 0)
[96b02eb9]814 return (sysarg_t) rc;
[da1bafb]815
[b60c582]816 namebuf[name_len] = 0;
[da1bafb]817
[4680ef5]818 /*
819 * In case of failure, kernel_uarg will be deallocated in this function.
820 * In case of success, kernel_uarg will be freed in uinit().
[da1bafb]821 *
[4680ef5]822 */
[da1bafb]823 uspace_arg_t *kernel_uarg =
824 (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
[4680ef5]825
[e3c762cd]826 rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
827 if (rc != 0) {
828 free(kernel_uarg);
[96b02eb9]829 return (sysarg_t) rc;
[e3c762cd]830 }
[da1bafb]831
832 thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
[d8431986]833 THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf, false);
[da1bafb]834 if (thread) {
[d8431986]835 if (uspace_thread_id != NULL) {
[da1bafb]836 rc = copy_to_uspace(uspace_thread_id, &thread->tid,
837 sizeof(thread->tid));
[d8431986]838 if (rc != 0) {
839 /*
840 * We have encountered a failure, but the thread
841 * has already been created. We need to undo its
842 * creation now.
843 */
[da1bafb]844
[d8431986]845 /*
[ea7890e7]846 * The new thread structure is initialized, but
847 * is still not visible to the system.
[d8431986]848 * We can safely deallocate it.
849 */
[da1bafb]850 slab_free(thread_slab, thread);
851 free(kernel_uarg);
852
[96b02eb9]853 return (sysarg_t) rc;
[d8431986]854 }
855 }
[da1bafb]856
[9a1b20c]857#ifdef CONFIG_UDEBUG
[13964ef]858 /*
859 * Generate udebug THREAD_B event and attach the thread.
860 * This must be done atomically (with the debug locks held),
861 * otherwise we would either miss some thread or receive
862 * THREAD_B events for threads that already existed
863 * and could be detected with THREAD_READ before.
864 */
[da1bafb]865 udebug_thread_b_event_attach(thread, TASK);
[13964ef]866#else
[da1bafb]867 thread_attach(thread, TASK);
[9a1b20c]868#endif
[da1bafb]869 thread_ready(thread);
870
[d8431986]871 return 0;
[201abde]872 } else
[0f250f9]873 free(kernel_uarg);
[da1bafb]874
[96b02eb9]875 return (sysarg_t) ENOMEM;
[9f52563]876}
877
878/** Process syscall to terminate thread.
879 *
880 */
[96b02eb9]881sysarg_t sys_thread_exit(int uspace_status)
[9f52563]882{
[68091bd]883 thread_exit();
[da1bafb]884
[68091bd]885 /* Unreachable */
886 return 0;
[9f52563]887}
[b45c443]888
[3ce7f082]889/** Syscall for getting TID.
890 *
[201abde]891 * @param uspace_thread_id Userspace address of 8-byte buffer where to store
892 * current thread ID.
893 *
894 * @return 0 on success or an error code from @ref errno.h.
[da1bafb]895 *
[b45c443]896 */
[96b02eb9]897sysarg_t sys_thread_get_id(thread_id_t *uspace_thread_id)
[3ce7f082]898{
899 /*
900 * No need to acquire lock on THREAD because tid
901 * remains constant for the lifespan of the thread.
[da1bafb]902 *
[3ce7f082]903 */
[96b02eb9]904 return (sysarg_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
[201abde]905 sizeof(THREAD->tid));
[3ce7f082]906}
[6f4495f5]907
[d9ece1cb]908/** Syscall wrapper for sleeping. */
[96b02eb9]909sysarg_t sys_thread_usleep(uint32_t usec)
[d9ece1cb]910{
[22e6802]911 thread_usleep(usec);
[d9ece1cb]912 return 0;
913}
914
[7e7b791]915sysarg_t sys_thread_udelay(uint32_t usec)
916{
[8d6c1f1]917 delay(usec);
[7e7b791]918 return 0;
919}
920
[3ce7f082]921/** @}
922 */
Note: See TracBrowser for help on using the repository browser.