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
53 #include "wine/server.h"
55 #include "wine/winbase16.h"
56 #include "wine/exception.h"
57 #include "wine/library.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
62 struct thread_cleanup_info
69 /* temporary stacks used on thread exit */
70 #define TEMP_STACK_SIZE 1024
71 #define NB_TEMP_STACKS 8
72 static char temp_stacks
[NB_TEMP_STACKS
][TEMP_STACK_SIZE
];
73 static LONG next_temp_stack
; /* next temp stack to use */
75 /***********************************************************************
76 * SYSDEPS_SetCurThread
78 * Make 'thread' the current thread.
80 void SYSDEPS_SetCurThread( TEB
*teb
)
83 /* On the i386, the current thread is in the %fs register */
86 wine_ldt_set_base( &fs_entry
, teb
);
87 wine_ldt_set_limit( &fs_entry
, 0xfff );
88 wine_ldt_set_flags( &fs_entry
, WINE_LDT_FLAGS_DATA
|WINE_LDT_FLAGS_32BIT
);
89 wine_ldt_init_fs( teb
->teb_sel
, &fs_entry
);
90 #elif defined(__powerpc__)
91 /* On PowerPC, the current TEB is in the gpr13 register */
92 __asm__
__volatile__("mr 2, %0" : : "r" (teb
));
93 #elif defined(HAVE__LWP_CREATE)
94 /* On non-i386 Solaris, we use the LWP private pointer */
95 _lwp_setprivate( teb
);
100 /***********************************************************************
101 * call_on_thread_stack
103 * Call a function once we switched to the thread stack.
105 static void call_on_thread_stack( void *func
)
109 void (*funcptr
)(void) = func
;
112 __EXCEPT(UnhandledExceptionFilter
)
114 TerminateThread( GetCurrentThread(), GetExceptionCode() );
117 SYSDEPS_ExitThread(0); /* should never get here */
121 /***********************************************************************
124 * Get a temporary stack address to run the thread exit code on.
126 inline static char *get_temp_stack(void)
128 unsigned int next
= InterlockedExchangeAdd( &next_temp_stack
, 1 );
129 return temp_stacks
[next
% NB_TEMP_STACKS
];
133 /***********************************************************************
136 * Cleanup the remains of a thread. Runs on a temporary stack.
138 static void cleanup_thread( void *ptr
)
140 /* copy the info structure since it is on the stack we will free */
141 struct thread_cleanup_info info
= *(struct thread_cleanup_info
*)ptr
;
142 munmap( info
.stack_base
, info
.stack_size
);
143 wine_ldt_free_fs( wine_get_fs() );
144 #ifdef HAVE__LWP_CREATE
147 _exit( info
.status
);
151 /***********************************************************************
152 * SYSDEPS_StartThread
154 * Startup routine for a new thread.
156 static void SYSDEPS_StartThread( TEB
*teb
)
158 SYSDEPS_SetCurThread( teb
);
165 __EXCEPT(UnhandledExceptionFilter
)
167 TerminateThread( GetCurrentThread(), GetExceptionCode() );
170 SYSDEPS_ExitThread(0); /* should never get here */
174 /***********************************************************************
175 * SYSDEPS_SpawnThread
177 * Start running a new thread.
178 * Return -1 on error, 0 if OK.
180 int SYSDEPS_SpawnThread( TEB
*teb
)
186 pthread_attr_init( &attr
);
187 pthread_attr_setdetachstate( &attr
, PTHREAD_CREATE_DETACHED
);
188 pthread_attr_setstack( &attr
, teb
->stack_base
, (char *)teb
->stack_top
- (char *)teb
->stack_base
);
189 if (pthread_create( &id
, &attr
, (void * (*)(void *))SYSDEPS_StartThread
, teb
)) return -1;
191 #elif defined(HAVE_CLONE)
192 if (clone( (int (*)(void *))SYSDEPS_StartThread
, teb
->stack_top
,
193 CLONE_VM
| CLONE_FS
| CLONE_FILES
| SIGCHLD
, teb
) < 0)
196 #elif defined(HAVE_RFORK)
197 void **sp
= (void **)teb
->stack_top
;
200 *--sp
= SYSDEPS_StartThread
;
201 __asm__
__volatile__(
202 "pushl %2;\n\t" /* flags */
203 "pushl $0;\n\t" /* 0 ? */
204 "movl %1,%%eax;\n\t" /* SYS_rfork */
205 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
206 "cmpl $0, %%edx;\n\t"
208 "movl %0,%%esp;\n\t" /* child -> new thread */
210 "1:\n\t" /* parent -> caller thread */
212 : "r" (sp
), "g" (SYS_rfork
), "g" (RFPROC
| RFMEM
)
215 #elif defined(HAVE__LWP_CREATE)
217 _lwp_makecontext( &context
, (void(*)(void *))SYSDEPS_StartThread
, teb
,
218 NULL
, teb
->stack_base
, (char *)teb
->stack_top
- (char *)teb
->stack_base
);
219 if ( _lwp_create( &context
, 0, NULL
) )
224 FIXME("CreateThread: stub\n" );
229 /***********************************************************************
230 * SYSDEPS_CallOnStack
232 void DECLSPEC_NORETURN
SYSDEPS_CallOnStack( void (*func
)(LPVOID
), LPVOID arg
);
235 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack
,
236 "movl 4(%esp),%ecx\n\t" /* func */
237 "movl 8(%esp),%edx\n\t" /* arg */
238 ".byte 0x64\n\tmovl 0x04,%esp\n\t" /* teb->stack_top */
242 "int $3" /* we never return here */ );
243 # elif defined(_MSC_VER)
244 __declspec(naked
) void SYSDEPS_CallOnStack( void (*func
)(LPVOID
), LPVOID arg
)
246 __asm mov ecx
, 4[esp
];
247 __asm mov edx
, 8[esp
];
248 __asm mov fs
:[0x04], esp
;
254 # endif /* defined(__GNUC__) || defined(_MSC_VER) */
255 #elif defined(__sparc__) && defined(__GNUC__)
256 __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack
,
257 "mov %o0, %l0\n\t" /* store first argument */
258 "call " __ASM_NAME("NtCurrentTeb") ", 0\n\t"
259 "mov %o1, %l1\n\t" /* delay slot: store second argument */
260 "ld [%o0+4], %sp\n\t" /* teb->stack_top */
261 "call %l0, 0\n\t" /* call func */
262 "mov %l1, %o0\n\t" /* delay slot: arg for func */
263 "ta 0x01\n\t"); /* breakpoint - we never get here */
264 #else /* !sparc, !i386 */
265 void SYSDEPS_CallOnStack( void (*func
)(LPVOID
), LPVOID arg
)
268 while(1); /* avoid warning */
270 #endif /* !defined(__i386__) && !defined(__sparc__) */
273 /***********************************************************************
274 * SYSDEPS_SwitchToThreadStack
276 void SYSDEPS_SwitchToThreadStack( void (*func
)(void) )
278 SYSDEPS_CallOnStack( call_on_thread_stack
, func
);
282 /***********************************************************************
285 * Exit a running thread; must not return.
287 void SYSDEPS_ExitThread( int status
)
289 TEB
*teb
= NtCurrentTeb();
290 struct thread_cleanup_info info
;
291 MEMORY_BASIC_INFORMATION meminfo
;
293 wine_ldt_free_entries( teb
->stack_sel
, 1 );
294 VirtualQuery( teb
->stack_top
, &meminfo
, sizeof(meminfo
) );
295 info
.stack_base
= meminfo
.AllocationBase
;
296 info
.stack_size
= meminfo
.RegionSize
+ ((char *)teb
->stack_top
- (char *)meminfo
.AllocationBase
);
297 info
.status
= status
;
302 SYSDEPS_AbortThread( status
);
305 VirtualFree( teb
->stack_base
, 0, MEM_RELEASE
| MEM_SYSTEM
);
306 close( teb
->wait_fd
[0] );
307 close( teb
->wait_fd
[1] );
308 close( teb
->reply_fd
);
309 close( teb
->request_fd
);
310 teb
->stack_low
= get_temp_stack();
311 teb
->stack_top
= (char *) teb
->stack_low
+ TEMP_STACK_SIZE
;
312 SYSDEPS_CallOnStack( cleanup_thread
, &info
);
317 /***********************************************************************
318 * SYSDEPS_AbortThread
320 * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
322 void SYSDEPS_AbortThread( int status
)
325 close( NtCurrentTeb()->wait_fd
[0] );
326 close( NtCurrentTeb()->wait_fd
[1] );
327 close( NtCurrentTeb()->reply_fd
);
328 close( NtCurrentTeb()->request_fd
);
330 pthread_exit( (void *)status
);
333 #ifdef HAVE__LWP_CREATE
336 for (;;) /* avoid warning */
340 /***********************************************************************
343 * Get the Unix tid of the current thread.
345 int SYSDEPS_GetUnixTid(void)
347 #ifdef HAVE__LWP_SELF
349 #elif defined(__linux__) && defined(__i386__)
351 __asm__("int $0x80" : "=a" (ret
) : "0" (224) /* SYS_gettid */);
352 if (ret
< 0) ret
= -1;
362 /* default errno before threading is initialized */
363 static int *default_errno_location(void)
365 static int static_errno
;
366 return &static_errno
;
369 /* default h_errno before threading is initialized */
370 static int *default_h_errno_location(void)
372 static int static_h_errno
;
373 return &static_h_errno
;
376 /* errno once threading is working */
377 static int *thread_errno_location(void)
379 return &NtCurrentTeb()->thread_errno
;
382 /* h_errno once threading is working */
383 static int *thread_h_errno_location(void)
385 return &NtCurrentTeb()->thread_h_errno
;
388 static int* (*errno_location_ptr
)(void) = default_errno_location
;
389 static int* (*h_errno_location_ptr
)(void) = default_h_errno_location
;
391 /***********************************************************************
392 * __errno_location/__error/__errno/___errno/__thr_errno
394 * Get the per-thread errno location.
396 int *__errno_location(void) { return errno_location_ptr(); } /* Linux */
397 int *__error(void) { return errno_location_ptr(); } /* FreeBSD */
398 int *__errno(void) { return errno_location_ptr(); } /* NetBSD */
399 int *___errno(void) { return errno_location_ptr(); } /* Solaris */
400 int *__thr_errno(void) { return errno_location_ptr(); } /* UnixWare */
402 /***********************************************************************
405 * Get the per-thread h_errno location.
407 int *__h_errno_location(void)
409 return h_errno_location_ptr();
412 #endif /* HAVE_NPTL */
415 #if defined(__linux__) && defined(__i386__)
416 static inline void writejump( const char *symbol
, void *dest
)
418 unsigned char *addr
= wine_dlsym( RTLD_NEXT
, symbol
, NULL
, 0 );
422 /* write a relative jump at the function address */
423 mprotect((void*)((unsigned int)addr
& ~(getpagesize()-1)), 5, PROT_READ
|PROT_EXEC
|PROT_WRITE
);
425 *(int *)(addr
+1) = (unsigned char *)dest
- (addr
+ 5);
426 mprotect((void*)((unsigned int)addr
& ~(getpagesize()-1)), 5, PROT_READ
|PROT_EXEC
);
430 /***********************************************************************
433 * Initialize errno handling.
435 void SYSDEPS_InitErrno(void)
438 errno_location_ptr
= thread_errno_location
;
439 h_errno_location_ptr
= thread_h_errno_location
;
441 # if defined(__linux__) && defined(__i386__)
442 writejump( "__errno_location", thread_errno_location
);
443 writejump( "__h_errno_location", thread_h_errno_location
);
445 #endif /* HAVE_NPTL */
449 /**********************************************************************
450 * NtCurrentTeb (NTDLL.@)
452 * This will crash and burn if called before threading is initialized
454 #if defined(__i386__) && defined(__GNUC__)
455 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
456 #elif defined(__i386__) && defined(_MSC_VER)
457 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
458 #elif defined(HAVE__LWP_CREATE)
459 /***********************************************************************
460 * NtCurrentTeb (NTDLL.@)
462 struct _TEB
* WINAPI
NtCurrentTeb(void)
464 extern void *_lwp_getprivate(void);
465 return (struct _TEB
*)_lwp_getprivate();
467 #elif defined(__powerpc__)
468 __ASM_GLOBAL_FUNC( NtCurrentTeb
, "\n\tmr 3,2\n\tblr" );
470 # error NtCurrentTeb not defined for this architecture
471 #endif /* __i386__ */