0
Rust Runtime

Recap: Last Week
run::Process::new(program, argv, options)
spawn_process_os(prog, args, env, dir, in_fd, …)
...
Plan for This Week
• How the Kernel Makes a Process
– Virtual Memory

• Thursday: diving into fork.c

12 November 2013

Un...
From Class 3:

Batch Processing

Program

Computer
Center

Your Program Runs

Output: Invalid Operation
Charge: $174.32
12...
Process Abstraction
Provide each program with the illusion that it
owns the whole machine.
The best example of this way to...
Memory Isolation
Process 1 should only be able to access Memory Space 1
Process 2 should only be able to access Memory Spa...
Software-Based Memory Isolation
Original Code

“Sandboxed” Code

…
movq %rax, -8(%rbp)
…

…
movq -8(%rbp),%rdx
andq %rdx,%...
SOSP 1993
SOSP 1993

12 November 2013

University of Virginia cs4414

7
12 November 2013

University of Virginia cs4414

8
Hardware-Based Memory Isolation
Original Code

Running Code

…
movq %rax, -8(%rbp)
…

…
movq %rax, -8(%rbp)
…

Memory
Spac...
Virtual Memory
address in Process P

Virtual Memory Mapping
physical address
owned by Process P

User-level processes cann...
Getting Into the Details…

12 November 2013

University of Virginia cs4414

11
SOSP 1967

Procedure Base Register:
segment number of executing
procedure
Argument Pointer
Base Pointer
Linkage Pointer
St...
Generating an Address
18 bits

18 bits

218 = 262144
12 November 2013

University of Virginia cs4414

13
Addressing Mode selects:
Argument Pointer
Base Pointer
Linkage Pointer
Stack Pointer
12 November 2013

University of Virgi...
Descriptor Base Register

What does
MULTICS
need to do
to switch
processes?

12 November 2013

University of Virginia cs44...
12 November 2013

University of Virginia cs4414

16
1982

12 November 2013

"It used to be that programs were
easy to copy and change. But
manufacturers began to lose money
a...
Intel 80186

Intel 80286
First x86 Processor with Virtual Memory Support
“Protected Mode”

12 November 2013

University of...
http://en.wikipedia.org/wiki/File:Intel_i80286_arch.svg
12 November 2013

University of Virginia cs4414

19
Five x86-64 Processor Modes
Real Mode: pretend to be an 8086
20-bit direct-access address space
Protected Mode: “native st...
Protected State (can
only be modified by
the kernel):
RFLAGS (includes EFLAGS)
Includes I/O Privilege Level
Control Regist...
Address Translation

Memory

University of Virginia cs4414

Paging
Unit

Physical Address

Linear Address

Logical Address...
Accessing Memory

16 bits to select segment, 16- 32- or 64- bits to select offset
Actual addressable space for user-level ...
Memory

Paging
Unit

Physical Address

Linear Address

Logical Address

Segmentatio
n Unit

Computing
the Linear
Address

...
Fetching an Instruction
EIP
CS

Instruction Pointer (Offset)

32 bits
16 bits

Code Segment

13 bits

1

Table Index

12 N...
Segmentation Tables
Global Descriptor Table (GDT)
limit

base address

linear
address
space
0-264 - 1

Segments can overla...
Segmentation Tables
Global Descriptor Table (GDT)
Local Descriptor
Table
(per process)

13 bits – up to 8192 entries
12 No...
The GDT and LDT are just
data structures in memory!
Special registers store their
locations

12 November 2013

University ...
Logical Address

Segmentatio
n Unit

Linear Address

Paging
Unit
Physical Address

Memory
12 November 2013

University of ...
Linear Address

Logical Address

64
2

Segmentation
Unit

Paging

linear addresses
What would it cost to have 264 bytes of...
$10 per 1GB = 230 bytes

264 bytes = $10 * 234

$172B ~ ½ Google’s Market Cap
12 November 2013

University of Virginia cs4...
Paging
Memory

Paging
Unit

Physical Address

Linear Address

Logical Address

Segmentation
Unit

