Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * System-dependent scheduler support |
| 3 | * |
| 4 | * Copyright 1998 Alexandre Julliard |
| 5 | */ |
| 6 | |
Francois Gouget | 386cf6e | 2001-10-14 16:25:47 +0000 | [diff] [blame^] | 7 | #include "wine/port.h" |
Marcus Meissner | c260638 | 1999-04-11 15:20:29 +0000 | [diff] [blame] | 8 | |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 9 | #include <signal.h> |
| 10 | #include <stdio.h> |
| 11 | #include <unistd.h> |
Ulrich Weigand | 06bf017 | 2000-06-08 00:39:59 +0000 | [diff] [blame] | 12 | #include <sys/time.h> |
Ulrich Weigand | 7e85ea7 | 2000-06-04 01:35:43 +0000 | [diff] [blame] | 13 | #include <sys/resource.h> |
Marcus Meissner | c260638 | 1999-04-11 15:20:29 +0000 | [diff] [blame] | 14 | #ifdef HAVE_SYS_SYSCALL_H |
| 15 | # include <sys/syscall.h> |
| 16 | #endif |
Ulrich Weigand | a44f9f8 | 1999-04-18 13:20:43 +0000 | [diff] [blame] | 17 | #ifdef HAVE_SYS_LWP_H |
| 18 | # include <sys/lwp.h> |
| 19 | #endif |
| 20 | #ifdef HAVE_UCONTEXT_H |
| 21 | # include <ucontext.h> |
| 22 | #endif |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 23 | #include "thread.h" |
Alexandre Julliard | 37e9503 | 2001-07-19 00:39:09 +0000 | [diff] [blame] | 24 | #include "wine/server.h" |
Alexandre Julliard | 03468f7 | 1998-02-15 19:40:49 +0000 | [diff] [blame] | 25 | #include "winbase.h" |
Alexandre Julliard | 3d06d20 | 1999-09-27 10:58:45 +0000 | [diff] [blame] | 26 | #include "wine/exception.h" |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 27 | #include "debugtools.h" |
Alexandre Julliard | 03468f7 | 1998-02-15 19:40:49 +0000 | [diff] [blame] | 28 | |
Dimitrie O. Paun | 529da54 | 2000-11-27 23:54:25 +0000 | [diff] [blame] | 29 | DEFAULT_DEBUG_CHANNEL(thread); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 30 | |
Bang Jun-Young | 289a252 | 2001-06-15 19:43:51 +0000 | [diff] [blame] | 31 | #if defined(linux) || defined(HAVE_CLONE) |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 32 | # ifdef HAVE_SCHED_H |
| 33 | # include <sched.h> |
| 34 | # endif |
| 35 | # ifndef CLONE_VM |
| 36 | # define CLONE_VM 0x00000100 |
| 37 | # define CLONE_FS 0x00000200 |
| 38 | # define CLONE_FILES 0x00000400 |
| 39 | # define CLONE_SIGHAND 0x00000800 |
| 40 | # define CLONE_PID 0x00001000 |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 41 | # endif /* CLONE_VM */ |
Bang Jun-Young | 289a252 | 2001-06-15 19:43:51 +0000 | [diff] [blame] | 42 | #endif /* linux || HAVE_CLONE */ |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 43 | |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 44 | /*********************************************************************** |
Alexandre Julliard | 54a39e2 | 1999-05-29 14:27:26 +0000 | [diff] [blame] | 45 | * SYSDEPS_SetCurThread |
| 46 | * |
| 47 | * Make 'thread' the current thread. |
| 48 | */ |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 49 | void SYSDEPS_SetCurThread( TEB *teb ) |
Alexandre Julliard | 54a39e2 | 1999-05-29 14:27:26 +0000 | [diff] [blame] | 50 | { |
Ulrich Weigand | cebd60b | 1999-09-03 16:45:44 +0000 | [diff] [blame] | 51 | #if defined(__i386__) |
Alexandre Julliard | 54a39e2 | 1999-05-29 14:27:26 +0000 | [diff] [blame] | 52 | /* On the i386, the current thread is in the %fs register */ |
Alexandre Julliard | 916f975 | 2000-02-26 16:51:13 +0000 | [diff] [blame] | 53 | __set_fs( teb->teb_sel ); |
Ulrich Weigand | cebd60b | 1999-09-03 16:45:44 +0000 | [diff] [blame] | 54 | #elif defined(HAVE__LWP_CREATE) |
| 55 | /* On non-i386 Solaris, we use the LWP private pointer */ |
| 56 | _lwp_setprivate( teb ); |
| 57 | #endif |
Alexandre Julliard | 54a39e2 | 1999-05-29 14:27:26 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /*********************************************************************** |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 61 | * SYSDEPS_StartThread |
| 62 | * |
| 63 | * Startup routine for a new thread. |
| 64 | */ |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 65 | static void SYSDEPS_StartThread( TEB *teb ) |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 66 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 67 | SYSDEPS_SetCurThread( teb ); |
Alexandre Julliard | 875c4b3 | 1999-03-23 14:09:41 +0000 | [diff] [blame] | 68 | CLIENT_InitThread(); |
Alexandre Julliard | 3d06d20 | 1999-09-27 10:58:45 +0000 | [diff] [blame] | 69 | SIGNAL_Init(); |
| 70 | __TRY |
| 71 | { |
| 72 | teb->startup(); |
| 73 | } |
| 74 | __EXCEPT(UnhandledExceptionFilter) |
| 75 | { |
| 76 | TerminateThread( GetCurrentThread(), GetExceptionCode() ); |
| 77 | } |
| 78 | __ENDTRY |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 79 | SYSDEPS_ExitThread(0); /* should never get here */ |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 80 | } |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | /*********************************************************************** |
| 84 | * SYSDEPS_SpawnThread |
| 85 | * |
| 86 | * Start running a new thread. |
| 87 | * Return -1 on error, 0 if OK. |
| 88 | */ |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 89 | int SYSDEPS_SpawnThread( TEB *teb ) |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 90 | { |
Alexandre Julliard | 598412e | 2001-01-17 20:22:22 +0000 | [diff] [blame] | 91 | #ifdef ERRNO_LOCATION |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 92 | |
Bang Jun-Young | 289a252 | 2001-06-15 19:43:51 +0000 | [diff] [blame] | 93 | #if defined(linux) || defined(HAVE_CLONE) |
Alexandre Julliard | 3174174 | 2000-03-28 18:47:24 +0000 | [diff] [blame] | 94 | const int flags = CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD; |
| 95 | if (clone( (int (*)(void *))SYSDEPS_StartThread, teb->stack_top, flags, teb ) < 0) |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 96 | return -1; |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 97 | if (!(flags & CLONE_FILES)) close( teb->request_fd ); /* close the child socket in the parent */ |
Ulrich Weigand | a44f9f8 | 1999-04-18 13:20:43 +0000 | [diff] [blame] | 98 | return 0; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
| 101 | #ifdef HAVE_RFORK |
Alexandre Julliard | 3174174 | 2000-03-28 18:47:24 +0000 | [diff] [blame] | 102 | const int flags = RFPROC | RFMEM; /*|RFFDG*/ |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 103 | void **sp = (void **)teb->stack_top; |
| 104 | *--sp = teb; |
Marcus Meissner | c260638 | 1999-04-11 15:20:29 +0000 | [diff] [blame] | 105 | *--sp = 0; |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 106 | *--sp = SYSDEPS_StartThread; |
| 107 | __asm__ __volatile__( |
Alexandre Julliard | 3174174 | 2000-03-28 18:47:24 +0000 | [diff] [blame] | 108 | "pushl %2;\n\t" /* flags */ |
Marcus Meissner | c260638 | 1999-04-11 15:20:29 +0000 | [diff] [blame] | 109 | "pushl $0;\n\t" /* 0 ? */ |
| 110 | "movl %1,%%eax;\n\t" /* SYS_rfork */ |
| 111 | ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */ |
| 112 | "cmpl $0, %%edx;\n\t" |
| 113 | "je 1f;\n\t" |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 114 | "movl %0,%%esp;\n\t" /* child -> new thread */ |
Marcus Meissner | c260638 | 1999-04-11 15:20:29 +0000 | [diff] [blame] | 115 | "ret;\n" |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 116 | "1:\n\t" /* parent -> caller thread */ |
Marcus Meissner | c260638 | 1999-04-11 15:20:29 +0000 | [diff] [blame] | 117 | "addl $8,%%esp" : |
Alexandre Julliard | 3174174 | 2000-03-28 18:47:24 +0000 | [diff] [blame] | 118 | : "r" (sp), "g" (SYS_rfork), "g" (flags) |
Marcus Meissner | c260638 | 1999-04-11 15:20:29 +0000 | [diff] [blame] | 119 | : "eax", "edx"); |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 120 | if (flags & RFFDG) close( teb->request_fd ); /* close the child socket in the parent */ |
Ulrich Weigand | a44f9f8 | 1999-04-18 13:20:43 +0000 | [diff] [blame] | 121 | return 0; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
Ulrich Weigand | a44f9f8 | 1999-04-18 13:20:43 +0000 | [diff] [blame] | 124 | #ifdef HAVE__LWP_CREATE |
| 125 | ucontext_t context; |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 126 | _lwp_makecontext( &context, (void(*)(void *))SYSDEPS_StartThread, teb, |
| 127 | NULL, teb->stack_base, (char *)teb->stack_top - (char *)teb->stack_base ); |
Ulrich Weigand | a44f9f8 | 1999-04-18 13:20:43 +0000 | [diff] [blame] | 128 | if ( _lwp_create( &context, 0, NULL ) ) |
| 129 | return -1; |
| 130 | return 0; |
| 131 | #endif |
| 132 | |
Alexandre Julliard | 598412e | 2001-01-17 20:22:22 +0000 | [diff] [blame] | 133 | #endif /* ERRNO_LOCATION */ |
Ulrich Weigand | a44f9f8 | 1999-04-18 13:20:43 +0000 | [diff] [blame] | 134 | |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 135 | FIXME("CreateThread: stub\n" ); |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | |
Ulrich Weigand | a44f9f8 | 1999-04-18 13:20:43 +0000 | [diff] [blame] | 140 | |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 141 | /*********************************************************************** |
| 142 | * SYSDEPS_ExitThread |
| 143 | * |
| 144 | * Exit a running thread; must not return. |
| 145 | */ |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 146 | void SYSDEPS_ExitThread( int status ) |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 147 | { |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 148 | int fd = NtCurrentTeb()->request_fd; |
| 149 | NtCurrentTeb()->request_fd = -1; |
| 150 | close( fd ); |
Alexandre Julliard | 9a0e28f | 2000-03-25 19:14:37 +0000 | [diff] [blame] | 151 | #ifdef HAVE__LWP_CREATE |
Ulrich Weigand | a44f9f8 | 1999-04-18 13:20:43 +0000 | [diff] [blame] | 152 | _lwp_exit(); |
| 153 | #endif |
Alexandre Julliard | 12f29b5 | 2000-03-17 15:16:57 +0000 | [diff] [blame] | 154 | _exit( status ); |
Patrik Stridvall | 3b23362 | 2000-03-24 21:19:02 +0000 | [diff] [blame] | 155 | /* |
| 156 | * It is of course impossible to come here, |
| 157 | * but it eliminates a compiler warning. |
| 158 | */ |
| 159 | exit( status ); |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
Ulrich Weigand | 7e85ea7 | 2000-06-04 01:35:43 +0000 | [diff] [blame] | 163 | /*********************************************************************** |
| 164 | * SYSDEPS_CallOnStack |
| 165 | */ |
Ulrich Weigand | 06bf017 | 2000-06-08 00:39:59 +0000 | [diff] [blame] | 166 | int SYSDEPS_DoCallOnStack( int (*func)(LPVOID), LPVOID arg ) |
Ulrich Weigand | 7e85ea7 | 2000-06-04 01:35:43 +0000 | [diff] [blame] | 167 | { |
| 168 | int retv = 0; |
| 169 | |
| 170 | __TRY |
| 171 | { |
| 172 | retv = func( arg ); |
| 173 | } |
| 174 | __EXCEPT(UnhandledExceptionFilter) |
| 175 | { |
| 176 | TerminateThread( GetCurrentThread(), GetExceptionCode() ); |
| 177 | return 0; |
| 178 | } |
| 179 | __ENDTRY |
| 180 | |
| 181 | return retv; |
| 182 | } |
| 183 | |
| 184 | #ifdef __i386__ |
| 185 | int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow, |
| 186 | int (*func)(LPVOID), LPVOID arg ); |
| 187 | __ASM_GLOBAL_FUNC( SYSDEPS_CallOnStack, |
| 188 | "pushl %ebp\n\t" |
| 189 | "movl %esp, %ebp\n\t" |
| 190 | ".byte 0x64; pushl 0x04\n\t" |
| 191 | ".byte 0x64; pushl 0x08\n\t" |
| 192 | "movl 8(%ebp), %esp\n\t" |
| 193 | "movl 12(%ebp), %eax\n\t" |
| 194 | ".byte 0x64; movl %esp, 0x04\n\t" |
| 195 | ".byte 0x64; movl %eax, 0x08\n\t" |
| 196 | "pushl 20(%ebp)\n\t" |
| 197 | "pushl 16(%ebp)\n\t" |
| 198 | "call " __ASM_NAME("SYSDEPS_DoCallOnStack") "\n\t" |
| 199 | "leal -8(%ebp), %esp\n\t" |
| 200 | ".byte 0x64; popl 0x08\n\t" |
| 201 | ".byte 0x64; popl 0x04\n\t" |
| 202 | "popl %ebp\n\t" |
| 203 | "ret" ); |
| 204 | #else |
| 205 | int SYSDEPS_CallOnStack( LPVOID stackTop, LPVOID stackLow, |
| 206 | int (*func)(LPVOID), LPVOID arg ) |
| 207 | { |
| 208 | return SYSDEPS_DoCallOnStack( func, arg ); |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | /*********************************************************************** |
| 213 | * SYSDEPS_SwitchToThreadStack |
| 214 | */ |
Ulrich Weigand | 7e85ea7 | 2000-06-04 01:35:43 +0000 | [diff] [blame] | 215 | void SYSDEPS_SwitchToThreadStack( void (*func)(void) ) |
| 216 | { |
Alexandre Julliard | 6c8edaa | 2000-12-13 20:22:47 +0000 | [diff] [blame] | 217 | DWORD page_size = getpagesize(); |
Alexandre Julliard | 062f533 | 2001-06-11 20:13:48 +0000 | [diff] [blame] | 218 | void *cur_stack = (void *)(((ULONG_PTR)&func + (page_size-1)) & ~(page_size-1)); |
Ulrich Weigand | 06bf017 | 2000-06-08 00:39:59 +0000 | [diff] [blame] | 219 | |
Ulrich Weigand | 7e85ea7 | 2000-06-04 01:35:43 +0000 | [diff] [blame] | 220 | TEB *teb = NtCurrentTeb(); |
| 221 | LPVOID stackTop = teb->stack_top; |
| 222 | LPVOID stackLow = teb->stack_low; |
| 223 | |
| 224 | struct rlimit rl; |
| 225 | |
| 226 | if ( getrlimit(RLIMIT_STACK, &rl) < 0 ) |
| 227 | { |
| 228 | WARN("Can't get rlimit\n"); |
| 229 | rl.rlim_cur = 8*1024*1024; |
| 230 | } |
| 231 | |
Alexandre Julliard | 062f533 | 2001-06-11 20:13:48 +0000 | [diff] [blame] | 232 | teb->stack_top = cur_stack; |
| 233 | teb->stack_low = (char *)cur_stack - rl.rlim_cur; |
Ulrich Weigand | 06bf017 | 2000-06-08 00:39:59 +0000 | [diff] [blame] | 234 | |
Ulrich Weigand | 7e85ea7 | 2000-06-04 01:35:43 +0000 | [diff] [blame] | 235 | SYSDEPS_CallOnStack( stackTop, stackLow, |
| 236 | (int (*)(void *))func, NULL ); |
| 237 | } |
| 238 | |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 239 | /********************************************************************** |
Patrik Stridvall | d0a4177 | 2001-02-14 23:11:17 +0000 | [diff] [blame] | 240 | * NtCurrentTeb (NTDLL.@) |
Alexandre Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 241 | * |
| 242 | * This will crash and burn if called before threading is initialized |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 243 | */ |
Alexandre Julliard | 916f975 | 2000-02-26 16:51:13 +0000 | [diff] [blame] | 244 | #ifdef __i386__ |
| 245 | __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" ); |
| 246 | #elif defined(HAVE__LWP_CREATE) |
Patrik Stridvall | db92305 | 2001-07-24 00:58:52 +0000 | [diff] [blame] | 247 | /*********************************************************************** |
| 248 | * NtCurrentTeb (NTDLL.@) |
| 249 | */ |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 250 | struct _TEB * WINAPI NtCurrentTeb(void) |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 251 | { |
Alexandre Julliard | 916f975 | 2000-02-26 16:51:13 +0000 | [diff] [blame] | 252 | extern void *_lwp_getprivate(void); |
| 253 | return (struct _TEB *)_lwp_getprivate(); |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 254 | } |
Alexandre Julliard | 916f975 | 2000-02-26 16:51:13 +0000 | [diff] [blame] | 255 | #else |
| 256 | # error NtCurrentTeb not defined for this architecture |
| 257 | #endif /* __i386__ */ |