8 virtual memory - university of california, riverside · virtual memory heap stack code static...

37
Virtual memory (I) Hung-Wei Tseng

Upload: others

Post on 07-Aug-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Virtual memory (I)Hung-Wei Tseng

Page 2: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

What happens when creating a process

!2

Virtual memory

heap

stack

Dynamic allocated data: malloc()

Local variables, arguments

code

static data

program

code

static data

Linux contains a .bss section for uninitialized global variables

Page 3: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

The Machine

Previously, we talked about virtualization

!3

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Virtually, every process seems to have a processor/memory space, but

only a few of them are physically executing/using the installed DRAM.

Page 4: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• The mechanism • Non-preemptive/cooperative: the run process itself initiate context switches — by

using system calls • Preemptive: the OS kernel can actively incur context switches — by using hardware

(timer) interrupts • The policy

• Non-preemptive • First Come First Serve • Shortest job first: SJF

• Preemptive • Round robin • Shortest Time-to-completion • Multi-level scheduling algorithm

!4

Virtualizing the processor

Page 5: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

The Machine

Previously, we talked about virtualization

!5

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Previously, we’ve talked about sharing the processor.How about sharing DRAM?

Page 6: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• Why virtualize your memory • Start with the basic proposal — segmentation • Demand paging

!6

Outline

Page 7: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Why Virtual Memory?

!7

Page 8: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

!8

If we expose memory directly to the processor (I)

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Memory

0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3

00c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 0000000800c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Data

00c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3

00c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008 00c2e800 00000008 00c2f000 00000008

00c2e800 00000008 00c2f000 00000008

00c2f800 00000008 00c30000 00000008

? What if my program needs more memory?

Page 9: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

!9

If we expose memory directly to the processor (II)

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Memory

0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3

00c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Memory

?

What if my program runs on a machine

with a different memory size?

Page 10: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

!10

If we expose memory directly to the processor (III)

Memory

0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3

00c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

?

What if both programs need to use memory?

Page 11: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

The Virtual Memory Abstraction

!11

Page 12: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Virtual memory

!12

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Virtual Memory SpaceVirtual Memory Space

Memory

00c2e800 00000008 00c2f000 00000008

instruction0x0

0f00bb27509cbd23 00005d24 0000bd24

data0x80000000 instruction

0x0

0f00bb27509cbd23 00005d24 0000bd24

00c2f800 00000008 00c30000 00000008

data0x80008000data

0x80008000

00c2f800 00000008 00c30000 00000008

Page 13: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Virtual memory

!13Physical memory

Virtual memory

CPU

address mapping

0x000000000000

0xFFFFFFFFFFFF

Code Static Data

Data

Heap

Stack

Process A0x000000000000

0xFFFFFFFFFFFF

CodeStatic Data

Data

Heap

Stack

Process B

Page 14: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• An abstraction of memory space available for programs/software/programmer

• Programs execute using virtual memory address • The operating system and hardware work together to handle

the mapping between virtual memory addresses and real/physical memory addresses

• Virtual memory organizes memory locations into “pages”

!14

Virtual memory

Page 15: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• Some processes may use the same processor • Each process has the same address for variable a, but different

values. • You may see the content of a compiled program using objdump

!15

Demo revisit: Virtualization

Page 16: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Demo: Virtualization

!16

double a;

int main(int argc, char *argv[]){ int cpu, status, i; int *address_from_malloc; cpu_set_t my_set; // Define your cpu_set bit mask. CPU_ZERO(&my_set); // Initialize it all to 0, i.e. no CPUs selected. CPU_SET(4, &my_set); // set the bit that represents core 7. sched_setaffinity(0, sizeof(cpu_set_t), &my_set); // Set affinity of this process to the defined mask, i.e. only 7. status = syscall(SYS_getcpu, &cpu, NULL, NULL);

if(argc < 2) { fprintf(stderr, "Usage: %s process_nickname\n",argv[0]); exit(1); }

srand((int)time(NULL)+(int)getpid()); a = rand();

fprintf(stderr, "\nProcess %s is using CPU: %d. Value of a is %lf and address of a is %p\n",argv[1], cpu, a, &a); sleep(1);

fprintf(stderr, "\nProcess %s is using CPU: %d. Value of a is %lf and address of a is %p\n",argv[1], cpu, a, &a); sleep(3);

return 0;}

getcpu system call to retrieve the executing CPU ID

create a random number

print the value of a and address of a

print the value of a and address of a again after sleep

Page 17: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

!17

The same processor!The same memory

address!

Different values

Different values are preserved

Page 18: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Demo revisited

!18

#define _GNU_SOURCE #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <sched.h> #include <sys/syscall.h> #include <time.h>

double a;

int main(int argc, char *argv[]) { int i, number_of_total_processes=4; number_of_total_processes = atoi(argv[1]); for(i = 0; i< number_of_total_processes-1 && fork(); i++); srand((int)time(NULL)+(int)getpid()); fprintf(stderr, "\nProcess %d is using CPU: %d. Value of a is %lf and address of a is %p\n”,getpid(), cpu, a, &a); sleep(10); fprintf(stderr, "\nProcess %d is using CPU: %d. Value of a is %lf and address of a is %p\n”,getpid(), cpu, a, &a); return 0; }

Process B

Process A&a = 0x601090

Process A’s Mapping Table

Process B’s Mapping Table

Page 19: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

How to map from virtual to physical? Let’s start from segmentation

!19

Page 20: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• The compiler generates code using virtual memory addresses • The OS works together with hardware to partition physical memory

space into segments for each running application • The hardware dynamically translates virtual addresses into physical

