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