[NTOS]: Fix incorrect assumptions that a PDE == PTE which have crept up throughout...
[reactos.git] / reactos / ntoskrnl / ke / arm / trapc.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/ke/arm/trapc.c
5 * PURPOSE: Implements the various trap handlers for ARM exceptions
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <ntoskrnl.h>
12 #include <internal/arm/ksarm.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 /* FUNCTIONS ******************************************************************/
17
18 #if 0
19 VOID
20 KiIdleLoop(VOID)
21 {
22 PKPCR Pcr = (PKPCR)KeGetPcr();
23 PKPRCB Prcb = Pcr->Prcb;
24 PKTHREAD OldThread, NewThread;
25
26 //
27 // Loop forever... that's why this is an idle loop
28 //
29 DPRINT1("[IDLE LOOP]\n");
30 while (TRUE);
31
32 while (TRUE)
33 {
34 //
35 // Cycle interrupts
36 //
37 _disable();
38 _enable();
39
40 //
41 // Check if there's DPC work to do
42 //
43 if ((Prcb->DpcData[0].DpcQueueDepth) ||
44 (Prcb->TimerRequest) ||
45 (Prcb->DeferredReadyListHead.Next))
46 {
47 //
48 // Clear the pending interrupt
49 //
50 HalClearSoftwareInterrupt(DISPATCH_LEVEL);
51
52 //
53 // Retire DPCs
54 //
55 KiRetireDpcList(Prcb);
56 }
57
58 //
59 // Check if there's a thread to schedule
60 //
61 if (Prcb->NextThread)
62 {
63 //
64 // Out with the old, in with the new...
65 //
66 OldThread = Prcb->CurrentThread;
67 NewThread = Prcb->NextThread;
68 Prcb->CurrentThread = NewThread;
69 Prcb->NextThread = NULL;
70
71 //
72 // Update thread state
73 //
74 NewThread->State = Running;
75
76 //
77 // Swap to the new thread
78 // On ARM we call KiSwapContext instead of KiSwapContextInternal,
79 // because we're calling this from C code and not assembly.
80 // This is similar to how it gets called for unwaiting, on x86
81 //
82 KiSwapContext(OldThread, NewThread);
83 }
84 else
85 {
86 //
87 // Go into WFI (sleep more)
88 //
89 KeArmWaitForInterrupt();
90 }
91 }
92 }
93 #endif
94
95 VOID
96 NTAPI
97 KiSwapProcess(IN PKPROCESS NewProcess,
98 IN PKPROCESS OldProcess)
99 {
100 ARM_TTB_REGISTER TtbRegister;
101 DPRINT1("Swapping from: %p (%16s) to %p (%16s)\n",
102 OldProcess, ((PEPROCESS)OldProcess)->ImageFileName,
103 NewProcess, ((PEPROCESS)NewProcess)->ImageFileName);
104
105 //
106 // Update the page directory base
107 //
108 TtbRegister.AsUlong = NewProcess->DirectoryTableBase[0];
109 ASSERT(TtbRegister.Reserved == 0);
110 KeArmTranslationTableRegisterSet(TtbRegister);
111
112 //
113 // FIXME: Flush the TLB
114 //
115
116
117 DPRINT1("Survived!\n");
118 while (TRUE);
119 }
120
121 #if 0
122 BOOLEAN
123 KiSwapContextInternal(IN PKTHREAD OldThread,
124 IN PKTHREAD NewThread)
125 {
126 PKIPCR Pcr = (PKIPCR)KeGetPcr();
127 PKPRCB Prcb = Pcr->Prcb;
128 PKPROCESS OldProcess, NewProcess;
129
130 DPRINT1("SWAP\n");
131 while (TRUE);
132
133 //
134 // Increase context switch count
135 //
136 Pcr->ContextSwitches++;
137
138 //
139 // Check if WMI tracing is enabled
140 //
141 if (Pcr->PerfGlobalGroupMask)
142 {
143 //
144 // We don't support this yet on x86 either
145 //
146 DPRINT1("WMI Tracing not supported\n");
147 ASSERT(FALSE);
148 }
149
150 //
151 // Check if the processes are also different
152 //
153 OldProcess = OldThread->ApcState.Process;
154 NewProcess = NewThread->ApcState.Process;
155 if (OldProcess != NewProcess)
156 {
157 //
158 // Check if address space switch is needed
159 //
160 if (OldProcess->DirectoryTableBase[0] !=
161 NewProcess->DirectoryTableBase[0])
162 {
163 //
164 // FIXME-USER: Support address space switch
165 //
166 DPRINT1("Address space switch not implemented\n");
167 ASSERT(FALSE);
168 }
169 }
170
171 //
172 // Increase thread context switches
173 //
174 NewThread->ContextSwitches++;
175 #if 0 // I don't buy this
176 //
177 // Set us as the current thread
178 // NOTE: On RISC Platforms, there is both a KPCR CurrentThread, and a
179 // KPRCB CurrentThread.
180 // The latter is set just like on x86-based builds, the former is only set
181 // when actually doing the context switch (here).
182 // Recall that the reason for the latter is due to the fact that the KPCR
183 // is shared with user-mode (read-only), so that information is exposed
184 // there as well.
185 //
186 Pcr->CurrentThread = NewThread;
187 #endif
188 //
189 // DPCs shouldn't be active
190 //
191 if (Prcb->DpcRoutineActive)
192 {
193 //
194 // Crash the machine
195 //
196 KeBugCheckEx(ATTEMPTED_SWITCH_FROM_DPC,
197 (ULONG_PTR)OldThread,
198 (ULONG_PTR)NewThread,
199 (ULONG_PTR)OldThread->InitialStack,
200 0);
201 }
202
203 //
204 // Kernel APCs may be pending
205 //
206 if (NewThread->ApcState.KernelApcPending)
207 {
208 //
209 // Are APCs enabled?
210 //
211 if (NewThread->SpecialApcDisable == 0)
212 {
213 //
214 // Request APC delivery
215 //
216 HalRequestSoftwareInterrupt(APC_LEVEL);
217 return TRUE;
218 }
219 }
220
221 //
222 // Return
223 //
224 return FALSE;
225 }
226 #endif
227
228 VOID
229 KiApcInterrupt(VOID)
230 {
231 KPROCESSOR_MODE PreviousMode;
232 KEXCEPTION_FRAME ExceptionFrame;
233 PKTRAP_FRAME TrapFrame = KeGetCurrentThread()->TrapFrame;
234
235 DPRINT1("[APC TRAP]\n");
236 while (TRUE);
237
238 //
239 // Isolate previous mode
240 //
241 PreviousMode = KiGetPreviousMode(TrapFrame);
242
243 //
244 // FIXME-USER: Handle APC interrupt while in user-mode
245 //
246 if (PreviousMode == UserMode) ASSERT(FALSE);
247
248 //
249 // Disable interrupts
250 //
251 _disable();
252
253 //
254 // Clear APC interrupt
255 //
256 HalClearSoftwareInterrupt(APC_LEVEL);
257
258 //
259 // Re-enable interrupts
260 //
261 _enable();
262
263 //
264 // Deliver APCs
265 //
266 KiDeliverApc(PreviousMode, &ExceptionFrame, TrapFrame);
267 }
268
269 #if 0
270 VOID
271 KiDispatchInterrupt(VOID)
272 {
273 PKIPCR Pcr;
274 PKPRCB Prcb;
275 PKTHREAD NewThread, OldThread;
276
277 DPRINT1("[DPC TRAP]\n");
278 while (TRUE);
279
280 //
281 // Get the PCR and disable interrupts
282 //
283 Pcr = (PKIPCR)KeGetPcr();
284 Prcb = Pcr->Prcb;
285 _disable();
286
287 //
288 //Check if we have to deliver DPCs, timers, or deferred threads
289 //
290 if ((Prcb->DpcData[0].DpcQueueDepth) ||
291 (Prcb->TimerRequest) ||
292 (Prcb->DeferredReadyListHead.Next))
293 {
294 //
295 // Retire DPCs
296 //
297 KiRetireDpcList(Prcb);
298 }
299
300 //
301 // Re-enable interrupts
302 //
303 _enable();
304
305 //
306 // Check for quantum end
307 //
308 if (Prcb->QuantumEnd)
309 {
310 //
311 // Handle quantum end
312 //
313 Prcb->QuantumEnd = FALSE;
314 KiQuantumEnd();
315 return;
316 }
317
318 //
319 // Check if we have a thread to swap to
320 //
321 if (Prcb->NextThread)
322 {
323 //
324 // Next is now current
325 //
326 OldThread = Prcb->CurrentThread;
327 NewThread = Prcb->NextThread;
328 Prcb->CurrentThread = NewThread;
329 Prcb->NextThread = NULL;
330
331 //
332 // Update thread states
333 //
334 NewThread->State = Running;
335 OldThread->WaitReason = WrDispatchInt;
336
337 //
338 // Make the old thread ready
339 //
340 KxQueueReadyThread(OldThread, Prcb);
341
342 //
343 // Swap to the new thread
344 // On ARM we call KiSwapContext instead of KiSwapContextInternal,
345 // because we're calling this from C code and not assembly.
346 // This is similar to how it gets called for unwaiting, on x86
347 //
348 KiSwapContext(OldThread, NewThread);
349 }
350 }
351 #endif
352
353 VOID
354 KiInterruptHandler(IN PKTRAP_FRAME TrapFrame,
355 IN ULONG Reserved)
356 {
357 KIRQL OldIrql, Irql;
358 ULONG InterruptCause, InterruptMask;
359 PKIPCR Pcr;
360 PKTRAP_FRAME OldTrapFrame;
361 ASSERT(TrapFrame->DbgArgMark == 0xBADB0D00);
362
363 //
364 // Increment interrupt count
365 //
366 Pcr = (PKIPCR)KeGetPcr();
367 Pcr->Prcb->InterruptCount++;
368
369 //
370 // Get the old IRQL
371 //
372 OldIrql = KeGetCurrentIrql();
373 TrapFrame->OldIrql = OldIrql;
374
375 //
376 // Get the interrupt source
377 //
378 InterruptCause = HalGetInterruptSource();
379 //DPRINT1("[INT] (%x) @ %p %p\n", InterruptCause, TrapFrame->SvcLr, TrapFrame->Pc);
380
381 //
382 // Get the new IRQL and Interrupt Mask
383 //
384 Irql = Pcr->IrqlMask[InterruptCause];
385 InterruptMask = Pcr->IrqlTable[Irql];
386
387 //
388 // Raise to the new IRQL
389 //
390 KfRaiseIrql(Irql);
391
392 //
393 // The clock ISR wants the trap frame as a parameter
394 //
395 OldTrapFrame = KeGetCurrentThread()->TrapFrame;
396 KeGetCurrentThread()->TrapFrame = TrapFrame;
397
398 //
399 // Check if this interrupt is at DISPATCH or higher
400 //
401 if (Irql > DISPATCH_LEVEL)
402 {
403 //
404 // FIXME-TODO: Switch to interrupt stack
405 //
406 //DPRINT1("[ISR]\n");
407 }
408 else
409 {
410 //
411 // We know this is APC or DPC.
412 //
413 //DPRINT1("[DPC/APC]\n");
414 HalClearSoftwareInterrupt(Irql);
415 }
416
417 //
418 // Call the registered interrupt routine
419 //
420 Pcr->InterruptRoutine[Irql]();
421 ASSERT(KeGetCurrentThread()->TrapFrame == TrapFrame);
422 KeGetCurrentThread()->TrapFrame = OldTrapFrame;
423 // DPRINT1("[ISR RETURN]\n");
424
425 //
426 // Restore IRQL and interrupts
427 //
428 KeLowerIrql(OldIrql);
429 _enable();
430 }
431
432 NTSTATUS
433 KiPrefetchAbortHandler(IN PKTRAP_FRAME TrapFrame)
434 {
435 PVOID Address = (PVOID)KeArmFaultAddressRegisterGet();
436 ASSERT(TrapFrame->DbgArgMark == 0xBADB0D00);
437 ULONG Instruction = *(PULONG)TrapFrame->Pc;
438 ULONG DebugType, Parameter0;
439 EXCEPTION_RECORD ExceptionRecord;
440
441 DPRINT1("[PREFETCH ABORT] (%x) @ %p/%p/%p\n",
442 KeArmInstructionFaultStatusRegisterGet(), Address, TrapFrame->SvcLr, TrapFrame->Pc);
443 while (TRUE);
444
445 //
446 // What we *SHOULD* do is look at the instruction fault status register
447 // and see if it's equal to 2 (debug trap). Unfortunately QEMU doesn't seem
448 // to emulate this behaviour properly, so we use a workaround.
449 //
450 //if (KeArmInstructionFaultStatusRegisterGet() == 2)
451 if (Instruction & 0xE1200070) // BKPT
452 {
453 //
454 // Okay, we know this is a breakpoint, extract the index
455 //
456 DebugType = Instruction & 0xF;
457 if (DebugType == BREAKPOINT_PRINT)
458 {
459 //
460 // Debug Service
461 //
462 Parameter0 = TrapFrame->R0;
463 TrapFrame->Pc += sizeof(ULONG);
464 }
465 else
466 {
467 //
468 // Standard INT3 (emulate x86 behavior)
469 //
470 Parameter0 = STATUS_SUCCESS;
471 }
472
473 //
474 // Build the exception record
475 //
476 ExceptionRecord.ExceptionCode = STATUS_BREAKPOINT;
477 ExceptionRecord.ExceptionFlags = 0;
478 ExceptionRecord.ExceptionRecord = NULL;
479 ExceptionRecord.ExceptionAddress = (PVOID)TrapFrame->Pc;
480 ExceptionRecord.NumberParameters = 3;
481
482 //
483 // Build the parameters
484 //
485 ExceptionRecord.ExceptionInformation[0] = Parameter0;
486 ExceptionRecord.ExceptionInformation[1] = TrapFrame->R1;
487 ExceptionRecord.ExceptionInformation[2] = TrapFrame->R2;
488
489 //
490 // Dispatch the exception
491 //
492 KiDispatchException(&ExceptionRecord,
493 NULL,
494 TrapFrame,
495 KiGetPreviousMode(TrapFrame),
496 TRUE);
497
498 //
499 // We're done
500 //
501 return STATUS_SUCCESS;
502 }
503
504 //
505 // Unhandled
506 //
507 UNIMPLEMENTED;
508 ASSERT(FALSE);
509 return STATUS_SUCCESS;
510 }
511
512 NTSTATUS
513 KiDataAbortHandler(IN PKTRAP_FRAME TrapFrame)
514 {
515 NTSTATUS Status;
516 PVOID Address = (PVOID)KeArmFaultAddressRegisterGet();
517 ASSERT(TrapFrame->DbgArgMark == 0xBADB0D00);
518
519 DPRINT1("[ABORT] (%x) @ %p/%p/%p\n",
520 KeArmFaultStatusRegisterGet(), Address, TrapFrame->SvcLr, TrapFrame->Pc);
521 while (TRUE);
522
523 //
524 // Check if this is a page fault
525 //
526 if (KeArmFaultStatusRegisterGet() == 21 || KeArmFaultStatusRegisterGet() == 23)
527 {
528 Status = MmAccessFault(FALSE,
529 Address,
530 KiGetPreviousMode(TrapFrame),
531 TrapFrame);
532 if (Status == STATUS_SUCCESS) return Status;
533 }
534
535 //
536 // Unhandled
537 //
538 UNIMPLEMENTED;
539 ASSERT(FALSE);
540 return STATUS_SUCCESS;
541 }
542
543 VOID
544 KiSoftwareInterruptHandler(IN PKTRAP_FRAME TrapFrame)
545 {
546 PKTHREAD Thread;
547 KPROCESSOR_MODE PreviousMode;
548 ULONG Instruction;
549 ASSERT(TrapFrame->DbgArgMark == 0xBADB0D00);
550
551 DPRINT1("[SWI] @ %p/%p\n", TrapFrame->SvcLr, TrapFrame->Pc);
552 while (TRUE);
553
554 //
555 // Get the current thread
556 //
557 Thread = KeGetCurrentThread();
558
559 //
560 // Isolate previous mode
561 //
562 PreviousMode = KiGetPreviousMode(TrapFrame);
563
564 //
565 // Save old previous mode
566 //
567 TrapFrame->PreviousMode = PreviousMode;
568 TrapFrame->PreviousTrapFrame = (ULONG_PTR)Thread->TrapFrame;
569
570 //
571 // Save previous mode and trap frame
572 //
573 Thread->TrapFrame = TrapFrame;
574 Thread->PreviousMode = PreviousMode;
575
576 //
577 // Read the opcode
578 //
579 Instruction = *(PULONG)(TrapFrame->Pc - sizeof(ULONG));
580
581 //
582 // Call the service call dispatcher
583 //
584 KiSystemService(Thread, TrapFrame, Instruction);
585 }
586
587 NTSTATUS
588 KiUndefinedExceptionHandler(IN PKTRAP_FRAME TrapFrame)
589 {
590 ASSERT(TrapFrame->DbgArgMark == 0xBADB0D00);
591
592 //
593 // This should never happen
594 //
595 DPRINT1("[UNDEF] @ %p/%p\n", TrapFrame->SvcLr, TrapFrame->Pc);
596 UNIMPLEMENTED;
597 ASSERT(FALSE);
598 return STATUS_SUCCESS;
599 }