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

📄 process.c

📁 深圳市微逻辑电子有限公司 巨果&#8226 Kingmos&reg 系统核心
💻 C
📖 第 1 页 / 共 3 页
字号:
/******************************************************
Copyright(c) 版权所有,1998-2003微逻辑。保留所有权利。
******************************************************/

/*****************************************************
文件说明:进程管理
版本号:2.0.0
开发时期:2000
作者:李林
修改记录:
	2004-12-30 SwitchBackProcess 去掉两句多余的代码 ??
    2004-10-22 修改	LPPROCESS GetHandleOwner( void )

	2004-09-20: 去掉了在 SwitchToProcess 中的一段代码:
	            //dwProcessSlots[0] = (DWORD)lpProcess->lpSegIndex->lpSeg;
	            //SetCPUId( lpProcess->dwVirtualAddressBase );

    2003-09-09: DoCreateProcess 增加对参数的检查
    2003-09-02: 将Application Name and lpCmdLine 放到 mainthread stack
    2003-05-23: LN, GetCallerProcess, 增加对 lpCurProcess的判断
    2003-05-22-增加对 CURRENT_PROCESS_HANDLE 的处理
    2003-05-21: 将 Process结构的 lpSegment 改为 lpSegIndex
    2003-05-19: 将CreateModule 放到后面
    2003-05-7:LN修改MODULE 结构
    2003-04-30:LN,为可执行(exe)增加接口
	           删除无用的代码
******************************************************/

#include <eframe.h>
#include <eprogram.h>
#include <efile.h>

#include <epheap.h>
#include <epalloc.h>
#include <epcore.h>
#include <coresrv.h>

extern LPTHREAD FASTCALL _CreateThread(
									   HANDLE hProcess,
									   LPSECURITY_ATTRIBUTES lpThreadAttributes,   // 
									   DWORD dwStackSize,                      // 
									   DWORD dwPreservStackSize,
									   LPTHREAD_START_ROUTINE lpStartAddress, // 
									   LPVOID lpParameter,                // 
									   DWORD dwCreationFlags,         // 
									   LPDWORD lpThreadId         // 
									   );




// ********************************************************************
//声明:static DWORD WINAPI MainProcessProc( LPVOID lParam ) 
//参数:
//	IN lParam - 进程结构指针,指向当前进程
//返回值:
//	不返回
//功能描述:
//	该函数是创建的新进程的起始入口,也是进程的内核态,同时为该进程初始化一个默认的堆
//  该进程将会将调用模块加载器(负责将进程模块装入内存并运行)
//引用:
// ********************************************************************

#define DEBUG_MAINPROCESSPROC 0
static DWORD WINAPI MainProcessProc( LPVOID lParam )
{
	extern HANDLE DoHeapCreate( DWORD flOptions, ULONG dwInitialSize, ULONG dwMaximumSize );
	DWORD dwExitCode = -1;
    LPPROCESS lpProcess = (LPPROCESS)lParam;

	RETAILMSG( DEBUG_MAINPROCESSPROC, ( "Load MainProcessProc=%s,id=%d,VMB=0x%x.\r\n", lpProcess->lpszApplicationName, lpProcess->dwProcessId, lpProcess->dwVirtualAddressBase ) );

	// 初始化一个默认的进程堆
#ifdef VIRTUAL_MEM
	lpProcess->lpHeap = DoHeapCreate( HEAP_VIRTUAL_ALLOC, 0, 0 );// &lpProcess->heap );
#else
	lpProcess->lpHeap = DoHeapCreate( 0, 0, 0 );// &lpProcess->heap );
#endif


	RETAILMSG( DEBUG_MAINPROCESSPROC, ( "MainProcessProc:call module exe.\r\n" ) );
	
	// 调用模块加载器
	dwExitCode = lpProcess->pModule->lpmd->lpLoadModule( lpProcess->pModule->hModuleData, lpProcess->pModule->hModule, lpProcess->lpszCommandLine, LF_DEFLOAD );//LN:2003-05-07, Add
	//

	while( 1 )
		KL_ExitProcess( dwExitCode );
	// 永不返回
}

