-
Notifications
You must be signed in to change notification settings - Fork 4
/
kernel.c
1259 lines (1102 loc) · 28 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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stm32f10x.h"
#include "RTOSConfig.h"
#include "kernel.h"
#include "syscall.h"
#ifdef DEBUG
#include "unit_test.h"
#endif
#include <stddef.h>
#include <ctype.h> //test ctype
void *malloc(size_t size)
{
static char m[1024] = {0};
return m;
}
void *memcpy(void *dest, const void *src, size_t n);
int strcmp(const char *a, const char *b) __attribute__ ((naked));
int strcmp(const char *a, const char *b)
{
asm(
"strcmp_lop: \n"
" ldrb r2, [r0],#1 \n"
" ldrb r3, [r1],#1 \n"
" cmp r2, #1 \n"
" it hi \n"
" cmphi r2, r3 \n"
" beq strcmp_lop \n"
" sub r0, r2, r3 \n"
" bx lr \n"
:::
);
}
int strncmp(const char *a, const char *b, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
if (a[i] != b[i])
return a[i] - b[i];
return 0;
}
size_t strlen(const char *s) __attribute__ ((naked));
size_t strlen(const char *s)
{
asm(
" sub r3, r0, #1 \n"
"strlen_loop: \n"
" ldrb r2, [r3, #1]! \n"
" cmp r2, #0 \n"
" bne strlen_loop \n"
" sub r0, r3, r0 \n"
" bx lr \n"
:::
);
}
void puts(char *s)
{
while (*s) {
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
/* wait */ ;
USART_SendData(USART2, *s);
s++;
}
}
#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
/*Global Variables*/
char next_line[3] = {'\n','\r','\0'};
size_t task_count = 0;
char cmd[HISTORY_COUNT][CMDBUF_SIZE];
int cur_his=0;
int fdout;
int fdin;
/* Command handlers. */
void export_envvar(int argc, char *argv[]);
void show_echo(int argc, char *argv[]);
void show_cmd_info(int argc, char *argv[]);
void show_task_info(int argc, char *argv[]);
void show_man_page(int argc, char *argv[]);
void show_history(int argc, char *argv[]);
/* Structure for command handler. */
typedef struct {
char cmd[MAX_CMDNAME + 1];
void (*func)(int, char**);
char description[MAX_CMDHELP + 1];
} hcmd_entry;
const hcmd_entry cmd_data[CMD_COUNT] = {
[CMD_ECHO] = {.cmd = "echo", .func = show_echo, .description = "Show words you input."},
[CMD_EXPORT] = {.cmd = "export", .func = export_envvar, .description = "Export environment variables."},
[CMD_HELP] = {.cmd = "help", .func = show_cmd_info, .description = "List all commands you can use."},
[CMD_HISTORY] = {.cmd = "history", .func = show_history, .description = "Show latest commands entered."},
[CMD_MAN] = {.cmd = "man", .func = show_man_page, .description = "Manual pager."},
[CMD_PS] = {.cmd = "ps", .func = show_task_info, .description = "List all the processes."}
};
evar_entry env_var[MAX_ENVCOUNT];
int env_count = 0;
/* 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;
};
struct task_control_block tasks[TASK_LIMIT];
/*
* 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;
int 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;
int 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);
}
void serial_readwrite_task()
{
int fdout;
int 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 the response message to be queued. */
memcpy(str, "Got:", 4);
while (1) {
curr_char = 4;
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] = '\n';
str[curr_char+1] = '\0';
done = -1;
}
/* Otherwise, add the character to the
* response string. */
else
str[curr_char++] = ch;
} while (!done);
/* Once we are done building the response string, queue the
* response to be sent to the RS232 port.
*/
write(fdout, str, curr_char+1 + 1);
}
}
void serial_test_task()
{
char put_ch[2]={'0','\0'};
char hint[] = USER_NAME "@" USER_NAME "-STM32:~$ ";
int hint_length = sizeof(hint);
char *p = NULL;
int cmd_count = 0;
fdout = mq_open("/tmp/mqueue/out", 0);
fdin = open("/dev/tty0/in", 0);
for (;; cur_his = (cur_his + 1) % HISTORY_COUNT) {
p = cmd[cur_his];
write(fdout, hint, hint_length);
while (1) {
read(fdin, put_ch, 1);
if (put_ch[0] == '\r' || put_ch[0] == '\n') {
*p = '\0';
write(fdout, next_line, 3);
break;
}
else if (put_ch[0] == 127 || put_ch[0] == '\b') {
if (p > cmd[cur_his]) {
p--;
write(fdout, "\b \b", 4);
}
}
else if (p - cmd[cur_his] < CMDBUF_SIZE - 1) {
*p++ = put_ch[0];
write(fdout, put_ch, 2);
}
}
check_keyword();
}
}
/* Split command into tokens. */
char *cmdtok(char *cmd)
{
static char *cur = NULL;
static char *end = NULL;
if (cmd) {
char quo = '\0';
cur = cmd;
for (end = cmd; *end; end++) {
if (*end == '\'' || *end == '\"') {
if (quo == *end)
quo = '\0';
else if (quo == '\0')
quo = *end;
*end = '\0';
}
else if (isspace(*end) && !quo)
*end = '\0';
}
}
else
for (; *cur; cur++)
;
for (; *cur == '\0'; cur++)
if (cur == end) return NULL;
return cur;
}
void check_keyword()
{
char *argv[MAX_ARGC + 1] = {NULL};
char cmdstr[CMDBUF_SIZE];
char buffer[CMDBUF_SIZE * MAX_ENVVALUE / 2 + 1];
char *p = buffer;
int argc = 1;
int i;
find_events();
strcpy(cmdstr, cmd[cur_his]);
argv[0] = cmdtok(cmdstr);
if (!argv[0])
return;
while (1) {
argv[argc] = cmdtok(NULL);
if (!argv[argc])
break;
argc++;
if (argc >= MAX_ARGC)
break;
}
for(i = 0; i < argc; i++) {
int l = fill_arg(p, argv[i]);
argv[i] = p;
p += l + 1;
}
for (i = 0; i < CMD_COUNT; i++) {
if (!strcmp(argv[0], cmd_data[i].cmd)) {
cmd_data[i].func(argc, argv);
break;
}
}
if (i == CMD_COUNT) {
write(fdout, argv[0], strlen(argv[0]) + 1);
write(fdout, ": command not found", 20);
write(fdout, next_line, 3);
}
}
void find_events()
{
char buf[CMDBUF_SIZE];
char *p = cmd[cur_his];
char *q;
int i;
for (; *p; p++) {
if (*p == '!') {
q = p;
while (*q && !isspace(*q))
q++;
for (i = cur_his + HISTORY_COUNT - 1; i > cur_his; i--) {
if (!strncmp(cmd[i % HISTORY_COUNT], p + 1, q - p - 1)) {
strcpy(buf, q);
strcpy(p, cmd[i % HISTORY_COUNT]);
p += strlen(p);
strcpy(p--, buf);
break;
}
}
}
}
}
char *find_envvar(const char *name)
{
int i;
for (i = 0; i < env_count; i++) {
if (!strcmp(env_var[i].name, name))
return env_var[i].value;
}
return NULL;
}
/* Fill in entire value of argument. */
int fill_arg(char *const dest, const char *argv)
{
char env_name[MAX_ENVNAME + 1];
char *buf = dest;
char *p = NULL;
for (; *argv; argv++) {
if (isalnum(*argv) || *argv == '_') {
if (p)
*p++ = *argv;
else
*buf++ = *argv;
}
else { /* Symbols. */
if (p) {
*p = '\0';
p = find_envvar(env_name);
if (p) {
strcpy(buf, p);
buf += strlen(p);
p = NULL;
}
}
if (*argv == '$')
p = env_name;
else
*buf++ = *argv;
}
}
if (p) {
*p = '\0';
p = find_envvar(env_name);
if (p) {
strcpy(buf, p);
buf += strlen(p);
}
}
*buf = '\0';
return buf - dest;
}
//export
void export_envvar(int argc, char *argv[])
{
char *found;
char *value;
int i;
for (i = 1; i < argc; i++) {
value = argv[i];
while (*value && *value != '=')
value++;
if (*value)
*value++ = '\0';
found = find_envvar(argv[i]);
if (found)
strcpy(found, value);
else if (env_count < MAX_ENVCOUNT) {
strcpy(env_var[env_count].name, argv[i]);
strcpy(env_var[env_count].value, value);
env_count++;
}
}
}
//ps
void show_task_info(int argc, char* argv[])
{
char ps_message[]="PID STATUS PRIORITY";
int ps_message_length = sizeof(ps_message);
int task_i;
int task;
write(fdout, &ps_message , ps_message_length);
write(fdout, &next_line , 3);
for (task_i = 0; task_i < task_count; task_i++) {
char task_info_pid[2];
char task_info_status[2];
char task_info_priority[3];
task_info_pid[0]='0'+tasks[task_i].pid;
task_info_pid[1]='\0';
task_info_status[0]='0'+tasks[task_i].status;
task_info_status[1]='\0';
itoa(tasks[task_i].priority,task_info_priority);
write(fdout, &task_info_pid , 2);
write_blank(3);
write(fdout, &task_info_status , 2);
write_blank(5);
write(fdout, &task_info_priority , 3);
write(fdout, &next_line , 3);
}
}
//this function helps to show int
void itoa(int n, char *buffer)
{
if (n == 0)
*(buffer++) = '0';
else {
int f = 10000;
if (n < 0) {
*(buffer++) = '-';
n = -n;
}
while (f != 0) {
int i = n / f;
if (i != 0) {
*(buffer++) = '0'+(i%10);;
}
f/=10;
}
}
*buffer = '\0';
}
//help
void show_cmd_info(int argc, char* argv[])
{
const char help_desp[] = "This system has commands as follow\n\r\0";
int i;
write(fdout, &help_desp, sizeof(help_desp));
for (i = 0; i < CMD_COUNT; i++) {
write(fdout, cmd_data[i].cmd, strlen(cmd_data[i].cmd) + 1);
write(fdout, ": ", 3);
write(fdout, cmd_data[i].description, strlen(cmd_data[i].description) + 1);
write(fdout, next_line, 3);
}
}
//echo
void show_echo(int argc, char* argv[])
{
const int _n = 1; /* Flag for "-n" option. */
int flag = 0;
int i;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-n"))
flag |= _n;
else
break;
}
for (; i < argc; i++) {
write(fdout, argv[i], strlen(argv[i]) + 1);
if (i < argc - 1)
write(fdout, " ", 2);
}
if (~flag & _n)
write(fdout, next_line, 3);
}
//man
void show_man_page(int argc, char *argv[])
{
int i;
if (argc < 2)
return;
for (i = 0; i < CMD_COUNT && strcmp(cmd_data[i].cmd, argv[1]); i++)
;
if (i >= CMD_COUNT)
return;
write(fdout, "NAME: ", 7);
write(fdout, cmd_data[i].cmd, strlen(cmd_data[i].cmd) + 1);
write(fdout, next_line, 3);
write(fdout, "DESCRIPTION: ", 14);
write(fdout, cmd_data[i].description, strlen(cmd_data[i].description) + 1);
write(fdout, next_line, 3);
}
void show_history(int argc, char *argv[])
{
int i;
for (i = cur_his + 1; i <= cur_his + HISTORY_COUNT; i++) {
if (cmd[i % HISTORY_COUNT][0]) {
write(fdout, cmd[i % HISTORY_COUNT], strlen(cmd[i % HISTORY_COUNT]) + 1);
write(fdout, next_line, 3);
}
}
}
int write_blank(int blank_num)
{
char blank[] = " ";
int blank_count = 0;
while (blank_count <= blank_num) {
write(fdout, blank, sizeof(blank));
blank_count++;
}
}
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_test_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)