From d2856d8812f761482e748daf9620465e431a2c2e Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 30 Nov 2022 17:13:59 +0100 Subject: [PATCH] Provide meaningful error numbers --- kernel/src/arch/x86_64/CPU.cpp | 7 ++++--- kernel/src/arch/x86_64/MMU.cpp | 27 ++++++++++++++------------- kernel/src/memory/Heap.cpp | 11 ++++++----- kernel/src/memory/MemoryManager.cpp | 7 ++++--- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/kernel/src/arch/x86_64/CPU.cpp b/kernel/src/arch/x86_64/CPU.cpp index 968ba063..83a65206 100644 --- a/kernel/src/arch/x86_64/CPU.cpp +++ b/kernel/src/arch/x86_64/CPU.cpp @@ -3,6 +3,7 @@ #include "arch/Timer.h" #include "arch/x86_64/IO.h" #include +#include #include #include @@ -347,11 +348,11 @@ namespace CPU static char brand_string[49]; u32 buf[4]; - if (!__get_cpuid(0x80000002, &buf[0], &buf[1], &buf[2], &buf[3])) return err; + if (!__get_cpuid(0x80000002, &buf[0], &buf[1], &buf[2], &buf[3])) return err(ENOTSUP); memcpy(brand_string, buf, 16); - if (!__get_cpuid(0x80000003, &buf[0], &buf[1], &buf[2], &buf[3])) return err; + if (!__get_cpuid(0x80000003, &buf[0], &buf[1], &buf[2], &buf[3])) return err(ENOTSUP); memcpy(&brand_string[16], buf, 16); - if (!__get_cpuid(0x80000004, &buf[0], &buf[1], &buf[2], &buf[3])) return err; + if (!__get_cpuid(0x80000004, &buf[0], &buf[1], &buf[2], &buf[3])) return err(ENOTSUP); memcpy(&brand_string[32], buf, 16); brand_string[48] = 0; // null-terminate it :) diff --git a/kernel/src/arch/x86_64/MMU.cpp b/kernel/src/arch/x86_64/MMU.cpp index 613fcacb..a36a038e 100644 --- a/kernel/src/arch/x86_64/MMU.cpp +++ b/kernel/src/arch/x86_64/MMU.cpp @@ -1,6 +1,7 @@ #include "arch/MMU.h" #include "memory/MemoryManager.h" #include +#include #define PAGE_SIZE 4096 @@ -161,12 +162,12 @@ namespace MMU Result find_entry(u64 virt) { auto& l4 = l4_entry(virt); - if (!l4.present) return err; + if (!l4.present) return err(EFAULT); auto& l3 = l3_entry(virt); - if (!l3.present) return err; + if (!l3.present) return err(EFAULT); if (l3.larger_pages) return &l3; auto& l2 = l2_entry(virt); - if (!l2.present) return err; + if (!l2.present) return err(EFAULT); if (l2.larger_pages) return &l2; return &l1_entry(virt); } @@ -174,16 +175,16 @@ namespace MMU Result apply_cascading_flags(u64 virt, int flags) { auto& l4 = l4_entry(virt); - if (!l4.present) return err; + if (!l4.present) return err(EFAULT); if (flags & Flags::ReadWrite) l4.read_write = true; if (flags & Flags::User) l4.user = true; auto& l3 = l3_entry(virt); - if (!l3.present) return err; + if (!l3.present) return err(EFAULT); if (l3.larger_pages) return &l3; if (flags & Flags::ReadWrite) l3.read_write = true; if (flags & Flags::User) l3.user = true; auto& l2 = l2_entry(virt); - if (!l2.present) return err; + if (!l2.present) return err(EFAULT); if (l2.larger_pages) return &l2; if (flags & Flags::ReadWrite) l2.read_write = true; if (flags & Flags::User) l2.user = true; @@ -217,7 +218,7 @@ namespace MMU if (flags & Flags::ReadWrite) l3.read_write = true; if (flags & Flags::User) l3.user = true; - if (l3.larger_pages) return err; // FIXME: Replacing larger pages is not supported ATM + if (l3.larger_pages) return err(EFIXME); // FIXME: Replacing larger pages is not supported ATM auto& l2 = l2_entry(virt); if (!l2.present) @@ -231,10 +232,10 @@ namespace MMU if (flags & Flags::ReadWrite) l2.read_write = true; if (flags & Flags::User) l2.user = true; - if (l2.larger_pages) return err; // FIXME: Replacing larger pages is not supported ATM + if (l2.larger_pages) return err(EFIXME); // FIXME: Replacing larger pages is not supported ATM auto& l1 = l1_entry(virt); - if (l1.present) return err; // Please explicitly unmap the page before mapping it again. + if (l1.present) return err(EEXIST); // Please explicitly unmap the page before mapping it again. l1.ignore0 = l1.ignore1 = false; l1.present = true; l1.read_write = (flags & Flags::ReadWrite); @@ -249,7 +250,7 @@ namespace MMU Result remap(u64 virt, int flags) { auto& l1 = *TRY(apply_cascading_flags(virt, flags)); - if (!l1.present) return err; + if (!l1.present) return err(EFAULT); l1.read_write = (flags & Flags::ReadWrite); l1.user = (flags & Flags::User); l1.write_through = (flags & Flags::WriteThrough); @@ -261,7 +262,7 @@ namespace MMU Result unmap(u64 virt) { auto& l1 = *TRY(find_entry(virt)); - if (!l1.present) return err; + if (!l1.present) return err(EFAULT); u64 address = l1.get_address(); memset(&l1, 0, sizeof(l1)); flush_page(virt); @@ -271,14 +272,14 @@ namespace MMU Result get_physical(u64 virt) { auto& l1 = *TRY(find_entry(virt)); - if (!l1.present) return err; + if (!l1.present) return err(EFAULT); return l1.get_address(); } Result get_flags(u64 virt) { auto& l1 = *TRY(find_entry(virt)); - if (!l1.present) return err; + if (!l1.present) return err(EFAULT); return arch_flags_to_mmu(l1); } diff --git a/kernel/src/memory/Heap.cpp b/kernel/src/memory/Heap.cpp index 74866481..1b18516f 100644 --- a/kernel/src/memory/Heap.cpp +++ b/kernel/src/memory/Heap.cpp @@ -4,6 +4,7 @@ #include "memory/MemoryManager.h" #include #include +#include static constexpr int BLOCK_USED = 1 << 0; static constexpr int BLOCK_START_MEM = 1 << 1; @@ -97,7 +98,7 @@ static Result split(HeapBlock* block, usize size) usize old_size = block->full_size; // Save the old value of this variable since we are going to use it after modifying it - if (available < (size + sizeof(HeapBlock))) return err; + if (available < (size + sizeof(HeapBlock))) return err(0); // This error is not propagated. usize offset = get_fair_offset_to_split_at(block, size + sizeof(HeapBlock)); block->full_size = offset; // shrink the old block to fit this offset @@ -244,13 +245,13 @@ Result kfree(void* ptr) else Serial::printf("ERROR: Attempt to free memory at %p, which wasn't allocated with kmalloc\n", ptr); - return err; + return err(EFAULT); } if (is_block_free(block)) { Serial::printf("ERROR: Attempt to free memory at %p, which was already freed\n", ptr); - return err; + return err(EFAULT); } else block->status &= ~BLOCK_USED; @@ -300,7 +301,7 @@ Result krealloc(void* ptr, usize size) else Serial::printf("ERROR: Attempt to realloc memory at %p, which wasn't allocated with kmalloc\n", ptr); - return err; + return err(EFAULT); } size = align_up(size, 16UL); @@ -308,7 +309,7 @@ Result krealloc(void* ptr, usize size) if (is_block_free(block)) { Serial::printf("ERROR: Attempt to realloc memory at %p, which was already freed\n", ptr); - return err; + return err(EFAULT); } if (block->full_size >= size) diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index 54561743..906efaae 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -4,6 +4,7 @@ #include "boot/bootboot.h" #include #include +#include #include extern BOOTBOOT bootboot; @@ -143,14 +144,14 @@ namespace MemoryManager return index * ARCH_PAGE_SIZE; } - return err; // FIXME: ENOMEM. + return err(ENOMEM); } Result free_frame(u64 frame) { const u64 index = frame / ARCH_PAGE_SIZE; - if (index > (page_bitmap_size * 8)) return err; - if (!page_bitmap_read(index)) return err; + if (index > (page_bitmap_size * 8)) return err(EFAULT); + if (!page_bitmap_read(index)) return err(EFAULT); page_bitmap_set(index, false); used_mem -= ARCH_PAGE_SIZE; free_mem += ARCH_PAGE_SIZE;