Commit Graph

612 Commits

Author SHA1 Message Date
Andreas Kling
88d490566f Kernel: Rename various *VMObject::create*() => try_create()
try_*() implies that it can fail (and they all return RefPtr with
nullptr signalling failure.)
2021-07-11 17:55:29 +02:00
Andreas Kling
af8c74a328 Kernel: Make SharedInodeVMObject allocation OOM-safe 2021-07-11 17:52:07 +02:00
Andreas Kling
59049ae4b7 Kernel: Store VMObject physical pages in a FixedArray
Let's enforce the invariant that VMObjects don't shrink or grow by
storing the pages in a FixedArray.
2021-07-11 17:42:31 +02:00
Andreas Kling
7d096a1287 Kernel: Make VMObject vend physical page range as a span
Stop exposing the internal data structure used for storing the physical
pages and return a Span<RefPtr<PhysicalPage>> instead.
2021-07-11 17:42:31 +02:00
Andreas Kling
055726ecf5 Kernel: Remove unused MemoryManager::find_region_from_vaddr() 2021-07-11 17:42:31 +02:00
Andreas Kling
112c7f9a5b Kernel: Remove unused MemoryManager::m_physical_page_entries_free 2021-07-11 15:15:57 +02:00
Andreas Kling
b2cd9b2c88 Kernel: Remove pointless ref-counting from PhysicalRegion
These are not multiple-owner objects and have no use for ref-counting.
Make them simple value types instead (not eternal heap-allocated.)
2021-07-11 15:15:57 +02:00
Andreas Kling
98080497d2 Kernel: Use Forward.h headers more 2021-07-11 14:14:51 +02:00
x-yl
1fe08759e3 Kernel: Support multiport for VirtIOConsole
This involves refactoring VirtIOConsole into VirtIOConsole and
VirtIOConsolePort. VirtIOConsole is the VirtIODevice, it owns multiple
VirtIOConsolePorts as well as two control queues. Each
VirtIOConsolePort is a CharacterDevice.
2021-07-09 13:19:21 +04:30
x-yl
1492bb2fd6 Kernel: Add support for reading from VirtIOConsole
This allows two-way communication with the host through a VirtIOConsole.
This is necessary for features like clipboard sharing.
2021-07-09 13:19:21 +04:30
Tom
c1006a3689 Kernel: Return an already destructed PhysicalPage to the allocators
By making sure the PhysicalPage instance is fully destructed the
allocators will have a chance to reclaim the PhysicalPageEntry for
free-list purposes. Just pass them the physical address of the page
that was freed, which is enough to lookup the PhysicalPageEntry later.
2021-07-08 11:43:34 +02:00
Tom
87dc4c3d2c Kernel: Move PhysicalPage classes out of the heap into an array
By moving the PhysicalPage classes out of the kernel heap into a static
array, one for each physical page, we can avoid the added overhead and
easily find them by indexing into an array.

This also wraps the PhysicalPage into a PhysicalPageEntry, which allows
us to re-use each slot with information where to find the next free
page.
2021-07-08 11:43:34 +02:00
Tom
ad5d9d648b Kernel: Use PAE to allow accessing all physical memory beyond 4GB
We already use PAE for the NX bit, but this changes the PhysicalAddress
structure to be able to hold 64 bit physical addresses. This allows us
to use all the available physical memory.
2021-07-08 11:43:34 +02:00
Liav A
4499b0418c Kernel/TypedMapping: Round up length with offset_in_page
Fixes #6948.
2021-07-02 13:16:12 +02:00
Gunnar Beutner
b5aad1c81d Kernel: Fix GDT and segment selectors to make userland work on x86_64
Userland faulted on the very first instruction before because the
PML4T/PDPT/etc. weren't marked as user-accessible. For some reason
x86 doesn't care about that.

