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