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

📄 system.cpp

📁 小型的操作系统开发的原代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	//ENTER_CRITICAL_SECTION();
	__ENTER_CRITICAL_SECTION(NULL,dwFlags);
	lpSystem->lpInterruptVector[INTERRUPT_VECTOR_TIMER] = lpIntObject;
	//
	//Here,maybe some code initializes all other interrupt vector.
	//
	//LEAVE_CRITICAL_SECTION();
	__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
	bResult = TRUE;

__TERMINAL:
	if(!bResult)
	{
		if(lpPriorityQueue != NULL)
		{
			ObjectManager.DestroyObject(&ObjectManager,
				(__COMMON_OBJECT*)lpPriorityQueue);
		}
		if(lpIntObject != NULL)
		{
			ObjectManager.DestroyObject(&ObjectManager,
				(__COMMON_OBJECT*)lpIntObject);
		}
	}
	return bResult;
}

//
//GetClockTickCounter routine.
//

static DWORD GetClockTickCounter(__COMMON_OBJECT* lpThis)
{
	__SYSTEM*    lpSystem = NULL;

	if(NULL == lpThis)
		return 0L;

	lpSystem = (__SYSTEM*)lpThis;

	return lpSystem->dwClockTickCounter;
}

//
//GetPhysicalMemorySize.
//

static DWORD GetPhysicalMemorySize(__COMMON_OBJECT* lpThis)
{
	if(NULL == lpThis)
		return 0L;

	return ((__SYSTEM*)lpThis)->dwPhysicalMemorySize;
}

//
//DispatchInterrupt.
//The routine calls another routine,DefaultIntHandler,to handle the interrupt
//without interrupt object.
//

//
//This routine is the default interrupt handler.
//If no entity(such as,kernel thread) install an interrupt handler,this handler
//will be called to handle the appropriate interrupt.
//

static VOID DefaultIntHandler(LPVOID lpEsp,UCHAR ucVector)
{
	BYTE          strBuffer[16] = {0};
	DWORD         dwTmp         = 0L;
	DWORD         dwLoop        = 0L;
	DWORD*        lpdwEsp       = NULL;
	WORD          wIsr;
	static DWORD  dwTotalNum    = 0L;

	dwTotalNum ++;    //Record this unhandled exception or interrupt.

	PrintLine("  Unhandled interrupt or Exception!");  //Print out this message.
	PrintLine("  Interrupt Vector:");

	dwTmp   = ucVector;
	lpdwEsp = (DWORD*)lpEsp;
	strBuffer[0] = ' ';
	strBuffer[1] = ' ';
	strBuffer[2] = ' ';
	strBuffer[3] = ' ';

	Hex2Str(dwTmp,&strBuffer[4]);
	PrintLine(strBuffer);  //Print out the interrupt or exception's vector.
	if(dwTmp == 0x2B)      //--------- ** debug ** ---------------
	{
		PrintLine("NIC interrupt : -------------------------------");
		//WriteWordToPort(0xd03e,0x0000);
		//ReadWordFromPort(&wIsr,(WORD)0xD03E);
		__asm
		{
			push eax
			push edx
			mov dx,0xd037
			mov al,0x10
			out dx,al      //Reset NIC
__REPEAT:
			in al,dx
			and al,0x10
			jnz __REPEAT   //Waiting the NIC to finished initialization.
			mov eax,0x00000100
			mov dx,0xd054
			out dx,eax
			mov dx,0xd03c
			mov ax,0xFFFF
			out dx,ax     //Set the IMR register.
			mov eax,0x00000010
			mov dx,0xd048
			out dx,eax    //Reset the timer register.
			pop edx
			pop eax
		}
	}
	PrintLine("  Context:");  //Print out system context information.
	for(dwLoop = 0;dwLoop < 12;dwLoop ++)
	{
		dwTmp = *lpdwEsp;
		Hex2Str(dwTmp,&strBuffer[4]);
		PrintLine(strBuffer);
		lpdwEsp ++;
	}

	if(dwTmp <= 0x20)    //---------- ** debug ** --------------
	{
		Hex2Str(dwTotalNum,strBuffer);
		PrintLine("Total unhandled exception or interrupt is:");
		PrintLine(strBuffer);
__LOOP:
	goto __LOOP;
	}

	/*
__DEADLOOP:    //If a unhandled interrupt or exception occurs,the system will halt.
	goto __DEADLOOP;*/
	__asm               //-------------- ** debug ** ------------
	{
		cli
	}

	return;
}


