Task Exception Handling Functions

Task exception handling functions handle exception events that are raised for a task in the context of that task.

The task exception handler is started when all the following processing has taken place:

  1. Register task exception handler by tk_def_tex

  2. Enable task exception by tk_ena_tex

  3. Raise task exception by tk_ras_tex

A task exception handler is executed as a part of the task where the task exception occurred, in the context of that task and at the protection level specified when the task was created. The task states in a task exception handler, except for those states concerning task exceptions, are the same as the states when running an ordinary task portion; and the same set of system calls are available.

A task exception handler can be started only when the target task is running in a task portion. If the task is running in any other portion when a task exception is raised, the task exception handler is started only after the control returns to the task portion. If a quasi-task portion (extended SVC) is executing when a task exception is raised, a break function corresponding to that extended SVC is called. The break function interrupts the extended SVC processing, and the task returns to the task portion.

Requested task exceptions are cleared when the task exception handler is called (when the task exception handler starts running).

Task exceptions are specified by task exception codes from 0 to 31, of which 0 has the highest priority and 31 the lowest. Task exception code 0 is handled differently from the others, as explained below.

Task exception codes 1 to 31 :

  • These task exception handlers cannot be executed by nesting them. A task exception (other than task exception code 0) raised while a task exception handler is running will be made pending.

  • On return from a task exception handler, the task resumes from the point where processing was interrupted by the exception.

  • It is also possible to use longjmp() or the like to jump to any point in the task without returning from the task exception handler.

Task exception code 0:

  • This exception can be executed by nesting it even while a task exception handler is executing for an exception of task exception code 1 to 31. Execution of task exception code 0 handlers is not nested.

  • A task exception handler runs after setting the user stack pointer to the initial setting when the task was started. In a system without a separate user stack and system stack, however, the stack pointer is not reset to its initial setting.

  • A task exception code 0 handler does not return to task processing. The task must be terminated by calling tk_ext_tsk or tk_exd_tsk.

tk_def_tex - Define Task Exception Handler

C Language Interface

#include <tk/tkernel.h>

ER ercd = tk_def_tex (ID tskid , CONST T_DTEX *pk_dtex );

Parameter

ID tskid Task IDTask ID
CONST T_DTEX* pk_dtex Packet to Define Task ExceptionTask exception handler definition information

pk_dtex Detail:

ATR texatr Task Exception AttributeTask exception handler attributes
FP texhdr Task Exception HandlerTask exception handler address
(Other implementation-dependent parameters may be added beyond this point.)

Return Parameter

ER ercd Error CodeError code

Error Code

E_OK Normal completion
E_NOMEM Insufficient memory (memory for control block cannot be allocated)
E_ID Invalid ID number (tskid is invalid or cannot be used)
E_NOEXS Object does not exist (the task specified in tskid does not exist)
E_OBJ Invalid object state (the task specified in tskid runs at protection level 0 (TA_RNG0))
E_RSATR Reserved attribute (texatr is invalid or cannot be used)
E_PAR Parameter error (pk_dtex is invalid or cannot be used)

Valid Context

Task portionQuasi-task portionTask-independent portion
YESYESNO

Description

Defines a task exception handler for the task specified in tskid. Only one task exception handler can be defined per task; if one is already defined, the last-defined handler is valid. Setting pk_dtex = NULL cancels a definition.

Defining or canceling a task exception handler clears pending task exception requests and disables all task exceptions.

texatr indicates system attributes in its lower bits and implementation-dependent attributes in its higher bits. The texatr system attributes are not assigned in the present version of T-Kernel specification, and system attributes are not used.

A task exception handler takes the following form.

void texhdr( INT texcd )
{
        /*
                Task exception handling
        */

        /* Task exception handler termination */
        if ( texcd == 0 ) {
                tk_ext_tsk() or tk_exd_tsk();
        } else {
                tk_end_tex();
                return or longjmp();
        }
}

A task exception handler behaves like a TA_ASM attribute object and cannot be called via a high-level language support routine. The entry part of the task exception handler must be written in assembly language. The kernel vendor must provide the assembly language source code of the entry routine for calling the above C language task exception handler. That is, source code equivalent to a high-level language support routine must be provided.

A task set to protection level TA_RNG0 when it is created cannot use task exceptions.

Additional Notes

At the time a task is created, no task exception handler is defined and task exceptions are disabled.

When a task reverts to DORMANT state, the task exception handler definition is canceled and task exceptions are disabled. Pending task exceptions are cleared. It is possible, however, to define a task exception handler for a task in DORMANT state.

Task exceptions are software interrupts raised by tk_ras_tex, with no direct relation to CPU exceptions.

