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