From 047f445651360cc735a206f81a7a658fd9d01871 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 19 Nov 2022 18:38:47 +0100 Subject: [PATCH] Kernel: Add helpers to validate memory addresses --- kernel/src/memory/MemoryManager.cpp | 15 +++++++++++++++ kernel/src/memory/MemoryManager.h | 3 +++ 2 files changed, 18 insertions(+) diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index 74357446..c8ce477b 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -184,6 +184,21 @@ namespace MemoryManager return {}; } + bool validate_readable_page(u64 address) + { + auto rc = MMU::get_flags(address); + if (rc.has_error()) return false; + return true; + } + + bool validate_writable_page(u64 address) + { + auto rc = MMU::get_flags(address); + if (rc.has_error()) return false; + if (rc.release_value() & MMU::ReadWrite) return true; + return false; + } + u64 free() { return free_mem; diff --git a/kernel/src/memory/MemoryManager.h b/kernel/src/memory/MemoryManager.h index d1ca0f5f..6250b863 100644 --- a/kernel/src/memory/MemoryManager.h +++ b/kernel/src/memory/MemoryManager.h @@ -17,6 +17,9 @@ namespace MemoryManager Result remap(u64 address, usize count, int flags); Result remap_unaligned(u64 address, usize count, int flags); + bool validate_readable_page(u64 address); + bool validate_writable_page(u64 address); + u64 free(); u64 used(); u64 reserved();