CS 3204 Operating Systems Godmar Back Lecture 11.

Slides:



Advertisements
Similar presentations
CS 4284 Systems Capstone Godmar Back Processes and Threads.
Advertisements

Memory Management: Overlays and Virtual Memory
CS 4284 Systems Capstone Godmar Back Virtual Memory – Paging Techniques.
ITEC 352 Lecture 25 Memory(3). Review Questions RAM –What is the difference between register memory, cache memory, and main memory? –What connects the.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 3 Memory Management Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
1 Pintos Project #3 Virtual Memory The following slides were created by Xiaomo Liu and others for CS 3204 Fall And Modified by Nick Ryan for Spring.
ITEC 352 Lecture 27 Memory(4). Review Questions? Cache control –L1/L2  Main memory example –Formulas for hits.
CS 4284 Systems Capstone Godmar Back Linking and Loading.
Jaishankar Sundararaman
1 Operating Systems and Protection CS Goals of Today’s Lecture How multiple programs can run at once  Processes  Context switching  Process.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
6.828: PC hardware and x86 Frans Kaashoek
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
CS 4284 Systems Capstone Godmar Back Virtual Memory – Page Tables.
Lecture-1 Compilation process
Recitation 4: The Stack & Lab3 Andrew Faulring Section A 30 September 2002.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
CS 3204 Operating Systems Godmar Back Lecture 15.
Recitation 6 – 2/26/01 Outline Linking Exam Review –Topics Covered –Your Questions Shaheen Gandhi Office Hours: Wednesday.
Topic 2d High-Level languages and Systems Software
CS 4284 Systems Capstone Godmar Back Virtual Memory – Paging Techniques.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
CS 3204 Operating Systems Godmar Back Lecture 17.
Memory Management: Overlays and Virtual Memory. Agenda Overview of Virtual Memory –Review material based on Computer Architecture and OS concepts Credits.
1 Pintos Virtual Memory Management Project (CS3204 Spring 2006 VT) Yi Ma.
Pintos project 3: Virtual Memory Management
CS 3204 Operating Systems Godmar Back Lecture 16.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.
CSc 453 Linking and Loading
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
Memory Management Chapter 5 Advanced Operating System.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Virtual Memory – Paging Techniques
Lecture 3 Translation.
CS 177 Computer Security Lecture 9
Instruction Set Architecture
MODERN OPERATING SYSTEMS Third Edition ANDREW S
CS 140 Lecture Notes: Virtual Memory
Virtualization Virtualize hardware resources through abstraction CPU
Virtual Memory – Page Tables
Anton Burtsev February, 2017
143A: Principles of Operating Systems Lecture 8: Basic Architecture of a Program Anton Burtsev October, 2017.
Homework In-line Assembly Code Machine Language
ICS143A: Principles of Operating Systems Lecture 21: Program linking and loading Anton Burtsev March, 2017.
CS 5204 Operating Systems Linking and Loading Godmar Back.
PA1 is out Best by Feb , 10:00 pm Enjoy early
x86 segmentation, page tables, and interrupts
Topic 2e High-Level languages and Systems Software
Computer Architecture and Assembly Language
CS 140 Lecture Notes: Virtual Memory
Computer Organization & Compilation Process
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
CS 4284 Systems Capstone Linking and Loading Godmar Back.
Understanding Program Address Space
CS 140 Lecture Notes: Virtual Memory
The Runtime Environment
CS 5204 Operating Systems Lecture 10
Machine-Level Programming: Introduction
Computer Organization & Compilation Process
CS 140 Lecture Notes: Virtual Memory
Computer Architecture and System Programming Laboratory
Presentation transcript:

CS 3204 Operating Systems Godmar Back Lecture 11

9/20/2015CS 3204 Fall Announcements Thursday, Oct 2: –Midterm –No office hours –Project 2 help session 7-9pm, Room McB 209 Group switch deadline is Sunday, Oct 5 –No need to notify us if group doesn’t change –Must notify if group did change for P2 See forum for CVS instructions Reading: –Read carefully

Linking and Loading

9/20/2015CS 3204 Fall Today: what you need to know for Project 2 (and life) Compiling, Linking, Loading –Where are my variables? –How are programs loaded into memory? Virtual Memory Basics –How are virtual addresses checked and how are they mapped? –What happens on a context-switch with respect to the MMU?

9/20/2015CS 3204 Fall Accessing Information All information a program reads/writes is stored somewhere in memory Programmer uses symbolic names: –local variables, global variables, assembly constants CPU instructions use virtual addresses: –absolute addresses (at 0xC ) –relative addresses (at $esp – 16) Actual memory uses physical addresses: Big Question: who does the translation & when?

