Everything you were supposed to have learned in CS3610…

Slides:



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

Computer Architecture
Chapter 6 Limited Direct Execution
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Interrupts (contd..) Multiple I/O devices may be connected to the processor and the memory via a bus. Some or all of these devices may be capable of generating.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Architectural Support for Operating Systems. Announcements Most office hours are finalized Assignments up every Wednesday, due next week CS 415 section.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Operating System Design. Today’s Lectures I/O subsystem and device drivers Interrupts and traps Protection, system calls and operating mode OS structure.
1 Computer System Overview OS-1 Course AA
Chapter 7 Interupts DMA Channels Context Switching.
Computer System Structures memory memory controller disk controller disk controller printer controller printer controller tape-drive controller tape-drive.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
What do operating systems do? manage processes manage memory and computer resources provide security features execute user programs make solving user.
1 Computer System Overview Chapter 1 Review of basic hardware concepts.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
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.
Introduction to Embedded Systems
System Calls 1.
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
Architecture Support for OS CSCI 444/544 Operating Systems Fall 2008.
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.
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
1 Chapter 2: Computer-System Structures  Computer System Operation  I/O Structure  Storage Structure  Storage Hierarchy  Hardware Protection  General.
The Functions of Operating Systems Interrupts. Learning Objectives Explain how interrupts are used to obtain processor time. Explain how processing of.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage Structure Storage Hierarchy Hardware Protection Network Structure.
13-Nov-15 (1) CSC Computer Organization Lecture 7: Input/Output Organization.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
1 CSE451 Architectural Supports for Operating Systems Autumn 2002 Gary Kimura Lecture #2 October 2, 2002.
Lecture Topics: 10/29 Architectural support for operating systems –timers –kernel mode –system calls –protected instructions.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
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.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
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.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
OSes: 2. Structs 1 Operating Systems v Objective –to give a (selective) overview of computer system architectures Certificate Program in Software Development.
Chapter 2: Computer-System Structures(Hardware) or Architecture or Organization Computer System Operation I/O Structure Storage Structure Storage Hierarchy.
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.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Chapter 2: Computer-System Structures
Module 2: Computer-System Structures
Architectural Support for OS
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
Module 2: Computer-System Structures
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
CSE 451: Operating Systems Winter 2007 Module 2 Architectural Support for Operating Systems Brian Bershad 562 Allen Center 1.
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Architectural Support for OS
Chapter 2: Computer-System Structures
Chapter 2: Computer-System Structures
Module 2: Computer-System Structures
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment. Phone:
Module 2: Computer-System Structures
Presentation transcript:

Everything you were supposed to have learned in CS3610…

Computer System Architecture Synchronizes memory access

I/O operations I/O devices and the CPU can execute concurrently. I/O is moving data between device & controller’s buffer CPU moves data between controller’s buffer & main memory Each device controller is in charge of certain device type. May be more than one device per controller SCSI can manage up to 7 devices Each device controller has local buffer, special registers A device driver for every device controller Knows details of the controller Presents a uniform interface to the rest of OS

Accessing I/O Devices Memory Mapped I/O I/O devices appear as regular memory to CPU Regular loads/stores used for accessing device This is more commonly used Programmed I/O Also called “channel” I/O CPU has separate bus for I/O devices Special instructions are required Which is better?

Building a Keyboard Controller Mesh detects which key is pressed

Building a Keyboard Controller Encoders convert lines to binary Encoder

Building a Keyboard Controller Latch stores encoding of pressed key Encoder Latch CLK

Building a Keyboard Controller Circuit to read Latch data Encoder Latch CLK CPU Data Address R/W Clock

Building a Keyboard Controller A simple functional keyboard! Encoder Latch CLK CPU Data Address R/W Clock Buffer DEMUX What is the problem?

Interrupts Mechanism required for device to interrupt the CPU Alternatively, CPU could poll. But this is inefficient Implementing interrupts A line to interrupt CPU Set of lines to specify priority CPU Memory Device DataAddr R/W Clock Interrupt Interrupt Priority

