source: mainline/generic/src/proc/thread.c@ a597e3f0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a597e3f0 was 3fa424a9, checked in by Jakub Jermar <jakub@…>, 20 years ago

Initialize ARCH_THREAD_DATA on all architectures.
On ia32 and amd64 this fixes bug caused by uninitialized TLS pointer.

  • Property mode set to 100644
File size: 11.2 KB
RevLine 
[f761f1eb]1/*
2 * Copyright (C) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <proc/scheduler.h>
30#include <proc/thread.h>
31#include <proc/task.h>
[0f250f9]32#include <proc/uarg.h>
[f761f1eb]33#include <mm/frame.h>
34#include <mm/page.h>
35#include <arch/asm.h>
36#include <arch.h>
37#include <synch/synch.h>
38#include <synch/spinlock.h>
39#include <synch/waitq.h>
40#include <synch/rwlock.h>
41#include <cpu.h>
42#include <func.h>
43#include <context.h>
[016acbe]44#include <adt/btree.h>
[5c9a08b]45#include <adt/list.h>
[f761f1eb]46#include <typedefs.h>
47#include <time/clock.h>
[5c9a08b]48#include <adt/list.h>
[4ffa9e0]49#include <config.h>
50#include <arch/interrupt.h>
[26a8604f]51#include <smp/ipi.h>
[f2ffad4]52#include <arch/faddr.h>
[23684b7]53#include <atomic.h>
[9c0a9b3]54#include <memstr.h>
[55ab0f1]55#include <print.h>
[266294a9]56#include <mm/slab.h>
57#include <debug.h>
[9f52563]58#include <main/uinit.h>
[f761f1eb]59
[70527f1]60char *thread_states[] = {"Invalid", "Running", "Sleeping", "Ready", "Entering", "Exiting"}; /**< Thread states */
[f761f1eb]61
[016acbe]62/** Lock protecting threads_head list. For locking rules, see declaration thereof. */
63SPINLOCK_INITIALIZE(threads_lock);
64btree_t threads_btree; /**< B+tree of all threads. */
[f761f1eb]65
[dc747e3]66SPINLOCK_INITIALIZE(tidlock);
[f761f1eb]67__u32 last_tid = 0;
68
[266294a9]69static slab_cache_t *thread_slab;
[f76fed4]70#ifdef ARCH_HAS_FPU
71slab_cache_t *fpu_context_slab;
72#endif
[266294a9]73
[70527f1]74/** Thread wrapper
75 *
76 * This wrapper is provided to ensure that every thread
[f761f1eb]77 * makes a call to thread_exit() when its implementing
78 * function returns.
79 *
[22f7769]80 * interrupts_disable() is assumed.
[70527f1]81 *
[f761f1eb]82 */
[e16e036a]83static void cushion(void)
[f761f1eb]84{
[43114c5]85 void (*f)(void *) = THREAD->thread_code;
86 void *arg = THREAD->thread_arg;
[f761f1eb]87
[3e1607f]88 /* this is where each thread wakes up after its creation */
[43114c5]89 spinlock_unlock(&THREAD->lock);
[22f7769]90 interrupts_enable();
[f761f1eb]91
92 f(arg);
93 thread_exit();
94 /* not reached */
95}
96
[266294a9]97/** Initialization and allocation for thread_t structure */
98static int thr_constructor(void *obj, int kmflags)
99{
100 thread_t *t = (thread_t *)obj;
[085d973]101 pfn_t pfn;
[2a46e10]102 int status;
[266294a9]103
104 spinlock_initialize(&t->lock, "thread_t_lock");
105 link_initialize(&t->rq_link);
106 link_initialize(&t->wq_link);
107 link_initialize(&t->th_link);
108
[f76fed4]109#ifdef ARCH_HAS_FPU
110# ifdef CONFIG_FPU_LAZY
111 t->saved_fpu_context = NULL;
112# else
113 t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags);
114 if (!t->saved_fpu_context)
115 return -1;
116# endif
117#endif
118
[a82500ce]119 pfn = frame_alloc_rc(STACK_FRAMES, FRAME_KA | kmflags,&status);
[f76fed4]120 if (status) {
121#ifdef ARCH_HAS_FPU
122 if (t->saved_fpu_context)
123 slab_free(fpu_context_slab,t->saved_fpu_context);
124#endif
[266294a9]125 return -1;
[f76fed4]126 }
[2a46e10]127 t->kstack = (__u8 *)PA2KA(PFN2ADDR(pfn));
[266294a9]128
129 return 0;
130}
131
132/** Destruction of thread_t object */
133static int thr_destructor(void *obj)
134{
135 thread_t *t = (thread_t *)obj;
136
[085d973]137 frame_free(ADDR2PFN(KA2PA(t->kstack)));
[f76fed4]138#ifdef ARCH_HAS_FPU
139 if (t->saved_fpu_context)
140 slab_free(fpu_context_slab,t->saved_fpu_context);
141#endif
[266294a9]142 return 1; /* One page freed */
143}
[70527f1]144
145/** Initialize threads
146 *
147 * Initialize kernel threads support.
148 *
149 */
[f761f1eb]150void thread_init(void)
151{
[43114c5]152 THREAD = NULL;
[80d2bdb]153 atomic_set(&nrdy,0);
[266294a9]154 thread_slab = slab_cache_create("thread_slab",
155 sizeof(thread_t),0,
156 thr_constructor, thr_destructor, 0);
[f76fed4]157#ifdef ARCH_HAS_FPU
158 fpu_context_slab = slab_cache_create("fpu_slab",
159 sizeof(fpu_context_t),
160 FPU_CONTEXT_ALIGN,
161 NULL, NULL, 0);
162#endif
[f761f1eb]163
[016acbe]164 btree_create(&threads_btree);
165}
[70527f1]166
167/** Make thread ready
168 *
169 * Switch thread t to the ready state.
170 *
171 * @param t Thread to make ready.
172 *
173 */
[f761f1eb]174void thread_ready(thread_t *t)
175{
176 cpu_t *cpu;
177 runq_t *r;
[22f7769]178 ipl_t ipl;
[80d2bdb]179 int i, avg;
[f761f1eb]180
[22f7769]181 ipl = interrupts_disable();
[f761f1eb]182
183 spinlock_lock(&t->lock);
184
[fbcfd458]185 ASSERT(! (t->state == Ready));
186
[22f7769]187 i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority;
[f761f1eb]188
[8262010]189 cpu = CPU;
[f761f1eb]190 if (t->flags & X_WIRED) {
191 cpu = t->cpu;
192 }
[81c4c6da]193 t->state = Ready;
[f761f1eb]194 spinlock_unlock(&t->lock);
195
[70527f1]196 /*
[f761f1eb]197 * Append t to respective ready queue on respective processor.
198 */
199 r = &cpu->rq[i];
200 spinlock_lock(&r->lock);
201 list_append(&t->rq_link, &r->rq_head);
202 r->n++;
203 spinlock_unlock(&r->lock);
204
[59e07c91]205 atomic_inc(&nrdy);
[80d2bdb]206 avg = atomic_get(&nrdy) / config.cpu_active;
[248fc1a]207 atomic_inc(&cpu->nrdy);
[76cec1e]208
[22f7769]209 interrupts_restore(ipl);
[f761f1eb]210}
211
[266294a9]212/** Destroy thread memory structure
213 *
214 * Detach thread from all queues, cpus etc. and destroy it.
215 *
216 * Assume thread->lock is held!!
217 */
218void thread_destroy(thread_t *t)
219{
220 ASSERT(t->state == Exiting);
221 ASSERT(t->task);
222 ASSERT(t->cpu);
223
224 spinlock_lock(&t->cpu->lock);
225 if(t->cpu->fpu_owner==t)
226 t->cpu->fpu_owner=NULL;
227 spinlock_unlock(&t->cpu->lock);
228
229 /*
230 * Detach from the containing task.
231 */
232 spinlock_lock(&t->task->lock);
233 list_remove(&t->th_link);
234 spinlock_unlock(&t->task->lock);
235
236 spinlock_unlock(&t->lock);
237
238 spinlock_lock(&threads_lock);
[016acbe]239 btree_remove(&threads_btree, (__native) t, NULL);
[266294a9]240 spinlock_unlock(&threads_lock);
241
242 slab_free(thread_slab, t);
243}
244
[70527f1]245/** Create new thread
246 *
247 * Create a new thread.
248 *
249 * @param func Thread's implementing function.
250 * @param arg Thread's implementing function argument.
251 * @param task Task to which the thread belongs.
252 * @param flags Thread flags.
[ff14c520]253 * @param name Symbolic name.
[70527f1]254 *
255 * @return New thread's structure on success, NULL on failure.
256 *
257 */
[ff14c520]258thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name)
[f761f1eb]259{
260 thread_t *t;
[bb68433]261 ipl_t ipl;
262
[266294a9]263 t = (thread_t *) slab_alloc(thread_slab, 0);
[2a46e10]264 if (!t)
265 return NULL;
[3fa424a9]266
267 thread_create_arch(t);
[f761f1eb]268
[bb68433]269 /* Not needed, but good for debugging */
[b6d4566]270 memsetb((__address)t->kstack, THREAD_STACK_SIZE * 1<<STACK_FRAMES, 0);
[bb68433]271
272 ipl = interrupts_disable();
273 spinlock_lock(&tidlock);
274 t->tid = ++last_tid;
275 spinlock_unlock(&tidlock);
276 interrupts_restore(ipl);
277
278 context_save(&t->saved_context);
279 context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE);
280
281 the_initialize((the_t *) t->kstack);
282
283 ipl = interrupts_disable();
284 t->saved_context.ipl = interrupts_read();
285 interrupts_restore(ipl);
286
[9f52563]287 memcpy(t->name, name, THREAD_NAME_BUFLEN);
288
[bb68433]289 t->thread_code = func;
290 t->thread_arg = arg;
291 t->ticks = -1;
292 t->priority = -1; /* start in rq[0] */
293 t->cpu = NULL;
294 t->flags = 0;
295 t->state = Entering;
296 t->call_me = NULL;
297 t->call_me_with = NULL;
298
299 timeout_initialize(&t->sleep_timeout);
300 t->sleep_queue = NULL;
301 t->timeout_pending = 0;
302
303 t->rwlock_holder_type = RWLOCK_NONE;
[6a27d63]304
[bb68433]305 t->task = task;
306
[6f8a426]307 t->fpu_context_exists = 0;
308 t->fpu_context_engaged = 0;
[bb68433]309
310 /*
311 * Register this thread in the system-wide list.
312 */
313 ipl = interrupts_disable();
314 spinlock_lock(&threads_lock);
[016acbe]315 btree_insert(&threads_btree, (__native) t, (void *) t, NULL);
[bb68433]316 spinlock_unlock(&threads_lock);
317
318 /*
319 * Attach to the containing task.
320 */
321 spinlock_lock(&task->lock);
322 list_append(&t->th_link, &task->th_head);
323 spinlock_unlock(&task->lock);
324
325 interrupts_restore(ipl);
[6f8a426]326
[f761f1eb]327 return t;
328}
329
[70527f1]330/** Make thread exiting
331 *
332 * End current thread execution and switch it to the exiting
333 * state. All pending timeouts are executed.
334 *
335 */
[f761f1eb]336void thread_exit(void)
337{
[22f7769]338 ipl_t ipl;
[f761f1eb]339
340restart:
[22f7769]341 ipl = interrupts_disable();
[43114c5]342 spinlock_lock(&THREAD->lock);
343 if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */
344 spinlock_unlock(&THREAD->lock);
[22f7769]345 interrupts_restore(ipl);
[f761f1eb]346 goto restart;
347 }
[43114c5]348 THREAD->state = Exiting;
349 spinlock_unlock(&THREAD->lock);
[f761f1eb]350 scheduler();
351}
352
[70527f1]353
354/** Thread sleep
355 *
356 * Suspend execution of the current thread.
357 *
358 * @param sec Number of seconds to sleep.
359 *
360 */
[f761f1eb]361void thread_sleep(__u32 sec)
362{
[76cec1e]363 thread_usleep(sec*1000000);
[f761f1eb]364}
[70527f1]365
366/** Thread usleep
367 *
368 * Suspend execution of the current thread.
369 *
370 * @param usec Number of microseconds to sleep.
371 *
372 */
[f761f1eb]373void thread_usleep(__u32 usec)
374{
375 waitq_t wq;
376
377 waitq_initialize(&wq);
378
379 (void) waitq_sleep_timeout(&wq, usec, SYNCH_NON_BLOCKING);
380}
381
[70527f1]382/** Register thread out-of-context invocation
383 *
384 * Register a function and its argument to be executed
385 * on next context switch to the current thread.
386 *
387 * @param call_me Out-of-context function.
388 * @param call_me_with Out-of-context function argument.
389 *
390 */
[f761f1eb]391void thread_register_call_me(void (* call_me)(void *), void *call_me_with)
392{
[22f7769]393 ipl_t ipl;
[f761f1eb]394
[22f7769]395 ipl = interrupts_disable();
[43114c5]396 spinlock_lock(&THREAD->lock);
397 THREAD->call_me = call_me;
398 THREAD->call_me_with = call_me_with;
399 spinlock_unlock(&THREAD->lock);
[22f7769]400 interrupts_restore(ipl);
[f761f1eb]401}
[55ab0f1]402
403/** Print list of threads debug info */
404void thread_print_list(void)
405{
406 link_t *cur;
407 ipl_t ipl;
408
409 /* Messing with thread structures, avoid deadlock */
410 ipl = interrupts_disable();
411 spinlock_lock(&threads_lock);
412
[016acbe]413 for (cur = threads_btree.leaf_head.next; cur != &threads_btree.leaf_head; cur = cur->next) {
414 btree_node_t *node;
415 int i;
416
417 node = list_get_instance(cur, btree_node_t, leaf_link);
418 for (i = 0; i < node->keys; i++) {
419 thread_t *t;
420
421 t = (thread_t *) node->value[i];
422 printf("%s: address=%P, tid=%d, state=%s, task=%P, code=%P, stack=%P, cpu=",
423 t->name, t, t->tid, thread_states[t->state], t->task, t->thread_code, t->kstack);
424 if (t->cpu)
425 printf("cpu%d ", t->cpu->id);
426 else
427 printf("none");
428 printf("\n");
429 }
[55ab0f1]430 }
431
432 spinlock_unlock(&threads_lock);
[37c57f2]433 interrupts_restore(ipl);
[55ab0f1]434}
[9f52563]435
[016acbe]436/** Check whether thread exists.
437 *
438 * Note that threads_lock must be already held and
439 * interrupts must be already disabled.
440 *
441 * @param t Pointer to thread.
442 *
443 * @return True if thread t is known to the system, false otherwise.
444 */
445bool thread_exists(thread_t *t)
446{
447 btree_node_t *leaf;
448
449 return btree_search(&threads_btree, (__native) t, &leaf) != NULL;
450}
451
[9f52563]452/** Process syscall to create new thread.
453 *
454 */
[0f250f9]455__native sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name)
[9f52563]456{
457 thread_t *t;
458 char namebuf[THREAD_NAME_BUFLEN];
[45fb65c]459 uspace_arg_t *kernel_uarg;
[9f52563]460 __u32 tid;
461
[0f250f9]462 copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN);
463
464 kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
465 copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
[9f52563]466
[0f250f9]467 if ((t = thread_create(uinit, kernel_uarg, TASK, 0, namebuf))) {
[9f52563]468 tid = t->tid;
469 thread_ready(t);
470 return (__native) tid;
471 } else {
[0f250f9]472 free(kernel_uarg);
[9f52563]473 }
474
475 return (__native) -1;
476}
477
478/** Process syscall to terminate thread.
479 *
480 */
[0f250f9]481__native sys_thread_exit(int uspace_status)
[9f52563]482{
483 thread_exit();
484 /* Unreachable */
485 return 0;
486}
Note: See TracBrowser for help on using the repository browser.