-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
914 lines (801 loc) · 22.1 KB
/
main.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
#include <stddef.h>
#include <sys/types.h>
#include <signal.h>
#include "uart.h"
#include "printf.h"
#include "string.h"
#include "memory.h"
#define UNUSED(x) (void)(x)
// error
int errno;
#define EINVAL 22
#define ERANGE 34
// limit
#define LONG_MAX 0x7FFFFFFFL
#define LONG_MIN (-LONG_MAX-1)
// ocaml entry function
extern int caml_startup(char **argv);
// constructors
typedef void (*constructor_t)(void);
extern constructor_t _init_array_start[];
extern constructor_t _init_array_end[];
void kernel_constructors(void) {
for(constructor_t *fn = _init_array_start; fn != _init_array_end; ++fn) {
(*fn)();
}
}
// memory
//extern char *_end;
//void *end = (void*)(((char*)&_end) + 4);
extern void *_end[];
// entry.S
extern uint32_t exception_table[];
double test_double(void) {
volatile double x = 2.0;
volatile double y = 3.0;
double z = x * y;
asm volatile("vmov %d0, %r0, %r1");
return z;
}
// enable interrupts
static inline void enable_irq(void) {
uint32_t t;
asm volatile("mrs %[t],cpsr; bic %[t], %[t], #0x80; msr cpsr_c, %[t]"
: [t]"=r"(t));
}
// disable interrupts
static inline void disable_irq(void) {
uint32_t t;
asm volatile("mrs %[t],cpsr; orr %[t], %[t], #0x80; msr cpsr_c, %[t]"
: [t]"=r"(t));
}
long int strtol(const char *nptr, char **endptr, int base);
void test_strtol(void) {
errno = 0; printf("strtol(\"foo\", NULL, 0) = %ld, errno = %d\n", strtol("foo", NULL, 0), errno);
errno = 0; printf("strtol(\"0\", NULL, -1) = %ld, errno = %d\n", strtol("0", NULL, -1), errno);
errno = 0; printf("strtol(\"0\", NULL, 1) = %ld, errno = %d\n", strtol("0", NULL, 1), errno);
errno = 0; printf("strtol(\"0\", NULL, 37) = %ld, errno = %d\n", strtol("0", NULL, 37), errno);
errno = 0; printf("strtol(\"0\", NULL, 0) = %ld, errno = %d\n", strtol("0", NULL, 0), errno);
errno = 0; printf("strtol(\"0\", NULL, 8) = %ld, errno = %d\n", strtol("0", NULL, 8), errno);
errno = 0; printf("strtol(\"0\", NULL, 16) = %ld, errno = %d\n", strtol("0", NULL, 16), errno);
errno = 0; printf("strtol(\"123\", NULL, 10) = %ld, errno = %d\n", strtol("123", NULL, 10), errno);
errno = 0; printf("strtol(\"20\", NULL, 16) = %ld, errno = %d\n", strtol("20", NULL, 16), errno);
errno = 0; printf("strtol(\"78\", NULL, 8) = %ld, errno = %d\n", strtol("78", NULL, 8), errno);
errno = 0; printf("strtol(\"08\", NULL, 0) = %ld, errno = %d\n", strtol("08", NULL, 0), errno);
errno = 0; printf("strtol(\"0x20\", NULL, 0) = %ld, errno = %d\n", strtol("0x20", NULL, 0), errno);
errno = 0; printf("strtol(\"017\", NULL, 0) = %ld, errno = %d\n", strtol("017", NULL, 0), errno);
errno = 0; printf("strtol(\"0x\", NULL, 0) = %ld, errno = %d\n", strtol("0x", NULL, 0), errno);
errno = 0; printf("strtol(\"+0x20\", NULL, 0) = %ld, errno = %d\n", strtol("+0x20", NULL, 0), errno);
errno = 0; printf("strtol(\"-0x20\", NULL, 0) = %ld, errno = %d\n", strtol("-0x20", NULL, 0), errno);
errno = 0; printf("strtol(\"0x81111111\", NULL, 16) = %ld, errno = %d\n", strtol("0x81111111", NULL, 16), errno);
errno = 0; printf("strtol(\"-0x81111111\", NULL, 16) = %ld, errno = %d\n", strtol("-0x81111111", NULL, 16), errno);
}
void kernel_main(int zero, int model, void *atags) {
(void)zero;
(void)model;
(void)atags;
char arg0[] = "ocaml kernel";
char *argv[] = {arg0, NULL};
uart_init();
puts("\n# uart initialized\n");
// delay(100000000);
printf("_end = %p\n", _end);
printf("model = %#x [expected 0xc42]\n", model);
printf("atags @ %p\n", atags);
uint32_t mem_size = ((uint32_t*)atags)[7];
printf("memory size = %#x\n", mem_size);
memory_init(_end, mem_size - ((intptr_t)_end - 0xC0000000));
{
char c;
printf("# stack = %p\n", &c);
}
// set exception vector base address register
asm volatile("mcr p15, 0, %[addr], c12, c0, 0"
: : [addr]"r"(exception_table));
puts("# exception vector set\n");
// delay(100000000);
puts("# enabling IRQs\n");
enable_irq();
printf("%06d\n", 0);
test_double();
puts("# doubles tested\n");
// delay(100000000);
test_strtol();
//delay(100000000);
caml_startup(argv);
// delay(100000000);
panic("all done\n");
}
/***************************************************************************
* math functions *
***************************************************************************/
double acos(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n");
UNUSED(x);
return 0;
}
double asin(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n");
UNUSED(x);
return 0;
}
double atan(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double atan2(double y, double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
UNUSED(y);
return 0;
}
double ceil(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double cos(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double cosh(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double exp(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double expm1(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double floor(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double fmod(double x, double y) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
UNUSED(y);
return 0;
}
double frexp(double x, int *expo) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
UNUSED(expo);
return 0;
}
double hypot(double x, double y) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
UNUSED(y);
return 0;
}
double ldexp(double x, int expo) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
UNUSED(expo);
return 0;
}
double log(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double log10(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double log1p(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double modf(double x, double *iptr) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
UNUSED(iptr);
return 0;
}
double pow(double x, double y) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
UNUSED(y);
return 0;
}
double sin(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double sinh(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double sqrt(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double tan(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
double tanh(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
int __fpclassify(double x) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(x);
return 0;
}
/***************************************************************************
* C functions *
***************************************************************************/
// IO
typedef struct FILE {
int fd;
char pad[66536];
} FILE;
FILE stdin = { .fd = 0 };
FILE stdout = { .fd = 1 };
FILE stderr = { .fd = 2 };
int fflush(FILE *stream) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(stream);
return 0;
}
int fputc(int c, FILE *stream) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(stream);
return putchar(c);
}
int fputs(const char *s, FILE *stream) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(stream);
return puts(s);
}
ssize_t write(int fd, const void *buf, size_t count) {
puts("# "); puts(__FUNCTION__); puts("()\n"); // delay(100000000);
UNUSED(fd);
size_t n = count;
const char *p = (const char *)buf;
while(n-- > 0) putchar(*p++);
return count;
}
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(stream);
size *= nmemb;
size_t n = size;
const char *p = (const char *)ptr;
while(n-- > 0) putchar(*p++);
return size;
}
ssize_t read(int fd, void *buf, size_t count) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(fd);
UNUSED(buf);
UNUSED(count);
// FIXME: read from uart
panic("MISSING: read()");
return -1;
}
// exit
void __attribute__((noreturn)) exit(int status) {
UNUSED(status);
panic("exit()");
}
// filesystem
int chdir(const char *path) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(path);
return -1;
}
int close(int fd) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(fd);
return 0;
}
typedef struct DIR { } DIR;
int closedir(DIR *dirp) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(dirp);
return 0;
}
int fcntl(int fd, int cmd, ... /* arg */ ) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(fd);
UNUSED(cmd);
return -1;
}
char *getcwd(char *buf, size_t size) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(size);
buf[0] = '/';
buf[1] = 0;
return buf;
}
//typedef uint64_t off_t;
off_t lseek64(int fd, off_t offset, int whence) {
puts("# "); puts(__FUNCTION__); puts("()\n"); // delay(100000000);
UNUSED(fd);
UNUSED(offset);
UNUSED(whence);
return (off_t)-1;
}
// typedef int mode_t;
int open64(const char *pathname, int flags, mode_t mode) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(pathname);
UNUSED(flags);
UNUSED(mode);
return -1;
}
DIR *opendir(const char *name) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(name);
return NULL;
}
struct dirent;
struct dirent *readdir64(DIR *dirp) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(dirp);
return NULL;
}
ssize_t readlink(const char *path, char *buf, size_t bufsiz) {
puts("# "); puts(__FUNCTION__); puts("()\n"); // delay(100000000);
UNUSED(path);
UNUSED(buf);
UNUSED(bufsiz);
return -1;
}
int rename(const char *oldpath, const char *newpath) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(oldpath);
UNUSED(newpath);
return -1;
}
int unlink(const char *pathname) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(pathname);
return -1;
}
struct stat;
int __xstat64(const char *path, struct stat *buf) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(path);
UNUSED(buf);
return -1;
}
// processes
char *getenv(const char *name) {
puts("# "); puts(__FUNCTION__); puts("()\n"); // delay(100000000);
UNUSED(name);
return NULL;
}
typedef int pid_t;
pid_t getpid(void) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
return 1;
}
pid_t getppid(void) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
return 0;
}
struct rlimit;
int getrlimit(int resource, struct rlimit *rlim) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(resource);
UNUSED(rlim);
return -1;
}
struct rusage;
int getrusage(int who, struct rusage *usage) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(who);
UNUSED(usage);
return -1;
}
// time
struct timeval;
struct timezone;
int gettimeofday(struct timeval *tv, struct timezone *tz) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(tv);
UNUSED(tz);
return -1;
}
// locale
char *setlocale(int category, const char *locale) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(category);
UNUSED(locale);
return NULL;
}
const unsigned short **__ctype_b_loc (void) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
static const unsigned short *ctypes[384];
return &ctypes[128];
}
// signals
//struct sigaction;
int sigaction(int signum, const struct sigaction *act,
struct sigaction *oldact) {
puts("# "); puts(__FUNCTION__); puts("()\n"); // delay(100000000);
UNUSED(signum);
UNUSED(act);
UNUSED(oldact);
if (oldact) {
memset(oldact, 0, sizeof(struct sigaction));
oldact->sa_handler = SIG_DFL;
}
return 0;
}
// typedef struct { } sigset_t;
int sigaddset(sigset_t *set, int signum) {
puts("# "); puts(__FUNCTION__); puts("()\n"); // delay(100000000);
UNUSED(set);
UNUSED(signum);
return -1;
}
int sigaltstack (const struct sigaltstack *__restrict ss,
struct sigaltstack *__restrict oss) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(ss);
UNUSED(oss);
return -1;
}
int sigdelset(sigset_t *set, int signum) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(set);
UNUSED(signum);
return -1;
}
int sigemptyset(sigset_t *set) {
puts("# "); puts(__FUNCTION__); puts("()\n"); // delay(100000000);
UNUSED(set);
return -1;
}
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
puts("# "); puts(__FUNCTION__); puts("()\n"); // delay(100000000);
UNUSED(how);
UNUSED(set);
UNUSED(oldset);
return -1;
}
double strtod(const char *nptr, char **endptr) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(nptr);
UNUSED(endptr);
return 0.0;
}
int isspace(int c) {
switch(c) {
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
case ' ':
return 1;
break;
default:
return 0;
}
}
int isdigit(int c) {
if (c >= '0' && c <= '9') return 1;
return 0;
}
int digit(int c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'Z') return c - 'A' + 16;
if (c >= 'a' && c <= 'z') return c - 'a' + 16;
return 256;
}
long int strtol(const char *nptr, char **endptr, int base) {
long int res = 0;
int sign = 1;
const char *ptr = nptr;
const char *p = nptr;
// printf("# %s(%s, %p, %d)\n", __FUNCTION__, nptr, endptr, base);
// sanity check
if (!nptr || base == 1 || base < 0 || base > 36) {
errno = EINVAL;
goto out;
}
// skip leading whitespace
while(*p != '\0' && isspace(*p)) ++p;
if (*p == '\0') goto out;
// parse sign
if (*p == '+') {
++p;
}
if (*p == '-') {
++p;
sign = -1;
}
if (*p == '\0') goto out;
// detect base for hex
if (base == 0 || base == 16) {
if (p[0] == '0' && p[1] == 'x' && digit(p[2]) < 16) {
ptr = p + 2;
base = 16;
}
}
// Handle base detection for octal
if (base == 0 && p[0] == '0') {
ptr = p + 1;
base = 8;
}
// Fall back on decimal when unknown
if (base == 0) {
base = 10;
}
// parse base prefix
const char *q = p;
switch(base) {
case 0:
if (*q == '0') {
++q;
if (*q == 'x' || *q == 'X') {
++q;
if (digit(*q) < 16) {
base = 16;
ptr = q;
} else {
base = 10;
ptr = p;
}
} else {
base = 8;
ptr = q;
}
} else if (digit(*q) < 10) {
base = 10;
ptr = p;
} else {
base = 10;
}
break;
case 16:
if (*q == '0') {
++q;
if (*q == 'x' || *q == 'X') {
++q;
if (digit(*q) < 16) {
ptr = q;
} else {
ptr = p;
}
} else {
ptr = p;
}
} else if (digit(*q) <= 16) {
ptr = p;
}
break;
default:
if (digit(*q) < base) {
ptr = q;
}
}
// parse number
long int min = LONG_MIN / base;
int d;
while((d = digit(*ptr)) < base) {
if (res < min) {
res = LONG_MIN;
errno = ERANGE;
} else {
res *= base;
if (res < LONG_MIN + d) {
res = LONG_MIN;
errno = ERANGE;
} else {
res -= d;
}
}
++ptr;
}
out:
// printf("nptr = '%s', ptr = '%s', res = %ld\n", nptr, ptr, -res);
if (endptr) *endptr = (char*)ptr;
if (nptr == ptr) errno = EINVAL;
if (sign == 1) {
if (res == LONG_MIN) {
errno = ERANGE;
return LONG_MAX;
} else {
return -res;
}
} else {
return res;
}
}
int * __errno_location(void) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
return &errno;
}
char *strerror(int errnum) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(errnum);
return NULL;
}
// system
int system(const char *command) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(command);
return -1;
}
// stack protector
void * __stack_chk_guard = NULL;
void __stack_chk_guard_setup(void) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
int * p;
p = (int *) &__stack_chk_guard;
/* If you have the ability to generate random numbers in your kernel then use them,
otherwise for 32-bit code: */
*p = 0x00000aff;
}
void __attribute__((noreturn)) __stack_chk_fail(void) {
panic("__stack_chk_fail()");
}
// printf / scanf
int __sprintf_chk(char * str, int flag, size_t len, const char * format, ...) {
puts("# "); puts(__FUNCTION__); puts("()\n"); //delay(100000000);
UNUSED(flag);
va_list args;
va_start(args, format);
len = vsnprintf(str, len, format, args);
va_end(args);
return len;
}
#define BUF_SIZE 4096
int sprintf(char * str, const char * format, ...) {
// puts("# "); puts(__FUNCTION__); puts("()\n"); //delay(100000000);
va_list args;
size_t len;
va_start(args, format);
len = vsnprintf(str, BUF_SIZE, format, args);
va_end(args);
return len;
}
int __fprintf_chk(FILE * stream, int flag, const char * format, ...) {
puts("# "); puts(__FUNCTION__); puts("()\n"); //delay(100000000);
UNUSED(stream);
UNUSED(flag);
va_list args;
char buf[BUF_SIZE];
ssize_t len;
va_start(args, format);
len = vsnprintf(buf, BUF_SIZE, format, args);
va_end(args);
if (len != -1 && len <= BUF_SIZE) {
puts(buf);
} else {
// FIXME: too long
panic("printf: too long\n");
}
return len;
}
int fprintf(FILE * stream, const char * format, ...) {
puts("# "); puts(__FUNCTION__); puts("()\n"); //delay(100000000);
UNUSED(stream);
va_list args;
char buf[BUF_SIZE];
ssize_t len;
va_start(args, format);
len = vsnprintf(buf, BUF_SIZE, format, args);
va_end(args);
if (len != -1 && len <= BUF_SIZE) {
puts(buf);
} else {
// FIXME: too long
panic("printf: too long\n");
}
return len;
}
int __isoc99_sscanf(const char *format, ...) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(format);
return -1;
}
// sigsetjmp
typedef struct { } sigjmp_buf;
int __sigsetjmp(sigjmp_buf env, int savesigs) {
puts("# "); puts(__FUNCTION__); puts("()\n"); // delay(100000000);
UNUSED(env);
UNUSED(savesigs);
return 0;
}
// unwind
int raise (int sig) {
UNUSED(sig);
panic("raise()\n");
}
void __aeabi_unwind_cpp_pr0(void) {
panic("__aeabi_unwind_cpp_pr0()\n");
}
/***************************************************************************
* dl functions *
***************************************************************************/
void *dlopen(const char *filename, int flag) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(filename);
UNUSED(flag);
return 0;
}
int dlclose(void *handle) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(handle);
return -1;
}
const char *dlerror(void) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
return "ERROR: dlerror()\n";
}
void *dlsym(void *handle, const char *symbol) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
UNUSED(handle); UNUSED(symbol);
return 0;
}
/***************************************************************************
* exception handlers *
***************************************************************************/
void dump(uint32_t *regs) {
static const char *name[] = {"R0 ", "R1 ", "R2 ", "R3 ",
"R4 ", "R5 ", "R6 ", "R7 ",
"R8 ", "R9 ", "R10", "R11",
"R12", "SPu", "LRu", "IPu"};
printf("regs @ %#8.8x", (uint32_t)regs);
for(int i = 0; i < 16; ++i) {
if (i % 4 == 0) putchar('\n');
printf("%s = %#8.8x ", name[i], regs[i]);
}
putchar('\n');
}
void exception_reset_handler(uint32_t *regs) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
dump(regs);
}
void exception_undefined_handler(uint32_t *regs) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
dump(regs);
}
void exception_syscall_handler(uint32_t *regs) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
dump(regs);
}
void exception_prefetch_abort_handler(uint32_t *regs) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
dump(regs);
}
void exception_data_abort_handler(uint32_t *regs) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
dump(regs);
}
extern void time_irq_timer1(uint32_t *regs);
void exception_irq_handler(uint32_t *regs) {
UNUSED(regs);
puts("# "); puts(__FUNCTION__); puts("()\n");
// dump(regs);
time_irq_timer1(regs);
}
void exception_fiq_handler(uint32_t *regs) {
puts("# "); puts(__FUNCTION__); puts("()\n"); delay(100000000);
dump(regs);
}