Commit graph

175 commits

Author SHA1 Message Date
Idan Horowitz f0166efe8c Kernel: Use the whole kernel PD range when randomizing the KASLR offset
Now that we reclaim the memory range that is created by KASLR before
the start of the kernel image, there's no need to be conservative with
the KASLR offset.
2022-03-23 19:49:49 +02:00
Idan Horowitz e18632660f Kernel: Use the pre-image kernel memory range introduced by KASLR
This ensures we don't just waste the memory range between the default
base load address and the actual load address that was shifted by the
KASLR offset.
2022-03-22 16:46:51 +01:00
Lenny Maiorani 190cf1507b Kernel: Use default constructors/destructors
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-17 00:51:36 -07:00
Idan Horowitz e37e4a7980 Kernel: Make Inode::set_shared_vmobject() OOM-fallible
Allocating a WeakPtr can fail, so this let's us properly propagate said
failure.
2022-02-14 11:35:20 +01:00
Idan Horowitz 75fe51a9ca Kernel: Stop trying to write unmapped Process regions into CoreDumps
If we crashed in the middle of mapping in Regions, some of the regions
may not have a page directory yet, and will result in a crash when
Region::remap() is called.
2022-02-11 17:49:46 +02:00
Idan Horowitz 57bce8ab97 Kernel: Set up Regions before adding them to a Process's AddressSpace
This reduces the amount of time in which not fully-initialized Regions
are present inside an AddressSpace's region tree.
2022-02-11 17:49:46 +02:00
Idan Horowitz d9d3362722 Kernel: Make SharedInodeVMObject pages Bitmap allocation OOM-fallible 2022-02-11 17:49:46 +02:00
Idan Horowitz 8030e2a88f Kernel: Make AnonymousVMObject COW-Bitmap allocation OOM-fallible 2022-02-11 17:49:46 +02:00
Idan Horowitz 871a53db76 AK: Make Bitmap construction OOM-fallible 2022-02-11 17:49:46 +02:00
Andreas Kling 2ff9db0245 Kernel: Make contiguous VM objects use "user physical pages" by default
If someone specifically wants contiguous memory in the low-physical-
address-for-DMA range ("super pages"), they can use the
allocate_dma_buffer_pages() helper.
2022-02-11 12:45:38 +01:00
Lenny Maiorani c6acf64558 Kernel: Change static constexpr variables to constexpr where possible
Function-local `static constexpr` variables can be `constexpr`. This
can reduce memory consumption, binary size, and offer additional
compiler optimizations.

These changes result in a stripped x86_64 kernel binary size reduction
of 592 bytes.
2022-02-09 21:04:51 +00:00
Idan Horowitz 8289727fac Kernel: Stop using the make<T> factory method in the Kernel
As make<T> is infallible, it really should not be used anywhere in the
Kernel. Instead replace with fallible `new (nothrow)` calls, that will
eventually be error-propagated.
2022-02-03 23:33:20 +01:00
Andreas Kling d85f062990 Revert "Kernel: Only update page tables for faulting region"
This reverts commit 1c5ffaae41.

This broke shared memory as used by OutOfProcessWebView. Let's do
a revert until we can figure out what went wrong.
2022-02-02 11:02:54 +01:00
Andreas Kling 1c5ffaae41 Kernel: Only update page tables for faulting region
When a page fault led to the mapping of a new physical page, we were
updating the page tables for *every* region that shared the same
underlying VMObject.

Let's just not do that, avoiding a bunch of unnecessary page table
updates and TLB invalidations.
2022-02-02 02:16:49 +01:00
Andreas Kling a44316fa8b Kernel: Release page directory and MM locks sooner in space finalization
We don't need to hold these locks when tearing down the region tree.
Release them as soon as unmapping is finished.
2022-01-30 16:21:59 +01:00
Andreas Kling 3845c90e08 Kernel: Remove unnecessary includes from Thread.h
...and deal with the fallout by adding missing includes everywhere.
2022-01-30 16:21:59 +01:00
Lenny Maiorani b0a54518d8 Everywhere: Remove redundant inline keyword
`constexpr` implies `inline` so when both are used it is redundant.
2022-01-29 21:45:17 +02:00
Andreas Kling a12e19c015 Kernel: Move kernel region checks from x86 page fault handler to MM
Ideally the x86 fault handler would only do x86 specific things and
delegate the rest of the work to MemoryManager. This patch moves some of
the address checks to a more generic place.
2022-01-28 23:41:18 +01:00
Andreas Kling 5092813a45 Kernel: Quickly reject userspace addresses in kernel_region_from_vaddr()
This avoids taking and releasing the MM lock just to reject an address
that we can tell from just looking at it that it won't ever be in the
kernel regions tree.
2022-01-28 23:41:18 +01:00
Idan Horowitz 5146315a15 Kernel: Convert MemoryManager::allocate_user_physical_page to ErrorOr
This allows is to use the TRY macro at the call sites, instead of using
clunky null checks.
2022-01-28 19:05:52 +02:00
Idan Horowitz bd5b56cab0 Kernel: Make allocate_supervisor_physical_page OOM-fallible 2022-01-28 19:05:52 +02:00
Idan Horowitz 4d2f1a05ec Kernel: Make allocate_contiguous_supervisor_physical_pages OOM-fallible 2022-01-28 19:05:52 +02:00
Idan Horowitz 956824afe2 Kernel: Use memset instead of fast_u32_fill in MemoryManager zero fills
When the values we're setting are not actually u32s and the size of the
area we're setting is PAGE_SIZE-aligned and a multiple of PAGE_SIZE in
size, there's no point in using fast_u32_fill, as that forces us to use
STOSDs instead of STOSQs.
2022-01-28 19:05:52 +02:00
Idan Horowitz 1e941fc3cc Kernel: Make VirtualRangeAllocator::carve_from_region OOM-fallible 2022-01-26 22:05:34 +00:00
Tom 6e46e21c42 Kernel: Implement Page Attribute Table (PAT) support and Write-Combine
This allows us to enable Write-Combine on e.g. framebuffers,
significantly improving performance on bare metal.

