From 4c6fd454d0b96734092661fb3739abe2a576c4ce Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sun, 24 Dec 2023 15:52:15 +0200 Subject: [PATCH] Kernel: Add MM helper for shrinking a virtual range to page boundaries --- Kernel/Memory/MemoryManager.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Kernel/Memory/MemoryManager.h b/Kernel/Memory/MemoryManager.h index 04812fb93b4..168d72fffb0 100644 --- a/Kernel/Memory/MemoryManager.h +++ b/Kernel/Memory/MemoryManager.h @@ -321,4 +321,18 @@ inline ErrorOr expand_range_to_page_boundaries(FlatPtr add return Memory::VirtualRange { base, end - base.get() }; } +inline ErrorOr shrink_range_to_page_boundaries(FlatPtr address, size_t size) +{ + if ((address + size) < address) + return EINVAL; + + auto base = TRY(Memory::page_round_up(address)); + auto end = Memory::page_round_down(address + size); + + if (end < base) + return EINVAL; + + return Memory::VirtualRange { VirtualAddress(base), end - base }; +} + }