System Service Support Method System Call and Interrupts Similar Mechanisms Different Policy Goals

EECS 678 Dr. Douglas Niehaus

Dual-Mode system execution is key System and User mode System mode provides access not available in user mode to: OS address space: code and data Privileged machine instructions: Copy across User-OS AS boundary Return-from-Interrupt (reti) User and Kernel stacks accessed automatically depending on the CPU mode

EECS 678

1

Dr. Douglas Niehaus © 2007

EECS 678

2

Dr. Douglas Niehaus © 2007

General System Call Outline
User Program begins by making subroutine call to userlibrary system call wrapper Wrapper routine marshals necessary information and makes the transition to system mode System call arguments passed on stack Can also be passed in CPU registers TRAP instruction in this example Trap frame on Kernel stack preserves user computation context which is later restored General System Call handling determines precise system call required and establishes access to data Specific OS routine called as normal subroutine
EECS 678 3 Dr. Douglas Niehaus © 2007

General System Call Outline
System call OS subroutine must be careful about using user-address-space pointers and use special routine for reading/writing data across OS-User address space boundary Return from specific OS subroutine is normal but returns to OS generic routine which uses special return from trap (reti) instruction to restore context Back in user library syscall wrapper, some massaging of data may happen Normal return from subroutine semantics returns to calling context in user program This is summarized in 2-page PDF handout
EECS 678 4 Dr. Douglas Niehaus © 2007

System Call Sequence of Events Overview

System Call Sequence of Events Overview

EECS 678

5

Dr. Douglas Niehaus © 2007

EECS 678

6

Dr. Douglas Niehaus © 2007

Transition from User to System Mode

Transition from User to System Mode
Program begins (1) as it is executing normally and calls a system call interface routine (SCIR) Normal library subroutine call of a routine with the special purpose of calling a system service Return address and subroutine arguments (read frame) are pushed onto the user stack SCIR does service-specific processing (2) In this case pushes arguments on stack in reverse order for convenience and system call number of requested service

EECS 678

7

Dr. Douglas Niehaus © 2007

EECS 678

8

Dr. Douglas Niehaus © 2007

Transition from User to System Mode
User stack state is now complete since it holds all information required by the OS routine TRAP instruction (3) typically takes a numeric argument which uniquely identifies the corresponding entry in the Interrupt Vector Table (IVT) IVT supports general interrupt handling semantics Each entry corresponds to unique interrupt source Many are devices Traps are “software interrupts” using a range of IVT entries indexed by TRAP argument
EECS 678 9 Dr. Douglas Niehaus © 2007

Transition from User to System Mode
TRAP instruction semantics determined by CPU Instruction Set Architecture (ISA) HW implementation Push Trap Frame (TF) on Kernel stack Return address in User Space (PC register) Processor Status Word: User mode and other state information of CPU at time of TRAP or interrupt, used for later restoration of user mode IVT entry contains new PSW (OS mode) and new PC entry (handler entry point) Same semantics used for all interrupt handlers CPU in OS mode and at start of specified handler
EECS 678 10 Dr. Douglas Niehaus © 2007

Transition from User to System Mode

Execution of System Call in OS

EECS 678

11

Dr. Douglas Niehaus © 2007

EECS 678

12

Dr. Douglas Niehaus © 2007

Execution of System Call in OS
Execution begins at the start of the generic system call handler which: Establishes system call context on the KERNEL mode stack Calls the appropriate system call routine as a normal subroutine call in OS mode OS system call subroutine returns using normal conventions to this routine as well Generic routine must use special instructions but it permits all OS syscall routines to be compiled normally, using standard compiler conventions
EECS 678 13 Dr. Douglas Niehaus © 2007

Execution of System Call in OS
Generic routine begins by determining which system call is being requested (4) Gets the system call number Consults the corresponding entry in System Call Table which gives number of arguments and OS entry point Generic routine reads arguments on the USER stack, pushing them onto the KERNEL stack Note: Argument arrangement on KERNEL stack match that created by normal subroutine call
EECS 678 14 Dr. Douglas Niehaus © 2007

