Architectural Support for OS

Slides:



Advertisements
Similar presentations
The Kernel Abstraction. Debugging as Engineering Much of your time in this course will be spent debugging – In industry, 50% of software dev is debugging.
Advertisements

The Kernel Abstraction. Challenge: Protection How do we execute code with restricted privileges? – Either because the code is buggy or if it might be.
Chapter 6 Limited Direct Execution
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
CMPT 300: Operating Systems I Dr. Mohamed Hefeeda
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.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Dr. Mohamed Hefeeda.
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.
1 Last Class: Introduction Operating system = interface between user & architecture Importance of OS OS history: Change is only constant User-level Applications.
1 OS & Computer Architecture Modern OS Functionality (brief review) Architecture Basics Hardware Support for OS Features.
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.
Protection and the Kernel: Mode, Space, and Context.
Architecture Support for OS CSCI 444/544 Operating Systems Fall 2008.
CHAPTER 2: COMPUTER-SYSTEM STRUCTURES Computer system operation Computer system operation I/O structure I/O structure Storage structure Storage structure.
Chapter 2: Computer-System Structures
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.
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.
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.
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.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
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
CSCE451/851 Introduction to Operating Systems
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Introduction to Operating Systems Concepts
Exceptional Control Flow
Introduction to Operating Systems
Introduction to Operating Systems
Chapter 2: Computer-System Structures(Hardware)
Chapter 2: Computer-System Structures
Processes and threads.
Exceptional Control Flow
Microprocessor Systems Design I
Protection and OS Structure
Anton Burtsev February, 2017
Mechanism: Limited Direct Execution
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
CSE 120 Principles of Operating
Introduction to Operating Systems
Computer-System Architecture
Module 2: Computer-System Structures
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CSE 153 Design of Operating Systems Winter 18
Exceptions Control Flow
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
Interrupts and System Calls
Presentation transcript:

Architectural Support for OS Reading Reference: Textbook 1 Chapter 2 Architectural Support for OS Tanzir Ahmed CSCE 313 Fall 2018

Theme of Today’s Discussion Understand Dual-Mode Operation (User Mode versus Kernel Mode) Also called Limited-Direct Execution Combine that understanding with that of Exception Control Flow

Quick Recap – Find Best Match for the Coffee Shop Analogy Computer Systems Analogy System Process Interrupt CPU System Call Telephone Ringing Customer Place an Order Coffee Shop Coffee Machine

Exceptions Recap – Find Best Match Exception Type Definition Interrupt System Call Fault Trap Abort Hard Reset Asynchronous Event Unintentional and Unrecoverable Invoked by File I/O Non-Resident Page in Memory Breakpoint Instruction for Debug Caused by arrival of packet from network

Architectural Support for OS Operating systems mediate between applications and the physical hardware of the computer Key goals of an OS are to enforce protection and resource sharing If done well, applications can be oblivious to HW details

Challenge: Protection Why do we execute code with restricted privileges? Either because the code is buggy or if it might be malicious Some examples: A script running in a web browser A program you just downloaded off the Internet A program you just wrote that you haven’t tested yet

Challenge: Safe Resource Sharing How do we ensure that resources are fairly, securely, and efficiently shared amongst (and utilized by) user programs? Some examples: Many students running code on a department machine Amazon.com servicing concurrent users Playing a movie on a computer while typing a project report and printing a document A process wants to open a 1TB file, but the disk storage is limited to 512MB

Architectural Features The features we design in HW to facilitate the OS to meet some key challenges Protection Modes Protection Ring / 2 modes: User/kernel Privileged Instructions Interrupts and Exceptions System Calls Timers (clock) Memory Protection Mechanisms I/O Control and Operation Synchronization Primitives (e.g., atomic instructions)

Dual-mode Execution Every CPU has at least 2 modes of execution (the same CPU is alternates between the modes) Kernel-mode: Execution with the full privileges of the hardware E.g. Read/write to any memory, access any I/O device, read/write any disk sector, send/receive any packet User-mode: Execution with Limited privileges Only those granted by the operating system kernel Some architectures support more than 2 Example: Both Intel and AMD support 4 modes These are often called protection ring However, both Windows and Linux support only 2 modes of operation Called Dual-mode operation User apps More drivers Drivers Kernel