// ********************************************************************
//声明:static DWORD AllocProcessId( void ) 
//参数:
//	无
//返回值:
//	假如成功,返回进程ID;否则,返回0
//功能描述:
//	分配一个进程ID
//引用:
// ********************************************************************

static DWORD AllocProcessId( void )
{
	DWORD i;
	// 0 slots is reserver
	for( i = 1; i < MAX_PROCESSES; i++ )
	{
		if( KL_InterlockedCompareExchange( (LPLONG)&lppProcessSegmentSlots[i], 1, 0 ) == 0 )
		{
			return i;
		}
	}
	return 0;
}

// ********************************************************************
//声明:static BOOL FreeProcessId( DWORD dwProcessId ) 
//参数:
//	IN dwProcessId - 进程ID
//返回值:
//	假如成功,返回TRUE;否则,返回FALSE
//功能描述:
//	释放一个进程ID
//引用:
// ********************************************************************

static BOOL FreeProcessId( DWORD dwProcessId )
{
	if( dwProcessId < MAX_PROCESSES && dwProcessId > 0 )
	{	
		UINT uiSave;
		LockIRQSave( &uiSave );

		lppProcessSegmentSlots[dwProcessId] = 0;
		lppProcessPtr[dwProcessId] = 0;

		UnlockIRQRestore( &uiSave );
	}
	return FALSE;
}

//LN:2003-05-12, Add	
#ifdef VIRTUAL_MEM
// ********************************************************************
//声明:static LPPROCESS_SEGMENTS AllocSegmentIndexAndSegment( DWORD dwSegBaseAdr ) 
//参数:
//	IN dwSegBaseAdr - 该段对应的基地址
//返回值:
//	假如成功,返回一个段索引结构指针;否则,返回NULL
//功能描述:
//	分配一个段索引和段结构(段索引用于连接其它段,段结构用于描述/记录32M的进程空间使用情况)
//引用:
// ********************************************************************

#define CACHE_PAGES 2
#define CACHE_PAGES_SIZE (1024*CACHE_PAGES)
#define DEBUG_AllocSegmentIndexAndSegment 0
LPPROCESS_SEGMENTS AllocSegmentIndexAndSegment( DWORD dwSegBaseAdr )
{	// 分配一个段索引
	LPPROCESS_SEGMENTS lpSegIndex = KHeap_Alloc( sizeof(PROCESS_SEGMENTS) );

	if( lpSegIndex )
	{   // 分配一个段结构
		memset( lpSegIndex, 0, sizeof(PROCESS_SEGMENTS) );

		if( (lpSegIndex->lpSeg = Seg_Alloc()) )
		{
			lpSegIndex->lpdwSecondPageTable = KHeap_Alloc( CACHE_PAGES_SIZE );
			//return lpSegIndex;
			if( lpSegIndex->lpdwSecondPageTable )
			{
				lpSegIndex->lpdwPhySecondPageTable = (LPDWORD)( (DWORD)_GetPhysicalAdr( lpSegIndex->lpdwSecondPageTable ) | 1 );
				lpSegIndex->lpdwSecondPageTable = (LPDWORD)CACHE_TO_UNCACHE(lpSegIndex->lpdwSecondPageTable);				
				lpSegIndex->dwSegBaseAddress = dwSegBaseAdr;
				lpSegIndex->uiPageTableCount = CACHE_PAGES;

				memset( lpSegIndex->lpdwSecondPageTable, 0, CACHE_PAGES_SIZE );
				DEBUGMSG( DEBUG_AllocSegmentIndexAndSegment, ( "dwSegBaseAdr=0x%x,lpdwPhySecondPageTable=0x%x.\r\n", dwSegBaseAdr, lpSegIndex->lpdwPhySecondPageTable ) );
				 
				return lpSegIndex;
			}
		}
		if( lpSegIndex->lpSeg )
		    Seg_Free( lpSegIndex->lpSeg );

		KHeap_Free( lpSegIndex, sizeof(PROCESS_SEGMENTS) );
		lpSegIndex = NULL;
	}
	return lpSegIndex;
}
// ********************************************************************
//声明: static void FreeSegmentIndexAndSegment( LPPROCESS_SEGMENTS lpSegIndex )
//参数:
//	IN lpSegIndex - 段索引结构指针
//返回值:
//	无
//功能描述:
//	与AllocSegmentIndexAndSegment相对应,释放一个段索引结构指针及其段空间
//引用:
// ********************************************************************

static void FreeSegmentIndexAndSegment( LPPROCESS_SEGMENTS lpSegIndex )
{
	ASSERT( lpSegIndex->lpSeg->lpBlks[0] == NULL );//每个段的前64k是保留不用的,必须是NULL

	if( lpSegIndex->lpdwSecondPageTable )
	{
		LPDWORD lpT = (LPDWORD)UNCACHE_TO_CACHE( lpSegIndex->lpdwSecondPageTable );
		lpSegIndex->lpdwSecondPageTable = NULL;
	    KHeap_Free( lpT, CACHE_PAGES_SIZE );
	}
	Seg_Free( lpSegIndex->lpSeg );
	KHeap_Free( lpSegIndex, sizeof(PROCESS_SEGMENTS) );
}

// ********************************************************************
//声明: static void FreeProcessSegmentIndexAndSegment( LPPROCESS_SEGMENTS lpSegIndex  )
//参数:
//	IN lpSegIndex - 段索引结构指针
//返回值:
//	无
//功能描述:
//	释放进程的所有段(一个进程段用于描述/记录32M的进程空间使用情况)
//引用:
// ********************************************************************

static void FreeProcessSegmentIndexAndSegment( LPPROCESS_SEGMENTS lpSegIndex  )
{
	LPPROCESS_SEGMENTS lpSegNext;
	while( lpSegIndex )
	{
		lpSegNext = lpSegIndex->lpNext;
		FreeSegmentIndexAndSegment( lpSegIndex );
		lpSegIndex = lpSegNext;
	}
}
#endif  //VIRTUAL_MEM

// ********************************************************************
//声明: void FreeProcessObject( LPPROCESS lpProcess )
//参数:
//	IN lpProcess - PROCESS结构指针
//返回值:
//	无
//功能描述:
//	释放进程结构,当调用该函数时,不在该进程空间(该进程已经死了)
//引用:
//  当创建进程失败或进程已经死了时会调用该函数
// ********************************************************************

void FreeProcessObject( LPPROCESS lpProcess )
{
	DWORD dwProcessId = lpProcess->dwProcessId;
	if( lpProcess->lpMainThread )
		;// ????
	// 释放模块的数据
	if( lpProcess->pModule )
		Module_DeInit( lpProcess->pModule );

#ifdef VIRTUAL_MEM
	// 释放进程的段
	if( lpProcess->lpProcessSegments )
	{
		VOID * lpT = lpProcess->lpProcessSegments;
		lpProcess->lpProcessSegments = NULL;

		FreeProcessSegmentIndexAndSegment( lpT );
	}
#endif

    // 释放进程句柄
	if( lpProcess->hProcess )
        Handle_Free( lpProcess->hProcess, TRUE );
	if( lpProcess->lpCpuPTS )
		FreeCPUPTS( lpProcess->lpCpuPTS );

	lpProcess->objType = OBJ_NULL;
    // 释放进程结构
    KHeap_Free( lpProcess, sizeof( PROCESS ) );
    // 释放进程ID
    if( dwProcessId )
	    FreeProcessId( dwProcessId );

}

