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

📄 tp3_esr.c

📁 小型操作系统,以VC为开发环境,需要boachs调试
💻 C
字号:
/***************************************************************************
**     File name   : tp3_esr.c
**     Author      : x.cheng
**     Create date :
**
**	   Comment:
**        Exceptions manager....
**
**     Revisions:
**     $Log: tp3_esr.c,v $
**     Revision 1.2  2005/08/04 15:31:54  x.cheng
**     breakpoint(#BP) trap handling~~~~~bug fix
**
**     Revision 1.1  2005/08/04 08:32:12  x.cheng
**     add into repositories
**
**
***************************************************************************/
#include "const.h"
#include "type.h"
#include "stdarg.h"

#include "..\..\Inc\i386\system.h"
#include "..\..\Inc\i386\io.h"
#include "..\..\Inc\i386\exception.h"
#include "..\..\Inc\i386\page.h"
#include "..\..\Inc\mts.h"
#include "..\..\Inc\pmm.h"
#include "..\..\Inc\tui.h"
#include "..\..\Inc\debug.h"


/****************extern variable*******************/
	// in file tp1_exception.asmp
extern void exc_DivideByZero, exc_Debug, exc_Nmi, exc_BreakPoint, exc_Overflow, exc_BoundRangeExceeded;
extern void exc_InvalidOpcode, exc_DeviceNotExist, exc_80x87OverRun, exc_InvalidTss, exc_SegmentNotPresent;
extern void exc_StackFault, exc_GeneralProtectFault, exc_PageFault, exc_Reserved;
extern void exc_MathFault, exc_AlignmentCheck, exc_MachineCheck, exc_SimdExtensions, exc_UnHandle;


/************local function prototype**************/
static inline void Tp3EsrDumpRegisters( ts_ExceptionContext* pstContext );

/************************************************************
*************************************************************
**      Function Name:			DefaultExceptionHandler
**      Author:                 x.cheng
**
**      Comment:
**			This is the default exception handler. It is invoked
**		  every time an exception occurs. The kernel must route the
**		  exececution to the opportune procedures to correctly manage
**		  the exception.
**
**      List of parameters:
**			pstContext - The context of the current task after an exception.
**
**      Return value:   
**          no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void DefaultExceptionHandler(ts_ExceptionContext* pstContext)
{
	ts_Task *pstTask;

	switch( pstContext->ulExcNb ) {
	case 0x00:
		kprintf("\nException 00: DIVISION BY ZERO\n");
		break;

	case 0x01:
		kprintf("\nException 01: DEBUG EXCEPTION DETECTED\n");
		break;

	case 0x02:
		kprintf("\nException 02: NON MASKABLE INTERRUPT\n");
		break;

	case 0x03:
		kprintf("\nException 03: BREAKPOINT INSTRUCTION DETECTED\n");
		Tp3EsrDumpRegisters( pstContext );
		pstTask = pstMtsGetCurrentTask();
		kprintf("\nBreakpoint from task [%s] (%i)\n", pstTask->szName, pstTask->iPid);
		// After a breakpoint we can restore execution.
		return;
		break;

	case 0x04:
		kprintf("\nException 04: INTO DETECTED OVERFLOW\n");
		break;

	case 0x05:
		kprintf("\nException 05: BOUND RANGE EXCEEDED\n");
		break;

	case 0x06:
		kprintf("\nException 06: INVALID OPCODE\n");
		break;

	case 0x07:
		kprintf("\nException 07: PROCESSOR EXTENSION NOT AVAILABLE\n");
		break;

	case 0x08:
		kprintf("\nException 08: DOUBLE FAULT DETECTED\n");
		break;
	
	case 0x09:
		kprintf("\nException 09: PROCESSOR EXTENSION PROTECTION FAULT\n");
		break;

	case 0x0A:
		kprintf("\nException 0A: INVALID TASK STATE SEGMENT\n");
		break;

	case 0x0B:
		kprintf("\nException 0B: SEGMENT NOT PRESENT\n");
		break;

	case 0x0C:
		kprintf("\nException 0C: STACK FAULT\n");
		break;

	case 0x0D:
/*
			if ((pstContext->ulEflags & EFLAGS_VM) == EFLAGS_VM)
			{
				v86_monitor( pstContext );
				return;
			}
*/
		kprintf("\nException 0D: GENERAL PROTECTION FAULT\n");
		break;

	case 0x0E:
//		Tp3EsrDumpRegisters( pstContext ); PANIC();
		if ((pstContext->ulErrorCode & P_PRESENT) != P_PRESENT) {
			register unsigned long ulCr2;

			__asm__ __volatile__ ("movl %%cr2, %0" : "=&r"(ulCr2));

//			kprintf("err= %x, ", pstContext->ulErrorCode);
			if ( !iPmmPageFaultHandler(ulCr2) ) {
				return;
			}
		} else if ((pstContext->ulErrorCode & P_WRITE) == P_WRITE) {
			register unsigned long ulCr2;

			__asm__ __volatile__ ("movl %%cr2, %0" : "=&r"(ulCr2));

			if ( !iPmmPageWriteProtectHandler(ulCr2) ) {
				return;
			}
		}
		kprintf("\nException 0E: PAGE FAULT (protection fault)!\n");
		break;

	case 0x0F:
		kprintf("\nException 0F: RESERVED EXCEPTION\n");
		break;

	case 0x10:
		kprintf("\nException 0x10: MATH FAULT EXCEPTION\n");
		break;

	case 0x11:
		kprintf("\nException 0x11: ALIGNMENT CHECK EXCEPTION\n");
		break;

	case 0x12:
		kprintf("\nException 0x12: MACHINE CHECK EXCEPTION\n");
		break;

	case 0x13:
		kprintf("\nException 0x13: STREAMING SIMD EXTENSIONS EXCEPTION\n");
		break;

	default:
		kprintf("\nException %02X: UNEXPECTED !!!\n", pstContext->ulExcNb);
		break;
	}

	Tp3EsrDumpRegisters( pstContext );
	PANIC();
}

