~jsvoboda/helenos/dnsr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
/*
 * Copyright (c) 2010 Jakub Jermar
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @addtogroup genericproc
 * @{
 */

/**
 * @file
 * @brief Thread management functions.
 */

#include <proc/scheduler.h>
#include <proc/thread.h>
#include <proc/task.h>
#include <mm/frame.h>
#include <mm/page.h>
#include <arch/asm.h>
#include <arch/cycle.h>
#include <arch.h>
#include <synch/spinlock.h>
#include <synch/waitq.h>
#include <cpu.h>
#include <str.h>
#include <context.h>
#include <adt/avl.h>
#include <adt/list.h>
#include <time/clock.h>
#include <time/timeout.h>
#include <time/delay.h>
#include <config.h>
#include <arch/interrupt.h>
#include <smp/ipi.h>
#include <arch/faddr.h>
#include <atomic.h>
#include <memstr.h>
#include <print.h>
#include <mm/slab.h>
#include <debug.h>
#include <main/uinit.h>
#include <syscall/copy.h>
#include <errno.h>

/** Thread states */
const char *thread_states[] = {
	"Invalid",
	"Running",
	"Sleeping",
	"Ready",
	"Entering",
	"Exiting",
	"Lingering"
};

typedef struct {
	thread_id_t thread_id;
	thread_t *thread;
} thread_iterator_t;

/** Lock protecting the threads_tree AVL tree.
 *
 * For locking rules, see declaration thereof.
 *
 */
IRQ_SPINLOCK_INITIALIZE(threads_lock);

/** AVL tree of all threads.
 *
 * When a thread is found in the threads_tree AVL tree, it is guaranteed to
 * exist as long as the threads_lock is held.
 *
 */
avltree_t threads_tree;

IRQ_SPINLOCK_STATIC_INITIALIZE(tidlock);
static thread_id_t last_tid = 0;

static slab_cache_t *thread_slab;

#ifdef CONFIG_FPU
slab_cache_t *fpu_context_slab;
#endif

/** Thread wrapper.
 *
 * This wrapper is provided to ensure that every thread makes a call to
 * thread_exit() when its implementing function returns.
 *
 * interrupts_disable() is assumed.
 *
 */
static void cushion(void)
{
	void (*f)(void *) = THREAD->thread_code;
	void *arg = THREAD->thread_arg;
	THREAD->last_cycle = get_cycle();
	
	/* This is where each thread wakes up after its creation */
	irq_spinlock_unlock(&THREAD->lock, false);
	interrupts_enable();
	
	f(arg);
	
	/* Accumulate accounting to the task */
	irq_spinlock_lock(&THREAD->lock, true);
	if (!THREAD->uncounted) {
		thread_update_accounting(true);
		uint64_t ucycles = THREAD->ucycles;
		THREAD->ucycles = 0;
		uint64_t kcycles = THREAD->kcycles;
		THREAD->kcycles = 0;
		
		irq_spinlock_pass(&THREAD->lock, &TASK->lock);
		TASK->ucycles += ucycles;
		TASK->kcycles += kcycles;
		irq_spinlock_unlock(&TASK->lock, true);
	} else
		irq_spinlock_unlock(&THREAD->lock, true);
	
	thread_exit();
	
	/* Not reached */
}

/** Initialization and allocation for thread_t structure
 *
 */
