forked from teawater/libhermit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.c
More file actions
903 lines (698 loc) · 22.2 KB
/
tasks.c
File metadata and controls
903 lines (698 loc) · 22.2 KB
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
/*
* Copyright (c) 2010, Stefan Lankes, RWTH Aachen University
* 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.
* * Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 REGENTS OR CONTRIBUTORS 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.
*/
#include <hermit/stddef.h>
#include <hermit/stdlib.h>
#include <hermit/stdio.h>
#include <hermit/string.h>
#include <hermit/tasks.h>
#include <hermit/tasks_types.h>
#include <hermit/spinlock.h>
#include <hermit/time.h>
#include <hermit/errno.h>
#include <hermit/syscall.h>
#include <hermit/memory.h>
#include <hermit/logging.h>
#include <asm/processor.h>
/*
* Note that linker symbols are not variables, they have no memory allocated for
* maintaining a value, rather their address is their value.
*/
extern atomic_int32_t cpu_online;
volatile uint32_t go_down = 0;
/** @brief Array of task structures (aka PCB)
*
* A task's id will be its position in this array.
*/
static task_t task_table[MAX_TASKS] = { \
[0] = {0, TASK_IDLE, 0, NULL, NULL, NULL, TASK_DEFAULT_FLAGS, 0, 0, 0, 0, NULL, 0, NULL, NULL, 0, 0, 0, NULL, FPU_STATE_INIT}, \
[1 ... MAX_TASKS-1] = {0, TASK_INVALID, 0, NULL, NULL, NULL, TASK_DEFAULT_FLAGS, 0, 0, 0, 0, NULL, 0, NULL, NULL, 0, 0, 0, NULL, FPU_STATE_INIT}};
static spinlock_irqsave_t table_lock = SPINLOCK_IRQSAVE_INIT;
#if MAX_CORES > 1
static readyqueues_t readyqueues[MAX_CORES] = { \
[0 ... MAX_CORES-1] = {NULL, NULL, 0, 0, 0, {[0 ... MAX_PRIO-2] = {NULL, NULL}}, {NULL, NULL}, SPINLOCK_IRQSAVE_INIT}};
#else
static readyqueues_t readyqueues[1] = {[0] = {task_table+0, NULL, 0, 0, 0, {[0 ... MAX_PRIO-2] = {NULL, NULL}}, {NULL, NULL}, SPINLOCK_IRQSAVE_INIT}};
#endif
DEFINE_PER_CORE(task_t*, current_task, task_table+0);
#if MAX_CORES > 1
DEFINE_PER_CORE(uint32_t, __core_id, 0);
#endif
static void update_timer(task_t* first)
{
if (first) {
if(first->timeout > get_clock_tick()) {
timer_deadline((uint32_t) (first->timeout - get_clock_tick()));
} else {
// workaround: start timer so new head will be serviced
timer_deadline(1);
}
} else {
// prevent spurious interrupts
timer_disable();
}
}
static void timer_queue_remove(uint32_t core_id, task_t* task)
{
if(BUILTIN_EXPECT(!task, 0)) {
return;
}
task_list_t* timer_queue = &readyqueues[core_id].timers;
#ifdef DYNAMIC_TICKS
// if task is first in timer queue, we need to update the oneshot
// timer for the next task
if(timer_queue->first == task) {
update_timer(task->next);
}
#endif
task_list_remove_task(timer_queue, task);
}
static void timer_queue_push(uint32_t core_id, task_t* task)
{
task_list_t* timer_queue = &readyqueues[core_id].timers;
spinlock_irqsave_lock(&readyqueues[core_id].lock);
task_t* first = timer_queue->first;
if(!first) {
timer_queue->first = timer_queue->last = task;
task->next = task->prev = NULL;
#ifdef DYNAMIC_TICKS
update_timer(task);
#endif
} else {
// lookup position where to insert task
task_t* tmp = first;
while(tmp && (task->timeout >= tmp->timeout))
tmp = tmp->next;
if(!tmp) {
// insert at the end of queue
task->next = NULL;
task->prev = timer_queue->last;
// there has to be a last element because there is also a first one
timer_queue->last->next = task;
timer_queue->last = task;
} else {
task->next = tmp;
task->prev = tmp->prev;
tmp->prev = task;
if(task->prev)
task->prev->next = task;
if(timer_queue->first == tmp) {
timer_queue->first = task;
#ifdef DYNAMIC_TICKS
update_timer(task);
#endif
}
}
}
spinlock_irqsave_unlock(&readyqueues[core_id].lock);
}
static inline void readyqueues_push_back(uint32_t core_id, task_t* task)
{
// idle task (prio=0) doesn't have a queue
task_list_t* readyqueue = &readyqueues[core_id].queue[task->prio - 1];
task_list_push_back(readyqueue, task);
// update priority bitmap
readyqueues[core_id].prio_bitmap |= (1 << task->prio);
}
static inline void readyqueues_remove(uint32_t core_id, task_t* task)
{
// idle task (prio=0) doesn't have a queue
task_list_t* readyqueue = &readyqueues[core_id].queue[task->prio - 1];
task_list_remove_task(readyqueue, task);
// no valid task in queue => update priority bitmap
if (readyqueue->first == NULL)
readyqueues[core_id].prio_bitmap &= ~(1 << task->prio);
}
void fpu_handler(void)
{
task_t* task = per_core(current_task);
uint32_t core_id = CORE_ID;
task->flags |= TASK_FPU_USED;
if (!(task->flags & TASK_FPU_INIT)) {
// use the FPU at the first time => Initialize FPU
fpu_init(&task->fpu);
task->flags |= TASK_FPU_INIT;
}
if (readyqueues[core_id].fpu_owner == task->id)
return;
spinlock_irqsave_lock(&readyqueues[core_id].lock);
// did another already use the the FPU? => save FPU state
if (readyqueues[core_id].fpu_owner) {
save_fpu_state(&(task_table[readyqueues[core_id].fpu_owner].fpu));
task_table[readyqueues[core_id].fpu_owner].flags &= ~TASK_FPU_USED;
}
readyqueues[core_id].fpu_owner = task->id;
spinlock_irqsave_unlock(&readyqueues[core_id].lock);
restore_fpu_state(&task->fpu);
}
int is_task_available(void)
{
uint32_t core_id = CORE_ID;
return readyqueues[core_id].nr_tasks > 0 ? 1 : 0;
}
void check_scheduling(void)
{
uint32_t prio = get_highest_priority();
task_t* curr_task = per_core(current_task);
if (prio > curr_task->prio) {
reschedule();
#ifdef DYNAMIC_TICKS
} else if ((prio > 0) && (prio == curr_task->prio)) {
// if a task is ready, check if the current task runs already one tick (one time slice)
// => reschedule to realize round robin
const uint64_t diff_cycles = get_rdtsc() - curr_task->last_tsc;
const uint64_t cpu_freq_hz = 1000000ULL * (uint64_t) get_cpu_frequency();
if (((diff_cycles * (uint64_t) TIMER_FREQ) / cpu_freq_hz) > 0) {
LOG_DEBUG("Time slice expired for task %d on core %d. New task has priority %u.\n", curr_task->id, CORE_ID, prio);
reschedule();
}
#endif
}
}
uint32_t get_highest_priority(void)
{
uint32_t prio = msb(readyqueues[CORE_ID].prio_bitmap);
if (prio > MAX_PRIO)
return 0;
return prio;
}
void* get_readyqueue(void)
{
return &readyqueues[CORE_ID];
}
int multitasking_init(void)
{
uint32_t core_id = CORE_ID;
if (BUILTIN_EXPECT(task_table[0].status != TASK_IDLE, 0)) {
LOG_ERROR("Task 0 is not an idle task\n");
return -ENOMEM;
}
task_table[0].prio = IDLE_PRIO;
task_table[0].stack = NULL; // will be initialized later
task_table[0].ist_addr = NULL; // will be initialized later
set_per_core(current_task, task_table+0);
readyqueues[core_id].idle = task_table+0;
return 0;
}
int set_boot_stack(tid_t id, size_t stack, size_t ist_addr)
{
if (id < MAX_CORES) {
task_table[id].stack = (void*) stack;
task_table[id].ist_addr = (void*) ist_addr;
return 0;
}
return -EINVAL;
}
tid_t set_idle_task(void)
{
uint32_t core_id = CORE_ID;
tid_t id = ~0;
spinlock_irqsave_lock(&table_lock);
for(uint32_t i=0; i<MAX_TASKS; i++) {
if (task_table[i].status == TASK_INVALID) {
task_table[i].id = id = i;
task_table[i].status = TASK_IDLE;
task_table[i].last_core = core_id;
task_table[i].last_stack_pointer = NULL;
task_table[i].stack = NULL;
task_table[i].ist_addr = NULL;
task_table[i].prio = IDLE_PRIO;
task_table[i].heap = NULL;
readyqueues[core_id].idle = task_table+i;
set_per_core(current_task, readyqueues[core_id].idle);
break;
}
}
spinlock_irqsave_unlock(&table_lock);
return id;
}
void finish_task_switch(void)
{
task_t* old;
const uint32_t core_id = CORE_ID;
spinlock_irqsave_lock(&readyqueues[core_id].lock);
if ((old = readyqueues[core_id].old_task) != NULL) {
readyqueues[core_id].old_task = NULL;
if (old->status == TASK_FINISHED) {
/* cleanup task */
if (old->stack) {
//LOG_INFO("Release stack at 0x%zx\n", old->stack);
destroy_stack(old->stack, DEFAULT_STACK_SIZE);
old->stack = NULL;
}
if (!old->parent && old->heap) {
kfree(old->heap);
old->heap = NULL;
}
if (old->ist_addr) {
destroy_stack(old->ist_addr, KERNEL_STACK_SIZE);
old->ist_addr = NULL;
}
old->last_stack_pointer = NULL;
if (readyqueues[core_id].fpu_owner == old->id)
readyqueues[core_id].fpu_owner = 0;
/* signalizes that this task could be reused */
old->status = TASK_INVALID;
} else {
// re-enqueue old task
readyqueues_push_back(core_id, old);
}
}
spinlock_irqsave_unlock(&readyqueues[core_id].lock);
}
void NORETURN do_exit(int arg)
{
task_t* curr_task = per_core(current_task);
const uint32_t core_id = CORE_ID;
LOG_INFO("Terminate task: %u, return value %d\n", curr_task->id, arg);
uint8_t flags = irq_nested_disable();
// decrease the number of active tasks
spinlock_irqsave_lock(&readyqueues[core_id].lock);
readyqueues[core_id].nr_tasks--;
spinlock_irqsave_unlock(&readyqueues[core_id].lock);
// release the thread local storage
destroy_tls();
curr_task->status = TASK_FINISHED;
reschedule();
irq_nested_enable(flags);
LOG_ERROR("Kernel panic: scheduler found no valid task\n");
while(1) {
HALT;
}
}
void NORETURN leave_kernel_task(void) {
int result;
result = 0; //get_return_value();
do_exit(result);
}
void NORETURN do_abort(void) {
do_exit(-1);
}
static uint32_t get_next_core_id(void)
{
uint32_t i;
static uint32_t core_id = MAX_CORES;
if (core_id >= MAX_CORES)
core_id = CORE_ID;
// we assume OpenMP applications
// => number of threads is (normaly) equal to the number of cores
// => search next available core
for(i=0, core_id=(core_id+1)%MAX_CORES; i<2*MAX_CORES; i++, core_id=(core_id+1)%MAX_CORES)
if (readyqueues[core_id].idle)
break;
if (BUILTIN_EXPECT(!readyqueues[core_id].idle, 0)) {
LOG_ERROR("BUG: no core available!\n");
return MAX_CORES;
}
return core_id;
}
int clone_task(tid_t* id, entry_point_t ep, void* arg, uint8_t prio)
{
int ret = -EINVAL;
uint32_t i;
void* stack = NULL;
void* ist = NULL;
task_t* curr_task;
uint32_t core_id;
if (BUILTIN_EXPECT(!ep, 0))
return -EINVAL;
if (BUILTIN_EXPECT(prio == IDLE_PRIO, 0))
return -EINVAL;
if (BUILTIN_EXPECT(prio > MAX_PRIO, 0))
return -EINVAL;
curr_task = per_core(current_task);
stack = create_stack(DEFAULT_STACK_SIZE);
if (BUILTIN_EXPECT(!stack, 0))
return -ENOMEM;
ist = create_stack(KERNEL_STACK_SIZE);
if (BUILTIN_EXPECT(!ist, 0)) {
destroy_stack(stack, DEFAULT_STACK_SIZE);
return -ENOMEM;
}
spinlock_irqsave_lock(&table_lock);
core_id = get_next_core_id();
if (BUILTIN_EXPECT(core_id >= MAX_CORES, 0))
{
spinlock_irqsave_unlock(&table_lock);
ret = -EINVAL;
goto out;
}
for(i=0; i<MAX_TASKS; i++) {
if (task_table[i].status == TASK_INVALID) {
task_table[i].id = i;
task_table[i].status = TASK_READY;
task_table[i].last_core = core_id;
task_table[i].last_stack_pointer = NULL;
task_table[i].stack = stack;
task_table[i].prio = prio;
task_table[i].heap = curr_task->heap;
task_table[i].start_tick = get_clock_tick();
task_table[i].last_tsc = 0;
task_table[i].parent = curr_task->id;
task_table[i].tls_addr = curr_task->tls_addr;
task_table[i].tls_size = curr_task->tls_size;
task_table[i].ist_addr = ist;
task_table[i].lwip_err = 0;
task_table[i].signal_handler = NULL;
if (id)
*id = i;
ret = create_default_frame(task_table+i, ep, arg, core_id);
if (ret)
goto out;
// add task in the readyqueues
spinlock_irqsave_lock(&readyqueues[core_id].lock);
readyqueues[core_id].prio_bitmap |= (1 << prio);
readyqueues[core_id].nr_tasks++;
if (!readyqueues[core_id].queue[prio-1].first) {
task_table[i].next = task_table[i].prev = NULL;
readyqueues[core_id].queue[prio-1].first = task_table+i;
readyqueues[core_id].queue[prio-1].last = task_table+i;
} else {
task_table[i].prev = readyqueues[core_id].queue[prio-1].last;
task_table[i].next = NULL;
readyqueues[core_id].queue[prio-1].last->next = task_table+i;
readyqueues[core_id].queue[prio-1].last = task_table+i;
}
// should we wakeup the core?
if (readyqueues[core_id].nr_tasks == 1)
wakeup_core(core_id);
spinlock_irqsave_unlock(&readyqueues[core_id].lock);
break;
}
}
spinlock_irqsave_unlock(&table_lock);
if (!ret) {
LOG_DEBUG("start new thread %d on core %d with stack address %p\n", i, core_id, stack);
}
out:
if (ret) {
destroy_stack(stack, DEFAULT_STACK_SIZE);
destroy_stack(ist, KERNEL_STACK_SIZE);
}
return ret;
}
int create_task(tid_t* id, entry_point_t ep, void* arg, uint8_t prio, uint32_t core_id)
{
int ret = -ENOMEM;
uint32_t i;
void* stack = NULL;
void* ist = NULL;
void* counter = NULL;
if (BUILTIN_EXPECT(!ep, 0))
return -EINVAL;
if (BUILTIN_EXPECT(prio == IDLE_PRIO, 0))
return -EINVAL;
if (BUILTIN_EXPECT(prio > MAX_PRIO, 0))
return -EINVAL;
if (BUILTIN_EXPECT(core_id >= MAX_CORES, 0))
return -EINVAL;
if (BUILTIN_EXPECT(!readyqueues[core_id].idle, 0))
return -EINVAL;
stack = create_stack(DEFAULT_STACK_SIZE);
if (BUILTIN_EXPECT(!stack, 0))
return -ENOMEM;
ist = create_stack(KERNEL_STACK_SIZE);
if (BUILTIN_EXPECT(!ist, 0)) {
destroy_stack(stack, DEFAULT_STACK_SIZE);
return -ENOMEM;
}
counter = kmalloc(sizeof(atomic_int64_t));
if (BUILTIN_EXPECT(!counter, 0)) {
destroy_stack(stack, KERNEL_STACK_SIZE);
destroy_stack(stack, DEFAULT_STACK_SIZE);
return -ENOMEM;
}
atomic_int64_set((atomic_int64_t*) counter, 0);
spinlock_irqsave_lock(&table_lock);
for(i=0; i<MAX_TASKS; i++) {
if (task_table[i].status == TASK_INVALID) {
task_table[i].id = i;
task_table[i].status = TASK_READY;
task_table[i].last_core = core_id;
task_table[i].last_stack_pointer = NULL;
task_table[i].stack = stack;
task_table[i].prio = prio;
task_table[i].heap = NULL;
task_table[i].start_tick = get_clock_tick();
task_table[i].last_tsc = 0;
task_table[i].parent = 0;
task_table[i].ist_addr = ist;
task_table[i].tls_addr = 0;
task_table[i].tls_size = 0;
task_table[i].lwip_err = 0;
task_table[i].signal_handler = NULL;
if (id)
*id = i;
ret = create_default_frame(task_table+i, ep, arg, core_id);
if (ret)
goto out;
// add task in the readyqueues
spinlock_irqsave_lock(&readyqueues[core_id].lock);
readyqueues[core_id].prio_bitmap |= (1 << prio);
readyqueues[core_id].nr_tasks++;
if (!readyqueues[core_id].queue[prio-1].first) {
task_table[i].next = task_table[i].prev = NULL;
readyqueues[core_id].queue[prio-1].first = task_table+i;
readyqueues[core_id].queue[prio-1].last = task_table+i;
} else {
task_table[i].prev = readyqueues[core_id].queue[prio-1].last;
task_table[i].next = NULL;
readyqueues[core_id].queue[prio-1].last->next = task_table+i;
readyqueues[core_id].queue[prio-1].last = task_table+i;
}
spinlock_irqsave_unlock(&readyqueues[core_id].lock);
break;
}
}
if (!ret)
LOG_INFO("start new task %d on core %d with stack address %p\n", i, core_id, stack);
out:
spinlock_irqsave_unlock(&table_lock);
if (ret) {
destroy_stack(stack, DEFAULT_STACK_SIZE);
destroy_stack(ist, KERNEL_STACK_SIZE);
kfree(counter);
}
return ret;
}
int create_kernel_task_on_core(tid_t* id, entry_point_t ep, void* args, uint8_t prio, uint32_t core_id)
{
if (prio > MAX_PRIO)
prio = NORMAL_PRIO;
return create_task(id, ep, args, prio, core_id);
}
int create_kernel_task(tid_t* id, entry_point_t ep, void* args, uint8_t prio)
{
if (prio > MAX_PRIO)
prio = NORMAL_PRIO;
return create_task(id, ep, args, prio, CORE_ID);
}
int wakeup_task(tid_t id)
{
task_t* task;
uint32_t core_id;
int ret = -EINVAL;
spinlock_irqsave_lock(&table_lock);
task = &task_table[id];
core_id = task->last_core;
if (task->status == TASK_BLOCKED) {
LOG_DEBUG("wakeup task %d on core %d\n", id, core_id);
task->status = TASK_READY;
spinlock_irqsave_unlock(&table_lock);
ret = 0;
spinlock_irqsave_lock(&readyqueues[core_id].lock);
// if task is in timer queue, remove it
if (task->flags & TASK_TIMER) {
task->flags &= ~TASK_TIMER;
timer_queue_remove(core_id, task);
}
// add task to the ready queue
readyqueues_push_back(core_id, task);
// increase the number of ready tasks
readyqueues[core_id].nr_tasks++;
// should we wakeup the core?
if (readyqueues[core_id].nr_tasks == 1)
wakeup_core(core_id);
LOG_DEBUG("update nr_tasks on core %d to %d\n", core_id, readyqueues[core_id].nr_tasks);
spinlock_irqsave_unlock(&readyqueues[core_id].lock);
} else {
spinlock_irqsave_unlock(&table_lock);
}
return ret;
}
int block_task(tid_t id)
{
task_t* task;
uint32_t core_id;
int ret = -EINVAL;
uint8_t flags;
flags = irq_nested_disable();
task = &task_table[id];
core_id = task->last_core;
if (task->status == TASK_RUNNING) {
LOG_DEBUG("block task %d on core %d\n", id, core_id);
task->status = TASK_BLOCKED;
spinlock_irqsave_lock(&readyqueues[core_id].lock);
// remove task from ready queue
readyqueues_remove(core_id, task);
// reduce the number of ready tasks
readyqueues[core_id].nr_tasks--;
LOG_DEBUG("update nr_tasks on core %d to %d\n", core_id, readyqueues[core_id].nr_tasks);
spinlock_irqsave_unlock(&readyqueues[core_id].lock);
ret = 0;
}
irq_nested_enable(flags);
return ret;
}
int block_current_task(void)
{
return block_task(per_core(current_task)->id);
}
int set_timer(uint64_t deadline)
{
task_t* curr_task;
uint32_t core_id;
uint8_t flags;
int ret = -EINVAL;
flags = irq_nested_disable();
curr_task = per_core(current_task);
core_id = CORE_ID;
if (curr_task->status == TASK_RUNNING) {
// blocks task and removes from ready queue
block_task(curr_task->id);
curr_task->flags |= TASK_TIMER;
curr_task->timeout = deadline;
timer_queue_push(core_id, curr_task);
ret = 0;
} else {
LOG_INFO("Task is already blocked. No timer will be set!\n");
}
irq_nested_enable(flags);
return ret;
}
void check_timers(void)
{
readyqueues_t* readyqueue = &readyqueues[CORE_ID];
spinlock_irqsave_lock(&readyqueue->lock);
// since IRQs are disabled, get_clock_tick() won't increase here
const uint64_t current_tick = get_clock_tick();
// wakeup tasks whose deadline has expired
task_t* task;
while ((task = readyqueue->timers.first) && (task->timeout <= current_tick))
{
// pops task from timer queue, so next iteration has new first element
wakeup_task(task->id);
}
#ifdef DYNAMIC_TICKS
task = readyqueue->timers.first;
if (task) {
update_timer(task);
}
#endif
spinlock_irqsave_unlock(&readyqueue->lock);
}
size_t** scheduler(void)
{
task_t* orig_task;
task_t* curr_task;
const uint32_t core_id = CORE_ID;
uint64_t prio;
orig_task = curr_task = per_core(current_task);
curr_task->last_core = core_id;
spinlock_irqsave_lock(&readyqueues[core_id].lock);
/* signalizes that this task could be realized */
if (curr_task->status == TASK_FINISHED)
readyqueues[core_id].old_task = curr_task;
else readyqueues[core_id].old_task = NULL; // reset old task
// do we receive a shutdown IPI => only the idle task should get the core
if (BUILTIN_EXPECT(go_down, 0)) {
if (curr_task->status == TASK_IDLE)
goto get_task_out;
curr_task = readyqueues[core_id].idle;
set_per_core(current_task, curr_task);
}
// determine highest priority
prio = msb(readyqueues[core_id].prio_bitmap);
const int readyqueue_empty = prio > MAX_PRIO;
if (readyqueue_empty) {
if ((curr_task->status == TASK_RUNNING) || (curr_task->status == TASK_IDLE))
goto get_task_out;
curr_task = readyqueues[core_id].idle;
set_per_core(current_task, curr_task);
} else {
// Does the current task have an higher priority? => no task switch
if ((curr_task->prio > prio) && (curr_task->status == TASK_RUNNING))
goto get_task_out;
// mark current task for later cleanup by finish_task_switch()
if (curr_task->status == TASK_RUNNING) {
curr_task->status = TASK_READY;
readyqueues[core_id].old_task = curr_task;
}
// get new task from its ready queue
curr_task = task_list_pop_front(&readyqueues[core_id].queue[prio-1]);
if(BUILTIN_EXPECT(curr_task == NULL, 0)) {
kputs("Kernel panic: No task in readyqueue\n");
LOG_ERROR("Kernel panic: No task in readyqueue\n");
while(1);
}
if (BUILTIN_EXPECT(curr_task->status == TASK_INVALID, 0)) {
LOG_ERROR("Kernel panic: Got invalid task %d, orig task %d\n",
curr_task->id, orig_task->id);
while(1);
}
// if we removed the last task from queue, update priority bitmap
if(readyqueues[core_id].queue[prio-1].first == NULL) {
readyqueues[core_id].prio_bitmap &= ~(1 << prio);
}
// finally make it the new current task
curr_task->status = TASK_RUNNING;
#ifdef DYNAMIC_TICKS
curr_task->last_tsc = get_rdtsc();
#endif
set_per_core(current_task, curr_task);
}
get_task_out:
spinlock_irqsave_unlock(&readyqueues[core_id].lock);
if (curr_task != orig_task) {
LOG_DEBUG("schedule on core %d from %u to %u with prio %u\n", core_id, orig_task->id, curr_task->id, (uint32_t)curr_task->prio);
return (size_t**) &(orig_task->last_stack_pointer);
}
return NULL;
}
int get_task(tid_t id, task_t** task)
{
if (BUILTIN_EXPECT(task == NULL, 0)) {
return -ENOMEM;
}
if (BUILTIN_EXPECT(id >= MAX_TASKS, 0)) {
return -ENOENT;
}
if (BUILTIN_EXPECT(task_table[id].status == TASK_INVALID, 0)) {
return -EINVAL;
}
*task = &task_table[id];
return 0;
}