2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-08-06 08:49:36 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-04-03 10:13:07 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-07-11 12:38:28 -03:00
|
|
|
#include <AK/FixedArray.h>
|
2021-05-26 06:57:46 -03:00
|
|
|
#include <AK/IntrusiveList.h>
|
2022-08-24 10:56:26 -03:00
|
|
|
#include <AK/RefPtr.h>
|
2021-07-11 06:49:16 -03:00
|
|
|
#include <Kernel/Forward.h>
|
2021-08-16 17:54:25 -03:00
|
|
|
#include <Kernel/Library/ListedRefCounted.h>
|
2022-08-19 15:53:40 -03:00
|
|
|
#include <Kernel/Library/LockWeakable.h>
|
2021-07-18 04:10:27 -03:00
|
|
|
#include <Kernel/Locking/Mutex.h>
|
2021-08-06 05:45:34 -03:00
|
|
|
#include <Kernel/Memory/Region.h>
|
2019-04-03 10:13:07 -03:00
|
|
|
|
2021-08-06 08:49:36 -03:00
|
|
|
namespace Kernel::Memory {
|
2020-02-15 21:27:42 -03:00
|
|
|
|
2021-08-16 17:54:25 -03:00
|
|
|
class VMObject
|
2021-12-28 19:22:14 -03:00
|
|
|
: public ListedRefCounted<VMObject, LockType::Spinlock>
|
2022-08-19 15:53:40 -03:00
|
|
|
, public LockWeakable<VMObject> {
|
2019-04-03 10:13:07 -03:00
|
|
|
friend class MemoryManager;
|
2019-11-03 20:45:33 -03:00
|
|
|
friend class Region;
|
2019-05-28 06:53:16 -03:00
|
|
|
|
2019-04-03 10:13:07 -03:00
|
|
|
public:
|
2019-08-07 13:06:17 -03:00
|
|
|
virtual ~VMObject();
|
2019-04-03 10:13:07 -03:00
|
|
|
|
2022-08-19 15:53:40 -03:00
|
|
|
virtual ErrorOr<NonnullLockRefPtr<VMObject>> try_clone() = 0;
|
2019-04-03 10:13:07 -03:00
|
|
|
|
2019-08-07 13:06:17 -03:00
|
|
|
virtual bool is_anonymous() const { return false; }
|
|
|
|
|
virtual bool is_inode() const { return false; }
|
2020-03-01 07:08:28 -03:00
|
|
|
virtual bool is_shared_inode() const { return false; }
|
|
|
|
|
virtual bool is_private_inode() const { return false; }
|
2019-04-03 10:13:07 -03:00
|
|
|
|
2019-08-07 15:12:50 -03:00
|
|
|
size_t page_count() const { return m_physical_pages.size(); }
|
2022-05-12 21:22:23 -03:00
|
|
|
|
2022-08-24 10:56:26 -03:00
|
|
|
virtual Span<RefPtr<PhysicalPage> const> physical_pages() const { return m_physical_pages.span(); }
|
|
|
|
|
virtual Span<RefPtr<PhysicalPage>> physical_pages() { return m_physical_pages.span(); }
|
2019-04-03 10:13:07 -03:00
|
|
|
|
2019-08-07 15:12:50 -03:00
|
|
|
size_t size() const { return m_physical_pages.size() * PAGE_SIZE; }
|
2019-04-03 10:13:07 -03:00
|
|
|
|
2021-07-11 12:57:52 -03:00
|
|
|
virtual StringView class_name() const = 0;
|
2020-02-28 16:58:57 -03:00
|
|
|
|
2021-07-22 21:40:16 -03:00
|
|
|
ALWAYS_INLINE void add_region(Region& region)
|
|
|
|
|
{
|
2021-08-21 20:49:22 -03:00
|
|
|
SpinlockLocker locker(m_lock);
|
2021-07-22 21:40:16 -03:00
|
|
|
m_regions.append(region);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ALWAYS_INLINE void remove_region(Region& region)
|
|
|
|
|
{
|
2021-08-21 20:49:22 -03:00
|
|
|
SpinlockLocker locker(m_lock);
|
2021-07-22 21:40:16 -03:00
|
|
|
m_regions.remove(region);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 13:06:17 -03:00
|
|
|
protected:
|
2022-08-24 10:56:26 -03:00
|
|
|
static ErrorOr<FixedArray<RefPtr<PhysicalPage>>> try_create_physical_pages(size_t);
|
|
|
|
|
ErrorOr<FixedArray<RefPtr<PhysicalPage>>> try_clone_physical_pages() const;
|
|
|
|
|
explicit VMObject(FixedArray<RefPtr<PhysicalPage>>&&);
|
2019-04-03 10:13:07 -03:00
|
|
|
|
2019-05-28 06:53:16 -03:00
|
|
|
template<typename Callback>
|
|
|
|
|
void for_each_region(Callback);
|
2019-04-03 10:13:07 -03:00
|
|
|
|
2021-05-26 06:57:46 -03:00
|
|
|
IntrusiveListNode<VMObject> m_list_node;
|
2022-08-24 10:56:26 -03:00
|
|
|
FixedArray<RefPtr<PhysicalPage>> m_physical_pages;
|
2019-08-07 13:06:17 -03:00
|
|
|
|
2022-08-18 16:46:28 -03:00
|
|
|
mutable RecursiveSpinlock m_lock { LockRank::None };
|
2020-09-05 18:52:14 -03:00
|
|
|
|
2019-08-07 13:06:17 -03:00
|
|
|
private:
|
2021-07-21 19:02:34 -03:00
|
|
|
VMObject& operator=(VMObject const&) = delete;
|
2019-08-07 13:06:17 -03:00
|
|
|
VMObject& operator=(VMObject&&) = delete;
|
|
|
|
|
VMObject(VMObject&&) = delete;
|
2021-01-02 16:03:14 -03:00
|
|
|
|
2021-07-22 21:40:16 -03:00
|
|
|
Region::ListInVMObject m_regions;
|
|
|
|
|
|
2021-05-26 06:57:46 -03:00
|
|
|
public:
|
2021-09-09 09:00:59 -03:00
|
|
|
using AllInstancesList = IntrusiveList<&VMObject::m_list_node>;
|
2021-08-21 20:37:17 -03:00
|
|
|
static SpinlockProtected<VMObject::AllInstancesList>& all_instances();
|
2019-04-03 10:13:07 -03:00
|
|
|
};
|
2020-02-15 21:27:42 -03:00
|
|
|
|
2021-07-22 21:40:16 -03:00
|
|
|
template<typename Callback>
|
|
|
|
|
inline void VMObject::for_each_region(Callback callback)
|
|
|
|
|
{
|
2021-08-21 20:49:22 -03:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-07-22 21:40:16 -03:00
|
|
|
for (auto& region : m_regions) {
|
|
|
|
|
callback(region);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
}
|