Title: Exceptional Control Flow I
1Exceptional Control Flow I
2Outline
- Operating Systems
- Exceptions
- Suggested reading 1.7, 8.1, 8.2
3Operating Systems
- Contemporary Applications never access hardware
resources directly - They rely on the services provided by the
operating system
4Operating System
- Is a program that manages the computer hardware
- Also provides a basis for application programs
- And acts as an intermediary between a user of a
computer and the computer hardware
5Two primary purposes
- Protect hardware from misuse by applications
- Provide applications with simple and uniform
mechanisms for manipulating hardware devices
6Operating Systems
- Fundamental abstractions
- Process
- Virtual memory
- files
7(No Transcript)
8Process phenomenon
- When a program runs on a modern system,
- the operating system provides the illusion
- that the program is the only one running on the
system
9Process phenomenon
- The program appears to have exclusive use of all
the processor, main memory, and I/O devices - The program appears to execute the instructions
in the program, one after the other, without
interruption - The code and data of the program appear to the
only objects in the systems memory
10Processes
- Def A process is an instance of a running
program. - Process is one of the most profound ideas in
computer science. - Each program in the system runs in the context of
some process
11When a New Process is Created
- Each time a user runs a program
- by typing the name of an executable object file
to the shell, - the shell creates a new process
- and then runs the executable object file
- in the context of this new process
12When a New Process is Created
- Application programs can also
- create new processes
- and run
- either their own
- or other application
- in the context of new process
13Processes
- Process provides each program with two key
abstractions - Logical control flow
- gives each program the illusion that it has
exclusive use of the CPU. - Private address space
- gives each program the illusion that has
exclusive use of main memory
14Processes
- How is this illusion maintained?
- Process executions interleaved (multitasking)
- Address spaces managed by virtual memory system
15Process
- Concurrency
- Multiple processes can run concurrently
- The instructions of one process are interleaved
with the instructions of another process
16Logical control flows
- Each process has its own logical control flow
- does not affect the states of any other processes
- Preempted
- Time slice
17Concurrent processes
- Two processes run concurrently
- are concurrent, if their flows overlap in time.
- Otherwise, they are sequential.
- Examples
- Concurrent A B, AC
- Sequential B C
18User view of concurrent processes
- Control flows for concurrent processes are
physically disjoint in time. - However, we can think of concurrent processes are
running in parallel with each other. - Multitasking or time slicing
19Contexts of Processes
- The context consists of the state that the
program needs to run correctly - This state includes
- the programs code and data stored in memory
- its stack
- the contents of its general-purpose registers
- its program counter
- environment variables
- and the set of open file descriptors
20(No Transcript)
21Private address spaces
- Each process has its own private address space
22Virtual Memory
- An abstraction
- Each process appears to have exclusive use of the
main memory - Virtual address space
- Program code and data (global variables)
- Heap
- Shared libraries (standard and math libraries)
- Stack (function calls)
- Kernel (operating system)
- Hardware supports to translate the virtual address
23(No Transcript)
24Files
- A sequence of bytes
- Each I/O device is modeled as a file
- Unix I/O
- using a small set of system calls reading and
writing files - All input and output in the system is performed
by Unix I/O
25Control flow
- From startup to shutdown, a CPU simply reads and
executes (interprets) a sequence of instructions,
one at a time. - This sequence is the systems physical control
flow (or flow of control).
26Altering the Control Flow
- Weve discussed two mechanisms for changing the
control flow - Jumps and branches
- Call and return using the stack discipline.
- Both react to changes in program state.
27Altering the Control Flow
- Insufficient for a useful system
- difficult for the CPU to react to changes in
system state - data arrives from a disk or a network adapter.
- instruction divides by zero
- user hits ctl-c at the keyboard
- system timer expires
- System needs mechanisms for exceptional control
flow
28Exceptions (review)
- Conditions under which pipeline cannot continue
normal operation - Causes
- Halt instruction (Current)
- Bad address for instruction or data (Previous)
- Invalid instruction (Previous)
- Pipeline control error (Previous)
29Exceptions (review)
- Desired Action
- Complete some instructions
- Either current or previous (depends on exception
type) - Discard others
- Call exception handler
- Like an unexpected procedure call
30Exceptional control flow
- Mechanisms for exceptional control flow exists at
all levels of a computer system. - Low level mechanism
- exceptions
- change in control flow in response to a system
event (i.e., change in system state) - Implemented as a combination of both hardware and
OS software
31System context for exceptions
Keyboard
Mouse
Printer
Modem
Processor
Interrupt controller
Serial port controller
Parallel port controller
Keyboard controller
Local/IO Bus
Network adapter
Video adapter
Memory
IDE disk controller
SCSI controller
SCSI bus
disk
Network
Display
disk
CDROM
32Exceptional control flow
- Higher level mechanisms
- process context switch (OS level)
- signals
- nonlocal jumps (setjmp/longjmp)
- Implemented by either
- OS software (context switch and signals).
- C language runtime library nonlocal jumps.
33Exceptions
- An exception is a transfer of control to the OS
in response to some event (i.e., change in
processor state)
34Interrupt vectors
- 1. Each type of event has a unique exception
number k - 2. Jump table (interrupt vector) entry k points
to a function (exception handler). - 3. Handler k is called each time exception k
occurs.
35Asynchronous exceptions (interrupts)
- Caused by events (changes in state) external to
the processor - Indicated by setting the processors interrupt
pin - handler returns to next instruction.
36Asynchronous exceptions (interrupts)
- Examples
- I/O interrupts
- hitting ctl-c at the keyboard
- arrival of a packet from a network
- arrival of a data sector from a disk
- Hard reset interrupt
- hitting the reset button
- Soft reset interrupt
- hitting ctl-alt-delete on a PC
37Synchronous exceptions
- Caused by events (changes in state) that occur as
a result of executing an instruction - Traps
- intentional
- returns control to next instruction
- Examples system calls, breakpoint traps
38Synchronous exceptions
- Caused by events (changes in state) that occur as
a result of executing an instruction - Faults
- unintentional but possibly recoverable
- either re-executes faulting (current)
instruction or aborts. - Examples page faults (recoverable), protection
faults (unrecoverable).
39Synchronous exceptions
- Caused by events (changes in state) that occur as
a result of executing an instruction - Aborts
- unintentional and unrecoverable
- aborts current program
- Examples parity error, machine check.
40Trap Example
- Opening a File
- User calls open(filename, options)
- Function open executes system call instruction int
0804d070 lt__libc_opengt . . . 804d082 cd 80
int 0x80 804d084 5b
pop ebx . . .
41Trap Example
- Opening a File
- OS must find or create file, get it ready for
reading or writing - Returns integer file descriptor
42Fault Example 1
- Memory Reference
- User writes to memory location
- That portion (page) of users memory is currently
on disk
int a1000 main () a500 13
80483b7 c7 05 10 9d 04 08 0d movl
0xd,0x8049d10
43Fault Example 1
- Memory Reference
- Page handler must load page into physical memory
- Returns to faulting instruction
- Successful on second try
44Fault Example 1
int a1000 main () a500 13
80483b7 c7 05 10 9d 04 08 0d movl
0xd,0x8049d10
45Fault Example 2
- Memory Reference
- User writes to memory location
- Address is not valid
int a1000 main () a5000 13
80483b7 c7 05 60 e3 04 08 0d movl
0xd,0x804e360
46Fault Example 2
- Memory Reference
- Page handler detects invalid address
- Sends SIGSEG signal to user process
- User process exits with segmentation fault
47Fault Example 2
int a1000 main () a5000 13
80483b7 c7 05 60 e3 04 08 0d movl
0xd,0x804e360