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