blob: 9073cf797de73cc8ee30a23dd1973d82de57b583 [file] [log] [blame]
Alexandre Julliard0623a6f1998-01-18 18:01:49 +00001/*
2 * System-dependent scheduler support
3 *
4 * Copyright 1998 Alexandre Julliard
5 */
6
Marcus Meissnerc2606381999-04-11 15:20:29 +00007#include "config.h"
8
Alexandre Julliard0623a6f1998-01-18 18:01:49 +00009#include <signal.h>
10#include <stdio.h>
11#include <unistd.h>
Ulrich Weigand06bf0172000-06-08 00:39:59 +000012#include <sys/time.h>
Ulrich Weigand7e85ea72000-06-04 01:35:43 +000013#include <sys/resource.h>
Marcus Meissnerc2606381999-04-11 15:20:29 +000014#ifdef HAVE_SYS_SYSCALL_H
15# include <sys/syscall.h>
16#endif
Ulrich Weiganda44f9f81999-04-18 13:20:43 +000017#ifdef HAVE_SYS_LWP_H
18# include <sys/lwp.h>
19#endif
20#ifdef HAVE_UCONTEXT_H
21# include <ucontext.h>
22#endif
Patrik Stridvallb9010211999-11-13 22:23:35 +000023#include "wine/port.h"
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000024#include "thread.h"
Alexandre Julliard37e95032001-07-19 00:39:09 +000025#include "wine/server.h"
Alexandre Julliard03468f71998-02-15 19:40:49 +000026#include "winbase.h"
Alexandre Julliard3d06d201999-09-27 10:58:45 +000027#include "wine/exception.h"
Alexandre Julliard15657091999-05-23 10:25:25 +000028#include "debugtools.h"
Alexandre Julliard03468f71998-02-15 19:40:49 +000029
Dimitrie O. Paun529da542000-11-27 23:54:25 +000030DEFAULT_DEBUG_CHANNEL(thread);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000031
Bang Jun-Young289a2522001-06-15 19:43:51 +000032#if defined(linux) || defined(HAVE_CLONE)
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000033# ifdef HAVE_SCHED_H
34# include <sched.h>
35# endif
36# ifndef CLONE_VM
37# define CLONE_VM 0x00000100
38# define CLONE_FS 0x00000200
39# define CLONE_FILES 0x00000400
40# define CLONE_SIGHAND 0x00000800
41# define CLONE_PID 0x00001000
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000042# endif /* CLONE_VM */
Bang Jun-Young289a2522001-06-15 19:43:51 +000043#endif /* linux || HAVE_CLONE */
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000044
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000045/***********************************************************************
Alexandre Julliard54a39e21999-05-29 14:27:26 +000046 * SYSDEPS_SetCurThread
47 *
48 * Make 'thread' the current thread.
49 */
Alexandre Julliard0a860a01999-06-22 11:43:42 +000050void SYSDEPS_SetCurThread( TEB *teb )
Alexandre Julliard54a39e21999-05-29 14:27:26 +000051{
Ulrich Weigandcebd60b1999-09-03 16:45:44 +000052#if defined(__i386__)
Alexandre Julliard54a39e21999-05-29 14:27:26 +000053 /* On the i386, the current thread is in the %fs register */
Alexandre Julliard916f9752000-02-26 16:51:13 +000054 __set_fs( teb->teb_sel );
Ulrich Weigandcebd60b1999-09-03 16:45:44 +000055#elif defined(HAVE__LWP_CREATE)
56 /* On non-i386 Solaris, we use the LWP private pointer */
57 _lwp_setprivate( teb );
58#endif
Alexandre Julliard54a39e21999-05-29 14:27:26 +000059}
60
61/***********************************************************************
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000062 * SYSDEPS_StartThread
63 *
64 * Startup routine for a new thread.
65 */
Alexandre Julliard0a860a01999-06-22 11:43:42 +000066static void SYSDEPS_StartThread( TEB *teb )
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000067{
Alexandre Julliard0a860a01999-06-22 11:43:42 +000068 SYSDEPS_SetCurThread( teb );
Alexandre Julliard875c4b31999-03-23 14:09:41 +000069 CLIENT_InitThread();
Alexandre Julliard3d06d201999-09-27 10:58:45 +000070 SIGNAL_Init();
71 __TRY
72 {
73 teb->startup();
74 }
75 __EXCEPT(UnhandledExceptionFilter)
76 {
77 TerminateThread( GetCurrentThread(), GetExceptionCode() );
78 }
79 __ENDTRY
Alexandre Julliard12f29b52000-03-17 15:16:57 +000080 SYSDEPS_ExitThread(0); /* should never get here */
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000081}
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000082
83
84/***********************************************************************
85 * SYSDEPS_SpawnThread
86 *
87 * Start running a new thread.
88 * Return -1 on error, 0 if OK.
89 */
Alexandre Julliard0a860a01999-06-22 11:43:42 +000090int SYSDEPS_SpawnThread( TEB *teb )
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000091{
Alexandre Julliard598412e2001-01-17 20:22:22 +000092#ifdef ERRNO_LOCATION
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000093
Bang Jun-Young289a2522001-06-15 19:43:51 +000094#if defined(linux) || defined(HAVE_CLONE)
Alexandre Julliard31741742000-03-28 18:47:24 +000095 const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
96 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000097 return -1;
Alexandre Julliard8859d772001-03-01 22:13:49 +000098 if (!(flags & CLONE_FILES)) close( teb->request_fd ); /* close the child socket in the parent */
Ulrich Weiganda44f9f81999-04-18 13:20:43 +000099 return 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000100#endif
101
102#ifdef HAVE_RFORK
Alexandre Julliard31741742000-03-28 18:47:24 +0000103 const int flags = RFPROC | RFMEM; /*|RFFDG*/
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000104 void **sp = (void **)teb->stack_top;
105 *--sp = teb;
Marcus Meissnerc2606381999-04-11 15:20:29 +0000106 *--sp = 0;
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000107 *--sp = SYSDEPS_StartThread;
108 __asm__ __volatile__(
Alexandre Julliard31741742000-03-28 18:47:24 +0000109 "pushl %2;\n\t" /* flags */
Marcus Meissnerc2606381999-04-11 15:20:29 +0000110 "pushl $0;\n\t" /* 0 ? */
111 "movl %1,%%eax;\n\t" /* SYS_rfork */
112 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
113 "cmpl $0, %%edx;\n\t"
114 "je 1f;\n\t"
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000115 "movl %0,%%esp;\n\t" /* child -> new thread */
Marcus Meissnerc2606381999-04-11 15:20:29 +0000116 "ret;\n"
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000117 "1:\n\t" /* parent -> caller thread */
Marcus Meissnerc2606381999-04-11 15:20:29 +0000118 "addl $8,%%esp" :
Alexandre Julliard31741742000-03-28 18:47:24 +0000119 : "r" (sp), "g" (SYS_rfork), "g" (flags)
Marcus Meissnerc2606381999-04-11 15:20:29 +0000120 : "eax", "edx");
Alexandre Julliard8859d772001-03-01 22:13:49 +0000121 if (flags & RFFDG) close( teb->request_fd ); /* close the child socket in the parent */
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000122 return 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000123#endif
124
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000125#ifdef HAVE__LWP_CREATE
126 ucontext_t context;
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000127 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
128 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000129 if ( _lwp_create( &context, 0, NULL ) )
130 return -1;
131 return 0;
132#endif
133
Alexandre Julliard598412e2001-01-17 20:22:22 +0000134#endif /* ERRNO_LOCATION */
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000135
Alexandre Julliard15657091999-05-23 10:25:25 +0000136 FIXME("CreateThread: stub\n" );
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000137 return 0;
138}
139
140
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000141
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000142/***********************************************************************
143 * SYSDEPS_ExitThread
144 *
145 * Exit a running thread; must not return.
146 */
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000147void SYSDEPS_ExitThread( int status )
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000148{
Alexandre Julliard8859d772001-03-01 22:13:49 +0000149 int fd = NtCurrentTeb()->request_fd;
150 NtCurrentTeb()->request_fd = -1;
151 close( fd );
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000152#ifdef HAVE__LWP_CREATE
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000153 _lwp_exit();
154#endif
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000155 _exit( status );
Patrik Stridvall3b233622000-03-24 21:19:02 +0000156 /*
157 * It is of course impossible to come here,
158 * but it eliminates a compiler warning.
159 */
160 exit( status );
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000161}
162
163
Ulrich Weigand7e85ea72000-06-04 01:35:43 +0000164/***********************************************************************
165 * SYSDEPS_CallOnStack
166 */
Ulrich Weigand06bf0172000-06-08 00:39:59 +0000167int SYSDEPS_DoCallOnStack( int (*func)(LPVOID), LPVOID arg )
Ulrich Weigand7e85ea72000-06-04 01:35:43 +0000168{
169 int retv = 0;
170
171 __TRY
172 {
173 retv = func( arg );
174 }
175 __EXCEPT(UnhandledExceptionFilter)
176 {
177 TerminateThread( GetCurrentThread(), GetExceptionCode() );
178 return 0;
179 }
180 __ENDTRY
181
182 return retv;
183}
184
185#ifdef __i386__
186int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
187 int (*func)(LPVOID), LPVOID arg );
188__ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
189 "pushl %ebp\n\t"
190 "movl %esp, %ebp\n\t"
191 ".byte 0x64; pushl 0x04\n\t"
192 ".byte 0x64; pushl 0x08\n\t"
193 "movl 8(%ebp), %esp\n\t"
194 "movl 12(%ebp), %eax\n\t"
195 ".byte 0x64; movl %esp, 0x04\n\t"
196 ".byte 0x64; movl %eax, 0x08\n\t"
197 "pushl 20(%ebp)\n\t"
198 "pushl 16(%ebp)\n\t"
199 "call " __ASM_NAME("SYSDEPS_DoCallOnStack") "\n\t"
200 "leal -8(%ebp), %esp\n\t"
201 ".byte 0x64; popl 0x08\n\t"
202 ".byte 0x64; popl 0x04\n\t"
203 "popl %ebp\n\t"
204 "ret" );
205#else
206int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
207 int (*func)(LPVOID), LPVOID arg )
208{
209 return SYSDEPS_DoCallOnStack( func, arg );
210}
211#endif
212
213/***********************************************************************
214 * SYSDEPS_SwitchToThreadStack
215 */
Ulrich Weigand7e85ea72000-06-04 01:35:43 +0000216void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
217{
Alexandre Julliard6c8edaa2000-12-13 20:22:47 +0000218 DWORD page_size = getpagesize();
Alexandre Julliard062f5332001-06-11 20:13:48 +0000219 void *cur_stack = (void *)(((ULONG_PTR)&func + (page_size-1)) & ~(page_size-1));
Ulrich Weigand06bf0172000-06-08 00:39:59 +0000220
Ulrich Weigand7e85ea72000-06-04 01:35:43 +0000221 TEB *teb = NtCurrentTeb();
222 LPVOID stackTop = teb->stack_top;
223 LPVOID stackLow = teb->stack_low;
224
225 struct rlimit rl;
226
227 if ( getrlimit(RLIMIT_STACK, &rl) < 0 )
228 {
229 WARN("Can't get rlimit\n");
230 rl.rlim_cur = 8*1024*1024;
231 }
232
Alexandre Julliard062f5332001-06-11 20:13:48 +0000233 teb->stack_top = cur_stack;
234 teb->stack_low = (char *)cur_stack - rl.rlim_cur;
Ulrich Weigand06bf0172000-06-08 00:39:59 +0000235
Ulrich Weigand7e85ea72000-06-04 01:35:43 +0000236 SYSDEPS_CallOnStack( stackTop, stackLow,
237 (int (*)(void *))func, NULL );
238}
239
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000240/**********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000241 * NtCurrentTeb (NTDLL.@)
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000242 *
243 * This will crash and burn if called before threading is initialized
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000244 */
Alexandre Julliard916f9752000-02-26 16:51:13 +0000245#ifdef __i386__
246__ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
247#elif defined(HAVE__LWP_CREATE)
Patrik Stridvalldb923052001-07-24 00:58:52 +0000248/***********************************************************************
249 * NtCurrentTeb (NTDLL.@)
250 */
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000251struct _TEB * WINAPI NtCurrentTeb(void)
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000252{
Alexandre Julliard916f9752000-02-26 16:51:13 +0000253 extern void *_lwp_getprivate(void);
254 return (struct _TEB *)_lwp_getprivate();
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000255}
Alexandre Julliard916f9752000-02-26 16:51:13 +0000256#else
257# error NtCurrentTeb not defined for this architecture
258#endif /* __i386__ */