⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdarm.c

📁 windows ce 3.00 嵌入式操作系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
#endif

    DEBUGMSG(ZONE_SCHEDULE, (L"MDCT: pTh=%8.8lx Pc=%8.8lx Psr=%4.4x GP=%8.8lx Sp=%8.8lx\r\n", pTh, pTh->ctx.Pc, pTh->ctx.Psr, pTh->ctx.R9, pTh->ctx.Sp));
}

// 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) {
    PBYTE buffer;
	LPCWSTR pcmdline;
	
	if (!((ulong)lpStack>>VA_SECTION))
		lpStack = (LPVOID)((ulong)(lpStack) + dwVMBase);
	pTh->dwStackBase = (DWORD)lpStack;
	buffer = (PBYTE)lpStack + cbStack - TLS_MINIMUM_AVAILABLE*4;
	KPlpvTls = pTh->tlsPtr = (PDWORD)buffer;

    // Allocate space for extra parameters on the stack and copy the bytes there.
    buffer -= (buflen+STKMSK)&~STKMSK;
    pcmdline = (LPCWSTR)buffer;
    memcpy(buffer, buf, buflen);

    buffer -= (buflen2+STKMSK)&~STKMSK;
    memcpy(buffer, buf2, buflen2);

	pTh->pOwnerProc->lpszProcName = (LPWSTR)buffer;
	return pcmdline;
}

void MDCreateMainThread2(PTHREAD pTh, DWORD cbStack, LPVOID lpBase, LPVOID lpStart,
	BOOL kmode, ulong p1, ulong p2, ulong buflen, ulong buflen2, ulong p4) {
    LPBYTE buffer;
    LPBYTE arg3; 
	// Allocate space for TLS & arguments to the start function.
	arg3 = (PBYTE)pTh->dwStackBase + cbStack - TLS_MINIMUM_AVAILABLE*4 - ((buflen+STKMSK)&~STKMSK);
	// Allocate space for process name.
	buffer = arg3 - ((buflen2+STKMSK)&~STKMSK);
	DEBUGCHK((LPBYTE)pTh->pOwnerProc->lpszProcName == buffer);
	// Allocate space for arguments to the start function.
	buffer -= sizeof(ulong);
	pTh->ctx.Sp = (ulong)buffer;
	pTh->dwStackBound = pTh->ctx.Sp & ~(PAGE_SIZE-1);
	pTh->ctx.R0 = (ulong)lpStart;
	pTh->ctx.R1 = p1;
	pTh->ctx.R2 = p2;
	pTh->ctx.R3 = (ulong)arg3;
	*(ulong*)buffer = p4;
	pTh->ctx.Lr = 4;
    pTh->ctx.Pc = (ULONG)lpBase;
	pTh->ctx.Psr = (kmode || bAllKMode) ? KERNEL_MODE : USER_MODE;
#if defined(THUMBSUPPORT)
    if ( (pTh->ctx.Pc & 0x01) != 0 ){
        pTh->ctx.Psr |= THUMB_STATE;
    }
#endif

    DEBUGMSG(ZONE_SCHEDULE, (L"MDCMainT: pTh=%8.8lx Pc=%8.8lx Psr=%4.4x TOC=%8.8lx Sp=%8.8lx\r\n", pTh, pTh->ctx.Pc, pTh->ctx.Psr, pTh->ctx.R9, pTh->ctx.Sp));
}

