‫Hello and welcome back, guys.
‫In the last lecture we implemented paging in this lecture, we're going to make it so we can change
‫the pages and change where they point.
‫So just go to page and see.
‫And we're going to create a brand new function called paging get indexes and it'll return and encourage
‫us to just go in it.
‫Paging get indexes.
‫And we're just going to want a virtual address passed into here.
‫OK, we're also going to want a Unit 32 t pointer for the directory index, and this will be an index
‫out.
‫So we'll be setting whatever they pass to us here.
‫Right.
‫And we also want a table index out and we could just return the structure if we wanted.
‫But I think it is unnecessary.
‫So the first thing we need to do is ensure that the virtual address is aligned.
‫Now, it doesn't look like we created a function for that yet.
‫So we're going to create one.
‫What we're going to do is we're going to say all paging is aligned and we're going to pass in a virtual
‫address here.
‫Oh, not even a virtual address, actually, just to address.
‫And we're going to just return address.
‫Paging.
‫Page cise.
‫And if this is equal to zero, then it's line, yeah.
‫And we also need to scroll up and we need to include
‫Sebold Page and we should put this in the header file, actually, because we're going to be putting
‫the prototype in here eventually.
‫So as we pull up the.
‫Hey, back to paging Dr. C..
‫And we can see it's complaining that's because it needs to be converted to a Unit 32 T.
‫OK, so just cast it.
‫There we are.
‫So now we can say if the paging is not aligned for the virtual dress, then I will get Crane Response
‫variable here.
‫Uh, response equals minus INS and we're going to include status Hajj to give us access to that.
‫OK, and we're going to go to out as well.
‫We're crying out label down here.
‫OK, nice.
‫So the first thing this will do is it will ensure that the the virtual address provided here is paging
‫aligned and basically is a valid paging address because remember, the pages are required to work on
‫a four thousand eighty six byte boundary.
‫That's just how the paging works.
‫Right.
‫Which is why we need to ensure alignment.
‫Now, this function that we're creating will be responsible for taking a virtual address and calculating
‫which directory index in the directory entry table and which in the page table are responsible for that
‫virtual address.
‫So, for example, if you put in zero zero zero, the directory index will be zero and the table entry
‫will be zero as well.
‫So that makes sense.
‫So essentially this will resolve a virtual address to a directory entry and a table index.
‫So this is directory entry, right?
‫And this is our page table entry here.
‫OK, so it will resolve which directory entry that we are using for that virtual address and which table
‫entry we're using for that virtual address.
‫OK, so we're going to write the equation for this now.
‫OK, so to calculate the directory index to as follows so we go directory index out equals.
‫So we're setting that directory index.
‫Fairbrother the pass to us via pointer here.
‫Right.
‫So we're going to set that and we're going to cast to you and it's 32 t.
‫The virtual address.
‫And now we're going to divide it by total page in total anticipatable.
‫Multiplied by paging page size.
‫OK, so that's how we calculate the directory index.
‫So it's the virtual address divided by.
‫One thousand twenty four multiplied by four nine six.
‫OK, that gives us the directory index that we are to be using.
‫So we now need to implement the page table entry.
‫We need to now calculate what that is.
‫So to do that, we just go table index out equals Unit 32 T. And we could name these variables something
‫a bit more appropriate, really.
‫But never mind.
‫You understand what I'm saying?
‫So we're going to go a page in total entries per table multiplied by paging page size.
‫And then here we're going to go divide by paging page size.
‫OK, so I hope that makes sense and then in the out, we just return wrests.
‫OK, so let's now explain the math here then.
‫So I'm going to open Python so I can show you.
‫So let's say we have a virtual address of zero X.
‫Four hundred and five thousand, and let's just ensure that that goes into four, nine six, I'm pretty
‫sure does.
‫Yes, it does.
‫Excellent.
‫OK, so we've we've created a python variable here.
‫You don't have to do this in Python.
‫Just follow what I'm doing.
‫Just follow the video so I can explain this math to you.
‫Right.
‫So we go virtual addresses for the directory index now, right.
‫Directory index equals virtual address divided by one thousand twenty four entries per table, multiplied
‫by the page size of four nine six.
‫And if we now print the directory index, we get one.
‫So by multiplying 1024 by four, nine six, that gives us the total number of bytes in a directory entry.
‫So essentially is the entire.
‫Page table that that directory entries pointing to, right?
‫This is the total number of bytes we can store in there.
‫So to calculate the directory index, we divide our virtual address by that value, by dividing it,
‫by that value.
‫It gives us the directory index that we care about because we know that each directory index points
‫to a page table that can manage this many bytes.
‫OK, so that is how we calculate the directory index.
‫I hope that makes sense.
‫So when are we going to calculate the page table index here?
‫And that is virtual address.
‫OK, remind us what is Modulus here.
‫And we also do want those 24 Mogwai by four nine six in here, right, and then we divide it by four,
‫nine, six, and we now print the page table index that gives us five.
‫OK, let's just try that once more with a different address.
‫So we're going to go VirTra dress equals zero x 400000, right?
‫Oh, that was fourteen thousand four hundred thousand.
‫And now we're now going to go print virtual address.
‫Modulus four nine six multiplied by one or two for.
‫OK, and that gives us zero, and now we divide that by four, nine, six, and we still get zero.
‫OK, so that is how that's working, guys.
‫We go virtual dress equals zero x 400000 and we change this to a one 400 1000.
‫We print that again.
‫Now we get one.
‫So you can see this is the equation for calculating the table index and calculate the directory.
‫The equation is print virtual address divided by four nine six multiplied by one or two four.
‫OK, so we can see that four four one zero x 400 1000.
‫We have a directory index of one and a page table index of one.
‫So the this is the math basically, right, or what I should say is this is the directory index here,
‫right?
‫This one and this one here is the page table index.
‫So I did that the wrong way round, but you know what I mean.
‫Right?
‫So that's how that's working.
‫So this equation here calculates the page table entry.
‫This equation here calculates the directory.
‫I hope that makes sense.
‫OK, so now that we've implemented page and get indexes, we are ready to implement the paging set function,
‫which will basically set a page for us.
‫So we're going to go into the paging set Unit 32 T.
‫Directory, so expect a directory provided to us.
‫And then we're going to say void virtual virt for virtual address unity to t Vall.
‫OK, so this will set a page table entry, basically one of these to the value provided.
‫So we're obviously expected to, you know, all the flags on and all this sort of stuff.
‫So this value will obviously contain the physical address that we want this virtual address to point
‫to, and it will also contain flags.
‫OK, because it is essentially the page table entry.
‫Right.
‫OK, so what we're going to do then, we need to check alignment.
‫Very first thing.
‫Paging is aligned virtual address.
‫Oh, and by the way, how you change this to that actually.
‫So by the way, before we continue any further, when we implemented Ahepe, you'll notice I purposefully
‫use the block size four nine six.
‫This means that anything you allocate on the heat because it's four four nine six aligned, it can be
‫used with paging is completely compatible.
‫So make sure you don't change the block size of a heap because they are paging aligned.
‫So if you've already done that, you should change that back four nine six.
‫Right.
‫Because when we came along, we can pass this straight into the paging.
‫It works because it's aligned.
‫If it's not aligned for 096, you'll have problems.
‫OK, so we're now going to return minus E invalid argument.
‫OK, so if then if the paging is an aligned invalid argument.
‫Good.
‫So we're now going to go unit three to T directory index equals zero.
‫Unit thirty two T table index equals zero and we're going to address equals paging get indexes and it's
‫going to ask for the virtual address.
‫So we're going to pass invert, it's going to ask for the directory out.
‫So we're going to pass in the directory index ampersand directory index for the address ampersand table
‫index, OK, for the table out.
‫And now if the rest is below zero, then there was an error and if there was an error, we just returned.
‫OK, we don't need to be using go to here and stuff because we are not cleaning up any memory.
‫OK, so we are now going to go unit 32 t entry equals directory directory index.
‫So what this will do, this will get our page directory entry.
‫OK, so that's what this does here.
‫This entry.
‫Right.
‫So this, this entry contains the pointed to the page table.
‫Right.
‫So we're now going to go unit three to T table for the page table because unity to T and you know,
‫this is a pointer pointer now and we're going to go entry ampersand.
‫So we're going to do some bitwise logic now to extract only the address.
‫OK, we don't want the flags, so we're going to go zero x f f f f f zero zero zero.
‫OK, now that should be right.
‫So each F here represents four bits.
‫So four, eight, 12.
‫16.
‫Twenty, OK, so that is correct, that's 20 beds, and if we look here, it's 11 to 31, that's 20
‫beds.
‫We want to extract that.
‫So you did if you did 31 minus 11, you get 20.
‫Right.
‫We've extracted the correct correct amount ignore in the last 12 beds here.
‫OK, and remember, the bit number starts to zero.
‫Right.
‫Which is why you only see 11 here.
‫So I just want to make that clear in case you're a bit confused.
‫Right.
‫OK, so that we've now extracted the page table address and we've assigned it to this point so we can
‫access the table directly.
‫So now the next page is very simple.
‫We just go table, table index equals value and it's as simple as that.
‫So now whenever we call page and set and we pass the value, we are expected.
‫By the way, don't forget the semicolon here.
‫We are expected to pass in the physical address along with the flags.
‫So this will be our page, table entry, physical address.
‫And there's the flags there, OK?
‫And that's what gets set down here.
‫And obviously here we extract only the address from the page directory entry.
‫And then once we have that address, we can point to it, OK, and then set it like that.
‫And the reason this works is because, remember, I said the the lower bits aren't used when we are
‫applying to four, nine, six.
‫And since every page is aligned to four nine six, we don't have a problem with this since every page
‫table entries are lined up for nine six.
‫OK, so there's our page and set complete.
‫We can now actually try that, I think.
‫So what we're going to do is we're going to copy and paste these these prototypes here into the file.
‫Right.
‫So Page set will go there into the header file and page is aligned.
‫Also deserves to be there.
‫Right, in case we use that elsewhere for whatever reason.
‫So once we now that we've done that, we can actually start manipulating these addresses.
‫So we're going to do that now.
‫OK, so now go to Canada and see and we're going to actually map some memory now.
‫So just above the neighbor page and we're going to go char PTA equals Kazal up four nine six.
‫OK, so we've created a chunk of four thousand eighty six bytes of memory and we're going to map the
‫virtual address you x 1000 to this address and it'll work fine because Casula will return a four thousand
‫ninety six byte aligned address, assuming that you didn't change the chunk size when we are creating
‫the heap.
‫So we're now going to do is we're going to go paging set and we're just going to pass in the directory.
‫So paging for paging four gigabyte chunk get directory, OK, and yeah, that could be a bit cleaner
‫but it is Fynes for demonstration.
‫So we're going to go XXXIV thousand here, but it expects a vote pointis so we're gonna have to cast
‫that vote pointer as well.
‫Guys, for the virtual dress.
‫Another value will be a the pointer, but we need to cast it to you in a three to T right.
‫So we go unity to t pointer Kasab and then we're going to go all paging axis from all or paging is present
‫or paging is writable.
‫OK, um so you can see here then we passed the address, the physical address now pointers, our physical
‫address, we pass that and we basically all of these flags onto it now because it's four thousand eighty
‫six aligned.
‫We know that all of those lower bits for these flags are zero.
‫So we so we know we can use them.
‫Right, because it's four thousand eighty six bytes aligned.
‫Right.
‫So we passing paging axis from all paging is present and paging is writable.
‫OK, so that is our physical address.
‫So our virtual U.S. selects a thousand should now map to pointer.
‫So whatever we do to address a thousand will affect physical address PTA.
‫It will not affect physical address X a thousand.
‫So just to demonstrate this then if we now did char PTA two equals char and we'll you know, we'll cast
‫six a thousand here.
‫Right, so we make a pointer to 6000.
‫We now go pointed to zero equals a pointer to one equals B and we now pointed to and this will work
‫because we kasar out.
‫Right.
‫So the second index indexed to would be null, terminated because we zero did.
‫So we now did this and we pointed to you will seab.
‫Right.
‫So that's just demonstrate that, make clean build that S.H. City been.
‫Kumu and you CAIB, OK, but now do Prince Peter and see what happens if we now run that again recompiling
‫first guys, uh, we now run that again and we only see one a B..
‫And the reason for this is because this code actually needs to be done after April Pageonce.
‫So just paste that there and we're going to run that again.
‫So back we go.
‫Make clean, build that S.H. Cribben kumu and now you see a baby.
‫So how is this possible?
‫Well, I'll point to two points to virtual dress.
‫A thousand, right.
‫We then set Amba and we print pointer to which basically says print xox a thousand.
‫However, our virtual address is map to the physical address, PTA, the Casula return.
‫So it might be Xerox one million or something like that.
‫Yeah.
‫Uh, and we mapped 6000 to that address.
‫OK, so then when we go print PTA, we're saying print zero x a million or whatever, but the page table
‫Fazzio X but the page table entry for Xerox, a million points to Xerox, a million.
‫It's LENNEAR.
‫OK.
‫Whereas the the address for pointed to is a thousand points to zero, Maximilien, you know, let's
‫just assume that case over 26 million, right.
‫So when we do print pointed to the memory management union, the CPU will see, which just selects 1000.
‫It will resolve it.
‫A physical address costs a million.
‫OK, and when we go print Xerox a million, it will go into the virtual address and it will see that
‫it maps to fiscal year six million.
‫OK, so both point and point to two point to zero, Maximilien, that at the at the page table level.
‫So that's how that's working guys.
‫So even though point to two is Xerox a thousand and Peter is a million, they both point to the same
‫data at the processor level in the page tables.
‫So Page is incredibly useful, guys, and I hope now you have a taste of how that's working.
‫So I hope that made sense.
‫Right.
‫I hope that made sense to you because these things can get confusing.
‫Right.
‫Let me just show you something else.
‫And if I comment now, paging said, OK, and we run the exact same code as before.
‫What you think you're going to see.
‫And of course, my mistake that we need to put pointed to here.
‫So I'm going to recompile that.
‫What do you think we're going to see?
‫Right.
‫We only see one B and why?
‫Because Xerox, a thousand virtual address is no longer mapped to physical address PTA, OK?
‫And because of that reason, we are now writing to the physical address because a thousand not a physical
‫address, six million or so pointer here, which is which return Xerox a million, eight points to six
‫million, but the Xerox thousand point zero thousand.
‫So the point represented by PR is never actually written to.
‫OK, so if we put that back then, you know, obviously what you think you're going to see.
‫A baby, so the benefits to paging are basically endless, right?
‫Uh, you know how we print to the video memory?
‫You know, that lecture we went to that address.
‫Right.
‫And then it outputs to the to the terminal.
‫Right.
‫We can use paging to map that address to some buffer in the process memory.
‫Right.
‫So then so then when one would task switching, when we're multitasking, it can still write to the
‫video memory.
‫Apart from us not writing the video memory, it's writing to a buffer.
‫Right.
‫And then when we switch back to the process, we can copy all of the all of all of that process, video
‫memory buffer and copy it back into the physical real video memory buffer so the programs can just act
‫as normal as if they're always right in the video memory.
‫I don't have to worry about if they're the visible process.
‫You know, we can abstract it all out with paging.
‫That's just one of the benefits.
‫Now, one thing I want you to know, we can map hardware addresses with paging because because the request
‫has to go through the CPU.
‫So when we have some machine code to ask for 6000, it goes to the CPU and it resolves to the page tables,
‫whereas the physical hardware doesn't do that.
‫So we can't map the video memory address somewhere else for the video card itself using paging.
‫So in other words, the video card will always look at the physical address.
‫We can't we can't map that with paging not not not this.
‫What?
‫Not like this at least.
‫So I hope that makes sense.
‫Any questions, please reach out.