2 * System-dependent scheduler support
4 * Copyright 1998 Alexandre Julliard
13 #include <sys/resource.h>
14 #ifdef HAVE_SYS_SYSCALL_H
15 # include <sys/syscall.h>
20 #ifdef HAVE_UCONTEXT_H
21 # include <ucontext.h>
24 #include "wine/server.h"
26 #include "wine/exception.h"
27 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(thread);
31 #if defined(linux) || defined(HAVE_CLONE)
36 # define CLONE_VM 0x00000100
37 # define CLONE_FS 0x00000200
38 # define CLONE_FILES 0x00000400
39 # define CLONE_SIGHAND 0x00000800
40 # define CLONE_PID 0x00001000
41 # endif /* CLONE_VM */
42 #endif /* linux || HAVE_CLONE */
44 /***********************************************************************
45 * SYSDEPS_SetCurThread
47 * Make 'thread' the current thread.
49 void SYSDEPS_SetCurThread( TEB *teb )
52 /* On the i386, the current thread is in the %fs register */
53 __set_fs( teb->teb_sel );
54 #elif defined(HAVE__LWP_CREATE)
55 /* On non-i386 Solaris, we use the LWP private pointer */
56 _lwp_setprivate( teb );
60 /***********************************************************************
63 * Startup routine for a new thread.
65 static void SYSDEPS_StartThread( TEB *teb )
67 SYSDEPS_SetCurThread( teb );
74 __EXCEPT(UnhandledExceptionFilter)
76 TerminateThread( GetCurrentThread(), GetExceptionCode() );
79 SYSDEPS_ExitThread(0); /* should never get here */
83 /***********************************************************************
86 * Start running a new thread.
87 * Return -1 on error, 0 if OK.
89 int SYSDEPS_SpawnThread( TEB *teb )
93 #if defined(linux) || defined(HAVE_CLONE)
94 const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
95 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
97 if (!(flags & CLONE_FILES)) close( teb->request_fd ); /* close the child socket in the parent */
102 const int flags = RFPROC | RFMEM; /*|RFFDG*/
103 void **sp = (void **)teb->stack_top;
106 *--sp = SYSDEPS_StartThread;
107 __asm__ __volatile__(
108 "pushl %2;\n\t" /* flags */
109 "pushl $0;\n\t" /* 0 ? */
110 "movl %1,%%eax;\n\t" /* SYS_rfork */
111 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
112 "cmpl $0, %%edx;\n\t"
114 "movl %0,%%esp;\n\t" /* child -> new thread */
116 "1:\n\t" /* parent -> caller thread */
118 : "r" (sp), "g" (SYS_rfork), "g" (flags)
120 if (flags & RFFDG) close( teb->request_fd ); /* close the child socket in the parent */
124 #ifdef HAVE__LWP_CREATE
126 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
127 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
128 if ( _lwp_create( &context, 0, NULL ) )
133 #endif /* ERRNO_LOCATION */
135 FIXME("CreateThread: stub\n" );
141 /***********************************************************************
144 * Exit a running thread; must not return.
146 void SYSDEPS_ExitThread( int status )
148 int fd = NtCurrentTeb()->request_fd;
149 NtCurrentTeb()->request_fd = -1;
151 #ifdef HAVE__LWP_CREATE
156 * It is of course impossible to come here,
157 * but it eliminates a compiler warning.
163 /***********************************************************************
164 * SYSDEPS_CallOnStack
166 int SYSDEPS_DoCallOnStack( int (*func)(LPVOID), LPVOID arg )
174 __EXCEPT(UnhandledExceptionFilter)
176 TerminateThread( GetCurrentThread(), GetExceptionCode() );
185 int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
186 int (*func)(LPVOID), LPVOID arg );
187 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
189 "movl %esp, %ebp\n\t"
190 ".byte 0x64; pushl 0x04\n\t"
191 ".byte 0x64; pushl 0x08\n\t"
192 "movl 8(%ebp), %esp\n\t"
193 "movl 12(%ebp), %eax\n\t"
194 ".byte 0x64; movl %esp, 0x04\n\t"
195 ".byte 0x64; movl %eax, 0x08\n\t"
198 "call " __ASM_NAME("SYSDEPS_DoCallOnStack") "\n\t"
199 "leal -8(%ebp), %esp\n\t"
200 ".byte 0x64; popl 0x08\n\t"
201 ".byte 0x64; popl 0x04\n\t"
205 int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
206 int (*func)(LPVOID), LPVOID arg )
208 return SYSDEPS_DoCallOnStack( func, arg );
212 /***********************************************************************
213 * SYSDEPS_SwitchToThreadStack
215 void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
217 DWORD page_size = getpagesize();
218 void *cur_stack = (void *)(((ULONG_PTR)&func + (page_size-1)) & ~(page_size-1));
220 TEB *teb = NtCurrentTeb();
221 LPVOID stackTop = teb->stack_top;
222 LPVOID stackLow = teb->stack_low;
226 if ( getrlimit(RLIMIT_STACK, &rl) < 0 )
228 WARN("Can't get rlimit\n");
229 rl.rlim_cur = 8*1024*1024;
232 teb->stack_top = cur_stack;
233 teb->stack_low = (char *)cur_stack - rl.rlim_cur;
235 SYSDEPS_CallOnStack( stackTop, stackLow,
236 (int (*)(void *))func, NULL );
239 /**********************************************************************
240 * NtCurrentTeb (NTDLL.@)
242 * This will crash and burn if called before threading is initialized
245 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
246 #elif defined(HAVE__LWP_CREATE)
247 /***********************************************************************
248 * NtCurrentTeb (NTDLL.@)
250 struct _TEB * WINAPI NtCurrentTeb(void)
252 extern void *_lwp_getprivate(void);
253 return (struct _TEB *)_lwp_getprivate();
256 # error NtCurrentTeb not defined for this architecture
257 #endif /* __i386__ */