Syscalls, exceptions, and interrupts, oh my!

Similar documents
Anne Bracy CS 3410 Computer Science Cornell University

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University

Traps, Exceptions, System Calls, & Privileged Mode

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. P&H Chapter 4.9, pages , appendix A.

Lec 22: Interrupts. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

Hardware OS & OS- Application interface

Traps, Exceptions, System Calls, & Privileged Mode

Traps, Exceptions, System Calls, & Privileged Mode!

Anne Bracy CS 3410 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Virtual Memory. P & H Chapter 5.4 (up to TLBs) Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University

The Kernel Abstraction

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

Lecture 11: Interrupt and Exception. James C. Hoe Department of ECE Carnegie Mellon University

P & H Chapter 5.7 (up to TLBs) Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

CS 201. Exceptions and Processes. Gerson Robboy Portland State University

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

CS5460: Operating Systems

The Kernel Abstraction. Chapter 2 OSPP Part I

Protection and System Calls. Otto J. Anshus

CSE 153 Design of Operating Systems

Virtual Memory 3. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. P & H Chapter 5.4

Prelim 3 Review. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University

Prelim 3 Review. Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Architectural Support for OS

CSE 153 Design of Operating Systems Fall 18

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

COS 318: Operating Systems. Overview. Prof. Margaret Martonosi Computer Science Department Princeton University

TCSS 422: OPERATING SYSTEMS

Architectural Support for Operating Systems

Architectural Support for Operating Systems. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 550 Operating Systems Spring Interrupt

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Caches (Writing) P & H Chapter 5.2 3, 5.5. Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

RISC Pipeline. Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University. See: P&H Chapter 4.6

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Virtual Memory. CS 3410 Computer System Organization & Programming

CSE 120 Principles of Operating Systems

for Operating Systems Computer Systems Laboratory Sungkyunkwan University

Inf2C - Computer Systems Lecture 16 Exceptions and Processor Management

Lecture 2: Architectural Support for OSes

Architectural Support for Operating Systems

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Sandboxing Untrusted Code: Software-Based Fault Isolation (SFI)

CSC369 Lecture 2. Larry Zhang

Virtual Memory. CS 3410 Computer System Organization & Programming. [K. Bala, A. Bracy, E. Sirer, and H. Weatherspoon]

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

CS333 Intro to Operating Systems. Jonathan Walpole

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

Review: Hardware user/kernel boundary

The UtePC/Yalnix Memory System

John Wawrzynek & Nick Weaver

Dan Noé University of New Hampshire / VeloBit

9/19/18. COS 318: Operating Systems. Overview. Important Times. Hardware of A Typical Computer. Today CPU. I/O bus. Network

Interrupts & System Calls

Computer architecture. A simplified model

CS510 Operating System Foundations. Jonathan Walpole

Machines and Virtualization. Systems and Networks Jeff Chase Spring 2006

CS 5460/6460 Operating Systems

Administrivia. Lab 1 due Friday 12pm. We give will give short extensions to groups that run into trouble. But us:

CS 104 Computer Organization and Design

Architectural Support for Operating Systems. Jinkyu Jeong ( Computer Systems Laboratory Sungkyunkwan University

Caches (Writing) P & H Chapter 5.2 3, 5.5. Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

19: I/O. Mark Handley. Direct Memory Access (DMA)

CSE 120 Principles of Operating Systems

Interrupts and System Calls

W4118: interrupt and system call. Junfeng Yang

Introduction to the Process David E. Culler CS162 Operating Systems and Systems Programming Lecture 2 Sept 3, 2014

COS 318: Operating Systems

CPS104 Computer Organization and Programming Lecture 17: Interrupts and Exceptions. Interrupts Exceptions and Traps. Visualizing an Interrupt

a) Do exercise (5th Edition Patterson & Hennessy). Note: Branches are calculated in the execution stage.

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

CS 550 Operating Systems Spring System Call

Exceptions and Processes

Systems Programming and Computer Architecture ( ) Timothy Roscoe

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 24