9/20/2015CS 3204 Fall The Big Picture Program Code.c/.cc Source File 1 Assembly Code.s File 1 Object Code.o File 1 Program Code.c/.cc Source File 2 Assembly Code.s File 2 Object Code.o File 2.h Header File 1.h Header File 2.h Header File 3 Executable Program1 Process1 Physical RAM (physically addressed) Preprocessor Compiler Assembler Linker Loader MMURun Time Load Time Link Time Compile Time Executable Program2 Process2Process3

9/20/2015CS 3204 Fall Step 1: Compilation Compiler resolves local variable names (and struct field offsets!) int initialized_variable = 1; int zero_initialized_variable; int global_function(int argument) { volatile int local_variable = 1; return argument + local_variable + initialized_variable + zero_initialized_variable; } int initialized_variable = 1; int zero_initialized_variable; int global_function(int argument) { volatile int local_variable = 1; return argument + local_variable + initialized_variable + zero_initialized_variable; }.data initialized_variable:.long 1.data initialized_variable:.long 1.comm zero_initialized_variable,4,4 global_function: pushl %ebp movl %esp, %ebp subl $16, %esp movl $1, -4(%ebp) /* local_variable */ movl -4(%ebp), %eax addl 8(%ebp), %eax /* argument */ addl initialized_variable, %eax addl zero_initialized_variable, %eax leave ret global_function: pushl %ebp movl %esp, %ebp subl $16, %esp movl $1, -4(%ebp) /* local_variable */ movl -4(%ebp), %eax addl 8(%ebp), %eax /* argument */ addl initialized_variable, %eax addl zero_initialized_variable, %eax leave ret whereismystuff.c whereismystuff.s

9/20/2015CS 3204 Fall Step 2: Assembly global_function: pushl %ebp movl %esp, %ebp subl $16, %esp movl $1, -4(%ebp) /* local_variable */ movl -4(%ebp), %eax addl 8(%ebp), %eax /* argument */ addl initialized_variable, %eax addl zero_initialized_variable, %eax leave ret global_function: pushl %ebp movl %esp, %ebp subl $16, %esp movl $1, -4(%ebp) /* local_variable */ movl -4(%ebp), %eax addl 8(%ebp), %eax /* argument */ addl initialized_variable, %eax addl zero_initialized_variable, %eax leave ret whereismystuff.s : 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 83 ec 10 sub $0x10,%esp 6: c7 45 fc movl $0x1,0xfffffffc(%ebp) d: 8b 45 fc mov 0xfffffffc(%ebp),%eax 10: add 0x8(%ebp),%eax 13: add 0x0,%eax // initialized_variable 19: add 0x0,%eax // zero_initialized_variable 1f: c9 leave 20: c3 ret : 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 83 ec 10 sub $0x10,%esp 6: c7 45 fc movl $0x1,0xfffffffc(%ebp) d: 8b 45 fc mov 0xfffffffc(%ebp),%eax 10: add 0x8(%ebp),%eax 13: add 0x0,%eax // initialized_variable 19: add 0x0,%eax // zero_initialized_variable 1f: c9 leave 20: c3 ret whereismystuff.o RELOCATION RECORDS FOR [.text]: OFFSET TYPE VALUE R_386_32 initialized_variable b R_386_32 zero_initialized_variable Contents of section.data: RELOCATION RECORDS FOR [.text]: OFFSET TYPE VALUE R_386_32 initialized_variable b R_386_32 zero_initialized_variable Contents of section.data:

9/20/2015CS 3204 Fall Step 3: Linking Linker resolves global addresses, stores instructions for loader in executable –Key: linker links multiple, independently assembled.o files into executable –Must decide on layout & then assign addresses & relocate c : c: 55 push %ebp d: 89 e5 mov %esp,%ebp f: 83 ec 10 sub $0x10,%esp : c7 45 fc movl $0x1,0xfffffffc(%ebp) : 8b 45 fc mov 0xfffffffc(%ebp),%eax c: add 0x8(%ebp),%eax f: add 0x ,%eax : add 0x ,%eax b: c9 leave c: c3 ret c : c: 55 push %ebp d: 89 e5 mov %esp,%ebp f: 83 ec 10 sub $0x10,%esp : c7 45 fc movl $0x1,0xfffffffc(%ebp) : 8b 45 fc mov 0xfffffffc(%ebp),%eax c: add 0x8(%ebp),%eax f: add 0x ,%eax : add 0x ,%eax b: c9 leave c: c3 ret whereismystuff (.exe)

9/20/2015CS 3204 Fall Step 4: Loading (Conceptual) Executable = set of instructions stored on disk for loader –consists of sections Picture compiler & linker had in mind when building executable –sections become segments Code Data BSS Stack start program here room for stack to grow room for heap to grow Heap stack segment data segment code segment 0 MAX_VIRTUAL Code: c : c: 55 push %ebp d: 89 e5 mov %esp,%ebp f: 83 ec 10 sub $0x10,%esp : c7 45 fc movl $0x1,0xfffffffc(%ebp) : 8b 45 fc mov 0xfffffffc(%ebp),%eax c: add 0x8(%ebp),%eax f: add 0x ,%eax : add 0x ,%eax b: c9 leave c: c3 ret Data: Contents of section.data: ELF Header: -start address 0x080482d8 BSS: - size & start of zero initialized data

9/20/2015CS 3204 Fall Virtual Memory & Paging (Simplified) Maps virtual addresses to physical addresses (indirection) –x86: page table is called page directory (one per process) –mapping at page granularity (x86: 1 page = 4 KB = 4,096 bytes) 0 MAX_VIRTUAL 0 MAX_PHYSICAL Page Table range depends on processor architecture IA32: sizeof(void*)=4 range depends on how much RAM you bought pages frames MMU

9/20/2015CS 3204 Fall ustack (1) Step 5: Loading (For Real) kernel ucode (1) kcode kdata kbss kheap 0 C C FFFFFFFF 3 GB 1 GB used free user (1) user code user (1) udata (1) user (1) user data + bss user stack kernel code kernel data kernel bss kernel heap after Pintos boots, all physical memory is mapped at C and up - part is already used by kernel - part is free (but mapped) Pintos then starts the first process … P1

9/20/2015CS 3204 Fall ustack (2) Step 5: Loading (2 nd Process) kernel user (1) ucode (2) kcode kdata kbss kheap 0 C C FFFFFFFF 3 GB 1 GB used free user (2) user code user (2) udata (2) user (2) user data + bss user stack P2 To load a second process, Pintos creates a new page table - clone boot page table - then add entries …

9/20/2015CS 3204 Fall Context Switching Each process has its own address space –This means that the meaning of addresses (say 0x08c4000) may be different depending on which process is active –Maps to different page in physical memory When processes switch, address spaces must switch –MMU must be reprogrammed

9/20/2015CS 3204 Fall ustack (1) Process 1 Active kernel ucode (1) kcode kdata kbss kheap 0 C C FFFFFFFF 3 GB 1 GB used free user (1) udata (1) user (1) user (2) P1

9/20/2015CS 3204 Fall ustack (2) Process 2 Active kernel user (1) ucode (2) kcode kdata kbss kheap 0 C C FFFFFFFF 3 GB 1 GB used free user (2) udata (2) user (2) access possible in user mode access requires kernel mode P2

9/20/2015CS 3204 Fall Context Switching Process 1 Process 2 Kernel user mode kernel mode

9/20/2015CS 3204 Fall ustack (1) Process 1 Active in user mode kernel ucode (1) kcode kdata kbss kheap 0 C C FFFFFFFF 3 GB 1 GB used free user (1) udata (1) user (1) user (2) access possible in user mode P1

9/20/2015CS 3204 Fall ustack (1) Process 1 Active in kernel mode kernel ucode (1) kcode kdata kbss kheap 0 C C FFFFFFFF 3 GB 1 GB used free user (1) udata (1) user (1) user (2) access possible in user mode access requires kernel mode Context Switch schedule_tail() calls process_activate: void process_activate (void) { struct thread *t = thread_current (); /* Activate thread's page tables. */ pagedir_activate (t->pagedir); } Context Switch schedule_tail() calls process_activate: void process_activate (void) { struct thread *t = thread_current (); /* Activate thread's page tables. */ pagedir_activate (t->pagedir); } P1

9/20/2015CS 3204 Fall ustack (2) Process 2 Active in kernel mode kernel user (1) ucode (2) kcode kdata kbss kheap 0 C C FFFFFFFF 3 GB 1 GB used free user (2) udata (2) user (2) access possible in user mode access requires kernel mode P2

9/20/2015CS 3204 Fall ustack (2) Process 2 Active in user mode kernel user (1) ucode (2) kcode kdata kbss kheap 0 C C FFFFFFFF 3 GB 1 GB used free user (2) udata (2) user (2) access possible in user mode P2

9/20/2015CS 3204 Fall Summary All you have to do in Project 2/3/4 is: –Be aware of what memory is currently accessible Your kernel executes in kernel mode, so you can access all memory (if you use >C addresses) But you must set it up such that your programs can run with the memory you allow them to access (at <C ) Don’t let user programs fool you into accessing other processes’ (or arbitrary kernel) memory –Kill them if they try Keep track of where stuff is –Virtually (toolchain’s view): below C (PHYS_BASE) user space above C (PHYS_BASE) kernel space –Physically (note: “physical” addresses are represented as 0xC phys_addr in Pintos b/c of permanent mapping)