In the lecture, we will see how to free memory. Suppose we want to free a page,
we first locate the entry in the page directory table and free the page using the physical address saved in the entry.
When we want to free the whole memory, we will free the physical pages first,
then we free page directory tables, page directory pointer tables and the page map level four table.
Generally, we could have a few page directory pointer tables and page directory tables.
So we need to loop through each of the tables in the code.
OK, let's get started.
We opened the memory.c file.
The first function we are going to talk about is free pages.
It is like the reverse process of mapping pages. The index is used to locate the correct entry in the page directory table
.
When the function is called, we assume that the vstart and vend are page aligned. So we use assert
to check the condition.
Ok, after the checks pass, we find the page directory table. Notice that we specify the alloc with value 0,
which means it returns null if the entry does not exist.
Because what we want to do here is free the existing page.
If the return table is valid, we use the index to find the corresponding entry.
The assert tests the present bit which should be set when we are about to free it.
Because the table in this case is page directory table, the entry in the page directory table is
pointing to 2m physical page.
The lower 21 bits should be cleared before we use the address.
So we use the macro
pte address to do that.
Then we convert the physical address to virtual address and call function kfree to free the page.
After that,
don’t forget to clear the entry in the table indicating that the entry now is unused.
Ok we move to the next page by adding the page size and continue the process until we reach
the end of the memory region.
What we are going to talk about next is the function
free vm. When we build a process in the following lectures, we will create vm and free it when the process exits
.
To free the vm, we will free the physical pages in the user space as well as page translation tables.
Because we need the page translation tables to locate the physical pages,
we will first free the pages by calling function free pages.
Then we free the page directory tables
and page directory pointer tables,
the last one is page map level 4 table. You see, the higher-level page tables are freed after the lower-level ones,
because we need them to locate the lower-level tables.
It’s worth mentioning that we comment out the free pages function at this point
because we haven’t implemented processes and user space. There is no memory pages allocated in the user space so far
.
OK, let's move on.
In the function free pdt, we will loop through the pml4 table and page directory pointer tables
to find page directory tables. We define a variable map entry pointing to pml4 table.
Since each entry in the page map level 4 table points to page directory pointer table,
we could have a total of 512 pdp tables.
Here we use for loop to loop through each of the entries.
If the entry is present, we will retrieve the address of the entry.
This address is
the address of page directory pointer table.
The pdp table also includes 512 entries which point to page directory tables.
So we loop through the entries as well. If this is a valid entry, we just free the page
and clear the entry meaning that this entry is not used. When we return, the page directory tables are freed.
The free pdpt function is used to free page directory pointer tables.
So we define a variable map entry which holds the address of pml4 table.
Then we loop through each of the 512 entries in the table.
If the entry is used, we will free the page pointed to by the entry, which is the page directory pointer table.
Then we clear the entry.
The free pml4t function is simple, we do nothing but call function kfree to
free the page map level 4 table.
Ok that’s it. In this example, we don’t do the test because we haven’t implemented processes.
When we have user space in the processes, we will use this function to free the vm.
Before we end this lecture, one more thing I need to mention is that the page translation table is 4k in size
.
The page size we use in the sytem is 2m. In the examples, for simplicity,
we allocated a new page to each page translation table.
Therefore, a large chuck of memory in the translation table pages is wasted.
OK, that's it for this lecture.