forked from embedded2013/rtenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.c
911 lines (806 loc) · 21.3 KB
/
kernel.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
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
#include "stm32f10x.h"
#include "RTOSConfig.h"
#include "syscall.h"
#include <stddef.h>
#include "utils.h"
#define NEWLINE "\n\r"
#define STACK_SIZE 512 /* Size of task stacks in words */
#define TASK_LIMIT 8 /* Max number of tasks we can handle */
#define PIPE_BUF 64 /* Size of largest atomic pipe message */
#define PATH_MAX 32 /* Longest absolute path */
#define PIPE_LIMIT (TASK_LIMIT * 2)
#define PATHSERVER_FD (TASK_LIMIT + 3)
/* File descriptor of pipe to pathserver */
#define PRIORITY_DEFAULT 20
#define PRIORITY_LIMIT (PRIORITY_DEFAULT * 2 - 1)
#define TASK_READY 0
#define TASK_WAIT_READ 1
#define TASK_WAIT_WRITE 2
#define TASK_WAIT_INTR 3
#define TASK_WAIT_TIME 4
#define S_IFIFO 1
#define S_IMSGQ 2
#define O_CREAT 4
/* Stack struct of user thread, see "Exception entry and return" */
struct user_thread_stack {
unsigned int r4;
unsigned int r5;
unsigned int r6;
unsigned int r7;
unsigned int r8;
unsigned int r9;
unsigned int r10;
unsigned int fp;
unsigned int _lr; /* Back to system calls or return exception */
unsigned int _r7; /* Backup from isr */
unsigned int r0;
unsigned int r1;
unsigned int r2;
unsigned int r3;
unsigned int ip;
unsigned int lr; /* Back to user thread code */
unsigned int pc;
unsigned int xpsr;
unsigned int stack[STACK_SIZE - 18];
};
/* Task Control Block */
struct task_control_block {
struct user_thread_stack *stack;
int pid;
int status;
int priority;
struct task_control_block **prev;
struct task_control_block *next;
};
// Global task variables
struct task_control_block tasks[TASK_LIMIT];
size_t task_count = 0;
/*
* pathserver assumes that all files are FIFOs that were registered
* with mkfifo. It also assumes a global tables of FDs shared by all
* processes. It would have to get much smarter to be generally useful.
*
* The first TASK_LIMIT FDs are reserved for use by their respective tasks.
* 0-2 are reserved FDs and are skipped.
* The server registers itself at /sys/pathserver
*/
#define PATH_SERVER_NAME "/sys/pathserver"
void pathserver()
{
char paths[PIPE_LIMIT - TASK_LIMIT - 3][PATH_MAX];
int npaths = 0;
int i = 0;
unsigned int plen = 0;
unsigned int replyfd = 0;
char path[PATH_MAX];
memcpy(paths[npaths++], PATH_SERVER_NAME, sizeof(PATH_SERVER_NAME));
while (1) {
read(PATHSERVER_FD, &replyfd, 4);
read(PATHSERVER_FD, &plen, 4);
read(PATHSERVER_FD, path, plen);
if (!replyfd) { /* mkfifo */
int dev;
read(PATHSERVER_FD, &dev, 4);
memcpy(paths[npaths], path, plen);
mknod(npaths + 3 + TASK_LIMIT, 0, dev);
npaths++;
}
else { /* open */
/* Search for path */
for (i = 0; i < npaths; i++) {
if (*paths[i] && strcmp(path, paths[i]) == 0) {
i += 3; /* 0-2 are reserved */
i += TASK_LIMIT; /* FDs reserved for tasks */
write(replyfd, &i, 4);
i = 0;
break;
}
}
if (i >= npaths) {
i = -1; /* Error: not found */
write(replyfd, &i, 4);
}
}
}
}
int mkfile(const char *pathname, int mode, int dev)
{
size_t plen = strlen(pathname)+1;
char buf[4+4+PATH_MAX+4];
(void) mode;
*((unsigned int *)buf) = 0;
*((unsigned int *)(buf + 4)) = plen;
memcpy(buf + 4 + 4, pathname, plen);
*((int *)(buf + 4 + 4 + plen)) = dev;
write(PATHSERVER_FD, buf, 4 + 4 + plen + 4);
return 0;
}
int mkfifo(const char *pathname, int mode)
{
mkfile(pathname, mode, S_IFIFO);
return 0;
}
int open(const char *pathname, int flags)
{
unsigned int replyfd = getpid() + 3;
size_t plen = strlen(pathname) + 1;
unsigned int fd = -1;
char buf[4 + 4 + PATH_MAX];
(void) flags;
*((unsigned int *)buf) = replyfd;
*((unsigned int *)(buf + 4)) = plen;
memcpy(buf + 4 + 4, pathname, plen);
write(PATHSERVER_FD, buf, 4 + 4 + plen);
read(replyfd, &fd, 4);
return fd;
}
int mq_open(const char *name, int oflag)
{
if (oflag & O_CREAT)
mkfile(name, 0, S_IMSGQ);
return open(name, 0);
}
void serialout(USART_TypeDef* uart, unsigned int intr)
{
int fd;
char c;
int doread = 1;
mkfifo("/dev/tty0/out", 0);
fd = open("/dev/tty0/out", 0);
while (1) {
if (doread)
read(fd, &c, 1);
doread = 0;
if (USART_GetFlagStatus(uart, USART_FLAG_TXE) == SET) {
USART_SendData(uart, c);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
doread = 1;
}
interrupt_wait(intr);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
}
void serialin(USART_TypeDef* uart, unsigned int intr)
{
int fd;
char c;
mkfifo("/dev/tty0/in", 0);
fd = open("/dev/tty0/in", 0);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
while (1) {
interrupt_wait(intr);
if (USART_GetFlagStatus(uart, USART_FLAG_RXNE) == SET) {
c = USART_ReceiveData(uart);
write(fd, &c, 1);
}
}
}
void greeting()
{
int fdout = open("/dev/tty0/out", 0);
char *string = "Hello, World!\n";
while (*string) {
write(fdout, string, 1);
string++;
}
}
void echo()
{
int fdout, fdin;
char c;
fdout = open("/dev/tty0/out", 0);
fdin = open("/dev/tty0/in", 0);
while (1) {
read(fdin, &c, 1);
write(fdout, &c, 1);
}
}
void rs232_xmit_msg_task()
{
int fdout, fdin;
char str[100];
int curr_char;
fdout = open("/dev/tty0/out", 0);
fdin = mq_open("/tmp/mqueue/out", O_CREAT);
setpriority(0, PRIORITY_DEFAULT - 2);
while (1) {
/* Read from the queue. Keep trying until a message is
* received. This will block for a period of time (specified
* by portMAX_DELAY). */
read(fdin, str, 100);
/* Write each character of the message to the RS232 port. */
curr_char = 0;
while (str[curr_char] != '\0') {
write(fdout, &str[curr_char], 1);
curr_char++;
}
}
}
void queue_str_task(const char *str, int delay)
{
int fdout = mq_open("/tmp/mqueue/out", 0);
int msg_len = strlen(str) + 1;
while (1) {
/* Post the message. Keep on trying until it is successful. */
write(fdout, str, msg_len);
/* Wait. */
sleep(delay);
}
}
void queue_str_task1()
{
queue_str_task("Hello 1\n", 200);
}
void queue_str_task2()
{
queue_str_task("Hello 2\n", 50);
}
char *get_task_status(int status)
{
switch(status) {
case TASK_READY:
return "Ready";
case TASK_WAIT_READ:
return "Wait Read";
case TASK_WAIT_WRITE:
return "Wait Write";
case TASK_WAIT_INTR:
return "Wait Intr";
case TASK_WAIT_TIME:
return "Wait Time";
default:
return "Unknown status";
}
}
void proc_cmd(char *cmd)
{
char tmp[16];
int i;
if(!strcmp(cmd, "ps")) {
puts("Task list : " NEWLINE "\0");
for(i = 0; i < task_count; i++) {
puts("PID -> ");
int2str(tasks[i].pid, tmp);
puts(tmp);
puts(", Status -> ");
puts(get_task_status(tasks[i].status));
puts(", Priority -> ");
int2str(tasks[i].priority, tmp);
puts(tmp);
puts(NEWLINE "\0");
}
}
else if(!strncmp(cmd, "echo", 4) && (strlen(cmd) == 4) || cmd[4] == ' ') {
if(strlen(cmd) == 4) {
puts(" \0");
}
else {
for(i = 5; i < strlen(cmd); i++) {
tmp[0] = cmd[i];
tmp[1] = '\0';
puts(tmp);
}
}
}
else if(!strcmp(cmd, "hello")) {
puts("Hello World!" NEWLINE "\0");
}
else if(!strcmp(cmd, "help")) {
puts("List all command :" NEWLINE "\0");
puts("ps - List all tasks" NEWLINE "\0");
puts("echo - Output message" NEWLINE "\0");
puts("hello - Show welcome message" NEWLINE "\0");
puts("help - Show this help table" NEWLINE "\0");
}
else {
puts("Command not found" NEWLINE "\0");
}
}
void serial_readwrite_task()
{
int fdout, fdin;
char str[100];
char ch;
int curr_char;
int done;
fdout = mq_open("/tmp/mqueue/out", 0);
fdin = open("/dev/tty0/in", 0);
// Prepare prompt hint string
char *prompt_hint = "rampant@rtenv:~/$ \0";
while (1) {
// Prompt hint
puts(prompt_hint);
curr_char = 0;
done = 0;
do {
/* Receive a byte from the RS232 port (this call will
* block). */
read(fdin, &ch, 1);
/* If the byte is an end-of-line type character, then
* finish the string and inidcate we are done.
*/
if (curr_char >= 98 || (ch == '\r') || (ch == '\n')) {
str[curr_char++] = '\0';
done = -1;
/* Otherwise, add the character to the
* response string. */
}
else if(ch == 27) {
read(fdin, &ch, 1);
read(fdin, &ch, 1);
char tmpesc[4];
tmpesc[0] = 27;
tmpesc[3] = '\0';
switch(ch) {
case 65: // Up key
case 66: // Down key
break;
case 67: // Right key
tmpesc[1] = '[';
tmpesc[2] = 'C';
puts(tmpesc);
curr_char++;
break;
case 68: // Left key
if(curr_char > 0) {
tmpesc[1] = '[';
tmpesc[2] = 'D';
puts(tmpesc);
curr_char--;
}
break;
}
}
else if(ch == 127) {
if(curr_char > 0) {
puts("\b \b\0");
curr_char--;
}
}
else {
str[curr_char++] = ch;
char tmpstr[2];
tmpstr[0] = ch;
tmpstr[1] = '\0';
puts(tmpstr);
}
// Output thr current input character
} while (!done);
/* Once we are done building the response string, queue the
* response to be sent to the RS232 port.
*/
puts(NEWLINE "\0");
proc_cmd(str);
puts(NEWLINE "\0");
}
}
void first()
{
setpriority(0, 0);
if (!fork()) setpriority(0, 0), pathserver();
if (!fork()) setpriority(0, 0), serialout(USART2, USART2_IRQn);
if (!fork()) setpriority(0, 0), serialin(USART2, USART2_IRQn);
if (!fork()) rs232_xmit_msg_task();
if (!fork()) setpriority(0, PRIORITY_DEFAULT - 10), serial_readwrite_task();
setpriority(0, PRIORITY_LIMIT);
while(1);
}
struct pipe_ringbuffer {
int start;
int end;
char data[PIPE_BUF];
int (*readable) (struct pipe_ringbuffer*, struct task_control_block*);
int (*writable) (struct pipe_ringbuffer*, struct task_control_block*);
int (*read) (struct pipe_ringbuffer*, struct task_control_block*);
int (*write) (struct pipe_ringbuffer*, struct task_control_block*);
};
#define RB_PUSH(rb, size, v) do { \
(rb).data[(rb).end] = (v); \
(rb).end++; \
if ((rb).end >= size) (rb).end = 0; \
} while (0)
#define RB_POP(rb, size, v) do { \
(v) = (rb).data[(rb).start]; \
(rb).start++; \
if ((rb).start >= size) (rb).start = 0; \
} while (0)
#define RB_PEEK(rb, size, v, i) do { \
int _counter = (i); \
int _src_index = (rb).start; \
int _dst_index = 0; \
while (_counter--) { \
((char*)&(v))[_dst_index++] = (rb).data[_src_index++]; \
if (_src_index >= size) _src_index = 0; \
} \
} while (0)
#define RB_LEN(rb, size) (((rb).end - (rb).start) + \
(((rb).end < (rb).start) ? size : 0))
#define PIPE_PUSH(pipe, v) RB_PUSH((pipe), PIPE_BUF, (v))
#define PIPE_POP(pipe, v) RB_POP((pipe), PIPE_BUF, (v))
#define PIPE_PEEK(pipe, v, i) RB_PEEK((pipe), PIPE_BUF, (v), (i))
#define PIPE_LEN(pipe) (RB_LEN((pipe), PIPE_BUF))
unsigned int *init_task(unsigned int *stack, void (*start)())
{
stack += STACK_SIZE - 9; /* End of stack, minus what we're about to push */
stack[8] = (unsigned int)start;
return stack;
}
int
task_push (struct task_control_block **list, struct task_control_block *item)
{
if (list && item) {
/* Remove itself from original list */
if (item->prev)
*(item->prev) = item->next;
if (item->next)
item->next->prev = item->prev;
/* Insert into new list */
while (*list) list = &((*list)->next);
*list = item;
item->prev = list;
item->next = NULL;
return 0;
}
return -1;
}
struct task_control_block*
task_pop (struct task_control_block **list)
{
if (list) {
struct task_control_block *item = *list;
if (item) {
*list = item->next;
if (item->next)
item->next->prev = list;
item->prev = NULL;
item->next = NULL;
return item;
}
}
return NULL;
}
void _read(struct task_control_block *task, struct task_control_block *tasks, size_t task_count, struct pipe_ringbuffer *pipes);
void _write(struct task_control_block *task, struct task_control_block *tasks, size_t task_count, struct pipe_ringbuffer *pipes);
void _read(struct task_control_block *task, struct task_control_block *tasks, size_t task_count, struct pipe_ringbuffer *pipes)
{
task->status = TASK_READY;
/* If the fd is invalid */
if (task->stack->r0 > PIPE_LIMIT) {
task->stack->r0 = -1;
}
else {
struct pipe_ringbuffer *pipe = &pipes[task->stack->r0];
if (pipe->readable(pipe, task)) {
size_t i;
pipe->read(pipe, task);
/* Unblock any waiting writes */
for (i = 0; i < task_count; i++)
if (tasks[i].status == TASK_WAIT_WRITE)
_write(&tasks[i], tasks, task_count, pipes);
}
}
}
void _write(struct task_control_block *task, struct task_control_block *tasks, size_t task_count, struct pipe_ringbuffer *pipes)
{
task->status = TASK_READY;
/* If the fd is invalid */
if (task->stack->r0 > PIPE_LIMIT) {
task->stack->r0 = -1;
}
else {
struct pipe_ringbuffer *pipe = &pipes[task->stack->r0];
if (pipe->writable(pipe, task)) {
size_t i;
pipe->write(pipe, task);
/* Unblock any waiting reads */
for (i = 0; i < task_count; i++)
if (tasks[i].status == TASK_WAIT_READ)
_read(&tasks[i], tasks, task_count, pipes);
}
}
}
int
fifo_readable (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
/* Trying to read too much */
if (task->stack->r2 > PIPE_BUF) {
task->stack->r0 = -1;
return 0;
}
if ((size_t)PIPE_LEN(*pipe) < task->stack->r2) {
/* Trying to read more than there is: block */
task->status = TASK_WAIT_READ;
return 0;
}
return 1;
}
int
mq_readable (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t msg_len;
/* Trying to read too much */
if ((size_t)PIPE_LEN(*pipe) < sizeof(size_t)) {
/* Nothing to read */
task->status = TASK_WAIT_READ;
return 0;
}
PIPE_PEEK(*pipe, msg_len, 4);
if (msg_len > task->stack->r2) {
/* Trying to read more than buffer size */
task->stack->r0 = -1;
return 0;
}
return 1;
}
int
fifo_read (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t i;
char *buf = (char*)task->stack->r1;
/* Copy data into buf */
for (i = 0; i < task->stack->r2; i++) {
PIPE_POP(*pipe, buf[i]);
}
return task->stack->r2;
}
int
mq_read (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t msg_len;
size_t i;
char *buf = (char*)task->stack->r1;
/* Get length */
for (i = 0; i < 4; i++) {
PIPE_POP(*pipe, *(((char*)&msg_len)+i));
}
/* Copy data into buf */
for (i = 0; i < msg_len; i++) {
PIPE_POP(*pipe, buf[i]);
}
return msg_len;
}
int
fifo_writable (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
/* If the write would be non-atomic */
if (task->stack->r2 > PIPE_BUF) {
task->stack->r0 = -1;
return 0;
}
/* Preserve 1 byte to distiguish empty or full */
if ((size_t)PIPE_BUF - PIPE_LEN(*pipe) - 1 < task->stack->r2) {
/* Trying to write more than we have space for: block */
task->status = TASK_WAIT_WRITE;
return 0;
}
return 1;
}
int
mq_writable (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t total_len = sizeof(size_t) + task->stack->r2;
/* If the write would be non-atomic */
if (total_len > PIPE_BUF) {
task->stack->r0 = -1;
return 0;
}
/* Preserve 1 byte to distiguish empty or full */
if ((size_t)PIPE_BUF - PIPE_LEN(*pipe) - 1 < total_len) {
/* Trying to write more than we have space for: block */
task->status = TASK_WAIT_WRITE;
return 0;
}
return 1;
}
int
fifo_write (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t i;
const char *buf = (const char*)task->stack->r1;
/* Copy data into pipe */
for (i = 0; i < task->stack->r2; i++)
PIPE_PUSH(*pipe,buf[i]);
return task->stack->r2;
}
int
mq_write (struct pipe_ringbuffer *pipe,
struct task_control_block *task)
{
size_t i;
const char *buf = (const char*)task->stack->r1;
/* Copy count into pipe */
for (i = 0; i < sizeof(size_t); i++)
PIPE_PUSH(*pipe,*(((char*)&task->stack->r2)+i));
/* Copy data into pipe */
for (i = 0; i < task->stack->r2; i++)
PIPE_PUSH(*pipe,buf[i]);
return task->stack->r2;
}
int
_mknod(struct pipe_ringbuffer *pipe, int dev)
{
switch(dev) {
case S_IFIFO:
pipe->readable = fifo_readable;
pipe->writable = fifo_writable;
pipe->read = fifo_read;
pipe->write = fifo_write;
break;
case S_IMSGQ:
pipe->readable = mq_readable;
pipe->writable = mq_writable;
pipe->read = mq_read;
pipe->write = mq_write;
break;
default:
return 1;
}
return 0;
}
int main()
{
unsigned int stacks[TASK_LIMIT][STACK_SIZE];
//struct task_control_block tasks[TASK_LIMIT];
struct pipe_ringbuffer pipes[PIPE_LIMIT];
struct task_control_block *ready_list[PRIORITY_LIMIT + 1]; /* [0 ... 39] */
struct task_control_block *wait_list = NULL;
//size_t task_count = 0;
size_t current_task = 0;
size_t i;
struct task_control_block *task;
int timeup;
unsigned int tick_count = 0;
SysTick_Config(configCPU_CLOCK_HZ / configTICK_RATE_HZ);
init_rs232();
__enable_irq();
tasks[task_count].stack = (void*)init_task(stacks[task_count], &first);
tasks[task_count].pid = 0;
tasks[task_count].priority = PRIORITY_DEFAULT;
task_count++;
/* Initialize all pipes */
for (i = 0; i < PIPE_LIMIT; i++)
pipes[i].start = pipes[i].end = 0;
/* Initialize fifos */
for (i = 0; i <= PATHSERVER_FD; i++)
_mknod(&pipes[i], S_IFIFO);
/* Initialize ready lists */
for (i = 0; i <= PRIORITY_LIMIT; i++)
ready_list[i] = NULL;
while (1) {
tasks[current_task].stack = activate(tasks[current_task].stack);
tasks[current_task].status = TASK_READY;
timeup = 0;
switch (tasks[current_task].stack->r7) {
case 0x1: /* fork */
if (task_count == TASK_LIMIT) {
/* Cannot create a new task, return error */
tasks[current_task].stack->r0 = -1;
}
else {
/* Compute how much of the stack is used */
size_t used = stacks[current_task] + STACK_SIZE
- (unsigned int*)tasks[current_task].stack;
/* New stack is END - used */
tasks[task_count].stack = (void*)(stacks[task_count] + STACK_SIZE - used);
/* Copy only the used part of the stack */
memcpy(tasks[task_count].stack, tasks[current_task].stack,
used * sizeof(unsigned int));
/* Set PID */
tasks[task_count].pid = task_count;
/* Set priority, inherited from forked task */
tasks[task_count].priority = tasks[current_task].priority;
/* Set return values in each process */
tasks[current_task].stack->r0 = task_count;
tasks[task_count].stack->r0 = 0;
tasks[task_count].prev = NULL;
tasks[task_count].next = NULL;
task_push(&ready_list[tasks[task_count].priority], &tasks[task_count]);
/* There is now one more task */
task_count++;
}
break;
case 0x2: /* getpid */
tasks[current_task].stack->r0 = current_task;
break;
case 0x3: /* write */
_write(&tasks[current_task], tasks, task_count, pipes);
break;
case 0x4: /* read */
_read(&tasks[current_task], tasks, task_count, pipes);
break;
case 0x5: /* interrupt_wait */
/* Enable interrupt */
NVIC_EnableIRQ(tasks[current_task].stack->r0);
/* Block task waiting for interrupt to happen */
tasks[current_task].status = TASK_WAIT_INTR;
break;
case 0x6: /* getpriority */
{
int who = tasks[current_task].stack->r0;
if (who > 0 && who < (int)task_count)
tasks[current_task].stack->r0 = tasks[who].priority;
else if (who == 0)
tasks[current_task].stack->r0 = tasks[current_task].priority;
else
tasks[current_task].stack->r0 = -1;
} break;
case 0x7: /* setpriority */
{
int who = tasks[current_task].stack->r0;
int value = tasks[current_task].stack->r1;
value = (value < 0) ? 0 : ((value > PRIORITY_LIMIT) ? PRIORITY_LIMIT : value);
if (who > 0 && who < (int)task_count)
tasks[who].priority = value;
else if (who == 0)
tasks[current_task].priority = value;
else {
tasks[current_task].stack->r0 = -1;
break;
}
tasks[current_task].stack->r0 = 0;
} break;
case 0x8: /* mknod */
if (tasks[current_task].stack->r0 < PIPE_LIMIT)
tasks[current_task].stack->r0 =
_mknod(&pipes[tasks[current_task].stack->r0],
tasks[current_task].stack->r2);
else
tasks[current_task].stack->r0 = -1;
break;
case 0x9: /* sleep */
if (tasks[current_task].stack->r0 != 0) {
tasks[current_task].stack->r0 += tick_count;
tasks[current_task].status = TASK_WAIT_TIME;
}
break;
default: /* Catch all interrupts */
if ((int)tasks[current_task].stack->r7 < 0) {
unsigned int intr = -tasks[current_task].stack->r7 - 16;
if (intr == SysTick_IRQn) {
/* Never disable timer. We need it for pre-emption */
timeup = 1;
tick_count++;
}
else {
/* Disable interrupt, interrupt_wait re-enables */
NVIC_DisableIRQ(intr);
}
/* Unblock any waiting tasks */
for (i = 0; i < task_count; i++)
if ((tasks[i].status == TASK_WAIT_INTR && tasks[i].stack->r0 == intr) ||
(tasks[i].status == TASK_WAIT_TIME && tasks[i].stack->r0 == tick_count))
tasks[i].status = TASK_READY;
}
}
/* Put waken tasks in ready list */
for (task = wait_list; task != NULL;) {
struct task_control_block *next = task->next;
if (task->status == TASK_READY)
task_push(&ready_list[task->priority], task);
task = next;
}
/* Select next TASK_READY task */
for (i = 0; i < (size_t)tasks[current_task].priority && ready_list[i] == NULL; i++);
if (tasks[current_task].status == TASK_READY) {
if (!timeup && i == (size_t)tasks[current_task].priority)
/* Current task has highest priority and remains execution time */
continue;
else
task_push(&ready_list[tasks[current_task].priority], &tasks[current_task]);
}
else {
task_push(&wait_list, &tasks[current_task]);
}
while (ready_list[i] == NULL)
i++;
current_task = task_pop(&ready_list[i])->pid;
}
return 0;
}