luna, kernel: More constness
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-01-10 19:31:41 +01:00
parent 5aa667c776
commit 84c82a4e75
Signed by: apio
GPG Key ID: B8A7D06E42258954
13 changed files with 48 additions and 46 deletions

View File

@ -191,7 +191,7 @@ namespace MMU
auto& l4 = l4_entry(virt); auto& l4 = l4_entry(virt);
if (!l4.present) if (!l4.present)
{ {
u64 addr = TRY(MemoryManager::alloc_frame()); const u64 addr = TRY(MemoryManager::alloc_frame());
l4.present = true; l4.present = true;
l4.set_address(addr); l4.set_address(addr);
memset(l3_table(virt), 0, ARCH_PAGE_SIZE); memset(l3_table(virt), 0, ARCH_PAGE_SIZE);
@ -202,7 +202,7 @@ namespace MMU
auto& l3 = l3_entry(virt); auto& l3 = l3_entry(virt);
if (!l3.present) if (!l3.present)
{ {
u64 addr = TRY(MemoryManager::alloc_frame()); const u64 addr = TRY(MemoryManager::alloc_frame());
l3.present = true; l3.present = true;
l3.set_address(addr); l3.set_address(addr);
memset(l2_table(virt), 0, ARCH_PAGE_SIZE); memset(l2_table(virt), 0, ARCH_PAGE_SIZE);
@ -215,7 +215,7 @@ namespace MMU
auto& l2 = l2_entry(virt); auto& l2 = l2_entry(virt);
if (!l2.present) if (!l2.present)
{ {
u64 addr = TRY(MemoryManager::alloc_frame()); const u64 addr = TRY(MemoryManager::alloc_frame());
l2.present = true; l2.present = true;
l2.set_address(addr); l2.set_address(addr);
memset(l1_table(virt), 0, ARCH_PAGE_SIZE); memset(l1_table(virt), 0, ARCH_PAGE_SIZE);
@ -288,10 +288,10 @@ namespace MMU
Result<PageDirectory*> create_page_directory_for_userspace() Result<PageDirectory*> create_page_directory_for_userspace()
{ {
u64 directory_virt = TRY(MemoryManager::alloc_for_kernel(1, MMU::ReadWrite | MMU::NoExecute)); const u64 directory_virt = TRY(MemoryManager::alloc_for_kernel(1, MMU::ReadWrite | MMU::NoExecute));
u64 directory_phys = MMU::get_physical(directory_virt).value(); const u64 directory_phys = MMU::get_physical(directory_virt).value();
PageDirectory* directory = (PageDirectory*)directory_virt; PageDirectory* const directory = (PageDirectory*)directory_virt;
memset(directory, 0, ARCH_PAGE_SIZE); memset(directory, 0, ARCH_PAGE_SIZE);
PageTableEntry& recursive_entry = directory->entries[rindex]; PageTableEntry& recursive_entry = directory->entries[rindex];
recursive_entry.read_write = true; recursive_entry.read_write = true;
@ -320,7 +320,7 @@ namespace MMU
MemoryManager::free_frame((u64)directory); MemoryManager::free_frame((u64)directory);
}); });
PageDirectory* table = l4_table(); PageDirectory* const table = l4_table();
// Let's iterate over every top-level entry, skipping the last two entries (recursive mapping and kernel pages) // Let's iterate over every top-level entry, skipping the last two entries (recursive mapping and kernel pages)
for (u64 i = 0; i < 510; i++) for (u64 i = 0; i < 510; i++)
@ -328,7 +328,7 @@ namespace MMU
PageTableEntry& l4 = table->entries[i]; PageTableEntry& l4 = table->entries[i];
if (!l4.present) continue; if (!l4.present) continue;
PageDirectory* pdp = raw_l3_table(i); PageDirectory* const pdp = raw_l3_table(i);
for (u64 j = 0; j < 512; j++) for (u64 j = 0; j < 512; j++)
{ {
@ -340,7 +340,7 @@ namespace MMU
TRY(MemoryManager::free_frame(l3.get_address())); TRY(MemoryManager::free_frame(l3.get_address()));
} }
PageDirectory* pd = raw_l2_table(i, j); PageDirectory* const pd = raw_l2_table(i, j);
for (u64 k = 0; k < 512; k++) for (u64 k = 0; k < 512; k++)
{ {
@ -352,7 +352,7 @@ namespace MMU
TRY(MemoryManager::free_frame(l2.get_address())); TRY(MemoryManager::free_frame(l2.get_address()));
} }
PageDirectory* pt = raw_l1_table(i, j, k); PageDirectory* const pt = raw_l1_table(i, j, k);
for (u64 l = 0; l < 512; l++) for (u64 l = 0; l < 512; l++)
{ {

View File

@ -224,7 +224,7 @@ namespace MemoryManager
while (pages_mapped < count) while (pages_mapped < count)
{ {
u64 frame = TRY(alloc_frame()); const u64 frame = TRY(alloc_frame());
TRY(MMU::map(virt, frame, flags)); TRY(MMU::map(virt, frame, flags));
virt += ARCH_PAGE_SIZE; virt += ARCH_PAGE_SIZE;
pages_mapped++; pages_mapped++;
@ -237,7 +237,7 @@ namespace MemoryManager
Result<u64> alloc_for_kernel(usize count, int flags) Result<u64> alloc_for_kernel(usize count, int flags)
{ {
u64 start = TRY(KernelVM::alloc_several_pages(count)); const u64 start = TRY(KernelVM::alloc_several_pages(count));
usize pages_mapped = 0; usize pages_mapped = 0;
auto guard = make_scope_guard([=, &pages_mapped] { auto guard = make_scope_guard([=, &pages_mapped] {
@ -249,7 +249,7 @@ namespace MemoryManager
while (pages_mapped < count) while (pages_mapped < count)
{ {
u64 frame = TRY(alloc_frame()); const u64 frame = TRY(alloc_frame());
TRY(MMU::map(virt, frame, flags)); TRY(MMU::map(virt, frame, flags));
virt += ARCH_PAGE_SIZE; virt += ARCH_PAGE_SIZE;
pages_mapped++; pages_mapped++;
@ -262,7 +262,7 @@ namespace MemoryManager
Result<u64> get_kernel_mapping_for_frames(u64 phys, usize count, int flags) Result<u64> get_kernel_mapping_for_frames(u64 phys, usize count, int flags)
{ {
u64 start = TRY(KernelVM::alloc_several_pages(count)); const u64 start = TRY(KernelVM::alloc_several_pages(count));
usize pages_mapped = 0; usize pages_mapped = 0;
@ -292,7 +292,7 @@ namespace MemoryManager
while (count--) while (count--)
{ {
u64 frame = TRY(MMU::unmap(virt)); const u64 frame = TRY(MMU::unmap(virt));
TRY(free_frame(frame)); TRY(free_frame(frame));
virt += ARCH_PAGE_SIZE; virt += ARCH_PAGE_SIZE;
} }

View File

@ -43,7 +43,7 @@ Option<MemoryMapEntry> MemoryMapIterator::at(usize index) const
Option<MemoryMapEntry> MemoryMapIterator::next() Option<MemoryMapEntry> MemoryMapIterator::next()
{ {
auto entry = TRY(at(m_cur_ent++)); const auto entry = TRY(at(m_cur_ent++));
#ifdef ARCH_X86_64 #ifdef ARCH_X86_64
// Workaround for https://gitlab.com/qemu-project/qemu/-/commit/8504f129450b909c88e199ca44facd35d38ba4de // Workaround for https://gitlab.com/qemu-project/qemu/-/commit/8504f129450b909c88e199ca44facd35d38ba4de

View File

@ -53,11 +53,11 @@ namespace Scheduler
// If anything fails, make sure to clean up. // If anything fails, make sure to clean up.
auto guard = make_scope_guard([&] { delete thread; }); auto guard = make_scope_guard([&] { delete thread; });
u64 thread_stack_vm = TRY(MemoryManager::alloc_for_kernel(4, MMU::NoExecute | MMU::ReadWrite)); const u64 thread_stack_vm = TRY(MemoryManager::alloc_for_kernel(4, MMU::NoExecute | MMU::ReadWrite));
guard.deactivate(); guard.deactivate();
Stack thread_stack { thread_stack_vm, ARCH_PAGE_SIZE * 4 }; const Stack thread_stack { thread_stack_vm, ARCH_PAGE_SIZE * 4 };
thread->set_sp(thread_stack.top()); thread->set_sp(thread_stack.top());
thread->stack = thread_stack; thread->stack = thread_stack;
@ -73,7 +73,7 @@ namespace Scheduler
Result<void> new_kernel_thread(u64 address) Result<void> new_kernel_thread(u64 address)
{ {
Thread* thread = TRY(new_thread()); Thread* const thread = TRY(new_thread());
thread->init_regs_kernel(); thread->init_regs_kernel();
thread->set_ip(address); thread->set_ip(address);
@ -82,7 +82,7 @@ namespace Scheduler
Result<void> new_kernel_thread(void (*func)(void)) Result<void> new_kernel_thread(void (*func)(void))
{ {
Thread* thread = TRY(new_thread()); Thread* const thread = TRY(new_thread());
thread->init_regs_kernel(); thread->init_regs_kernel();
thread->set_ip((u64)func); thread->set_ip((u64)func);
@ -91,7 +91,7 @@ namespace Scheduler
Result<void> new_kernel_thread(void (*func)(void*), void* arg) Result<void> new_kernel_thread(void (*func)(void*), void* arg)
{ {
Thread* thread = TRY(new_thread()); Thread* const thread = TRY(new_thread());
thread->init_regs_kernel(); thread->init_regs_kernel();
thread->set_ip((u64)func); thread->set_ip((u64)func);
thread->set_arguments((u64)arg, 0, 0, 0); thread->set_arguments((u64)arg, 0, 0, 0);
@ -107,7 +107,7 @@ namespace Scheduler
auto guard = make_scope_guard([&] { MemoryManager::unmap_owned(THREAD_STACK_BASE, 4); }); auto guard = make_scope_guard([&] { MemoryManager::unmap_owned(THREAD_STACK_BASE, 4); });
u64 kernel_stack_base = TRY(MemoryManager::alloc_for_kernel(4, MMU::ReadWrite | MMU::NoExecute)); const u64 kernel_stack_base = TRY(MemoryManager::alloc_for_kernel(4, MMU::ReadWrite | MMU::NoExecute));
guard.deactivate(); guard.deactivate();
@ -119,13 +119,13 @@ namespace Scheduler
Result<void> new_userspace_thread(const TarStream::Entry& entry, const TarStream& stream) Result<void> new_userspace_thread(const TarStream::Entry& entry, const TarStream& stream)
{ {
Thread* thread = TRY(new_thread()); Thread* const thread = TRY(new_thread());
thread->is_kernel = false; thread->is_kernel = false;
auto guard = make_scope_guard([&] { delete thread; }); auto guard = make_scope_guard([&] { delete thread; });
auto directory = TRY(MMU::create_page_directory_for_userspace()); PageDirectory* const directory = TRY(MMU::create_page_directory_for_userspace());
auto directory_guard = make_scope_guard([&] { auto directory_guard = make_scope_guard([&] {
MMU::switch_page_directory(MMU::kernel_page_directory()); MMU::switch_page_directory(MMU::kernel_page_directory());
@ -136,7 +136,7 @@ namespace Scheduler
thread->init_regs_user(); thread->init_regs_user();
auto data = TRY(ELFLoader::load(entry, stream)); const ELFData data = TRY(ELFLoader::load(entry, stream));
thread->set_ip(data.entry); thread->set_ip(data.entry);

View File

@ -13,7 +13,7 @@ void Thread::init()
Result<Thread*> new_thread() Result<Thread*> new_thread()
{ {
Thread* thread = TRY(make<Thread>()); Thread* const thread = TRY(make<Thread>());
thread->id = g_next_id++; thread->id = g_next_id++;

View File

@ -125,7 +125,7 @@ namespace TextConsole
{ {
auto guard = make_scope_guard([] { utf8_decoder.reset(); }); auto guard = make_scope_guard([] { utf8_decoder.reset(); });
auto maybe_wchar = TRY(utf8_decoder.feed(c)); const Option<wchar_t> maybe_wchar = TRY(utf8_decoder.feed(c));
guard.deactivate(); guard.deactivate();
@ -194,7 +194,7 @@ namespace TextConsole
{ {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
auto rc = TRY(cstyle_format( const usize rc = TRY(cstyle_format(
format, [](char c, void*) -> Result<void> { return putchar(c); }, nullptr, ap)); format, [](char c, void*) -> Result<void> { return putchar(c); }, nullptr, ap));
va_end(ap); va_end(ap);
return rc; return rc;

View File

@ -96,10 +96,10 @@ template <typename T, class... Args> Result<SharedPtr<T>> make_shared(Args... ar
{ {
using RefCount = __detail::RefCount; using RefCount = __detail::RefCount;
RefCount* ref_count = TRY(make<RefCount>()); RefCount* const ref_count = TRY(make<RefCount>());
auto guard = make_scope_guard([&] { delete ref_count; }); auto guard = make_scope_guard([&] { delete ref_count; });
T* ptr = TRY(make<T>(args...)); T* const ptr = TRY(make<T>(args...));
guard.deactivate(); guard.deactivate();
return SharedPtr<T> { ptr, ref_count }; return SharedPtr<T> { ptr, ref_count };
@ -109,7 +109,7 @@ template <typename T> Result<SharedPtr<T>> adopt_shared(T* ptr)
{ {
using RefCount = __detail::RefCount; using RefCount = __detail::RefCount;
RefCount* ref_count = TRY(make<RefCount>()); RefCount* const ref_count = TRY(make<RefCount>());
return SharedPtr<T> { ptr, ref_count }; return SharedPtr<T> { ptr, ref_count };
} }
@ -126,9 +126,11 @@ template <typename T> Result<SharedPtr<T>> adopt_shared_from_owned(OwnedPtr<T>&&
T* ptr = other.m_ptr; T* ptr = other.m_ptr;
other.m_ptr = nullptr; other.m_ptr = nullptr;
// FIXME: Should the pointee magically vanish on failure? Or go back into the OwnedPtr, even though it's been
// moved...
auto guard = make_scope_guard([&] { delete ptr; }); auto guard = make_scope_guard([&] { delete ptr; });
SharedPtr<T> shared_ptr = TRY(adopt_shared(ptr)); const SharedPtr<T> shared_ptr = TRY(adopt_shared(ptr));
guard.deactivate(); guard.deactivate();

View File

@ -6,14 +6,14 @@ struct Stack
Stack() = default; Stack() = default;
Stack(u64 base, usize bytes); Stack(u64 base, usize bytes);
u64 bottom() u64 bottom() const
{ {
return m_base; return m_base;
} }
u64 top(); u64 top() const;
usize bytes() usize bytes() const
{ {
return m_bytes; return m_bytes;
} }

View File

@ -91,7 +91,7 @@ Option<usize> Bitmap::find(bool value, usize begin) const
{ {
expect(initialized(), "Bitmap was never initialized"); expect(initialized(), "Bitmap was never initialized");
usize size = this->size(); const usize size = this->size();
expect(begin < size, "Start index out of range"); expect(begin < size, "Start index out of range");
@ -104,7 +104,7 @@ Option<usize> Bitmap::find(bool value, usize begin) const
if (begin == size) return {}; if (begin == size) return {};
usize i = begin / 8; usize i = begin / 8;
u8 byte_that_does_not_contain_value = value_byte(!value); const u8 byte_that_does_not_contain_value = value_byte(!value);
while (i < m_size_in_bytes) while (i < m_size_in_bytes)
{ {
if (m_location[i] == byte_that_does_not_contain_value) if (m_location[i] == byte_that_does_not_contain_value)
@ -128,7 +128,7 @@ Option<usize> Bitmap::find(bool value, usize begin) const
Option<usize> Bitmap::find_and_toggle(bool value, usize begin) Option<usize> Bitmap::find_and_toggle(bool value, usize begin)
{ {
usize index = TRY(find(value, begin)); const usize index = TRY(find(value, begin));
set(index, !value); set(index, !value);
return index; return index;
} }

View File

@ -4,7 +4,7 @@ Stack::Stack(u64 base, usize bytes) : m_base(base), m_bytes(bytes)
{ {
} }
u64 Stack::top() u64 Stack::top() const
{ {
return (m_base + m_bytes) - sizeof(void*); return (m_base + m_bytes) - sizeof(void*);
} }

View File

@ -99,7 +99,7 @@ usize TarStream::read_contents(const Entry& entry, void* buf, usize offset, usiz
Result<OwnedStringView> TarStream::read_contents_as_string(const Entry& entry, usize offset, usize max) const Result<OwnedStringView> TarStream::read_contents_as_string(const Entry& entry, usize offset, usize max) const
{ {
char* buf = TRY(make_array<char>(max + 1)); char* const buf = TRY(make_array<char>(max + 1));
usize nread = read_contents(entry, buf, offset, max); usize nread = read_contents(entry, buf, offset, max);

View File

@ -20,7 +20,7 @@ usize to_dynamic_unit_cstr(usize value, char* buffer, usize max)
Result<OwnedStringView> to_dynamic_unit(usize value) Result<OwnedStringView> to_dynamic_unit(usize value)
{ {
char* buf = TRY(make_array<char>(64)); char* const buf = TRY(make_array<char>(64));
to_dynamic_unit_cstr(value, buf, 64); to_dynamic_unit_cstr(value, buf, 64);

View File

@ -32,7 +32,7 @@ static inline usize wide_char_length_as_utf8_unchecked(wchar_t c)
static Result<void> encode_wide_char_as_utf8(wchar_t c, char* result, usize& len) static Result<void> encode_wide_char_as_utf8(wchar_t c, char* result, usize& len)
{ {
usize utf8_len = TRY(wide_char_length_as_utf8(c)); const usize utf8_len = TRY(wide_char_length_as_utf8(c));
if (utf8_len > len) { return err(EILSEQ); } if (utf8_len > len) { return err(EILSEQ); }
@ -70,7 +70,7 @@ static Result<void> encode_wide_char_as_utf8(wchar_t c, char* result, usize& len
static Result<wchar_t> encode_utf8_as_wide_char_impl(const char* beg, usize& len) static Result<wchar_t> encode_utf8_as_wide_char_impl(const char* beg, usize& len)
{ {
usize utf8_len = TRY(utf8_char_length(*beg)); const usize utf8_len = TRY(utf8_char_length(*beg));
if (utf8_len > len) return err(EILSEQ); // Unterminated sequence if (utf8_len > len) return err(EILSEQ); // Unterminated sequence
len = utf8_len; // Enough space for the sequence, let's return the resulting length len = utf8_len; // Enough space for the sequence, let's return the resulting length
@ -115,7 +115,7 @@ static Result<wchar_t> encode_utf8_as_wide_char_impl(const char* beg, usize& len
static Result<wchar_t> encode_utf8_as_wide_char(const char* beg, usize& len) static Result<wchar_t> encode_utf8_as_wide_char(const char* beg, usize& len)
{ {
wchar_t result = TRY(encode_utf8_as_wide_char_impl(beg, len)); const wchar_t result = TRY(encode_utf8_as_wide_char_impl(beg, len));
// NOTE: We already know this is a valid code-point, since we constructed it ourselves and already checked the // NOTE: We already know this is a valid code-point, since we constructed it ourselves and already checked the
// range. // range.
if (len != wide_char_length_as_utf8_unchecked(result)) if (len != wide_char_length_as_utf8_unchecked(result))
@ -137,7 +137,7 @@ Result<usize> Utf8StringDecoder::code_points() const
while ((usize)(it - m_str) < m_byte_length) while ((usize)(it - m_str) < m_byte_length)
{ {
usize utf8_len = TRY(utf8_char_length(*it)); const usize utf8_len = TRY(utf8_char_length(*it));
if ((usize)(it - m_str) + utf8_len > m_byte_length) return err(EILSEQ); if ((usize)(it - m_str) + utf8_len > m_byte_length) return err(EILSEQ);
it += utf8_len; it += utf8_len;
len++; len++;
@ -234,7 +234,7 @@ Result<Option<wchar_t>> Utf8StateDecoder::feed(char c)
if (m_state_index == m_state_len - 1) if (m_state_index == m_state_len - 1)
{ {
usize len = m_state_len; usize len = m_state_len;
wchar_t wc = TRY(encode_utf8_as_wide_char(m_state, len)); const wchar_t wc = TRY(encode_utf8_as_wide_char(m_state, len));
m_state_len = 0; m_state_len = 0;
return Option<wchar_t> { wc }; return Option<wchar_t> { wc };
} }