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