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

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

Fix block comment formatting (ccheck).

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