AddressSpace: make clone() perform a deep copy

Which unlinks the address space from its parent.
This commit is contained in:
apio 2022-10-15 20:10:05 +02:00
parent 397b1a2819
commit 16f797eeee
2 changed files with 68 additions and 33 deletions

View File

@ -7,8 +7,6 @@ struct AddressSpace
void destroy(); void destroy();
void detach();
AddressSpace clone(); AddressSpace clone();
PageTable* get_pml4() PageTable* get_pml4()
@ -16,13 +14,6 @@ struct AddressSpace
return m_pml4; return m_pml4;
} }
bool is_cloned()
{
return *m_refs > 1;
}
private: private:
PageTable* m_pml4; PageTable* m_pml4;
int* m_refs;
}; };

View File

@ -4,27 +4,21 @@
#include "log/Log.h" #include "log/Log.h"
#include "memory/PMM.h" #include "memory/PMM.h"
#include "memory/VMM.h" #include "memory/VMM.h"
#include "misc/hang.h"
#include "std/stdlib.h" #include "std/stdlib.h"
#include "std/string.h"
#include "utils/move.h" #include "utils/move.h"
AddressSpace AddressSpace::create() AddressSpace AddressSpace::create()
{ {
AddressSpace result; AddressSpace result;
result.m_pml4 = (PageTable*)PMM::request_page(); result.m_pml4 = (PageTable*)PMM::request_page();
result.m_refs = (int*)kmalloc(sizeof(int));
*result.m_refs = 1;
VMM::install_kernel_page_directory_into_address_space(result); VMM::install_kernel_page_directory_into_address_space(result);
return move(result); return move(result);
} }
void AddressSpace::destroy() void AddressSpace::destroy()
{ {
if (is_cloned())
{
kdbgln("Will not destroy a cloned address space, I don't own it");
(*m_refs)--;
return;
}
uint64_t pages_freed = 0; uint64_t pages_freed = 0;
for (int i = 0; i < 512; i++) for (int i = 0; i < 512; i++)
{ {
@ -78,26 +72,76 @@ void AddressSpace::destroy()
pages_freed++; pages_freed++;
PMM::free_page(m_pml4); PMM::free_page(m_pml4);
kfree(m_refs);
kdbgln("Reclaimed %ld pages from address space!", pages_freed); kdbgln("Reclaimed %ld pages from address space!", pages_freed);
} }
void AddressSpace::detach() AddressSpace AddressSpace::clone() // FIXME: Add out-of-memory checks to this function.
{
if (!is_cloned()) return;
(*m_refs)--;
m_refs = (int*)kmalloc(sizeof(int));
*m_refs = 1;
m_pml4 = (PageTable*)PMM::request_page();
VMM::install_kernel_page_directory_into_address_space(*this);
}
AddressSpace AddressSpace::clone()
{ {
AddressSpace result; AddressSpace result;
result.m_pml4 = m_pml4; result.m_pml4 = (PageTable*)PMM::request_page();
result.m_refs = m_refs; if (!result.m_pml4) return result;
*m_refs = *m_refs + 1; memcpy(result.m_pml4, m_pml4, PAGE_SIZE);
for (int i = 0; i < 512; i++)
{
PageDirectoryEntry& pdp_pde = m_pml4->entries[i];
PageDirectoryEntry& cloned_pdp_pde = result.m_pml4->entries[i];
if (!pdp_pde.present) continue;
if (pdp_pde.larger_pages)
{
void* cloned = PMM::request_page();
memcpy(cloned, (void*)pdp_pde.get_address(), PAGE_SIZE);
cloned_pdp_pde.set_address((uint64_t)cloned);
continue;
}
PageTable* pdp = (PageTable*)pdp_pde.get_address();
PageTable* cloned_pdp = (PageTable*)PMM::request_page();
memcpy(cloned_pdp, pdp, PAGE_SIZE);
cloned_pdp_pde.set_address((uint64_t)cloned_pdp);
for (int j = 0; j < 511; j++) // skip the last page directory, it's the kernel one
{
PageDirectoryEntry& pd_pde = pdp->entries[j];
PageDirectoryEntry& cloned_pd_pde = cloned_pdp->entries[j];
if (!pd_pde.present) continue;
if (pd_pde.larger_pages)
{
void* cloned = PMM::request_page();
memcpy(cloned, (void*)pd_pde.get_address(), PAGE_SIZE);
cloned_pd_pde.set_address((uint64_t)cloned);
continue;
}
PageTable* pd = (PageTable*)pd_pde.get_address();
PageTable* cloned_pd = (PageTable*)PMM::request_page();
memcpy(cloned_pd, pd, PAGE_SIZE);
cloned_pd_pde.set_address((uint64_t)cloned_pd);
for (int k = 0; k < 512; k++)
{
PageDirectoryEntry& pt_pde = pd->entries[k];
PageDirectoryEntry& cloned_pt_pde = cloned_pd->entries[k];
if (!pt_pde.present) continue;
if (pt_pde.larger_pages)
{
void* cloned = PMM::request_page();
memcpy(cloned, (void*)pt_pde.get_address(), PAGE_SIZE);
cloned_pt_pde.set_address((uint64_t)cloned);
continue;
}
PageTable* pt = (PageTable*)pt_pde.get_address();
PageTable* cloned_pt = (PageTable*)PMM::request_page();
memcpy(cloned_pt, pt, PAGE_SIZE);
cloned_pt_pde.set_address((uint64_t)cloned_pt);
for (int l = 0; l < 512; l++)
{
PageDirectoryEntry& pde = pt->entries[l];
PageDirectoryEntry& cloned_pde = cloned_pt->entries[l];
if (!pde.present) continue;
void* cloned = PMM::request_page();
memcpy(cloned, (void*)pde.get_address(), PAGE_SIZE);
cloned_pde.set_address((uint64_t)cloned);
continue;
}
}
}
}
VMM::install_kernel_page_directory_into_address_space(result);
return result; return result;
} }