Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS201 - Lecture 17 Exceptions

Similar presentations


Presentation on theme: "CS201 - Lecture 17 Exceptions"— Presentation transcript:

1 CS201 - Lecture 17 Exceptions
Raoul rivas Portland state university

2 Announcements CS201: Computer Science Programming
Raoul Rivas (Based on slides from Bryant and O’Hallaron)

3 Control Flow Processors do only one thing: Physical control flow
From startup to shutdown, a CPU simply reads and executes (interprets) a sequence of instructions, one at a time This sequence is the CPU’s control flow (or flow of control) Physical control flow <startup> inst1 inst2 inst3 instn <shutdown> Time CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

4 Altering Control Flow Up to now: two mechanisms for changing control flow: Jumps and branches Call and return React to changes in program state Insufficient for a useful system: Difficult to react to changes in system state Data arrives from a disk or a network adapter Instruction divides by zero User hits Ctrl-C at the keyboard System timer expires System needs mechanisms for “exceptional control flow” CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

5 Exceptional Control Flow
Exists at all levels of a computer system Low level mechanisms 1. Exceptions Change in control flow in response to a system event (i.e., change in system state) Implemented using combination of hardware and OS software Higher level mechanisms 2. Process context switch Implemented by OS software and hardware timer 3. Signals Implemented by OS software CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

6 Exceptions An exception is a transfer of control to the OS kernel in response to some event (i.e., change in processor state) Kernel is the memory-resident part of the OS Examples of events: Divide by 0, arithmetic overflow, page fault, I/O request completes, typing Ctrl-C User code Kernel code Exception Event I_current I_next Exception processing by exception handler Return to I_current Return to I_next Abort CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

7 Interrupt Descriptor Tables (Exception Tables)
numbers Each type of event has a unique exception number k k = index into exception table (a.k.a. interrupt vector) Handler k is called each time exception k occurs Code for exception handler 0 Exception Table Code for exception handler 1 1 Code for exception handler 2 2 ... n-1 ... Code for exception handler n-1 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

8 Interrupts (Asynchronous Exceptions)
Caused by events external to the processor Indicated by setting the processor’s interrupt pin Handler returns to “next” instruction Examples: Timer interrupt Every few ms, an external timer chip triggers an interrupt Used by the kernel to take back control from user programs I/O interrupt from external device Hitting Ctrl-C at the keyboard Arrival of a packet from a network Arrival of data from a disk CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

9 Synchronous Exceptions
Caused by events that occur as a result of executing an instruction: Traps Intentional Examples: system calls, breakpoint traps, special instructions Returns control to “next” instruction Faults Unintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable), floating point exceptions Either re-executes faulting (“current”) instruction or aborts Aborts Unintentional and unrecoverable Examples: illegal instruction, parity error, machine check Aborts current program CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

10 User and Kernel Space Operating System Hardware User Space User
To ensure integrity of the Operating System The execution and memory are divided in two spaces Kernel Space (Operating System) User Space (Programs) User Space User Program User Program User Program Operating System Kernel Space Hardware CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

11 System Calls TRAP Kernel Space I D T User Space 0xff023
Linux Kernel Code Sys_write(handle, size); Functions that call the Operating System are isolated to ensure security and system protection Implemented as Traps (Exceptions) User specifies the System Call number and parameters Generates a Software Interrupt and Traps into the Operating System OS executes operation on behalf of the user OS returns control to user I D T Return TRAP Int 0x80 C library syscall(WRITE, handle, size); UserCode.c write(handle, size); User Space CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

12 System Call Table Number Name Description read Read file 1 write
read Read file 1 write Write file 2 open Open file 3 close Close file 4 stat Get info about file 57 fork Create process 59 execve Execute a program 60 _exit Terminate process 62 kill Send signal to process CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

13 System Call Example User code Kernel code
User calls: open(filename, options) Calls __open function, which invokes system call instruction syscall e5d70 <__open>: ... e5d79: b mov $0x2,%eax # open is syscall #2 e5d7e: 0f syscall # Return value in %rax e5d80: d 01 f0 ff ff cmp $0xfffffffffffff001,%rax e5dfa: c retq %rax contains syscall number Other arguments in %rdi, %rsi, %rdx, %r10, %r8, %r9 Return value in %rax Negative value is an error corresponding to negative errno User code Kernel code Exception syscall cmp Open file Returns CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

14 Fault Example: Invalid Memory Reference
int a[1000]; main () { a[5000] = 13; } 80483b7: c e d movl $0xd,0x804e360 User code Kernel code Exception: page fault movl Detect invalid address Signal process Sends SIGSEGV signal to user process User process exits with “segmentation fault” CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

15 Process Definition: A process is an instance of a running program.
One of the most profound ideas in computer science Process provides each program with two key abstractions: Logical control flow Each program seems to have exclusive use of the CPU Provided by kernel mechanism called context switching Private address space Each program seems to have exclusive use of main memory. Provided by kernel mechanism called virtual memory Memory Stack Heap Code Data CPU Registers CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

