#include "./memory.hpp" #include "arch/x86_64/arch.hpp" #include "vga.hpp" namespace x86_64 { vm::vm(pmm& pmm) : m_pmm(&pmm), m_root(allocate_node()) { static_assert((INIT_START & 0x1000) == 0); static_assert((INIT_END & 0x1000) == 0); static_assert(INIT_START < INIT_END); if (m_root == nullptr) { vga::puts("Failed to allocate memory for Virtual Address Allocator!!!", 0, 0); asm volatile("cli; hlt"); } m_root->base = INIT_START; m_root->maxSize = m_root->size = INIT_END - INIT_START; } virtaddr_t vm::reserve(u64 size) { size = (size + 0xFFF) & ~0xFFF; auto* node = find_first_fit(size); if (node == nullptr) return NULLADDR; u64 result = node->base; if (node->size == size) { remove(node); return result; } node->base += size; node->size -= size; node->recalculate_upwards(); return result; } bool vm::commit(virtaddr_t addr, u64 size, u64 flags) { const u64 pageEnd = (addr + size + 0xFFFULL) >> 12ULL; u64 page = addr >> 12ULL; for (; page != pageEnd; ++page) { auto alloced = arch::get().get_pmm().alloc_page(); if (alloced == INVALID_PHYSADDR) break; arch::get().get_paging().map(alloced, page << 12ULL, flags); } if (page == pageEnd) return true; // Roll-back on failure for (u64 cur = addr >> 12ULL; cur != page; ++cur) { arch::get().get_pmm().free_page(arch::get().get_paging().get(cur << 12ULL)); arch::get().get_paging().unmap(cur << 12ULL); } return false; } bool vm::uncommit(virtaddr_t addr, u64 size) { const u64 pageEnd = (addr + size + 0xFFFULL) >> 12ULL; for (u64 page = addr >> 12ULL; page != pageEnd; ++page) { physaddr_t paddr = arch::get().get_paging().get(page << 12ULL); if (paddr == INVALID_PHYSADDR) return false; arch::get().get_pmm().free_page(paddr); arch::get().get_paging().unmap(page << 12ULL); } return true; } bool vm::release(virtaddr_t addr, u64 size) { if (size == 0) return false; const u64 pageStart = addr & ~0xFFFULL; const u64 pageEnd = (addr + size + 0xFFF) & ~0xFFFULL; if (pageStart == pageEnd) return false; if (pageEnd < pageStart) return false; if (pageStart < INIT_START) return false; if (pageEnd > INIT_END) return false; node* next = lower_bound(pageStart); node* prev = nullptr; if (next != nullptr) prev = next->pred(); else if (m_root != nullptr) prev = m_root->max(); if (prev != nullptr && prev->base + prev->size > pageStart) return false; if (next != nullptr && pageEnd > next->base) return false; const bool mergePrev = prev != nullptr && prev->base + prev->size == pageStart; const bool mergeNext = next != nullptr && pageEnd == next->base; if (mergePrev && mergeNext) { prev->size += (pageEnd - pageStart) + next->size; remove(next); prev->recalculate_upwards(); return true; } if (mergePrev) { prev->size += (pageEnd - pageStart); prev->recalculate_upwards(); return true; } if (mergeNext) { next->base = pageStart; next->size += (pageEnd - pageStart); next->recalculate_upwards(); return true; } node* node = allocate_node(); if (node == nullptr) return false; node->base = pageStart; node->maxSize = node->size = (pageEnd - pageStart); node->make_red(); insert(node); return true; } void vm::insert(node* node) { decltype(node) prn = nullptr; decltype(node) cur = m_root; while (cur != nullptr) { prn = cur; cur = node->base < cur->base ? cur->left : cur->right; } node->parent = prn; if (prn == nullptr) { m_root = node; } else if (node->base < prn->base) { prn->left = node; } else { prn->right = node; } node->left = node->right = nullptr; node->make_red(); node->recalculate_upwards(); insert_fixup(node); node->recalculate_upwards(); } void vm::insert_fixup(node* node) { while (node->parent != nullptr && node->parent->is_red()) { auto* parent = node->parent; auto* grandp = parent->parent; if (parent == grandp->left) { auto* uncle = grandp->right; if (uncle != nullptr && uncle->is_red()) { parent->make_black(); uncle->make_black(); grandp->make_red(); node = grandp; continue; } if (node == parent->right) { node = parent; rotate_left(node); parent = node->parent; grandp = parent != nullptr ? parent->parent : nullptr; } parent->make_black(); grandp->make_red(); rotate_right(grandp); continue; } auto* uncle = grandp->left; if (uncle != nullptr && uncle->is_red()) { parent->make_black(); uncle->make_black(); grandp->make_red(); node = grandp; continue; } if (node == parent->left) { node = parent; rotate_right(node); parent = node->parent; grandp = parent != nullptr ? parent->parent : nullptr; } parent->make_black(); grandp->make_red(); rotate_left(grandp); } m_root->make_black(); } void vm::transplant(node* first, node* second) { if (first->parent == nullptr) { m_root = second; } else if (first == first->parent->left) { first->parent->left = second; } else { first->parent->right = second; } if (second != nullptr) { second->parent = first->parent; } } void vm::remove(node* node) { auto* oth = node; const bool origRed = oth->is_red(); decltype(node) xNode = nullptr; decltype(node) xPrnt = nullptr; if (node->left == nullptr) { xNode = node->right; xPrnt = node->parent; transplant(node, node->right); if (xNode != nullptr) { xPrnt = xNode->parent; } if (xPrnt != nullptr) xPrnt->recalculate_upwards(); } else if (node->right == nullptr) { xNode = node->left; xPrnt = node->parent; transplant(node, node->left); if (xNode != nullptr) { xPrnt = xNode->parent; } if (xPrnt != nullptr) xPrnt->recalculate_upwards(); } else { oth = node->right->min(); const bool othOrigRed = oth->is_red(); xNode = oth->right; auto* oldOthPrnt = oth->parent; if (oth->parent == node) { xPrnt = oth; if (xNode != nullptr) xNode->parent = oth; } else { xPrnt = oth->parent; transplant(oth, oth->right); oldOthPrnt->recalculate_upwards(); oth->right = node->right; oth->right->parent = oth; } transplant(node, oth); oth->left = node->left; oth->left->parent = oth; if (node->is_red()) { oth->make_red(); } else { oth->make_black(); } oth->recalculate_upwards(); if (!othOrigRed) remove_fixup(xNode, xPrnt); if (xNode != nullptr) { xNode->recalculate_upwards(); } else if (xPrnt != nullptr) { xPrnt->recalculate_upwards(); } free_node(node); return; } if (!origRed) { remove_fixup(xNode, xPrnt); } if (xNode != nullptr) { xNode->recalculate_upwards(); } else if (xPrnt != nullptr) { xPrnt->recalculate_upwards(); } free_node(node); } void vm::remove_fixup(node* first, node* second) { while (first != m_root && (first == nullptr || !first->is_red())) { if (second == nullptr) break; if (first == second->left) { node* sibling = second->right; if (sibling->is_red()) { sibling->make_black(); second->make_red(); rotate_left(second); sibling = second->right; } const bool siblingLeftRed = sibling != nullptr ? sibling->left->is_red() : false; const bool siblingRightRed = sibling != nullptr ? sibling->right->is_red() : false; if (!siblingLeftRed && !siblingRightRed) { if (sibling != nullptr) sibling->make_red(); first = second; second = first->parent; continue; } if (!siblingRightRed) { if (sibling != nullptr && sibling->left != nullptr) { sibling->left->make_black(); } if (sibling != nullptr) { sibling->make_red(); rotate_right(sibling); } sibling = second->right; } if (sibling != nullptr) { if (second->is_red()) { sibling->make_red(); } else { sibling->make_black(); } } second->make_black(); if (sibling != nullptr && sibling->right != nullptr) { sibling->right->make_black(); } rotate_left(second); first = m_root; second = nullptr; continue; } node* sibling = second->left; if (sibling != nullptr && sibling->is_red()) { sibling->make_black(); second->make_red(); rotate_right(second); sibling = second->left; } const bool siblingRightRed = sibling != nullptr ? sibling->right->is_red() : false; const bool siblingLeftRed = sibling != nullptr ? sibling->left->is_red() : false; if (!siblingRightRed && !siblingLeftRed) { if (sibling != nullptr) sibling->make_red(); first = second; second = first->parent; continue; } if (!siblingLeftRed) { if (sibling != nullptr && sibling->right != nullptr) { sibling->right->make_black(); } if (sibling != nullptr) { sibling->make_red(); rotate_left(sibling); } sibling = second->left; } if (sibling != nullptr) { if (second->is_red()) { sibling->make_red(); } else { sibling->make_black(); } } second->make_black(); if (sibling != nullptr && sibling->left != nullptr) { sibling->left->make_black(); } rotate_right(second); first = m_root; second = nullptr; } if (first != nullptr) { first->make_black(); } } vm::node* vm::lower_bound(u64 base) { node* res = nullptr; node* cur = m_root; while (cur != nullptr) { if (cur->base >= base) { res = cur; cur = cur->left; } else { cur = cur->right; } } return res; } vm::node* vm::find_first_fit(u64 size) { node* cur = m_root; while (cur != nullptr) { if (cur->left != nullptr && cur->left->maxSize >= size) { cur = cur->left; continue; } if (cur->size >= size) return cur; if (cur->right != nullptr && cur->right->maxSize >= size) { cur = cur->right; continue; } return nullptr; } return nullptr; } void vm::rotate_left(node* node) { auto* oth = node->right; node->right = oth->left; if (oth->left != nullptr) oth->left->parent = node; oth->parent = node->parent; if (node->parent == nullptr) { m_root = oth; } else if (node == node->parent->left) { node->parent->left = oth; } else { node->parent->right = oth; } oth->left = node; node->parent = oth; node->recalculate(); oth->recalculate_upwards(); } void vm::rotate_right(node* node) { auto* oth = node->left; node->left = oth->right; if (oth->right != nullptr) oth->right->parent = node; oth->parent = node->parent; if (node->parent == nullptr) { m_root = oth; } else if (node == node->parent->right) { node->parent->right = oth; } else { node->parent->left = oth; } oth->right = node; node->parent = oth; node->recalculate(); oth->recalculate_upwards(); } vm::node* vm::allocate_node() { if (m_firstFree != nullptr) { node* node = m_firstFree; m_firstFree = m_firstFree->right; m_firstFree->parent = node->parent; return node; } return allocate_page(); } void vm::free_node(node* node) { *node = {}; if (m_firstFree == nullptr) { m_firstFree = node; node->parent = node; } else { m_firstFree->parent = m_firstFree->parent->right = node; } } vm::node* vm::allocate_page() { physaddr_t pagePhys = m_pmm->alloc_page(); if (pagePhys == INVALID_PHYSADDR) return nullptr; node* first = reinterpret_cast(pagePhys + paging::HHDM_BASE); *first = {}; node* last = first + page::ENTRY_COUNT - 1; *last = {}; node* it = first + 1; *it = {}; if (m_firstFree == nullptr) m_firstFree = it; while (it != last) { it->right = it + 1; it += 1; *it = {}; } return (m_firstFree->parent = last, first); } } // namespace x86_64