The Memory section of Core provides memory management functions.
The memory model is not very intuitive, so the following documentation is important to understand before delving too much into the code.
| Memory | The Memory section of Core provides memory management functions. |
| 64KB pages | The first important note is that pages are treated in 64KB groups instead of single 4KB pages. |
| Page Table Page Table | |
| Physical To Virtual Table | |
| Memory Map | |
| Common Abbreviations in this section |
The first important note is that pages are treated in 64KB groups instead of single 4KB pages. As such, one 64KB page is actually 16, 4KB pages. This is done to greatly reduce the overhead involved with keeping track of every page. Since the OS will primarily be running just a few processes at a time (just with many threads), the larger granularity is not a significant problem (in fact, may be considered an advantage in some ways).
To avoid confusion, page tables follow in size, also being treated as 64KB. This combination means that the page directory (still 4KB) now effectively contains only 64 entries, where each entry is 16 original entries all pointing to consecutive addresses. Each page table (64KB) still effectively has 1024 entries, but again each entry is 16 original entries all pointing to consectuive addresses.
Having 64 bytes per entry (16 smaller entries * 4 bytes) also means that the page file can hold more than 4GB of pages if needed, since the index into the page file is no longer restricted to fit into the previous 4-byte entry (minus the bits indicating data about the page). However, this is not a major concern at the moment, considering that a process can have less than 4GB of virtual memory anyway.
Below is an example for clarity.
Page Directory:
/-----------+-----------+------------------\
| Phys Addr | Entry # | Content |
|-----------+-----------+------------------|
| 12AB3000h | 00h | 11110xxxh |
|-----------+-----------+------------------|
| . . . | . . . | . . . |
|-----------+-----------+------------------|
| 12AB34B8h | 12h | 5678Exxxh |
|-----------+-----------+------------------|
| 12AB34BCh | 12h | 5678Fxxxh |
|-----------+-----------+------------------|
| 12AB34C0h | 13h | 12340xxxh |
|-----------+-----------+------------------|
| 12AB34C4h | 13h | 12341xxxh |
|-----------+-----------+------------------|
| 12AB34C8h | 13h | 12342xxxh |
|-----------+-----------+------------------|
| 12AB34CCh | 13h | 12343xxxh |
|-----------+-----------+------------------|
| 12AB34D0h | 13h | 12344xxxh |
|-----------+-----------+------------------|
| 12AB34D4h | 13h | 12345xxxh |
|-----------+-----------+------------------|
| 12AB34D8h | 13h | 12346xxxh |
|-----------+-----------+------------------|
| 12AB34DCh | 13h | 12347xxxh |
|-----------+-----------+------------------|
| 12AB34E0h | 13h | 12348xxxh |
|-----------+-----------+------------------|
| 12AB34E4h | 13h | 12349xxxh |
|-----------+-----------+------------------|
| 12AB34E8h | 13h | 1234Axxxh |
|-----------+-----------+------------------|
| 12AB34ECh | 13h | 1234Bxxxh |
|-----------+-----------+------------------|
| 12AB34F0h | 13h | 1234Cxxxh |
|-----------+-----------+------------------|
| 12AB34F4h | 13h | 1234Dxxxh |
|-----------+-----------+------------------|
| 12AB34F8h | 13h | 1234Exxxh |
|-----------+-----------+------------------|
| 12AB34FCh | 13h | 1234Fxxxh |
|-----------+-----------+------------------|
| 12AB3500h | 14h | 98760xxxh |
|-----------+-----------+------------------|
| 12AB3504h | 14h | 98761xxxh |
|-----------+-----------+------------------|
| . . . | . . . | . . . |
|-----------+-----------+------------------|
| 12AB3FFCh | 3Fh | 77771xxxh |
\-----------+-----------+------------------/
Page Table 13h:
/-----------+-----------+------------------\
| Phys Addr | Entry # | Content |
|-----------+-----------+------------------|
| 12340000h | 000h | 12120xxxh |
|-----------+-----------+------------------|
| . . . | . . . | . . . |
|-----------+-----------+------------------|
| 12349278h | 249h | 4444Exxxh |
|-----------+-----------+------------------|
| 1234927Ch | 249h | 4444Fxxxh |
|-----------+-----------+------------------|
| 12349280h | 24Ah | 85760xxxh |
|-----------+-----------+------------------|
| 12349284h | 24Ah | 85761xxxh |
|-----------+-----------+------------------|
| 12349288h | 24Ah | 85762xxxh |
|-----------+-----------+------------------|
| 1234928Ch | 24Ah | 85763xxxh |
|-----------+-----------+------------------|
| 12349290h | 24Ah | 85764xxxh |
|-----------+-----------+------------------|
| 12349294h | 24Ah | 85765xxxh |
|-----------+-----------+------------------|
| 12349298h | 24Ah | 85766xxxh |
|-----------+-----------+------------------|
| 1234929Ch | 24Ah | 85767xxxh |
|-----------+-----------+------------------|
| 123492A0h | 24Ah | 85768xxxh |
|-----------+-----------+------------------|
| 123492A4h | 24Ah | 85769xxxh |
|-----------+-----------+------------------|
| 123492A8h | 24Ah | 8576Axxxh |
|-----------+-----------+------------------|
| 123492ACh | 24Ah | 8576Bxxxh |
|-----------+-----------+------------------|
| 123492B0h | 24Ah | 8576Cxxxh |
|-----------+-----------+------------------|
| 123492B4h | 24Ah | 8576Dxxxh |
|-----------+-----------+------------------|
| 123492B8h | 24Ah | 8576Exxxh |
|-----------+-----------+------------------|
| 123492BCh | 24Ah | 8576Fxxxh |
|-----------+-----------+------------------|
| 123492C0h | 24Bh | 67890xxxh |
|-----------+-----------+------------------|
| 123492C4h | 24Bh | 67891xxxh |
|-----------+-----------+------------------|
| . . . | . . . | . . . |
|-----------+-----------+------------------|
| 1234FFFCh | 3FFh | 2468Fxxxh |
\-----------+-----------+------------------/