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