To keep things simple we right now only use one of up to three bits
(bit 7 in the PTE), which maps to the PA4 entry in the PAT MSR, which
we set to the Write-Combine mode on each CPU at boot time.
2022-01-26 09:21:04 +02:00
Idan Horowitz a6f0ab358a Kernel: Make AddressSpace::find_regions_intersecting OOM-fallible 2022-01-26 02:37:03 +02:00
Idan Horowitz bd603003b5 Kernel: Make AddressSpace::amount_clean_inode() OOM-fallible 2022-01-26 02:37:03 +02:00
Marco Cutecchia a7ce0c2297 Kernel: Add missing #include <AK/Badge.h> to MemoryManager.h
This missing header broke the aarch64 build
2022-01-23 14:31:50 +01:00
Idan Horowitz d65347d39d Kernel: Make Memory::RingBuffer construction fallible 2022-01-21 16:27:21 +01:00
Andreas Kling df34f7b90b Kernel: Use an IntrusiveRedBlackTree for kernel regions
We were already using a non-intrusive RedBlackTree, and since the kernel
regions tree is non-owning, this is a trivial conversion that makes a
bunch of the tree operations infallible (by being allocation-free.) :^)
2022-01-16 23:31:01 +01:00
creator1creeper1 326c6130a5 Kernel: Don't access directory table of uninitialized PageDirectory
PageDirectory gets initialized step-by-step in
PageDirectory::try_create_for_userspace(). This initialization may fail
anywhere in this function - for example, we may not be able to
allocate a directory table, in which case
PageDirectory::try_create_for_userspace() will return a null pointer.
We recognize this condition and early-return ENOMEM. However, at this
point, we need to correctly destruct the only partially initialized
PageDirectory. Previously, PageDirectory::~PageDirectory() would assume
that the object it was destructing was always fully initialized. It now
uses the new helper PageDirectory::is_cr3_initialized() to correctly
recognize when the directory table was not yet initialized. This helper
checks if the pointer to the directory table is null. Only if it is not
null does the destructor try to fetch the directory table using
PageDirectory::cr3().
2022-01-16 12:08:57 -08:00
creator1creeper1 1a2f71581b Kernel: Remove infallible VMObject resource factory functions
These infallible resource factory functions were only there to ease the
conversion to the new factory functions. Since all child classes of
VMObject now use the fallible resource factory functions, we don't
need the infallible versions anymore.
2022-01-15 22:16:00 +02:00
creator1creeper1 2a4e410b63 Kernel: Make SharedInodeVMObject construction OOM-aware
This commit moves the allocation of the resources required for
SharedInodeVMObject from its constructors to its factory functions.

We're making this change to expose the fallibility of the allocation.
2022-01-15 22:16:00 +02:00
creator1creeper1 9a1dfe70fe Kernel: Make PrivateInodeVMObject construction OOM-aware
This commit moves the allocation of the resources required for
PrivateInodeVMObject from its constructors to its factory functions.

We're making this change to expose the fallibility of the allocation.
2022-01-15 22:16:00 +02:00
creator1creeper1 ad480ff18b Kernel: Make InodeVMOBject construction OOM-aware
This commit moves the allocation of the resources required for
InodeVMObject from its constructors to the constructors of its child
classes.

We're making this change to give the child classes the chance to expose
the fallibility of the allocation.
2022-01-15 22:16:00 +02:00
creator1creeper1 3879e70447 Kernel: Make AnonymousVMObject construction OOM-aware
This commit moves the allocation of the resources required for
AnonymousVMObject from its constructors to its factory functions.

We're making this change to expose the fallibility of the allocation.
2022-01-15 22:16:00 +02:00
creator1creeper1 d1f265e851 Kernel: Make VMOBject construction OOM-aware
This commit moves the allocation of the resources required for VMObject
from its constructors to the constructors of its child classes.

