2 * System-dependent scheduler support
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
32 #ifdef HAVE_SYS_SYSCALL_H
33 # include <sys/syscall.h>
38 #ifdef HAVE_UCONTEXT_H
39 # include <ucontext.h>
41 #ifdef HAVE_SYS_MMAN_H
47 #ifdef HAVE_VALGRIND_MEMCHECK_H
48 #include <valgrind/memcheck.h>
56 #include "wine/server.h"
58 #include "wine/winbase16.h"
59 #include "wine/exception.h"
60 #include "wine/library.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
65 struct thread_cleanup_info
72 /* temporary stacks used on thread exit */
73 #define TEMP_STACK_SIZE 1024
74 #define NB_TEMP_STACKS 8
75 static char temp_stacks
[NB_TEMP_STACKS
][TEMP_STACK_SIZE
];
76 static LONG next_temp_stack
; /* next temp stack to use */
78 /***********************************************************************
79 * SYSDEPS_SetCurThread
81 * Make 'thread' the current thread.
83 void SYSDEPS_SetCurThread( TEB
*teb
)
86 /* On the i386, the current thread is in the %fs register */
89 wine_ldt_set_base( &fs_entry
, teb
);
90 wine_ldt_set_limit( &fs_entry
, 0xfff );
91 wine_ldt_set_flags( &fs_entry
, WINE_LDT_FLAGS_DATA
|WINE_LDT_FLAGS_32BIT
);
92 wine_ldt_init_fs( teb
->teb_sel
, &fs_entry
);
93 #elif defined(__powerpc__)
94 /* On PowerPC, the current TEB is in the gpr13 register */
95 __asm__
__volatile__("mr 2, %0" : : "r" (teb
));
96 #elif defined(HAVE__LWP_CREATE)
97 /* On non-i386 Solaris, we use the LWP private pointer */
98 _lwp_setprivate( teb
);
103 /***********************************************************************
104 * call_on_thread_stack
106 * Call a function once we switched to the thread stack.
108 static void call_on_thread_stack( void *func
)
112 void (*funcptr
)(void) = func
;
115 __EXCEPT(UnhandledExceptionFilter
)
117 TerminateThread( GetCurrentThread(), GetExceptionCode() );
120 SYSDEPS_ExitThread(0); /* should never get here */
124 /***********************************************************************
127 * Get a temporary stack address to run the thread exit code on.
129 inline static char *get_temp_stack(void)
131 unsigned int next
= InterlockedExchangeAdd( &next_temp_stack
, 1 );
132 return temp_stacks
[next
% NB_TEMP_STACKS
];
136 /***********************************************************************
139 * Cleanup the remains of a thread. Runs on a temporary stack.
141 static void cleanup_thread( void *ptr
)
143 /* copy the info structure since it is on the stack we will free */
144 struct thread_cleanup_info info
= *(struct thread_cleanup_info
*)ptr
;
145 munmap( info
.stack_base
, info
.stack_size
);
146 wine_ldt_free_fs( wine_get_fs() );
147 #ifdef HAVE__LWP_CREATE
150 _exit( info
.status
);
154 /***********************************************************************
155 * SYSDEPS_StartThread
157 * Startup routine for a new thread.
159 static void SYSDEPS_StartThread( TEB
*teb
)
161 SYSDEPS_SetCurThread( teb
);
168 __EXCEPT(UnhandledExceptionFilter
)
170 TerminateThread( GetCurrentThread(), GetExceptionCode() );
173 SYSDEPS_ExitThread(0); /* should never get here */
177 /***********************************************************************
178 * SYSDEPS_SpawnThread
180 * Start running a new thread.
181 * Return -1 on error, 0 if OK.
183 int SYSDEPS_SpawnThread( TEB
*teb
)
189 pthread_attr_init( &attr
);
190 pthread_attr_setdetachstate( &attr
, PTHREAD_CREATE_DETACHED
);
191 pthread_attr_setstack( &attr
, teb
->stack_base
, (char *)teb
->stack_top
- (char *)teb
->stack_base
);
192 if (pthread_create( &id
, &attr
, (void * (*)(void *))SYSDEPS_StartThread
, teb
)) return -1;
194 #elif defined(HAVE_CLONE)
195 if (clone( (int (*)(void *))SYSDEPS_StartThread
, teb
->stack_top
,
196 CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
, teb
) < 0)
199 #elif defined(HAVE_RFORK)
200 void **sp
= (void **)teb
->stack_top
;
203 *--sp
= SYSDEPS_StartThread
;
204 __asm__
__volatile__(
205 "pushl %2;\n\t" /* flags */
206 "pushl $0;\n\t" /* 0 ? */
207 "movl %1,%%eax;\n\t" /* SYS_rfork */
208 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
209 "cmpl $0, %%edx;\n\t"
211 "movl %0,%%esp;\n\t" /* child -> new thread */
213 "1:\n\t" /* parent -> caller thread */
215 : "r" (sp
), "g" (SYS_rfork
), "g" (RFPROC
| RFMEM
)
218 #elif defined(HAVE__LWP_CREATE)
220 _lwp_makecontext( &context
, (void(*)(void *))SYSDEPS_StartThread
, teb
,
221 NULL
, teb
->stack_base
, (char *)teb
->stack_top
- (char *)teb
->stack_base
);
222 if ( _lwp_create( &context
, 0, NULL
) )
227 FIXME("CreateThread: stub\n" );
232 /***********************************************************************
233 * SYSDEPS_CallOnStack
235 void DECLSPEC_NORETURN
SYSDEPS_CallOnStack( void (*func
)(LPVOID
), LPVOID arg
);
238 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack
,
239 "movl 4(%esp),%ecx\n\t" /* func */
240 "movl 8(%esp),%edx\n\t" /* arg */
241 ".byte 0x64\n\tmovl 0x04,%esp\n\t" /* teb->stack_top */
245 "int $3" /* we never return here */ );
246 # elif defined(_MSC_VER)
247 __declspec(naked
) void SYSDEPS_CallOnStack( void (*func
)(LPVOID
), LPVOID arg
)
249 __asm mov ecx
, 4[esp
];
250 __asm mov edx
, 8[esp
];
251 __asm mov fs
:[0x04], esp
;
257 # endif /* defined(__GNUC__) || defined(_MSC_VER) */
258 #elif defined(__sparc__) && defined(__GNUC__)
259 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack
,
260 "mov %o0, %l0\n\t" /* store first argument */
261 "call " __ASM_NAME("NtCurrentTeb") ", 0\n\t"
262 "mov %o1, %l1\n\t" /* delay slot: store second argument */
263 "ld [%o0+4], %sp\n\t" /* teb->stack_top */
264 "call %l0, 0\n\t" /* call func */
265 "mov %l1, %o0\n\t" /* delay slot: arg for func */
266 "ta 0x01\n\t"); /* breakpoint - we never get here */
267 #else /* !sparc, !i386 */
268 void SYSDEPS_CallOnStack( void (*func
)(LPVOID
), LPVOID arg
)
271 while(1); /* avoid warning */
273 #endif /* !defined(__i386__) && !defined(__sparc__) */
276 /***********************************************************************
277 * SYSDEPS_SwitchToThreadStack
279 void SYSDEPS_SwitchToThreadStack( void (*func
)(void) )
281 SYSDEPS_CallOnStack( call_on_thread_stack
, func
);
285 /***********************************************************************
288 * Exit a running thread; must not return.
290 void SYSDEPS_ExitThread( int status
)
292 TEB
*teb
= NtCurrentTeb();
293 struct thread_cleanup_info info
;
294 MEMORY_BASIC_INFORMATION meminfo
;
296 VirtualQuery( teb
->stack_top
, &meminfo
, sizeof(meminfo
) );
297 info
.stack_base
= meminfo
.AllocationBase
;
298 info
.stack_size
= meminfo
.RegionSize
+ ((char *)teb
->stack_top
- (char *)meminfo
.AllocationBase
);
299 info
.status
= status
;
304 SYSDEPS_AbortThread( status
);
307 VirtualFree( teb
->stack_base
, 0, MEM_RELEASE
| MEM_SYSTEM
);
308 close( teb
->wait_fd
[0] );
309 close( teb
->wait_fd
[1] );
310 close( teb
->reply_fd
);
311 close( teb
->request_fd
);
312 teb
->stack_low
= get_temp_stack();
313 teb
->stack_top
= (char *) teb
->stack_low
+ TEMP_STACK_SIZE
;
314 SYSDEPS_CallOnStack( cleanup_thread
, &info
);
319 /***********************************************************************
320 * SYSDEPS_AbortThread
322 * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
324 void SYSDEPS_AbortThread( int status
)
327 close( NtCurrentTeb()->wait_fd
[0] );
328 close( NtCurrentTeb()->wait_fd
[1] );
329 close( NtCurrentTeb()->reply_fd
);
330 close( NtCurrentTeb()->request_fd
);
332 pthread_exit( (void *)status
);
335 #ifdef HAVE__LWP_CREATE
338 for (;;) /* avoid warning */
342 /***********************************************************************
345 * Get the Unix tid of the current thread.
347 int SYSDEPS_GetUnixTid(void)
349 #ifdef HAVE__LWP_SELF
351 #elif defined(__linux__) && defined(__i386__)
353 __asm__("int $0x80" : "=a" (ret
) : "0" (224) /* SYS_gettid */);
354 if (ret
< 0) ret
= -1;
364 /* default errno before threading is initialized */
365 static int *default_errno_location(void)
367 static int static_errno
;
368 return &static_errno
;
371 /* default h_errno before threading is initialized */
372 static int *default_h_errno_location(void)
374 static int static_h_errno
;
375 return &static_h_errno
;
378 /* errno once threading is working */
379 static int *thread_errno_location(void)
381 return &NtCurrentTeb()->thread_errno
;
384 /* h_errno once threading is working */
385 static int *thread_h_errno_location(void)
387 return &NtCurrentTeb()->thread_h_errno
;
390 static int* (*errno_location_ptr
)(void) = default_errno_location
;
391 static int* (*h_errno_location_ptr
)(void) = default_h_errno_location
;
393 /***********************************************************************
394 * __errno_location/__error/__errno/___errno/__thr_errno
396 * Get the per-thread errno location.
398 int *__errno_location(void) { return errno_location_ptr(); } /* Linux */
399 int *__error(void) { return errno_location_ptr(); } /* FreeBSD */
400 int *__errno(void) { return errno_location_ptr(); } /* NetBSD */
401 int *___errno(void) { return errno_location_ptr(); } /* Solaris */
402 int *__thr_errno(void) { return errno_location_ptr(); } /* UnixWare */
404 /***********************************************************************
407 * Get the per-thread h_errno location.
409 int *__h_errno_location(void)
411 return h_errno_location_ptr();
414 #endif /* HAVE_NPTL */
417 #if defined(__linux__) && defined(__i386__)
418 static inline void writejump( const char *symbol
, void *dest
)
420 unsigned char *addr
= wine_dlsym( RTLD_NEXT
, symbol
, NULL
, 0 );
424 /* write a relative jump at the function address */
425 mprotect((void*)((unsigned int)addr
& ~(getpagesize()-1)), 5, PROT_READ
|PROT_EXEC
|PROT_WRITE
);
427 *(int *)(addr
+1) = (unsigned char *)dest
- (addr
+ 5);
428 mprotect((void*)((unsigned int)addr
& ~(getpagesize()-1)), 5, PROT_READ
|PROT_EXEC
);
430 #ifdef HAVE_VALGRIND_MEMCHECK_H
431 VALGRIND_DISCARD_TRANSLATIONS( addr
, 5 );
436 /***********************************************************************
439 * Initialize errno handling.
441 void SYSDEPS_InitErrno(void)
444 errno_location_ptr
= thread_errno_location
;
445 h_errno_location_ptr
= thread_h_errno_location
;
447 # if defined(__linux__) && defined(__i386__)
448 writejump( "__errno_location", thread_errno_location
);
449 writejump( "__h_errno_location", thread_h_errno_location
);
451 #endif /* HAVE_NPTL */
455 /**********************************************************************
456 * NtCurrentTeb (NTDLL.@)
458 * This will crash and burn if called before threading is initialized
460 #if defined(__i386__) && defined(__GNUC__)
461 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
462 #elif defined(__i386__) && defined(_MSC_VER)
463 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
464 #elif defined(HAVE__LWP_CREATE)
465 /***********************************************************************
466 * NtCurrentTeb (NTDLL.@)
468 struct _TEB
* WINAPI
NtCurrentTeb(void)
470 extern void *_lwp_getprivate(void);
471 return (struct _TEB
*)_lwp_getprivate();
473 #elif defined(__powerpc__)
474 __ASM_GLOBAL_FUNC( NtCurrentTeb
, "\n\tmr 3,2\n\tblr" );
476 # error NtCurrentTeb not defined for this architecture
477 #endif /* __i386__ */