tk_ena_tex - Enable Task Exception

C Language Interface

#include <tk/tkernel.h>

ER ercd = tk_ena_tex (ID tskid , UINT texptn );

Parameter

ID tskid Task IDTask ID
UINT texptn Task Exception PatternTask exception pattern

Return Parameter

ER ercd Error CodeError code

Error Code

E_OK Normal completion
E_ID Invalid ID number (tskid is invalid or cannot be used)
E_NOEXS Object does not exist (the task specified in tskid does not exist or no task exception handler is defined)
E_PAR Parameter error (texptn is invalid or cannot be used)

Valid Context

Task portionQuasi-task portionTask-independent portion
YESYESNO

Description

Enables task exceptions for the task specified in tskid.

The parameter texptn is a logical OR bit array representing task exception codes in the form 1 << task exception code.

tk_ena_tex enables the task exceptions specified in texptn. If the current exception enabled status is texmask, it changes as follows.

enable: texmask |= texptn

If all the bits of texptn are cleared to 0, no operation is made to texmask. No error will result in this case.

Task exceptions cannot be enabled for a task with no task exception handler defined.

This system call can be called to tasks in DORMANT state.

tk_dis_tex - Disable Task Exception

C Language Interface

#include <tk/tkernel.h>

ER ercd = tk_dis_tex (ID tskid , UINT texptn );

Parameter

ID tskid Task IDTask ID
UINT texptn Task Exception PatternTask exception pattern

Return Parameter

ER ercd Error CodeError code

Error Code

E_OK Normal completion
E_ID Invalid ID number (tskid is invalid or cannot be used)
E_NOEXS Object does not exist (the task specified in tskid does not exist or no task exception handler is defined)
E_PAR Parameter error (texptn is invalid or cannot be used)

Valid Context

Task portionQuasi-task portionTask-independent portion
YESYESNO

Description

Disables task exceptions for the task specified in tskid.

The parameter texptn is a logical OR bit array representing task exception codes in the form 1 << task exception code.

tk_dis_tex disables the task exceptions specified in texptn. If the current exception enabled status is texmask, it changes as follows.

disable: texmask &= ~texptn

If all the bits of texptn are cleared to 0, no operation is made to texmask. No error will result in either case.

A disabled task exception is ignored, and is not made pending. If exceptions are disabled for a task while there are pending task exceptions, the pending task exception requests are discarded (their pending status is cleared).

This system call can be called to tasks in DORMANT state.

tk_ras_tex - Raise Task Exception

C Language Interface

#include <tk/tkernel.h>

ER ercd = tk_ras_tex (ID tskid , INT texcd );

Parameter

ID tskid Task IDTask ID
INT texcd Task Exception CodeTask exception code (0 to 31)

Return Parameter

ER ercd Error CodeError code

Error Code

E_OK Normal completion
E_ID Invalid ID number (tskid is invalid or cannot be used)
E_NOEXS Object does not exist (the task specified in tskid does not exist or no task exception handler is defined)
E_OBJ Invalid object state (the task specified in tskid is in DORMANT state)
E_PAR Parameter error (texcd is invalid or cannot be used)
E_CTX Context error (issued from task-independent portion, or in dispatch disabled state)

Valid Context

Task portionQuasi-task portionTask-independent portion
YESYESNO

Description

Raises the task exception specified in texcd for the task specified in tskid. If the task specified in tskid disables the task exception specified in texcd, the raised task exception is ignored, and is not made pending. In this case, E_OK is returned to this system call.

If a task exception handler is already running in the task specified in tskid, the newly raised task exception is made pending. If an exception is pending, a break function is not executed even if the target task is executing an extended SVC.

In the case of texcd = 0, however, exceptions are not made pending even if the target task is executing an exception handler. If the target task is running a task exception handler for an exception of task exception codes 1 to 31, the task exception is accepted; and if an extended SVC is executing, a break function is called. If the target task is running a task exception handler for an exception of task exception code 0, task exceptions are ignored.

The invoking task can be specified by setting tskid = TSK_SELF = 0.

If this system call is issued from a task-independent portion, error code E_CTX is returned.

Additional Notes

If the target task is executing an extended SVC, the break function corresponding to the extended SVC runs as a quasi-task portion of the task that issued tk_ras_tex. That is, it is executed in the context of the quasi-task portion whose requesting task is the task that issued tk_ras_tex.

In such a case tk_ras_tex does not return control until the break function processing ends. For this reason, the specification does not allow tk_ras_tex to be issued from a task-independent portion.

Task exceptions raised in the task that called tk_ras_tex while the break function is running are held until the break function ends.