16 Private Address Space Ideal case!
So far we have been concerned with running one program at a time Executable Objects are non relocatable Compiler generates code with relative offsets to a base address What happens when two programs are loaded into memory? Physical Memory Program 2 Operating System 0x8000 0x9000 Program 1 0x8000 0x7000 Program 1 Ideal case! 0x7000 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

17 Can we assign each program its own address space?
Private Address Space Most likely we are not going to be so lucky Physical Memory Program 2 Operating System 0x7000 0x9000 Program 1 0x8000 0x7000 Program 1 0x7000 Can we assign each program its own address space? CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

18 Virtual Memory Each process has its own virtual address space ... ...
Each process can view memory as a simple linear array Hardware converts Virtual Addresses to Physical Addresses Address translation Virtual Address Space for Process 1: Physical Address Space (DRAM) VP 1 VP 2 PP 2 ... N-1 (e.g., read-only library code) PP 6 Virtual Address Space for Process 2: PP 8 VP 1 VP 2 ... ... CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

19 Virtual Memory: Page Tables
A page table is an array of page table entries (PTEs) that maps virtual pages to physical pages. Physical memory (DRAM) Physical page number or disk address Valid VP 1 PP 0 VP 2 PTE 0 null VP 7 1 VP 4 PP 3 1 1 Swap Page File (disk) null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

20 Linking and Loading Linking Loading
Memory invisible to user code Linking Each program has similar virtual address space Code, stack, and shared libraries always start at the same address Loading execve allocates virtual pages for .text and .data sections & creates PTEs marked as invalid The .text and .data sections are copied, page by page from the disk, on demand by the virtual memory system Operating System User stack (created at runtime) %rsp (stack pointer) Memory-mapped region for shared libraries brk Run-time heap (created by malloc) Read/write data segment .data Loaded from the executable file Read-only code segment .text Unused 0x400000 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

21 Memory Protection Virtual Memory creates a separate virtual address space for each process Processes cannot access each other’s memory space unless explicitly allowed They do not know which Physical Address the other process is located! Physical Memory Program 2 Operating System 0x7000 0x4000 Program 2 Virtual Addresses 0x3000 Program 1 Program 1 0x2000 0x7000 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

22 Multiprocessing … Memory Memory Memory CPU CPU CPU
Stack Stack Stack Heap Heap Heap Data Data Data Code Code Code CPU CPU CPU Registers Registers Registers Computer runs many processes simultaneously Applications for one or more users Web browsers, clients, editors, … Background tasks Monitoring network & I/O devices CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

23 Multiprocessing Running program “top” on Mac
System has 123 processes, 5 of which are active Identified by Process ID (PID) CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

24 Multiprocessing … Memory CPU
Stack Stack Stack Heap Heap Heap Data Data Data Code Code Code Saved registers Saved registers Saved registers CPU Registers Single processor executes multiple processes concurrently Process executions interleaved (multitasking) Address spaces managed by virtual memory system (later in course) Register values for nonexecuting processes saved in memory CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

25 Multiprocessing … Memory CPU Save current registers in memory Stack
Heap Heap Heap Data Data Data Code Code Code Saved registers Saved registers Saved registers CPU Registers Save current registers in memory CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

26 Multiprocessing … Memory CPU
Stack Stack Stack Heap Heap Heap Data Data Data Code Code Code Saved registers Saved registers Saved registers CPU Registers Schedule next process for execution (Context Switch) Load registers of next process CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

27 Concurrent Execution Each process is a logical control flow.
Two processes run concurrently (are concurrent) if their flows overlap in time Otherwise, they are sequential Examples (running on single core): Concurrent: A & B, A & C Sequential: B & C Process A Process B Process C Time CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

28 Concurrent Execution: User View
Control flows for concurrent processes are physically disjoint in time However, we can think of concurrent processes as running in parallel with each other Process A Process B Process C Time CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

29 SaveRegs[A], LoadRegs[B] SaveRegs[B], LoadRegs[A]
Context Switching Processes are managed by a shared chunk of memory-resident OS code called the kernel Important: the kernel is not a separate process, but rather runs as part of some existing process. Control flow passes from one process to another via a context switch Process A Process B user code SaveRegs[A], LoadRegs[B] kernel code context switch Time user code kernel code context switch SaveRegs[B], LoadRegs[A] user code CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

30 Summary An exception is a transfer of control to the OS kernel in response to some event User and Kernel Space provide separate memory address spaces for Operating System and User Programs System Calls are implemented as traps to ensure inetrgity of the Operating System A process is an instance of a running program. Private address space Logical control flow Control flow passes from one process to another via a context switch CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)


Download ppt "CS201 - Lecture 17 Exceptions"

Similar presentations


Ads by Google