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
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 <proc/scheduler.h>
39#include <proc/thread.h>
40#include <proc/task.h>
41#include <proc/uarg.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/synch.h>
48#include <synch/spinlock.h>
49#include <synch/waitq.h>
50#include <cpu.h>
51#include <str.h>
52#include <context.h>
53#include <adt/avl.h>
54#include <adt/list.h>
55#include <time/clock.h>
56#include <time/timeout.h>
57#include <time/delay.h>
58#include <config.h>
59#include <arch/interrupt.h>
60#include <smp/ipi.h>
61#include <arch/faddr.h>
62#include <atomic.h>
63#include <memstr.h>
64#include <print.h>
65#include <mm/slab.h>
66#include <debug.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_slab;
106
107#ifdef CONFIG_FPU
108slab_cache_t *fpu_context_slab;
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 int 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_slab, kmflags);
172 if (!thread->saved_fpu_context)
173 return -1;
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) {
179#ifdef CONFIG_FPU
180 if (thread->saved_fpu_context)
181 slab_free(fpu_context_slab, thread->saved_fpu_context);
182#endif
183 return -1;
184 }
185
186#ifdef CONFIG_UDEBUG
187 mutex_initialize(&thread->udebug.lock, MUTEX_PASSIVE);
188#endif
189
190 return 0;
191}
192
193/** Destruction of thread_t object */
194static size_t thr_destructor(void *obj)
195{
196 thread_t *thread = (thread_t *) obj;
197
198 /* call the architecture-specific part of the destructor */
199 thr_destructor_arch(thread);
200
201 frame_free(KA2PA(thread->kstack));
202
203#ifdef CONFIG_FPU
204 if (thread->saved_fpu_context)
205 slab_free(fpu_context_slab, thread->saved_fpu_context);
206#endif
207
208 return 1; /* One page freed */
209}
210
211/** Initialize threads
212 *
213 * Initialize kernel threads support.
214 *
215 */
216void thread_init(void)
217{
218 THREAD = NULL;
219
220 atomic_set(&nrdy, 0);
221 thread_slab = slab_cache_create("thread_slab", sizeof(thread_t), 0,
222 thr_constructor, thr_destructor, 0);
223
224#ifdef CONFIG_FPU
225 fpu_context_slab = slab_cache_create("fpu_slab", sizeof(fpu_context_t),
226 FPU_CONTEXT_ALIGN, NULL, NULL, 0);
227#endif
228
229 avltree_create(&threads_tree);
230}
231
232/** Make thread ready
233 *
234 * Switch thread to the ready state.
235 *
236 * @param thread Thread to make ready.
237 *
238 */
239void thread_ready(thread_t *thread)
240{
241 irq_spinlock_lock(&thread->lock, true);
242
243 ASSERT(thread->state != Ready);
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;
252 }
253 thread->state = Ready;
254
255 irq_spinlock_pass(&thread->lock, &(cpu->rq[i].lock));
256
257 /*
258 * Append thread to respective ready queue
259 * on respective processor.
260 */
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
266 atomic_inc(&nrdy);
267 // FIXME: Why is the avg value not used
268 // avg = atomic_get(&nrdy) / config.cpu_active;
269 atomic_inc(&cpu->nrdy);
270}
271
272/** Create new thread
273 *
274 * Create a new thread.
275 *
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.
285 *
286 * @return New thread's structure on success, NULL on failure.
287 *
288 */
289thread_t *thread_create(void (* func)(void *), void *arg, task_t *task,
290 unsigned int flags, const char *name, bool uncounted)
291{
292 thread_t *thread = (thread_t *) slab_alloc(thread_slab, 0);
293 if (!thread)
294 return NULL;
295
296 /* Not needed, but good for debugging */
297 memsetb(thread->kstack, STACK_SIZE, 0);
298
299 irq_spinlock_lock(&tidlock, true);
300 thread->tid = ++last_tid;
301 irq_spinlock_unlock(&tidlock, true);
302
303 context_save(&thread->saved_context);
304 context_set(&thread->saved_context, FADDR(cushion),
305 (uintptr_t) thread->kstack, STACK_SIZE);
306
307 the_initialize((the_t *) thread->kstack);
308
309 ipl_t ipl = interrupts_disable();
310 thread->saved_context.ipl = interrupts_read();
311 interrupts_restore(ipl);
312
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;
345
346#ifdef CONFIG_UDEBUG
347 /* Initialize debugging stuff */
348 thread->btrace = false;
349 udebug_thread_initialize(&thread->udebug);
350#endif
351
352 /* Might depend on previous initialization */
353 thread_create_arch(thread);
354
355 if (!(flags & THREAD_FLAG_NOATTACH))
356 thread_attach(thread, task);
357
358 return thread;
359}
360
361/** Destroy thread memory structure
362 *
363 * Detach thread from all queues, cpus etc. and destroy it.
364 *
365 * @param thread Thread to be destroyed.
366 * @param irq_res Indicate whether it should unlock thread->lock
367 * in interrupts-restore mode.
368 *
369 */
370void thread_destroy(thread_t *thread, bool irq_res)
371{
372 ASSERT(irq_spinlock_locked(&thread->lock));
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
388 /*
389 * Detach from the containing task.
390 */
391 list_remove(&thread->th_link);
392 irq_spinlock_unlock(&thread->task->lock, irq_res);
393
394 /*
395 * Drop the reference to the containing task.
396 */
397 task_release(thread->task);
398 slab_free(thread_slab, thread);
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
404 * threads_tree.
405 *
406 * @param t Thread to be attached to the task.
407 * @param task Task to which the thread is to be attached.
408 *
409 */
410void thread_attach(thread_t *thread, task_t *task)
411{
412 /*
413 * Attach to the specified task.
414 */
415 irq_spinlock_lock(&task->lock, true);
416
417 /* Hold a reference to the task. */
418 task_hold(task);
419
420 /* Must not count kbox thread into lifecount */
421 if (thread->flags & THREAD_FLAG_USPACE)
422 atomic_inc(&task->lifecount);
423
424 list_append(&thread->th_link, &task->th_head);
425
426 irq_spinlock_pass(&task->lock, &threads_lock);
427
428 /*
429 * Register this thread in the system-wide list.
430 */
431 avltree_insert(&threads_tree, &thread->threads_tree_node);
432 irq_spinlock_unlock(&threads_lock, true);
433}
434
435/** Terminate thread.
436 *
437 * End current thread execution and switch it to the exiting state.
438 * All pending timeouts are executed.
439 *
440 */
441void thread_exit(void)
442{
443 if (THREAD->flags & THREAD_FLAG_USPACE) {
444#ifdef CONFIG_UDEBUG
445 /* Generate udebug THREAD_E event */
446 udebug_thread_e_event();
447
448 /*
449 * This thread will not execute any code or system calls from
450 * now on.
451 */
452 udebug_stoppable_begin();
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.
461 *
462 */
463 ipc_cleanup();
464 futex_cleanup();
465 LOG("Cleanup of task %" PRIu64" completed.", TASK->taskid);
466 }
467 }
468
469restart:
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);
474 goto restart;
475 }
476
477 THREAD->state = Exiting;
478 irq_spinlock_unlock(&THREAD->lock, true);
479
480 scheduler();
481
482 /* Not reached */
483 while (true);
484}
485
486/** Thread sleep
487 *
488 * Suspend execution of the current thread.
489 *
490 * @param sec Number of seconds to sleep.
491 *
492 */
493void thread_sleep(uint32_t sec)
494{
495 /* Sleep in 1000 second steps to support
496 full argument range */
497 while (sec > 0) {
498 uint32_t period = (sec > 1000) ? 1000 : sec;
499
500 thread_usleep(period * 1000000);
501 sec -= period;
502 }
503}
504
505/** Wait for another thread to exit.
506 *
507 * @param thread Thread to join on exit.
508 * @param usec Timeout in microseconds.
509 * @param flags Mode of operation.
510 *
511 * @return An error code from errno.h or an error code from synch.h.
512 *
513 */
514int thread_join_timeout(thread_t *thread, uint32_t usec, unsigned int flags)
515{
516 if (thread == THREAD)
517 return EINVAL;
518
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
524 irq_spinlock_lock(&thread->lock, true);
525 ASSERT(!thread->detached);
526 irq_spinlock_unlock(&thread->lock, true);
527
528 return waitq_sleep_timeout(&thread->join_wq, usec, flags);
529}
530
531/** Detach thread.
532 *
533 * Mark the thread as detached. If the thread is already
534 * in the Lingering state, deallocate its resources.
535 *
536 * @param thread Thread to be detached.
537 *
538 */
539void thread_detach(thread_t *thread)
540{
541 /*
542 * Since the thread is expected not to be already detached,
543 * pointer to it must be still valid.
544 */
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);
554 return;
555 } else {
556 thread->detached = true;
557 }
558
559 irq_spinlock_unlock(&thread->lock, true);
560}
561
562/** Thread usleep
563 *
564 * Suspend execution of the current thread.
565 *
566 * @param usec Number of microseconds to sleep.
567 *
568 */
569void thread_usleep(uint32_t usec)
570{
571 waitq_t wq;
572
573 waitq_initialize(&wq);
574
575 (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING);
576}
577
578static bool thread_walker(avltree_node_t *node, void *arg)
579{
580 bool *additional = (bool *) arg;
581 thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
582
583 uint64_t ucycles, kcycles;
584 char usuffix, ksuffix;
585 order_suffix(thread->ucycles, &ucycles, &usuffix);
586 order_suffix(thread->kcycles, &kcycles, &ksuffix);
587
588 char *name;
589 if (str_cmp(thread->name, "uinit") == 0)
590 name = thread->task->name;
591 else
592 name = thread->name;
593
594#ifdef __32_BITS__
595 if (*additional)
596 printf("%-8" PRIu64 " %10p %10p %9" PRIu64 "%c %9" PRIu64 "%c ",
597 thread->tid, thread->thread_code, thread->kstack,
598 ucycles, usuffix, kcycles, ksuffix);
599 else
600 printf("%-8" PRIu64 " %-14s %10p %-8s %10p %-5" PRIu32 "\n",
601 thread->tid, name, thread, thread_states[thread->state],
602 thread->task, thread->task->container);
603#endif
604
605#ifdef __64_BITS__
606 if (*additional)
607 printf("%-8" PRIu64 " %18p %18p\n"
608 " %9" PRIu64 "%c %9" PRIu64 "%c ",
609 thread->tid, thread->thread_code, thread->kstack,
610 ucycles, usuffix, kcycles, ksuffix);
611 else
612 printf("%-8" PRIu64 " %-14s %18p %-8s %18p %-5" PRIu32 "\n",
613 thread->tid, name, thread, thread_states[thread->state],
614 thread->task, thread->task->container);
615#endif
616
617 if (*additional) {
618 if (thread->cpu)
619 printf("%-5u", thread->cpu->id);
620 else
621 printf("none ");
622
623 if (thread->state == Sleeping) {
624#ifdef __32_BITS__
625 printf(" %10p", thread->sleep_queue);
626#endif
627
628#ifdef __64_BITS__
629 printf(" %18p", thread->sleep_queue);
630#endif
631 }
632
633 printf("\n");
634 }
635
636 return true;
637}
638
639/** Print list of threads debug info
640 *
641 * @param additional Print additional information.
642 *
643 */
644void thread_print_list(bool additional)
645{
646 /* Messing with thread structures, avoid deadlock */
647 irq_spinlock_lock(&threads_lock, true);
648
649#ifdef __32_BITS__
650 if (additional)
651 printf("[id ] [code ] [stack ] [ucycles ] [kcycles ]"
652 " [cpu] [waitqueue]\n");
653 else
654 printf("[id ] [name ] [address ] [state ] [task ]"
655 " [ctn]\n");
656#endif
657
658#ifdef __64_BITS__
659 if (additional) {
660 printf("[id ] [code ] [stack ]\n"
661 " [ucycles ] [kcycles ] [cpu] [waitqueue ]\n");
662 } else
663 printf("[id ] [name ] [address ] [state ]"
664 " [task ] [ctn]\n");
665#endif
666
667 avltree_walk(&threads_tree, thread_walker, &additional);
668
669 irq_spinlock_unlock(&threads_lock, true);
670}
671
672/** Check whether thread exists.
673 *
674 * Note that threads_lock must be already held and
675 * interrupts must be already disabled.
676 *
677 * @param thread Pointer to thread.
678 *
679 * @return True if thread t is known to the system, false otherwise.
680 *
681 */
682bool thread_exists(thread_t *thread)
683{
684 ASSERT(interrupts_disabled());
685 ASSERT(irq_spinlock_locked(&threads_lock));
686
687 avltree_node_t *node =
688 avltree_search(&threads_tree, (avltree_key_t) ((uintptr_t) thread));
689
690 return node != NULL;
691}
692
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 *
698 * @param user True to update user accounting, false for kernel.
699 *
700 */
701void thread_update_accounting(bool user)
702{
703 uint64_t time = get_cycle();
704
705 ASSERT(interrupts_disabled());
706 ASSERT(irq_spinlock_locked(&THREAD->lock));
707
708 if (user)
709 THREAD->ucycles += time - THREAD->last_cycle;
710 else
711 THREAD->kcycles += time - THREAD->last_cycle;
712
713 THREAD->last_cycle = time;
714}
715
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{
742 ASSERT(interrupts_disabled());
743 ASSERT(irq_spinlock_locked(&threads_lock));
744
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
755#ifdef CONFIG_UDEBUG
756
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}
799
800#endif /* CONFIG_UDEBUG */
801
802/** Process syscall to create new thread.
803 *
804 */
805sysarg_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name,
806 size_t name_len, thread_id_t *uspace_thread_id)
807{
808 if (name_len > THREAD_NAME_BUFLEN - 1)
809 name_len = THREAD_NAME_BUFLEN - 1;
810
811 char namebuf[THREAD_NAME_BUFLEN];
812 int rc = copy_from_uspace(namebuf, uspace_name, name_len);
813 if (rc != 0)
814 return (sysarg_t) rc;
815
816 namebuf[name_len] = 0;
817
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().
821 *
822 */
823 uspace_arg_t *kernel_uarg =
824 (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
825
826 rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
827 if (rc != 0) {
828 free(kernel_uarg);
829 return (sysarg_t) rc;
830 }
831
832 thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
833 THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf, false);
834 if (thread) {
835 if (uspace_thread_id != NULL) {
836 rc = copy_to_uspace(uspace_thread_id, &thread->tid,
837 sizeof(thread->tid));
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 */
844
845 /*
846 * The new thread structure is initialized, but
847 * is still not visible to the system.
848 * We can safely deallocate it.
849 */
850 slab_free(thread_slab, thread);
851 free(kernel_uarg);
852
853 return (sysarg_t) rc;
854 }
855 }
856
857#ifdef CONFIG_UDEBUG
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 */
865 udebug_thread_b_event_attach(thread, TASK);
866#else
867 thread_attach(thread, TASK);
868#endif
869 thread_ready(thread);
870
871 return 0;
872 } else
873 free(kernel_uarg);
874
875 return (sysarg_t) ENOMEM;
876}
877
878/** Process syscall to terminate thread.
879 *
880 */
881sysarg_t sys_thread_exit(int uspace_status)
882{
883 thread_exit();
884
885 /* Unreachable */
886 return 0;
887}
888
889/** Syscall for getting TID.
890 *
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.
895 *
896 */
897sysarg_t sys_thread_get_id(thread_id_t *uspace_thread_id)
898{
899 /*
900 * No need to acquire lock on THREAD because tid
901 * remains constant for the lifespan of the thread.
902 *
903 */
904 return (sysarg_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
905 sizeof(THREAD->tid));
906}
907
908/** Syscall wrapper for sleeping. */
909sysarg_t sys_thread_usleep(uint32_t usec)
910{
911 thread_usleep(usec);
912 return 0;
913}
914
915sysarg_t sys_thread_udelay(uint32_t usec)
916{
917 delay(usec);
918 return 0;
919}
920
921/** @}
922 */
Note: See TracBrowser for help on using the repository browser.