Fixed other bugs within MMIO implementation. Now, it's possible to
[wine] / scheduler / sysdeps.c
1 /*
2  * System-dependent scheduler support
3  *
4  * Copyright 1998 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 /* Get pointers to the static errno and h_errno variables used by Xlib. This
10    must be done before including <errno.h> makes the variables invisible.  */
11 extern int errno;
12 static int *perrno = &errno;
13 extern int h_errno;
14 static int *ph_errno = &h_errno;
15
16 #include <signal.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #ifdef HAVE_SYS_SYSCALL_H
20 # include <sys/syscall.h>
21 #endif
22 #ifdef HAVE_SYS_LWP_H
23 # include <sys/lwp.h>
24 #endif
25 #ifdef HAVE_UCONTEXT_H
26 # include <ucontext.h>
27 #endif
28 #include "wine/port.h"
29 #include "thread.h"
30 #include "selectors.h"
31 #include "server.h"
32 #include "winbase.h"
33 #include "wine/exception.h"
34 #include "debugtools.h"
35
36 DEFAULT_DEBUG_CHANNEL(thread)
37
38 /* Xlib critical section (FIXME: does not belong here) */
39 CRITICAL_SECTION X11DRV_CritSection = CRITICAL_SECTION_INIT;
40
41 #ifdef linux
42 # ifdef HAVE_SCHED_H
43 #  include <sched.h>
44 # endif
45 # ifndef CLONE_VM
46 #  define CLONE_VM      0x00000100
47 #  define CLONE_FS      0x00000200
48 #  define CLONE_FILES   0x00000400
49 #  define CLONE_SIGHAND 0x00000800
50 #  define CLONE_PID     0x00001000
51 # endif  /* CLONE_VM */
52 #endif  /* linux */
53
54 static int init_done;
55
56 #ifndef NO_REENTRANT_LIBC
57
58 /***********************************************************************
59  *           __errno_location/__error/___errno
60  *
61  * Get the per-thread errno location.
62  */
63 #ifdef HAVE__ERRNO_LOCATION
64 int *__errno_location()
65 #endif
66 #ifdef HAVE__ERROR
67 int *__error()
68 #endif
69 #ifdef HAVE___ERRNO
70 int *___errno()
71 #endif
72 #ifdef HAVE__THR_ERRNO
73 int *__thr_errno()
74 #endif
75 {
76     if (!init_done) return perrno;
77 #ifdef NO_REENTRANT_X11
78     /* Use static libc errno while running in Xlib. */
79     if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
80         return perrno;
81 #endif
82     return &NtCurrentTeb()->thread_errno;
83 }
84
85 /***********************************************************************
86  *           __h_errno_location
87  *
88  * Get the per-thread h_errno location.
89  */
90 int *__h_errno_location()
91 {
92     if (!init_done) return ph_errno;
93 #ifdef NO_REENTRANT_X11
94     /* Use static libc h_errno while running in Xlib. */
95     if (X11DRV_CritSection.OwningThread == GetCurrentThreadId())
96         return ph_errno;
97 #endif
98     return &NtCurrentTeb()->thread_h_errno;
99 }
100
101 #endif /* NO_REENTRANT_LIBC */
102
103 /***********************************************************************
104  *           SYSDEPS_SetCurThread
105  *
106  * Make 'thread' the current thread.
107  */
108 void SYSDEPS_SetCurThread( TEB *teb )
109 {
110 #if defined(__i386__)
111     /* On the i386, the current thread is in the %fs register */
112     __set_fs( teb->teb_sel );
113 #elif defined(HAVE__LWP_CREATE)
114     /* On non-i386 Solaris, we use the LWP private pointer */
115     _lwp_setprivate( teb );
116 #endif
117
118     init_done = 1;  /* now we can use threading routines */
119 }
120
121 /***********************************************************************
122  *           SYSDEPS_StartThread
123  *
124  * Startup routine for a new thread.
125  */
126 static void SYSDEPS_StartThread( TEB *teb )
127 {
128     SYSDEPS_SetCurThread( teb );
129     CLIENT_InitThread();
130     SIGNAL_Init();
131     __TRY
132     {
133         teb->startup();
134     }
135     __EXCEPT(UnhandledExceptionFilter)
136     {
137         TerminateThread( GetCurrentThread(), GetExceptionCode() );
138     }
139     __ENDTRY
140     SYSDEPS_ExitThread(0);  /* should never get here */
141 }
142
143
144 /***********************************************************************
145  *           SYSDEPS_SpawnThread
146  *
147  * Start running a new thread.
148  * Return -1 on error, 0 if OK.
149  */
150 int SYSDEPS_SpawnThread( TEB *teb )
151 {
152 #ifndef NO_REENTRANT_LIBC
153
154 #ifdef linux
155     const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD;
156     if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0)
157         return -1;
158     if (!(flags & CLONE_FILES)) close( teb->socket );  /* close the child socket in the parent */
159     return 0;
160 #endif
161
162 #ifdef HAVE_RFORK
163     const int flags = RFPROC | RFMEM; /*|RFFDG*/
164     void **sp = (void **)teb->stack_top;
165     *--sp = teb;
166     *--sp = 0;
167     *--sp = SYSDEPS_StartThread;
168     __asm__ __volatile__(
169     "pushl %2;\n\t"             /* flags */
170     "pushl $0;\n\t"             /* 0 ? */
171     "movl %1,%%eax;\n\t"        /* SYS_rfork */
172     ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
173     "cmpl $0, %%edx;\n\t"
174     "je 1f;\n\t"
175     "movl %0,%%esp;\n\t"        /* child -> new thread */
176     "ret;\n"
177     "1:\n\t"            /* parent -> caller thread */
178     "addl $8,%%esp" :
179     : "r" (sp), "g" (SYS_rfork), "g" (flags)
180     : "eax", "edx");
181     if (flags & RFFDG) close( teb->socket );  /* close the child socket in the parent */
182     return 0;
183 #endif
184
185 #ifdef HAVE__LWP_CREATE
186     ucontext_t context;
187     _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb,
188                       NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base );
189     if ( _lwp_create( &context, 0, NULL ) )
190         return -1;
191     return 0;
192 #endif
193
194 #endif /* NO_REENTRANT_LIBC */
195
196     FIXME("CreateThread: stub\n" );
197     return 0;
198 }
199
200
201
202 /***********************************************************************
203  *           SYSDEPS_ExitThread
204  *
205  * Exit a running thread; must not return.
206  */
207 void SYSDEPS_ExitThread( int status )
208 {
209     int socket = NtCurrentTeb()->socket;
210     NtCurrentTeb()->socket = -1;
211     close( socket );
212 #ifdef HAVE__LWP_CREATE
213     _lwp_exit();
214 #endif
215     _exit( status );
216     /*
217      * It is of course impossible to come here,
218      * but it eliminates a compiler warning.
219      */
220     exit( status );
221 }
222
223
224 /**********************************************************************
225  *           NtCurrentTeb   (NTDLL.89)
226  *
227  * This will crash and burn if called before threading is initialized
228  */
229 #ifdef __i386__
230 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
231 #elif defined(HAVE__LWP_CREATE)
232 struct _TEB * WINAPI NtCurrentTeb(void)
233 {
234     extern void *_lwp_getprivate(void);
235     return (struct _TEB *)_lwp_getprivate();
236 }
237 #else
238 # error NtCurrentTeb not defined for this architecture
239 #endif  /* __i386__ */