Вы находитесь на странице: 1из 8

System Service Support Method


Dual-Mode system execution is key
System Call and Interrupts

System and User mode

System mode provides access not available in user
Similar Mechanisms mode to:
Different Policy Goals 
OS address space: code and data

Privileged machine instructions:

Copy across User-OS AS boundary
EECS 678 
Return-from-Interrupt (reti)

Dr. Douglas Niehaus



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 General System Call Outline



User Program begins by making subroutine call to user- 
System call OS subroutine must be careful about using
library system call wrapper user-address-space pointers and use special routine for

Wrapper routine marshals necessary information and reading/writing data across OS-User address space
makes the transition to system mode boundary

System call arguments passed on stack 
Return from specific OS subroutine is normal but returns

Can also be passed in CPU registers to OS generic routine which uses special return from

TRAP instruction in this example trap (reti) instruction to restore context

Trap frame on Kernel stack preserves user 
Back in user library syscall wrapper, some massaging of
computation context which is later restored data may happen

General System Call handling determines precise 
Normal return from subroutine semantics returns to
system call required and establishes access to data calling context in user program

Specific OS routine called as normal subroutine 
This is summarized in 2-page PDF handout
EECS 678 3 Dr. Douglas Niehaus © 2007 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 Transition from User to System Mode

User stack state is now complete since it holds all 
TRAP instruction semantics determined by CPU
information required by the OS routine Instruction Set Architecture (ISA) HW implementation

TRAP instruction (3) typically takes a numeric

Push Trap Frame (TF) on Kernel stack
argument which uniquely identifies the corresponding

Return address in User Space (PC register)
entry in the Interrupt Vector Table (IVT)

Processor Status Word: User mode and other
state information of CPU at time of TRAP or

IVT supports general interrupt handling semantics
interrupt, used for later restoration of user mode

Each entry corresponds to unique interrupt source 
IVT entry contains new PSW (OS mode) and new

Many are devices PC entry (handler entry point)

Traps are “software interrupts” using a range of

Same semantics used for all interrupt handlers
IVT entries indexed by TRAP argument

CPU in OS mode and at start of specified handler
EECS 678 9 Dr. Douglas Niehaus © 2007 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 of System Call in OS

Execution begins at the start of the generic system 
Generic routine begins by determining which system
call handler which: call is being requested (4)

Establishes system call context on the KERNEL 
Gets the system call number
mode stack 
Consults the corresponding entry in System Call

Calls the appropriate system call routine as a
Table which gives number of arguments and OS
normal subroutine call in OS mode
entry point

OS system call subroutine returns using normal
conventions to this routine as well

Generic routine reads arguments on the USER

Generic routine must use special instructions but it stack, pushing them onto the KERNEL stack
permits all OS syscall routines to be compiled 
Note: Argument arrangement on KERNEL stack
normally, using standard compiler conventions match that created by normal subroutine call
EECS 678 13 Dr. Douglas Niehaus © 2007 EECS 678 14 Dr. Douglas Niehaus © 2007

Execution of System Call in OS Execution of System Call in OS



Having established proper context on the KERNEL 
When the OS handler is finished it specifies a return
stack, the generic OS syscall handler calls requested value using normal subroutine semantics (7)
handler using the function pointer in the system call 
Here we assume that CPU register 0 (R0) contains
table entry specified by the system call number the return value by convention with the compiler

Control is transferred to OS system call (sys_read) as 
Note that as presented this requires the same
a normal subroutine call (5)
compiler convention to be used for USER and OS

OS handler executes normally, but must be careful to
code. It need not be exactly the same compiler.
handle USER AS pointers specially

Sys_read checks for available data and if present,

The actions of the system call are complete but
copies required data in USER AS buffer (6) restoration of User mode context remains

Special routine used to copy across AS boundary
EECS 678 15 Dr. Douglas Niehaus © 2007 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 System Call Return



Normal subroutine from the OS system call handler 
System call wrapper may do a lot of processing after
(sys_read) pops the handler call frame from the return from system mode, or it may do none
KERNEL stack under normal compiler conventions 
This example does none

Generic system call handler uses the special return- 
Normal subroutine return instruction (9)
from-interrupt instruction (reti) to restore the 
Uses normal conventions about where the return
interrupted context in USER mode (8)
address is on the USER stack and that R0

Uses the information stored on the kernel stack in contains the return value of the system call and
the Trap Frame. This includes the PSW and PC. subroutine

Control returned to user library syscall wrapper at 
R0 remains undisturbed from when sys_read set
instruction following the TRAP it (7) until it is used to set RV in the user code (9)
EECS 678 19 Dr. Douglas Niehaus © 2007 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 – User Mode



Interrupt handling is has many similarities to system 
Each IVT entry may transfer control to the IRQ
call handling, with some differences, of course specific handler, or it may go indirectly through a

Consider a generic process executing a generic generic IRQ handler (Linux)
program, consisting of a sequence of generic

Generic method illustrated here (2)
instructions 
Generic IRQ handler can also be split in the ASM

Interrupt delivery is generally constrained by the and C versions for ease of development
CPU ISA to occur between CPU instructions (1)

ASM portion gathers information and machine
state, and then call C portion (3)

IRQ delivery causes a Trap Frame (TF) to be
pushed on the kernel stack(1), and the PSW and

Generic C routine (do_IRQ in Linux) can do generic
PC to be set from the IVT entry for the IRQ(2) IRQ handling activities and check and then calls IRQ
specific routine (4)
EECS 678 23 Dr. Douglas Niehaus © 2007 EECS 678 24 Dr. Douglas Niehaus © 2007
Interrupt Handling – User Mode 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-from-
interrupt (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 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

Вам также может понравиться