* Sync up to trunk head (r65147).
[reactos.git] / ntoskrnl / ke / powerpc / kiinit.c
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];
25 extern ULONG KeLoaderModuleCount;
26 extern ULONG_PTR MmFreeLdrLastKernelAddress;
27 KPRCB PrcbData[MAXIMUM_PROCESSORS];
28 /* BIOS Memory Map. Not NTLDR-compliant yet */
29 extern ULONG KeMemoryMapRangeCount;
30 extern ADDRESS_RANGE KeMemoryMap[64];
31
32 /* FUNCTIONS *****************************************************************/
33 /*
34 * Trap frame:
35 * r0 .. r32
36 * lr, ctr, srr0, srr1, dsisr
37 */
38
39 extern int syscall_start[], syscall_end, KiDecrementerTrapHandler[],
40 KiDecrementerTrapHandlerEnd;
41
42 VOID
43 NTAPI
44 KiSetupSyscallHandler()
45 {
46 paddr_t handler_target;
47 int *source;
48 for(source = syscall_start, handler_target = 0xc00;
49 source < &syscall_end;
50 source++, handler_target += sizeof(int))
51 SetPhys(handler_target, *source);
52 }
53
54 VOID
55 NTAPI
56 KiSetupDecrementerTrap()
57 {
58 paddr_t handler_target;
59 int *source;
60
61 /* Turn off EE bit while redefining dec trap */
62 _disable();
63
64 for(source = KiDecrementerTrapHandler, handler_target = 0x900;
65 source != &KiDecrementerTrapHandlerEnd;
66 source++, handler_target += sizeof(int))
67 SetPhys(handler_target, *source);
68
69 DPRINT("CurrentThread %08x IdleThread %08x\n",
70 KeGetCurrentThread(), KeGetCurrentPrcb()->IdleThread);
71
72 /* Kick decmrenter! */
73 __asm__("mtdec %0" : : "r" (0));
74
75 /* Enable interrupts! */
76 _enable();
77 }
78
79 VOID
80 NTAPI
81 KiInitializePcr(IN ULONG ProcessorNumber,
82 IN PKIPCR Pcr,
83 IN PKTHREAD IdleThread,
84 IN PVOID DpcStack)
85 {
86 Pcr->MajorVersion = PCR_MAJOR_VERSION;
87 Pcr->MinorVersion = PCR_MINOR_VERSION;
88 Pcr->CurrentIrql = PASSIVE_LEVEL;
89 Pcr->PrcbData = &PrcbData[ProcessorNumber];
90 Pcr->PrcbData->MajorVersion = PRCB_MAJOR_VERSION;
91 Pcr->PrcbData->MinorVersion = 0;
92 Pcr->PrcbData->Number = 0; /* UP for now */
93 Pcr->PrcbData->SetMember = 1;
94 #if DBG
95 Pcr->PrcbData->BuildType = PRCB_BUILD_DEBUG;
96 #else
97 Pcr->PrcbData->BuildType = 0;
98 #endif
99 Pcr->PrcbData->DpcStack = DpcStack;
100 KeGetPcr()->Prcb = Pcr->PrcbData;
101 KiProcessorBlock[ProcessorNumber] = Pcr->PrcbData;
102 }
103
104 extern ULONG KiGetFeatureBits();
105 extern VOID KiSetProcessorType();
106 extern VOID KiGetCacheInformation();
107
108 VOID
109 NTAPI
110 KiInitializeKernel(IN PKPROCESS InitProcess,
111 IN PKTHREAD InitThread,
112 IN PVOID IdleStack,
113 IN PKPRCB Prcb,
114 IN CCHAR Number,
115 IN PLOADER_PARAMETER_BLOCK LoaderBlock)
116 {
117 ULONG FeatureBits;
118 LARGE_INTEGER PageDirectory;
119 PVOID DpcStack;
120
121 /* Detect and set the CPU Type */
122 KiSetProcessorType();
123
124 /* Initialize the Power Management Support for this PRCB */
125 PoInitializePrcb(Prcb);
126
127 /* Get the processor features for the CPU */
128 FeatureBits = KiGetFeatureBits();
129
130 /* Save feature bits */
131 Prcb->FeatureBits = FeatureBits;
132
133 /* Get cache line information for this CPU */
134 KiGetCacheInformation();
135
136 /* Initialize spinlocks and DPC data */
137 KiInitSpinLocks(Prcb, Number);
138
139 /* Check if this is the Boot CPU */
140 if (!Number)
141 {
142 /* Set Node Data */
143 KeNodeBlock[0] = &KiNode0;
144 Prcb->ParentNode = KeNodeBlock[0];
145 KeNodeBlock[0]->ProcessorMask = Prcb->SetMember;
146
147 /* Set boot-level flags */
148 KeProcessorArchitecture = 0;
149 KeProcessorLevel = (USHORT)Prcb->CpuType;
150 KeFeatureBits = FeatureBits;
151
152 /* Set the current MP Master KPRCB to the Boot PRCB */
153 Prcb->MultiThreadSetMaster = Prcb;
154
155 /* Lower to APC_LEVEL */
156 KeLowerIrql(APC_LEVEL);
157
158 /* Initialize portable parts of the OS */
159 KiInitSystem();
160
161 /* Initialize the Idle Process and the Process Listhead */
162 InitializeListHead(&KiProcessListHead);
163 PageDirectory.QuadPart = 0;
164 KeInitializeProcess(InitProcess,
165 0,
166 0xFFFFFFFF,
167 &PageDirectory,
168 TRUE);
169 InitProcess->QuantumReset = MAXCHAR;
170 }
171 else
172 {
173 /* FIXME */
174 DPRINT1("SMP Boot support not yet present\n");
175 }
176
177 /* Setup the Idle Thread */
178 KeInitializeThread(InitProcess,
179 InitThread,
180 NULL,
181 NULL,
182 NULL,
183 NULL,
184 NULL,
185 IdleStack);
186 InitThread->NextProcessor = Number;
187 InitThread->Priority = HIGH_PRIORITY;
188 InitThread->State = Running;
189 InitThread->Affinity = 1 << Number;
190 InitThread->WaitIrql = DISPATCH_LEVEL;
191 InitProcess->ActiveProcessors = 1 << Number;
192
193 /* HACK for MmUpdatePageDir */
194 ((PETHREAD)InitThread)->ThreadsProcess = (PEPROCESS)InitProcess;
195
196 /* Set up the thread-related fields in the PRCB */
197 Prcb->CurrentThread = InitThread;
198 Prcb->NextThread = NULL;
199 Prcb->IdleThread = InitThread;
200
201 /* Initialize Kernel Memory Address Space */
202 MmInit1(MmFreeLdrFirstKrnlPhysAddr,
203 MmFreeLdrLastKrnlPhysAddr,
204 MmFreeLdrLastKernelAddress,
205 KeMemoryMap,
206 KeMemoryMapRangeCount,
207 4096);
208
209 /* Initialize the Kernel Executive */
210 ExpInitializeExecutive(0, LoaderBlock);
211
212 /* Only do this on the boot CPU */
213 if (!Number)
214 {
215 /* Calculate the time reciprocal */
216 KiTimeIncrementReciprocal =
217 KiComputeReciprocal(KeMaximumIncrement,
218 &KiTimeIncrementShiftCount);
219
220 /* Update DPC Values in case they got updated by the executive */
221 Prcb->MaximumDpcQueueDepth = KiMaximumDpcQueueDepth;
222 Prcb->MinimumDpcRate = KiMinimumDpcRate;
223 Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
224
225 /* Allocate the DPC Stack */
226 DpcStack = MmCreateKernelStack(FALSE, 0);
227 if (!DpcStack) KeBugCheckEx(NO_PAGES_AVAILABLE, 1, 0, 0, 0);
228 Prcb->DpcStack = DpcStack;
229 }
230
231 KfRaiseIrql(DISPATCH_LEVEL);
232
233 KeSetPriorityThread(InitThread, 0);
234 /* Setup decrementer exception */
235 KiSetupDecrementerTrap();
236
237 KfLowerIrql(PASSIVE_LEVEL);
238
239 /* Should not return */
240 while(1)
241 {
242 NtYieldExecution();
243 }
244 }
245
246 extern int KiPageFaultTrap();
247 KTRAP_FRAME KiInitialTrapFrame;
248
249 /* Use this for early boot additions to the page table */
250 VOID
251 NTAPI
252 KiSystemStartupReal(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
253 {
254 ULONG Cpu;
255 ppc_map_info_t info[4];
256 PKIPCR Pcr = (PKIPCR)KPCR_BASE;
257 PKPRCB Prcb;
258
259 __asm__("mr 13,%0" : : "r" (KPCR_BASE));
260
261 /* Set the page fault handler to the kernel */
262 MmuSetTrapHandler(3,KiPageFaultTrap);
263 MmuSetTrapHandler(4,KiPageFaultTrap);
264
265 // Make 0xf... special
266 MmuAllocVsid(2, 0x8000);
267 MmuSetVsid(15,16,2);
268
269 /* Get the current CPU */
270 Cpu = KeNumberProcessors;
271 if (!Cpu)
272 {
273 /* We'll allocate a page from the end of the kernel area for KPCR. This code will probably
274 * change when we get SMP support.
275 */
276 info[0].phys = 0;
277 info[0].proc = 2;
278 info[0].addr = (vaddr_t)Pcr;
279 info[0].flags = MMU_KRW_UR;
280 info[1].phys = 0;
281 info[1].proc = 2;
282 info[1].addr = ((vaddr_t)Pcr) + (1 << PAGE_SHIFT);
283 info[1].flags = MMU_KRW_UR;
284 info[2].phys = 0;
285 info[2].proc = 2;
286 info[2].addr = (vaddr_t)KI_USER_SHARED_DATA;
287 info[2].flags = MMU_KRW_UR;
288 info[3].phys = 0;
289 info[3].proc = 2;
290 info[3].addr = (vaddr_t)KIP0PCRADDRESS;
291 info[3].flags = MMU_KRW_UR;
292 MmuMapPage(info, 4);
293 }
294
295 /* Skip initial setup if this isn't the Boot CPU */
296 if (Cpu) goto AppCpuInit;
297
298 /* Initialize the PCR */
299 RtlZeroMemory(Pcr, PAGE_SIZE);
300 KiInitializePcr(Cpu,
301 Pcr,
302 &KiInitialThread.Tcb,
303 KiDoubleFaultStack);
304
305 /* Set us as the current process */
306 KiInitialThread.Tcb.ApcState.Process = &KiInitialProcess.Pcb;
307 KiInitialThread.Tcb.TrapFrame = &KiInitialTrapFrame;
308
309 /* Setup CPU-related fields */
310 AppCpuInit:
311 Pcr->Number = Cpu;
312 Pcr->SetMember = 1 << Cpu;
313 Prcb = KeGetCurrentPrcb();
314 Prcb->SetMember = 1 << Cpu;
315
316 /* Initialize the Processor with HAL */
317 HalInitializeProcessor(Cpu, LoaderBlock);
318
319 /* Set active processors */
320 KeActiveProcessors |= Pcr->SetMember;
321 KeNumberProcessors++;
322
323 /* Initialize the Debugger for the Boot CPU */
324 if (!Cpu) KdInitSystem (0, LoaderBlock);
325
326 /* Check for break-in */
327 if (KdPollBreakIn())
328 {
329 DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C);
330 }
331
332 /* Raise to HIGH_LEVEL */
333 KfRaiseIrql(HIGH_LEVEL);
334
335 /* Call main kernel intialization */
336 KiInitializeKernel(&KiInitialProcess.Pcb,
337 &KiInitialThread.Tcb,
338 P0BootStack,
339 Prcb,
340 Cpu,
341 (PVOID)LoaderBlock);
342 }
343
344 VOID
345 NTAPI
346 KiInitMachineDependent(VOID)
347 {
348 }
349
350 void abort()
351 {
352 KeBugCheck(KMODE_EXCEPTION_NOT_HANDLED);
353 while(1);
354 }