static int thr_constructor(void *obj, unsigned int kmflags)
{
	thread_t *thread = (thread_t *) obj;
	
	irq_spinlock_initialize(&thread->lock, "thread_t_lock");
	link_initialize(&thread->rq_link);
	link_initialize(&thread->wq_link);
	link_initialize(&thread->th_link);
	
	/* call the architecture-specific part of the constructor */
	thr_constructor_arch(thread);
	
#ifdef CONFIG_FPU
#ifdef CONFIG_FPU_LAZY
	thread->saved_fpu_context = NULL;
#else /* CONFIG_FPU_LAZY */
	thread->saved_fpu_context = slab_alloc(fpu_context_slab, kmflags);
	if (!thread->saved_fpu_context)
		return -1;
#endif /* CONFIG_FPU_LAZY */
#endif /* CONFIG_FPU */
	
	/*
	 * Allocate the kernel stack from the low-memory to prevent an infinite
	 * nesting of TLB-misses when accessing the stack from the part of the
	 * TLB-miss handler written in C.
	 *
	 * Note that low-memory is safe to be used for the stack as it will be
	 * covered by the kernel identity mapping, which guarantees not to
	 * nest TLB-misses infinitely (either via some hardware mechanism or
	 * by the construciton of the assembly-language part of the TLB-miss
	 * handler).
	 *
	 * This restriction can be lifted once each architecture provides
	 * a similar guarantee, for example by locking the kernel stack
	 * in the TLB whenever it is allocated from the high-memory and the
	 * thread is being scheduled to run.
	 */
	kmflags |= FRAME_LOWMEM;
	kmflags &= ~FRAME_HIGHMEM;
	
	thread->kstack = (uint8_t *) frame_alloc(STACK_FRAMES, FRAME_KA | kmflags);
	if (!thread->kstack) {
#ifdef CONFIG_FPU
		if (thread->saved_fpu_context)
			slab_free(fpu_context_slab, thread->saved_fpu_context);
#endif
		return -1;
	}
	
#ifdef CONFIG_UDEBUG
	mutex_initialize(&thread->udebug.lock, MUTEX_PASSIVE);
#endif
	
	return 0;
}

/** Destruction of thread_t object */
static size_t thr_destructor(void *obj)
{
	thread_t *thread = (thread_t *) obj;
	
	/* call the architecture-specific part of the destructor */
	thr_destructor_arch(thread);
	
	frame_free(KA2PA(thread->kstack));
	
#ifdef CONFIG_FPU
	if (thread->saved_fpu_context)
		slab_free(fpu_context_slab, thread->saved_fpu_context);
#endif
	
	return 1;  /* One page freed */
}

/** Initialize threads
 *
 * Initialize kernel threads support.
 *
 */
void thread_init(void)
{
	THREAD = NULL;
	
	atomic_set(&nrdy, 0);
	thread_slab = slab_cache_create("thread_t", sizeof(thread_t), 0,
	    thr_constructor, thr_destructor, 0);
	
#ifdef CONFIG_FPU
	fpu_context_slab = slab_cache_create("fpu_context_t",
	    sizeof(fpu_context_t), FPU_CONTEXT_ALIGN, NULL, NULL, 0);
#endif
	
	avltree_create(&threads_tree);
}

/** Wire thread to the given CPU
 *
 * @param cpu CPU to wire the thread to.
 *
 */
void thread_wire(thread_t *thread, cpu_t *cpu)
{
	irq_spinlock_lock(&thread->lock, true);
	thread->cpu = cpu;
	thread->wired = true;
	irq_spinlock_unlock(&thread->lock, true);
}

/** Make thread ready
 *
 * Switch thread to the ready state.
 *
 * @param thread Thread to make ready.
 *
 */
void thread_ready(thread_t *thread)
{
	irq_spinlock_lock(&thread->lock, true);
	
	ASSERT(thread->state != Ready);
	
	int i = (thread->priority < RQ_COUNT - 1) ?
	    ++thread->priority : thread->priority;
	
	cpu_t *cpu;
	if (thread->wired || thread->nomigrate || thread->fpu_context_engaged) {
		ASSERT(thread->cpu != NULL);
		cpu = thread->cpu;
	} else
		cpu = CPU;
	
	thread->state = Ready;
	
	irq_spinlock_pass(&thread->lock, &(cpu->rq[i].lock));
	
	/*
	 * Append thread to respective ready queue
	 * on respective processor.
	 */
	
	list_append(&thread->rq_link, &cpu->rq[i].rq);
	cpu->rq[i].n++;
	irq_spinlock_unlock(&(cpu->rq[i].lock), true);
	
	atomic_inc(&nrdy);
	// FIXME: Why is the avg value not used
	// avg = atomic_get(&nrdy) / config.cpu_active;
	atomic_inc(&cpu->nrdy);
}