Also, we need to provide an appropriate userspace stack segment
selector to iretq.
2021-06-28 22:29:28 +02:00
Hendiadyoin1
10728cd421 Kernel: Fix page round wrap detection for 64-bit
We were assuming 32-bit pointers, which will not always be the case
Also fixed an incorrect comment about wrapping
2021-06-28 19:26:06 +02:00
Hendiadyoin1
65566d6868 Kernel: Make and use KERNEL_BASE
This is to make the 0xc0000000 less a magic number, and will make it
easier in the future to move the Kernel around
2021-06-28 19:26:06 +02:00
Gunnar Beutner
cdcb75709a Kernel: Fix type for PageDirectory::s_cr3_map 2021-06-28 15:55:00 +02:00
Gunnar Beutner
f285241cb8 Kernel: Rename Thread::tss to Thread::regs and add x86_64 support
We're using software context switches so calling this struct tss is
somewhat misleading.
2021-06-27 15:46:42 +02:00
Gunnar Beutner
1e20dd2a45 Kernel: Add PML4T support for the PageDirectory class 2021-06-26 11:08:52 +02:00
Daniel Bertalan
f820917a76 Everywhere: Use nothrow new with adopt_{ref,own}_if_nonnull
This commit converts naked `new`s to `AK::try_make` and `AK::try_create`
wherever possible. If the called constructor is private, this can not be
done, so we instead now use the standard-defined and compiler-agnostic
`new (nothrow)`.
2021-06-24 17:35:49 +04:30
Gunnar Beutner
38fca26f54 Kernel: Add stubs for missing x86_64 functionality
This adds just enough stubs to make the kernel compile on x86_64. Obviously
it won't do anything useful - in fact it won't even attempt to boot because
Multiboot doesn't support ELF64 binaries - but it gets those compiler errors
out of the way so more progress can be made getting all the missing
functionality in place.
2021-06-24 09:27:13 +02:00
Hendiadyoin1
62f9377656 Kernel: Move special sections into Sections.h
This also removes a lot of CPU.h includes infavor for Sections.h
2021-06-24 00:38:23 +02:00
Hendiadyoin1
7ca3d413f7 Kernel: Pull apart CPU.h
This does not add any functional changes
2021-06-24 00:38:23 +02:00
Gunnar Beutner
67c2d39422 Kernel: Fix incorrect argument name 2021-06-17 19:52:13 +02:00
Gunnar Beutner
bc3076f894 Kernel: Remove various other uses of ssize_t 2021-06-16 21:29:36 +02:00
Andreas Kling
dc65f54c06 AK: Rename Vector::append(Vector) => Vector::extend(Vector)
Let's make it a bit more clear when we're appending the elements from
one vector to the end of another vector.
2021-06-12 13:24:45 +02:00
Gunnar Beutner
6f38ce8f47 Kernel: Avoid allocations in the VMObject constructor
This avoids allocations in the VMObject constructor. The number of
inline elements was determined empirically and covers most common cases
including LibC malloc.
2021-06-02 23:00:14 +01:00
Gunnar Beutner
fe0ae3161a Kernel: Fix use-after-free in sys$mremap
Now that Region::name() has been changed to return a StringView we
can't rely on keeping a copy of the region's name past the region's
destruction just by holding a copy of the StringView.
2021-06-02 18:00:13 +02:00
Ben Wiederhake
a7c265f341 Everywhere: Sort out superfluous QuickSort.h imports
They were sorta unneeded. :^)
2021-05-29 23:41:54 +01:00
Gunnar Beutner
95c2166ca9 Kernel: Move sys$munmap functionality into a helper method 2021-05-29 15:53:08 +02:00
Brian Gianforcaro
9b1ff3d3ac Kernel: Make Region creation API OOM safe
- Make Region::create_kernel_only OOM safe.

- Make Region::create_user_accessible mostly OOM safe, there are still
  some tendrils to untangle before it and be completely fixed.
2021-05-29 09:04:05 +02:00
Brian Gianforcaro
8fc6168f21 Kernel: Make Space::create API OOM safe 2021-05-29 09:04:05 +02:00
Brian Gianforcaro
864b1a65e3 Kernel: Make ContiguousVMObject factory API OOM safe 2021-05-29 09:04:05 +02:00
Brian Gianforcaro
cb45b2c001 Kernel: Make AnonymousVMObject::clone() API OOM safe
Propagate allocation failure of m_shared_committed_cow_pages,
and uncommit previously committed COW pages on failure.

This method needs a closer look in terms of error handling, as we
will eventually need to rollback all changes on allocation failure.
Alternatively we could allocate the anonymous object much earlier
and only initialize it once the other steps have succeeded.
2021-05-29 09:04:05 +02:00
Brian Gianforcaro
65d5f81afc Kernel: Make PrivateInodeVMObject factory APIs OOM safe 2021-05-29 09:04:05 +02:00
Andreas Kling
fc9ce22981 Kernel: Use KString for Region names
Replace the AK::String used for Region::m_name with a KString.

This seems beneficial across the board, but as a specific data point,
it reduces time spent in sys$set_mmap_name() by ~50% on test-js. :^)
2021-05-28 09:37:09 +02:00
Brian Gianforcaro
2045782a6e Kernel: Switch VMObject to IntrusiveList from InlineLinkedList 2021-05-26 20:24:32 +02:00
Brian Gianforcaro
e6f73d69a2 Kernel: Switch Region to IntrusiveList from InlineLinkedList 2021-05-26 20:24:32 +02:00
Tom
fe679de791 Kernel: Release the paging lock while reading from the disk
Because reading from the disk may preempt, we need to release the
paging lock.
2021-05-25 10:35:41 +02:00
Brian Gianforcaro
1415b2cfc3 Kernel: Do not allocate AnonymousVMObject's under spin lock
Spinlocks guard short regions, with hopefully no other locks being taken
in the process. Violating constraints usually had detrimental effects on
platform stability as well as performance and scalability. Allocating
memory takes it own locks, and can in some cases even allocate new
regions, and thus violates these tenants.

Move the AnonymousVMObject creation outside of the spinlock as
creation does not modify any shared state.
2021-05-20 08:10:07 +02:00
Lenny Maiorani
5751327195 Kernel: static vs non-static constexpr variables
Problem:
- `static` variables consume memory and sometimes are less
  optimizable.
- `static const` variables can be `constexpr`, usually.
- `static` function-local variables require an initialization check
  every time the function is run.

Solution:
- If a global `static` variable is only used in a single function then
  move it into the function and make it non-`static` and `constexpr`.
- Make all global `static` variables `constexpr` instead of `const`.
- Change function-local `static const[expr]` variables to be just
  `constexpr`.
2021-05-19 21:21:47 +01:00
Liav A
dac129e10b Kernel: Expand the kernel memory slot from 16 MiB to 32 MiB
Like in 8cd5477e54, we need to expand the
kernel slot again to be able to boot again.
2021-05-16 19:58:33 +02:00
Nicholas Baron
aa4d41fe2c
AK+Kernel+LibELF: Remove the need for IteratorDecision::Continue
By constraining two implementations, the compiler will select the best
fitting one. All this will require is duplicating the implementation and
simplifying for the `void` case.

