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

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

prepare the public API of the frame allocator for the new backend
remove FRAME_KA (can be easily implemented explicitly)

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