10.5 User Program Traps

The “system call” mechanism which enables user mode programs to call on the operating system for assistance, involves the execution by the user mode program of one of 256 versions of the “trap” instruction. (The “version” is the value of the low order byte of the instruction word.)

0518:

Execution of the trap instruction in a user mode program causes a trap to occur to vector location 34 which causes the PC to be loaded with the value of the label “trap” (lines 0512, 0755). A new PS is set which indicates

present mode = kernel mode

previous mode = user mode

priority = 7

0756:

The next instruction executed is the first instruction of “trap”. This saves the processor status word in the stack two words beyond the current “top of stack”.

It is important to save the PS as soon as possible, before it can be changed, since it contains information defining the type of trap that occurred. The somewhat unconventional destination of the “move” is to provide compatibility with the handling of interrupts, so that the same code can be used further on;

0757:

“nofault” will be zero so the branch is not taken;

0759:

The memory management status registers are stored just in case they will be needed, and the memory management unit is reinitialised;

0762:

A subroutine entry is made to “call” using r0. (This neatly stores the old value of r0 in the stack, but not a return address. The new value is the address of the address of the routine to be entered next (in this case the “trap” routine in the file “trap.c” (2693));

0772:

The stack pointer is adjusted to point to the location which already contains the copy of PS;

0773:

The CPU priority is set to zero;

From here the same path as for an interrupt is followed.