2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-08 10:53:24 +02:00
|
|
|
#include <AK/InlineLinkedList.h>
|
2019-09-16 10:19:44 +02:00
|
|
|
#include <AK/String.h>
|
2020-02-24 13:24:30 +01:00
|
|
|
#include <AK/Weakable.h>
|
2020-06-27 17:06:33 -06:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-09-16 10:19:44 +02:00
|
|
|
#include <Kernel/Heap/SlabAllocator.h>
|
2019-05-17 04:32:08 +02:00
|
|
|
#include <Kernel/VM/RangeAllocator.h>
|
2020-04-05 22:58:44 +03:00
|
|
|
#include <Kernel/VM/VMObject.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
class Inode;
|
|
|
|
class VMObject;
|
|
|
|
|
2019-11-04 00:45:33 +01:00
|
|
|
enum class PageFaultResponse {
|
|
|
|
ShouldCrash,
|
2020-05-06 21:11:38 +02:00
|
|
|
OutOfMemory,
|
2019-11-04 00:45:33 +01:00
|
|
|
Continue,
|
|
|
|
};
|
|
|
|
|
2020-02-24 13:24:30 +01:00
|
|
|
class Region final
|
|
|
|
: public InlineLinkedListNode<Region>
|
|
|
|
, public Weakable<Region> {
|
2019-04-03 15:13:07 +02:00
|
|
|
friend class MemoryManager;
|
2019-06-07 11:43:58 +02:00
|
|
|
|
2019-09-16 10:19:44 +02:00
|
|
|
MAKE_SLAB_ALLOCATED(Region)
|
2019-04-03 15:13:07 +02:00
|
|
|
public:
|
2019-06-07 17:13:23 +02:00
|
|
|
enum Access {
|
2019-05-30 16:14:37 +02:00
|
|
|
Read = 1,
|
|
|
|
Write = 2,
|
|
|
|
Execute = 4,
|
|
|
|
};
|
|
|
|
|
2020-04-12 20:22:26 +02:00
|
|
|
enum class InheritMode {
|
|
|
|
Default,
|
|
|
|
ZeroedOnFork,
|
|
|
|
};
|
|
|
|
|
2020-01-09 23:29:31 +02:00
|
|
|
static NonnullOwnPtr<Region> create_user_accessible(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const StringView& name, u8 access, bool cacheable = true);
|
|
|
|
static NonnullOwnPtr<Region> create_kernel_only(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const StringView& name, u8 access, bool cacheable = true);
|
2019-07-19 16:09:34 +02:00
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
~Region();
|
|
|
|
|
2019-08-29 20:55:40 +02:00
|
|
|
const Range& range() const { return m_range; }
|
2019-06-07 12:56:50 +02:00
|
|
|
VirtualAddress vaddr() const { return m_range.base(); }
|
2019-05-17 04:32:08 +02:00
|
|
|
size_t size() const { return m_range.size(); }
|
2019-05-30 16:14:37 +02:00
|
|
|
bool is_readable() const { return m_access & Access::Read; }
|
|
|
|
bool is_writable() const { return m_access & Access::Write; }
|
|
|
|
bool is_executable() const { return m_access & Access::Execute; }
|
2020-01-09 23:29:31 +02:00
|
|
|
bool is_cacheable() const { return m_cacheable; }
|
2019-06-07 20:58:12 +02:00
|
|
|
const String& name() const { return m_name; }
|
2019-08-29 20:55:40 +02:00
|
|
|
unsigned access() const { return m_access; }
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2019-06-07 20:58:12 +02:00
|
|
|
void set_name(const String& name) { m_name = name; }
|
2020-09-11 21:11:07 -06:00
|
|
|
void set_name(String&& name) { m_name = move(name); }
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2019-09-04 11:27:14 +02:00
|
|
|
const VMObject& vmobject() const { return *m_vmobject; }
|
|
|
|
VMObject& vmobject() { return *m_vmobject; }
|
2020-04-05 22:58:44 +03:00
|
|
|
void set_vmobject(NonnullRefPtr<VMObject>&& obj) { m_vmobject = obj; }
|
2019-04-03 15:13:07 +02:00
|
|
|
|
|
|
|
bool is_shared() const { return m_shared; }
|
|
|
|
void set_shared(bool shared) { m_shared = shared; }
|
|
|
|
|
2019-11-17 12:11:43 +01:00
|
|
|
bool is_stack() const { return m_stack; }
|
|
|
|
void set_stack(bool stack) { m_stack = stack; }
|
|
|
|
|
2019-11-24 12:24:16 +01:00
|
|
|
bool is_mmap() const { return m_mmap; }
|
|
|
|
void set_mmap(bool mmap) { m_mmap = mmap; }
|
|
|
|
|
2019-07-19 16:09:34 +02:00
|
|
|
bool is_user_accessible() const { return m_user_accessible; }
|
2019-12-15 20:11:57 +01:00
|
|
|
void set_user_accessible(bool b) { m_user_accessible = b; }
|
2019-07-19 16:09:34 +02:00
|
|
|
|
2020-06-01 22:55:09 -06:00
|
|
|
bool is_kernel() const { return m_kernel || vaddr().get() >= 0xc0000000; }
|
|
|
|
void set_kernel(bool kernel) { m_kernel = kernel; }
|
|
|
|
|
2019-11-04 00:45:33 +01:00
|
|
|
PageFaultResponse handle_fault(const PageFault&);
|
|
|
|
|
2019-09-27 14:19:07 +02:00
|
|
|
NonnullOwnPtr<Region> clone();
|
2019-05-17 04:32:08 +02:00
|
|
|
|
2019-06-07 12:56:50 +02:00
|
|
|
bool contains(VirtualAddress vaddr) const
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2019-06-07 12:56:50 +02:00
|
|
|
return m_range.contains(vaddr);
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
2019-08-29 20:55:40 +02:00
|
|
|
bool contains(const Range& range) const
|
|
|
|
{
|
|
|
|
return m_range.contains(range);
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:56:50 +02:00
|
|
|
unsigned page_index_from_address(VirtualAddress vaddr) const
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2019-06-07 12:56:50 +02:00
|
|
|
return (vaddr - m_range.base()).get() / PAGE_SIZE;
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
2020-09-18 09:49:51 +02:00
|
|
|
|
2020-07-06 12:47:08 -06:00
|
|
|
VirtualAddress vaddr_from_page_index(size_t page_index) const
|
|
|
|
{
|
|
|
|
return vaddr().offset(page_index * PAGE_SIZE);
|
|
|
|
}
|
2019-04-03 15:13:07 +02:00
|
|
|
|
|
|
|
size_t first_page_index() const
|
|
|
|
{
|
2019-12-19 19:13:44 +01:00
|
|
|
return m_offset_in_vmobject / PAGE_SIZE;
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t last_page_index() const
|
|
|
|
{
|
|
|
|
return (first_page_index() + page_count()) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t page_count() const
|
|
|
|
{
|
2019-05-17 04:32:08 +02:00
|
|
|
return size() / PAGE_SIZE;
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:19:50 +02:00
|
|
|
const PhysicalPage* physical_page(size_t index) const
|
|
|
|
{
|
|
|
|
ASSERT(index < page_count());
|
|
|
|
return vmobject().physical_pages()[first_page_index() + index];
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<PhysicalPage>& physical_page_slot(size_t index)
|
|
|
|
{
|
|
|
|
ASSERT(index < page_count());
|
|
|
|
return vmobject().physical_pages()[first_page_index() + index];
|
|
|
|
}
|
|
|
|
|
2019-10-01 11:38:59 +02:00
|
|
|
size_t offset_in_vmobject() const
|
|
|
|
{
|
2019-12-19 19:13:44 +01:00
|
|
|
return m_offset_in_vmobject;
|
2019-10-01 11:38:59 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:43:32 +01:00
|
|
|
bool commit();
|
2019-04-03 15:13:07 +02:00
|
|
|
|
|
|
|
size_t amount_resident() const;
|
|
|
|
size_t amount_shared() const;
|
2019-12-29 12:28:32 +01:00
|
|
|
size_t amount_dirty() const;
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2019-10-01 19:58:41 +02:00
|
|
|
bool should_cow(size_t page_index) const;
|
|
|
|
void set_should_cow(size_t page_index, bool);
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2019-12-15 16:53:00 +01:00
|
|
|
u32 cow_pages() const;
|
|
|
|
|
2019-12-25 02:39:03 +01:00
|
|
|
void set_readable(bool b) { set_access_bit(Access::Read, b); }
|
|
|
|
void set_writable(bool b) { set_access_bit(Access::Write, b); }
|
|
|
|
void set_executable(bool b) { set_access_bit(Access::Execute, b); }
|
2019-12-02 19:14:16 +01:00
|
|
|
|
2020-01-09 23:29:31 +02:00
|
|
|
void set_page_directory(PageDirectory&);
|
2020-09-01 16:10:54 -06:00
|
|
|
bool map(PageDirectory&);
|
2019-11-03 20:37:03 +01:00
|
|
|
enum class ShouldDeallocateVirtualMemoryRange {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
void unmap(ShouldDeallocateVirtualMemoryRange = ShouldDeallocateVirtualMemoryRange::Yes);
|
|
|
|
|
2019-11-03 20:59:54 +01:00
|
|
|
void remap();
|
2019-11-03 15:32:11 +01:00
|
|
|
|
2019-08-08 10:53:24 +02:00
|
|
|
// For InlineLinkedListNode
|
|
|
|
Region* m_next { nullptr };
|
|
|
|
Region* m_prev { nullptr };
|
|
|
|
|
2019-09-27 14:19:07 +02:00
|
|
|
// NOTE: These are public so we can make<> them.
|
2020-06-01 22:55:09 -06:00
|
|
|
Region(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const String&, u8 access, bool cacheable, bool kernel);
|
2019-07-19 16:09:34 +02:00
|
|
|
|
2020-04-12 20:22:26 +02:00
|
|
|
void set_inherit_mode(InheritMode inherit_mode) { m_inherit_mode = inherit_mode; }
|
|
|
|
|
2019-09-27 14:19:07 +02:00
|
|
|
private:
|
2019-10-01 19:58:41 +02:00
|
|
|
Bitmap& ensure_cow_map() const;
|
|
|
|
|
2019-12-25 02:39:03 +01:00
|
|
|
void set_access_bit(Access access, bool b)
|
|
|
|
{
|
|
|
|
if (b)
|
|
|
|
m_access |= access;
|
|
|
|
else
|
|
|
|
m_access &= ~access;
|
|
|
|
}
|
|
|
|
|
2020-07-06 12:47:08 -06:00
|
|
|
bool commit(size_t page_index);
|
2020-09-01 16:10:54 -06:00
|
|
|
bool remap_page(size_t index, bool with_flush = true);
|
2020-07-06 12:47:08 -06:00
|
|
|
|
2019-11-04 00:45:33 +01:00
|
|
|
PageFaultResponse handle_cow_fault(size_t page_index);
|
|
|
|
PageFaultResponse handle_inode_fault(size_t page_index);
|
|
|
|
PageFaultResponse handle_zero_fault(size_t page_index);
|
|
|
|
|
2020-09-01 16:10:54 -06:00
|
|
|
bool map_individual_page_impl(size_t page_index);
|
2020-01-01 19:30:38 +01:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<PageDirectory> m_page_directory;
|
2019-05-17 04:32:08 +02:00
|
|
|
Range m_range;
|
2019-12-19 19:13:44 +01:00
|
|
|
size_t m_offset_in_vmobject { 0 };
|
2019-09-04 11:27:14 +02:00
|
|
|
NonnullRefPtr<VMObject> m_vmobject;
|
2019-04-03 15:13:07 +02:00
|
|
|
String m_name;
|
2019-07-03 21:17:35 +02:00
|
|
|
u8 m_access { 0 };
|
2020-04-12 20:22:26 +02:00
|
|
|
InheritMode m_inherit_mode : 3 { InheritMode::Default };
|
2020-02-19 12:01:39 +01:00
|
|
|
bool m_shared : 1 { false };
|
|
|
|
bool m_user_accessible : 1 { false };
|
|
|
|
bool m_cacheable : 1 { false };
|
|
|
|
bool m_stack : 1 { false };
|
|
|
|
bool m_mmap : 1 { false };
|
2020-06-01 22:55:09 -06:00
|
|
|
bool m_kernel : 1 { false };
|
2019-10-01 19:58:41 +02:00
|
|
|
mutable OwnPtr<Bitmap> m_cow_map;
|
2019-04-03 15:13:07 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
inline unsigned prot_to_region_access_flags(int prot)
|
|
|
|
{
|
|
|
|
unsigned access = 0;
|
|
|
|
if (prot & PROT_READ)
|
|
|
|
access |= Region::Access::Read;
|
|
|
|
if (prot & PROT_WRITE)
|
|
|
|
access |= Region::Access::Write;
|
|
|
|
if (prot & PROT_EXEC)
|
|
|
|
access |= Region::Access::Execute;
|
|
|
|
return access;
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|