Now, let's talk about how virtual memory works, and we're going to introduce two concepts here, virtual and physical addresses and some acronyms for them.
So how does virtual memory work?
The basic idea is we have these separate memory spaces.
So virtual memory is what a program sees.
Physical memory is the physical RAM in the computer.
When you install memory in your computer or you buy a computer with memory, this is what you're paying for.
What the program sees, the virtual memory, this is what the ISA promises.
So remember in MIPS, this promises the virtual address.
So if we do a load R4,024 R0, this is going to access a virtual address 1,024.
So the MIPS ISA promises a certain virtual memory address space, but the physical memory is what you have in your machine.
So if you have two gigabytes of RAM installed,
you're going to have physical addresses from 0 to 2 to the 31 minus 1, that's 2 billion bytes of memory.
So virtual addresses or VA,
we're going to abbreviate the virtual addresses VA, this is what the program uses, and in MIPS it's a 32-bit address space.
The physical addresses or PA, is what the hardware uses, or And this is determined by how much RAM you have installed.
If you only have a little bit of RAM installed, your physical address space, or your PA, is going to be small.
If you have a lot of RAM, it's going to be big.
But now we have separate address spaces here.
So each program is going to have its own virtual address space, and we have a physical address space for our memory.
So how do we make this work?
So, the program executes a load, and the load is going to specify a virtual address because that's the way programs work.
So, here's our processor, it's running the program virtual address space, and here's our load.
What's the physical address going to be here?
Well, it's just going to be a thing.
So the processor is going to access a virtual address 1024.
That's what this instruction is going to do.
Now, the computer is going to translate this virtual address to the physical address in the memory.
So we're going to have a translation here.
This is our map from virtual address to physical address.
And it's going to tell us how to get the data over here.
So here's the data we want for R3.
This map is going to tell us how to get this.
So we go ahead and tell the translation or the map here, we want virtual address 1,024.
It looks it up in its map, it oh, virtual address 1,024 maps to physical address 2.
So it goes over here to physical address 2,
and it goes over here Now, if the data is not in memory, it goes and loads it from disk.
In this case, it is in memory, so we're going to take this data here and we're going to return it.
So we took virtual address 1024, translated it to physical address 2, and now we send this back so we get the data.
Now, we go ahead and do another instruction.
This instruction here is going to load virtual address 512.
So we go over to our translation, ask it where 512 goes, it hey, it to physical address 12.
So we get on to physical address 12 here, go ahead and get our data, and we send it back.
Now we do another instruction, this is an add instruction, it's just using registers.
So we don't need to do any translation here.
And then we go and we have another instruction, this is going to access virtual address 700.
So we go to virtual address 786.
The map tells us it's on disk.
So what do we need to do with to get it?
Well, we need to load it into memory.
So we go ahead and load this data into memory.
Once we've loaded in memory, now we go and update the translation.
Okay, so now we no longer want to translate address 786 to disk.
We want to translate address 786 to disk location and memory.
Now when we put in virtual address 786, we get out physical address 4.
We ahead and load it from data from the memory and we return the value.
So, program executes and load with a virtual address, the computer translates it into the
we it from disk, and then the computer reads the RAM using the physical address and returns it to the program.
That's how virtual memory works.
What address is located here, so address r12 has the add, sorry, value and the program issues this load.
What location in RAM is going to be accessed?
Well, we need more information here.
The program is accessing virtual address 1536,
that's what this is going to do here, but we don't know what physical address that's going to be.
It's in the program's virtual address space and we do this load.
This load is going to generate virtual address 1536,
but we're going to have a map and the map's going to tell us which physical address it is.
Now, if I don't tell you what the mapping here is, you have no idea what location in RAM this address is going to access.
So we need more information to know what memory we're actually going to access when we issue this instruction.
There's another question about using too much If a program uses more data that can fit into RAM, where does the data go?
Well, it's going to go to disk.
If it doesn't fit into the memory, we're going to use virtual memory to map it to disk.
So this load's going into our table and we didn't have enough space and memory in physical memory for this virtual address.
The translation table, the map here, will tell us that it's on disk and we'll know it's on the disk.
And so you can put the data on disk if it doesn't fit into memory.