Virtual Memory 1 Changelog Changes made in this version not seen - - PowerPoint PPT Presentation

virtual memory
SMART_READER_LITE
LIVE PREVIEW

Virtual Memory 1 Changelog Changes made in this version not seen - - PowerPoint PPT Presentation

Virtual Memory 1 Changelog Changes made in this version not seen in fjrst lecture: 16 November 2017: shared libraries: clarifjed that its multiple read-only virtual copies backed by one physical copy 1 SIMD notes (1) sorry for two typos:


slide-1
SLIDE 1

Virtual Memory

1

slide-2
SLIDE 2

Changelog

Changes made in this version not seen in fjrst lecture:

16 November 2017: shared libraries: clarifjed that it’s multiple read-only virtual copies backed by one physical copy

1

slide-3
SLIDE 3

SIMD notes (1)

sorry for two typos: partial_sums = _mm_setzero_si128(), not (crazy markdown formatting error) …and _mm_mullo_epi32 truncated to 32-bits (not 16) lab solutions posted on Collab, resources tab useful for HW?

2

slide-4
SLIDE 4

SIMD notes (2)

don’t try to use array indexing on __m128i variables

compiler doesn’t know what size values you put in it

_mm_extract_epi16(some_vector, 3)

reads 3rd 16-bit integer in some_vector

  • r store/load from array on the stack

3

slide-5
SLIDE 5

SIMD notes (2)

_mm_setr_epi16(a[i], a[i+1], a[i+2], ...) is much slower than _mm_loadu_si128((__m128i*) &a[i])

(GCC doesn’t fjgure out that a[i] and a[i+1] are adjacent… so it makes a temporary array on the stack, element-by-element)

but _mm_setr_epi16, etc. are good for constants

4

slide-6
SLIDE 6

program memory

0xFFFF FFFF FFFF FFFF 0xFFFF 8000 0000 0000 0x7F… 0x0000 0000 0040 0000 Used by OS Stack Heap / other dynamic Writable data Code + Constants

5

slide-7
SLIDE 7

Recall: address space

illuision of dedicated memory

Program A addresses Program B addresses mapping (set by OS) mapping (set by OS) Program A code Program B code Program A data Program B data OS data … real memory trigger error = kernel-mode only

6

slide-8
SLIDE 8

address translation

Program A addresses “virtual” every address accessed instructions and data mapping (set by OS) stored in processor? format? Program A code Program B code Program A data Program B data OS data … real memory “physical” program addresses are ‘virtual’ real addresses are ‘physical’ can be difgerent sizes!

7

slide-9
SLIDE 9

address translation

Program A addresses “virtual” every address accessed instructions and data mapping (set by OS) stored in processor? format? Program A code Program B code Program A data Program B data OS data … real memory “physical” program addresses are ‘virtual’ real addresses are ‘physical’ can be difgerent sizes!

7

slide-10
SLIDE 10

address translation

Program A addresses “virtual” every address accessed instructions and data mapping (set by OS) stored in processor? format? Program A code Program B code Program A data Program B data OS data … real memory “physical” program addresses are ‘virtual’ real addresses are ‘physical’ can be difgerent sizes!

7

slide-11
SLIDE 11

address translation

Program A addresses “virtual” every address accessed instructions and data mapping (set by OS) stored in processor? format? Program A code Program B code Program A data Program B data OS data … real memory “physical” program addresses are ‘virtual’ real addresses are ‘physical’ can be difgerent sizes!

7

slide-12
SLIDE 12
  • n virtual address sizes

virtual address size = size of pointer?

  • ften, but — sometimes part of pointer not used

example: typical x86-64 only use 48 bits

rest of bits have fjxed value

virtual address size is amount used for mapping

8

slide-13
SLIDE 13

address space sizes

amount of stufg that can be addressed = address space size

based on number of unique addresses

e.g. 32-bit virtual address = 232 byte virtual address space e.g. 20-bit physical addresss = 220 byte physical address space what if my machine has 3GB of memory (not power of two)?

not all addresses in physical address space are useful most common situation (since CPUs support having a lot of memory)

9

slide-14
SLIDE 14

address space sizes

amount of stufg that can be addressed = address space size

based on number of unique addresses

e.g. 32-bit virtual address = 232 byte virtual address space e.g. 20-bit physical addresss = 220 byte physical address space what if my machine has 3GB of memory (not power of two)?

not all addresses in physical address space are useful most common situation (since CPUs support having a lot of memory)

9

slide-15
SLIDE 15

toy program memory

code data/heap empty/more heap? stack

