Commit Graph

181 Commits

Author SHA1 Message Date
Sönke Holz
d0e77ec377 AK+Kernel: Don't allow Vector::ensure_capacity() in the kernel
This function doesn't propagate allocation failures.

Replace all current usages of this function by using
`try_ensure_capacity()` instead.
2025-11-22 19:51:20 +01:00
Sönke Holz
b089e9d0f6 AK+Kernel: Don't allow Vector::grow_capacity() in the kernel
This function doesn't propagate allocation failures.

Replace all current usages of this function by using
`try_grow_capacity()` instead.
2025-11-22 19:51:20 +01:00
Sönke Holz
e8d9734a9c AK+Kernel+Userland: Don't allow Vector::append() in the kernel
This makes it not as easy to forgot to handle OOMs in the kernel.

This commit replaces most usages of this function with
`try_append(...).release_value_but_fixme_should_propagate_errors()`.
But in some cases, using the `TRY` macro or `unchecked_append()` is
already possible.

In places where allocations should not fail or an OOM would be fatal
anyways, `MUST(try_append(...))` should be used explicitly.
2025-11-17 11:08:45 +01:00
Sönke Holz
92954aa67c Kernel/MM: Ensure zeroing of DMA pages is visible to devices 2025-10-19 11:09:19 +02:00
implicitfield
2887302286 Kernel: Avoid deadlocking while initializing KASAN shadow memory
Performing allocations while the global data is locked will lead to
multiple complex deadlocks. To get around this, we can just forego
locking entirely here, since KASAN shadow memory is initialized
early enough that APs haven't booted yet and the scheduler isn't
running.
2025-09-06 13:16:35 -04:00
Sönke Holz
5f25b30ccf Kernel/MM: Remove "Virtual" from "ContiguousPhysicalVirtualRange"
This was accidentally renamed in cd5faf4e42 but wasn't noticed in the
partial revert in f770b9d430.

This struct isn't needed in `parse_memory_map_multiboot`, so remove it
from that function.
2025-08-02 20:01:07 -04:00
Sönke Holz
c88d7e12ea Kernel/MM: Rename UserMemoryRangeTypeNames variable
Those range names are for use*d* ranges, not for use*r* ranges.
Since this is a variable, convert it to snake_case.
2025-08-02 20:01:07 -04:00
Sönke Holz
b8c52999ef Kernel/MM: Replace "{User physical => Physical} region" in dmesg message
Since 1c499e75bd there are no supervisor pages anymore.
2025-08-02 20:01:07 -04:00
Sönke Holz
4cd9c76427 Kernel/MM: Remove "Initialize MMU" message
This message was added in 95c469ca4c so the startup debug messages were
the same as on AArch64.

However, this message doesn't really make sense, as the MMU is already
initialized. It also doesn't match the style of most other startup
messages.

