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
|
|
|
|
|
2020-05-16 12:00:04 +02:00
|
|
|
#include <Kernel/PhysicalAddress.h>
|
2020-09-05 15:52:14 -06:00
|
|
|
#include <Kernel/VM/AllocationStrategy.h>
|
2021-06-21 17:34:09 +02:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
2020-09-05 15:52:14 -06:00
|
|
|
#include <Kernel/VM/PageFaultResponse.h>
|
|
|
|
#include <Kernel/VM/PurgeablePageRanges.h>
|
2020-07-26 17:15:51 +02:00
|
|
|
#include <Kernel/VM/VMObject.h>
|
2019-08-07 18:06:17 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-05-03 01:06:15 -07:00
|
|
|
class AnonymousVMObject final : public VMObject {
|
2020-09-05 15:52:14 -06:00
|
|
|
friend class PurgeablePageRanges;
|
|
|
|
|
2019-08-07 18:06:17 +02:00
|
|
|
public:
|
|
|
|
virtual ~AnonymousVMObject() override;
|
|
|
|
|
2021-07-11 17:55:29 +02:00
|
|
|
static RefPtr<AnonymousVMObject> try_create_with_size(size_t, AllocationStrategy);
|
|
|
|
static RefPtr<AnonymousVMObject> try_create_for_physical_range(PhysicalAddress paddr, size_t size);
|
|
|
|
static RefPtr<AnonymousVMObject> try_create_with_physical_page(PhysicalPage& page);
|
|
|
|
static RefPtr<AnonymousVMObject> try_create_with_physical_pages(NonnullRefPtrVector<PhysicalPage>);
|
2021-07-11 19:07:00 +02:00
|
|
|
virtual RefPtr<VMObject> try_clone() override;
|
2020-09-04 21:12:25 -06:00
|
|
|
|
2020-09-05 15:52:14 -06:00
|
|
|
RefPtr<PhysicalPage> allocate_committed_page(size_t);
|
|
|
|
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);
|
|
|
|
|
|
|
|
void register_purgeable_page_ranges(PurgeablePageRanges&);
|
|
|
|
void unregister_purgeable_page_ranges(PurgeablePageRanges&);
|
|
|
|
|
|
|
|
int purge();
|
|
|
|
int purge_with_interrupts_disabled(Badge<MemoryManager>);
|
|
|
|
|
|
|
|
bool is_any_volatile() const;
|
|
|
|
|
2021-05-16 02:36:52 -07:00
|
|
|
template<IteratorFunction<const VolatilePageRange&> F>
|
2020-09-05 15:52:14 -06:00
|
|
|
IterationDecision for_each_volatile_range(F f) const
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_lock.is_locked());
|
2020-09-05 15:52:14 -06:00
|
|
|
// This is a little ugly. Basically, we're trying to find the
|
|
|
|
// volatile ranges that all share, because those are the only
|
|
|
|
// pages we can actually purge
|
|
|
|
for (auto* purgeable_range : m_purgeable_ranges) {
|
|
|
|
ScopedSpinLock purgeable_lock(purgeable_range->m_volatile_ranges_lock);
|
|
|
|
for (auto& r1 : purgeable_range->volatile_ranges().ranges()) {
|
|
|
|
VolatilePageRange range(r1);
|
|
|
|
for (auto* purgeable_range2 : m_purgeable_ranges) {
|
|
|
|
if (purgeable_range2 == purgeable_range)
|
|
|
|
continue;
|
|
|
|
ScopedSpinLock purgeable2_lock(purgeable_range2->m_volatile_ranges_lock);
|
|
|
|
if (purgeable_range2->is_empty()) {
|
|
|
|
// If just one doesn't allow any purging, we can
|
|
|
|
// immediately bail
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
for (const auto& r2 : purgeable_range2->volatile_ranges().ranges()) {
|
|
|
|
range = range.intersected(r2);
|
|
|
|
if (range.is_empty())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (range.is_empty())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (range.is_empty())
|
|
|
|
continue;
|
|
|
|
IterationDecision decision = f(range);
|
|
|
|
if (decision != IterationDecision::Continue)
|
|
|
|
return decision;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
2021-05-16 02:36:52 -07:00
|
|
|
template<IteratorFunction<const VolatilePageRange&> F>
|
2020-09-05 15:52:14 -06:00
|
|
|
IterationDecision for_each_nonvolatile_range(F f) const
|
|
|
|
{
|
|
|
|
size_t base = 0;
|
|
|
|
for_each_volatile_range([&](const VolatilePageRange& volatile_range) {
|
|
|
|
if (volatile_range.base == base)
|
|
|
|
return IterationDecision::Continue;
|
2021-05-16 02:36:52 -07:00
|
|
|
IterationDecision decision = f(VolatilePageRange { base, volatile_range.base - base });
|
2020-09-05 15:52:14 -06:00
|
|
|
if (decision != IterationDecision::Continue)
|
|
|
|
return decision;
|
|
|
|
base = volatile_range.base + volatile_range.count;
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
if (base < page_count())
|
2021-05-16 02:36:52 -07:00
|
|
|
return f(VolatilePageRange { base, page_count() - base });
|
2020-09-05 15:52:14 -06:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
2021-05-16 02:36:52 -07:00
|
|
|
template<VoidFunction<const VolatilePageRange&> F>
|
|
|
|
IterationDecision for_each_volatile_range(F f) const
|
|
|
|
{
|
|
|
|
return for_each_volatile_range([&](auto& range) {
|
|
|
|
f(range);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<VoidFunction<const VolatilePageRange&> F>
|
|
|
|
IterationDecision for_each_nonvolatile_range(F f) const
|
|
|
|
{
|
|
|
|
return for_each_nonvolatile_range([&](auto range) {
|
|
|
|
f(move(range));
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-05 15:52:14 -06:00
|
|
|
private:
|
|
|
|
explicit AnonymousVMObject(size_t, AllocationStrategy);
|
|
|
|
explicit AnonymousVMObject(PhysicalAddress, size_t);
|
|
|
|
explicit AnonymousVMObject(PhysicalPage&);
|
2021-02-26 14:32:43 +02:00
|
|
|
explicit AnonymousVMObject(NonnullRefPtrVector<PhysicalPage>);
|
2019-08-07 18:06:17 +02:00
|
|
|
explicit AnonymousVMObject(const AnonymousVMObject&);
|
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
|
|
|
|
2020-09-05 15:52:14 -06:00
|
|
|
int purge_impl();
|
|
|
|
void update_volatile_cache();
|
|
|
|
void set_was_purged(const VolatilePageRange&);
|
|
|
|
size_t remove_lazy_commit_pages(const VolatilePageRange&);
|
|
|
|
void range_made_volatile(const VolatilePageRange&);
|
|
|
|
void range_made_nonvolatile(const VolatilePageRange&);
|
|
|
|
size_t count_needed_commit_pages_for_nonvolatile_range(const VolatilePageRange&);
|
|
|
|
size_t mark_committed_pages_for_nonvolatile_range(const VolatilePageRange&, size_t);
|
|
|
|
bool is_nonvolatile(size_t page_index);
|
2019-08-07 18:06:17 +02:00
|
|
|
|
|
|
|
AnonymousVMObject& operator=(const AnonymousVMObject&) = delete;
|
|
|
|
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();
|
|
|
|
|
|
|
|
VolatilePageRanges m_volatile_ranges_cache;
|
|
|
|
bool m_volatile_ranges_cache_dirty { true };
|
|
|
|
Vector<PurgeablePageRanges*> m_purgeable_ranges;
|
|
|
|
size_t m_unused_committed_pages { 0 };
|
|
|
|
|
2021-03-04 10:05:38 +01:00
|
|
|
Bitmap m_cow_map;
|
2020-09-05 15:52:14 -06:00
|
|
|
|
|
|
|
// We share a pool of committed cow-pages with clones
|
|
|
|
RefPtr<CommittedCowPages> m_shared_committed_cow_pages;
|
2019-08-07 18:06:17 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2020-05-08 22:10:47 +02:00
|
|
|
}
|