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