00 0000 0000 = 0x000 01 0000 0000 = 0x100 10 0000 0000 = 0x200 11 0000 0000 = 0x300 11 1111 1111 = 0x3FF

virtual page# 0 virtual page# 1 virtual page# 2 virtual page# 3 divide memory into pages ( bytes in this case) “virtual” = addresses the program sees page number is upper bits of address (because page size is power of two) rest of address is called page ofgset

10

slide-16
SLIDE 16

toy program memory

code data/heap empty/more heap? stack

00 0000 0000 = 0x000 01 0000 0000 = 0x100 10 0000 0000 = 0x200 11 0000 0000 = 0x300 11 1111 1111 = 0x3FF

virtual page# 0 virtual page# 1 virtual page# 2 virtual page# 3 divide memory into pages ( bytes in this case) “virtual” = addresses the program sees page number is upper bits of address (because page size is power of two) rest of address is called page ofgset

10

slide-17
SLIDE 17

toy program memory

code data/heap empty/more heap? stack

00 0000 0000 = 0x000 01 0000 0000 = 0x100 10 0000 0000 = 0x200 11 0000 0000 = 0x300 11 1111 1111 = 0x3FF

virtual page# 0 virtual page# 1 virtual page# 2 virtual page# 3 divide memory into pages (28 bytes in this case) “virtual” = addresses the program sees page number is upper bits of address (because page size is power of two) rest of address is called page ofgset

10

slide-18
SLIDE 18

toy program memory

code data/heap empty/more heap? stack

00 0000 0000 = 0x000 01 0000 0000 = 0x100 10 0000 0000 = 0x200 11 0000 0000 = 0x300 11 1111 1111 = 0x3FF

virtual page# 0 virtual page# 1 virtual page# 2 virtual page# 3 divide memory into pages ( bytes in this case) “virtual” = addresses the program sees page number is upper bits of address (because page size is power of two) rest of address is called page ofgset

10

slide-19
SLIDE 19

toy program memory

code data/heap empty/more heap? stack

00 0000 0000 = 0x000 01 0000 0000 = 0x100 10 0000 0000 = 0x200 11 0000 0000 = 0x300 11 1111 1111 = 0x3FF

virtual page# 0 virtual page# 1 virtual page# 2 virtual page# 3 divide memory into pages ( bytes in this case) “virtual” = addresses the program sees page number is upper bits of address (because page size is power of two) rest of address is called page ofgset

10

slide-20
SLIDE 20

toy physical memory

program memory virtual addresses

00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 10 1111 1111 11 0000 0000 to 11 1111 1111

real memory physical addresses

000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 111 0000 0000 to 111 1111 1111

physical page 0 physical page 1 physical page 7 virtual page # physical page # 00 010 (2) 01 111 (7) 10 none 11 000 (0) page table!

11

slide-21
SLIDE 21

toy physical memory

program memory virtual addresses

00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 10 1111 1111 11 0000 0000 to 11 1111 1111

real memory physical addresses

000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 111 0000 0000 to 111 1111 1111

physical page 0 physical page 1 physical page 7 virtual page # physical page # 00 010 (2) 01 111 (7) 10 none 11 000 (0) page table!

11

slide-22
SLIDE 22

toy physical memory

program memory virtual addresses

00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 10 1111 1111 11 0000 0000 to 11 1111 1111

real memory physical addresses

000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 111 0000 0000 to 111 1111 1111

physical page 0 physical page 1 physical page 7 virtual page # physical page # 00 010 (2) 01 111 (7) 10 none 11 000 (0) page table!

11

slide-23
SLIDE 23

toy physical memory

program memory virtual addresses

00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 10 1111 1111 11 0000 0000 to 11 1111 1111

real memory physical addresses

000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 111 0000 0000 to 111 1111 1111

physical page 0 physical page 1 physical page 7 virtual page # physical page # 00 010 (2) 01 111 (7) 10 none 11 000 (0) page table!

11

slide-24
SLIDE 24

toy physical memory

program memory virtual addresses

00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 10 1111 1111 11 0000 0000 to 11 1111 1111

real memory physical addresses

000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 111 0000 0000 to 111 1111 1111

physical page 0 physical page 1 physical page 7 virtual page # physical page # 00 010 (2) 01 111 (7) 10 none 11 000 (0) page table!

11

slide-25
SLIDE 25

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

12

slide-26
SLIDE 26

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

12

slide-27
SLIDE 27

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

12

slide-28
SLIDE 28

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

12

slide-29
SLIDE 29

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

12

slide-30
SLIDE 30

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

12

slide-31
SLIDE 31

switching page tables

part of context switch is changing the page table extra privileged instructions where in memory is the code that does this switching?

