1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
1 Exceptions, Interrupts & Traps Operating System Hebrew University Spring 2007.
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Chapter 6 Limited Direct Execution
1 OS Structure, Processes & Process Management. 2 Recap OS functions  Coordinator  Protection  Communication  Resource management  Service provider.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Thursday, June 08, 2006 The number of UNIX installations has grown to 10, with more expected. The UNIX Programmer's Manual, 2nd Edition, June, 1972.
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
OS Spring’03 Introduction Operating Systems Spring 2003.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Hardware Support for Operating Systems Sunny Gleason Vivek Uppal COM S 414
Figure 1.1 Interaction between applications and the operating system.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
OS Spring’04 Introduction Operating Systems Spring 2004.
Exceptions, Interrupts & Traps
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
System Calls 1.
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment.
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 CS/COE0447 Computer Organization & Assembly Language Chapter 5 part 4 Exceptions.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
Background: Operating Systems Brad Karp UCL Computer Science CS GZ03 / M th November, 2008.
CSC 2405 Computer Systems II Exceptions Mini-Lecture Traps & Interrupts.
Operating Systems Lecture November 2015© Copyright Virtual University of Pakistan 2 Agenda for Today Review of previous lecture Hardware (I/O, memory,
1 CSE451 Architectural Supports for Operating Systems Autumn 2002 Gary Kimura Lecture #2 October 2, 2002.
Operating System Structure A key concept of operating systems is multiprogramming. –Goal of multiprogramming is to efficiently utilize all of the computing.
Exceptional Control Flow Topics Exceptions except1.ppt CS 105 “Tour of the Black Holes of Computing”
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.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Operating Systems Engineering Based on MIT (2012, lec3) Recitation 2: OS Organization.
Concurrency, Processes, and System calls Benefits and issues of concurrency The basic concept of process System calls.
Processes and Virtual Memory
University of Washington Exceptional Control Flow The Hardware/Software Interface CSE351 Winter 2013.
Bootable Programs Building an O/S. Basic Requirements of any O/S Respond to interrupts (all kinds) Preserve user environment Protect users and self from.
I/O Software CS 537 – Introduction to Operating Systems.
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.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Of Privilege, Traps, Interrupts & Exceptions Prof. Sirer CS 316 Cornell University.
Exceptional Control Flow
CS 3214 Computer Systems Lecture 9 Godmar Back.
Exceptional Control Flow
Protection and OS Structure
Protection of System Resources
Exceptional Control Flow
Overview of today’s lecture
CS 3305 System Calls Lecture 7.
Exceptional Control Flow: System Calls, Page Faults etc.
Operating Systems Chapter 5: Input/Output Management
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Lecture Topics: 11/1 General Operating System Concepts Processes
Exceptions Control Flow
CS201 - Lecture 17 Exceptions
Operating Systems Lecture 3.
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment. Phone:
Operating System Concepts
Operating System Concepts
Interrupts and System Calls
Presentation transcript:

1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University

Kernel and User Mode Kernel Mode –When the CPU runs in this mode: It can execute any machine instruction –Examples of privilege instructions: –LMSW, SMSW (load/store Machine Status Word register) –MOV DBn, MOV CRn (move to debug/control registers) –LSL (load stack limit, adjusting stack space available to a process) –HLT (halt the CPU) It can access/modify any location in memory It can access and modify any register in the CPU and any device. –The OS kernel instructions run in kernel mode.

Kernel and User Mode User Mode –When the CPU runs in this mode: The CPU can use a limited set of instructions The CPU can only modify only the sections of memory assigned to the process running the program. The CPU can access only a subset of registers in the CPU and it cannot access registers in devices. There is a limited access to the resources of the computer. –The user processes run in user mode

Kernel and User Mode When the OS boots, it starts in kernel mode. In kernel mode the OS sets up all the interrupt vectors and initializes all the devices. Then it starts the first process and switches to user mode. In user mode it runs all the background system processes (daemons or services). Then it runs the user shell or windows manager.

Kernel and User Mode User programs run in user mode. The programs switch to kernel mode to request OS services (through the system call interface) Also user programs switch to kernel mode when an interrupt is raised. They switch back to user mode when interrupt returns. The interrupt handling code (part of OS) is executed in kernel mode. The interrupt vector can be modified only in kernel mode. Most of the CPU time is spent in User mode (overhead of mode switch and context switch)

UNIX Interfaces The layers of a UNIX system. User Interface

UNIX Kernel

Kernel and User Mode Separation of user/kernel mode is used for: –Security: The OS calls in kernel mode make sure that the user has enough privileges to run that call. – Robustness: If a process that tries to write to an invalid memory location, the OS will kill the program, but the OS continues to run. A crash in the process will not crash the OS. –A bug in user mode causes program to crash, OS still runs. A bug in kernel mode may cause OS and system to crash. –Fairness: OS enforces fair access among user processes.

Interrupts An interrupt is an event that requires immediate attention. In hardware, a device sets the interrupt line to high. When an interrupt is received, the CPU will stop whatever it is doing and it will jump to to the 'interrupt handler' that handles that specific interrupt. After executing the handler, it will return to the same place where the interrupt happened and the program continues. Examples: –move mouse –type key –Network packet

Steps of Handling an Interrupt 1.The CPU saves the Program Counter and registers in execution stack 2.CPU looks up the corresponding interrupt handler in the interrupt vector. 3.CPU jumps to interrupt handler and run it. 4.CPU restores the registers and return back to the place in a program that was previously interrupted. The program continues execution as if nothing happened. 5.In some cases it retries the instruction the instruction that was interrupted (E.g. Virtual memory page fault handlers).

Running with Interrupts Interrupts allow CPU and device to run in parallel without waiting for each other. 1. OS Requests Device Operation (E.g.Write to disk) 2. Device Runs Operation 2. OS does other things in parallel with device. 3. When Operation is complete interrupt OS 4. OS services interrupt and continues

Interrupt Vector It is an array of pointers that point to the different interrupt handlers of the different types of interrupts. Hard Drive Interrupt handler USB Interrupt handler (mouse, kbd) Ethernet Card Interrupt handler Page Fault Interrupt handler

Interrupts and Kernel Mode Interrupt must be handled in kernel mode. Why? –An interrupt handler must read device/CPU registers and execute instructions only available in kernel mode. Interrupt vector can be modified only in kernel mode (security) Interrupt vector initialized during boot time; modified when drivers added to system

Types of Interrupts 1. Device Interrupts generated by Devices when a request is complete or an event that requires CPU attention happens. –The mouse is moved –A key is typed –A network packet arrives. –The hard drive has completed a read/write operation. –A CD has been inserted in the CD drive.

Types of Interrupts 2. Math exceptions generated by the CPU when there is a math error. –Divide by zero 3. Page Faults generated by the MMU (Memory Management Unit) that converts Virtual memory addresses to physical memory addresses –Invalid address: interrupt prompts a SEGV signal to the process –Access to a valid address but there is not page in memory. This causes the CPU to load the page from disk –Invalid permission (I.e. trying to write on a read only page) causes a SEGV signal to the process.

Types of Interrupts 4. Software Interrupt generated by software with a special assembly instruction. This is how a program running in user mode requests operating systems services. int 0x80 (system call number in EAX)

System Calls System Calls is the way user programs request services from the OS System calls use Software Interrupts –Difference from hardware interrupts? Examples of system calls are: –open(filename, mode) –read(file, buffer, size) –write(file, buffer, size) –fork() –execve(cmd, args); System calls is the API of the OS from the user program’s point of view. See /usr/include/sys/syscall.h

Why do we use Software Interrupts for syscalls instead of function calls? Software Interrupts will switch into kernel mode OS services need to run in kernel mode because: –They need privileged instructions –Accessing devices and kernel data structures –They need to enforce the security in kernel mode.

System Calls Only operations that need to be executed by the OS in kernel mode are part of the system calls. Function like sin(x), cos(x) are not system calls. Some C functions like printf(s) run mainly in user mode but eventually call write() when for example the buffer is full and needs to be flushed. Also malloc(size) will run mostly in user mode but eventually it will call sbrk() to extend the heap.

System Calls Libc (the C library) provides wrappers for the system calls that eventually generate the system calls. User Mode: int open(fname, mode) { return syscall(SYS_open, fname, mode); } int syscall(syscall_num, …) { asm(INT); } Kernel Mode: Syscall interrupt handler: Read:… Write:… open: - Get file name and mode - Check if file exists - Verify permissions of file against mode. - Ask disk to Perform operation. Make process wait until operation is complete - return fd (file descriptor) Software Interrupt

System Calls and Interrupts Example 1.The user program calls the write(fd, buff, n) system call to write to disk. 2.The write wrapper in libc generates a software interrupt for the system call. 3.The OS checks the arguments. It verifies that fd is a file descriptor for a file opened in write mode. And also that [buff, buff+n-1] is a valid memory range. If any of the checks fail write return -1 and sets errno to the error value.

System Calls and Interrupts Example 4. The OS tells the hard drive to write the buffer in [buff, buff+n] to disk to the file specified by fd. 5. The OS puts the current process in wait state until the disk operation is complete. Meanwhile, the OS switches to another process. 6. The Disk completes the write operation and generates an interrupt. 7. The interrupt handler puts the process calling write into ready state so this process will be scheduled by the OS in the next chance.