2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-08-06 13:49:36 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
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
|
|
|
|
|
|
2021-07-11 17:38:28 +02:00
|
|
|
#include <AK/FixedArray.h>
|
2020-12-21 23:21:58 -07:00
|
|
|
#include <AK/HashTable.h>
|
2021-05-26 02:57:46 -07:00
|
|
|
#include <AK/IntrusiveList.h>
|
2019-06-21 18:58:45 +02:00
|
|
|
#include <AK/RefCounted.h>
|
2019-08-07 18:06:17 +02:00
|
|
|
#include <AK/RefPtr.h>
|
2020-09-07 11:53:54 +02:00
|
|
|
#include <AK/Vector.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/Weakable.h>
|
2021-07-11 11:49:16 +02:00
|
|
|
#include <Kernel/Forward.h>
|
2021-07-18 09:10:27 +02:00
|
|
|
#include <Kernel/Locking/Mutex.h>
|
2021-08-06 10:45:34 +02:00
|
|
|
#include <Kernel/Memory/Region.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
|
|
|
|
2020-12-21 23:21:58 -07:00
|
|
|
class VMObjectDeletedHandler {
|
|
|
|
|
public:
|
2021-02-28 14:42:08 +01:00
|
|
|
virtual ~VMObjectDeletedHandler() = default;
|
2020-12-21 23:21:58 -07:00
|
|
|
virtual void vmobject_deleted(VMObject&) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-21 15:29:31 +02:00
|
|
|
class VMObject : public RefCounted<VMObject>
|
2021-05-26 02:57:46 -07:00
|
|
|
, public Weakable<VMObject> {
|
2019-04-03 15:13:07 +02:00
|
|
|
friend class MemoryManager;
|
2019-11-04 00:45:33 +01:00
|
|
|
friend class Region;
|
2019-05-28 11:53:16 +02:00
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
public:
|
2019-08-07 18:06:17 +02:00
|
|
|
virtual ~VMObject();
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-08-15 09:07:59 +00:00
|
|
|
virtual KResultOr<NonnullRefPtr<VMObject>> try_clone() = 0;
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2019-08-07 18:06:17 +02:00
|
|
|
virtual bool is_anonymous() const { return false; }
|
|
|
|
|
virtual bool is_inode() const { return false; }
|
2020-03-01 11:08:28 +01:00
|
|
|
virtual bool is_shared_inode() const { return false; }
|
|
|
|
|
virtual bool is_private_inode() const { return false; }
|
2020-03-07 19:24:41 +02:00
|
|
|
virtual bool is_contiguous() const { return false; }
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2019-08-07 20:12:50 +02:00
|
|
|
size_t page_count() const { return m_physical_pages.size(); }
|
2021-07-11 17:19:07 +02: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 15:13:07 +02:00
|
|
|
|
2019-08-07 20:12:50 +02:00
|
|
|
size_t size() const { return m_physical_pages.size() * PAGE_SIZE; }
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-07-11 17:57:52 +02:00
|
|
|
virtual StringView class_name() const = 0;
|
2020-02-28 20:58:57 +01:00
|
|
|
|
2021-07-23 02:40:16 +02:00
|
|
|
ALWAYS_INLINE void add_region(Region& region)
|
|
|
|
|
{
|
|
|
|
|
ScopedSpinLock locker(m_lock);
|
|
|
|
|
m_regions.append(region);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ALWAYS_INLINE void remove_region(Region& region)
|
|
|
|
|
{
|
|
|
|
|
ScopedSpinLock locker(m_lock);
|
|
|
|
|
m_regions.remove(region);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 23:21:58 -07:00
|
|
|
void register_on_deleted_handler(VMObjectDeletedHandler& handler)
|
|
|
|
|
{
|
2021-07-25 16:59:51 +02:00
|
|
|
ScopedSpinLock locker(m_on_deleted_lock);
|
2020-12-21 23:21:58 -07:00
|
|
|
m_on_deleted.set(&handler);
|
|
|
|
|
}
|
|
|
|
|
void unregister_on_deleted_handler(VMObjectDeletedHandler& handler)
|
|
|
|
|
{
|
2021-07-25 16:59:51 +02:00
|
|
|
ScopedSpinLock locker(m_on_deleted_lock);
|
2020-12-21 23:21:58 -07:00
|
|
|
m_on_deleted.remove(&handler);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 18:06:17 +02:00
|
|
|
protected:
|
2019-08-07 20:12:50 +02:00
|
|
|
explicit VMObject(size_t);
|
2021-07-22 00:02:34 +02:00
|
|
|
explicit VMObject(VMObject const&);
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2019-05-28 11:53:16 +02:00
|
|
|
template<typename Callback>
|
|
|
|
|
void for_each_region(Callback);
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-05-26 02:57:46 -07:00
|
|
|
IntrusiveListNode<VMObject> m_list_node;
|
2021-07-11 17:38:28 +02:00
|
|
|
FixedArray<RefPtr<PhysicalPage>> m_physical_pages;
|
2019-08-07 18:06:17 +02:00
|
|
|
|
2021-07-23 02:40:16 +02:00
|
|
|
mutable RecursiveSpinLock m_lock;
|
2020-09-05 15:52:14 -06:00
|
|
|
|
2019-08-07 18:06:17 +02:00
|
|
|
private:
|
2021-07-22 00:02:34 +02:00
|
|
|
VMObject& operator=(VMObject const&) = delete;
|
2019-08-07 18:06:17 +02:00
|
|
|
VMObject& operator=(VMObject&&) = delete;
|
|
|
|
|
VMObject(VMObject&&) = delete;
|
2021-01-02 12:03:14 -07:00
|
|
|
|
2020-12-21 23:21:58 -07:00
|
|
|
HashTable<VMObjectDeletedHandler*> m_on_deleted;
|
|
|
|
|
SpinLock<u8> m_on_deleted_lock;
|
2021-05-26 02:57:46 -07:00
|
|
|
|
2021-07-23 02:40:16 +02:00
|
|
|
Region::ListInVMObject m_regions;
|
|
|
|
|
|
2021-05-26 02:57:46 -07:00
|
|
|
public:
|
|
|
|
|
using List = IntrusiveList<VMObject, RawPtr<VMObject>, &VMObject::m_list_node>;
|
2019-04-03 15:13:07 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2021-07-23 02:40:16 +02:00
|
|
|
template<typename Callback>
|
|
|
|
|
inline void VMObject::for_each_region(Callback callback)
|
|
|
|
|
{
|
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
|
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 01:27:42 +01:00
|
|
|
}
|