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>
23 #include "wine/port.h"
27 #include "wine/exception.h"
28 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(thread
);
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
42 # endif /* CLONE_VM */
45 /***********************************************************************
46 * SYSDEPS_SetCurThread
48 * Make 'thread' the current thread.
50 void SYSDEPS_SetCurThread( TEB
*teb
)
53 /* On the i386, the current thread is in the %fs register */
54 __set_fs( teb
->teb_sel
);
55 #elif defined(HAVE__LWP_CREATE)
56 /* On non-i386 Solaris, we use the LWP private pointer */
57 _lwp_setprivate( teb
);
61 /***********************************************************************
64 * Startup routine for a new thread.
66 static void SYSDEPS_StartThread( TEB
*teb
)
68 SYSDEPS_SetCurThread( teb
);
75 __EXCEPT(UnhandledExceptionFilter
)
77 TerminateThread( GetCurrentThread(), GetExceptionCode() );
80 SYSDEPS_ExitThread(0); /* should never get here */
84 /***********************************************************************
87 * Start running a new thread.
88 * Return -1 on error, 0 if OK.
90 int SYSDEPS_SpawnThread( TEB
*teb
)
95 const int flags
= CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
;
96 if (clone( (int (*)(void *))SYSDEPS_StartThread
, teb
->stack_top
, flags
, teb
) < 0)
98 if (!(flags
& CLONE_FILES
)) close( teb
->socket
); /* close the child socket in the parent */
103 const int flags
= RFPROC
| RFMEM
; /*|RFFDG*/
104 void **sp
= (void **)teb
->stack_top
;
107 *--sp
= SYSDEPS_StartThread
;
108 __asm__
__volatile__(
109 "pushl %2;\n\t" /* flags */
110 "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"
115 "movl %0,%%esp;\n\t" /* child -> new thread */
117 "1:\n\t" /* parent -> caller thread */
119 : "r" (sp
), "g" (SYS_rfork
), "g" (flags
)
121 if (flags
& RFFDG
) close( teb
->socket
); /* close the child socket in the parent */
125 #ifdef HAVE__LWP_CREATE
127 _lwp_makecontext( &context
, (void(*)(void *))SYSDEPS_StartThread
, teb
,
128 NULL
, teb
->stack_base
, (char *)teb
->stack_top
- (char *)teb
->stack_base
);
129 if ( _lwp_create( &context
, 0, NULL
) )
134 #endif /* ERRNO_LOCATION */
136 FIXME("CreateThread: stub\n" );
142 /***********************************************************************
145 * Exit a running thread; must not return.
147 void SYSDEPS_ExitThread( int status
)
149 int socket
= NtCurrentTeb()->socket
;
150 NtCurrentTeb()->socket
= -1;
152 #ifdef HAVE__LWP_CREATE
157 * It is of course impossible to come here,
158 * but it eliminates a compiler warning.
164 /***********************************************************************
165 * SYSDEPS_CallOnStack
167 int SYSDEPS_DoCallOnStack( int (*func
)(LPVOID
), LPVOID arg
)
175 __EXCEPT(UnhandledExceptionFilter
)
177 TerminateThread( GetCurrentThread(), GetExceptionCode() );
186 int SYSDEPS_CallOnStack( LPVOID stackTop
, LPVOID stackLow
,
187 int (*func
)(LPVOID
), LPVOID arg
);
188 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack
,
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"
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"
206 int SYSDEPS_CallOnStack( LPVOID stackTop
, LPVOID stackLow
,
207 int (*func
)(LPVOID
), LPVOID arg
)
209 return SYSDEPS_DoCallOnStack( func
, arg
);
213 /***********************************************************************
214 * SYSDEPS_SwitchToThreadStack
216 void SYSDEPS_SwitchToThreadStack( void (*func
)(void) )
218 DWORD page_size
= getpagesize();
219 DWORD cur_stack
= (((DWORD
)&func
) + (page_size
-1)) & ~(page_size
-1);
221 TEB
*teb
= NtCurrentTeb();
222 LPVOID stackTop
= teb
->stack_top
;
223 LPVOID stackLow
= teb
->stack_low
;
227 if ( getrlimit(RLIMIT_STACK
, &rl
) < 0 )
229 WARN("Can't get rlimit\n");
230 rl
.rlim_cur
= 8*1024*1024;
233 teb
->stack_top
= (LPVOID
) cur_stack
;
234 teb
->stack_low
= (LPVOID
)(cur_stack
- rl
.rlim_cur
);
236 SYSDEPS_CallOnStack( stackTop
, stackLow
,
237 (int (*)(void *))func
, NULL
);
240 /**********************************************************************
241 * NtCurrentTeb (NTDLL.@)
243 * This will crash and burn if called before threading is initialized
246 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
247 #elif defined(HAVE__LWP_CREATE)
248 struct _TEB
* WINAPI
NtCurrentTeb(void)
250 extern void *_lwp_getprivate(void);
251 return (struct _TEB
*)_lwp_getprivate();
254 # error NtCurrentTeb not defined for this architecture
255 #endif /* __i386__ */