Quiet some valgrind reports.
[wine.git] / scheduler / sysdeps.c
blob933b24ca366d0e81afbe5543418afe9483aee60e
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <signal.h>
25 #include <stdio.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 #endif
32 #ifdef HAVE_SYS_SYSCALL_H
33 # include <sys/syscall.h>
34 #endif
35 #ifdef HAVE_SYS_LWP_H
36 # include <sys/lwp.h>
37 #endif
38 #ifdef HAVE_UCONTEXT_H
39 # include <ucontext.h>
40 #endif
41 #ifdef HAVE_SYS_MMAN_H
42 #include <sys/mman.h>
43 #endif
44 #ifdef HAVE_SCHED_H
45 #include <sched.h>
46 #endif
47 #ifdef HAVE_VALGRIND_MEMCHECK_H
48 #include <valgrind/memcheck.h>
49 #endif
51 #ifdef HAVE_NPTL
52 #include <pthread.h>
53 #endif
55 #include "thread.h"
56 #include "wine/server.h"
57 #include "winbase.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
67 void *stack_base;
68 int stack_size;
69 int status;
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 )
85 #if defined(__i386__)
86 /* On the i386, the current thread is in the %fs register */
87 LDT_ENTRY fs_entry;
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 );
99 #endif
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 )
110 __TRY
112 void (*funcptr)(void) = func;
113 funcptr();
115 __EXCEPT(UnhandledExceptionFilter)
117 TerminateThread( GetCurrentThread(), GetExceptionCode() );
119 __ENDTRY
120 SYSDEPS_ExitThread(0); /* should never get here */
124 /***********************************************************************
125 * get_temp_stack
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 /***********************************************************************
137 * cleanup_thread
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
148 _lwp_exit();
149 #endif
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 );
162 SIGNAL_Init();
163 CLIENT_InitThread();
164 __TRY
166 teb->startup();
168 __EXCEPT(UnhandledExceptionFilter)
170 TerminateThread( GetCurrentThread(), GetExceptionCode() );
172 __ENDTRY
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 )
185 #ifdef HAVE_NPTL
186 pthread_t id;
187 pthread_attr_t attr;
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;
193 return 0;
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)
197 return -1;
198 return 0;
199 #elif defined(HAVE_RFORK)
200 void **sp = (void **)teb->stack_top;
201 *--sp = teb;
202 *--sp = 0;
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"
210 "je 1f;\n\t"
211 "movl %0,%%esp;\n\t" /* child -> new thread */
212 "ret;\n"
213 "1:\n\t" /* parent -> caller thread */
214 "addl $8,%%esp" :
215 : "r" (sp), "g" (SYS_rfork), "g" (RFPROC | RFMEM)
216 : "eax", "edx");
217 return 0;
218 #elif defined(HAVE__LWP_CREATE)
219 ucontext_t context;
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 ) )
223 return -1;
224 return 0;
225 #endif
227 FIXME("CreateThread: stub\n" );
228 return -1;
232 /***********************************************************************
233 * SYSDEPS_CallOnStack
235 void DECLSPEC_NORETURN SYSDEPS_CallOnStack( void (*func)(LPVOID), LPVOID arg );
236 #ifdef __i386__
237 # ifdef __GNUC__
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 */
242 "pushl %edx\n\t"
243 "xorl %ebp,%ebp\n\t"
244 "call *%ecx\n\t"
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;
252 __asm push edx;
253 __asm xor ebp, ebp;
254 __asm call [ecx];
255 __asm int 3;
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 )
270 func( 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 /***********************************************************************
286 * SYSDEPS_ExitThread
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;
301 SIGNAL_Block();
303 #ifdef HAVE_NPTL
304 SYSDEPS_AbortThread( status );
305 #else
306 SIGNAL_Reset();
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 );
315 #endif
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 )
326 SIGNAL_Block();
327 close( NtCurrentTeb()->wait_fd[0] );
328 close( NtCurrentTeb()->wait_fd[1] );
329 close( NtCurrentTeb()->reply_fd );
330 close( NtCurrentTeb()->request_fd );
331 #ifdef HAVE_NPTL
332 pthread_exit( (void *)status );
333 #endif
334 SIGNAL_Reset();
335 #ifdef HAVE__LWP_CREATE
336 _lwp_exit();
337 #endif
338 for (;;) /* avoid warning */
339 _exit( status );
342 /***********************************************************************
343 * SYSDEPS_GetUnixTid
345 * Get the Unix tid of the current thread.
347 int SYSDEPS_GetUnixTid(void)
349 #ifdef HAVE__LWP_SELF
350 return _lwp_self();
351 #elif defined(__linux__) && defined(__i386__)
352 int ret;
353 __asm__("int $0x80" : "=a" (ret) : "0" (224) /* SYS_gettid */);
354 if (ret < 0) ret = -1;
355 return ret;
356 #else
357 return -1;
358 #endif
362 #ifndef HAVE_NPTL
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 /***********************************************************************
405 * __h_errno_location
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 );
422 if (!addr) return;
424 /* write a relative jump at the function address */
425 mprotect((void*)((unsigned int)addr & ~(getpagesize()-1)), 5, PROT_READ|PROT_EXEC|PROT_WRITE);
426 addr[0] = 0xe9;
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 );
432 #endif
434 #endif
436 /***********************************************************************
437 * SYSDEPS_InitErrno
439 * Initialize errno handling.
441 void SYSDEPS_InitErrno(void)
443 #ifndef HAVE_NPTL
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 );
450 # endif
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" );
475 #else
476 # error NtCurrentTeb not defined for this architecture
477 #endif /* __i386__ */