Store %gs in the TEB on every call to 16-bit code, and don't restore
[wine] / scheduler / thread.c
1 /*
2  * Win32 threads
3  *
4  * Copyright 1996 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 <assert.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_MMAN_H
28 #include <sys/mman.h>
29 #endif
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include "wine/winbase16.h"
37 #include "thread.h"
38 #include "task.h"
39 #include "module.h"
40 #include "winerror.h"
41 #include "selectors.h"
42 #include "winnt.h"
43 #include "wine/server.h"
44 #include "stackframe.h"
45 #include "wine/debug.h"
46 #include "winnls.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(thread);
49 WINE_DECLARE_DEBUG_CHANNEL(relay);
50
51 /* TEB of the initial thread */
52 static TEB initial_teb;
53
54 extern struct _PDB current_process;
55
56 /***********************************************************************
57  *           THREAD_IdToTEB
58  *
59  * Convert a thread id to a TEB, making sure it is valid.
60  */
61 TEB *THREAD_IdToTEB( DWORD id )
62 {
63     TEB *ret = NULL;
64
65     if (!id || id == GetCurrentThreadId()) return NtCurrentTeb();
66
67     SERVER_START_REQ( get_thread_info )
68     {
69         req->handle = 0;
70         req->tid_in = id;
71         if (!wine_server_call( req )) ret = reply->teb;
72     }
73     SERVER_END_REQ;
74
75     if (!ret)
76     {
77         /* Allow task handles to be used; convert to main thread */
78         if ( IsTask16( id ) )
79         {
80             TDB *pTask = TASK_GetPtr( id );
81             if (pTask) return pTask->teb;
82         }
83         SetLastError( ERROR_INVALID_PARAMETER );
84     }
85     return ret;
86 }
87
88
89 /***********************************************************************
90  *           THREAD_InitTEB
91  *
92  * Initialization of a newly created TEB.
93  */
94 static BOOL THREAD_InitTEB( TEB *teb )
95 {
96     teb->except    = (void *)~0UL;
97     teb->self      = teb;
98     teb->tibflags  = TEBF_WIN32;
99     teb->tls_ptr   = teb->tls_array;
100     teb->exit_code = STILL_ACTIVE;
101     teb->request_fd = -1;
102     teb->reply_fd   = -1;
103     teb->wait_fd[0] = -1;
104     teb->wait_fd[1] = -1;
105     teb->stack_top  = (void *)~0UL;
106     teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
107     teb->StaticUnicodeString.Buffer = (PWSTR)teb->StaticUnicodeBuffer;
108     teb->teb_sel = wine_ldt_alloc_fs();
109     return (teb->teb_sel != 0);
110 }
111
112
113 /***********************************************************************
114  *           THREAD_FreeTEB
115  *
116  * Free data structures associated with a thread.
117  * Must be called from the context of another thread.
118  */
119 static void THREAD_FreeTEB( TEB *teb )
120 {
121     TRACE("(%p) called\n", teb );
122     /* Free the associated memory */
123     wine_ldt_free_entries( teb->stack_sel, 1 );
124     wine_ldt_free_fs( teb->teb_sel );
125     VirtualFree( teb->stack_base, 0, MEM_RELEASE );
126 }
127
128
129 /***********************************************************************
130  *           THREAD_InitStack
131  *
132  * Allocate the stack of a thread.
133  */
134 TEB *THREAD_InitStack( TEB *teb, DWORD stack_size )
135 {
136     DWORD old_prot, total_size;
137     DWORD page_size = getpagesize();
138     void *base;
139
140     /* Allocate the stack */
141
142     if (stack_size >= 16*1024*1024)
143         WARN("Thread stack size is %ld MB.\n",stack_size/1024/1024);
144
145     /* if size is smaller than default, get stack size from parent */
146     if (stack_size < 1024 * 1024)
147     {
148         if (teb)
149             stack_size = 1024 * 1024;  /* no parent */
150         else
151             stack_size = ((char *)NtCurrentTeb()->stack_top - (char *)NtCurrentTeb()->stack_base
152                           - SIGNAL_STACK_SIZE - 3 * page_size);
153     }
154
155     /* FIXME: some Wine functions use a lot of stack, so we add 64Kb here */
156     stack_size += 64 * 1024;
157
158     /* Memory layout in allocated block:
159      *
160      *   size                 contents
161      * 1 page              NOACCESS guard page
162      * SIGNAL_STACK_SIZE   signal stack
163      * 1 page              NOACCESS guard page
164      * 1 page              PAGE_GUARD guard page
165      * stack_size          normal stack
166      * 64Kb                16-bit stack (optional)
167      * 1 page              TEB (except for initial thread)
168      * 1 page              debug info (except for initial thread)
169      */
170
171     stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
172     total_size = stack_size + SIGNAL_STACK_SIZE + 3 * page_size;
173     total_size += 0x10000; /* 16-bit stack */
174     if (!teb) total_size += 2 * page_size;
175
176     if (!(base = VirtualAlloc( NULL, total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE )))
177         return NULL;
178
179     if (!teb)
180     {
181         teb = (TEB *)((char *)base + total_size - 2 * page_size);
182         if (!THREAD_InitTEB( teb )) goto error;
183         teb->debug_info = (char *)teb + page_size;
184     }
185
186     teb->stack_low    = base;
187     teb->stack_base   = base;
188     teb->signal_stack = (char *)base + page_size;
189     teb->stack_top    = (char *)base + 3 * page_size + SIGNAL_STACK_SIZE + stack_size;
190
191     /* Setup guard pages */
192
193     VirtualProtect( base, 1, PAGE_NOACCESS, &old_prot );
194     VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE, 1, PAGE_NOACCESS, &old_prot );
195     VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE + page_size, 1,
196                     PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_prot );
197
198     /* Allocate the 16-bit stack selector */
199
200     teb->stack_sel = SELECTOR_AllocBlock( teb->stack_top, 0x10000, WINE_LDT_FLAGS_DATA );
201     if (!teb->stack_sel) goto error;
202     teb->cur_stack = MAKESEGPTR( teb->stack_sel, 0x10000 - sizeof(STACK16FRAME) );
203
204     return teb;
205
206 error:
207     wine_ldt_free_fs( teb->teb_sel );
208     VirtualFree( base, 0, MEM_RELEASE );
209     return NULL;
210 }
211
212
213 /***********************************************************************
214  *           thread_errno_location
215  *
216  * Get the per-thread errno location.
217  */
218 static int *thread_errno_location(void)
219 {
220     return &NtCurrentTeb()->thread_errno;
221 }
222
223 /***********************************************************************
224  *           thread_h_errno_location
225  *
226  * Get the per-thread h_errno location.
227  */
228 static int *thread_h_errno_location(void)
229 {
230     return &NtCurrentTeb()->thread_h_errno;
231 }
232
233 /***********************************************************************
234  *           THREAD_Init
235  *
236  * Setup the initial thread.
237  *
238  * NOTES: The first allocated TEB on NT is at 0x7ffde000.
239  */
240 void THREAD_Init(void)
241 {
242     if (!initial_teb.self)  /* do it only once */
243     {
244         THREAD_InitTEB( &initial_teb );
245         assert( initial_teb.teb_sel );
246         initial_teb.process = &current_process;
247         SYSDEPS_SetCurThread( &initial_teb );
248         wine_errno_location = thread_errno_location;
249         wine_h_errno_location = thread_h_errno_location;
250     }
251 }
252
253 DECL_GLOBAL_CONSTRUCTOR(thread_init) { THREAD_Init(); }
254
255
256 /***********************************************************************
257  *           THREAD_Start
258  *
259  * Start execution of a newly created thread. Does not return.
260  */
261 static void THREAD_Start(void)
262 {
263     LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
264
265     if (TRACE_ON(relay))
266         DPRINTF("%04lx:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func );
267
268     PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
269     MODULE_DllThreadAttach( NULL );
270     ExitThread( func( NtCurrentTeb()->entry_arg ) );
271 }
272
273
274 /***********************************************************************
275  *           CreateThread   (KERNEL32.@)
276  */
277 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack,
278                             LPTHREAD_START_ROUTINE start, LPVOID param,
279                             DWORD flags, LPDWORD id )
280 {
281     HANDLE handle = 0;
282     TEB *teb;
283     DWORD tid = 0;
284     int request_pipe[2];
285
286     if (pipe( request_pipe ) == -1)
287     {
288         SetLastError( ERROR_TOO_MANY_OPEN_FILES );
289         return 0;
290     }
291     fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
292     wine_server_send_fd( request_pipe[0] );
293
294     SERVER_START_REQ( new_thread )
295     {
296         req->suspend    = ((flags & CREATE_SUSPENDED) != 0);
297         req->inherit    = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
298         req->request_fd = request_pipe[0];
299         if (!wine_server_call_err( req ))
300         {
301             handle = reply->handle;
302             tid = reply->tid;
303         }
304         close( request_pipe[0] );
305     }
306     SERVER_END_REQ;
307
308     if (!handle || !(teb = THREAD_InitStack( NULL, stack )))
309     {
310         close( request_pipe[1] );
311         return 0;
312     }
313
314     teb->process     = NtCurrentTeb()->process;
315     teb->tid         = tid;
316     teb->request_fd  = request_pipe[1];
317     teb->entry_point = start;
318     teb->entry_arg   = param;
319     teb->startup     = THREAD_Start;
320     teb->htask16     = GetCurrentTask();
321
322     if (id) *id = tid;
323     if (SYSDEPS_SpawnThread( teb ) == -1)
324     {
325         CloseHandle( handle );
326         close( request_pipe[1] );
327         THREAD_FreeTEB( teb );
328         return 0;
329     }
330     return handle;
331 }
332
333 /***********************************************************************
334  * ExitThread [KERNEL32.@]  Ends a thread
335  *
336  * RETURNS
337  *    None
338  */
339 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
340 {
341     BOOL last;
342     SERVER_START_REQ( terminate_thread )
343     {
344         /* send the exit code to the server */
345         req->handle    = GetCurrentThread();
346         req->exit_code = code;
347         wine_server_call( req );
348         last = reply->last;
349     }
350     SERVER_END_REQ;
351
352     if (last)
353     {
354         LdrShutdownProcess();
355         exit( code );
356     }
357     else
358     {
359         LdrShutdownThread();
360         if (!(NtCurrentTeb()->tibflags & TEBF_WIN32)) TASK_ExitTask();
361         SYSDEPS_ExitThread( code );
362     }
363 }
364
365 /***********************************************************************
366  * OpenThread Retrieves a handle to a thread from its thread id
367  *
368  * RETURNS
369  *    None
370  */
371 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
372 {
373     HANDLE ret = 0;
374     SERVER_START_REQ( open_thread )
375     {
376         req->tid     = dwThreadId;
377         req->access  = dwDesiredAccess;
378         req->inherit = bInheritHandle;
379         if (!wine_server_call_err( req )) ret = reply->handle;
380     }
381     SERVER_END_REQ;
382     return ret;
383 }
384
385 /***********************************************************************
386  * SetThreadContext [KERNEL32.@]  Sets context of thread.
387  *
388  * RETURNS
389  *    Success: TRUE
390  *    Failure: FALSE
391  */
392 BOOL WINAPI SetThreadContext( HANDLE handle,           /* [in]  Handle to thread with context */
393                               const CONTEXT *context ) /* [in] Address of context structure */
394 {
395     BOOL ret;
396     SERVER_START_REQ( set_thread_context )
397     {
398         req->handle = handle;
399         req->flags = context->ContextFlags;
400         wine_server_add_data( req, context, sizeof(*context) );
401         ret = !wine_server_call_err( req );
402     }
403     SERVER_END_REQ;
404     return ret;
405 }
406
407
408 /***********************************************************************
409  * GetThreadContext [KERNEL32.@]  Retrieves context of thread.
410  *
411  * RETURNS
412  *    Success: TRUE
413  *    Failure: FALSE
414  */
415 BOOL WINAPI GetThreadContext( HANDLE handle,     /* [in]  Handle to thread with context */
416                               CONTEXT *context ) /* [out] Address of context structure */
417 {
418     BOOL ret;
419     SERVER_START_REQ( get_thread_context )
420     {
421         req->handle = handle;
422         req->flags = context->ContextFlags;
423         wine_server_add_data( req, context, sizeof(*context) );
424         wine_server_set_reply( req, context, sizeof(*context) );
425         ret = !wine_server_call_err( req );
426     }
427     SERVER_END_REQ;
428     return ret;
429 }
430
431
432 /**********************************************************************
433  * GetThreadPriority [KERNEL32.@]  Returns priority for thread.
434  *
435  * RETURNS
436  *    Success: Thread's priority level.
437  *    Failure: THREAD_PRIORITY_ERROR_RETURN
438  */
439 INT WINAPI GetThreadPriority(
440     HANDLE hthread) /* [in] Handle to thread */
441 {
442     INT ret = THREAD_PRIORITY_ERROR_RETURN;
443     SERVER_START_REQ( get_thread_info )
444     {
445         req->handle = hthread;
446         req->tid_in = 0;
447         if (!wine_server_call_err( req )) ret = reply->priority;
448     }
449     SERVER_END_REQ;
450     return ret;
451 }
452
453
454 /**********************************************************************
455  * SetThreadPriority [KERNEL32.@]  Sets priority for thread.
456  *
457  * RETURNS
458  *    Success: TRUE
459  *    Failure: FALSE
460  */
461 BOOL WINAPI SetThreadPriority(
462     HANDLE hthread, /* [in] Handle to thread */
463     INT priority)   /* [in] Thread priority level */
464 {
465     BOOL ret;
466     SERVER_START_REQ( set_thread_info )
467     {
468         req->handle   = hthread;
469         req->priority = priority;
470         req->mask     = SET_THREAD_INFO_PRIORITY;
471         ret = !wine_server_call_err( req );
472     }
473     SERVER_END_REQ;
474     return ret;
475 }
476
477
478 /**********************************************************************
479  * GetThreadPriorityBoost [KERNEL32.@]  Returns priority boost for thread.
480  *
481  * Always reports that priority boost is disabled.
482  *
483  * RETURNS
484  *    Success: TRUE.
485  *    Failure: FALSE
486  */
487 BOOL WINAPI GetThreadPriorityBoost(
488     HANDLE hthread, /* [in] Handle to thread */
489     PBOOL pstate)   /* [out] pointer to var that receives the boost state */
490 {
491     if (pstate) *pstate = FALSE;
492     return NO_ERROR;
493 }
494
495
496 /**********************************************************************
497  * SetThreadPriorityBoost [KERNEL32.@]  Sets priority boost for thread.
498  *
499  * Priority boost is not implemented. Thsi function always returns
500  * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
501  *
502  * RETURNS
503  *    Always returns FALSE to indicate a failure
504  */
505 BOOL WINAPI SetThreadPriorityBoost(
506     HANDLE hthread, /* [in] Handle to thread */
507     BOOL disable)   /* [in] TRUE to disable priority boost */
508 {
509     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
510     return FALSE;
511 }
512
513
514 /**********************************************************************
515  *           SetThreadAffinityMask   (KERNEL32.@)
516  */
517 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
518 {
519     DWORD ret;
520     SERVER_START_REQ( set_thread_info )
521     {
522         req->handle   = hThread;
523         req->affinity = dwThreadAffinityMask;
524         req->mask     = SET_THREAD_INFO_AFFINITY;
525         ret = !wine_server_call_err( req );
526         /* FIXME: should return previous value */
527     }
528     SERVER_END_REQ;
529     return ret;
530 }
531
532 /**********************************************************************
533  * SetThreadIdealProcessor [KERNEL32.@]  Obtains timing information.
534  *
535  * RETURNS
536  *    Success: Value of last call to SetThreadIdealProcessor
537  *    Failure: -1
538  */
539 DWORD WINAPI SetThreadIdealProcessor(
540     HANDLE hThread,          /* [in] Specifies the thread of interest */
541     DWORD dwIdealProcessor)  /* [in] Specifies the new preferred processor */
542 {
543     FIXME("(%p): stub\n",hThread);
544     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
545     return -1L;
546 }
547
548 /**********************************************************************
549  * TerminateThread [KERNEL32.@]  Terminates a thread
550  *
551  * RETURNS
552  *    Success: TRUE
553  *    Failure: FALSE
554  */
555 BOOL WINAPI TerminateThread( HANDLE handle,    /* [in] Handle to thread */
556                              DWORD exit_code)  /* [in] Exit code for thread */
557 {
558     NTSTATUS status = NtTerminateThread( handle, exit_code );
559     if (status) SetLastError( RtlNtStatusToDosError(status) );
560     return !status;
561 }
562
563
564 /**********************************************************************
565  *              GetExitCodeThread (KERNEL32.@)
566  *
567  * Gets termination status of thread.
568  *
569  * RETURNS
570  *    Success: TRUE
571  *    Failure: FALSE
572  */
573 BOOL WINAPI GetExitCodeThread(
574     HANDLE hthread, /* [in]  Handle to thread */
575     LPDWORD exitcode) /* [out] Address to receive termination status */
576 {
577     BOOL ret;
578     SERVER_START_REQ( get_thread_info )
579     {
580         req->handle = hthread;
581         req->tid_in = 0;
582         ret = !wine_server_call_err( req );
583         if (ret && exitcode) *exitcode = reply->exit_code;
584     }
585     SERVER_END_REQ;
586     return ret;
587 }
588
589
590 /**********************************************************************
591  * ResumeThread [KERNEL32.@]  Resumes a thread.
592  *
593  * Decrements a thread's suspend count.  When count is zero, the
594  * execution of the thread is resumed.
595  *
596  * RETURNS
597  *    Success: Previous suspend count
598  *    Failure: 0xFFFFFFFF
599  *    Already running: 0
600  */
601 DWORD WINAPI ResumeThread(
602     HANDLE hthread) /* [in] Identifies thread to restart */
603 {
604     DWORD ret = 0xffffffff;
605     SERVER_START_REQ( resume_thread )
606     {
607         req->handle = hthread;
608         if (!wine_server_call_err( req )) ret = reply->count;
609     }
610     SERVER_END_REQ;
611     return ret;
612 }
613
614
615 /**********************************************************************
616  * SuspendThread [KERNEL32.@]  Suspends a thread.
617  *
618  * RETURNS
619  *    Success: Previous suspend count
620  *    Failure: 0xFFFFFFFF
621  */
622 DWORD WINAPI SuspendThread(
623     HANDLE hthread) /* [in] Handle to the thread */
624 {
625     DWORD ret = 0xffffffff;
626     SERVER_START_REQ( suspend_thread )
627     {
628         req->handle = hthread;
629         if (!wine_server_call_err( req )) ret = reply->count;
630     }
631     SERVER_END_REQ;
632     return ret;
633 }
634
635
636 /***********************************************************************
637  *              QueueUserAPC  (KERNEL32.@)
638  */
639 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
640 {
641     DWORD ret;
642     SERVER_START_REQ( queue_apc )
643     {
644         req->handle = hthread;
645         req->user   = 1;
646         req->func   = func;
647         req->param  = (void *)data;
648         ret = !wine_server_call_err( req );
649     }
650     SERVER_END_REQ;
651     return ret;
652 }
653
654
655 /**********************************************************************
656  * GetThreadTimes [KERNEL32.@]  Obtains timing information.
657  *
658  * RETURNS
659  *    Success: TRUE
660  *    Failure: FALSE
661  */
662 BOOL WINAPI GetThreadTimes(
663     HANDLE thread,         /* [in]  Specifies the thread of interest */
664     LPFILETIME creationtime, /* [out] When the thread was created */
665     LPFILETIME exittime,     /* [out] When the thread was destroyed */
666     LPFILETIME kerneltime,   /* [out] Time thread spent in kernel mode */
667     LPFILETIME usertime)     /* [out] Time thread spent in user mode */
668 {
669     BOOL ret = TRUE;
670
671     if (creationtime || exittime)
672     {
673         /* We need to do a server call to get the creation time or exit time */
674         /* This works on any thread */
675
676         SERVER_START_REQ( get_thread_info )
677         {
678             req->handle = thread;
679             req->tid_in = 0;
680             if ((ret = !wine_server_call_err( req )))
681             {
682                 if (creationtime)
683                     RtlSecondsSince1970ToTime( reply->creation_time, (LARGE_INTEGER*)creationtime );
684                 if (exittime)
685                     RtlSecondsSince1970ToTime( reply->exit_time, (LARGE_INTEGER*)exittime );
686             }
687         }
688         SERVER_END_REQ;
689     }
690     if (ret && (kerneltime || usertime))
691     {
692         /* We call times(2) for kernel time or user time */
693         /* We can only (portably) do this for the current thread */
694         if (thread == GetCurrentThread())
695         {
696             ULONGLONG time;
697             struct tms time_buf;
698             long clocks_per_sec = sysconf(_SC_CLK_TCK);
699
700             times(&time_buf);
701             if (kerneltime)
702             {
703                 time = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
704                 kerneltime->dwHighDateTime = time >> 32;
705                 kerneltime->dwLowDateTime = (DWORD)time;
706             }
707             if (usertime)
708             {
709                 time = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
710                 usertime->dwHighDateTime = time >> 32;
711                 usertime->dwLowDateTime = (DWORD)time;
712             }
713         }
714         else
715         {
716             if (kerneltime) kerneltime->dwHighDateTime = kerneltime->dwLowDateTime = 0;
717             if (usertime) usertime->dwHighDateTime = usertime->dwLowDateTime = 0;
718             FIXME("Cannot get kerneltime or usertime of other threads\n");
719         }
720     }
721     return ret;
722 }
723
724
725 /**********************************************************************
726  * VWin32_BoostThreadGroup [KERNEL.535]
727  */
728 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
729 {
730     FIXME("(0x%08lx,%d): stub\n", threadId, boost);
731 }
732
733 /**********************************************************************
734  * VWin32_BoostThreadStatic [KERNEL.536]
735  */
736 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
737 {
738     FIXME("(0x%08lx,%d): stub\n", threadId, boost);
739 }
740
741
742 /***********************************************************************
743  * GetCurrentThread [KERNEL32.@]  Gets pseudohandle for current thread
744  *
745  * RETURNS
746  *    Pseudohandle for the current thread
747  */
748 #undef GetCurrentThread
749 HANDLE WINAPI GetCurrentThread(void)
750 {
751     return (HANDLE)0xfffffffe;
752 }
753
754
755 /***********************************************************************
756  * ProcessIdToSessionId   (KERNEL32.@)
757  * This function is available on Terminal Server 4SP4 and Windows 2000
758  */
759 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
760 {
761         /* According to MSDN, if the calling process is not in a terminal
762          * services environment, then the sessionid returned is zero.
763          */
764         *sessionid_ptr = 0;
765         return TRUE;
766 }
767
768 /***********************************************************************
769  * SetThreadExecutionState (KERNEL32.@)
770  *
771  * Informs the system that activity is taking place for
772  * power management purposes.
773  */
774 EXECUTION_STATE WINAPI SetThreadExecutionState(EXECUTION_STATE flags)
775 {
776     static EXECUTION_STATE current =
777             ES_SYSTEM_REQUIRED|ES_DISPLAY_REQUIRED|ES_USER_PRESENT;
778     EXECUTION_STATE old = current;
779
780     if (!(current & ES_CONTINUOUS) || (flags & ES_CONTINUOUS))
781         current = flags;
782     FIXME("(0x%lx): stub, harmless (power management).\n", flags);
783     return old;
784 }
785
786
787 #ifdef __i386__
788
789 /***********************************************************************
790  *              SetLastError (KERNEL.147)
791  *              SetLastError (KERNEL32.@)
792  */
793 /* void WINAPI SetLastError( DWORD error ); */
794 __ASM_GLOBAL_FUNC( SetLastError,
795                    "movl 4(%esp),%eax\n\t"
796                    ".byte 0x64\n\t"
797                    "movl %eax,0x60\n\t"
798                    "ret $4" );
799
800 /***********************************************************************
801  *              GetLastError (KERNEL.148)
802  *              GetLastError (KERNEL32.@)
803  */
804 /* DWORD WINAPI GetLastError(void); */
805 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
806
807 /***********************************************************************
808  *              GetCurrentProcessId (KERNEL.471)
809  *              GetCurrentProcessId (KERNEL32.@)
810  */
811 /* DWORD WINAPI GetCurrentProcessId(void) */
812 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
813
814 /***********************************************************************
815  *              GetCurrentThreadId (KERNEL.462)
816  *              GetCurrentThreadId (KERNEL32.@)
817  */
818 /* DWORD WINAPI GetCurrentThreadId(void) */
819 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
820
821 #else  /* __i386__ */
822
823 /**********************************************************************
824  *              SetLastError (KERNEL.147)
825  *              SetLastError (KERNEL32.@)
826  *
827  * Sets the last-error code.
828  */
829 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
830 {
831     NtCurrentTeb()->last_error = error;
832 }
833
834 /**********************************************************************
835  *              GetLastError (KERNEL.148)
836  *              GetLastError (KERNEL32.@)
837  *
838  * Returns last-error code.
839  */
840 DWORD WINAPI GetLastError(void)
841 {
842     return NtCurrentTeb()->last_error;
843 }
844
845 /***********************************************************************
846  *              GetCurrentProcessId (KERNEL.471)
847  *              GetCurrentProcessId (KERNEL32.@)
848  *
849  * Returns process identifier.
850  */
851 DWORD WINAPI GetCurrentProcessId(void)
852 {
853     return (DWORD)NtCurrentTeb()->pid;
854 }
855
856 /***********************************************************************
857  *              GetCurrentThreadId (KERNEL.462)
858  *              GetCurrentThreadId (KERNEL32.@)
859  *
860  * Returns thread identifier.
861  */
862 DWORD WINAPI GetCurrentThreadId(void)
863 {
864     return NtCurrentTeb()->tid;
865 }
866
867 #endif  /* __i386__ */