This constraining also informs both the caller and compiler by passing
the callback parameter types as part of the constraint
(e.g.: `IterationFunction<int>`).

Some `for_each` functions in LibELF only take functions which return
`void`. This is a minimal correctness check, as it removes one way for a
function to incompletely do something.

There seems to be a possible idiom where inside a lambda, a `return;` is
the same as `continue;` in a for-loop.
2021-05-16 10:36:52 +01:00
Brian Gianforcaro
a324d4d6a3 Kernel: Make AnonymousVMObject physical page APIs OOM safe
AnonymousVMObject::create_with_physical_page(s) can't be NonnullRefPtr
as it allocates internally. Fixing the API then surfaced an issue in
ScatterGatherList, where the code was attempting to create an
AnonymousVMObject in the constructor which will not be observable
during OOM.

Fix all of these issues and start propagating errors at the callers
of the AnonymousVMObject and ScatterGatherList APis.
2021-05-15 09:01:32 +02:00
Brian Gianforcaro
d45db06826 Kernel: Make AnonymousVMObject::clone/create APIs OOM safe 2021-05-15 09:01:32 +02:00
Sahan Fernando
ed0e7b53a5 Kernel: Move VirtIO code away from using a scatter gather list
Currently, when passing buffers into VirtIOQueues, we use scatter-gather
lists, which contain an internal vector of buffers. This vector is
allocated, filled and the destroy whenever we try to provide buffers
into a virtqueue, which would happen a lot in performance cricital code
(the main transport mechanism for certain paravirtualized devices).

This commit moves it over to using VirtIOQueueChains and building the
chain in place in the VirtIOQueue. Also included are a bunch of fixups
for the VirtIO Console device, making it use an internal VM::RingBuffer
instead.
2021-05-13 10:00:42 +02:00
Sahan Fernando
13d5cdcd08 Kernel: Create VM::RingBuffer class 2021-05-13 10:00:42 +02:00
Sahan Fernando
8131c0de8c Kernel: Move AHCIPort::ScatterList to VM::ScatterGatherList
We want to move this out of the AHCI subsystem into the VM system,
since other parts of the kernel may need to perform scatter-gather IO.

We rename the current VM::ScatterGatherList impl that's used in the
virtio subsystem to VM::ScatterGatherRefList, since its distinguishing
feature from the AHCI scatter-gather list is that it doesn't own its
buffers.
2021-05-13 10:00:42 +02:00
Brian Gianforcaro
65138171f9 Kernel: Mark AsyncBlockDeviceRequest + AnonymousVMObject as final
Mark final to aid in de-virtualization since they are not currently
derived from.
2021-05-03 16:03:17 +02:00
Brian Gianforcaro
234c6ae32d Kernel: Change Inode::{read/write}_bytes interface to KResultOr<ssize_t>
The error handling in all these cases was still using the old style
negative values to indicate errors. We have a nicer solution for this
now with KResultOr<T>. This change switches the interface and then all
implementers to use the new style.
2021-05-02 13:27:37 +02:00
Gunnar Beutner
6cf59b6ae9 Everywhere: Turn #if *_DEBUG into dbgln_if/if constexpr 2021-05-01 21:25:06 +02:00
Linus Groh
649d2faeab Everywhere: Use "the SerenityOS developers." in copyright headers
We had some inconsistencies before:

- Sometimes "The", sometimes "the"
- Sometimes trailing ".", sometimes no trailing "."

I picked the most common one (lowecase "the", trailing ".") and applied
it to all copyright headers.

