PXROS-HR trap handling

The PXROS-HR kernel, being an interrupt lock-free kernel, does not provide Enable/Disable interrupts API. Moreover, it re-enables the core interrupts in its exception entry (trap prologue) that might lead to unexpected side-effects when Enable/Disable interrupts are implemented on the application level.

The PXROS-HR trap handling needs at least 3 free CSAs for:

  1. Any trap (except FCU) stores an upper context (done by the TriCore)

  2. The trap prologue saves the lower context

  3. The call of the installed user-defined trap handler saves another upper context

The PXROS-HR default trap handler is an internal function that is called in all trap class handlers except SYSCALLs. The default trap handling routine — PxTrapAbort() — is called when no user-trap handler is installed.

PXROS-HR trap vector table

PXROS-HR provides a PXROS-HR trap vector table with 8 trap entries, one for each class. The table is initialized with the default trap handling routine and contains user-defined trap handlers once they are installed. Registering two different user trap handlers for one class on one core is impossible. Items in the trap handler table are overridable; only the last handler set for a specific class is used. However, it is possible to map one handler to several classes and decide in runtime which class was called by trap number, which is passed as an argument to a user trap handler.

Users can define and register up to 7 user trap handlers because the kernel reserves number 6 for SYSCALLs.

The following table shows for which trap classes the default PXROS-HR trap handling can be replaced with the user-defined trap handlers.

Tab. 1. Replaceability by a user trap handler
Trap class number Trap class name Replacable by a user trap handler

0

MMU Traps

Yes

1

Internal Protection Traps

Partly

2

Instruction Errors

Yes

3

Context Management

Yes*

4

System Bus and Peripheral Errors

Yes

5

Assertion Traps

Yes

6

System Call

No

7

Non-Maskable Interrupt

Yes

* For Free Context List Underflow (FCU) trap, the user-defined trap handler is not called

Trap execution sequence

There are several steps from the trap occurrence to the user-defined trap handler execution. The following PXROS-HR components are involved in PXROS-HR trap handling, ordered as they are executed in sequence (details are provided below):

  • Trap prologue — 32 bytes of code per entry (class) ending with a jump to a class handler

  • Class handler — 8 trap classes, one class handler per class

  • Special internal PXROS-HR handlers — memory access resolution for MPU traps

  • PXROS-HR default trap handler — preparation of the context for a user-defined trap handler

  • User-defined trap handler — implemented by the user

  • PXROS-HR default trap handling routine — PxTrapAbort()

The trap prologue saves the lower context, reads DSTR and DEADD registers, clears DSTR, enables interrupts and jumps to a PXROS-HR class handler. Only entry number 6, reserved by the kernel for SYSCALLs, has a different prologue.

There are some differences in the PXROS-HR class handler implementation for some specific classes — more detailed at Special classes. Each PXROS-HR class handler (except SYSCALLs) contains a call to the PXROS-HR default trap handler.

If the trap occurs when ICR.CCPN is 0, the PXROS-HR default trap handler sets ICR.CCPN to 1. By setting ICR.CCPN to 1, the PXROS kernel mode is entered, scheduling is disabled and all trap handlers are executed in handler mode. At the end of executing the PXROS-HR default trap handler, it is decided whether to continue with a user-defined trap handler if it was previously registered or the default trap handling routine.

  • A user trap handler was registered

    If the installer task is available, it is switched to its context. Otherwise, the trap handler is executed in kernel context. Then, a registered user trap handler is called. Trapped context is restored if the installer task exists. If the user trap handler successfully handled a trap, OS executes a return from the trap and leaves the supervisor mode. If the user trap handler cannot solve the trap (returned false), PxTrapAbort() is the default trap handling routine.

  • A user trap handler was not registered

    PxTrapAbort() is called as the trap handling routine.

A simplified image showing the execution sequence is available below.

class1 execution sequence
Fig. 1. Trap execution sequence — example for class 1 (Internal Protection Trap)

Returning from trap

For a synchronous trap, the return address is the PC of the instruction that caused the trap. On a SYS trap triggered by the SYSCALL instruction, the return address points to the instruction immediately following SYSCALL. For an asynchronous trap, the return address is that of the instruction that would have been executed next if the asynchronous trap had not been taken. The return address for an interrupt follows the same rule.