+/**\r
+ Special handler for #DB exception, which will restore the page attributes\r
+ (not-present). It should work with #PF handler which will set pages to\r
+ 'present'.\r
+\r
+ @param ExceptionType Exception type.\r
+ @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+DebugExceptionHandler (\r
+ IN EFI_EXCEPTION_TYPE ExceptionType,\r
+ IN EFI_SYSTEM_CONTEXT SystemContext\r
+ )\r
+{\r
+ UINTN CpuIndex;\r
+ UINTN PFEntry;\r
+ BOOLEAN IsWpEnabled;\r
+\r
+ MpInitLibWhoAmI (&CpuIndex);\r
+\r
+ //\r
+ // Clear last PF entries\r
+ //\r
+ IsWpEnabled = IsReadOnlyPageWriteProtected ();\r
+ if (IsWpEnabled) {\r
+ DisableReadOnlyPageWriteProtect ();\r
+ }\r
+\r
+ for (PFEntry = 0; PFEntry < mPFEntryCount[CpuIndex]; PFEntry++) {\r
+ if (mLastPFEntryPointer[CpuIndex][PFEntry] != NULL) {\r
+ *mLastPFEntryPointer[CpuIndex][PFEntry] &= ~IA32_PG_P;\r
+ }\r
+ }\r
+\r
+ if (IsWpEnabled) {\r
+ EnableReadOnlyPageWriteProtect ();\r
+ }\r
+\r
+ //\r
+ // Reset page fault exception count for next page fault.\r
+ //\r
+ mPFEntryCount[CpuIndex] = 0;\r
+\r
+ //\r
+ // Flush TLB\r
+ //\r
+ CpuFlushTlb ();\r
+\r
+ //\r
+ // Clear TF in EFLAGS\r
+ //\r
+ if (mPagingContext.MachineType == IMAGE_FILE_MACHINE_I386) {\r
+ SystemContext.SystemContextIa32->Eflags &= (UINT32)~BIT8;\r
+ } else {\r
+ SystemContext.SystemContextX64->Rflags &= (UINT64)~BIT8;\r
+ }\r
+}\r
+\r
+/**\r
+ Special handler for #PF exception, which will set the pages which caused\r
+ #PF to be 'present'. The attribute of those pages should be restored in\r
+ the subsequent #DB handler.\r
+\r
+ @param ExceptionType Exception type.\r
+ @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+PageFaultExceptionHandler (\r
+ IN EFI_EXCEPTION_TYPE ExceptionType,\r
+ IN EFI_SYSTEM_CONTEXT SystemContext\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT64 PFAddress;\r
+ PAGE_TABLE_LIB_PAGING_CONTEXT PagingContext;\r
+ PAGE_ATTRIBUTE PageAttribute;\r
+ UINT64 Attributes;\r
+ UINT64 *PageEntry;\r
+ UINTN Index;\r
+ UINTN CpuIndex;\r
+ UINTN PageNumber;\r
+ BOOLEAN NonStopMode;\r
+\r
+ PFAddress = AsmReadCr2 () & ~EFI_PAGE_MASK;\r
+ if (PFAddress < BASE_4KB) {\r
+ NonStopMode = NULL_DETECTION_NONSTOP_MODE ? TRUE : FALSE;\r
+ } else {\r
+ NonStopMode = HEAP_GUARD_NONSTOP_MODE ? TRUE : FALSE;\r
+ }\r
+\r
+ if (NonStopMode) {\r
+ MpInitLibWhoAmI (&CpuIndex);\r
+ GetCurrentPagingContext (&PagingContext);\r
+ //\r
+ // Memory operation cross page boundary, like "rep mov" instruction, will\r
+ // cause infinite loop between this and Debug Trap handler. We have to make\r
+ // sure that current page and the page followed are both in PRESENT state.\r
+ //\r
+ PageNumber = 2;\r
+ while (PageNumber > 0) {\r
+ PageEntry = GetPageTableEntry (&PagingContext, PFAddress, &PageAttribute);\r
+ ASSERT(PageEntry != NULL);\r
+\r
+ if (PageEntry != NULL) {\r
+ Attributes = GetAttributesFromPageEntry (PageEntry);\r
+ if ((Attributes & EFI_MEMORY_RP) != 0) {\r
+ Attributes &= ~EFI_MEMORY_RP;\r
+ Status = AssignMemoryPageAttributes (&PagingContext, PFAddress,\r
+ EFI_PAGE_SIZE, Attributes, NULL);\r
+ if (!EFI_ERROR(Status)) {\r
+ Index = mPFEntryCount[CpuIndex];\r
+ //\r
+ // Re-retrieve page entry because above calling might update page\r
+ // table due to table split.\r
+ //\r
+ PageEntry = GetPageTableEntry (&PagingContext, PFAddress, &PageAttribute);\r
+ mLastPFEntryPointer[CpuIndex][Index++] = PageEntry;\r
+ mPFEntryCount[CpuIndex] = Index;\r
+ }\r
+ }\r
+ }\r
+\r
+ PFAddress += EFI_PAGE_SIZE;\r
+ --PageNumber;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Initialize the serial port before dumping.\r
+ //\r
+ SerialPortInitialize ();\r
+ //\r
+ // Display ExceptionType, CPU information and Image information\r
+ //\r
+ DumpCpuContext (ExceptionType, SystemContext);\r
+ if (!NonStopMode) {\r
+ CpuDeadLoop ();\r
+ }\r
+}\r
+\r