‫In this lecture, we are going to create paging so in your source directory and in the memory directory,
‫just create a new directory and we'll call it paging.
‫OK, we're going to create a new file called Paging Dr. Hajj.
‫OK.
‫And obviously, this will be the Hadifa for our paging functionality and we'll get to a new file as
‫well called Page and Dot see inside of that page and directory.
‫And that's just going to include paging Dr..
‫Now, just for reference for you guys, I'm going to get up the Osteria article for paging.
‫Just see you guys also have a resource to use.
‫So you can see this basically explains what I explained to you in the presentation.
‫You can see the page directory entries here and the page table entries here.
‫Right.
‫It also shows you how you can set up these page tables and enabling paging, but I'm going to cover
‫all that with you now so you don't have to worry about that.
‫I just wanted you to realize that there's other resources to cross-reference with this lecture as well.
‫And of course, if you run into any problems, feel free to send a message.
‫So, OK, so now that you're aware of this, we can continue now.
‫OK, so back to Page and R.H. And we're going to create some definitions here.
‫So we're going to go to find paging cash disabled zero, be one, two, three, four, so forth.
‫But here down is one.
‫Five, six, seven, eight.
‫So this little bit mass that I'm doing, by the way, it refers to this table by here page table entry
‫here, right?
‫Stuff like that.
‫So if we look at the pit zero one, two, three, four, before we can see zero one, two, three,
‫four, we can see see which is the cash disabled.
‫So I just want to let you know that we're just creating definitions for this pet mask here.
‫So we're going to create another mass now.
‫Paging right through.
‫This is the right through bit, Searby.
‫One, two, three, four.
‫And now comes the one.
‫One, two, three.
‫OK, and we just kind of line up the binary there, keep it nice and neat.
‫So we're going to go define paging access from all Serabee one, two, three, four, five.
‫Now comes the one and now another.
‫Zero zero here, we're just going to line up the binary.
‫Keep it clean.
‫OK, so we've got that, so the paging access from all, by the way, that is built to here.
‫So 012, that's our user supervisor bed, right.
‫And if we look in the user supervisor bed, we can see this says this better controls access to the
‫page based on privilege level if the bit is set than the page may be accessed by all.
‫So that's why we call it paging access from all.
‫So whenever we pass this bet a supervisor or use the space can access that page.
‫This does not mean all processes can access that memory because the processor has no concept of processes.
‫So I just wanted to make that very clear.
‫Our paging axis from orbit is simply the supervisor bed, which states that if the bed is set, then
‫it can be accessed from all privileged levels, not just the supervisor.
‫Privileged level, ring three, ring zero in one, room two and so on can access it if this bit is set.
‫If it isn't set, then only the supervisor rings can access that page.
‫OK, so we're not done.
‫We still have some more so defined paging is writable zero b one, two, three, four, five six one
‫zero by there and just space it out as best you space it out like I've done here guys, because then,
‫you know, if you've accidentally put in a zero or something or too many bits.
‫Right, you could use hexadecimal with bit mask's.
‫I always prefer to use binary.
‫I think it's much more clear personally.
‫OK, so the next one then define
‫paging is present.
‫OK, so we go zero b one, two, three, four, five, six, seven, eight.
‫There you go.
‫And we're going to just space that out like that.
‫OK, excellent.
‫And we're just going to create a definition here to find paging total entries per table 1024, because
‫we don't really want to use magic numbers here, guys.
‫And OK, that is it for the definitions.
‫OK, so now comes our structures.
‫So we're going to create a structure here struct paging four gigabyte chunk.
‫So obviously we're going to have a patient directory in here that will map the entire four gigabyte
‫address space.
‫Right.
‫So then we just go unit three to T directory entry and we're just going to include up here as an advantage.
‫And we'll also include C def Don Hajj.
‫And there we are that resolves you in a 30 to.
‫OK, so using this structure, then we can specify four gigabyte page in chunks and this is a pointer
‫to the page directory.
‫OK, so when are we going to create a functioning page and see responsible for creating a four gigabyte
‫page and Schunk for us.
‫So we just go struct paging four gigabyte chunk.
‫OK, so this is a return, a pointer to one of these structures, right, and we're going to go paging
‫new for gigabyte and then we're going to pass in the flags in here.
‫OK, so first thing we need to do then is create our directory.
‫So we go directory equals Kazal, OK?
‫And obviously, guys, we need to include the heap's just go include memory, keep keep dot.
‫OK, and then that'll resolve Kasar for us.
‫So we're going to go kazal ok.
‫Oh and look here, we actually have an implement excel yet if we just go to help guys keep we can see
‫we have Malot but we don't have K.
‫Right.
‫And this is OK, we're going to create Kazal OK now.
‫So just go void kazal ok.
‫And the Casula could basically mallock and set the whole thing to now.
‫OK, so we set the size there.
‫OK, however void pointer equals came along size.
‫OK, and then we're just going to go memsahib PTA and we're going to need to include that.
‫Had a file.
‫Guys scroll up here.
‫We're going to go include memory memory dot.
‫OK, so we're going to go meme's said Peter.
‫And this is the, the we want to set it to and and there's the size there.
‫OK, so obviously if the pointer is no, then we'll return zero here, right.
‫Otherwise, we'll return here, so, yeah, I did forget to do that.
‫Guys, this is the prototype for this.
‫This is how you how you implement this.
‫OK, so we mallock the the pointer and then we set it to zero and that that makes our case.
‫ALAC, I just copy and paste the prototype here now and we're going to put it in the header file.
‫OK, so um yes.
‫No problem.
‫So now we have excellent.
‫Excellent.
‫And we're just going to go size of you know, 30 to see multiplied by one thousand twenty four unless
‫a magic number.
‫We're going to change that.
‫Now go back to page and page in total entries per table.
‫OK, yeah, this is OK.
‫We can use this.
‫So so we'll just paste on here.
‫Right, because we know that we can use this for both the directories and the page table because we
‫know that 1024 entries, OK.
‫OK, so that creates our directory.
‫OK, and what we now need to do is we need to start initializing this array.
‫So if we just go for installer I could zero Ebola, one thousand twenty four and actually no paging
‫total entries per table I plus plus.
‫Yeah.
‫So I'm going to loop through that entire directory table now which is basically.
‫Directory page, directory entry, right?
‫So each of these page directory entries and we're looping through them.
‫OK, so obviously we need to create a table for each entry in the directory.
‫So we're going to create a table now.
‫So we're going to give you and 30 Tuti.
‫Uh, page, table entry or you know what, we can just call it entry, that's fine.
‫Entry equals Kasar block size of Unit 32 t multiply by page in total entries per table.
‫OK.
‫So this century now we can apply to we can apply to directory entry zero, for example, and this is
‫our page table entry, right.
‫Which is an array of 1024.
‫Uh, page table entries, so that makes sense.
‫So essentially when we said directory zero or whatever, so this entry and we can't just do that because
‫there's flags and stuff involved here, we have to all those on.
‫Right.
‫But when we do something like this.
‫Right, the process, when it goes to directory zero here it lnc this entry as our entry table and this
‫contains 1024 page table entries which explain how the man was laid out.
‫Take a look at the presentation.
‫If you're still confused, go back and go through that, because I explained that quite in depth.
‫So anyway, get rid of that.
‫So here we've now accelerate our entry table, our page table entry table.
‫So we need to now loop through all those entries and now initialize those.
‫OK, so before we go any further, this function here is going to create the page directory and page
‫entry tables.
‫Right.
‫But they're going to be mapped linearly, so in a linear fashion.
‫So, for example, address selection will point to address zero zero zero zero physical addresses,
‫zero.
‫Any address we have, let's say, for example, something like something like this would point to something
‫like that.
‫Just see what I mean, you know, so we're not going to manipulate the pages.
‫So they point to different parts of memory.
‫This function is not responsible for that.
‫This function just gives us a nice linear memory mapped linearly as the memory looks in physical ram.
‫It brings us back something like that, OK?
‫And then once we have our paging four gigabyte chunk will then have functions that will implement later,
‫which will allow you to change individual parts of that.
‫So I could change, I know, page five or something.
‫So point to a different physical address.
‫OK, but this function will return something linear, so every virtual address points to the same physical
‫address.
‫So it's almost as if you never even enabled paging in the first place.
‫OK, so now I've covered that we can carry on going, going through this implementation here now.
‫OK, so the next step then we need to have another follow up.
‫And again, paging tolerances per table.
‫And before we go any further, just go at the top here and we're just going to create an offset and
‫set that to zero.
‫OK, and here we're going to go entry B equals offsets plus B, multiply by.
‫Page sighs And we don't have that yet, so go to Page and we're going to define paging page size, OK,
‫and that'll be four nine six.
‫OK.
‫And go back to Page and Zazi.
‫So we're going to go multiply by paging page size.
‫And then we're going to draw on the flags, OK, and that should give us everything we need.
‫So I'm just going to get a python a minute so I can show you how this works.
‫Right.
‫Our offices start to zero, so we go zero plus zero multiplied by four nine six.
‫OK, and then we all on the flags.
‫We don't have to own the flags for now.
‫Let's just worry about the address so I can show you.
‫Yeah.
‫So we can see that because this zero so that makes the address for our page table entry to map to zero
‫physical address, which is what we wanted for the first entry map to zero.
‫Right.
‫And we can see where manipulating these bits here fit eleven to thirty one.
‫OK, and if you remember, I told you the reason we don't use the full 32 bits for the address is because
‫of the alignment.
‫The alignment will only will only require the 20 bits for any address aligned to four nine six.
‫OK, so obviously but zero to 11 are the flags and so on and so on.
‫OK, so again, when we're still looping through that page table entry there, the office is still zero,
‫but now we have an index of one.
‫Yeah.
‫So now it maps, it maps the second page to four nine six address four nine six.
‫We have index two for page eight one nine two.
‫So this is correct.
‫OK, so this is perfect.
‫And the reason we use the offset is because once we finish the table, we need to do offset plus equals
‫page in total entries per table multiplied.
‫By our page size.
‫OK, so now let's say we go through 1024 entries, so we finish the first table, OK, 1024 multiplied
‫by four nine six is equal to four one nine four, three or four.
‫So if we do four one nine four three or four plus zero times four nine six, that gives us the same
‫value.
‫Right.
‫We copy and paste this into a calculator here because I just want to break this down so you can see
‫how it works.
‫Right.
‫So we paste this in here is decimal and we switch to Hex.
‫We can see that's the address for the second page table.
‫OK, so for example, when we go to the second page directory, this is the virtual addresses.
‫We will actually be working with six 400000.
‫Right.
‫So now we put the math in here when our B is now equal to one.
‫Yeah, one times four nine six.
‫So this is the second virtual address for that for the.
‫Second page table index one, yes, for one is you understand?
‫So if you still don't get that, watch the presentation again.
‫If you're still confused, drop me a message.
‫I'll try and break it down a bit more.
‫But I hope that gives the gist of it.
‫And of course, this article here as well is very good.
‫OK, so before we go any further than when I now going to go directory I is equal.
‫And we're going to cast this here.
‫Flags, OK, and by default, we're also going to make this page rideable, so was it paging is right,
‫OK, and the reason we do that is because we want to put it in the directory to be said is rideable
‫so that the entire page table is readable and then the individual pages in that page table, they might
‫not be readable.
‫Right.
‫But we don't want to restrict the whole directory, which is why we all that on.
‫OK, so we're nearly done here.
‫We now need to create a paging four gigabyte chunk.
‫OK, so we'll go paging four gigabyte chunk.
‫Chunk for gigabyte equals Casula size of struct, paging four gigabyte chunk and now we simply go chunk
‫four gigabyte directory entry equals directory and then we return the four gigabyte chunk.
‫So there we go.
‫That creates a page directory with a bunch of page tables that essentially cover the entire four gigabytes
‫of RAM and then we can manipulate these values anywhere.
‫We like to point virtual addresses to anywhere in memory that we like anywhere in physical memory.
‫So you a thousand maybe it points to zero x 5000.
‫OK, so now we're going to make small functions, then we're going to go unit three to T paging four
‫gigabyte chunk to direct get directory, something like that.
‫That'll be fine.
‫Yeah.
‫And then they'll pass in the paging four gigabyte chunk and we'll just return Choung Directory entry.
‫OK, so we can use this function to just extract that from the structure.
‫OK, rather than is the structure directly.
‫OK, so we're now going to create a paging switch function which will be in charge of switching between
‫the pages.
‫So this looks set to directory.
‫OK, we expect a directory pass to us here, OK, and you know, we could make a TYPEDEF if you wanted
‫to, but you and Theta T is fine for now.
‫So we're going to call paging load directory, which we haven't made yet, by the way.
‫And we're going to pass in the directory and then we're just going to go current directory equals directory,
‫OK?
‫And we haven't created current directory yet either.
‫So we're going to create current directory.
‫So we're going to go unit three to T current directory.
‫Just set that to zero and that's going to be static.
‫We don't want this accessible outside of this file.
‫And there we are.
‫That works fine.
‫Now, as for paging load directory, we're going to rate the prototype because this will be an assembly
‫function Savoy paging load directory, and it's going to accept a unit 30 to 40.
‫And there we go.
‫So that's fine.
‫So what I now need us to do is copy and paste these into the header file.
‫OK, paging switch and paging you for gigabyte.
‫We don't need to put paging load directory because it's a very low level function.
‫We don't want people calling that.
‫We want them to be calling paging switch.
‫So we're going to create a new file called Page in the ASRM now.
‫OK, so we're just going to go new for paging Diesem.
‫Page 32, section diesem, which they, too, because we're writing 32 bit assembly, I think it defaults
‫to that anyway.
‫But just best to put it, uh, so we're going to go paging load directory.
‫OK, so we're going to push the old base, point out we're just creating a stack frame here.
‫OK.
‫So we move the value they pass to us, you know, the point of value into the X register and how we
‫move that into the C3 register and then, you know, we return from the function.
‫Uh, so, yeah, I remember I was saying how the CFE Register should contain the address to our page
‫directory.
‫OK, so here then we're going to go global paging load directory and we're also going to go global enable
‫paging because we're about to create a assembly function for that to sit down here, enable paging.
‫And we're just going to go push EBP, which is creating a static frame again, guys, OK, move the
‫Stackpole to the base appointer.
‫Move the move the closer you register to the cash register, because we can't change C0 directly and
‫we're going to order the register and we're going to apply bête 31, so it enables paging.
‫So one, two, three, four, five, six, seven.
‫I think that's right.
‫And now we're going to move back into the C0 register, OK, which will change that, the process of
‫configuration and make it use paging.
‫So there we go.
‫That is done.
‫Now, if you go to paging Dr. Hajj, we're going to create a function function prototype so we can call
‫this anywhere in our program.
‫So we're going to go void enable paging.
‫Simple as that.
‫Now, you do not call, enable paging and we'll move that down here because it's cleaner.
‫We do not call, enable paging until we have created a paging four gigabyte chunk and we switch to that
‫page directory.
‫OK, if they don't have that and we enable paging, the system's going to panic.
‫So make sure that we switch to a page directory before enable paging.
‫OK, so if we now go to Colonel Dot C, we're going to scroll up and we're going to include paging paging
‫Dr. Hajj, OK.
‫And before we go any further, let's just change our makefile so it assembles and compiles these new
‫files we've made.
‫So go to the Makefile.
‫We're going to scroll down.
‫We're going to copy and paste this.
‫And we're going to go MEMRI paging, paging Dr. Memory, paging, paging Dr. C..
‫And we're just going to go paging and hear me paging and and the source lobby paging, paging Dr. C
‫and the follow, we build memory paging, paging Dr..
‫OK, and now we're going to get the same for the assembly here.
‫So we're going to copy this one, paste that in here.
‫So we're going to go build memory.
‫Paging paging Dr. Assem.
‫Dotto, source memory paging.
‫Paging Diesem source memory paging.
‫Paging Dr. Diesem built memory paging paging Dr. HSM.
‫And I were going to copy and paste, build memory, paging, paging, and we're going to copy and paste
‫that appear into the files.
‫OK, and we're going to paste it again and we're going to change this to assembled, OK, so we have
‫these two as the requirements.
‫OK, excellent.
‫So if we now, uh, just scroll up and we're going to create a new folder in memory in the built in
‫the build directory, guys, and we're just going to call it paging and we're going to create a keep
‫file in here if you use it like I do.
‫OK, excellent.
‫So we're now going to build that to just go make clean up forward slash, build LSH and we see no follow
‫directory candidates.
‫See, that's fine.
‫We need to do MEMRI forward slash, paging forward slash.
‫Paging Dr. A.H..
‫My mistake, Paula.
‫Now when you see that Kampala's absolutely fine.
‫Good when now ready to continue and set up our paging in the kernel main function.
‫So we'll be doing this above.
‫Enable interrupts So we're just going to go set up paging.
‫Right.
‫So we're going to be paging you for gigabyte and it's going to ask us for the flags.
‫Well we want something that is rideable.
‫We want something that is present.
‫And you know what, we are making a kernel page, but let's allow access from all this while, OK?
‫We want a entire four gigabyte chunk that is rideable, is present and is accessed from everybody.
‫So any privilege, any privileged level can access it.
‫OK, so that that returns a struct kernel for gigabytes and we'll just call that kernel directory.
‫Now, Colonel Chung.
‫OK, now we're going to copy this.
‫We're going to stay here and we're going to set it to zero and we're going to make that static.
‫So that is accessible only from Canada, let's see.
‫And there we are.
‫Colonel Shank equals paging you for gigabyte.
‫OK, now, before we go any further, let's make clean I the research and we see a problem.
‫And that is because I named it wrong.
‫Shouldn't be.
‫Colonel, four gigabyte Schunk paging four gigabyte chunk.
‫My mistake.
‫Make sure you correct that variable here guys.
‫And then we're going to build again.
‫There we are.
‫That Beltz.
‫I'm going to keep and run this and we can see it doesn't panic.
‫So, so far so good.
‫It created that memory.
‫And now we're going to go paging switch and we're just going to go into Pageonce and see a moment.
‫And you see paging forget about that directory.
‫We're going to copy and paste this into halophiles.
‫So we have a prototype for this.
‫Yeah.
‫So just do that, OK?
‫And then we go back to candidate C, we can do paging four gigabyte chunk get directory and we're going
‫to pass in our kernel chunk and then we're going to go paging switch.
‫OK, and there we are.
‫So that'll get the directory from the four gigabyte chunk and then it will switch to it.
‫So switch to Col, paging Schunk.
‫And now the final thing to do is to enable paging and we do that by calling enable paging now make sure
‫enable interruptions below, enable paging guys because paging can cause interrupts.
‫OK, so we're going to make clean that now.
‫We're going to build that and now everything should run fine and it does.
‫So we've successfully enabled paging.
‫Now obviously you don't notice anything different because we've created an entire linnear page directory.
‫So 060 points to physical address Ceric Series zero zero, a thousand points to physical address, Selex
‫a thousand and so on, so on.
‫Now, by manipulating values in the page directory and page directory entry, we can change where those
‫addresses point to and then switch switch to the page directory using Pageonce switch and then we can
‫make zero zero zero point zero thousand, for example.
‫But we will get to that next lecture.
‫So congratulations on enabling paging.