4b997b4c1ff6170f07ce1c2b4a35ad9200213bc6
[akaros.git] / tools / compilers / gcc-glibc / glibc-2.11.1-ros / sysdeps / ros / start.c
1 #include <abort-instr.h>
2 #include <string.h>
3 #include <ros/arch/hart.h>
4 #include <stdio.h>
5 #include <ros/syscall.h>
6 #include <ros/procinfo.h>
7
8 void** __hart_stack_pointers = NULL;
9 weak_alias(__hart_stack_pointers,hart_stack_pointers)
10 void** __hart_thread_control_blocks = NULL;
11 weak_alias(__hart_thread_control_blocks,hart_thread_control_blocks)
12
13 void
14 __hart_entry(void)
15 {
16         fputs("define a hart_entry() function, foo!",stderr);
17         abort();
18 }
19 weak_alias(__hart_entry,hart_entry)
20
21 void
22 __hart_yield(void)
23 {
24 }
25 weak_alias(__hart_yield,hart_yield)
26
27 void
28 _start(void)
29 {
30         // threads besides thread 0 must acquire a stack and TCB
31         if(__hart_self() != 0)
32         {
33                 __hart_set_stack_pointer(__hart_stack_pointers[__hart_self()]);
34                 TLS_INIT_TP(__hart_thread_control_blocks[__hart_self()],0);
35                 hart_entry();
36                 hart_yield();
37         }
38
39         static int init = 0;
40         if(init)
41         {
42                 fputs("why is thread 0 re-entering _start, foo?!",stderr);
43                 abort();
44         }
45         init = 1;
46
47         extern int main(int,char**,char**);
48         extern void __libc_csu_init(int,char**,char**);
49         extern void __libc_csu_fini(void);
50         extern void __libc_start_main(typeof(&main),int,char**,
51                       typeof(&__libc_csu_init),
52                       typeof(&__libc_csu_fini),
53                       void*,void*);
54
55         char** argv = (char**)alloca(sizeof(__procinfo.argp));
56         memcpy(argv,__procinfo.argp,sizeof(__procinfo.argp));
57
58         char* argbuf = (char*)alloca(sizeof(__procinfo.argbuf));
59         memcpy(argbuf,__procinfo.argbuf,sizeof(__procinfo.argbuf));
60
61         for(int i = 0; i < PROCINFO_MAX_ARGP; i++)
62                 if(argv[i])
63                         argv[i] += argbuf - __procinfo.argbuf;
64
65         int argc = 0;
66         while(argv[argc])
67                 argc++;
68
69         __libc_start_main(&main,argc,argv,&__libc_csu_init,&__libc_csu_fini,0,0);
70
71         #ifdef ABORT_INSTRUCTION
72         ABORT_INSTRUCTION;
73         #endif
74         while(1);
75 }
76