-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path0x04_scheduling.c
More file actions
646 lines (547 loc) · 20.8 KB
/
0x04_scheduling.c
File metadata and controls
646 lines (547 loc) · 20.8 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
#include "npu.h"
/*
* scheduler_init - Address: 0xcb3c
*
* Scheduler state structure initialization function.
*/
void scheduler_init() {
/* Sets the priority group indices to 0 */
g_scheduler_state.prio_grp0 = 0;
for (int i = 0; i < 4; i++)
g_scheduler_state.prio_grp1[i] = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; i < 8; i++)
g_scheduler_state.prio_grp2[i][j] = 0;
/* Sets the currently scheduled task to NULL */
g_current_task = 0;
/*
* Sets the number of tasks
* (and other unknown values, but these are not too important afaict)
*/
g_scheduler_state.nb_tasks = 0;
g_scheduler_state.count_sched_slices = 1;
g_scheduler_state._unk_840 = 0;
/* Sets the scheduler state as stopped */
g_scheduler_state.scheduler_stopped = 1;
g_scheduler_state.forbid_scheduling = 1;
/* Initializes delayed, tasks and ready lists */
list_init(&g_scheduler_state.delayed_list);
list_init(&g_scheduler_state.tasks_list);
for (int prio = 0; prio < TASK_MAX_PRIORITY; prio++)
list_init(&g_scheduler_state.ready_list[prio]);
}
/*
* schedule_start - Address: 0xd118
*
* Starts the scheduler and schedule the first task, which is the task
* with the highest priority in the ready list.
*/
int schedule_start()
{
/* Disables interrupts */
__disable_irq();
/* Retrieves the highest priority to schedule next */
u32 prio_grp0_idx = g_scheduler_state.prio_grp0;
u32 prio_grp0_val = g_priority_table[prio_grp0_idx];
u32 prio_grp1_idx = g_scheduler_state.prio_grp1[prio_grp0_val];
u32 prio_grp1_val = g_priority_table[prio_grp1_idx];
u32 prio_grp2_idx = g_scheduler_state.prio_grp2[prio_grp0_val][prio_grp1_val];
u32 prio_grp2_val = g_priority_table[prio_grp2_idx];
u32 priority = \
(prio_grp0_val << 6) | \
(prio_grp1_val << 3) | \
prio_grp0_val;
/* Gets the task with the highest priority from the ready list */
struct list_head *ready_list = &g_scheduler_state.ready_list[priority];
g_current_task = container_of(
ready_list->next, struct task, ready_list_entry);
/* Sanity checks on the task */
if (g_current_task->magic == TASK_MAGIC) {
g_task_to_schedule = g_current_task;
/* Sets the current task state as running */
g_current_task->state |= TASK_RUNNING;
/* Sets the scheduler state as started */
g_scheduler_state.scheduler_stopped = 0;
g_scheduler_state.forbid_scheduling = 0;
/*
* Loads the stack pointer from the task structure and retrieves the
* values of the registers from it.
* The last two values loaded (into r1 and r0) are the CPSR to use and
* a pointer to the function `run_task`.
* `run_task` will call the task handler and start the scheduling
* process.
*/
asm volatile(
"mov sp, %[stk_ptr]\n"
"pop {r0-r12, lr}\n"
"pop {r0-r1}\n"
"msr cpsr, r1\n"
"mov pc, r0\n"
);
} else {
return -42;
}
}
/*
* schedule - Address: 0x2d0
*
* Function called when we want to perform an explicit schedule of a task.
*/
void __attribute__((naked)) schedule()
{
asm volatile(
"mrs r0, cpsr\n"
"push {r0}\n"
"push {lr}\n"
"push {r0-r3, r12, lr}\n"
);
/* Finds the next task to schedule */
schedule_task();
/*
* If the task to schedule is different from the one currently running
* then set the new task as the current task
*/
if (g_task_to_schedule != g_current_task) {
/* Saves the registers on the stack */
asm volatile("push {r4-r11}\n");
/* Saves the current stack pointer */
g_current_task->stack_ptr = get_stack_pointer();
/* Sets the new current task */
g_current_task = g_task_to_schedule;
/* Changes the stack pointer to the one of the new task */
set_stack_pointer(g_current_task->stack_ptr);
asm volatile(
"ldr r0, [sp, $0x3c]\n" // CPSR stored when `g_task_to_schedule`
// called `schedule`
"msr spsr, r0\n"
"ldr r0, [sp, $0x38]\n" // lr stored when `g_task_to_schedule`
// called `schedule`
"str r0, [sp, $0x3c]\n" // This value will be popped into `pc``
// to continue the execution of the task
// after its call to `schedule`
);
/*
* Restores the registers and branch to the `lr` value stored before
* the call to `schedule`
*/
asm volatile(
"pop {r4-r11}\n"
"pop {r0-r3, r12, lr}\n"
"add sp, sp, $4\n"
"pop {pc}^\n"
);
}
/*
* If tasks are the same, just pop back the register and return from the
* function
*/
asm volatile(
"pop {r0-r3, r12, lr}\n"
"add sp, sp, $8\n"
"bx lr\n"
);
}
/*
* schedule_task - Address: 0xcc34
*
* Looks for the next task to schedule from the ready list based on its
* priority.
*/
void schedule_task() {
/* Checks that the scheduler is allowed to run */
u32 forbid_scheduling = g_scheduler_state.forbid_scheduling;
if (forbid_scheduling)
return;
u32 state = g_current_task->state;
u32 priority = g_current_task->priority;
u32 do_schedule = 0;
/* Checks that the task is not in an invalid state */
if (!(state & (TASK_SUSPENDED | TASK_READY | TASK_SLEEPING | TASK_PENDING)))
abort();
/*
* If the current task is running, take it off the ready list and add
* it back to the end of it.
*/
if (state == (TASK_READY | TASK_RUNNING) && \
g_current_task->remaining_sched_slices <= 0) {
list_del(&g_current_task->ready_list_entry);
g_current_task->remaining_sched_slices = g_current_task->max_sched_slices;
struct list_head *ready_list = &g_scheduler_state.ready_list[priority];
/*
* If the ready list for the current priority is empty when we remove
* the task, do the scheduling even if the priority of the next one is
* lower.
*/
if (is_list_empty(ready_list))
do_schedule = 1;
list_add(&g_current_task->ready_list_entry, ready_list);
} else {
/* If no task is currently running, schedules a new one */
do_schedule = 1;
}
/* Finds the priority of the next task to schedule */
u32 prio_grp0_idx = g_scheduler_state.prio_grp0;
u32 prio_grp0_val = g_priority_table[prio_grp0_idx];
u32 prio_grp1_idx = g_scheduler_state.prio_grp1[prio_grp0_val];
u32 prio_grp1_val = g_priority_table[prio_grp1_idx];
u32 prio_grp2_idx = g_scheduler_state.prio_grp2[prio_grp0_val][prio_grp1_val];
u32 prio_grp2_val = g_priority_table[prio_grp2_idx];
u32 next_priority = \
(prio_grp0_val << 6) | \
(prio_grp1_val << 3) | \
prio_grp2_val;
/*
* Only schedule if the priority value is lower than the current one
* (priority values are the inverse of the actual priority, the lower the
* value, the higher the priority).
*/
if (next_priority < priority)
do_schedule = 1;
/* If we have to schedule a new task */
if (do_schedule) {
/*
* Finds the next task in the ready list associated to the priority
* we computed above.
*/
struct list_head *ready_list = &g_scheduler_state.ready_list[next_priority];
/* Aborts if the list is empty, because it should not happen. */
if (is_list_empty(ready_list))
abort();
/*
* Retrieves the beginning of the task structure base on its
* `ready_list_entry` pointer */
struct task *task = container_of(
ready_list->next, struct task, ready_list_entry);
/* Sets the task we got as the next one to schedule */
g_task_to_schedule = task;
/* Stops the current task */
g_current_task->state &= ~(TASK_RUNNING);
/* Marks the next task as running */
g_task_to_schedule->state |= TASK_RUNNING;
}
}
/*
* schedule_tick - Address: 0xc324
*
* Updates scheduling info for the current task after one scheduler tick.
* Also wakes up tasks in the delayed list if needed.
*/
void schedule_tick() {
if (!g_current_task)
abort();
/* Updating the scheduling slices */
g_current_task->total_sched_slices++;
if (g_scheduler_state.count_sched_slices == 1)
g_current_task->remaining_sched_slices--;
/* Returns if there are no sleeping tasks */
if (is_list_empty(&g_scheduler_state.delayed_list))
return;
struct task *delayed_task = container_of(&g_scheduler_state.delayed_list,
struct task, delayed_list_entry);
/* Sanity checks on the first sleeping task */
if (delayed_task->magic != TASK_MAGIC)
abort();
/* Decrements the delay of the first task */
delayed_task->delay--;
/* Iterates over tasks in the delayed list until their delay is above zero */
while (delayed_task->delay <= 0) {
/*
* Setting this variable will allow the scheduling of a new task when
* the execution goes back to the irq handler.
*/
g_do_schedule = 1;
/* Sanity checks on the delayed task */
if (!delayed_task \
|| delayed_task->magic != TASK_MAGIC \
|| !(delayed_task->state & TASK_SLEEPING))
abort();
/* Updates the delay of the next task (delay could be negative?) */
struct task* next_delayed_task = delayed_task->delayed_list_entry.next;
if (next_delayed_task != &g_scheduler_state.delayed_list)
next_delayed_task->delay += delayed_task->delay;
/* Removes the task from the delayed list */
list_del(&delayed_task->delayed_list_entry);
/* Adds the task we woke up to the ready list */
if (__add_to_ready_list(delayed_task))
abort();
/* Returns if there are no sleeping tasks */
if (is_list_empty(&g_scheduler_state.delayed_list))
return;
/* Sets the next task as the current one and continues the loop */
delayed_task = next_delayed_task;
if (delayed_task->magic != TASK_MAGIC)
abort();
}
}
/*
* __add_to_ready_list - Address: 0xc324
*
* Adds a task to a ready list when it's ready to run.
*/
int __add_to_ready_list(struct task *task) {
/* Sanity checks on the task */
if (!task || task->magic != TASK_MAGIC)
abort();
/* Retrieves the ready list associated to the task's priority */
u32 priority = task->priority;
struct list_head *ready_list = &g_scheduler_state.ready_list[priority];
/* If the ready list is empty, add the priority in the priority groups */
if (is_list_empty(ready_list)) {
/* Computes the priority group values based on the task's priority */
u8 grp0_val = priority >> 6;
u8 grp1_val = (priority >> 3) & 7;
u8 grp2_val = priority & 7;
/* Adds the current task's priority to the priority group values */
g_scheduler_state.prio_grp0 |= 1 << grp0_val;
g_scheduler_state.prio_grp1[grp0_val] |= 1 << grp1_val;
g_scheduler_state.prio_grp2[grp0_val][grp1_val] |= 1 << grp2_val;
}
/* Initializes the number of scheduling slices */
task->remaining_sched_slices = task->max_sched_slices;
/* Adds the task to the correponding ready list */
list_add(&task->ready_list_entry, ready_list);
/* Sets the task's state as ready */
task->state = TASK_READY;
return 0;
}
/*
* __del_from_ready_list - Address: 0xbf30
*
* Removes a task from its ready list.
*/
int __del_from_ready_list(struct task *task) {
if (!task || task->magic != TASK_MAGIC)
abort();
if (!(task->state & TASK_READY))
return -42;
u32 priority = task->priority;
struct list_head *ready_list = &g_scheduler_state.ready_list[priority];
/* If the ready list is empty, return an error */
if (is_list_empty(ready_list))
return -42;
/* It there is one item in the ready list */
if (ready_list->next->next == ready_list) {
/* Computes the priority group values based on the current priority */
u8 grp0_val = priority >> 6;
u8 grp1_val = (priority >> 3) & 7;
u8 grp2_val = priority & 7;
u8 *prio_grp0 = &g_scheduler_state.prio_grp0;
u8 *prio_grp1 = &g_scheduler_state.prio_grp1[grp0_val];
u8 *prio_grp2 = &g_scheduler_state.prio_grp2[grp0_val][grp1_val];
/* Removes the current priority from the priority group values */
u8 prio_tbl_val = *prio_grp2 & ~(1 << grp2_val);
*prio_grp2 = prio_tbl_val;
if (!prio_tbl_val) {
u8 grp1_val = *prio_grp1 & ~(1 << grp1_val);
*prio_grp1 = grp1_val;
if (!grp1_val)
*prio_grp0 = *prio_grp0 & ~(1 << grp0_val);
}
}
/* Removes the task from the list */
list_del(&task->ready_list_entry);
/* Updates the task state */
task->state |= TASK_NOT_READY;
return 0;
}
/*
* __add_to_pending_list - Address: 0xc760
*
* Adds `task` to the pending list `wq`.
*/
int __add_to_pending_list(struct task *task, struct workqueue *wq) {
if (!task || task->magic != TASK_MAGIC)
abort();
if(!(task->state & TASK_PENDING) || !(task->state & TASK_NOT_READY))
return -42;
u32 priority = TASK_MAGIC;
u32 service = wq->service;
struct list_head* wq_list = &wq->head;
/* The operation performed depends on the `service` type of the workqueue */
switch (service) {
case 0:
/* Simply adds the task to the workqueue list */
list_add(&task->pending_list_entry, wq_list);
break;
case 1:
if (!is_list_empty(wq_list)) {
/*
* If the workqueue/pending list is not empty, then we retrieve
* the priority of the task since it's used to sort the tasks in
* the pending list.
*/
priority = task->priority;
} else {
/*
* Otherwise we just add the task to the list, since it doesn't
* need any sorting.
*/
list_add(&task->pending_list_entry, wq_list);
break;
}
struct task *neighbor_task = 0;
struct list_head* curr_list_entry = 0;
/* Gets the first element in the workqueue */
struct task *curr_task = container_of(
&wq_list->next, struct task, pending_list_entry);
/* Gets the second element in the workqueue */
struct task *next_task = container_of(
&wq_list->next->next, struct task, pending_list_entry);
/*
* This loop tries to find a "neighbor" task, which is the first task
* found in the list, while iterating, that has a priority greater
* or equal than `priority`.
* The task we want to add to the pending list will be added right
* before the "neighbor" one.
*/
do {
/*
* The loop ends if the priority of the task we are currently
* iterating on is bigger than the one we want to add.
*/
if (priority < curr_task->priority)
break;
neighbor_task = curr_task;
curr_task = next_task;
next_task = container_of(
&next_task->pending_list_entry.next, struct task,
pending_list_entry);
curr_list_entry = &curr_task->pending_list_entry;
/* The loop ends if we're back at the beginning of the list */
} while (curr_list_entry != wq_list);
if (neighbor_task) {
/* The task is added before the neighbor task */
list_add(&task->pending_list_entry,
neighbor_task->pending_list_entry);
else
/* The task is added at the end of the pending list */
list_add(&task->pending_list_entry, wq_list);
break;
default:
return -22;
}
/* Sets the task state as pending */
task->state = TASK_PENDING;
/* References the workqueue the task was added to */
task->wait_queue = wq;
return 0;
}
/*
* __del_from_pending_list - Address: 0xbdb4
*
* Removes `task` from the pending list it's currently attached to.
*/
int __del_from_pending_list(struct task *task) {
if (!task || task->magic != TASK_MAGIC || !task->wait_queue)
abort();
/* Checks that the task is actually pending */
if (!(task->state & TASK_PENDING))
return -42;
/* Checks that the pending list is not empty */
if (is_list_empty(&task->wait_queue->head))
return -42;
/* Removes the task from the pending list */
list_del(&task->pending_list_entry);
task->wait_queue = 0;
task->state |= TASK_NOT_READY;
return 0;
}
/*
* __add_to_delayed_list - Address: 0xc4e8
*
* Adds a task to the delayed list for `delay` seconds.
*/
int __add_to_delayed_list(u32 delay) {
u32 ret = 0;
struct task *task = g_current_task;
/* Checks that the scheduler is allowed to run */
u32 forbid_scheduling = g_scheduler_state.forbid_scheduling;
if (forbid_scheduling)
return ret;
ret = __del_from_ready_list(g_current_task);
if (ret)
return ret;
if (!task || task->magic != TASK_MAGIC || delay <= 0)
abort();
if (!(task->state & (TASK_READY | TASK_NOT_READY)))
return -42;
struct task *neighbor_task = 0;
struct list_head* curr_list_entry = 0;
struct list_head* delayed_list = &g_scheduler_state.delayed_list;
/* Gets the first element in the delayed list */
struct task *curr_task = container_of(
delayed_list, struct task, delayed_list_entry);
/* Gets the second element in the delayed list */
struct task *next_task = container_of(
delayed_list, struct task, delayed_list_entry);
/*
* This loop tries to find a "neighbor" task, which is the first task
* found in the list, while iterating, that has a delay greater
* or equal than `delay`.
* The task we want to add to the delayed list will be added right
* before the "neighbor" one.
*/
do {
/*
* The loop ends if the delay of the task we are currently
* iterating on is bigger than the one we want to add.
*/
if (delay < curr_task->delay)
break;
/*
* Subtracts the delay of the task, since tasks are stored with a delay
* relative to each others.
*/
delay -= curr_task->delay;
neighbor_task = curr_task;
curr_task = next_task;
next_task = container_of(
&next_task->delayed_list_entry.next, struct task,
delayed_list_entry);
curr_list_entry = &curr_task->delayed_list_entry;
/* The loop ends if we're back at the beginning of the list */
} while (curr_list_entry != delayed_list);
if (neighbor_task) {
/* The task is added before the neighbor task */
list_add(&task->delayed_list_entry, neighbor_task->delayed_list_entry);
else
/* The task is added at the end of the delayed list */
list_add(&task->delayed_list_entry, delayed_list);
/*
* Updates the delay of the next task to keep all delays in the list
* relative to each other
*/
if (&curr_task->delayed_list_entry != &g_scheduler_state.delayed_list)
curr_task->delay -= delay;
/* Updates task info */
task->state = TASK_SLEEPING;
task->delay = delay;
/* Schedules another task */
schedule();
return 0;
}
/*
* __del_from_delayed_list - Address: 0xd590
*
* Removes a sleeping task from the delayed list.
*/
unsigned int __del_from_delayed_list(struct task *task) {
/* Sanity checks*/
if (!task || task->magic != TASK_MAGIC)
abort();
/* Checks that the task is effectively sleeping */
if (!(task->state & TASK_SLEEPING))
return -42;
/* If the task is not the last in the list, update the delay of the next one */
struct task *next_task = container_of(
task->delayed_list_entry.next, struct task, delayed_list_entry);
if (&next_task->delayed_list_entry != &g_scheduler_state.delayed_list)
next_task->delay = next_task->delay + task->delay;
/* Deletes the task from the delayed list */
list_del(task->delayed_list_entry);
/* Resets the delay to 0 */
task->delay = 0;
/* Updates the task's state */
task->state |= TASK_NOT_READY;
return 0;
}