xref: /reactos/sdk/lib/rtl/i386/thread.c (revision 139a3d66)
1 /*
2  * COPYRIGHT:         See COPYING in the top level directory
3  * PROJECT:           ReactOS system libraries
4  * PURPOSE:           Rtl user thread functions
5  * FILE:              lib/rtl/i386/thread.c
6  * PROGRAMERS:
7  *                    Alex Ionescu (alex@relsoft.net)
8  *                    Eric Kohl
9  *                    KJK::Hyperion
10  */
11 
12 /* INCLUDES *****************************************************************/
13 
14 #include <rtl.h>
15 #include "i386/ketypes.h"
16 
17 #define NDEBUG
18 #include <debug.h>
19 
20 /* PRIVATE FUNCTIONS *******************************************************/
21 
22 /*
23  * @implemented
24  */
25 VOID
26 NTAPI
27 RtlInitializeContext(IN HANDLE ProcessHandle,
28                      OUT PCONTEXT ThreadContext,
29                      IN PVOID ThreadStartParam  OPTIONAL,
30                      IN PTHREAD_START_ROUTINE ThreadStartAddress,
31                      IN PINITIAL_TEB InitialTeb)
32 {
33     DPRINT("RtlInitializeContext: (hProcess: %p, ThreadContext: %p, Teb: %p\n",
34             ProcessHandle, ThreadContext, InitialTeb);
35 
36     /*
37      * Set the Initial Registers
38      * This is based on NT's default values -- crazy apps might expect this...
39      */
40     ThreadContext->Ebp = 0;
41     ThreadContext->Eax = 0;
42     ThreadContext->Ebx = 1;
43     ThreadContext->Ecx = 2;
44     ThreadContext->Edx = 3;
45     ThreadContext->Esi = 4;
46     ThreadContext->Edi = 5;
47 
48     /* Set the Selectors */
49     ThreadContext->SegGs = 0;
50     ThreadContext->SegFs = KGDT_R3_TEB;
51     ThreadContext->SegEs = KGDT_R3_DATA;
52     ThreadContext->SegDs = KGDT_R3_DATA;
53     ThreadContext->SegSs = KGDT_R3_DATA;
54     ThreadContext->SegCs = KGDT_R3_CODE;
55 
56     /* Enable Interrupts */
57     ThreadContext->EFlags = EFLAGS_INTERRUPT_MASK;
58 
59     /* Settings passed */
60     ThreadContext->Eip = (ULONG)ThreadStartAddress;
61     ThreadContext->Esp = (ULONG)InitialTeb;
62 
63     /* Only the basic Context is initialized */
64     ThreadContext->ContextFlags = CONTEXT_CONTROL |
65                                   CONTEXT_INTEGER |
66                                   CONTEXT_SEGMENTS;
67 
68     /* Set up ESP to the right value */
69     ThreadContext->Esp -= sizeof(PVOID);
70     ZwWriteVirtualMemory(ProcessHandle,
71                          (PVOID)ThreadContext->Esp,
72                          (PVOID)&ThreadStartParam,
73                          sizeof(PVOID),
74                          NULL);
75 
76     /* Push it down one more notch for RETEIP */
77     ThreadContext->Esp -= sizeof(PVOID);
78 }
79 
80 NTSTATUS
81 NTAPI
82 RtlQueueApcWow64Thread(
83     _In_ HANDLE ThreadHandle,
84     _In_ PKNORMAL_ROUTINE ApcRoutine,
85     _In_opt_ PVOID NormalContext,
86     _In_opt_ PVOID SystemArgument1,
87     _In_opt_ PVOID SystemArgument2)
88 {
89     return NtQueueApcThread(ThreadHandle,
90                             ApcRoutine,
91                             NormalContext,
92                             SystemArgument1,
93                             SystemArgument2);
94 }
95 
96 /* EOF */
97