/** Create new thread
 *
 * Create a new thread.
 *
 * @param func      Thread's implementing function.
 * @param arg       Thread's implementing function argument.
 * @param task      Task to which the thread belongs. The caller must
 *                  guarantee that the task won't cease to exist during the
 *                  call. The task's lock may not be held.
 * @param flags     Thread flags.
 * @param name      Symbolic name (a copy is made).
 *
 * @return New thread's structure on success, NULL on failure.
 *
 */
thread_t *thread_create(void (* func)(void *), void *arg, task_t *task,
    thread_flags_t flags, const char *name)
{
	thread_t *thread = (thread_t *) slab_alloc(thread_slab, 0);
	if (!thread)
		return NULL;
	
	/* Not needed, but good for debugging */
	memsetb(thread->kstack, STACK_SIZE, 0);
	
	irq_spinlock_lock(&tidlock, true);
	thread->tid = ++last_tid;
	irq_spinlock_unlock(&tidlock, true);
	
	context_save(&thread->saved_context);
	context_set(&thread->saved_context, FADDR(cushion),
	    (uintptr_t) thread->kstack, STACK_SIZE);
	
	the_initialize((the_t *) thread->kstack);
	
	ipl_t ipl = interrupts_disable();
	thread->saved_context.ipl = interrupts_read();
	interrupts_restore(ipl);
	
	str_cpy(thread->name, THREAD_NAME_BUFLEN, name);
	
	thread->thread_code = func;
	thread->thread_arg = arg;
	thread->ticks = -1;
	thread->ucycles = 0;
	thread->kcycles = 0;
	thread->uncounted =
	    ((flags & THREAD_FLAG_UNCOUNTED) == THREAD_FLAG_UNCOUNTED);
	thread->priority = -1;          /* Start in rq[0] */
	thread->cpu = NULL;
	thread->wired = false;
	thread->stolen = false;
	thread->uspace =
	    ((flags & THREAD_FLAG_USPACE) == THREAD_FLAG_USPACE);
	
	thread->nomigrate = 0;
	thread->state = Entering;
	
	timeout_initialize(&thread->sleep_timeout);
	thread->sleep_interruptible = false;
	thread->sleep_queue = NULL;
	thread->timeout_pending = false;
	
	thread->in_copy_from_uspace = false;
	thread->in_copy_to_uspace = false;
	
	thread->interrupted = false;
	thread->detached = false;
	waitq_initialize(&thread->join_wq);
	
	thread->task = task;
	
	thread->fpu_context_exists = false;
	thread->fpu_context_engaged = false;
	
	avltree_node_initialize(&thread->threads_tree_node);
	thread->threads_tree_node.key = (uintptr_t) thread;
	
#ifdef CONFIG_UDEBUG
	/* Initialize debugging stuff */
	thread->btrace = false;
	udebug_thread_initialize(&thread->udebug);
#endif
	
	/* Might depend on previous initialization */
	thread_create_arch(thread);
	
	if ((flags & THREAD_FLAG_NOATTACH) != THREAD_FLAG_NOATTACH)
		thread_attach(thread, task);
	
	return thread;
}

/** Destroy thread memory structure
 *
 * Detach thread from all queues, cpus etc. and destroy it.
 *
 * @param thread  Thread to be destroyed.
 * @param irq_res Indicate whether it should unlock thread->lock
 *                in interrupts-restore mode.
 *
 */
