ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

thrdini.c
Go to the documentation of this file.
00001 /*
00002  * COPYRIGHT:       See COPYING in the top level directory
00003  * PROJECT:         ReactOS kernel
00004  * FILE:            ntoskrnl/ke/powerpc/thread.c
00005  * PURPOSE:         i386 Thread Context Creation
00006  * PROGRAMMER:      Alex Ionescu (alex@relsoft.net)
00007  *                  arty (ppc adaptation)
00008  */
00009 
00010 /* INCLUDES ******************************************************************/
00011 
00012 #include <ntoskrnl.h>
00013 //#define NDEBUG
00014 #include <debug.h>
00015 #include <ndk/powerpc/ketypes.h>
00016 #include <ppcmmu/mmu.h>
00017 
00018 typedef struct _KSWITCHFRAME
00019 {
00020     PVOID ExceptionList;
00021     BOOLEAN ApcBypassDisable;
00022     PVOID RetAddr;
00023 } KSWITCHFRAME, *PKSWITCHFRAME;
00024 
00025 typedef struct _KSTART_FRAME
00026 {
00027     PKSYSTEM_ROUTINE SystemRoutine;
00028     PKSTART_ROUTINE StartRoutine;
00029     PVOID StartContext;
00030     BOOLEAN UserThread;
00031 } KSTART_FRAME, *PKSTART_FRAME;
00032 
00033 typedef struct _KUINIT_FRAME
00034 {
00035     KSWITCHFRAME CtxSwitchFrame;
00036     KSTART_FRAME StartFrame;
00037     KTRAP_FRAME TrapFrame;
00038     FX_SAVE_AREA FxSaveArea;
00039 } KUINIT_FRAME, *PKUINIT_FRAME;
00040 
00041 typedef struct _KKINIT_FRAME
00042 {
00043     KSWITCHFRAME CtxSwitchFrame;
00044     KSTART_FRAME StartFrame;
00045     KTRAP_FRAME TrapFrame;
00046     FX_SAVE_AREA FxSaveArea;
00047 } KKINIT_FRAME, *PKKINIT_FRAME;
00048 
00049 /* FUNCTIONS *****************************************************************/
00050 
00051 VOID
00052 NTAPI
00053 KiInitializeContextThread(IN PKTHREAD Thread,
00054                           IN PKSYSTEM_ROUTINE SystemRoutine,
00055                           IN PKSTART_ROUTINE StartRoutine,
00056                           IN PVOID StartContext,
00057                           IN PCONTEXT ContextPointer)
00058 {
00059     PFX_SAVE_AREA FxSaveArea;
00060     PKSTART_FRAME StartFrame;
00061     PKSWITCHFRAME CtxSwitchFrame;
00062     PKTRAP_FRAME TrapFrame;
00063     CONTEXT LocalContext;
00064     PCONTEXT Context = NULL;
00065     ppc_map_info_t pagemap[16];
00066     PETHREAD EThread = (PETHREAD)Thread;
00067     PEPROCESS Process = EThread->ThreadsProcess;
00068     ULONG ContextFlags, i, pmsize = sizeof(pagemap) / sizeof(pagemap[0]);
00069     
00070     DPRINT("Thread: %08x ContextPointer: %08x SystemRoutine: %08x StartRoutine: %08x StartContext: %08x\n",
00071            Thread,
00072            ContextPointer,
00073            SystemRoutine,
00074            StartRoutine,
00075            StartContext);
00076 
00077     /* Check if this is a With-Context Thread */
00078     if (ContextPointer)
00079     {
00080         /* Set up the Initial Frame */
00081         PKUINIT_FRAME InitFrame;
00082         InitFrame = (PKUINIT_FRAME)((ULONG_PTR)Thread->InitialStack -
00083                                     sizeof(KUINIT_FRAME));
00084 
00085         /* Copy over the context we got */
00086         RtlCopyMemory(&LocalContext, ContextPointer, sizeof(CONTEXT));
00087         Context = &LocalContext;
00088         ContextFlags = CONTEXT_CONTROL;
00089 
00090         /* Zero out the trap frame and save area */
00091         RtlZeroMemory(&InitFrame->TrapFrame,
00092                       KTRAP_FRAME_LENGTH + sizeof(FX_SAVE_AREA));
00093 
00094         /* Setup the Fx Area */
00095         FxSaveArea = &InitFrame->FxSaveArea;
00096 
00097         /* Disable any debug regiseters */
00098         Context->ContextFlags &= ~CONTEXT_DEBUG_REGISTERS;
00099 
00100         /* Setup the Trap Frame */
00101         TrapFrame = &InitFrame->TrapFrame;
00102 
00103         /* Set up a trap frame from the context. */
00104         KeContextToTrapFrame(Context,
00105                              NULL,
00106                              TrapFrame,
00107                              Context->ContextFlags | ContextFlags,
00108                              UserMode);
00109 
00110         /* Set the previous mode as user */
00111         TrapFrame->PreviousMode = UserMode;
00112 
00113         /* Terminate the Exception Handler List */
00114         RtlZeroMemory(TrapFrame->ExceptionRecord, sizeof(TrapFrame->ExceptionRecord));
00115 
00116         /* Setup the Stack for KiThreadStartup and Context Switching */
00117         StartFrame = &InitFrame->StartFrame;
00118         CtxSwitchFrame = &InitFrame->CtxSwitchFrame;
00119 
00120         /* Tell the thread it will run in User Mode */
00121         Thread->PreviousMode = UserMode;
00122 
00123         /* Tell KiThreadStartup of that too */
00124         StartFrame->UserThread = TRUE;
00125 
00126         Thread->TrapFrame = TrapFrame;
00127 
00128         DPRINT("Thread %08x Iar %08x Msr %08x Gpr1 %08x Gpr3 %08x\n",
00129                Thread,
00130                TrapFrame->Iar,
00131                TrapFrame->Msr,
00132                TrapFrame->Gpr1,
00133                TrapFrame->Gpr3);
00134     }
00135     else
00136     {
00137         /* Set up the Initial Frame for the system thread */
00138         PKKINIT_FRAME InitFrame;
00139         InitFrame = (PKKINIT_FRAME)((ULONG_PTR)Thread->InitialStack -
00140                                     sizeof(KKINIT_FRAME));
00141 
00142         /* Setup the Fx Area */
00143         FxSaveArea = &InitFrame->FxSaveArea;
00144         RtlZeroMemory(FxSaveArea, sizeof(FX_SAVE_AREA));
00145 
00146         /* Setup the Stack for KiThreadStartup and Context Switching */
00147         StartFrame = &InitFrame->StartFrame;
00148         CtxSwitchFrame = &InitFrame->CtxSwitchFrame;
00149 
00150         /* Tell the thread it will run in Kernel Mode */
00151         Thread->PreviousMode = KernelMode;
00152 
00153         /* Tell KiThreadStartup of that too */
00154         StartFrame->UserThread = FALSE;
00155 
00156         /* Setup the Trap Frame */
00157         TrapFrame = &InitFrame->TrapFrame;
00158         Thread->TrapFrame = TrapFrame;
00159 
00160         TrapFrame->OldIrql = PASSIVE_LEVEL;
00161         TrapFrame->Iar = (ULONG)SystemRoutine;
00162         TrapFrame->Msr = 0xb030;
00163         TrapFrame->Gpr1 = ((ULONG)&InitFrame->StartFrame) - 0x200;
00164         TrapFrame->Gpr3 = (ULONG)StartRoutine;
00165         TrapFrame->Gpr4 = (ULONG)StartContext;
00166         __asm__("mr %0,13" : "=r" (((PULONG)&TrapFrame->Gpr0)[13]));
00167 
00168         DPRINT("Thread %08x Iar %08x Msr %08x Gpr1 %08x Gpr3 %08x\n",
00169                Thread,
00170                TrapFrame->Iar,
00171                TrapFrame->Msr,
00172                TrapFrame->Gpr1,
00173                TrapFrame->Gpr3);
00174     }
00175 
00176     /* Now setup the remaining data for KiThreadStartup */
00177     StartFrame->StartContext = StartContext;
00178     StartFrame->StartRoutine = StartRoutine;
00179     StartFrame->SystemRoutine = SystemRoutine;
00180 
00181     /* And set up the Context Switch Frame */
00182     CtxSwitchFrame->RetAddr = KiThreadStartup;
00183     CtxSwitchFrame->ApcBypassDisable = TRUE;
00184     CtxSwitchFrame->ExceptionList = EXCEPTION_CHAIN_END;
00185 
00186     /* Save back the new value of the kernel stack. */
00187     Thread->KernelStack = (PVOID)CtxSwitchFrame;
00188 
00189     /* If we're the first thread of the new process, copy the top 16 pages
00190      * from process 0 */
00191     if (Process && IsListEmpty(&Process->ThreadListHead))
00192     {
00193         DPRINT("First Thread in Process %x\n", Process);
00194         MmuAllocVsid((ULONG)Process->UniqueProcessId, 0xff);
00195         
00196         for (i = 0; i < pmsize; i++)
00197         {
00198             pagemap[i].proc = 0;
00199             pagemap[i].addr = 0x7fff0000 + (i * PAGE_SIZE);
00200         }
00201         
00202         MmuInqPage(pagemap, pmsize);
00203         
00204         for (i = 0; i < pmsize; i++)
00205         {
00206             if (pagemap[i].phys)
00207             {
00208                 pagemap[i].proc = (ULONG)Process->UniqueProcessId;
00209                 pagemap[i].phys = 0;
00210                 MmuMapPage(&pagemap[i], 1);
00211                 DPRINT("Added map to the new process: P %08x A %08x\n",
00212                        pagemap[i].proc, pagemap[i].addr);
00213             }
00214         }
00215         
00216         DPRINT("Did additional aspace setup in the new process\n");
00217     }
00218 }
00219 
00220 /* EOF */
00221 
00222 

Generated on Sun May 27 2012 04:37:31 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.