summaryrefslogtreecommitdiff
path: root/Kernel/Heap/kmalloc.h
AgeCommit message (Collapse)Author
2021-08-13Kernel: Allow `kfree_aligned` to be called on null pointersDaniel Bertalan
The C++ standard specifies that `free` and `operator delete` should be callable with nullptr. The non-aligned `kfree` already handles this, but because of the pointer arithmetic to obtain the allocation start pointer, the aligned version would produce undefined behavior.
2021-08-13Kernel: Allow kmalloc(..) / kmalloc_aligned(..) to return nullptrBrian Gianforcaro
Now that we have a significant amount of code paths handling OOM, lets enable kmalloc and friends to actually return nullptr. This way we can start stressing these paths and validating all of they work as expected.
2021-07-16Kernel+AK: Generate compile-time error for non-sized `delete`Daniel Bertalan
This is a much more ergonomic option than getting a `VERIFY_NOT_REACHED()` failure at run-time. I encountered this issue with Clang, where sized deallocation is not the default due to ABI breakage concerns. Note that we can't simply just not declare these functions, because the C++ standard states: > If this function with size parameter is defined, the program shall > also define the version without the size parameter.
2021-07-16Kernel: Implement aligned `operator new` and use itDaniel Bertalan
The compiler will use these to allocate objects that have alignment requirements greater than that of our normal `operator new` (4/8 byte aligned). This means we can now use smart pointers for over-aligned types. Fixes a FIXME.
2021-07-11Kernel: Remove krealloc()Andreas Kling
This was only used by a single class (AK::ByteBuffer) in the kernel and not in an OOM-safe way. Now that ByteBuffer no longer uses it, there's no need for the kernel heap to burden itself with supporting this.
2021-07-11Kernel: Add kfree_sized(), kfree() with a known allocation sizeAndreas Kling
C++14 gave us sized operator delete, but we haven't been taking advantage of it. Let's get to a point where it can help us by adding kfree_sized(void*, size_t).
2021-06-24AK+Kernel: Make fallible allocations compiler-agnosticDaniel Bertalan
In standard C++, operators `new` and `new[]` are guaranteed to return a valid (non-null) pointer and throw an exception if the allocation couldn't be performed. Based on this, compilers did not check the returned pointer before attempting to use them for object construction. To avoid this, the allocator operators were changed to be `noexcept` in PR #7026, which made GCC emit the desired null checks. Unfortunately, this is a non-standard feature which meant that Clang would not accept these function definitions, as it did not match its expected declaration. To make compiling using Clang possible, the special "nothrow" versions of `new` are implemented in this commit. These take a tag type of `std::nothrow_t` (used for disambiguating from placement new/etc.), and are allowed by the standard to return null. There is a global variable, `std::nothrow`, declared with this type, which is also exported into the global namespace. To perform fallible allocations, the following syntax should be used: ```cpp auto ptr = new (nothrow) T; ``` As we don't support exceptions in the kernel, the only way of uphold the "throwing" new's guarantee is to abort if the allocation couldn't be performed. Once we have proper OOM handling in the kernel, this should only be used for critical allocations, where we wouldn't be able to recover from allocation failures anyway.
2021-06-24Kernel: Remove PAGE_SIZE from CPU.hHendiadyoin1
We have that information in LibC, lets use that instead
2021-05-15AK+LibC: Implement malloc_good_size() and use it for Vector/HashTableGunnar Beutner
This implements the macOS API malloc_good_size() which returns the true allocation size for a given requested allocation size. This allows us to make use of all the available memory in a malloc chunk. For example, for a malloc request of 35 bytes our malloc would internally use a chunk of size 64, however the remaining 29 bytes would be unused. Knowing the true allocation size allows us to request more usable memory that would otherwise be wasted and make that available for Vector, HashTable and potentially other callers in the future.
2021-05-13Kernel: Declare operator new/delete noexcept for MAKE_ALIGNED_ALLOCATEDBrian Gianforcaro
2021-05-13Kernel: Declare operator new/delete as noexcept for the KernelBrian Gianforcaro
For Kernel OOM hardening to work correctly, we need to be able to call a "nothrow" version of operator new. Unfortunately the default "throwing" version of operator new assumes that the allocation will never return on failure and will always throw an exception. This isn't true in the Kernel, as we don't have exceptions. So if we call the normal/throwing new and kmalloc returns NULL, the generated code will happily go and dereference that NULL pointer by invoking the constructor before we have a chance to handle the failure. To fix this we declare operator new as noexcept in the Kernel headers, which will allow the caller to actually handle allocation failure. The delete implementations need to match the prototype of the new which allocated them, so we need define delete as noexcept as well. GCC then errors out declaring that you should implement sized delete as well, so this change provides those stubs in order to compile cleanly. Finally the new operator definitions have been standardized as being declared with [[nodiscard]] to avoid potential memory leaks. So lets declares the kernel versions that way as well.
2021-04-22Everything: Move to SPDX license identifiers in all files.Brian Gianforcaro
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-03-11Kernel: Add MAKE_ALIGNED_ALLOCATED helper macroAndreas Kling
This macro inserts operator new/delete into a class, allowing you to very easily specify a specific heap alignment.
2021-03-11Kernel: Allow kmalloc_aligned() alignment up to 4096Andreas Kling
This allows us to get kmalloc() memory aligned to the VM page size.
2021-03-04Kernel: Remove unused KMALLOC_DEBUG_LARGE_ALLOCATIONS modeAndreas Kling
This was a thing back when the system was so little that any kernel allocation above 1 MiB was basically guaranteed to be a bug. :^)
2021-01-26Meta: Split debug defines into multiple headers.asynts
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-25Everywhere: Hook up remaining debug macros to Debug.h.asynts
2021-01-25Everywhere: Remove unnecessary debug comments.asynts
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-22Kernel: Move kmalloc heaps and super pages inside .bss segmentJean-Baptiste Boric
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.
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-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-14Kernel: mark kmalloc with attributesMuhammad Zahalqa
2020-05-16Kernel: Use consistent names for kmalloc globals and remove volatileAndreas Kling
2020-04-30AK: Add ALWAYS_INLINE, NEVER_INLINE and FLATTEN macrosAndreas Kling
It's tedious to write (and look at) [[gnu::always_inline]] etc. :^)
2020-02-01Kernel: Add crash logging heuristic for uninitialized kmalloc()/kfree()Andreas Kling
Since we scrub both kmalloc() and kfree() with predictable values, we can log a helpful message when hitting a crash that looks like it might be a dereference of such scrubbed data.
2020-01-27Kernel: Never validate access to the kmalloc memory rangeAndreas Kling
Memory validation is used to verify that user syscalls are allowed to access a given memory range. Ring 0 threads never make syscalls, and so will never end up in validation anyway. The reason we were allowing kmalloc memory accesses is because kernel thread stacks used to be allocated in kmalloc memory. Since that's no longer the case, we can stop making exceptions for kmalloc in the validation code.
2020-01-18Meta: Add license header to source filesAndreas Kling
As suggested by Joshua, this commit adds the 2-clause BSD license as a comment block to the top of every source file. For the first pass, I've just added myself for simplicity. I encourage everyone to add themselves as copyright holders of any file they've added or modified in some significant way. If I've added myself in error somewhere, feel free to replace it with the appropriate copyright holder instead. Going forward, all new source files should include a license header.
2019-11-27Kernel: Demangle userspace ELF symbols in backtracesAndreas Kling
Turns out we can use abi::__cxa_demangle() for this, and all we need to provide is sprintf(), realloc() and free(), so this patch exposes them. We now have fully demangled C++ backtraces :^)
2019-09-16Kernel: Move kmalloc() into a Kernel/Heap/ directoryAndreas Kling