needs a page table entry pointing to it (alternate: HW changes page table when starting exception handler)

code better not be modifjed by user program

  • therwise: uncontrolled way to “escape” user mode

13

slide-32
SLIDE 32

switching page tables

part of context switch is changing the page table extra privileged instructions where in memory is the code that does this switching?

needs a page table entry pointing to it (alternate: HW changes page table when starting exception handler)

code better not be modifjed by user program

  • therwise: uncontrolled way to “escape” user mode

13

slide-33
SLIDE 33

switching page tables

part of context switch is changing the page table extra privileged instructions where in memory is the code that does this switching?

needs a page table entry pointing to it (alternate: HW changes page table when starting exception handler)

code better not be modifjed by user program

  • therwise: uncontrolled way to “escape” user mode

13

slide-34
SLIDE 34

switching page tables

part of context switch is changing the page table extra privileged instructions where in memory is the code that does this switching?

needs a page table entry pointing to it (alternate: HW changes page table when starting exception handler)

code better not be modifjed by user program

  • therwise: uncontrolled way to “escape” user mode

13

slide-35
SLIDE 35

kernel-mode only

virtual page # valid? physical page # kernel

  • nly?

00 1 010 (2, code) 01 1 111 (7, data) 10 1 000 (0, stack) 11 1 001 (1, OS ) 1 01 1101 0010 — address from CPU trigger exception if 0? trigger exception if 1 and in user mode? 111 1101 0010 to cache

14

slide-36
SLIDE 36

kernel-mode only

virtual page # valid? physical page # kernel

  • nly?

00 1 010 (2, code) 01 1 111 (7, data) 10 1 000 (0, stack) 11 1 001 (1, OS ) 1 01 1101 0010 — address from CPU trigger exception if 0? trigger exception if 1 and in user mode? 111 1101 0010 to cache

14

slide-37
SLIDE 37

kernel-mode only

virtual page # valid? physical page # kernel

  • nly?

00 1 010 (2, code) 01 1 111 (7, data) 10 1 000 (0, stack) 11 1 001 (1, OS ) 1 01 1101 0010 — address from CPU trigger exception if 0? trigger exception if 1 and in user mode? 111 1101 0010 to cache

14

slide-38
SLIDE 38

kernel-mode only

virtual page # valid? physical page # kernel

  • nly?

00 1 010 (2, code) 01 1 111 (7, data) 10 1 000 (0, stack) 11 1 001 (1, OS ) 1 01 1101 0010 — address from CPU trigger exception if 0? trigger exception if 1 and in user mode? 111 1101 0010 to cache

14

slide-39
SLIDE 39

kernel-mode only

virtual page # valid? physical page # kernel

  • nly?

00 1 010 (2, code) 01 1 111 (7, data) 10 1 000 (0, stack) 11 1 001 (1, OS ) 1 01 1101 0010 — address from CPU trigger exception if 0? trigger exception if 1 and in user mode? 111 1101 0010 to cache

14

slide-40
SLIDE 40

kernel-mode only

virtual page # valid? physical page # kernel

  • nly?

00 1 010 (2, code) 01 1 111 (7, data) 10 1 000 (0, stack) 11 1 001 (1, OS ) 1 01 1101 0010 — address from CPU trigger exception if 0? trigger exception if 1 and in user mode? 111 1101 0010 to cache

14

slide-41
SLIDE 41

exercise: page counting

suppose 32-bit virtual (program) addresses and each page is 4096 bytes (212 bytes) how many virtual pages?

15

slide-42
SLIDE 42

exercise: page counting

suppose 32-bit virtual (program) addresses and each page is 4096 bytes (212 bytes) how many virtual pages? 232/212 = 220

15

slide-43
SLIDE 43

exercise: page table size

suppose 32-bit virtual (program) addresses suppose 30-bit physical (hardware) addresses each page is 4096 bytes (212 bytes) pgae table entries have physical page #, valid bit, kernel-mode bit how big is the page table (if laid out like ones we’ve seen)? entries bits per entry

issue: where can we store that?

16

slide-44
SLIDE 44

exercise: page table size

suppose 32-bit virtual (program) addresses suppose 30-bit physical (hardware) addresses each page is 4096 bytes (212 bytes) pgae table entries have physical page #, valid bit, kernel-mode bit how big is the page table (if laid out like ones we’ve seen)? 220 entries ×(18 + 2) bits per entry

issue: where can we store that?

16

slide-45
SLIDE 45

exercise: address splitting

and each page is 4096 bytes (212 bytes) split the address 0x12345678 into page number and page ofgset: page #: 0x12345; ofgset: 0x678

17

slide-46
SLIDE 46

exercise: address splitting

and each page is 4096 bytes (212 bytes) split the address 0x12345678 into page number and page ofgset: page #: 0x12345; ofgset: 0x678

17

slide-47
SLIDE 47

page tables in memory

where can processor store megabytes of page tables? in memory

valid (bit 15) kernel (bit 14) physical page # (bits 4–13) unused (bit 0-3) page table entry layout virtual page # valid? kernel? physical page # 0000 0000 00 0000 0000 0000 0001 1 10 0010 0110 0000 0010 1 00 0000 1100 0000 0011 1 11 0000 0011 … 1111 1111 1 00 1110 1000 page table (logically) addresses bytes 0x00000000-1 00000000 00000000 … 0x00010000-1 00000000 00000000 0x00010002-3 10100010 01100000 0x00010004-5 10000010 11000000 0x00010006-7 10110000 00110000 … 0x000101FE-F 10001110 10000000 0x00010200-1 10100010 00111010 physical memory 0x00010000 page table base register 18

slide-48
SLIDE 48

page tables in memory

where can processor store megabytes of page tables? in memory

valid (bit 15) kernel (bit 14) physical page # (bits 4–13) unused (bit 0-3) page table entry layout virtual page # valid? kernel? physical page # 0000 0000 00 0000 0000 0000 0001 1 10 0010 0110 0000 0010 1 00 0000 1100 0000 0011 1 11 0000 0011 … 1111 1111 1 00 1110 1000 page table (logically) addresses bytes 0x00000000-1 00000000 00000000 … 0x00010000-1 00000000 00000000 0x00010002-3 10100010 01100000 0x00010004-5 10000010 11000000 0x00010006-7 10110000 00110000 … 0x000101FE-F 10001110 10000000 0x00010200-1 10100010 00111010 physical memory 0x00010000 page table base register 18

slide-49
SLIDE 49

page tables in memory

where can processor store megabytes of page tables? in memory

valid (bit 15) kernel (bit 14) physical page # (bits 4–13) unused (bit 0-3) page table entry layout virtual page # valid? kernel? physical page # 0000 0000 00 0000 0000 0000 0001 1 10 0010 0110 0000 0010 1 00 0000 1100 0000 0011 1 11 0000 0011 … 1111 1111 1 00 1110 1000 page table (logically) addresses bytes 0x00000000-1 00000000 00000000 … 0x00010000-1 00000000 00000000 0x00010002-3 10100010 01100000 0x00010004-5 10000010 11000000 0x00010006-7 10110000 00110000 … 0x000101FE-F 10001110 10000000 0x00010200-1 10100010 00111010 physical memory 0x00010000 page table base register 18

slide-50
SLIDE 50

page tables in memory

where can processor store megabytes of page tables? in memory

valid (bit 15) kernel (bit 14) physical page # (bits 4–13) unused (bit 0-3) page table entry layout virtual page # valid? kernel? physical page # 0000 0000 00 0000 0000 0000 0001 1 10 0010 0110 0000 0010 1 00 0000 1100 0000 0011 1 11 0000 0011 … 1111 1111 1 00 1110 1000 page table (logically) addresses bytes 0x00000000-1 00000000 00000000 … 0x00010000-1 00000000 00000000 0x00010002-3 10100010 01100000 0x00010004-5 10000010 11000000 0x00010006-7 10110000 00110000 … 0x000101FE-F 10001110 10000000 0x00010200-1 10100010 00111010 physical memory 0x00010000 page table base register 18

slide-51
SLIDE 51

page tables in memory

where can processor store megabytes of page tables? in memory

valid (bit 15) kernel (bit 14) physical page # (bits 4–13) unused (bit 0-3) page table entry layout virtual page # valid? kernel? physical page # 0000 0000 00 0000 0000 0000 0001 1 10 0010 0110 0000 0010 1 00 0000 1100 0000 0011 1 11 0000 0011 … 1111 1111 1 00 1110 1000 page table (logically) addresses bytes 0x00000000-1 00000000 00000000 … 0x00010000-1 00000000 00000000 0x00010002-3 10100010 01100000 0x00010004-5 10000010 11000000 0x00010006-7 10110000 00110000 … 0x000101FE-F 10001110 10000000 0x00010200-1 10100010 00111010 physical memory 0x00010000 page table base register 18

slide-52
SLIDE 52

page tables in memory

where can processor store megabytes of page tables? in memory

valid (bit 15) kernel (bit 14) physical page # (bits 4–13) unused (bit 0-3) page table entry layout virtual page # valid? kernel? physical page # 0000 0000 00 0000 0000 0000 0001 1 10 0010 0110 0000 0010 1 00 0000 1100 0000 0011 1 11 0000 0011 … 1111 1111 1 00 1110 1000 page table (logically) addresses bytes 0x00000000-1 00000000 00000000 … 0x00010000-1 00000000 00000000 0x00010002-3 10100010 01100000 0x00010004-5 10000010 11000000 0x00010006-7 10110000 00110000 … 0x000101FE-F 10001110 10000000 0x00010200-1 10100010 00111010 physical memory 0x00010000 page table base register 18

slide-53
SLIDE 53

page tables in memory

where can processor store megabytes of page tables? in memory

valid (bit 15) kernel (bit 14) physical page # (bits 4–13) unused (bit 0-3) page table entry layout virtual page # valid? kernel? physical page # 0000 0000 00 0000 0000 0000 0001 1 10 0010 0110 0000 0010 1 00 0000 1100 0000 0011 1 11 0000 0011 … 1111 1111 1 00 1110 1000 page table (logically) addresses bytes 0x00000000-1 00000000 00000000 … 0x00010000-1 00000000 00000000 0x00010002-3 10100010 01100000 0x00010004-5 10000010 11000000 0x00010006-7 10110000 00110000 … 0x000101FE-F 10001110 10000000 0x00010200-1 10100010 00111010 physical memory 0x00010000 page table base register 18

slide-54
SLIDE 54

page tables in memory

where can processor store megabytes of page tables? in memory

valid (bit 15) kernel (bit 14) physical page # (bits 4–13) unused (bit 0-3) page table entry layout virtual page # valid? kernel? physical page # 0000 0000 00 0000 0000 0000 0001 1 10 0010 0110 0000 0010 1 00 0000 1100 0000 0011 1 11 0000 0011 … 1111 1111 1 00 1110 1000 page table (logically) addresses bytes 0x00000000-1 00000000 00000000 … 0x00010000-1 00000000 00000000 0x00010002-3 10100010 01100000 0x00010004-5 10000010 11000000 0x00010006-7 10110000 00110000 … 0x000101FE-F 10001110 10000000 0x00010200-1 10100010 00111010 physical memory 0x00010000 page table base register 18

slide-55
SLIDE 55

page tables in memory

where can processor store megabytes of page tables? in memory

valid (bit 15) kernel (bit 14) physical page # (bits 4–13) unused (bit 0-3) page table entry layout virtual page # valid? kernel? physical page # 0000 0000 00 0000 0000 0000 0001 1 10 0010 0110 0000 0010 1 00 0000 1100 0000 0011 1 11 0000 0011 … 1111 1111 1 00 1110 1000 page table (logically) addresses bytes 0x00000000-1 00000000 00000000 … 0x00010000-1 00000000 00000000 0x00010002-3 10100010 01100000 0x00010004-5 10000010 11000000 0x00010006-7 10110000 00110000 … 0x000101FE-F 10001110 10000000 0x00010200-1 10100010 00111010 physical memory 0x00010000 page table base register 18

slide-56
SLIDE 56

page tables in memory

where can processor store megabytes of page tables? in memory

valid (bit 15) kernel (bit 14) physical page # (bits 4–13) unused (bit 0-3) page table entry layout virtual page # valid? kernel? physical page # 0000 0000 00 0000 0000 0000 0001 1 10 0010 0110 0000 0010 1 00 0000 1100 0000 0011 1 11 0000 0011 … 1111 1111 1 00 1110 1000 page table (logically) addresses bytes 0x00000000-1 00000000 00000000 … 0x00010000-1 00000000 00000000 0x00010002-3 10100010 01100000 0x00010004-5 10000010 11000000 0x00010006-7 10110000 00110000 … 0x000101FE-F 10001110 10000000 0x00010200-1 10100010 00111010 physical memory 0x00010000 page table base register 18

slide-57
SLIDE 57

memory access with page table

memory management unit (MMU)

11 0101 01 00 1101 1111

PTE size

0x10000 page table base register

+

data or instruction cache

1101 0011 11

check valid and kernel bit split PTE parts

cause fault?

00 1101 1111

physical address virtual address

  • ne program cache/memory access becomes

multiple cache/memory accesses

19

slide-58
SLIDE 58

memory access with page table

memory management unit (MMU)

11 0101 01 00 1101 1111

× PTE size

0x10000 page table base register

+

data or instruction cache

1101 0011 11

check valid and kernel bit split PTE parts

cause fault?

00 1101 1111

physical address virtual address

  • ne program cache/memory access becomes

multiple cache/memory accesses

19

slide-59
SLIDE 59

memory access with page table

memory management unit (MMU)

11 0101 01 00 1101 1111

× PTE size

0x10000 page table base register

+

data or instruction cache

1101 0011 11

check valid and kernel bit split PTE parts

cause fault?

00 1101 1111

physical address virtual address

  • ne program cache/memory access becomes

multiple cache/memory accesses

19

slide-60
SLIDE 60

memory access with page table

memory management unit (MMU)

11 0101 01 00 1101 1111

× PTE size

0x10000 page table base register

+

data or instruction cache

1101 0011 11

check valid and kernel bit split PTE parts

cause fault?

00 1101 1111

physical address virtual address

  • ne program cache/memory access becomes

multiple cache/memory accesses

19

slide-61
SLIDE 61

memory access with page table

memory management unit (MMU)

11 0101 01 00 1101 1111

× PTE size

0x10000 page table base register

+

data or instruction cache

1101 0011 11

check valid and kernel bit split PTE parts

cause fault?

00 1101 1111

physical address virtual address

  • ne program cache/memory access becomes

multiple cache/memory accesses

19

slide-62
SLIDE 62

memory access with page table

memory management unit (MMU)

11 0101 01 00 1101 1111

× PTE size

0x10000 page table base register

+

data or instruction cache

1101 0011 11

check valid and kernel bit split PTE parts

cause fault?

00 1101 1111

physical address virtual address

  • ne program cache/memory access becomes

multiple cache/memory accesses

19

slide-63
SLIDE 63

memory access with page table

memory management unit (MMU)

11 0101 01 00 1101 1111

× PTE size

0x10000 page table base register

+

data or instruction cache

1101 0011 11

check valid and kernel bit split PTE parts

cause fault?

00 1101 1111

physical address virtual address

  • ne program cache/memory access becomes

multiple cache/memory accesses

19

slide-64
SLIDE 64

memory access with page table

memory management unit (MMU)

11 0101 01 00 1101 1111

× PTE size

0x10000 page table base register

+

data or instruction cache

1101 0011 11

check valid and kernel bit split PTE parts

cause fault?

00 1101 1111

physical address virtual address

  • ne program cache/memory access becomes

multiple cache/memory accesses

19

slide-65
SLIDE 65

MMUs in the pipeline

MMU i-cache

decode execute

MMU d-cache

writeback fetch memory

up to four memory accesses per instruction challenging to make this fast (topic for a future date)

20

slide-66
SLIDE 66

MMUs in the pipeline

MMU i-cache

decode execute

MMU d-cache

writeback fetch memory

up to four memory accesses per instruction challenging to make this fast (topic for a future date)

20

slide-67
SLIDE 67

exercise: 64-bit system

my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? exercise: how large are physical page numbers? page table entries are 8 bytes (room for expansion, metadata) would take up bytes?? (512GB??)

top 16 bits of address not used for translation

21

slide-68
SLIDE 68

exercise: 64-bit system

my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? exercise: how large are physical page numbers? page table entries are 8 bytes (room for expansion, metadata) would take up bytes?? (512GB??)

top 16 bits of address not used for translation

21

slide-69
SLIDE 69

exercise: 64-bit system

my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? exercise: how large are physical page numbers? page table entries are 8 bytes (room for expansion, metadata) would take up bytes?? (512GB??)

top 16 bits of address not used for translation

21

slide-70
SLIDE 70

exercise: 64-bit system

my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? 248/212 = 236 entries exercise: how large are physical page numbers? 39 − 12 = 27 bits page table entries are 8 bytes (room for expansion, metadata) would take up bytes?? (512GB??)

top 16 bits of address not used for translation

21

slide-71
SLIDE 71

exercise: 64-bit system

my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? 248/212 = 236 entries exercise: how large are physical page numbers? 39 − 12 = 27 bits page table entries are 8 bytes (room for expansion, metadata) would take up 239 bytes?? (512GB??)

top 16 bits of address not used for translation

21

slide-72
SLIDE 72

two big problems to solve later

two extra cache accesses seems really slow solution: more caches (called “TLB”, topic for later weeks) page tables seem really huge solution: not just a fmat table

22

slide-73
SLIDE 73

exercise setup

5-bit virtual addresses, 6-bit physical addresses, 8-byte pages

virtual page # valid? physical page # 00 1 010 01 1 111 10 000 11 1 000 page table

physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C

  • phys. page 0
  • phys. page 1

23

slide-74
SLIDE 74

exercise setup

5-bit virtual addresses, 6-bit physical addresses, 8-byte pages

virtual page # valid? physical page # 00 1 010 01 1 111 10 000 11 1 000 page table

physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C

  • phys. page 0
  • phys. page 1