BOOL DoThreadGetContext(HANDLE hTh, PCONTEXT lpContext) {
	PTHREAD pth;
	if (!(pth = HandleToThread(hTh))) {
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
	if (lpContext->ContextFlags & ~CONTEXT_FULL) {
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
	if (pth->pThrdDbg && pth->pThrdDbg->psavedctx) {
		ULONG ulOldAky = CurAKey;
		SETCURKEY((DWORD)-1);
		if (lpContext->ContextFlags & CONTEXT_CONTROL) {
			lpContext->Psr = pth->pThrdDbg->psavedctx->Psr;
			lpContext->Pc = pth->pThrdDbg->psavedctx->Pc;
			lpContext->Lr = pth->pThrdDbg->psavedctx->Lr;
			lpContext->Sp = pth->pThrdDbg->psavedctx->Sp;
		}
		if (lpContext->ContextFlags & CONTEXT_INTEGER) {
			lpContext->R0 = pth->pThrdDbg->psavedctx->R0;
			lpContext->R1 = pth->pThrdDbg->psavedctx->R1;
			lpContext->R2 = pth->pThrdDbg->psavedctx->R2;
			lpContext->R3 = pth->pThrdDbg->psavedctx->R3;
			lpContext->R4 = pth->pThrdDbg->psavedctx->R4;
			lpContext->R5 = pth->pThrdDbg->psavedctx->R5;
			lpContext->R6 = pth->pThrdDbg->psavedctx->R6;
			lpContext->R7 = pth->pThrdDbg->psavedctx->R7;
			lpContext->R8 = pth->pThrdDbg->psavedctx->R8;
			lpContext->R9 = pth->pThrdDbg->psavedctx->R9;
			lpContext->R10 = pth->pThrdDbg->psavedctx->R10;
			lpContext->R11 = pth->pThrdDbg->psavedctx->R11;
			lpContext->R12 = pth->pThrdDbg->psavedctx->R12;
		}
		SETCURKEY(ulOldAky);
	} else {
		if (lpContext->ContextFlags & CONTEXT_CONTROL) {
			lpContext->Psr = pth->ctx.Psr;
			lpContext->Pc = pth->ctx.Pc;
			lpContext->Lr = pth->ctx.Lr;
			lpContext->Sp = pth->ctx.Sp;
		}
		if (lpContext->ContextFlags & CONTEXT_INTEGER) {
			lpContext->R0 = pth->ctx.R0;
			lpContext->R1 = pth->ctx.R1;
			lpContext->R2 = pth->ctx.R2;
			lpContext->R3 = pth->ctx.R3;
			lpContext->R4 = pth->ctx.R4;
			lpContext->R5 = pth->ctx.R5;
			lpContext->R6 = pth->ctx.R6;
			lpContext->R7 = pth->ctx.R7;
			lpContext->R8 = pth->ctx.R8;
			lpContext->R9 = pth->ctx.R9;
			lpContext->R10 = pth->ctx.R10;
			lpContext->R11 = pth->ctx.R11;
			lpContext->R12 = pth->ctx.R12;
		}
	}
	return TRUE;
}

BOOL DoThreadSetContext(HANDLE hTh, const CONTEXT *lpContext) {
	PTHREAD pth;
	if (!(pth = HandleToThread(hTh))) {
		SetLastError(ERROR_INVALID_HANDLE);
		return FALSE;
	}
	if (lpContext->ContextFlags & ~CONTEXT_FULL) {
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
	if (pth->pThrdDbg && pth->pThrdDbg->psavedctx) {
		ULONG ulOldAky = CurAKey;
		SETCURKEY((DWORD)-1);
		if (lpContext->ContextFlags & CONTEXT_CONTROL) {
			pth->pThrdDbg->psavedctx->Psr = (pth->pThrdDbg->psavedctx->Psr & 0xdf) | (lpContext->Psr & ~0xdf);
			pth->pThrdDbg->psavedctx->Pc = lpContext->Pc;
			pth->pThrdDbg->psavedctx->Lr = lpContext->Lr;
			pth->pThrdDbg->psavedctx->Sp = lpContext->Sp;
		}
		if (lpContext->ContextFlags & CONTEXT_INTEGER) {
			pth->pThrdDbg->psavedctx->R0 = lpContext->R0;
			pth->pThrdDbg->psavedctx->R1 = lpContext->R1;
			pth->pThrdDbg->psavedctx->R2 = lpContext->R2;
			pth->pThrdDbg->psavedctx->R3 = lpContext->R3;
			pth->pThrdDbg->psavedctx->R4 = lpContext->R4;
			pth->pThrdDbg->psavedctx->R5 = lpContext->R5;
			pth->pThrdDbg->psavedctx->R6 = lpContext->R6;
			pth->pThrdDbg->psavedctx->R7 = lpContext->R7;
			pth->pThrdDbg->psavedctx->R8 = lpContext->R8;
			pth->pThrdDbg->psavedctx->R9 = lpContext->R9;
			pth->pThrdDbg->psavedctx->R10 = lpContext->R10;
			pth->pThrdDbg->psavedctx->R11 = lpContext->R11;
			pth->pThrdDbg->psavedctx->R12 = lpContext->R12;
		}
		SETCURKEY(ulOldAky);
	} else {
		if (lpContext->ContextFlags & CONTEXT_CONTROL) {
			pth->ctx.Psr = (pth->ctx.Psr & 0xdf) | (lpContext->Psr & ~0xdf);
			pth->ctx.Pc = lpContext->Pc;
			pth->ctx.Lr = lpContext->Lr;
			pth->ctx.Sp = lpContext->Sp;
		}
		if (lpContext->ContextFlags & CONTEXT_INTEGER) {
			pth->ctx.R0 = lpContext->R0;
			pth->ctx.R1 = lpContext->R1;
			pth->ctx.R2 = lpContext->R2;
			pth->ctx.R3 = lpContext->R3;
			pth->ctx.R4 = lpContext->R4;
			pth->ctx.R5 = lpContext->R5;
			pth->ctx.R6 = lpContext->R6;
			pth->ctx.R7 = lpContext->R7;
			pth->ctx.R8 = lpContext->R8;
			pth->ctx.R9 = lpContext->R9;
			pth->ctx.R10 = lpContext->R10;
			pth->ctx.R11 = lpContext->R11;
			pth->ctx.R12 = lpContext->R12;
		}
	}
	return TRUE;
}

//
// Define procedure prototypes for exception filter and termination handler
// execution routines defined in jmpunwnd.s
//

// For ARM the compiler generated exception functions expect the following
// input arguments:
//
//	Exception Filter: r0 = exception pointers, r11 = frame pointer
//
//	Termination Handler: r0 = abnormal term flag, r11 = frame pointer

long __declspec(iw32) __C_ExecuteExceptionFilter(
    PEXCEPTION_POINTERS ExceptionPointers,
    EXCEPTION_FILTER ExceptionFilter,
    ULONG EstablisherFrame);

void __declspec(iw32) __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 = CONTEXT_TO_PROGRAM_COUNTER(ContextRecord);
        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
                // end of the scope as the new control PC address and call
                // the termination handler.
                if (ScopeTable->ScopeRecord[Index].JumpTarget != 0) {
                    if (TargetPc == ScopeTable->ScopeRecord[Index].JumpTarget)
                        break;
                } else {
                    DispatcherContext->ControlPc = ScopeTable->ScopeRecord[Index].EndAddress + 4;
                    TerminationHandler = (TERMINATION_HANDLER)ScopeTable->ScopeRecord[Index].HandlerAddress;
                    __C_ExecuteTerminationHandler(TRUE, TerminationHandler, (ULONG)EstablisherFrame);
                }
            }
        }
    }
    // Continue search for exception or termination handlers.
    return ExceptionContinueSearch;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -