2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2022-08-24 13:56:26 +00:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2023-02-24 17:54:30 +00:00
|
|
|
#include <Kernel/Memory/PhysicalAddress.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2021-07-17 11:30:47 +00:00
|
|
|
enum class MayReturnToFreeList : bool {
|
|
|
|
No,
|
|
|
|
Yes
|
|
|
|
};
|
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
class PhysicalPage {
|
2021-07-13 21:17:02 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(PhysicalPage);
|
|
|
|
AK_MAKE_NONMOVABLE(PhysicalPage);
|
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
friend class MemoryManager;
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
public:
|
2021-07-08 01:50:05 +00:00
|
|
|
PhysicalAddress paddr() const;
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2023-02-19 22:01:04 +00:00
|
|
|
void ref() const
|
2019-04-03 13:13:07 +00:00
|
|
|
{
|
2022-08-24 14:07:29 +00:00
|
|
|
m_ref_count.fetch_add(1, AK::memory_order_relaxed);
|
2019-04-03 13:13:07 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 22:01:04 +00:00
|
|
|
void unref() const
|
2019-04-03 13:13:07 +00:00
|
|
|
{
|
2021-07-08 02:28:51 +00:00
|
|
|
if (m_ref_count.fetch_sub(1, AK::memory_order_acq_rel) == 1)
|
|
|
|
free_this();
|
2019-04-03 13:13:07 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
static NonnullRefPtr<PhysicalPage> create(PhysicalAddress, MayReturnToFreeList may_return_to_freelist = MayReturnToFreeList::Yes);
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2020-08-22 03:49:50 +00:00
|
|
|
u32 ref_count() const { return m_ref_count.load(AK::memory_order_consume); }
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2020-02-15 12:12:02 +00:00
|
|
|
bool is_shared_zero_page() const;
|
2020-09-05 03:12:25 +00:00
|
|
|
bool is_lazy_committed_page() const;
|
2020-02-15 12:12:02 +00:00
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
private:
|
2021-07-17 11:30:47 +00:00
|
|
|
explicit PhysicalPage(MayReturnToFreeList may_return_to_freelist);
|
2021-02-28 13:42:08 +00:00
|
|
|
~PhysicalPage() = default;
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2023-02-19 22:01:04 +00:00
|
|
|
void free_this() const;
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2023-02-19 22:01:04 +00:00
|
|
|
mutable Atomic<u32> m_ref_count { 1 };
|
2021-07-17 11:30:47 +00:00
|
|
|
MayReturnToFreeList m_may_return_to_freelist { MayReturnToFreeList::Yes };
|
2021-07-08 01:50:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PhysicalPageEntry {
|
|
|
|
union {
|
2021-07-12 20:52:17 +00:00
|
|
|
// If it's a live PhysicalPage object:
|
|
|
|
struct {
|
|
|
|
PhysicalPage physical_page;
|
|
|
|
} allocated;
|
|
|
|
|
|
|
|
// If it's an entry in a PhysicalZone::Bucket's freelist.
|
|
|
|
struct {
|
|
|
|
i16 next_index;
|
|
|
|
i16 prev_index;
|
|
|
|
} freelist;
|
2021-07-08 01:50:05 +00:00
|
|
|
};
|
2019-04-03 13:13:07 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|