Lab 3 Intro
Memory Management
1
Administrivia
2
Today’s Agenda
3
Memory in xk
Motivation
In Lab 3 you are required to implement multiple features which deal with virtual memory.
Therefore, understanding the existing virtual memory implementation in xk is essential
Refresher: Xk’s Virtual Memory Layout
This diagram from memory.md shows the layout of virtual addresses for a process in xk. (The addresses shown on left are virtual).
Key Takeaway: “virtual memory” is just a mapping from virtual addresses to physical addresses.
^From “memory.md” in the repo.
x86 Virtual Memory Datastructures
…but where do those mappings come from?
Thus a “virtual address space” is defined by a single page table, and “switching virtual address spaces” means to point %CR3 to a new page table.
xk’s Platform-Agnostic Virtual Memory
xk implements virtual memory data structures which:
Key point: you should manipulate these (rather than the page table directly).
Note that xk provides functions in vspace.c for manipulating these.
xk’s Platform-Agnostic Virtual Memory
You’ve already seen some of these platform-agnostic data structures, but the main ones are:
Throughout this lab you will become masters of wielding these structs.
Virtual Memory Summary
And that’s that! Just as a quick recap:
Now let’s talk about the lab!
Part 1: sbrk
(“set program break”)
Get more heap space
12
Refresher: User Level Heap Management
13
I’ve run out of heap space…
But modifying the page table is a privileged operation… how does malloc get more free memory?
What is the name of the system call that gets more heap space?
14
Enter sbrk(n)
What does sbrk() do?
You need to implement this syscall for Lab 3 Part 1
15
16
before
after
sbrk details
17
sbrk’s Byte Granularity
Wait a second… Physical memory can only be allocated in pages, and sbrk() operates on byte granularity!
If a new process calls sbrk(1) followed by a sbrk(100), how many pages should we allocate?
18
What to do for sbrk
What is provided for you:
What you need to do in sbrk():
If you find yourself writing > dozen lines of code, you’re probably reinventing the wheel…
19
Part 2: The Shell
finally some interactions
20
The Shell
How does the shell work?
21
How to Test the Shell
Remember when we said to make your lab 1 and lab 2 system calls as comprehensive as possible? Well, the shell will put them to the test!
What you need to do:
Part 3: Let it Grow
m o r e s t a c k
23
Grow Stack on Demand
How do we tell when more stack needs to be added?
What happens when you access beyond the current stack page?
24
Grow Stack on Demand
25
Stack Growth vs sbrk
Stack growth and sbrk call similar vspace functions to grow the respective vregions.
However, the growth trigger for stack growth vs heap growth is different:
What to do for Lab 3
What is provided:
What you need to do:
27
Part 4: COW Fork
28
Copy on Write Fork
Remember when you called vspacecopy to copy the vspace of the parent to the child?
Well, your lab2 fork implementation is inefficient because of how this function currently works :(
vspacecopy at a high-level:
29
30
Page A
Page B
Page C
Page D
Frame 1
Frame 2
Frame 3
Frame 4
Frame 5
Frame 6
Frame 7
Frame 8
Frame 9
Physical
VSpace A
Read/Write Virtual Page
Read only Virtual Page
Allocated Physical Frame
Unallocated Memory (physical or virtual)
31
Page A
Page B
Page C
Page D
Page A
Page B
Page C
Page D
Frame 1
Frame 2
Frame 3�(Copy of Frame 1)
Frame 4�(Copy of Frame 2)
Frame 5
Frame 6
Frame 7�(Copy of Frame 5)
Frame 8�(Copy of Frame 9)
Frame 9
Physical
VSpace B
VSpace A
Read/Write Virtual Page
Read only Virtual Page
Allocated Physical Frame
Unallocated Memory (physical or virtual)
We copied all the pages :(
As a consequence:
Your job in Lab 3 Part 4 is to fix this!
32
Optimizing Fork Memory
How would we optimize this to reduce the memory footprint of processes? What can we do better?
34
Page A
Page B
Page C
Page D
Page A
Page B
Page C
Page D
Frame 1
Frame 2
Frame 3
Frame 4
Frame 5
Frame 6
Frame 7
Frame 8
Frame 9
Physical
VSpace B
VSpace A
Read/Write Virtual Page
Read only Virtual Page
Allocated Physical Frame
Unallocated Memory (physical or virtual)
Page A in Vspace B could be safely set to writable (if there are no other vspace pointers pointing at Frame 7).
35
Page A
Page B
Page C
Page D
Page A*
Page B
Page C
Page D
Frame 1
Frame 2
Frame 3
Frame 4
Frame 5
Frame 6
Frame 7
Frame 8
Frame 9
Physical
VSpace B
VSpace A
Read/Write Virtual Page
Read only Virtual Page
Allocated Physical Frame
Unallocated Memory (physical or virtual)
COW Food For Thought
COW Food For Thought cont.
37
What to do for Lab 3
What is provided:
What you need to do:
Ok cool! I’m starting to understand Lab 3…
But wait! How do I distinguish stack growth vs COW page faults?
Handling Page Faults in x86-64
40
Error Codes
41
Error Codes
What will the error code be if the page fault was from touching the stack region of memory?�
42
Error Codes
What will the error code be if the page fault was from touching the stack region of memory?�
What about writing to a copy-on-write page?
43
A closer look at the vspace
Vregions vs Page Tables
Recall earlier we mentioned that there’s a platform-specific page table and platform-agnostic data structures?
struct vspace tracks them both:
45
How do we update the page table to reflect the vspace regions?
vspaceupdate(vs)
We call vspaceupdate!
When should you call vspaceupdate in Lab 3?
47
vspaceinstall(p)
We potentially have to vspaceinstall the vspace after we update it
When should you call vspaceinstall in Lab3?
48