We're making this change to give the child classes the chance to expose
the fallibility of the allocation.
2022-01-15 22:16:00 +02:00
Andreas Kling 094b88e6a5 Kernel: Don't remap already non-writable regions when they become CoW
The only purpose of the remap() in Region::try_clone() is to ensure
non-writable page table entries for CoW regions. If a region is already
non-writable, there's no need to waste time updating the page tables.
2022-01-15 19:51:16 +01:00
Andreas Kling c6adefcfc0 Kernel: Don't bother with page tables for PROT_NONE mappings
When mapping or unmapping completely inaccessible memory regions,
we don't need to update the page tables at all. This saves a bunch of
time in some situations, most notably during dynamic linking, where we
make a large VM reservation and immediately throw it away. :^)
2022-01-15 19:51:16 +01:00
Andreas Kling 9ddccbc6da Kernel: Use move() in Region::try_clone() to avoid a VMObject::unref() 2022-01-15 19:51:15 +01:00
Andreas Kling 8e0387e674 Kernel: Only register kernel regions with MemoryManager
We were already only tracking kernel regions, this patch just makes it
more clear by having it reflected in the name of the registration
helpers.

We also stop calling them for userspace regions, avoiding some spinlock
action in such cases.
2022-01-15 19:51:15 +01:00
Andreas Kling 4fa3c1bf2d Kernel: Remove old "region lookup cache" optimization
This optimization was added when region lookup was O(n), before we had
the O(log n) RedBlackTree. Let's remove it to simplify the code, as we
have no evidence that it remains valuable.
2022-01-15 19:51:15 +01:00
Idan Horowitz 6e37487477 Kernel: Always remove PageDirectories from the cr3 map on destruction
Previously we would only remove them from the map if they were attached
to an AddressSpace, even though we would always add them to the map on
construction. This results in an assertion failure on destruction if
the page directory was never attached to an AddressSpace. (for example,
on an allocation failure of said AddressSpace)
2022-01-15 11:04:07 +01:00
Idan Horowitz fb3e46e930 Kernel: Make map_typed() & map_typed_writable() fallible using ErrorOr
This mostly just moved the problem, as a lot of the callers are not
capable of propagating the errors themselves, but it's a step in the
right direction.
2022-01-13 22:40:25 +01:00
Andreas Kling de05223122 Kernel: Don't flush TLB after creating brand-new mappings
The CPU will not cache TLB entries for non-present mappings, so we
can simply skip flushing the TLB after creating entirely new mappings.
2022-01-13 11:22:11 +01:00
Idan Horowitz 618f123463 Kernel: Use StringView instead of String in RingBuffer's constructor
This String was being copied into a KString internally anyways.
2022-01-13 00:20:08 -08:00
Andreas Kling 5f71925aa4 Kernel: Actually clear page slots in Region::clear_to_zero()
We were copying the RefPtr<PhysicalPage> and zeroing the copy instead
of zeroing the slot itself.
2022-01-12 14:52:47 +01:00
Andreas Kling d8206c1059 Kernel: Don't release/relock spinlocks repeatedly during space teardown
Grab the page directory and MM locks once at the start of address space
teardown, then hold onto them across all the region unmapping work.
2022-01-12 14:52:47 +01:00
Andreas Kling 2323cdd914 Kernel: Do less unnecessary work when tearing down process address space
When deleting an entire AddressSpace, we don't need to do TLB flushes
at all (since the entire page directory is going away anyway).

We also don't need to deallocate VM ranges one by one, since the entire
VM range allocator will be deleted anyway.
2022-01-12 14:52:47 +01:00
Andreas Kling 24ecf1d021 Kernel: Remove redundant hash map of page tables in PageDirectory
The purpose of the PageDirectory::m_page_tables map was really just
to act as ref-counting storage for PhysicalPage objects that were
being used for the directory's page tables.

However, this was basically redundant, since we can find the physical
address of each page table from the page directory, and we can find the
PhysicalPage object from MemoryManager::get_physical_page_entry().
So if we just manually ref() and unref() the pages when they go in and
out of the directory, we no longer need PageDirectory::m_page_tables!

Not only does this remove a bunch of kmalloc() traffic, it also solves
a race condition that would occur when lazily adding a new page table
to a directory:

Previously, when MemoryManager::ensure_pte() would call HashMap::set()
to insert the new page table into m_page_tables, if the HashMap had to
grow its internal storage, it would call kmalloc(). If that kmalloc()
would need to perform heap expansion, it would end up calling
ensure_pte() again, which would clobber the page directory mapping used
by the outer invocation of ensure_pte().

The net result of the above bug would be that any invocation of
MemoryManager::ensure_pte() could erroneously return a pointer into
a kernel page table instead of the correct one!

This whole problem goes away when we remove the HashMap, as ensure_pte()
no longer does anything that allocates from the heap.
2022-01-10 16:22:37 +01:00