Rev 4874 | Rev 5190 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4874 | Rev 4921 | ||
---|---|---|---|
1 | #include <_ansi.h> |
1 | #include <_ansi.h> |
2 | #include |
2 | #include |
3 | #include |
3 | #include |
4 | #include |
4 | #include |
5 | #include |
5 | #include |
6 | #include |
6 | #include |
7 | #include |
7 | #include |
8 | #include |
8 | #include |
9 | #include |
9 | #include |
10 | 10 | ||
11 | struct app_hdr |
11 | struct app_hdr |
12 | { |
12 | { |
13 | char banner[8]; |
13 | char banner[8]; |
14 | int version; |
14 | int version; |
15 | int start; |
15 | int start; |
16 | int iend; |
16 | int iend; |
17 | int memsize; |
17 | int memsize; |
18 | int stacktop; |
18 | int stacktop; |
19 | char *cmdline; |
19 | char *cmdline; |
20 | char *path; |
20 | char *path; |
21 | int reserved; |
21 | int reserved; |
22 | void *__idata_start; |
22 | void *__idata_start; |
23 | void *__idata_end; |
23 | void *__idata_end; |
24 | int (*main)(int argc, char **argv, char **envp); |
24 | int (*main)(int argc, char **argv, char **envp); |
25 | }; |
25 | }; |
26 | 26 | ||
27 | int _argc; |
27 | int _argc; |
28 | char **_argv; |
28 | char **_argv; |
29 | 29 | ||
30 | 30 | ||
31 | void* get_entry_point(void *raw); |
31 | void* get_entry_point(void *raw); |
32 | int (*entry)(int, char **, char **); |
32 | int (*entry)(int, char **, char **); |
33 | 33 | ||
34 | void init_loader(void *libc_image); |
34 | void init_loader(void *libc_image); |
35 | 35 | ||
36 | 36 | ||
37 | void init_reent(); |
37 | void init_reent(); |
38 | 38 | ||
39 | jmp_buf loader_env; |
39 | jmp_buf loader_env; |
40 | 40 | ||
41 | void __attribute__((noreturn)) |
41 | void __attribute__((noreturn)) |
42 | __thread_startup (int (*entry)(void*), void *param, |
42 | __thread_startup (int (*entry)(void*), void *param, |
43 | void *stacklow, void *stackhigh) |
43 | void *stacklow, void *stackhigh) |
44 | { |
44 | { |
45 | int retval; |
45 | int retval; |
46 | 46 | ||
47 | asm volatile ( "xchgw %bx, %bx"); |
47 | asm volatile ( "xchgw %bx, %bx"); |
48 | 48 | ||
49 | __asm__ __volatile__( // save stack limits |
49 | __asm__ __volatile__( // save stack limits |
50 | "movl %0, %%fs:8 \n\t" // use TLS |
50 | "movl %0, %%fs:8 \n\t" // use TLS |
51 | "movl %1, %%fs:12 \n\t" |
51 | "movl %1, %%fs:12 \n\t" |
52 | ::"r"(stacklow), "r"(stackhigh)); |
52 | ::"r"(stacklow), "r"(stackhigh)); |
53 | 53 | ||
54 | init_reent(); // initialize thread reentry structure |
54 | init_reent(); // initialize thread reentry structure |
55 | 55 | ||
56 | retval = entry(param); // call user thread function |
56 | retval = entry(param); // call user thread function |
57 | 57 | ||
58 | _exit(retval); |
58 | _exit(retval); |
59 | }; |
59 | }; |
60 | 60 | ||
61 | char * __libc_getenv(const char *name) |
61 | char * __libc_getenv(const char *name) |
62 | { |
62 | { |
63 | return NULL; |
63 | return NULL; |
64 | } |
64 | } |
65 | 65 | ||
66 | void _pei386_runtime_relocator (void); |
66 | void _pei386_runtime_relocator (void); |
67 | int link_app(); |
67 | int link_app(); |
68 | 68 | ||
69 | char __appcwd[1024]; |
69 | char __appcwd[1024]; |
70 | int __appcwdlen; |
70 | int __appcwdlen; |
71 | char* __appenv; |
71 | char* __appenv; |
72 | int __appenv_size; |
72 | int __appenv_size; |
73 | 73 | ||
74 | static char *arg[2]; |
74 | static char *arg[2]; |
75 | 75 | ||
76 | extern char _tls_map[128]; |
76 | extern char _tls_map[128]; |
77 | 77 | ||
78 | void __attribute__((noreturn)) |
78 | void __attribute__((noreturn)) |
79 | libc_crt_startup (void *libc_base) |
79 | libc_crt_startup (void *libc_base) |
80 | { |
80 | { |
81 | struct app_hdr *header = NULL; |
81 | struct app_hdr *header = NULL; |
82 | 82 | ||
83 | int len; |
83 | int len; |
84 | char *p; |
84 | char *p; |
85 | 85 | ||
86 | void *my_app; |
86 | void *my_app; |
87 | int retval = 0; |
87 | int retval = 0; |
88 | 88 | ||
89 | _pei386_runtime_relocator(); |
89 | _pei386_runtime_relocator(); |
90 | 90 | ||
91 | memset(_tls_map, 0xFF, 32*4); |
91 | memset(_tls_map, 0xFF, 32*4); |
92 | _tls_map[0] = 0xE0; |
92 | _tls_map[0] = 0xE0; |
93 | init_reent(); |
93 | init_reent(); |
94 | __initPOSIXHandles(); |
94 | init_stdio(); |
95 | 95 | ||
96 | // __appenv = load_file("/sys/system.env", &__appenv_size); |
96 | // __appenv = load_file("/sys/system.env", &__appenv_size); |
97 | 97 | ||
98 | init_loader(libc_base); |
98 | init_loader(libc_base); |
99 | 99 | ||
100 | if( link_app() == 0) |
100 | if( link_app() == 0) |
101 | goto done; |
101 | goto done; |
102 | 102 | ||
103 | __appcwdlen = strrchr(header->path, '/') - header->path; |
103 | __appcwdlen = strrchr(header->path, '/') - header->path; |
104 | __appcwdlen = __appcwdlen > 1022 ? 1022 : __appcwdlen; |
104 | __appcwdlen = __appcwdlen > 1022 ? 1022 : __appcwdlen; |
105 | memcpy(__appcwd, header->path, __appcwdlen); |
105 | memcpy(__appcwd, header->path, __appcwdlen); |
106 | set_cwd(__appcwd); |
106 | set_cwd(__appcwd); |
107 | 107 | ||
108 | arg[0] = header->path; |
108 | arg[0] = header->path; |
109 | 109 | ||
110 | if( header->cmdline[0] != 0) |
110 | if( header->cmdline[0] != 0) |
111 | { |
111 | { |
112 | _argc = 2; |
112 | _argc = 2; |
113 | arg[1] = header->cmdline; |
113 | arg[1] = header->cmdline; |
114 | } |
114 | } |
115 | else _argc = 1; |
115 | else _argc = 1; |
116 | 116 | ||
117 | _argv = arg; |
117 | _argv = arg; |
118 | 118 | ||
119 | retval = header->main(_argc, _argv, NULL); |
119 | retval = header->main(_argc, _argv, NULL); |
120 | done: |
120 | done: |
121 | exit (retval); |
121 | exit (retval); |
122 | } |
122 | } |