// ********************************************************************
//声明:BOOL FASTCALL DoCreateProcess(
//						   LPCTSTR lpszApplicationName,
//						   LPCTSTR lpszCommandLine, 
//						   LPSECURITY_ATTRIBUTES lpProcessAttributes,
//						   LPSECURITY_ATTRIBUTES lpThreadAttributes,
//						   BOOL bInheritHandles,
//						   DWORD dwCreationFlags,
//						   LPVOID lpEnvironment,
//						   LPCTSTR lpCurrentDirectory,
//						   LPSTARTUPINFO lpStartupInfo,
//						   LPPROCESS_INFORMATION lpProcessInformation,
//						   PCMODULE_DRV pmd
//						   )
//参数:
//	   IN lpszApplicationName - 可执行模块名
//	   IN lpszCommandLine - 传给WinMain的参数
//	   IN lpProcessAttributes - 进程安全属性
//	   IN lpThreadAttributes - 线程安全属性
//	   IN bInheritHandles - 是否继承父进程的句柄,必须为FALSE
//	   IN dwCreationFlags - 创建标志:
//							CREATE_SUSPENDED - 创建后挂起
//	   IN lpEnvironment - 环境块(必须为NULL)
//	   IN lpCurrentDirectory - 进程运行的当前目录(必须为NULL)
//	   IN lpStartupInfo - 启动信息(必须为NULL)
//	   OUT lpProcessInformation - PROCESS_INFORMATION结构指针(可以为NULL),用于接受进程的某些信息
//	   IN pmd - 该进程模块所对应的加载器
//返回值:
//	假如成功,返回TRUE,如果lpProcessInformation不为NULL,回返回进程信息;
//  否则,返回FALSE。
//功能描述:
//	创建新的进程
//引用:
//	KL_CreateProcess & KL_LoadApplication
// ********************************************************************

#define DEBUG_DOCREATEPROCESS 0
BOOL FASTCALL DoCreateProcess(
						   LPCTSTR lpszApplicationName,
						   LPCTSTR lpszCommandLine,
						   LPSECURITY_ATTRIBUTES lpProcessAttributes,
						   LPSECURITY_ATTRIBUTES lpThreadAttributes,
						   BOOL bInheritHandles,
						   DWORD dwCreationFlags,
						   LPVOID lpEnvironment,
						   LPCTSTR lpCurrentDirectory,
						   LPSTARTUPINFO lpStartupInfo,
						   LPPROCESS_INFORMATION lpProcessInformation,
						   PCMODULE_DRV pmd
						   )
{

    DWORD id = 0;
	LPPROCESS lpProcess = NULL;
	int iCmdLineLen;
	ACCESS_KEY akySave;

	if( lpszApplicationName == NULL ||
		*lpszApplicationName == 0 )
	{
		KL_SetLastError( ERROR_INVALID_PARAMETER );
		return FALSE;
	}

	//
    DEBUGMSG( DEBUG_DOCREATEPROCESS, ( "DoCreateProcess:%s.\r\n", lpszApplicationName ) );
	if( lpszCommandLine == 0 )
	{
		lpszCommandLine = "";
	}

	iCmdLineLen = strlen( lpszCommandLine ) + strlen( lpszApplicationName ) + 2; // one null end, one space
	// 分配进程结构
    DEBUGMSG( DEBUG_DOCREATEPROCESS, ( "DoCreateProcess:alloc process struct.\r\n" ) );
	lpProcess = KHeap_Alloc( sizeof( PROCESS ) );
	if( lpProcess )
	{   
		memset( lpProcess, 0, sizeof( PROCESS ) );
		// 分配进程ID
		if( ( lpProcess->dwProcessId = AllocProcessId() ) == 0 )
		    goto _alloc_error;
		// 分配进程句柄
		DEBUGMSG( DEBUG_DOCREATEPROCESS, ( "DoCreateProcess:alloc process handle.\r\n" ) );
		if( ( lpProcess->hProcess = Handle_Alloc( lpProcess, lpProcess, OBJ_PROCESS ) ) == NULL )
			goto _alloc_error;

#ifdef VIRTUAL_MEM
		// 分配一个段
		if( ( lpProcess->lpProcessSegments = AllocSegmentIndexAndSegment(lpProcess->dwProcessId << SEGMENT_SHIFT) ) == NULL )
			goto _alloc_error;
		lppProcessSegmentSlots[lpProcess->dwProcessId] = lpProcess->lpProcessSegments;//->lpSeg;
		

⌨️ 快捷键说明

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