Skip to content

Commit

Permalink
Typo fixes in documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamos82 committed Aug 21, 2024
1 parent 1ce2573 commit 3809cee
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions docs/kernel/MemoryManagement.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The memory map is used to understand what parts of the physical memory are reser

Before explaining what `pmm_setup` does, let's see first an issue that arised and how we decided to fix it.

So one of the problems, especially if we use 4k pages (it was not present using 2M pages) is that we need to map a lot of memory to cater for all the structures needed by the PMM, but also by the VMM, and everything else. When the kernel is loaded we usually have some space already mapped just after it's end, but it is pure coincidence, and if we eventually reach an unmapped part of memory, without considering the potential `#PF`, it requires the kernel to map that new region but that can be a problem on early stages, let' s see why.
So one of the problems, especially if we use 4k pages (it was not present using 2M pages) is that we need to map a lot of memory to cater for all the structures needed by the PMM, but also by the VMM, and everything else. When the kernel is loaded we usually have some space already mapped just after its end, but it is pure coincidence, and we can eventually reach an unmapped part of memory, causing a `#PF`, the kernel needs to map that new region in advance, but that can be a problem on early stages, let' s see why.

On many architectures paging is usually obtained using a `multi-level` approach, where a portion of a address is just an entry into a specific table, and the value is the address of the next level table, until we reach the last part of the address that is the offset within the address contained on the last-level entry table (we don' t cover the details here, if interested in learning more this is explained in the [Paging](https://github.com/dreamportdev/Osdev-Notes/blob/master/04_Memory_Management/03_Paging.md) chapter of the _Osdev Notes_).

Expand All @@ -60,12 +60,12 @@ char *pointer_to_variable = (char *) hhdm_get_variable(physical_address);

And we will get access to the content of the `physical_address` variable using it's *higher half representative*

And again we face the problem that the _HHDM_ needs to be initialized, and to do it we still need the PMM, but if we could be able to initialize the `hhdm` before everyhing else in the PMM it will provide us two important new information:
And again we face the problem that the _HHDM_ needs to be initialized, and to do it we still need the PMM, but if we could be able to initialize the `hhdm` before everyhing else it will provide us two important new information:

* The first is that once the hhdm is initialized we are sure that for the rest of initialization of any memory management part, we don't neet to map anything else, because we can access all the memory we want already.
* The `hhdm` will never change once initailized, yeah the content of the memory can changes, but the mapping will always be the same. So there will nevery be the need to free any address within the hhdm.
* The first is that once the hhdm is initialized we are sure that for the rest of initialization of any memory management part, we don't need to map anything else, because we can already access all the memory we want.
* The `hhdm` will never change once initailized, yeah the content of the memory can change, but the mapping will always be the same. So there will never be the need to free any address within the hhdm.

Another important thing that we already know is that usually any memory right after the kernel is usually free, and if it is not is because something is loaded there, and we should be able to tell it by parsing some data structures provided by grub (the memory map, and the modules). At this point we can safely assume that unless the addresses above the end of the kernel are reserved in the mmap, or used by some module (and that can be easily achieved by parsing the tables already present in memory), that area can be used.
Another important thing that we already know is that usually any memory right after the kernel is free, and if it is not is because something is loaded there, we should be able to tell it by parsing some data structures provided by grub (the memory map, and the modules). At this point we can safely assume that unless the addresses above the end of the kernel are reserved in the mmap, or used by some module, that area can be used.

And here is the solution, until the memory manager is not initialized, we will keep returning addresses that are just after the kernel for creating the page tables required for the `hhdm` Initialization (again with the exclusion of the parts that we already knows are marked as used by the bios/bootloader).

Expand All @@ -77,7 +77,7 @@ The first thing that the `pmm_setup()` function does is setting the start addres

These addresses are tracked in `anon_memory_loc` (for the virtual address to be returned) and `anon_physical_memory_loc` (for the physical counterpart).

Then it calls the `hhdm_map_physical_memory()` function, thata prepare the mememory map inthe higher half. After this point we are able to access the whole physical memory using it's `hhdm` represenation. To clarify, a phyiscal address hhdm representation is given by a simple formula:
Then it calls the `hhdm_map_physical_memory()` function, thata prepare the memory map in the higher half. After this point we are able to access the whole physical memory using it's `hhdm` represenation. To clarify, a phyiscal address hhdm representation is given by a simple formula:

```c
hhdm_var_addres = var_address_phys + HHDM_OFFSET
Expand All @@ -90,7 +90,7 @@ At this point is possible to initalize in the following order:
* the physical memory bitmap: that keeps track of the physical memory that is allocated and free
* then mark as reserved in the bitmap the memory used for the bitmap itself

Before returning this function set the status of the `pmm_initialized` variable as `true`, meaning that from now on, any page table allocation request from now on will pass through the pmm, instead of using the `anonymous allocation`.
Before returning this function set the status of the `pmm_initialized` variable as `true`, meaning that from now on, any page table allocation request from will pass through the `pmm`, instead of using the `anonymous allocation`.

This is also the end of the physical memory initialization.

Expand Down

0 comments on commit 3809cee

Please sign in to comment.