Do not use the PEB lock as loader lock, use a separate critical
[wine] / scheduler / sysdeps.c
1 /*
2  * System-dependent scheduler support
3  *
4  * Copyright 1998 Alexandre Julliard
5  */
6
7 #include "config.h"
8 #include "wine/port.h"
9
10 #include <signal.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <sys/time.h>
14 #include <sys/resource.h>
15 #ifdef HAVE_SYS_SYSCALL_H
16 # include <sys/syscall.h>
17 #endif
18 #ifdef HAVE_SYS_LWP_H
19 # include <sys/lwp.h>
20 #endif
21 #ifdef HAVE_UCONTEXT_H
22 # include <ucontext.h>
23 #endif
24 #ifdef HAVE_SYS_MMAN_H
25 #include <sys/mman.h>
26 #endif
27 #include "thread.h"
28 #include "wine/server.h"
29 #include "winbase.h"
30 #include "wine/winbase16.h"
31 #include "wine/exception.h"
32 #include "debugtools.h"
33
34 DEFAULT_DEBUG_CHANNEL(thread);
35
36 #if defined(linux) || defined(HAVE_CLONE)
37 # ifdef HAVE_SCHED_H
38 #  include <sched.h>
39 # endif
40 # ifndef CLONE_VM
41 #  define CLONE_VM      0x00000100
42 #  define CLONE_FS      0x00000200
43 #  define CLONE_FILES   0x00000400
44 #  define CLONE_SIGHAND 0x00000800
45 #  define CLONE_PID     0x00001000
46 # endif  /* CLONE_VM */
47 #endif  /* linux || HAVE_CLONE */
48
49 extern void SELECTOR_FreeFs(void);
50
51 struct thread_cleanup_info
52 {
53     void *stack_base;
54     int   stack_size;
55     int   status;
56 };
57
58 /* temporary stacks used on thread exit */
59 #define TEMP_STACK_SIZE 1024
60 #define NB_TEMP_STACKS  8
61 static char temp_stacks[NB_TEMP_STACKS][TEMP_STACK_SIZE];
62 static LONG next_temp_stack;  /* next temp stack to use */
63
64 /***********************************************************************
65  *           SYSDEPS_SetCurThread
66  *
67  * Make 'thread' the current thread.
68  */
69 void SYSDEPS_SetCurThread( TEB *teb )
70 {
71 #if defined(__i386__)
72     /* On the i386, the current thread is in the %fs register */
73     __set_fs( teb->teb_sel );
74 #elif defined(HAVE__LWP_CREATE)
75     /* On non-i386 Solaris, we use the LWP private pointer */
76     _lwp_setprivate( teb );
77 #endif
78 }
79
80
81 /***********************************************************************
82  *           call_on_thread_stack
83  *
84  * Call a function once we switched to the thread stack.
85  */
86 static void call_on_thread_stack( void *func )
87 {
88     __TRY
89     {
90         void (*funcptr)(void) = func;
91         funcptr();
92     }
93     __EXCEPT(UnhandledExceptionFilter)
94     {
95         TerminateThread( GetCurrentThread(), GetExceptionCode() );
96     }
97     __ENDTRY
98     SYSDEPS_ExitThread(0);  /* should never get here */
99 }
100
101
102 /***********************************************************************
103  *           get_temp_stack
104  *
105  * Get a temporary stack address to run the thread exit code on.
106  */
107 inline static char *get_temp_stack(void)
108 {
109     unsigned int next = InterlockedExchangeAdd( &next_temp_stack, 1 );
110     return temp_stacks[next % NB_TEMP_STACKS];
111 }
112
113
114 /***********************************************************************
115  *           cleanup_thread
116  *
117  * Cleanup the remains of a thread. Runs on a temporary stack.
118  */
119 static void cleanup_thread( void *ptr )
120 {
121     /* copy the info structure since it is on the stack we will free */
122     struct thread_cleanup_info info = *(struct thread_cleanup_info *)ptr;
123     munmap( info.stack_base, info.stack_size );
124     SELECTOR_FreeFs();
125 #ifdef HAVE__LWP_CREATE
126     _lwp_exit();
127 #endif
128     _exit( info.status );
129 }
130
131
132 /***********************************************************************
133  *           SYSDEPS_StartThread
134  *
135  * Startup routine for a new thread.
136  */
137 static void SYSDEPS_StartThread( TEB *teb )
138 {
139     SYSDEPS_SetCurThread( teb );
140     CLIENT_InitThread();
141     SIGNAL_Init();
142     __TRY
143     {
144         teb->startup();
145     }
146     __EXCEPT(UnhandledExceptionFilter)
147     {
148         TerminateThread( GetCurrentThread(), GetExceptionCode() );
149     }
150     __ENDTRY
151     SYSDEPS_ExitThread(0);  /* should never get here */
152 }
153
154
155 /***********************************************************************
156  *           SYSDEPS_SpawnThread
157  *
158  * Start running a new thread.
159  * Return -1 on error, 0 if OK.
160  */
161 int SYSDEPS_SpawnThread( TEB *teb )
162 {
163 #ifdef ERRNO_LOCATION
164
165 #if defined(linux) || defined(HAVE_CLONE)
166     const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
167     if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
168         return -1;
169     if (!(flags & CLONE_FILES)) close( teb->request_fd );  /* close the child socket in the parent */
170     return 0;
171 #endif
172
173 #ifdef HAVE_RFORK
174     const int flags = RFPROC | RFMEM; /*|RFFDG*/
175     void **sp = (void **)teb->stack_top;
176     *--sp = teb;
177     *--sp = 0;
178     *--sp = SYSDEPS_StartThread;
179     __asm__ __volatile__(
180     "pushl %2;\n\t"             /* flags */
181     "pushl $0;\n\t"             /* 0 ? */
182     "movl %1,%%eax;\n\t"        /* SYS_rfork */
183     ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
184     "cmpl $0, %%edx;\n\t"
185     "je 1f;\n\t"
186     "movl %0,%%esp;\n\t"        /* child -> new thread */
187     "ret;\n"
188     "1:\n\t"            /* parent -> caller thread */
189     "addl $8,%%esp" :
190     : "r" (sp), "g" (SYS_rfork), "g" (flags)
191     : "eax", "edx");
192     if (flags & RFFDG) close( teb->request_fd );  /* close the child socket in the parent */
193     return 0;
194 #endif
195
196 #ifdef HAVE__LWP_CREATE
197     ucontext_t context;
198     _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
199                       NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
200     if ( _lwp_create( &context, 0, NULL ) )
201         return -1;
202     return 0;
203 #endif
204
205 #endif /* ERRNO_LOCATION */
206
207     FIXME("CreateThread: stub\n" );
208     return 0;
209 }
210
211
212 /***********************************************************************
213  *           SYSDEPS_CallOnStack
214  */
215 void SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg ) WINE_NORETURN;
216 #ifdef __i386__
217 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
218                    "movl 4(%esp),%ecx\n\t"  /* func */
219                    "movl 8(%esp),%edx\n\t"  /* arg */
220                    ".byte 0x64\n\tmovl 0x04,%esp\n\t"  /* teb->stack_top */
221                    "pushl %edx\n\t"
222                    "xorl %ebp,%ebp\n\t"
223                    "call *%ecx\n\t"
224                    "int $3" /* we never return here */ );
225 #else
226 void SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg )
227 {
228     func( arg );
229     while(1); /* avoid warning */
230 }
231 #endif
232
233
234 /***********************************************************************
235  *           SYSDEPS_SwitchToThreadStack
236  */
237 void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
238 {
239     SYSDEPS_CallOnStack( call_on_thread_stack, func );
240 }
241
242
243 /***********************************************************************
244  *           SYSDEPS_ExitThread
245  *
246  * Exit a running thread; must not return.
247  */
248 void SYSDEPS_ExitThread( int status )
249 {
250     TEB *teb = NtCurrentTeb();
251     struct thread_cleanup_info info;
252     MEMORY_BASIC_INFORMATION meminfo;
253
254     FreeSelector16( teb->stack_sel );
255     VirtualQuery( teb->stack_top, &meminfo, sizeof(meminfo) );
256     info.stack_base = meminfo.AllocationBase;
257     info.stack_size = meminfo.RegionSize + ((char *)teb->stack_top - (char *)meminfo.AllocationBase);
258     info.status     = status;
259
260     SIGNAL_Reset();
261
262     VirtualFree( teb->stack_base, 0, MEM_RELEASE | MEM_SYSTEM );
263     close( teb->wait_fd[0] );
264     close( teb->wait_fd[1] );
265     close( teb->reply_fd );
266     close( teb->request_fd );
267     teb->stack_low = get_temp_stack();
268     teb->stack_top = teb->stack_low + TEMP_STACK_SIZE;
269     SYSDEPS_CallOnStack( cleanup_thread, &info );
270 }
271
272
273 /***********************************************************************
274  *           SYSDEPS_AbortThread
275  *
276  * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
277  */
278 void SYSDEPS_AbortThread( int status )
279 {
280     SIGNAL_Reset();
281     close( NtCurrentTeb()->wait_fd[0] );
282     close( NtCurrentTeb()->wait_fd[1] );
283     close( NtCurrentTeb()->reply_fd );
284     close( NtCurrentTeb()->request_fd );
285 #ifdef HAVE__LWP_CREATE
286     _lwp_exit();
287 #endif
288     for (;;)  /* avoid warning */
289         _exit( status );
290 }
291
292
293 /**********************************************************************
294  *           NtCurrentTeb   (NTDLL.@)
295  *
296  * This will crash and burn if called before threading is initialized
297  */
298 #ifdef __i386__
299 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
300 #elif defined(HAVE__LWP_CREATE)
301 /***********************************************************************
302  *              NtCurrentTeb (NTDLL.@)
303  */
304 struct _TEB * WINAPI NtCurrentTeb(void)
305 {
306     extern void *_lwp_getprivate(void);
307     return (struct _TEB *)_lwp_getprivate();
308 }
309 #else
310 # error NtCurrentTeb not defined for this architecture
311 #endif  /* __i386__ */