2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-07 16:06:17 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/AllocationStrategy.h>
|
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
|
|
|
#include <Kernel/Memory/PageFaultResponse.h>
|
|
|
|
#include <Kernel/Memory/VMObject.h>
|
2020-05-16 10:00:04 +00:00
|
|
|
#include <Kernel/PhysicalAddress.h>
|
2019-08-07 16:06:17 +00:00
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
class AnonymousVMObject final : public VMObject {
|
2019-08-07 16:06:17 +00:00
|
|
|
public:
|
|
|
|
virtual ~AnonymousVMObject() override;
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<AnonymousVMObject>> try_create_with_size(size_t, AllocationStrategy);
|
|
|
|
static ErrorOr<NonnullRefPtr<AnonymousVMObject>> try_create_for_physical_range(PhysicalAddress paddr, size_t size);
|
|
|
|
static ErrorOr<NonnullRefPtr<AnonymousVMObject>> try_create_with_physical_pages(Span<NonnullRefPtr<PhysicalPage>>);
|
|
|
|
static ErrorOr<NonnullRefPtr<AnonymousVMObject>> try_create_purgeable_with_size(size_t, AllocationStrategy);
|
|
|
|
static ErrorOr<NonnullRefPtr<AnonymousVMObject>> try_create_physically_contiguous_with_size(size_t);
|
|
|
|
virtual ErrorOr<NonnullRefPtr<VMObject>> try_clone() override;
|
2020-09-05 03:12:25 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
[[nodiscard]] NonnullRefPtr<PhysicalPage> allocate_committed_page(Badge<Region>);
|
2020-09-05 21:52:14 +00:00
|
|
|
PageFaultResponse handle_cow_fault(size_t, VirtualAddress);
|
|
|
|
size_t cow_pages() const;
|
|
|
|
bool should_cow(size_t page_index, bool) const;
|
2022-02-10 17:17:27 +00:00
|
|
|
ErrorOr<void> set_should_cow(size_t page_index, bool);
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
bool is_purgeable() const { return m_purgeable; }
|
|
|
|
bool is_volatile() const { return m_volatile; }
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> set_volatile(bool is_volatile, bool& was_purged);
|
2021-07-24 23:46:44 +00:00
|
|
|
|
|
|
|
size_t purge();
|
2021-05-16 09:36:52 +00:00
|
|
|
|
2020-09-05 21:52:14 +00:00
|
|
|
private:
|
2021-08-04 20:58:58 +00:00
|
|
|
class SharedCommittedCowPages;
|
|
|
|
|
2022-02-10 17:17:27 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<AnonymousVMObject>> try_create_with_shared_cow(AnonymousVMObject const&, NonnullRefPtr<SharedCommittedCowPages>, FixedArray<RefPtr<PhysicalPage>>&&);
|
|
|
|
|
2022-01-12 15:23:48 +00:00
|
|
|
explicit AnonymousVMObject(FixedArray<RefPtr<PhysicalPage>>&&, AllocationStrategy, Optional<CommittedPhysicalPageSet>);
|
|
|
|
explicit AnonymousVMObject(PhysicalAddress, FixedArray<RefPtr<PhysicalPage>>&&);
|
|
|
|
explicit AnonymousVMObject(FixedArray<RefPtr<PhysicalPage>>&&);
|
2022-07-10 13:40:48 +00:00
|
|
|
explicit AnonymousVMObject(WeakPtr<AnonymousVMObject>, NonnullRefPtr<SharedCommittedCowPages>, FixedArray<RefPtr<PhysicalPage>>&&);
|
2019-12-09 18:12:38 +00:00
|
|
|
|
2021-07-11 15:57:52 +00:00
|
|
|
virtual StringView class_name() const override { return "AnonymousVMObject"sv; }
|
2020-02-28 19:58:57 +00:00
|
|
|
|
2021-07-21 22:02:34 +00:00
|
|
|
AnonymousVMObject& operator=(AnonymousVMObject const&) = delete;
|
2019-08-07 16:06:17 +00:00
|
|
|
AnonymousVMObject& operator=(AnonymousVMObject&&) = delete;
|
|
|
|
AnonymousVMObject(AnonymousVMObject&&) = delete;
|
|
|
|
|
|
|
|
virtual bool is_anonymous() const override { return true; }
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2022-02-10 17:17:27 +00:00
|
|
|
ErrorOr<void> ensure_cow_map();
|
|
|
|
ErrorOr<void> ensure_or_reset_cow_map();
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-08-04 20:49:13 +00:00
|
|
|
Optional<CommittedPhysicalPageSet> m_unused_committed_pages;
|
2021-03-04 09:05:38 +00:00
|
|
|
Bitmap m_cow_map;
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-08-04 20:49:13 +00:00
|
|
|
// AnonymousVMObject shares committed COW pages with cloned children (happens on fork)
|
|
|
|
class SharedCommittedCowPages : public RefCounted<SharedCommittedCowPages> {
|
|
|
|
AK_MAKE_NONCOPYABLE(SharedCommittedCowPages);
|
|
|
|
|
|
|
|
public:
|
|
|
|
SharedCommittedCowPages() = delete;
|
|
|
|
|
|
|
|
explicit SharedCommittedCowPages(CommittedPhysicalPageSet&&);
|
|
|
|
~SharedCommittedCowPages();
|
|
|
|
|
|
|
|
[[nodiscard]] bool is_empty() const { return m_committed_pages.is_empty(); }
|
|
|
|
|
2021-08-05 15:14:13 +00:00
|
|
|
[[nodiscard]] NonnullRefPtr<PhysicalPage> take_one();
|
|
|
|
void uncommit_one();
|
|
|
|
|
2021-10-31 22:41:43 +00:00
|
|
|
private:
|
2021-09-05 17:02:03 +00:00
|
|
|
Spinlock m_lock;
|
2021-08-04 20:49:13 +00:00
|
|
|
CommittedPhysicalPageSet m_committed_pages;
|
|
|
|
};
|
|
|
|
|
2022-07-10 13:40:48 +00:00
|
|
|
WeakPtr<AnonymousVMObject> m_cow_parent;
|
2021-08-04 20:49:13 +00:00
|
|
|
RefPtr<SharedCommittedCowPages> m_shared_committed_cow_pages;
|
2021-07-24 23:46:44 +00:00
|
|
|
|
|
|
|
bool m_purgeable { false };
|
|
|
|
bool m_volatile { false };
|
|
|
|
bool m_was_purged { false };
|
2019-08-07 16:06:17 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2020-05-08 20:10:47 +00:00
|
|
|
}
|