Provide meaningful error numbers

This commit is contained in:
apio 2022-11-30 17:13:59 +01:00
parent 29dad5651d
commit d2856d8812
4 changed files with 28 additions and 24 deletions

View File

@ -3,6 +3,7 @@
#include "arch/Timer.h" #include "arch/Timer.h"
#include "arch/x86_64/IO.h" #include "arch/x86_64/IO.h"
#include <String.h> #include <String.h>
#include <SystemError.h>
#include <Types.h> #include <Types.h>
#include <cpuid.h> #include <cpuid.h>
@ -347,11 +348,11 @@ namespace CPU
static char brand_string[49]; static char brand_string[49];
u32 buf[4]; 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); 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); 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); memcpy(&brand_string[32], buf, 16);
brand_string[48] = 0; // null-terminate it :) brand_string[48] = 0; // null-terminate it :)

View File

@ -1,6 +1,7 @@
#include "arch/MMU.h" #include "arch/MMU.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include <String.h> #include <String.h>
#include <SystemError.h>
#define PAGE_SIZE 4096 #define PAGE_SIZE 4096
@ -161,12 +162,12 @@ namespace MMU
Result<PageTableEntry*> find_entry(u64 virt) Result<PageTableEntry*> find_entry(u64 virt)
{ {
auto& l4 = l4_entry(virt); auto& l4 = l4_entry(virt);
if (!l4.present) return err; if (!l4.present) return err(EFAULT);
auto& l3 = l3_entry(virt); auto& l3 = l3_entry(virt);
if (!l3.present) return err; if (!l3.present) return err(EFAULT);
if (l3.larger_pages) return &l3; if (l3.larger_pages) return &l3;
auto& l2 = l2_entry(virt); auto& l2 = l2_entry(virt);
if (!l2.present) return err; if (!l2.present) return err(EFAULT);
if (l2.larger_pages) return &l2; if (l2.larger_pages) return &l2;
return &l1_entry(virt); return &l1_entry(virt);
} }
@ -174,16 +175,16 @@ namespace MMU
Result<PageTableEntry*> apply_cascading_flags(u64 virt, int flags) Result<PageTableEntry*> apply_cascading_flags(u64 virt, int flags)
{ {
auto& l4 = l4_entry(virt); 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::ReadWrite) l4.read_write = true;
if (flags & Flags::User) l4.user = true; if (flags & Flags::User) l4.user = true;
auto& l3 = l3_entry(virt); auto& l3 = l3_entry(virt);
if (!l3.present) return err; if (!l3.present) return err(EFAULT);
if (l3.larger_pages) return &l3; if (l3.larger_pages) return &l3;
if (flags & Flags::ReadWrite) l3.read_write = true; if (flags & Flags::ReadWrite) l3.read_write = true;
if (flags & Flags::User) l3.user = true; if (flags & Flags::User) l3.user = true;
auto& l2 = l2_entry(virt); auto& l2 = l2_entry(virt);
if (!l2.present) return err; if (!l2.present) return err(EFAULT);
if (l2.larger_pages) return &l2; if (l2.larger_pages) return &l2;
if (flags & Flags::ReadWrite) l2.read_write = true; if (flags & Flags::ReadWrite) l2.read_write = true;
if (flags & Flags::User) l2.user = 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::ReadWrite) l3.read_write = true;
if (flags & Flags::User) l3.user = 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); auto& l2 = l2_entry(virt);
if (!l2.present) if (!l2.present)
@ -231,10 +232,10 @@ namespace MMU
if (flags & Flags::ReadWrite) l2.read_write = true; if (flags & Flags::ReadWrite) l2.read_write = true;
if (flags & Flags::User) l2.user = 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); 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.ignore0 = l1.ignore1 = false;
l1.present = true; l1.present = true;
l1.read_write = (flags & Flags::ReadWrite); l1.read_write = (flags & Flags::ReadWrite);
@ -249,7 +250,7 @@ namespace MMU
Result<void> remap(u64 virt, int flags) Result<void> remap(u64 virt, int flags)
{ {
auto& l1 = *TRY(apply_cascading_flags(virt, 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.read_write = (flags & Flags::ReadWrite);
l1.user = (flags & Flags::User); l1.user = (flags & Flags::User);
l1.write_through = (flags & Flags::WriteThrough); l1.write_through = (flags & Flags::WriteThrough);
@ -261,7 +262,7 @@ namespace MMU
Result<u64> unmap(u64 virt) Result<u64> unmap(u64 virt)
{ {
auto& l1 = *TRY(find_entry(virt)); auto& l1 = *TRY(find_entry(virt));
if (!l1.present) return err; if (!l1.present) return err(EFAULT);
u64 address = l1.get_address(); u64 address = l1.get_address();
memset(&l1, 0, sizeof(l1)); memset(&l1, 0, sizeof(l1));
flush_page(virt); flush_page(virt);
@ -271,14 +272,14 @@ namespace MMU
Result<u64> get_physical(u64 virt) Result<u64> get_physical(u64 virt)
{ {
auto& l1 = *TRY(find_entry(virt)); auto& l1 = *TRY(find_entry(virt));
if (!l1.present) return err; if (!l1.present) return err(EFAULT);
return l1.get_address(); return l1.get_address();
} }
Result<int> get_flags(u64 virt) Result<int> get_flags(u64 virt)
{ {
auto& l1 = *TRY(find_entry(virt)); auto& l1 = *TRY(find_entry(virt));
if (!l1.present) return err; if (!l1.present) return err(EFAULT);
return arch_flags_to_mmu(l1); return arch_flags_to_mmu(l1);
} }

View File

@ -4,6 +4,7 @@
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include <Alignment.h> #include <Alignment.h>
#include <String.h> #include <String.h>
#include <SystemError.h>
static constexpr int BLOCK_USED = 1 << 0; static constexpr int BLOCK_USED = 1 << 0;
static constexpr int BLOCK_START_MEM = 1 << 1; static constexpr int BLOCK_START_MEM = 1 << 1;
@ -97,7 +98,7 @@ static Result<HeapBlock*> split(HeapBlock* block, usize size)
usize old_size = usize old_size =
block->full_size; // Save the old value of this variable since we are going to use it after modifying it 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)); usize offset = get_fair_offset_to_split_at(block, size + sizeof(HeapBlock));
block->full_size = offset; // shrink the old block to fit this offset block->full_size = offset; // shrink the old block to fit this offset
@ -244,13 +245,13 @@ Result<void> kfree(void* ptr)
else else
Serial::printf("ERROR: Attempt to free memory at %p, which wasn't allocated with kmalloc\n", ptr); 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)) if (is_block_free(block))
{ {
Serial::printf("ERROR: Attempt to free memory at %p, which was already freed\n", ptr); Serial::printf("ERROR: Attempt to free memory at %p, which was already freed\n", ptr);
return err; return err(EFAULT);
} }
else else
block->status &= ~BLOCK_USED; block->status &= ~BLOCK_USED;
@ -300,7 +301,7 @@ Result<void*> krealloc(void* ptr, usize size)
else else
Serial::printf("ERROR: Attempt to realloc memory at %p, which wasn't allocated with kmalloc\n", ptr); 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); size = align_up(size, 16UL);
@ -308,7 +309,7 @@ Result<void*> krealloc(void* ptr, usize size)
if (is_block_free(block)) if (is_block_free(block))
{ {
Serial::printf("ERROR: Attempt to realloc memory at %p, which was already freed\n", ptr); 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) if (block->full_size >= size)

View File

@ -4,6 +4,7 @@
#include "boot/bootboot.h" #include "boot/bootboot.h"
#include <Alignment.h> #include <Alignment.h>
#include <String.h> #include <String.h>
#include <SystemError.h>
#include <Types.h> #include <Types.h>
extern BOOTBOOT bootboot; extern BOOTBOOT bootboot;
@ -143,14 +144,14 @@ namespace MemoryManager
return index * ARCH_PAGE_SIZE; return index * ARCH_PAGE_SIZE;
} }
return err; // FIXME: ENOMEM. return err(ENOMEM);
} }
Result<void> free_frame(u64 frame) Result<void> free_frame(u64 frame)
{ {
const u64 index = frame / ARCH_PAGE_SIZE; const u64 index = frame / ARCH_PAGE_SIZE;
if (index > (page_bitmap_size * 8)) return err; if (index > (page_bitmap_size * 8)) return err(EFAULT);
if (!page_bitmap_read(index)) return err; if (!page_bitmap_read(index)) return err(EFAULT);
page_bitmap_set(index, false); page_bitmap_set(index, false);
used_mem -= ARCH_PAGE_SIZE; used_mem -= ARCH_PAGE_SIZE;
free_mem += ARCH_PAGE_SIZE; free_mem += ARCH_PAGE_SIZE;