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