| [f761f1eb] | 1 | /*
|
|---|
| [7ed8530] | 2 | * Copyright (c) 2010 Jakub Jermar
|
|---|
| [ef1eab7] | 3 | * Copyright (c) 2018 Jiri Svoboda
|
|---|
| [f761f1eb] | 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| [174156fd] | 30 | /** @addtogroup kernel_generic_proc
|
|---|
| [b45c443] | 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| [9179d0a] | 34 | /**
|
|---|
| [b45c443] | 35 | * @file
|
|---|
| [da1bafb] | 36 | * @brief Thread management functions.
|
|---|
| [9179d0a] | 37 | */
|
|---|
| 38 |
|
|---|
| [63e27ef] | 39 | #include <assert.h>
|
|---|
| [f761f1eb] | 40 | #include <proc/scheduler.h>
|
|---|
| 41 | #include <proc/thread.h>
|
|---|
| 42 | #include <proc/task.h>
|
|---|
| 43 | #include <mm/frame.h>
|
|---|
| 44 | #include <mm/page.h>
|
|---|
| 45 | #include <arch/asm.h>
|
|---|
| [cce6acf] | 46 | #include <arch/cycle.h>
|
|---|
| [f761f1eb] | 47 | #include <arch.h>
|
|---|
| 48 | #include <synch/spinlock.h>
|
|---|
| 49 | #include <synch/waitq.h>
|
|---|
| 50 | #include <cpu.h>
|
|---|
| [e535eeb] | 51 | #include <str.h>
|
|---|
| [f761f1eb] | 52 | #include <context.h>
|
|---|
| [5c9a08b] | 53 | #include <adt/list.h>
|
|---|
| [ef1eab7] | 54 | #include <adt/odict.h>
|
|---|
| [f761f1eb] | 55 | #include <time/clock.h>
|
|---|
| [b3f8fb7] | 56 | #include <time/timeout.h>
|
|---|
| [8d6c1f1] | 57 | #include <time/delay.h>
|
|---|
| [4ffa9e0] | 58 | #include <config.h>
|
|---|
| 59 | #include <arch/interrupt.h>
|
|---|
| [26a8604f] | 60 | #include <smp/ipi.h>
|
|---|
| [f2ffad4] | 61 | #include <arch/faddr.h>
|
|---|
| [23684b7] | 62 | #include <atomic.h>
|
|---|
| [44a7ee5] | 63 | #include <mem.h>
|
|---|
| [bab75df6] | 64 | #include <stdio.h>
|
|---|
| [aafed15] | 65 | #include <stdlib.h>
|
|---|
| [9f52563] | 66 | #include <main/uinit.h>
|
|---|
| [e3c762cd] | 67 | #include <syscall/copy.h>
|
|---|
| 68 | #include <errno.h>
|
|---|
| [aae365bc] | 69 | #include <debug.h>
|
|---|
| [52755f1] | 70 |
|
|---|
| [fe19611] | 71 | /** Thread states */
|
|---|
| [a000878c] | 72 | const char *thread_states[] = {
|
|---|
| [fe19611] | 73 | "Invalid",
|
|---|
| 74 | "Running",
|
|---|
| 75 | "Sleeping",
|
|---|
| 76 | "Ready",
|
|---|
| 77 | "Entering",
|
|---|
| 78 | "Exiting",
|
|---|
| [48d14222] | 79 | "Lingering"
|
|---|
| [e1b6742] | 80 | };
|
|---|
| 81 |
|
|---|
| [ef1eab7] | 82 | /** Lock protecting the @c threads ordered dictionary .
|
|---|
| [4e33b6b] | 83 | *
|
|---|
| 84 | * For locking rules, see declaration thereof.
|
|---|
| 85 | */
|
|---|
| [da1bafb] | 86 | IRQ_SPINLOCK_INITIALIZE(threads_lock);
|
|---|
| [88169d9] | 87 |
|
|---|
| [ef1eab7] | 88 | /** Ordered dictionary of all threads by their address (i.e. pointer to
|
|---|
| 89 | * the thread_t structure).
|
|---|
| [88169d9] | 90 | *
|
|---|
| [ef1eab7] | 91 | * When a thread is found in the @c threads ordered dictionary, it is
|
|---|
| 92 | * guaranteed to exist as long as the @c threads_lock is held.
|
|---|
| [da1bafb] | 93 | *
|
|---|
| [ef1eab7] | 94 | * Members are of type thread_t.
|
|---|
| [88169d9] | 95 | */
|
|---|
| [ef1eab7] | 96 | odict_t threads;
|
|---|
| [f761f1eb] | 97 |
|
|---|
| [da1bafb] | 98 | IRQ_SPINLOCK_STATIC_INITIALIZE(tidlock);
|
|---|
| 99 | static thread_id_t last_tid = 0;
|
|---|
| [f761f1eb] | 100 |
|
|---|
| [82d515e9] | 101 | static slab_cache_t *thread_cache;
|
|---|
| [da1bafb] | 102 |
|
|---|
| [0f81ceb7] | 103 | #ifdef CONFIG_FPU
|
|---|
| [82d515e9] | 104 | slab_cache_t *fpu_context_cache;
|
|---|
| [f76fed4] | 105 | #endif
|
|---|
| [266294a9] | 106 |
|
|---|
| [ef1eab7] | 107 | static void *threads_getkey(odlink_t *);
|
|---|
| 108 | static int threads_cmp(void *, void *);
|
|---|
| 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();
|
|---|
| [a35b458] | 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();
|
|---|
| [a35b458] | 127 |
|
|---|
| [f761f1eb] | 128 | f(arg);
|
|---|
| [a35b458] | 129 |
|
|---|
| [0313ff0] | 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;
|
|---|
| [a35b458] | 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);
|
|---|
| [a35b458] | 145 |
|
|---|
| [f761f1eb] | 146 | thread_exit();
|
|---|
| [a35b458] | 147 |
|
|---|
| [da1bafb] | 148 | /* Not reached */
|
|---|
| [f761f1eb] | 149 | }
|
|---|
| 150 |
|
|---|
| [da1bafb] | 151 | /** Initialization and allocation for thread_t structure
|
|---|
| 152 | *
|
|---|
| 153 | */
|
|---|
| [b7fd2a0] | 154 | static errno_t thr_constructor(void *obj, unsigned int kmflags)
|
|---|
| [266294a9] | 155 | {
|
|---|
| [da1bafb] | 156 | thread_t *thread = (thread_t *) obj;
|
|---|
| [a35b458] | 157 |
|
|---|
| [da1bafb] | 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);
|
|---|
| [a35b458] | 162 |
|
|---|
| [32fffef0] | 163 | /* call the architecture-specific part of the constructor */
|
|---|
| [da1bafb] | 164 | thr_constructor_arch(thread);
|
|---|
| [a35b458] | 165 |
|
|---|
| [0f81ceb7] | 166 | #ifdef CONFIG_FPU
|
|---|
| [abf6c01] | 167 | thread->saved_fpu_context = slab_alloc(fpu_context_cache,
|
|---|
| 168 | FRAME_ATOMIC | kmflags);
|
|---|
| [da1bafb] | 169 | if (!thread->saved_fpu_context)
|
|---|
| [7f11dc6] | 170 | return ENOMEM;
|
|---|
| [da1bafb] | 171 | #endif /* CONFIG_FPU */
|
|---|
| [a35b458] | 172 |
|
|---|
| [38ff925] | 173 | /*
|
|---|
| 174 | * Allocate the kernel stack from the low-memory to prevent an infinite
|
|---|
| 175 | * nesting of TLB-misses when accessing the stack from the part of the
|
|---|
| 176 | * TLB-miss handler written in C.
|
|---|
| 177 | *
|
|---|
| 178 | * Note that low-memory is safe to be used for the stack as it will be
|
|---|
| 179 | * covered by the kernel identity mapping, which guarantees not to
|
|---|
| 180 | * nest TLB-misses infinitely (either via some hardware mechanism or
|
|---|
| 181 | * by the construciton of the assembly-language part of the TLB-miss
|
|---|
| 182 | * handler).
|
|---|
| 183 | *
|
|---|
| 184 | * This restriction can be lifted once each architecture provides
|
|---|
| 185 | * a similar guarantee, for example by locking the kernel stack
|
|---|
| 186 | * in the TLB whenever it is allocated from the high-memory and the
|
|---|
| 187 | * thread is being scheduled to run.
|
|---|
| 188 | */
|
|---|
| 189 | kmflags |= FRAME_LOWMEM;
|
|---|
| 190 | kmflags &= ~FRAME_HIGHMEM;
|
|---|
| [a35b458] | 191 |
|
|---|
| [95d45482] | 192 | // NOTE: All kernel stacks must be aligned to STACK_SIZE,
|
|---|
| 193 | // see get_stack_base().
|
|---|
| [d1da1ff2] | 194 |
|
|---|
| [cd3b380] | 195 | uintptr_t stack_phys =
|
|---|
| 196 | frame_alloc(STACK_FRAMES, kmflags, STACK_SIZE - 1);
|
|---|
| 197 | if (!stack_phys) {
|
|---|
| [0f81ceb7] | 198 | #ifdef CONFIG_FPU
|
|---|
| [ba9a150] | 199 | assert(thread->saved_fpu_context);
|
|---|
| 200 | slab_free(fpu_context_cache, thread->saved_fpu_context);
|
|---|
| [f76fed4] | 201 | #endif
|
|---|
| [7f11dc6] | 202 | return ENOMEM;
|
|---|
| [f76fed4] | 203 | }
|
|---|
| [a35b458] | 204 |
|
|---|
| [cd3b380] | 205 | thread->kstack = (uint8_t *) PA2KA(stack_phys);
|
|---|
| [a35b458] | 206 |
|
|---|
| [9a1b20c] | 207 | #ifdef CONFIG_UDEBUG
|
|---|
| [da1bafb] | 208 | mutex_initialize(&thread->udebug.lock, MUTEX_PASSIVE);
|
|---|
| [9a1b20c] | 209 | #endif
|
|---|
| [a35b458] | 210 |
|
|---|
| [7f11dc6] | 211 | return EOK;
|
|---|
| [266294a9] | 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /** Destruction of thread_t object */
|
|---|
| [da1bafb] | 215 | static size_t thr_destructor(void *obj)
|
|---|
| [266294a9] | 216 | {
|
|---|
| [da1bafb] | 217 | thread_t *thread = (thread_t *) obj;
|
|---|
| [a35b458] | 218 |
|
|---|
| [32fffef0] | 219 | /* call the architecture-specific part of the destructor */
|
|---|
| [da1bafb] | 220 | thr_destructor_arch(thread);
|
|---|
| [a35b458] | 221 |
|
|---|
| [5df1963] | 222 | frame_free(KA2PA(thread->kstack), STACK_FRAMES);
|
|---|
| [a35b458] | 223 |
|
|---|
| [0f81ceb7] | 224 | #ifdef CONFIG_FPU
|
|---|
| [ba9a150] | 225 | assert(thread->saved_fpu_context);
|
|---|
| 226 | slab_free(fpu_context_cache, thread->saved_fpu_context);
|
|---|
| [f76fed4] | 227 | #endif
|
|---|
| [a35b458] | 228 |
|
|---|
| [e7c4115d] | 229 | return STACK_FRAMES; /* number of frames freed */
|
|---|
| [266294a9] | 230 | }
|
|---|
| [70527f1] | 231 |
|
|---|
| 232 | /** Initialize threads
|
|---|
| 233 | *
|
|---|
| 234 | * Initialize kernel threads support.
|
|---|
| 235 | *
|
|---|
| 236 | */
|
|---|
| [f761f1eb] | 237 | void thread_init(void)
|
|---|
| 238 | {
|
|---|
| [43114c5] | 239 | THREAD = NULL;
|
|---|
| [a35b458] | 240 |
|
|---|
| [e3306d04] | 241 | atomic_store(&nrdy, 0);
|
|---|
| [82d515e9] | 242 | thread_cache = slab_cache_create("thread_t", sizeof(thread_t), 0,
|
|---|
| [6f4495f5] | 243 | thr_constructor, thr_destructor, 0);
|
|---|
| [a35b458] | 244 |
|
|---|
| [0f81ceb7] | 245 | #ifdef CONFIG_FPU
|
|---|
| [82d515e9] | 246 | fpu_context_cache = slab_cache_create("fpu_context_t",
|
|---|
| [f97f1e51] | 247 | sizeof(fpu_context_t), FPU_CONTEXT_ALIGN, NULL, NULL, 0);
|
|---|
| [f76fed4] | 248 | #endif
|
|---|
| [a35b458] | 249 |
|
|---|
| [ef1eab7] | 250 | odict_initialize(&threads, threads_getkey, threads_cmp);
|
|---|
| [016acbe] | 251 | }
|
|---|
| [70527f1] | 252 |
|
|---|
| [6eef3c4] | 253 | /** Wire thread to the given CPU
|
|---|
| 254 | *
|
|---|
| 255 | * @param cpu CPU to wire the thread to.
|
|---|
| 256 | *
|
|---|
| 257 | */
|
|---|
| 258 | void thread_wire(thread_t *thread, cpu_t *cpu)
|
|---|
| 259 | {
|
|---|
| 260 | irq_spinlock_lock(&thread->lock, true);
|
|---|
| 261 | thread->cpu = cpu;
|
|---|
| 262 | thread->wired = true;
|
|---|
| 263 | irq_spinlock_unlock(&thread->lock, true);
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| [8a64e81e] | 266 | /** Invoked right before thread_ready() readies the thread. thread is locked. */
|
|---|
| 267 | static void before_thread_is_ready(thread_t *thread)
|
|---|
| 268 | {
|
|---|
| [63e27ef] | 269 | assert(irq_spinlock_locked(&thread->lock));
|
|---|
| [8a64e81e] | 270 | }
|
|---|
| 271 |
|
|---|
| [70527f1] | 272 | /** Make thread ready
|
|---|
| 273 | *
|
|---|
| [da1bafb] | 274 | * Switch thread to the ready state.
|
|---|
| [70527f1] | 275 | *
|
|---|
| [df58e44] | 276 | * @param thread Thread to make ready.
|
|---|
| [70527f1] | 277 | *
|
|---|
| 278 | */
|
|---|
| [da1bafb] | 279 | void thread_ready(thread_t *thread)
|
|---|
| [f761f1eb] | 280 | {
|
|---|
| [da1bafb] | 281 | irq_spinlock_lock(&thread->lock, true);
|
|---|
| [a35b458] | 282 |
|
|---|
| [63e27ef] | 283 | assert(thread->state != Ready);
|
|---|
| [518dd43] | 284 |
|
|---|
| [8a64e81e] | 285 | before_thread_is_ready(thread);
|
|---|
| [a35b458] | 286 |
|
|---|
| [6eef3c4] | 287 | int i = (thread->priority < RQ_COUNT - 1) ?
|
|---|
| 288 | ++thread->priority : thread->priority;
|
|---|
| [518dd43] | 289 |
|
|---|
| [8ad7dd1] | 290 | cpu_t *cpu;
|
|---|
| 291 | if (thread->wired || thread->nomigrate || thread->fpu_context_engaged) {
|
|---|
| 292 | /* Cannot ready to another CPU */
|
|---|
| [63e27ef] | 293 | assert(thread->cpu != NULL);
|
|---|
| [8ad7dd1] | 294 | cpu = thread->cpu;
|
|---|
| 295 | } else if (thread->stolen) {
|
|---|
| 296 | /* Ready to the stealing CPU */
|
|---|
| [6eef3c4] | 297 | cpu = CPU;
|
|---|
| [8ad7dd1] | 298 | } else if (thread->cpu) {
|
|---|
| 299 | /* Prefer the CPU on which the thread ran last */
|
|---|
| [63e27ef] | 300 | assert(thread->cpu != NULL);
|
|---|
| [8ad7dd1] | 301 | cpu = thread->cpu;
|
|---|
| 302 | } else {
|
|---|
| 303 | cpu = CPU;
|
|---|
| 304 | }
|
|---|
| [a35b458] | 305 |
|
|---|
| [da1bafb] | 306 | thread->state = Ready;
|
|---|
| [a35b458] | 307 |
|
|---|
| [da1bafb] | 308 | irq_spinlock_pass(&thread->lock, &(cpu->rq[i].lock));
|
|---|
| [a35b458] | 309 |
|
|---|
| [70527f1] | 310 | /*
|
|---|
| [da1bafb] | 311 | * Append thread to respective ready queue
|
|---|
| 312 | * on respective processor.
|
|---|
| [f761f1eb] | 313 | */
|
|---|
| [a35b458] | 314 |
|
|---|
| [55b77d9] | 315 | list_append(&thread->rq_link, &cpu->rq[i].rq);
|
|---|
| [da1bafb] | 316 | cpu->rq[i].n++;
|
|---|
| 317 | irq_spinlock_unlock(&(cpu->rq[i].lock), true);
|
|---|
| [a35b458] | 318 |
|
|---|
| [59e07c91] | 319 | atomic_inc(&nrdy);
|
|---|
| [248fc1a] | 320 | atomic_inc(&cpu->nrdy);
|
|---|
| [f761f1eb] | 321 | }
|
|---|
| 322 |
|
|---|
| [70527f1] | 323 | /** Create new thread
|
|---|
| 324 | *
|
|---|
| 325 | * Create a new thread.
|
|---|
| 326 | *
|
|---|
| [da1bafb] | 327 | * @param func Thread's implementing function.
|
|---|
| 328 | * @param arg Thread's implementing function argument.
|
|---|
| 329 | * @param task Task to which the thread belongs. The caller must
|
|---|
| 330 | * guarantee that the task won't cease to exist during the
|
|---|
| 331 | * call. The task's lock may not be held.
|
|---|
| 332 | * @param flags Thread flags.
|
|---|
| 333 | * @param name Symbolic name (a copy is made).
|
|---|
| [70527f1] | 334 | *
|
|---|
| [da1bafb] | 335 | * @return New thread's structure on success, NULL on failure.
|
|---|
| [70527f1] | 336 | *
|
|---|
| 337 | */
|
|---|
| [3bacee1] | 338 | thread_t *thread_create(void (*func)(void *), void *arg, task_t *task,
|
|---|
| [6eef3c4] | 339 | thread_flags_t flags, const char *name)
|
|---|
| [f761f1eb] | 340 | {
|
|---|
| [abf6c01] | 341 | thread_t *thread = (thread_t *) slab_alloc(thread_cache, FRAME_ATOMIC);
|
|---|
| [da1bafb] | 342 | if (!thread)
|
|---|
| [2a46e10] | 343 | return NULL;
|
|---|
| [a35b458] | 344 |
|
|---|
| [deacd722] | 345 | if (thread_create_arch(thread, flags) != EOK) {
|
|---|
| 346 | slab_free(thread_cache, thread);
|
|---|
| 347 | return NULL;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| [bb68433] | 350 | /* Not needed, but good for debugging */
|
|---|
| [26aafe8] | 351 | memsetb(thread->kstack, STACK_SIZE, 0);
|
|---|
| [a35b458] | 352 |
|
|---|
| [da1bafb] | 353 | irq_spinlock_lock(&tidlock, true);
|
|---|
| 354 | thread->tid = ++last_tid;
|
|---|
| 355 | irq_spinlock_unlock(&tidlock, true);
|
|---|
| [a35b458] | 356 |
|
|---|
| [edc64c0] | 357 | memset(&thread->saved_context, 0, sizeof(thread->saved_context));
|
|---|
| [da1bafb] | 358 | context_set(&thread->saved_context, FADDR(cushion),
|
|---|
| [26aafe8] | 359 | (uintptr_t) thread->kstack, STACK_SIZE);
|
|---|
| [a35b458] | 360 |
|
|---|
| [a6e55886] | 361 | current_initialize((current_t *) thread->kstack);
|
|---|
| [a35b458] | 362 |
|
|---|
| [da1bafb] | 363 | ipl_t ipl = interrupts_disable();
|
|---|
| 364 | thread->saved_context.ipl = interrupts_read();
|
|---|
| [bb68433] | 365 | interrupts_restore(ipl);
|
|---|
| [a35b458] | 366 |
|
|---|
| [da1bafb] | 367 | str_cpy(thread->name, THREAD_NAME_BUFLEN, name);
|
|---|
| [a35b458] | 368 |
|
|---|
| [da1bafb] | 369 | thread->thread_code = func;
|
|---|
| 370 | thread->thread_arg = arg;
|
|---|
| 371 | thread->ticks = -1;
|
|---|
| 372 | thread->ucycles = 0;
|
|---|
| 373 | thread->kcycles = 0;
|
|---|
| [6eef3c4] | 374 | thread->uncounted =
|
|---|
| 375 | ((flags & THREAD_FLAG_UNCOUNTED) == THREAD_FLAG_UNCOUNTED);
|
|---|
| [da1bafb] | 376 | thread->priority = -1; /* Start in rq[0] */
|
|---|
| 377 | thread->cpu = NULL;
|
|---|
| [6eef3c4] | 378 | thread->wired = false;
|
|---|
| 379 | thread->stolen = false;
|
|---|
| 380 | thread->uspace =
|
|---|
| 381 | ((flags & THREAD_FLAG_USPACE) == THREAD_FLAG_USPACE);
|
|---|
| [a35b458] | 382 |
|
|---|
| [43ac0cc] | 383 | thread->nomigrate = 0;
|
|---|
| [da1bafb] | 384 | thread->state = Entering;
|
|---|
| [a35b458] | 385 |
|
|---|
| [da1bafb] | 386 | timeout_initialize(&thread->sleep_timeout);
|
|---|
| 387 | thread->sleep_interruptible = false;
|
|---|
| [b59318e] | 388 | thread->sleep_composable = false;
|
|---|
| [da1bafb] | 389 | thread->sleep_queue = NULL;
|
|---|
| 390 | thread->timeout_pending = false;
|
|---|
| [a35b458] | 391 |
|
|---|
| [da1bafb] | 392 | thread->in_copy_from_uspace = false;
|
|---|
| 393 | thread->in_copy_to_uspace = false;
|
|---|
| [a35b458] | 394 |
|
|---|
| [da1bafb] | 395 | thread->interrupted = false;
|
|---|
| 396 | thread->detached = false;
|
|---|
| 397 | waitq_initialize(&thread->join_wq);
|
|---|
| [a35b458] | 398 |
|
|---|
| [da1bafb] | 399 | thread->task = task;
|
|---|
| [a35b458] | 400 |
|
|---|
| [6eef3c4] | 401 | thread->fpu_context_exists = false;
|
|---|
| 402 | thread->fpu_context_engaged = false;
|
|---|
| [a35b458] | 403 |
|
|---|
| [ef1eab7] | 404 | odlink_initialize(&thread->lthreads);
|
|---|
| [a35b458] | 405 |
|
|---|
| [9a1b20c] | 406 | #ifdef CONFIG_UDEBUG
|
|---|
| [5b7a107] | 407 | /* Initialize debugging stuff */
|
|---|
| 408 | thread->btrace = false;
|
|---|
| [da1bafb] | 409 | udebug_thread_initialize(&thread->udebug);
|
|---|
| [9a1b20c] | 410 | #endif
|
|---|
| [a35b458] | 411 |
|
|---|
| [6eef3c4] | 412 | if ((flags & THREAD_FLAG_NOATTACH) != THREAD_FLAG_NOATTACH)
|
|---|
| [da1bafb] | 413 | thread_attach(thread, task);
|
|---|
| [a35b458] | 414 |
|
|---|
| [da1bafb] | 415 | return thread;
|
|---|
| [d8431986] | 416 | }
|
|---|
| 417 |
|
|---|
| 418 | /** Destroy thread memory structure
|
|---|
| 419 | *
|
|---|
| 420 | * Detach thread from all queues, cpus etc. and destroy it.
|
|---|
| [da1bafb] | 421 | *
|
|---|
| 422 | * @param thread Thread to be destroyed.
|
|---|
| 423 | * @param irq_res Indicate whether it should unlock thread->lock
|
|---|
| 424 | * in interrupts-restore mode.
|
|---|
| [d8431986] | 425 | *
|
|---|
| 426 | */
|
|---|
| [da1bafb] | 427 | void thread_destroy(thread_t *thread, bool irq_res)
|
|---|
| [d8431986] | 428 | {
|
|---|
| [63e27ef] | 429 | assert(irq_spinlock_locked(&thread->lock));
|
|---|
| 430 | assert((thread->state == Exiting) || (thread->state == Lingering));
|
|---|
| 431 | assert(thread->task);
|
|---|
| 432 | assert(thread->cpu);
|
|---|
| [a35b458] | 433 |
|
|---|
| [da1bafb] | 434 | irq_spinlock_lock(&thread->cpu->lock, false);
|
|---|
| 435 | if (thread->cpu->fpu_owner == thread)
|
|---|
| 436 | thread->cpu->fpu_owner = NULL;
|
|---|
| 437 | irq_spinlock_unlock(&thread->cpu->lock, false);
|
|---|
| [a35b458] | 438 |
|
|---|
| [da1bafb] | 439 | irq_spinlock_pass(&thread->lock, &threads_lock);
|
|---|
| [a35b458] | 440 |
|
|---|
| [ef1eab7] | 441 | odict_remove(&thread->lthreads);
|
|---|
| [a35b458] | 442 |
|
|---|
| [da1bafb] | 443 | irq_spinlock_pass(&threads_lock, &thread->task->lock);
|
|---|
| [a35b458] | 444 |
|
|---|
| [d8431986] | 445 | /*
|
|---|
| 446 | * Detach from the containing task.
|
|---|
| 447 | */
|
|---|
| [da1bafb] | 448 | list_remove(&thread->th_link);
|
|---|
| 449 | irq_spinlock_unlock(&thread->task->lock, irq_res);
|
|---|
| [a35b458] | 450 |
|
|---|
| [ea7890e7] | 451 | /*
|
|---|
| [7ed8530] | 452 | * Drop the reference to the containing task.
|
|---|
| [ea7890e7] | 453 | */
|
|---|
| [da1bafb] | 454 | task_release(thread->task);
|
|---|
| [82d515e9] | 455 | slab_free(thread_cache, thread);
|
|---|
| [d8431986] | 456 | }
|
|---|
| 457 |
|
|---|
| 458 | /** Make the thread visible to the system.
|
|---|
| 459 | *
|
|---|
| 460 | * Attach the thread structure to the current task and make it visible in the
|
|---|
| [5dcee525] | 461 | * threads_tree.
|
|---|
| [d8431986] | 462 | *
|
|---|
| [da1bafb] | 463 | * @param t Thread to be attached to the task.
|
|---|
| 464 | * @param task Task to which the thread is to be attached.
|
|---|
| 465 | *
|
|---|
| [d8431986] | 466 | */
|
|---|
| [da1bafb] | 467 | void thread_attach(thread_t *thread, task_t *task)
|
|---|
| [d8431986] | 468 | {
|
|---|
| 469 | /*
|
|---|
| [9a1b20c] | 470 | * Attach to the specified task.
|
|---|
| [d8431986] | 471 | */
|
|---|
| [da1bafb] | 472 | irq_spinlock_lock(&task->lock, true);
|
|---|
| [a35b458] | 473 |
|
|---|
| [7ed8530] | 474 | /* Hold a reference to the task. */
|
|---|
| 475 | task_hold(task);
|
|---|
| [a35b458] | 476 |
|
|---|
| [9a1b20c] | 477 | /* Must not count kbox thread into lifecount */
|
|---|
| [6eef3c4] | 478 | if (thread->uspace)
|
|---|
| [9a1b20c] | 479 | atomic_inc(&task->lifecount);
|
|---|
| [a35b458] | 480 |
|
|---|
| [55b77d9] | 481 | list_append(&thread->th_link, &task->threads);
|
|---|
| [a35b458] | 482 |
|
|---|
| [da1bafb] | 483 | irq_spinlock_pass(&task->lock, &threads_lock);
|
|---|
| [a35b458] | 484 |
|
|---|
| [bb68433] | 485 | /*
|
|---|
| [ef1eab7] | 486 | * Register this thread in the system-wide dictionary.
|
|---|
| [bb68433] | 487 | */
|
|---|
| [ef1eab7] | 488 | odict_insert(&thread->lthreads, &threads, NULL);
|
|---|
| [da1bafb] | 489 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| [f761f1eb] | 490 | }
|
|---|
| 491 |
|
|---|
| [0182a665] | 492 | /** Terminate thread.
|
|---|
| [70527f1] | 493 | *
|
|---|
| [da1bafb] | 494 | * End current thread execution and switch it to the exiting state.
|
|---|
| 495 | * All pending timeouts are executed.
|
|---|
| 496 | *
|
|---|
| [70527f1] | 497 | */
|
|---|
| [f761f1eb] | 498 | void thread_exit(void)
|
|---|
| 499 | {
|
|---|
| [6eef3c4] | 500 | if (THREAD->uspace) {
|
|---|
| [9a1b20c] | 501 | #ifdef CONFIG_UDEBUG
|
|---|
| 502 | /* Generate udebug THREAD_E event */
|
|---|
| 503 | udebug_thread_e_event();
|
|---|
| [a35b458] | 504 |
|
|---|
| [0ac99db] | 505 | /*
|
|---|
| 506 | * This thread will not execute any code or system calls from
|
|---|
| 507 | * now on.
|
|---|
| 508 | */
|
|---|
| 509 | udebug_stoppable_begin();
|
|---|
| [9a1b20c] | 510 | #endif
|
|---|
| 511 | if (atomic_predec(&TASK->lifecount) == 0) {
|
|---|
| 512 | /*
|
|---|
| 513 | * We are the last userspace thread in the task that
|
|---|
| 514 | * still has not exited. With the exception of the
|
|---|
| 515 | * moment the task was created, new userspace threads
|
|---|
| 516 | * can only be created by threads of the same task.
|
|---|
| 517 | * We are safe to perform cleanup.
|
|---|
| [da1bafb] | 518 | *
|
|---|
| [9a1b20c] | 519 | */
|
|---|
| [ea7890e7] | 520 | ipc_cleanup();
|
|---|
| [669f3d32] | 521 | futex_task_cleanup();
|
|---|
| [3bacee1] | 522 | LOG("Cleanup of task %" PRIu64 " completed.", TASK->taskid);
|
|---|
| [ea7890e7] | 523 | }
|
|---|
| 524 | }
|
|---|
| [a35b458] | 525 |
|
|---|
| [f761f1eb] | 526 | restart:
|
|---|
| [da1bafb] | 527 | irq_spinlock_lock(&THREAD->lock, true);
|
|---|
| 528 | if (THREAD->timeout_pending) {
|
|---|
| 529 | /* Busy waiting for timeouts in progress */
|
|---|
| 530 | irq_spinlock_unlock(&THREAD->lock, true);
|
|---|
| [f761f1eb] | 531 | goto restart;
|
|---|
| 532 | }
|
|---|
| [a35b458] | 533 |
|
|---|
| [43114c5] | 534 | THREAD->state = Exiting;
|
|---|
| [da1bafb] | 535 | irq_spinlock_unlock(&THREAD->lock, true);
|
|---|
| [a35b458] | 536 |
|
|---|
| [f761f1eb] | 537 | scheduler();
|
|---|
| [a35b458] | 538 |
|
|---|
| [874621f] | 539 | /* Not reached */
|
|---|
| [3bacee1] | 540 | while (true)
|
|---|
| 541 | ;
|
|---|
| [f761f1eb] | 542 | }
|
|---|
| 543 |
|
|---|
| [518dd43] | 544 | /** Interrupts an existing thread so that it may exit as soon as possible.
|
|---|
| [1b20da0] | 545 | *
|
|---|
| 546 | * Threads that are blocked waiting for a synchronization primitive
|
|---|
| [897fd8f1] | 547 | * are woken up with a return code of EINTR if the
|
|---|
| [518dd43] | 548 | * blocking call was interruptable. See waitq_sleep_timeout().
|
|---|
| [1b20da0] | 549 | *
|
|---|
| [518dd43] | 550 | * The caller must guarantee the thread object is valid during the entire
|
|---|
| 551 | * function, eg by holding the threads_lock lock.
|
|---|
| [1b20da0] | 552 | *
|
|---|
| [518dd43] | 553 | * Interrupted threads automatically exit when returning back to user space.
|
|---|
| [1b20da0] | 554 | *
|
|---|
| [518dd43] | 555 | * @param thread A valid thread object. The caller must guarantee it
|
|---|
| 556 | * will remain valid until thread_interrupt() exits.
|
|---|
| 557 | */
|
|---|
| 558 | void thread_interrupt(thread_t *thread)
|
|---|
| 559 | {
|
|---|
| [63e27ef] | 560 | assert(thread != NULL);
|
|---|
| [a35b458] | 561 |
|
|---|
| [518dd43] | 562 | irq_spinlock_lock(&thread->lock, true);
|
|---|
| [a35b458] | 563 |
|
|---|
| [518dd43] | 564 | thread->interrupted = true;
|
|---|
| 565 | bool sleeping = (thread->state == Sleeping);
|
|---|
| [a35b458] | 566 |
|
|---|
| [518dd43] | 567 | irq_spinlock_unlock(&thread->lock, true);
|
|---|
| [a35b458] | 568 |
|
|---|
| [518dd43] | 569 | if (sleeping)
|
|---|
| 570 | waitq_interrupt_sleep(thread);
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | /** Returns true if the thread was interrupted.
|
|---|
| [1b20da0] | 574 | *
|
|---|
| [518dd43] | 575 | * @param thread A valid thread object. User must guarantee it will
|
|---|
| 576 | * be alive during the entire call.
|
|---|
| 577 | * @return true if the thread was already interrupted via thread_interrupt().
|
|---|
| 578 | */
|
|---|
| 579 | bool thread_interrupted(thread_t *thread)
|
|---|
| 580 | {
|
|---|
| [63e27ef] | 581 | assert(thread != NULL);
|
|---|
| [a35b458] | 582 |
|
|---|
| [518dd43] | 583 | bool interrupted;
|
|---|
| [a35b458] | 584 |
|
|---|
| [518dd43] | 585 | irq_spinlock_lock(&thread->lock, true);
|
|---|
| 586 | interrupted = thread->interrupted;
|
|---|
| 587 | irq_spinlock_unlock(&thread->lock, true);
|
|---|
| [a35b458] | 588 |
|
|---|
| [518dd43] | 589 | return interrupted;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| [43ac0cc] | 592 | /** Prevent the current thread from being migrated to another processor. */
|
|---|
| 593 | void thread_migration_disable(void)
|
|---|
| 594 | {
|
|---|
| [63e27ef] | 595 | assert(THREAD);
|
|---|
| [a35b458] | 596 |
|
|---|
| [43ac0cc] | 597 | THREAD->nomigrate++;
|
|---|
| 598 | }
|
|---|
| 599 |
|
|---|
| 600 | /** Allow the current thread to be migrated to another processor. */
|
|---|
| 601 | void thread_migration_enable(void)
|
|---|
| 602 | {
|
|---|
| [63e27ef] | 603 | assert(THREAD);
|
|---|
| 604 | assert(THREAD->nomigrate > 0);
|
|---|
| [a35b458] | 605 |
|
|---|
| [6eef3c4] | 606 | if (THREAD->nomigrate > 0)
|
|---|
| 607 | THREAD->nomigrate--;
|
|---|
| [43ac0cc] | 608 | }
|
|---|
| 609 |
|
|---|
| [70527f1] | 610 | /** Thread sleep
|
|---|
| 611 | *
|
|---|
| 612 | * Suspend execution of the current thread.
|
|---|
| 613 | *
|
|---|
| 614 | * @param sec Number of seconds to sleep.
|
|---|
| 615 | *
|
|---|
| 616 | */
|
|---|
| [7f1c620] | 617 | void thread_sleep(uint32_t sec)
|
|---|
| [f761f1eb] | 618 | {
|
|---|
| [7c3fb9b] | 619 | /*
|
|---|
| 620 | * Sleep in 1000 second steps to support
|
|---|
| 621 | * full argument range
|
|---|
| 622 | */
|
|---|
| [22e6802] | 623 | while (sec > 0) {
|
|---|
| 624 | uint32_t period = (sec > 1000) ? 1000 : sec;
|
|---|
| [a35b458] | 625 |
|
|---|
| [22e6802] | 626 | thread_usleep(period * 1000000);
|
|---|
| 627 | sec -= period;
|
|---|
| 628 | }
|
|---|
| [f761f1eb] | 629 | }
|
|---|
| [70527f1] | 630 |
|
|---|
| [fe19611] | 631 | /** Wait for another thread to exit.
|
|---|
| 632 | *
|
|---|
| [da1bafb] | 633 | * @param thread Thread to join on exit.
|
|---|
| 634 | * @param usec Timeout in microseconds.
|
|---|
| 635 | * @param flags Mode of operation.
|
|---|
| [fe19611] | 636 | *
|
|---|
| 637 | * @return An error code from errno.h or an error code from synch.h.
|
|---|
| [da1bafb] | 638 | *
|
|---|
| [fe19611] | 639 | */
|
|---|
| [b7fd2a0] | 640 | errno_t thread_join_timeout(thread_t *thread, uint32_t usec, unsigned int flags)
|
|---|
| [fe19611] | 641 | {
|
|---|
| [da1bafb] | 642 | if (thread == THREAD)
|
|---|
| [fe19611] | 643 | return EINVAL;
|
|---|
| [a35b458] | 644 |
|
|---|
| [fe19611] | 645 | /*
|
|---|
| 646 | * Since thread join can only be called once on an undetached thread,
|
|---|
| 647 | * the thread pointer is guaranteed to be still valid.
|
|---|
| 648 | */
|
|---|
| [a35b458] | 649 |
|
|---|
| [da1bafb] | 650 | irq_spinlock_lock(&thread->lock, true);
|
|---|
| [63e27ef] | 651 | assert(!thread->detached);
|
|---|
| [da1bafb] | 652 | irq_spinlock_unlock(&thread->lock, true);
|
|---|
| [a35b458] | 653 |
|
|---|
| [897fd8f1] | 654 | return waitq_sleep_timeout(&thread->join_wq, usec, flags, NULL);
|
|---|
| [abf6c01] | 655 |
|
|---|
| 656 | // FIXME: join should deallocate the thread.
|
|---|
| 657 | // Current code calls detach after join, that's contrary to how
|
|---|
| 658 | // join is used in other threading APIs.
|
|---|
| [fe19611] | 659 | }
|
|---|
| 660 |
|
|---|
| 661 | /** Detach thread.
|
|---|
| 662 | *
|
|---|
| [df58e44] | 663 | * Mark the thread as detached. If the thread is already
|
|---|
| 664 | * in the Lingering state, deallocate its resources.
|
|---|
| [fe19611] | 665 | *
|
|---|
| [da1bafb] | 666 | * @param thread Thread to be detached.
|
|---|
| 667 | *
|
|---|
| [fe19611] | 668 | */
|
|---|
| [da1bafb] | 669 | void thread_detach(thread_t *thread)
|
|---|
| [fe19611] | 670 | {
|
|---|
| 671 | /*
|
|---|
| [31d8e10] | 672 | * Since the thread is expected not to be already detached,
|
|---|
| [fe19611] | 673 | * pointer to it must be still valid.
|
|---|
| 674 | */
|
|---|
| [da1bafb] | 675 | irq_spinlock_lock(&thread->lock, true);
|
|---|
| [63e27ef] | 676 | assert(!thread->detached);
|
|---|
| [a35b458] | 677 |
|
|---|
| [da1bafb] | 678 | if (thread->state == Lingering) {
|
|---|
| 679 | /*
|
|---|
| 680 | * Unlock &thread->lock and restore
|
|---|
| 681 | * interrupts in thread_destroy().
|
|---|
| 682 | */
|
|---|
| 683 | thread_destroy(thread, true);
|
|---|
| [fe19611] | 684 | return;
|
|---|
| 685 | } else {
|
|---|
| [da1bafb] | 686 | thread->detached = true;
|
|---|
| [fe19611] | 687 | }
|
|---|
| [a35b458] | 688 |
|
|---|
| [da1bafb] | 689 | irq_spinlock_unlock(&thread->lock, true);
|
|---|
| [fe19611] | 690 | }
|
|---|
| 691 |
|
|---|
| [70527f1] | 692 | /** Thread usleep
|
|---|
| 693 | *
|
|---|
| 694 | * Suspend execution of the current thread.
|
|---|
| 695 | *
|
|---|
| 696 | * @param usec Number of microseconds to sleep.
|
|---|
| 697 | *
|
|---|
| [1b20da0] | 698 | */
|
|---|
| [7f1c620] | 699 | void thread_usleep(uint32_t usec)
|
|---|
| [f761f1eb] | 700 | {
|
|---|
| 701 | waitq_t wq;
|
|---|
| [a35b458] | 702 |
|
|---|
| [f761f1eb] | 703 | waitq_initialize(&wq);
|
|---|
| [a35b458] | 704 |
|
|---|
| [897fd8f1] | 705 | (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING, NULL);
|
|---|
| [f761f1eb] | 706 | }
|
|---|
| 707 |
|
|---|
| [ef1eab7] | 708 | static void thread_print(thread_t *thread, bool additional)
|
|---|
| [5dcee525] | 709 | {
|
|---|
| [1ba37fa] | 710 | uint64_t ucycles, kcycles;
|
|---|
| 711 | char usuffix, ksuffix;
|
|---|
| [da1bafb] | 712 | order_suffix(thread->ucycles, &ucycles, &usuffix);
|
|---|
| 713 | order_suffix(thread->kcycles, &kcycles, &ksuffix);
|
|---|
| [a35b458] | 714 |
|
|---|
| [577f042a] | 715 | char *name;
|
|---|
| 716 | if (str_cmp(thread->name, "uinit") == 0)
|
|---|
| 717 | name = thread->task->name;
|
|---|
| 718 | else
|
|---|
| 719 | name = thread->name;
|
|---|
| [a35b458] | 720 |
|
|---|
| [52755f1] | 721 | #ifdef __32_BITS__
|
|---|
| [ef1eab7] | 722 | if (additional)
|
|---|
| [ae0300b5] | 723 | printf("%-8" PRIu64 " %10p %10p %9" PRIu64 "%c %9" PRIu64 "%c ",
|
|---|
| [577f042a] | 724 | thread->tid, thread->thread_code, thread->kstack,
|
|---|
| 725 | ucycles, usuffix, kcycles, ksuffix);
|
|---|
| [48dcc69] | 726 | else
|
|---|
| [ae0300b5] | 727 | printf("%-8" PRIu64 " %-14s %10p %-8s %10p %-5" PRIu32 "\n",
|
|---|
| [577f042a] | 728 | thread->tid, name, thread, thread_states[thread->state],
|
|---|
| [26aafe8] | 729 | thread->task, thread->task->container);
|
|---|
| [52755f1] | 730 | #endif
|
|---|
| [a35b458] | 731 |
|
|---|
| [52755f1] | 732 | #ifdef __64_BITS__
|
|---|
| [ef1eab7] | 733 | if (additional)
|
|---|
| [ae0300b5] | 734 | printf("%-8" PRIu64 " %18p %18p\n"
|
|---|
| [48dcc69] | 735 | " %9" PRIu64 "%c %9" PRIu64 "%c ",
|
|---|
| 736 | thread->tid, thread->thread_code, thread->kstack,
|
|---|
| 737 | ucycles, usuffix, kcycles, ksuffix);
|
|---|
| [5dcee525] | 738 | else
|
|---|
| [ae0300b5] | 739 | printf("%-8" PRIu64 " %-14s %18p %-8s %18p %-5" PRIu32 "\n",
|
|---|
| [577f042a] | 740 | thread->tid, name, thread, thread_states[thread->state],
|
|---|
| [26aafe8] | 741 | thread->task, thread->task->container);
|
|---|
| [48dcc69] | 742 | #endif
|
|---|
| [a35b458] | 743 |
|
|---|
| [ef1eab7] | 744 | if (additional) {
|
|---|
| [48dcc69] | 745 | if (thread->cpu)
|
|---|
| 746 | printf("%-5u", thread->cpu->id);
|
|---|
| 747 | else
|
|---|
| 748 | printf("none ");
|
|---|
| [a35b458] | 749 |
|
|---|
| [48dcc69] | 750 | if (thread->state == Sleeping) {
|
|---|
| [52755f1] | 751 | #ifdef __32_BITS__
|
|---|
| [48dcc69] | 752 | printf(" %10p", thread->sleep_queue);
|
|---|
| [52755f1] | 753 | #endif
|
|---|
| [a35b458] | 754 |
|
|---|
| [52755f1] | 755 | #ifdef __64_BITS__
|
|---|
| [48dcc69] | 756 | printf(" %18p", thread->sleep_queue);
|
|---|
| [52755f1] | 757 | #endif
|
|---|
| [48dcc69] | 758 | }
|
|---|
| [a35b458] | 759 |
|
|---|
| [48dcc69] | 760 | printf("\n");
|
|---|
| [43b1e86] | 761 | }
|
|---|
| [5dcee525] | 762 | }
|
|---|
| 763 |
|
|---|
| [da1bafb] | 764 | /** Print list of threads debug info
|
|---|
| [48dcc69] | 765 | *
|
|---|
| 766 | * @param additional Print additional information.
|
|---|
| [da1bafb] | 767 | *
|
|---|
| 768 | */
|
|---|
| [48dcc69] | 769 | void thread_print_list(bool additional)
|
|---|
| [55ab0f1] | 770 | {
|
|---|
| [ef1eab7] | 771 | thread_t *thread;
|
|---|
| 772 |
|
|---|
| [55ab0f1] | 773 | /* Messing with thread structures, avoid deadlock */
|
|---|
| [da1bafb] | 774 | irq_spinlock_lock(&threads_lock, true);
|
|---|
| [a35b458] | 775 |
|
|---|
| [da1bafb] | 776 | #ifdef __32_BITS__
|
|---|
| [48dcc69] | 777 | if (additional)
|
|---|
| [577f042a] | 778 | printf("[id ] [code ] [stack ] [ucycles ] [kcycles ]"
|
|---|
| 779 | " [cpu] [waitqueue]\n");
|
|---|
| [48dcc69] | 780 | else
|
|---|
| 781 | printf("[id ] [name ] [address ] [state ] [task ]"
|
|---|
| [26aafe8] | 782 | " [ctn]\n");
|
|---|
| [52755f1] | 783 | #endif
|
|---|
| [a35b458] | 784 |
|
|---|
| [52755f1] | 785 | #ifdef __64_BITS__
|
|---|
| [48dcc69] | 786 | if (additional) {
|
|---|
| 787 | printf("[id ] [code ] [stack ]\n"
|
|---|
| 788 | " [ucycles ] [kcycles ] [cpu] [waitqueue ]\n");
|
|---|
| 789 | } else
|
|---|
| 790 | printf("[id ] [name ] [address ] [state ]"
|
|---|
| [26aafe8] | 791 | " [task ] [ctn]\n");
|
|---|
| [52755f1] | 792 | #endif
|
|---|
| [a35b458] | 793 |
|
|---|
| [aab5e46] | 794 | thread = thread_first();
|
|---|
| 795 | while (thread != NULL) {
|
|---|
| [ef1eab7] | 796 | thread_print(thread, additional);
|
|---|
| [aab5e46] | 797 | thread = thread_next(thread);
|
|---|
| [ef1eab7] | 798 | }
|
|---|
| [a35b458] | 799 |
|
|---|
| [da1bafb] | 800 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| [55ab0f1] | 801 | }
|
|---|
| [9f52563] | 802 |
|
|---|
| [016acbe] | 803 | /** Check whether thread exists.
|
|---|
| 804 | *
|
|---|
| 805 | * Note that threads_lock must be already held and
|
|---|
| 806 | * interrupts must be already disabled.
|
|---|
| 807 | *
|
|---|
| [da1bafb] | 808 | * @param thread Pointer to thread.
|
|---|
| [016acbe] | 809 | *
|
|---|
| 810 | * @return True if thread t is known to the system, false otherwise.
|
|---|
| [da1bafb] | 811 | *
|
|---|
| [016acbe] | 812 | */
|
|---|
| [da1bafb] | 813 | bool thread_exists(thread_t *thread)
|
|---|
| [016acbe] | 814 | {
|
|---|
| [63e27ef] | 815 | assert(interrupts_disabled());
|
|---|
| 816 | assert(irq_spinlock_locked(&threads_lock));
|
|---|
| [1d432f9] | 817 |
|
|---|
| [ef1eab7] | 818 | odlink_t *odlink = odict_find_eq(&threads, thread, NULL);
|
|---|
| 819 | return odlink != NULL;
|
|---|
| [016acbe] | 820 | }
|
|---|
| 821 |
|
|---|
| [cce6acf] | 822 | /** Update accounting of current thread.
|
|---|
| 823 | *
|
|---|
| 824 | * Note that thread_lock on THREAD must be already held and
|
|---|
| 825 | * interrupts must be already disabled.
|
|---|
| 826 | *
|
|---|
| [da1bafb] | 827 | * @param user True to update user accounting, false for kernel.
|
|---|
| 828 | *
|
|---|
| [cce6acf] | 829 | */
|
|---|
| [a2a00e8] | 830 | void thread_update_accounting(bool user)
|
|---|
| [cce6acf] | 831 | {
|
|---|
| 832 | uint64_t time = get_cycle();
|
|---|
| [1d432f9] | 833 |
|
|---|
| [63e27ef] | 834 | assert(interrupts_disabled());
|
|---|
| 835 | assert(irq_spinlock_locked(&THREAD->lock));
|
|---|
| [a35b458] | 836 |
|
|---|
| [da1bafb] | 837 | if (user)
|
|---|
| [a2a00e8] | 838 | THREAD->ucycles += time - THREAD->last_cycle;
|
|---|
| [da1bafb] | 839 | else
|
|---|
| [a2a00e8] | 840 | THREAD->kcycles += time - THREAD->last_cycle;
|
|---|
| [a35b458] | 841 |
|
|---|
| [cce6acf] | 842 | THREAD->last_cycle = time;
|
|---|
| 843 | }
|
|---|
| 844 |
|
|---|
| [e1b6742] | 845 | /** Find thread structure corresponding to thread ID.
|
|---|
| 846 | *
|
|---|
| 847 | * The threads_lock must be already held by the caller of this function and
|
|---|
| 848 | * interrupts must be disabled.
|
|---|
| 849 | *
|
|---|
| 850 | * @param id Thread ID.
|
|---|
| 851 | *
|
|---|
| 852 | * @return Thread structure address or NULL if there is no such thread ID.
|
|---|
| 853 | *
|
|---|
| 854 | */
|
|---|
| 855 | thread_t *thread_find_by_id(thread_id_t thread_id)
|
|---|
| 856 | {
|
|---|
| [ef1eab7] | 857 | thread_t *thread;
|
|---|
| 858 |
|
|---|
| [63e27ef] | 859 | assert(interrupts_disabled());
|
|---|
| 860 | assert(irq_spinlock_locked(&threads_lock));
|
|---|
| [a35b458] | 861 |
|
|---|
| [aab5e46] | 862 | thread = thread_first();
|
|---|
| 863 | while (thread != NULL) {
|
|---|
| [ef1eab7] | 864 | if (thread->tid == thread_id)
|
|---|
| 865 | return thread;
|
|---|
| [a35b458] | 866 |
|
|---|
| [aab5e46] | 867 | thread = thread_next(thread);
|
|---|
| [ef1eab7] | 868 | }
|
|---|
| [a35b458] | 869 |
|
|---|
| [ef1eab7] | 870 | return NULL;
|
|---|
| [e1b6742] | 871 | }
|
|---|
| 872 |
|
|---|
| [aab5e46] | 873 | /** Get count of threads.
|
|---|
| 874 | *
|
|---|
| 875 | * @return Number of threads in the system
|
|---|
| 876 | */
|
|---|
| 877 | size_t thread_count(void)
|
|---|
| 878 | {
|
|---|
| 879 | assert(interrupts_disabled());
|
|---|
| 880 | assert(irq_spinlock_locked(&threads_lock));
|
|---|
| 881 |
|
|---|
| 882 | return odict_count(&threads);
|
|---|
| 883 | }
|
|---|
| 884 |
|
|---|
| 885 | /** Get first thread.
|
|---|
| 886 | *
|
|---|
| 887 | * @return Pointer to first thread or @c NULL if there are none.
|
|---|
| 888 | */
|
|---|
| 889 | thread_t *thread_first(void)
|
|---|
| 890 | {
|
|---|
| 891 | odlink_t *odlink;
|
|---|
| 892 |
|
|---|
| 893 | assert(interrupts_disabled());
|
|---|
| 894 | assert(irq_spinlock_locked(&threads_lock));
|
|---|
| 895 |
|
|---|
| 896 | odlink = odict_first(&threads);
|
|---|
| 897 | if (odlink == NULL)
|
|---|
| 898 | return NULL;
|
|---|
| 899 |
|
|---|
| 900 | return odict_get_instance(odlink, thread_t, lthreads);
|
|---|
| 901 | }
|
|---|
| 902 |
|
|---|
| 903 | /** Get next thread.
|
|---|
| 904 | *
|
|---|
| 905 | * @param cur Current thread
|
|---|
| 906 | * @return Pointer to next thread or @c NULL if there are no more threads.
|
|---|
| 907 | */
|
|---|
| 908 | thread_t *thread_next(thread_t *cur)
|
|---|
| 909 | {
|
|---|
| 910 | odlink_t *odlink;
|
|---|
| 911 |
|
|---|
| 912 | assert(interrupts_disabled());
|
|---|
| 913 | assert(irq_spinlock_locked(&threads_lock));
|
|---|
| 914 |
|
|---|
| 915 | odlink = odict_next(&cur->lthreads, &threads);
|
|---|
| 916 | if (odlink == NULL)
|
|---|
| 917 | return NULL;
|
|---|
| 918 |
|
|---|
| 919 | return odict_get_instance(odlink, thread_t, lthreads);
|
|---|
| 920 | }
|
|---|
| 921 |
|
|---|
| [5b7a107] | 922 | #ifdef CONFIG_UDEBUG
|
|---|
| 923 |
|
|---|
| [df58e44] | 924 | void thread_stack_trace(thread_id_t thread_id)
|
|---|
| 925 | {
|
|---|
| 926 | irq_spinlock_lock(&threads_lock, true);
|
|---|
| [a35b458] | 927 |
|
|---|
| [df58e44] | 928 | thread_t *thread = thread_find_by_id(thread_id);
|
|---|
| 929 | if (thread == NULL) {
|
|---|
| 930 | printf("No such thread.\n");
|
|---|
| 931 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 932 | return;
|
|---|
| 933 | }
|
|---|
| [a35b458] | 934 |
|
|---|
| [df58e44] | 935 | irq_spinlock_lock(&thread->lock, false);
|
|---|
| [a35b458] | 936 |
|
|---|
| [df58e44] | 937 | /*
|
|---|
| 938 | * Schedule a stack trace to be printed
|
|---|
| 939 | * just before the thread is scheduled next.
|
|---|
| 940 | *
|
|---|
| 941 | * If the thread is sleeping then try to interrupt
|
|---|
| 942 | * the sleep. Any request for printing an uspace stack
|
|---|
| 943 | * trace from within the kernel should be always
|
|---|
| 944 | * considered a last resort debugging means, therefore
|
|---|
| 945 | * forcing the thread's sleep to be interrupted
|
|---|
| 946 | * is probably justifiable.
|
|---|
| 947 | */
|
|---|
| [a35b458] | 948 |
|
|---|
| [df58e44] | 949 | bool sleeping = false;
|
|---|
| 950 | istate_t *istate = thread->udebug.uspace_state;
|
|---|
| 951 | if (istate != NULL) {
|
|---|
| 952 | printf("Scheduling thread stack trace.\n");
|
|---|
| 953 | thread->btrace = true;
|
|---|
| 954 | if (thread->state == Sleeping)
|
|---|
| 955 | sleeping = true;
|
|---|
| 956 | } else
|
|---|
| 957 | printf("Thread interrupt state not available.\n");
|
|---|
| [a35b458] | 958 |
|
|---|
| [df58e44] | 959 | irq_spinlock_unlock(&thread->lock, false);
|
|---|
| [a35b458] | 960 |
|
|---|
| [df58e44] | 961 | if (sleeping)
|
|---|
| 962 | waitq_interrupt_sleep(thread);
|
|---|
| [a35b458] | 963 |
|
|---|
| [df58e44] | 964 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 965 | }
|
|---|
| [e1b6742] | 966 |
|
|---|
| [5b7a107] | 967 | #endif /* CONFIG_UDEBUG */
|
|---|
| [e1b6742] | 968 |
|
|---|
| [ef1eab7] | 969 | /** Get key function for the @c threads ordered dictionary.
|
|---|
| 970 | *
|
|---|
| 971 | * @param odlink Link
|
|---|
| 972 | * @return Pointer to thread structure cast as 'void *'
|
|---|
| 973 | */
|
|---|
| 974 | static void *threads_getkey(odlink_t *odlink)
|
|---|
| 975 | {
|
|---|
| 976 | thread_t *thread = odict_get_instance(odlink, thread_t, lthreads);
|
|---|
| 977 | return (void *) thread;
|
|---|
| 978 | }
|
|---|
| 979 |
|
|---|
| 980 | /** Key comparison function for the @c threads ordered dictionary.
|
|---|
| 981 | *
|
|---|
| 982 | * @param a Pointer to thread A
|
|---|
| 983 | * @param b Pointer to thread B
|
|---|
| 984 | * @return -1, 0, 1 iff pointer to A is less than, equal to, greater than B
|
|---|
| 985 | */
|
|---|
| 986 | static int threads_cmp(void *a, void *b)
|
|---|
| 987 | {
|
|---|
| 988 | if (a > b)
|
|---|
| 989 | return -1;
|
|---|
| 990 | else if (a == b)
|
|---|
| 991 | return 0;
|
|---|
| 992 | else
|
|---|
| 993 | return +1;
|
|---|
| 994 | }
|
|---|
| 995 |
|
|---|
| [9f52563] | 996 | /** Process syscall to create new thread.
|
|---|
| 997 | *
|
|---|
| 998 | */
|
|---|
| [b7fd2a0] | 999 | sys_errno_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name,
|
|---|
| [7faabb7] | 1000 | size_t name_len, thread_id_t *uspace_thread_id)
|
|---|
| [9f52563] | 1001 | {
|
|---|
| [24345a5] | 1002 | if (name_len > THREAD_NAME_BUFLEN - 1)
|
|---|
| [7faabb7] | 1003 | name_len = THREAD_NAME_BUFLEN - 1;
|
|---|
| [a35b458] | 1004 |
|
|---|
| [da1bafb] | 1005 | char namebuf[THREAD_NAME_BUFLEN];
|
|---|
| [b7fd2a0] | 1006 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
|---|
| [a53ed3a] | 1007 | if (rc != EOK)
|
|---|
| [b7fd2a0] | 1008 | return (sys_errno_t) rc;
|
|---|
| [a35b458] | 1009 |
|
|---|
| [b60c582] | 1010 | namebuf[name_len] = 0;
|
|---|
| [a35b458] | 1011 |
|
|---|
| [4680ef5] | 1012 | /*
|
|---|
| 1013 | * In case of failure, kernel_uarg will be deallocated in this function.
|
|---|
| 1014 | * In case of success, kernel_uarg will be freed in uinit().
|
|---|
| 1015 | */
|
|---|
| [da1bafb] | 1016 | uspace_arg_t *kernel_uarg =
|
|---|
| [11b285d] | 1017 | (uspace_arg_t *) malloc(sizeof(uspace_arg_t));
|
|---|
| [7473807] | 1018 | if (!kernel_uarg)
|
|---|
| 1019 | return (sys_errno_t) ENOMEM;
|
|---|
| [a35b458] | 1020 |
|
|---|
| [e3c762cd] | 1021 | rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
|
|---|
| [a53ed3a] | 1022 | if (rc != EOK) {
|
|---|
| [e3c762cd] | 1023 | free(kernel_uarg);
|
|---|
| [b7fd2a0] | 1024 | return (sys_errno_t) rc;
|
|---|
| [e3c762cd] | 1025 | }
|
|---|
| [a35b458] | 1026 |
|
|---|
| [da1bafb] | 1027 | thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
|
|---|
| [6eef3c4] | 1028 | THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf);
|
|---|
| [da1bafb] | 1029 | if (thread) {
|
|---|
| [d8431986] | 1030 | if (uspace_thread_id != NULL) {
|
|---|
| [da1bafb] | 1031 | rc = copy_to_uspace(uspace_thread_id, &thread->tid,
|
|---|
| 1032 | sizeof(thread->tid));
|
|---|
| [a53ed3a] | 1033 | if (rc != EOK) {
|
|---|
| [d8431986] | 1034 | /*
|
|---|
| 1035 | * We have encountered a failure, but the thread
|
|---|
| 1036 | * has already been created. We need to undo its
|
|---|
| 1037 | * creation now.
|
|---|
| 1038 | */
|
|---|
| [a35b458] | 1039 |
|
|---|
| [d8431986] | 1040 | /*
|
|---|
| [ea7890e7] | 1041 | * The new thread structure is initialized, but
|
|---|
| 1042 | * is still not visible to the system.
|
|---|
| [d8431986] | 1043 | * We can safely deallocate it.
|
|---|
| 1044 | */
|
|---|
| [82d515e9] | 1045 | slab_free(thread_cache, thread);
|
|---|
| [da1bafb] | 1046 | free(kernel_uarg);
|
|---|
| [a35b458] | 1047 |
|
|---|
| [b7fd2a0] | 1048 | return (sys_errno_t) rc;
|
|---|
| [3bacee1] | 1049 | }
|
|---|
| [d8431986] | 1050 | }
|
|---|
| [a35b458] | 1051 |
|
|---|
| [9a1b20c] | 1052 | #ifdef CONFIG_UDEBUG
|
|---|
| [13964ef] | 1053 | /*
|
|---|
| 1054 | * Generate udebug THREAD_B event and attach the thread.
|
|---|
| 1055 | * This must be done atomically (with the debug locks held),
|
|---|
| 1056 | * otherwise we would either miss some thread or receive
|
|---|
| 1057 | * THREAD_B events for threads that already existed
|
|---|
| 1058 | * and could be detected with THREAD_READ before.
|
|---|
| 1059 | */
|
|---|
| [da1bafb] | 1060 | udebug_thread_b_event_attach(thread, TASK);
|
|---|
| [13964ef] | 1061 | #else
|
|---|
| [da1bafb] | 1062 | thread_attach(thread, TASK);
|
|---|
| [9a1b20c] | 1063 | #endif
|
|---|
| [da1bafb] | 1064 | thread_ready(thread);
|
|---|
| [a35b458] | 1065 |
|
|---|
| [d8431986] | 1066 | return 0;
|
|---|
| [201abde] | 1067 | } else
|
|---|
| [0f250f9] | 1068 | free(kernel_uarg);
|
|---|
| [a35b458] | 1069 |
|
|---|
| [b7fd2a0] | 1070 | return (sys_errno_t) ENOMEM;
|
|---|
| [9f52563] | 1071 | }
|
|---|
| 1072 |
|
|---|
| 1073 | /** Process syscall to terminate thread.
|
|---|
| 1074 | *
|
|---|
| 1075 | */
|
|---|
| [b7fd2a0] | 1076 | sys_errno_t sys_thread_exit(int uspace_status)
|
|---|
| [9f52563] | 1077 | {
|
|---|
| [68091bd] | 1078 | thread_exit();
|
|---|
| [9f52563] | 1079 | }
|
|---|
| [b45c443] | 1080 |
|
|---|
| [3ce7f082] | 1081 | /** Syscall for getting TID.
|
|---|
| 1082 | *
|
|---|
| [201abde] | 1083 | * @param uspace_thread_id Userspace address of 8-byte buffer where to store
|
|---|
| 1084 | * current thread ID.
|
|---|
| 1085 | *
|
|---|
| 1086 | * @return 0 on success or an error code from @ref errno.h.
|
|---|
| [da1bafb] | 1087 | *
|
|---|
| [b45c443] | 1088 | */
|
|---|
| [b7fd2a0] | 1089 | sys_errno_t sys_thread_get_id(thread_id_t *uspace_thread_id)
|
|---|
| [3ce7f082] | 1090 | {
|
|---|
| 1091 | /*
|
|---|
| 1092 | * No need to acquire lock on THREAD because tid
|
|---|
| 1093 | * remains constant for the lifespan of the thread.
|
|---|
| [da1bafb] | 1094 | *
|
|---|
| [3ce7f082] | 1095 | */
|
|---|
| [b7fd2a0] | 1096 | return (sys_errno_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
|
|---|
| [201abde] | 1097 | sizeof(THREAD->tid));
|
|---|
| [3ce7f082] | 1098 | }
|
|---|
| [6f4495f5] | 1099 |
|
|---|
| [d9ece1cb] | 1100 | /** Syscall wrapper for sleeping. */
|
|---|
| [b7fd2a0] | 1101 | sys_errno_t sys_thread_usleep(uint32_t usec)
|
|---|
| [d9ece1cb] | 1102 | {
|
|---|
| [22e6802] | 1103 | thread_usleep(usec);
|
|---|
| [d9ece1cb] | 1104 | return 0;
|
|---|
| 1105 | }
|
|---|
| 1106 |
|
|---|
| [b7fd2a0] | 1107 | sys_errno_t sys_thread_udelay(uint32_t usec)
|
|---|
| [7e7b791] | 1108 | {
|
|---|
| [8d6c1f1] | 1109 | delay(usec);
|
|---|
| [7e7b791] | 1110 | return 0;
|
|---|
| 1111 | }
|
|---|
| 1112 |
|
|---|
| [3ce7f082] | 1113 | /** @}
|
|---|
| 1114 | */
|
|---|