void thread_destroy(thread_t *thread, bool irq_res)
{
	ASSERT(irq_spinlock_locked(&thread->lock));
	ASSERT((thread->state == Exiting) || (thread->state == Lingering));
	ASSERT(thread->task);
	ASSERT(thread->cpu);
	
	irq_spinlock_lock(&thread->cpu->lock, false);
	if (thread->cpu->fpu_owner == thread)
		thread->cpu->fpu_owner = NULL;
	irq_spinlock_unlock(&thread->cpu->lock, false);
	
	irq_spinlock_pass(&thread->lock, &threads_lock);
	
	avltree_delete(&threads_tree, &thread->threads_tree_node);
	
	irq_spinlock_pass(&threads_lock, &thread->task->lock);
	
	/*
	 * Detach from the containing task.
	 */
	list_remove(&thread->th_link);
	irq_spinlock_unlock(&thread->task->lock, irq_res);
	
	/*
	 * Drop the reference to the containing task.
	 */
	task_release(thread->task);
	slab_free(thread_slab, thread);
}

/** Make the thread visible to the system.
 *
 * Attach the thread structure to the current task and make it visible in the
 * threads_tree.
 *
 * @param t    Thread to be attached to the task.
 * @param task Task to which the thread is to be attached.
 *
 */
void thread_attach(thread_t *thread, task_t *task)
{
	/*
	 * Attach to the specified task.
	 */
	irq_spinlock_lock(&task->lock, true);
	
	/* Hold a reference to the task. */
	task_hold(task);
	
	/* Must not count kbox thread into lifecount */
	if (thread->uspace)
		atomic_inc(&task->lifecount);
	
	list_append(&thread->th_link, &task->threads);
	
	irq_spinlock_pass(&task->lock, &threads_lock);
	
	/*
	 * Register this thread in the system-wide list.
	 */
	avltree_insert(&threads_tree, &thread->threads_tree_node);
	irq_spinlock_unlock(&threads_lock, true);
}

/** Terminate thread.
 *
 * End current thread execution and switch it to the exiting state.
 * All pending timeouts are executed.
 *
 */
void thread_exit(void)
{
	if (THREAD->uspace) {
#ifdef CONFIG_UDEBUG
		/* Generate udebug THREAD_E event */
		udebug_thread_e_event();
		
		/*
		 * This thread will not execute any code or system calls from
		 * now on.
		 */
		udebug_stoppable_begin();
#endif
		if (atomic_predec(&TASK->lifecount) == 0) {
			/*
			 * We are the last userspace thread in the task that
			 * still has not exited. With the exception of the
			 * moment the task was created, new userspace threads
			 * can only be created by threads of the same task.
			 * We are safe to perform cleanup.
			 *
			 */
			ipc_cleanup();
			futex_cleanup();
			LOG("Cleanup of task %" PRIu64" completed.", TASK->taskid);
		}
	}
	
restart:
	irq_spinlock_lock(&THREAD->lock, true);
	if (THREAD->timeout_pending) {
		/* Busy waiting for timeouts in progress */
		irq_spinlock_unlock(&THREAD->lock, true);
		goto restart;
	}
	
	THREAD->state = Exiting;
	irq_spinlock_unlock(&THREAD->lock, true);
	
	scheduler();
	
	/* Not reached */
	while (true);
}

/** Prevent the current thread from being migrated to another processor. */
void thread_migration_disable(void)
{
	ASSERT(THREAD);
	
	THREAD->nomigrate++;
}

/** Allow the current thread to be migrated to another processor. */
void thread_migration_enable(void)
{
	ASSERT(THREAD);
	ASSERT(THREAD->nomigrate > 0);
	
	if (THREAD->nomigrate > 0)
		THREAD->nomigrate--;
}

/** Thread sleep
 *
 * Suspend execution of the current thread.
 *
 * @param sec Number of seconds to sleep.
 *
 */