Fast access ===> use map to find object. HW == SW ===> map is in HW or SW or combo. Extend range ===> longer, hierarchical names

There are different characteristics for exceptions. They are as follows:

Virtual Memory. 1 Administrivia. Tom Kelliher, CS 240. May. 1, Announcements. Homework, toolboxes due Friday. Assignment.

What are Exceptions? EE 457 Unit 8. Exception Processing. Exception Examples 1. Exceptions What Happens When Things Go Wrong

Memory Protection. Machines and Virtualization. Architectural Foundations of OS Kernels. Memory and the CPU. Introduction to Virtual Addressing

Process Scheduling Queues

Chapter 5 B. Large and Fast: Exploiting Memory Hierarchy

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

Operating Systems and Interrupts/Exceptions

Fast access ===> use map to find object. HW == SW ===> map is in HW or SW or combo. Extend range ===> longer, hierarchical names

POLITECNICO DI MILANO. Exception handling. Donatella Sciuto:

OS Extensibility: SPIN and Exokernels. Robert Grimm New York University

Interrupts Peter Rounce

Main Memory: Address Translation

COS 318: Operating Systems

Virtual Machines & the OS Kernel

Modeling Page Replacement: Stack Algorithms. Design Issues for Paging Systems

Lecture 2 Fundamental OS Concepts. Bo 2018, Spring

Virtual Machines & the OS Kernel

Transcription:

Syscalls, exceptions, and interrupts, oh my! Hakim Weatherspoon CS 3410 Computer Science Cornell University [Altinbuken, Weatherspoon, Bala, Bracy, McKee, and Sirer]

Announcements P4-Buffer Overflow is due tomorrow Due Tuesday, April 16th C practice assignment Due Friday, April 19th Due Friday, April 27th

Outline for Today How do we protect processes from one another? Skype should not crash Chrome. How do we protect the operating system (OS) from other processes? Chrome should not crash the computer! How does the CPU and OS (software) handle exceptional conditions? Division by 0, Page Fault, Syscall, etc. 3

Outline for Today How do we protect processes from one another? Skype should not crash Chrome. Operating System How do we protect the operating system (OS) from other processes? Chrome should not crash the computer! Privileged Mode How does the CPU and OS (software) handle exceptional conditions? Division by 0, Page Fault, Syscall, etc. Traps, System calls, Exceptions, Interrupts 4

Operating System

Operating System Manages all of the software and hardware on the computer. Many processes running at the same time, requiring resources CPU, Memory, Storage, etc. The Operating System multiplexes these resources amongst different processes, and isolates and protects processes from one another! 6

Operating System Operating System (OS) is a trusted mediator: Safe control transfer between processes Isolation (memory, registers) of processes untrusted trusted P1 P2 P3 P4 VM OS MMU CPU filesystem net driver driver disk netw card software hardware 7

Outline for Today How do we protect processes from one another? Skype should not crash Chrome. Operating System How do we protect the operating system (OS) from other processes? Chrome should not crash the computer! Privileged Mode How does the CPU and OS (software) handle exceptional conditions? Division by 0, Page Fault, Syscall, etc. Traps, System calls, Exceptions, Interrupts 8

Privileged (Kernel) Mode

One Brain, Many Personalities You are what you execute. Brain Personalities: hailstone_recursive Microsoft Word Minecraft Linux yes, this is just software like every other program that runs on the CPU Are they all equal? 10

Trusted vs. Untrusted Only trusted processes should access & change important things Editing TLB, Page Tables, OS code, OS sp, OS fp If an untrusted process could change the OS sp/fp/gp/etc., OS would crash! 11

Privileged Mode CPU Mode Bit in Process Status Register Many bits about the current process Mode bit is just one of them Mode bit: 0 = user mode = untrusted: Privileged instructions and registers are disabled by CPU 1 = kernel mode = trusted All instructions and registers are enabled 12

