-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.c
857 lines (712 loc) · 26.2 KB
/
scheduler.c
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
#include "scheduler.h"
#include <stddef.h>
#include <libopencm3/stm32/timer.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/scb.h>
sched scheduler = {0};
/**************************** List functions **********************************/
static void list_unlink(sched_list *list, sched_task *task) {
if(list->first == task && list->last == task) {
list->first = NULL;
list->last = NULL;
sched_expect(task->list.prev == NULL);
sched_expect(task->list.next == NULL);
} else {
sched_task *prev = task->list.prev;
sched_task *next = task->list.next;
if(prev != NULL) prev->list.next = next;
if(next != NULL) next->list.prev = prev;
if(list->first == task) list->first = task->list.next;
if(list->last == task) list->last = task->list.prev;
task->list.prev = task->list.next = NULL;
}
}
static void list_append(sched_list *list, sched_task *task) {
sched_expect(task->list.prev == NULL);
sched_expect(task->list.next == NULL);
if(list->first == NULL /* && list->last == NULL */) {
list->first = list->last = task;
} else {
sched_task *prev = list->last;
prev->list.next = task;
task->list.prev = prev;
list->last = task;
}
}
static void list_insertbefore(sched_list *list, sched_task *task, sched_task *next) {
if(next == NULL) {
list_append(list, task);
} else {
sched_task *prev = next->list.prev;
task->list.prev = prev;
if(prev) prev->list.next = task;
else list->first = task;
next->list.prev = task;
task->list.next = next;
}
}
static void list_insert(sched_list *list, sched_task *task,
bool (*is_lower)(sched_task *one, sched_task *two)) {
sched_task *before = NULL, *cur = list->last;
while(cur != NULL) {
if(!is_lower(task, cur)) break;
before = cur;
cur = cur->list.prev;
}
list_insertbefore(list, task, before);
}
static sched_task *list_find_for_mutex(sched_list *list, sched_mutex *mutex) {
sched_task *task = list->first;
while(task != NULL) {
if(task->awaiting_mutex == mutex) {
break;
}
task = task->list.next;
}
return task;
}
/**************************** Queue functions *********************************/
static sched_task *queue_dequeue(sched_queue *queue) {
uint32_t primask = sched_irq_disable();
if(queue->first == NULL) {
sched_irq_restore(primask);
return NULL;
}
sched_task *task = queue->first;
if(queue->first == task && queue->last == task) {
queue->first = queue->last = NULL;
} else {
queue->first = task->queue.next;
}
task->queue.next = NULL;
sched_irq_restore(primask);
return task;
}
static void queue_enqueue(sched_queue *queue, sched_task *task) {
uint32_t primask = sched_irq_disable();
if(queue->first == NULL /* && queue->last == NULL */) {
queue->first = queue->last = task;
} else {
sched_task *prev = queue->last;
prev->queue.next = task;
queue->last = task;
}
sched_irq_restore(primask);
}
/**************************** List comparators ********************************/
static bool list_rt_islower(sched_task *one, sched_task *two) {
return one->priority > two->priority;
}
static bool list_rtw_islower(sched_task *one, sched_task *two) {
uint32_t ticks = sched_ticks();
int32_t rem_one = one->next_execution - ticks;
int32_t rem_two = two->next_execution - ticks;
return rem_one < rem_two;
}
/**************************** Utilities ***************************************/
uint32_t sched_ticks(void) {
uint16_t cnt_lo, cnt2_lo = TIM_CNT(SCHED_TIMlo);
uint16_t cnt_hi, cnt2_hi = TIM_CNT(SCHED_TIMhi);
do {
cnt_hi = cnt2_hi;
cnt_lo = cnt2_lo;
cnt2_hi = TIM_CNT(SCHED_TIMhi);
cnt2_lo = TIM_CNT(SCHED_TIMlo);
} while((cnt_lo >> 15) != (cnt2_lo >> 15) || cnt_hi != cnt2_hi);
return ((uint32_t) cnt_hi << 16) | cnt_lo;
}
static void rcc_periph_clock_enable_tim(uint32_t TIM) {
if(TIM == TIM1) {
rcc_periph_clock_enable(RCC_TIM1);
} else if(TIM == TIM2) {
rcc_periph_clock_enable(RCC_TIM2);
} else if(TIM == TIM3) {
rcc_periph_clock_enable(RCC_TIM3);
} else if(TIM == TIM4) {
rcc_periph_clock_enable(RCC_TIM4);
}
}
static void nvic_enable_irq_tim(uint32_t TIM) {
const uint8_t priority = 0xff;
if(TIM == TIM1) {
nvic_enable_irq(NVIC_TIM1_CC_IRQ);
nvic_set_priority(NVIC_TIM1_CC_IRQ, priority);
} else if(TIM == TIM2) {
nvic_enable_irq(NVIC_TIM2_IRQ);
nvic_set_priority(NVIC_TIM2_IRQ, priority);
} else if(TIM == TIM3) {
nvic_enable_irq(NVIC_TIM3_IRQ);
nvic_set_priority(NVIC_TIM3_IRQ, priority);
} else if(TIM == TIM4) {
nvic_enable_irq(NVIC_TIM4_IRQ);
nvic_set_priority(NVIC_TIM4_IRQ, priority);
}
}
/**************************** Main task ***************************************/
static uint8_t sleep_task_sp[128];
void sleep_task_entry(void *ign) {
#ifdef SCHED_DEBUG
while(1) {
// Keep syncing to save power
asm volatile("ISB");
}
#else
// Just keep entering sleep mode and wait for TIMer interrupt to wake us up
while(1) {
asm volatile("WFI");
}
#endif
}
/**************************** Scheduler functions *****************************/
void sched_init(void) {
/** Init scheduler **/
scheduler.realtime_tasks.first = scheduler.realtime_tasks.last = NULL;
scheduler.fired_tasks.first = scheduler.fired_tasks.last = NULL;
scheduler.cur_task = &scheduler.sleep_task;
scheduler.sleep_task.state = SCHEDSTATE_READY;
scheduler.is_running = false;
scheduler.sleep_task.task_list_next = NULL;
sched_task_init(&scheduler.sleep_task, "<SLEEP>", 0, sleep_task_sp, sizeof(sleep_task_sp), sleep_task_entry, NULL);
scheduler.task_list_first = &scheduler.sleep_task;
nvic_set_priority(NVIC_PENDSV_IRQ, 0xff);
/** Init timer **/
// Enable RCC clock for these timers
rcc_periph_clock_enable_tim(SCHED_TIMlo);
rcc_periph_clock_enable_tim(SCHED_TIMhi);
// Setup prescaler
timer_set_prescaler(SCHED_TIMlo, SCHED_CPU_MHZ-1);
// Setup TIM2 as Master-mode timer, TIM3 as slave-mode timer
TIM_CR2(SCHED_TIMlo) |= TIM_CR2_MMS_UPDATE;
switch(SCHED_ITR) {
default:
case 0:
TIM_SMCR(SCHED_TIMhi) |= TIM_SMCR_TS_ITR0;
break;
case 1:
TIM_SMCR(SCHED_TIMhi) |= TIM_SMCR_TS_ITR1;
break;
case 2:
TIM_SMCR(SCHED_TIMhi) |= TIM_SMCR_TS_ITR2;
break;
case 3:
TIM_SMCR(SCHED_TIMhi) |= TIM_SMCR_TS_ITR3;
break;
}
TIM_SMCR(SCHED_TIMhi) |= TIM_SMCR_SMS_ECM1;
// Enable timers
timer_enable_counter(SCHED_TIMhi);
timer_enable_counter(SCHED_TIMlo);
}
void uart_print(char *c);
void sched_start(void) {
// Enable interrupts from timers
nvic_enable_irq_tim(SCHED_TIMhi);
nvic_enable_irq_tim(SCHED_TIMlo);
sched_trigger_pendsv();
asm volatile("nop\n\tnop\n\tnop");
}
void sched_apply(void) {
sched_syscall(NULL, NULL);
}
/**************************** Static scheduler functions **********************/
static inline void sched_setup_hi(uint16_t goal) {
// Disable TIMlo.CC1 interrupt
TIM_DIER(SCHED_TIMlo) &= ~TIM_DIER_CC1IE;
// Set goal for hi timer
TIM_CCR1(SCHED_TIMhi) = goal;
// Clear CC1 interrupt flag
TIM_SR(SCHED_TIMhi) &= ~TIM_SR_CC1IF;
// Enable TIMhi.CC1 interrupt
TIM_DIER(SCHED_TIMhi) |= TIM_DIER_CC1IE;
}
static inline void sched_setup_lo(uint16_t goal) {
// Disable TIMhi.CC1 interrupt
TIM_DIER(SCHED_TIMhi) &= ~TIM_DIER_CC1IE;
// Set goal for lo timer
TIM_CCR1(SCHED_TIMlo) = goal;
// Clear CC1 interrupt flag
TIM_SR(SCHED_TIMlo) &= ~TIM_SR_CC1IF;
// Enable TIMhi.CC1 interrupt
TIM_DIER(SCHED_TIMlo) |= TIM_DIER_CC1IE;
}
static bool sched_setup(uint32_t goal_time) {
int32_t time, rem;
uint32_t primask = sched_irq_disable();
do {
time = sched_ticks();
rem = goal_time - time;
} while((rem > 0xffff && rem <= 0x10001)
|| (rem > 0 && rem <= 1));
if(rem <= 0) {
sched_irq_restore(primask);
return true;
}
if(rem > 0xffff) {
// setup high timer
sched_setup_hi(goal_time >> 16);
} else {
// setup low timer
sched_setup_lo(goal_time & 0xffff);
}
sched_irq_restore(primask);
return false;
}
static void sched_unsetup(void) {
// Disable TIMlo.CC1 interrupt
TIM_DIER(SCHED_TIMlo) &= ~TIM_DIER_CC1IE;
// Disable TIMhi.CC1 interrupt
TIM_DIER(SCHED_TIMhi) &= ~TIM_DIER_CC1IE;
}
static sched_task *sched_nexttask(void) {
do {
// Add fired tasks from queue into realtime_tasks or
// realtime_tasks_waiting list
sched_task *task;
do {
// Dequeue task
task = queue_dequeue(&scheduler.fired_tasks);
if(task == NULL) break;
if(task->list_type == SCHEDLISTTYPE_WAITING) {
list_insert(&scheduler.realtime_tasks_waiting, task, list_rtw_islower);
} else {
list_insert(&scheduler.realtime_tasks, task, list_rt_islower);
}
} while(true);
// Move no-longer-waiting tasks into task pending list `realtime_tasks`
uint32_t ticks = sched_ticks();
do {
// If there are no waiting tasks, break
task = scheduler.realtime_tasks_waiting.first;
if(task == NULL) break;
// If current task is still waiting, break
int32_t rem_ticks = task->next_execution - ticks;
if(rem_ticks > 0) break;
list_unlink(&scheduler.realtime_tasks_waiting, task);
list_insert(&scheduler.realtime_tasks, task, list_rt_islower);
} while(true);
if(scheduler.realtime_tasks_waiting.first == NULL) {
sched_unsetup();
break;
} else {
if(!sched_setup(scheduler.realtime_tasks_waiting.first->next_execution))
break;
}
} while(true);
if(scheduler.realtime_tasks.first != NULL) {
return scheduler.cur_task = scheduler.realtime_tasks.first;
}
return scheduler.cur_task = &scheduler.sleep_task;
}
static void sched_movetask(void) {
list_unlink(&scheduler.realtime_tasks, scheduler.cur_task);
}
/**************************** Task privileged functions ***********************/
// Can be run only at initialization phase or from syscall
static void sched_taskp_reinit(sched_task *task, sched_entry_function entry_function,
void *function_data, void *sp_end) {
task->sp = (char*) sp_end - sizeof(sched_stack);
sched_stack *stack = (sched_stack *) task->sp;
stack->psr = 0x21000000;
stack->pc = entry_function;
stack->lr = sched_task_delete;
stack->r0 = (uint32_t) function_data;
}
/**************************** Task built-in syscalls **************************/
static bool sched_task_delete_syscall(void *data, sched_task *cur_task) {
(void) data;
cur_task->state = SCHEDSTATE_DEAD;
// Remove the task from list O(n)
sched_task *prev = NULL;
sched_task *cur = scheduler.task_list_first;
while(cur != NULL) {
if(cur == cur_task) break;
prev = cur;
cur = cur->task_list_next;
}
sched_expect(cur != NULL);
prev->task_list_next = cur_task->task_list_next;
cur_task->task_list_next = NULL;
return false;
}
static bool sched_task_sleepuntil_syscall(void *data, sched_task *cur_task) {
uint32_t goal = (uint32_t) data;
cur_task->next_execution = goal;
list_insert(&scheduler.realtime_tasks_waiting, cur_task, list_rtw_islower);
return false;
}
static bool sched_task_add_syscall(void *data, sched_task *cur_task) {
sched_task *task = (sched_task *) data;
sched_task *next = scheduler.task_list_first;
task->task_list_next = next;
scheduler.task_list_first = task;
list_insert(&scheduler.realtime_tasks, task, list_rt_islower);
return false;
}
static bool sched_mutex_lock_syscall(void *data, sched_task *cur_task) {
sched_mutex *mutex = (sched_mutex *) data;
sched_task *locked_task = mutex->last_to_lock;
if(locked_task == NULL) {
mutex->last_to_lock = cur_task;
mutex->owner = cur_task;
// mutex_list_append(&cur_task->locked_mutexes, mutex);
return true;
}
mutex->last_to_lock = cur_task;
// mutex->owner is already set to the first mutex that called
// sched_mutex_lock function.
cur_task->awaiting_mutex = mutex;
list_append(&locked_task->dependant_tasks, cur_task);
return false;
}
static bool sched_mutex_unlock_syscall(void *data, sched_task *task) {
sched_mutex *mutex = (sched_mutex *) data;
task = mutex->owner;
if(task == NULL) {
// Not locked, return
return false;
}
sched_task *resumed_task = list_find_for_mutex(&task->dependant_tasks, mutex);
sched_expect(resumed_task != NULL);
mutex->owner = resumed_task;
list_unlink(&task->dependant_tasks, resumed_task);
list_insert(&scheduler.realtime_tasks, resumed_task, list_rt_islower);
list_insert(&scheduler.realtime_tasks, task, list_rt_islower);
return false;
}
typedef struct {
sched_cond *cond;
sched_mutex *mutex;
} sched_cond_mutex_pair;
static bool sched_cond_wait_syscall(void *data, sched_task *cur_task) {
sched_cond_mutex_pair *pair = (sched_cond_mutex_pair *) data;
sched_cond *cond = pair->cond;
sched_mutex *mutex = pair->mutex;
uint32_t primask = sched_irq_disable();
// Add current task to the conditional list
list_append(&cond->tasks, cur_task);
sched_irq_restore(primask);
sched_task * cur_value = mutex->last_to_lock;
sched_task * expected_value = cur_task;
if(cur_value == expected_value) {
// Just change the variable
mutex->last_to_lock = NULL;
mutex->owner = NULL;
} else {
// Unlock dependant task
sched_task *resumed_task = list_find_for_mutex(&cur_task->dependant_tasks, mutex);
sched_expect(resumed_task != NULL);
mutex->owner = resumed_task;
list_unlink(&cur_task->dependant_tasks, resumed_task);
list_insert(&scheduler.realtime_tasks, resumed_task, list_rt_islower);
}
return false;
}
static bool sched_cond_signal_syscall(void *data, sched_task *cur_task) {
sched_cond *cond = (sched_cond *) data;
uint32_t primask = sched_irq_disable();
sched_task *resumed_task = cond->tasks.first;
if(resumed_task == NULL) {
sched_irq_restore(primask);
// Done
} else {
list_unlink(&cond->tasks, resumed_task);
sched_irq_restore(primask);
list_insert(&scheduler.realtime_tasks, resumed_task, list_rt_islower);
}
return true;
}
static bool sched_cond_broadcast_syscall(void *data, sched_task *cur_task) {
sched_cond *cond = (sched_cond *) data;
uint32_t primask = sched_irq_disable();
sched_task *resumed_task;
while((resumed_task = cond->tasks.first) != NULL) {
list_unlink(&cond->tasks, resumed_task);
sched_irq_restore(primask);
list_insert(&scheduler.realtime_tasks, resumed_task, list_rt_islower);
primask = sched_irq_disable();
}
sched_irq_restore(primask);
return true;
}
static bool sched_monit_getruntime_syscall(void *data, sched_task *cur_task) {
sched_task *task = (sched_task*) data;
if(task == cur_task) {
uint32_t cur_time = sched_ticks();
task->running_time += cur_time - task->sched_time;
task->sched_time = cur_time;
}
sched_task_set_exit_code64(cur_task, task->running_time);
return true;
}
/**************************** Cond functions from ISR *************************/
void sched_cond_signal_fromisr(sched_cond *cond) {
uint32_t primask = sched_irq_disable();
sched_task *resumed_task = cond->tasks.first;
if(resumed_task == NULL) {
sched_irq_restore(primask);
// Done
} else {
list_unlink(&cond->tasks, resumed_task);
sched_irq_restore(primask);
sched_task_enqueue(resumed_task);
}
}
void sched_cond_broadcast_fromisr(sched_cond *cond) {
uint32_t primask = sched_irq_disable();
sched_task *resumed_task;
while((resumed_task = cond->tasks.first) != NULL) {
list_unlink(&cond->tasks, resumed_task);
sched_irq_restore(primask);
sched_task_enqueue(resumed_task);
primask = sched_irq_disable();
}
sched_irq_restore(primask);
}
/**************************** Task functions **********************************/
void sched_task_init(sched_task *task, const char *name, uint8_t priority,
uint8_t *sp, unsigned sp_length,
sched_entry_function function, void *data
) {
task->priority = priority;
task->state = SCHEDSTATE_DEAD;
task->name = name;
sched_taskp_reinit(task, function, data, sp + sp_length);
task->awaiting_mutex = NULL;
// task->locked_mutexes.first = task->locked_mutexes.last = NULL;
task->list.prev = task->list.next = task->queue.next = NULL;
}
void sched_task_add(sched_task *task) {
if(scheduler.is_running) {
sched_syscall(sched_task_add_syscall, task);
} else {
sched_task *next = scheduler.task_list_first;
task->task_list_next = next;
scheduler.task_list_first = task;
sched_task_enqueue(task);
}
}
void sched_task_delete(void) {
sched_syscall(sched_task_delete_syscall, NULL);
}
void sched_task_enqueue(sched_task *task) {
// Set state to READY
task->list_type = SCHEDLISTTYPE_RUNNING;
task->state = SCHEDSTATE_READY;
// Finally enqueue it into fired tasks queue to be processed from PendSV
// handler
queue_enqueue(&scheduler.fired_tasks, task);
}
void sched_task_fire(sched_task *task, int return_value) {
// Set return value
sched_stack *stack = (sched_stack *) task->sp;
stack->r0 = return_value;
sched_task_enqueue(task);
}
void sched_task_sleepuntil(uint32_t ticks) {
sched_syscall(sched_task_sleepuntil_syscall, (void *) ticks);
}
void sched_task_sleep(uint32_t ticks) {
sched_task_sleepuntil(sched_ticks() + ticks);
}
/**************************** Mutex functions *********************************/
void sched_mutex_lock(sched_mutex *mutex) {
sched_task * new_mutex_value = scheduler.cur_task;
uint32_t irq = sched_irq_disable();
sched_task * prev_value = mutex->last_to_lock;
if(prev_value == NULL) {
mutex->last_to_lock = new_mutex_value;
mutex->owner = new_mutex_value;
sched_irq_restore(irq);
} else {
sched_irq_restore(irq);
sched_syscall(sched_mutex_lock_syscall, mutex);
}
}
bool sched_mutex_trylock(sched_mutex *mutex) {
sched_task * new_mutex_value = scheduler.cur_task;
uint32_t irq = sched_irq_disable();
sched_task * prev_value = mutex->last_to_lock;
if(prev_value == NULL) {
mutex->last_to_lock = new_mutex_value;
mutex->owner = new_mutex_value;
sched_irq_restore(irq);
return true;
} else {
sched_irq_restore(irq);
return false;
}
}
void sched_mutex_unlock(sched_mutex *mutex) {
sched_task * expected_value = mutex->owner;
uint32_t irq = sched_irq_disable();
sched_task * cur_val = mutex->last_to_lock;
if(cur_val == expected_value) {
mutex->last_to_lock = NULL;
mutex->owner = NULL;
sched_irq_restore(irq);
} else if(cur_val != NULL) {
sched_irq_restore(irq);
sched_syscall(sched_mutex_unlock_syscall, mutex);
}
}
/**************************** Cond functions **********************************/
void sched_cond_wait(sched_cond *cond, sched_mutex *mutex) {
sched_cond_mutex_pair pair = {.cond = cond, .mutex = mutex};
// This syscall will block until either signal or broadcast is performed
sched_syscall(sched_cond_wait_syscall, &pair);
// It will then wake up and try to acquire this mutex
sched_mutex_lock(mutex);
}
void sched_cond_signal(sched_cond *cond) {
sched_syscall(sched_cond_signal_syscall, cond);
}
void sched_cond_broadcast(sched_cond *cond) {
sched_syscall(sched_cond_broadcast_syscall, cond);
}
/*************************** Monitoring functions *****************************/
uint64_t sched_monit_getruntime(sched_task *task) {
return sched_syscall64(sched_monit_getruntime_syscall, task);
}
/**************************** Timer handlers **********************************/
void SCHED_TIMlo_IRQHandler(void) {
sched_trigger_pendsv();
}
void SCHED_TIMhi_IRQHandler(void) {
if(scheduler.realtime_tasks_waiting.first == NULL) {
sched_unsetup();
} else {
if(!sched_setup(scheduler.realtime_tasks_waiting.first->next_execution)) {
sched_trigger_pendsv();
}
}
}
/**************************** PendSV handler **********************************/
// This can't get inlined, because it uses many registers which results in some
// variables being pushed onto the stack - something that can't be done in naked
// function which calls this function.
__attribute__((__noinline__))
static sched_task *sched_handle_syscall_svc(void) {
sched_stack *stack = (sched_stack *) scheduler.cur_task->sp;
sched_syscall_function syscall_func = (sched_syscall_function) stack->r0;
void * data = (void *) stack->r1;
if(syscall_func != NULL) {
sched_task *cur_task = scheduler.cur_task;
cur_task->state = SCHEDSTATE_BLOCKING;
sched_movetask();
if(syscall_func(data, scheduler.cur_task) == true) {
scheduler.cur_task->state = SCHEDSTATE_READY;
list_insertbefore(&scheduler.realtime_tasks, scheduler.cur_task, scheduler.realtime_tasks.first);
}
}
return sched_nexttask();
}
// This can't get inlined, because it uses many registers which results in some
// variables being pushed onto the stack - something that can't be done in naked
// function which calls this function.
__attribute__((__noinline__))
static sched_task *sched_handle_syscall_pendsv(void) {
return sched_nexttask();
}
void __attribute__((__naked__)) SVC_Handler(void) {
/* 0. NVIC has already pushed some registers on the program/main stack.
* We are free to modify R0..R3 and R12 without saving them again, and
* additionally the compiler may choose to use R4..R11 in this function.
* If it does so, the naked attribute will prevent it from saving those
* registers on the stack, so we'll just have to hope that it doesn't do
* anything with them before our stm or after our ldm instructions.
* Luckily, we don't ever intend to return to the original caller on the
* main stack, so this question is moot. */
/* Read the link register */
uint32_t lr;
__asm__("MOV %0, lr" : "=r" (lr));
if (lr & 0x4) {
/* This PendSV call was made from a task using the PSP */
/* 1. Push all other registers (R4..R11) on the program stack */
void *psp;
__asm__(
/* Load PSP to a temporary register */
"MRS %0, psp\n"
/* Push context relative to the address in the temporary
* register, update register with resulting address */
"STMDB %0!, {r4-r11}\n"
/* Put back the new stack pointer in PSP (pointless) */
"MSR psp, %0\n"
: "=r" (psp));
/* 2. Store that PSP in the current TCB */
scheduler.cur_task->sp = psp;
} else {
/* This PendSV call was made from a task using the MSP. Don't do
anything, stack does not have to be saved since we will never context
switch to a task using MSP, only PSP. */
scheduler.is_running = true;
}
sched_task *prev = scheduler.cur_task;
/* 3. Call context switch function, changes current TCB */
sched_task *task = sched_handle_syscall_svc();
uint32_t cur_time = sched_ticks();
if(prev != NULL)
prev->running_time += cur_time - prev->sched_time;
task->sched_time = cur_time;
/* 4. Load PSP from TCB */
void *psp = task->sp;
/* 5. Pop R4..R11 from the program stack */
__asm__(
"LDMIA %0!, {r4-r11}\n"
"MSR psp, %0\n"
:: "r" (psp));
// Finally, return. NVIC will pop registers from stack we set up and jump
// where PC points to.
__asm__("bx %0" :: "r"(0xfffffffd));
}
void __attribute__((__naked__)) PendSV_Handler(void) {
/* 0. NVIC has already pushed some registers on the program/main stack.
* We are free to modify R0..R3 and R12 without saving them again, and
* additionally the compiler may choose to use R4..R11 in this function.
* If it does so, the naked attribute will prevent it from saving those
* registers on the stack, so we'll just have to hope that it doesn't do
* anything with them before our stm or after our ldm instructions.
* Luckily, we don't ever intend to return to the original caller on the
* main stack, so this question is moot. */
/* Read the link register */
uint32_t lr;
__asm__("MOV %0, lr" : "=r" (lr));
if (lr & 0x4) {
/* This PendSV call was made from a task using the PSP */
/* 1. Push all other registers (R4..R11) on the program stack */
void *psp;
__asm__(
/* Load PSP to a temporary register */
"MRS %0, psp\n"
/* Push context relative to the address in the temporary
* register, update register with resulting address */
"STMDB %0!, {r4-r11}\n"
/* Put back the new stack pointer in PSP (pointless) */
"MSR psp, %0\n"
: "=r" (psp));
/* 2. Store that PSP in the current TCB */
scheduler.cur_task->sp = psp;
} else {
/* This PendSV call was made from a task using the MSP. Don't do
anything, stack does not have to be saved since we will never context
switch to a task using MSP, only PSP. */
scheduler.is_running = true;
}
sched_task *prev = scheduler.cur_task;
/* 3. Call context switch function, changes current TCB */
sched_task *task = sched_handle_syscall_pendsv();
uint32_t cur_time = sched_ticks();
if(prev != NULL)
prev->running_time += cur_time - prev->sched_time;
task->sched_time = cur_time;
/* 4. Load PSP from TCB */
void *psp = task->sp;
/* 5. Pop R4..R11 from the program stack */
__asm__(
"LDMIA %0!, {r4-r11}\n"
"MSR psp, %0\n"
:: "r" (psp));
// Finally, return. NVIC will pop registers from stack we set up and jump
// where PC points to.
__asm__("bx %0" :: "r"(0xfffffffd));
}