Execution of System Call in OS
Having established proper context on the KERNEL stack, the generic OS syscall handler calls requested handler using the function pointer in the system call table entry specified by the system call number Control is transferred to OS system call (sys_read) as a normal subroutine call (5) OS handler executes normally, but must be careful to handle USER AS pointers specially Sys_read checks for available data and if present, copies required data in USER AS buffer (6) Special routine used to copy across AS boundary
EECS 678 15 Dr. Douglas Niehaus © 2007

Execution of System Call in OS
When the OS handler is finished it specifies a return value using normal subroutine semantics (7) Here we assume that CPU register 0 (R0) contains the return value by convention with the compiler Note that as presented this requires the same compiler convention to be used for USER and OS code. It need not be exactly the same compiler. The actions of the system call are complete but restoration of User mode context remains

EECS 678

16

Dr. Douglas Niehaus © 2007

Execution of System Call in OS

System Call Return

EECS 678

17

Dr. Douglas Niehaus © 2007

EECS 678

18

Dr. Douglas Niehaus © 2007

System Call Return
Normal subroutine from the OS system call handler (sys_read) pops the handler call frame from the KERNEL stack under normal compiler conventions Generic system call handler uses the special returnfrom-interrupt instruction (reti) to restore the interrupted context in USER mode (8) Uses the information stored on the kernel stack in the Trap Frame. This includes the PSW and PC. Control returned to user library syscall wrapper at instruction following the TRAP
EECS 678 19 Dr. Douglas Niehaus © 2007

System Call Return
System call wrapper may do a lot of processing after return from system mode, or it may do none This example does none Normal subroutine return instruction (9) Uses normal conventions about where the return address is on the USER stack and that R0 contains the return value of the system call and subroutine R0 remains undisturbed from when sys_read set it (7) until it is used to set RV in the user code (9)
EECS 678 20 Dr. Douglas Niehaus © 2007

System Call Return

Interrupt Handling – User Mode

EECS 678

21

Dr. Douglas Niehaus © 2007

EECS 678

22

Dr. Douglas Niehaus © 2007

Interrupt Handling – User Mode
Interrupt handling is has many similarities to system call handling, with some differences, of course Consider a generic process executing a generic program, consisting of a sequence of generic instructions Interrupt delivery is generally constrained by the CPU ISA to occur between CPU instructions (1) IRQ delivery causes a Trap Frame (TF) to be pushed on the kernel stack(1), and the PSW and PC to be set from the IVT entry for the IRQ(2)
EECS 678 23 Dr. Douglas Niehaus © 2007

Interrupt Handling – User Mode
Each IVT entry may transfer control to the IRQ specific handler, or it may go indirectly through a generic IRQ handler (Linux) Generic method illustrated here (2) Generic IRQ handler can also be split in the ASM and C versions for ease of development ASM portion gathers information and machine state, and then call C portion (3) Generic C routine (do_IRQ in Linux) can do generic IRQ handling activities and check and then calls IRQ specific routine (4)
EECS 678 24 Dr. Douglas Niehaus © 2007

Interrupt Handling – User Mode
When the IRQ specific routine it completes is finished it returns as any normal C subroutine does in the kernel The generic C routine may do some generic cleanup, but when finished it returns as a normal C subroutine (5) Generic ASM routine uses special return-frominterrupt (reti) instruction to restore USER mode state from the TF (6) Execution resumes at the point int he user program where the interrupt occurred
EECS 678 25 Dr. Douglas Niehaus © 2007

Interrupt Handling – User Mode

EECS 678

26

Dr. Douglas Niehaus © 2007

Interrupt Handling – Kernel Mode

Interrupt Handling – Kernel Mode
CPU already in KERNEL mode, executing generic section of OS code Very similar to User mode interrupt handling Exception is that the KERNEL stack does not start empty as with User mode interrupts and system calls Questions: What implications does this have for exceeding KERNEL stack size? What are the implications of nested interrupts?

EECS 678

27

Dr. Douglas Niehaus © 2007

EECS 678

28

Dr. Douglas Niehaus © 2007

Conclusions
Basic Interrupt (TRAP) handling HW semantics are a mechanism used for two significantly different policy goals Interrupt handler execution concurrent with other CPU based computations System services provided to programs through System Calls and Dual Mode execution Compiler-OS are collaborators in implementing the programming model expected by programs Role of compiler conventions for arguments
EECS 678 29 Dr. Douglas Niehaus © 2007