Handling Multiple Devices Interrupt controller OR all interrupt lines Highest priority passed to CPU Can remap priority levels CPU Priority Selector

How does I/O work? Device driver loads controller registers appropriately Controller examines registers, executes I/O Controller signals I/O completion to device driver Using interrupts High overhead for moving bulk data (i.e. disk I/O)

Direct Memory Access (DMA) Transfer data directly between device and memory No CPU intervention Device controller transfers blocks of data Interrupts when block transfer completed As compared to when byte is completed Very useful for high-speed I/O devices

Example I/O cache CPU (*N) Thread of execution Memory Instructions and Data Instruction execution cycle Data movement Keyboard Device Driver and Keyboard Controller I/O Request Perform I/O Read Data Interrupt Data Disk Device Driver and Disk Controller DMA

Interrupts (very important!) Notification from interface that device needs servicing Hardware: sends trigger on bus Software: uses a system call Steps followed on receiving an interrupt: Stop kernel execution Save machine context at interrupted instruction Commonly, incoming interrupts are disabled Transfer execution to Interrupt Service Routine (ISR) Mapping done using the Interrupt Vector (faster) After ISR, restore kernel state and resume execution Most operating systems are interrupt-driven

Interrupt Timeline

Traps and Exceptions Software generated interrupt Exception: user program acts silly Caused by an error (div by 0, or memory access violation) Just a performance optimization Trap: user program requires OS service Caused by system calls Handled similar to hardware interrupts: Stops executing the process Calls handler subroutine Restores state after servicing the trap

Why Protection? Application programs could: Start scribbling into memory Get into infinite loops Other users could be: Gluttonous Evil Or just too numerous Correct operation of system should be guaranteed  A protection mechanism

Preventing Runaway Programs Also how to prevent against infinite loops Set a timer to generate an interrupt in a given time Before transferring to user, OS loads timer with time to interrupt Operating system decrements counter until it reaches 0 The program is then interrupted and OS regains control. Ensures OS gets control of CPU When erroneous programs get into infinite loop Programs purposely continue to execute past time limit Setting this timer value is a privileged operation Can only be done by OS

Protecting Memory Protect program from accessing other program’s data Protect the OS from user programs Simplest scheme is base and limit registers: Virtual memory and segmentation are similar memory Prog A Prog B Prog C Base register Limit register Loaded by OS before starting program

Virtual Memory / Segmentation Here we have a table of base and limit values Application references some address x CPU breaks this into a page number and an offset (or a segment number and offset) (More common) Pages have fixed size, usually 512 bytes (Now rare) Segments: variable size up to some maximum Looks up the page table entry (PTE) for this reference PTE tells the processor where to find the data in memory (by adding the offset to the base address in the PTE) PTE can also specify other things such as protection status of page, which “end” it starts at if the page is only partially filled, etc.

Protected Instructions Also called privileged instructions. Some examples: Direct user access to some hardware resources Direct access to I/O devices like disks, printers, etc. Instructions that manipulate memory management state page table pointers, TLB load, etc. Setting of special mode bits Halt instruction Needed for: Abstraction/ease of use and protection

Dual-Mode Operation Allows OS to protect itself and other system components User mode and kernel mode OS runs in kernel mode, user programs in user mode OS is god, the applications are peasants Privileged instructions only executable in kernel mode Mode bit provided by hardware Can distinguish if system is running user code or kernel code System call changes mode to kernel Return from call using RTI resets it to user How do user programs do something privileged?

Crossing Protection Boundaries User calls OS procedure for “privileged” operations Calling a kernel mode service from user mode program: Using System Calls computer switches execution to kernel mode User processSystem Call Trap Mode bit = 0 Save Caller’s stateExecute system callRestore state Return Mode bit = 1 Resume process User Mode Mode bit = 1 Kernel Mode Mode bit = 0

System Calls Programming interface to services provided by the OS Typically written in a high-level language (C or C++) Mostly accessed by programs using APIs Three most common APIs: Win32 API for Windows POSIX API for POSIX-based systems (UNIX, Linux, Mac OS X) Java API for the Java virtual machine (JVM) Why use APIs rather than system calls? Easier to use

Why APIs? System call sequence to copy contents of one file to another Standard API

Reducing System Call Overhead Problem: The user-kernel mode distinction poses a performance barrier Crossing this hardware barrier is costly. System calls take 10x-1000x more time than a procedure call Solution: Perform some system functionality in user mode Libraries (DLLs) can reduce number of system calls, by caching results (getpid) or buffering ops (open/read/write vs. fopen/fread/ fwrite).

SideBar: Real System Have Holes OSes protect some things, ignore others Most will blow up when you run: Usual response: freeze To unfreeze, reboot If not, also try touching memory More broadly: O/S isn’t able to defend itself against deliberate (or stupid) misuse, attack, etc Goal: make it feasible to write applications that are good citizens Promote effective resource sharing, correctness, security, high performance… but you need to understand the O/S to make effective use of its features int main() { while (1) { fork(); }

Back to Protection Boundaries User calls OS procedure for “privileged” operations Calling a kernel mode service from user mode program: Using System Calls computer switches execution to kernel mode User processSystem Call Trap Mode bit = 0 Save Caller’s stateExecute system callRestore state Return Mode bit = 1 Resume process User Mode Mode bit = 1 Kernel Mode Mode bit = 0

Interrupts… same idea Device “asks” kernel to call an interrupt handler User processRead Call Push StateRemove dataPop State Resume process The quick brown fox jumped ov… Keyboard Device Handler RTI Push StatePop State

How interrupts work Hardware requests an interrupt At next opportunity, processor pauses and triggers the same sequence we saw for system calls Whatever was running has its state saved (kernel stack) Switch to kernel mode if we weren’t in it already Kernel handler wakes up, finds device id Table indexed by device-id gives address of handler Handler looks just like any other function But notice that an interrupt can occur while we’re already in the kernel executing a system call

Interrupts vs. Procedure Call Procedure call Push args on stack “Call” pushes PC, then jumps to function Function saves registers it will use Execute function Pop (restore) registers Pop return PC address, jumping to caller Interrupt, trap, etc Pause normal execution CPU pushes PC, enters kernel mode, jumps to a standard address Save registers and decide what handler to call Procedure call to handler RTI/RTT pops (restore) registers and resumes prior execution

Making it real What issues make it tricky to write an interrupt handler or a kernel procedure? User processRead Call Push StateRemove dataPop State Resume process The quick brown fox jumped ov… Keyboard Device Handler RTI Push StatePop State

Implementing a queue How should we implement the data structure that stores the input characters? O/S doesn’t want to risk memory “exhaustion” Best: a fixed maximum-size buffer So: the queue is a byte array: byte[N] InputQueue = new byte[N];

A circular structure We’ll have a pointer into the queue, pointing to the next free slot. It increments from 0..(N-1), then to 0 Int NextFree = 0; NextFree = (NextFree+1)%N; Similarly for the next available character, NextAvail

A circular structure  | T | h | e | | q | u |  |  |  NextAvail = 1 NextFree = 8 N = 10 NAvail = 6 N: Length of the InputQueue NextAvail: “pointer” to the next available character, if NAvail > 0 NAvail: how many characters are currently available NextFree: pointer to the next free slot, if Navail < N.

Code fragments char ReadInput() { …. char c = Buf[NextAvail]; NextAvail = (NextAvail+1)%N; NAvail = NAvail-1; …. return c; } void Interrupt(char c) { …. Buf[NextFree] = c; NextFree = (NextFree+1)%N; NAvail = Navail+1; …. } Not shown: “block” (make the application wait) if there are no characters when a Read is done. Don’t restart the keyboard if there won’t be room for the next input character.

A bug! User runs the program and types in 34 characters: The quick brown fox jumped over the lazy dog Application receives 22, and some are wrong: Th11zpjkj[1brow171jznz How can this be? The code was trivial!

Assembly code char ReadInput() { …. // NAvail = NAvail -1 mov NAvail, R1 sub $1,R1 mov R1,NAvail …. } void Interrupt(char c) { …. // NAvail = NAvail +1 mov NAvail, R1 add $1,R1 mov R1,NAvail …. } But when does “Interrupt” actually execute?

A bear walks into a bar. Where does it sit? An interrupt needs to occur. When does it happen? Whenever it likes User processRead Call Push StateRemove dataPop State Resume process The quick brown fox jumped ov… Keyboard Device Handler RTI Push StatePop State

Assembly code char ReadInput() { …. // NAvail = NAvail -1 mov NAvail, R1 sub $1,R1 mov R1,NAvail …. } void Interrupt(char c) { …. // NAvail = NAvail +1 mov NAvail, R1 add $1,R1 mov R1,NAvail …. } Push will save the value of R1 on the stack. Pop restores R1 to its value prior to the interrupt Push State Pop State

Example: NAvail initially 3 char ReadInput() { …. // NAvail = NAvail -1 mov NAvail, R1 sub $1,R1 mov R1,NAvail …. } void Interrupt(char c) { …. // NAvail = NAvail +1 mov NAvail, R1 add $1,R1 mov R1,NAvail …. } NAvail should be 3 (old value – 1 + 1)… but we leave it at 2! A character will vanish from the input buffer Push State Pop State R1 = 3-1 = 2 Push 2 on stack NAvail is still 3 R1 = 3+1 = 4 NAvail = 4 Pop 2 from stack into R1, restoring pre-interrupt value NAvail = 2

Effect of interrupts? NAvail will have the wrong value in it. We can forget that we added a character With other plausible code sequences we could forget that we removed characters So… reads could return the wrong number of characters and could read data from parts of the buffer that have junk in them!

An easy fix… Interrupt handler can disable interrupts Done with a special hardware instruction accessed from a special kernel function (normal user code can’t execute this instruction or call this kernel function – it isn’t a system call) While interrupts are disabled, hardware that needs service will be “waiting” and can hurt performance if used too often Notice the tension Disabling interrupts makes it easier to write correct code But we prefer not to disable them if possible, because doing so makes our O/S “less responsive” to I/O events Forcing us to think hard: “when must I disable them?”

Hardware interrupt “levels” Rather than have all interrupts blocked, there are usually a few “levels” of interrupts Each device has its own associated level Illegal instructions, bad memory accesses: top priority Then the system clock/timer Next, disks and the network – high speed DMA devices Then USB stuff: USB memories, keyboard, etc So if the keyboard driver disables interrupts, it probably only blocks keyboard interrupts – not others

This illustrates a bigger issue Part of the job of the operating system is to maximize concurrency Having more things happening at once means that more work is getting done Overlapping I/O with computation is just one example of concurrency… we’ll see many others Allowing concurrency creates a risk that concurrent events will interfere with one-another (bugs) Can temporarily disallow concurrency, but this will slow the computer down (defeating the purpose)

Definition: A race condition We say that code contains a race condition if: We are executing a fragment of code Correctness of the outcome depends on whether or not some other concurrent event occurs before we finish the fragment Our example illustrated a race condition between the ReadInput system call and the Interrupt handler Disabling interrupts during ReadInput would be one way of eliminating the race condition

Road Map Looking at the next few weeks We’ll be exploring ways of dealing with problems such as race conditions (there are several such problems) They arise within the O/S, for example in interrupt handlers, but also in multi-threaded applications To understand these issues, we’ll need to understand the “abstraction” of a program running in an O/S First, explore the “process abstraction” Then, revisit race conditions in this context Learn to build safe, concurrent code