blob: b931e779721bf5f9c38aa5fa7f7130311fd89f37 [file] [log] [blame]
Alexandre Julliard0623a6f1998-01-18 18:01:49 +00001/*
2 * System-dependent scheduler support
3 *
4 * Copyright 1998 Alexandre Julliard
5 */
6
Francois Gouget386cf6e2001-10-14 16:25:47 +00007#include "wine/port.h"
Marcus Meissnerc2606381999-04-11 15:20:29 +00008
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
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000023#include "thread.h"
Alexandre Julliard37e95032001-07-19 00:39:09 +000024#include "wine/server.h"
Alexandre Julliard03468f71998-02-15 19:40:49 +000025#include "winbase.h"
Alexandre Julliard3d06d201999-09-27 10:58:45 +000026#include "wine/exception.h"
Alexandre Julliard15657091999-05-23 10:25:25 +000027#include "debugtools.h"
Alexandre Julliard03468f71998-02-15 19:40:49 +000028
Dimitrie O. Paun529da542000-11-27 23:54:25 +000029DEFAULT_DEBUG_CHANNEL(thread);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000030
Bang Jun-Young289a2522001-06-15 19:43:51 +000031#if defined(linux) || defined(HAVE_CLONE)
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000032# ifdef HAVE_SCHED_H
33# include <sched.h>
34# endif
35# ifndef CLONE_VM
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
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000041# endif /* CLONE_VM */
Bang Jun-Young289a2522001-06-15 19:43:51 +000042#endif /* linux || HAVE_CLONE */
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000043
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000044/***********************************************************************
Alexandre Julliard54a39e21999-05-29 14:27:26 +000045 * SYSDEPS_SetCurThread
46 *
47 * Make 'thread' the current thread.
48 */
Alexandre Julliard0a860a01999-06-22 11:43:42 +000049void SYSDEPS_SetCurThread( TEB *teb )
Alexandre Julliard54a39e21999-05-29 14:27:26 +000050{
Ulrich Weigandcebd60b1999-09-03 16:45:44 +000051#if defined(__i386__)
Alexandre Julliard54a39e21999-05-29 14:27:26 +000052 /* On the i386, the current thread is in the %fs register */
Alexandre Julliard916f9752000-02-26 16:51:13 +000053 __set_fs( teb->teb_sel );
Ulrich Weigandcebd60b1999-09-03 16:45:44 +000054#elif defined(HAVE__LWP_CREATE)
55 /* On non-i386 Solaris, we use the LWP private pointer */
56 _lwp_setprivate( teb );
57#endif
Alexandre Julliard54a39e21999-05-29 14:27:26 +000058}
59
60/***********************************************************************
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000061 * SYSDEPS_StartThread
62 *
63 * Startup routine for a new thread.
64 */
Alexandre Julliard0a860a01999-06-22 11:43:42 +000065static void SYSDEPS_StartThread( TEB *teb )
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000066{
Alexandre Julliard0a860a01999-06-22 11:43:42 +000067 SYSDEPS_SetCurThread( teb );
Alexandre Julliard875c4b31999-03-23 14:09:41 +000068 CLIENT_InitThread();
Alexandre Julliard3d06d201999-09-27 10:58:45 +000069 SIGNAL_Init();
70 __TRY
71 {
72 teb->startup();
73 }
74 __EXCEPT(UnhandledExceptionFilter)
75 {
76 TerminateThread( GetCurrentThread(), GetExceptionCode() );
77 }
78 __ENDTRY
Alexandre Julliard12f29b52000-03-17 15:16:57 +000079 SYSDEPS_ExitThread(0); /* should never get here */
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000080}
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000081
82
83/***********************************************************************
84 * SYSDEPS_SpawnThread
85 *
86 * Start running a new thread.
87 * Return -1 on error, 0 if OK.
88 */
Alexandre Julliard0a860a01999-06-22 11:43:42 +000089int SYSDEPS_SpawnThread( TEB *teb )
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000090{
Alexandre Julliard598412e2001-01-17 20:22:22 +000091#ifdef ERRNO_LOCATION
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000092
Bang Jun-Young289a2522001-06-15 19:43:51 +000093#if defined(linux) || defined(HAVE_CLONE)
Alexandre Julliard31741742000-03-28 18:47:24 +000094 const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
95 if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
Alexandre Julliard0623a6f1998-01-18 18:01:49 +000096 return -1;
Alexandre Julliard8859d772001-03-01 22:13:49 +000097 if (!(flags & CLONE_FILES)) close( teb->request_fd ); /* close the child socket in the parent */
Ulrich Weiganda44f9f81999-04-18 13:20:43 +000098 return 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000099#endif
100
101#ifdef HAVE_RFORK
Alexandre Julliard31741742000-03-28 18:47:24 +0000102 const int flags = RFPROC | RFMEM; /*|RFFDG*/
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000103 void **sp = (void **)teb->stack_top;
104 *--sp = teb;
Marcus Meissnerc2606381999-04-11 15:20:29 +0000105 *--sp = 0;
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000106 *--sp = SYSDEPS_StartThread;
107 __asm__ __volatile__(
Alexandre Julliard31741742000-03-28 18:47:24 +0000108 "pushl %2;\n\t" /* flags */
Marcus Meissnerc2606381999-04-11 15:20:29 +0000109 "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"
113 "je 1f;\n\t"
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000114 "movl %0,%%esp;\n\t" /* child -> new thread */
Marcus Meissnerc2606381999-04-11 15:20:29 +0000115 "ret;\n"
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000116 "1:\n\t" /* parent -> caller thread */
Marcus Meissnerc2606381999-04-11 15:20:29 +0000117 "addl $8,%%esp" :
Alexandre Julliard31741742000-03-28 18:47:24 +0000118 : "r" (sp), "g" (SYS_rfork), "g" (flags)
Marcus Meissnerc2606381999-04-11 15:20:29 +0000119 : "eax", "edx");
Alexandre Julliard8859d772001-03-01 22:13:49 +0000120 if (flags & RFFDG) close( teb->request_fd ); /* close the child socket in the parent */
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000121 return 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000122#endif
123
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000124#ifdef HAVE__LWP_CREATE
125 ucontext_t context;
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000126 _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
127 NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000128 if ( _lwp_create( &context, 0, NULL ) )
129 return -1;
130 return 0;
131#endif
132
Alexandre Julliard598412e2001-01-17 20:22:22 +0000133#endif /* ERRNO_LOCATION */
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000134
Alexandre Julliard15657091999-05-23 10:25:25 +0000135 FIXME("CreateThread: stub\n" );
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000136 return 0;
137}
138
139
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000140
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000141/***********************************************************************
142 * SYSDEPS_ExitThread
143 *
144 * Exit a running thread; must not return.
145 */
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000146void SYSDEPS_ExitThread( int status )
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000147{
Alexandre Julliard8859d772001-03-01 22:13:49 +0000148 int fd = NtCurrentTeb()->request_fd;
149 NtCurrentTeb()->request_fd = -1;
150 close( fd );
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000151#ifdef HAVE__LWP_CREATE
Ulrich Weiganda44f9f81999-04-18 13:20:43 +0000152 _lwp_exit();
153#endif
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000154 _exit( status );
Patrik Stridvall3b233622000-03-24 21:19:02 +0000155 /*
156 * It is of course impossible to come here,
157 * but it eliminates a compiler warning.
158 */
159 exit( status );
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000160}
161
162
Ulrich Weigand7e85ea72000-06-04 01:35:43 +0000163/***********************************************************************
164 * SYSDEPS_CallOnStack
165 */
Ulrich Weigand06bf0172000-06-08 00:39:59 +0000166int SYSDEPS_DoCallOnStack( int (*func)(LPVOID), LPVOID arg )
Ulrich Weigand7e85ea72000-06-04 01:35:43 +0000167{
168 int retv = 0;
169
170 __TRY
171 {
172 retv = func( arg );
173 }
174 __EXCEPT(UnhandledExceptionFilter)
175 {
176 TerminateThread( GetCurrentThread(), GetExceptionCode() );
177 return 0;
178 }
179 __ENDTRY
180
181 return retv;
182}
183
184#ifdef __i386__
185int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
186 int (*func)(LPVOID), LPVOID arg );
187__ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack,
188 "pushl %ebp\n\t"
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"
196 "pushl 20(%ebp)\n\t"
197 "pushl 16(%ebp)\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"
202 "popl %ebp\n\t"
203 "ret" );
204#else
205int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow,
206 int (*func)(LPVOID), LPVOID arg )
207{
208 return SYSDEPS_DoCallOnStack( func, arg );
209}
210#endif
211
212/***********************************************************************
213 * SYSDEPS_SwitchToThreadStack
214 */
Ulrich Weigand7e85ea72000-06-04 01:35:43 +0000215void SYSDEPS_SwitchToThreadStack( void (*func)(void) )
216{
Alexandre Julliard6c8edaa2000-12-13 20:22:47 +0000217 DWORD page_size = getpagesize();
Alexandre Julliard062f5332001-06-11 20:13:48 +0000218 void *cur_stack = (void *)(((ULONG_PTR)&func + (page_size-1)) & ~(page_size-1));
Ulrich Weigand06bf0172000-06-08 00:39:59 +0000219
Ulrich Weigand7e85ea72000-06-04 01:35:43 +0000220 TEB *teb = NtCurrentTeb();
221 LPVOID stackTop = teb->stack_top;
222 LPVOID stackLow = teb->stack_low;
223
224 struct rlimit rl;
225
226 if ( getrlimit(RLIMIT_STACK, &rl) < 0 )
227 {
228 WARN("Can't get rlimit\n");
229 rl.rlim_cur = 8*1024*1024;
230 }
231
Alexandre Julliard062f5332001-06-11 20:13:48 +0000232 teb->stack_top = cur_stack;
233 teb->stack_low = (char *)cur_stack - rl.rlim_cur;
Ulrich Weigand06bf0172000-06-08 00:39:59 +0000234
Ulrich Weigand7e85ea72000-06-04 01:35:43 +0000235 SYSDEPS_CallOnStack( stackTop, stackLow,
236 (int (*)(void *))func, NULL );
237}
238
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000239/**********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000240 * NtCurrentTeb (NTDLL.@)
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000241 *
242 * This will crash and burn if called before threading is initialized
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000243 */
Alexandre Julliard916f9752000-02-26 16:51:13 +0000244#ifdef __i386__
245__ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
246#elif defined(HAVE__LWP_CREATE)
Patrik Stridvalldb923052001-07-24 00:58:52 +0000247/***********************************************************************
248 * NtCurrentTeb (NTDLL.@)
249 */
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000250struct _TEB * WINAPI NtCurrentTeb(void)
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000251{
Alexandre Julliard916f9752000-02-26 16:51:13 +0000252 extern void *_lwp_getprivate(void);
253 return (struct _TEB *)_lwp_getprivate();
Alexandre Julliard0623a6f1998-01-18 18:01:49 +0000254}
Alexandre Julliard916f9752000-02-26 16:51:13 +0000255#else
256# error NtCurrentTeb not defined for this architecture
257#endif /* __i386__ */