Dual-mode Operation Depending on the architecture, execution mode is stored either in the Program Status Word (PSW) register or scattered on multiple registers (e.g., EFLAGS in Intel) The mode tells us whether the instruction should be checked or not If set (i.e., in user mode), each instruction is checked to see if allowed E.g., CLF (Clear Interrupt Flag) is never allowed in user mode; otherwise apps could ignore all Interrupts If not set (i.e., in kernel mode), no check is performed

Mode Switch Safe control transfer Mode separation does NOT mean that a user program cannot request a Kernel-mode operation User mode to kernel mode switch is very common (e.g., printf/cout, writing to a file) How do we switch from one mode to the other? Such that the protection is not compromised

CPU Instruction Fetch & Exec A Simple CPU Model branch/jump address … 100: ADD $rs, $rt, $rd 104: JEQ $r1, $r2, -100 … +4 Program Counter (PC) CPU Instruction Fetch & Exec Select PC Opcode (if branch/jump)

A CPU with Dual-Mode Operation Select PC Program Counter (PC) CPU Instruction Fetch & Exec +4 branch/jump address New PC Exception/ Interrupt Handler PC jump/branch? Exception? None? New Mode Mode Select Mode Kernel If trap? RTU? None? User Opcode

Dual-Mode Operation: Minimal Hardware Requirement Privileged instructions Subsect of instructions available only to the kernel Limits on memory accesses To prevent user code from overwriting the kernel or each other Timer To regain control from a user program periodically

Privileged Instructions - Examples Only the OS should be able to Directly access I/O devices (disks, printers..) Allows OS to enforce security and fairness User programs cannot possibly be fair to each other Manipulate memory management state E.g., page tables (Virtual-> Physical), protection bits, TLB entries, etc. Processes use them, but cannot modify – that would defeat the protection Adjust protected control registers User   Kernel modes or Raise/Lower interrupt level Execute CLF instruction A small cache for page tables

Question What should happen if a user program attempts to execute a privileged instruction? Will be treated by the Kernel as an attempt to go around protection measures, and will result in terminating the user application

Hardware Timer Operating system timer is a critical building block Many resources are time-shared; e.g., CPU Allows OS to prevent infinite loops Fallback mechanism by which OS regains control When timer expires, generates an interrupt Handled by kernel, which controls resumption context Basis for OS scheduler; more later… Setting (and clearing) a timer is a privileged instruction

User  Kernel Mode Switch From user-mode to kernel-mode Interrupts Triggered by timer and I/O devices Checked by the CPU after every instruction (Synchronous) Exceptions (Faults and Aborts) Triggered by unexpected program behavior Or malicious behavior! System calls (traps) (aka protected procedure call) Request by program for kernel to do some operation on its behalf Only limited # of very carefully coded entry points

Kernel  User Mode Switch From kernel-mode to user-mode New process/new thread start Jump to first instruction in program/thread Return from interrupt, exception, system call Resume suspended execution Process/thread context switch Resume some other process User-level upcall Asynchronous notification to user program by the kernel Example: Writing customized Page Fault handler

Safe Mode Transfer – Interrupt Handling First, User->Kernel. The main idea is rather simple Store the state of the running process (so that it can be resumed later) Execute the handler Return to the interrupted process by restoring the saved state But the actual implementation is a bit more complicated We first need to know which process info must be stored

Process State To transparently replace the running process in the CPU, we need to store/retrieve some information: Stack Pointer (SP) – points to the data Program Counter (PC) – points to code The Execution Flags register (EFLAGS) – contains processor state, e.g., overflow flag, if/else condition code (true/false) to make a jump Also, the other general purpose registers Because the current process might be using them lw $a, 0($b) add $a, $a, 1 sw $a, 0($b)

Saving Process State: Difficulty foo (){ x = x + 1; y = y - 2; } User-level Process CPU PC=100 SP EFLAGS Other Regs User Stack Handler (){ 6000: save PC 6004: … … } Kernel foo (){ 100: x=x+1; 104: y=y-2; … }

