📄 mdppc.c
字号:
case ID_CPAGE_FAULT: // Code page fault
// The general fault handler records the faulting address in
// pexi->excInfo for both data and code page faults
if (ProcessPageFault(er.ExceptionInformation[0], pexi->excInfo)) {
pth->pcstkTop = (PCALLSTACK)(pexi->linkage & ~1);
goto continueExecution;
}
er.ExceptionCode = STATUS_ACCESS_VIOLATION;
er.ExceptionInformation[1] = pexi->excInfo;
er.NumberParameters = 2;
break;
case ID_STACK_FAULT: // Stack overflow
er.ExceptionCode = STATUS_STACK_OVERFLOW;
er.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
break;
}
}
if (er.ExceptionCode != STATUS_BREAKPOINT) {
NKDbgPrintfW(L"Exception %02x00 Thread=%8.8lx Proc=%8.8lx '%s'\r\n",
id, pth, hCurProc, pCurProc->lpszProcName ? pCurProc->lpszProcName : L"");
NKDbgPrintfW(L"AKY=%8.8lx PC=%8.8lx RA=%8.8lx excInfo=%8.8lx\r\n",
pCurThread->aky, pctx->Iar, pctx->Lr, pexi->excInfo);
if (UTlsPtr()[TLSSLOT_KERNEL] & TLSKERN_NOFAULT) {
NKDbgPrintfW(L"TLSKERN_NOFAULT set... bypassing kernel debugger.\r\n");
}
}
// Invoke the kernel debugger to attempt to debug the exception before
// letting the program resolve the condition via SEH.
pth->pcstkTop = (PCALLSTACK)(pexi->linkage & ~1);
if (!UserDbgTrap(&er,pctx,FALSE) && ((UTlsPtr()[TLSSLOT_KERNEL] & TLSKERN_NOFAULT) || !KDTrap(&er, pctx, FALSE))) {
if (er.ExceptionCode == STATUS_BREAKPOINT) {
RETAILMSG(1, (TEXT("DEBUG_BREAK @%8.8lx Ignored.\r\n"), pctx->Iar));
pctx->Iar += 4; // skip over the BREAK instruction
} else {
// Turn the EXCINFO into a CALLSTACK and link it into the
// thread's call list.
((PCALLSTACK)pexi)->akyLast = 0;
bHandled = NKDispatchException(pth, &er, pctx);
if (!bHandled) {
if (!UserDbgTrap(&er, pctx, TRUE) && !KDTrap(&er, pctx, TRUE)) {
// Terminate the process.
RETAILMSG(1, (TEXT("\r\nUnhandled exception %8.8lx:\r\n"),er.ExceptionCode));
DumpFrame(pth, (PCPUCONTEXT)&pctx->Gpr0,id,pexi->excInfo,0);
if (InSysCall()) {
OutputDebugStringW(L"Halting system\r\n");
for (;;)
;
} else {
if (!GET_DEAD(pth)) {
SET_DEAD(pth);
pctx->Iar = (ULONG)pExitThread;
pctx->Gpr3 = 0;
RETAILMSG(1, (TEXT("Terminating thread %8.8lx\r\n"), pth));
} else {
RETAILMSG(1, (TEXT("Can't terminate thread %8.8lx, sleeping forever\r\n"), pth));
SurrenderCritSecs();
Sleep(INFINITE);
DEBUGCHK(0); // should never get here
}
}
}
}
}
}
if ((id == ID_CPAGE_FAULT) || (id == ID_DPAGE_FAULT))
GuardCommit(pexi->excInfo);
continueExecution:
// If returning from handling a stack overflow, reset the thread's stack
// overflow flag. It would be good to free the tail of the stack at this
// time so that the thread will stack fault again if the stack gets too
// big. But we are currently using that stack page.
if (id == ID_STACK_FAULT)
CLEAR_STACKFAULT(pth);
if (GET_DYING(pth) && !GET_DEAD(pth) && (pCurProc == pth->pOwnerProc)) {
SET_DEAD(pth);
CLEAR_USERBLOCK(pth);
CLEAR_DEBUGWAIT(pth);
pctx->Iar = (ULONG)pExitThread;
pctx->Gpr3 = 0;
}
}
#if PPC821
#define WriteAccess(entry) ((entry & PG_PROTECTION) == PG_PROT_WRITE)
#else
#define WriteAccess(entry) ((entry & PG_PROT_WRITE) == PG_PROT_WRITE)
#endif
LPVOID VerifyAccess(LPVOID pvAddr, DWORD dwFlags, ACCESSKEY aky) {
PSECTION pscn;
MEMBLOCK *pmb;
ulong entry;
if ((long)pvAddr >= 0) {
if ((pscn = SectionTable[(ulong)pvAddr>>VA_SECTION]) &&
(pmb = (*pscn)[((ulong)pvAddr>>VA_BLOCK)&BLOCK_MASK]) &&
(pmb != RESERVED_BLOCK) && (pmb->alk & aky) &&
((entry = pmb->aPages[((ulong)pvAddr>>VA_PAGE)&PAGE_MASK]) & PG_VALID_MASK) &&
(!(dwFlags & VERIFY_WRITE_FLAG) || WriteAccess(entry)))
return Phys2Virt(PFNfromEntry(entry) | ((ulong)pvAddr & (PAGE_SIZE-1)));
// Access to the Kernel Page. (4K data area at address KPAGE_BASE)
if (((ulong)pvAddr >> 12) == (KPAGE_BASE >> 12))
return(pvAddr);
} else {
// Kernel mode only address. If the "kernel mode OK" flag is set or if the
// thread is running in kernel mode, allow the access.
if (((dwFlags & VERIFY_KERNEL_OK) || (GetThreadMode(pCurThread) == KERNEL_MODE)) && ((ulong)pvAddr < 0xC0000000))
return pvAddr;
}
return 0;
}
/* Machine dependent thread creation */
#define STKALIGN 8
#define STKMSK (STKALIGN-1)
// normal thread stack: from top, TLS then args then free
void MDCreateThread(PTHREAD pTh, LPVOID lpStack, DWORD cbStack, LPVOID lpBase,
LPVOID lpStart, DWORD dwVMBase, BOOL kmode, ulong param) {
if (!((ulong)lpStack>>VA_SECTION))
lpStack = (LPVOID)((ulong)lpStack + dwVMBase);
pTh->dwStackBase = (DWORD)lpStack;
// Leave room for arguments and TLS on the stack
pTh->ctx.Gpr1 = (ulong)lpStack + cbStack - (TLS_MINIMUM_AVAILABLE*4)
- sizeof(STACK_FRAME_HEADER);
pTh->dwStackBound = pTh->ctx.Gpr1 & ~(PAGE_SIZE-1);
pTh->tlsPtr = (LPDWORD)((ulong)lpStack+cbStack-(TLS_MINIMUM_AVAILABLE*4));
pTh->ctx.Gpr3 = (ulong)lpStart;
pTh->ctx.Gpr4 = param;
pTh->ctx.Lr = 4;
pTh->ctx.Iar = (ULONG)lpBase;
pTh->ctx.Msr = (kmode || bAllKMode) ? KERNEL_MSR : USER_MSR;
DEBUGMSG(ZONE_SCHEDULE, (L"MDCT: pTh=%8.8lx Iar=%8.8lx Msr=%4.4x GP=%8.8lx Sp=%8.8lx\r\n", pTh, pTh->ctx.Iar, pTh->ctx.Msr, pTh->ctx.Gpr2, pTh->ctx.Gpr1));
}
// main thread stack: from top, TLS then buf then buf2 then buf2 (ascii) then args then free
LPCWSTR MDCreateMainThread1(PTHREAD pTh, LPVOID lpStack, DWORD cbStack, DWORD dwVMBase,
LPBYTE buf, ulong buflen, LPBYTE buf2, ulong buflen2) {
LPCWSTR pcmdline;
if (!((ulong)lpStack>>VA_SECTION))
lpStack = (LPVOID)((ulong)lpStack + dwVMBase);
pTh->dwStackBase = (DWORD)lpStack;
pcmdline = (LPCWSTR)((LPBYTE)lpStack+cbStack-(TLS_MINIMUM_AVAILABLE*4)-((buflen+STKMSK)&~STKMSK));
memcpy((LPBYTE)lpStack+cbStack-(TLS_MINIMUM_AVAILABLE*4)-((buflen+STKMSK)&~STKMSK),
buf,buflen);
memcpy((LPBYTE)lpStack+cbStack-(TLS_MINIMUM_AVAILABLE*4)-((buflen+STKMSK)&~STKMSK)-
((buflen2+STKMSK)&~STKMSK),buf2,buflen2);
KPlpvTls = pTh->tlsPtr = (LPDWORD)((ulong)lpStack+cbStack-(TLS_MINIMUM_AVAILABLE*4));
pTh->pOwnerProc->lpszProcName = (LPWSTR)((ulong)lpStack + cbStack
- (TLS_MINIMUM_AVAILABLE*4) - ((buflen+STKMSK)&~STKMSK) -
((buflen2+STKMSK)&~STKMSK));
return pcmdline;
}
void MDCreateMainThread2(PTHREAD pTh, DWORD cbStack, LPVOID lpBase, LPVOID lpStart,
BOOL kmode, ulong p1, ulong p2, ulong buflen, ulong buflen2, ulong p4) {
// Leave room for arguments on the stack
pTh->ctx.Gpr1 = pTh->dwStackBase + cbStack - (TLS_MINIMUM_AVAILABLE*4) -
sizeof(STACK_FRAME_HEADER) - ((buflen+STKMSK)&~STKMSK) -
((buflen2+STKMSK)&~STKMSK);
pTh->dwStackBound = pTh->ctx.Gpr1 & ~(PAGE_SIZE-1);
pTh->ctx.Gpr3 = (ulong)lpStart;
pTh->ctx.Gpr4 = p1;
pTh->ctx.Gpr5 = p2;
pTh->ctx.Gpr6 = pTh->dwStackBase+cbStack-(TLS_MINIMUM_AVAILABLE*4)-((buflen+STKMSK)&~STKMSK);
pTh->ctx.Gpr7 = p4;
pTh->ctx.Lr = 4;
pTh->ctx.Iar = (ULONG)lpBase;
pTh->ctx.Msr = (kmode || bAllKMode) ? KERNEL_MSR : USER_MSR;
DEBUGMSG(ZONE_SCHEDULE, (L"MDCMainT: pTh=%8.8lx Iar=%8.8lx Msr=%4.4x GP=%8.8lx Sp=%8.8lx\r\n", pTh, pTh->ctx.Iar, pTh->ctx.Msr, pTh->ctx.Gpr2, pTh->ctx.Gpr1));
}
LONG __C_ExecuteExceptionFilter (
PEXCEPTION_POINTERS ExceptionPointers,
EXCEPTION_FILTER ExceptionFilter,
ULONG EstablisherFrame);
VOID __C_ExecuteTerminationHandler (
BOOLEAN AbnormalTermination,
TERMINATION_HANDLER TerminationHandler,
ULONG EstablisherFrame);
/*++
Routine Description:
This function scans the scope tables associated with the specified
procedure and calls exception and termination handlers as necessary.
Arguments:
ExceptionRecord - Supplies a pointer to an exception record.
EstablisherFrame - Supplies a pointer to frame of the establisher function.
ContextRecord - Supplies a pointer to a context record.
DispatcherContext - Supplies a pointer to the exception dispatcher or
unwind dispatcher context.
Return Value:
If the exception is handled by one of the exception filter routines, then
there is no return from this routine and RtlUnwind is called. Otherwise,
an exception disposition value of continue execution or continue search is
returned.
--*/
EXCEPTION_DISPOSITION __C_specific_handler(
IN PEXCEPTION_RECORD ExceptionRecord,
IN PVOID EstablisherFrame,
IN OUT PCONTEXT ContextRecord,
IN OUT PDISPATCHER_CONTEXT DispatcherContext) {
ULONG ControlPc;
EXCEPTION_FILTER ExceptionFilter;
EXCEPTION_POINTERS ExceptionPointers;
PRUNTIME_FUNCTION FunctionEntry;
ULONG Index;
PSCOPE_TABLE ScopeTable;
ULONG TargetPc;
TERMINATION_HANDLER TerminationHandler;
LONG Value;
// Get address of where control left the establisher, the address of the
// function table entry that describes the function, and the address of
// the scope table.
ControlPc = DispatcherContext->ControlPc;
FunctionEntry = DispatcherContext->FunctionEntry;
ScopeTable = (PSCOPE_TABLE)(FunctionEntry->HandlerData);
// If an unwind is not in progress, then scan the scope table and call
// the appropriate exception filter routines. Otherwise, scan the scope
// table and call the appropriate termination handlers using the target
// PC obtained from the context record.
// are called.
if (IS_DISPATCHING(ExceptionRecord->ExceptionFlags)) {
// Scan the scope table and call the appropriate exception filter
// routines.
ExceptionPointers.ExceptionRecord = ExceptionRecord;
ExceptionPointers.ContextRecord = ContextRecord;
for (Index = 0; Index < ScopeTable->Count; Index += 1) {
if ((ControlPc >= ScopeTable->ScopeRecord[Index].BeginAddress) &&
(ControlPc < ScopeTable->ScopeRecord[Index].EndAddress) &&
(ScopeTable->ScopeRecord[Index].JumpTarget != 0)) {
// Call the exception filter routine.
ExceptionFilter =
(EXCEPTION_FILTER)ScopeTable->ScopeRecord[Index].HandlerAddress;
Value = __C_ExecuteExceptionFilter(&ExceptionPointers,
ExceptionFilter,
(ULONG)EstablisherFrame);
// If the return value is less than zero, then dismiss the
// exception. Otherwise, if the value is greater than zero,
// then unwind to the target exception handler. Otherwise,
// continue the search for an exception filter.
if (Value < 0)
return ExceptionContinueExecution;
if (Value > 0) {
DispatcherContext->ControlPc = ScopeTable->ScopeRecord[Index].JumpTarget;
return ExceptionExecuteHandler;
}
}
}
} else {
// Scan the scope table and call the appropriate termination handler
// routines.
TargetPc = ContextRecord->Iar;
for (Index = 0; Index < ScopeTable->Count; Index += 1) {
if ((ControlPc >= ScopeTable->ScopeRecord[Index].BeginAddress) &&
(ControlPc < ScopeTable->ScopeRecord[Index].EndAddress)) {
// If the target PC is within the same scope the control PC
// is within, then this is an uplevel goto out of an inner try
// scope or a long jump back into a try scope. Terminate the
// scan termination handlers.
//
// N.B. The target PC can be just beyond the end of the scope,
// in which case it is a leave from the scope.
if ((TargetPc >= ScopeTable->ScopeRecord[Index].BeginAddress) &&
(TargetPc < ScopeTable->ScopeRecord[Index].EndAddress))
break;
// If the scope table entry describes an exception filter
// and the associated exception handler is the target of
// the unwind, then terminate the scan for termination
// handlers. Otherwise, if the scope table entry describes
// a termination handler, then record the address of the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -