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

📄 kwin32.c

📁 WinCE5.0部分核心源码
💻 C
📖 第 1 页 / 共 4 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// This source code is licensed under Microsoft Shared Source License
// Version 1.0 for Windows CE.
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223.
//
/**     TITLE("Kernel Win32 Handle")
 *++
 *
 *
 * Module Name:
 *
 *    KWin32.c
 *
 * Abstract:
 *
 *  This file contains the definition of the Win32 system API handle.
 *
 *--
 */
#include "kernel.h"
#include "kdstub.h"
#include "hdstub.h"
#include "osaxs.h"
#include <kitlpriv.h>

BOOL KernelIoctl (DWORD dwIoControlCode, LPVOID lpInBuf, DWORD nInBufSize, LPVOID lpOutBuf, DWORD nOutBufSize, LPDWORD lpBytesReturned);
#define SECURE_WORKAROUND
#ifdef SECURE_WORKAROUND
DWORD SC_CallForward (PCALLBACKINFO pcbi, DWORD arg1, DWORD arg2, DWORD arg3, DWORD arg4, DWORD arg5, DWORD arg6, DWORD arg7)
{
    if (!bAllKMode)
        RETAILMSG (1, (L"!!!! Work around Security violation (call forward), pth = %8.8lx, proc = '%s' !!!!\n", pCurThread, pCurProc->lpszProcName));
    return PerformCallBack (pcbi, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
#endif 

static BOOL CheckAccessToVM (LPVOID lpvAddress, DWORD cbSize)
{
    // always okay if calling from kernel itself
    if (ProcArray == pCurProc)
        return TRUE;

    // kernel address is invalid
    if ((int) lpvAddress >= 0) {
        
        DWORD dwSlot = ((DWORD) lpvAddress >> VA_SECTION);
        DWORD dwZeroAddr = (DWORD) ZeroPtr (lpvAddress);

        switch (dwSlot) {
            
        case MODULE_SECTION:
        case RESOURCE_SECTION:
        case SHARED_SECTION:
            // resource, shared, and module section are invalid            
            break;
            
        case 0:
            // slot 0 address, map it current process
            dwSlot = pCurProc->dwVMBase >> VA_SECTION;

            // fall through
        default:
            if (dwSlot <= MAX_PROCESSES) {
                // non-trusted app cannot change VM beyond DllLoadBase
                if ((KERN_TRUST_FULL != pCurProc->bTrustLevel)
                    && ((dwZeroAddr + (cbSize? cbSize : 1)) > (DWORD) DllLoadBase)) {
                    break;
                }
                // slot 1-32: check process aky
                if (pCurThread->aky & (1 << (dwSlot-1))) {
                    return TRUE;
                }
            } else {
                // memory mapped area, check alk of 1st MEMBLOCK
                MEMBLOCK *pmb = (*SectionTable[dwSlot])[0];
                if (pmb && (RESERVED_BLOCK != pmb) && (pCurThread->aky & pmb->alk))
                    return TRUE;
            }
            break;
        }
    }
    KSetLastError (pCurThread, ERROR_INVALID_PARAMETER);
    return FALSE;
}

static LPVOID EXT_VirtualAlloc (LPVOID lpvAddress, DWORD cbSize, DWORD fdwAllocationType, DWORD fdwProtect)
{
    DWORD dwErr = 0;
    
    // make sure the thread has access to the address
    if (!CheckAccessToVM (lpvAddress, cbSize)) {
        return NULL;
    }

    // MEM_TOP_DOWN is not support when calling from outside kernel
    fdwAllocationType &= ~MEM_TOP_DOWN;
    return DoVirtualAlloc (lpvAddress, cbSize, fdwAllocationType, fdwProtect, 0, 0);
}

static BOOL EXT_VirtualFree (LPVOID lpvAddress, DWORD cbSize, DWORD fdwFreeType)
{
    // only trusted apps can free shared section
    if (IsInSharedSection (lpvAddress)) {
        TRUSTED_API ("CeVirtualFree", FALSE);
        
    // make sure the thread has access to the address
    } else if (!CheckAccessToVM (lpvAddress, cbSize)) {
        return FALSE;
    }

    return SC_VirtualFree (lpvAddress, cbSize, fdwFreeType);
}

static BOOL EXT_VirtualProtect (LPVOID lpvAddress, DWORD cbSize, DWORD fdwNewProtect, PDWORD pfdwOldProtect)
{
    // make sure the thread has access to the address
    if (!CheckAccessToVM (lpvAddress, cbSize)) {
        return FALSE;
    }

    // verify user pointer
    if ((KERN_TRUST_FULL != pCurProc->bTrustLevel) 
        && pfdwOldProtect
        && !SC_MapPtrWithSize (pfdwOldProtect, sizeof (DWORD), hCurProc)) {
        KSetLastError (pCurThread, ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    // verify if trying to make ROM R/W
    if ((fdwNewProtect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE)) 
        && IsROM (lpvAddress, cbSize)) {
        KSetLastError (pCurThread, ERROR_ACCESS_DENIED);
        return FALSE;
    }
    
    return SC_VirtualProtect (lpvAddress, cbSize, fdwNewProtect, pfdwOldProtect);
}

static DWORD EXT_VirtualQuery (LPVOID lpvAddress, PMEMORY_BASIC_INFORMATION pmbiBuffer, DWORD cbLength)
{
    if ((KERN_TRUST_FULL != pCurProc->bTrustLevel)
        && !SC_MapPtrWithSize (pmbiBuffer, sizeof (MEMORY_BASIC_INFORMATION), hCurProc)) {
        KSetLastError (pCurThread, ERROR_INVALID_PARAMETER);
        return 0;
    }
    return SC_VirtualQuery (lpvAddress, pmbiBuffer, cbLength);
}

static BOOL SC_GetRealTime (LPSYSTEMTIME lpst)
{
    if (!SC_MapPtrWithSize (lpst, sizeof (SYSTEMTIME), hCurProc))
        return FALSE;
    return OEMGetRealTime (lpst);
}

static BOOL SC_SetRealTime (LPSYSTEMTIME lpst)
{
    BOOL fRet = OEMSetRealTime (lpst);
    // indicate time has changed
    KInfoTable[KINX_TIMECHANGECOUNT] ++;
    return fRet;
}

BOOL NKDeleteStaticMapping (LPVOID pAddr, DWORD cbSize)
{
    // not implemented, always succeed
    return TRUE;
}

static BOOL SC_PageOutModule (HANDLE hModule, DWORD dwFlags);

const PFNVOID Win32Methods[] = {
    (PFNVOID)SC_Nop,
    (PFNVOID)SC_NotSupported,
    (PFNVOID)SC_CreateAPISet,               //  2
    (PFNVOID)EXT_VirtualAlloc,              //  3
    (PFNVOID)EXT_VirtualFree,               //  4
    (PFNVOID)EXT_VirtualProtect,            //  5
    (PFNVOID)EXT_VirtualQuery,              //  6
    (PFNVOID)SC_VirtualCopy,                //  7
    (PFNVOID)0,                             //  8 Was SC_LoadLibraryW
    (PFNVOID)SC_FreeLibrary,                //  9
    (PFNVOID)SC_GetProcAddressW,            // 10
    (PFNVOID)SC_ThreadAttachOrDetach,       // 11 Was SC_ThreadAttachAllDLLs
    (PFNVOID)0,                             // 12 Was SC_ThreadDetachAllDLLs
    (PFNVOID)SC_GetTickCount,               // 13
    (PFNVOID)OutputDebugStringW,            // 14
    (PFNVOID)SC_TlsCall,                    // 15
    (PFNVOID)SC_GetSystemInfo,              // 16
    (PFNVOID)0,                             // 17  Was ropen
    (PFNVOID)0,                             // 18  Was rread
    (PFNVOID)0,                             // 19  Was rwrite
    (PFNVOID)0,                             // 20  Was rlseek
    (PFNVOID)0,                             // 21  Was rclose
    (PFNVOID)SC_RegisterDbgZones,           // 22
    (PFNVOID)NKvDbgPrintfW,                 // 23
    (PFNVOID)SC_ProfileSyscall,             // 24
    (PFNVOID)SC_FindResource,               // 25
    (PFNVOID)SC_LoadResource,               // 26
    (PFNVOID)SC_SizeofResource,             // 27
    (PFNVOID)SC_GetRealTime,                // 28
    (PFNVOID)SC_SetRealTime,                // 29
    (PFNVOID)SC_ProcessDetachAllDLLs,       // 30
    (PFNVOID)SC_ExtractResource,            // 31
    (PFNVOID)SC_GetRomFileInfo,             // 32
    (PFNVOID)SC_GetRomFileBytes,            // 33
    (PFNVOID)SC_CacheRangeFlush,            // 34
    (PFNVOID)SC_Nop,                        // 35
    (PFNVOID)SC_Nop,                        // 36
    (PFNVOID)SC_Nop,                        // 37
    (PFNVOID)SC_GetKPhys,                   // 38
    (PFNVOID)SC_GiveKPhys,                  // 39
    (PFNVOID)SC_SetExceptionHandler,        // 40
    (PFNVOID)SC_Nop,                        // 41
    (PFNVOID)SC_Nop,                        // 42
    (PFNVOID)SC_SetKernelAlarm,             // 43
    (PFNVOID)SC_RefreshKernelAlarm,         // 44
    (PFNVOID)SC_CeGetRandomSeed,            // 45
    (PFNVOID)SC_CloseProcOE,                // 46
    (PFNVOID)SC_SetGwesOOMEvent,            // 47
    (PFNVOID)SC_FSStringCompress,           // 48
    (PFNVOID)SC_FSStringDecompress,         // 49
    (PFNVOID)SC_FSBinaryCompress,           // 50
    (PFNVOID)SC_FSBinaryDecompress,         // 51
    (PFNVOID)SC_CreateEvent,                // 52
    (PFNVOID)SC_CreateProc,                 // 53
    (PFNVOID)SC_CreateThread,               // 54
    (PFNVOID)InputDebugCharW,               // 55
    (PFNVOID)UB_TakeCritSec,                // 56
    (PFNVOID)SC_LeaveCritSec,               // 57
    (PFNVOID)UB_WaitForMultiple,            // 58
    (PFNVOID)SC_MapPtrToProcess,            // 59
    (PFNVOID)SC_MapPtrUnsecure,             // 60
    (PFNVOID)SC_GetProcFromPtr,             // 61
    (PFNVOID)SC_IsBadPtr,                   // 62
    (PFNVOID)SC_GetProcAddrBits,            // 63
    (PFNVOID)SC_GetFSHeapInfo,              // 64
    (PFNVOID)SC_OtherThreadsRunning,        // 65
    (PFNVOID)SC_KillAllOtherThreads,        // 66
    (PFNVOID)SC_GetOwnerProcess,            // 67
    (PFNVOID)SC_GetCallerProcess,           // 68
    (PFNVOID)SC_GetIdleTime,                // 69
    (PFNVOID)SC_SetLowestScheduledPriority, // 70
    (PFNVOID)SC_IsPrimaryThread,            // 71
    (PFNVOID)SC_SetProcPermissions,         // 72
    (PFNVOID)SC_GetCurrentPermissions,      // 73
    (PFNVOID)0,                             // 74
    (PFNVOID)SC_SetDaylightTime,            // 75
    (PFNVOID)SC_SetTimeZoneBias,            // 76
    (PFNVOID)SC_SetCleanRebootFlag,         // 77
    (PFNVOID)SC_CreateCrit,                 // 78
    (PFNVOID)SC_PowerOffSystem,             // 79
    (PFNVOID)SC_CreateMutex,                // 80
    (PFNVOID)SC_SetDbgZone,                 // 81
    (PFNVOID)UB_Sleep,                      // 82
    (PFNVOID)SC_TurnOnProfiling,            // 83
    (PFNVOID)SC_TurnOffProfiling,           // 84
    (PFNVOID)SC_CeGetCurrentTrust,          // 85
    (PFNVOID)SC_CeGetCallerTrust,           // 86
    (PFNVOID)SC_NKTerminateThread,          // 87
    (PFNVOID)SC_SetLastError,               // 88
    (PFNVOID)SC_GetLastError,               // 89
    (PFNVOID)SC_GetProcName,                // 90
    (PFNVOID)SC_TerminateSelf,              // 91
    (PFNVOID)SC_CloseAllHandles,            // 92
    (PFNVOID)SC_SetHandleOwner,             // 93
    (PFNVOID)0,                             // 94 Was SC_LoadDriver
    (PFNVOID)SC_CreateFileMapping,          // 95
    (PFNVOID)SC_UnmapViewOfFile,            // 96
    (PFNVOID)SC_FlushViewOfFile,            // 97
    (PFNVOID)SC_CreateFileForMapping,       // 98
    (PFNVOID)KernelIoctl,                   // 99
    (PFNVOID)SC_GetThreadCallStack,         // 100
    (PFNVOID)SC_Nop,                        // 101
    (PFNVOID)0,                             // 102
    (PFNVOID)SC_UpdateNLSInfo,              // 103
    (PFNVOID)SC_ConnectDebugger,            // 104
    (PFNVOID)SC_InterruptInitialize,        // 105
    (PFNVOID)SC_InterruptDone,              // 106
    (PFNVOID)SC_InterruptDisable,           // 107
    (PFNVOID)SC_SetKMode,                   // 108
    (PFNVOID)SC_SetPowerOffHandler,         // 109
    (PFNVOID)SC_SetGwesPowerHandler,        // 110
    (PFNVOID)SC_SetHardwareWatch,           // 111
    (PFNVOID)SC_QueryAPISetID,              // 112
#ifdef SECURE_WORKAROUND
    (PFNVOID)SC_CallForward,
#else
    (PFNVOID)-1,                            // 113 (PerformCallBack)
#endif
    (PFNVOID)CaptureContext,                // 114 (RaiseException)
    (PFNVOID)SC_GetCallerIndex,             // 115
    (PFNVOID)SC_WaitForDebugEvent,          // 116
    (PFNVOID)SC_ContinueDebugEvent,         // 117
    (PFNVOID)SC_DebugNotify,                // 118
    (PFNVOID)SC_OpenProcess,                // 119
    (PFNVOID)SC_THCreateSnapshot,           // 120
    (PFNVOID)SC_THGrow,                     // 121
    (PFNVOID)SC_NotifyForceCleanboot,       // 122
    (PFNVOID)SC_DumpKCallProfile,           // 123
    (PFNVOID)SC_GetProcessVersion,          // 124
    (PFNVOID)SC_GetModuleFileNameW,         // 125
    (PFNVOID)SC_QueryPerformanceCounter,    // 126
    (PFNVOID)SC_QueryPerformanceFrequency,  // 127
    (PFNVOID)SC_KernExtractIcons,           // 128
    (PFNVOID)SC_ForcePageout,               // 129
    (PFNVOID)SC_GetThreadTimes,             // 130
    (PFNVOID)SC_GetModuleHandleW,           // 131
    (PFNVOID)0,                                         // 132
    (PFNVOID)SC_SetStdioPathW,              // 133
    (PFNVOID)SC_GetStdioPathW,              // 134
    (PFNVOID)SC_ReadRegistryFromOEM,        // 135
    (PFNVOID)SC_WriteRegistryToOEM,         // 136
    (PFNVOID)SC_WriteDebugLED,              // 137
    (PFNVOID)SC_LockPages,                  // 138
    (PFNVOID)SC_UnlockPages,                // 139
    (PFNVOID)NKVirtualSetAttributes,        // 140
#ifdef SH4
    (PFNVOID)SC_SetRAMMode,                 // 141
    (PFNVOID)SC_SetStoreQueueBase,          // 142
#else
    (PFNVOID)0,                             // 141
    (PFNVOID)0,                             // 142
#endif
    (PFNVOID)SC_FlushViewOfFileMaybe,       // 143
    (PFNVOID)SC_GetProcAddressA,            // 144
    (PFNVOID)SC_GetCommandLineW,            // 145
    (PFNVOID)SC_DisableThreadLibraryCalls,  // 146
    (PFNVOID)SC_CreateSemaphore,            // 147
    (PFNVOID)SC_LoadLibraryExW,             // 148
#ifdef SECURE_WORKAROUND
    (PFNVOID)SC_CallForward,
#else
    (PFNVOID)-2,                            // 149 (PerformCallForward)
#endif
    (PFNVOID)SC_CeMapArgumentArray,         // 150
    (PFNVOID)SC_KillThreadIfNeeded,         // 151
    (PFNVOID)SC_ProcGetIndex,               // 152
    (PFNVOID)SC_RegisterGwesHandler,        // 153
    (PFNVOID)SC_GetProfileBaseAddress,      // 154
    (PFNVOID)SC_SetProfilePortAddress,      // 155
    (PFNVOID)SC_CeLogData,                  // 156
    (PFNVOID)SC_CeLogSetZones,              // 157
    (PFNVOID)NULL,                          // 158 (was ModuleJit)
    (PFNVOID)SC_CeSetExtendedPdata,         // 159
    (PFNVOID)SC_VerQueryValueW,             // 160
    (PFNVOID)SC_GetFileVersionInfoSizeW,    // 161
    (PFNVOID)SC_GetFileVersionInfoW,        // 162
    (PFNVOID)SC_CreateLocaleView,           // 163
    (PFNVOID)SC_CeLogReSync,                // 164
    (PFNVOID)SC_LoadIntChainHandler,        // 165
    (PFNVOID)SC_FreeIntChainHandler,        // 166
    (PFNVOID)SC_LoadKernelLibrary,          // 167
    (PFNVOID)SC_AllocPhysMem,               // 168
    (PFNVOID)SC_FreePhysMem,                // 169
    (PFNVOID)SC_KernelLibIoControl,         // 170
    (PFNVOID)SC_OpenEvent,                  // 171
    (PFNVOID)UB_SleepTillTick,              // 172
    (PFNVOID)SC_DuplicateHandle,            // 173
    (PFNVOID)SC_CreateStaticMapping,        // 174
    (PFNVOID)SC_MapCallerPtr,               // 175
    (PFNVOID)SC_MapPtrWithSize,             // 176
    (PFNVOID)SC_LoadStringW,                // 177
    (PFNVOID)SC_QueryInstructionSet,        // 178
    (PFNVOID)SC_CeLogGetZones,              // 179
    (PFNVOID)SC_ProcGetIDFromIndex,         // 180
    (PFNVOID)SC_IsProcessorFeaturePresent,  // 181
    (PFNVOID)SC_DecompressBinaryBlock,      // 182
    (PFNVOID)SC_PageOutModule,              // 183
    (PFNVOID)SC_InterruptMask,              // 184
    (PFNVOID)SC_GetProcModList,             // 185
    (PFNVOID)SC_FreeModFromCurrProc,        // 186
    (PFNVOID)SC_CeVirtualSharedAlloc,       // 187
    (PFNVOID)NKDeleteStaticMapping,         // 188
    (PFNVOID)SC_CreateToken,                // 189
    (PFNVOID)SC_RevertToSelf,               // 190
    (PFNVOID)SC_CeImpersonateCurrProc,      // 191
    (PFNVOID)SC_CeDuplicateToken,           // 192
    (PFNVOID)SC_ConnectHdstub,              // 193 
    (PFNVOID)SC_ConnectOsAxsT0,             // 194 
    (PFNVOID)SC_IsNamedEventSignaled,       // 195
    (PFNVOID)SC_ConnectOsAxsT1,             // 196
};

const CINFO cinfWin32 = {
    "Wn32",
    DISPATCH_I_KPSL,
    0,
    sizeof(Win32Methods)/sizeof(Win32Methods[0]),
    Win32Methods,
};

extern const CINFO cinfThread;
extern const CINFO cinfProc;
extern const CINFO cinfMutex;
extern const CINFO cinfSem;
extern const CINFO cinfEvent;
extern const CINFO cinfAPISet;
extern const CINFO cinfMap;
extern const CINFO cinfToken;
const CINFO CinfFile = { "FILE", DISPATCH_KERNEL, HT_FILE, 0, 0 };
const CINFO CinfFind = { "FIND", DISPATCH_KERNEL, HT_FIND, 0, 0 };
const CINFO CinfDBFile = { "DFIL", DISPATCH_KERNEL, HT_DBFILE, 0, 0 };
const CINFO CinfDBFind = { "DFND", DISPATCH_KERNEL, HT_DBFIND, 0, 0 };
const CINFO CinfSocket = { "SKT", DISPATCH_KERNEL, HT_SOCKET, 0, 0 };
const CINFO CinfWnetEnum = { "ENUM", DISPATCH_KERNEL, HT_WNETENUM, 0, 0 };

extern void InitMemoryPool(void);
extern void ProfInit(void);
extern void HeapInit(void);
extern void SchedInit(void);
extern void ProcInit(void);
extern CRITICAL_SECTION VAcs, RFBcs, PhysCS, LLcs, ModListcs, ODScs, CompCS, MapCS, NameCS, EventCS, MutexCS, SemCS,
            DbgApiCS, PagerCS, WriterCS, MapNameCS, ppfcs, PageOutCS, IntChainCS, DirtyPageCS, WDcs;

extern BOOL fNoDebugger;
extern BOOL fDebuggerLoaded;

#ifdef SHx
extern void MD_CBRtn(void);
#if defined(SH4)
extern void FPUFlushContext(void);

⌨️ 快捷键说明

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