void thread_sleep(uint32_t sec)
{
	/* Sleep in 1000 second steps to support
	   full argument range */
	while (sec > 0) {
		uint32_t period = (sec > 1000) ? 1000 : sec;
		
		thread_usleep(period * 1000000);
		sec -= period;
	}
}

/** Wait for another thread to exit.
 *
 * @param thread Thread to join on exit.
 * @param usec   Timeout in microseconds.
 * @param flags  Mode of operation.
 *
 * @return An error code from errno.h or an error code from synch.h.
 *
 */
int thread_join_timeout(thread_t *thread, uint32_t usec, unsigned int flags)
{
	if (thread == THREAD)
		return EINVAL;
	
	/*
	 * Since thread join can only be called once on an undetached thread,
	 * the thread pointer is guaranteed to be still valid.
	 */
	
	irq_spinlock_lock(&thread->lock, true);
	ASSERT(!thread->detached);
	irq_spinlock_unlock(&thread->lock, true);
	
	return waitq_sleep_timeout(&thread->join_wq, usec, flags);
}

/** Detach thread.
 *
 * Mark the thread as detached. If the thread is already
 * in the Lingering state, deallocate its resources.
 *
 * @param thread Thread to be detached.
 *
 */
void thread_detach(thread_t *thread)
{
	/*
	 * Since the thread is expected not to be already detached,
	 * pointer to it must be still valid.
	 */
	irq_spinlock_lock(&thread->lock, true);
	ASSERT(!thread->detached);
	
	if (thread->state == Lingering) {
		/*
		 * Unlock &thread->lock and restore
		 * interrupts in thread_destroy().
		 */
		thread_destroy(thread, true);
		return;
	} else {
		thread->detached = true;
	}
	
	irq_spinlock_unlock(&thread->lock, true);
}

/** Thread usleep
 *
 * Suspend execution of the current thread.
 *
 * @param usec Number of microseconds to sleep.
 *
 */	
void thread_usleep(uint32_t usec)
{
	waitq_t wq;
	
	waitq_initialize(&wq);
	
	(void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING);
}

static bool thread_walker(avltree_node_t *node, void *arg)
{
	bool *additional = (bool *) arg;
	thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
	
	uint64_t ucycles, kcycles;
	char usuffix, ksuffix;
	order_suffix(thread->ucycles, &ucycles, &usuffix);
	order_suffix(thread->kcycles, &kcycles, &ksuffix);
	
	char *name;
	if (str_cmp(thread->name, "uinit") == 0)
		name = thread->task->name;
	else
		name = thread->name;
	
#ifdef __32_BITS__
	if (*additional)
		printf("%-8" PRIu64 " %10p %10p %9" PRIu64 "%c %9" PRIu64 "%c ",
		    thread->tid, thread->thread_code, thread->kstack,
		    ucycles, usuffix, kcycles, ksuffix);
	else
		printf("%-8" PRIu64 " %-14s %10p %-8s %10p %-5" PRIu32 "\n",
		    thread->tid, name, thread, thread_states[thread->state],
		    thread->task, thread->task->container);
#endif
	
#ifdef __64_BITS__
	if (*additional)
		printf("%-8" PRIu64 " %18p %18p\n"
		    "         %9" PRIu64 "%c %9" PRIu64 "%c ",
		    thread->tid, thread->thread_code, thread->kstack,
		    ucycles, usuffix, kcycles, ksuffix);
	else
		printf("%-8" PRIu64 " %-14s %18p %-8s %18p %-5" PRIu32 "\n",
		    thread->tid, name, thread, thread_states[thread->state],
		    thread->task, thread->task->container);
#endif
	
	if (*additional) {
		if (thread->cpu)
			printf("%-5u", thread->cpu->id);
		else
			printf("none ");
		
		if (thread->state == Sleeping) {
#ifdef __32_BITS__
			printf(" %10p", thread->sleep_queue);
#endif
			
#ifdef __64_BITS__
			printf(" %18p", thread->sleep_queue);
#endif
		}
		
		printf("\n");
	}
	
	return true;
}

