ReactOS  0.4.15-dev-449-g64abd9f
thrdini.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
#include <ndk/powerpc/ketypes.h>
#include <ppcmmu/mmu.h>
Include dependency graph for thrdini.c:

Go to the source code of this file.

Classes

struct  _KSWITCHFRAME
 
struct  _KSTART_FRAME
 
struct  _KUINIT_FRAME
 
struct  _KKINIT_FRAME
 

Typedefs

typedef struct _KSWITCHFRAME KSWITCHFRAME
 
typedef struct _KSWITCHFRAMEPKSWITCHFRAME
 
typedef struct _KSTART_FRAME KSTART_FRAME
 
typedef struct _KSTART_FRAMEPKSTART_FRAME
 
typedef struct _KUINIT_FRAME KUINIT_FRAME
 
typedef struct _KUINIT_FRAMEPKUINIT_FRAME
 
typedef struct _KKINIT_FRAME KKINIT_FRAME
 
typedef struct _KKINIT_FRAMEPKKINIT_FRAME
 

Functions

VOID NTAPI KiInitializeContextThread (IN PKTHREAD Thread, IN PKSYSTEM_ROUTINE SystemRoutine, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext, IN PCONTEXT ContextPointer)
 

Typedef Documentation

◆ KKINIT_FRAME

◆ KSTART_FRAME

◆ KSWITCHFRAME

◆ KUINIT_FRAME

◆ PKKINIT_FRAME

◆ PKSTART_FRAME

◆ PKSWITCHFRAME

◆ PKUINIT_FRAME

Function Documentation

◆ KiInitializeContextThread()

VOID NTAPI KiInitializeContextThread ( IN PKTHREAD  Thread,
IN PKSYSTEM_ROUTINE  SystemRoutine,
IN PKSTART_ROUTINE  StartRoutine,
IN PVOID  StartContext,
IN PCONTEXT  ContextPointer 
)

Definition at line 53 of file thrdini.c.

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 }
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 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
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
uint32_t ULONG_PTR
Definition: typedefs.h:64
VOID NTAPI KiThreadStartup(VOID)
Definition: thrdini.c:63
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
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
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
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
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
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
ULONG Iar
Definition: ketypes.h:133
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