I don't think this message is necessary, so simply remove it.
2025-08-02 20:01:07 -04:00
Sönke Holz
2202ad0bc5 Kernel/MM: Add MemoryType argument to allocate_physical_page()
This makes it possible to specify the MemoryType that should be used
when zeroing the allocated page.
2025-08-01 17:55:05 +02:00
Sönke Holz
81194028ca Kernel/MM: Add MemoryType argument to quickmap_page()
This makes it possible to request MemoryTypes other than Normal.
2025-08-01 17:55:05 +02:00
Sönke Holz
22d65564c4 Kernel/MM: Correct 2 dbgln() format strings
`:p` doesn't work for VirtualAddress.
This caused us to reach a VERIFY_NOT_REACHED if a syscall was made from
writable memory.
2025-06-21 12:57:20 +02:00
implicitfield
d10fcfb727 Kernel/MemoryManager: Prefer non-recursive Spinlocks 2025-06-05 22:02:40 +02:00
Hendiadyoin1
421a697982 Everywhere: Remove a bunch of unused variables
The compiler starts to recognize these as soon as Optional is constexpr.
2025-04-06 19:21:49 -04:00
Sönke Holz
ff7293f06e Kernel: Find the SMBIOS Entry Point in the EFI configuration table
The BIOS region currently used to find the SMBIOS Entry Point might not
be present on (U)EFI systems.
2025-03-07 01:41:50 +01:00
Vladimir Serbinenko
bc06c9efa4 Kernel/Memory: Ignore reserved range at 0x000003ff f000 0000
On Meteor Lake this range covers the GFX address space reservations,
at a very high address. Nothing actually ends up using it, so we can
ignore it. Moreover if we don't we end up with highest_address at the
4 TiB mark which would need an enormous page table to map this
huge (0-4TiB) range. Until we support mapping I/O space piecewise we
need to skip this range.
2025-02-26 17:31:05 +01:00
Sönke Holz
65cdde40ab Kernel/MM: Unmap the EFIPrekernel paging bootstrap page
The EFIPrekernel will identity map one page to set up virtual memory,
instead of identity mapping its entire memory like the current
multiboot prekernel.
2025-01-28 13:01:40 +01:00
Sönke Holz
690069bb75 Kernel: Use correct kernel range when booted via EFI on non-x86 2025-01-28 13:01:40 +01:00
Sönke Holz
e2954dbb88 Kernel/MM: Add EFI memory map parser 2025-01-28 13:01:40 +01:00
Sönke Holz
6fa8165d91 Kernel/MM: Use same memory type for zeroing pages as the actual mapping
On some architectures like ARM, using mismatched memory types for
different aliases of the same physical memory can lead to unexpected
behavior.
(https://developer.arm.com/documentation/102376/0200/Memory-aliasing-and-mismatched-memory-types)

Flushing the data cache and using appropriate memory barriers might be
enough, but should have worse performance.
2025-01-13 19:30:54 +01:00
Sönke Holz
1d3a255ea1 Kernel: Recognize a stack pointer pointing to the top of stack as valid
This also removes the explicit decrementing of the stack pointer in
`sys$create_thread` before passing it to `validate_user_stack`,
as it's unnecessary now.
2024-12-18 16:09:52 -05:00
Sönke Holz
10c217070f Kernel/MM: Allow setting the memory type for DMA regions 2024-12-12 16:07:25 +01:00
Sönke Holz
fd395e5705 Kernel/MM: Only return true in is_initialized if completely initialized 2024-11-23 19:29:50 +01:00
Sönke Holz
d3a0ae5c57 Kernel/MM: Replace Region::Cacheable with a more generic MemoryType enum
This replaces all usages of Cacheable::Yes with MemoryType::Normal and
Cacheable::No with either MemoryType::NonCacheable or MemoryType::IO,
depending on the context.

The Page{Directory,Table}::set_cache_disabled function therefore also
has been replaced with a more appropriate set_memory_type_function.
Adding a memory_type "getter" would not be as easy, as some
architectures may not support all memory types, so getting the memory
type again may be a lossy conversion. The is_cache_disabled function
was never used, so just simply remove it altogether.

There is no difference between MemoryType::NonCacheable and
MemoryType::IO on x86 for now.

Other architectures currently don't respect the MemoryType at all.
2024-11-23 19:29:50 +01:00
Sönke Holz
4f8490b5ff Kernel: Move boot info variables into a shared struct
This commit reorganizes the BootInfo struct definition so it can be
shared for all architectures.

The existing free extern "C" boot info variables have been removed and
replaced with a global BootInfo struct, 'g_boot_info'.

On x86-64, the BootInfo is directly copied from the Prekernel-provided
struct.
On AArch64 and RISC-V, BootInfo is populated during pre_init.
2024-10-30 18:51:35 -04:00
Hendiadyoin1
f74f80e13b Kernel/aarch64: Use the FDT to get the device/boot info
This removes the old hacky multiboot memory map and instead uses the
FDT to get the actual memory map.
2024-08-20 21:52:12 -04:00
Hendiadyoin1
e0a177061e Kernel/Firmware+riscv64: Move devicetree handling to Firmware directory
This also adds a `verify_fdt` method which will be used in later commits
2024-08-20 21:52:12 -04:00
Sönke Holz
194d9df34f Kernel/Memory: Handle devicetree memory nodes with more than one region 2024-08-20 21:52:12 -04:00
Sönke Holz
ab44530304 Kernel/Memory: Remove x86 LAPIC address hack
MM is now able to handle MMIO after our "highest_phyisical_address".
2024-08-20 21:52:12 -04:00
Liav A.
dd59fe35c7 Kernel+Userland: Reduce jails to be a simple boolean flag
The whole concept of Jails was far more complicated than I actually want
it to be, so let's reduce the complexity of how it works from now on.
Please note that we always leaked the attach count of a Jail object in
the fork syscall if it failed midway.
Instead, we should have attach to the jail just before registering the
new Process, so we don't need to worry about unsuccessful Process
creation.

The reduction of complexity in regard to jails means that instead of
relying on jails to provide PID isolation, we could simplify the whole
idea of them to be a simple SetOnce, and let the ProcessList (now called
ScopedProcessList) to be responsible for this type of isolation.

Therefore, we apply the following changes to do so:
- We make the Jail concept no longer a class of its own. Instead, we
  simplify the idea of being jailed to a simple ProtectedValues boolean
  flag. This means that we no longer check of matching jail pointers
  anywhere in the Kernel code.
  To set a process as jailed, a new prctl option was added to set a
  Kernel SetOnce boolean flag (so it cannot change ever again).
- We provide Process & Thread methods to iterate over process lists.
  A process can either iterate on the global process list, or if it's
  attached to a scoped process list, then only over that list.
  This essentially replaces the need of checking the Jail pointer of a
  process when iterating over process lists.
2024-07-21 11:44:23 +02:00
Idan Horowitz
26cff62a0a Kernel: Rename Memory::PhysicalPage to Memory::PhysicalRAMPage
Since these are now only used to represent RAM pages, (and not MMIO
pages) rename them to make their purpose more obvious.
2024-05-17 15:38:28 -06:00
Idan Horowitz
827322c139 Kernel: Stop allocating physical pages for mapped MMIO regions
As MMIO is placed at fixed physical addressed, and does not need to be
backed by real RAM physical pages, there's no need to use PhysicalPage
instances to track their pages.
This results in slightly reduced allocations, but more importantly
makes MMIO addresses which end up after the normal RAM ranges work,
like 64-bit PCI BARs usually are.
2024-05-17 15:38:28 -06:00
Liav A
d068af89d5 Kernel/x86: Bake the Prekernel and the Kernel into one image
The new baked image is a Prekernel and a Kernel baked together now, so
essentially we no longer need to pass the Prekernel as -kernel and the
actual kernel image as -initrd to QEMU, leaving the option to pass an
actual initrd or initramfs module later on with multiboot.
2024-05-14 23:37:38 +02:00
Hendiadyoin1
8ea8b7a6e5 Kernel/MM: Parse /memreserve/ blocks in FDT based memory mapping mode
These seem to be actually used in the RPi FDTs
2024-05-02 07:44:13 -06:00
Hendiadyoin1
2b13769dd5 Kernel/MM: Skip non static reserved memory regions instead of crashing
Crashing seems a bit harsh, so let's just skip them instead, as they
actually show up in the device tree of RPis.
2024-05-02 07:44:13 -06:00
Hendiadyoin1
d3f6b03733 Kernel/riscv64: Take the memory map from the FDT and dump it
For this the BootInfo struct was made architecture specific
2024-02-24 16:43:44 -07:00
Hendiadyoin1
23d6c88027 Kernel/MM: Don't allocate a temporary Vector when parsing the memory map
Instead we can achieve the same by just using an optional.
2024-01-12 15:59:47 -07:00
Idan Horowitz
f7a1f28d7f Kernel: Add initial basic support for KASAN
This commit adds minimal support for compiler-instrumentation based
memory access sanitization.
Currently we only support detection of kmalloc redzone accesses, and
kmalloc use-after-free accesses.

Support for inline checks (for improved performance), and for stack
use-after-return and use-after-return detection is left for future PRs.
2023-12-30 13:57:10 +01:00
Sönke Holz
28a3089dc3 Kernel/riscv64: Return correct range in kernel_virtual_range on RISC-V
riscv64 doesn't use a prekernel, so use the same code as aarch64 for
determining the kernel virtual address range.
2023-12-29 16:45:08 +01:00
Vladimir Serbinenko
160609d80a Kernel/Memory: Map framebuffer and address space <4GiB
Address space under 4GiB is used for I/O but is absent
from memory maps on some systems.
2023-10-03 16:19:03 -06:00
Liav A
3fd4997fc2 Kernel: Don't allocate memory for names of processes and threads
Instead, use the FixedCharBuffer class to ensure we always use a static
buffer storage for these names. This ensures that if a Process or a
Thread were created, there's a guarantee that setting a new name will
never fail, as only copying of strings should be done to that static
storage.

The limits which are set are 32 characters for processes' names and 64
characters for thread names - this is because threads' names could be
more verbose than processes' names.
2023-08-09 21:06:54 -06:00
Liav A
336fb4f313 Kernel: Move InterruptDisabler to the Interrupts subdirectory 2023-06-04 21:32:34 +02:00
Liav A
8f21420a1d Kernel: Move all boot-related code to the new Boot subdirectory 2023-06-04 21:32:34 +02:00
Liav A
7c0540a229 Everywhere: Move global Kernel pattern code to Kernel/Library directory
This has KString, KBuffer, DoubleBuffer, KBufferBuilder, IOWindow,
UserOrKernelBuffer and ScopedCritical classes being moved to the
Kernel/Library subdirectory.

Also, move the panic and assertions handling code to that directory.
2023-06-04 21:32:34 +02:00
Liav A
1b04726c85 Kernel: Move all tasks-related code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
Ben Wiederhake
36ff6187f6 Everywhere: Change spelling of 'behaviour' to 'behavior'
"The official project language is American English […]."
5d2e915623/CONTRIBUTING.md (L30)

Here's a short statistic of the occurrences of the word "behavio(u)r":

$ git grep -IPioh 'behaviou?r' | sort | uniq -c | sort -n
      2 BEHAVIOR
     24 Behaviour
     32 behaviour
    407 Behavior
    992 behavior

Therefore, it is clear that "behaviour" (56 occurrences) should be
regarded a typo, and "behavior" (1401 occurrences) should be preferred.

Note that The occurrences in LibJS are intentionally NOT changed,
because there are taken verbatim from the specification. Hence:

$ git grep -IPioh 'behaviou?r' | sort | uniq -c | sort -n
      2 BEHAVIOR
     10 behaviour
     24 Behaviour
    407 Behavior
   1014 behavior
2023-05-07 01:05:09 +02:00
Liav A
36bb04d792 Kernel/Memory: Fix UNMAP_AFTER_INIT page fault handling
This was discovered by me during a work on USB keyboard patches, so it
triggered this bug.

The printing format for the VirtualAddress part is incorrect, leading to
another crash when handling page fault after accessing UNMAP_AFTER_INIT
code section.
2023-05-06 08:03:34 +02:00
Tim Schumacher
9ab598af49 Revert "Kernel/x86: Bake the Prekernel and the Kernel into one image"
Some hardware/software configurations crash KVM as soon as we try to
start Serenity. The exact cause is currently unknown, so just fully
revert it for now.

This reverts commit 897c4e5145.
2023-04-28 23:24:19 +02:00
Liav A
897c4e5145 Kernel/x86: Bake the Prekernel and the Kernel into one image
The new baked image is a Prekernel and a Kernel baked together now, so
essentially we no longer need to pass the Prekernel as -kernel and the
actual  kernel image as -initrd to QEMU, leaving the option to pass an
actual initrd or initramfs module later on with multiboot.
2023-04-28 09:23:30 +02:00
Liav A
fb8d4b7032 Kernel/Memory: Explain better why we don't use the first 1 MiB on x86_64 2023-04-09 19:40:45 -06:00