Unformatted text preview:

COMP 530 Operating Systems Process Address Spaces and Binary Formats Don Porter 1 COMP 530 Operating Systems Background We ve talked some about processes This lecture overall virtual memory abstractions Key abstraction Address space We will learn about the mechanics of virtual memory later COMP 530 Operating Systems Basics Process includes a virtual address space An address space is composed of Memory mapped files Includes program binary Anonymous pages no file backing When the process exits their contents go away 3 COMP 530 Operating Systems Address Space Generation The compilation pipeline prog P foo end P P push inc SP x jmp foo foo 0 75 push push inc SP 4 inc SP 4 jmp 75 jmp 75 0 100 175 Library Library Routines Routines jmp 175 jmp 175 1000 1100 1175 Library Routines jmp 1175 Compilation Assembly Linking Loading prog P foo end PP push inc SP xjmp foo foo COMP 530 Operating Systems Need addresses at compile time You write code even in assembly using symbolic names Machine code ultimately needs to use addresses Recall from 311 411 the arguments for jump load store At compile time addresses Compiler needs to generate machine code using run time So compiler must specify where data and code go And or generate code that can be fixed up at runtime 5 COMP 530 Operating Systems Address Space Layout Determined mostly by the application compiler Link directives can influence this OS reserves part of the address space to map itself Upper GB on x86 Linux Application can dynamically request new mappings from the OS or delete mappings 6 COMP 530 Operating Systems Simple Example Virtual Address Space hello heap stk libc so 0 0xffffffff Hello world binary specified load address Also specifies where it wants libc Dynamically asks kernel for anonymous pages for its heap and stack 7 hellolibc soheapstk COMP 530 Operating Systems In practice You can see part of the requested memory layout of a program using ldd ldd usr bin git linux vdso so 1 0x00007fff197be000 libz so 1 lib libz so 1 0x00007f31b9d4e000 libpthread so 0 lib libpthread so 0 0x00007f31b9b31000 libc so 6 lib libc so 6 0x00007f31b97ac000 lib64 ld linux x86 64 so 2 0x00007f31b9f86000 8 COMP 530 Operating Systems Many address spaces What if every program wants to map libc at the same address No problem Every process has the abstraction of its own address space Only one active at a given time on a given core But many can exist in DRAM How does this work COMP 530 Operating Systems Memory Mapping Process 1 Process 2 Virtual Memory 0x1000 Only one physical address 0x1000 Virtual Memory Program expects x 0x1000 to always be at address 0x1000 int x 0x1000 0x1000 Physical Memory Only one physical address 0x1000 COMP 530 Operating Systems Two System Goals 1 Provide an abstraction of contiguous isolated virtual memory to a program We will study the details of virtual memory later 2 Prevent illegal operations Prevent access to other application No way to address another application s memory Detect failures early e g segfault on address 0 COMP 530 Operating Systems What about the kernel Most OSes reserve part of the address space in every process by convention Other ways to do this nothing mandated by hardware COMP 530 Operating Systems Example Redux Virtual Address Space hello heap stk libc so 0 Kernel always at the top of the address space Hello world binary specifies most of the memory map Dynamically asks kernel for anonymous pages for its heap and stack Linux 0xffffffff hellolibc soheapstkLinux COMP 530 Operating Systems Why a fixed mapping Makes the kernel internal bookkeeping simpler Example Remember how interrupt handlers are organized in a big table How does the table refer to these handlers By virtual address Awfully nice when one table works in every process COMP 530 Operating Systems Kernel protection So I protect programs from each other by running in different virtual address spaces But the kernel is in every virtual address space COMP 530 Operating Systems Decoupling CPU mode and Addr Space CPU operates in 2 modes user and supervisor Applications execute in user mode Kernel executes in supervisor mode Idea restrict some addresses to supervisor mode Although mapped will fault if touched in user mode 16 COMP 530 Operating Systems Putting protection together Permissions on the memory map protect against programs Randomly reading secret data like cached file contents Writing into kernel data structures The only way to access protected data is to trap into the kernel How Interrupt or syscall instruction Interrupt table entries protect against jumping into unexpected code COMP 530 Operating Systems Outline Basics of process address spaces Kernel mapping Protection How to dynamically change your address space Overview of loading a program COMP 530 Operating Systems Reminder Two types of mappings Memory mapped files Includes program binary Anonymous pages no file backing When the process exits their contents go away 19 Packing flags into a single integer COMP 530 Operating Systems Common Linux C idiom Example Access modes PROT READ 20 PROT WRITE 21 PROT EXEC 22 How to request read and write permission int flags PROT READ PROT WRITE 1 2 3 Sets bits 0 and 1 but leaves other blank Make sure you understand why flags are OR ed 20 COMP 530 Operating Systems Linux APIs mmap void addr size t length int prot int flags int fd off t offset munmap void addr size t length How to create an anonymous mapping What if you don t care where a memory region goes as long as it doesn t clobber something else COMP 530 Operating Systems Example Let s map a 1 page 4k anonymous region for data read write at address 0x40000 mmap 0x40000 4096 PROT READ PROT WRITE MAP ANONYMOUS 1 0 Why wouldn t we want exec permission 22 COMP 530 Operating Systems Idiosyncrasy 1 Stacks Grow Down In Linux Unix as you add frames to a stack they actually decrease in virtual address order Example Stack bottom 0x13000 main foo bar 0x12600 0x12300 0x11900 OS allocates a new page Exceeds stack page 2 issues How to expand and why down not up main foo bar Exceeds stack pageOS allocates a new page COMP 530 Operating Systems Problem 1 Expansion Recall OS is free to allocate any free page in the virtual address space if user doesn t specify an address What if the OS allocates the page below the top of the stack You can t grow the stack any further Out of memory fault with plenty of memory spare OS must reserve enough virtual address space after top of stack But how much is enough COMP 530


View Full Document

UNC-Chapel Hill COMP 530 - Process Address Spaces and Binary Formats

Download Process Address Spaces and Binary Formats
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Process Address Spaces and Binary Formats and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Process Address Spaces and Binary Formats and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?