We don’t need to store t...
Memory

Physical Address

Linear Address

Paging
Unit

Image from Wikipedia
12 November 2013

University of Virginia cs441...
Overview (Intel 386)
32-bit linear address
CR3

Dir

Page

10 bits
(1K tables)

Page
Directory

Offset

10 bits
12 bits
(1...
Page Table Entries
CR3
Page Entry

Page Table
Page
Directory

12 November 2013

20 bits: physical
address of page
12 bits:...
386 Checkup
32-bit linear address
CR3

Dir

Page

10 bits
(1K tables)

Page
Directory

Offset

10 bits
12 bits
(1K entries...
How slow is this???!
Memory

Page

Paging
Unit

Physical Address

Dir

Linear Address

Logical Address

Segmentation Unit
...
Translation Lookaside Buffer (Cache)

Memory

Page

Paging
Unit

Physical Address

Dir

Linear Address

Logical Address

S...
Page Fault
CR3
Page Entry

Page Table
Page
Directory

12 November 2013

Physical
Memory

20 bits: physical
address of page...
How common are page faults?

12 November 2013

University of Virginia cs4414

40
top -o mem -stats pid,command,cpu,mem,mregion,vsize,faults
12 November 2013

University of Virginia cs4414

41
Physical Memory:
9106M used (2553M “wired” – cannot be paged out)
+ 5090M unused
= 14196M
where is my missing ~2GB???!

Vi...
How expensive is a page fault?

12 November 2013

University of Virginia cs4414

43
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
char *s = (char *) malloc (1);
int i= 0;
while (...
Charge
• Make progress on your projects: everyone
should have a clear idea what you are doing
now
• Will post more details...
Virtual Memory (Making a Process)
Upcoming SlideShare
Loading in...5
×

Virtual Memory (Making a Process)

826

Published on

http://www.rust-class.org

Implementing the process abstraction
Virtual memory on MULTICS
Virtual memory on x86
Segmentation tables
Page tables

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
826
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Virtual Memory (Making a Process)"

  1. 1. Rust Runtime Recap: Last Week run::Process::new(program, argv, options) spawn_process_os(prog, args, env, dir, in_fd, …) fork() int 0x80 libc: fork() jumps into kernel code sets supervisor mode linux kernel: fork syscall 12 November 2013 University of Virginia cs4414 1
  2. 2. Plan for This Week • How the Kernel Makes a Process – Virtual Memory • Thursday: diving into fork.c 12 November 2013 University of Virginia cs4414 2
  3. 3. From Class 3: Batch Processing Program Computer Center Your Program Runs Output: Invalid Operation Charge: $174.32 12 November 2013 University of Virginia cs4414 3
  4. 4. Process Abstraction Provide each program with the illusion that it owns the whole machine. The best example of this way to do things is Linux, which is an operating system, which is a program that keeps track of other programs in a computer and gives each its due in space and time. Guy Steele, “How to Grow a Language” HT: Anonymous for posting link in Piazza forum 12 November 2013 University of Virginia cs4414 4
  5. 5. Memory Isolation Process 1 should only be able to access Memory Space 1 Process 2 should only be able to access Memory Space 2 Memory Space 1 12 November 2013 University of Virginia cs4414 Memory Space 2 5
  6. 6. Software-Based Memory Isolation Original Code “Sandboxed” Code … movq %rax, -8(%rbp) … … movq -8(%rbp),%rdx andq %rdx,%rgx movq %rax, %rdx … Safe Loader Assumes %rdx is reserved and %rgx is protected and holds a mask for the memory segment 12 November 2013 University of Virginia cs4414 6
  7. 7. SOSP 1993 SOSP 1993 12 November 2013 University of Virginia cs4414 7
  8. 8. 12 November 2013 University of Virginia cs4414 8
  9. 9. Hardware-Based Memory Isolation Original Code Running Code … movq %rax, -8(%rbp) … … movq %rax, -8(%rbp) … Memory Space 1 12 November 2013 University of Virginia cs4414 Memory Space 2 9
  10. 10. Virtual Memory address in Process P Virtual Memory Mapping physical address owned by Process P User-level processes cannot access physical memory directly: all memory addresses created by process P are virtual addresses, mapped into physical addresses owned by process P 12 November 2013 University of Virginia cs4414 10
  11. 11. Getting Into the Details… 12 November 2013 University of Virginia cs4414 11
  12. 12. SOSP 1967 Procedure Base Register: segment number of executing procedure Argument Pointer Base Pointer Linkage Pointer Stack Pointer Descriptor Base Register 12 November 2013 University of Virginia cs4414 12
  13. 13. Generating an Address 18 bits 18 bits 218 = 262144 12 November 2013 University of Virginia cs4414 13
  14. 14. Addressing Mode selects: Argument Pointer Base Pointer Linkage Pointer Stack Pointer 12 November 2013 University of Virginia cs4414 14
  15. 15. Descriptor Base Register What does MULTICS need to do to switch processes? 12 November 2013 University of Virginia cs4414 15
  16. 16. 12 November 2013 University of Virginia cs4414 16
  17. 17. 1982 12 November 2013 "It used to be that programs were easy to copy and change. But manufacturers began to lose money as many people made copies of software and gave them to their friends. Now, many manufacturers have figured out how to 'copyprotect' discs. A copy-protected disc– like a cartridge–can’t be copied or changed. To our mind this is a disaster: Most people learn programming by changing programs to fit their own needs. This capability of customization is what makes computers so attractive. New ways of copy protection will probably be found soon. Until then, a computer owner may have to put up with being 'locked out' of his own machine.” Popular Mechanics, January 1982 University of Virginia cs4414 17
  18. 18. Intel 80186 Intel 80286 First x86 Processor with Virtual Memory Support “Protected Mode” 12 November 2013 University of Virginia cs4414 18
  19. 19. http://en.wikipedia.org/wiki/File:Intel_i80286_arch.svg 12 November 2013 University of Virginia cs4414 19
  20. 20. Five x86-64 Processor Modes Real Mode: pretend to be an 8086 20-bit direct-access address space Protected Mode: “native state” System Management Mode: platformspecific power management and security (separate address space) Compatibility Mode: pretend to be x86-32 IA-32e/64-bit Mode: run applications “For brevity, the 64-bit submode is referred to as 64-bit in 64-bit address space mode in IA-32 architecture.” 12 November 2013 University of Virginia cs4414 20
  21. 21. Protected State (can only be modified by the kernel): RFLAGS (includes EFLAGS) Includes I/O Privilege Level Control Registers CR0 bit 0: controls if processor is in protected mode CR3: page directory base register 12 November 2013 University of Virginia cs4414 21
  22. 22. Address Translation Memory University of Virginia cs4414 Paging Unit Physical Address Linear Address Logical Address 12 November 2013 Segmentation Unit 22
  23. 23. Accessing Memory 16 bits to select segment, 16- 32- or 64- bits to select offset Actual addressable space for user-level process in Unix: 247 bytes = 128TiB 12 November 2013 University of Virginia cs4414 23
  24. 24. Memory Paging Unit Physical Address Linear Address Logical Address Segmentatio n Unit Computing the Linear Address Segment selection is inferred from instruction type Logical (“General”, “Virtual”) Address Segment Selector 12 November 2013 Offset University of Virginia cs4414 24
  25. 25. Fetching an Instruction EIP CS Instruction Pointer (Offset) 32 bits 16 bits Code Segment 13 bits 1 Table Index 12 November 2013 University of Virginia cs4414 Ring Global or Local Table Only Kernel can write to Segment Registers 2 25
  26. 26. Segmentation Tables Global Descriptor Table (GDT) limit base address linear address space 0-264 - 1 Segments can overlap! 13 bits – up to 8192 entries 12 November 2013 University of Virginia cs4414 26
  27. 27. Segmentation Tables Global Descriptor Table (GDT) Local Descriptor Table (per process) 13 bits – up to 8192 entries 12 November 2013 How does the processor find the GDT/LDT? University of Virginia cs4414 27
  28. 28. The GDT and LDT are just data structures in memory! Special registers store their locations 12 November 2013 University of Virginia cs4414 28
  29. 29. Logical Address Segmentatio n Unit Linear Address Paging Unit Physical Address Memory 12 November 2013 University of Virginia cs4414 29
  30. 30. Linear Address Logical Address 64 2 Segmentation Unit Paging linear addresses What would it cost to have 264 bytes of RAM? 12 November 2013 University of Virginia cs4414 30
  31. 31. $10 per 1GB = 230 bytes 264 bytes = $10 * 234 $172B ~ ½ Google’s Market Cap 12 November 2013 University of Virginia cs4414 31
  32. 32. Paging Memory Paging Unit Physical Address Linear Address Logical Address Segmentation Unit We don’t need to store the whole address space in memory! Most of it is unused, and we can store rarely-used parts on the disk. 12 November 2013 University of Virginia cs4414 32
  33. 33. Memory Physical Address Linear Address Paging Unit Image from Wikipedia 12 November 2013 University of Virginia cs4414 33
  34. 34. Overview (Intel 386) 32-bit linear address CR3 Dir Page 10 bits (1K tables) Page Directory Offset 10 bits 12 bits (1K entries) (4K pages) Page Entry Page Table Physical Memory Page + Offset CR3+Dir 12 November 2013 University of Virginia cs4414 34
  35. 35. Page Table Entries CR3 Page Entry Page Table Page Directory 12 November 2013 20 bits: physical address of page 12 bits: flags user/kernel page write permission present University of Virginia cs4414 Physical Memory Page + Offset 35
  36. 36. 386 Checkup 32-bit linear address CR3 Dir Page 10 bits (1K tables) Page Directory Offset 10 bits 12 bits (1K entries) (4K pages) 20 bits addr / 12 bits flags Page Table Physical Memory Page + Offset CR3+Dir How many pages do we need to store the page table? 12 November 2013 University of Virginia cs4414 36
  37. 37. How slow is this???! Memory Page Paging Unit Physical Address Dir Linear Address Logical Address Segmentation Unit Offset CR3 Page Directory Page Table Physical Memory GDTR Global Descriptor Table 12 November 2013 University of Virginia cs4414 37
  38. 38. Translation Lookaside Buffer (Cache) Memory Page Paging Unit Physical Address Dir Linear Address Logical Address Segmentation Unit Offset CR3 Page Directory Page Table Physical Memory GDTR Global Descriptor Table 12 November 2013 University of Virginia cs4414 38
  39. 39. Page Fault CR3 Page Entry Page Table Page Directory 12 November 2013 Physical Memory 20 bits: physical address of page 12 bits: flags user/kernel page write permission present University of Virginia cs4414 39
  40. 40. How common are page faults? 12 November 2013 University of Virginia cs4414 40
  41. 41. top -o mem -stats pid,command,cpu,mem,mregion,vsize,faults 12 November 2013 University of Virginia cs4414 41
  42. 42. Physical Memory: 9106M used (2553M “wired” – cannot be paged out) + 5090M unused = 14196M where is my missing ~2GB???! Virtual Memory: 594G (total) top -o mem -stats pid,command,cpu,mem,mregion,vsize,faults 12 November 2013 University of Virginia cs4414 42
  43. 43. How expensive is a page fault? 12 November 2013 University of Virginia cs4414 43
  44. 44. #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char *s = (char *) malloc (1); int i= 0; while (1) { printf("%d: %xn", i, s[i]); i += 4; } } 12 November 2013 What will this program do? > ./a.out 0: 0 4: 0 8: 0 12: 0 …1033872: 0 1033876: 0 1033880: 0 1033884: 0 Segmentation fault: 11 University of Virginia cs4414 44
  45. 45. Charge • Make progress on your projects: everyone should have a clear idea what you are doing now • Will post more details on next deliverable (design reviews) soon Challenge: write a program that takes N as an input and produces (nearly) exactly N page faults. 12 November 2013 University of Virginia cs4414 45
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×