virtual & dynamic memory management

22
Virtual & Dynamic Memory Management Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

Upload: zaria

Post on 05-Jan-2016

44 views

Category:

Documents


1 download

DESCRIPTION

Virtual & Dynamic Memory Management. Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University. Your vision? Seek with all your heart?. Course Objectives. The better knowledge of computer systems, the better programing. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Virtual & Dynamic  Memory Management

Virtual & Dynamic Memory Management

Summer 2014

COMP 2130 Intro Computer Systems

Computing ScienceThompson Rivers University

Page 2: Virtual & Dynamic  Memory Management

TRU-COMP2130 DMM 2

Course Objectives

The better knowledge of computer systems, the better programing.Computer System C Programming Language

Computer architectureCPU (Central Processing Unit)IA32 assembly language

Introduction to C language

Compiling, linking, loading, executing

Physical main memoryMMU (Memory Management Unit)

Virtual memory space

Memory hierarchyCache

Dynamic memory management

Better coding – locality

Reliable and efficient programming for power programmers(to avoid strange errors, to optimize codes, to avoid security holes, …)

Your vision? Seek with all your heart?

Page 3: Virtual & Dynamic  Memory Management

TRU-COMP2130 DMM 3

Course Contents

Introduction to computer systems: B&O 1 Introduction to C programming: K&R 1 – 4 Data representations: B&O 2.1 – 2.4 C: advanced topics: K&R 5.1 – 5.10, 6 – 7 Introduction to IA32 (Intel Architecture 32): B&O 3.1 – 3.8, 3.13 Compiling, linking, loading, and executing: B&O 7 (except 7.12) Dynamic memory management – Heap: B&O 9.1–2, 9.4–5, 9.9.1–2,

9.9.4–5, 9.11 Code optimization: B&O 5.1 – 5.6, 5.13 Memory hierarchy, locality, caching: B&O 5.12, 6.1 – 6.3, 6.4.1 –

6.4.2, 6.5, 6.6.2 – 6.6.3, 6.7 Virtual memory (if time permits): B&O 9.4 – 9.5

Your vision? Seek with all your heart?

Page 4: Virtual & Dynamic  Memory Management

TRU-COMP2130 DMM 4

Unit Learning Objectives

Which type of addresses are used by machine instructions? Virtual addresses, or physical addresses?

List the three advantages of using virtual memory. What hardware component translates virtual addresses to physical

addresses? Explain how virtual memory support memory protection? Use of malloc() and free() in C programs. Identify memory-related problems in given C code.

Your vision? Seek with all your heart?

Page 5: Virtual & Dynamic  Memory Management

TRU-COMP2130 DMM 5

Unit Contents

1. Physical and Virtual Addressing

2. Address Spaces

3. VM as a Tool for Memory Management

4. VM as a Tool for Memory Protection

5. Dynamic Memory Management

6. Common Memory-Related Bugs in C Programs

Your vision? Seek with all your heart?

Page 6: Virtual & Dynamic  Memory Management

TRU-COMP2130 DMM 6

9.1 Physical and Virtual Addressing

Your vision? Seek with all your heart?

Page 7: Virtual & Dynamic  Memory Management

A System Using Physical Addressing

Used in “simple” systems like embedded microcontrollers in devices like cars, elevators, and digital picture frames

Instructions use physical addresses. Programmers need to think of main memory.

0:1:

M-1:

Main memory

CPU

2:3:4:5:6:7:

Physical address(PA)

Data word

8: ...

4

Carnegie Mellon

[Q] Do instructions use PAs or VAs?

Page 8: Virtual & Dynamic  Memory Management

A System Using Virtual Addressing

Used in all modern servers, desktops, and laptops. One of the great ideas in computer science. Instructions use virtual addresses. Programmer do not have to think of main

memory.

0:1:

M-1:

Main memory

MMU

2:3:4:5:6:7:

Physical address(PA)

Data word

8: ...

CPU

Virtual address(VA)

CPU Chip

44100

Carnegie Mellon

[Q] Do instructions use PAs or VAs?

(Memory Management Unit:VAs are translated to PAs.)

Page 9: Virtual & Dynamic  Memory Management

TRU-COMP2130 DMM 9

9.2 Address SpacesYour vision? Seek with all your heart?

Page 10: Virtual & Dynamic  Memory Management

Address Spaces Linear address space: Ordered set of contiguous non-negative integer

addresses:{0, 1, 2, 3 … }

Virtual address space:

Set of N = 2n virtual addresses{0, 1, 2, 3, …, N-1}

[Q] For each process ???

Yes. Uniform view for every process. Physical address space: Set of M = 2m physical addresses

{0, 1, 2, 3, …, M-1}

[Q] For each process ??? Clean distinction between data (bytes) and their attributes (addresses) Every byte in main memory:

one physical address, one (or more) virtual addresses

Carnegie Mellon

[Q] M >= N ???

PP 2m-p-1

Physical memory

Empty

Empty

Uncached

VP 0VP 1

VP 2n-p-1

Virtual memory

Unallocated

Cached

Uncached

Unallocated

Cached

Uncached

PP 0PP 1

Empty

Cached

0

N-1M-1

0

Virtual Pages (VP's) stored on disk

Physical Pages (PP's) cached in DRAM

ThroughMMU

Page 11: Virtual & Dynamic  Memory Management

Why Virtual Memory (VM)? Uses main memory efficiently

Use DRAM as a cache for the parts of a virtual address space [Q] Should all the parts of an executable file be loaded into DRAM?

Simplifies memory management Each process gets the same uniform linear address space -> easy jobs for compilers,

linkers and loaders

Isolates address spaces -> memory protection One process can’t interfere with another’s memory User program cannot access privileged kernel information [Q] How? By software, or hardware?

Carnegie Mellon

PP 2m-p-1

Empty

Empty

Uncached

VP 0VP 1

VP 2n-p-1

Unallocated

Cached

Uncached

Unallocated

Cached

Uncached

PP 0PP 1

Empty

Cached

0

N-1M-1

0

ThroughMMU

Page 12: Virtual & Dynamic  Memory Management

TRU-COMP2130 DMM 12

9.4 VM for Memory Management

Your vision? Seek with all your heart?

Page 13: Virtual & Dynamic  Memory Management

VM as a Tool for Memory Management

Key idea: Each process has its own virtual address space. It can view memory as a simple linear array. Mapping function scatters addresses through physical memory

Well chosen mappings simplify memory allocation and management. [Q] Is a VP-PP mapping table used for each process, or one common table for all processes?

Each process

Virtual Address

Space for Process 1:

Physical Address

Space (DRAM)

0

N-1(e.g., read-only

library code)

Virtual Address

Space for Process 2:

VP 1VP 2...

0

N-1

VP 1VP 2...

PP 2

PP 6

PP 8

...

0

M-1

Address translation

[Q] by what?

Carnegie Mellon

Page 14: Virtual & Dynamic  Memory Management

Memory allocation Each virtual page can be mapped to any physical page. A virtual page can be stored in different physical pages at different times.

Sharing code and data among processes Map virtual pages to the same physical page (here: PP 6).

Virtual Address

Space for Process 1:

Physical Address

Space (DRAM)

0

N-1(e.g., read-only

library code)

Virtual Address

Space for Process 2:

VP 1VP 2...

0

N-1

VP 1VP 2...

PP 2

PP 6

PP 8

...

0

M-1

Carnegie Mellon

Address translation

[Q] by what?

Page 15: Virtual & Dynamic  Memory Management

Simplifying Linking and Loading

Linking Each program has similar virtual

address space Code, stack, and shared libraries

always start at the same address

Loading [Q] Does each process use its own

VP-PP mapping table? execve() allocates virtual pages

for .text and .data sections = creates PTEs (Page Table Entries) marked as invalid

The .text and .data sections are copied, page by page, on demand by the virtual memory system

Kernel virtual memory

Memory-mapped region forshared libraries

Run-time heap(created by malloc)

User stack(created at runtime)

Unused0

%esp (stack

pointer)

Memoryinvisible touser code

brk

0xc0000000

0x08048000

0x40000000

Read/write segment(.data, .bss)

Read-only segment(.init, .text, .rodata)

Loaded from the

executable file

Carnegie Mellon

Page 16: Virtual & Dynamic  Memory Management

TRU-COMP2130 DMM 16

9.5 VM for Memory ProtectionYour vision? Seek with all your heart?

Page 17: Virtual & Dynamic  Memory Management

VM as a Tool for Memory Protection

Extend PTEs (Page Table Entries) with permission bits Page fault handler checks these before remapping

If violated, send process SIGSEGV (segmentation fault). [Q] To what? [Q] Is segmentation fault detected by software, or hardware?

Process i: AddressREAD WRITE

PP 6Yes NoPP 4Yes YesPP 2Yes

VP 0:VP 1:VP 2:

•••

Process j:

Yes

SUP

NoNoYes

AddressREAD WRITE

PP 9Yes NoPP 6Yes Yes

PP 11Yes Yes

SUP

NoYesNo

VP 0:VP 1:VP 2:

Physical Address Space

PP 2

PP 4

PP 6

PP 8PP 9

PP 11

Carnegie Mellon

SUP indicates whether the process must be running in kernel (supervisor) mode to access the page.

MMU

Page 18: Virtual & Dynamic  Memory Management

TRU-COMP2130 DMM 18

9.9 Dynamic Memory Allocation

Your vision? Seek with all your heart?

Page 19: Virtual & Dynamic  Memory Management

Dynamic Memory Allocation Programmers use dynamic

memory allocators (such as malloc) to acquire VM at run time.

For data structures whose size is only known at runtime.

Dynamic memory allocators manage an area of process virtual memory known as the heap.

Heap (via malloc)

Program text (.text)

Initialized data (.data)

Uninitialized data (.bss)

User stack

0

Top of heap (brk ptr)

Application

Dynamic Memory Allocator

Heap

Carnegie Mellon

Page 20: Virtual & Dynamic  Memory Management

Allocator maintains heap as collection of variable sized blocks, which are either allocated or free

Types of allocators Explicit allocator: application allocates and frees space

E.g., malloc() and free() in C Implicit allocator: application allocates, but does not free space

E.g. garbage collection in Java, ML, and Lisp

Will discuss simple explicit memory allocation

Carnegie Mellon

Page 21: Virtual & Dynamic  Memory Management

The malloc Package#include <stdlib.h>

void *malloc(size_t size) Successful:

Returns a pointer to a memory block of at least size bytes(typically) aligned to 8-byte boundary

If size == 0, returns NULL Unsuccessful: returns NULL (0) and sets errno

void free(void *p) Returns the block pointed at by p to pool of available memory p must come from a previous call to malloc or realloc

Other functions calloc: Version of malloc that initializes allocated block to zero. realloc: Changes the size of a previously allocated block. brk, sbrk: Used internally by allocators to grow or shrink the heap

Carnegie Mellon

Page 22: Virtual & Dynamic  Memory Management

malloc Examplevoid foo(int n, int m) { int i, *p; /* Allocate a block of n ints */ p = (int *) malloc(n * sizeof(int)); if (p == NULL) { perror("malloc"); // [Q] what is it??? exit(0); } /* Initialize allocated block */ for (i=0; i<n; i++) p[i] = i;

/* Return p to the heap */ free(p); }

Carnegie Mellon