cs 423
play

CS 423 Operating System Design: The Kernel Abstraction Tian anyin - PowerPoint PPT Presentation

CS 423 Operating System Design: The Kernel Abstraction Tian anyin yin Xu Xu * Thanks for Prof. Adam Bates for the slides. CS423: Operating Systems Design Logistics MP 0 due is postponed to next Tue. C4 Paper Summary submitted to


  1. CS 423 Operating System Design: The Kernel Abstraction Tian anyin yin Xu Xu * Thanks for Prof. Adam Bates for the slides. CS423: Operating Systems Design

  2. Logistics • MP 0 due is postponed to next Tue. • C4 Paper Summary submitted to cs423sp20@gmail.com CS423: Operating Systems Design 2

  3. Let’s do something fun. Let’s start with some questions. CS423: Operating Systems Design 3

  4. Overview Process concept • A process is the OS abstraction for executing a program with limited privileges Dual-mode operation: user vs. kernel • Kernel-mode: execute with complete privileges • User-mode: execute with fewer privileges Safe control transfer • How do we switch from one mode to the other? CS423: Operating Systems Design 4

  5. Process Abstraction Process: an instance of a program that runs with limited rights on the machine • Thread: a sequence of instructions within a process • Potentially many threads per process (for now, assume 1:1) • Address space: set of rights of a process • Memory that the process can access • Other permissions the process has (e.g., which system calls it can make, what files it can access) CS423: Operating Systems Design 5

  6. Thought Experiment How can we permit a process to execute with only limited privileges? CS 423: Operating Systems Design 6

  7. Thought Experiment How can we implement execution with limited privilege? • Execute each program instruction in a simulator • If the instruction is permitted, do the instruction • Otherwise, stop the process • Basic model in Javascript and other interpreted languages CS423: Operating Systems Design 7

  8. Thought Experiment How can we implement execution with limited privilege? • Execute each program instruction in a simulator • If the instruction is permitted, do the instruction • Otherwise, stop the process • Basic model in Javascript and other interpreted languages Ok… but how do we go faster? CS423: Operating Systems Design 8

  9. Thought Experiment How can we implement execution with limited privilege? • Execute each program instruction in a simulator • If the instruction is permitted, do the instruction • Otherwise, stop the process • Basic model in Javascript and other interpreted languages Ok… but how do we go faster? • Run the unprivileged code directly on the CPU! CS423: Operating Systems Design 9

  10. A Model of a CPU CS423: Operating Systems Design 10

  11. A CPU with Dual-Mode Operation CS423: Operating Systems Design 11

  12. HW Support for Dual-Mode Privileged instructions • Available to kernel • Not available to user code Limits on memory accesses • To prevent user code from overwriting the kernel Timer • To regain control from a user program in a loop Safe way to switch from user mode to kernel mode, and vice versa CS423: Operating Systems Design 12

  13. Privileged Instructions Examples? What should happen if a user program attempts to execute a privileged instruction? CS423: Operating Systems Design 13

  14. User->Kernel Switches How/when do we switch from user to kernel mode? 1. Interrupts • Triggered by timer and I/O devices 2. Exceptions • Triggered by unexpected program behavior • Or malicious behavior! 3. System calls (aka protected procedure call) • Request by program for kernel to do some operation on its behalf • Only limited # of very carefully coded entry points CS423: Operating Systems Design 14

  15. Question How does the OS know when a process is in an infinite loop? CS 423: Operating Systems Design 15

  16. Hardware Timer Hardware device that periodically interrupts the processor • Returns control to the kernel handler • Interrupt frequency set by the kernel Not by user code! • Interrupts can be temporarily deferred Not by user code! Interrupt deferral crucial for implementing mutual exclusion CS423: Operating Systems Design 16

  17. Kernel->User Switches How/when do we switch from kernel to user mode? 1. New process/new thread start • Jump to first instruction in program/thread 2. Return from interrupt, exception, system call • Resume suspended execution (return to PC) 3. Process/thread context switch • Resume some other process (return to PC) 4. User-level upcall (UNIX signal) • Asynchronous notification to user program CS423: Operating Systems Design 17

  18. CPU State What is the CPU’s behavior defined by at any given moment? CS 423: Operating Systems Design 18

  19. CPU State What is the CPU’s behavior defined by at any given moment? Code Segment Offset Program Counter Program instructions CS 423: Operating Systems Design 19

  20. CPU State What is the CPU’s behavior defined by at any given moment? Code Data Segment Segment Offset Program Counter Offset OpCode Operand Current Instruction Data Operand Program instructions Heap CS 423: Operating Systems Design 20

  21. CPU State What is the CPU’s behavior defined by at any given moment? Code Data Stack Segment Segment Segment Offset Offset Program Counter Offset Stack Pointer OpCode Operand Current Instruction Data Operand Program instructions Heap Stack CS 423: Operating Systems Design 21

  22. CPU State What is the CPU’s behavior defined by at any given moment? Registers Code Data Stack Segment Segment Segment Offset Offset Program Counter Offset Stack Pointer OpCode Operand Current Instruction Data Operand Program instructions Heap Stack CS 423: Operating Systems Design 22

  23. CPU State What defines the STATE of the CPU? Registers Code Data Stack Segment Segment Segment Offset Offset Program Counter Offset Stack Pointer OpCode Operand Current Instruction Data Operand Program instructions Heap Stack CS 423: Operating Systems Design 23

  24. What’s a ‘real’ CPU? What’s the STATE of a real CPU? Registers Code Data Stack Segment Segment Segment Offset Offset Program Counter Offset Stack Pointer OpCode Operand Current Instruction Data Operand Program instructions Heap Stack CS 423: Operating Systems Design 24

  25. The Context Switch Registers Load State (Context) Code Data Stack Segment Segment Segment Offset Program Counter Stack OpCode Operand Pointer Data Registers Operand Program instructions Heap Stack Code Data Stack Segment Segment Segment Offset Program Counter Save State Stack OpCode Operand Pointer (Context) Data Operand Program instructions Heap Stack CS 423: Operating Systems Design 25

  26. Process Control Block The state for processes that are not running on the CPU are maintained in the Process Control Block (PCB) data structure Updated during context switch An alternate PCB diagram CS 423: Operating Systems Design 26

  27. The Context Switch Registers Load State (Context) Code Data Stack Segment Segment Segment Offset Program Counter Stack OpCode Operand Pointer Data Registers Operand Program instructions Heap Stack Code Data Stack Segment Segment Segment Offset Program Counter Save State Stack OpCode Operand Pointer (Context) Data Operand Program instructions Heap Stack CS 423: Operating Systems Design 27

  28. The Context Switch Note: In thread context Data switches, heap is not switched! Segment Registers Load State (Context) Code Stack Segment Segment Offset Program Data Counter Operand Heap Stack OpCode Operand Pointer Registers Program instructions Stack Code Stack Segment Segment Offset Program Counter Save State Stack OpCode Operand Pointer (Context) Program instructions Stack CS 423: Operating Systems Design 28

  29. The Context Switch Note: In thread context Data switches, heap is not switched! Segment Registers Load State (Context) Code Stack Segment Segment Offset Program Data Counter Operand Heap Stack OpCode Operand Pointer Registers Program instructions Stack Code Stack Segment Segment Offset Program Counter Save State Stack OpCode Operand Pointer (Context) Program instructions Stack CS 423: Operating Systems Design 29

  30. Thread Context Switch Note: In thread context Data switches, heap is not switched! Segment Registers Load State (Context) Code Stack Segment Segment Offset Program Data Counter Operand Heap Stack OpCode Operand Pointer So who does the Registers Program instructions context switch, Stack Code Stack and when??? Segment Segment Offset Program Counter Save State Stack OpCode Operand Pointer (Context) Program instructions Stack CS 423: Operating Systems Design 30

  31. Thread Context Switch Note: In thread context Data switches, heap is not switched! Segment Registers Load State (Context) Code Stack Segment Segment Offset Program Data Counter Operand Heap Stack OpCode Operand Pointer Solution 1: Registers An Interrupt Program instructions Stack Code Stack Segment Segment Offset Program Counter Save State Stack OpCode Operand Pointer (Context) Program instructions Stack CS 423: Operating Systems Design 31

  32. CTX Switch: Interrupt Running Thread Registers Registers Code Code Stack Stack Segment Segment Segment Segment Offset Offset Program Program Counter Counter Stack Stack Pointer Pointer Program instructions Stack Program instructions Stack CS 423: Operating Systems Design 32

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend