summaryrefslogtreecommitdiff
path: root/Kernel/VM
AgeCommit message (Collapse)Author
2020-12-29Kernel/VM: Make local_offset in PhysicalRegion::free_page_at unsignedLuke
Anything above or equal to the 2 GB mark has the left most bit set (0x8000...), which was falsely interpreted as negative due to local_offset being signed. This makes it unsigned by using FlatPtr. To check for underflow as was intended, lets use Checked instead. Fixes #4585
2020-12-25AK: Remove custom %w format string specifierAndreas Kling
This was a non-standard specifier alias for %04x. This patch replaces all uses of it with new-style formatting functions instead.
2020-12-21Kernel: Don't skip if found free page to allocate from a super regionLiav A
This was a bad pattern that wasn't detected because we only had one super physical region that was initialized by MemoryManager.
2020-12-21Everywhere: Switch from (void) to [[maybe_unused]] (#4473)Lenny Maiorani
Problem: - `(void)` simply casts the expression to void. This is understood to indicate that it is ignored, but this is really a compiler trick to get the compiler to not generate a warning. Solution: - Use the `[[maybe_unused]]` attribute to indicate the value is unused. Note: - Functions taking a `(void)` argument list have also been changed to `()` because this is not needed and shows up in the same grep command.
2020-12-18Kernel: Move KBufferBuilder to the fallible KBuffer APIAndreas Kling
KBufferBuilder::build() now returns an OwnPtr<KBuffer> and can fail. Clients of the API have been updated to handle that situation.
2020-12-12Kernel: Fix some issues related to fixes and block conditionsTom
Fix some problems with join blocks where the joining thread block condition was added twice, which lead to a crash when trying to unblock that condition a second time. Deferred block condition evaluation by File objects were also not properly keeping the File object alive, which lead to some random crashes and corruption problems. Other problems were caused by the fact that the Queued state didn't handle signals/interruptions consistently. To solve these issues we remove this state entirely, along with Thread::wait_on and change the WaitQueue into a BlockCondition instead. Also, deliver signals even if there isn't going to be a context switch to another thread. Fixes #4336 and #4330
2020-12-01Kernel: Fix some problems with Thread::wait_on and LockTom
This changes the Thread::wait_on function to not enable interrupts upon leaving, which caused some problems with page fault handlers and in other situations. It may now be called from critical sections, with interrupts enabled or disabled, and returns to the same state. This also requires some fixes to Lock. To aid debugging, a new define LOCK_DEBUG is added that enables checking for Lock leaks upon finalization of a Thread.
2020-11-11Kernel: Protect the PageDirectory from concurrent accessTom
2020-11-11Kernel: Add locks around RangeAllocatorTom
We need to keep multiple processors from changing it at the same time.
2020-11-10AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safeTom
This makes most operations thread safe, especially so that they can safely be used in the Kernel. This includes obtaining a strong reference from a weak reference, which now requires an explicit call to WeakPtr::strong_ref(). Another major change is that Weakable::make_weak_ref() may require the explicit target type. Previously we used reinterpret_cast in WeakPtr, assuming that it can be properly converted. But WeakPtr does not necessarily have the knowledge to be able to do this. Instead, we now ask the class itself to deliver a WeakPtr to the type that we want. Also, WeakLink is no longer specific to a target type. The reason for this is that we want to be able to safely convert e.g. WeakPtr<T> to WeakPtr<U>, and before this we just reinterpret_cast the internal WeakLink<T> to WeakLink<U>, which is a bold assumption that it would actually produce the correct code. Instead, WeakLink now operates on just a raw pointer and we only make those constructors/operators available if we can verify that it can be safely cast. In order to guarantee thread safety, we now use the least significant bit in the pointer for locking purposes. This also means that only properly aligned pointers can be used.
2020-11-01Kernel: Flush TLB when quick-mapping PD/PT that was mapped on other CPUTom
If a PD/PT was quick-mapped by another CPU we still need to flush the TLB on the current CPU. Fixes #3885
2020-10-16Kernel: Ensure PhysicalRegion free page hint is within valid rangeTom
Fixes #3770
2020-10-12MemoryManager: Off-by-one error when collecting memory pages.asynts
Notice that we ensured that the size is a multiple of the page size and that there is at least one page there, otherwise, this change would be invalid. We create an empty region and then expand it: // First iteration. m_user_physical_regions.append(PhysicalRegion::create(addr, addr)); // Following iterations. region->expand(region->lower(), addr); So if the memory region only has one page, we would end up with an empty region. Thus we need to do one more iteration.
2020-09-25Meta+Kernel: Make clang-format-10 cleanBen Wiederhake
2020-09-16Kernel: Return ENOMEM in more placesLuke
There are plenty of places in the kernel that aren't checking if they actually got their allocation. This fixes some of them, but definitely not all. Fixes #3390 Fixes #3391 Also, let's make find_one_free_page() return nullptr if it doesn't get a free index. This stops the kernel crashing when out of memory and allows memory purging to take place again. Fixes #3487
2020-09-13Kernel: Make copy_to/from_user safe and remove unnecessary checksTom
Since the CPU already does almost all necessary validation steps for us, we don't really need to attempt to do this. Doing it ourselves doesn't really work very reliably, because we'd have to account for other processors modifying virtual memory, and we'd have to account for e.g. pages not being able to be allocated due to insufficient resources. So change the copy_to/from_user (and associated helper functions) to use the new safe_memcpy, which will return whether it succeeded or not. The only manual validation step needed (which the CPU can't perform for us) is making sure the pointers provided by user mode aren't pointing to kernel mappings. To make it easier to read/write from/to either kernel or user mode data add the UserOrKernelBuffer helper class, which will internally either use copy_from/to_user or directly memcpy, or pass the data through directly using a temporary buffer on the stack. Last but not least we need to keep syscall params trivial as we need to copy them from/to user mode using copy_from/to_user.
2020-09-12Kernel: Fix various forward declarationsBen Wiederhake
I decided to modify MappedROM.h because all other entried in Forward.h are also classes, and this is visually more pleasing. Other than that, it just doesn't make any difference which way we resolve the conflicts.
2020-09-09Kernel: Optimize single physical page allocation and randomize returnsTom
Rather than trying to find a contiguous set of bits of size 1, just find one single available bit using a hint. Also, try to randomize returned physical pages a bit by placing them into a 256 entry queue rather than making them available immediately. Then, once the queue is filled, pick a random one, make it available again and use that slot for the latest page to be returned.
2020-09-08Refactor: Replace usages of FixedArray with Vector.asynts
2020-09-02Kernel: Handle committing pages in regions more gracefullyTom
Sometimes a physical underlying page may be there, but we may be unable to allocate a page table that may be needed to map it. Bubble up such mapping errors so that they can be handled more appropriately.
2020-09-01Kernel: Fix memory purge clobbering mapped page directory in ensure_pteTom
If allocating a page table triggers purging memory, we need to call quickmap_pd again to make sure the underlying physical page is remapped to the correct one. This is needed because purging itself may trigger calls to ensure_pte as well. Fixes #3370
2020-09-01Kernel: Remove assertion from Region::commitTom
We should be able to gracefully fail a commit in low-memory situations.
2020-09-01Kernel: Only remap regions if memory was purged from themTom
2020-09-01Kernel: Preserve internal state in cloned PurgeableVMObjectsAndreas Kling
When cloning a purgeable memory region (which happens on fork), we need to preserve the "was purged" and "volatile" state of the original region, or they will always appear as non-volatile and unpurged regions in the child process. Fixes #3374.
2020-08-30Kernel: Remove unused variable PhysicalRegion::m_lastAndreas Kling
2020-08-30Kernel: Make Heap implementation reusable, and make kmalloc expandableTom
Add an ExpandableHeap and switch kmalloc to use it, which allows for the kmalloc heap to grow as needed. In order to make heap expansion to work, we keep around a 1 MiB backup memory region, because creating a region would require space in the same heap. This means, the heap will grow as soon as the reported utilization is less than 1 MiB. It will also return memory if an entire subheap is no longer needed, although that is rarely possible.
2020-08-30Kernel: Unbreak building with extra debug macros, part 2Ben Wiederhake
2020-08-28Kernel: Release page tables when no longer neededTom
When unmapping regions, check if page tables can be freed. This is a follow-up change for #3254.
2020-08-26Kernel: Fix losing PTEsTom
We can't use a HashMap with a small key that doesn't guarantee collisions. Change it to a HashTable instead. Fixes #3254
2020-08-26Kernel: Protect looping over VMObject regionsTom
We need to hold the memory manager lock so nobody else can modify these lists while we're iterating them.
2020-08-25Kernel: Switch singletons to use new Singleton classTom
MemoryManager cannot use the Singleton class because MemoryManager::initialize is called before the global constructors are run. That caused the Singleton to be re-initialized, causing it to create another MemoryManager instance. Fixes #3226
2020-08-25Kernel: Fix kmalloc memory corruptionTom
Rather than hardcoding where the kmalloc pool should be, place it at the end of the kernel image instead. This avoids corrupting global variables or other parts of the kernel as it grows. Fixes #3257
2020-08-25Kernel: Make PhysicalPage not movable and use atomic ref countingTom
We should not be moving ref-counted objects.
2020-08-22Revert "Kernel: Switch singletons to use new Singleton class"Andreas Kling
This reverts commit f48feae0b2a300992479abf0b2ded85e45ac6045.
2020-08-22Revert "Kernel: Move Singleton class to AK"Andreas Kling
This reverts commit f0906250a181c831508a45434b9f645ff98f33e4.
2020-08-22Revert "Kernel: Fix regression where MemoryManager is initialized twice"Andreas Kling
This reverts commit 8a75e0b892ab8e1c4765ac4e2f7289b258f1bf5a.
2020-08-22Revert "AK: Get rid of make_singleton function"Andreas Kling
This reverts commit 5a98e329d157a2db8379e0c97c6bdc1328027843.
2020-08-22Revert "Kernel: Make PhysicalPage not movable and use atomic ref counting"Andreas Kling
This reverts commit a89ccd842becdfbc951436da5384d8819374e0f4.
2020-08-22Kernel: Make PhysicalPage not movable and use atomic ref countingTom
We should not be moving ref-counted objects.
2020-08-22AK: Get rid of make_singleton functionTom
Just default the InitFunction template argument.
2020-08-22Kernel: Fix regression where MemoryManager is initialized twiceTom
MemoryManager cannot use the Singleton class because MemoryManager::initialize is called before the global constructors are run. That caused the Singleton to be re-initialized, causing it to create another MemoryManager instance.
2020-08-22Kernel: Move Singleton class to AKTom
2020-08-21Kernel: Fix assertion when releasing contiguous memory regionTom
There is no guarantee that the memory manager lock is held when physical pages are released, so just acquire the memory manager lock.
2020-08-21Kernel: Switch singletons to use new Singleton classTom
Fixes #3226
2020-08-16Kernel: Switch a comment to GiBNico Weber
2020-08-16AK: Rename KB, MB, GB to KiB, MiB, GiBNico Weber
The SI prefixes "k", "M", "G" mean "10^3", "10^6", "10^9". The IEC prefixes "Ki", "Mi", "Gi" mean "2^10", "2^20", "2^30". Let's use the correct name, at least in code. Only changes the name of the constants, no other behavior change.
2020-08-13Kernel: Don't request a random u32 when all but 5 bits are immediately ↵Nico Weber
masked off
2020-08-02AK: Fix overflow and mixed-signedness issues in binary_search() (#2961)Muhammad Zahalqa
2020-07-30Kernel: Rename region_from_foo() => find_region_from_foo()Andreas Kling
Let's emphasize that these functions actually go out and find regions.
2020-07-30Kernel: Move syscall implementations out of Process.cppAndreas Kling
This is something I've been meaning to do for a long time, and here we finally go. This patch moves all sys$foo functions out of Process.cpp and into files in Kernel/Syscalls/. It's not exactly one syscall per file (although it could be, but I got a bit tired of the repetitive work here..) This makes hacking on individual syscalls a lot less painful since you don't have to rebuild nearly as much code every time. I'm also hopeful that this makes it easier to understand individual syscalls. :^)