loader: Add missing checks for FreeBSD_kernel.
[wine] / loader / main.c
1 /*
2  * Emulator initialisation code
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_SYS_MMAN_H
28 # include <sys/mman.h>
29 #endif
30 #ifdef HAVE_SYS_RESOURCE_H
31 # include <sys/resource.h>
32 #endif
33 #ifdef HAVE_SYS_SYSCALL_H
34 # include <sys/syscall.h>
35 #endif
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <pthread.h>
40
41 #include "wine/library.h"
42 #include "main.h"
43
44 #ifdef __APPLE__
45
46 __asm__(".zerofill WINE_DOS, WINE_DOS, ___wine_dos, 0x40000000");
47 __asm__(".zerofill WINE_SHAREDHEAP, WINE_SHAREDHEAP, ___wine_shared_heap, 0x03000000");
48 extern char __wine_dos[0x40000000], __wine_shared_heap[0x03000000];
49
50 static const struct wine_preload_info wine_main_preload_info[] =
51 {
52     { __wine_dos,         sizeof(__wine_dos) },          /* DOS area + PE exe */
53     { __wine_shared_heap, sizeof(__wine_shared_heap) },  /* shared user data + shared heap */
54     { 0, 0 }  /* end of list */
55 };
56
57 static inline void reserve_area( void *addr, size_t size )
58 {
59     wine_anon_mmap( addr, size, PROT_NONE, MAP_FIXED | MAP_NORESERVE );
60     wine_mmap_add_reserved_area( addr, size );
61 }
62
63 #else  /* __APPLE__ */
64
65 /* the preloader will set this variable */
66 const struct wine_preload_info *wine_main_preload_info = NULL;
67
68 static inline void reserve_area( void *addr, size_t size )
69 {
70     wine_mmap_add_reserved_area( addr, size );
71 }
72
73 #endif  /* __APPLE__ */
74
75 /***********************************************************************
76  *           check_command_line
77  *
78  * Check if command line is one that needs to be handled specially.
79  */
80 static void check_command_line( int argc, char *argv[] )
81 {
82     static const char usage[] =
83         "Usage: wine PROGRAM [ARGUMENTS...]   Run the specified program\n"
84         "       wine --help                   Display this help and exit\n"
85         "       wine --version                Output version information and exit";
86
87     if (argc <= 1)
88     {
89         fprintf( stderr, "%s\n", usage );
90         exit(1);
91     }
92     if (!strcmp( argv[1], "--help" ))
93     {
94         printf( "%s\n", usage );
95         exit(0);
96     }
97     if (!strcmp( argv[1], "--version" ))
98     {
99         printf( "%s\n", wine_get_build_id() );
100         exit(0);
101     }
102 }
103
104
105 #if defined(__linux__) && defined(__i386__)
106
107 /* separate thread to check for NPTL and TLS features */
108 static void *needs_pthread( void *arg )
109 {
110     pid_t tid = syscall( SYS_gettid );
111     /* check for NPTL */
112     if (tid != -1 && tid != getpid()) return (void *)1;
113     /* check for TLS glibc */
114     if (wine_get_gs() != 0) return (void *)1;
115     /* check for exported epoll_create to detect new glibc versions without TLS */
116     if (wine_dlsym( RTLD_DEFAULT, "epoll_create", NULL, 0 ))
117         fprintf( stderr,
118                  "wine: glibc >= 2.3 without NPTL or TLS is not a supported combination.\n"
119                  "      Please upgrade to a glibc with NPTL support.\n" );
120     else
121         fprintf( stderr,
122                  "wine: Your C library is too old. You need at least glibc 2.3 with NPTL support.\n" );
123     return 0;
124 }
125
126 /* check if we support the glibc threading model */
127 static void check_threading(void)
128 {
129     pthread_t id;
130     void *ret;
131
132     pthread_create( &id, NULL, needs_pthread, NULL );
133     pthread_join( id, &ret );
134     if (!ret) exit(1);
135 }
136
137 static void check_vmsplit( void *stack )
138 {
139     if (stack < (void *)0x80000000)
140     {
141         /* if the stack is below 0x80000000, assume we can safely try a munmap there */
142         if (munmap( (void *)0x80000000, 1 ) == -1 && errno == EINVAL)
143             fprintf( stderr,
144                      "Warning: memory above 0x80000000 doesn't seem to be accessible.\n"
145                      "Wine requires a 3G/1G user/kernel memory split to work properly.\n" );
146     }
147 }
148
149 static void set_max_limit( int limit )
150 {
151     struct rlimit rlimit;
152
153     if (!getrlimit( limit, &rlimit ))
154     {
155         rlimit.rlim_cur = rlimit.rlim_max;
156         setrlimit( limit, &rlimit );
157     }
158 }
159
160 static int pre_exec(void)
161 {
162     int temp;
163
164     check_threading();
165     check_vmsplit( &temp );
166     set_max_limit( RLIMIT_AS );
167     return 1;
168 }
169
170 #elif defined(__linux__) && defined(__x86_64__)
171
172 static int pre_exec(void)
173 {
174     return 1;  /* we have a preloader on x86-64 */
175 }
176
177 #elif (defined(__FreeBSD__) || defined (__FreeBSD_kernel__)) && defined(__i386__)
178
179 static int pre_exec(void)
180 {
181     struct rlimit rl;
182
183     rl.rlim_cur = 0x02000000;
184     rl.rlim_max = 0x02000000;
185     setrlimit( RLIMIT_DATA, &rl );
186     return 1;
187 }
188
189 #else
190
191 static int pre_exec(void)
192 {
193     return 0;  /* no exec needed */
194 }
195
196 #endif
197
198
199 /**********************************************************************
200  *           main
201  */
202 int main( int argc, char *argv[] )
203 {
204     char error[1024];
205     int i;
206
207     if (!getenv( "WINELOADERNOEXEC" ))  /* first time around */
208     {
209         static char noexec[] = "WINELOADERNOEXEC=1";
210
211         putenv( noexec );
212         check_command_line( argc, argv );
213         if (pre_exec())
214         {
215             wine_init_argv0_path( argv[0] );
216             wine_exec_wine_binary( NULL, argv, getenv( "WINELOADER" ));
217             fprintf( stderr, "wine: could not exec the wine loader\n" );
218             exit(1);
219         }
220     }
221
222     if (wine_main_preload_info)
223     {
224         for (i = 0; wine_main_preload_info[i].size; i++)
225             reserve_area( wine_main_preload_info[i].addr, wine_main_preload_info[i].size );
226     }
227
228     wine_init( argc, argv, error, sizeof(error) );
229     fprintf( stderr, "wine: failed to initialize: %s\n", error );
230     exit(1);
231 }