/************************************************************
*************************************************************
**      Function Name:			Tp3EsrInitialize
**      Author:                 x.cheng
**
**      Comment:
**
**
**      List of parameters:
**			no
**
**      Return value:   
**          no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void Tp3EsrInitialize( void )
{
	int i;

	// Initialize exception handlers (0x00..0x1F).
	for( i = 0x00; i <= 0x1F; i++ )
		vSetIntrGate( i, &exc_UnHandle );

	vSetTrapGate( 0x00, &exc_DivideByZero );	// divide error.
	vSetTrapGate( 0x01, &exc_Debug );			// debug.
	vSetTrapGate( 0x02, &exc_Nmi );				// nmi.
	//vSetSystemGate( 0x03, &exc_BreakPoint );	// int3.
	vSetTrapGate( 0x03, &exc_BreakPoint );	// int3.
	
	vSetSystemGate( 0x04, &exc_Overflow );		// overflow.
	vSetSystemGate( 0x05, &exc_BoundRangeExceeded );	// bounds.
	vSetTrapGate( 0x06, &exc_InvalidOpcode );			// invalid operation.
	vSetTrapGate( 0x07, &exc_DeviceNotExist );			// device not available.
	//set_task_gate( 0x08, GDT_DOUBLE_FAULT_ENTRY );	// double fault.
	vSetTrapGate( 0x09, &exc_80x87OverRun );			// coprocessor segment overrun.
	vSetTrapGate( 0x0a, &exc_InvalidTss );				// invalid tss.
	vSetTrapGate( 0x0b, &exc_SegmentNotPresent );		// segment not present.
	vSetTrapGate( 0x0c, &exc_StackFault );				// stack fault.
	vSetTrapGate( 0x0d, &exc_GeneralProtectFault );		// general protection.
	vSetTrapGate( 0x0e, &exc_PageFault );				// page fault.
	vSetTrapGate( 0x0f, &exc_Reserved );				// spurious interrupt.

	vSetTrapGate( 0x10, &exc_MathFault );				// Math Fault	
	vSetTrapGate( 0x11, &exc_AlignmentCheck );			// Alignment Check
	vSetTrapGate( 0x12, &exc_MachineCheck );			// Machine Check
	vSetTrapGate( 0x13, &exc_SimdExtensions );			// Streaming SIMD extensions
}



//----------------------------------------------------------------------------------
//==============================local function======================================
//----------------------------------------------------------------------------------



/************************************************************
*************************************************************
**      Function Name:			Tp3EsrDumpRegisters
**      Author:                 x.cheng
**
**      Comment:
**			Dump the CPU registers.
**
**      List of parameters:
**			pstContext - The context of the current task after an exception.
**
**      Return value:   
**          no
**
**      Revisions:
**
*************************************************************
*************************************************************/
static inline void Tp3EsrDumpRegisters( ts_ExceptionContext* pstContext )
{
	register unsigned short uiSs;
	register unsigned long ulCr0, ulCr2, ulCr3, ulCr4;
	register unsigned short uiFs, uiGs;

	// Save stack segment register.
	__asm__ __volatile__ ("movw %%ss, %0" : "=&r"(uiSs) : );

	// Save %fs and %gs registers.
	__asm__ __volatile__ ("movw %%fs, %0" : "=m"(uiFs) :);
	__asm__ __volatile__ ("movw %%gs, %0" : "=m"(uiGs) :);

	// Save control registers.
	__asm__ __volatile__ ("movl %%cr0, %0" : "=&r"(ulCr0) : );
	__asm__ __volatile__ ("movl %%cr2, %0" : "=&r"(ulCr2) : );
	__asm__ __volatile__ ("movl %%cr3, %0" : "=&r"(ulCr3) : );
	__asm__ __volatile__ ("movl %%cr2, %0" : "=&r"(ulCr4) : );

	// Dump registers.
	kprintf("\neax = %#010x  ds = %#010x  cr0 = %#010x  esp    = %#010x", pstContext->ulEax, (unsigned short)(pstContext->ulDs), ulCr0, pstContext);
	kprintf("\nebx = %#010x  es = %#010x  cr2 = %#010x  ebp    = %#010x", pstContext->ulEbx, (unsigned short)(pstContext->ulEs), ulCr2, pstContext->ulEbp);
	kprintf("\necx = %#010x  fs = %#010x  cr3 = %#010x  eip    = %#010x", pstContext->ulEcx, (unsigned short)(uiFs), ulCr3, pstContext->ulEip);
	kprintf("\nedx = %#010x  gs = %#010x  cr4 = %#010x  eflags = %#010x", pstContext->ulEdx, (unsigned short)(uiGs), ulCr4, pstContext->ulEflags);
	kprintf("\nesi = %#010x  ss = %#010x  exc = %#010x", pstContext->ulEsi, (unsigned short)(uiSs), pstContext->ulExcNb);
	kprintf("\nedi = %#010x  cs = %#010x  err = %#010x", pstContext->ulEdi, (unsigned short)(pstContext->ulCs), pstContext->ulErrorCode);
}

⌨️ 快捷键说明

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