By using the exact same string everywhere we can ensure nothing gets
missed during a global search (and replace), and that these
inconsistencies are not spread any further (as copyright headers are
commonly copied to new files).
2021-04-29 00:59:26 +02:00
Brian Gianforcaro
8d6e9fad40 Kernel: Remove the now defunct LOCKER(..) macro. 2021-04-25 09:38:27 +02:00
Andreas Kling
b91c49364d AK: Rename adopt() to adopt_ref()
This makes it more symmetrical with adopt_own() (which is used to
create a NonnullOwnPtr from the result of a naked new.)
2021-04-23 16:46:57 +02:00
Brian Gianforcaro
1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Andreas Kling
c6b7b98b64 Kernel: Don't consider kernel memory regions for syscall origin check
We should never enter the syscall handler from a kernel address.
2021-04-20 23:38:27 +02:00
Idan Horowitz
acdd1424bc Kernel: Implement a simple Scatter/Gather List
This allows converting a single virtual buffer into its non-physically
contiguous parts, this is especially useful for DMA-based devices that
support scatter/gather-like functionality, as it eliminates the need
to clone outgoing buffers into one physically contiguous buffer.
2021-04-17 10:21:23 +02:00
Idan Horowitz
2c93123daf Kernel: Replace process' regions vector with a Red Black tree
This should provide some speed up, as currently searches for regions
containing a given address were performed in O(n) complexity, while
this container allows us to do those in O(logn).
2021-04-12 18:03:44 +02:00
Idan Horowitz
497c759ab7 Kernel: Remove old region from process' regions vector before splitting
This does not affect functionality right now, but it means that the
regions vector will now never have any overlapping regions, which will
allow the use of balance binary search trees instead of a vector in the
future. (since they require keys to be exclusive)
2021-04-12 18:03:44 +02:00
Andreas Kling
54f6b52f71 Kernel: Don't dump regions twice when crashing due to bad access
For whatever reason we were dumping regions when first handling the
page fault, and then again when tearing down the process.
2021-03-30 11:50:49 +02:00
Hendiadyoin1
0d934fc991 Kernel::CPU: Move headers into common directory
Alot of code is shared between i386/i686/x86 and x86_64
and a lot probably will be used for compatability modes.
So we start by moving the headers into one Directory.
We will probalby be able to move some cpp files aswell.
2021-03-21 09:35:23 +01:00
Hendiadyoin1
61f0aa6e75 Kernel: Implement helper to find multiple Regions in a Range 2021-03-13 10:00:46 +01:00
Hendiadyoin1
7874b89426 Kernel: Add a Range::intersect(other) helper 2021-03-13 10:00:46 +01:00
Andreas Kling
ef1e5db1d0 Everywhere: Remove klog(), dbg() and purge all LogStream usage :^)
Good-bye LogStream. Long live AK::Format!
2021-03-12 17:29:37 +01:00
Andreas Kling
3985468e83 Kernel: Convert klog() => AK::Format in PurgeablePageRanges 2021-03-12 11:38:43 +01:00
Andreas Kling
96fb3d4a11 Kernel: Add MemoryManager::set_page_writable_direct()
This helper function goes directly to the page tables and makes a
virtual address writable or non-writable.
2021-03-11 14:21:49 +01:00
Andreas Kling
b007bc07b7 Kernel: Convert klog() => dmesgln() in MemoryManager 2021-03-09 22:44:04 +01:00
Andreas Kling
b12734cf13 Kernel: Convert klog() => dmesgln() in PageDirectory 2021-03-09 22:10:41 +01:00
Andreas Kling
aef6474ea7 Kernel: Convert klog() to dmesgln() in Region 2021-03-09 22:10:41 +01:00
Andreas Kling
07564577c0 Kernel: Convert klog() => dmesgln() in AnonymousVMObject 2021-03-09 22:10:41 +01:00
Brian Gianforcaro
eaef57443c Kernel: Make MemoryManager API type-safe for Region::Access enum
Increase type-safety moving the MemoryManager APIs which take a
Region::Access to actually use that type instead of a `u8`.

Eventually the actually m_access can be moved there as well, but
I hit some weird bug where it wasn't using the correct operators
in `set_access_bit(..)` even though it's declared (and tested).
Something to fix-up later.
2021-03-08 18:47:40 +01:00
Liav A
ecb169b61e Kernel: Add AnonymousVMObject constructor for a Vector of physical pages
This will be used later on by the AHCI code to create a Region
that spans over scattered DMA pages.
2021-03-05 11:29:34 +01:00
Andreas Kling
adb2e6be5f Kernel: Make the kernel compile & link for x86_64
It's now possible to build the whole kernel with an x86_64 toolchain.
There's no bootstrap code so it doesn't work yet (obviously.)
2021-03-04 18:25:01 +01:00
Andreas Kling
2871df6f0d Kernel: Stop trying to keep InodeVMObject in sync with disk changes
As it turns out, Dr. POSIX doesn't require that post-mmap() changes
to a file are reflected in the memory mappings. So we don't actually
have to care about the file size changing (or the contents.)

IIUC, as long as all the MAP_SHARED mappings that refer to the same
inode are in sync, we're good.

This means that VMObjects don't need resizing capabilities. I'm sure
there are ways we can take advantage of this fact.
2021-03-04 15:42:51 +01:00
Andreas Kling
1208fc7d37 AK: Simplify Bitmap and implement in terms of BitmapView
Add Bitmap::view() and forward most of the calls to BitmapView since
the code was identical.

Bitmap is now primarily concerned with its dynamically allocated
backing store and BitmapView deals with the rest.
2021-03-04 11:25:45 +01:00
Andreas Kling
4515652001 Kernel: Remove 1 level of indirection for AnonymousVMObject CoW bitmaps
Instead of keeping AnonymousVMObject::m_cow_map in an OwnPtr<Bitmap>,
just make the Bitmap a regular value member. This increases the size
of the VMObject by 8 bytes, but removes some of the kmalloc/kfree spam
incurred by sys$fork().
2021-03-04 10:11:37 +01:00
Andreas Kling
a819eb5016 Kernel: Skip TLB flushes while cloning regions in sys$fork()
Since we know for sure that the virtual memory regions in the new
process being created are not being used on any CPU, there's no need
to do TLB flushes for every mapped page.
2021-03-03 22:57:45 +01:00
Ben Wiederhake
860a3bbce3 Kernel: Use default con/de-structors
This may seem like a no-op change, however it shrinks down the Kernel by a bit:
.text -432
.unmap_after_init -60
.data -480
.debug_info -673
.debug_aranges 8
.debug_ranges -232
.debug_line -558
.debug_str -308
.debug_frame -40