23

slide-75
SLIDE 75

exercise

5-bit virtual addresses, 6-bit physical addresses, 8-byte pages (virtual addresses) 0x18 = ???; 0x03 = ???; 0x0A = ???; 0x13 = ???

virtual page # valid? physical page # 00 1 010 01 1 111 10 000 11 1 000 page table

physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C

24

slide-76
SLIDE 76

exercise

5-bit virtual addresses, 6-bit physical addresses, 8-byte pages (virtual addresses) 0x18 = 00; 0x03 = ???; 0x0A = ???; 0x13 = ???

virtual page # valid? physical page # 00 1 010 01 1 111 10 000 11 1 000 page table

physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C

24

slide-77
SLIDE 77

exercise

5-bit virtual addresses, 6-bit physical addresses, 8-byte pages (virtual addresses) 0x18 = 00; 0x03 = 0x4A; 0x0A = ???; 0x13 = ???

virtual page # valid? physical page # 00 1 010 01 1 111 10 000 11 1 000 page table

physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C

24

slide-78
SLIDE 78

exercise

5-bit virtual addresses, 6-bit physical addresses, 8-byte pages (virtual addresses) 0x18 = 00; 0x03 = 0x4A; 0x0A = 0xDC; 0x13 = ???

virtual page # valid? physical page # 00 1 010 01 1 111 10 000 11 1 000 page table

physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C

24

slide-79
SLIDE 79

exercise

5-bit virtual addresses, 6-bit physical addresses, 8-byte pages (virtual addresses) 0x18 = 00; 0x03 = 0x4A; 0x0A = 0xDC; 0x13 = fault

virtual page # valid? physical page # 00 1 010 01 1 111 10 000 11 1 000 page table

physical addresses bytes 0x00-3 00 11 22 33 0x04-7 44 55 66 77 0x08-B 88 99 AA BB 0x0C-F CC DD EE FF 0x10-3 1A 2A 3A 4A 0x14-7 1B 2B 3B 4B 0x18-B 1C 2C 3C 4C 0x1C-F 1C 2C 3C 4C physical addresses bytes 0x20-3 D0 D1 D2 D3 0x24-7 D4 D5 D6 D7 0x28-B 89 9A AB BC 0x2C-F CD DE EF F0 0x30-3 BA 0A BA 0A 0x34-7 CB 0B CB 0B 0x38-B DC 0C DC 0C 0x3C-F EC 0C EC 0C

24

slide-80
SLIDE 80

emacs (two copies)

Used by OS Emacs (run by user mst3k) Stack Heap / other dynamic Writable data emacs.exe (Code + Constants) Used by OS Emacs (run by user xyz4w) Stack Heap / other dynamic Writable data emacs.exe (Code + Constants) same data?

25

slide-81
SLIDE 81

emacs (two copies)

Used by OS Emacs (run by user mst3k) Stack Heap / other dynamic Writable data emacs.exe (Code + Constants) Used by OS Emacs (run by user xyz4w) Stack Heap / other dynamic Writable data emacs.exe (Code + Constants) same data?

25

slide-82
SLIDE 82

two copies of program

would like to only have one copy of program what if mst3k’s emacs tries to modify its code? would break process abstraction:

“illusion of own memory”

26

slide-83
SLIDE 83

typical page table entries

solution: same idea as kernel-only bit page table entry will have more permissions bits

can read? can write? can execute?

checked by MMU like valid/kernel bit

virtual page # valid? kernel? write? exec? physical page # 0000 0000 00 0000 0000 0000 0001 1 1 10 0010 0110 0000 0010 1 1 00 0000 1100 0000 0011 1 1 11 0000 0011 … 1111 1111 1 1 00 1110 1000

page table (logically)

27

slide-84
SLIDE 84

shared libraries

C standard library has lots of common code:

printf fopen qsort …

would like to not have multiple copies solution: multiple virtual read-only copies, one physical copy

28

slide-85
SLIDE 85

effjcient shared libraries

recall: linking

placeholders to fjll in with addresses need to change depending on where library ends up?

difgerent code depending on where library ends up?

  • ne solution: position-independent code

29

slide-86
SLIDE 86

position-independent code: one idea

Y86 encoding for jump: include target address alternate encoding: include target address - PC

“relative address”

handles jumps within standard library

30

slide-87
SLIDE 87

real shared libraries

Linux tool ldd — what shared libraries does this program use:

$ ldd /bin/ls linux-vdso.so.1 => (0x00007ffebbd49000) libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f4fac17c000) libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f4fabf74000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4fabbab000) libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f4fab96d000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4fab769000) /lib64/ld-linux-x86-64.so.2 (0x00007f4fac39f000) libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f4fab564000)

