2022-10-13 19:19:51 +02:00
|
|
|
#define MODULE "vmm"
|
|
|
|
|
|
|
|
#include "memory/AddressSpace.h"
|
|
|
|
#include "log/Log.h"
|
|
|
|
#include "memory/PMM.h"
|
|
|
|
#include "memory/VMM.h"
|
2022-10-15 17:40:33 +02:00
|
|
|
#include "std/stdlib.h"
|
2022-10-14 19:04:56 +02:00
|
|
|
#include "utils/move.h"
|
2022-10-13 19:19:51 +02:00
|
|
|
|
|
|
|
AddressSpace AddressSpace::create()
|
|
|
|
{
|
|
|
|
AddressSpace result;
|
|
|
|
result.m_pml4 = (PageTable*)PMM::request_page();
|
2022-10-15 17:40:33 +02:00
|
|
|
result.m_refs = (int*)kmalloc(sizeof(int));
|
|
|
|
*result.m_refs = 1;
|
2022-10-13 19:19:51 +02:00
|
|
|
VMM::install_kernel_page_directory_into_address_space(result);
|
2022-10-14 19:04:56 +02:00
|
|
|
return move(result);
|
2022-10-13 19:19:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddressSpace::destroy()
|
|
|
|
{
|
2022-10-15 17:40:33 +02:00
|
|
|
if (is_cloned())
|
2022-10-14 18:17:57 +02:00
|
|
|
{
|
|
|
|
kdbgln("Will not destroy a cloned address space, I don't own it");
|
2022-10-15 17:40:33 +02:00
|
|
|
(*m_refs)--;
|
2022-10-14 18:17:57 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-10-13 19:19:51 +02:00
|
|
|
uint64_t pages_freed = 0;
|
|
|
|
for (int i = 0; i < 512; i++)
|
|
|
|
{
|
|
|
|
PageDirectoryEntry& pdp_pde = m_pml4->entries[i];
|
|
|
|
if (!pdp_pde.present) continue;
|
|
|
|
if (pdp_pde.larger_pages)
|
|
|
|
{
|
|
|
|
pages_freed++;
|
|
|
|
PMM::free_page((void*)pdp_pde.get_address());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
PageTable* pdp = (PageTable*)pdp_pde.get_address();
|
|
|
|
for (int j = 0; j < 511; j++) // skip the last page directory, it's the kernel one
|
|
|
|
{
|
|
|
|
PageDirectoryEntry& pd_pde = pdp->entries[j];
|
|
|
|
if (!pd_pde.present) continue;
|
|
|
|
if (pd_pde.larger_pages)
|
|
|
|
{
|
|
|
|
pages_freed++;
|
|
|
|
PMM::free_page((void*)pd_pde.get_address());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
PageTable* pd = (PageTable*)pd_pde.get_address();
|
|
|
|
for (int k = 0; k < 512; k++)
|
|
|
|
{
|
|
|
|
PageDirectoryEntry& pt_pde = pd->entries[k];
|
|
|
|
if (!pt_pde.present) continue;
|
|
|
|
if (pt_pde.larger_pages)
|
|
|
|
{
|
|
|
|
pages_freed++;
|
|
|
|
PMM::free_page((void*)pt_pde.get_address());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
PageTable* pt = (PageTable*)pt_pde.get_address();
|
|
|
|
for (int l = 0; l < 512; l++)
|
|
|
|
{
|
|
|
|
PageDirectoryEntry& pde = pt->entries[l];
|
2022-10-13 21:14:39 +02:00
|
|
|
if (!pde.present) continue;
|
2022-10-13 19:19:51 +02:00
|
|
|
pages_freed++;
|
|
|
|
PMM::free_page((void*)pde.get_address());
|
|
|
|
}
|
|
|
|
pages_freed++;
|
|
|
|
PMM::free_page(pt);
|
|
|
|
}
|
|
|
|
pages_freed++;
|
|
|
|
PMM::free_page(pd);
|
|
|
|
}
|
|
|
|
pages_freed++;
|
|
|
|
PMM::free_page(pdp);
|
|
|
|
}
|
|
|
|
pages_freed++;
|
|
|
|
PMM::free_page(m_pml4);
|
|
|
|
|
2022-10-15 17:40:33 +02:00
|
|
|
kfree(m_refs);
|
|
|
|
|
2022-10-14 16:46:00 +02:00
|
|
|
kdbgln("Reclaimed %ld pages from address space!", pages_freed);
|
|
|
|
}
|
|
|
|
|
2022-10-14 18:17:57 +02:00
|
|
|
void AddressSpace::detach()
|
2022-10-14 16:46:00 +02:00
|
|
|
{
|
2022-10-15 17:40:33 +02:00
|
|
|
if (!is_cloned()) return;
|
|
|
|
(*m_refs)--;
|
|
|
|
m_refs = (int*)kmalloc(sizeof(int));
|
|
|
|
*m_refs = 1;
|
2022-10-14 18:17:57 +02:00
|
|
|
m_pml4 = (PageTable*)PMM::request_page();
|
2022-10-14 16:46:00 +02:00
|
|
|
VMM::install_kernel_page_directory_into_address_space(*this);
|
2022-10-14 18:17:57 +02:00
|
|
|
}
|
2022-10-14 16:46:00 +02:00
|
|
|
|
2022-10-14 18:17:57 +02:00
|
|
|
AddressSpace AddressSpace::clone()
|
|
|
|
{
|
|
|
|
AddressSpace result;
|
|
|
|
result.m_pml4 = m_pml4;
|
2022-10-15 17:40:33 +02:00
|
|
|
result.m_refs = m_refs;
|
|
|
|
*m_refs = *m_refs + 1;
|
2022-10-14 18:17:57 +02:00
|
|
|
return result;
|
2022-10-13 19:19:51 +02:00
|
|
|
}
|