kmisc.c

来自「WinCE5.0部分核心源码」· C语言 代码 · 共 1,872 行 · 第 1/5 页

C
1,872
字号




//------------------------------------------------------------------------------
// @func HANDLE | GetProcFromPtr | Returns the process id which owns the pointer passed in
// @parm LPVOID | ptr | pointer from which to find a process
// @rdesc  Returns the process id of the owning process
// @comm Returns the owner process of the pointer, or NULL if the pointer is not valid.
//------------------------------------------------------------------------------
HANDLE
SC_GetProcFromPtr(
    LPVOID lpv
    )
{
    HANDLE hRet = hCurProc;
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetProcFromPtr entry: %8.8lx\r\n",lpv));
    if (ZeroPtr(lpv) == (DWORD)lpv) {
        if (IsModCodeAddr (lpv)) {
            HANDLE hCaller = SC_GetCallerProcess ();
            hRet = hCaller? hCaller : hCurProc;
        }
    } else if (IsSecureVa(lpv)) {
        hRet = ProcArray[0].hProc;
    } else {
        int idx = ((DWORD)lpv>>VA_SECTION)-1;
        hRet = ((idx >= MAX_PROCESSES) || !ProcArray[idx].dwVMBase)
            ? 0 : ProcArray[idx].hProc;
    }
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetProcFromPtr exit :%8.8lx\r\n", hRet));
    return hRet;
}




//------------------------------------------------------------------------------
// @func LPVOID | MapPtrToProcess | Maps an unmapped pointer to a mapped pointer in a process
// @parm LPVOID | lpv | pointer to map
// @parm HANDLE | hProc | process to map into
// @rdesc  Returns a mapped version of the pointer, or 0 for failure
// @comm If the pointer is already mapped, the original pointer is returned if the caller
//       has access to dereference that pointer, else 0 is returned.  If the pointer is
//       unmapped, it first maps it, then returns the mapped pointer if the caller can access
//       it, else 0.  This function should be called to map pointers which are passed to a PSL where the pointer is not
//       a parameter directly, but obtained from a structure, and needs to be adjusted for the address space.
// @xref <f MapPtrToProcess>
//------------------------------------------------------------------------------
LPVOID
SC_MapPtrToProcess(
    LPVOID lpv,
    HANDLE hProc
    )
{
    PPROCESS pProc;
    DEBUGMSG(ZONE_ENTRY,(L"SC_MapPtrToProcess entry: %8.8lx %8.8lx\r\n",lpv,hProc));
    if (!(pProc = HandleToProc(hProc))) {
        KSetLastError(pCurThread,ERROR_INVALID_PARAMETER);
        lpv = 0;
    } else if ((DWORD)lpv>>VA_SECTION != 0) {
        if (!IsAccessOK(lpv,CurAKey)) {
            KSetLastError(pCurThread,ERROR_ACCESS_DENIED);
            lpv = 0;
        }
    } else if ((DWORD)lpv > 0x10000)
        lpv = MapPtrProc(lpv,pProc);
    DEBUGMSG(ZONE_ENTRY,(L"SC_MapPtrToProcess exit: %8.8lx\r\n",lpv));
    return lpv;
}

LPVOID SC_MapPtrWithSize (LPVOID ptr, DWORD dwLen, HANDLE hProc)
{
    DWORD dwAddr = (DWORD) SC_MapPtrToProcess (ptr, hProc);
    DWORD dwEnd;
    PPROCESS pProc;

    // fail if we don't have a caller process
    if (!(pProc = HandleToProc(hProc))) {
        KSetLastError(pCurThread,ERROR_INVALID_PARAMETER);
        return 0;
    }

    // dwLen == 0 is treated as dwLen == 1
    if (!dwLen)
        dwLen = 1;

    dwEnd = dwAddr + dwLen - 1;

    if ((KERN_TRUST_FULL != pProc->bTrustLevel) && !(KTHRDINFO(pCurThread) & UTLS_NKCALLOUT)) {
        ACCESSKEY aky = pProc->aky | ProcArray[0].aky;

        // not valid if it overlapped with any KMode only address
        if ((dwEnd < dwAddr)                        // address wrap around
            || ((int) dwAddr < 0x10000)             // invalid start address
            || ((int) dwEnd < 0x10000)              // invalid end address
            || !IsAccessOK ((LPVOID) dwAddr, aky)
            || !IsAccessOK ((LPVOID) dwEnd, aky)) {

            KSetLastError(pCurThread,ERROR_INVALID_PARAMETER);
            return 0;
        }
    }

    return (LPVOID) dwAddr;
}

LPVOID SC_MapCallerPtr (LPVOID ptr, DWORD dwLen)
{
    HANDLE hCaller = SC_GetCallerProcess ();
    if (!hCaller)
        hCaller = hCurProc;
    return SC_MapPtrWithSize (ptr, dwLen, hCaller);
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
DWORD
SC_GetProcAddrBits(
    HANDLE hproc
    )
{
    PPROCESS pproc;
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetProcAddrBits entry: %8.8lx\r\n",hproc));
    if (!(pproc = HandleToProc(hproc))) {
        KSetLastError(pCurThread,ERROR_INVALID_PARAMETER);
        DEBUGMSG(ZONE_ENTRY,(L"SC_GetProcAddrBits exit: %8.8lx\r\n",0));
        return 0;
    }
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetProcAddrBits exit: %8.8lx\r\n",pproc->dwVMBase));
    return pproc->dwVMBase;
}




//------------------------------------------------------------------------------
// @func DWORD | GetFSHeapInfo | Gets info on the physical space reserved for the file system
// @comm Retrieves the start of the physical memory reserved for the file system
//------------------------------------------------------------------------------
DWORD
SC_GetFSHeapInfo(void)
{
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetFSHeapInfo entry\r\n"));
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetFSHeapInfo exit: %8.8lx\r\n",PAGEALIGN_UP(pTOC->ulRAMFree + MemForPT)));
    return PAGEALIGN_UP(pTOC->ulRAMFree+MemForPT);
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void
SC_UpdateNLSInfo(
    DWORD ocp,
    DWORD acp,
    DWORD sysloc,
    DWORD userloc
    )
{

    KInfoTable[KINX_NLS_CP] = (DWORD)(((WORD)ocp << 16) + (WORD)acp);
    KInfoTable[KINX_NLS_SYSLOC] = sysloc;
    KInfoTable[KINX_NLS_USERLOC] = userloc;
}

DWORD randdw1, randdw2;



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
__int64
SC_CeGetRandomSeed()
{
    return (((__int64)randdw1)<<32) | (__int64)randdw2;
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
DWORD
SC_GetIdleTime(void)
{
    DWORD result;
    __int64 temp;
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetIdleTime entry\r\n"));
    temp = curridlehigh;
    if (idleconv) {
        temp = (temp * 0x100000000) + curridlelow;
        result = (DWORD)(temp/idleconv);
    } else
        result = 0xffffffff;
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetIdleTime exit: %8.8lx\r\n",result));
    return result;
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
LPCWSTR
SC_GetProcName(void)
{
    LPWSTR retval;
    DEBUGMSG(ZONE_ENTRY,(L"SC_ProcGetName entry\r\n"));
    retval = MapPtr(pCurProc->lpszProcName);
    DEBUGMSG(ZONE_ENTRY,(L"SC_ProcGetName exit: %8.8lx\r\n",retval));
    return retval;
}




//------------------------------------------------------------------------------
// @func HANDLE | GetOwnerProcess | Returns the process id which owns the current thread
// @rdesc Returns the process id of the process which spawned the current thread
// @comm Returns the process id of the process which spawned the current thread
//------------------------------------------------------------------------------
HANDLE
SC_GetOwnerProcess(void)
{
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetOwnerProcess entry\r\n"));
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetOwnerProcess exit: %8.8lx\r\n",pCurThread->pOwnerProc->hProc));
    return pCurThread->pOwnerProc->hProc;
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
LPWSTR
SC_GetCommandLineW(void)
{
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetCommandLineW entry\r\n"));
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetCommandLineW exit: %8.8lx\r\n",pCurThread->pOwnerProc->pcmdline));
    return (LPWSTR)pCurThread->pOwnerProc->pcmdline;
}



//------------------------------------------------------------------------------
// @func HANDLE | GetCallerProcess | Returns the process id which called the currently running PSL
// @rdesc Returns the process id of the process which called the currently running PSL
// @comm Returns the process id of the process which called the currently running PSL
//------------------------------------------------------------------------------
HANDLE
SC_GetCallerProcess(void)
{
    PCALLSTACK pcstk = pCurThread->pcstkTop;
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetCallerProcess entry\r\n"));
    while (pcstk && (pcstk->dwPrcInfo & CST_IN_KERNEL) && (pcstk->pprcLast != ProcArray)) {
        pcstk = pcstk->pcstkNext;
    }
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetCallerProcess exit: %8.8lx\r\n",pcstk? pcstk->pprcLast->hProc : 0));
    return pcstk? pcstk->pprcLast->hProc : 0;
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
DWORD
SC_CeGetCurrentTrust(void)
{
    DWORD retval;
    DEBUGMSG(ZONE_ENTRY,(L"SC_CeGetCurrentTrust entry\r\n"));
    retval = pCurProc->bTrustLevel;
    DEBUGMSG(ZONE_ENTRY,(L"SC_CeGetCurrentTrust exit: %8.8lx\r\n",retval));
    return retval;
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
DWORD
SC_CeGetCallerTrust(void)
{
    HANDLE hval = SC_GetCallerProcess();
    DWORD retval;
    PPROCESS pproc;
    DEBUGMSG(ZONE_ENTRY,(L"SC_CeGetCallerTrust entry\r\n"));
    if (hval) {
        pproc = HandleToProc(hval);
        DEBUGCHK(pproc);
        retval = pproc->bTrustLevel;
    } else
        retval = pCurProc->bTrustLevel;
    DEBUGMSG(ZONE_ENTRY,(L"SC_CeGetCallerTrust exit: %8.8lx\r\n",retval));
    return retval;
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
DWORD
SC_GetCallerIndex(void)
{
    HANDLE hval = SC_GetCallerProcess();
    DWORD retval;
    PPROCESS pproc;
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetCallerIndex entry\r\n"));
    if (hval) {
        pproc = HandleToProc(hval);
        DEBUGCHK(pproc);
        retval = pproc->procnum;
    } else
        retval = (DWORD)-1;
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetCallerIndex exit: %8.8lx\r\n",retval));
    return retval;
}

// Must be kept in sync with filesys\main\fsmain.c
#define MAX_APPSTART_KEYNAME 128


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void
RunApps(
    ulong param
    )
{
        WCHAR  pName[MAX_APPSTART_KEYNAME];
    BYTE   pProp[MAX_PATH*sizeof(WCHAR)];
    DWORD  size, type;
    HANDLE hFSReady;
    DWORD cBytes, cPages;
    BOOL   fFileSysStarted;

    // Start filesys and wait for the registry to become available
    hFSReady = CreateEvent(NULL, TRUE, FALSE, TEXT("SYSTEM/FSReady"));
    DEBUGCHK(hFSReady);
    if (IsCeLogStatus(CELOGSTATUS_ENABLED_GENERAL)) {
        CELOG_LaunchingFilesys();
    }
    if (CreateProcess(L"filesys.exe", 0,0,0,0,0x80000000,0,0,0,0) && hFSReady) {
        WaitForMultipleObjects(1, &hFSReady, 0, INFINITE);
        CloseHandle(hFSReady);
    }


    // Initialize MUI-Resource loader (requires registry)
    InitMUILanguages();

    fFileSysStarted = (SystemAPISets[SH_FILESYS_APIS] != NULL);

    // Now that filesys is ready, the registry is available
    size = sizeof(pName);
    if (fFileSysStarted && (RegQueryValueExW(HKEY_LOCAL_MACHINE, L"JITDebugger", (LPDWORD)L"Debug",
                          &type, (LPBYTE)pName, &size) == ERROR_SUCCESS)
        && (type == REG_SZ) && (size < sizeof(pProp))
        && (pDebugger = AllocName((strlenW(pName)+1) * sizeof(WCHAR)))) {
        kstrcpyW(pDebugger->name, pName);
    }

    size = sizeof(pProp);
    if (fFileSysStarted && (RegQueryValueExW(HKEY_LOCAL_MACHINE, L"SystemPath", (LPDWORD)L"Loader",
                          &type, (LPBYTE)pProp, &size) == ERROR_SUCCESS)
        && (type == REG_MULTI_SZ) && (size < sizeof(pProp))
        && (pPath = AllocName(size)))
        memcpy(pPath->name, pProp, size);

    // set default value for PageOutTrigger and PageOutLevel
    if (!fFileSysStarted || RegQueryValueEx(HKEY_LOCAL_MACHINE, L"cbLow", (LPDWORD)L"SYSTEM\\OOM",
                        &type, (LPBYTE)&cBytes, &size) != ERROR_SUCCESS)
        cBytes = 0;
    if (!fFileSysStarted || RegQueryValueEx(HKEY_LOCAL_MACHINE, L"cpLow", (LPDWORD)L"SYSTEM\\OOM",
                        &type, (LPBYTE)&cPages, &size) != ERROR_SUCCESS)
        cPages = 0;
    if (cBytes || cPages) {
        cBytes = (cBytes + PAGE_SIZE -1) / PAGE_SIZE;   // convert to pages

⌨️ 快捷键说明

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