2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-08-06 11:49:36 +00:00
|
|
|
* Copyright (c) 2018-2021, 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
|
|
|
|
|
2021-07-11 15:38:28 +00:00
|
|
|
#include <AK/FixedArray.h>
|
2021-05-26 09:57:46 +00:00
|
|
|
#include <AK/IntrusiveList.h>
|
2019-08-07 16:06:17 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Weakable.h>
|
2021-07-11 09:49:16 +00:00
|
|
|
#include <Kernel/Forward.h>
|
2021-08-16 20:54:25 +00:00
|
|
|
#include <Kernel/Library/ListedRefCounted.h>
|
2021-07-18 07:10:27 +00:00
|
|
|
#include <Kernel/Locking/Mutex.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/Region.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-08-16 20:54:25 +00:00
|
|
|
class VMObject
|
2021-12-28 22:22:14 +00:00
|
|
|
: public ListedRefCounted<VMObject, LockType::Spinlock>
|
2021-05-26 09:57:46 +00:00
|
|
|
, public Weakable<VMObject> {
|
2019-04-03 13:13:07 +00:00
|
|
|
friend class MemoryManager;
|
2019-11-03 23:45:33 +00:00
|
|
|
friend class Region;
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
public:
|
2019-08-07 16:06:17 +00:00
|
|
|
virtual ~VMObject();
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
virtual ErrorOr<NonnullRefPtr<VMObject>> try_clone() = 0;
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-08-07 16:06:17 +00:00
|
|
|
virtual bool is_anonymous() const { return false; }
|
|
|
|
virtual bool is_inode() const { return false; }
|
2020-03-01 10:08:28 +00:00
|
|
|
virtual bool is_shared_inode() const { return false; }
|
|
|
|
virtual bool is_private_inode() const { return false; }
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-08-07 18:12:50 +00:00
|
|
|
size_t page_count() const { return m_physical_pages.size(); }
|
2021-07-11 15:19:07 +00:00
|
|
|
Span<RefPtr<PhysicalPage> const> physical_pages() const { return m_physical_pages.span(); }
|
|
|
|
Span<RefPtr<PhysicalPage>> physical_pages() { return m_physical_pages.span(); }
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-08-07 18:12:50 +00:00
|
|
|
size_t size() const { return m_physical_pages.size() * PAGE_SIZE; }
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2021-07-11 15:57:52 +00:00
|
|
|
virtual StringView class_name() const = 0;
|
2020-02-28 19:58:57 +00:00
|
|
|
|
2021-07-23 00:40:16 +00:00
|
|
|
ALWAYS_INLINE void add_region(Region& region)
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker locker(m_lock);
|
2021-07-23 00:40:16 +00:00
|
|
|
m_regions.append(region);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void remove_region(Region& region)
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker locker(m_lock);
|
2021-07-23 00:40:16 +00:00
|
|
|
m_regions.remove(region);
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:06:17 +00:00
|
|
|
protected:
|
2022-01-11 17:05:47 +00: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 13:13:07 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_region(Callback);
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2021-05-26 09:57:46 +00:00
|
|
|
IntrusiveListNode<VMObject> m_list_node;
|
2021-07-11 15:38:28 +00:00
|
|
|
FixedArray<RefPtr<PhysicalPage>> m_physical_pages;
|
2019-08-07 16:06:17 +00:00
|
|
|
|
2021-08-21 23:37:17 +00:00
|
|
|
mutable RecursiveSpinlock m_lock;
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2019-08-07 16:06:17 +00:00
|
|
|
private:
|
2021-07-21 22:02:34 +00:00
|
|
|
VMObject& operator=(VMObject const&) = delete;
|
2019-08-07 16:06:17 +00:00
|
|
|
VMObject& operator=(VMObject&&) = delete;
|
|
|
|
VMObject(VMObject&&) = delete;
|
2021-01-02 19:03:14 +00:00
|
|
|
|
2021-07-23 00:40:16 +00:00
|
|
|
Region::ListInVMObject m_regions;
|
|
|
|
|
2021-05-26 09:57:46 +00:00
|
|
|
public:
|
2021-09-09 12:00:59 +00:00
|
|
|
using AllInstancesList = IntrusiveList<&VMObject::m_list_node>;
|
2021-08-21 23:37:17 +00:00
|
|
|
static SpinlockProtected<VMObject::AllInstancesList>& all_instances();
|
2019-04-03 13:13:07 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2021-07-23 00:40:16 +00:00
|
|
|
template<typename Callback>
|
|
|
|
inline void VMObject::for_each_region(Callback callback)
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-07-23 00:40:16 +00:00
|
|
|
for (auto& region : m_regions) {
|
|
|
|
callback(region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline PhysicalPage const* Region::physical_page(size_t index) const
|
|
|
|
{
|
|
|
|
VERIFY(index < page_count());
|
|
|
|
return vmobject().physical_pages()[first_page_index() + index];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline RefPtr<PhysicalPage>& Region::physical_page_slot(size_t index)
|
|
|
|
{
|
|
|
|
VERIFY(index < page_count());
|
|
|
|
return vmobject().physical_pages()[first_page_index() + index];
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|