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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a2a00e8 was a2a00e8, checked in by Stanislav Kozina <stanislav.kozina@…>, 15 years ago

Accounting separated to kernel and user time.

  • Property mode set to 100644
File size: 19.6 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 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
[9179d0a]35 * @brief Thread management functions.
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 <synch/rwlock.h>
51#include <cpu.h>
52#include <func.h>
53#include <context.h>
[5dcee525]54#include <adt/avl.h>
[5c9a08b]55#include <adt/list.h>
[f761f1eb]56#include <time/clock.h>
[b3f8fb7]57#include <time/timeout.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
71
72#ifndef LOADED_PROG_STACK_PAGES_NO
73#define LOADED_PROG_STACK_PAGES_NO 1
74#endif
[f761f1eb]75
[fe19611]76
77/** Thread states */
[a000878c]78const char *thread_states[] = {
[fe19611]79 "Invalid",
80 "Running",
81 "Sleeping",
82 "Ready",
83 "Entering",
84 "Exiting",
[48d14222]85 "Lingering"
[fe19611]86};
[f761f1eb]87
[5dcee525]88/** Lock protecting the threads_tree AVL tree.
[4e33b6b]89 *
90 * For locking rules, see declaration thereof.
91 */
[016acbe]92SPINLOCK_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.
[88169d9]98 */
[5dcee525]99avltree_t threads_tree;
[f761f1eb]100
[dc747e3]101SPINLOCK_INITIALIZE(tidlock);
[201abde]102thread_id_t last_tid = 0;
[f761f1eb]103
[266294a9]104static slab_cache_t *thread_slab;
[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();
[f761f1eb]122
[0313ff0]123 /* This is where each thread wakes up after its creation */
[43114c5]124 spinlock_unlock(&THREAD->lock);
[22f7769]125 interrupts_enable();
[f761f1eb]126
127 f(arg);
[0313ff0]128
129 /* Accumulate accounting to the task */
130 ipl_t ipl = interrupts_disable();
131
132 spinlock_lock(&THREAD->lock);
[62b6d17]133 if (!THREAD->uncounted) {
[a2a00e8]134 thread_update_accounting(true);
[62b6d17]135 uint64_t cycles = THREAD->cycles;
136 THREAD->cycles = 0;
[a2a00e8]137 uint64_t ucycles = THREAD->ucycles;
138 THREAD->ucycles = 0;
139 uint64_t kcycles = THREAD->kcycles;
140 THREAD->kcycles = 0;
141
[62b6d17]142 spinlock_unlock(&THREAD->lock);
143
144 spinlock_lock(&TASK->lock);
145 TASK->cycles += cycles;
[a2a00e8]146 TASK->ucycles += ucycles;
147 TASK->kcycles += kcycles;
[62b6d17]148 spinlock_unlock(&TASK->lock);
149 } else
150 spinlock_unlock(&THREAD->lock);
[0313ff0]151
152 interrupts_restore(ipl);
153
[f761f1eb]154 thread_exit();
155 /* not reached */
156}
157
[266294a9]158/** Initialization and allocation for thread_t structure */
159static int thr_constructor(void *obj, int kmflags)
160{
[764c302]161 thread_t *t = (thread_t *) obj;
[266294a9]162
163 spinlock_initialize(&t->lock, "thread_t_lock");
164 link_initialize(&t->rq_link);
165 link_initialize(&t->wq_link);
166 link_initialize(&t->th_link);
[32fffef0]167
168 /* call the architecture-specific part of the constructor */
169 thr_constructor_arch(t);
[266294a9]170
[0f81ceb7]171#ifdef CONFIG_FPU
[d8431986]172#ifdef CONFIG_FPU_LAZY
[f76fed4]173 t->saved_fpu_context = NULL;
[d8431986]174#else
175 t->saved_fpu_context = slab_alloc(fpu_context_slab, kmflags);
[f76fed4]176 if (!t->saved_fpu_context)
177 return -1;
[d8431986]178#endif
[0f81ceb7]179#endif
[f76fed4]180
[4184e76]181 t->kstack = (uint8_t *) frame_alloc(STACK_FRAMES, FRAME_KA | kmflags);
[d8431986]182 if (!t->kstack) {
[0f81ceb7]183#ifdef CONFIG_FPU
[f76fed4]184 if (t->saved_fpu_context)
[d8431986]185 slab_free(fpu_context_slab, t->saved_fpu_context);
[f76fed4]186#endif
[266294a9]187 return -1;
[f76fed4]188 }
[266294a9]189
[9a1b20c]190#ifdef CONFIG_UDEBUG
191 mutex_initialize(&t->udebug.lock, MUTEX_PASSIVE);
192#endif
193
[266294a9]194 return 0;
195}
196
197/** Destruction of thread_t object */
198static int thr_destructor(void *obj)
199{
[764c302]200 thread_t *t = (thread_t *) obj;
[266294a9]201
[32fffef0]202 /* call the architecture-specific part of the destructor */
203 thr_destructor_arch(t);
204
[2e9eae2]205 frame_free(KA2PA(t->kstack));
[0f81ceb7]206#ifdef CONFIG_FPU
[f76fed4]207 if (t->saved_fpu_context)
[d8431986]208 slab_free(fpu_context_slab, t->saved_fpu_context);
[f76fed4]209#endif
[266294a9]210 return 1; /* One page freed */
211}
[70527f1]212
213/** Initialize threads
214 *
215 * Initialize kernel threads support.
216 *
217 */
[f761f1eb]218void thread_init(void)
219{
[43114c5]220 THREAD = NULL;
[7217199]221 atomic_set(&nrdy, 0);
[4e33b6b]222 thread_slab = slab_cache_create("thread_slab", sizeof(thread_t), 0,
[6f4495f5]223 thr_constructor, thr_destructor, 0);
[4e33b6b]224
[0f81ceb7]225#ifdef CONFIG_FPU
[4e33b6b]226 fpu_context_slab = slab_cache_create("fpu_slab", sizeof(fpu_context_t),
[6f4495f5]227 FPU_CONTEXT_ALIGN, NULL, NULL, 0);
[f76fed4]228#endif
[f761f1eb]229
[5dcee525]230 avltree_create(&threads_tree);
[016acbe]231}
[70527f1]232
233/** Make thread ready
234 *
235 * Switch thread t to the ready state.
236 *
237 * @param t Thread to make ready.
238 *
239 */
[f761f1eb]240void thread_ready(thread_t *t)
241{
242 cpu_t *cpu;
243 runq_t *r;
[22f7769]244 ipl_t ipl;
[80d2bdb]245 int i, avg;
[f761f1eb]246
[22f7769]247 ipl = interrupts_disable();
[f761f1eb]248
249 spinlock_lock(&t->lock);
250
[d8431986]251 ASSERT(!(t->state == Ready));
[fbcfd458]252
[4e33b6b]253 i = (t->priority < RQ_COUNT - 1) ? ++t->priority : t->priority;
[f761f1eb]254
[8262010]255 cpu = CPU;
[32fffef0]256 if (t->flags & THREAD_FLAG_WIRED) {
[4365d10]257 ASSERT(t->cpu != NULL);
[f761f1eb]258 cpu = t->cpu;
259 }
[81c4c6da]260 t->state = Ready;
[f761f1eb]261 spinlock_unlock(&t->lock);
262
[70527f1]263 /*
[f761f1eb]264 * Append t to respective ready queue on respective processor.
265 */
266 r = &cpu->rq[i];
267 spinlock_lock(&r->lock);
268 list_append(&t->rq_link, &r->rq_head);
269 r->n++;
270 spinlock_unlock(&r->lock);
271
[59e07c91]272 atomic_inc(&nrdy);
[137691a]273 // FIXME: Why is the avg value never read?
[80d2bdb]274 avg = atomic_get(&nrdy) / config.cpu_active;
[248fc1a]275 atomic_inc(&cpu->nrdy);
[76cec1e]276
[22f7769]277 interrupts_restore(ipl);
[f761f1eb]278}
279
[70527f1]280/** Create new thread
281 *
282 * Create a new thread.
283 *
[5d12283]284 * @param func Thread's implementing function.
285 * @param arg Thread's implementing function argument.
286 * @param task Task to which the thread belongs. The caller must
287 * guarantee that the task won't cease to exist during the
288 * call. The task's lock may not be held.
289 * @param flags Thread flags.
[24345a5]290 * @param name Symbolic name (a copy is made).
[5d12283]291 * @param uncounted Thread's accounting doesn't affect accumulated task
292 * accounting.
[70527f1]293 *
[5d12283]294 * @return New thread's structure on success, NULL on failure.
[70527f1]295 *
296 */
[4e33b6b]297thread_t *thread_create(void (* func)(void *), void *arg, task_t *task,
[a000878c]298 int flags, const char *name, bool uncounted)
[f761f1eb]299{
300 thread_t *t;
[bb68433]301 ipl_t ipl;
302
[266294a9]303 t = (thread_t *) slab_alloc(thread_slab, 0);
[2a46e10]304 if (!t)
305 return NULL;
[aa8d0f7]306
[bb68433]307 /* Not needed, but good for debugging */
[e32e092]308 memsetb(t->kstack, THREAD_STACK_SIZE * 1 << STACK_FRAMES, 0);
[bb68433]309
310 ipl = interrupts_disable();
311 spinlock_lock(&tidlock);
312 t->tid = ++last_tid;
313 spinlock_unlock(&tidlock);
314 interrupts_restore(ipl);
315
316 context_save(&t->saved_context);
[4e33b6b]317 context_set(&t->saved_context, FADDR(cushion), (uintptr_t) t->kstack,
[6f4495f5]318 THREAD_STACK_SIZE);
[bb68433]319
320 the_initialize((the_t *) t->kstack);
321
322 ipl = interrupts_disable();
323 t->saved_context.ipl = interrupts_read();
324 interrupts_restore(ipl);
325
[9f52563]326 memcpy(t->name, name, THREAD_NAME_BUFLEN);
[b60c582]327 t->name[THREAD_NAME_BUFLEN - 1] = 0;
[9f52563]328
[bb68433]329 t->thread_code = func;
330 t->thread_arg = arg;
331 t->ticks = -1;
[cce6acf]332 t->cycles = 0;
[a2a00e8]333 t->ucycles = 0;
334 t->kcycles = 0;
[62b6d17]335 t->uncounted = uncounted;
[bb68433]336 t->priority = -1; /* start in rq[0] */
337 t->cpu = NULL;
[32fffef0]338 t->flags = flags;
[bb68433]339 t->state = Entering;
340 t->call_me = NULL;
341 t->call_me_with = NULL;
342
343 timeout_initialize(&t->sleep_timeout);
[116d1ef4]344 t->sleep_interruptible = false;
[bb68433]345 t->sleep_queue = NULL;
346 t->timeout_pending = 0;
[e3c762cd]347
348 t->in_copy_from_uspace = false;
349 t->in_copy_to_uspace = false;
[7509ddc]350
351 t->interrupted = false;
[fe19611]352 t->detached = false;
353 waitq_initialize(&t->join_wq);
354
[bb68433]355 t->rwlock_holder_type = RWLOCK_NONE;
[6a27d63]356
[bb68433]357 t->task = task;
358
[6f8a426]359 t->fpu_context_exists = 0;
360 t->fpu_context_engaged = 0;
[32fffef0]361
[5dcee525]362 avltree_node_initialize(&t->threads_tree_node);
363 t->threads_tree_node.key = (uintptr_t) t;
364
[9a1b20c]365#ifdef CONFIG_UDEBUG
366 /* Init debugging stuff */
367 udebug_thread_initialize(&t->udebug);
368#endif
369
[4e33b6b]370 /* might depend on previous initialization */
371 thread_create_arch(t);
[d8431986]372
373 if (!(flags & THREAD_FLAG_NOATTACH))
374 thread_attach(t, task);
375
376 return t;
377}
378
379/** Destroy thread memory structure
380 *
381 * Detach thread from all queues, cpus etc. and destroy it.
382 *
383 * Assume thread->lock is held!!
384 */
385void thread_destroy(thread_t *t)
386{
[48d14222]387 ASSERT(t->state == Exiting || t->state == Lingering);
[d8431986]388 ASSERT(t->task);
389 ASSERT(t->cpu);
390
391 spinlock_lock(&t->cpu->lock);
392 if (t->cpu->fpu_owner == t)
393 t->cpu->fpu_owner = NULL;
394 spinlock_unlock(&t->cpu->lock);
395
396 spinlock_unlock(&t->lock);
397
398 spinlock_lock(&threads_lock);
[5dcee525]399 avltree_delete(&threads_tree, &t->threads_tree_node);
[d8431986]400 spinlock_unlock(&threads_lock);
401
402 /*
403 * Detach from the containing task.
404 */
405 spinlock_lock(&t->task->lock);
406 list_remove(&t->th_link);
407 spinlock_unlock(&t->task->lock);
[ea7890e7]408
409 /*
410 * t is guaranteed to be the very last thread of its task.
411 * It is safe to destroy the task.
412 */
413 if (atomic_predec(&t->task->refcount) == 0)
[d8431986]414 task_destroy(t->task);
415
416 slab_free(thread_slab, t);
417}
418
419/** Make the thread visible to the system.
420 *
421 * Attach the thread structure to the current task and make it visible in the
[5dcee525]422 * threads_tree.
[d8431986]423 *
424 * @param t Thread to be attached to the task.
425 * @param task Task to which the thread is to be attached.
426 */
427void thread_attach(thread_t *t, task_t *task)
428{
429 ipl_t ipl;
430
431 /*
[9a1b20c]432 * Attach to the specified task.
[d8431986]433 */
[ea7890e7]434 ipl = interrupts_disable();
[d8431986]435 spinlock_lock(&task->lock);
[9a1b20c]436
[ea7890e7]437 atomic_inc(&task->refcount);
[9a1b20c]438
439 /* Must not count kbox thread into lifecount */
440 if (t->flags & THREAD_FLAG_USPACE)
441 atomic_inc(&task->lifecount);
442
[7509ddc]443 list_append(&t->th_link, &task->th_head);
444 spinlock_unlock(&task->lock);
445
[bb68433]446 /*
447 * Register this thread in the system-wide list.
448 */
449 spinlock_lock(&threads_lock);
[5dcee525]450 avltree_insert(&threads_tree, &t->threads_tree_node);
[bb68433]451 spinlock_unlock(&threads_lock);
452
453 interrupts_restore(ipl);
[f761f1eb]454}
455
[0182a665]456/** Terminate thread.
[70527f1]457 *
[4e33b6b]458 * End current thread execution and switch it to the exiting state. All pending
459 * timeouts are executed.
[70527f1]460 */
[f761f1eb]461void thread_exit(void)
462{
[22f7769]463 ipl_t ipl;
[f761f1eb]464
[9a1b20c]465 if (THREAD->flags & THREAD_FLAG_USPACE) {
466#ifdef CONFIG_UDEBUG
467 /* Generate udebug THREAD_E event */
468 udebug_thread_e_event();
469#endif
470 if (atomic_predec(&TASK->lifecount) == 0) {
471 /*
472 * We are the last userspace thread in the task that
473 * still has not exited. With the exception of the
474 * moment the task was created, new userspace threads
475 * can only be created by threads of the same task.
476 * We are safe to perform cleanup.
477 */
[ea7890e7]478 ipc_cleanup();
[52755f1]479 futex_cleanup();
480 LOG("Cleanup of task %" PRIu64" completed.", TASK->taskid);
[ea7890e7]481 }
482 }
483
[f761f1eb]484restart:
[22f7769]485 ipl = interrupts_disable();
[43114c5]486 spinlock_lock(&THREAD->lock);
[4e33b6b]487 if (THREAD->timeout_pending) {
488 /* busy waiting for timeouts in progress */
[43114c5]489 spinlock_unlock(&THREAD->lock);
[22f7769]490 interrupts_restore(ipl);
[f761f1eb]491 goto restart;
492 }
[ea7890e7]493
[43114c5]494 THREAD->state = Exiting;
495 spinlock_unlock(&THREAD->lock);
[f761f1eb]496 scheduler();
[874621f]497
498 /* Not reached */
499 while (1)
500 ;
[f761f1eb]501}
502
[70527f1]503
504/** Thread sleep
505 *
506 * Suspend execution of the current thread.
507 *
508 * @param sec Number of seconds to sleep.
509 *
510 */
[7f1c620]511void thread_sleep(uint32_t sec)
[f761f1eb]512{
[22e6802]513 /* Sleep in 1000 second steps to support
514 full argument range */
515 while (sec > 0) {
516 uint32_t period = (sec > 1000) ? 1000 : sec;
517
518 thread_usleep(period * 1000000);
519 sec -= period;
520 }
[f761f1eb]521}
[70527f1]522
[fe19611]523/** Wait for another thread to exit.
524 *
525 * @param t Thread to join on exit.
526 * @param usec Timeout in microseconds.
527 * @param flags Mode of operation.
528 *
529 * @return An error code from errno.h or an error code from synch.h.
530 */
[7f1c620]531int thread_join_timeout(thread_t *t, uint32_t usec, int flags)
[fe19611]532{
533 ipl_t ipl;
534 int rc;
535
536 if (t == THREAD)
537 return EINVAL;
538
539 /*
540 * Since thread join can only be called once on an undetached thread,
541 * the thread pointer is guaranteed to be still valid.
542 */
543
544 ipl = interrupts_disable();
545 spinlock_lock(&t->lock);
546 ASSERT(!t->detached);
547 spinlock_unlock(&t->lock);
[7b3e7f4]548 interrupts_restore(ipl);
[fe19611]549
[0182a665]550 rc = waitq_sleep_timeout(&t->join_wq, usec, flags);
551
[fe19611]552 return rc;
553}
554
555/** Detach thread.
556 *
[48d14222]557 * Mark the thread as detached, if the thread is already in the Lingering
558 * state, deallocate its resources.
[fe19611]559 *
560 * @param t Thread to be detached.
561 */
562void thread_detach(thread_t *t)
563{
564 ipl_t ipl;
565
566 /*
[31d8e10]567 * Since the thread is expected not to be already detached,
[fe19611]568 * pointer to it must be still valid.
569 */
570 ipl = interrupts_disable();
571 spinlock_lock(&t->lock);
572 ASSERT(!t->detached);
[48d14222]573 if (t->state == Lingering) {
[fe19611]574 thread_destroy(t); /* unlocks &t->lock */
575 interrupts_restore(ipl);
576 return;
577 } else {
578 t->detached = true;
579 }
580 spinlock_unlock(&t->lock);
581 interrupts_restore(ipl);
582}
583
[70527f1]584/** Thread usleep
585 *
586 * Suspend execution of the current thread.
587 *
588 * @param usec Number of microseconds to sleep.
589 *
590 */
[7f1c620]591void thread_usleep(uint32_t usec)
[f761f1eb]592{
593 waitq_t wq;
[22e6802]594
[f761f1eb]595 waitq_initialize(&wq);
[22e6802]596
[116d1ef4]597 (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING);
[f761f1eb]598}
599
[70527f1]600/** Register thread out-of-context invocation
601 *
602 * Register a function and its argument to be executed
603 * on next context switch to the current thread.
604 *
605 * @param call_me Out-of-context function.
606 * @param call_me_with Out-of-context function argument.
607 *
608 */
[f761f1eb]609void thread_register_call_me(void (* call_me)(void *), void *call_me_with)
610{
[22f7769]611 ipl_t ipl;
[f761f1eb]612
[22f7769]613 ipl = interrupts_disable();
[43114c5]614 spinlock_lock(&THREAD->lock);
615 THREAD->call_me = call_me;
616 THREAD->call_me_with = call_me_with;
617 spinlock_unlock(&THREAD->lock);
[22f7769]618 interrupts_restore(ipl);
[f761f1eb]619}
[55ab0f1]620
[b76a2217]621static bool thread_walker(avltree_node_t *node, void *arg)
[5dcee525]622{
[52755f1]623 thread_t *t = avltree_get_instance(node, thread_t, threads_tree_node);
624
[a2a00e8]625 uint64_t cycles, ucycles, kcycles;
626 char suffix, usuffix, ksuffix;
[5dcee525]627 order(t->cycles, &cycles, &suffix);
[a2a00e8]628 order(t->ucycles, &ucycles, &usuffix);
629 order(t->kcycles, &kcycles, &ksuffix);
[52755f1]630
631#ifdef __32_BITS__
[a2a00e8]632 printf("%-6" PRIu64" %-10s %10p %-8s %10p %-3" PRIu32 " %10p %10p %9" PRIu64 "%c %9" PRIu64 "%c %9" PRIu64 "%c ",
[52755f1]633 t->tid, t->name, t, thread_states[t->state], t->task,
[a2a00e8]634 t->task->context, t->thread_code, t->kstack, cycles, suffix, ucycles, usuffix, kcycles, ksuffix);
[52755f1]635#endif
636
637#ifdef __64_BITS__
[a2a00e8]638 printf("%-6" PRIu64" %-10s %18p %-8s %18p %-3" PRIu32 " %18p %18p %9" PRIu64 "%c %9" PRIu64 "%c %9" PRIu64 "%c ",
[52755f1]639 t->tid, t->name, t, thread_states[t->state], t->task,
[a2a00e8]640 t->task->context, t->thread_code, t->kstack, cycles, suffix, ucycles, usuffix, kcycles, ksuffix);
[52755f1]641#endif
[5dcee525]642
643 if (t->cpu)
[52755f1]644 printf("%-4u", t->cpu->id);
[5dcee525]645 else
646 printf("none");
647
[43b1e86]648 if (t->state == Sleeping) {
[52755f1]649#ifdef __32_BITS__
650 printf(" %10p", t->sleep_queue);
651#endif
652
653#ifdef __64_BITS__
654 printf(" %18p", t->sleep_queue);
655#endif
[43b1e86]656 }
[5dcee525]657
658 printf("\n");
[b76a2217]659
660 return true;
[5dcee525]661}
662
[55ab0f1]663/** Print list of threads debug info */
664void thread_print_list(void)
665{
666 ipl_t ipl;
667
668 /* Messing with thread structures, avoid deadlock */
669 ipl = interrupts_disable();
670 spinlock_lock(&threads_lock);
[52755f1]671
672#ifdef __32_BITS__
673 printf("tid name address state task "
[a2a00e8]674 "ctx code stack cycles ucycles kcycles cpu "
[52755f1]675 "waitqueue\n");
676 printf("------ ---------- ---------- -------- ---------- "
[a2a00e8]677 "--- ---------- ---------- ---------- ---------- ---------- ---- "
[52755f1]678 "----------\n");
679#endif
680
681#ifdef __64_BITS__
682 printf("tid name address state task "
[a2a00e8]683 "ctx code stack cycles ucycles kcycles cpu "
[52755f1]684 "waitqueue\n");
685 printf("------ ---------- ------------------ -------- ------------------ "
[a2a00e8]686 "--- ------------------ ------------------ ---------- ---------- ---------- ---- "
[52755f1]687 "------------------\n");
688#endif
[55ab0f1]689
[b76a2217]690 avltree_walk(&threads_tree, thread_walker, NULL);
[55ab0f1]691
692 spinlock_unlock(&threads_lock);
[37c57f2]693 interrupts_restore(ipl);
[55ab0f1]694}
[9f52563]695
[016acbe]696/** Check whether thread exists.
697 *
698 * Note that threads_lock must be already held and
699 * interrupts must be already disabled.
700 *
701 * @param t Pointer to thread.
702 *
703 * @return True if thread t is known to the system, false otherwise.
704 */
705bool thread_exists(thread_t *t)
706{
[5dcee525]707 avltree_node_t *node;
708
709 node = avltree_search(&threads_tree, (avltree_key_t) ((uintptr_t) t));
[016acbe]710
[5dcee525]711 return node != NULL;
[016acbe]712}
713
[cce6acf]714/** Update accounting of current thread.
715 *
716 * Note that thread_lock on THREAD must be already held and
717 * interrupts must be already disabled.
718 *
[a2a00e8]719 * @param user True to update user accounting, false for kernel.
[cce6acf]720 */
[a2a00e8]721void thread_update_accounting(bool user)
[cce6acf]722{
723 uint64_t time = get_cycle();
724 THREAD->cycles += time - THREAD->last_cycle;
[a2a00e8]725 if (user) {
726 THREAD->ucycles += time - THREAD->last_cycle;
727 } else {
728 THREAD->kcycles += time - THREAD->last_cycle;
729 }
[cce6acf]730 THREAD->last_cycle = time;
731}
732
[9f52563]733/** Process syscall to create new thread.
734 *
735 */
[f6d2c81]736unative_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name,
[7faabb7]737 size_t name_len, thread_id_t *uspace_thread_id)
[9f52563]738{
[68091bd]739 thread_t *t;
740 char namebuf[THREAD_NAME_BUFLEN];
[45fb65c]741 uspace_arg_t *kernel_uarg;
[e3c762cd]742 int rc;
[9f52563]743
[24345a5]744 if (name_len > THREAD_NAME_BUFLEN - 1)
[7faabb7]745 name_len = THREAD_NAME_BUFLEN - 1;
746
747 rc = copy_from_uspace(namebuf, uspace_name, name_len);
[e3c762cd]748 if (rc != 0)
[7f1c620]749 return (unative_t) rc;
[0f250f9]750
[b60c582]751 namebuf[name_len] = 0;
[7faabb7]752
[4680ef5]753 /*
754 * In case of failure, kernel_uarg will be deallocated in this function.
755 * In case of success, kernel_uarg will be freed in uinit().
756 */
757 kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
758
[e3c762cd]759 rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
760 if (rc != 0) {
761 free(kernel_uarg);
[7f1c620]762 return (unative_t) rc;
[e3c762cd]763 }
[9f52563]764
[d8431986]765 t = thread_create(uinit, kernel_uarg, TASK,
766 THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf, false);
[6f4495f5]767 if (t) {
[d8431986]768 if (uspace_thread_id != NULL) {
769 int rc;
770
771 rc = copy_to_uspace(uspace_thread_id, &t->tid,
772 sizeof(t->tid));
773 if (rc != 0) {
774 /*
775 * We have encountered a failure, but the thread
776 * has already been created. We need to undo its
777 * creation now.
778 */
779
780 /*
[ea7890e7]781 * The new thread structure is initialized, but
782 * is still not visible to the system.
[d8431986]783 * We can safely deallocate it.
784 */
785 slab_free(thread_slab, t);
786 free(kernel_uarg);
787
788 return (unative_t) rc;
789 }
790 }
[9a1b20c]791#ifdef CONFIG_UDEBUG
[13964ef]792 /*
793 * Generate udebug THREAD_B event and attach the thread.
794 * This must be done atomically (with the debug locks held),
795 * otherwise we would either miss some thread or receive
796 * THREAD_B events for threads that already existed
797 * and could be detected with THREAD_READ before.
798 */
799 udebug_thread_b_event_attach(t, TASK);
800#else
801 thread_attach(t, TASK);
[9a1b20c]802#endif
[13964ef]803 thread_ready(t);
[9a1b20c]804
[d8431986]805 return 0;
[201abde]806 } else
[0f250f9]807 free(kernel_uarg);
[9f52563]808
[7f1c620]809 return (unative_t) ENOMEM;
[9f52563]810}
811
812/** Process syscall to terminate thread.
813 *
814 */
[7f1c620]815unative_t sys_thread_exit(int uspace_status)
[9f52563]816{
[68091bd]817 thread_exit();
818 /* Unreachable */
819 return 0;
[9f52563]820}
[b45c443]821
[3ce7f082]822/** Syscall for getting TID.
823 *
[201abde]824 * @param uspace_thread_id Userspace address of 8-byte buffer where to store
825 * current thread ID.
826 *
827 * @return 0 on success or an error code from @ref errno.h.
[b45c443]828 */
[201abde]829unative_t sys_thread_get_id(thread_id_t *uspace_thread_id)
[3ce7f082]830{
831 /*
832 * No need to acquire lock on THREAD because tid
833 * remains constant for the lifespan of the thread.
834 */
[201abde]835 return (unative_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
836 sizeof(THREAD->tid));
[3ce7f082]837}
[6f4495f5]838
[d9ece1cb]839/** Syscall wrapper for sleeping. */
840unative_t sys_thread_usleep(uint32_t usec)
841{
[22e6802]842 thread_usleep(usec);
[d9ece1cb]843 return 0;
844}
845
[3ce7f082]846/** @}
847 */
Note: See TracBrowser for help on using the repository browser.