Chap.6 Limited Direct Execution. Dongkun Shin, SKKU

Similar documents
The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

Brief tour of hardware

Tutorial 10 Protection Cont.

Lecture 2: Architectural Support for OSes

CSE 120 Principles of Operating Systems

Architectural Support for OS

CSC369 Lecture 2. Larry Zhang

CSE 120 Principles of Operating Systems

The Kernel Abstraction

Lecture 2: September 9

Operating Systems. Computer Science & Information Technology (CS) Rank under AIR 100

CSC369 Lecture 2. Larry Zhang, September 21, 2015

W4118: interrupt and system call. Junfeng Yang

TCSS 422: OPERATING SYSTEMS

Hardware OS & OS- Application interface

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

Interrupts & System Calls

OS: An Overview. ICS332 Operating Systems

CSE 153 Design of Operating Systems Fall 18

Operating Systems ECE344

Part I. X86 architecture overview. Secure Operating System Design and Implementation x86 architecture. x86 processor modes. X86 architecture overview

Microkernel Construction

Processes. Johan Montelius KTH

Process Scheduling Queues

A process. the stack

Computer Systems II. First Two Major Computer System Evolution Steps

Architectural Support for Operating Systems

Operating Systems. Operating System Structure. Lecture 2 Michael O Boyle

Microkernel Construction

6. Mechanism: Limited Direct Execution

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

Processes. Dr. Yingwu Zhu

OS Structure. Hardware protection & privilege levels Control transfer to and from the operating system

Windows Interrupts

The Kernel Abstraction. Chapter 2 OSPP Part I

Anne Bracy CS 3410 Computer Science Cornell University

CS 550 Operating Systems Spring System Call

CS 537 Lecture 2 Computer Architecture and Operating Systems. OS Tasks

Protection. OS central role. Fundamental to other OS goals. OS kernel. isolation of misbehaving applications. Relaibility Security Privacy fairness

Four Components of a Computer System

Yielding, General Switching. November Winter Term 2008/2009 Gerd Liefländer Universität Karlsruhe (TH), System Architecture Group

Computer System Architecture. CMPT 300 Operating Systems I. Summer Segment 3: Computer System Architecture. Melissa O Neill

Review: Easy Piece 1

Inf2C - Computer Systems Lecture 16 Exceptions and Processor Management

int $0x32 // call interrupt number 50

CS510 Operating System Foundations. Jonathan Walpole

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

Background: Operating Systems

CS333 Intro to Operating Systems. Jonathan Walpole

CS 550 Operating Systems Spring Interrupt

An Interrupt is either a Hardware generated CALL (externally derived from a hardware signal)

CS370 Operating Systems

Operating System Architecture. CS3026 Operating Systems Lecture 03

Anne Bracy CS 3410 Computer Science Cornell University

Architecture and OS. To do. q Architecture impact on OS q OS impact on architecture q Next time: OS components and structure

CS 326: Operating Systems. Process Execution. Lecture 5

Four Components of a Computer System. Operating System Concepts 7 th Edition, Jan 12, 2005

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

Background. IBM sold expensive mainframes to large organizations. Monitor sits between one or more OSes and HW

Architectural Support for Operating Systems

Part I Introduction Hardware and OS Review

Course: Operating Systems Instructor: M Umair. M Umair

CSCE Introduction to Computer Systems Spring 2019

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

BASIC INTERRUPT PROCESSING

Virtual Machines & the OS Kernel

CS5460: Operating Systems

3. Process Management in xv6

CSCI 6411: Operating Systems. Acknowledgements: Some slide material derived from Silberschatz, et al.

CSC 2405: Computer Systems II

Syscalls, exceptions, and interrupts, oh my!

ECEN 449 Microprocessor System Design. Hardware-Software Communication. Texas A&M University

hardware interrupts software interrupts

Process Description and Control

Virtual machines are an interesting extension of the virtual-memory concept: not only do we give processes the illusion that they have all of memory

Protection and System Calls. Otto J. Anshus

Preemptive Scheduling

Processes (Intro) Yannis Smaragdakis, U. Athens

Even coarse architectural trends impact tremendously the design of systems

Architectural Support for Operating Systems

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

Even coarse architectural trends impact tremendously the design of systems. Even coarse architectural trends impact tremendously the design of systems

Interrupts and System Calls

Operating System Review

Overview. This Lecture. Interrupts and exceptions Source: ULK ch 4, ELDD ch1, ch2 & ch4. COSC440 Lecture 3: Interrupts 1

آنستیتیوت تکنالوجی معلوماتی و مخابراتی ICTI

CSE 153 Design of Operating Systems

Virtual Machines & the OS Kernel

PROCESSES AND THREADS THREADING MODELS. CS124 Operating Systems Winter , Lecture 8

18-349: Introduction to Embedded Real-Time Systems

Chapter 5 B. Large and Fast: Exploiting Memory Hierarchy

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

KERNEL THREAD IMPLEMENTATION DETAILS. CS124 Operating Systems Winter , Lecture 9

[537] Virtual Machines. Tyler Harter

CSE 451: Operating Systems Winter Module 2 Architectural Support for Operating Systems

Operating System Services

COS 318: Operating Systems

Transcription:

Chap.6 Limited Direct Execution 1

Problems of Direct Execution The OS must virtualize the CPU in an efficient manner while retaining control over the system. Problems how can the OS make sure the program doesn t do anything that we don t want it to do, while still running it efficiently? such as issuing an I/O request to a disk, or gaining access to more system resources such as CPU or memory how does the OS stop the process from running and switch to another process, thus implementing the time sharing? 2