/** Print list of threads debug info
 *
 * @param additional Print additional information.
 *
 */
void thread_print_list(bool additional)
{
	/* Messing with thread structures, avoid deadlock */
	irq_spinlock_lock(&threads_lock, true);
	
#ifdef __32_BITS__
	if (additional)
		printf("[id    ] [code    ] [stack   ] [ucycles ] [kcycles ]"
		    " [cpu] [waitqueue]\n");
	else
		printf("[id    ] [name        ] [address ] [state ] [task    ]"
		    " [ctn]\n");
#endif
	
#ifdef __64_BITS__
	if (additional) {
		printf("[id    ] [code            ] [stack           ]\n"
		    "         [ucycles ] [kcycles ] [cpu] [waitqueue       ]\n");
	} else
		printf("[id    ] [name        ] [address         ] [state ]"
		    " [task            ] [ctn]\n");
#endif
	
	avltree_walk(&threads_tree, thread_walker, &additional);
	
	irq_spinlock_unlock(&threads_lock, true);
}

/** Check whether thread exists.
 *
 * Note that threads_lock must be already held and
 * interrupts must be already disabled.
 *
 * @param thread Pointer to thread.
 *
 * @return True if thread t is known to the system, false otherwise.
 *
 */
bool thread_exists(thread_t *thread)
{
	ASSERT(interrupts_disabled());
	ASSERT(irq_spinlock_locked(&threads_lock));

	avltree_node_t *node =
	    avltree_search(&threads_tree, (avltree_key_t) ((uintptr_t) thread));
	
	return node != NULL;
}

/** Update accounting of current thread.
 *
 * Note that thread_lock on THREAD must be already held and
 * interrupts must be already disabled.
 *
 * @param user True to update user accounting, false for kernel.
 *
 */
void thread_update_accounting(bool user)
{
	uint64_t time = get_cycle();

	ASSERT(interrupts_disabled());
	ASSERT(irq_spinlock_locked(&THREAD->lock));
	
	if (user)
		THREAD->ucycles += time - THREAD->last_cycle;
	else
		THREAD->kcycles += time - THREAD->last_cycle;
	
	THREAD->last_cycle = time;
}

static bool thread_search_walker(avltree_node_t *node, void *arg)
{
	thread_t *thread =
	    (thread_t *) avltree_get_instance(node, thread_t, threads_tree_node);
	thread_iterator_t *iterator = (thread_iterator_t *) arg;
	
	if (thread->tid == iterator->thread_id) {
		iterator->thread = thread;
		return false;
	}
	
	return true;
}

/** Find thread structure corresponding to thread ID.
 *
 * The threads_lock must be already held by the caller of this function and
 * interrupts must be disabled.
 *
 * @param id Thread ID.
 *
 * @return Thread structure address or NULL if there is no such thread ID.
 *
 */
thread_t *thread_find_by_id(thread_id_t thread_id)
{
	ASSERT(interrupts_disabled());
	ASSERT(irq_spinlock_locked(&threads_lock));
	
	thread_iterator_t iterator;
	
	iterator.thread_id = thread_id;
	iterator.thread = NULL;
	
	avltree_walk(&threads_tree, thread_search_walker, (void *) &iterator);
	
	return iterator.thread;
}

#ifdef CONFIG_UDEBUG

