[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>
|
---|
[d314571] | 50 | #include <synch/syswaitq.h>
|
---|
[f761f1eb] | 51 | #include <cpu.h>
|
---|
[e535eeb] | 52 | #include <str.h>
|
---|
[f761f1eb] | 53 | #include <context.h>
|
---|
[5c9a08b] | 54 | #include <adt/list.h>
|
---|
[ef1eab7] | 55 | #include <adt/odict.h>
|
---|
[f761f1eb] | 56 | #include <time/clock.h>
|
---|
[b3f8fb7] | 57 | #include <time/timeout.h>
|
---|
[8d6c1f1] | 58 | #include <time/delay.h>
|
---|
[4ffa9e0] | 59 | #include <config.h>
|
---|
| 60 | #include <arch/interrupt.h>
|
---|
[26a8604f] | 61 | #include <smp/ipi.h>
|
---|
[f2ffad4] | 62 | #include <arch/faddr.h>
|
---|
[23684b7] | 63 | #include <atomic.h>
|
---|
[44a7ee5] | 64 | #include <mem.h>
|
---|
[bab75df6] | 65 | #include <stdio.h>
|
---|
[aafed15] | 66 | #include <stdlib.h>
|
---|
[9f52563] | 67 | #include <main/uinit.h>
|
---|
[e3c762cd] | 68 | #include <syscall/copy.h>
|
---|
| 69 | #include <errno.h>
|
---|
[aae365bc] | 70 | #include <debug.h>
|
---|
[111b9b9] | 71 | #include <halt.h>
|
---|
[52755f1] | 72 |
|
---|
[fe19611] | 73 | /** Thread states */
|
---|
[a000878c] | 74 | const char *thread_states[] = {
|
---|
[fe19611] | 75 | "Invalid",
|
---|
| 76 | "Running",
|
---|
| 77 | "Sleeping",
|
---|
| 78 | "Ready",
|
---|
| 79 | "Entering",
|
---|
| 80 | "Exiting",
|
---|
[48d14222] | 81 | "Lingering"
|
---|
[e1b6742] | 82 | };
|
---|
| 83 |
|
---|
[111b9b9] | 84 | enum sleep_state {
|
---|
| 85 | SLEEP_INITIAL,
|
---|
| 86 | SLEEP_ASLEEP,
|
---|
| 87 | SLEEP_WOKE,
|
---|
| 88 | };
|
---|
| 89 |
|
---|
[ef1eab7] | 90 | /** Lock protecting the @c threads ordered dictionary .
|
---|
[4e33b6b] | 91 | *
|
---|
| 92 | * For locking rules, see declaration thereof.
|
---|
| 93 | */
|
---|
[da1bafb] | 94 | IRQ_SPINLOCK_INITIALIZE(threads_lock);
|
---|
[88169d9] | 95 |
|
---|
[ef1eab7] | 96 | /** Ordered dictionary of all threads by their address (i.e. pointer to
|
---|
| 97 | * the thread_t structure).
|
---|
[88169d9] | 98 | *
|
---|
[ef1eab7] | 99 | * When a thread is found in the @c threads ordered dictionary, it is
|
---|
| 100 | * guaranteed to exist as long as the @c threads_lock is held.
|
---|
[da1bafb] | 101 | *
|
---|
[ef1eab7] | 102 | * Members are of type thread_t.
|
---|
[1871118] | 103 | *
|
---|
| 104 | * This structure contains weak references. Any reference from it must not leave
|
---|
| 105 | * threads_lock critical section unless strengthened via thread_try_ref().
|
---|
[88169d9] | 106 | */
|
---|
[ef1eab7] | 107 | odict_t threads;
|
---|
[f761f1eb] | 108 |
|
---|
[da1bafb] | 109 | IRQ_SPINLOCK_STATIC_INITIALIZE(tidlock);
|
---|
| 110 | static thread_id_t last_tid = 0;
|
---|
[f761f1eb] | 111 |
|
---|
[82d515e9] | 112 | static slab_cache_t *thread_cache;
|
---|
[da1bafb] | 113 |
|
---|
[ef1eab7] | 114 | static void *threads_getkey(odlink_t *);
|
---|
| 115 | static int threads_cmp(void *, void *);
|
---|
| 116 |
|
---|
[4e33b6b] | 117 | /** Thread wrapper.
|
---|
[70527f1] | 118 | *
|
---|
[4e33b6b] | 119 | * This wrapper is provided to ensure that every thread makes a call to
|
---|
| 120 | * thread_exit() when its implementing function returns.
|
---|
[f761f1eb] | 121 | *
|
---|
[22f7769] | 122 | * interrupts_disable() is assumed.
|
---|
[70527f1] | 123 | *
|
---|
[f761f1eb] | 124 | */
|
---|
[e16e036a] | 125 | static void cushion(void)
|
---|
[f761f1eb] | 126 | {
|
---|
[43114c5] | 127 | void (*f)(void *) = THREAD->thread_code;
|
---|
| 128 | void *arg = THREAD->thread_arg;
|
---|
[449dc1ed] | 129 | THREAD->last_cycle = get_cycle();
|
---|
[a35b458] | 130 |
|
---|
[0313ff0] | 131 | /* This is where each thread wakes up after its creation */
|
---|
[da1bafb] | 132 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
[22f7769] | 133 | interrupts_enable();
|
---|
[a35b458] | 134 |
|
---|
[f761f1eb] | 135 | f(arg);
|
---|
[a35b458] | 136 |
|
---|
[0313ff0] | 137 | /* Accumulate accounting to the task */
|
---|
[da1bafb] | 138 | irq_spinlock_lock(&THREAD->lock, true);
|
---|
[62b6d17] | 139 | if (!THREAD->uncounted) {
|
---|
[a2a00e8] | 140 | thread_update_accounting(true);
|
---|
| 141 | uint64_t ucycles = THREAD->ucycles;
|
---|
| 142 | THREAD->ucycles = 0;
|
---|
| 143 | uint64_t kcycles = THREAD->kcycles;
|
---|
| 144 | THREAD->kcycles = 0;
|
---|
[a35b458] | 145 |
|
---|
[da1bafb] | 146 | irq_spinlock_pass(&THREAD->lock, &TASK->lock);
|
---|
[a2a00e8] | 147 | TASK->ucycles += ucycles;
|
---|
| 148 | TASK->kcycles += kcycles;
|
---|
[da1bafb] | 149 | irq_spinlock_unlock(&TASK->lock, true);
|
---|
[62b6d17] | 150 | } else
|
---|
[da1bafb] | 151 | irq_spinlock_unlock(&THREAD->lock, true);
|
---|
[a35b458] | 152 |
|
---|
[f761f1eb] | 153 | thread_exit();
|
---|
[a35b458] | 154 |
|
---|
[da1bafb] | 155 | /* Not reached */
|
---|
[f761f1eb] | 156 | }
|
---|
| 157 |
|
---|
[da1bafb] | 158 | /** Initialization and allocation for thread_t structure
|
---|
| 159 | *
|
---|
| 160 | */
|
---|
[b7fd2a0] | 161 | static errno_t thr_constructor(void *obj, unsigned int kmflags)
|
---|
[266294a9] | 162 | {
|
---|
[da1bafb] | 163 | thread_t *thread = (thread_t *) obj;
|
---|
[a35b458] | 164 |
|
---|
[da1bafb] | 165 | irq_spinlock_initialize(&thread->lock, "thread_t_lock");
|
---|
| 166 | link_initialize(&thread->rq_link);
|
---|
| 167 | link_initialize(&thread->wq_link);
|
---|
| 168 | link_initialize(&thread->th_link);
|
---|
[a35b458] | 169 |
|
---|
[32fffef0] | 170 | /* call the architecture-specific part of the constructor */
|
---|
[da1bafb] | 171 | thr_constructor_arch(thread);
|
---|
[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
|
---|
[c477c80] | 181 | * by the construction of the assembly-language part of the TLB-miss
|
---|
[38ff925] | 182 | * handler).
|
---|
| 183 | *
|
---|
| 184 | * This restriction can be lifted once each architecture provides
|
---|
[c477c80] | 185 | * a similar guarantee, for example, by locking the kernel stack
|
---|
[38ff925] | 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 |
|
---|
[128359eb] | 192 | /*
|
---|
| 193 | * NOTE: All kernel stacks must be aligned to STACK_SIZE,
|
---|
| 194 | * see CURRENT.
|
---|
| 195 | */
|
---|
[d1da1ff2] | 196 |
|
---|
[cd3b380] | 197 | uintptr_t stack_phys =
|
---|
| 198 | frame_alloc(STACK_FRAMES, kmflags, STACK_SIZE - 1);
|
---|
[0366d09d] | 199 | if (!stack_phys)
|
---|
[7f11dc6] | 200 | return ENOMEM;
|
---|
[a35b458] | 201 |
|
---|
[cd3b380] | 202 | thread->kstack = (uint8_t *) PA2KA(stack_phys);
|
---|
[a35b458] | 203 |
|
---|
[9a1b20c] | 204 | #ifdef CONFIG_UDEBUG
|
---|
[da1bafb] | 205 | mutex_initialize(&thread->udebug.lock, MUTEX_PASSIVE);
|
---|
[9a1b20c] | 206 | #endif
|
---|
[a35b458] | 207 |
|
---|
[7f11dc6] | 208 | return EOK;
|
---|
[266294a9] | 209 | }
|
---|
| 210 |
|
---|
| 211 | /** Destruction of thread_t object */
|
---|
[da1bafb] | 212 | static size_t thr_destructor(void *obj)
|
---|
[266294a9] | 213 | {
|
---|
[da1bafb] | 214 | thread_t *thread = (thread_t *) obj;
|
---|
[a35b458] | 215 |
|
---|
[32fffef0] | 216 | /* call the architecture-specific part of the destructor */
|
---|
[da1bafb] | 217 | thr_destructor_arch(thread);
|
---|
[a35b458] | 218 |
|
---|
[5df1963] | 219 | frame_free(KA2PA(thread->kstack), STACK_FRAMES);
|
---|
[a35b458] | 220 |
|
---|
[e7c4115d] | 221 | return STACK_FRAMES; /* number of frames freed */
|
---|
[266294a9] | 222 | }
|
---|
[70527f1] | 223 |
|
---|
| 224 | /** Initialize threads
|
---|
| 225 | *
|
---|
| 226 | * Initialize kernel threads support.
|
---|
| 227 | *
|
---|
| 228 | */
|
---|
[f761f1eb] | 229 | void thread_init(void)
|
---|
| 230 | {
|
---|
[43114c5] | 231 | THREAD = NULL;
|
---|
[a35b458] | 232 |
|
---|
[e3306d04] | 233 | atomic_store(&nrdy, 0);
|
---|
[0366d09d] | 234 | thread_cache = slab_cache_create("thread_t", sizeof(thread_t), _Alignof(thread_t),
|
---|
[6f4495f5] | 235 | thr_constructor, thr_destructor, 0);
|
---|
[a35b458] | 236 |
|
---|
[ef1eab7] | 237 | odict_initialize(&threads, threads_getkey, threads_cmp);
|
---|
[016acbe] | 238 | }
|
---|
[70527f1] | 239 |
|
---|
[6eef3c4] | 240 | /** Wire thread to the given CPU
|
---|
| 241 | *
|
---|
| 242 | * @param cpu CPU to wire the thread to.
|
---|
| 243 | *
|
---|
| 244 | */
|
---|
| 245 | void thread_wire(thread_t *thread, cpu_t *cpu)
|
---|
| 246 | {
|
---|
| 247 | irq_spinlock_lock(&thread->lock, true);
|
---|
| 248 | thread->cpu = cpu;
|
---|
| 249 | thread->wired = true;
|
---|
| 250 | irq_spinlock_unlock(&thread->lock, true);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[8a64e81e] | 253 | /** Invoked right before thread_ready() readies the thread. thread is locked. */
|
---|
| 254 | static void before_thread_is_ready(thread_t *thread)
|
---|
| 255 | {
|
---|
[63e27ef] | 256 | assert(irq_spinlock_locked(&thread->lock));
|
---|
[8a64e81e] | 257 | }
|
---|
| 258 |
|
---|
[70527f1] | 259 | /** Make thread ready
|
---|
| 260 | *
|
---|
[1871118] | 261 | * Switch thread to the ready state. Consumes reference passed by the caller.
|
---|
[70527f1] | 262 | *
|
---|
[df58e44] | 263 | * @param thread Thread to make ready.
|
---|
[70527f1] | 264 | *
|
---|
| 265 | */
|
---|
[da1bafb] | 266 | void thread_ready(thread_t *thread)
|
---|
[f761f1eb] | 267 | {
|
---|
[da1bafb] | 268 | irq_spinlock_lock(&thread->lock, true);
|
---|
[a35b458] | 269 |
|
---|
[63e27ef] | 270 | assert(thread->state != Ready);
|
---|
[518dd43] | 271 |
|
---|
[8a64e81e] | 272 | before_thread_is_ready(thread);
|
---|
[a35b458] | 273 |
|
---|
[6eef3c4] | 274 | int i = (thread->priority < RQ_COUNT - 1) ?
|
---|
| 275 | ++thread->priority : thread->priority;
|
---|
[518dd43] | 276 |
|
---|
[8ad7dd1] | 277 | cpu_t *cpu;
|
---|
| 278 | if (thread->wired || thread->nomigrate || thread->fpu_context_engaged) {
|
---|
| 279 | /* Cannot ready to another CPU */
|
---|
[63e27ef] | 280 | assert(thread->cpu != NULL);
|
---|
[8ad7dd1] | 281 | cpu = thread->cpu;
|
---|
| 282 | } else if (thread->stolen) {
|
---|
| 283 | /* Ready to the stealing CPU */
|
---|
[6eef3c4] | 284 | cpu = CPU;
|
---|
[8ad7dd1] | 285 | } else if (thread->cpu) {
|
---|
| 286 | /* Prefer the CPU on which the thread ran last */
|
---|
[63e27ef] | 287 | assert(thread->cpu != NULL);
|
---|
[8ad7dd1] | 288 | cpu = thread->cpu;
|
---|
| 289 | } else {
|
---|
| 290 | cpu = CPU;
|
---|
| 291 | }
|
---|
[a35b458] | 292 |
|
---|
[da1bafb] | 293 | thread->state = Ready;
|
---|
[a35b458] | 294 |
|
---|
[da1bafb] | 295 | irq_spinlock_pass(&thread->lock, &(cpu->rq[i].lock));
|
---|
[a35b458] | 296 |
|
---|
[70527f1] | 297 | /*
|
---|
[da1bafb] | 298 | * Append thread to respective ready queue
|
---|
| 299 | * on respective processor.
|
---|
[f761f1eb] | 300 | */
|
---|
[a35b458] | 301 |
|
---|
[55b77d9] | 302 | list_append(&thread->rq_link, &cpu->rq[i].rq);
|
---|
[da1bafb] | 303 | cpu->rq[i].n++;
|
---|
| 304 | irq_spinlock_unlock(&(cpu->rq[i].lock), true);
|
---|
[a35b458] | 305 |
|
---|
[59e07c91] | 306 | atomic_inc(&nrdy);
|
---|
[248fc1a] | 307 | atomic_inc(&cpu->nrdy);
|
---|
[f761f1eb] | 308 | }
|
---|
| 309 |
|
---|
[70527f1] | 310 | /** Create new thread
|
---|
| 311 | *
|
---|
| 312 | * Create a new thread.
|
---|
| 313 | *
|
---|
[da1bafb] | 314 | * @param func Thread's implementing function.
|
---|
| 315 | * @param arg Thread's implementing function argument.
|
---|
| 316 | * @param task Task to which the thread belongs. The caller must
|
---|
| 317 | * guarantee that the task won't cease to exist during the
|
---|
| 318 | * call. The task's lock may not be held.
|
---|
| 319 | * @param flags Thread flags.
|
---|
| 320 | * @param name Symbolic name (a copy is made).
|
---|
[70527f1] | 321 | *
|
---|
[da1bafb] | 322 | * @return New thread's structure on success, NULL on failure.
|
---|
[70527f1] | 323 | *
|
---|
| 324 | */
|
---|
[3bacee1] | 325 | thread_t *thread_create(void (*func)(void *), void *arg, task_t *task,
|
---|
[6eef3c4] | 326 | thread_flags_t flags, const char *name)
|
---|
[f761f1eb] | 327 | {
|
---|
[abf6c01] | 328 | thread_t *thread = (thread_t *) slab_alloc(thread_cache, FRAME_ATOMIC);
|
---|
[da1bafb] | 329 | if (!thread)
|
---|
[2a46e10] | 330 | return NULL;
|
---|
[a35b458] | 331 |
|
---|
[1871118] | 332 | refcount_init(&thread->refcount);
|
---|
| 333 |
|
---|
[deacd722] | 334 | if (thread_create_arch(thread, flags) != EOK) {
|
---|
| 335 | slab_free(thread_cache, thread);
|
---|
| 336 | return NULL;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
[bb68433] | 339 | /* Not needed, but good for debugging */
|
---|
[26aafe8] | 340 | memsetb(thread->kstack, STACK_SIZE, 0);
|
---|
[a35b458] | 341 |
|
---|
[da1bafb] | 342 | irq_spinlock_lock(&tidlock, true);
|
---|
| 343 | thread->tid = ++last_tid;
|
---|
| 344 | irq_spinlock_unlock(&tidlock, true);
|
---|
[a35b458] | 345 |
|
---|
[edc64c0] | 346 | memset(&thread->saved_context, 0, sizeof(thread->saved_context));
|
---|
[da1bafb] | 347 | context_set(&thread->saved_context, FADDR(cushion),
|
---|
[26aafe8] | 348 | (uintptr_t) thread->kstack, STACK_SIZE);
|
---|
[a35b458] | 349 |
|
---|
[a6e55886] | 350 | current_initialize((current_t *) thread->kstack);
|
---|
[a35b458] | 351 |
|
---|
[da1bafb] | 352 | ipl_t ipl = interrupts_disable();
|
---|
[c030818] | 353 | thread->saved_ipl = interrupts_read();
|
---|
[bb68433] | 354 | interrupts_restore(ipl);
|
---|
[a35b458] | 355 |
|
---|
[da1bafb] | 356 | str_cpy(thread->name, THREAD_NAME_BUFLEN, name);
|
---|
[a35b458] | 357 |
|
---|
[da1bafb] | 358 | thread->thread_code = func;
|
---|
| 359 | thread->thread_arg = arg;
|
---|
| 360 | thread->ucycles = 0;
|
---|
| 361 | thread->kcycles = 0;
|
---|
[6eef3c4] | 362 | thread->uncounted =
|
---|
| 363 | ((flags & THREAD_FLAG_UNCOUNTED) == THREAD_FLAG_UNCOUNTED);
|
---|
[da1bafb] | 364 | thread->priority = -1; /* Start in rq[0] */
|
---|
| 365 | thread->cpu = NULL;
|
---|
[6eef3c4] | 366 | thread->wired = false;
|
---|
| 367 | thread->stolen = false;
|
---|
| 368 | thread->uspace =
|
---|
| 369 | ((flags & THREAD_FLAG_USPACE) == THREAD_FLAG_USPACE);
|
---|
[a35b458] | 370 |
|
---|
[43ac0cc] | 371 | thread->nomigrate = 0;
|
---|
[da1bafb] | 372 | thread->state = Entering;
|
---|
[a35b458] | 373 |
|
---|
[111b9b9] | 374 | atomic_init(&thread->sleep_queue, NULL);
|
---|
[a35b458] | 375 |
|
---|
[da1bafb] | 376 | thread->in_copy_from_uspace = false;
|
---|
| 377 | thread->in_copy_to_uspace = false;
|
---|
[a35b458] | 378 |
|
---|
[da1bafb] | 379 | thread->interrupted = false;
|
---|
[111b9b9] | 380 | atomic_init(&thread->sleep_state, SLEEP_INITIAL);
|
---|
| 381 |
|
---|
[da1bafb] | 382 | waitq_initialize(&thread->join_wq);
|
---|
[a35b458] | 383 |
|
---|
[da1bafb] | 384 | thread->task = task;
|
---|
[a35b458] | 385 |
|
---|
[6eef3c4] | 386 | thread->fpu_context_exists = false;
|
---|
| 387 | thread->fpu_context_engaged = false;
|
---|
[a35b458] | 388 |
|
---|
[ef1eab7] | 389 | odlink_initialize(&thread->lthreads);
|
---|
[a35b458] | 390 |
|
---|
[9a1b20c] | 391 | #ifdef CONFIG_UDEBUG
|
---|
[5b7a107] | 392 | /* Initialize debugging stuff */
|
---|
| 393 | thread->btrace = false;
|
---|
[da1bafb] | 394 | udebug_thread_initialize(&thread->udebug);
|
---|
[9a1b20c] | 395 | #endif
|
---|
[a35b458] | 396 |
|
---|
[6eef3c4] | 397 | if ((flags & THREAD_FLAG_NOATTACH) != THREAD_FLAG_NOATTACH)
|
---|
[da1bafb] | 398 | thread_attach(thread, task);
|
---|
[a35b458] | 399 |
|
---|
[da1bafb] | 400 | return thread;
|
---|
[d8431986] | 401 | }
|
---|
| 402 |
|
---|
| 403 | /** Destroy thread memory structure
|
---|
| 404 | *
|
---|
| 405 | * Detach thread from all queues, cpus etc. and destroy it.
|
---|
[da1bafb] | 406 | *
|
---|
[11d2c983] | 407 | * @param obj Thread to be destroyed.
|
---|
[d8431986] | 408 | *
|
---|
| 409 | */
|
---|
[1871118] | 410 | static void thread_destroy(void *obj)
|
---|
[d8431986] | 411 | {
|
---|
[1871118] | 412 | thread_t *thread = (thread_t *) obj;
|
---|
| 413 |
|
---|
[11d2c983] | 414 | assert_link_not_used(&thread->rq_link);
|
---|
| 415 | assert_link_not_used(&thread->wq_link);
|
---|
| 416 |
|
---|
[63e27ef] | 417 | assert(thread->task);
|
---|
[11d2c983] | 418 |
|
---|
| 419 | ipl_t ipl = interrupts_disable();
|
---|
| 420 |
|
---|
| 421 | /* Remove thread from task's list. */
|
---|
| 422 | irq_spinlock_lock(&thread->task->lock, false);
|
---|
| 423 | list_remove(&thread->th_link);
|
---|
| 424 | irq_spinlock_unlock(&thread->task->lock, false);
|
---|
| 425 |
|
---|
| 426 | /* Remove thread from global list. */
|
---|
| 427 | irq_spinlock_lock(&threads_lock, false);
|
---|
| 428 | odict_remove(&thread->lthreads);
|
---|
| 429 | irq_spinlock_unlock(&threads_lock, false);
|
---|
| 430 |
|
---|
| 431 | /* Clear cpu->fpu_owner if set to this thread. */
|
---|
| 432 | irq_spinlock_lock(&thread->lock, false);
|
---|
| 433 |
|
---|
| 434 | assert((thread->state == Exiting) || (thread->state == Lingering));
|
---|
[63e27ef] | 435 | assert(thread->cpu);
|
---|
[a35b458] | 436 |
|
---|
[da1bafb] | 437 | irq_spinlock_lock(&thread->cpu->lock, false);
|
---|
| 438 | if (thread->cpu->fpu_owner == thread)
|
---|
| 439 | thread->cpu->fpu_owner = NULL;
|
---|
| 440 | irq_spinlock_unlock(&thread->cpu->lock, false);
|
---|
[a35b458] | 441 |
|
---|
[11d2c983] | 442 | irq_spinlock_unlock(&thread->lock, false);
|
---|
[a35b458] | 443 |
|
---|
[11d2c983] | 444 | interrupts_restore(ipl);
|
---|
[a35b458] | 445 |
|
---|
[ea7890e7] | 446 | /*
|
---|
[7ed8530] | 447 | * Drop the reference to the containing task.
|
---|
[ea7890e7] | 448 | */
|
---|
[da1bafb] | 449 | task_release(thread->task);
|
---|
[11d2c983] | 450 | thread->task = NULL;
|
---|
| 451 |
|
---|
[82d515e9] | 452 | slab_free(thread_cache, thread);
|
---|
[d8431986] | 453 | }
|
---|
| 454 |
|
---|
[1871118] | 455 | void thread_put(thread_t *thread)
|
---|
| 456 | {
|
---|
| 457 | if (refcount_down(&thread->refcount)) {
|
---|
| 458 | thread_destroy(thread);
|
---|
| 459 | }
|
---|
| 460 | }
|
---|
| 461 |
|
---|
[d8431986] | 462 | /** Make the thread visible to the system.
|
---|
| 463 | *
|
---|
| 464 | * Attach the thread structure to the current task and make it visible in the
|
---|
[5dcee525] | 465 | * threads_tree.
|
---|
[d8431986] | 466 | *
|
---|
[da1bafb] | 467 | * @param t Thread to be attached to the task.
|
---|
| 468 | * @param task Task to which the thread is to be attached.
|
---|
| 469 | *
|
---|
[d8431986] | 470 | */
|
---|
[da1bafb] | 471 | void thread_attach(thread_t *thread, task_t *task)
|
---|
[d8431986] | 472 | {
|
---|
[1871118] | 473 | ipl_t ipl = interrupts_disable();
|
---|
| 474 |
|
---|
[d8431986] | 475 | /*
|
---|
[9a1b20c] | 476 | * Attach to the specified task.
|
---|
[d8431986] | 477 | */
|
---|
[1871118] | 478 | irq_spinlock_lock(&task->lock, false);
|
---|
[a35b458] | 479 |
|
---|
[7ed8530] | 480 | /* Hold a reference to the task. */
|
---|
| 481 | task_hold(task);
|
---|
[a35b458] | 482 |
|
---|
[9a1b20c] | 483 | /* Must not count kbox thread into lifecount */
|
---|
[6eef3c4] | 484 | if (thread->uspace)
|
---|
[9a1b20c] | 485 | atomic_inc(&task->lifecount);
|
---|
[a35b458] | 486 |
|
---|
[55b77d9] | 487 | list_append(&thread->th_link, &task->threads);
|
---|
[a35b458] | 488 |
|
---|
[1871118] | 489 | irq_spinlock_unlock(&task->lock, false);
|
---|
[a35b458] | 490 |
|
---|
[bb68433] | 491 | /*
|
---|
[ef1eab7] | 492 | * Register this thread in the system-wide dictionary.
|
---|
[bb68433] | 493 | */
|
---|
[1871118] | 494 | irq_spinlock_lock(&threads_lock, false);
|
---|
[ef1eab7] | 495 | odict_insert(&thread->lthreads, &threads, NULL);
|
---|
[1871118] | 496 | irq_spinlock_unlock(&threads_lock, false);
|
---|
| 497 |
|
---|
| 498 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 499 | }
|
---|
| 500 |
|
---|
[0182a665] | 501 | /** Terminate thread.
|
---|
[70527f1] | 502 | *
|
---|
[da1bafb] | 503 | * End current thread execution and switch it to the exiting state.
|
---|
| 504 | * All pending timeouts are executed.
|
---|
| 505 | *
|
---|
[70527f1] | 506 | */
|
---|
[f761f1eb] | 507 | void thread_exit(void)
|
---|
| 508 | {
|
---|
[6eef3c4] | 509 | if (THREAD->uspace) {
|
---|
[9a1b20c] | 510 | #ifdef CONFIG_UDEBUG
|
---|
| 511 | /* Generate udebug THREAD_E event */
|
---|
| 512 | udebug_thread_e_event();
|
---|
[a35b458] | 513 |
|
---|
[0ac99db] | 514 | /*
|
---|
| 515 | * This thread will not execute any code or system calls from
|
---|
| 516 | * now on.
|
---|
| 517 | */
|
---|
| 518 | udebug_stoppable_begin();
|
---|
[9a1b20c] | 519 | #endif
|
---|
| 520 | if (atomic_predec(&TASK->lifecount) == 0) {
|
---|
| 521 | /*
|
---|
| 522 | * We are the last userspace thread in the task that
|
---|
| 523 | * still has not exited. With the exception of the
|
---|
| 524 | * moment the task was created, new userspace threads
|
---|
| 525 | * can only be created by threads of the same task.
|
---|
| 526 | * We are safe to perform cleanup.
|
---|
[da1bafb] | 527 | *
|
---|
[9a1b20c] | 528 | */
|
---|
[ea7890e7] | 529 | ipc_cleanup();
|
---|
[d314571] | 530 | sys_waitq_task_cleanup();
|
---|
[3bacee1] | 531 | LOG("Cleanup of task %" PRIu64 " completed.", TASK->taskid);
|
---|
[ea7890e7] | 532 | }
|
---|
| 533 | }
|
---|
[a35b458] | 534 |
|
---|
[da1bafb] | 535 | irq_spinlock_lock(&THREAD->lock, true);
|
---|
[43114c5] | 536 | THREAD->state = Exiting;
|
---|
[da1bafb] | 537 | irq_spinlock_unlock(&THREAD->lock, true);
|
---|
[a35b458] | 538 |
|
---|
[f761f1eb] | 539 | scheduler();
|
---|
[a35b458] | 540 |
|
---|
[661a5ac] | 541 | panic("should never be reached");
|
---|
[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 | * Interrupted threads automatically exit when returning back to user space.
|
---|
[1b20da0] | 551 | *
|
---|
[1871118] | 552 | * @param thread A valid thread object.
|
---|
[518dd43] | 553 | */
|
---|
[111b9b9] | 554 | void thread_interrupt(thread_t *thread)
|
---|
[518dd43] | 555 | {
|
---|
[63e27ef] | 556 | assert(thread != NULL);
|
---|
[111b9b9] | 557 | thread->interrupted = true;
|
---|
| 558 | thread_wakeup(thread);
|
---|
| 559 | }
|
---|
[a35b458] | 560 |
|
---|
[111b9b9] | 561 | /** Prepare for putting the thread to sleep.
|
---|
| 562 | *
|
---|
| 563 | * @returns whether the thread is currently terminating. If THREAD_OK
|
---|
| 564 | * is returned, the thread is guaranteed to be woken up instantly if the thread
|
---|
| 565 | * is terminated at any time between this function's return and
|
---|
| 566 | * thread_wait_finish(). If THREAD_TERMINATING is returned, the thread can still
|
---|
| 567 | * go to sleep, but doing so will delay termination.
|
---|
| 568 | */
|
---|
| 569 | thread_termination_state_t thread_wait_start(void)
|
---|
| 570 | {
|
---|
| 571 | assert(THREAD != NULL);
|
---|
[a35b458] | 572 |
|
---|
[111b9b9] | 573 | /*
|
---|
| 574 | * This is an exchange rather than a store so that we can use the acquire
|
---|
| 575 | * semantics, which is needed to ensure that code after this operation sees
|
---|
| 576 | * memory ops made before thread_wakeup() in other thread, if that wakeup
|
---|
| 577 | * was reset by this operation.
|
---|
| 578 | *
|
---|
| 579 | * In particular, we need this to ensure we can't miss the thread being
|
---|
| 580 | * terminated concurrently with a synchronization primitive preparing to
|
---|
| 581 | * sleep.
|
---|
| 582 | */
|
---|
| 583 | (void) atomic_exchange_explicit(&THREAD->sleep_state, SLEEP_INITIAL,
|
---|
| 584 | memory_order_acquire);
|
---|
[a35b458] | 585 |
|
---|
[111b9b9] | 586 | return THREAD->interrupted ? THREAD_TERMINATING : THREAD_OK;
|
---|
| 587 | }
|
---|
[a35b458] | 588 |
|
---|
[111b9b9] | 589 | static void thread_wait_internal(void)
|
---|
| 590 | {
|
---|
| 591 | assert(THREAD != NULL);
|
---|
[1871118] | 592 |
|
---|
[111b9b9] | 593 | ipl_t ipl = interrupts_disable();
|
---|
| 594 |
|
---|
| 595 | if (atomic_load(&haltstate))
|
---|
| 596 | halt();
|
---|
| 597 |
|
---|
| 598 | /*
|
---|
| 599 | * Lock here to prevent a race between entering the scheduler and another
|
---|
| 600 | * thread rescheduling this thread.
|
---|
| 601 | */
|
---|
| 602 | irq_spinlock_lock(&THREAD->lock, false);
|
---|
| 603 |
|
---|
| 604 | int expected = SLEEP_INITIAL;
|
---|
| 605 |
|
---|
| 606 | /* Only set SLEEP_ASLEEP in sleep pad if it's still in initial state */
|
---|
| 607 | if (atomic_compare_exchange_strong_explicit(&THREAD->sleep_state, &expected,
|
---|
| 608 | SLEEP_ASLEEP, memory_order_acq_rel, memory_order_acquire)) {
|
---|
| 609 | THREAD->state = Sleeping;
|
---|
| 610 | scheduler_locked(ipl);
|
---|
| 611 | } else {
|
---|
| 612 | assert(expected == SLEEP_WOKE);
|
---|
| 613 | /* Return immediately. */
|
---|
| 614 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
| 615 | interrupts_restore(ipl);
|
---|
| 616 | }
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | static void thread_wait_timeout_callback(void *arg)
|
---|
| 620 | {
|
---|
| 621 | thread_wakeup(arg);
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | /**
|
---|
| 625 | * Suspends this thread's execution until thread_wakeup() is called on it,
|
---|
| 626 | * or deadline is reached.
|
---|
| 627 | *
|
---|
| 628 | * The way this would normally be used is that the current thread call
|
---|
| 629 | * thread_wait_start(), and if interruption has not been signaled, stores
|
---|
| 630 | * a reference to itself in a synchronized structure (such as waitq).
|
---|
| 631 | * After that, it releases any spinlocks it might hold and calls this function.
|
---|
| 632 | *
|
---|
| 633 | * The thread doing the wakeup will acquire the thread's reference from said
|
---|
| 634 | * synchronized structure and calls thread_wakeup() on it.
|
---|
| 635 | *
|
---|
| 636 | * Notably, there can be more than one thread performing wakeup.
|
---|
| 637 | * The number of performed calls to thread_wakeup(), or their relative
|
---|
| 638 | * ordering with thread_wait_finish(), does not matter. However, calls to
|
---|
| 639 | * thread_wakeup() are expected to be synchronized with thread_wait_start()
|
---|
| 640 | * with which they are associated, otherwise wakeups may be missed.
|
---|
| 641 | * However, the operation of thread_wakeup() is defined at any time,
|
---|
| 642 | * synchronization notwithstanding (in the sense of C un/defined behavior),
|
---|
| 643 | * and is in fact used to interrupt waiting threads by external events.
|
---|
| 644 | * The waiting thread must operate correctly in face of spurious wakeups,
|
---|
| 645 | * and clean up its reference in the synchronization structure if necessary.
|
---|
| 646 | *
|
---|
| 647 | * Returns THREAD_WAIT_TIMEOUT if timeout fired, which is a necessary condition
|
---|
| 648 | * for it to have been waken up by the timeout, but the caller must assume
|
---|
| 649 | * that proper wakeups, timeouts and interrupts may occur concurrently, so
|
---|
| 650 | * the fact timeout has been registered does not necessarily mean the thread
|
---|
| 651 | * has not been woken up or interrupted.
|
---|
| 652 | */
|
---|
| 653 | thread_wait_result_t thread_wait_finish(deadline_t deadline)
|
---|
| 654 | {
|
---|
| 655 | assert(THREAD != NULL);
|
---|
| 656 |
|
---|
| 657 | timeout_t timeout;
|
---|
| 658 |
|
---|
| 659 | if (deadline != DEADLINE_NEVER) {
|
---|
| 660 | /* Extra check to avoid setting up a deadline if we don't need to. */
|
---|
| 661 | if (atomic_load_explicit(&THREAD->sleep_state, memory_order_acquire) !=
|
---|
| 662 | SLEEP_INITIAL)
|
---|
| 663 | return THREAD_WAIT_SUCCESS;
|
---|
| 664 |
|
---|
| 665 | timeout_initialize(&timeout);
|
---|
| 666 | timeout_register_deadline(&timeout, deadline,
|
---|
| 667 | thread_wait_timeout_callback, THREAD);
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | thread_wait_internal();
|
---|
| 671 |
|
---|
| 672 | if (deadline != DEADLINE_NEVER && !timeout_unregister(&timeout)) {
|
---|
| 673 | return THREAD_WAIT_TIMEOUT;
|
---|
| 674 | } else {
|
---|
| 675 | return THREAD_WAIT_SUCCESS;
|
---|
| 676 | }
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | void thread_wakeup(thread_t *thread)
|
---|
| 680 | {
|
---|
| 681 | assert(thread != NULL);
|
---|
| 682 |
|
---|
| 683 | int state = atomic_exchange_explicit(&thread->sleep_state, SLEEP_WOKE,
|
---|
| 684 | memory_order_release);
|
---|
| 685 |
|
---|
| 686 | if (state == SLEEP_ASLEEP) {
|
---|
| 687 | /*
|
---|
| 688 | * Only one thread gets to do this.
|
---|
| 689 | * The reference consumed here is the reference implicitly passed to
|
---|
| 690 | * the waking thread by the sleeper in thread_wait_finish().
|
---|
| 691 | */
|
---|
| 692 | thread_ready(thread);
|
---|
| 693 | }
|
---|
[518dd43] | 694 | }
|
---|
| 695 |
|
---|
[43ac0cc] | 696 | /** Prevent the current thread from being migrated to another processor. */
|
---|
| 697 | void thread_migration_disable(void)
|
---|
| 698 | {
|
---|
[63e27ef] | 699 | assert(THREAD);
|
---|
[a35b458] | 700 |
|
---|
[43ac0cc] | 701 | THREAD->nomigrate++;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | /** Allow the current thread to be migrated to another processor. */
|
---|
| 705 | void thread_migration_enable(void)
|
---|
| 706 | {
|
---|
[63e27ef] | 707 | assert(THREAD);
|
---|
| 708 | assert(THREAD->nomigrate > 0);
|
---|
[a35b458] | 709 |
|
---|
[6eef3c4] | 710 | if (THREAD->nomigrate > 0)
|
---|
| 711 | THREAD->nomigrate--;
|
---|
[43ac0cc] | 712 | }
|
---|
| 713 |
|
---|
[70527f1] | 714 | /** Thread sleep
|
---|
| 715 | *
|
---|
| 716 | * Suspend execution of the current thread.
|
---|
| 717 | *
|
---|
| 718 | * @param sec Number of seconds to sleep.
|
---|
| 719 | *
|
---|
| 720 | */
|
---|
[7f1c620] | 721 | void thread_sleep(uint32_t sec)
|
---|
[f761f1eb] | 722 | {
|
---|
[7c3fb9b] | 723 | /*
|
---|
| 724 | * Sleep in 1000 second steps to support
|
---|
| 725 | * full argument range
|
---|
| 726 | */
|
---|
[22e6802] | 727 | while (sec > 0) {
|
---|
| 728 | uint32_t period = (sec > 1000) ? 1000 : sec;
|
---|
[a35b458] | 729 |
|
---|
[22e6802] | 730 | thread_usleep(period * 1000000);
|
---|
| 731 | sec -= period;
|
---|
| 732 | }
|
---|
[f761f1eb] | 733 | }
|
---|
[70527f1] | 734 |
|
---|
[5110d0a] | 735 | errno_t thread_join(thread_t *thread)
|
---|
| 736 | {
|
---|
| 737 | return thread_join_timeout(thread, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
|
---|
| 738 | }
|
---|
| 739 |
|
---|
[fe19611] | 740 | /** Wait for another thread to exit.
|
---|
[1871118] | 741 | * This function does not destroy the thread. Reference counting handles that.
|
---|
[fe19611] | 742 | *
|
---|
[da1bafb] | 743 | * @param thread Thread to join on exit.
|
---|
| 744 | * @param usec Timeout in microseconds.
|
---|
| 745 | * @param flags Mode of operation.
|
---|
[fe19611] | 746 | *
|
---|
| 747 | * @return An error code from errno.h or an error code from synch.h.
|
---|
[da1bafb] | 748 | *
|
---|
[fe19611] | 749 | */
|
---|
[b7fd2a0] | 750 | errno_t thread_join_timeout(thread_t *thread, uint32_t usec, unsigned int flags)
|
---|
[fe19611] | 751 | {
|
---|
[da1bafb] | 752 | if (thread == THREAD)
|
---|
[fe19611] | 753 | return EINVAL;
|
---|
[a35b458] | 754 |
|
---|
[da1bafb] | 755 | irq_spinlock_lock(&thread->lock, true);
|
---|
[1871118] | 756 | state_t state = thread->state;
|
---|
[da1bafb] | 757 | irq_spinlock_unlock(&thread->lock, true);
|
---|
[a35b458] | 758 |
|
---|
[1871118] | 759 | if (state == Exiting) {
|
---|
| 760 | return EOK;
|
---|
[fe19611] | 761 | } else {
|
---|
[111b9b9] | 762 | return _waitq_sleep_timeout(&thread->join_wq, usec, flags);
|
---|
[fe19611] | 763 | }
|
---|
| 764 | }
|
---|
| 765 |
|
---|
[70527f1] | 766 | /** Thread usleep
|
---|
| 767 | *
|
---|
| 768 | * Suspend execution of the current thread.
|
---|
| 769 | *
|
---|
| 770 | * @param usec Number of microseconds to sleep.
|
---|
| 771 | *
|
---|
[1b20da0] | 772 | */
|
---|
[7f1c620] | 773 | void thread_usleep(uint32_t usec)
|
---|
[f761f1eb] | 774 | {
|
---|
| 775 | waitq_t wq;
|
---|
[a35b458] | 776 |
|
---|
[f761f1eb] | 777 | waitq_initialize(&wq);
|
---|
[a35b458] | 778 |
|
---|
[111b9b9] | 779 | (void) waitq_sleep_timeout(&wq, usec);
|
---|
[f761f1eb] | 780 | }
|
---|
| 781 |
|
---|
[ef1eab7] | 782 | static void thread_print(thread_t *thread, bool additional)
|
---|
[5dcee525] | 783 | {
|
---|
[1ba37fa] | 784 | uint64_t ucycles, kcycles;
|
---|
| 785 | char usuffix, ksuffix;
|
---|
[da1bafb] | 786 | order_suffix(thread->ucycles, &ucycles, &usuffix);
|
---|
| 787 | order_suffix(thread->kcycles, &kcycles, &ksuffix);
|
---|
[a35b458] | 788 |
|
---|
[577f042a] | 789 | char *name;
|
---|
| 790 | if (str_cmp(thread->name, "uinit") == 0)
|
---|
| 791 | name = thread->task->name;
|
---|
| 792 | else
|
---|
| 793 | name = thread->name;
|
---|
[a35b458] | 794 |
|
---|
[ef1eab7] | 795 | if (additional)
|
---|
[c1b073b7] | 796 | printf("%-8" PRIu64 " %p %p %9" PRIu64 "%c %9" PRIu64 "%c ",
|
---|
[577f042a] | 797 | thread->tid, thread->thread_code, thread->kstack,
|
---|
| 798 | ucycles, usuffix, kcycles, ksuffix);
|
---|
[48dcc69] | 799 | else
|
---|
[c1b073b7] | 800 | printf("%-8" PRIu64 " %-14s %p %-8s %p %-5" PRIu32 "\n",
|
---|
[577f042a] | 801 | thread->tid, name, thread, thread_states[thread->state],
|
---|
[26aafe8] | 802 | thread->task, thread->task->container);
|
---|
[a35b458] | 803 |
|
---|
[ef1eab7] | 804 | if (additional) {
|
---|
[48dcc69] | 805 | if (thread->cpu)
|
---|
| 806 | printf("%-5u", thread->cpu->id);
|
---|
| 807 | else
|
---|
| 808 | printf("none ");
|
---|
[a35b458] | 809 |
|
---|
[48dcc69] | 810 | if (thread->state == Sleeping) {
|
---|
[c1b073b7] | 811 | printf(" %p", thread->sleep_queue);
|
---|
[48dcc69] | 812 | }
|
---|
[a35b458] | 813 |
|
---|
[48dcc69] | 814 | printf("\n");
|
---|
[43b1e86] | 815 | }
|
---|
[5dcee525] | 816 | }
|
---|
| 817 |
|
---|
[da1bafb] | 818 | /** Print list of threads debug info
|
---|
[48dcc69] | 819 | *
|
---|
| 820 | * @param additional Print additional information.
|
---|
[da1bafb] | 821 | *
|
---|
| 822 | */
|
---|
[48dcc69] | 823 | void thread_print_list(bool additional)
|
---|
[55ab0f1] | 824 | {
|
---|
[ef1eab7] | 825 | thread_t *thread;
|
---|
| 826 |
|
---|
[1871118] | 827 | /* Accessing system-wide threads list through thread_first()/thread_next(). */
|
---|
[da1bafb] | 828 | irq_spinlock_lock(&threads_lock, true);
|
---|
[a35b458] | 829 |
|
---|
[c1b073b7] | 830 | if (sizeof(void *) <= 4) {
|
---|
| 831 | if (additional)
|
---|
| 832 | printf("[id ] [code ] [stack ] [ucycles ] [kcycles ]"
|
---|
| 833 | " [cpu] [waitqueue]\n");
|
---|
| 834 | else
|
---|
| 835 | printf("[id ] [name ] [address ] [state ] [task ]"
|
---|
| 836 | " [ctn]\n");
|
---|
| 837 | } else {
|
---|
| 838 | if (additional) {
|
---|
| 839 | printf("[id ] [code ] [stack ] [ucycles ] [kcycles ]"
|
---|
| 840 | " [cpu] [waitqueue ]\n");
|
---|
| 841 | } else
|
---|
| 842 | printf("[id ] [name ] [address ] [state ]"
|
---|
| 843 | " [task ] [ctn]\n");
|
---|
| 844 | }
|
---|
[a35b458] | 845 |
|
---|
[aab5e46] | 846 | thread = thread_first();
|
---|
| 847 | while (thread != NULL) {
|
---|
[ef1eab7] | 848 | thread_print(thread, additional);
|
---|
[aab5e46] | 849 | thread = thread_next(thread);
|
---|
[ef1eab7] | 850 | }
|
---|
[a35b458] | 851 |
|
---|
[da1bafb] | 852 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[55ab0f1] | 853 | }
|
---|
[9f52563] | 854 |
|
---|
[1871118] | 855 | static bool thread_exists(thread_t *thread)
|
---|
[016acbe] | 856 | {
|
---|
[ef1eab7] | 857 | odlink_t *odlink = odict_find_eq(&threads, thread, NULL);
|
---|
| 858 | return odlink != NULL;
|
---|
[016acbe] | 859 | }
|
---|
| 860 |
|
---|
[1871118] | 861 | /** Check whether the thread exists, and if so, return a reference to it.
|
---|
| 862 | */
|
---|
| 863 | thread_t *thread_try_get(thread_t *thread)
|
---|
| 864 | {
|
---|
| 865 | irq_spinlock_lock(&threads_lock, true);
|
---|
| 866 |
|
---|
| 867 | if (thread_exists(thread)) {
|
---|
| 868 | /* Try to strengthen the reference. */
|
---|
| 869 | thread = thread_try_ref(thread);
|
---|
| 870 | } else {
|
---|
| 871 | thread = NULL;
|
---|
| 872 | }
|
---|
| 873 |
|
---|
| 874 | irq_spinlock_unlock(&threads_lock, true);
|
---|
| 875 |
|
---|
| 876 | return thread;
|
---|
| 877 | }
|
---|
| 878 |
|
---|
[cce6acf] | 879 | /** Update accounting of current thread.
|
---|
| 880 | *
|
---|
| 881 | * Note that thread_lock on THREAD must be already held and
|
---|
| 882 | * interrupts must be already disabled.
|
---|
| 883 | *
|
---|
[da1bafb] | 884 | * @param user True to update user accounting, false for kernel.
|
---|
| 885 | *
|
---|
[cce6acf] | 886 | */
|
---|
[a2a00e8] | 887 | void thread_update_accounting(bool user)
|
---|
[cce6acf] | 888 | {
|
---|
| 889 | uint64_t time = get_cycle();
|
---|
[1d432f9] | 890 |
|
---|
[63e27ef] | 891 | assert(interrupts_disabled());
|
---|
| 892 | assert(irq_spinlock_locked(&THREAD->lock));
|
---|
[a35b458] | 893 |
|
---|
[da1bafb] | 894 | if (user)
|
---|
[a2a00e8] | 895 | THREAD->ucycles += time - THREAD->last_cycle;
|
---|
[da1bafb] | 896 | else
|
---|
[a2a00e8] | 897 | THREAD->kcycles += time - THREAD->last_cycle;
|
---|
[a35b458] | 898 |
|
---|
[cce6acf] | 899 | THREAD->last_cycle = time;
|
---|
| 900 | }
|
---|
| 901 |
|
---|
[e1b6742] | 902 | /** Find thread structure corresponding to thread ID.
|
---|
| 903 | *
|
---|
| 904 | * The threads_lock must be already held by the caller of this function and
|
---|
| 905 | * interrupts must be disabled.
|
---|
| 906 | *
|
---|
[1871118] | 907 | * The returned reference is weak.
|
---|
| 908 | * If the caller needs to keep it, thread_try_ref() must be used to upgrade
|
---|
| 909 | * to a strong reference _before_ threads_lock is released.
|
---|
| 910 | *
|
---|
[e1b6742] | 911 | * @param id Thread ID.
|
---|
| 912 | *
|
---|
| 913 | * @return Thread structure address or NULL if there is no such thread ID.
|
---|
| 914 | *
|
---|
| 915 | */
|
---|
| 916 | thread_t *thread_find_by_id(thread_id_t thread_id)
|
---|
| 917 | {
|
---|
[ef1eab7] | 918 | thread_t *thread;
|
---|
| 919 |
|
---|
[63e27ef] | 920 | assert(interrupts_disabled());
|
---|
| 921 | assert(irq_spinlock_locked(&threads_lock));
|
---|
[a35b458] | 922 |
|
---|
[aab5e46] | 923 | thread = thread_first();
|
---|
| 924 | while (thread != NULL) {
|
---|
[ef1eab7] | 925 | if (thread->tid == thread_id)
|
---|
| 926 | return thread;
|
---|
[a35b458] | 927 |
|
---|
[aab5e46] | 928 | thread = thread_next(thread);
|
---|
[ef1eab7] | 929 | }
|
---|
[a35b458] | 930 |
|
---|
[ef1eab7] | 931 | return NULL;
|
---|
[e1b6742] | 932 | }
|
---|
| 933 |
|
---|
[aab5e46] | 934 | /** Get count of threads.
|
---|
| 935 | *
|
---|
| 936 | * @return Number of threads in the system
|
---|
| 937 | */
|
---|
| 938 | size_t thread_count(void)
|
---|
| 939 | {
|
---|
| 940 | assert(interrupts_disabled());
|
---|
| 941 | assert(irq_spinlock_locked(&threads_lock));
|
---|
| 942 |
|
---|
| 943 | return odict_count(&threads);
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 | /** Get first thread.
|
---|
| 947 | *
|
---|
| 948 | * @return Pointer to first thread or @c NULL if there are none.
|
---|
| 949 | */
|
---|
| 950 | thread_t *thread_first(void)
|
---|
| 951 | {
|
---|
| 952 | odlink_t *odlink;
|
---|
| 953 |
|
---|
| 954 | assert(interrupts_disabled());
|
---|
| 955 | assert(irq_spinlock_locked(&threads_lock));
|
---|
| 956 |
|
---|
| 957 | odlink = odict_first(&threads);
|
---|
| 958 | if (odlink == NULL)
|
---|
| 959 | return NULL;
|
---|
| 960 |
|
---|
| 961 | return odict_get_instance(odlink, thread_t, lthreads);
|
---|
| 962 | }
|
---|
| 963 |
|
---|
| 964 | /** Get next thread.
|
---|
| 965 | *
|
---|
| 966 | * @param cur Current thread
|
---|
| 967 | * @return Pointer to next thread or @c NULL if there are no more threads.
|
---|
| 968 | */
|
---|
| 969 | thread_t *thread_next(thread_t *cur)
|
---|
| 970 | {
|
---|
| 971 | odlink_t *odlink;
|
---|
| 972 |
|
---|
| 973 | assert(interrupts_disabled());
|
---|
| 974 | assert(irq_spinlock_locked(&threads_lock));
|
---|
| 975 |
|
---|
| 976 | odlink = odict_next(&cur->lthreads, &threads);
|
---|
| 977 | if (odlink == NULL)
|
---|
| 978 | return NULL;
|
---|
| 979 |
|
---|
| 980 | return odict_get_instance(odlink, thread_t, lthreads);
|
---|
| 981 | }
|
---|
| 982 |
|
---|
[5b7a107] | 983 | #ifdef CONFIG_UDEBUG
|
---|
| 984 |
|
---|
[df58e44] | 985 | void thread_stack_trace(thread_id_t thread_id)
|
---|
| 986 | {
|
---|
| 987 | irq_spinlock_lock(&threads_lock, true);
|
---|
[1871118] | 988 | thread_t *thread = thread_try_ref(thread_find_by_id(thread_id));
|
---|
| 989 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[a35b458] | 990 |
|
---|
[df58e44] | 991 | if (thread == NULL) {
|
---|
| 992 | printf("No such thread.\n");
|
---|
| 993 | return;
|
---|
| 994 | }
|
---|
[a35b458] | 995 |
|
---|
[df58e44] | 996 | /*
|
---|
| 997 | * Schedule a stack trace to be printed
|
---|
| 998 | * just before the thread is scheduled next.
|
---|
| 999 | *
|
---|
| 1000 | * If the thread is sleeping then try to interrupt
|
---|
| 1001 | * the sleep. Any request for printing an uspace stack
|
---|
| 1002 | * trace from within the kernel should be always
|
---|
| 1003 | * considered a last resort debugging means, therefore
|
---|
| 1004 | * forcing the thread's sleep to be interrupted
|
---|
| 1005 | * is probably justifiable.
|
---|
| 1006 | */
|
---|
[a35b458] | 1007 |
|
---|
[1871118] | 1008 | irq_spinlock_lock(&thread->lock, true);
|
---|
| 1009 |
|
---|
[df58e44] | 1010 | bool sleeping = false;
|
---|
| 1011 | istate_t *istate = thread->udebug.uspace_state;
|
---|
| 1012 | if (istate != NULL) {
|
---|
| 1013 | printf("Scheduling thread stack trace.\n");
|
---|
| 1014 | thread->btrace = true;
|
---|
| 1015 | if (thread->state == Sleeping)
|
---|
| 1016 | sleeping = true;
|
---|
| 1017 | } else
|
---|
| 1018 | printf("Thread interrupt state not available.\n");
|
---|
[a35b458] | 1019 |
|
---|
[1871118] | 1020 | irq_spinlock_unlock(&thread->lock, true);
|
---|
[a35b458] | 1021 |
|
---|
[df58e44] | 1022 | if (sleeping)
|
---|
[111b9b9] | 1023 | thread_wakeup(thread);
|
---|
[a35b458] | 1024 |
|
---|
[1871118] | 1025 | thread_put(thread);
|
---|
[df58e44] | 1026 | }
|
---|
[e1b6742] | 1027 |
|
---|
[5b7a107] | 1028 | #endif /* CONFIG_UDEBUG */
|
---|
[e1b6742] | 1029 |
|
---|
[ef1eab7] | 1030 | /** Get key function for the @c threads ordered dictionary.
|
---|
| 1031 | *
|
---|
| 1032 | * @param odlink Link
|
---|
| 1033 | * @return Pointer to thread structure cast as 'void *'
|
---|
| 1034 | */
|
---|
| 1035 | static void *threads_getkey(odlink_t *odlink)
|
---|
| 1036 | {
|
---|
| 1037 | thread_t *thread = odict_get_instance(odlink, thread_t, lthreads);
|
---|
| 1038 | return (void *) thread;
|
---|
| 1039 | }
|
---|
| 1040 |
|
---|
| 1041 | /** Key comparison function for the @c threads ordered dictionary.
|
---|
| 1042 | *
|
---|
| 1043 | * @param a Pointer to thread A
|
---|
| 1044 | * @param b Pointer to thread B
|
---|
| 1045 | * @return -1, 0, 1 iff pointer to A is less than, equal to, greater than B
|
---|
| 1046 | */
|
---|
| 1047 | static int threads_cmp(void *a, void *b)
|
---|
| 1048 | {
|
---|
| 1049 | if (a > b)
|
---|
| 1050 | return -1;
|
---|
| 1051 | else if (a == b)
|
---|
| 1052 | return 0;
|
---|
| 1053 | else
|
---|
| 1054 | return +1;
|
---|
| 1055 | }
|
---|
| 1056 |
|
---|
[9f52563] | 1057 | /** Process syscall to create new thread.
|
---|
| 1058 | *
|
---|
| 1059 | */
|
---|
[5a5269d] | 1060 | sys_errno_t sys_thread_create(uspace_ptr_uspace_arg_t uspace_uarg, uspace_ptr_char uspace_name,
|
---|
| 1061 | size_t name_len, uspace_ptr_thread_id_t uspace_thread_id)
|
---|
[9f52563] | 1062 | {
|
---|
[24345a5] | 1063 | if (name_len > THREAD_NAME_BUFLEN - 1)
|
---|
[7faabb7] | 1064 | name_len = THREAD_NAME_BUFLEN - 1;
|
---|
[a35b458] | 1065 |
|
---|
[da1bafb] | 1066 | char namebuf[THREAD_NAME_BUFLEN];
|
---|
[b7fd2a0] | 1067 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
[a53ed3a] | 1068 | if (rc != EOK)
|
---|
[b7fd2a0] | 1069 | return (sys_errno_t) rc;
|
---|
[a35b458] | 1070 |
|
---|
[b60c582] | 1071 | namebuf[name_len] = 0;
|
---|
[a35b458] | 1072 |
|
---|
[4680ef5] | 1073 | /*
|
---|
| 1074 | * In case of failure, kernel_uarg will be deallocated in this function.
|
---|
| 1075 | * In case of success, kernel_uarg will be freed in uinit().
|
---|
| 1076 | */
|
---|
[da1bafb] | 1077 | uspace_arg_t *kernel_uarg =
|
---|
[11b285d] | 1078 | (uspace_arg_t *) malloc(sizeof(uspace_arg_t));
|
---|
[7473807] | 1079 | if (!kernel_uarg)
|
---|
| 1080 | return (sys_errno_t) ENOMEM;
|
---|
[a35b458] | 1081 |
|
---|
[e3c762cd] | 1082 | rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
|
---|
[a53ed3a] | 1083 | if (rc != EOK) {
|
---|
[e3c762cd] | 1084 | free(kernel_uarg);
|
---|
[b7fd2a0] | 1085 | return (sys_errno_t) rc;
|
---|
[e3c762cd] | 1086 | }
|
---|
[a35b458] | 1087 |
|
---|
[da1bafb] | 1088 | thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
|
---|
[6eef3c4] | 1089 | THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf);
|
---|
[da1bafb] | 1090 | if (thread) {
|
---|
[5a5269d] | 1091 | if (uspace_thread_id) {
|
---|
[da1bafb] | 1092 | rc = copy_to_uspace(uspace_thread_id, &thread->tid,
|
---|
| 1093 | sizeof(thread->tid));
|
---|
[a53ed3a] | 1094 | if (rc != EOK) {
|
---|
[d8431986] | 1095 | /*
|
---|
| 1096 | * We have encountered a failure, but the thread
|
---|
| 1097 | * has already been created. We need to undo its
|
---|
| 1098 | * creation now.
|
---|
| 1099 | */
|
---|
[a35b458] | 1100 |
|
---|
[d8431986] | 1101 | /*
|
---|
[ea7890e7] | 1102 | * The new thread structure is initialized, but
|
---|
| 1103 | * is still not visible to the system.
|
---|
[d8431986] | 1104 | * We can safely deallocate it.
|
---|
| 1105 | */
|
---|
[82d515e9] | 1106 | slab_free(thread_cache, thread);
|
---|
[da1bafb] | 1107 | free(kernel_uarg);
|
---|
[a35b458] | 1108 |
|
---|
[b7fd2a0] | 1109 | return (sys_errno_t) rc;
|
---|
[3bacee1] | 1110 | }
|
---|
[d8431986] | 1111 | }
|
---|
[a35b458] | 1112 |
|
---|
[9a1b20c] | 1113 | #ifdef CONFIG_UDEBUG
|
---|
[13964ef] | 1114 | /*
|
---|
| 1115 | * Generate udebug THREAD_B event and attach the thread.
|
---|
| 1116 | * This must be done atomically (with the debug locks held),
|
---|
| 1117 | * otherwise we would either miss some thread or receive
|
---|
| 1118 | * THREAD_B events for threads that already existed
|
---|
| 1119 | * and could be detected with THREAD_READ before.
|
---|
| 1120 | */
|
---|
[da1bafb] | 1121 | udebug_thread_b_event_attach(thread, TASK);
|
---|
[13964ef] | 1122 | #else
|
---|
[da1bafb] | 1123 | thread_attach(thread, TASK);
|
---|
[9a1b20c] | 1124 | #endif
|
---|
[da1bafb] | 1125 | thread_ready(thread);
|
---|
[a35b458] | 1126 |
|
---|
[d8431986] | 1127 | return 0;
|
---|
[201abde] | 1128 | } else
|
---|
[0f250f9] | 1129 | free(kernel_uarg);
|
---|
[a35b458] | 1130 |
|
---|
[b7fd2a0] | 1131 | return (sys_errno_t) ENOMEM;
|
---|
[9f52563] | 1132 | }
|
---|
| 1133 |
|
---|
| 1134 | /** Process syscall to terminate thread.
|
---|
| 1135 | *
|
---|
| 1136 | */
|
---|
[b7fd2a0] | 1137 | sys_errno_t sys_thread_exit(int uspace_status)
|
---|
[9f52563] | 1138 | {
|
---|
[68091bd] | 1139 | thread_exit();
|
---|
[9f52563] | 1140 | }
|
---|
[b45c443] | 1141 |
|
---|
[3ce7f082] | 1142 | /** Syscall for getting TID.
|
---|
| 1143 | *
|
---|
[201abde] | 1144 | * @param uspace_thread_id Userspace address of 8-byte buffer where to store
|
---|
| 1145 | * current thread ID.
|
---|
| 1146 | *
|
---|
| 1147 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[da1bafb] | 1148 | *
|
---|
[b45c443] | 1149 | */
|
---|
[5a5269d] | 1150 | sys_errno_t sys_thread_get_id(uspace_ptr_thread_id_t uspace_thread_id)
|
---|
[3ce7f082] | 1151 | {
|
---|
| 1152 | /*
|
---|
| 1153 | * No need to acquire lock on THREAD because tid
|
---|
| 1154 | * remains constant for the lifespan of the thread.
|
---|
[da1bafb] | 1155 | *
|
---|
[3ce7f082] | 1156 | */
|
---|
[b7fd2a0] | 1157 | return (sys_errno_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
|
---|
[201abde] | 1158 | sizeof(THREAD->tid));
|
---|
[3ce7f082] | 1159 | }
|
---|
[6f4495f5] | 1160 |
|
---|
[d9ece1cb] | 1161 | /** Syscall wrapper for sleeping. */
|
---|
[b7fd2a0] | 1162 | sys_errno_t sys_thread_usleep(uint32_t usec)
|
---|
[d9ece1cb] | 1163 | {
|
---|
[22e6802] | 1164 | thread_usleep(usec);
|
---|
[d9ece1cb] | 1165 | return 0;
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
[b7fd2a0] | 1168 | sys_errno_t sys_thread_udelay(uint32_t usec)
|
---|
[7e7b791] | 1169 | {
|
---|
[8d6c1f1] | 1170 | delay(usec);
|
---|
[7e7b791] | 1171 | return 0;
|
---|
| 1172 | }
|
---|
| 1173 |
|
---|
[3ce7f082] | 1174 | /** @}
|
---|
| 1175 | */
|
---|