TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

TCSS 422: OPERATING SYSTEMS OBJECTIVES – 4/7

 Questions from 4/2  Chapter 4: Processes INTRODUCTION TO . Kernel data structures for processes and threads OPERATING SYSTEMS,  Chapter 5: API PROCESSES . fork(), wait(), exec()  Assignment 0  Chapter 6: Limited Direct Execution Wes J. Lloyd . Direct execution School of Engineering and Technology . Limited direct execution University of Washington - Tacoma . CPU modes . System calls and traps . Cooperative multi-tasking . Context switching and preemptive multi-tasking TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 April 7, 2020 L3.2 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

1 2

MATERIAL / PACE FEEDBACK FROM 4/2

 Please classify your perspective on material covered in  On Chapter 2: today’s class (60 respondents):  “I feel like I got lost at points but I can't drill down  1-mostly review, 5-equal new/review, 10-mostly new exactly what the issue was or what I was missing to fully understand what you were showing us. I just recall you  Average – 7.03 ( from 5.83) saying this was an intro instead of a deep dive into the material”  Please rate the pace of today’s class:  YES! Chapter 2 provides a broad survey of the “three easy  1-slow, 5-just right, 10-fast pieces” all in one lecture. We will revisit everything in  Average – 5.76 ( from 5.17) more detail as we go along…  I also introduced “context switches” ahead of time with the mem.c example. We’ll go over this again today in Ch. 6

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.3 April 7, 2020 L3.4 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

3 4

FEEDBACK - 2 FEEDBACK - 3

 Processes have their own address space (i.e. memory),  The overall process of concurrency is still a bit unclear to me. threads do not. A process crashing won't affect another process.  Concurrency simply means doing multiple things at the same time on a computer. . i.e. Chrome tabs are processes, and a crashed tab does not crash the entire browser  Today’s computer have multiple processing  ** Are there any other differences between threads and cores processes? (i.e. CPU cores)   Linux threads have a smaller data structure than processes  Each core can perform a different independent   Threads are owned by processes task “concurrently”   Creating a new is faster than creating a new process at the same time   Threads share memory, and can communicate through RAM  Check how many CPU   Inter-process communication involves opening a file or cores in Ubuntu with network stream (pipe) between processes “lscpu” (covered in TCSS 430) TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.5 April 7, 2020 L3.6 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

5 6

Slides by Wes J. Lloyd L3.1 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

FEEDBACK - 4 FEEDBACK - 5

 Why is it best for the CPU to perform a context  Another student suggested that a single program switch when a thread is blocked? that didn't have any concurrency would be able to run without an OS, however I am having trouble Deschedule  Which states is the Running d Ready imagining a computer without an OS. process (job) idle? Scheduled  In a sense, sort of, yes  What are the I/O: initiate I/O: done  We talk about DIRECT EXECUTION today as part of consequences of suspending an Chapter 6, which is a bit like a computer without

idle process? Blocked an OS.  a running process?

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.7 April 7, 2020 L3.8 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

7 8

MOTIVATION FOR LINUX MOTIVATION FOR LINUX - 2

 It is worth noting the importance of Linux for today’s  Consider an example where you’re asked to develop a web developers and computer scientists. services backend that requires 10 x 8-CPU-core virtual servers  The CLOUD runs many virtual machines, recently in 2019 a key  Your organization investigates hosting costs on Amazon cloud milestone was reached.  8-core VM is “c5d.2xlarge”  Even on Microsoft Azure (the Microsoft Cloud), there were more Linux Virtual Machines (> 50%) than Windows.  https://www.zdnet.com/article/microsoft-developer-reveals- linux-is-now-more-used-on-azure-than-windows-server/  https://www.zdnet.com/article/it-runs-on-the-cloud-and-the- cloud-runs-on-linux-any-questions/

 The majority of application back-ends (server-side), cloud or  Windows hourly price 75.2 not, run on Linux.  Linux hourly price 38.4  This is due to licensing costs, example:  See: https://www.ec2instances.info/

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.9 April 7, 2020 L3.10 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

9 10

