10.6 The Kernel Stack

The state of the kernel stack at the time that the “trap” procedure (“C” version) or one of the specialised interrupt handling routines is entered, is shown in Figure 10.1.

     

....

Previous top of stack

(rps

2)

7

ps

old PS

(r7

1)

6

pc

old PC (r7)

(r0

0)

5 –$>$

r0

old r0

   

4

nps

new PS after trap

(r1

–2)

3

r1

old r1

(r6

–3)

2

sp

old SP for previous mode

   

1

dev

masked new PS

   

0 –$>$

tpc

return address in “call”

(r5

–6

–1

(r5)

old r5

(r4

–7

–2

(r4)

old r4

(r3

–8

–3

(r3)

old r3

(r2

–9

–4

(r2)

old r2

Figure 10.1

Columns (2) and (3) give the positions of stack words relative to the positions in the stack of the words labelled “r0” and “tpc” respectively.

Columns (1) and (2) define (or explain) the contents of the file “reg.h” (Sheet 26).

“dev”, “sp”, “r1”, “nps” “r0”, “pc” and “ps” in that order are the names of the parameters used in the declaration of the procedures “trap” (2693) and “clock” (3725).

Note that just before entry to “trap” (“C” version) or the other interrupt handling routines, the values for the registers r2, r3, r4 and r5 have not yet been saved in the stack. This is performed by a call on “csv” (lg20) which is automatically included by the “C” compiler at the beginning of every compiled procedure. The form of the call on “csv” is equivalent to the assembler instruction

   jsr r5,csv

This saves the current value of r5 on the stack and replaces it by the address of the next instruction in the “C” procedure.

1421:

This value of r5 is copied into r0;

1422:

the current value of the stack pointer is copied into r5.

Note that at this point, r5 points to a stack location containing the previous value of r5 i.e. it points to the beginning of a chain of pointers, one per procedure, which “thread” the stack. When a “C” procedure exits, it actually returns to “cret” (1430) where the value of r5 is used to restore the stack and r2, r3 and r4 to their earlier condition (i.e. as they were immediately prior to entering the procedure). For this reason r5 is often called the environment pointer.