Privileged Mode at Startup 1. Boot sequence load first sector of disk (containing OS code) to predetermined address in memory Mode 1; PC predetermined address 2. OS takes over initializes devices, MMU, timers, etc. loads programs from disk, sets up page tables, etc. Mode 0; PC program entry point - User programs regularly yield control back to OS 13

Users need access to resources If an untrusted process does not have privileges to use system resources, how can it Use the screen to print? Send message on the network? Allocate pages? Schedule processes? 14

System Call Examples putc(): Print character to screen Need to multiplex screen between competing processes send(): Send a packet on the network Need to manipulate the internals of a device sbrk(): Allocate a page Needs to update page tables & MMU sleep(): put current prog to sleep, wake other Need to update page table base register 15

System Calls System calls called executive calls (ecall) in RISC- System call: Not just a function call Don t let process jump just anywhere in OS code OS can t trust process registers (sp, fp, gp, etc.) ECALL instruction: safe transfer of control to OS RISC-V system call convention: Exception handler saves temp regs, saves ra, but: a7 = system call number, which specifies the operation the application is requesting 16

User Application printf() 0xfffffffc 0x80000000 0x7ffffffc SYSCALL! system reserved stack System Call Interface top printf.c Implementation of printf() syscall! User Mode Privileged (Kernel) Mode dynamic data (heap) 0x10000000 static data.data 0x00400000 0x00000000 code (text) system reserved bottom.text 17

Libraries and Wrappers Compilers do not emit SYSCALL instructions Compiler doesn t know OS interface Libraries implement standard API from system API libc (standard C library): getc() ecall sbrk() ecall write() ecall gets() getc() printf() write() malloc() sbrk() 18