tk_end_tex - end task exception handler

C Language Interface

#include <tk/tkernel.h>

INT texcd = tk_end_tex (BOOL enatex );

Parameter

BOOL enatex Enable Task ExceptionTask exception handler calling enabled flag

Return Parameter

INT texcd Task Exception CodeRaised exception code (0 to 31)
orError CodeError code

Error Code

E_CTX Context error (called for other than a task exception handler or task exception code 0 (detection is implementation-dependent))

Valid Context

Task portionQuasi-task portionTask-independent portion
YESYESNO

Description

Ends a task exception handler and enables the new task exception handler. If there are pending task exceptions, the highest-priority task exception code among them is passed in the return code. If there are no pending task exceptions, 0 is returned.

If enatex = FALSE and there are pending task exception, calling the new task exception handler is not allowed. In this case, the exception handler specified in return code texcd is in running state upon return from tk_end_tex. If there are no pending task exceptions, calling the new task exception handler is allowed.

If enatex = TRUE, calling the new task exception handler is allowed regardless of whether there are pending task exceptions. Even if there are pending task exceptions, the task exception handler is in terminated status.

There is no way of ending a task exception handler other than by calling tk_end_tex. A task exception handler continues executing from the time it is started until tk_end_tex is called. Even if return is made from a task exception handler without calling tk_end_tex, the task exception handler will still be running at the point of return. Similarly, even if longjmp is used to get out of a task exception handler without calling tk_end_tex, the task exception handler will still be running at the jump destination.

Calling tk_end_tex while task exceptions are pending results in a new task exception being accepted. At this time even when tk_end_tex is called from an extended SVC handler, a break function cannot be called for that extended SVC handler. If extended SVC calls are nested, then when the extended SVC nesting goes down one level, the break function corresponding to the extended SVC return destination can be called. Calling of a task exception handler takes place upon return to the task portion.

The tk_end_tex system call cannot be issued in the case of task exception code 0 since the task exception handler cannot be ended in this case. The task must be terminated by calling tk_ext_tsk or tk_exd_tsk. If tk_end_tex is called while processing the task exception code 0, the behavior is undefined (implementation-dependent).

This system call cannot be issued from other than a task exception handler. The behavior when it is called from other than a task exception handler is undefined (implementation-dependent).

Additional Notes

When tk_end_tex (TRUE) is called and there are pending task exceptions, another task exception handler call is made immediately following tk_end_tex. In this case, a task exception handler is called without restoring the stack, giving rise to possible stack overflow.

Ordinarily tk_end_tex (FALSE) can be used, and processing looped as illustrated below while there are task exceptions pending.

void texhdr( INT texcd )
{
	if ( texcd == 0 ){
		/*
			Processing for task exception 0
		*/
		tk_exd_tsk();
	}

	do {
		/*
			Processing for task exception 1~31
		*/
	} while ( (texcd = tk_end_tex(FALSE)) > 0 );
}

Strictly speaking, if a task exception were to occur during the interval after 0 is returned by tk_end_tex ending the loop and before exit from texhdr, the possibility exists of reentering texhdr without restoring the stack. Since task exceptions are software driven, however, ordinarily they do not occur independently of executing tasks; so in practice this is not a problem.

tk_ref_tex - Reference Task Exception Status

C Language Interface

#include <tk/tkernel.h>

ER ercd = tk_ref_tex (ID tskid , T_RTEX *pk_rtex );

Parameter

ID tskid Task IDTask ID
T_RTEX* pk_rtex Packet to Return Task Exception StatusPointer to the area to return the task exception status

Return Parameter

ER ercd Error CodeError code

pk_rtex Detail:

UINT pendtex Pending Task ExceptionPending task exceptions
UINT texmask Task Exception MaskAllowed task exceptions
(Other implementation-dependent parameters may be added beyond this point.)

Error Code

E_OK Normal completion
E_ID Invalid ID number (tskid is invalid or cannot be used)
E_NOEXS Object does not exist (the task specified in tskid does not exist)
E_PAR Parameter error (invalid pk_rtex)

Valid Context

Task portionQuasi-task portionTask-independent portion
YESYESNO

Description

Gets the status of task exceptions for the task specified in tskid.

pendtex indicates the currently pending task exceptions. A raised task exception is indicated in pendtex from the time the task exception is raised until its task exception handler is called.

texmask indicates allowed task exceptions.

Both pendtex and texmask are bit arrays of the form 1 << task exception code.

The invoking task can be specified by setting tskid = TSK_SELF = 0. Note, however, that when tskid = TSK_SELF = 0 is specified in a system call issued from a task-independent portion, error code E_ID is returned.