“QUIZ” 0 – C PROGRAMMING MOTIVATION FOR LINUX - 2 BACKGROUND SURVEY

 Consider an example where you’re asked to develop a web  Available via Canvas System servicesOne year backend cloud that hosting requires cost:10 x 8-CPU-core virtual servers  Your organization investigates hosting costs on Amazon cloud  8-coreWINDOWS VM is “c5d.2xlarge”  Under: 10 VMs x 8,760 hours x $.752 = $65,875.20 Assignments  Tutorials/Quizzes/In-class Activities

Linux  Please disregard grade assigned by Canvas 10 VMs x 8,760 hours x $.384 = $33,638.40  All submissions will receive 10 pts after assignment  WindowsWindows hourly comes price 75.2 at a 95.8% price premium  Linux hourly price 38.4 closes - (closes Thursday 4/9 @ 11:59p)  See: https://www.ec2instances.info/

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.11 April 7, 2020 L3.12 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

11 12

Slides by Wes J. Lloyd L3.2 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

VIRTUAL MACHINE SURVEY OBJECTIVES – 4/7

 Virtual Machine Survey  Questions from 4/2  Chapter 4: Processes  Request for Ubuntu 18.04 VMs has been sent to . Kernel data structures for processes and threads the School of Engineering and Technology LABS  Chapter 5: Process API  Expect response soon regarding connection . fork(), wait(), exec() information  Assignment 0  Chapter 6: Limited Direct Execution . Direct execution  Thank you! . Limited direct execution . CPU modes . System calls and traps . Cooperative multi-tasking . Context switching and preemptive multi-tasking TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.13 April 7, 2020 L3.14 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

13 14

PROCESS DATA STRUCTURES

 OS provides data structures to track process information . Process list . Process Data . State of process: Ready, Blocked, Running CHAPTER 4: . Register context

PROCESSES  PCB (Process Control Block) . A C-structure that contains information about each process

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 April 7, 2020 L3.16 School of Engineering and Technology, University of Washington - Tacoma L3.15 School of Engineering and Technology, University of Washington - Tacoma

15 16

XV6 KERNEL DATA STRUCTURES XV6 KERNEL DATA STRUCTURES - 2

 xv6: pedagogical implementation of Linux // the information xv6 tracks about each process  Simplified structures // including its register context and state struct proc { // the registers xv6 will save and restore char *mem; // Start of process memory // to stop and subsequently restart a process uint sz; // Size of process memory struct context { char *kstack; // Bottom of kernel stack int eip; // Index pointer register // for this process int esp; // Stack pointer register enum proc_state state; // Process state int ebx; // Called the base register int pid; // Process ID int ecx; // Called the counter register struct proc *parent; // Parent process int edx; // Called the data register void *chan; // If non-zero, sleeping on chan int esi; // Source index register int killed; // If non-zero, have been killed int edi; // Destination index register struct file *ofile[NOFILE]; // Open files int ebp; // Stack base pointer register struct inode *cwd; // Current directory }; struct context context; // Switch here to run process struct trapframe *tf; // Trap frame for the // the different states a process can be in // current enum proc_state { UNUSED, EMBRYO, SLEEPING, }; RUNNABLE, RUNNING, ZOMBIE };

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.17 April 7, 2020 L3.18 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

17 18

Slides by Wes J. Lloyd L3.3 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

