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
|
|
|
*/
|
|
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-06-21 18:45:35 +02:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
#include <Kernel/Assertions.h>
|
2019-09-16 10:19:44 +02:00
|
|
|
#include <Kernel/Heap/SlabAllocator.h>
|
2020-05-16 12:00:04 +02:00
|
|
|
#include <Kernel/PhysicalAddress.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
class PhysicalPage {
|
|
|
|
|
friend class MemoryManager;
|
|
|
|
|
friend class PageDirectory;
|
|
|
|
|
friend class VMObject;
|
2019-05-28 11:53:16 +02:00
|
|
|
|
2020-08-21 21:49:50 -06:00
|
|
|
MAKE_SLAB_ALLOCATED(PhysicalPage);
|
|
|
|
|
AK_MAKE_NONMOVABLE(PhysicalPage);
|
|
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
public:
|
|
|
|
|
PhysicalAddress paddr() const { return m_paddr; }
|
|
|
|
|
|
2019-06-21 15:29:31 +02:00
|
|
|
void ref()
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2020-08-21 21:49:50 -06:00
|
|
|
m_ref_count.fetch_add(1, AK::memory_order_acq_rel);
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-23 15:14:21 +01:00
|
|
|
void unref()
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2020-08-21 21:49:50 -06:00
|
|
|
if (m_ref_count.fetch_sub(1, AK::memory_order_acq_rel) == 1) {
|
2019-04-03 15:13:07 +02:00
|
|
|
if (m_may_return_to_freelist)
|
2020-08-21 21:49:50 -06:00
|
|
|
return_to_freelist();
|
2019-06-14 15:05:40 +03:00
|
|
|
delete this;
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
static NonnullRefPtr<PhysicalPage> create(PhysicalAddress, bool supervisor, bool may_return_to_freelist = true);
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2020-08-21 21:49:50 -06:00
|
|
|
u32 ref_count() const { return m_ref_count.load(AK::memory_order_consume); }
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2020-02-15 13:12:02 +01:00
|
|
|
bool is_shared_zero_page() const;
|
2020-09-04 21:12:25 -06:00
|
|
|
bool is_lazy_committed_page() const;
|
2020-02-15 13:12:02 +01:00
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
private:
|
|
|
|
|
PhysicalPage(PhysicalAddress paddr, bool supervisor, bool may_return_to_freelist = true);
|
2021-02-28 14:42:08 +01:00
|
|
|
~PhysicalPage() = default;
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2020-08-21 21:49:50 -06:00
|
|
|
void return_to_freelist() const;
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2020-08-21 21:49:50 -06:00
|
|
|
Atomic<u32> m_ref_count { 1 };
|
2019-04-03 15:13:07 +02:00
|
|
|
bool m_may_return_to_freelist { true };
|
|
|
|
|
bool m_supervisor { false };
|
|
|
|
|
PhysicalAddress m_paddr;
|
|
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
|
}
|