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