2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-03-22 13:12:45 +13:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/VM/PhysicalPage.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
NonnullRefPtr<PhysicalPage> PhysicalPage::create(PhysicalAddress paddr, bool supervisor, bool may_return_to_freelist)
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2021-07-07 19:50:05 -06: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 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-07 19:50:05 -06:00
|
|
|
PhysicalPage::PhysicalPage(bool supervisor, bool may_return_to_freelist)
|
2019-04-03 15:13:07 +02:00
|
|
|
: m_may_return_to_freelist(may_return_to_freelist)
|
|
|
|
|
, m_supervisor(supervisor)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-07 19:50:05 -06:00
|
|
|
PhysicalAddress PhysicalPage::paddr() const
|
|
|
|
|
{
|
|
|
|
|
return MM.get_physical_address(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-07 20:28:51 -06:00
|
|
|
void PhysicalPage::free_this()
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2021-07-07 20:28:51 -06: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 15:13:07 +02:00
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
|
}
|