summaryrefslogtreecommitdiff
path: root/Kernel/Memory
AgeCommit message (Collapse)Author
2023-02-21Kernel: Make NNRP<PhysicalPage const> possibleAndreas Kling
This wasn't possible before as ref() and unref() were non-const.
2023-02-08Everywhere: Use ReadonlySpan<T> instead of Span<T const>MacDue
2023-02-06Kernel: Protect Process::m_name with a spinlockSam Atkins
This also lets us remove the `get_process_name` and `set_process_name` syscalls from the big lock. :^)
2023-01-28AK: Remove `try_` prefix from FixedArray creation functionsLinus Groh
2023-01-27Kernel: Move Memory/PageDirectory.{cpp,h} to arch-specific directoryTimon Kruiper
The handling of page tables is very architecture specific, so belongs in the Arch directory. Some parts were already architecture-specific, however this commit moves the rest of the PageDirectory class into the Arch directory. While we're here the aarch64/PageDirectory.{h,cpp} files are updated to be aarch64 specific, by renaming some members and removing x86_64 specific code.
2023-01-25Kernel: Move Aarch64 MMU debug message into memory manager initializerkonrad
Doing so unifies startup debug messages visually.
2023-01-24Kernel/aarch64: Change MMU::kernel_virtual_range to high virtual memoryTimon Kruiper
This was previously hardcoded this to be the physical memory range, since we identity mapped the memory, however we now run the kernel at a high virtual memory address. Also changes PageDirectory.h to store up-to 512 pages, as the code now needs access to more than 4 pages.
2023-01-21Kernel+LibC: Move name length constants to Kernel/API from limits.hAndrew Kaster
Reduce inclusion of limits.h as much as possible at the same time. This does mean that kmalloc.h is now including Kernel/API/POSIX/limits.h instead of LibC/limits.h, but the scope could be limited a lot more. Basically every file in the kernel includes kmalloc.h, and needs the limits.h include for PAGE_SIZE.
2023-01-02Everywhere: Remove unused includes of AK/Memory.hBen Wiederhake
These instances were detected by searching for files that include AK/Memory.h, but don't match the regex: \\b(fast_u32_copy|fast_u32_fill|secure_zero|timing_safe_compare)\\b This regex is pessimistic, so there might be more files that don't actually use any memory function. In theory, one might use LibCPP to detect things like this automatically, but let's do this one step after another.
2023-01-02Kernel: Turn lock ranks into template parameterskleines Filmröllchen
This step would ideally not have been necessary (increases amount of refactoring and templates necessary, which in turn increases build times), but it gives us a couple of nice properties: - SpinlockProtected inside Singleton (a very common combination) can now obtain any lock rank just via the template parameter. It was not previously possible to do this with SingletonInstanceCreator magic. - SpinlockProtected's lock rank is now mandatory; this is the majority of cases and allows us to see where we're still missing proper ranks. - The type already informs us what lock rank a lock has, which aids code readability and (possibly, if gdb cooperates) lock mismatch debugging. - The rank of a lock can no longer be dynamic, which is not something we wanted in the first place (or made use of). Locks randomly changing their rank sounds like a disaster waiting to happen. - In some places, we might be able to statically check that locks are taken in the right order (with the right lock rank checking implementation) as rank information is fully statically known. This refactoring even more exposes the fact that Mutex has no lock rank capabilites, which is not fixed here.
2022-12-29Kernel: Put x86_64 specific VERIFY in PageDirectory.cpp behind ifdefTimon Kruiper
This makes it possible to run this code on aarch64.
2022-12-28Kernel: Remove the two remaining ARCH(I386) checksAndreas Kling
2022-12-28Kernel: Remove i686 supportLiav A
2022-12-22AK: Rename Bitmap::try_create() to ::create()Sam Atkins
This is step 1 to removing `must_create()`.
2022-12-16Kernel/Memory: Add option to annotate region mapping as immutableLiav A
We add this basic functionality to the Kernel so Userspace can request a particular virtual memory mapping to be immutable. This will be useful later on in the DynamicLoader code. The annotation of a particular Kernel Region as immutable implies that the following restrictions apply, so these features are prohibited: - Changing the region's protection bits - Unmapping the region - Annotating the region with other virtual memory flags - Applying further memory advises on the region - Changing the region name - Re-mapping the region
2022-12-14Kernel: Ignore an invalid QEMU multiboot entryimplicitfield
This was introduced in the QEMU commit 8504f12 and was causing the kernel to fail to boot on the q35 machine. Fixes #14952.
2022-12-12Kernel: Use `size_t` to keep track of the number of pages in a regionTim Schumacher
We were previously using a 32-bit unsigned integer for this, which caused us to start truncating region sizes when multiplied with `PAGE_SIZE` on hardware with a lot of memory.
2022-12-07Kernel: Add missing VERIFY in MM::allocate_committed_physical_pageThomas Queiroz
2022-12-07Kernel: Don't panic if MemoryManager::find_free_physical_page failsThomas Queiroz
2022-12-03Everywhere: Run clang-formatLinus Groh
2022-11-05Kernel: Add support for jailsLiav A
Our implementation for Jails resembles much of how FreeBSD jails are working - it's essentially only a matter of using a RefPtr in the Process class to a Jail object. Then, when we iterate over all processes in various cases, we could ensure if either the current process is in jail and therefore should be restricted what is visible in terms of PID isolation, and also to be able to expose metadata about Jails in /sys/kernel/jails node (which does not reveal anything to a process which is in jail). A lifetime model for the Jail object is currently plain simple - there's simpy no way to manually delete a Jail object once it was created. Such feature should be carefully designed to allow safe destruction of a Jail without the possibility of releasing a process which is in Jail from the actual jail. Each process which is attached into a Jail cannot leave it until the end of a Process (i.e. when finalizing a Process). All jails are kept being referenced in the JailManagement. When a last attached process is finalized, the Jail is automatically destroyed.
2022-10-18Kernel: Reintroduce `ScopedAddressSpaceSwitcher` to aarch64 buildJesse Buhagiar
2022-10-17Kernel: Move InterruptDisabler out of Arch directoryTimon Kruiper
The code in this file is not architecture specific, so it can be moved to the base Kernel directory.
2022-10-01Kernel/aarch64: Set up pointer to kernel page directoryTimon Kruiper
The MemoryManager uses this pointer to adds its newly created page tables to the kernel page directory.
2022-10-01Kernel/aarch64: Only identity map kernel image, instead of all of RAMTimon Kruiper
For the initial page tables we only need to identity map the kernel image, the rest of the memory will be managed by the MemoryManager. The linker script is updated to get the kernel image start and end addresses.
2022-10-01Kernel: Don't reserve Low Memory (0-1MB) on non-x86 architecturesTimon Kruiper
This memory is only reserved on x86(-64) and is usable on other architectures.
2022-09-26Kernel/FileSystem: Remove the locking of a Inode mutex in InodeVMObjectsLiav A
We no longer require to lock the m_inode_lock in the SharedInodeVMObject code as the methods write_bytes and read_bytes of the Inode class do this for us now.
2022-09-26Kernel: Send SIGBUS to threads that use after valid Inode mmaped rangeLiav A
According to Dr. POSIX, we should allow to call mmap on inodes even on ranges that currently don't map to any actual data. Trying to read or write to those ranges should result in SIGBUS being sent to the thread that did violating memory access. To implement this restriction, we simply check if the result of read_bytes on an Inode returns 0, which means we have nothing valid to map to the program, hence it should receive a SIGBUS in that case.
2022-09-24Revert "Kernel: Send SIGBUS to threads that use after valid Inode mmaped range"Liav A
This reverts commit 0c675192c91999cc2b60cbbea708dbf7b5637897.
2022-09-23Kernel: Introduce the IOWindow classLiav A
This class is intended to replace all IOAddress usages in the Kernel codebase altogether. The idea is to ensure IO can be done in arch-specific manner that is determined mostly in compile-time, but to still be able to use most of the Kernel code in non-x86 builds. Specific devices that rely on x86-specific IO instructions are already placed in the Arch/x86 directory and are omitted for non-x86 builds. The reason this works so well is the fact that x86 IO space acts in a similar fashion to the traditional memory space being available in most CPU architectures - the x86 IO space is essentially just an array of bytes like the physical memory address space, but requires x86 IO instructions to load and store data. Therefore, many devices allow host software to interact with the hardware registers in both ways, with a noticeable trend even in the modern x86 hardware to move away from the old x86 IO space to exclusively using memory-mapped IO. Therefore, the IOWindow class encapsulates both methods for x86 builds. The idea is to allow PCI devices to be used in either way in x86 builds, so when trying to map an IOWindow on a PCI BAR, the Kernel will try to find the proper method being declared with the PCI BAR flags. For old PCI hardware on non-x86 builds this might turn into a problem as we can't use port mapped IO, so the Kernel will gracefully fail with ENOTSUP error code if that's the case, as there's really nothing we can do within such case. For general IO, the read{8,16,32} and write{8,16,32} methods are available as a convenient API for other places in the Kernel. There are simply no direct 64-bit IO API methods yet, as it's not needed right now and is not considered to be Arch-agnostic too - the x86 IO space doesn't support generating 64 bit cycle on IO bus and instead requires two 2 32-bit accesses. If for whatever reason it appears to be necessary to do IO in such manner, it could probably be added with some neat tricks to do so. It is recommended to use Memory::TypedMapping struct if direct 64 bit IO is actually needed.
2022-09-23Kernel/Memory: Introduce a method to allocate TypedMapping on the heapLiav A
This will be used later on to allocate such structure on the heap when it is necessary to do so.
2022-09-20Kernel/x86: Move RTC and CMOS code to x86 arch-specific subdirectoryLiav A
The RTC and CMOS are currently only supported for x86 platforms and use specific x86 instructions to produce only certain x86 plaform operations and results, therefore, we move them to the Arch/x86 specific directory.
2022-09-16Kernel: Send SIGBUS to threads that use after valid Inode mmaped rangeLiav A
According to Dr. POSIX, we should allow to call mmap on inodes even on ranges that currently don't map to any actual data. Trying to read or write to those ranges should result in SIGBUS being sent to the thread that did violating memory access.
2022-09-16Kernel: Handle mmap requests on zero-length data file inodes safelyLiav A
2022-09-12Kernel: Add basic aarch64 support to `MemoryManager`Filiph Sandström
FIXME: There's still a lot to do like for example, port `quickmap_page`. This does however get us further into the boot process than before.
2022-08-27Kernel: Dump OOM debug info after releasing the MM global data lockIdan Horowitz
Otherwise we would be holding the MM global data lock and the Process address space locks in reversed order to the rest of the system, which can lead to deadlocks.
2022-08-26Kernel: Use InterruptsState in Spinlock codeTimon Kruiper
This commit updates the lock function from Spinlock and RecursiveSpinlock to return the InterruptsState of the processor, instead of the processor flags. The unlock functions would only look at the interrupt flag of the processor flags, so we now use the InterruptsState enum to clarify the intent, and such that we can use the same Spinlock code for the aarch64 build. To not break the build, all the call sites are updated aswell.
2022-08-26Kernel: Remove global MM lock in favor of SpinlockProtectedAndreas Kling
Globally shared MemoryManager state is now kept in a GlobalData struct and wrapped in SpinlockProtected. A small set of members are left outside the GlobalData struct as they are only set during boot initialization, and then remain constant. This allows us to access those members without taking any locks.
2022-08-24Kernel: Use RefPtr instead of LockRefPtr for PhysicalPageAndreas Kling
I believe this to be safe, as the main thing that LockRefPtr provides over RefPtr is safe copying from a shared LockRefPtr instance. I've inspected the uses of RefPtr<PhysicalPage> and it seems they're all guarded by external locking. Some of it is less obvious, but this is an area where we're making continuous headway.
2022-08-24Kernel: Make PhysicalPage::ref() use relaxed memory orderAndreas Kling
When incrementing a reference count, it should be sufficient to use relaxed ordering. Note that unref() still uses acquire-release.
2022-08-24Kernel: Don't take MM lock in ~PageDirectory()Andreas Kling
We don't need the MM lock to unregister a PageDirectory from the CR3 map. This is already protected by the CR3 map's own lock.
2022-08-24Kernel: Don't take MM lock in MemoryManager::dump_kernel_regions()Andreas Kling
We have to hold the region tree lock while dumping its regions anyway, and taking the MM lock here was unnecessary.
2022-08-24Kernel: Don't take MM lock in MemoryManager::enter_address_space()Andreas Kling
We're not accessing any of the MM members here. Also remove some redundant code to update CR3, since it calls activate_page_directory() which does exactly the same thing.
2022-08-24Kernel: Update comment about what the MM lock protectsAndreas Kling
2022-08-24Kernel: Don't wrap AddressSpace's RegionTree in SpinlockProtectedAndreas Kling
Now that AddressSpace itself is always SpinlockProtected, we don't need to also wrap the RegionTree. Whoever has the AddressSpace locked is free to poke around its tree.
2022-08-24Kernel: Make file-backed memory regions remember description permissionsAndreas Kling
This allows sys$mprotect() to honor the original readable & writable flags of the open file description as they were at the point we did the original sys$mmap(). IIUC, this is what Dr. POSIX wants us to do: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mprotect.html Also, remove the bogus and racy "W^X" checking we did against mappings based on their current inode metadata. If we want to do this, we can do it properly. For now, it was not only racy, but also did blocking I/O while holding a spinlock.
2022-08-24Kernel: Wrap process address spaces in SpinlockProtectedAndreas Kling
This forces anyone who wants to look into and/or manipulate an address space to lock it. And this replaces the previous, more flimsy, manual spinlock use. Note that pointers *into* the address space are not safe to use after you unlock the space. We've got many issues like this, and we'll have to track those down as wlel.
2022-08-24Kernel: Don't hog the MM lock while unmapping regionsAndreas Kling
We were holding the MM lock across all of the region unmapping code. This was previously necessary since the quickmaps used during unmapping required holding the MM lock. Now that it's no longer necessary, we can leave the MM lock alone here.
2022-08-24Kernel: Wrap RegionTree objects in SpinlockProtectedAndreas Kling
This makes locking them much more straightforward, and we can remove a bunch of confusing use of AddressSpace::m_lock. That lock will also be converted to use of SpinlockProtected in a subsequent patch.
2022-08-22Kernel: Stop taking MM lock while using regular quickmapsAndreas Kling
You're still required to disable interrupts though, as the mappings are per-CPU. This exposed the fact that our CR3 lookup map is insufficiently protected (but we'll address that in a separate commit.)