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