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