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
|
|
|
*/
|
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-06-21 19:45:35 +03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2020-05-16 13:00:04 +03:00
|
|
|
#include <Kernel/PhysicalAddress.h>
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-07-17 14:30:47 +03:00
|
|
|
enum class MayReturnToFreeList : bool {
|
|
|
|
No,
|
|
|
|
Yes
|
|
|
|
};
|
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
class PhysicalPage {
|
2021-07-14 00:17:02 +03:00
|
|
|
AK_MAKE_NONCOPYABLE(PhysicalPage);
|
|
|
|
AK_MAKE_NONMOVABLE(PhysicalPage);
|
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
friend class MemoryManager;
|
2019-05-28 12:53:16 +03:00
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
public:
|
2021-07-08 04:50:05 +03:00
|
|
|
PhysicalAddress paddr() const;
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2019-06-21 16:29:31 +03:00
|
|
|
void ref()
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2020-08-22 06:49:50 +03:00
|
|
|
m_ref_count.fetch_add(1, AK::memory_order_acq_rel);
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
2020-01-23 17:14:21 +03:00
|
|
|
void unref()
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2021-07-08 05:28:51 +03:00
|
|
|
if (m_ref_count.fetch_sub(1, AK::memory_order_acq_rel) == 1)
|
|
|
|
free_this();
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-17 14:30:47 +03:00
|
|
|
static NonnullRefPtr<PhysicalPage> create(PhysicalAddress, MayReturnToFreeList may_return_to_freelist = MayReturnToFreeList::Yes);
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2020-08-22 06:49:50 +03:00
|
|
|
u32 ref_count() const { return m_ref_count.load(AK::memory_order_consume); }
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2020-02-15 15:12:02 +03:00
|
|
|
bool is_shared_zero_page() const;
|
2020-09-05 06:12:25 +03:00
|
|
|
bool is_lazy_committed_page() const;
|
2020-02-15 15:12:02 +03:00
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
private:
|
2021-07-17 14:30:47 +03:00
|
|
|
explicit PhysicalPage(MayReturnToFreeList may_return_to_freelist);
|
2021-02-28 16:42:08 +03:00
|
|
|
~PhysicalPage() = default;
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2021-07-08 05:28:51 +03:00
|
|
|
void free_this();
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2020-08-22 06:49:50 +03:00
|
|
|
Atomic<u32> m_ref_count { 1 };
|
2021-07-17 14:30:47 +03:00
|
|
|
MayReturnToFreeList m_may_return_to_freelist { MayReturnToFreeList::Yes };
|
2021-07-08 04:50:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PhysicalPageEntry {
|
|
|
|
union {
|
2021-07-12 23:52:17 +03: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 04:50:05 +03:00
|
|
|
};
|
2019-04-03 16:13:07 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|