static VOID DispatchInterrupt(__COMMON_OBJECT* lpThis,
							  LPVOID           lpEsp,
							  UCHAR ucVector)
{
	__INTERRUPT_OBJECT*    lpIntObject  = NULL;
	__SYSTEM*              lpSystem     = NULL;

	if((NULL == lpThis) || (NULL == lpEsp))
		return;

	lpSystem = (__SYSTEM*)lpThis;
	
	lpSystem->ucIntNestLevel += 1;    //Increment nesting level.
	if(lpSystem->ucIntNestLevel <= 1)
	{
		//Call thread hook here,because current kernel thread is
		//interrupted.
		//If interrupt occurs before any kernel thread is scheduled,
		//lpCurrentKernelThread is NULL.
		if(KernelThreadManager.lpCurrentKernelThread)
		{
			KernelThreadManager.CallThreadHook(
				THREAD_HOOK_TYPE_ENDSCHEDULE,
				KernelThreadManager.lpCurrentKernelThread,
				NULL);
		}
	}
	lpIntObject = lpSystem->lpInterruptVector[ucVector];

	if(NULL == lpIntObject)  //The current interrupt vector has not handler object.
	{
		DefaultIntHandler(lpEsp,ucVector);
		goto __RETFROMINT;
	}

	while(lpIntObject)    //Travel the whole interrupt list of this vector.
	{
		if(lpIntObject->InterruptHandler(lpEsp,
			lpIntObject->lpHandlerParam))    //If an interrupt object handles the interrupt,then returns.
		{
			break;
		}
		lpIntObject = lpIntObject->lpNextInterruptObject;
	}

__RETFROMINT:
	lpSystem->ucIntNestLevel -= 1;    //Decrement interrupt nesting level.
	if(0 == lpSystem->ucIntNestLevel)  //The outmost interrupt.
	{
		KernelThreadManager.ScheduleFromInt((__COMMON_OBJECT*)&KernelThreadManager,
			lpEsp);  //Re-schedule kernel thread.
	}
	else
	{
		BUG();  //In current version(V1.5),interrupt nesting is not supportted yet.
	}
	return;
}

//
//SetTimer.
//The routine do the following:
// 1. Create a timer object;
// 2. Initialize the timer object;
// 3. Insert into the timer object into timer queue of system object;
// 4. Return the timer object's base address if all successfully.
//

static __COMMON_OBJECT* SetTimer(__COMMON_OBJECT* lpThis,
								 __KERNEL_THREAD_OBJECT* lpKernelThread,
					             DWORD  dwTimerID,
								 DWORD  dwTimeSpan,
								 __DIRECT_TIMER_HANDLER lpHandler,
					             LPVOID lpHandlerParam,
								 DWORD  dwTimerFlags)
{
	__PRIORITY_QUEUE*            lpPriorityQueue    = NULL;
	__SYSTEM*                    lpSystem           = NULL;
	__TIMER_OBJECT*              lpTimerObject      = NULL;
	BOOL                         bResult            = FALSE;
	DWORD                        dwPriority         = 0L;
	DWORD                        dwFlags            = 0L;

	if((NULL == lpThis) ||
	   (NULL == lpKernelThread))    //Parameters check.
	   return NULL;

	if(dwTimeSpan <= SYSTEM_TIME_SLICE)
		dwTimeSpan = SYSTEM_TIME_SLICE;

	lpSystem    = (__SYSTEM*)lpThis;
	lpTimerObject = (__TIMER_OBJECT*)ObjectManager.CreateObject(&ObjectManager,
		NULL,
		OBJECT_TYPE_TIMER);
	if(NULL == lpTimerObject)    //Can not create timer object.
		goto __TERMINAL;
	bResult = lpTimerObject->Initialize((__COMMON_OBJECT*)lpTimerObject);  //Initialize.
	if(!bResult)
		goto __TERMINAL;

	lpTimerObject->dwTimerID           = dwTimerID;
	lpTimerObject->dwTimeSpan          = dwTimeSpan;
	lpTimerObject->lpKernelThread      = lpKernelThread;
	lpTimerObject->DirectTimerHandler  = lpHandler;
	lpTimerObject->lpHandlerParam      = lpHandlerParam;
	lpTimerObject->dwTimerFlags        = dwTimerFlags;

	//
	//The following code calculates the priority value of the timer object.
	//
	dwPriority     = dwTimeSpan;
	dwPriority    /= SYSTEM_TIME_SLICE;
	dwPriority    += lpSystem->dwClockTickCounter;    //Now,the dwPriority countains the
	                                                  //tick counter this timer must be
	                                                  //processed.
	dwPriority     = MAX_DWORD_VALUE - dwPriority;    //Final priority value.

	__ENTER_CRITICAL_SECTION(NULL,dwFlags);
	bResult = lpSystem->lpTimerQueue->InsertIntoQueue((__COMMON_OBJECT*)lpSystem->lpTimerQueue,
		(__COMMON_OBJECT*)lpTimerObject,
		dwPriority);
	if(!bResult)
	{
		__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
		goto __TERMINAL;
	}

	dwPriority = MAX_DWORD_VALUE - dwPriority;    //Now,dwPriority countains the next timer
	                                              //tick value.
	//ENTER_CRITICAL_SECTION();
	//__ENTER_CRITICAL_SECTION(NULL,dwFlags);
	if((System.dwNextTimerTick > dwPriority) ||
	   (System.dwNextTimerTick == 0))
		System.dwNextTimerTick = dwPriority;    //Update the next timer tick counter.
	//LEAVE_CRITICAL_SECTION();
	__LEAVE_CRITICAL_SECTION(NULL,dwFlags);

__TERMINAL:
	if(!bResult)
	{
		if(lpTimerObject != NULL)
		{
			ObjectManager.DestroyObject(&ObjectManager,(__COMMON_OBJECT*)lpTimerObject);
			lpTimerObject = NULL;
		}
	}
	return (__COMMON_OBJECT*)lpTimerObject;
}