Problem #1: Restricted Operations Without limits on running programs, the OS wouldn t be in control of anything and thus would be just a library Two different privileged modes user mode code that runs in user mode is restricted in what it can do. For example, when running in user mode, a process can t issue I/O requests; doing so would result in the processor raising an exception; kernel mode the operating system (or kernel) runs in. In this mode, code that runs can do what it likes, including privileged operations such as issuing I/O requests and executing all types of restricted instructions. 3

Problem #1: Restricted Operations System call When a user process wishes to perform some kind of privileged operation a program must execute a special trap instruction. Trap must save the caller s registers in order to be able to return correctly to the caller This instruction simultaneously jumps into the kernel and raises the privilege level to kernel mode. User code specifies a system-call number Trap table has the jump address for the system-call number once in the kernel, the system can now perform whatever privileged operations are needed (if allowed), and thus do the required work for the calling process. When finished, the OS calls a special return-from-trap instruction, which returns into the calling user program while simultaneously reducing the privilege level back to user mode. 4

System Mode Dual-mode operation allows OS to protect itself and other system components User mode and kernel mode (supervisor mode, privileged mode) Mode bit provided by hardware Some instructions designated as privileged, only executable in kernel mode Mode change When a trap or interrupt occurs, the H/W switches from user mode to kernel mode 5

Interrupt and trap Interrupt Unexpected external event I/O interrupt, Clock interrupt, Console interrupt Machine check interrupt, Inter-process communication interrupt, etc From either the H/W or the S/W H/W may trigger an interrupt by sending a signal to the CPU Trap Software-generated interrupt caused by Exception: An error of the running program (div by zero) System call: A specific request from a user program: 6

Interrupts Interrupt handling process 7

Interrupts 8

Privileged instructions Machine instructions that can be executed only in kernel mode Examples I/O control Timer management Interrupt management Switching to user mode Etc Intel privileged instructions CLTS - Clear Task-Switched Flag HLT - Halt Processor LGDT - Load GDT Register LIDT - Load IDT Register LLDT - Load LDT Register LMSW - Load Machine Status LTR - Load Task Register MOV CRn - Move Control Register MOV DRn - Move Debug Register MOV TRn - Move Test Register 9

System Call Interface Interface between applications/processes and the OS Services that OS provides to applications/processes Means for a user program to ask the OS to perform tasks reserved for OS on the user program s behalf Means used by a process to request action by the OS Generally, programs uses a high-level Application Programming Interface (API) rather than direct system call POSIX API Executed by a generic trap instruction syscall instruction in MIPS R2000 swi instruction in ARM INT/SYSENTER in x86 10

Limited Direct Execution each process has a kernel stack where registers are saved to and restored from (by the hardware) when transitioning into and out of the kernel (x86 Architecture) 11

API System Call OS Relationship User Application #include <stdio.h> int main() { printf( hello world! ); return 0: } Standard C library trap syscall(3); /* write */ User mode Kernel mode 3 Trap Handler write() implementation of write system call return Kernel Code 12

Problem #2: Switching Between Processes How can the OS regain control of the CPU so that it can switch between processes? A Cooperative Approach: Wait For System Calls OS trusts the processes of the system to behave reasonably. Processes that run for too long are assumed to periodically give up the CPU so that the OS can decide to run some other task. via system calls (including explicit yield), trap by exception Passive approach What happens if a process (whether malicious, or just full of bugs) ends up in an infinite loop, and never makes a system call? 13

Problem #2: Switching Between Processes A Non-Cooperative Approach: The OS Takes Control timer interrupt: A timer device can be programmed to raise an interrupt every so many milliseconds when the interrupt is raised, the currently running process is halted, and a pre-configured interrupt handler in the OS runs. save the state of the program that was running when the interrupt occurred At this point, the OS has regained control of the CPU, and thus can do what it pleases: stop the current process, and start a different one. 14

Problem #2: Switching Between Processes Application Process Application Process Application Process Application Process System Lib. System Call Interface OS Mem Mgr Process Mgr File System IO Mgr Device Drivers ROM bootstrap Hardware app1 OS app2 OS app3 OS app1 timer interrupt timer interrupt timer interrupt Time 15

Problem #2: Switching Between Processes When OS has regained control, it must decide whether to continue running the currently-running process, or switch to a different one need scheduler If the decision is made to switch, the OS then executes a low-level piece of code which we refer to as a context switch save a few register values for the currently-executing process (onto its kernel stack, for example) and restore a few for the soon-to-beexecuting process (from its kernel stack). when the return-from-trap instruction is finally executed, instead of returning to the process that was running, the system resumes execution of another process. 16

Problem #2: Switching Between Processes implicitly saved by H/W How long? explicitly saved by OS 17

Context Switching When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process via a context switch Context Set of information related to a process User-level context Register context System-level context 18

Context Switching Context switch Context saving: Saving the register context of a process when the process releases processor Context restoring: Reloading the register context of a process to continue execution Context switching: Saving the context of a process and restoring the context of another process Context switch time Pure overhead The more complex the OS and the PCB the longer the context switch Highly dependent on hardware support Some processors provide multiple sets of registers Context switch by changing the pointer to the current register set Sun UltraSPARC, ARM 19

Context Switching 20

Worried About Concurrency? Concurrency Issues Interrupt during trap handling What happens when, during a system call, a timer interrupt occurs? Interrupt during interrupt handling What happens when you re handling one interrupt and another one happens? One simple solution is disable interrupts during interrupt processing disabling interrupts for too long could lead to lost interrupts More sophisticated approach is locking protect concurrent access to internal data structures. enables multiple activities to be on-going within the kernel at the same time, particularly useful on multiprocessors 21