Administrivia 1. markem/cs333/ 2. Staff 3. Prerequisites 4. Grading Course Objectives 1. Theory and application 2. Benefits 3. Labs TAs Overview 1. What is a computer system? CPU PC ALU System bus Memory bus Bus interface I/O bridge Main memory USB Graphics adapter I/O bus Disk other devices such as network adapters Mouse Keyboard Disk hello executable stored on disk Figure 1: Hardware Model 2. Set of services system calls 3. Privileged program 1
4. Hardware arbitrator 5. Manager of hardware and privileged software operations 6. Abstract virtual machine that provides several useful additional abstractions 1. CPU Instruction sets PC: program counter; IP: instruction pointer IP: instruction register ALU general registers privileged register SP: stack pointer PSW: processor status work; FLAGS: flags register fetch decode execute interrupt 2. Boot process 3. First process 4. Creating new processes 5. Idealized Model Figure 2: Process State Transition Reference Model. 2
6. Executing new programs 7. OS as resource manager How does OS gain control? Can user processes game the system? 8. Traps v. Interrupts Involuntary Interrupt Voluntary Yield interrupt is asynchronous associated with external events time sensitive timer interrupt trap is a synchronous primarily used to access resources within the kernel mode change request for kernel service 9. Context switch Virtualization and Abstraction 1. Isolation is important 2. Virtual processor(s) xv6 has more than one physical 3. VM address space 4. Process abstraction Own processor and address space Cannot see other processes (normally) 5. Memory hierarchy Speed v. cost Caching: MMU/TLB, buffer cache, I/O controller, device (disk) 3
Smaller, faster, and costlier (per byte) storage devices Larger, slower, and cheaper (per byte) storage devices L5: L6: L4: L3: L2: L1: L0: Regs L1 cache (SRAM) L2 cache (SRAM) L3 cache (SRAM) Main memory (DRAM) Local secondary storage (local disks) Remote secondary storage CPU registers hold words retrieved from cache memory. L1 cache holds cache lines retrieved from the L2 cache. L2 cache holds cache lines retrieved from L3 cache L3 cache holds cache lines retrieved from memory. Main memory holds disk blocks retrieved from local disks. retrieved from disks on remote network servers. Figure 3: Memory Hierarchy Issues data consistency fair allocation latency hiding concurrent access by multiple CPUs 6. Other memory issues Application relocation in phys mem Protection 7. VM v. phys mem idea: hide difference from program virtual address: a new abstraction! base and bounds registers (segmentation) paged VM. Non contiguous at small scale 8. I/O device abstraction byte stream buffer often convenient memory, local disks, remote storage more or less the same to program 4
The Process 1. A program in execution 2. Memory (program instructions, static and dynamic data values) 3. CPU state (registers, PC, SP, etc.) 4. Operating system state (open files, accounting statistics, etc.) Process Address Space 1. Unique to each process 2. Text, Data, Stack(and others that we ll ignore) 3. Each process has its own not per program! Context Switch 1. process context state 2. context switch to scheduler primary task: select next process to execute 3. select next process 4. context switch to next process Process v. Thread 1. fork() and exec() key differences 2. container 3. always at least one thread of control 4. process creation heavyweight 5. thread creation lightweight 5
xv6 Overview Process State Machine The idealized model captures the lifecycle of a process once it is ready to run in the system. An implementation model will have to account for all other possible stated in the system. 1. XV6 Model Figure 4: XV6 Process State Transition Model 2. Syscall Interface trap 0x40 (int 64) Return a code required; often indicates SUCCESS or FAILURE. User mode code must check return code. Only exception is exec() which only returns on failure. All arguments are passed on the stack. e.g., x86 not x86 64 style. Kernel helper functions: argint(), argptr(), argstr() How to Boot Assume that our project directory is markem/xv6. 1. Normal $ make qemu nox qemu system i386 nographic hdb f s. img xv6. img smp 2 m 512 xv6... 6
cpu1 : s t a r t i n g cpu0 : s t a r t i n g sb : s i z e 1000 nblocks 941 ninodes 200 nlog 30 l o g s t a r t 2 i n o d e s t a r t 32 i n i t : s t a r t i n g sh $ 2. Debug requires 2 windows First Window $ make qemu nox gdb dd i f =/dev/ zero o f=xv6. img count =10000 10000+0 r e c o r d s in 10000+0 r e c o r d s out 5120000 bytes ( 5. 1 MB) copied, 0.0167251 s, 306 MB/ s dd i f=bootblock o f=xv6. img conv=notrunc 1+0 r e c o r d s in 1+0 r e c o r d s out 512 bytes (512 B) copied, 0.0158223 s, 32.4 kb/ s dd i f=k e r n e l o f=xv6. img seek=1 conv=notrunc 353+1 r e c o r d s in 353+1 r e c o r d s out 181005 bytes (181 kb) copied, 0.00314437 s, 57.6 MB/ s sed s / l o c a l h o s t :1234/ l o c a l h o s t :27377/ <. g d b i n i t. tmpl >. g d b i n i t Now run gdb. qemu system i386 nographic hdb f s. img xv6. img smp 2 m 512 S gdb tcp : : 2 7 3 7 7 Second Window $ gdb GNU gdb ( Ubuntu 7.7.1 0 ubuntu5 1 4. 0 4. 2 ) 7. 7. 1 Copyright (C) 2014 Free Software Foundation, Inc. L i c e n s e GPLv3+: GNU GPL v e r s i o n 3 or l a t e r <http : / / gnu. org / l i c e n s e s / gp This i s f r e e s o f t w a r e : you are f r e e to change and r e d i s t r i b u t e i t. There i s NO WARRANTY, to the extent permitted by law. Type show copying and show warranty f o r d e t a i l s. This GDB was c o n f i g u r e d as x86 64 linux gnu. Type show c o n f i g u r a t i o n f o r c o n f i g u r a t i o n d e t a i l s. For bug r e p o r t i n g i n s t r u c t i o n s, p l e a s e s e e : <http : / /www. gnu. org / s o f t w a r e /gdb/ bugs />. 7
Find the GDB manual and other documentation r e s o u r c e s o n l i n e at : <http : / /www. gnu. org / s o f t w a r e /gdb/ documentation />. For help, type help. Type apropos word to search f o r commands r e l a t e d to word. warning : F i l e /u/markem/xv6/. g d b i n i t auto l o a d i n g has been d e c l i n e d b To enable e x e c u t i o n o f t h i s f i l e add add auto load s a f e path /u/markem/xv6/. g d b i n i t l i n e to your c o n f i g u r a t i o n f i l e /u/markem /. g d b i n i t. To completely d i s a b l e t h i s s e c u r i t y p r o t e c t i o n add s e t auto load s a f e path / l i n e to your c o n f i g u r a t i o n f i l e /u/markem /. g d b i n i t. For more i n f o r m a t i o n about t h i s s e c u r i t y p r o t e c t i o n s e e the Auto l o a d i n g s a f e path s e c t i o n in the GDB manual. E. g., run from the s h e l l : i n f o ( gdb ) Auto l o a d i n g s a f e path ( gdb ) The file.gdbinit in your $HOME directory must be updated. How to Exit XV6 1. By halt Command The command halt is designed to tell QEMU to show down the kernel. It has been known to not always work, although course staff cannot duplicate the problem. 2. By Control Sequence Use the control sequence control a x. 8