With '= default', the compiler can do more inlining, hence the savings.
I intentionally omitted some opportunities for '= default', because they
would increase the Kernel size.
2021-02-28 18:09:12 +01:00
Andreas Kling
19fc62f445 Kernel: Use get_fast_random() for MAP_RANDOMIZED addresses
Let's not block sys$mmap() on kernel randomness.
2021-02-27 16:56:50 +01:00
Andreas Kling
8129f3da52 Kernel: Move SMAP disabler RAII helper to its own file
Added this in a new directory called Kernel/Arch/x86/ where stuff
that applies to both i386 and x86_64 can live.
2021-02-25 17:25:34 +01:00
Andreas Kling
17f076d912 Kernel: Move the VM Range class to its own files 2021-02-25 16:21:14 +01:00
Andreas Kling
8cd5477e54 Kernel: Expand the kernel memory slot from 8 MiB to 16 MiB
We were only 448 KiB away from filling up the old slot size we reserve
for the kernel above the 3 GiB mark. This expands the slot to 16 MiB,
which allows us to continue booting the kernel until somebody takes
the time to improve our loader.
2021-02-23 21:50:18 +01:00
Andreas Kling
5d180d1f99 Everywhere: Rename ASSERT => VERIFY
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED)

Since all of these checks are done in release builds as well,
let's rename them to VERIFY to prevent confusion, as everyone is
used to assertions being compiled out in release.

We can introduce a new ASSERT macro that is specifically for debug
checks, but I'm doing this wholesale conversion first since we've
accumulated thousands of these already, and it's not immediately
obvious which ones are suitable for ASSERT.
2021-02-23 20:56:54 +01:00
Andreas Kling
fdf03852c9 Kernel: Slap UNMAP_AFTER_INIT on a whole bunch of functions
There's no real system here, I just added it to various functions
that I don't believe we ever want to call after initialization
has finished.

With these changes, we're able to unmap 60 KiB of kernel text
after init. :^)
2021-02-19 20:23:05 +01:00
Andreas Kling
6136faa4eb Kernel: Add .unmap_after_init section for code we don't need after init
You can now declare functions with UNMAP_AFTER_INIT and they'll get
segregated into a separate kernel section that gets completely
unmapped at the end of initialization.

This can be used for anything we don't need to call once we've booted
into userspace.

There are two nice things about this mechanism:

- It allows us to free up entire pages of memory for other use.
  (Note that this patch does not actually make use of the freed
  pages yet, but in the future we totally could!)

- It allows us to get rid of obviously dangerous gadgets like
  write-to-CR0 and write-to-CR4 which are very useful for an attacker
  trying to disable SMAP/SMEP/etc.

I've also made sure to include a helpful panic message in case you
hit a kernel crash because of this protection. :^)
2021-02-19 20:23:05 +01:00
Brian Gianforcaro
7482cb6531 Kernel: Avoid some un-necessary copies coming from range based for loops
- The irq_controller was getting add_ref/released needlessly during enumeration.

- Used ranges were also getting needlessly copied.
2021-02-15 15:25:23 +01:00
Andreas Kling
d8013c60bb Kernel: Add mechanism to make some memory read-only after init finishes
You can now use the READONLY_AFTER_INIT macro when declaring a variable
and we will put it in a special ".ro_after_init" section in the kernel.

Data in that section remains writable during the boot and init process,
and is then marked read-only just before launching the SystemServer.

This is based on an idea from the Linux kernel. :^)
2021-02-14 18:11:32 +01:00
Andreas Kling
09b1b09c19 Kernel: Assert if rounding-up-to-page-size would wrap around to 0
If we try to align a number above 0xfffff000 to the next multiple of
the page size (4 KiB), it would wrap around to 0. This is most likely
never what we want, so let's assert if that happens.
2021-02-14 10:01:50 +01:00
Andreas Kling
198d641808 Kernel: Panic on attempt to map mmap'ed page at a kernel address
If we somehow get tricked into mapping user-controlled mmap memory
at a kernel address, let's just panic the kernel.
2021-02-14 09:36:58 +01:00
Andreas Kling
4021264201 Kernel: Make the Region constructor private
We can use adopt_own(*new T) instead of make<T>().
2021-02-14 01:39:04 +01:00
Andreas Kling
8415866c03 Kernel: Remove user/kernel flags from Region
Now that we no longer need to support the signal trampolines being
user-accessible inside the kernel memory range, we can get rid of the
"kernel" and "user-accessible" flags on Region and simply use the
address of the region to determine whether it's kernel or user.

This also tightens the page table mapping code, since it can now set
user-accessibility based solely on the virtual address of a page.
2021-02-14 01:34:23 +01:00
Andreas Kling
a5def4e98c Kernel: Sanity check the VM range when constructing a Region
This should help us catch bogus VM ranges ending up in a process's
address space sooner.
2021-02-13 01:18:03 +01:00
Andreas Kling
7551090056 Kernel: Round up ranges to page size multiples in munmap and mprotect
This prevents passing bad inputs to RangeAllocator who then asserts.

Found by fuzz-syscalls. :^)
2021-02-13 01:18:03 +01:00
Andreas Kling
e050577f0a Kernel: Make MAP_RANDOMIZED honor alignment requests
Previously, we only cared about the alignment on the fallback path.
2021-02-12 19:15:59 +01:00
Andreas Kling
4e2802bf91 Kernel: Move region dumps from dmesg to debug log
Also fix a broken format string caught by the new format string checks.
2021-02-12 16:33:58 +01:00
Andreas Kling
c4db224c94 Kernel: Convert klog() => dmesgln() / dbgln() in MemoryManager 2021-02-12 16:24:40 +01:00
Andreas Kling
5af69d6e93 Kernel: Convert klog() to dmesgln() in RangeAllocator 2021-02-12 16:24:40 +01:00
Andreas Kling
54986228bf Kernel: Oops, add missing #include to fix ENABLE_ALL_THE_DEBUG_MACROS 2021-02-11 22:15:55 +01:00
Andreas Kling
0dbb22e9e0 Kernel: Remove a handful of unused things in VM/ directory
Also add some missing initializers.
2021-02-11 22:02:39 +01:00
Andreas Kling
4cd2c475a8 Kernel: Make the space lock a RecursiveSpinLock 2021-02-08 22:28:48 +01:00
Andreas Kling
9ca42c4c0e Kernel: Always hold space lock while calculating memory statistics
And put the locker at the top of the functions for clarity.
2021-02-08 22:23:29 +01:00
Andreas Kling
8bda30edd2 Kernel: Move memory statistics helpers from Process to Space 2021-02-08 22:23:29 +01:00
Andreas Kling
f1b5def8fd Kernel: Factor address space management out of the Process class
This patch adds Space, a class representing a process's address space.

- Each Process has a Space.
- The Space owns the PageDirectory and all Regions in the Process.

This allows us to reorganize sys$execve() so that it constructs and
populates a new Space fully before committing to it.

Previously, we would construct the new address space while still
running in the old one, and encountering an error meant we had to do
tedious and error-prone rollback.

Those problems are now gone, replaced by what's hopefully a set of much
smaller problems and missing cleanups. :^)
2021-02-08 18:27:28 +01:00
Andreas Kling
b2cba3036e Kernel: Remove unused MemoryManager::validate_range()
This is no longer used since we've switched to using the MMU to
generate EFAULT errors.
2021-02-08 18:27:28 +01:00
AnotherTest
09a43969ba Everywhere: Replace dbgln<flag>(...) with dbgln_if(flag, ...)
Replacement made by `find Kernel Userland -name '*.h' -o -name '*.cpp' | sed -i -Ee 's/dbgln\b<(\w+)>\(/dbgln_if(\1, /g'`
2021-02-08 18:08:55 +01:00
Andreas Kling
9c77980965 Everywhere: Remove some bitrotted "#if 0" blocks 2021-02-03 11:17:47 +01:00
Andreas Kling
823186031d Kernel: Add a way to specify which memory regions can make syscalls
This patch adds sys$msyscall() which is loosely based on an OpenBSD
mechanism for preventing syscalls from non-blessed memory regions.

It works similarly to pledge and unveil, you can call it as many
times as you like, and when you're finished, you call it with a null
pointer and it will stop accepting new regions from then on.

If a syscall later happens and doesn't originate from one of the
previously blessed regions, the kernel will simply crash the process.
2021-02-02 20:13:44 +01:00
Liav A
5ab1864497 Kernel: Introduce the MemoryDevice
This is a character device that is being used by the dmidecode utility.
We only allow to map the BIOS ROM area to userspace with this device.
2021-02-01 17:13:23 +01:00
Andreas Kling
1320b9351e Revert "Kernel: Don't clone kernel mappings for bottom 2 MiB VM into processes"
This reverts commit da7b21dc06.