memory addresses

!20

Segmentation

Physical memory of the machineApplication A0

X

Application B

X

00

X

2X

Page 21: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Address translation in segmentation

!21

Physical memory of the machine0

X

2X

Virtual memory of Application

X

0Processor

PC Base

0 X+ = X

load 0x4000

X0x4000 + = X + 0x4000

Page 22: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Protection again malicious processes

!26

Physical memory of the machine0

X

2X

Virtual memory of Application

X

0Processor

PC Base

0 X+ = X

load 0x4000

0x4000 X+ = X + 0x4000what if X + 0x4000 belongs to

another process?

what if we are load a “negative offset”?

Page 23: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Protection again malicious processes

!27

Physical memory of the machine0

X

2X

Virtual memory of Application

X

0Processor

PC Base

X = X + 0x4000

load 0x4000

0x4000 +

Bound

X Yes — proceed<0x4000

only allow the access if it’s yes

No — segmentation fault!!!

a segmentation fault (often shortened to segfault), raised by hardware with

memory protection, when the software has attempted to access a restricted area of memory (a memory access violation).

Page 24: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

What if?

!29

Application B

X

0

Y

X+Y

!29

Application A0

Y

Physical memory of the machine0

zApplication C0

XWhere can we map

Application C?

X

X

External Fragment

What if Application B only uses part of the

allocated space?

Internal Fragment

Even though we have space, we still cannot map App. C We waste some space in

the allocated segment

Page 25: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

When to create a virtual to physical address mapping? —

Demand paging

!30

Page 26: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• Paging: partition virtual/physical memory spaces into fix-sized pages

• Demand paging: Allocate a physical memory page for a virtual memory page when the virtual page is needed • There is also shadow paging used by embedded systems, mobile

phones — they load the whole program/data into the physical memory when you launch it

!31

Demand paging

Page 27: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• Paging: partition virtual/physical memory spaces into fix-sized pages

• Demand paging: Allocate a physical memory page for a virtual memory page when the virtual page is needed

!32

Demand paging

Application A0

X

Application B

X

0Physical memory of the

machine0

each of these cells is a page

Page 28: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• Page fault: if the demanding page is not in the physical memory • How to handle page fault: the processor raises an exception and

transfers the control (change the PC) to the page fault handler in OS code • Allocates a physical memory location for the page • Creates an entry recording this allocation

!33

Page fault

Application A0

X

Application B

X

0Physical memory of the

machine0

each of these cells is a page

Page fault

— where?

Page 29: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

page offsetphysical page number

page offsetvirtual page number

0x D E A D B

• Processor receives virtual addresses from the running code, main memory uses physical memory addresses

• Virtual address space is organized into “pages” • The system references the page table to translate

addresses • Each process has its own

page table • The page table

content is maintained by OS

• In addition to valid bit and physical page #, the page table may also store

• Reference bit • Modified bit • Permissions

!39

Address translationVirtual

address 0x 0 0 0 0 B E E F

valid

acce

sspe

rmiss

ion

Physical address E E F

Page table

Page 30: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• Page tables are too large to be kept on the chip (millions of entries)

• Instead, the page tables are kept in memory • Address translation in x86

• A special register, the page table base register (PTBR), points to the beginning of the page table for the running process

• The CPU walks through the page table to figure out the mapping • The contents of this register must be changed during a context

switch

!55

Address translation (cont.)— space overhead

— memory access overhead

Page 31: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Smaller page tables

!56

Page 32: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Virtual memory

0x0000000000000000

0xFFFFFFFFFFFFFFFF

Do we really need a large table?

!57

heap

stack

Dynamic allocated data: malloc()

Local variables, arguments

code

static data

Your program probably never uses this huge area!

Page 33: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Virtual memory

0x0000000000000000

0xFFFFFFFFFFFFFFFF

Hierarchical page table

!58

heap

stack

Dynamic allocated data: malloc()

Local variables, arguments

code

static data

1110000011

valid

1111111111

valid1111111111

valid

1111111111

valid

1111111111

valid

1111111111

valid

Each of these nodes occupies exactly a pageWhy?

Otherwise, you always need to find more than one consecutive

pages — difficult!

Page 34: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• Break the virtual page number into several pieces • If one piece has N bits, build an 2N-ary tree • Only store the part of the tree that contain valid pages • Walk down the tree to translate the virtual address

!59

Hierarchical page table

level 2 index offsetlevel 1 index

physical page # offset

Page 35: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• Two-level, 4KB, 10 bits index in each level • If we are accessing 0x1006000 now…

10 bits 10 bits offset = 12 bits

!60

Page table walking example

0000000100 0000000110 000000000000

0x7fffffffe000

0x4a

the memory page in 0x7fffffffe000

physical page # offset1001010 000000000000

Page 36: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

• Only store the valid second level pages.

1

0

1

0

0

0

0

0

0

0

valid

!66

Hierarchical page table1

1

1

1

0

1

1

1

1

0

1

1

1

1

0

1

1

0

1

1

Each of these nodes must fit in a page

Page 37: 8 Virtual memory - University of California, Riverside · Virtual memory heap stack code static data Virtual memory heap stack code static data Virtual memory heap stack code static

Case study: Address translation in x86-64

!73

63:48 (16 bits) 47:39 (9 bits) 38:30 (9 bits) 29:21 (9 bits) 20:12 (9 bits) 11:0 (12 bits)SignExt L4 index L3 index L2 index L1 index page offset

X86 Processor

CR3 Reg.

……

…512 entries

……

512 entries

……

512 entries

……

512 entries

11:0 (12 bits)physical page # page offset