Today Exceptional Control Flow: Exceptional Control Flow Exceptions Exceptions and Processes Processes Process Control CSci 2021: Machine Architecture and Organization December 3rd, 2018 Your instructor: Stephen McCamant Based on slides originally by: Randy Bryant, Dave O’Hallaron 1 2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Control Flow Altering the Control Flow Processors do only one thing: Up to now: two mechanisms for changing control flow: From startup to shutdown, a CPU simply reads and executes Jumps and branches (interprets) a sequence of instructions, one at a time Call and return This sequence is the CPU’s control flow (or flow of control ) React to changes in program state Physical control flow Insufficient for a useful system: <startup> Difficult to react to changes in system state inst 1 Data arrives from a disk or a network adapter inst 2 Instruction divides by zero Time inst 3 User hits Ctrl-C at the keyboard … System timer expires inst n <shutdown> System needs mechanisms for “exceptional control flow” 3 4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Exceptional Control Flow Today Exists at all levels of a computer system Exceptional Control Flow Low level mechanisms Exceptions 1. Exceptions Processes Change in control flow in response to a system event Process Control (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 4. Nonlocal jumps : setjmp() and longjmp() Implemented by C runtime library 5 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 1
Exception Tables Exceptions An exception is a transfer of control to the OS kernel in response Exception to some event (i.e., change in processor state) numbers Kernel is the memory-resident part of the OS Each type of event has a Code for Examples of events: Divide by 0, arithmetic overflow, page fault, I/O exception handler 0 unique exception number k request completes, typing Ctrl-C Exception Code for Table exception handler 1 k = index into exception table User code Kernel code 0 (a.k.a. interrupt vector) 1 Code for 2 exception handler 2 ... Exception Event I_current ... Handler k is called each time n-1 I_next Exception processing exception k occurs by exception handler Code for • Return to I_current exception handler n-1 • Return to I_next • Abort 7 8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Asynchronous Exceptions (Interrupts) Synchronous Exceptions Caused by events that occur as a result of executing an Caused by events external to the processor instruction: Indicated by setting the processor’s interrupt pin Traps Handler returns to “next” instruction Intentional Examples: system calls , breakpoint traps, special instructions Returns control to “next” instruction Examples: Faults Timer interrupt Unintentional but possibly recoverable Every few ms, an external timer chip triggers an interrupt Examples: page faults (recoverable), protection faults Used by the kernel to take back control from user programs (unrecoverable), floating point exceptions I/O interrupt from external device Either re- executes faulting (“current”) instruction or aborts Aborts Hitting Ctrl-C at the keyboard Unintentional and unrecoverable Arrival of a packet from a network Examples: illegal instruction, parity error, machine check Arrival of data from a disk Aborts current program 9 10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition System Call Example: Opening File System Calls User calls: open(filename, options) Calls __ open function, which invokes system call instruction syscall Each x86-64 system call has a unique ID number Examples: 00000000000e5d70 <__open>: ... Number Name Description e5d79: b8 02 00 00 00 mov $0x2,%eax # open is syscall #2 e5d7e: 0f 05 syscall # Return value in %rax read 0 Read file e5d80: 48 3d 01 f0 ff ff cmp $0xfffffffffffff001,%rax write 1 Write file ... e5dfa: c3 retq open 2 Open file close 3 Close file stat 4 Get info about file User code Kernel code %rax contains syscall number fork 57 Create process Other arguments in %rdi , execve %rsi , %rdx , %r10 , %r8 , %r9 59 Execute a program Exception syscall _exit Return value in %rax 60 Terminate process cmp Open file kill 62 Send signal to process Negative value is an error Returns corresponding to negative errno 11 12 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2
Fault Example: Invalid Memory Reference Fault Example: Page Fault int a[1000]; int a[1000]; main () User writes to memory location main () { That portion (page) of user’s memory { a[500] = 13; a[5000] = 13; is currently on disk } } 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360 User code Kernel code User code Kernel code Exception: page fault movl Exception: page fault Detect invalid address movl Copy page from Signal process disk to memory Return and reexecute movl Sends SIGSEGV signal to user process User process exits with “segmentation fault” 13 14 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Processes Today Definition: A process is an instance of a running Exceptional Control Flow program. Exceptions One of the most profound ideas in computer science Not the same as “program” or “processor” Processes Process Control Process provides each program with two key Memory abstractions: Stack Logical control flow Heap Each program seems to have exclusive use of the CPU Data Provided by kernel mechanism called context switching Code Private address space CPU Each program seems to have exclusive use of main memory. Registers Provided by kernel mechanism called virtual memory 15 16 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Multiprocessing: The Illusion Multiprocessing Example Memory Memory Memory 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, email clients, editors, … Running program “top” on Mac Background tasks System has 123 processes, 5 of which are active Monitoring network & I/O devices Identified by Process ID (PID) 17 18 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3
Recommend
More recommend