This broke SMP boot, oops! :^)
2021-01-31 19:00:53 +01:00
Andreas Kling
da7b21dc06 Kernel: Don't clone kernel mappings for bottom 2 MiB VM into processes
I can't think of anything that needs these mappings anymore, so let's
get rid of them.
2021-01-31 15:20:18 +01:00
Andreas Kling
e55ef70e5e Kernel: Remove "has made executable exception for dynamic loader" flag
As Idan pointed out, this flag is actually not needed, since we don't
allow transitioning from previously-executable to writable anyway.
2021-01-30 10:06:52 +01:00
Jorropo
df30b3e54c
Kernel: RangeAllocator randomized correctly check if size is in bound. (#5164)
The random address proposals were not checked with the size so it was
increasely likely to try to allocate outside of available space with
larger and larger sizes.

Now they will be ignored instead of triggering a Kernel assertion
failure.

This is a continuation of: c8e7baf4b8
2021-01-29 17:18:23 +01:00
Andreas Kling
af3d3c5c4a Kernel: Enforce W^X more strictly (like PaX MPROTECT)
This patch adds enforcement of two new rules:

- Memory that was previously writable cannot become executable
- Memory that was previously executable cannot become writable

Unfortunately we have to make an exception for text relocations in the
dynamic loader. Since those necessitate writing into a private copy
of library code, we allow programs to transition from RW to RX under
very specific conditions. See the implementation of sys$mprotect()'s
should_make_executable_exception_for_dynamic_loader() for details.
2021-01-29 14:52:27 +01:00
Andreas Kling
c8e7baf4b8 Kernel: Check for alignment size overflow when allocating VM ranges
Also add some sanity check assertions that we're generating and
returning ranges contained within the RangeAllocator's total range.

Fixes #5162.
2021-01-29 12:11:42 +01:00
Tom
affb4ef01b Kernel: Allow specifying a physical alignment when allocating
Some drivers may require allocating contiguous physical pages with
a specific alignment for the physical address.
2021-01-28 18:52:59 +01:00
Andreas Kling
80837d43a2 Kernel: Remove outdated debug logging from RangeAllocator
If someone wants to debug this code, it's better that they rewrite the
logging code to take randomization and guard pages into account.
2021-01-28 16:23:38 +01:00
Andreas Kling
b6937e2560 Kernel+LibC: Add MAP_RANDOMIZED flag for sys$mmap()
This can be used to request random VM placement instead of the highly
predictable regular mmap(nullptr, ...) VM allocation strategy.

It will soon be used to implement ASLR in the dynamic loader. :^)
2021-01-28 16:23:38 +01:00
Andreas Kling
d3de138d64 Kernel: Add sanity check assertion in RangeAllocator::allocate_specific
The specific virtual address should always be page aligned.
2021-01-28 16:23:38 +01:00
Andreas Kling
27d07796b4 Kernel: Add sanity check assertion in RangeAllocator::allocate_anywhere
The requested alignment should always be a multiple of the page size.
2021-01-28 16:23:38 +01:00
Tom
250a310454 Kernel: Release MM lock while yielding from inode page fault handler
We need to make sure other processors can grab the MM lock while we
wait, so release it when we might block. Reading the page from
disk may also block, so release it during that time as well.
2021-01-27 22:48:41 +01:00
Andreas Kling
e67402c702 Kernel: Remove Range "valid" state and use Optional<Range> instead
It's easier to understand VM ranges if they are always valid. We can
simply use an empty Optional<Range> to encode absence when needed.
2021-01-27 21:14:42 +01:00
Tom
e2f9e557d3 Kernel: Make Processor::id a static function
This eliminates the window between calling Processor::current and
the member function where a thread could be moved to another
processor. This is generally not as big of a concern as with
Processor::current_thread, but also slightly more light weight.
2021-01-27 21:12:24 +01:00
Andreas Kling
76a69be217 Kernel: Assert in RangeAllocator that sizes are multiple of PAGE_SIZE 2021-01-27 19:45:53 +01:00
asynts
7cf0c7cc0d Meta: Split debug defines into multiple headers.
The following script was used to make these changes:

    #!/bin/bash
    set -e

    tmp=$(mktemp -d)

    echo "tmp=$tmp"

    find Kernel \( -name '*.cpp' -o -name '*.h' \) | sort > $tmp/Kernel.files
    find . \( -path ./Toolchain -prune -o -path ./Build -prune -o -path ./Kernel -prune \) -o \( -name '*.cpp' -o -name '*.h' \) -print | sort > $tmp/EverythingExceptKernel.files

    cat $tmp/Kernel.files | xargs grep -Eho '[A-Z0-9_]+_DEBUG' | sort | uniq > $tmp/Kernel.macros
    cat $tmp/EverythingExceptKernel.files | xargs grep -Eho '[A-Z0-9_]+_DEBUG' | sort | uniq > $tmp/EverythingExceptKernel.macros

    comm -23 $tmp/Kernel.macros $tmp/EverythingExceptKernel.macros > $tmp/Kernel.unique
    comm -1 $tmp/Kernel.macros $tmp/EverythingExceptKernel.macros > $tmp/EverythingExceptKernel.unique

    cat $tmp/Kernel.unique | awk '{ print "#cmakedefine01 "$1 }' > $tmp/Kernel.header
    cat $tmp/EverythingExceptKernel.unique | awk '{ print "#cmakedefine01 "$1 }' > $tmp/EverythingExceptKernel.header

    for macro in $(cat $tmp/Kernel.unique)
    do
        cat $tmp/Kernel.files | xargs grep -l $macro >> $tmp/Kernel.new-includes ||:
    done
    cat $tmp/Kernel.new-includes | sort > $tmp/Kernel.new-includes.sorted

    for macro in $(cat $tmp/EverythingExceptKernel.unique)
    do
        cat $tmp/Kernel.files | xargs grep -l $macro >> $tmp/Kernel.old-includes ||:
    done
    cat $tmp/Kernel.old-includes | sort > $tmp/Kernel.old-includes.sorted

    comm -23 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.new
    comm -13 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.old
    comm -12 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.mixed

    for file in $(cat $tmp/Kernel.includes.new)
    do
        sed -i -E 's/#include <AK\/Debug\.h>/#include <Kernel\/Debug\.h>/' $file
    done

    for file in $(cat $tmp/Kernel.includes.mixed)
    do
        echo "mixed include in $file, requires manual editing."
    done
2021-01-26 21:20:00 +01:00
Andreas Kling
3ff88a1d77 Kernel: Assert on attempt to map private region backed by shared inode
If we find ourselves with a user-accessible, non-shared Region backed by
a SharedInodeVMObject, that's pretty bad news, so let's just panic the
kernel instead of getting abused.

There might be a better place for this kind of check, so I've added a
FIXME about putting more thought into that.
2021-01-26 18:35:10 +01:00
Andreas Kling
a131927c75 Kernel: sys$munmap() region splitting did not preserve "shared" flag
This was exploitable since the shared flag determines whether inode
permission checks are applied in sys$mprotect().

The bug was pretty hard to spot due to default arguments being used
instead. This patch removes the default arguments to make explicit
at each call site what's being done.
2021-01-26 18:35:04 +01:00
asynts
eea72b9b5c Everywhere: Hook up remaining debug macros to Debug.h. 2021-01-25 09:47:36 +01:00
asynts
8465683dcf Everywhere: Debug macros instead of constexpr.
This was done with the following script:

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec sed -i -E 's/dbgln<debug_([a-z_]+)>/dbgln<\U\1_DEBUG>/' {} \;

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec sed -i -E 's/if constexpr \(debug_([a-z0-9_]+)/if constexpr \(\U\1_DEBUG/' {} \;
2021-01-25 09:47:36 +01:00
asynts
acdcf59a33 Everywhere: Remove unnecessary debug comments.
It would be tempting to uncomment these statements, but that won't work
with the new changes.