//
//CancelTimer implementation.
//This routine is used to cancel timer.
//

static VOID CancelTimer(__COMMON_OBJECT* lpThis,__COMMON_OBJECT* lpTimer)
{
	__SYSTEM*                  lpSystem       = NULL;
	DWORD                      dwPriority     = 0L;
	__TIMER_OBJECT*            lpTimerObject  = NULL;

	if((NULL == lpThis) || (NULL == lpTimer))
		return;

	lpSystem = (__SYSTEM*)lpThis;
	//if(((__TIMER_OBJECT*)lpTimer)->dwTimerFlags != TIMER_FLAGS_ALWAYS)
	//	return;
	lpSystem->lpTimerQueue->DeleteFromQueue((__COMMON_OBJECT*)lpSystem->lpTimerQueue,
		lpTimer);
	lpTimerObject = (__TIMER_OBJECT*)
		lpSystem->lpTimerQueue->GetHeaderElement(
		(__COMMON_OBJECT*)lpSystem->lpTimerQueue,
		&dwPriority);
	if(NULL == lpTimerObject)    //There is not any timer object to be processed.
		return;

	//
	//The following code updates the tick counter that timer object should be processed.
	//
	dwPriority = MAX_DWORD_VALUE - dwPriority;
	if(dwPriority > lpSystem->dwNextTimerTick)
		lpSystem->dwNextTimerTick = dwPriority;
	dwPriority = MAX_DWORD_VALUE - dwPriority;
	lpSystem->lpTimerQueue->InsertIntoQueue(
		(__COMMON_OBJECT*)lpSystem->lpTimerQueue,
		(__COMMON_OBJECT*)lpTimerObject,
		dwPriority);    //Insert into timer object queue.

	return;

	/*__SYSTEM*  lpSystem  = NULL;

	if((NULL == lpThis) || (NULL == lpTimer))
		return;

	lpSystem = (__SYSTEM*)lpThis;
	//if(((__TIMER_OBJECT*)lpTimer)->dwTimerFlags != TIMER_FLAGS_ALWAYS)
	//	return;
	if(lpSystem->lpTimerQueue->DeleteFromQueue((__COMMON_OBJECT*)lpSystem->lpTimerQueue,
		lpTimer))
	{
		ObjectManager.DestroyObject(&ObjectManager,
			lpTimer);
	}
	return;*/
}

/***************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
***************************************************************************************/

//The definition of system object.

__SYSTEM System = {
	{0},                      //lpInterruptVector[MAX_INTERRUPT_VECTOR].
	NULL,                     //lpTimerQueue.
	0L,                       //dwClockTickCounter,
	0L,                       //dwNextTimerTick,
	0,                        //ucIntNestLeve;
	0,
	0,
	0,                        //ucReserved3;
	0L,                       //dwPhysicalMemorySize,
    SystemInitialize,         //Initialize routine.
	GetClockTickCounter,      //GetClockTickCounter routine.
	GetPhysicalMemorySize,    //GetPhysicalMemorySize routine.
	DispatchInterrupt,        //DispatchInterrupt routine.
	ConnectInterrupt,         //ConnectInterrupt.
	DisconnectInterrupt,      //DisconnectInterrupt.
	SetTimer,                 //SetTimerRoutine.
	CancelTimer
};

//***************************************************************************************
//
//             General Interrupt Handler
//
//***************************************************************************************

//
//GeneralIntHandler.
//This routine is the general handler of all interrupts.
//Once an interrupt occurs,the low layer code (resides in Mini-Kernel) calls this routine,
//this routine then calls DispatchInterrupt of system object.
//

VOID GeneralIntHandler(DWORD dwVector,LPVOID lpEsp)
{
	UCHAR    ucVector = LOBYTE(LOWORD(dwVector));

	System.DispatchInterrupt((__COMMON_OBJECT*)&System,
		lpEsp,
		ucVector);
}




⌨️ 快捷键说明

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