Moved stack switch handling (large stack) to sysdeps.c
[wine.git] / scheduler / sysdeps.c
blobe3f0b056093e4cec92713514d242762fe18c5ef3
1 /*
2 * System-dependent scheduler support
4 * Copyright 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 /* Get pointers to the static errno and h_errno variables used by Xlib. This
10 must be done before including <errno.h> makes the variables invisible. */
11 extern int errno;
12 static int *perrno = &errno;
13 extern int h_errno;
14 static int *ph_errno = &h_errno;
16 #include <signal.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <sys/resource.h>
20 #ifdef HAVE_SYS_SYSCALL_H
21 # include <sys/syscall.h>
22 #endif
23 #ifdef HAVE_SYS_LWP_H
24 # include <sys/lwp.h>
25 #endif
26 #ifdef HAVE_UCONTEXT_H
27 # include <ucontext.h>
28 #endif
29 #include "wine/port.h"
30 #include "thread.h"
31 #include "selectors.h"
32 #include "server.h"
33 #include "winbase.h"
34 #include "wine/exception.h"
35 #include "debugtools.h"
37 DEFAULT_DEBUG_CHANNEL(thread)
39 /* Xlib critical section (FIXME: does not belong here) */
40 CRITICAL_SECTION X11DRV_CritSection = CRITICAL_SECTION_INIT;
42 #ifdef linux
43 # ifdef HAVE_SCHED_H
44 # include <sched.h>
45 # endif
46 # ifndef CLONE_VM
47 # define CLONE_VM 0x00000100
48 # define CLONE_FS 0x00000200
49 # define CLONE_FILES 0x00000400
50 # define CLONE_SIGHAND 0x00000800
51 # define CLONE_PID 0x00001000
52 # endif /* CLONE_VM */
53 #endif /* linux */
55 static int init_done;
57 #ifndef NO_REENTRANT_LIBC
59 /***********************************************************************
60 * __errno_location/__error/___errno
62 * Get the per-thread errno location.
64 #ifdef HAVE__ERRNO_LOCATION
65 int *__errno_location()
66 #endif
67 #ifdef HAVE__ERROR
68 int *__error()
69 #endif
70 #ifdef HAVE___ERRNO
71 int *___errno()
72 #endif
73 #ifdef HAVE__THR_ERRNO
74 int *__thr_errno()
75 #endif
77 if (!init_done) return perrno;
78 #ifdef NO_REENTRANT_X11
79 /* Use static libc errno while running in Xlib. */
80 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
81 return perrno;
82 #endif
83 return &NtCurrentTeb()->thread_errno;
86 /***********************************************************************
87 * __h_errno_location
89 * Get the per-thread h_errno location.
91 int *__h_errno_location()
93 if (!init_done) return ph_errno;
94 #ifdef NO_REENTRANT_X11
95 /* Use static libc h_errno while running in Xlib. */
96 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
97 return ph_errno;
98 #endif
99 return &NtCurrentTeb()->thread_h_errno;
102 #endif /* NO_REENTRANT_LIBC */
104 /***********************************************************************
105 * SYSDEPS_SetCurThread
107 * Make 'thread' the current thread.
109 void SYSDEPS_SetCurThread( TEB *teb )
111 #if defined(__i386__)
112 /* On the i386, the current thread is in the %fs register */
113 __set_fs( teb->teb_sel );
114 #elif defined(HAVE__LWP_CREATE)
115 /* On non-i386 Solaris, we use the LWP private pointer */
116 _lwp_setprivate( teb );
117 #endif
119 init_done = 1; /* now we can use threading routines */
122 /***********************************************************************
123 * SYSDEPS_StartThread
125 * Startup routine for a new thread.
127 static void SYSDEPS_StartThread( TEB *teb )
129 SYSDEPS_SetCurThread( teb );
130 CLIENT_InitThread();
131 SIGNAL_Init();
132 __TRY
134 teb->startup();
136 __EXCEPT(UnhandledExceptionFilter)
138 TerminateThread( GetCurrentThread(), GetExceptionCode() );
140 __ENDTRY
141 SYSDEPS_ExitThread(0); /* should never get here */
145 /***********************************************************************
146 * SYSDEPS_SpawnThread
148 * Start running a new thread.
149 * Return -1 on error, 0 if OK.
151 int SYSDEPS_SpawnThread( TEB *teb )
153 #ifndef NO_REENTRANT_LIBC
155 #ifdef linux
156 const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
157 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
158 return -1;
159 if (!(flags & CLONE_FILES)) close( teb->socket ); /* close the child socket in the parent */
160 return 0;
161 #endif
163 #ifdef HAVE_RFORK
164 const int flags = RFPROC | RFMEM; /*|RFFDG*/
165 void **sp = (void **)teb->stack_top;
166 *--sp = teb;
167 *--sp = 0;
168 *--sp = SYSDEPS_StartThread;
169 __asm__ __volatile__(
170 "pushl %2;\n\t" /* flags */
171 "pushl $0;\n\t" /* 0 ? */
172 "movl %1,%%eax;\n\t" /* SYS_rfork */
173 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
174 "cmpl $0, %%edx;\n\t"
175 "je 1f;\n\t"
176 "movl %0,%%esp;\n\t" /* child -> new thread */
177 "ret;\n"
178 "1:\n\t" /* parent -> caller thread */
179 "addl $8,%%esp" :
180 : "r" (sp), "g" (SYS_rfork), "g" (flags)
181 : "eax", "edx");
182 if (flags & RFFDG) close( teb->socket ); /* close the child socket in the parent */
183 return 0;
184 #endif
186 #ifdef HAVE__LWP_CREATE
187 ucontext_t context;
188 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
189 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
190 if ( _lwp_create( &context, 0, NULL ) )
191 return -1;
192 return 0;
193 #endif
195 #endif /* NO_REENTRANT_LIBC */
197 FIXME("CreateThread: stub\n" );
198 return 0;
203 /***********************************************************************
204 * SYSDEPS_ExitThread
206 * Exit a running thread; must not return.
208 void SYSDEPS_ExitThread( int status )
210 int socket = NtCurrentTeb()->socket;
211 NtCurrentTeb()->socket = -1;
212 close( socket );
213 #ifdef HAVE__LWP_CREATE
214 _lwp_exit();
215 #endif
216 _exit( status );
218 * It is of course impossible to come here,
219 * but it eliminates a compiler warning.
221 exit( status );
225 /***********************************************************************
226 * SYSDEPS_CallOnStack
228 static int SYSDEPS_DoCallOnStack( int (*func)(LPVOID), LPVOID arg ) WINE_UNUSED;
229 static int SYSDEPS_DoCallOnStack( int (*func)(LPVOID), LPVOID arg )
231 int retv = 0;
233 __TRY
235 retv = func( arg );
237 __EXCEPT(UnhandledExceptionFilter)
239 TerminateThread( GetCurrentThread(), GetExceptionCode() );
240 return 0;
242 __ENDTRY
244 return retv;
247 #ifdef __i386__
248 int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
249 int (*func)(LPVOID), LPVOID arg );
250 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
251 "pushl %ebp\n\t"
252 "movl %esp, %ebp\n\t"
253 ".byte 0x64; pushl 0x04\n\t"
254 ".byte 0x64; pushl 0x08\n\t"
255 "movl 8(%ebp), %esp\n\t"
256 "movl 12(%ebp), %eax\n\t"
257 ".byte 0x64; movl %esp, 0x04\n\t"
258 ".byte 0x64; movl %eax, 0x08\n\t"
259 "pushl 20(%ebp)\n\t"
260 "pushl 16(%ebp)\n\t"
261 "call " __ASM_NAME("SYSDEPS_DoCallOnStack") "\n\t"
262 "leal -8(%ebp), %esp\n\t"
263 ".byte 0x64; popl 0x08\n\t"
264 ".byte 0x64; popl 0x04\n\t"
265 "popl %ebp\n\t"
266 "ret" );
267 #else
268 int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
269 int (*func)(LPVOID), LPVOID arg )
271 return SYSDEPS_DoCallOnStack( func, arg );
273 #endif
275 /***********************************************************************
276 * SYSDEPS_SwitchToThreadStack
279 static LPVOID SYSDEPS_LargeStackTop = NULL;
280 static LPVOID SYSDEPS_LargeStackLow = NULL;
282 void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
284 TEB *teb = NtCurrentTeb();
285 LPVOID stackTop = teb->stack_top;
286 LPVOID stackLow = teb->stack_low;
288 struct rlimit rl;
290 if ( getrlimit(RLIMIT_STACK, &rl) < 0 )
292 WARN("Can't get rlimit\n");
293 rl.rlim_cur = 8*1024*1024;
296 SYSDEPS_LargeStackTop = teb->stack_top = &func - 128;
297 SYSDEPS_LargeStackLow = teb->stack_low = teb->stack_top - rl.rlim_cur;
299 SYSDEPS_CallOnStack( stackTop, stackLow,
300 (int (*)(void *))func, NULL );
303 /***********************************************************************
304 * SYSDEPS_CallOnLargeStack
306 int SYSDEPS_CallOnLargeStack( int (*func)(LPVOID), LPVOID arg )
308 static int recurse = 0;
309 int retv;
311 if ( recurse++ == 0 && SYSDEPS_LargeStackTop )
312 retv = SYSDEPS_CallOnStack( SYSDEPS_LargeStackTop,
313 SYSDEPS_LargeStackLow, func, arg );
314 else
315 retv = func( arg );
317 recurse--;
318 return retv;
322 /**********************************************************************
323 * NtCurrentTeb (NTDLL.89)
325 * This will crash and burn if called before threading is initialized
327 #ifdef __i386__
328 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
329 #elif defined(HAVE__LWP_CREATE)
330 struct _TEB * WINAPI NtCurrentTeb(void)
332 extern void *_lwp_getprivate(void);
333 return (struct _TEB *)_lwp_getprivate();
335 #else
336 # error NtCurrentTeb not defined for this architecture
337 #endif /* __i386__ */