31

slide-88
SLIDE 88

position-independent code: indirection

what about code between libraries? what about changing the library code? use indirection:

/* instead of: */ call printf /* use something like: */ relative_movq LOOKUP_TABLE + 100, %rax // x86-64 syntax: movq LOOKUP_TABLE+100(%rip), %rax call *%rax

populate lookup table for each function used

lookup table not shared between programs

add version of move instruction that uses relative address

32

slide-89
SLIDE 89

page tables

program memory = virtual; real memory = physical each memory divided into fjxed-sizes pages each virtual page has a page table entry:

has location of physical page (if any) has valid bit has permission bits

all page table entries stored in page table permission or validity error triggers exception

33

slide-90
SLIDE 90

page table tricks

page tables let operating systems do ‘magic’ with memory key idea: OS doesn’t need to crash on a fault! instead: change page tables and rerun memory access

similar idea to trap-and-emulate will talk about these in future weeks today: what makes paging useful

34

slide-91
SLIDE 91

space on demand

Used by OS Program Memory Stack Heap / other dynamic Writable data Code + Constants used stack space (12 KB) wasted space? (huge??) OS would like to allocate space only if needed

35

slide-92
SLIDE 92

space on demand

Used by OS Program Memory Stack Heap / other dynamic Writable data Code + Constants used stack space (12 KB) wasted space? (huge??) OS would like to allocate space only if needed

35

slide-93
SLIDE 93

space on demand

Used by OS Program Memory Stack Heap / other dynamic Writable data Code + Constants used stack space (12 KB) wasted space? (huge??) OS would like to allocate space only if needed

35

slide-94
SLIDE 94

allocating space on demand

... // requires more stack space A: pushq %rbx B: movq 8(%rcx), %rbx C: addq %rbx, %rax ...

%rsp = 0x7FFFC000 VPN valid? physical page … … … 0x7FFFB

  • 0x7FFFC

1 0x200DF 0x7FFFD 1 0x12340 0x7FFFE 1 0x12347 0x7FFFF 1 0x12345 … … … pushq triggers exception hardware says “accessing address 0x7FFFBFF8” OS looks up what’s should be there — “stack” in exception handler, OS allocates more stack space OS updates the page table then returns to retry the instruction

36

slide-95
SLIDE 95

allocating space on demand

... // requires more stack space A: pushq %rbx B: movq 8(%rcx), %rbx C: addq %rbx, %rax ...

%rsp = 0x7FFFC000 VPN valid? physical page … … … 0x7FFFB

  • 0x7FFFC

1 0x200DF 0x7FFFD 1 0x12340 0x7FFFE 1 0x12347 0x7FFFF 1 0x12345 … … … pushq triggers exception hardware says “accessing address 0x7FFFBFF8” OS looks up what’s should be there — “stack” in exception handler, OS allocates more stack space OS updates the page table then returns to retry the instruction

36

slide-96
SLIDE 96

allocating space on demand

... // requires more stack space A: pushq %rbx B: movq 8(%rcx), %rbx C: addq %rbx, %rax ...

%rsp = 0x7FFFC000 VPN valid? physical page … … … 0x7FFFB 1 0x200D8 0x7FFFC 1 0x200DF 0x7FFFD 1 0x12340 0x7FFFE 1 0x12347 0x7FFFF 1 0x12345 … … … pushq triggers exception hardware says “accessing address 0x7FFFBFF8” OS looks up what’s should be there — “stack” in exception handler, OS allocates more stack space OS updates the page table then returns to retry the instruction

36

slide-97
SLIDE 97

allocating space on demand

note: the space doesn’t have to be initially empty

  • nly change: load from fjle, etc. instead of allocating empty page

loading program can be merely creating empty page table everything else can be handled in response to page faults

no time/space spent loading/allocating unneeded space

37

slide-98
SLIDE 98

page tricks generally

deliberately make program trigger page/protection fault but don’t assume page/protection fault is an error have seperate data structures represent logically allocated memory

e.g. “addresses 0x7FFF8000 to 0x7FFFFFFFF are the stack” might talk about Linux data structures later (book section 9.7)

page table is for the hardware and not the OS

38

slide-99
SLIDE 99

hardware help for page table tricks

information about the address causing the fault

e.g. special register with memory address accessed harder alternative: OS disassembles instruction, look at registers

(by default) rerun faulting instruction when returning from exception precise exceptions: no side efgects from faulting instruction or after

e.g. pushq that caused did not change %rsp before fault e.g. instructions reordered after faulting instruction not visible

39