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

📄 excmgr.c

📁 T-kernel 的extension源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *---------------------------------------------------------------------- *    T-Kernel / Standard Extension * *    Copyright (C) 2006 by Ken Sakamura. All rights reserved. *    T-Kernel / Standard Extension is distributed  *      under the T-License for T-Kernel / Standard Extension. *---------------------------------------------------------------------- * *    Version:   1.00.00 *    Released by T-Engine Forum(http://www.t-engine.org) at 2006/8/11. * *---------------------------------------------------------------------- *//* *	excmgr.c (memory) * *	System exception handling (SH3 SH4 Virtual storage version) */#include "segmgr.h"#include <sys/sysexc.h>#include <tm/tmonitor.h>#include <sys/bdm.h>IMPORT	void	_ExitTask( PINFO *pi, ID tskid );  /* proc/prcmgr.c */#define	BMS_KILL_PROC	TRAP_KILLPROC	/* Process forced termination request from BMS *//* Message to system exception handling task */typedef struct {	BDM_ExcInfo	i;		/* Exception information */	unsigned int	pgfault:1;	/* Page fault: TRUE */	unsigned int	killproc:1;	/* Process forced termination request: TRUE */} ExcMsg;/* Message buffer for communicating with system exception handling task */#define	MaxExcMsg	64ULOCAL ExcMsg	ExcMsg_buf[MaxExcMsg];LOCAL UW	ExcMsg_top, ExcMsg_cnt;LOCAL ID	SysExcTskID;		/* System exception handling task *//* * Structure of interrupt stack at exception handler entry */typedef struct {	UW	r0_bank1;	UW	spc;	UW	ssr;	UW	mdr;	UW	usp;} ExcStack;Inline	BOOL	TaskIndependent( ExcStack *isp );LOCAL void print_regs( ID tid );LOCAL ER sendExcMsg( ExcMsg *msg );LOCAL void recvExcMsg( ExcMsg *msg );LOCAL void SysExcTsk( void );LOCAL void _SysExcHdr( UW exccode, UW excinfo, ExcStack *isp );LOCAL void SysExcHdr( UW exccode, UW excinfo );LOCAL ER RegistPageFaultHdr( void );#define TSD_HDR_VAL_3		3#define TSD_SEH_SFT_2		2U#define TSD_IEM_SFT_5		5#define TSD_HDR_MSK_15		15#define TSD_PFH_VAL_M1		(-1)#define TSD_SEH_VAL_0X160	0x160U#define TSD_RSH_DHL_0X1000	0x1000U#define TSD_IEM_STK_2048	2048/* * Exception occurred in the task-independent section: TRUE */Inline BOOL TaskIndependent( ExcStack *isp ){IMPORT	VW	*int_stack_top;	/* Exception/interrupt stack top (T-Kernel) */	W	n;	n = ( (isp->mdr & (UW)MDR_CPL(TSD_HDR_VAL_3)) > 0U )?		sizeof(ExcStack): offsetof(ExcStack, usp);	return ( ((B*)isp + n) < (B*)int_stack_top );}/* * Indicate task register value *	PC: 12345678 SR: 12345678 MDR:12345678 *	R0: 12345678 R1: 12345678 R2: 12345678 R3: 12345678 *	R4: 12345678 R5: 12345678 R6: 12345678 R7: 12345678 *	R8: 12345678 R9: 12345678 R10:12345678 R11:12345678 *	R12:12345678 R13:12345678 R14:12345678 *	MAC:12345678 12345678     GBR:12345678 PR: 12345678 *	USP:12345678 SSP:12345678 LSID:1234   UATB:12345678 */LOCAL void print_regs( ID tid ){#if DEBUGFUNC_SUPPORT	T_REGS	g;	T_EIT	e;	T_CREGS	c;	ER	er;	er = tk_get_reg(tid, &g, &e, &c);	if ( er < E_OK ) {		BMS_DEBUG_PRINT(("- register can not read -\n"));		return;	}	PrintTaskRegister(bms_printf, &g, &e, &c);#endif /* DEBUGFUNC_SUPPORT */}/* * Send exception message *	Called in interrupt-disabled state */LOCAL ER sendExcMsg( ExcMsg *msg ){	UW	pos;	if ( ExcMsg_cnt >= (UW)MaxExcMsg ) {		return E_LIMIT;	}	pos = (UW)((ExcMsg_top + ExcMsg_cnt) % (UW)MaxExcMsg);	ExcMsg_buf[pos] = *msg;	if ( ExcMsg_cnt++ == 0U ) {		tk_wup_tsk(SysExcTskID);	}	return E_OK;}/* * Receive exception message */LOCAL void recvExcMsg( ExcMsg *msg ){	UINT	imask;	for ( ;; ) {		DI(imask);		if ( ExcMsg_cnt > 0U ) {			*msg = ExcMsg_buf[ExcMsg_top];			if ( ++ExcMsg_top >= (UW)MaxExcMsg ) {				ExcMsg_top = 0U;			}			ExcMsg_cnt--;			break;		}		EI(imask);		tk_slp_tsk(TMO_FEVR);	}	EI(imask);}/* * System exception handling task */LOCAL void SysExcTsk( void ){#if USE_PROCESS_MANAGERIMPORT	WER	_snd_hdr_msg( MESSAGE *msg, W opt );	/* Message management */#endif	ExcMsg	excmsg;	PINFO	*pinfo;	ID	oldrid;	EXCMESG	msg;	ER	er;	for ( ;; ) {	/* Not terminate on its own */		/* Receive system exception */		recvExcMsg(&excmsg);		if ( excmsg.pgfault != 0U ) {			/* Handle page fault */			UW lsid = GetLSID_tid(excmsg.i.taskid);			LockSEG();			if ( excmsg.i.exccode != EXC_TLBPRO_W ) {				er = PageIn((VP)excmsg.i.excaddr, lsid);			} else {				er = CopyOnWrite((VP)excmsg.i.excaddr, lsid);			}			if ( er >= E_OK ) {				/* Load into TLB. */				UW pte = GET_PTE((VP)excmsg.i.excaddr, lsid);				UpdateTLB((VP)excmsg.i.excaddr, lsid, pte);			}			UnlockSEG();			if ( er >= E_OK ) {				/* Reopen intended task */				er = tk_frsm_tsk(excmsg.i.taskid);				if ( er >= E_OK ) {					continue;				}			}			DEBUG_PRINT(("SysExcTsk PageIn err = %d\n", er));		}#if USE_PROCESS_MANAGER		/* Obtain PINFO on exception occurrence process. */		pinfo = GetPinfo(excmsg.i.taskid);		if ( pinfo == NULL ) {			goto system_down;		}		/* If the task is in the course of termination processing,		   call task termination processing directly from process management. */		if ( pinfo->exitkind != 0 ) {			oldrid = tk_set_rid(TSK_SELF, pinfo->resid);			_ExitTask(pinfo, excmsg.i.taskid);			tk_set_rid(TSK_SELF, oldrid);			continue;		}		if ( excmsg.killproc != 0U ) {			goto kill_proc;		}		/* Send message */		msg.type = MS_SYS1;		msg.size = (W)(sizeof(EXCMESG) - offsetof(EXCMESG, taskid));		msg.taskid = excmsg.i.taskid;		msg.procid = pinfo->procid;		msg.exccode = excmsg.i.exccode;		msg.excinfo = excmsg.i.excinfo;		msg.excaddr = excmsg.i.excaddr;		/* Send to exception server */		er = _snd_hdr_msg((MESSAGE*)&msg, NOWAIT);		if ( er <= 0 ) {			DEBUG_PRINT(("SysExcTsk _snd_hdr_msg err = %d\n", er));		kill_proc:			/* If message could not be sent to exception server,			   forcedly terminate the process. */			tk_ras_tex(excmsg.i.taskid, 0);			if ( excmsg.i.taskid != pinfo->tsk[0].tskid ) {				/* Terminate main task as well. */				tk_ras_tex(pinfo->tsk[0].tskid, 0);			}			pinfo->exitkind = MS_ABORT;			tk_frsm_tsk(excmsg.i.taskid);		}#endif	}system_down:	/* Use BMS to output message. */	BMS_DEBUG_PRINT(("\n***** OS SYSTEM DOWN *****\n"));	BMS_DEBUG_PRINT(("INT:%03x TEA:%08x tid:%d\n",		excmsg.i.exccode, excmsg.i.excaddr, excmsg.i.taskid));	print_regs(excmsg.i.taskid);	tm_monitor();	/* To BMS */}/* * Body of system exception handler */LOCAL void _SysExcHdr( UW exccode, UW excinfo, ExcStack *isp ){	ExcMsg		excmsg;	PINFO		*pinfo;	ER		er;	memset(&excmsg, 0, (size_t)sizeof(excmsg));	excmsg.i.exccode = exccode;	excmsg.i.excinfo = excinfo;	excmsg.i.excaddr = *(_UW*)TEA;	excmsg.pgfault = FALSE;	if ( (exccode == TSD_SEH_VAL_0X160) /*TRAPA*/	  && (excinfo == ((UW)BMS_KILL_PROC << TSD_SEH_SFT_2)) ) {		/* Not considered as BMS_KILL_PROC request		   when called from CPL > 0. */		if ( (isp->mdr & MDR_CPL(TSD_HDR_VAL_3)) == 0U ) {			/* In the case of BMS_KILL_PROC, its immediately preceding stack			   indicates the actual state at the occurrence of exception. */			isp = (ExcStack*)&isp->usp;			excmsg.killproc = TRUE;			/* Turn back interrupt stack. */			Asm("ldc %0, r6_bank":: "r"(isp));		}	}	/* Obtain intended task ID */	excmsg.i.taskid = er = tk_get_tid();	if ( er < E_OK ) {		goto system_down;	}	/* If an exception occurs during the execution of interrupt handler (task independent	   section) or in the interrupt-disabled state, shut down the system. */	if ( ((isp->ssr & SR_I(TSD_HDR_MSK_15)) > 0U)	  || TaskIndependent(isp) ) {		goto system_down;	}	/* Notify BDM of the exception. */	er = BDM_NotifyException(&excmsg.i);	if ( er == E_OK ) {		return;	}	/* If BDM does not exist, continue exception handling. */	/* If an exception occurs during the execution of a task (extension, SVC, non-process,	   etc.) within OS, shut down the system. */	if ( ((isp->mdr & MDR_CPL(TSD_HDR_VAL_3)) == 0U)	  || ((pinfo = GetPinfo(excmsg.i.taskid)) == NULL) ) {		goto system_down;	}	/* If the current task is a system process that is in the course of termination	   processing, shut down the system. */	if ( (pinfo->exitkind != 0)	  && ((isp->mdr & MDR_CPL(TSD_HDR_VAL_3)) < MDR_CPL(TSD_HDR_VAL_3)) ) {		goto system_down;	}	/* Suspend current task */	er = tk_sus_tsk(excmsg.i.taskid);	if ( er < E_OK ) {		goto system_down;	}	/* Notify to exception handling task */	er = sendExcMsg(&excmsg);	if ( er < E_OK ) {		goto system_down;	}	return;	/** Use BMS to indicate messages **/

⌨️ 快捷键说明

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