ead669261d32f24d54d05eb1b132b213ca224f9f
[akaros.git] / tools / compilers / gcc-glibc / glibc-2.14.1-ros / sysdeps / ros / start.c
1 #include <abort-instr.h>
2 #include <string.h>
3 #include <vcore.h>
4 #include <stdio.h>
5 #include <ros/syscall.h>
6 #include <ros/procinfo.h>
7 #include <unistd.h>
8 #include <tls.h>
9
10 void** __vcore_thread_control_blocks = NULL;
11 weak_alias(__vcore_thread_control_blocks,vcore_thread_control_blocks)
12
13 __thread int __vcoreid = 0;
14 __thread bool __vcore_context = FALSE;
15
16 void __uthread_vcore_entry(void)
17 {
18         fputs("Define a uthread_vcore_entry() or a vcore_entry(), foo!\n", stderr);
19         abort();
20 }
21 weak_alias(__uthread_vcore_entry, uthread_vcore_entry)
22
23 void __vcore_entry(void)
24 {
25         uthread_vcore_entry();
26 }
27 weak_alias(__vcore_entry, vcore_entry)
28
29 void __vcore_event_init(void)
30 {
31         fputs("Build your application with -lparlib\n", stderr);
32         abort();
33 }
34 weak_alias(__vcore_event_init, vcore_event_init)
35
36 #define failmsg(str) write(2,str"\n",sizeof(str"\n")-1)
37
38 void
39 _start(void)
40 {
41         // WARNING: __vcore_self_on_entry must be read before
42         // anything is register-allocated!
43         int id = __vcore_id_on_entry;
44         static int init = 0;
45         // For dynamically-linked programs, the first time through,
46         // __vcore_self_on_entry could be clobbered (on x86), because
47         // the linker will have overwritten eax.  Happily, the first
48         // time through, we know we are vcore 0.  Subsequent entries
49         // into this routine do not have this problem.
50         if(init == 0)
51                 id = 0;
52         
53         // vcore0 when it comes up again, and all threads besides thread 0 must
54         // acquire a TCB.
55         if(init || (id != 0))
56         {
57                 set_tls_desc(__vcore_thread_control_blocks[id],id);
58                 __vcoreid = id;
59                 __vcore_context = TRUE;
60                 vcore_entry();
61                 failmsg("why did vcore_entry() return?");
62                 goto diediedie;
63         }
64
65         init = 1;
66
67         extern int main(int,char**,char**);
68         extern void __libc_csu_init(int,char**,char**);
69         extern void __libc_csu_fini(void);
70         extern void __libc_start_main(typeof(&main),int,char**,
71                       typeof(&__libc_csu_init),
72                       typeof(&__libc_csu_fini),
73                       void*,void*);
74
75         char** argv = (char**)alloca(sizeof(__procinfo.argp));
76         memcpy(argv,__procinfo.argp,sizeof(__procinfo.argp));
77
78         char* argbuf = (char*)alloca(sizeof(__procinfo.argbuf));
79         memcpy(argbuf,__procinfo.argbuf,sizeof(__procinfo.argbuf));
80
81         // touch up pointers, but don't mess up auxp!
82         for(int i = 0, zeros = 0; i < PROCINFO_MAX_ARGP; i++)
83         {
84                 if(argv[i])
85                         argv[i] += argbuf - __procinfo.argbuf;
86                 else if(++zeros == 2)
87                         break;
88         }
89
90         int argc = 0;
91         while(argv[argc])
92                 argc++;
93
94         extern char** _environ;
95         _environ = argv+argc+1;
96
97         vcore_event_init();
98         __libc_start_main(&main,argc,argv,&__libc_csu_init,&__libc_csu_fini,0,0);
99
100         failmsg("why did main() return?");
101
102 diediedie:
103         abort();
104         #ifdef ABORT_INSTRUCTION
105         ABORT_INSTRUCTION;
106         #endif
107         while(1);
108 }