‫What is paging paging allows us to remap memory addresses, to point to other memory addresses, it
‫can be used to provide the illusion we have the maximum amount of RAM installed on a 64 bit machine.
‫This could be terabytes of RAM on a 32 bit machine, a maximum of four gigabytes of RAM.
‫Paging can also be used to hide memory from other processes, which is great for security and allows
‫processes to run in the same address.
‫Space remapping memory paging allows us to remap one memory address to another.
‫So, for example, we could remap the address.
‫Xerox one zero zero zero zero zero and pointed to address two zero zero zero zero zero.
‫Now paging works in four thousand ninety six by block sizes by default.
‫And we call these blocks pages.
‫So in my example where I say we can map zero one zero zero zero zero, so on to zero two zero zero zero
‫and so on, that mapping would be a four thousand eighty six by block.
‫So it would actually map zero one zero zero zero zero zero two zero one zero one zero zero zero.
‫And it would map the boundary from those two addresses to zero six two zero zero zero zero zero two
‫zero two zero one zero zero zero.
‫And that is how that works.
‫OK, we can explain more about that later, but don't worry too much.
‫We'll get into that.
‫Just keep that in the back of your mind for now.
‫So when we enable paging the memory management unit, we'll look at your allocated page tables to resolve
‫virtual addresses into physical addresses.
‫Now, I know you don't know what that is, but we'll get to it.
‫Paging also allows us to pretend memory exists when it actually does not.
‫So what are virtual addresses and what a physical addresses?
‫Virtual addresses are addresses that are not actually pointing to the address in memory that their value
‫says they are.
‫So, for example, the address 2x one zero zero zero zero zero might point to physical address zero
‫two zero zero zero zero zero as an example.
‫Now, when anyone was to access the virtual address, six one zero zero zero zero, they would actually
‫be accessing the address.
‫Cinram, that is zero two zero zero zero zero zero now because the paging works on a four thousand eighty
‫six byte block boundary.
‫If we was to access the address zero one zero zero zero zero one, we would actually be accessing the
‫address zero two zero zero zero zero one.
‫Does that make sense?
‫So physical addresses are absolute addresses in memory whose value points to the same address in memory.
‫So, for example, if we have a physical address of zero one zero zero zero zero and it points to the
‫address, zero one zero zero zero zero, then this is a physical address because we're not pulling the
‫curtain over the program as is where the pointer is pointing is actually where it's pointing in memory.
‫There's no illusion or remapping of memory going on.
‫So essentially, virtual addresses and physical addresses are just terms that we use to explain how
‫a piece of memory is being accessed.
‫OK, so this is an illustration of paging so you can see essentially how the paging works is we have
‫page directories, right?
‫And in here we have 1024 entries and these entries point to page tables and each page table also has
‫1024 entries and the page table points to the real memory.
‫So if we look at page directory zero and page table zero, we can see that index zero in the page table
‫in page typed zero explains virtual address Coke zero zero.
‫That's exactly what it does.
‫And somewhere in this page table entry, it will describe that it points to this address in real memory.
‫Now, anybody who accesses address you zero zero will actually be accessing this address in real memory.
‫Now, each page table entry describes four thousand eighty six bytes a memory.
‫So we can see the next entry in the table index one in the page table in table zero is actually representing
‫virtual address X 1000.
‫OK, and we can see that somewhere in there points to 450000.
‫And again, we can see that that describes four thousand eighty six bytes of memory index two in the
‫page table.
‫We can see zero x 2000.
‫This points to zero x 57000.
‫OK, so you get the idea right now in our page directory table, we can see that index zero points to
‫page table zero, which I just explained.
‫However, index one in the table points to page table one and page table one.
‫Index zero describes Xerox 400000 in memory index one in page.
‫Table one describes virtual address phones and 1000 in memory and so on.
‫So essentially the way this works is each page table has 1024 entries.
‫Each page is four thousand ninety six bytes.
‫Each page table entry describes four thousand eighty six bytes.
‫So the equation to figure out how much memory one page table can manage is very simple.
‫One thousand twenty four multiplied by four thousand eighty six.
‫This is why our page directory index one is zero x 400000.
‫So you can see that this whole system basically consists of page directories and page tables.
‫We have the page to actually table and and each entry in this table points to one page table.
‫Now, each page table has 1024 entries that point to 1024, four thousand ninety six by blocks of memory.
‫OK, so let's just go over the page and structure a little bit.
‫We have 1024 page directories that point to 1024 page tables.
‫This assumes a 32 bit architecture right now, guys.
‫OK, we have 1024 page table entries per page table.
‫OK, these page table entries describe which physical address the page points to.
‫They also have some flags, if it's readable, writable and so on, we will get to that each page table
‫entry covers four thousand six bytes of memory.
‫The very first page directory index points to the very first page table and the very first page tables,
‫index covers address zero to four thousand eighty six bytes of memory.
‫So each four thousand eighty six byte block of memory is called a page.
‫Let's multiply the page directories by the page table entries and then by the page size.
‫What do we get?
‫Four gigabytes of addressable memory using 1024 page directories that point to page tables that consist
‫of 1024 page table entries.
‫We can address the entire four gig of memory.
‫So the page directory structure, it holds a pointer to a page table and it holds attributes.
‫This is how it looks.
‫Now, this is bitwise logic and these numbers represent the best numbers, so we have zero here, the
‫lower end of the integer and you know, we have Bit9 here, but 11 here and so on.
‫So between 11 to 31 is the page table aligned address.
‫Now, I know you're thinking this is not a 32 bit value.
‫How can we possibly store an address to a page table?
‫Well, if you are lying to four kilobytes, four thousand eighty six bytes, you never actually use
‫more than 20 bits.
‫And because we don't use more than 20 bits, it means we only need 20 bits to store that for kilobyte
‫aligned address.
‫So the ASP, this is set to zero four for kilobyte pages, set to one four four megabyte pages.
‫We will not be working with four megabyte pages.
‫This is way too big and especially for a 32 bit.
‫Architecture for kilobyte pages is plenty.
‫We now have the access bits a this is set to one by the CPU.
‫If we if we access this page, we have the cash to save a bit.
‫This is set to one to disable page caching.
‫We have the right bit.
‫If this is set, then right through caching is enabled.
‫If not, then write back is enabled instead.
‫We have the use of supervisor beds, if this bed is set, then the page can be accessed by all privilege
‫ring levels.
‫If it is not set and only the supervisor ring can access this page, so only rings with authority.
‫I don't know if you remember, I explained on one of the first lectures since we went to Protected Mode
‫about how those privileged rings work.
‫So I suggest you go back in the course if you need to see that ring.
‫Three is for use of programs.
‫Ring zero is generally for colonel operations and gives us access to privileged instructions in the
‫C.P.U.
‫So here we have the orbit's if this bit is set in the pages, readable and readable.
‫If it's not, then this page is only readable.
‫However, the WP bit in the C0 Register can allow writing in all cases for supervisor privileged rings
‫the bit a set if the page exists in real memory.
‫This is called the present bit.
‫If this page is not actually available, then we, the kernel developer, should set this bit to zero.
‫And now if someone accesses this memory, a page fault will occur and we expect it to load that memory.
‫From desk or from a network, whatever we might be doing, we expect to load that back into memory and
‫then tell the CPU that everything's fine now.
‫And the programmer using that memory address has no idea what is going on.
‫Everything just works.
‫It's almost as if that memory was always there.
‫He hasn't a clue.
‫This is incredibly useful because it allows us to to have, let's say, four gig of RAM, even though
‫we only have one gig installed, we can do very clever things with paging.
‫We can allow people to address the full four gigabytes of RAM, even though only one gigabyte of RAM
‫is installed.
‫So the page table entry structure is very similar to the page directory structure.
‫There's just a few differences, so feel free to pause the video and take a read through those.
‫However, I'm not going to go through it again because it's very similar to the directory structure.
‫So let's imagine the very first page table in the system.
‫Page Directory zero, page zero so we can see page directory zero, page table zero.
‫So we can see that the very first entry in this table.
‫Is responsible for virtual address zero zero zero.
‫The second entry, zero x 1000, third zero x 2000 there in four thousand eighty six byte increments,
‫you understand this goes on and on.
‫Now, let's take a look at zero x 10000 here.
‫We can imagine that this page table entry points to physical address, Xbox 500000.
‫So if someone after this paging set up, if someone was to access a virtual address X one thousand one,
‫they would actually be accessing zero X 500000.
‫And why was to access the X 10000 to their access in five hundred thousand and two and so on and so
‫on until they cross the four thousand eighty six by by boundary, in which case they will then be using
‫the page table entry for 06 11000, which points nowhere.
‫So this will cause a page for.
‫We can see the same for CBX 13000, we can see this points to physical address here, 5000.
‫We can see our virtual address here, 5000 doesn't point anywhere.
‫So, again, a page for what would happen if you see 5000.
‫If you access the 13000, you actually access and see 5000.
‫But from your perspective, your access in Xerox 13000, if you access your 5000 from your perspective,
‫your access in virtual address sucks 5000, but it doesn't point anywhere.
‫So there's no physical address.
‫A page full occurs.
‫So let's take a look at the second page table.
‫We can see zero x 400000, zero x 400, 1600 and 2000 because remember, each page table covers 1024
‫entries.
‫So 1024 multiplied by four thousand ninety six gives us this virtual address, zero x 400000.
‫And remember the page directory index zero points to table one, the page directory index one points
‫to page table to the second page table here, or I guess it was down the index from zero then page table
‫one.
‫But anyway, I think you get the idea.
‫So here Xerox 400, 11000 points to physical address, you exchange a thousand.
‫Zero nine hundred thousand physical address is pointed up from zero phones and 12000 virtual address.
‫So page four exceptions can occur in certain situations, so the page for the exception is essentially
‫in 614.
‫So this is invoke when there was a problem with paging.
‫So this exception is invoked.
‫If you access a page in memory that does not have the present set, it's invoked if you access a page
‫that is for supervisor, but you are not a supervisor.
‫So maybe you're entering three right now.
‫It is invoked if you write to a page that is read only and you are not supervisor, there's other reasons
‫why this exception gets thrown.
‫But I don't feel the need to cover them here so we can hide memory from processes using paging as an
‫incredible feature.
‫And it's great for security.
‫We can essentially give each process its entire own memory.
‫It's not aware of anything else that's going on.
‫We are the magician with with the coding and they're part of an illusion and don't even realize it.
‫Right.
‫The program is part of an illusion and is not even aware as far as it's concerned.
‫It's access and physical memory.
‫As far as it's concerned.
‫It's loaded into address books 400000.
‫It doesn't think that other programs are loaded into that address, but it can appear that way because
‫of paging.
‫So if we give each process its own page directory table, then we can map the memory for the process
‫however we want it to be.
‫We can make it so the process can only see itself.
‫It can't see the any other processed memory at all.
‫It can only see itself.
‫We give it we give it its entire own virtual address space.
‫It has no idea.
‫It has no idea about the calculator program you're running.
‫It can only see itself.
‫So hiding memory can be achieved by switching the page directories when we move between processes.
‫So we just use a assembly instruction to essentially load in a different page directory just before
‫we resume a different task.
‫Essentially.
‫Now, if there was an interest, we'd have to switch back to the kernel page directory and the kernel
‫page directory should be linear.
‫In other words, page zero will point to the to the same memory addresses as the page table entries
‫represent.
‫Okay, so page zero, index zero that represents zero will point to physical address.
‫You use zero.
‫Index, one that represents virtual address like a thousand will point to physical address Selex a thousand
‫and so on, the kernel should be like that because that gives us a nice linear view of the entire real
‫physical memory.
‫The kernel should have access to that.
‫So all processes can access the same virtual memory addresses, but they will point to different physical
‫addresses.
‫So by swapping out these page directories, all processes can be loaded into the same address.
‫Now, obviously, they're not logged into the same physical address, their different physical addresses,
‫but they all share the same virtual address and they're not even aware of each other.
‫So we can also give the illusion of more memory.
‫For example, in a 64 bit system, we can pretend there's a terabyte, a memory and a 32 bit system.
‫We can we can pretend that they have access to the full four gigabyte, even though they only have 100
‫megabytes installed.
‫We can do this by setting the page table entries as not present.
‫Now, if they try to access them, it will cause a page fault.
‫And we are then responsible for loading that page from disk or a network or something into memory and
‫then mapping it and telling the processor everything's OK now and in and in the program will have access
‫to that memory as if it was always loaded.
‫It had no idea that it was never loaded until it asked for it.
‫And how did it ask for it as simply try to access that memory address, the processor did the rest.
‫The processor interrupted us and told us that we need to resolve this memory address.
‫Now, at this point in time, we could cause an error or something or we could resolve it.
‫So the benefits to paging are simple.
‫Each process can have the same virtual virtual memory addresses, never writing to each other.
‫If I have one process that's using an address, virtual address 600000 and another process using virtual
‫address 400000, they will never overwrite each other.
‫And the reason for that is because they have different page directories and we swap to them whenever
‫we run that other task again.
‫Now, obviously, they are loaded into different physical memory addresses, but the page tables are
‫different.
‫They both have the virtual gesture, 400000 pointing at the physical memory, but they point a different
‫physical memory.
‫They don't point to the same physical memory, but because they access it through a virtual address
‫due to the way the CPU is set up, when you switch to paging, they have no idea.
‫As far as they're concerned, they're writing to address you 400000.
‫So security is an added benefit.
‫We can essentially map out the entire kernel and we can also map out all of the processes.
‫So paging can also be used to prevent overwriting of sensitive data such as program code, for example.
‫We can make it so pages of read-only.
‫You know, if we load a text section from ANAO file into memory, we don't want them changing that.
‫We can set that all of those pages to read only.
‫And now if their program malfunctions and starts overriding code, it will cause a fault.
‫Many more benefits exist to paging, enabling.
‫Paging is very simple.
‫Take a look at these assembly routines.
‫Paging load directory.
‫This simply moves the address of the page directory into the C3 Register and then returns.
‫And now when we enable paging, we set it 31 in the Cesira Register and then what that will do that
‫will enable paging.
‫You must have a page directory loaded before you enable paging or you're going to be very upset when
‫your colonel panic's.