ntdll: Make sure to not unmap anything from reserved areas
[wine] / loader / pthread.c
1 /*
2  * Wine threading routines using the pthread library
3  *
4  * Copyright 2003 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #ifdef HAVE_PTHREAD_H
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_MMAN_H
35 #include <sys/mman.h>
36 #endif
37
38 #include "wine/library.h"
39 #include "wine/pthread.h"
40
41 static int init_done;
42
43 #ifndef __i386__
44 static pthread_key_t teb_key;
45 #endif
46
47 /***********************************************************************
48  *           init_process
49  *
50  * Initialization for a newly created process.
51  */
52 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
53 {
54     init_done = 1;
55 }
56
57
58 /***********************************************************************
59  *           init_thread
60  *
61  * Initialization for a newly created thread.
62  */
63 static void init_thread( struct wine_pthread_thread_info *info )
64 {
65     /* retrieve the stack info (except for main thread) */
66     if (init_done)
67     {
68 #ifdef HAVE_PTHREAD_GETATTR_NP
69         pthread_attr_t attr;
70         pthread_getattr_np( pthread_self(), &attr );
71         pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
72         pthread_attr_destroy( &attr );
73 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
74         char dummy;
75         info->stack_size = pthread_get_stacksize_np(pthread_self());
76         info->stack_base = pthread_get_stackaddr_np(pthread_self());
77         /* if base is too large assume it's the top of the stack instead */
78         if ((char *)info->stack_base > &dummy)
79             info->stack_base = (char *)info->stack_base - info->stack_size;
80 #else
81         /* assume that the stack allocation is page aligned */
82         char dummy;
83         size_t page_size = getpagesize();
84         char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
85         info->stack_base = stack_top - info->stack_size;
86 #endif
87     }
88 }
89
90
91 /***********************************************************************
92  *           create_thread
93  */
94 static int create_thread( struct wine_pthread_thread_info *info )
95 {
96     pthread_t id;
97     pthread_attr_t attr;
98     int ret = 0;
99
100     pthread_attr_init( &attr );
101     pthread_attr_setstacksize( &attr, info->stack_size );
102     pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
103     pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread on Solaris */
104     if (pthread_create( &id, &attr, (void * (*)(void *))info->entry, info )) ret = -1;
105     pthread_attr_destroy( &attr );
106     return ret;
107 }
108
109
110 /***********************************************************************
111  *           init_current_teb
112  *
113  * Set the current TEB for a new thread.
114  */
115 static void init_current_teb( struct wine_pthread_thread_info *info )
116 {
117 #ifdef __i386__
118     /* On the i386, the current thread is in the %fs register */
119     LDT_ENTRY fs_entry;
120
121     wine_ldt_set_base( &fs_entry, info->teb_base );
122     wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
123     wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
124     wine_ldt_init_fs( info->teb_sel, &fs_entry );
125 #else
126     if (!init_done)  /* first thread */
127         pthread_key_create( &teb_key, NULL );
128     pthread_setspecific( teb_key, info->teb_base );
129 #endif
130
131     /* set pid and tid */
132     info->pid = getpid();
133 #ifdef __sun
134     info->tid = pthread_self();  /* this should return the lwp id on solaris */
135 #else
136     info->tid = gettid();
137 #endif
138 }
139
140
141 /***********************************************************************
142  *           get_current_teb
143  */
144 static void *get_current_teb(void)
145 {
146 #ifdef __i386__
147     void *ret;
148     __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
149     return ret;
150 #else
151     return pthread_getspecific( teb_key );
152 #endif
153 }
154
155
156 /***********************************************************************
157  *           exit_thread
158  */
159 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
160 {
161     wine_ldt_free_fs( info->teb_sel );
162     if (info->teb_size) munmap( info->teb_base, info->teb_size );
163     pthread_exit( (void *)info->exit_status );
164 }
165
166
167 /***********************************************************************
168  *           abort_thread
169  */
170 static void DECLSPEC_NORETURN abort_thread( long status )
171 {
172     pthread_exit( (void *)status );
173 }
174
175
176 /***********************************************************************
177  *           pthread_functions
178  */
179 const struct wine_pthread_functions pthread_functions =
180 {
181     init_process,
182     init_thread,
183     create_thread,
184     init_current_teb,
185     get_current_teb,
186     exit_thread,
187     abort_thread,
188     pthread_sigmask
189 };
190
191 #endif  /* HAVE_PTHREAD_H */