Invoking System Calls char *gets(char *buf) { while (...) { buf[i] = getc(); } } int getc() { asm("addi a7, 0, 4"); asm( ecall"); } 19

Anatomy of a Process, v1 0xfffffffc system reserved 0x80000000 0x7ffffffc stack dynamic data (heap) 0x10000000 static data?? 0x00400000 0x00000000 code (text) (user) gets (library) getc system reserved 20

Where does the OS live? In its own address space? Syscall has to switch to a different address space Hard to support syscall arguments passed as pointers... So, NOPE In the same address space as the user process? Protection bits prevent user code from writing kernel Higher part of virtual memory Lower part of physical memory... Yes, this is how we do it. 21

Anatomy of a Process 0xfffffffc system reserved top 0x80000000 0x7ffffffc stack dynamic data (heap) 0x10000000 static data.data 0x00400000 0x00000000 code (text) system reserved.text bottom 22

Full System Layout All kernel text & most data: At same virtual address in every address space OS is omnipresent, available to help user-level applications Typically in high memory 0xfffffffc 0x80000000 0x7ffffffc OS Stack OS Heap OS Data OS Text stack dynamic data (heap) 0x10000000 0x00400000 0x00000000 static data code (text) system reserved 23

Full System Layout 0xfffffffc 0x80000000 0x7ffffffc OS Stack OS Heap OS Data OS Text stack dynamic data (heap) OS Stack 0x10000000 0x00400000 0x00000000 static data code (text) system reserved Virtual Memory 0x00...00 OS Heap OS Data OS Text Physical Memory 24

Anatomy of a Process, v2 0xfffffffc system reserved 0x80000000 0x7ffffffc implementation of getc() syscall stack 0x10000000 0x00400000 0x00000000 dynamic data (heap) static data code (text) system reserved gets getc 25

Inside the ECALL instruction ECALL is s SYSCALL in RISC-V ECALL instruction does an atomic jump to a controlled location (i.e. RISC-V 0x8000 0180) Switches the sp to the kernel stack Saves the old (user) SP value Saves the old (user) PC value (= return address) Saves the old privilege mode Sets the new privilege mode to 1 Sets the new PC to the kernel syscall handler 26

Inside the ECALL implementation Kernel system call handler carries out the desired system call Saves callee-save registers Examines the syscall ecall number Checks arguments for sanity Performs operation Stores result in a0 Restores callee-save registers Performs a supervisor exception return (SRET) instruction, which restores the privilege mode, SP and PC 27

Takeaway It is necessary to have a privileged (kernel) mode to enable the Operating System (OS): provides isolation between processes protects shared resources provides safe control transfer 28

Outline for Today How do we protect processes from one another? Skype should not crash Chrome. Operating System How do we protect the operating system (OS) from other processes? Chrome should not crash the computer! Privileged Mode How does the CPU and OS (software) handle exceptional conditions? Division by 0, Page Fault, Syscall, etc. Traps, System calls, Exceptions, Interrupts 29

Exceptional Control Flow Anything that isn t a user program executing its own user-level instructions. System Calls: just one type of exceptional control flow Process requesting a service from the OS Intentional it s in the executable! 30

Software Exceptions Trap Intentional Examples: System call (OS performs service) Breakpoint traps Privileged instructions Fault Unintentional but Possibly recoverable Examples: Division by zero Page fault Abort Unintentional Not recoverable Examples: Parity error One of many ontology / terminology trees. 31

Hardware support for exceptions SEPC register Supervisor Exception Program Counter or SEPC 32-bit register, holds addr of affected instruction Syscall case: Address of ECALL SCAUSE register Supervisor Exception Cause Register or SCAUSE Register to hold the cause of the exception Syscall case: 8, Sys Special instructions to load TLB Only do-able by kernel 33

Hardware support for exceptions Code Stored in Memory (also, data and stack) memory PC +4 new pc Instruction Fetch inst compute jump/branch targets x0 x1 register file x30 x31 control extend detect hazard Instruction Decode imm B A ctrl SEPC SCAUSE alu forward unit Execute d in addr d out memory Memory Write - Back IF/ID ID/EX EX/MEM MEM/WB B D ctrl Stack, Data, Code Stored in Memory D M ctrl 34

Hardware support for exceptions Precise exceptions: Hardware guarantees (similar to a branch) Previous instructions complete Later instructions are flushed SEPC and SCAUSE register are set Jump to prearranged address in OS When you come back, restart instruction Disable exceptions while responding to one - Otherwise can overwrite SEPC and SCAUSE 35

Exceptional Control Flow AKA Exceptions Hardware interrupts Asynchronous = caused by events external to CPU Software exceptions Synchronous = caused by CPU executing an instruction Maskable Can be turned off by CPU Example: alert from network device that a packet just arrived, clock notifying CPU of clock tick Unmaskable Cannot be ignored Example: alert from the power supply that electricity is about to go out 36

Interrupts & Unanticipated Exceptions No ECALL instruction. Hardware steps in: Saves PC of supervisor exception instruction (SEPC) Saves cause of the interrupt/privilege (Cause register) Switches the sp to the kernel stack Saves the old (user) SP value Saves the old (user) PC value Saves the old privilege mode Sets the new privilege mode to 1 Sets the new PC to the kernel syscall hander interrupt/exception handler SYSCAL 37

Inside Interrupts & Unanticipated Exceptions interrupt/exception handler handles event Kernel system call handler carries out system call all Saves callee-save registers Examines the syscall number cause Checks arguments for sanity Performs operation Stores result in a0 all Restores callee-save registers Performs a SRET instruction (restores the privilege mode, SP and PC) 38

Address Translation: HW/SW Division of Labor Virtual physical address translation! Hardware has a concept of operating in physical or virtual mode helps manage the TLB raises page faults keeps Page Table Base Register (PTBR) and ProcessID Software/OS manages Page Table storage handles Page Faults updates Dirty and Reference bits in the Page Tables keeps TLB valid on context switch: Flush TLB when new process runs (x86) Store process id (MIPS) 39

Demand Paging on RISC-V 1. TLB miss 2. Trap to kernel 3. Walk Page Table 4. Find page is invalid 5. Convert virtual address to file + offset 6. Allocate page frame Evict page if needed 7. Initiate disk block read into page frame 8. Disk interrupt when DMA complete 9. Mark page as valid 10. Load TLB entry 11. Resume process at faulting instruction 12. Execute instruction 40