ReactOS  0.4.14-dev-342-gdc047f9
thrdini.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS kernel
4  * FILE: ntoskrnl/ke/powerpc/thrdini.c
5  * PURPOSE: i386 Thread Context Creation
6  * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7  * arty (ppc adaptation)
8  */
9 
10 /* INCLUDES ******************************************************************/
11 
12 #include <ntoskrnl.h>
13 //#define NDEBUG
14 #include <debug.h>
15 #include <ndk/powerpc/ketypes.h>
16 #include <ppcmmu/mmu.h>
17 
18 typedef struct _KSWITCHFRAME
19 {
22  PVOID RetAddr;
24 
25 typedef struct _KSTART_FRAME
26 {
32 
33 typedef struct _KUINIT_FRAME
34 {
40 
41 typedef struct _KKINIT_FRAME
42 {
48 
49 /* FUNCTIONS *****************************************************************/
50 
51 VOID
52 NTAPI
54  IN PKSYSTEM_ROUTINE SystemRoutine,
56  IN PVOID StartContext,
57  IN PCONTEXT ContextPointer)
58 {
59  PFX_SAVE_AREA FxSaveArea;
60  PKSTART_FRAME StartFrame;
61  PKSWITCHFRAME CtxSwitchFrame;
62  PKTRAP_FRAME TrapFrame;
63  CONTEXT LocalContext;
65  ppc_map_info_t pagemap[16];
66  PETHREAD EThread = (PETHREAD)Thread;
67  PEPROCESS Process = EThread->ThreadsProcess;
68  ULONG ContextFlags, i, pmsize = sizeof(pagemap) / sizeof(pagemap[0]);
69 
70  DPRINT("Thread: %08x ContextPointer: %08x SystemRoutine: %08x StartRoutine: %08x StartContext: %08x\n",
71  Thread,
72  ContextPointer,
73  SystemRoutine,
75  StartContext);
76 
77  /* Check if this is a With-Context Thread */
78  if (ContextPointer)
79  {
80  /* Set up the Initial Frame */
81  PKUINIT_FRAME InitFrame;
82  InitFrame = (PKUINIT_FRAME)((ULONG_PTR)Thread->InitialStack -
83  sizeof(KUINIT_FRAME));
84 
85  /* Copy over the context we got */
86  RtlCopyMemory(&LocalContext, ContextPointer, sizeof(CONTEXT));
87  Context = &LocalContext;
88  ContextFlags = CONTEXT_CONTROL;
89 
90  /* Zero out the trap frame and save area */
91  RtlZeroMemory(&InitFrame->TrapFrame,
93 
94  /* Setup the Fx Area */
95  FxSaveArea = &InitFrame->FxSaveArea;
96 
97  /* Disable any debug regiseters */
98  Context->ContextFlags &= ~CONTEXT_DEBUG_REGISTERS;
99 
100  /* Setup the Trap Frame */
101  TrapFrame = &InitFrame->TrapFrame;
102 
103  /* Set up a trap frame from the context. */
105  NULL,
106  TrapFrame,
107  Context->ContextFlags | ContextFlags,
108  UserMode);
109 
110  /* Set the previous mode as user */
111  TrapFrame->PreviousMode = UserMode;
112 
113  /* Terminate the Exception Handler List */
114  RtlZeroMemory(TrapFrame->ExceptionRecord, sizeof(TrapFrame->ExceptionRecord));
115 
116  /* Setup the Stack for KiThreadStartup and Context Switching */
117  StartFrame = &InitFrame->StartFrame;
118  CtxSwitchFrame = &InitFrame->CtxSwitchFrame;
119 
120  /* Tell the thread it will run in User Mode */
121  Thread->PreviousMode = UserMode;
122 
123  /* Tell KiThreadStartup of that too */
124  StartFrame->UserThread = TRUE;
125 
126  Thread->TrapFrame = TrapFrame;
127 
128  DPRINT("Thread %08x Iar %08x Msr %08x Gpr1 %08x Gpr3 %08x\n",
129  Thread,
130  TrapFrame->Iar,
131  TrapFrame->Msr,
132  TrapFrame->Gpr1,
133  TrapFrame->Gpr3);
134  }
135  else
136  {
137  /* Set up the Initial Frame for the system thread */
138  PKKINIT_FRAME InitFrame;
139  InitFrame = (PKKINIT_FRAME)((ULONG_PTR)Thread->InitialStack -
140  sizeof(KKINIT_FRAME));
141 
142  /* Setup the Fx Area */
143  FxSaveArea = &InitFrame->FxSaveArea;
144  RtlZeroMemory(FxSaveArea, sizeof(FX_SAVE_AREA));
145 
146  /* Setup the Stack for KiThreadStartup and Context Switching */
147  StartFrame = &InitFrame->StartFrame;
148  CtxSwitchFrame = &InitFrame->CtxSwitchFrame;
149 
150  /* Tell the thread it will run in Kernel Mode */
151  Thread->PreviousMode = KernelMode;
152 
153  /* Tell KiThreadStartup of that too */
154  StartFrame->UserThread = FALSE;
155 
156  /* Setup the Trap Frame */
157  TrapFrame = &InitFrame->TrapFrame;
158  Thread->TrapFrame = TrapFrame;
159 
160  TrapFrame->OldIrql = PASSIVE_LEVEL;
161  TrapFrame->Iar = (ULONG)SystemRoutine;
162  TrapFrame->Msr = 0xb030;
163  TrapFrame->Gpr1 = ((ULONG)&InitFrame->StartFrame) - 0x200;
164  TrapFrame->Gpr3 = (ULONG)StartRoutine;
165  TrapFrame->Gpr4 = (ULONG)StartContext;
166  __asm__("mr %0,13" : "=r" (((PULONG)&TrapFrame->Gpr0)[13]));
167 
168  DPRINT("Thread %08x Iar %08x Msr %08x Gpr1 %08x Gpr3 %08x\n",
169  Thread,
170  TrapFrame->Iar,
171  TrapFrame->Msr,
172  TrapFrame->Gpr1,
173  TrapFrame->Gpr3);
174  }
175 
176  /* Now setup the remaining data for KiThreadStartup */
177  StartFrame->StartContext = StartContext;
178  StartFrame->StartRoutine = StartRoutine;
179  StartFrame->SystemRoutine = SystemRoutine;
180 
181  /* And set up the Context Switch Frame */
182  CtxSwitchFrame->RetAddr = KiThreadStartup;
183  CtxSwitchFrame->ApcBypassDisable = TRUE;
184  CtxSwitchFrame->ExceptionList = EXCEPTION_CHAIN_END;
185 
186  /* Save back the new value of the kernel stack. */
187  Thread->KernelStack = (PVOID)CtxSwitchFrame;
188 
189  /* If we're the first thread of the new process, copy the top 16 pages
190  * from process 0 */
191  if (Process && IsListEmpty(&Process->ThreadListHead))
192  {
193  DPRINT("First Thread in Process %x\n", Process);
194  MmuAllocVsid((ULONG)Process->UniqueProcessId, 0xff);
195 
196  for (i = 0; i < pmsize; i++)
197  {
198  pagemap[i].proc = 0;
199  pagemap[i].addr = 0x7fff0000 + (i * PAGE_SIZE);
200  }
201 
202  MmuInqPage(pagemap, pmsize);
203 
204  for (i = 0; i < pmsize; i++)
205  {
206  if (pagemap[i].phys)
207  {
208  pagemap[i].proc = (ULONG)Process->UniqueProcessId;
209  pagemap[i].phys = 0;
210  MmuMapPage(&pagemap[i], 1);
211  DPRINT("Added map to the new process: P %08x A %08x\n",
212  pagemap[i].proc, pagemap[i].addr);
213  }
214  }
215 
216  DPRINT("Did additional aspace setup in the new process\n");
217  }
218 }
219 
220 /* EOF */
221 
222 
VOID NTAPI KeContextToTrapFrame(PCONTEXT Context, PKEXCEPTION_FRAME ExeptionFrame, PKTRAP_FRAME TrapFrame, ULONG ContextFlags, KPROCESSOR_MODE PreviousMode)
KSWITCH_FRAME CtxSwitchFrame
Definition: thrdini.c:18
#define CONTEXT_CONTROL
Definition: nt_native.h:1369
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
unsigned long proc
Definition: mmu.h:107
UCHAR ExceptionRecord[ROUND_UP(sizeof(EXCEPTION_RECORD), sizeof(ULONGLONG))]
Definition: ketypes.h:100
vaddr_t addr
Definition: mmu.h:108
_In_ ULONG _In_opt_ POBJECT_ATTRIBUTES _In_opt_ HANDLE _Out_opt_ PCLIENT_ID _In_ PKSTART_ROUTINE StartRoutine
Definition: psfuncs.h:87
PKSTART_ROUTINE StartRoutine
Definition: thrdini.c:25
ULONG Msr
Definition: ketypes.h:132
PKSYSTEM_ROUTINE SystemRoutine
Definition: thrdini.c:24
static HANDLE proc()
Definition: pdb.c:32
BOOLEAN ApcBypassDisable
Definition: thrdini.c:20
VOID NTAPI KiInitializeContextThread(IN PKTHREAD Thread, IN PKSYSTEM_ROUTINE SystemRoutine, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext, IN PCONTEXT Context)
Definition: thrdini.c:36
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
uint32_t ULONG_PTR
Definition: typedefs.h:63
VOID NTAPI KiThreadStartup(VOID)
Definition: thrdini.c:63
struct _KSWITCHFRAME * PKSWITCHFRAME
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
VOID(NTAPI * PKSYSTEM_ROUTINE)(PKSTART_ROUTINE StartRoutine, PVOID StartContext)
Definition: ketypes.h:625
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
KSTART_ROUTINE * PKSTART_ROUTINE
Definition: ketypes.h:487
KSTART_FRAME StartFrame
Definition: thrdini.c:19
__asm__("\t.globl GetPhys\n" "GetPhys:\t\n" "mflr 0\n\t" "stwu 0,-16(1)\n\t" "mfmsr 5\n\t" "andi. 6,5,0xffef\n\t" "mtmsr 6\n\t" "isync\n\t" "sync\n\t" "lwz 3,0(3)\n\t" "mtmsr 5\n\t" "isync\n\t" "sync\n\t" "lwz 0,0(1)\n\t" "addi 1,1,16\n\t" "mtlr 0\n\t" "blr")
PVOID RetAddr
Definition: thrdini.c:21
FX_SAVE_AREA FxSaveArea
Definition: thrdini.c:42
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
ULONG Gpr1
Definition: ketypes.h:103
ULONG Gpr4
Definition: ketypes.h:106
void DPRINT(...)
Definition: polytest.cpp:61
ULONG Gpr0
Definition: ketypes.h:102
struct _KSTART_FRAME KSTART_FRAME
void * PVOID
Definition: retypes.h:9
ULONG Gpr3
Definition: ketypes.h:105
static void * MmuAllocVsid(int vsid, int mask)
Definition: mmu.h:242
KTRAP_FRAME TrapFrame
Definition: thrdini.c:21
struct _KKINIT_FRAME KKINIT_FRAME
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
KTRAP_FRAME TrapFrame
Definition: thrdini.c:45
GLenum const GLvoid * addr
Definition: glext.h:9621
#define CONTEXT_DEBUG_REGISTERS
Definition: nt_native.h:1373
struct _KUINIT_FRAME * PKUINIT_FRAME
#define PAGE_SIZE
Definition: env_spec_w32.h:49
KSTART_FRAME StartFrame
Definition: thrdini.c:28
struct _KUINIT_FRAME KUINIT_FRAME
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
static int MmuMapPage(ppc_map_info_t *info, int count)
Definition: mmu.h:197
struct _KSTART_FRAME * PKSTART_FRAME
static void MmuInqPage(ppc_map_info_t *info, int count)
Definition: mmu.h:217
struct _KKINIT_FRAME * PKKINIT_FRAME
PVOID StartContext
Definition: thrdini.c:26
#define EXCEPTION_CHAIN_END
Definition: rtltypes.h:63
PVOID ExceptionList
Definition: thrdini.c:19
UCHAR OldIrql
Definition: ketypes.h:96
unsigned int * PULONG
Definition: retypes.h:1
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
FX_SAVE_AREA FxSaveArea
Definition: thrdini.c:35
unsigned int ULONG
Definition: retypes.h:1
struct _KSWITCHFRAME KSWITCHFRAME
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
ULONG Iar
Definition: ketypes.h:133
paddr_t phys
Definition: mmu.h:109
struct _ETHREAD * PETHREAD
Definition: nt_native.h:29
#define KTRAP_FRAME_LENGTH
Definition: asm.h:126
BOOLEAN UserThread
Definition: thrdini.c:27
KSWITCH_FRAME CtxSwitchFrame
Definition: thrdini.c:27
CHAR PreviousMode
Definition: ketypes.h:313