ReactOS  0.4.14-dev-115-g4576127
kiinit.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Kernel
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: ntoskrnl/ke/powerpc/kiinit.c
5  * PURPOSE: Kernel Initialization for x86 CPUs
6  * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7  * Art Yerkes (ayerkes@speakeasy.net)
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include <ntoskrnl.h>
13 
14 //#define NDEBUG
15 #include <debug.h>
16 #include "ppcmmu/mmu.h"
17 
18 /* GLOBALS *******************************************************************/
19 
20 /* Ku bit should be set, so that we get the best options for page protection */
21 #define PPC_SEG_Ku 0x40000000
22 #define PPC_SEG_Ks 0x20000000
23 
24 extern LOADER_MODULE KeLoaderModules[64];
28 /* BIOS Memory Map. Not NTLDR-compliant yet */
30 extern ADDRESS_RANGE KeMemoryMap[64];
31 
32 /* FUNCTIONS *****************************************************************/
33 /*
34  * Trap frame:
35  * r0 .. r32
36  * lr, ctr, srr0, srr1, dsisr
37  */
38 
41 
42 VOID
43 NTAPI
45 {
46  paddr_t handler_target;
47  int *source;
48  for(source = syscall_start, handler_target = 0xc00;
50  source++, handler_target += sizeof(int))
51  {
52  SetPhys(handler_target, *source);
53  }
54 }
55 
56 VOID
57 NTAPI
59 {
60  paddr_t handler_target;
61  int *source;
62 
63  /* Turn off EE bit while redefining dec trap */
64  _disable();
65 
66  for(source = KiDecrementerTrapHandler, handler_target = 0x900;
68  source++, handler_target += sizeof(int))
69  SetPhys(handler_target, *source);
70 
71  DPRINT("CurrentThread %08x IdleThread %08x\n",
72  KeGetCurrentThread(), KeGetCurrentPrcb()->IdleThread);
73 
74  /* Kick decmrenter! */
75  __asm__("mtdec %0" : : "r" (0));
76 
77  /* Enable interrupts! */
78  _enable();
79 }
80 
81 VOID
82 NTAPI
83 KiInitializePcr(IN ULONG ProcessorNumber,
84  IN PKIPCR Pcr,
85  IN PKTHREAD IdleThread,
86  IN PVOID DpcStack)
87 {
88  Pcr->MajorVersion = PCR_MAJOR_VERSION;
89  Pcr->MinorVersion = PCR_MINOR_VERSION;
90  Pcr->CurrentIrql = PASSIVE_LEVEL;
91  Pcr->PrcbData = &PrcbData[ProcessorNumber];
92  Pcr->PrcbData->MajorVersion = PRCB_MAJOR_VERSION;
93  Pcr->PrcbData->MinorVersion = 0;
94  Pcr->PrcbData->Number = 0; /* UP for now */
95  Pcr->PrcbData->SetMember = 1;
96 #if DBG
97  Pcr->PrcbData->BuildType = PRCB_BUILD_DEBUG;
98 #else
99  Pcr->PrcbData->BuildType = 0;
100 #endif
101  Pcr->PrcbData->DpcStack = DpcStack;
102  KeGetPcr()->Prcb = Pcr->PrcbData;
103  KiProcessorBlock[ProcessorNumber] = Pcr->PrcbData;
104 }
105 
106 extern ULONG KiGetFeatureBits();
107 extern VOID KiSetProcessorType();
108 extern VOID KiGetCacheInformation();
109 
110 VOID
111 NTAPI
113  IN PKTHREAD InitThread,
114  IN PVOID IdleStack,
115  IN PKPRCB Prcb,
116  IN CCHAR Number,
117  IN PLOADER_PARAMETER_BLOCK LoaderBlock)
118 {
119  ULONG FeatureBits;
120  LARGE_INTEGER PageDirectory;
121  PVOID DpcStack;
122 
123  /* Detect and set the CPU Type */
125 
126  /* Initialize the Power Management Support for this PRCB */
127  PoInitializePrcb(Prcb);
128 
129  /* Get the processor features for the CPU */
130  FeatureBits = KiGetFeatureBits();
131 
132  /* Save feature bits */
133  Prcb->FeatureBits = FeatureBits;
134 
135  /* Get cache line information for this CPU */
137 
138  /* Initialize spinlocks and DPC data */
139  KiInitSpinLocks(Prcb, Number);
140 
141  /* Check if this is the Boot CPU */
142  if (!Number)
143  {
144  /* Set Node Data */
145  KeNodeBlock[0] = &KiNode0;
146  Prcb->ParentNode = KeNodeBlock[0];
147  KeNodeBlock[0]->ProcessorMask = Prcb->SetMember;
148 
149  /* Set boot-level flags */
151  KeProcessorLevel = (USHORT)Prcb->CpuType;
152  KeFeatureBits = FeatureBits;
153 
154  /* Set the current MP Master KPRCB to the Boot PRCB */
155  Prcb->MultiThreadSetMaster = Prcb;
156 
157  /* Lower to APC_LEVEL */
159 
160  /* Initialize portable parts of the OS */
161  KiInitSystem();
162 
163  /* Initialize the Idle Process and the Process Listhead */
165  PageDirectory.QuadPart = 0;
166  KeInitializeProcess(InitProcess,
167  0,
168  0xFFFFFFFF,
169  &PageDirectory,
170  TRUE);
171  InitProcess->QuantumReset = MAXCHAR;
172  }
173  else
174  {
175  /* FIXME */
176  DPRINT1("SMP Boot support not yet present\n");
177  }
178 
179  /* Setup the Idle Thread */
180  KeInitializeThread(InitProcess,
181  InitThread,
182  NULL,
183  NULL,
184  NULL,
185  NULL,
186  NULL,
187  IdleStack);
188  InitThread->NextProcessor = Number;
189  InitThread->Priority = HIGH_PRIORITY;
190  InitThread->State = Running;
191  InitThread->Affinity = 1 << Number;
192  InitThread->WaitIrql = DISPATCH_LEVEL;
193  InitProcess->ActiveProcessors = 1 << Number;
194 
195  /* HACK for MmUpdatePageDir */
196  ((PETHREAD)InitThread)->ThreadsProcess = (PEPROCESS)InitProcess;
197 
198  /* Set up the thread-related fields in the PRCB */
199  Prcb->CurrentThread = InitThread;
200  Prcb->NextThread = NULL;
201  Prcb->IdleThread = InitThread;
202 
203  /* Initialize Kernel Memory Address Space */
204  MmInit1(MmFreeLdrFirstKrnlPhysAddr,
205  MmFreeLdrLastKrnlPhysAddr,
207  KeMemoryMap,
209  4096);
210 
211  /* Initialize the Kernel Executive */
212  ExpInitializeExecutive(0, LoaderBlock);
213 
214  /* Only do this on the boot CPU */
215  if (!Number)
216  {
217  /* Calculate the time reciprocal */
221 
222  /* Update DPC Values in case they got updated by the executive */
223  Prcb->MaximumDpcQueueDepth = KiMaximumDpcQueueDepth;
224  Prcb->MinimumDpcRate = KiMinimumDpcRate;
225  Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
226 
227  /* Allocate the DPC Stack */
228  DpcStack = MmCreateKernelStack(FALSE, 0);
229  if (!DpcStack) KeBugCheckEx(NO_PAGES_AVAILABLE, 1, 0, 0, 0);
230  Prcb->DpcStack = DpcStack;
231  }
232 
234 
235  KeSetPriorityThread(InitThread, 0);
236  /* Setup decrementer exception */
238 
240 
241  /* Should not return */
242  while(1)
243  {
245  }
246 }
247 
248 extern int KiPageFaultTrap();
250 
251 /* Use this for early boot additions to the page table */
252 VOID
253 NTAPI
255 {
256  ULONG Cpu;
257  ppc_map_info_t info[4];
258  PKIPCR Pcr = (PKIPCR)KPCR_BASE;
259  PKPRCB Prcb;
260 
261  __asm__("mr 13,%0" : : "r" (KPCR_BASE));
262 
263  /* Set the page fault handler to the kernel */
266 
267  // Make 0xf... special
268  MmuAllocVsid(2, 0x8000);
269  MmuSetVsid(15,16,2);
270 
271  /* Get the current CPU */
272  Cpu = KeNumberProcessors;
273  if (!Cpu)
274  {
275  /* We'll allocate a page from the end of the kernel area for KPCR. This code will probably
276  * change when we get SMP support.
277  */
278  info[0].phys = 0;
279  info[0].proc = 2;
280  info[0].addr = (vaddr_t)Pcr;
281  info[0].flags = MMU_KRW_UR;
282  info[1].phys = 0;
283  info[1].proc = 2;
284  info[1].addr = ((vaddr_t)Pcr) + (1 << PAGE_SHIFT);
285  info[1].flags = MMU_KRW_UR;
286  info[2].phys = 0;
287  info[2].proc = 2;
288  info[2].addr = (vaddr_t)KI_USER_SHARED_DATA;
289  info[2].flags = MMU_KRW_UR;
290  info[3].phys = 0;
291  info[3].proc = 2;
292  info[3].addr = (vaddr_t)KIP0PCRADDRESS;
293  info[3].flags = MMU_KRW_UR;
294  MmuMapPage(info, 4);
295  }
296 
297  /* Skip initial setup if this isn't the Boot CPU */
298  if (Cpu) goto AppCpuInit;
299 
300  /* Initialize the PCR */
301  RtlZeroMemory(Pcr, PAGE_SIZE);
302  KiInitializePcr(Cpu,
303  Pcr,
306 
307  /* Set us as the current process */
310 
311  /* Setup CPU-related fields */
312 AppCpuInit:
313  Pcr->Number = Cpu;
314  Pcr->SetMember = 1 << Cpu;
315  Prcb = KeGetCurrentPrcb();
316  Prcb->SetMember = 1 << Cpu;
317 
318  /* Initialize the Processor with HAL */
319  HalInitializeProcessor(Cpu, LoaderBlock);
320 
321  /* Set active processors */
324 
325  /* Initialize the Debugger for the Boot CPU */
326  if (!Cpu) KdInitSystem (0, LoaderBlock);
327 
328  /* Check for break-in */
329  if (KdPollBreakIn())
330  {
332  }
333 
334  /* Raise to HIGH_LEVEL */
336 
337  /* Call main kernel intialization */
340  P0BootStack,
341  Prcb,
342  Cpu,
343  (PVOID)LoaderBlock);
344 }
345 
346 VOID
347 NTAPI
349 {
350 }
351 
352 void abort(VOID)
353 {
354  KeBugCheck(KMODE_EXCEPTION_NOT_HANDLED);
355  while(1);
356 }
#define MAXIMUM_PROCESSORS
Definition: rwlock.h:5
VOID KiGetCacheInformation()
KTRAP_FRAME KiInitialTrapFrame
Definition: kiinit.c:249
ULONG KeMaximumIncrement
Definition: clock.c:20
VOID NTAPI KiSetupDecrementerTrap(VOID)
Definition: kiinit.c:58
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
#define IN
Definition: typedefs.h:38
#define KIP0PCRADDRESS
Definition: amd64.h:27
ULONG_PTR MmFreeLdrLastKernelAddress
#define TRUE
Definition: types.h:120
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
VOID NTAPI MmInit1(VOID)
KPRCB PrcbData[MAXIMUM_PROCESSORS]
Definition: kiinit.c:27
KNODE KiNode0
Definition: krnlinit.c:38
void abort(VOID)
Definition: kiinit.c:352
INIT_FUNCTION VOID NTAPI KiInitMachineDependent(VOID)
Definition: kiinit.c:46
void __cdecl _enable(void)
Definition: intrin_arm.h:373
KTHREAD Tcb
Definition: pstypes.h:1034
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1062
struct _EPROCESS * PEPROCESS
Definition: nt_native.h:30
struct _KIPCR * PKIPCR
UCHAR Number
Definition: ketypes.h:766
INIT_FUNCTION VOID NTAPI PoInitializePrcb(IN PKPRCB Prcb)
Definition: power.c:396
PKTRAP_FRAME TrapFrame
Definition: ketypes.h:1664
INIT_FUNCTION VOID NTAPI KiInitSpinLocks(IN PKPRCB Prcb, IN CCHAR Number)
Definition: krnlinit.c:187
ULONG KeFeatureBits
Definition: krnlinit.c:22
ULONG KiAdjustDpcThreshold
Definition: dpc.c:21
ULONG KeLoaderModuleCount
int KiPageFaultTrap()
VOID NTAPI KeInitializeProcess(struct _KPROCESS *Process, KPRIORITY Priority, KAFFINITY Affinity, PULONG_PTR DirectoryTableBase, IN BOOLEAN Enable)
#define PRCB_BUILD_DEBUG
Definition: ketypes.h:242
unsigned long paddr_t
Definition: mmu.h:89
ULONG KeMemoryMapRangeCount
#define KeGetPcr()
Definition: ke.h:25
VOID NTAPI KiInitializePcr(IN PKIPCR Pcr, IN ULONG ProcessorNumber, IN PKTHREAD IdleThread, IN PVOID DpcStack)
Definition: kiinit.c:90
uint32_t ULONG_PTR
Definition: typedefs.h:63
ULONG_PTR P0BootStack
Definition: kiinit.c:34
static MmuTrapHandler MmuSetTrapHandler(int trap, MmuTrapHandler cb)
Definition: mmu.h:212
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
VOID NTAPI HalInitializeProcessor(IN ULONG ProcessorNumber, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: processor.c:48
__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")
KAPC_STATE ApcState
Definition: ketypes.h:1668
unsigned long vaddr_t
Definition: mmu.h:90
BOOLEAN NTAPI KdPollBreakIn(VOID)
Definition: kdmain.c:352
smooth NULL
Definition: ftsmooth.c:416
KAFFINITY SetMember
Definition: ketypes.h:763
void DPRINT(...)
Definition: polytest.cpp:61
VOID NTAPI KeBugCheck(ULONG BugCheckCode)
Definition: bug.c:1507
LARGE_INTEGER KiTimeIncrementReciprocal
Definition: timerobj.c:18
UCHAR KiTimeIncrementShiftCount
Definition: timerobj.c:19
#define HIGH_PRIORITY
ULONG_PTR KiDoubleFaultStack
Definition: kiinit.c:35
KIRQL FASTCALL KfRaiseIrql(IN KIRQL NewIrql)
Definition: pic.c:187
USHORT KeProcessorArchitecture
Definition: krnlinit.c:19
#define MAXCHAR
Definition: umtypes.h:112
static void * MmuAllocVsid(int vsid, int mask)
Definition: mmu.h:242
char CCHAR
Definition: typedefs.h:50
NTSTATUS NTAPI NtYieldExecution(VOID)
Definition: thrdschd.c:744
int syscall_end
PKNODE KeNodeBlock[1]
Definition: krnlinit.c:39
ULONG KiMinimumDpcRate
Definition: dpc.c:20
INIT_FUNCTION VOID NTAPI ExpInitializeExecutive(IN ULONG Cpu, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: init.c:918
INIT_FUNCTION VOID NTAPI KiInitSystem(VOID)
Definition: krnlinit.c:71
int KiDecrementerTrapHandler[]
#define KI_USER_SHARED_DATA
#define PAGE_SIZE
Definition: env_spec_w32.h:49
int KiDecrementerTrapHandlerEnd
KPROCESS Pcb
Definition: pstypes.h:1193
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:204
static int MmuMapPage(ppc_map_info_t *info, int count)
Definition: mmu.h:197
KAFFINITY KeActiveProcessors
Definition: krnlinit.c:23
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
EPROCESS KiInitialProcess
Definition: krnlinit.c:45
UINT64 SetMember
Definition: ketypes.h:578
ULONG KiGetFeatureBits()
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
#define PCR_MINOR_VERSION
Definition: ke.h:277
ULONG KiMaximumDpcQueueDepth
Definition: dpc.c:19
ADDRESS_RANGE KeMemoryMap[64]
INIT_FUNCTION LARGE_INTEGER NTAPI KiComputeReciprocal(IN LONG Divisor, OUT PUCHAR Shift)
Definition: krnlinit.c:123
#define HIGH_LEVEL
Definition: env_spec_w32.h:703
VOID FASTCALL KfLowerIrql(IN KIRQL NewIrql)
Definition: pic.c:232
BOOLEAN NTAPI KdInitSystem(ULONG Reserved, PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: kdinit.c:173
KPRIORITY NTAPI KeSetPriorityThread(IN PKTHREAD Thread, IN KPRIORITY Priority)
Definition: thrdobj.c:1327
LOADER_MODULE KeLoaderModules[64]
unsigned short USHORT
Definition: pedump.c:61
GLsizei GLsizei GLchar * source
Definition: glext.h:6048
VOID NTAPI KeInitializeThread(IN PKPROCESS Process, IN OUT PKTHREAD Thread, IN PKSYSTEM_ROUTINE SystemRoutine, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext, IN PCONTEXT Context, IN PVOID Teb, IN PVOID KernelStack)
Definition: thrdobj.c:900
PKPRCB KiProcessorBlock[]
Definition: krnlinit.c:32
ETHREAD KiInitialThread
Definition: krnlinit.c:44
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
VOID KiSetProcessorType()
USHORT MajorVersion
Definition: ketypes.h:598
int syscall_start[]
VOID NTAPI KiSetupSyscallHandler(VOID)
Definition: kiinit.c:44
#define DPRINT1
Definition: precomp.h:8
VOID NTAPI KiSystemStartupReal(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: kiinit.c:254
USHORT KeProcessorLevel
Definition: krnlinit.c:20
#define DBG_STATUS_CONTROL_C
Definition: kdtypes.h:39
void __cdecl _disable(void)
Definition: intrin_arm.h:365
static void MmuSetVsid(int start, int end, int vsid)
Definition: mmu.h:207
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
struct _ETHREAD * PETHREAD
Definition: nt_native.h:29
__analysis_noreturn NTSYSAPI VOID NTAPI DbgBreakPointWithStatus(_In_ ULONG Status)
KAFFINITY ProcessorMask
Definition: ketypes.h:776
PVOID NTAPI MmCreateKernelStack(BOOLEAN GuiStack, UCHAR Node)
#define KeGetCurrentThread
Definition: hal.h:44
void SetPhys(ULONG Addr, ULONG Data)
#define KPCR_BASE
Definition: mboot.c:46
#define APC_LEVEL
Definition: env_spec_w32.h:695
VOID NTAPI KiInitializeKernel(IN PKPROCESS InitProcess, IN PKTHREAD InitThread, IN PVOID IdleStack, IN PKPRCB Prcb, IN CCHAR Number, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: kiinit.c:44
#define PCR_MAJOR_VERSION
Definition: ke.h:278
#define PRCB_MAJOR_VERSION
Definition: ketypes.h:241
VOID NTAPI KeBugCheckEx(_In_ ULONG BugCheckCode, _In_ ULONG_PTR BugCheckParameter1, _In_ ULONG_PTR BugCheckParameter2, _In_ ULONG_PTR BugCheckParameter3, _In_ ULONG_PTR BugCheckParameter4)
Definition: rtlcompat.c:107
LIST_ENTRY KiProcessListHead
Definition: procobj.c:18
LONGLONG QuadPart
Definition: typedefs.h:112
#define MMU_KRW_UR
Definition: mmu.h:77
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31