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