This was done with the following commands:

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/#define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/#define/ { toggle = 1 }' {} \;

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/ #define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/ #define/ { toggle = 1 }' {} \;
2021-01-25 09:47:36 +01:00
asynts
1a3a0836c0 Everywhere: Use CMake to generate AK/Debug.h.
This was done with the help of several scripts, I dump them here to
easily find them later:

    awk '/#ifdef/ { print "#cmakedefine01 "$2 }' AK/Debug.h.in

    for debug_macro in $(awk '/#ifdef/ { print $2 }' AK/Debug.h.in)
    do
        find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec sed -i -E 's/#ifdef '$debug_macro'/#if '$debug_macro'/' {} \;
    done

    # Remember to remove WRAPPER_GERNERATOR_DEBUG from the list.
    awk '/#cmake/ { print "set("$2" ON)" }' AK/Debug.h.in
2021-01-25 09:47:36 +01:00
Jean-Baptiste Boric
ec056f3bd1 Kernel: Parse boot modules from Multiboot specification 2021-01-22 22:17:39 +01:00
Jean-Baptiste Boric
3cbe805486 Kernel: Move kmalloc heaps and super pages inside .bss segment
The kernel ignored the first 8 MiB of RAM while parsing the memory map
because the kmalloc heaps and the super physical pages lived here. Move
all that stuff inside the .bss segment so that those memory regions are
accounted for, otherwise we risk overwriting boot modules placed next
to the kernel.
2021-01-22 22:17:39 +01:00
Jean-Baptiste Boric
5cd1217b6e Kernel: Remove trace log in MemoryManager::deallocate_user_physical_page() 2021-01-22 22:17:39 +01:00
asynts
27bc48e06c Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
Linus Groh
421587c15c Everywhere: Fix typos 2021-01-22 18:41:29 +01:00
Andreas Kling
cfe54f86bd Kernel: Remove unused /proc/mm file
This was a file I used very early on to dump information about kernel
VM objects. It's long since superseded by other JSON-based files.
2021-01-17 21:14:20 +01:00
Tom
1d621ab172 Kernel: Some futex improvements
This adds support for FUTEX_WAKE_OP, FUTEX_WAIT_BITSET, FUTEX_WAKE_BITSET,
FUTEX_REQUEUE, and FUTEX_CMP_REQUEUE, as well well as global and private
futex and absolute/relative timeouts against the appropriate clock. This
also changes the implementation so that kernel resources are only used when
a thread is blocked on a futex.

Global futexes are implemented as offsets in VMObjects, so that different
processes can share a futex against the same VMObject despite potentially
being mapped at different virtual addresses.
2021-01-17 20:30:31 +01:00
Andreas Kling
43109f9614 Kernel: Remove unused syscall sys$minherit()
This is no longer used. We can bring it back the day we need it.
2021-01-16 14:52:04 +01:00
Lenny Maiorani
e6f907a155 AK: Simplify constructors and conversions from nullptr_t
Problem:
- Many constructors are defined as `{}` rather than using the ` =
  default` compiler-provided constructor.
- Some types provide an implicit conversion operator from `nullptr_t`
  instead of requiring the caller to default construct. This violates
  the C++ Core Guidelines suggestion to declare single-argument
  constructors explicit
  (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit).

Solution:
- Change default constructors to use the compiler-provided default
  constructor.
- Remove implicit conversion operators from `nullptr_t` and change
  usage to enforce type consistency without conversion.
2021-01-12 09:11:45 +01:00
Andreas Kling
f7435dd95f Kernel: Remove MM_DEBUG debug spam code
This was too spammy to ever actually be used anyway.
2021-01-11 22:09:40 +01:00
Andreas Kling
7c4ddecacb Kernel: Convert a bunch of String::format() => String::formatted() 2021-01-11 22:07:01 +01:00
Sahan Fernando
9bf76a85c8 Everywhere: Fix incorrect uses of String::format and StringBuilder::appendf
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-11 21:06:32 +01:00
Sahan Fernando
099b83fd28 Everywhere: Fix incorrect uses of String::format and StringBuilder::appendf
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-11 21:06:32 +01:00
asynts
723effd051 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.Everything:
2021-01-11 11:55:47 +01:00
asynts
938e5c7719 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.Everything:

The modifications in this commit were automatically made using the
following command:

    find . -name '*.cpp' -exec sed -i -E 's/dbg\(\) << ("[^"{]*");/dbgln\(\1\);/' {} \;
2021-01-09 21:11:09 +01:00
Tom
d3e6cdf21f Kernel: Provide consistent memory stats in ProcFS
We should take the MM lock when gathering all the statistics that
we need so that the values are consistent.
2021-01-05 10:59:00 +01:00
Tom
901ef3f1c8 Kernel: Specify default memory order for some non-synchronizing Atomics 2021-01-04 19:13:52 +01:00
Tom
0d44ee6f2b Kernel: Ignore TLB flush requests for user addresses of other processes
If a TLB flush request is broadcast to other processors and the addresses
to flush are user mode addresses, we can ignore such a request on the
target processor if the page directory currently in use doesn't match
the addresses to be flushed. We still need to broadcast to all processors
in that case because the other processors may switch to that same page
directory at any time.
2021-01-02 20:56:35 +01:00
Tom
c630669304 Kernel: If a VMObject is shared, broadcast page remappings
If we remap pages (e.g. lazy allocation) inside a VMObject that is
shared among more than one region, broadcast it to any other region
that may be mapping the same page.
2021-01-02 20:56:35 +01:00