Added back harts support
[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 #include <unistd.h>
8
9 void** __hart_stack_pointers = NULL;
10 weak_alias(__hart_stack_pointers,hart_stack_pointers)
11 void** __hart_thread_control_blocks = NULL;
12 weak_alias(__hart_thread_control_blocks,hart_thread_control_blocks)
13
14 void
15 __hart_entry(void)
16 {
17         fputs("define a hart_entry() function, foo!",stderr);
18         abort();
19 }
20 weak_alias(__hart_entry,hart_entry)
21
22 void
23 __hart_yield(void)
24 {
25 }
26 weak_alias(__hart_yield,hart_yield)
27
28 #define failmsg(str) write(2,str"\n",sizeof(str"\n")-1)
29
30 void
31 _start(void)
32 {
33         // threads besides thread 0 must acquire a stack and TCB.
34         // WARNING: no function calls or register spills may occur
35         // before the stack pointer is set!
36         // WARNING2: __hart_self_on_entry must be read before
37         // anything is register-allocated!
38         int id = __hart_self_on_entry;
39         if(id != 0)
40         {
41                 __hart_set_stack_pointer(__hart_stack_pointers[id]);
42                 TLS_INIT_TP(__hart_thread_control_blocks[id],0);
43                 hart_entry();
44                 hart_yield();
45                 failmsg("why did hart_yield() return?");
46                 goto diediedie;
47         }
48
49         static int init = 0;
50         if(init)
51         {
52                 failmsg("why did thread 0 re-enter _start?");
53                 goto diediedie;
54         }
55         init = 1;
56
57         extern int main(int,char**,char**);
58         extern void __libc_csu_init(int,char**,char**);
59         extern void __libc_csu_fini(void);
60         extern void __libc_start_main(typeof(&main),int,char**,
61                       typeof(&__libc_csu_init),
62                       typeof(&__libc_csu_fini),
63                       void*,void*);
64
65         char** argv = (char**)alloca(sizeof(__procinfo.argp));
66         memcpy(argv,__procinfo.argp,sizeof(__procinfo.argp));
67
68         char* argbuf = (char*)alloca(sizeof(__procinfo.argbuf));
69         memcpy(argbuf,__procinfo.argbuf,sizeof(__procinfo.argbuf));
70
71         for(int i = 0; i < PROCINFO_MAX_ARGP; i++)
72                 if(argv[i])
73                         argv[i] += argbuf - __procinfo.argbuf;
74
75         int argc = 0;
76         while(argv[argc])
77                 argc++;
78
79         __libc_start_main(&main,argc,argv,&__libc_csu_init,&__libc_csu_fini,0,0);
80
81         failmsg("why did main() return?");
82
83 diediedie:
84         abort();
85         #ifdef ABORT_INSTRUCTION
86         ABORT_INSTRUCTION;
87         #endif
88         while(1);
89 }
90