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

📄 kmisc.c

📁 WinCE5.0部分核心源码
💻 C
📖 第 1 页 / 共 5 页
字号:
        cPages = (cBytes>cPages)?cBytes:cPages;
    }
    PageOutTrigger = cPages + PAGEOUT_LOW;
    PageOutLevel = cPages + PAGEOUT_HIGH;

    // get Inject Dlls from registry
    if (fFileSysStarted && (RegQueryValueEx(HKEY_LOCAL_MACHINE, L"InjectDLL", (LPDWORD)L"SYSTEM\\KERNEL",
                         &type, NULL, &size) == ERROR_SUCCESS)
         && (type == REG_MULTI_SZ) && (size != 0)
         && (pInjectDLLs = AllocName(size))) {
         if (RegQueryValueEx(HKEY_LOCAL_MACHINE, L"InjectDLL", (LPDWORD)L"SYSTEM\\KERNEL",
                         &type, (LPBYTE)(pInjectDLLs->name), &size) != ERROR_SUCCESS) {
            FreeName(pInjectDLLs);
            pInjectDLLs = NULL;
         }
    }

    // Tell filesys that the remaining kernel work is done
    if (fFileSysStarted) {
        RegQueryValueEx (HKEY_LOCAL_MACHINE, L"AllowSystemAccess", (LPDWORD)L"SYSTEM\\ObjectStore",
                        &type, (LPBYTE)&g_fSysFileReadable, &size);

        pSignalStarted(0);
    }
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL
SC_SetDbgZone(
    DWORD dwProcid,
    LPVOID lpvMod,
    LPVOID BasePtr,
    DWORD zone,
    LPDBGPARAM lpdbgTgt
    )
{
    DWORD retval = FALSE;
    PMODULE pMod;
    PPROCESS pProc;
    LPDBGPARAM lpdbgSrc = 0;
    ACCESSKEY ulOldKey;

    // make sure the parameter is okay
    if (lpdbgTgt && !SC_MapPtrWithSize (lpdbgTgt, sizeof (DBGPARAM), hCurProc)) {
        return FALSE;
    }

    SWITCHKEY(ulOldKey,0xffffffff);

    // Cannot turn all 32 on -- zone 0xFFFFFFFF is used to query

    if (dwProcid) {
        if (!lpvMod && (pProc = HandleToProc((HANDLE)dwProcid)) && pProc->ZonePtr) {
            if (zone != 0xffffffff)
                pProc->ZonePtr->ulZoneMask=zone;
            lpdbgSrc = pProc->ZonePtr;
            retval = TRUE;
        }
    } else if (lpvMod) {
        pMod = (PMODULE)lpvMod;
        if (IsValidModule(pMod) && pMod->ZonePtr) {
            DWORD dwUseMask = pMod->inuse | 0x1;    // Add the kernel's bit
            DWORD dwZeroAddr = (DWORD)(ZeroPtr((LPVOID)(pMod->ZonePtr)));
            INT i=0;
            // set source for copying names: use kernel's version since it's guaranteed to be there !
            lpdbgSrc = (LPDBGPARAM)(MapPtrProc(dwZeroAddr,&ProcArray[0]));
            if (zone != 0xffffffff) {
                for (i = 0; dwUseMask; i++, dwUseMask >>= 1) {
                    if (dwUseMask & 0x1)
                        ((LPDBGPARAM)MapPtrProc(dwZeroAddr,&ProcArray[i]))->ulZoneMask = zone;
                }
            }
            retval = TRUE;
        }
    }
    // Copy information if required
    if (lpdbgSrc && lpdbgTgt)
        memcpy(lpdbgTgt, lpdbgSrc, sizeof(DBGPARAM));
    // restore access rights
    SETCURKEY(ulOldKey);
    return retval;
}

extern o32_lite *FindOptr (o32_lite *optr, int nCnt, DWORD dwAddr);


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL
SC_RegisterDbgZones(
    HANDLE hMod,
    LPDBGPARAM lpdbgparam
    )
{
    DWORD hKeyPeg=0, hKeyZones=0;
    DWORD dwType, dwData, dwSize;
    CHAR szName[16];
    PMODULE pMod = (PMODULE)hMod;
    HKEY hZones;
    BOOL bTryLocal;
    int i;

    if (!(lpdbgparam = MapPtr(lpdbgparam))) {
        return FALSE;
    }
    if (pMod) {
        o32_lite *optr;
        if (!IsValidModule(pMod))
            return FALSE;
        if (pMod->ZonePtr)
            return TRUE;

        // verify lpdbgparam is in RO section of this module
        if (!(optr = FindOptr (pMod->o32_ptr, pMod->e32.e32_objcnt, ZeroPtr (lpdbgparam)))
            || !(optr->o32_flags & IMAGE_SCN_MEM_WRITE)) {
            DEBUGMSG (1, (L"SC_RegisterDbgZones failed with invalide DBGPARAM (%8.8lx)\r\n", lpdbgparam));
            return FALSE;
        }
        pMod->ZonePtr = (LPDBGPARAM)MapPtrProc(ZeroPtr(lpdbgparam),&ProcArray[0]);
    } else {
        if (!SC_MapPtrWithSize (lpdbgparam, sizeof(DBGPARAM), hCurProc)) {
            DEBUGMSG (1, (L"SC_RegisterDbgZones failed with invalide DBGPARAM (%8.8lx)\r\n", lpdbgparam));
            return FALSE;
        }
        pCurThread->pOwnerProc->ZonePtr = lpdbgparam;
    }

    bTryLocal = TRUE;
    //
    // See if the debug zone mask is specified in the desktop PC's registry
    //
    rRegOpen((DWORD)HKEY_CURRENT_USER, "Pegasus", &hKeyPeg);
    if (hKeyPeg) {
        rRegOpen(hKeyPeg, "Zones", &hKeyZones);
        if (hKeyZones) {
            KUnicodeToAscii(szName, lpdbgparam->lpszName, sizeof(szName));
            rRegGet(hKeyZones, szName, &dwType, (LPBYTE)&dwData, &dwSize);
            if (dwType == REG_DWORD) {
                if (pMod)
                    SC_SetDbgZone(0,pMod,0,dwData,0);
                else
                    SC_SetDbgZone((DWORD)hCurProc,0,0,dwData,0);
                bTryLocal = FALSE;
            }
            rRegClose(hKeyZones);
        }
        rRegClose(hKeyPeg);
    } else {
        dwData = -1;
        for (i = 0; i < HOST_TRANSCFG_NUM_REGKEYS; i++) {
            if (strcmpW (lpdbgparam->lpszName, g_keyNames[i]) == 0) {
                dwData = g_dwKeys[i];
                if (dwData != -1) {
                    if (pMod)
                        SC_SetDbgZone(0,pMod,0,dwData,0);
                    else
                        SC_SetDbgZone((DWORD)hCurProc,0,0,dwData,0);
                    bTryLocal = FALSE;
                }
                break;
            }
        }
    }

    //
    // Can only check the local registry if the filesys API is registered.
    //
    if (bTryLocal && (UserKInfo[KINX_API_MASK] & (1 << SH_FILESYS_APIS))) {
        //
        // See if the debug zone mask is specified in the Windows CE registry
        // (in HLM\DebugZones:<module name>)
        //
        if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("DebugZones"), 0, 0,
                        &hZones) == ERROR_SUCCESS) {
            dwSize = sizeof(DWORD);
            if (RegQueryValueEx(hZones, lpdbgparam->lpszName, 0, &dwType,
                        (PUCHAR)&dwData, &dwSize) == ERROR_SUCCESS) {
                if (dwType == REG_DWORD) {
                    if (pMod)
                        SC_SetDbgZone(0,pMod,0,dwData,0);
                    else
                        SC_SetDbgZone((DWORD)hCurProc,0,0,dwData,0);
                }
            }
            RegCloseKey(hZones);
        }
    }
    return TRUE;
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
PTHREAD
ZeroAllThreadsTLS(
    PTHREAD pCur,
    DWORD slot
    )
{
    KCALLPROFON(65);
    if (pCur == (PTHREAD)1)
        pCur = pCurProc->pTh;
    else if (HandleToThread(pCur->hTh) != pCur)
        return (PTHREAD)1;
    pCur->tlsNonSecure[slot] = 0;
    KCALLPROFOFF(65);
    return pCur->pNextInProc;
}



