2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-03-22 03:12:45 +03:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2019-04-03 16:13:07 +03:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/VM/PhysicalPage.h>
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
NonnullRefPtr<PhysicalPage> PhysicalPage::create(PhysicalAddress paddr, bool supervisor, bool may_return_to_freelist)
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2021-07-08 04:50:05 +03:00
|
|
|
auto& physical_page_entry = MM.get_physical_page_entry(paddr);
|
|
|
|
return adopt_ref(*new (&physical_page_entry.physical_page) PhysicalPage(supervisor, may_return_to_freelist));
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-08 04:50:05 +03:00
|
|
|
PhysicalPage::PhysicalPage(bool supervisor, bool may_return_to_freelist)
|
2019-04-03 16:13:07 +03:00
|
|
|
: m_may_return_to_freelist(may_return_to_freelist)
|
|
|
|
, m_supervisor(supervisor)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-08 04:50:05 +03:00
|
|
|
PhysicalAddress PhysicalPage::paddr() const
|
|
|
|
{
|
|
|
|
return MM.get_physical_address(*this);
|
|
|
|
}
|
|
|
|
|
2021-07-08 05:28:51 +03:00
|
|
|
void PhysicalPage::free_this()
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2021-07-08 05:28:51 +03:00
|
|
|
if (m_may_return_to_freelist) {
|
|
|
|
auto paddr = MM.get_physical_address(*this);
|
|
|
|
bool is_supervisor = m_supervisor;
|
|
|
|
|
|
|
|
this->~PhysicalPage(); // delete in place
|
|
|
|
|
|
|
|
if (is_supervisor)
|
|
|
|
MM.deallocate_supervisor_physical_page(paddr);
|
|
|
|
else
|
|
|
|
MM.deallocate_user_physical_page(paddr);
|
|
|
|
} else {
|
|
|
|
this->~PhysicalPage(); // delete in place
|
|
|
|
}
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|