LINUX: STRUCTURES LINUX: THREAD_INFO

 struct task_struct, equivalent to struct proc struct thread_info { struct task_struct *task; /* main task structure */ . Provides process description struct exec_domain *exec_domain; /* execution domain */ __u32 flags; /* low level flags */ . Large: 10,000+ bytes __u32 status; /* thread synchronous flags */ . /usr/src/linux-headers-{kernel version}/include/linux/sched.h __u32 cpu; /* current CPU */ int preempt_count; /* 0 => preemptable, . ~ LOC 1391 – 1852 (4.4.0-170) <0 => BUG */ earlier was LOC 1227 – 1587 mm_segment_t addr_limit; struct restart_block restart_block; void __user *sysenter_return; #ifdef CONFIG_X86_32  struct thread_info, provides “context” unsigned long previous_esp; /* ESP of the previous stack in case of nested (IRQ) stacks . thread_info.h is at: */ __u8 supervisor_stack[0]; /usr/src/linux-headers-{kernel version} /arch/x86/include/asm/ #endif int uaccess_err; };

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.19 April 7, 2020 L3.20 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

19 20

LINUX STRUCTURES - 2

 List of Linux data structures: http://www.tldp.org/LDP/tlk/ds/ds.html

 Description of process data structures: https://learning.oreilly.com/library/view/linux-kernel- development/9780768696974/cover.html CHAPTER 5: 3rd edition is online (dated from 2010): See chapter 3 on Process Management C PROCESS API

Safari online – accessible using UW ID SSO login Linux Kernel Development, 3rd edition Robert Love Addison-Wesley

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.21 April 7, 2020 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma L3.22

21 22

OBJECTIVES – 4/7 fork()

 Questions from 4/2  Creates a new process - think of “a fork in the road”  Chapter 4: Processes  “Parent” process is the original . Kernel data structures for processes and threads  Creates “child” process of the program from the current  Chapter 5: Process API execution point . fork(), wait(), exec()  Book says “pretty odd”  Assignment 0  Creates a duplicate program instance (these are processes!)  Chapter 6: Limited Direct Execution  Copy of . Address space (memory) . Direct execution . Register . Limited direct execution . Program Counter (PC) . CPU modes  Fork returns . System calls and traps . child PID to parent . Cooperative multi-tasking . 0 to child . Context switching and preemptive multi-tasking TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.23 April 7, 2020 L3.24 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

23 24

Slides by Wes J. Lloyd L3.4 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

FORK EXAMPLE FORK EXAMPLE - 2

 p1.c  Non deterministic ordering of execution

#include prompt> ./p1 #include hello world (pid:29146) #include hello, I am parent of 29147 (pid:29146) hello, I am child (pid:29147) int main(int argc, char *argv[]){ prompt> printf("hello world (pid:%d)\n", (int) getpid()); int rc = fork(); if (rc < 0) { // fork failed; exit or fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { // child (new process) prompt> ./p1 printf("hello, I am child (pid:%d)\n", (int) getpid()); hello world (pid:29146) } else { // parent goes down this path (main) hello, I am child (pid:29147) printf("hello, I am parent of %d (pid:%d)\n", hello, I am parent of 29147 (pid:29146) rc, (int) getpid()); prompt> } return 0; }  CPU scheduler determines which to run first

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.25 April 7, 2020 L3.26 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

25 26

:(){ :|: & };: wait()

 wait(), waitpid()  Called by parent process  Waits for a child process to finish executing  Not a sleep() function  Provides some ordering to multi-process execution

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.27 April 7, 2020 L3.28 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

27 28

FORK WITH WAIT FORK WITH WAIT - 2

 Deterministic ordering of execution #include #include #include prompt> ./p2 #include hello world (pid:29266) hello, I am child (pid:29267) int main(int argc, char *argv[]){ hello, I am parent of 29267 (wc:29267) (pid:29266) printf("hello world (pid:%d)\n", (int) getpid()); prompt> int rc = fork(); if (rc < 0) { // fork failed; exit fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { // child (new process) printf("hello, I am child (pid:%d)\n", (int) getpid()); } else { // parent goes down this path (main) int wc = wait(NULL); printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", rc, wc, (int) getpid()); } return 0; }

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.29 April 7, 2020 L3.30 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

29 30

Slides by Wes J. Lloyd L3.5 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

FORK EXAMPLE exec()

 Linux example  Supports running an external program  6 types: execl(), execlp(), execle(), execv(), execvp(), execvpe()

 execl(), execlp(), execle(): const char *arg (example: execl.c) Provide cmd and args as individual params to the function Each arg is a pointer to a null-terminated string ODD: pass a variable number of args: (arg0, arg1, .. argn)

 Execv(), execvp(), execvpe() (example: exec.c) Provide cmd and args as an Array of pointers to strings Strings are null-terminated First argument is name of command being executed Fixed number of args passed in

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.31 April 7, 2020 L3.32 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

31 32

EXEC() - 2 EXEC EXAMPLE

 Common use case: #include  Write a new program which wraps a legacy one #include #include  Provide a new interface to an old system: Web services #include  Legacy program thought of as a “black box” #include int main(int argc, char *argv[]){ printf("hello world (pid:%d)\n", (int) getpid()); int rc = fork();  We don’t want to know what is inside… if (rc < 0) { // fork failed; exit fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { // child (new process) printf("hello, I am child (pid:%d)\n", (int) getpid()); char *myargs[3]; myargs[0] = strdup("wc"); // program: "wc" (word count) myargs[1] = strdup("p3.c"); // argument: file to count myargs[2] = NULL; // marks end of array …

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.33 April 7, 2020 L3.34 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

33 34

EXEC EXAMPLE - 2 EXEC WITH FILE REDIRECTION (OUTPUT)

… execvp(myargs[0], myargs); // runs word count printf("this shouldn’t print out"); #include } else { // parent goes down this path (main) #include int wc = wait(NULL); #include printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", #include rc, wc, (int) getpid()); #include } #include return 0; } int main(int argc, char *argv[]){ int rc = fork(); if (rc < 0) { // fork failed; exit fprintf(stderr, "fork failed\n"); exit(1); prompt> ./p3 } else if (rc == 0) { // child: redirect standard output to a file hello world (pid:29383) close(STDOUT_FILENO); hello, I am child (pid:29384) open("./p4.output", O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU); 29 107 1030 p3.c … hello, I am parent of 29384 (wc:29384) (pid:29383) prompt>

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.35 April 7, 2020 L3.36 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

35 36

Slides by Wes J. Lloyd L3.6 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

FILE MODE BITS EXEC W/ FILE REDIRECTION (OUTPUT) - 2

S_IRWXU read, write, execute/search by owner S_IRUSR read permission, owner … S_IWUSR // now exec "wc"... write permission, owner char *myargs[3]; S_IXUSR myargs[0] = strdup("wc"); // program: "wc" (word count) execute/search permission, owner myargs[1] = strdup("p4.c"); // argument: file to count S_IRWXG myargs[2] = NULL; // marks end of array read, write, execute/search by group execvp(myargs[0], myargs); // runs word count S_IRGRP } else { // parent goes down this path (main) int wc = wait(NULL); read permission, group } S_IWGRP return 0; write permission, group } S_IXGRP execute/search permission, group S_IRWXO read, write, execute/search by others S_IROTH prompt> ./p4 read permission, others prompt> cat p4.output S_IWOTH 32 109 846 p4.c write permission, others prompt>

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.37 April 7, 2020 L3.38 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

37 38

QUESTION: PROCESS API

 Which Process API call is used to launch a different program from the current program?

 (a) Fork()  (b) Exec()  (c) Wait()  (d) None of the above  (e) All of the above

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 April 7, 2020 L3.40 School of Engineering and Technology, University of Washington - Tacoma L3.39 School of Engineering and Technology, University of Washington - Tacoma

39 40

OBJECTIVES – 4/7

 Questions from 4/2  Chapter 4: Processes . Kernel data structures for processes and threads  Chapter 5: Process API . fork(), wait(), exec() TCSS 422 WILL RETURN  Assignment 0  Chapter 6: Limited Direct Execution AT 2:40PM . Direct execution . Limited direct execution . CPU modes . System calls and traps . Cooperative multi-tasking . Context switching and preemptive multi-tasking TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 April 7, 2020 L3.42 School of Engineering and Technology, University of Washington - Tacoma L1.41 School of Engineering and Technology, University of Washington - Tacoma

41 42

Slides by Wes J. Lloyd L3.7 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

OBJECTIVES – 4/7

 Questions from 4/2  Chapter 4: Processes . Kernel data structures for processes and threads  Chapter 5: Process API CH. 6: . fork(), wait(), exec()  Assignment 0 LIMITED DIRECT  Chapter 6: Limited Direct Execution EXECUTION . Direct execution . Limited direct execution . CPU modes . System calls and traps . Cooperative multi-tasking . Context switching and preemptive multi-tasking TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 April 7, 2020 L3.44 School of Engineering and Technology, University of Washington - Tacoma L3.43 School of Engineering and Technology, University of Washington - Tacoma

43 44

COMPUTER BOOT SEQUENCE: VIRTUALIZING THE CPU OS WITH DIRECT EXECUTION

 How does the CPU support running so many jobs  What if programs could directly control the CPU / system? simultaneously? OS Program  Time Sharing 1. Create entry for process list 2. Allocate memory for  Tradeoffs: program . Performance 3. Load program into memory . Excessive overhead 4. Set up stack with argc / argv . Control 5. Clear registers 7. Run main() . Fairness 6. Execute call main() 8. Execute return from main() . Security

 Both HW and OS support 9. Free memory of process is used 10. Remove from process list

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.45 April 7, 2020 L3.46 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

45 46

COMPUTER BOOT SEQUENCE: DIRECT EXECUTION - 2 OS WITH DIRECT EXECUTION

 What if programs could directly control the CPU / system?  With direct execution:

OS Program How does the OS stop a program from running, and switch 1. Create entry for process list to another to support time sharing? 2. Allocate memory for program Without limits on running programs, How do programs share disks and perform I/O if they are 3. Load programthe OSinto wouldn’t memory be in control of anything 4. Set up stack withand argc would/ “just be a library” given direct control? Do they know about each other? argv 5. Clear registers 7. Run main() With direct execution, how can dynamic memory structures 6. Execute call main() 8. Execute return from main() such as linked lists grow over time?

9. Free memory of process 10. Remove from process list

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.47 April 7, 2020 L3.48 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

47 48

Slides by Wes J. Lloyd L3.8 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

CONTROL TRADEOFF CONTEXT SWITCHING OVERHEAD

 Too little control: . No security . No time sharing

 Too much control: . Too much OS overhead . Poor performance for compute & I/O Overhead . Complex (system calls), difficult to use

Time

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.49 April 7, 2020 L3.50 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

49 50

LIMITED DIRECT EXECUTION CPU MODES

 OS implements LDE to support time/resource sharing  Utilize CPU Privilege Rings (Intel x86) . rings 0 (kernel), 1 (VM kernel), 2 (unused), 3 (user)  Limited direct execution means “only limited” processes access no access can execute DIRECTLY on the CPU in trusted mode  User mode:  TRUSTED means the process is trusted, and it can do Application is running, but w/o direct I/O access anything… (e.g. it is a system / kernel level process)  Kernel mode:  Enabled by protected (safe) control transfer OS kernel is running performing restricted operations

 CPU supported

 Provides data isolation

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.51 April 7, 2020 L3.52 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

51 52

CPU MODES SYSTEM CALLS

 User mode: ring 3 - untrusted  Implement restricted “OS” operations . Some instructions and registers are disabled by the CPU  Kernel exposes key functions through an API: . Exception registers . Device I/O (e.g. file I/O) . HALT instruction . Task swapping: context switching between processes . MMU instructions . /allocation: malloc() . OS memory access . Creating/destroying processes . I/O device access

 Kernel mode: ring 0 – trusted . All instructions and registers enabled

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.53 April 7, 2020 L3.54 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

53 54

Slides by Wes J. Lloyd L3.9 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

TRAPS: EXCEPTION TYPES SYSTEM CALLS, EXCEPTIONS,

Synchronous vs. User request vs. User maskable vs. Within vs. between Exception type Resume vs. terminate asynchronous coerced nonmaskable instructions

 Trap: any transfer to kernel mode I/O device request Asynchronous Coerced Nonmaskable Between Resume

Invoke Synchronous User request Nonmaskable Between Resume

 Three kinds of traps Tracing instruction execution Synchronous User request User maskable Between Resume . : (planned) user  kernel Breakpoint Synchronous User request User maskable Between Resume

. SYSCALL for I/O, etc. Integer arithmetic overflow Synchronous Coerced User maskable Within Resume

Floating-point arithmetic overflow Synchronous Coerced User maskable Within Resume . Exception: (error) user  kernel or underflow . Div by zero, page fault, page protection error Page fault Synchronous Coerced Nonmaskable Within Resume Misaligned memory accesses Synchronous Coerced User maskable Within Resume

. Interrupt: (event) user  kernel violation Synchronous Coerced Nonmaskable Within Resume . Non-maskable vs. maskable Using undefined instruction Synchronous Coerced Nonmaskable Within Terminate

. Keyboard event, network packet arrival, timer ticks Hardware malfunction Asynchronous Coerced Nonmaskable Within Terminate . Memory parity error (ECC), hard drive failure Power failure Asynchronous Coerced Nonmaskable Within Terminate

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.55 April 7, 2020 L3.56 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

55 56

Computer BOOT Sequence: OS with Limited Direct Execution

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 April 7, 2020 School of Engineering and Technology, University of Washington - Tacoma L3.57 School of Engineering and Technology, University of Washington - Tacoma L3.58

57 58

MULTITASKING MULTITASKING

 How/when should the OS regain control of the CPU to  How/when should the OS regain control of the CPU to switch between processes? switch between processes?

(mostly pre 32-bit)  Cooperative multitasking (mostly pre 32-bit) . < Windows 95, Mac OSX . < WindowsA process 95, Macgets OSX stuck in an infinite loop. . Opportunistic: running programs must give up control . Opportunistic: running programs must give up control  Reboot the machine . User programs must call a special yield system call . User programs must call a special yield system call . When performing I/O . When performing I/O . Illegal operations . Illegal operations

. (POLLEV) . (POLLEV) What problems could you for see with this approach? What problems could you for see with this approach?

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.59 April 7, 2020 L3.60 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

59 60

Slides by Wes J. Lloyd L3.10 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

QUESTION: MULTITASKING

 What problems exist for regaining the control of the CPU with cooperative multitasking OSes?

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 April 7, 2020 L3.62 School of Engineering and Technology, University of Washington - Tacoma L3.61 School of Engineering and Technology, University of Washington - Tacoma

61 62

MULTITASKING - 2 MULTITASKING - 2

 Preemptive multitasking (32 & 64 bit OSes)  Preemptive multitasking (32 & 64 bit OSes)  >= Mac OSX, Windows 95+  >= Mac OSX, Windows 95+

 Timer interrupt  Timer interrupt . Raised at some regular interval (in ms) . RaisedA attimer some interrupt regular interval gives OS (in the ms) ability to run again on a CPU. . Interrupt handling . Interrupt handling 1. Current program is halted 1. Current program is halted 2. Program states are saved 2. Program states are saved 3. OS Interrupt handler is run (kernel mode) 3. OS Interrupt handler is run (kernel mode)

 (PollEV) What is a good interval for the timer interrupt?  (PollEV) What is a good interval for the timer interrupt?

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.63 April 7, 2020 L3.64 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

63 64

QUESTION: TIME SLICE

 For an OS that uses a system timer to force arbitrary context switches to share the CPU, what is a good value (in seconds) for the timer interrupt?

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 April 7, 2020 L3.66 School of Engineering and Technology, University of Washington - Tacoma L3.65 School of Engineering and Technology, University of Washington - Tacoma

65 66

Slides by Wes J. Lloyd L3.11 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

CONTEXT SWITCH CONTEXT SWITCH - 2

 Preemptive multitasking initiates “trap” 1. Save register values of the current process to its kernel into the OS code to determine: stack . General purpose registers  Whether to continue running the current process, . PC: program counter (instruction pointer) or switch to a different one. . kernel stack pointer

 If the decision is made to switch, the OS performs a context switch swapping out the current process for a new one. 2. Restore soon-to-be-executing process from its kernel stack 3. Switch to the kernel stack for the soon-to-be-executing process

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.67 April 7, 2020 L3.68 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

67 68

Context Switch

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 April 7, 2020 School of Engineering and Technology, University of Washington - Tacoma L3.69 School of Engineering and Technology, University of Washington - Tacoma L3.70

69 70

INTERRUPTED INTERRUPTS PREEMPTIVE KERNEL

 What happens if during an interrupt (trap to kernel  Use “locks” as markers of regions of non- mode), another interrupt occurs? preemptibility (non-maskable interrupt)

 Linux  counter (preempt_count) . < 2.6 kernel: non-preemptive kernel . begins at zero . >= 2.6 kernel: preemptive kernel . increments for each lock acquired (not safe to preempt) . decrements when locks are released

 Interrupt can be interrupted when preempt_count=0 . It is safe to preempt (maskable interrupt) . the interrupt is more important

TCSS422: Operating Systems [Spring 2020] TCSS422: Operating Systems [Spring 2020] April 7, 2020 L3.71 April 7, 2020 L3.72 School of Engineering and Technology, University of Washington - Tacoma School of Engineering and Technology, University of Washington - Tacoma

71 72

Slides by Wes J. Lloyd L3.12 TCSS 422 A – Spring 2020 4/7/2020 School of Engineering and Technology

QUESTIONS

73

Slides by Wes J. Lloyd L3.13