//------------------------------------------------------------------------------
/* Thread local storage APIs */
//------------------------------------------------------------------------------
DWORD
SC_TlsCall(
    DWORD type,
    DWORD slot
    )
{
    int loop, mask;
    DWORD used;
    PTHREAD pth;
    DEBUGMSG(ZONE_ENTRY,(L"SC_TlsCall entry: %8.8lx %8.8lx\r\n",type,slot));

    // We need to remove the debugchk for most DLL does TlsAlloc/TlsFree at PROCESS_ATTACH/PROCESS_DETACH.
    // It'll be okay as long as the thread doesn't perform TlsGet/TlsSet in other process. The DEBUGCHK
    // is now done in TlsGet/TlsSet.
    // PSL should NOT call TlsAlloc/TlsFree on caller thread's stack. Fail the call if it does.
    //if (pCurThread->pOwnerProc != pCurProc) {
    //    DEBUGCHK (0);
    //    KSetLastError (pCurThread, ERROR_INVALID_PARAMETER);
    //    return 0xffffffff;
    //}
    switch (type) {
        case TLS_FUNCALLOC:
            do {
                used = pCurProc->tlsLowUsed;
                for (loop = 0, mask = 1; loop < 32; loop++, mask <<= 1)
                    if (!(used & mask))
                        break;
            } while ((loop != 32) && ((DWORD)InterlockedTestExchange(&pCurProc->tlsLowUsed,used,used | mask) != used));
            if (loop == 32) {
                do {
                    used = pCurProc->tlsHighUsed;
                    for (mask = 1; loop < 64; loop++, mask <<= 1)
                        if (!(used & mask))
                            break;
                } while ((loop != 64) && ((DWORD)InterlockedTestExchange(&pCurProc->tlsHighUsed,used,used|mask) != used));
                if (loop == 64) {
                    DEBUGMSG(ZONE_ENTRY,(L"SC_TlsCall exit: %8.8lx\r\n",0xffffffff));
                    return 0xffffffff;
                }
            }
            DEBUGMSG(ZONE_ENTRY,(L"SC_TlsCall exit: %8.8lx\r\n",loop));
            return loop;
        case TLS_FUNCFREE:
            if (slot >= 64) {
                DEBUGMSG(ZONE_ENTRY,(L"SC_TlsCall exit: %8.8lx\r\n",0));
                return 0;
            }
            pth = (PTHREAD)1;
            while (pth = (PTHREAD)KCall((PKFN)ZeroAllThreadsTLS,pth,slot))
                ;
            if (slot < 32) {
                do {
                    used = pCurProc->tlsLowUsed;
                } while ((DWORD)InterlockedTestExchange(&pCurProc->tlsLowUsed,used,used & ~(1<<slot)) != used);
            } else {
                do {
                    used = pCurProc->tlsHighUsed;
                } while ((DWORD)InterlockedTestExchange(&pCurProc->tlsHighUsed,used,used & ~(1<<(slot-32))) != used);
            }
            DEBUGMSG(ZONE_ENTRY,(L"SC_TlsCall exit: %8.8lx\r\n",1));
            return 1;
        default:
            DEBUGCHK(0);
            DEBUGMSG(ZONE_ENTRY,(L"SC_TlsCall exit: %8.8lx\r\n",0));
            return 0;
    }
}



//------------------------------------------------------------------------------
//  @func DWORD | SetProcPermissions | Sets the kernel's internal thread permissions
//  @rdesc Returns the new permission dword
//  @parm DWORD | newpermissions | the new permissions bitmask
//  @comm Sets the internal permissions bitmask for the current thread
//------------------------------------------------------------------------------
DWORD
SC_SetProcPermissions(
    DWORD newperms
    )
{
    DWORD retval = 0;
    PCALLSTACK pcstk = pCurThread->pcstkTop;

    TRUSTED_API (L"SC_SetProcPermissions", 0);

    DEBUGMSG(ZONE_ENTRY,(L"SC_SetProcPermissions entry: %8.8lx\r\n",newperms));
    if (!newperms)
        newperms = 0xffffffff;
    else {
        // prevent user from removing kernel access
        AddAccess(&newperms, ProcArray[0].aky);
        // prevent user from removing access to his stack
        AddAccess(&newperms, pCurThread->pOwnerProc->aky);
        // prevent user from removing access to current process
        AddAccess(&newperms, pCurProc->aky);
    }

    if (!pcstk || !(pcstk->dwPrcInfo & CST_IN_KERNEL)) {
        // calling from kMode
        SWITCHKEY(retval, newperms);
    } else {
        // update pcstkTop->aky, which will be set when returning from kernel
        retval = pcstk->akyLast;
        pcstk->akyLast = newperms;
    }
    DEBUGMSG(ZONE_ENTRY,(L"SC_SetProcPermissions exit: %8.8lx\r\n",retval));
    return retval;
}



//------------------------------------------------------------------------------
//  @func DWORD | GetCurrentPermissions | Obtains the kernel's internal thread permissions dword
//  @rdesc Returns the thread permissions dword
//  @comm Obtains the internal permissions bitmask for the current thread
//------------------------------------------------------------------------------
DWORD
SC_GetCurrentPermissions(void)
{
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetCurrentPermissions entry\r\n"));
    DEBUGMSG(ZONE_ENTRY,(L"SC_GetCurrentPermissions exit: %8.8lx\r\n",pCurThread->aky));
    return pCurThread->aky;
}

DWORD NormalBias, DaylightBias, InDaylight;



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void
SC_SetDaylightTime(
    DWORD dst
    )
{
    DEBUGMSG(ZONE_ENTRY,(L"SC_SetDaylightTime entry: %8.8lx\r\n",dst));
    InDaylight = dst;
    KInfoTable[KINX_TIMEZONEBIAS] = (InDaylight ? DaylightBias : NormalBias);
    KInfoTable[KINX_TIMECHANGECOUNT] ++;
    DEBUGMSG(ZONE_ENTRY,(L"SC_SetDaylightTime exit\r\n"));
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void
SC_SetTimeZoneBias(
    DWORD dwBias,
    DWORD dwDaylightBias
    )
{
    DEBUGMSG(ZONE_ENTRY,(L"SC_SetTimeZoneBias entry: %8.8lx %8.8lx\r\n",dwBias,dwDaylightBias));
    NormalBias = dwBias;
    DaylightBias = dwDaylightBias;
    KInfoTable[KINX_TIMEZONEBIAS] = (InDaylight ? DaylightBias : NormalBias);

⌨️ 快捷键说明

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