Exceptions, Interrupts & Traps

Slides:



Advertisements
Similar presentations
Computer-System Structures Er.Harsimran Singh
Advertisements

1 Exceptions, Interrupts & Traps Operating System Hebrew University Spring 2007.
Input and Output CS 215 Lecture #20.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
OS2-1 Chapter 2 Computer System Structures. OS2-2 Outlines Computer System Operation I/O Structure Storage Structure Storage Hierarchy Hardware Protection.
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
OS Spring’03 Introduction Operating Systems Spring 2003.
Abhinav Kamra Computer Science, Columbia University 2.1 Operating System Concepts Silberschatz, Galvin and Gagne  2002 Chapter 2: Computer-System Structures.
Computer System Structures memory memory controller disk controller disk controller printer controller printer controller tape-drive controller tape-drive.
A. Frank - P. Weisberg Operating Systems Functional View of Operating System.
OS Spring’04 Introduction Operating Systems Spring 2004.
INPUT/OUTPUT ORGANIZATION INTERRUPTS CS147 Summer 2001 Professor: Sin-Min Lee Presented by: Jing Chen.
1 Computer System Overview Chapter 1 Review of basic hardware concepts.
General System Architecture and I/O.  I/O devices and the CPU can execute concurrently.  Each device controller is in charge of a particular device.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Chapter 1. Introduction What is an Operating System? Mainframe Systems
Input and Output Computer Organization and Assembly Language: Module 9.
MICROPROCESSOR INPUT/OUTPUT
2.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts with Java Chapter 2: Computer-System Structures Computer System Operation I/O Structure.
CHAPTER 2: COMPUTER-SYSTEM STRUCTURES Computer system operation Computer system operation I/O structure I/O structure Storage structure Storage structure.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 2: Computer-System Structures Computer System Operation I/O Structure.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Chapter 2: Computer-System Structures
1 CSE Department MAITSandeep Tayal Computer-System Structures Computer System Operation I/O Structure Storage Structure Storage Hierarchy Hardware Protection.
2: Computer-System Structures
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
1 Chapter 2: Computer-System Structures  Computer System Operation  I/O Structure  Storage Structure  Storage Hierarchy  Hardware Protection  General.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 5 part 4 Exceptions.
The Functions of Operating Systems Interrupts. Learning Objectives Explain how interrupts are used to obtain processor time. Explain how processing of.
1 COMPUTER ARCHITECTURE (for Erasmus students) Assoc.Prof. Stasys Maciulevičius Computer Dept.
Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage Structure Storage Hierarchy Hardware Protection Network Structure.
מנהלות. שעות קבלה סיוון סבתו -) יעודכן בקרוב באתר ( רוס 60 עומר לב - רביעי 16:00-17:00 רוס 25 ) קומה 1-( –בתאום מראש במייל הקורס !
CSC 2405 Computer Systems II Exceptions Mini-Lecture Traps & Interrupts.
Exceptional Control Flow Topics Exceptions except1.ppt CS 105 “Tour of the Black Holes of Computing”
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 2 Computer-System Structures Slide 1 Chapter 2 Computer-System Structures.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 4 Computer Systems Review.
Silberschatz, Galvin and Gagne  Applied Operating System Concepts Chapter 2: Computer-System Structures Computer System Architecture and Operation.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
1 Lecture 1: Computer System Structures We go over the aspects of computer architecture relevant to OS design  overview  input and output (I/O) organization.
بسم الله الرحمن الرحيم MEMORY AND I/O.
Chapter 2: Computer-System Structures(Hardware) or Architecture or Organization Computer System Operation I/O Structure Storage Structure Storage Hierarchy.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Overview: Using Hardware.
Computer System Structures Interrupts
Chapter 2: Computer-System Structures(Hardware)
Chapter 2: Computer-System Structures
Exceptional Control Flow
Lesson Objectives Aims Key Words Interrupt, Buffer, Priority, Stack
Exceptional Control Flow
Exceptional Control Flow
Exceptional Control Flow
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Computer-System Architecture
Module 2: Computer-System Structures
Processor Fundamentals
Exceptions Control Flow
Architectural Support for OS
Module 2: Computer-System Structures
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Architectural Support for OS
Chapter 2: Computer-System Structures
Chapter 2: Computer-System Structures
Module 2: Computer-System Structures
Module 2: Computer-System Structures
Presentation transcript:

Exceptions, Interrupts & Traps Operating System Hebrew University Spring 2007

Memory Hierarchy Main Memory - located on chips inside the system unit. The program instructions and the processes data are kept in main memory during computer works. External Memory - disk. Information stored on a disk is not deleted when the computer turned off. The main memory has less storage capacity than the hard disk. The hard disk can write and read information to and from the main memory. The access speed of main memory is much faster than a hard disk. Programs are stored on the disk until they are loaded into memory, and then use the disk as both the source and destination of the information for their processing.

Typical Memory Hierarchy

Definitions… When the CPU is in kernel mode, it is assumed to be executing trusted software, and thus it can execute any instructions and reference any memory addresses. The kernel is the core of the operating system and it has complete control over everything that occurs in the system. The kernel is trusted software, but all other programs are considered untrusted software. A system call is a request to the kernel in a Unix-like operating system by an active process for a service performed by the kernel.

More Definitions… A process is an executing instance of a program. An active process is a process that is currently advancing in the CPU (while other processes are waiting in memory for their turns to use the CPU). Input/output (I/O) is any program, operation or device that transfers data to or from the CPU and to or from a peripheral device (such as disk drives, keyboards, mice and printers). Processes in kernel mode can be interrupted by an interrupt or an exception.

More Definitions… An interrupt is a signal to the operating system indicating that an event has occurred, and it results in changes in the sequence of instructions that is executed by the CPU. In the case of a hardware interrupt, the signal originates from a hardware device such as a keyboard (i.e., when a user presses a key), mouse or system clock (used to coordinate the computer's activities). A software interrupt is an interrupt that originates in software, usually triggered by a program in user mode. All processes initially execute in user mode, and they switch to kernel mode only when obtaining a service provided by the kernel.

More Definitions… User mode is a non-privileged mode in which it is forbidden for processes in this mode to access those portions of memory that have been allocated to the kernel or to other programs. When a user mode process wants to use a service that is provided by the kernel, it must switch temporarily into kernel mode. Process in kernel mode has root (i.e., administrative) privileges and access to key system resources. The entire kernel, which is not a process but a controller of processes, executes only in kernel mode. When the kernel has satisfied the request by a process, it returns the process to user mode.

Interrupts - Motivation Much of the functionality embedded inside a personal computer is implemented by hardware devices other than the processor. Since each device operates at its own pace, a method is needed for synchronizing the operation of the processor with these devices. One solution is for the processor to sit in a tight loop, asking each device about its current state. When data is available in one of the devices, the processor can then read and process the incoming bytes.

Interrupts - Motivation This method works but it has two main disadvantages: Wasteful in terms of processing power - the processor is constantly busy reading the status of the attached devices instead of executing some useful code. When the rate of data transfer is extremely high, the processor might lose data bytes arriving from the hardware devices.

Hardware Interrupts Instead of polling hardware devices to wait for their response, each device is responsible for notifying the processor about its current state. When a hardware device needs the processor's attention, it simply sends an electrical signal (hardware interrupt) through a dedicated pin in the interrupt controller chip (located on the computer's motherboard).

Software Interrupts Most of these interrupts are synchronous rather than asynchronous. They are generated by the processor itself as a result of some command.

Signals Signals that come from outside the process are asynchronous software interrupts. A process can install handlers to take special action when a signal arrives. Signals are defined and handled entirely through software. For example: A user at a terminal typing the interrupt key to stop a program (^C). Signal causes: User press certain terminal key Exception (for example: invalid memory reference) The kill function

Exceptions Exceptions - special type of software interrupts. They are generated by the processor itself whenever some unexpected critical event occurs. For instance, a page fault exception is triggered when the processor attempts to access memory portion, which is marked as not-present. The exception handler can then reload this memory portion (page) from disk and restart the instruction which generated the exception.

Exceptions – Cont. Three types of exceptions can be generated by the processor: Faults Traps Aborts

Fault When a fault exception occurs, the registers point to the address of the instruction, which generated the exception. This gives the exception handler a chance to fix the condition which caused the exception to occur, before restarting the faulting instruction. The program is restarted at the address of the fault.

Fault Example A program requests data that is not currently in real memory. An interrupt triggers the operating system to fetch the data from the disk and load it into main memory. The program gets its data without even know that an exception has occurred. The program continue with the same instruction.

Trap The execution of an instruction that intended for user programs and transfers control to the operating system. Trap causes branch to OS code and a switch to kernel mode. When in kernel mode, a trap handler is executed to service the request. Restarted at the address following the address causing the trap. Example: any System Call.

An Example open(“/tmp/foo”): USER: KERNEL: store the system call number and the parameters in a predefined kernel memory location; trap(); retrieve the response from a predefined kernel memory location; return the response to the calling application; KERNEL: trap(): jump &int[80]; // transfer control to the gate routine Gate routine: switch(sys_call_num) { case OPEN: … }

Abort Aborts neglect to specify the location of the faulting instruction, since they are used to indicate severe errors (such as hardware errors) which are not recoverable. Give no reliable restart address. Examples: Divide by zero Access to unallocated memory, a segmentation fault.

Hardware Interrupts Ideally, to perform multiple tasks (e.g., run the program, service the display, service the keyboard etc.) we would employ multiple processors. Alternatively we can allow the CPU to split its time between the main tasks and handling requests as they occur. If done efficiently, then conceptually the CPU is performing all tasks simultaneously

Dealing with Interrupts Combination of hardware & software is necessary to deal with interrupts: hardware chooses time to interrupt the program and transfer control. software in the form of the handler to deal with the interrupt.

The Handler Determined by the cause that generated the interrupt and determines how it should be dealt with (e.g., an input device has some data ready). Must preserve the state of the previously running program. Should be fast so minimal (none?) impact on previously running program.

The Interrupt Controller The interrupt controller serves as an intermediate between the hardware devices and the processor. Its responsibility is to alert the processor when one of the hardware devices needs its immediate attention. In this case, the processor stops its current activity and jumps to execute a function (interrupt handler) which was previously associated with the calling device.

Hardware Interrupt Causes Caused by an external event which typically needs routine attention. For example: Disk drive has data that was requested 20 ms ago. User pressed a key on the keyboard. User sneezed, causing mouse to move. Timer (used by the OS as an alarm clock) expired.

APC The interrupt is effectively invisible to the interrupted program. The interrupt is asynchronous. Can also be characterized as an "asynchronous procedure call"

Example add r1, r2, r3 sub r4, r5, r6 xor r7, r8, r9 As execution reaches code above, achoooo (user sneezes)  moving mouse  triggering an interrupt. Based on time of sneeze (in the middle of sub), hardware completes add and sub, but squashes xor (for now). The handler starts: The screen pointer (the little arrow) is moved The handler finishes Execution resumes with xor.

The basic mechanism Getting the interrupt Transfer control Saving current status The request is serviced Previous state is restored Return control

Getting the Interrupt External event interrupts the main program execution. An electronic signal is provided to the processor - indicating the need to handle an interrupt request. This signal is only recognized at the end of the instruction cycle loop (after the current instruction has been processed, but before the next instruction is "fetched" from memory).

Transfer control Control is transferred to a different "program" – the kernel Switching to kernel mode

Saving Current Status Before an interrupt can be serviced, the processor must save its current status. Servicing an interrupt is like performing a subroutine call. One of the most critical pieces of information that must be saved is the value of the Program Counter (i.e. the location of the next instruction to be performed after servicing of the interrupt is complete). Processing an interrupt request involves performing a series of instructions for that request. This tends to modify the contents of registers, so the registers also need to be saved.

The Request is Serviced The processor needs to check which device sent the interrupt request. The processor determine where to find the necessary instructions needed to service that specific request (typically handled using a " interrupt vector" which contains interrupt device numbers and the addresses of service subroutines for each interrupt number). The interrupt vector is stored at a predefined memory location The handler address is found using the interrupt number as an index into the interrupt vector

Previous State is Restored As a final step in each service routine, all register values, including the Program Counter, must be restored to their original values as they were just before the interrupt occurred.

Return control Control is returned to the interrupted program The next instruction is pointed by the program counter Back to user mode!