Saving Process State: Difficulty foo (){ x = x + 1; y = y - 2; } User-level Process CPU PC=6000 SP EFLAGS Other Regs User Stack Handler (){ 6000: save PC 6004: … … } Kernel foo (){ 100: x=x+1; 104: y=y-2; … } Kernel Stack This will only save 6000, the PC of the handler. The original PC=100 of user app is lost forever!!

To Summarize The processor has only 1 set of SP, PC, EFLAGS etc. Any piece of code (e.g. handler code as well) will require its own PC (and also SP and others) first loaded into the CPU Switching from User code to Handler code means overwriting PC, SP etc. with the handler PC, SP etc. But ALAS!!! We just lost the PC, SP for the user code How can we ever recover those?? Quoting the Anderson book: “This is akin to rebuilding the car’s transmission while it barrels down the road 60mph” Solution: Take hardware help Clearly, any other code will also need PC, SP,.. Hardware does not need to use SP, PC to implement a logic

User to Interrupt Handler – Mechanism on x86 Hardware does the following: Mask further interrupts (they are stored, not thrown away) Save PC, SP, EFLAGS in some special registers in CPU Change SP to point to the Kernel Interrupt Stack Change mode to Kernel Push PC, SP, EFLAGS in the special registers into the new stack the SP now points to (i.e., the Kernel Interrupt Stack) Push the exception/interrupt code Invoke the interrupt handler by vectoring through the Interrupt Vector Table (i.e., overwrite PC with the handler PC) Software (i.e., the handler code) does the following: Stores the rest of the general purpose registers being used by the interrupted process Does the interrupt handling operation

PC=100 SP EFLAGS Other Regs Before foo (){ 100:x=x+1; 104:y=y-2; } User-level Process CPU handler (){ pushad … } Kernel Registers PC=100 SP EFLAGS Other Regs User Stack Interrupts Enabled Kernel Interrupt Stack

Hardware Action foo (){ 100:x=x+1; 104:y=y-2; } User-level Process CPU handler (){ pushad … } Kernel Registers PC SP EFLAGS Other Regs User Stack Interrupts Disabled SP PC(100) EFLAGS Kernel Interrupt Stack

As Handler Starts foo (){ 100:x=x+1; 104:y=y-2; } User-level Process CPU handler (){ pushad … } Kernel Registers PC SP EFLAGS Other Regs User Stack Interrupts Disabled SP PC(100) EFLAGS Other Regs Kernel Interrupt Stack

At the end of handler Software does the following: Handler code restores the saved general registers Hardware does the following: Restores PC, SP, EFLAGS Switch to user mode

User-to-Kernel Mode Switch in System Calls Locate arguments In registers or on user stack Copy arguments From user memory into kernel memory Protect kernel from malicious code evading checks Validate arguments Protect kernel from errors in user code Copy results back into user memory

System Calls User Program Kernel (1) (6) (4) (3) Kernel Stub User Stub foo(){ open(“test”,“rw”); } User Program open_handler(arg1,arg2){ //do operation } Kernel (1) (6) (3) (4) open(arg1,arg2){ push SYSOPEN trap return } User Stub open_handler_stub(){ //copy args from user memory //check args open_handler(arg1,arg2) //copy return value to user mem. return } Kernel Stub (2) (5)

Summary: User/Kernel (Privileged) Mode User Mode interrupt FP exception FP Exception error syscall rtn rfi exec exit Kernel Mode Processor routinely checks for interrupts and if one is logged, it goes back to save the state of the process it is executing and enters the kernel to service the interrupt. The service is done by entering a interrupt vector table which contains the address of the specific interrupt service routine. Once the routine is serviced the kernel gives control back to the user process. In multiprocessor systems, only one processor has to service the interrupt from an external device. What happens when you move a mouse pointer? Each move or point or click registers as an interrupt which is noticed by the processor which moves the control back to the Kernel to service the interrupt routine and that shows up as an action to its associated process (e.g. move of mouse pointer in the active window, or highlight of a word selected, etc.) An alternative to Interrupt would be “polling” where the Kernel would go in a round-robin fashion to check the status of each device to see if they have an interrupt. Limited HW access Full HW access

Today’s Learnings Architectural support for user and kernel modes in CPU execution, especially Privileged Instructions Protection Timer Interrupt/System Call handling Most of the path switching User<->Kernel