123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- /*++
- Copyright (c) 2017 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- pcexcept.c
- Abstract:
- This module implements common interrupt dispatch functionality between
- x86 and AMD64 processors.
- Author:
- Evan Green 11-Jun-2017
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/kernel.h>
- #include <minoca/kernel/kdebug.h>
- #if __SIZEOF_LONG__ == 8
- #include <minoca/kernel/x64.h>
- #else
- #include <minoca/kernel/x86.h>
- #endif
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- VOID
- KeDispatchInterrupt (
- PTRAP_FRAME TrapFrame
- )
- /*++
- Routine Description:
- This routine dispatches a device interrupt.
- Arguments:
- Vector - Supplies the vector this interrupt came in on.
- TrapFrame - Supplies a pointer to the machine state immediately before the
- interrupt.
- Return Value:
- None.
- --*/
- {
- CYCLE_ACCOUNT PreviousPeriod;
- ASSERT(ArAreInterruptsEnabled() == FALSE);
- PreviousPeriod = KeBeginCycleAccounting(CycleAccountInterrupt);
- //
- // The vector byte was sign extended, so cast back down to get rid of the
- // high bytes.
- //
- HlDispatchInterrupt((UCHAR)(TrapFrame->ErrorCode), TrapFrame);
- KeBeginCycleAccounting(PreviousPeriod);
- return;
- }
- VOID
- KeDispatchBreakPointTrap (
- PTRAP_FRAME TrapFrame
- )
- /*++
- Routine Description:
- This routine dispatches a breakpoint trap.
- Arguments:
- TrapFrame - Supplies a pointer to the machine state immediately before the
- trap.
- Return Value:
- None.
- --*/
- {
- CYCLE_ACCOUNT PreviousPeriod;
- PKTHREAD Thread;
- ASSERT(ArAreInterruptsEnabled() == FALSE);
- if (IS_TRAP_FRAME_FROM_PRIVILEGED_MODE(TrapFrame) == FALSE) {
- PreviousPeriod = KeBeginCycleAccounting(CycleAccountKernel);
- ArEnableInterrupts();
- Thread = KeGetCurrentThread();
- PsSignalThread(Thread, SIGNAL_TRAP, NULL, TRUE);
- PsCheckRuntimeTimers(Thread);
- PsDispatchPendingSignals(Thread, TrapFrame);
- ArDisableInterrupts();
- KeBeginCycleAccounting(PreviousPeriod);
- } else {
- KdDebugExceptionHandler(EXCEPTION_BREAK, NULL, TrapFrame);
- }
- return;
- }
- VOID
- KeDispatchDivideByZeroTrap (
- PTRAP_FRAME TrapFrame
- )
- /*++
- Routine Description:
- This routine dispatches a divide-by-zero trap.
- Arguments:
- TrapFrame - Supplies a pointer to the machine state immediately before the
- trap.
- Return Value:
- None.
- --*/
- {
- CYCLE_ACCOUNT PreviousPeriod;
- PKTHREAD Thread;
- if (IS_TRAP_FRAME_FROM_PRIVILEGED_MODE(TrapFrame) == FALSE) {
- PreviousPeriod = KeBeginCycleAccounting(CycleAccountKernel);
- ASSERT(ArAreInterruptsEnabled() != FALSE);
- Thread = KeGetCurrentThread();
- PsSignalThread(Thread, SIGNAL_MATH_ERROR, NULL, TRUE);
- PsCheckRuntimeTimers(Thread);
- PsDispatchPendingSignals(Thread, TrapFrame);
- KeBeginCycleAccounting(PreviousPeriod);
- } else {
- KdDebugExceptionHandler(EXCEPTION_DIVIDE_BY_ZERO, NULL, TrapFrame);
- KeCrashSystem(CRASH_DIVIDE_BY_ZERO,
- (UINTN)TrapFrame,
- (UINTN)ArGetInstructionPointer(TrapFrame),
- 0,
- 0);
- }
- ArDisableInterrupts();
- return;
- }
- VOID
- KeDispatchFpuAccessTrap (
- PTRAP_FRAME TrapFrame
- )
- /*++
- Routine Description:
- This routine dispatches a floating point access trap.
- Arguments:
- TrapFrame - Supplies a pointer to the machine state immediately before the
- trap.
- Return Value:
- None.
- --*/
- {
- RUNLEVEL OldRunLevel;
- CYCLE_ACCOUNT PreviousPeriod;
- PKTHREAD Thread;
- //
- // FPU access faults are "trap" type gates, so they shouldn't disable
- // interrupts.
- //
- ASSERT(ArAreInterruptsEnabled() != FALSE);
- PreviousPeriod = KeBeginCycleAccounting(CycleAccountKernel);
- Thread = KeGetCurrentThread();
- //
- // If the thread has never used the FPU before, allocate FPU context while
- // still at low level.
- //
- if (Thread->FpuContext == NULL) {
- ASSERT((Thread->FpuFlags & THREAD_FPU_FLAG_IN_USE) == 0);
- Thread->FpuContext =
- ArAllocateFpuContext(PS_FPU_CONTEXT_ALLOCATION_TAG);
- if (Thread->FpuContext == NULL) {
- PsSignalThread(Thread, SIGNAL_BUS_ERROR, NULL, TRUE);
- goto DispatchFpuAccessTrapEnd;
- }
- }
- OldRunLevel = KeRaiseRunLevel(RunLevelDispatch);
- //
- // Restore context if this is not the thread's first time using the FPU. If
- // the thread happens to already have its state on the CPU, then there's no
- // need to do the restore.
- //
- if ((Thread->FpuFlags & THREAD_FPU_FLAG_IN_USE) != 0) {
- if ((Thread->FpuFlags & THREAD_FPU_FLAG_OWNER) != 0) {
- ArEnableFpu();
- } else {
- ArRestoreFpuState(Thread->FpuContext);
- }
- //
- // If this is the first time using the FPU, enable it, initialize it, and
- // mark the thread as using it. An NMI could come in between the enable
- // and initialize, which would cause the initialize to fault.
- //
- } else {
- ArEnableFpu();
- ArInitializeFpu();
- Thread->FpuFlags |= THREAD_FPU_FLAG_IN_USE;
- }
- Thread->FpuFlags |= THREAD_FPU_FLAG_OWNER;
- KeLowerRunLevel(OldRunLevel);
- DispatchFpuAccessTrapEnd:
- KeBeginCycleAccounting(PreviousPeriod);
- return;
- }
- VOID
- KeDispatchProtectionFault (
- PTRAP_FRAME TrapFrame
- )
- /*++
- Routine Description:
- This routine dispatches a protection fault trap.
- Arguments:
- TrapFrame - Supplies a pointer to the machine state immediately before the
- trap.
- Return Value:
- None.
- --*/
- {
- CYCLE_ACCOUNT PreviousPeriod;
- PKTHREAD Thread;
- if (IS_TRAP_FRAME_FROM_PRIVILEGED_MODE(TrapFrame) == FALSE) {
- PreviousPeriod = KeBeginCycleAccounting(CycleAccountKernel);
- ArEnableInterrupts();
- Thread = KeGetCurrentThread();
- PsHandleUserModeFault(NULL,
- FAULT_FLAG_PROTECTION_FAULT,
- TrapFrame,
- Thread->OwningProcess);
- PsCheckRuntimeTimers(Thread);
- PsDispatchPendingSignals(Thread, TrapFrame);
- KeBeginCycleAccounting(PreviousPeriod);
- } else {
- KdDebugExceptionHandler(EXCEPTION_ACCESS_VIOLATION, NULL, TrapFrame);
- KeCrashSystem(CRASH_PAGE_FAULT,
- (UINTN)TrapFrame,
- (UINTN)ArGetInstructionPointer(TrapFrame),
- 0,
- 0);
- }
- ArDisableInterrupts();
- return;
- }
- VOID
- KeDispatchMathFault (
- PTRAP_FRAME TrapFrame
- )
- /*++
- Routine Description:
- This routine dispatches a math fault from the x87 unit.
- Arguments:
- TrapFrame - Supplies a pointer to the machine state immediately before the
- trap.
- Return Value:
- None.
- --*/
- {
- CYCLE_ACCOUNT PreviousPeriod;
- PKTHREAD Thread;
- ASSERT(ArAreInterruptsEnabled() == FALSE);
- if (IS_TRAP_FRAME_FROM_PRIVILEGED_MODE(TrapFrame) == FALSE) {
- PreviousPeriod = KeBeginCycleAccounting(CycleAccountKernel);
- ArEnableInterrupts();
- Thread = KeGetCurrentThread();
- PsSignalThread(Thread, SIGNAL_MATH_ERROR, NULL, TRUE);
- PsCheckRuntimeTimers(Thread);
- PsDispatchPendingSignals(Thread, TrapFrame);
- KeBeginCycleAccounting(PreviousPeriod);
- } else {
- KdDebugExceptionHandler(EXCEPTION_MATH_FAULT, NULL, TrapFrame);
- KeCrashSystem(CRASH_MATH_FAULT,
- (UINTN)TrapFrame,
- (UINTN)ArGetInstructionPointer(TrapFrame),
- 0,
- 0);
- }
- ArDisableInterrupts();
- return;
- }
- VOID
- KeDispatchPageFault (
- PVOID FaultingAddress,
- PTRAP_FRAME TrapFrame
- )
- /*++
- Routine Description:
- This routine handles page faults.
- Arguments:
- FaultingAddress - Supplies the address that caused the fault.
- TrapFrame - Supplies a pointer to the trap frame of the fault.
- Return Value:
- None.
- --*/
- {
- ULONG FaultFlags;
- CYCLE_ACCOUNT PreviousPeriod;
- PreviousPeriod = KeBeginCycleAccounting(CycleAccountKernel);
- FaultFlags = 0;
- if ((TrapFrame->ErrorCode & X86_FAULT_ERROR_CODE_PRESENT) == 0) {
- FaultFlags |= FAULT_FLAG_PAGE_NOT_PRESENT;
- } else {
- FaultFlags |= FAULT_FLAG_PERMISSION_ERROR;
- }
- if ((TrapFrame->ErrorCode & X86_FAULT_ERROR_CODE_WRITE) != 0) {
- FaultFlags |= FAULT_FLAG_WRITE;
- }
- MmHandleFault(FaultFlags, FaultingAddress, TrapFrame);
- KeBeginCycleAccounting(PreviousPeriod);
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|