void thread_stack_trace(thread_id_t thread_id)
{
	irq_spinlock_lock(&threads_lock, true);
	
	thread_t *thread = thread_find_by_id(thread_id);
	if (thread == NULL) {
		printf("No such thread.\n");
		irq_spinlock_unlock(&threads_lock, true);
		return;
	}
	
	irq_spinlock_lock(&thread->lock, false);
	
	/*
	 * Schedule a stack trace to be printed
	 * just before the thread is scheduled next.
	 *
	 * If the thread is sleeping then try to interrupt
	 * the sleep. Any request for printing an uspace stack
	 * trace from within the kernel should be always
	 * considered a last resort debugging means, therefore
	 * forcing the thread's sleep to be interrupted
	 * is probably justifiable.
	 */
	
	bool sleeping = false;
	istate_t *istate = thread->udebug.uspace_state;
	if (istate != NULL) {
		printf("Scheduling thread stack trace.\n");
		thread->btrace = true;
		if (thread->state == Sleeping)
			sleeping = true;
	} else
		printf("Thread interrupt state not available.\n");
	
	irq_spinlock_unlock(&thread->lock, false);
	
	if (sleeping)
		waitq_interrupt_sleep(thread);
	
	irq_spinlock_unlock(&threads_lock, true);
}

#endif /* CONFIG_UDEBUG */

/** Process syscall to create new thread.
 *
 */
sysarg_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name,
    size_t name_len, thread_id_t *uspace_thread_id)
{
	if (name_len > THREAD_NAME_BUFLEN - 1)
		name_len = THREAD_NAME_BUFLEN - 1;
	
	char namebuf[THREAD_NAME_BUFLEN];
	int rc = copy_from_uspace(namebuf, uspace_name, name_len);
	if (rc != 0)
		return (sysarg_t) rc;
	
	namebuf[name_len] = 0;
	
	/*
	 * In case of failure, kernel_uarg will be deallocated in this function.
	 * In case of success, kernel_uarg will be freed in uinit().
	 */
	uspace_arg_t *kernel_uarg =
	    (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
	
	rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
	if (rc != 0) {
		free(kernel_uarg);
		return (sysarg_t) rc;
	}
	
	thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
	    THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf);
	if (thread) {
		if (uspace_thread_id != NULL) {
			rc = copy_to_uspace(uspace_thread_id, &thread->tid,
			    sizeof(thread->tid));
			if (rc != 0) {
				/*
				 * We have encountered a failure, but the thread
				 * has already been created. We need to undo its
				 * creation now.
				 */
				
				/*
				 * The new thread structure is initialized, but
				 * is still not visible to the system.
				 * We can safely deallocate it.
				 */
				slab_free(thread_slab, thread);
				free(kernel_uarg);
				
				return (sysarg_t) rc;
			 }
		}
		
#ifdef CONFIG_UDEBUG
		/*
		 * Generate udebug THREAD_B event and attach the thread.
		 * This must be done atomically (with the debug locks held),
		 * otherwise we would either miss some thread or receive
		 * THREAD_B events for threads that already existed
		 * and could be detected with THREAD_READ before.
		 */
		udebug_thread_b_event_attach(thread, TASK);
#else
		thread_attach(thread, TASK);
#endif
		thread_ready(thread);
		
		return 0;
	} else
		free(kernel_uarg);
	
	return (sysarg_t) ENOMEM;
}

/** Process syscall to terminate thread.
 *
 */
sysarg_t sys_thread_exit(int uspace_status)
{
	thread_exit();
	
	/* Unreachable */
	return 0;
}

/** Syscall for getting TID.
 *
 * @param uspace_thread_id Userspace address of 8-byte buffer where to store
 * current thread ID.
 *
 * @return 0 on success or an error code from @ref errno.h.
 *
 */
sysarg_t sys_thread_get_id(thread_id_t *uspace_thread_id)
{
	/*
	 * No need to acquire lock on THREAD because tid
	 * remains constant for the lifespan of the thread.
	 *
	 */
	return (sysarg_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
	    sizeof(THREAD->tid));
}

/** Syscall wrapper for sleeping. */
sysarg_t sys_thread_usleep(uint32_t usec)
{
	thread_usleep(usec);
	return 0;
}

sysarg_t sys_thread_udelay(uint32_t usec)
{
	delay(usec);
	return 0;
}

/** @}
 */