CS5460: Operating Systems Lecture 2: OS – Hardware Interface (Chapter 2) CS 5460: Operating Systems Lecture 2
Course web page: – http://www.eng.utah.edu/~cs5460/ CADE lab: – WEB L224 and L226 – http://www.cade.utah.edu/ – Projects will be on Linux machines in CADE – You don’t need to go there, just ssh in Join the class mailing list – Go to: https://sympa.eng.utah.edu/sympa/ – So far 30 out of 61 have subscribed CS 5460: Operating Systems Lecture 2
Last Time OS History – Mainframe era – Minicomputer era – PC era – Ubiquitous era Basic OS principles apply over the entire 60-year history – Concurrency – Resource management – Providing usable abstractions – Hiding complexity – Etc. CS 5460: Operating Systems Lecture 2
Operating System Organization CS 5460: Operating Systems Lecture 2
What is an Operating System? Interface between user and hardware – Exports simpler “ virtual machine ” interface – Hides ugly details of real hardware User Applications Manages shared resources Virtual – CPU, memory, I/O devices, … Machine – Ensure fairness Interface – Protect processes from one another Operating Provides common services System – File system, VM, CPU scheduling, … Physical OS design reacts to HW changes Machine Interface – What OS can do dictated by architecture – Arch support (or lack thereof) can greatly Hardware simplify (of complicate) OS design – Example: Early PCs, Motorola 68000 CS 5460: Operating Systems Lecture 2
Modern OS Functionality 1 Provides a “virtual processor” for application code – Clean up after an application when it finishes – Applications are isolated from each other by default – Can communicate (using the OS) when desired Concurrency – Overlap I/O and computation – even for a single thread – Optionally, multiple threads – even for a single processor – Support running across multiple processors Manage I/O devices – OS usually sees an asynchronous interface (initiate -> interrupt) – OS usually provides a synchronous interface CS 5460: Operating Systems Lecture 2
Modern OS Functionality 2 Memory management – Provides a virtual address space for each process – Moves data between DRAM and disk – OS decides how much memory each application gets Files – Fast access to slow storage devices – Coherence model Network – OS provides a high-level interface to the Internet Security – OS implements a “security policy” Other: – Accounting, logging, error recovery, … CS 5460: Operating Systems Lecture 2
Generic Computer Architecture CPU: ALUs, pipeline, LD/ST, … Virtual Physical tag CPU L1$ addrs Memory: virtual vs physical addrs addrs TLB/MMU Core Trap System bus tags RDs/WRs L2$ Interrupts – Split transaction, addr/data/IO/cntl – Separate “ interrupt ” lines Bus adapter System bus – Converts system bus à à PCI/SCSI/ … Controller I/O bus adapter Memory Device controllers (e.g., PCI, SCSI) – CPU writes to mem-mapped IO regs – Devices signal CPU via interrupts Disk ctlr Things to consider: Serial ctlr – Interrupts and traps … DRAM – Memory-mapped I/O devices I/O bus … – VA à à PA translation Network controller – DMA CS 5460: Operating Systems Lecture 2
OS Abstractions of HW Hardware Resource OS abstraction CPU Processes / Threads Main memory Address spaces Disk File system Network Sockets / ports Devices Virtual devices CS 5460: Operating Systems Lecture 2
HW Support for OS Abstractions OS Service Hardware Support Isolation between processes Kernel/user mode Protected instructions Memory management unit Virtual memory MMU / TLB System calls Traps Asynchronous I/O Interrupts / DMA CPU scheduling / accounting Timer interrupts Synchronization Atomic instructions Question: Which pieces of HW support are truly necessary? CS 5460: Operating Systems Lecture 2
Typical OS Structure User-level – Applications User – Libraries: many common “ OS ” functions Libs Apps – Example: malloc() vs sbrk() Kernel-level Trap Direct manipulation – Top-level: machine independent – Bottom-level: machine dependent Thrd | File | VM | SIG OS – Runs in protected mode kernel – Need a way to switch (user ßà ßà kernel) CPU | Disk | Mem | Int Hardware-level Interrupts I/O regs DMA DMA – Device maker exports known interface – Device driver initiates operations by writing to device registers Hardware – Devices use interrupts to signal completion – DMA (offloads work, but has restrictions) CS 5460: Operating Systems Lecture 2
Virtual Address Space (Simplified) 0xffffffff Parts of the address space: User stack segment – Code: binary image of program – Data/BSS: Static variables (globals) User mode – Heap: explicitly alloc ’ d data ( malloc ) User heap – Stack: implicitly alloc ’ d data – I/O registers: not shown User data segment Huge unmapped areas exist, especially on 64-bit Read Only User code segment Small unmapped region around 0 0x80000000 Kernel mapped into all processes Kernel heap Question: How might we support Supervisor-only shared libraries? Kernel data segment MMU hardware: – Remaps VAs to PAs Read Only Kernel code segment – Supports read-only, supervisor-only – Detects accesses to unmapped regions 0x00000000 CS 5460: Operating Systems Lecture 2
Kernel Mode vs User Mode Some instructions can only be used in kernel mode – Direct access to I/O devices (typically) – Instructions to manipulate VA à à PA mappings and TLB – Enable/disable interrupts – Halt the machine Architecture typically supports: – Status bit in protected processor register indicating mode – Restricts ability to perform certain instructions if not kernel mode How do user applications get access to protected instructions? – Trap or interrupt à à indirect jump via OS-managed function table – System call typically implemented with special TRAP instruction CS 5460: Operating Systems Lecture 2
Anatomy of a System Call User applications make system calls to execute privileged instructions foo: … Anatomy of a system call: movl r1, (arg1) movl r0, #foo – Program puts syscall params in registers syscall – Program executes a trap: … » Minimal processor state (PC, PSW) User pushed on stack » CPU switches mode to KERNEL Kernel syscallhdlr(){ foo() { » CPU vectors to registered trap handler … … in the OS kernel switch (r0){ return res; – Trap handler uses param to jump to desired … } case: foo handler (e.g., fork, exec, open, … ) r0 ß ß foo(…); – When complete, reverse operation } asm( “ ret ” ); » Place return code in register } » Return from exception CS 5460: Operating Systems Lecture 2
System Calls Arguments passed in registers: (Why?) – Integer constants (e.g., buffer size, file offset, fileid) – Pointers to blocks of memory (e.g., strings, file buffers, … ) – Handles (e.g., file handle, socket handle, … ) OS typically returns: – Return code (value of –1 often denotes error) – Other results written into buffers in user space – You should always (always!) check syscall return values Principle: Dialogue between user-mode and kernel should be semantically simple – Simpler interfaces easier to work with – Simpler interfaces easier to implement correctly – Simpler interfaces tend to be easier to make efficient CS 5460: Operating Systems Lecture 2
System Call Example Source Assembly Code <main>: void foo (void) { pushq %rax write(1, “hello\n”, 6); mov $0x6,%edx } mov $0x694010,%esi mov $0x1,%edi callq libc_write Q: Where do all the magic xorl %eax,%eax numbers in the assembly come popq %rdx from? ret <libc_write>: Note: For this class mov $0x1,%eax syscall you will need to be cmp $0xfffffffffffff001,%rax able to read x86 and jae <__syscall_error> x86-64 assembly code retq CS 5460: Operating Systems Lecture 2
Traps Architecture detects special events: – trap instruction TRAP VECTOR: – invalid memory access – floating point exception 0x0082404 Illegal address – privileged instruction by user mode code 0x0084d08 Mem Violation – … 0x008211c Illegal instruction When processor detects condition: 0x0082000 System call – Save minimal CPU state (PC, sp, … ) – done by hardware … – Switches to KERNEL mode – Transfers control to trap handler » Indexes trap table w/ trap number Here, 0x82404 is address of » Jumps to address in trap table (*) handle_illegal_addr() . » Handler saves more state and may disable interrupts – RTE instruction reverses operation CS 5460: Operating Systems Lecture 2
Important from Today HW provides support for OS abstractions – Make them possible to implement – Make them easier to implement – Make them more efficient User programs run in processes – Each see its own virtual address space Kernel has a very different view of memory Modern processors support (at least) two modes – User mode and kernel mode The kernel is not a process User programs trap into kernel mode to access OS functionality CS 5460: Operating Systems Lecture 2
Recommend
More recommend