Fix the case of product and company names.
[wine] / dlls / ntdll / sysdeps.c
1 /*
2  * System-dependent scheduler support
3  *
4  * Copyright 1998 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
48 #include "ntstatus.h"
49 #include "thread.h"
50 #include "wine/pthread.h"
51 #include "wine/server.h"
52 #include "winbase.h"
53 #include "wine/library.h"
54 #include "wine/debug.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(thread);
57
58 struct thread_cleanup_info
59 {
60     void *stack_base;
61     int   stack_size;
62     int   status;
63 };
64
65 /* temporary stacks used on thread exit */
66 #define TEMP_STACK_SIZE 1024
67 #define NB_TEMP_STACKS  8
68 static char temp_stacks[NB_TEMP_STACKS][TEMP_STACK_SIZE];
69 static LONG next_temp_stack;  /* next temp stack to use */
70
71
72 /***********************************************************************
73  *           SYSDEPS_SetCurThread
74  *
75  * Make 'thread' the current thread.
76  */
77 void SYSDEPS_SetCurThread( TEB *teb )
78 {
79 #if defined(__i386__)
80     /* On the i386, the current thread is in the %fs register */
81     LDT_ENTRY fs_entry;
82
83     wine_ldt_set_base( &fs_entry, teb );
84     wine_ldt_set_limit( &fs_entry, 0xfff );
85     wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
86     wine_ldt_init_fs( teb->teb_sel, &fs_entry );
87 #elif defined(__powerpc__)
88     /* On PowerPC, the current TEB is in the gpr13 register */
89 # ifdef __APPLE__
90     __asm__ __volatile__("mr r13, %0" : : "r" (teb));
91 # else
92     __asm__ __volatile__("mr 2, %0" : : "r" (teb));
93 # endif
94 #elif defined(HAVE__LWP_CREATE)
95     /* On non-i386 Solaris, we use the LWP private pointer */
96     _lwp_setprivate( teb );
97 #endif
98
99 #ifdef HAVE_NPTL
100     teb->pthread_data = (void *)pthread_self();
101 #else
102     wine_pthread_init_thread();
103 #endif
104 }
105
106
107 /***********************************************************************
108  *           get_temp_stack
109  *
110  * Get a temporary stack address to run the thread exit code on.
111  */
112 inline static char *get_temp_stack(void)
113 {
114     unsigned int next = interlocked_xchg_add( &next_temp_stack, 1 );
115     return temp_stacks[next % NB_TEMP_STACKS] + TEMP_STACK_SIZE;
116 }
117
118
119 /***********************************************************************
120  *           cleanup_thread
121  *
122  * Cleanup the remains of a thread. Runs on a temporary stack.
123  */
124 static void cleanup_thread( void *ptr )
125 {
126     /* copy the info structure since it is on the stack we will free */
127     struct thread_cleanup_info info = *(struct thread_cleanup_info *)ptr;
128     munmap( info.stack_base, info.stack_size );
129     wine_ldt_free_fs( wine_get_fs() );
130 #ifdef HAVE__LWP_CREATE
131     _lwp_exit();
132 #endif
133     _exit( info.status );
134 }
135
136
137 /***********************************************************************
138  *           SYSDEPS_SpawnThread
139  *
140  * Start running a new thread.
141  * Return -1 on error, 0 if OK.
142  */
143 int SYSDEPS_SpawnThread( void (*func)(TEB *), TEB *teb )
144 {
145 #ifdef HAVE_NPTL
146     pthread_t id;
147     pthread_attr_t attr;
148
149     pthread_attr_init( &attr );
150     pthread_attr_setstack( &attr, teb->DeallocationStack,
151                            (char *)teb->Tib.StackBase - (char *)teb->DeallocationStack );
152     if (pthread_create( &id, &attr, (void * (*)(void *))func, teb )) return -1;
153     return 0;
154 #elif defined(HAVE_CLONE)
155     if (clone( (int (*)(void *))func, teb->Tib.StackBase,
156                CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, teb ) < 0)
157         return -1;
158     return 0;
159 #elif defined(HAVE_RFORK)
160     void **sp = (void **)teb->Tib.StackBase;
161     *--sp = teb;
162     *--sp = 0;
163     *--sp = func;
164     __asm__ __volatile__(
165     "pushl %2;\n\t"             /* flags */
166     "pushl $0;\n\t"             /* 0 ? */
167     "movl %1,%%eax;\n\t"        /* SYS_rfork */
168     ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
169     "cmpl $0, %%edx;\n\t"
170     "je 1f;\n\t"
171     "movl %0,%%esp;\n\t"        /* child -> new thread */
172     "ret;\n"
173     "1:\n\t"            /* parent -> caller thread */
174     "addl $8,%%esp" :
175     : "r" (sp), "g" (SYS_rfork), "g" (RFPROC | RFMEM)
176     : "eax", "edx");
177     return 0;
178 #elif defined(HAVE__LWP_CREATE)
179     ucontext_t context;
180     _lwp_makecontext( &context, (void(*)(void *))func, teb,
181                       NULL, teb->DeallocationStack, (char *)teb->Tib.StackBase - (char *)teb->DeallocationStack );
182     if ( _lwp_create( &context, 0, NULL ) )
183         return -1;
184     return 0;
185 #endif
186
187     FIXME("CreateThread: stub\n" );
188     return -1;
189 }
190
191
192 /***********************************************************************
193  *           SYSDEPS_ExitThread
194  *
195  * Exit a running thread; must not return.
196  */
197 void SYSDEPS_ExitThread( int status )
198 {
199     TEB *teb = NtCurrentTeb();
200     DWORD size = 0;
201
202 #ifdef HAVE_NPTL
203     static TEB *teb_to_free;
204     TEB *free_teb;
205
206     if ((free_teb = interlocked_xchg_ptr( (void **)&teb_to_free, teb )) != NULL)
207     {
208         void *ptr;
209
210         TRACE("freeing prev teb %p stack %p fs %04x\n",
211               free_teb, free_teb->DeallocationStack, free_teb->teb_sel );
212
213         pthread_join( (pthread_t)free_teb->pthread_data, &ptr );
214         wine_ldt_free_fs( free_teb->teb_sel );
215         ptr = free_teb->DeallocationStack;
216         NtFreeVirtualMemory( GetCurrentProcess(), &ptr, &size, MEM_RELEASE );
217     }
218     SYSDEPS_AbortThread( status );
219 #else
220     struct thread_cleanup_info info;
221     MEMORY_BASIC_INFORMATION meminfo;
222
223     NtQueryVirtualMemory( GetCurrentProcess(), teb->Tib.StackBase, MemoryBasicInformation,
224                           &meminfo, sizeof(meminfo), NULL );
225     info.stack_base = meminfo.AllocationBase;
226     info.stack_size = meminfo.RegionSize + ((char *)teb->Tib.StackBase - (char *)meminfo.AllocationBase);
227     info.status     = status;
228
229     SIGNAL_Block();
230     size = 0;
231     NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE | MEM_SYSTEM );
232     close( teb->wait_fd[0] );
233     close( teb->wait_fd[1] );
234     close( teb->reply_fd );
235     close( teb->request_fd );
236     SIGNAL_Reset();
237     wine_switch_to_stack( cleanup_thread, &info, get_temp_stack() );
238 #endif
239 }
240
241
242 /***********************************************************************
243  *           SYSDEPS_AbortThread
244  *
245  * Same as SYSDEPS_ExitThread, but must not do anything that requires a server call.
246  */
247 void SYSDEPS_AbortThread( int status )
248 {
249     SIGNAL_Block();
250     close( NtCurrentTeb()->wait_fd[0] );
251     close( NtCurrentTeb()->wait_fd[1] );
252     close( NtCurrentTeb()->reply_fd );
253     close( NtCurrentTeb()->request_fd );
254 #ifdef HAVE_NPTL
255     pthread_exit( (void *)status );
256 #endif
257     SIGNAL_Reset();
258 #ifdef HAVE__LWP_CREATE
259     _lwp_exit();
260 #endif
261     for (;;)  /* avoid warning */
262         _exit( status );
263 }
264
265 /***********************************************************************
266  *           SYSDEPS_GetUnixTid
267  *
268  * Get the Unix tid of the current thread.
269  */
270 int SYSDEPS_GetUnixTid(void)
271 {
272 #ifdef HAVE__LWP_SELF
273     return _lwp_self();
274 #elif defined(__linux__) && defined(__i386__)
275     int ret;
276     __asm__("int $0x80" : "=a" (ret) : "0" (224) /* SYS_gettid */);
277     if (ret < 0) ret = -1;
278     return ret;
279 #else
280     return -1;
281 #endif
282 }
283
284 /**********************************************************************
285  *           NtCurrentTeb   (NTDLL.@)
286  *
287  * This will crash and burn if called before threading is initialized
288  */
289 #if defined(__i386__) && defined(__GNUC__)
290 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
291 #elif defined(__i386__) && defined(_MSC_VER)
292 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
293 #elif defined(HAVE__LWP_CREATE)
294 /***********************************************************************
295  *              NtCurrentTeb (NTDLL.@)
296  */
297 struct _TEB * WINAPI NtCurrentTeb(void)
298 {
299     extern void *_lwp_getprivate(void);
300     return (struct _TEB *)_lwp_getprivate();
301 }
302 #elif defined(__powerpc__)
303 # ifdef __APPLE__
304 __ASM_GLOBAL_FUNC( NtCurrentTeb, "\n\tmr r3,r13\n\tblr" );
305 # else
306 __ASM_GLOBAL_FUNC( NtCurrentTeb, "\n\tmr 3,2\n\tblr" );
307 # endif
308 #else
309 # error NtCurrentTeb not defined for this architecture
310 #endif  /* __i386__ */